0

My main goal is to capture 2 audio streams and store them as a vector<BYTE> then come up with a congruence algorithm to check for equality. Right now I am only capturing one stream, however the values of the stream are 0 '/0'. Why am I getting null terminated values for all elements in my BYTE vector?

    void AudioDeviceOperator::TakeInput(AudioStreamModel* m)
{
  HRESULT hr;
  IAudioClient *iac = NULL;
  IAudioCaptureClient *pCaptureClient = NULL;
  WAVEFORMATEX *mixFormat;
  UINT32 bufferFrameCount;

  HRESULT de;
  de = AudioDeviceEnumerator -> GetDefaultAudioEndpoint(eCapture, eConsole, &SelectedAudioDeviceModel->AudioDevice);
  hr = SelectedAudioDeviceModel->AudioDevice -> Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&iac);


  REFERENCE_TIME bufferDuration = 0;  //default to min
  REFERENCE_TIME periodicity = 0;

  GUID trashGuid;
  HRESULT tg = CoCreateGuid(&trashGuid);
  LPCGUID AudioSessionGuid = &trashGuid;
  GUID guid2 = *AudioSessionGuid;
  HRESULT guidError = UuidCreate(&guid2); //project -> properties -> Linker -> Command Line -> Rpctr4.lib

  iac->GetMixFormat(&mixFormat);
  m->StreamFormat = *mixFormat;
  if (SUCCEEDED(guidError)) {
    cout << "/n" << "Initializing audio stream..";
    hr = iac->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_CROSSPROCESS, bufferDuration, periodicity, mixFormat, AudioSessionGuid);
    cout << hr;
    hr = iac->GetBufferSize(&bufferFrameCount);
    cout << hr;
    iac->GetService(IID_IAudioCaptureClient, (void**)&pCaptureClient);
    // Calculate the actual duration of the allocated buffer.
    double hnsActualDuration = (double)REFTIMES_PER_SEC * bufferFrameCount / mixFormat-> nSamplesPerSec;

    bool recordAudio = TRUE;
    BYTE *sData;
    UINT32 numFramesAvailable = 0;
    DWORD flags;
    UINT32 packetLength = 0;
    int numOfPackets = 0;
    iac->Start();
    while (recordAudio == TRUE)
    {
      hr = pCaptureClient->GetNextPacketSize(&packetLength);
      while (packetLength != 0) {
        hr = pCaptureClient->GetBuffer(&sData, &numFramesAvailable, &flags, NULL, NULL);
        if (sData != NULL) {
          m->Stream.push_back((*sData)); //here is where I write to the vector
          numOfPackets++;
        }
        if (numOfPackets == 100) {  // just getting 100 packets for testing
          recordAudio = FALSE;
          break;
        }
      }
      hr = pCaptureClient->ReleaseBuffer(numFramesAvailable);
    }
  }
  else
    cout << "AudioSessionGuidError";

  CoTaskMemFree(iac);
  AudioDeviceEnumerator->Release();
  //pCaptureClient->Release();  // releaseBuffer seeming to release capture client interface as weell.
};

When the audio session starts I make sure to make some noise. With the vector values the way they are I have nothing to compare. I'm also assuming that using that byte vector and rendering it with a IAudioRenderClient will result in nothing, however that is my next plan of action. Any ideas??

lonious
  • 676
  • 9
  • 25
  • First, are you sure the `GetNextPacketSize` and `GetBuffer` calls succeed? `sData` is not initialized to `nullptr` or `NULL` and that's the only value you are checking. Is there a guarantee that it's set if the call fails? Second, `m->Stream.push_back((*sData))` only pushes the first element of the buffer, not the entire buffer. – Captain Obvlious Nov 01 '14 at 19:04
  • Yes, both `GetNextPacketSize` and `GetBuffer` both succeed. Also, it was my understanding that I am reading the buffer packet by packet. I do this by writing the packet to `sData` each iteration. Here's what the docs say: "ppData [out] Pointer to a pointer variable into which the method writes the starting address of the next data packet that is available for the client to read." – lonious Nov 02 '14 at 01:25
  • 1
    Great, you're reading packets. You're still only storing the first byte of each packet read into the vector. You're also leaking memory when `numOfPackets` reaches 100 since you break out of the loop before `ReleaseBuffer` is called. – Captain Obvlious Nov 02 '14 at 01:29

0 Answers0