/
AudioFilePlayer.c
354 lines (291 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
This file based on Apple sample code. We haven't changed the file name,
so if you want to see the original search for it on apple.com/developer
*/
25
#include "SDL_config.h"
26
27
28
29
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AudioFilePlayer.cpp
*/
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
#include "AudioFilePlayer.h"
/*
void ThrowResult (OSStatus result, const char* str)
{
SDL_SetError ("Error: %s %d", str, result);
throw result;
}
*/
#if DEBUG
static void PrintStreamDesc (AudioStreamBasicDescription *inDesc)
{
if (!inDesc) {
printf ("Can't print a NULL desc!\n");
return;
}
printf ("- - - - - - - - - - - - - - - - - - - -\n");
printf (" Sample Rate:%f\n", inDesc->mSampleRate);
printf (" Format ID:%s\n", (char*)&inDesc->mFormatID);
printf (" Format Flags:%lX\n", inDesc->mFormatFlags);
printf (" Bytes per Packet:%ld\n", inDesc->mBytesPerPacket);
printf (" Frames per Packet:%ld\n", inDesc->mFramesPerPacket);
printf (" Bytes per Frame:%ld\n", inDesc->mBytesPerFrame);
printf (" Channels per Frame:%ld\n", inDesc->mChannelsPerFrame);
printf (" Bits per Channel:%ld\n", inDesc->mBitsPerChannel);
printf ("- - - - - - - - - - - - - - - - - - - -\n");
}
#endif
static int AudioFilePlayer_SetDestination (AudioFilePlayer *afp, AudioUnit *inDestUnit)
{
64
/*if (afp->mConnected) throw static_cast<OSStatus>(-1);*/ /* can't set dest if already engaged */
65
66
67
if (afp->mConnected)
return 0 ;
68
SDL_memcpy(&afp->mPlayUnit, inDestUnit, sizeof (afp->mPlayUnit));
69
70
71
72
OSStatus result = noErr;
73
/* we can "down" cast a component instance to a component */
74
75
ComponentDescription desc;
result = GetComponentInfo ((Component)*inDestUnit, &desc, 0, 0, 0);
76
if (result) return 0; /*THROW_RESULT("GetComponentInfo")*/
77
78
79
80
/* we're going to use this to know which convert routine to call
a v1 audio unit will have a type of 'aunt'
a v2 audio unit will have one of several different types. */
81
82
if (desc.componentType != kAudioUnitComponentType) {
result = badComponentInstance;
83
/*THROW_RESULT("BAD COMPONENT")*/
84
85
86
87
88
89
90
91
92
93
if (result) return 0;
}
/* Set the input format of the audio unit. */
result = AudioUnitSetProperty (*inDestUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&afp->mFileDescription,
sizeof (afp->mFileDescription));
94
/*THROW_RESULT("AudioUnitSetProperty")*/
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
if (result) return 0;
return 1;
}
static void AudioFilePlayer_SetNotifier(AudioFilePlayer *afp, AudioFilePlayNotifier inNotifier, void *inRefCon)
{
afp->mNotifier = inNotifier;
afp->mRefCon = inRefCon;
}
static int AudioFilePlayer_IsConnected(AudioFilePlayer *afp)
{
return afp->mConnected;
}
static AudioUnit AudioFilePlayer_GetDestUnit(AudioFilePlayer *afp)
{
return afp->mPlayUnit;
}
static void AudioFilePlayer_Print(AudioFilePlayer *afp)
{
#if DEBUG
printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false"));
printf ("- - - - - - - - - - - - - - \n");
#endif
}
static void AudioFilePlayer_SetStartFrame (AudioFilePlayer *afp, int frame)
{
SInt64 position = frame * 2352;
afp->mStartFrame = frame;
afp->mAudioFileManager->SetPosition (afp->mAudioFileManager, position);
}
static int AudioFilePlayer_GetCurrentFrame (AudioFilePlayer *afp)
{
return afp->mStartFrame + (afp->mAudioFileManager->GetByteCounter(afp->mAudioFileManager) / 2352);
}
static void AudioFilePlayer_SetStopFrame (AudioFilePlayer *afp, int frame)
{
SInt64 position = frame * 2352;
afp->mAudioFileManager->SetEndOfFile (afp->mAudioFileManager, position);
}
void delete_AudioFilePlayer(AudioFilePlayer *afp)
{
if (afp != NULL)
{
afp->Disconnect(afp);
if (afp->mAudioFileManager) {
delete_AudioFileManager(afp->mAudioFileManager);
afp->mAudioFileManager = 0;
}
if (afp->mForkRefNum) {
FSClose (afp->mForkRefNum);
afp->mForkRefNum = 0;
}
159
SDL_free(afp);
160
161
162
163
164
165
166
167
168
169
170
171
172
}
}
static int AudioFilePlayer_Connect(AudioFilePlayer *afp)
{
#if DEBUG
printf ("Connect:%x, engaged=%d\n", (int)afp->mPlayUnit, (afp->mConnected ? 1 : 0));
#endif
if (!afp->mConnected)
{
if (!afp->mAudioFileManager->DoConnect(afp->mAudioFileManager))
return 0;
173
/* set the render callback for the file data to be supplied to the sound converter AU */
174
175
176
177
178
179
180
181
182
afp->mInputCallback.inputProc = afp->mAudioFileManager->FileInputProc;
afp->mInputCallback.inputProcRefCon = afp->mAudioFileManager;
OSStatus result = AudioUnitSetProperty (afp->mPlayUnit,
kAudioUnitProperty_SetInputCallback,
kAudioUnitScope_Input,
0,
&afp->mInputCallback,
sizeof(afp->mInputCallback));
183
if (result) return 0; /*THROW_RESULT("AudioUnitSetProperty")*/
184
185
186
187
188
189
afp->mConnected = 1;
}
return 1;
}
190
191
/* warning noted, now please go away ;-) */
/* #warning This should redirect the calling of notification code to some other thread */
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
static void AudioFilePlayer_DoNotification (AudioFilePlayer *afp, OSStatus inStatus)
{
if (afp->mNotifier) {
(*afp->mNotifier) (afp->mRefCon, inStatus);
} else {
SDL_SetError ("Notification posted with no notifier in place");
if (inStatus == kAudioFilePlay_FileIsFinished)
afp->Disconnect(afp);
else if (inStatus != kAudioFilePlayErr_FilePlayUnderrun)
afp->Disconnect(afp);
}
}
static void AudioFilePlayer_Disconnect (AudioFilePlayer *afp)
{
#if DEBUG
printf ("Disconnect:%x,%ld, engaged=%d\n", (int)afp->mPlayUnit, 0, (afp->mConnected ? 1 : 0));
#endif
if (afp->mConnected)
{
afp->mConnected = 0;
afp->mInputCallback.inputProc = 0;
afp->mInputCallback.inputProcRefCon = 0;
OSStatus result = AudioUnitSetProperty (afp->mPlayUnit,
kAudioUnitProperty_SetInputCallback,
kAudioUnitScope_Input,
0,
&afp->mInputCallback,
sizeof(afp->mInputCallback));
if (result)
SDL_SetError ("AudioUnitSetProperty:RemoveInputCallback:%ld", result);
afp->mAudioFileManager->Disconnect(afp->mAudioFileManager);
}
}
typedef struct {
UInt32 offset;
UInt32 blockSize;
} SSNDData;
static int AudioFilePlayer_OpenFile (AudioFilePlayer *afp, const FSRef *inRef, SInt64 *outFileDataSize)
{
ContainerChunk chunkHeader;
ChunkHeader chunk;
SSNDData ssndData;
OSErr result;
HFSUniStr255 dfName;
ByteCount actual;
SInt64 offset;
246
/* Open the data fork of the input file */
247
result = FSGetDataForkName(&dfName);
248
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSGetDataForkName")*/
249
250
result = FSOpenFork(inRef, dfName.length, dfName.unicode, fsRdPerm, &afp->mForkRefNum);
251
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSOpenFork")*/
252
253
/* Read the file header, and check if it's indeed an AIFC file */
254
result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(chunkHeader), &chunkHeader, &actual);
255
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
256
257
258
if (chunkHeader.ckID != 'FORM') {
result = -1;
259
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): chunk id is not 'FORM'");*/
260
261
262
263
}
if (chunkHeader.formType != 'AIFC') {
result = -1;
264
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): file format is not 'AIFC'");*/
265
266
}
267
268
269
270
271
/* Search for the SSND chunk. We ignore all compression etc. information
in other chunks. Of course that is kind of evil, but for now we are lazy
and rely on the cdfs to always give us the same fixed format.
TODO: Parse the COMM chunk we currently skip to fill in mFileDescription.
*/
272
273
274
offset = 0;
do {
result = FSReadFork(afp->mForkRefNum, fsFromMark, offset, sizeof(chunk), &chunk, &actual);
275
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
276
277
/* Skip the chunk data */
278
279
280
offset = chunk.ckSize;
} while (chunk.ckID != 'SSND');
281
282
/* Read the header of the SSND chunk. After this, we are positioned right
at the start of the audio data. */
283
result = FSReadFork(afp->mForkRefNum, fsAtMark, 0, sizeof(ssndData), &ssndData, &actual);
284
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")*/
285
286
result = FSSetForkPosition(afp->mForkRefNum, fsFromMark, ssndData.offset);
287
if (result) return 0; /*THROW_RESULT("AudioFilePlayer::OpenFile(): FSSetForkPosition")*/
288
289
/* Data size */
290
291
*outFileDataSize = chunk.ckSize - ssndData.offset - 8;
292
/* File format */
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
afp->mFileDescription.mSampleRate = 44100;
afp->mFileDescription.mFormatID = kAudioFormatLinearPCM;
afp->mFileDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
afp->mFileDescription.mBytesPerPacket = 4;
afp->mFileDescription.mFramesPerPacket = 1;
afp->mFileDescription.mBytesPerFrame = 4;
afp->mFileDescription.mChannelsPerFrame = 2;
afp->mFileDescription.mBitsPerChannel = 16;
return 1;
}
AudioFilePlayer *new_AudioFilePlayer (const FSRef *inFileRef)
{
SInt64 fileDataSize = 0;
309
AudioFilePlayer *afp = (AudioFilePlayer *) SDL_malloc(sizeof (AudioFilePlayer));
310
311
if (afp == NULL)
return NULL;
312
SDL_memset(afp, '\0', sizeof (*afp));
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#define SET_AUDIOFILEPLAYER_METHOD(m) afp->m = AudioFilePlayer_##m
SET_AUDIOFILEPLAYER_METHOD(SetDestination);
SET_AUDIOFILEPLAYER_METHOD(SetNotifier);
SET_AUDIOFILEPLAYER_METHOD(SetStartFrame);
SET_AUDIOFILEPLAYER_METHOD(GetCurrentFrame);
SET_AUDIOFILEPLAYER_METHOD(SetStopFrame);
SET_AUDIOFILEPLAYER_METHOD(Connect);
SET_AUDIOFILEPLAYER_METHOD(Disconnect);
SET_AUDIOFILEPLAYER_METHOD(DoNotification);
SET_AUDIOFILEPLAYER_METHOD(IsConnected);
SET_AUDIOFILEPLAYER_METHOD(GetDestUnit);
SET_AUDIOFILEPLAYER_METHOD(Print);
SET_AUDIOFILEPLAYER_METHOD(OpenFile);
#undef SET_AUDIOFILEPLAYER_METHOD
if (!afp->OpenFile (afp, inFileRef, &fileDataSize))
{
331
SDL_free(afp);
332
333
334
return NULL;
}
335
/* we want about 4 seconds worth of data for the buffer */
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
int bytesPerSecond = (UInt32) (4 * afp->mFileDescription.mSampleRate * afp->mFileDescription.mBytesPerFrame);
#if DEBUG
printf("File format:\n");
PrintStreamDesc (&afp->mFileDescription);
#endif
afp->mAudioFileManager = new_AudioFileManager(afp, afp->mForkRefNum,
fileDataSize,
bytesPerSecond);
if (afp->mAudioFileManager == NULL)
{
delete_AudioFilePlayer(afp);
return NULL;
}
return afp;
}