1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2014 |
---|
3 | // Created by Roman Ryltsov roman@alax.info, http://alax.info |
---|
4 | |
---|
5 | #pragma once |
---|
6 | |
---|
7 | #include <mmdeviceapi.h> |
---|
8 | #include <audioclient.h> |
---|
9 | #include "resource.h" // main symbols |
---|
10 | #include "PlayService_i.h" |
---|
11 | |
---|
12 | using namespace ATL; |
---|
13 | |
---|
14 | //////////////////////////////////////////////////////////// |
---|
15 | // CPlay |
---|
16 | |
---|
17 | class ATL_NO_VTABLE CPlay : |
---|
18 | public CComObjectRootEx<CComMultiThreadModel>, |
---|
19 | public CComCoClass<CPlay, &CLSID_Play>, |
---|
20 | public IDispatchImpl<IPlay> |
---|
21 | { |
---|
22 | public: |
---|
23 | |
---|
24 | DECLARE_REGISTRY_RESOURCEID(IDR_PLAY) |
---|
25 | |
---|
26 | DECLARE_PROTECT_FINAL_CONSTRUCT() |
---|
27 | |
---|
28 | BEGIN_COM_MAP(CPlay) |
---|
29 | COM_INTERFACE_ENTRY(IPlay) |
---|
30 | COM_INTERFACE_ENTRY(IDispatch) |
---|
31 | END_COM_MAP() |
---|
32 | |
---|
33 | public: |
---|
34 | HRESULT FinalConstruct() |
---|
35 | { |
---|
36 | { |
---|
37 | CComPtr<IMMDeviceEnumerator> pMmDeviceEnumerator; |
---|
38 | ATLENSURE_SUCCEEDED(pMmDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator))); |
---|
39 | CComPtr<IMMDevice> pMmDevice; |
---|
40 | ATLENSURE_SUCCEEDED(pMmDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pMmDevice)); |
---|
41 | CComPtr<IAudioClient> pAudioClient; |
---|
42 | ATLENSURE_SUCCEEDED(pMmDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (VOID**) &pAudioClient)); |
---|
43 | CComHeapPtr<WAVEFORMATEX> pWaveFormatEx; |
---|
44 | ATLENSURE_SUCCEEDED(pAudioClient->GetMixFormat(&pWaveFormatEx)); |
---|
45 | static const ULONG g_nDuration = 5000; // 5 seconds |
---|
46 | ATLENSURE_SUCCEEDED(pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, (g_nDuration + (g_nDuration >> 16)) * 10000i64, 0, pWaveFormatEx, NULL)); |
---|
47 | CComPtr<IAudioRenderClient> pAudioRenderClient; |
---|
48 | ATLENSURE_SUCCEEDED(pAudioClient->GetService(__uuidof(IAudioRenderClient), (VOID**) &pAudioRenderClient)); |
---|
49 | ATLASSERT(pWaveFormatEx->wFormatTag == WAVE_FORMAT_EXTENSIBLE); |
---|
50 | ATLASSERT(pWaveFormatEx->wBitsPerSample == 32); |
---|
51 | UINT32 nSampleCount = g_nDuration * pWaveFormatEx->nSamplesPerSec / 1000; |
---|
52 | BYTE* pnData; |
---|
53 | ATLENSURE_SUCCEEDED(pAudioRenderClient->GetBuffer(nSampleCount, &pnData)); |
---|
54 | ZeroMemory(pnData, pWaveFormatEx->nAvgBytesPerSec); |
---|
55 | static const FLOAT g_pfValues[] = { 0, -0.05f, 0, +0.05f }; |
---|
56 | for(SIZE_T nSampleIndex = nSampleCount; nSampleIndex > 0; nSampleIndex--) |
---|
57 | ((FLOAT*) pnData)[(nSampleIndex - 1) * pWaveFormatEx->nChannels] = g_pfValues[nSampleIndex % _countof(g_pfValues)]; |
---|
58 | ATLENSURE_SUCCEEDED(pAudioRenderClient->ReleaseBuffer(nSampleCount, 0)); |
---|
59 | ATLENSURE_SUCCEEDED(pAudioClient->Start()); |
---|
60 | Sleep(g_nDuration); |
---|
61 | } |
---|
62 | return S_OK; |
---|
63 | } |
---|
64 | }; |
---|
65 | |
---|
66 | OBJECT_ENTRY_AUTO(__uuidof(Play), CPlay) |
---|