本文整理汇总了C++中IAudioClient::Release方法的典型用法代码示例。如果您正苦于以下问题:C++ IAudioClient::Release方法的具体用法?C++ IAudioClient::Release怎么用?C++ IAudioClient::Release使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAudioClient
的用法示例。
在下文中一共展示了IAudioClient::Release方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: propagateWithRawCurrentFormat
void propagateWithRawCurrentFormat(WAVEFORMATEX *toThis) {
WAVEFORMATEX *pwfx;
IMMDevice *pMMDevice;
IAudioClient *pAudioClient;
HANDLE hTask;
DWORD nTaskIndex = 0;
hTask = AvSetMmThreadCharacteristics(L"Capture", &nTaskIndex);
HRESULT hr = get_default_device(&pMMDevice);
if (FAILED(hr)) {
assert(false);
}
// activate an (the default, for us, since we want loopback) IAudioClient
hr = pMMDevice->Activate(
__uuidof(IAudioClient),
CLSCTX_ALL, NULL,
(void**)&pAudioClient
);
if (FAILED(hr)) {
ShowOutput("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
assert(false);
}
hr = pAudioClient->GetMixFormat(&pwfx);
if (FAILED(hr)) {
ShowOutput("IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(pwfx);
pAudioClient->Release();
assert(false);
}
pAudioClient->Stop();
AvRevertMmThreadCharacteristics(hTask);
pAudioClient->Release();
pMMDevice->Release();
memcpy(toThis, pwfx, sizeof(WAVEFORMATEX));
CoTaskMemFree(pwfx);
}
示例2: EnumerateDevicesEx
//.........这里部分代码省略.........
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint properties failed.");
SAFE_RELEASE(pDevice);
goto failed;
}
hr = pProperty->GetValue(PKEY_Device_FriendlyName, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint device name failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strFriendlyName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_GUID, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint GUID failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strDevName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_FormFactor, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint form factor failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strWinDevType = winEndpoints[(EndpointFormFactor)varName.uiVal].winEndpointType;
AEDeviceType aeDeviceType = winEndpoints[(EndpointFormFactor)varName.uiVal].aeDeviceType;
PropVariantClear(&varName);
/* In shared mode Windows tells us what format the audio must be in. */
IAudioClient *pClient;
hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pClient);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Activate device failed (%s)", WASAPIErrToStr(hr));
goto failed;
}
//hr = pClient->GetMixFormat(&pwfxex);
hr = pProperty->GetValue(PKEY_AudioEngine_DeviceFormat, &varName);
if (SUCCEEDED(hr) && varName.blob.cbSize > 0)
{
WAVEFORMATEX* smpwfxex = (WAVEFORMATEX*)varName.blob.pBlobData;
deviceInfo.m_channels = layoutsByChCount[std::max(std::min(smpwfxex->nChannels, (WORD) DS_SPEAKER_COUNT), (WORD) 2)];
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_FLOAT));
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_AC3));
deviceInfo.m_sampleRates.push_back(std::min(smpwfxex->nSamplesPerSec, (DWORD) 192000));
}
else
{
CLog::Log(LOGERROR, __FUNCTION__": Getting DeviceFormat failed (%s)", WASAPIErrToStr(hr));
}
pClient->Release();
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
deviceInfo.m_deviceName = strDevName;
deviceInfo.m_displayName = strWinDevType.append(strFriendlyName);
deviceInfo.m_displayNameExtra = std::string("DirectSound: ").append(strFriendlyName);
deviceInfo.m_deviceType = aeDeviceType;
deviceInfoList.push_back(deviceInfo);
// add the default device with m_deviceName = default
if(strDD == strDevName)
{
deviceInfo.m_deviceName = std::string("default");
deviceInfo.m_displayName = std::string("default");
deviceInfo.m_displayNameExtra = std::string("");
deviceInfoList.push_back(deviceInfo);
}
}
return;
failed:
if (FAILED(hr))
CLog::Log(LOGERROR, __FUNCTION__": Failed to enumerate WASAPI endpoint devices (%s).", WASAPIErrToStr(hr));
SAFE_RELEASE(pEnumDevices);
SAFE_RELEASE(pEnumerator);
}
示例3: EnumerateDevicesEx
//.........这里部分代码省略.........
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint device name failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strFriendlyName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_GUID, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint GUID failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strDevName = localWideToUtf(varName.pwszVal);
PropVariantClear(&varName);
hr = pProperty->GetValue(PKEY_AudioEndpoint_FormFactor, &varName);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Retrieval of DirectSound endpoint form factor failed.");
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
goto failed;
}
std::string strWinDevType = winEndpoints[(EndpointFormFactor)varName.uiVal].winEndpointType;
AEDeviceType aeDeviceType = winEndpoints[(EndpointFormFactor)varName.uiVal].aeDeviceType;
PropVariantClear(&varName);
/* In shared mode Windows tells us what format the audio must be in. */
IAudioClient *pClient;
hr = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pClient);
if (FAILED(hr))
{
CLog::Log(LOGERROR, __FUNCTION__": Activate device failed (%s)", WASAPIErrToStr(hr));
goto failed;
}
//hr = pClient->GetMixFormat(&pwfxex);
hr = pProperty->GetValue(PKEY_AudioEngine_DeviceFormat, &varName);
if (SUCCEEDED(hr) && varName.blob.cbSize > 0)
{
WAVEFORMATEX* smpwfxex = (WAVEFORMATEX*)varName.blob.pBlobData;
deviceInfo.m_channels = layoutsByChCount[std::max(std::min(smpwfxex->nChannels, (WORD) DS_SPEAKER_COUNT), (WORD) 2)];
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_FLOAT));
deviceInfo.m_dataFormats.push_back(AEDataFormat(AE_FMT_AC3));
deviceInfo.m_sampleRates.push_back(std::min(smpwfxex->nSamplesPerSec, (DWORD) 192000));
}
else
{
CLog::Log(LOGERROR, __FUNCTION__": Getting DeviceFormat failed (%s)", WASAPIErrToStr(hr));
}
pClient->Release();
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
deviceInfo.m_deviceName = strDevName;
deviceInfo.m_displayName = strWinDevType.append(strFriendlyName);
deviceInfo.m_displayNameExtra = std::string("DirectSound: ").append(strFriendlyName);
deviceInfo.m_deviceType = aeDeviceType;
deviceInfoList.push_back(deviceInfo);
}
// since AE takes the first device in deviceInfoList as default audio device we need
// to sort it in order to use the real default device
if(deviceInfoList.size() > 1)
{
std::string strDD = GetDefaultDevice();
for (AEDeviceInfoList::iterator itt = deviceInfoList.begin(); itt != deviceInfoList.end(); ++itt)
{
CAEDeviceInfo devInfo = *itt;
if(devInfo.m_deviceName == strDD)
{
deviceInfoList.erase(itt);
deviceInfoList.insert(deviceInfoList.begin(), devInfo);
break;
}
}
}
return;
failed:
if (FAILED(hr))
CLog::Log(LOGERROR, __FUNCTION__": Failed to enumerate WASAPI endpoint devices (%s).", WASAPIErrToStr(hr));
SAFE_RELEASE(pEnumDevices);
SAFE_RELEASE(pEnumerator);
}
示例4: EnumerateDevicesEx
//.........这里部分代码省略.........
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.nSamplesPerSec = 48000;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
bool hasLpcm = false;
// Try with KSAUDIO_SPEAKER_DIRECTOUT
for (unsigned int k = WASAPI_SPEAKER_COUNT; k > 0; k--)
{
wfxex.dwChannelMask = KSAUDIO_SPEAKER_DIRECTOUT;
wfxex.Format.nChannels = k;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if (k > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
break;
}
}
/* Try with reported channel mask */
for (unsigned int k = WASAPI_SPEAKER_COUNT; k > 0; k--)
{
wfxex.dwChannelMask = uiChannelMask;
wfxex.Format.nChannels = k;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if ( !hasLpcm && k > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
break;
}
}
/* Try with specific speakers configurations */
for (unsigned int i = 0; i < ARRAYSIZE(layoutsList); i++)
{
unsigned int nmbOfCh;
wfxex.dwChannelMask = ChLayoutToChMask(layoutsList[i], &nmbOfCh);
wfxex.Format.nChannels = nmbOfCh;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
if ( deviceChannels.Count() < nmbOfCh)
deviceChannels = layoutsList[i];
if ( !hasLpcm && nmbOfCh > 3) // Add only multichannel LPCM
{
deviceInfo.m_dataFormats.push_back(AE_FMT_LPCM);
hasLpcm = true;
}
}
}
pClient->Release();
}
else
{
CLog::Log(LOGDEBUG, __FUNCTION__": Failed to activate device for passthrough capability testing.");
}
deviceInfo.m_deviceName = strDevName;
deviceInfo.m_displayName = strWinDevType.append(strFriendlyName);
deviceInfo.m_displayNameExtra = std::string("WASAPI: ").append(strFriendlyName);
deviceInfo.m_deviceType = aeDeviceType;
deviceInfo.m_channels = deviceChannels;
/* Store the device info */
deviceInfoList.push_back(deviceInfo);
if(pDevice->GetId(&pwszID) == S_OK)
{
if(wstrDDID.compare(pwszID) == 0)
{
deviceInfo.m_deviceName = std::string("default");
deviceInfo.m_displayName = std::string("default");
deviceInfo.m_displayNameExtra = std::string("");
deviceInfoList.push_back(deviceInfo);
}
CoTaskMemFree(pwszID);
}
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pProperty);
}
示例5: LoopbackCaptureFor
void LoopbackCaptureFor(IMMDevice* mmDevice, std::string filename, int secs)
{
// open new file
MMIOINFO mi = { 0 };
// some flags cause mmioOpen write to this buffer
// but not any that we're using
std::wstring wsFilename(filename.begin(), filename.end()); // mmioOpen wants a wstring
HMMIO file = mmioOpen(const_cast<LPWSTR>(wsFilename.c_str()), &mi, MMIO_WRITE | MMIO_CREATE);
time_t startTime = time(nullptr);
// activate an IAudioClient
IAudioClient* audioClient;
HRESULT hr = mmDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&audioClient);
if (FAILED(hr))
{
fprintf(stderr, "IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
return;
}
// get the default device periodicity
REFERENCE_TIME hnsDefaultDevicePeriod;
hr = audioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, nullptr);
if (FAILED(hr))
{
fprintf(stderr, "IAudioClient::GetDevicePeriod failed: hr = 0x%08x\n", hr);
audioClient->Release();
return;
}
// get the default device format
WAVEFORMATEX* waveform;
hr = audioClient->GetMixFormat(&waveform);
if (FAILED(hr))
{
fprintf(stderr, "IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(waveform);
audioClient->Release();
return;
}
// coerce int-16 wave format
// can do this in-place since we're not changing the size of the format
// also, the engine will auto-convert from float to int for us
switch (waveform->wFormatTag)
{
case WAVE_FORMAT_IEEE_FLOAT:
waveform->wFormatTag = WAVE_FORMAT_PCM;
waveform->wBitsPerSample = BITS_PER_SAMPLE;
waveform->nBlockAlign = BLOCK_ALIGN;
waveform->nAvgBytesPerSec = BYTE_RATE;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(waveform);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat))
{
pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
pEx->Samples.wValidBitsPerSample = BITS_PER_SAMPLE;
waveform->wBitsPerSample = BITS_PER_SAMPLE;
waveform->nBlockAlign = waveform->nChannels * BYTE_PER_SAMPLE;
waveform->nAvgBytesPerSec = waveform->nBlockAlign * waveform->nSamplesPerSec;
}
break;
}
}
MMCKINFO ckRIFF = { 0 };
MMCKINFO ckData = { 0 };
hr = WriteWaveHeader(file, waveform, &ckRIFF, &ckData);
// create a periodic waitable timer
HANDLE hWakeUp = CreateWaitableTimer(nullptr, FALSE, nullptr);
UINT32 nBlockAlign = waveform->nBlockAlign;
// call IAudioClient::Initialize
// note that AUDCLNT_STREAMFLAGS_LOOPBACK and AUDCLNT_STREAMFLAGS_EVENTCALLBACK
// do not work together...
// the "data ready" event never gets set
// so we're going to do a timer-driven loop
hr = audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, waveform, 0);
if (FAILED(hr))
{
fprintf(stderr, "IAudioClient::Initialize failed: hr = 0x%08x\n", hr);
CloseHandle(hWakeUp);
audioClient->Release();
return;
}
// free up waveform
CoTaskMemFree(waveform);
// activate an IAudioCaptureClient
IAudioCaptureClient* audioCaptureClient;
hr = audioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&audioCaptureClient);
// register with MMCSS
DWORD nTaskIndex = 0;
//.........这里部分代码省略.........
示例6: Process
//HRESULT LoopbackCapture(
// IMMDevice *pMMDevice,
// bool bInt16,
// HANDLE hStartedEvent,
// HANDLE hStopEvent,
// PUINT32 pnFrames,
// HMMIO hFile,
// AudioBuffer *pBuffer
//)
HRESULT LoopbackCapture::Process()
{
HRESULT hr;
// activate an IAudioClient
IAudioClient *pAudioClient;
hr = pMMDevice->Activate(
__uuidof(IAudioClient),
CLSCTX_ALL, NULL,
(void**)&pAudioClient
);
if (FAILED(hr)) {
printf("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
return hr;
}
// get the default device periodicity
REFERENCE_TIME hnsDefaultDevicePeriod;
hr = pAudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, NULL);
if (FAILED(hr)) {
printf("IAudioClient::GetDevicePeriod failed: hr = 0x%08x\n", hr);
pAudioClient->Release();
return hr;
}
// get the default device format
WAVEFORMATEX *pwfx;
hr = pAudioClient->GetMixFormat(&pwfx);
if (FAILED(hr)) {
printf("IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return hr;
}
if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
{
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
//pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
printf("WAVE_FORMAT_EXTENSIBLE\n");
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat))
{
printf("float\n");
}//
else if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_PCM, pEx->SubFormat))
{
printf("PCM\n");
}//KSDATAFORMAT_SUBTYPE_WAVEFORMATEX
else if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_WAVEFORMATEX, pEx->SubFormat))
{
printf("WAVEFORMATEX\n");
}
}
if (bInt16) {
// coerce int-16 wave format
// can do this in-place since we're not changing the size of the format
// also, the engine will auto-convert from float to int for us
switch (pwfx->wFormatTag) {
case WAVE_FORMAT_IEEE_FLOAT:
pwfx->wFormatTag = WAVE_FORMAT_PCM;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat)) {
pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
pEx->Samples.wValidBitsPerSample = 16;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
} else {
printf("Don't know how to coerce mix format to int-16\n");
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
}
break;
default:
printf("Don't know how to coerce WAVEFORMATEX with wFormatTag = 0x%08x to int-16\n", pwfx->wFormatTag);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
//.........这里部分代码省略.........
示例7: main
int main(int argc, char *argv[])
{
CoInitialize(nullptr);
listDevices();
IAudioClient *pAudioClient;
IMMDevice *device;
getDefaultDevice(&device);
HRESULT hr = device->Activate(__uuidof(IAudioClient),
CLSCTX_ALL, nullptr, (void**)&pAudioClient);
if (FAILED(hr)) {
printf("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
return hr;
}
REFERENCE_TIME hnsDefaultDevicePeriod;
hr = pAudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, nullptr);
if (FAILED(hr)) {
printf("IAudioClient::GetDevicePeriod failed: hr = 0x%08x\n", hr);
pAudioClient->Release();
return hr;
}
// get the default device format
WAVEFORMATEX *pwfx;
hr = pAudioClient->GetMixFormat(&pwfx);
if (FAILED(hr)) {
printf("IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return hr;
}
DVAR(pwfx->wFormatTag);
DVAR(pwfx->wBitsPerSample);
DVAR(pwfx->nBlockAlign);
DVAR(pwfx->nAvgBytesPerSec);
switch (pwfx->wFormatTag) {
case WAVE_FORMAT_IEEE_FLOAT:
pwfx->wFormatTag = WAVE_FORMAT_PCM;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat)) {
pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
pEx->Samples.wValidBitsPerSample = 16;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
} else {
printf("Don't know how to coerce mix format to int-16\n");
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
}
break;
default:
printf("Don't know how to coerce WAVEFORMATEX with wFormatTag = 0x%08x to int-16\n", pwfx->wFormatTag);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
DVAR(pwfx->wFormatTag);
DVAR(pwfx->wBitsPerSample);
DVAR(pwfx->nBlockAlign);
DVAR(pwfx->nAvgBytesPerSec);
hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, pwfx, 0 );
if (FAILED(hr)) {
printf("IAudioClient::Initialize failed: hr = 0x%08x\n", hr);
pAudioClient->Release();
return hr;
}
IAudioCaptureClient *pAudioCaptureClient;
hr = pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pAudioCaptureClient);
if (FAILED(hr)) {
printf("IAudioClient::GetService(IAudioCaptureClient) failed: hr 0x%08x\n", hr);
pAudioClient->Release();
return hr;
}
hr = pAudioClient->Start();
if (FAILED(hr)) {
printf("IAudioClient::Start failed: hr = 0x%08x\n", hr);
pAudioCaptureClient->Release();
//.........这里部分代码省略.........
示例8: _tmain
int _tmain(int argc, _TCHAR* argv[]) {
IMMDeviceEnumerator *enumerator = 0;
IMMDevice *device = 0;
FILE *f;
f=fopen("c:/1.wav","w");
CoInitialize(0);
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
(void**) &enumerator);
enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
HANDLE processOutWrite, processOutRead, processInWrite, processInRead;
/*wchar_t processCommand[2000];
{
FILE* commandFile;
fopen_s(&commandFile, "command.txt", "r");
char cmd[2000];
fread(cmd, sizeof(char), 2000, commandFile);
fclose(commandFile);
size_t count;
mbstowcs_s(&count, processCommand, cmd, 2000);
}*/
/*{
//create pipes for plink process
SECURITY_ATTRIBUTES pipeAttributes = {0};
pipeAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
pipeAttributes.bInheritHandle = TRUE;
pipeAttributes.lpSecurityDescriptor= NULL;
CreatePipe(&processOutRead, &processOutWrite, &pipeAttributes, 0);
CreatePipe(&processInRead, &processInWrite, &pipeAttributes, 0);
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(STARTUPINFO));
startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.hStdError = processOutWrite;
startupInfo.hStdOutput = processOutWrite;
startupInfo.hStdInput = processInRead;
startupInfo.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION processInfo = {0};
//launch process
CreateProcess(NULL, processCommand, NULL, NULL, TRUE, 0, NULL, NULL, &startupInfo, &processInfo);
//wait for plink to connect to minimze sound delay (magic number)
Sleep(2500);
}*/
HRESULT hr;
// activate an IAudioClient
IAudioClient *audioClient;
hr = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**) &audioClient);
if (FAILED(hr)) {
printf("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
return hr;
}
// get the default device format
WAVEFORMATEX *waveFormat;
hr = audioClient->GetMixFormat(&waveFormat);
if (FAILED(hr)) {
printf("IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(waveFormat);
audioClient->Release();
return hr;
}
// coerce int-16 wave format
// can do this in-place since we're not changing the size of the format
// also, the engine will auto-convert from float to int for us
switch (waveFormat->wFormatTag) {
case WAVE_FORMAT_IEEE_FLOAT:
waveFormat->wFormatTag = WAVE_FORMAT_PCM;
waveFormat->wBitsPerSample = 16;
waveFormat->nBlockAlign = waveFormat->nChannels * waveFormat->wBitsPerSample / 8;
waveFormat->nAvgBytesPerSec = waveFormat->nBlockAlign * waveFormat->nSamplesPerSec;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE waveFormatEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(waveFormat);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, waveFormatEx->SubFormat)) {
waveFormatEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
waveFormatEx->Samples.wValidBitsPerSample = 16;
waveFormat->wBitsPerSample = 16;
waveFormat->nBlockAlign = waveFormat->nChannels * waveFormat->wBitsPerSample / 8;
waveFormat->nAvgBytesPerSec = waveFormat->nBlockAlign * waveFormat->nSamplesPerSec;
} else {
printf("Don't know how to coerce mix format to int-16\n");
CoTaskMemFree(waveFormat);
audioClient->Release();
return E_UNEXPECTED;
}
}
break;
default:
printf("Don't know how to coerce WAVEFORMATEX with wFormatTag = 0x%08x to int-16\n", waveFormat->wFormatTag);
CoTaskMemFree(waveFormat);
audioClient->Release();
//.........这里部分代码省略.........
示例9: LoopbackCapture
HRESULT LoopbackCapture(
IMMDevice *pMMDevice,
bool bInt16,
HANDLE hStartedEvent,
HANDLE hStopEvent,
PUINT32 pnFrames,
bool bMono,
INT32 iSampleRateDivisor
) {
HRESULT hr;
SimpleTcpServer server;
// Wait for client connection before attempting any audio capture
server.setup();
server.waitForClient();
// activate an IAudioClient
IAudioClient *pAudioClient;
hr = pMMDevice->Activate(
__uuidof(IAudioClient),
CLSCTX_ALL, NULL,
(void**)&pAudioClient
);
if (FAILED(hr)) {
printf("IMMDevice::Activate(IAudioClient) failed: hr = 0x%08x", hr);
return hr;
}
// get the default device periodicity
REFERENCE_TIME hnsDefaultDevicePeriod;
hr = pAudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, NULL);
if (FAILED(hr)) {
printf("IAudioClient::GetDevicePeriod failed: hr = 0x%08x\n", hr);
pAudioClient->Release();
return hr;
}
// get the default device format
WAVEFORMATEX *pwfx;
hr = pAudioClient->GetMixFormat(&pwfx);
if (FAILED(hr)) {
printf("IAudioClient::GetMixFormat failed: hr = 0x%08x\n", hr);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return hr;
}
if (bInt16) {
// coerce int-16 wave format
// can do this in-place since we're not changing the size of the format
// also, the engine will auto-convert from float to int for us
switch (pwfx->wFormatTag) {
case WAVE_FORMAT_IEEE_FLOAT:
pwfx->wFormatTag = WAVE_FORMAT_PCM;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
break;
case WAVE_FORMAT_EXTENSIBLE:
{
// naked scope for case-local variable
PWAVEFORMATEXTENSIBLE pEx = reinterpret_cast<PWAVEFORMATEXTENSIBLE>(pwfx);
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, pEx->SubFormat)) {
pEx->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
pEx->Samples.wValidBitsPerSample = 16;
pwfx->wBitsPerSample = 16;
pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample / 8;
pwfx->nAvgBytesPerSec = pwfx->nBlockAlign * pwfx->nSamplesPerSec;
} else {
printf("Don't know how to coerce mix format to int-16\n");
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
}
break;
default:
printf("Don't know how to coerce WAVEFORMATEX with wFormatTag = 0x%08x to int-16\n", pwfx->wFormatTag);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return E_UNEXPECTED;
}
}
// create a periodic waitable timer
HANDLE hWakeUp = CreateWaitableTimer(NULL, FALSE, NULL);
if (NULL == hWakeUp) {
DWORD dwErr = GetLastError();
printf("CreateWaitableTimer failed: last error = %u\n", dwErr);
CoTaskMemFree(pwfx);
pAudioClient->Release();
return HRESULT_FROM_WIN32(dwErr);
}
UINT32 nBlockAlign = pwfx->nBlockAlign;
UINT32 nBufferSize;
*pnFrames = 0;
//.........这里部分代码省略.........