【问题标题】:How to know when MMsystem audio-recording have finished?如何知道 MMsystem 录音何时结束?
【发布时间】:2020-10-15 06:12:59
【问题描述】:

我正在使用“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 函数。

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

【问题讨论】:

  • 鸡和蛋,直到用你的代码阻止它。
  • 好吧,我的声音缓冲区有一个精确的长度,我只想知道在给定的时间他是否被填满。我可以用chronos 做到这一点,但我认为有一个 winmm 函数可以做到这一点

标签: c++ audio-recording winmm


【解决方案1】:

异步线程可以正常工作:

// 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多