C ++中的简单声音

时间:2013-11-10 19:55:43

标签: c++ audio

在Visual Studio 2012中使用C ++ 11,我试图听到我在Pascal中观察到的声音。在Pascal中,您似乎能够向内置扬声器发送频率,该内置扬声器播放此频率,直到您告诉它停止(或直到​​您告诉它播放不同的频率)。所以这就是我需要的:

  • 我必须能够指定声音的频率
  • 声音必须很小或没有间隙(最多可达5ms)
  • 我不想使用外部声音库(请不要浪费我的时间建议它们,除非它们令人难以置信的轻量级并且提供非常广泛的使用范围)
  • 优选地,声音将在内置扬声器上播放,而不是通过计算机的常规扬声器播放

我在visual studio中找不到任何可包含的库/标题,它们能够向内部扬声器发送波形。我愿意尝试直接与内部发言人合作(我知道这很难,但我不是白痴 - 我想我可以通过一些指导来解决它),但我找不到任何文件在Windows中访问内部扬声器。

编辑:从this post,我得知大多数计算机现在实际上没有内置扬声器。游民。这很好 - 我可以使用连接的扬声器,但我仍然有以下要求:

  • 我需要能够指定一个频率并让扬声器播放该频率,直到我告诉他们停止
  • 我不想使用外部库

编辑2 :这是我正在上课的课程:

#define HALF_NOTE 1.059463094359 // HALF_NOTE ^ 12 = 2

#include <Windows.h>
#include <math.h>

class SoundEffect
{
public:
    SoundEffect(){}

    void Play()
    {
        for (int i = 0; data[i + 1] > 0; i++)
        {
            Beep(16 * pow(HALF_NOTE, data[i++] - 1), data[i] * 10); // (frequency of c0) * (twelfth root of 2) ^ (number of half steps above c0)

            // Ideally, the code would look more like this (pseudocode):
            // sound(16 * pow(HALF_NOTE, data[i++] - 1)); // Start playing the specified frequency
            // delay(data[i] * 10);
        }
        // nosound();
    }

    int& operator[] (int location) { return data[location]; }

private:
    int data[256];
};

2 个答案:

答案 0 :(得分:1)

我在不久前搜索了类似的东西(生成简单的声音),发现这些库可以完成这项工作:

但是,我没有时间尝试比较它们。玩得开心:))

答案 1 :(得分:1)

我最终使用Windows Multimedia API创建波形并将其发送到声音设备。我的解决方案基于教程here。这就是我最终的结果:

#define HALF_NOTE 1.059463094359 // HALF_NOTE ^ 12 = 2
#define PI 3.14159265358979

#include <Windows.h>
#include <math.h>
using namespace std;

class SoundEffect
{
public:
    SoundEffect()
    {
        m_data = NULL;
    }
    SoundEffect(const int noteInfo[], const int arraySize)
    {
        // Initialize the sound format we will request from sound card
        m_waveFormat.wFormatTag = WAVE_FORMAT_PCM;     // Uncompressed sound format
        m_waveFormat.nChannels = 1;                    // 1 = Mono, 2 = Stereo
        m_waveFormat.wBitsPerSample = 8;               // Bits per sample per channel
        m_waveFormat.nSamplesPerSec = 11025;           // Sample Per Second
        m_waveFormat.nBlockAlign = m_waveFormat.nChannels * m_waveFormat.wBitsPerSample / 8;
        m_waveFormat.nAvgBytesPerSec = m_waveFormat.nSamplesPerSec * m_waveFormat.nBlockAlign;
        m_waveFormat.cbSize = 0;

        int dataLength = 0, moment = (m_waveFormat.nSamplesPerSec / 75);
        double period = 2.0 * PI / (double) m_waveFormat.nSamplesPerSec;

        // Calculate how long we need the sound buffer to be
        for (int i = 1; i < arraySize; i += 2)
            dataLength += (noteInfo[i] != 0) ? noteInfo[i] * moment : moment;

        // Allocate the array
        m_data = new char[m_bufferSize = dataLength];

        int placeInData = 0;

        // Make the sound buffer
        for (int i = 0; i < arraySize; i += 2)
        {
            int relativePlaceInData = placeInData;

            while ((relativePlaceInData - placeInData) < ((noteInfo[i + 1] != 0) ? noteInfo[i + 1] * moment : moment))
            {
                // Generate the sound wave (as a sinusoid)
                // - x will have a range of -1 to +1
                double x = sin((relativePlaceInData - placeInData) * 55 * pow(HALF_NOTE, noteInfo[i]) * period);

                // Scale x to a range of 0-255 (signed char) for 8 bit sound reproduction
                m_data[relativePlaceInData] = (char) (127 * x + 128);

                relativePlaceInData++;
            }

            placeInData = relativePlaceInData;
        }
    }
    SoundEffect(SoundEffect& otherInstance)
    {
        m_bufferSize = otherInstance.m_bufferSize;
        m_waveFormat = otherInstance.m_waveFormat;

        if (m_bufferSize > 0)
        {
            m_data = new char[m_bufferSize];

            for (int i = 0; i < otherInstance.m_bufferSize; i++)
                m_data[i] = otherInstance.m_data[i];
        }
    }
    ~SoundEffect()
    {
        if (m_bufferSize > 0)
            delete [] m_data;
    }

    SoundEffect& operator=(SoundEffect& otherInstance)
    {
        if (m_bufferSize > 0)
            delete [] m_data;

        m_bufferSize = otherInstance.m_bufferSize;
        m_waveFormat = otherInstance.m_waveFormat;

        if (m_bufferSize > 0)
        {
            m_data = new char[m_bufferSize];

            for (int i = 0; i < otherInstance.m_bufferSize; i++)
                m_data[i] = otherInstance.m_data[i];
        }

        return *this;
    }

    void Play()
    {
        // Create our "Sound is Done" event
        m_done = CreateEvent (0, FALSE, FALSE, 0);

        // Open the audio device
        if (waveOutOpen(&m_waveOut, 0, &m_waveFormat, (DWORD) m_done, 0, CALLBACK_EVENT) != MMSYSERR_NOERROR) 
        {
            cout << "Sound card cannot be opened." << endl;
            return;
        }

        // Create the wave header for our sound buffer
        m_waveHeader.lpData = m_data;
        m_waveHeader.dwBufferLength = m_bufferSize;
        m_waveHeader.dwFlags = 0;
        m_waveHeader.dwLoops = 0;

        // Prepare the header for playback on sound card
        if (waveOutPrepareHeader(m_waveOut, &m_waveHeader, sizeof(m_waveHeader)) != MMSYSERR_NOERROR)
        {
            cout << "Error preparing Header!" << endl;
            return;
        }

        // Play the sound!
        ResetEvent(m_done); // Reset our Event so it is non-signaled, it will be signaled again with buffer finished

        if (waveOutWrite(m_waveOut, &m_waveHeader, sizeof(m_waveHeader)) != MMSYSERR_NOERROR)
        {
            cout << "Error writing to sound card!" << endl;
            return;
        }

        // Wait until sound finishes playing
        if (WaitForSingleObject(m_done, INFINITE) != WAIT_OBJECT_0)
        {
            cout << "Error waiting for sound to finish" << endl;
            return;
        }

        // Unprepare our wav header
        if (waveOutUnprepareHeader(m_waveOut, &m_waveHeader,sizeof(m_waveHeader)) != MMSYSERR_NOERROR)
        {
            cout << "Error unpreparing header!" << endl;
            return;
        }

        // Close the wav device
        if (waveOutClose(m_waveOut) != MMSYSERR_NOERROR)
        {
            cout << "Sound card cannot be closed!" << endl;
            return;
        }

        // Release our event handle
        CloseHandle(m_done);
    }

private:
    HWAVEOUT m_waveOut; // Handle to sound card output
    WAVEFORMATEX m_waveFormat; // The sound format
    WAVEHDR m_waveHeader; // WAVE header for our sound data
    HANDLE m_done; // Event Handle that tells us the sound has finished being played.
                   // This is a very efficient way to put the program to sleep
                   // while the sound card is processing the sound buffer

    char* m_data; // Sound data buffer
    int m_bufferSize; // Size of sound data buffer
};

相当复杂,但确实有效。我用这样的文本文件(DOS mario&amp; luigi的声音效果):

LifeMusic 56 8 61 8 65 8 61 8 63 8 68 8
GrowMusic 37 4 44 4 49 4 38 4 45 4 50 4 39 4 46 4 51 4
CoinMusic 66 1
PipeMusic 13 0 13 8 1 0 1 16 13 0 13 8 1 0 1 16 13 0 13 8 1 0 1 16
FireMusic 41 1 46 1
HitMusic 25 2 13 3 1 4 25 1 13 2 1 3
DeadMusic 25 3 13 4 1 6
NoteMusic 1 3 13 4 1 6
StarMusic 37 4 41 4 44 4 49 4 53 4 56 4 61 4 65 4 68 4 73 4

为了简要概述,我的主文件读入这些行。对于每一行,它从整数数组创建声音效果,并创建一个地图,其中键是声音效果的名称,值是创建的SoundEffect个实例。

在文本文件中,每一行应具有偶数个整数。如果你将一行整数分成两对,第一个数字将是A1之上的半步数(以确定频率),第二个数字将是持续时间,以75秒为单位(任意,I知道)。