JunkBox_Win_Lib 1.5.3
Loading...
Searching...
No Matches
KinectAudio.cpp
Go to the documentation of this file.
1
2//#include "WinBaseLib.h"
3#include "KinectAudio.h"
4
5
6#ifdef ENABLE_KINECT_SDK
7
8
9using namespace jbxl;
10using namespace jbxwl;
11
12
13CKinectAudio::CKinectAudio(INuiSensor* sensor, int mode)
14{
15 m_audioStream = NULL;
16 m_audioSource = NULL;
17 m_audioMedia = NULL;
18 m_audioProperty = NULL;
19 m_pStream = NULL;
20
21 //
22 if (sensor!=NULL) init(sensor, mode);
23}
24
25
26
27CKinectAudio::~CKinectAudio(void)
28{
29 DEBUG_INFO("DESTRUCTOR: CKinectAudio: START\n");
30
31 free();
32
33 DEBUG_INFO("DESTRUCTOR: CKinectAudio: END\n");
34}
35
36
37
38BOOL CKinectAudio::init(INuiSensor* sensor, int mode)
39{
40 free();
41
42 if (sensor!=NULL) {
43 HRESULT hr = sensor->NuiGetAudioSource(&m_audioSource);
44 if (SUCCEEDED(hr)) hr = m_audioSource->QueryInterface(IID_IMediaObject, (void**)&m_audioMedia);
45 if (SUCCEEDED(hr)) hr = m_audioSource->QueryInterface(IID_IPropertyStore, (void**)&m_audioProperty);
46 //
47 if (SUCCEEDED(hr)) {
48 BOOL ret = setProperty(mode);
49 if (ret) ret = setMediaType();
50 if (ret) {
51 m_audioStream = new CWinAudioStream(m_audioMedia);
52 m_audioStream->QueryInterface(IID_IStream, (void**)&m_pStream);
53 return TRUE;
54 }
55 }
56 }
57 return FALSE;
58}
59
60
61
62void CKinectAudio::free(void)
63{
64 releaseNull(m_pStream);
65 deleteNull (m_audioStream);
66
67 releaseNull(m_audioProperty);
68 releaseNull(m_audioMedia);
69 releaseNull(m_audioSource);
70}
71
72
73
83BOOL CKinectAudio::setProperty(int mode)
84{
85 if (m_audioProperty==NULL) return FALSE;
86
87 PROPVARIANT prop;
88
89 PropVariantInit(&prop);
90 prop.vt = VT_I4;
91 prop.lVal = (LONG)mode;
92 m_audioProperty->SetValue(MFPKEY_WMAAECMA_SYSTEM_MODE, prop);
93 PropVariantClear(&prop);
94
95 return TRUE;
96}
97
98
99
100BOOL CKinectAudio::setMediaType(void)
101{
102 if (m_audioMedia==NULL) return FALSE;
103
104 DMO_MEDIA_TYPE mt = {0};
105 MoInitMediaType(&mt, sizeof(WAVEFORMATEX));
106 mt.majortype = MEDIATYPE_Audio;
107 mt.subtype = MEDIASUBTYPE_PCM;
108 mt.lSampleSize = 0;
109 mt.bFixedSizeSamples = TRUE;
110 mt.bTemporalCompression = FALSE;
111 mt.formattype = FORMAT_WaveFormatEx;
112 memcpy(mt.pbFormat, &getAudioFormat(), sizeof(WAVEFORMATEX));
113
114 HRESULT hr = m_audioMedia->SetOutputType(0, &mt, 0);
115 MoFreeMediaType(&mt);
116
117 if (FAILED(hr)) return FALSE;
118 return TRUE;
119}
120
121
122
123void CKinectAudio::getAudioData(Buffer* pbuf)
124{
125 if (m_pStream==NULL || pbuf==NULL) return;
126
127 ULONG len = 0;
128
129 m_pStream->Read((void*)pbuf->buf, (ULONG)pbuf->bufsz, &len);
130 pbuf->vldsz = (int)len;
131
132 return;
133}
134
135
136
137WAVEFORMATEX CKinectAudio::getAudioFormat(void)
138{
139 WORD blockAlign = ((KinectAudioBitsPerSample+7)/8)*KinectAudioChannels;
140 DWORD avgBytesPerSec = blockAlign*KinectAudioSamplesPerSec;
141
142 WAVEFORMATEX format = {
143 KinectAudioFormat, KinectAudioChannels, KinectAudioSamplesPerSec,
144 avgBytesPerSec, blockAlign, KinectAudioBitsPerSample, 0
145 };
146 return format;
147}
148
149
150#endif // ENABLE_KINECT_SDK
151
152
void deleteNull(T *&ptr)
Definition WinTools.h:229
void releaseNull(T *&ptr)
Definition WinTools.h:220