c++audio-recordingwinmm

How to know when MMsystem audio-recording have finished?


I’m using the "winmm.lib" c++ library to access audio via the microphone.

I have a short int array with 8192 elements that I want to fill with the audio stream.

It works fine when I record sound and sleep for the right period of time:

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

But now, I would like to do stuff after the waveInStart() function is called, while the sound is recording, like so:

    // 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 there a function from this library to know wheter or not sound buffer have been totally filled?

EDIT:

My post was not clear enough: what I’m looking for is the winmm function for "is_still_recording" in my example.

I know that there is one, but I was not able to find it anywhere.


Solution

  • An async thread would work fine:

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