/
NuiAudioStream.cpp
286 lines (245 loc) · 7.93 KB
/
NuiAudioStream.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//------------------------------------------------------------------------------
// <copyright file="NuiAudioStream.cpp" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
#include "stdafx.h"
#include <uuids.h>
#include <wmcodecdsp.h>
#include "NuiAudioStream.h"
#include "Utility.h"
#include "WaveWriter.h"
// For StringCch* and such
#include <strsafe.h>
#define TIMER_PERIOD 20 /////20milliseconds Check KinectWindow
const UINT FileSizeInMS = 60000; //60 seconds each file
const UINT TimersPerFile = FileSizeInMS / TIMER_PERIOD; ///////
const wchar_t *knownPath = L"C:\\Users\\yuner\\Desktop\\KinectGUI\\DataLog\\Room1";
//const wchar_t *knownPath = L"C:\\Users\\heal1\\Desktop\\Data";
//const wchar_t *knownPath = L"C:\\Users\\heal\\Desktop\\Data";
/// <summary>
/// Constructor
/// </summary>
/// <param name="pNuiSensor">The pointer to Nui sensor object</param>
NuiAudioStream::NuiAudioStream(INuiSensor* pNuiSensor, PCWSTR instanceName)
//NuiAudioStream::NuiAudioStream(INuiSensor* pNuiSensor)
: m_pNuiSensor(pNuiSensor)
, m_pNuiAudioSource(nullptr)
, m_pDMO(nullptr)
, m_pPropertyStore(nullptr)
, m_pAudioViewer(nullptr)
, m_pWaveWriter(nullptr) /////////
, m_TimerCount(0)
, m_instanceName((WCHAR*)instanceName)
, m_Recording(false)
{
if (m_pNuiSensor)
{
m_pNuiSensor->AddRef();
}
}
/// <summary>
/// Destructor
/// </summary>
NuiAudioStream::~NuiAudioStream()
{
SafeRelease(m_pPropertyStore);
SafeRelease(m_pDMO);
SafeRelease(m_pNuiAudioSource);
SafeRelease(m_pNuiSensor);
if (m_pWaveWriter) //////////////////
{
m_pWaveWriter->Stop();
delete m_pWaveWriter;
m_pWaveWriter = NULL;
}
}
/// <summary>
/// Start processing stream
/// </summary>
/// <returns>Indicates success or failure</returns>
HRESULT NuiAudioStream::StartStream()
{
// Get audio source interface
HRESULT hr = m_pNuiSensor->NuiGetAudioSource(&m_pNuiAudioSource);
if (FAILED(hr))
{
return hr;
}
// Query dmo interface
hr = m_pNuiAudioSource->QueryInterface(IID_IMediaObject, (void**)&m_pDMO);
if (FAILED(hr))
{
return hr;
}
// Query property store interface
hr = m_pNuiAudioSource->QueryInterface(IID_IPropertyStore, (void**)&m_pPropertyStore);
if (FAILED(hr))
{
return hr;
}
// Set AEC-MicArray DMO system mode. This must be set for the DMO to work properly.
// Possible values are:
// SINGLE_CHANNEL_AEC = 0
// OPTIBEAM_ARRAY_ONLY = 2
// OPTIBEAM_ARRAY_AND_AEC = 4
// SINGLE_CHANNEL_NSAGC = 5
PROPVARIANT pvSysMode;
// Initialize the variable
PropVariantInit(&pvSysMode);
// Assign properties
pvSysMode.vt = VT_I4;
pvSysMode.lVal = (LONG)(2); // Use OPTIBEAM_ARRAY_ONLY setting. Set OPTIBEAM_ARRAY_AND_AEC instead if you expect to have sound playing from speakers.
// Set properties
m_pPropertyStore->SetValue(MFPKEY_WMAAECMA_SYSTEM_MODE, pvSysMode);
// Release the variable
PropVariantClear(&pvSysMode);
// Set DMO output format
WAVEFORMATEX wfxOut = {AudioFormat, AudioChannels, AudioSamplesPerSecond, AudioAverageBytesPerSecond, AudioBlockAlign, AudioBitsPerSample, 0};
memcpy_s(&m_wfxOut,sizeof(WAVEFORMATEX),&wfxOut,sizeof(WAVEFORMATEX));
DMO_MEDIA_TYPE mt = {0};
// Initialize variable
MoInitMediaType(&mt, sizeof(WAVEFORMATEX));
// Assign format
mt.majortype = MEDIATYPE_Audio;
mt.subtype = MEDIASUBTYPE_PCM;
mt.lSampleSize = 0;
mt.bFixedSizeSamples = TRUE;
mt.bTemporalCompression = FALSE;
mt.formattype = FORMAT_WaveFormatEx;
memcpy_s(mt.pbFormat, sizeof(WAVEFORMATEX), &m_wfxOut, sizeof(WAVEFORMATEX));
// Set format
hr = m_pDMO->SetOutputType(0, &mt, 0);
// Release variable
MoFreeMediaType(&mt);
return hr;
}
/// <summary>
/// Attach stream viewer
/// </summary>
/// <param name="pViewer">The pointer to the viewer to attach</param>
void NuiAudioStream::SetStreamViewer(NuiAudioViewer* pViewer)
{
m_pAudioViewer = pViewer;
}
/// <summary>
/// Get the audio readings from the stream
/// </summary>
void NuiAudioStream::ProcessStream()
{
////// Getting the wave writer
if (m_Recording)
{
if ((m_TimerCount++)%TimersPerFile==0)
{
WCHAR szFilename[MAX_PATH] = { 0 };
if (SUCCEEDED(GetFileName(szFilename, _countof(szFilename), m_instanceName, AudioSensor)))
{
m_pWaveWriter = WaveWriter::Start(szFilename, &m_wfxOut);
}
m_TimerCount%=TimersPerFile;
}
}
if (m_pNuiAudioSource && m_pDMO)
{
// Set buffer
ULONG cbProduced = 0; //////
BYTE *pProduced = NULL; //////
DWORD dwStatus;
DMO_OUTPUT_DATA_BUFFER outputBuffer = {0};
outputBuffer.pBuffer = &m_captureBuffer;
do
{
m_captureBuffer.Init(0);
outputBuffer.dwStatus = 0;
// Process audio data
HRESULT hr = m_pDMO->ProcessOutput(0, 1, &outputBuffer, &dwStatus);
if (S_OK == hr)
{
m_captureBuffer.GetBufferAndLength(&pProduced, &cbProduced);
//// Get the reading
double beamAngle, sourceAngle, sourceConfidence;
if (SUCCEEDED(m_pNuiAudioSource->GetBeam(&beamAngle)) &&
SUCCEEDED(m_pNuiAudioSource->GetPosition(&sourceAngle, &sourceConfidence)))
{
if (m_pAudioViewer)
{
// Set reading sto viewer
m_pAudioViewer->SetAudioReadings(beamAngle, sourceAngle, sourceConfidence);
}
}
///////write buffer into wav files
if (m_Recording)
{
if (m_pWaveWriter)
{
m_pWaveWriter->WriteBytes(pProduced, cbProduced);
}
}
}
if (S_FALSE == hr)
{
cbProduced = 0;
}
}while (outputBuffer.dwStatus & DMO_OUTPUT_DATA_BUFFERF_INCOMPLETE);//Check if there is still remaining data
if (m_Recording)
{
if (m_TimerCount%TimersPerFile==0)
{
m_pWaveWriter->Stop();
delete m_pWaveWriter;
m_pWaveWriter = NULL;
}
}
}
}
/////////////////////////////
HRESULT GetFileName(wchar_t *FileName, UINT FileNameSize, WCHAR* instanceName, SensorType senserID)
{
wchar_t DevIdOut[20];
HRESULT hr;
GetDevIdforFile(instanceName, DevIdOut);
// Get the time
wchar_t timeString[MAX_PATH];
wchar_t dateString[MAX_PATH];
GetTimeFormatEx(NULL, TIME_FORCE24HOURFORMAT, NULL, L"hh'-'mm'-'ss", timeString, _countof(timeString));
GetDateFormatEx(NULL, 0, NULL, L"yyyy'-'MMM'-'dd", dateString, _countof(dateString), NULL);
// File name will be DeviceConnectID-KinectAudio-HH-MM-SS.wav
switch (senserID)
{
case AudioSensor:
hr=StringCchPrintfW(FileName, FileNameSize, L"%s\\KinectAudio\\Audio-%s-%s-%s.wav", knownPath, DevIdOut, dateString, timeString);
break;
case RGBSensor:
hr=StringCchPrintfW(FileName, FileNameSize, L"%s\\KinectRGB\\RGB-%s-%s-%s.avi", knownPath, DevIdOut, dateString, timeString);
break;
case DepthSensor:
hr=StringCchPrintfW(FileName, FileNameSize, L"%s\\KinectDepth\\Depth-%s-%s-%s.avi", knownPath, DevIdOut, dateString, timeString);
break;
case SpeechRecog:
hr=StringCchPrintfW(FileName, FileNameSize, L"%s\\KinectSpeechRecog\\SpeechRecog-%s-%s-%s.txt", knownPath, DevIdOut, dateString, timeString);
break;
default:
break;
}
return hr;
}
bool GetDevIdforFile(wchar_t *USBDeviceInstancePath, wchar_t *pDevIdOut)
{
int k=0, n=k;
while (USBDeviceInstancePath[k]) k++;
while (--k>0)
{
pDevIdOut[n++]=USBDeviceInstancePath[k];
if (n==10) break;
};
if (k==0) return false;
pDevIdOut[n]=L'\0';
return true;
}
void NuiAudioStream::SetRecordingStatus (bool RecStatus)
{
m_Recording=RecStatus;
m_TimerCount=0;
}
bool NuiAudioStream::GetRecordingStauts () const {return m_Recording;}