1

我正在使用“winmm.lib”c++ 库通过麦克风访问音频。

我有一个包含 8192 个元素的短整数数组,我想用音频流填充它们。

当我在正确的时间段内录制声音和睡眠时,它工作正常:

    const int lenBuffer = 8192; // 2**15
    short int SOUND_BUFFER[lenBuffer];

    WaveInHdr.lpData = (LPSTR)SOUND_BUFFER;  // set up buffer
    WaveInHdr.dwBufferLength = lenBuffer * 2;
    WaveInHdr.dwBytesRecorded = 0;
    WaveInHdr.dwUser = 0L; WaveInHdr.dwFlags = 0L; WaveInHdr.dwLoops = 0L;

    // Specify recording parameters

    result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
    waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
    result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));

    // sampling input
    result = waveInStart(hWaveIn);

    // Wait until finished recording
    cout << "recording...     ";
    Sleep(lenBuffer*1000/samplingRate);

    
    waveInClose(hWaveIn);

但是现在,我想在waveInStart()调用函数之后做一些事情,同时录音,就像这样:

    // sampling input
    result = waveInStart(hWaveIn);

    cout << "recording...     ";

    // do stuff here

    // Wait until recording done
    // e.g the buffer is filled
    while (is_still_recording()) Sleep(50)

    waveInClose(hWaveIn);

这个库中是否有一个函数可以知道声音缓冲区是否已被完全填满?

编辑:

我的帖子不够清楚:我正在寻找的是我的示例中“is_still_recording”的 winmm 函数。

我知道有一个,但我无法在任何地方找到它。

4

1 回答 1

-2

异步线程可以正常工作:

// sampling input
auto future = std::async(waveInStart, hWaveIn);  //future is your async thread

cout << "recording...     " << std::endl; //maybe move this inside waveInStart

// do stuff here

type result = future.get(); // the program will wait to execut this line if the async thread isn't yet finished with recording and hasn't yet returned your value. .get() gets the return value for you.
cout << "Recording Finished" << std::endl;

waveInClose(hWaveIn);
于 2020-06-24T22:52:11.283 回答