-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
202 lines (153 loc) · 7.33 KB
/
main.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
#include "unimotion.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <AudioToolbox/AudioToolbox.h>
#include "CAXException.h"
#include "CAStreamBasicDescription.h"
#include "CAAudioUnit.h"
// helper functions
int isDelta(double threshold, double x, double y, double z, double prev_x, double prev_y, double prev_z);
double PrepareFileAU (CAAudioUnit &au, CAStreamBasicDescription &fileFormat, AudioFileID audioFile);
void MakeSimpleGraph (AUGraph &theGraph, CAAudioUnit &fileAU, CAStreamBasicDescription &fileFormat, AudioFileID audioFile);
int main (int argc, char * const argv[])
{
char inputFile[]="blip.mp3";
static const double threshold=0.50;
int hardware=macbookpro;
double x,y,z,prev_x,prev_y,prev_z;
AudioFileID audioFile;
CFURLRef theURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8*)inputFile, strlen(inputFile), false);
XThrowIfError (AudioFileOpenURL (theURL, kAudioFileReadPermission, 0, &audioFile), "AudioFileOpenURL");
// get the number of channels of the file
CAStreamBasicDescription fileFormat;
UInt32 propsize = sizeof(CAStreamBasicDescription);
XThrowIfError (AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &propsize, &fileFormat), "AudioFileGetProperty");
// lets set up our playing state now
AUGraph theGraph;
CAAudioUnit fileAU;
// this makes the graph, the file AU and sets it all up for playing
MakeSimpleGraph (theGraph, fileAU, fileFormat, audioFile);
// now we load the file contents up for playback before we start playing
// this has to be done the AU is initialized and anytime it is reset or uninitialized
Float64 fileDuration = PrepareFileAU (fileAU, fileFormat, audioFile);
printf ("file duration: %f secs\n", fileDuration);
read_sms_real(hardware,&x,&y,&z);
prev_x=x;
prev_y=y;
prev_z=z;
for(;;) {
read_sms_real(hardware,&x,&y,&z);
//printf("x: %f y: %f z: %f\n",x,y,z);
if(isDelta(threshold,x,y,z,prev_x,prev_y,prev_z))
XThrowIfError (AUGraphStart (theGraph), "AUGraphStart");
prev_x=x;
prev_y=y;
prev_z=z;
}
// sleep until the file is finished
//usleep ((int)(fileDuration * 1000. * 1000.));
// lets clean up
XThrowIfError (AUGraphStop (theGraph), "AUGraphStop");
XThrowIfError (AUGraphUninitialize (theGraph), "AUGraphUninitialize");
XThrowIfError (AudioFileClose (audioFile), "AudioFileClose");
XThrowIfError (AUGraphClose (theGraph), "AUGraphClose");
return 0;
}
double PrepareFileAU (CAAudioUnit &au, CAStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
//
// calculate the duration
UInt64 nPackets;
UInt32 propsize = sizeof(nPackets);
XThrowIfError (AudioFileGetProperty(audioFile, kAudioFilePropertyAudioDataPacketCount, &propsize, &nPackets), "kAudioFilePropertyAudioDataPacketCount");
Float64 fileDuration = (nPackets * fileFormat.mFramesPerPacket) / fileFormat.mSampleRate;
ScheduledAudioFileRegion rgn;
memset (&rgn.mTimeStamp, 0, sizeof(rgn.mTimeStamp));
rgn.mTimeStamp.mFlags = kAudioTimeStampSampleTimeValid;
rgn.mTimeStamp.mSampleTime = 0;
rgn.mCompletionProc = NULL;
rgn.mCompletionProcUserData = NULL;
rgn.mAudioFile = audioFile;
rgn.mLoopCount = 1;
rgn.mStartFrame = 0;
rgn.mFramesToPlay = UInt32(nPackets * fileFormat.mFramesPerPacket);
// tell the file player AU to play all of the file
XThrowIfError (au.SetProperty (kAudioUnitProperty_ScheduledFileRegion,
kAudioUnitScope_Global, 0,&rgn, sizeof(rgn)), "kAudioUnitProperty_ScheduledFileRegion");
// prime the fp AU with default values
UInt32 defaultVal = 0;
XThrowIfError (au.SetProperty (kAudioUnitProperty_ScheduledFilePrime,
kAudioUnitScope_Global, 0, &defaultVal, sizeof(defaultVal)), "kAudioUnitProperty_ScheduledFilePrime");
// tell the fp AU when to start playing (this ts is in the AU's render time stamps; -1 means next render cycle)
AudioTimeStamp startTime;
memset (&startTime, 0, sizeof(startTime));
startTime.mFlags = kAudioTimeStampSampleTimeValid;
startTime.mSampleTime = -1;
XThrowIfError (au.SetProperty(kAudioUnitProperty_ScheduleStartTimeStamp,
kAudioUnitScope_Global, 0, &startTime, sizeof(startTime)), "kAudioUnitProperty_ScheduleStartTimeStamp");
return fileDuration;
}
void MakeSimpleGraph (AUGraph &theGraph, CAAudioUnit &fileAU, CAStreamBasicDescription &fileFormat, AudioFileID audioFile)
{
XThrowIfError (NewAUGraph (&theGraph), "NewAUGraph");
CAComponentDescription cd;
// output node
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
AUNode outputNode;
XThrowIfError (AUGraphAddNode (theGraph, &cd, &outputNode), "AUGraphAddNode");
// file AU node
AUNode fileNode;
cd.componentType = kAudioUnitType_Generator;
cd.componentSubType = kAudioUnitSubType_AudioFilePlayer;
XThrowIfError (AUGraphAddNode (theGraph, &cd, &fileNode), "AUGraphAddNode");
// connect & setup
XThrowIfError (AUGraphOpen (theGraph), "AUGraphOpen");
// install overload listener to detect when something is wrong
AudioUnit anAU;
XThrowIfError (AUGraphNodeInfo(theGraph, fileNode, NULL, &anAU), "AUGraphNodeInfo");
fileAU = CAAudioUnit (fileNode, anAU);
// prepare the file AU for playback
// set its output channels
XThrowIfError (fileAU.SetNumberChannels (kAudioUnitScope_Output, 0, fileFormat.NumberChannels()), "SetNumberChannels");
// set the output sample rate of the file AU to be the same as the file:
XThrowIfError (fileAU.SetSampleRate (kAudioUnitScope_Output, 0, fileFormat.mSampleRate), "SetSampleRate");
// load in the file
XThrowIfError (fileAU.SetProperty(kAudioUnitProperty_ScheduledFileIDs,
kAudioUnitScope_Global, 0, &audioFile, sizeof(audioFile)), "SetScheduleFile");
XThrowIfError (AUGraphConnectNodeInput (theGraph, fileNode, 0, outputNode, 0), "AUGraphConnectNodeInput");
// AT this point we make sure we have the file player AU initialized
// this also propogates the output format of the AU to the output unit
XThrowIfError (AUGraphInitialize (theGraph), "AUGraphInitialize");
// workaround a race condition in the file player AU
usleep (10 * 1000);
// if we have a surround file, then we should try to tell the output AU what the order of the channels will be
if (fileFormat.NumberChannels() > 2) {
UInt32 layoutSize = 0;
OSStatus err;
XThrowIfError (err = AudioFileGetPropertyInfo (audioFile, kAudioFilePropertyChannelLayout, &layoutSize, NULL),
"kAudioFilePropertyChannelLayout");
if (!err && layoutSize) {
char* layout = new char[layoutSize];
err = AudioFileGetProperty(audioFile, kAudioFilePropertyChannelLayout, &layoutSize, layout);
XThrowIfError (err, "Get Layout From AudioFile");
// ok, now get the output AU and set its layout
XThrowIfError (AUGraphNodeInfo(theGraph, outputNode, NULL, &anAU), "AUGraphNodeInfo");
err = AudioUnitSetProperty (anAU, kAudioUnitProperty_AudioChannelLayout,
kAudioUnitScope_Input, 0, layout, layoutSize);
XThrowIfError (err, "kAudioUnitProperty_AudioChannelLayout");
delete [] layout;
}
}
}
int isDelta(double threshold, double x, double y, double z, double prev_x, double prev_y, double prev_z)
{
double deltax=x-prev_x;
double deltay=y-prev_y;
double deltaz=z-prev_z;
double delta=sqrt(deltax*deltax+deltay*deltay+deltaz*deltaz);
printf("delta: %f\n",delta);
return delta>=threshold;
}