1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2012 |
---|
3 | // Created by Roman Ryltsov roman@alax.info |
---|
4 | // |
---|
5 | // $Id: SetAudioClientVolume.cpp 145 2012-11-18 17:22:36Z roman $ |
---|
6 | |
---|
7 | #include "stdafx.h" |
---|
8 | |
---|
9 | #define _USE_MATH_DEFINES |
---|
10 | #include <math.h> |
---|
11 | #include <mmdeviceapi.h> |
---|
12 | #include <audioclient.h> |
---|
13 | |
---|
14 | #define _A ATLASSERT |
---|
15 | #define __C ATLENSURE_SUCCEEDED |
---|
16 | #define __D ATLENSURE_THROW |
---|
17 | |
---|
18 | int _tmain(int argc, _TCHAR* argv[]) |
---|
19 | { |
---|
20 | __C(CoInitialize(NULL)); |
---|
21 | CComPtr<IMMDeviceEnumerator> pMmDeviceEnumerator; |
---|
22 | __C(pMmDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator))); |
---|
23 | CComPtr<IMMDevice> pMmDevice; |
---|
24 | __C(pMmDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pMmDevice)); |
---|
25 | CComPtr<IAudioClient> pAudioClient; |
---|
26 | __C(pMmDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (VOID**) &pAudioClient)); |
---|
27 | CComHeapPtr<WAVEFORMATEX> pWaveFormatEx; |
---|
28 | __C(pAudioClient->GetMixFormat(&pWaveFormatEx)); |
---|
29 | static const REFERENCE_TIME g_nBufferTime = 60 * 1000 * 10000i64; // 1 minute |
---|
30 | __C(pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, g_nBufferTime, 0, pWaveFormatEx, NULL)); |
---|
31 | #pragma region Data |
---|
32 | CComPtr<IAudioRenderClient> pAudioRenderClient; |
---|
33 | __C(pAudioClient->GetService(__uuidof(IAudioRenderClient), (VOID**) &pAudioRenderClient)); |
---|
34 | UINT32 nSampleCount = (UINT32) (g_nBufferTime / (1000 * 10000i64) * pWaveFormatEx->nSamplesPerSec) / 2; |
---|
35 | _A(pWaveFormatEx->wFormatTag == WAVE_FORMAT_EXTENSIBLE); |
---|
36 | const WAVEFORMATEXTENSIBLE* pWaveFormatExtensible = (const WAVEFORMATEXTENSIBLE*) (const WAVEFORMATEX*) pWaveFormatEx; |
---|
37 | _A(pWaveFormatExtensible->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT); |
---|
38 | // ASSU: Mixing format is IEEE Float PCM |
---|
39 | BYTE* pnData = NULL; |
---|
40 | __C(pAudioRenderClient->GetBuffer(nSampleCount, &pnData)); |
---|
41 | FLOAT* pfFloatData = (FLOAT*) pnData; |
---|
42 | for(UINT32 nSampleIndex = 0; nSampleIndex < nSampleCount; nSampleIndex++) |
---|
43 | for(WORD nChannelIndex = 0; nChannelIndex < pWaveFormatEx->nChannels; nChannelIndex++) |
---|
44 | pfFloatData[nSampleIndex * pWaveFormatEx->nChannels + nChannelIndex] = sin(1000.0f * nSampleIndex / pWaveFormatEx->nSamplesPerSec * 2 * M_PI); |
---|
45 | __C(pAudioRenderClient->ReleaseBuffer(nSampleCount, 0)); |
---|
46 | #pragma endregion |
---|
47 | CComPtr<ISimpleAudioVolume> pSimpleAudioVolume; |
---|
48 | __C(pAudioClient->GetService(__uuidof(ISimpleAudioVolume), (VOID**) &pSimpleAudioVolume)); |
---|
49 | __C(pSimpleAudioVolume->SetMasterVolume(0.50f, NULL)); |
---|
50 | _tprintf(_T("Playing Loud\n")); |
---|
51 | __C(pAudioClient->Start()); |
---|
52 | Sleep(5 * 1000); |
---|
53 | _tprintf(_T("Playing Quiet\n")); |
---|
54 | __C(pSimpleAudioVolume->SetMasterVolume(0.10f, NULL)); |
---|
55 | Sleep(15 * 1000); |
---|
56 | // NOTE: We don't care for termination crash |
---|
57 | return 0; |
---|
58 | } |
---|
59 | |
---|