-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectAudioStream.cpp
executable file
·121 lines (91 loc) · 3.3 KB
/
KinectAudioStream.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
/**
* @file KinectAudioStream.cpp
* @ingroup Kinect
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#include "KinectAudioStream.h"
using namespace MobileRGBD::Kinect2;
/* virtual */ void FUNCTION_CALL_TYPE KinectAudioStream::Run()
{
if ( RecContext == nullptr )
{
fprintf( stderr, "No buffer available for audio\n" );
return;
}
WAITABLE_HANDLE hAudioSource = NULL;
timeb lTimestamp;
IKinectSensor * pSensor = Caller.pSensor;
if ( pSensor == nullptr )
{
fprintf( stderr, "Unable to find Kinect sensor for audio\n" );
return;
}
// Open Audio
IAudioSource * pAudioSource = nullptr;
pSensor->get_AudioSource(&pAudioSource);
IAudioBeamFrameReader *pAudioReader = nullptr;
pAudioSource->OpenReader(&pAudioReader);
pAudioReader->SubscribeFrameArrived(&hAudioSource);
unsigned char * InitBuffer = (unsigned char*)RecContext->BufferData;
while(!StopPending())
{
IAudioBeamFrameArrivedEventArgs* pArgs = nullptr;
if ( SUCCEEDED(pAudioReader->GetFrameArrivedEventData(hAudioSource, &pArgs)) )
{
// we do not use them
SafeRelease(pArgs);
// Get timestamp
ftime(&lTimestamp);
// get data
IAudioBeamFrameList * pAudioFrameList = nullptr;
if ( FAILED(pAudioReader->AcquireLatestBeamFrames(&pAudioFrameList)) ) { continue; }
// Only one beam for the moment, SDK v2.0.1409
UINT NbBeams = 1; // pAudioFrameList->get_BeamCount(&NbBeams);
bool NbValidBeam = 0;
for( UINT NumBeam = 0; NumBeam < NbBeams; NumBeam++ ) // As only one beam now, compiler will simplify this, but the code will remind for further SDK
{
IAudioBeamFrame * pAudioBeamFrame = nullptr;
UINT SubFrameCount = 0;
unsigned char * InBuffer = InitBuffer;
RecContext->BufferSize = 0;
if ( SUCCEEDED(pAudioFrameList->OpenAudioBeamFrame(NbBeams-1, &pAudioBeamFrame)) &&
SUCCEEDED(pAudioBeamFrame->get_SubFrameCount(&SubFrameCount)) )
{
for( UINT i = 0; i < SubFrameCount; i++ )
{
IAudioBeamSubFrame* pAudioBeamSubFrame = NULL;
if ( SUCCEEDED( pAudioBeamFrame->GetSubFrame(i, &pAudioBeamSubFrame) ) )
{
UINT BufferSize = 0;
BYTE * BufferAddress = nullptr;
if ( SUCCEEDED(pAudioBeamSubFrame->AccessUnderlyingBuffer(&BufferSize,&BufferAddress)) )
{
memcpy( InBuffer, BufferAddress, BufferSize );
InBuffer += BufferSize;
RecContext->BufferSize += BufferSize;
}
}
SafeRelease(pAudioBeamSubFrame);
}
NbValidBeam++;
}
SafeRelease(pAudioBeamFrame);
}
SafeRelease(pAudioFrameList);
// Save audio data if any
if ( RecContext != nullptr && RecContext->BufferSize != 0 )
{
// To make source code looks like others
RawKinectRecording& rcs = *RecContext;
// Create quick a string containing number of samples
char TmpNb[10];
sprintf( TmpNb, "%d", rcs.BufferSize/sizeof(float) );
// Save Data, first set buffer size (in term of number of faces)
rcs.SaveDataAndIncreaseInputNumber( lTimestamp, TmpNb );
// Ok, here call the virtual function from the parent thread, from now, one beam, one call
// Caller.ProcessAudio();
}
}
}
}