CMpcAudioRenderer::CMpcAudioRenderer(LPUNKNOWN punk, HRESULT *phr) : CBaseRenderer(__uuidof(this), NAME("MPC - Audio Renderer"), punk, phr) , m_pDSBuffer (NULL ) , m_pSoundTouch (NULL ) , m_pDS (NULL ) , m_dwDSWriteOff (0 ) , m_nDSBufSize (0 ) , m_dRate (1.0 ) , m_pReferenceClock (NULL ) , m_pWaveFileFormat (NULL ) , pAudioClient (NULL ) , pRenderClient (NULL ) , useWASAPI (true ) , bufferFrameCount (0 ) , hnsRequestedDuration (0 ) , hTask (NULL ) { if (useWASAPI) *phr = GetDefaultAudioDevice(&pAudioClient); else { m_pSoundTouch = new soundtouch::SoundTouch(); *phr = DirectSoundCreate8 (NULL, &m_pDS, NULL); } }
int main(int argc, char** argv) { if (argc < 2) { fprintf(stderr, "Missing args. Synthax: %s $period", argv[0]); return 1; } // initialize COM library HRESULT hr = CoInitialize(nullptr); IMMDevice* mmDevice = GetDefaultAudioDevice(); int period = atoi(argv[1]); int fileID = 0; for (;;) { std::string filename = "loopback_capture" + std::to_string(fileID) + ".wav"; fileID = ++fileID % 10; // no more than 10 sound files LoopbackCaptureFor(mmDevice, filename, period); printf("%s\n", filename.c_str()); fflush(stdout); // needs forced stdout flush, is part of a pipe } CoUninitialize(); return 0; }
HRESULT CMpcAudioRenderer::CheckAudioClient(WAVEFORMATEX *pWaveFormatEx) { HRESULT hr = S_OK; CAutoLock cAutoLock(&m_csCheck); TRACE(_T("CMpcAudioRenderer::CheckAudioClient")); if (pMMDevice == NULL) hr=GetDefaultAudioDevice(&pMMDevice); // If no WAVEFORMATEX structure provided and client already exists, return it if (pAudioClient != NULL && pWaveFormatEx == NULL) return hr; // Just create the audio client if no WAVEFORMATEX provided if (pAudioClient == NULL && pWaveFormatEx==NULL) { if (SUCCEEDED (hr)) hr=CreateAudioClient(pMMDevice, &pAudioClient); return hr; } // Compare the exisiting WAVEFORMATEX with the one provided WAVEFORMATEX *pNewWaveFormatEx = NULL; if (CheckFormatChanged(pWaveFormatEx, &pNewWaveFormatEx)) { // Format has changed, audio client has to be reinitialized TRACE(_T("CMpcAudioRenderer::CheckAudioClient Format changed, reinitialize the audio client")); if (m_pWaveFileFormat) { BYTE *p = (BYTE *)m_pWaveFileFormat; SAFE_DELETE_ARRAY(p); } m_pWaveFileFormat=pNewWaveFormatEx; hr = pAudioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, pWaveFormatEx, NULL); if (SUCCEEDED(hr)) { if (pAudioClient!=NULL && isAudioClientStarted) pAudioClient->Stop(); isAudioClientStarted=false; SAFE_RELEASE(pRenderClient); SAFE_RELEASE(pAudioClient); if (SUCCEEDED (hr)) hr=CreateAudioClient(pMMDevice, &pAudioClient); } else { TRACE(_T("CMpcAudioRenderer::CheckAudioClient New format not supported, accept it anyway")); return S_OK; } } else if (pRenderClient == NULL) { TRACE(_T("CMpcAudioRenderer::CheckAudioClient First initialization of the audio renderer")); } else return hr; SAFE_RELEASE(pRenderClient); if (SUCCEEDED (hr)) hr=InitAudioClient(pWaveFormatEx, pAudioClient, &pRenderClient); return hr; }