如何知道MMsystem录音什么时候结束?
How to know when MMsystem audio-recording have finished?
我正在使用“winmm.lib”c++ 库通过麦克风访问音频。
我有一个包含 8192 个元素的 short int 数组,我想用音频流填充它。
当我录制声音并在适当的时间休眠时,它工作正常:
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);
这个库中是否有一个函数可以知道声音缓冲区是否已完全填满?
编辑:
我的 post 不够清楚:我正在寻找的是我示例中“is_still_recording”的 winmm 函数。
我知道有一个,但我到处都找不到。
异步线程可以正常工作:
// 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);
我正在使用“winmm.lib”c++ 库通过麦克风访问音频。
我有一个包含 8192 个元素的 short int 数组,我想用音频流填充它。
当我录制声音并在适当的时间休眠时,它工作正常:
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);
这个库中是否有一个函数可以知道声音缓冲区是否已完全填满?
编辑:
我的 post 不够清楚:我正在寻找的是我示例中“is_still_recording”的 winmm 函数。
我知道有一个,但我到处都找不到。
异步线程可以正常工作:
// 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);