波形音频 - waveOutWrite 发出断断续续的声音

Waveform Audio - waveOutWrite gives choppy sound

我正在尝试使用 Waveform Audio library that would be playing AudioFrames (raw audio data, each frame consists of about 1920 bytes) provided by another program (right now I'm just simulating that by reading file as AudioFrames). Modifying code from this thread 创建一个 C++ 程序,我能够制作 SoundPlayer class 来完成这项工作,但我得到的输出非常不稳定。帧大小越大越好,但即使帧大到 96000 字节,音频仍然每秒左右出现故障(我需要的帧也比这小得多)。

我该如何解决这个问题?

Here 是我正在使用的测试文件。这是代码本身:

#include <windows.h>
#include <iostream>
#pragma comment(lib, "Winmm.lib")

constexpr int FRAME_SIZE_IN_BYTES = 1920;

struct AudioFrame
{
    char *Data;
    int DataSize;
};

class SoundPlayer
{
public:

    SoundPlayer()
    {
        // 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 = 16;               // Bits per sample per channel
        m_waveFormat.nSamplesPerSec = 48000;           // 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;
    }

    void Play(AudioFrame* af)
    {
        // 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)
        {
            std::cout << "Sound card cannot be opened." << std::endl;
            return;
        }

        // Create the wave header for our sound buffer
        m_waveHeader.lpData = af->Data;
        m_waveHeader.dwBufferLength = af->DataSize;
        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)
        {
            std::cout << "Error preparing Header!" << std::endl;
            return;
        }

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

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

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

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

        // Close the wav device
        if (waveOutClose(m_waveOut) != MMSYSERR_NOERROR)
        {
            std::cout << "Sound card cannot be closed!" << std::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

};

int main()
{
    FILE * fileDes;
    fopen_s(&fileDes, "Ducksauce.raw", "rb");
    if (fileDes == nullptr)
        std::cout << "File opening failed.\n";

    int bufferSize = FRAME_SIZE_IN_BYTES;
    char *buffer = new char[bufferSize];

    SoundPlayer sp;

    while (fread(buffer, sizeof(char), bufferSize, fileDes) > 0)
    {
        AudioFrame af;
        af.Data = buffer;
        af.DataSize = bufferSize;
        sp.Play(&af);
    }

    fclose(fileDes);
    delete[] buffer;
    return 0;
}

编辑:版本号 2。仍然没有按预期工作。

#include <windows.h>
#include <iostream>
#pragma comment(lib, "Winmm.lib")

constexpr int FRAME_SIZE_IN_BYTES = 1920;

struct AudioFrame
{
    char *Data;
    int DataSize;
};

class SoundPlayer
{
public:

    SoundPlayer()
    {
        // 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 = 16;               // Bits per sample per channel
        m_waveFormat.nSamplesPerSec = 48000;           // 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;

        // 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)
        {
            std::cout << "Sound card cannot be opened." << std::endl;
            return;
        }
    }

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

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

    void StartPlaying(AudioFrame* af)
    {
        // Create the wave header for our sound buffer
        m_waveHeader.lpData = af->Data;
        m_waveHeader.dwBufferLength = af->DataSize;
        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)
        {
            std::cout << "Error preparing Header!" << std::endl;
            return;
        }

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

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

    void WaitUntilFrameFinishes()
    {
        // Wait until sound finishes playing
        if (WaitForSingleObject(m_done, INFINITE) != WAIT_OBJECT_0)
        {
            std::cout << "Error waiting for sound to finish" << std::endl;
            return;
        }
        // Unprepare our wav header
        if (waveOutUnprepareHeader(m_waveOut, &m_waveHeader, sizeof(m_waveHeader)) != MMSYSERR_NOERROR)
        {
            std::cout << "Error unpreparing header!" << std::endl;
            return;
        }
    }

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

};

int main()
{
    FILE * fileDes;
    fopen_s(&fileDes, "Ducksauce.raw", "rb");
    if (fileDes == nullptr)
        std::cout << "File opening failed.\n";

    int bufferSize = FRAME_SIZE_IN_BYTES;
    char *buffer = new char[bufferSize];

    SoundPlayer sp;

    // Read first time
    fread(buffer, sizeof(char), bufferSize, fileDes);

    while (true)
    {
        AudioFrame af;
        af.Data = buffer;
        af.DataSize = bufferSize;
        // Start playing, but don't block
        sp.StartPlaying(&af);
        // Prepare the next chunk
        if (fread(buffer, sizeof(char), bufferSize, fileDes) <= 0)
            break;
        // Now block the code, waiting with next chunk already loaded
        // and ready to be played in the next iteration.
        sp.WaitUntilFrameFinishes();
    }

    fclose(fileDes);
    delete[] buffer;
    return 0;
}

编辑 2:如果我在 while:

之前添加这个就可以了
for (int i = 0; i < 3; i++ )
{
    fread(buffer, sizeof(char), bufferSize, fileDes);
    af.Data = buffer;
    af.DataSize = bufferSize;
    sp.StartPlaying(&af);
}

我也修改了一下:

while (true)
{
    // Prepare the next chunk
    if (fread(buffer, sizeof(char), bufferSize, fileDes) <= 0)
        break;
    // Now block the code, waiting with next chunk already loaded
    // and ready to be played in the next iteration.
    sp.WaitUntilFrameFinishes();

    af.Data = buffer;
    af.DataSize = bufferSize;
    sp.StartPlaying(&af);
}

您应该在播放声音时从磁盘读取数据,而不是在缓冲区之间!

如果您不能一次读取整个文件,您应该更改 Play 函数,使其不只是调用 WaitForSingleObject。使用它会使您的代码阻塞并等待声音停止播放。

你需要的是开始播放,然后回到你的阅读循环,准备下一个缓冲区,然后然后等待音乐结束,就像这样(在SoundPlayer):

void WaitUntilFrameFinishes() {
    // Wait until sound finishes playing
    if (WaitForSingleObject(m_done, INFINITE) != WAIT_OBJECT_0)

    // ... move all the code from Play till the end here
}

然后回到 main 循环:

// Read first frame
fread(buffer, sizeof(char), bufferSize, fileDes);

while (true)
{
    AudioFrame af;
    af.Data = buffer;
    af.DataSize = bufferSize;

    // Start playing, but don't block
    sp.Play(&af);

    // Prepare the next chunk
    if (fread(buffer, sizeof(char), bufferSize, fileDes) <= 0) {
        break;

    // Now block the code, waiting with next chunk already loaded
    // and ready to be played in the next iteration.
    sp.WaitUntilFrameFinishes();
}

理想情况下,您还将 fread 调用包装成可以以更好的方式提供块的东西。

经过一天尝试仅根据文档找出如何进行音频播放后,我发现 this excellent tutorial。如果有人在尝试使用 Waveform 音频创建音频播放时发现此线程,这是一个很好的参考点(肯定比我上面的错误代码好得多)。

关于我的代码,我怀疑它不能正常工作,因为应该使用 waveOutWrite() 保持 AudioFrames 队列始终至少有几个帧,以防止声卡必须等待的情况另一个 AudioFrame。