Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Captured audio buffers are all silent on Windows Phone 8

I'm trying to capture audio using WASAPI. My code is largely based on the ChatterBox VoIP sample app. I'm getting audio buffers, but they are all silent (flagged AUDCLNT_BUFFERFLAGS_SILENT).

I'm using Visual Studio Express 2012 for Windows Phone. Running on the emulator.

like image 585
oferei Avatar asked Jan 24 '26 15:01

oferei


2 Answers

I had the exact same problem and managed to reproduce it in the ChatterBox sample app if I set Visual Studio to native debugging and at any point stepped through the code.

Also, closing the App without going through the "Stop" procedure and stopping the AudioClient will require you to restart the emulator/device before being able to capture audio data again.

It nearly drove me nuts before I figured out the before mentioned problems but I finally got it working.

So.. 1. Be sure to NOT do native debugging 2. Always call IAudioClient->Stop(); before terminating the App. 3. Make sure you pass the correct parameters to IAudioClient->Initialize();

I've included a piece of code that works 100% of the time for me. I've left out error checking for clarity..

LPCWSTR pwstrDefaultCaptureDeviceId =
  GetDefaultAudioCaptureId(AudioDeviceRole::Communications);
HRESULT hr = ActivateAudioInterface(pwstrDefaultCaptureDeviceId,
  __uuidof(IAudioClient2), (void**)&m_pAudioClient);
hr = m_pAudioClient->GetMixFormat(&m_pwfx);
m_frameSizeInBytes = (m_pwfx->wBitsPerSample / 8) * m_pwfx->nChannels;
hr = m_pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
  AUDCLNT_STREAMFLAGS_NOPERSIST | AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 
  latency * 10000, 0, m_pwfx, NULL);
hr = m_pAudioClient->SetEventHandle(m_hCaptureEvent);
hr = m_pAudioClient->GetService(__uuidof(IAudioCaptureClient),
  (void**)&m_pCaptureClient);

And that's it.. Before calling this code I've started a worker thread that will listen to m_hCaptureEvent and call IAudioCaptureClient->GetBuffer(); whenever the capture event is triggered.

Of course using Microsoft.XNA.Audio.Microphone works fine to, but it's not always an option to reference the XNA framework.. :)

like image 136
BennyO Avatar answered Jan 26 '26 16:01

BennyO


It was a really annoying problem which waste about 2 complete days of mine.My problem was solved by setting AudioClientProperties.eCatagory to AudioCategory_Communications instead of AudioCategory_Other.

After this long try and error period I am not sure that the problem won't repeat in the future because the API doesn't act very stable and every run may return a different result.

Edit:Yeah my guess was true.Restarting the wp emulator makes the buffer silent again.But changing the AudioClientProperties.eCatagory back to AudioCategory_Other again solve it.I still don't know what is wrong with it and what is the final solution.

Again I encounter the same problem and this time commenting (removing) the properties.eCategory = AudioCategory_Communications; solve the problem.

like image 42
harsini Avatar answered Jan 26 '26 17:01

harsini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!