1

I ported the MSDN capture stream example - https://msdn.microsoft.com/en-us/library/windows/desktop/dd370800

and modified it for loopback, exactly as seen here -

https://github.com/slavanap/WaveRec/blob/754b0cfdeec8d1edc59c60d179867ca6088bbfaa/wavetest.cpp

So I am requesting a duration of 1 second recording, and actual duration verifies that it is 1 second.

However I am stuck in an infinite loop in this packet reading here, packetLength is always a value of 448 (numFramesAvailable is also 448, I'm not sure why its never becoming 0 as the while loop is expecting.

https://github.com/slavanap/WaveRec/blob/754b0cfdeec8d1edc59c60d179867ca6088bbfaa/wavetest.cpp#L208-L232

Code is -

        while (packetLength != 0)
        {
            // Get the available data in the shared buffer.
            hr = pCaptureClient->GetBuffer(
                                   &pData,
                                   &numFramesAvailable,
                                   &flags, NULL, NULL);
            EXIT_ON_ERROR(hr)

            if (flags & AUDCLNT_BUFFERFLAGS_SILENT)
            {
                pData = NULL;  // Tell CopyData to write silence.
            }

            // Copy the available capture data to the audio sink.
            // hr = pMySink->CopyData(pData, numFramesAvailable, &bDone);
            // EXIT_ON_ERROR(hr)

            hr = pCaptureClient->ReleaseBuffer(numFramesAvailable);
            EXIT_ON_ERROR(hr)

            hr = pCaptureClient->GetNextPacketSize(&packetLength);
            EXIT_ON_ERROR(hr)
        }

My ported code is in ctypes and is here - https://github.com/Noitidart/ostypes_playground/blob/audio-capture/bootstrap.js#L71-L180

Simon Bosley
  • 1,114
  • 3
  • 18
  • 41
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    Is anything played at all in the system? Like an MP3 file in media player. Since you are doing loopback recording, I thought that it might be simply no data to forward to you and hence the loop. – Roman R. Jun 09 '16 at 07:26
  • Thansk @RomanR. nope nothing is playing, but i did adjust the volume to hear the *ting* sound you know from here: http://i.imgur.com/bqbG56a.png – Noitidart Jun 09 '16 at 10:26
  • @RomanR. I just tried with a youtube video playing, and now I get `packetLength: 896` and `numFramesAvailable: 896` but again infinite loop. :( – Noitidart Jun 09 '16 at 10:42
  • 1
    I don't understand. So you have the data if anything is played and loopback has data to get back to you. The loop is built by you, make it finite as you wish, e.g. on user request or once data stops come in. You describe behavior, which is normal, and it's up to you when to stop recording session. – Roman R. Jun 09 '16 at 11:37
  • Oh i just copied from msdn example on capture stream @RomanR. I thought the why for why does the MSDN example loop till packetLength is 0, is because it gets neutered? Does it expect us to neuter the byte array in our `CopyData` mthod? – Noitidart Jun 09 '16 at 14:14
  • MSDN code snippet expects that `bDone` will somehow get set externally and indicates that it's time to finalize the capture... – Roman R. Jun 09 '16 at 14:17
  • But @Roman `bDone` would not break out of the `packetLenght != 0` loop, it will only break out of the `(bDone == FALSE)` loop no? And my infinite loop is the packetLength one. – Noitidart Jun 09 '16 at 14:42

1 Answers1

1

I guess, there is hidden bug in Microsoft example that appears to happen in your case. Inner loop processing time of your code is much enough that new data appears in incoming audio buffer, thus leaving you spin in inner loop forever or until you can process incoming data at decent speed. Thus, I suggest to add && !bDone condition to inner loop.

while (bDone == FALSE)
{
    ...

    while (packetLength != 0 && bDone == FALSE)
    {
        ...
    }
}
  • Ah this makes sense! I accept it right away and will try to apply it. I will update you on how it goes. Thanks so much! – Noitidart Jul 21 '16 at 03:10
  • Hi there @slavanap may you please see my code here - http://stackoverflow.com/q/39951512/1828637 - I am trying to make a mp3 or wav buffer so i can write it to file or upload, I was having a tough time with it, may you please see if you can help me there. I'm not sure where to set `bDone` to false :( – Noitidart Oct 11 '16 at 15:53
  • 1
    @Noitidart `bDone` is set in `CopyData` function according to `Ctrl+C` handler (because otherwise recording will be infinite). Check out [the refactored version of WaveRec](https://github.com/slavanap/WaveRec). I've introduced more `C++`-like syntax. – Vyacheslav Napadovsky Oct 18 '16 at 12:04
  • Thanks very much @slavanap! – Noitidart Oct 18 '16 at 17:23