我正在使用适用于 Windows 8.1 商店的 Windows 音频会话 (WASAPI) 示例的音频捕获部分,上次更新时间为 2015 年 3 月 24 日。我之所以写,是因为我可以看到一些看似错误或过于复杂的代码示例。我猜那是因为我不完全了解 WASAPI 接口和 Windows 运行时工作队列,所以我希望 Stack Overflow 社区能告诉我为什么这不是错误。
所有这些都在 WASAPICapture.cpp 中。现在,我将用最简单(也是最严肃的)示例来测试水域:使用指向已释放缓冲区的指针。
下面是一些伪代码,用于突出显示有问题的特定行。行号是相对于更完整的代码提取进一步向下。
PS - 我的互联网访问是零星的,而且我是一个新手 - 所以请耐心等待我的回复。
Line 1: hr = m_AudioCaptureClient->GetBuffer( &Data, &FramesAvailable, ...
... returns in Data a pointer to the audio buffer
Line 21: auto dataByte = ref new Platform::Array<BYTE, 1>( Data, cbBytesToCapture );
... copies from the buffer into a new Platform Array for the GUI's 'scope.
Line 24: m_AudioCaptureClient->ReleaseBuffer( FramesAvailable );
... releases the buffer
Line 27: ProcessScopeData( Data, cbBytesToCapture );
... uses the pointer after the underlying buffer has been released!?
...这是更完整的代码摘录:
hr = m_AudioCaptureClient->GetBuffer( &Data, &FramesAvailable, &dwCaptureFlags, &u64DevicePosition, &u64QPCPosition );
if (FAILED( hr ))
{
goto exit;
}
if (dwCaptureFlags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY)
{
// Pass down a discontinuity flag in case the app is interested and reset back to capturing
m_DeviceStateChanged->SetState( DeviceState::DeviceStateDiscontinuity, S_OK, true );
m_DeviceStateChanged->SetState( DeviceState::DeviceStateCapturing, S_OK, false );
}
// Zero out sample if silence
if ( (dwCaptureFlags & AUDCLNT_BUFFERFLAGS_SILENT) || IsSilence )
{
memset( Data, 0, FramesAvailable * m_MixFormat->nBlockAlign );
}
// Store data in array
auto dataByte = ref new Platform::Array<BYTE, 1>( Data, cbBytesToCapture );
// Release buffer back
m_AudioCaptureClient->ReleaseBuffer( FramesAvailable );
// Update plotter data
ProcessScopeData( Data, cbBytesToCapture );
// Write File and async store
m_WAVDataWriter->WriteBytes( dataByte );