1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | HRESULT InitRecord() {     HRESULT hr;
      hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pDeviceEnum);     if (FAILED(hr)) {         printf("Create device enumerator failed, hr: 0x%x", hr);         return hr;     }
      hr = pDeviceEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);     if (FAILED(hr)) {         printf("Get default audio device failed, hr: 0x%x", hr);         return hr;     }
      hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pAudioClient);     if (FAILED(hr)) {         printf("Create audio client failed, hr: 0x%x", hr);         return hr;     }
      hr = pAudioClient->GetMixFormat(&pWaveFormat);     if (FAILED(hr)) {         printf("Get mix format failed, hr: 0x%x", hr);         return hr;     }
      printf("Channel: %d, SamplesPerSec: %d, BitsPerSample: %d\n", pWaveFormat->nChannels, pWaveFormat->nSamplesPerSec, pWaveFormat->wBitsPerSample);
      hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, BUFFER_TIME_100NS, 0, pWaveFormat, NULL);     if (FAILED(hr)) {                           
                   pWaveFormat->nChannels = 2;         pWaveFormat->nBlockAlign = (2 * pWaveFormat->wBitsPerSample) / 8;         pWaveFormat->nAvgBytesPerSec = pWaveFormat->nSamplesPerSec * pWaveFormat->nBlockAlign;
          hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, BUFFER_TIME_100NS, 0, pWaveFormat, NULL);         if (FAILED(hr)) {             printf("Initialize audio client failed, hr: 0x%x", hr);             return hr;         }     }
      hr = pAudioClient->GetService(IID_IAudioCaptureClient, (void**)&pAudioCaptureClient);     if (FAILED(hr)) {         printf("Get audio capture client failed, hr: 0x%x", hr);         return hr;     }
      return S_OK; }
  |