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
|
/************************************************************************************
Filename : WavPlayer_OSX.cpp
Content : An Apple OSX audio handler.
Created : March 5, 2013
Authors : Robotic Arm Software - Peter Hoff, Dan Goodman, Bryan Croteau
Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "OSX_WavPlayer.h"
namespace OVR { namespace OvrPlatform { namespace OSX {
WavPlayer::WavPlayer(const char* fileName)
{
FileName = fileName;
}
bool WavPlayer::isDataChunk(unsigned char* buffer, int index)
{
unsigned char a = buffer[index];
unsigned char b = buffer[index + 1];
unsigned char c = buffer[index + 2];
unsigned char d = buffer[index + 3];
return (a == 'D' || a == 'd') && (b == 'A' || b == 'a') &&
(c == 'T' || c == 't') && (d == 'A' || d == 'a');
}
int WavPlayer::getWord(unsigned char* buffer, int index)
{
unsigned char a = buffer[index];
unsigned char b = buffer[index + 1];
unsigned char c = buffer[index + 2];
unsigned char d = buffer[index + 3];
int result = 0;
result |= a;
result |= b << 8;
result |= c << 16;
result |= d << 24;
return result;
}
short WavPlayer::getHalf(unsigned char* buffer, int index)
{
unsigned char a = buffer[index];
unsigned char b = buffer[index + 1];
short result = 0;
result |= a;
result |= b << 8;
return result;
}
void *WavPlayer::LoadPCM(const char *filename, unsigned long *len)
{
FILE *file;
struct stat s;
void *pcm;
if(stat(filename, &s))
{
return NULL;
}
*len = s.st_size;
pcm = (void *) malloc(s.st_size);
if(!pcm)
{
return NULL;
}
file = fopen(filename, "rb");
if(!file)
{
free(pcm);
return NULL;
}
fread(pcm, s.st_size, 1, file);
fclose(file);
return pcm;
}
int WavPlayer::PlayBuffer(void *pcmbuffer, unsigned long len)
{
AQCallbackStruct aqc;
UInt32 err, bufferSize;
int i;
aqc.DataFormat.mSampleRate = SampleRate;
aqc.DataFormat.mFormatID = kAudioFormatLinearPCM;
if(BitsPerSample == 16)
{
aqc.DataFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger
| kAudioFormatFlagIsPacked;
}
aqc.DataFormat.mBytesPerPacket = NumChannels * (BitsPerSample / 8);
aqc.DataFormat.mFramesPerPacket = 1;
aqc.DataFormat.mBytesPerFrame = NumChannels * (BitsPerSample / 8);
aqc.DataFormat.mChannelsPerFrame = NumChannels;
aqc.DataFormat.mBitsPerChannel = BitsPerSample;
aqc.FrameCount = SampleRate / 60;
aqc.SampleLen = (UInt32)(len);
aqc.PlayPtr = 0;
aqc.PCMBuffer = static_cast<unsigned char*>(pcmbuffer);
err = AudioQueueNewOutput(&aqc.DataFormat,
aqBufferCallback,
&aqc,
NULL,
kCFRunLoopCommonModes,
0,
&aqc.Queue);
if(err)
{
return err;
}
aqc.FrameCount = SampleRate / 60;
bufferSize = aqc.FrameCount * aqc.DataFormat.mBytesPerPacket;
for(i = 0; i < AUDIO_BUFFERS; i++)
{
err = AudioQueueAllocateBuffer(aqc.Queue, bufferSize,
&aqc.Buffers[i]);
if(err)
{
return err;
}
aqBufferCallback(&aqc, aqc.Queue, aqc.Buffers[i]);
}
err = AudioQueueStart(aqc.Queue, NULL);
if(err)
{
return err;
}
while(true)
{
}
sleep(1);
return 0;
}
void WavPlayer::aqBufferCallback(void *in, AudioQueueRef inQ, AudioQueueBufferRef outQB)
{
AQCallbackStruct *aqc;
unsigned char *coreAudioBuffer;
aqc = (AQCallbackStruct *) in;
coreAudioBuffer = (unsigned char*) outQB->mAudioData;
printf("Sync: %u / %u\n", aqc->PlayPtr, aqc->SampleLen);
if(aqc->FrameCount > 0)
{
outQB->mAudioDataByteSize = aqc->DataFormat.mBytesPerFrame * aqc->FrameCount;
for(int i = 0; i < aqc->FrameCount * aqc->DataFormat.mBytesPerFrame; i++)
{
if(aqc->PlayPtr > aqc->SampleLen)
{
aqc->PlayPtr = 0;
i = 0;
}
coreAudioBuffer[i] = aqc->PCMBuffer[aqc->PlayPtr];
aqc->PlayPtr++;
}
AudioQueueEnqueueBuffer(inQ, outQB, 0, NULL);
}
}
int WavPlayer::PlayAudio()
{
unsigned long len;
void *pcmbuffer;
int ret;
pcmbuffer = LoadPCM(FileName, &len);
if(!pcmbuffer)
{
fprintf(stderr, "%s: %s\n", FileName, strerror(errno));
exit(EXIT_FAILURE);
}
unsigned char* bytes = (unsigned char*)pcmbuffer;
int index = 0;
// 'RIFF'
getWord(bytes, index);
index += 4;
// int Length
getWord(bytes, index);
index += 4;
// 'WAVE'
getWord(bytes, index);
index += 4;
// 'fmt '
getWord(bytes, index);
index += 4;
// int Format Length
int fmtLen = getWord(bytes, index);
index += 4;
AudioFormat = getHalf(bytes, index);
index += 2;
NumChannels = getHalf(bytes, index);
index += 2;
SampleRate = getWord(bytes, index);
index += 4;
ByteRate = getWord(bytes, index);
index += 4;
BlockAlign = getHalf(bytes, index);
index += 2;
BitsPerSample = getHalf(bytes, index);
index += 2;
index += fmtLen - 16;
while(!isDataChunk(bytes, index))
{
// Any Chunk
getWord(bytes, index);
index += 4;
// Any Chunk Length
int anyChunkLen = getWord(bytes, index);
index += 4 + anyChunkLen;
}
// 'data'
getWord(bytes, index);
index += 4;
// int Data Length
unsigned long dataLen = getWord(bytes, index);
index += 4;
unsigned char* target = &bytes[index];
ret = PlayBuffer((void *)target, dataLen);
free(pcmbuffer);
return ret;
}
}}}
|