Skip to content

Latest commit

 

History

History
330 lines (269 loc) · 10.7 KB

AudioFilePlayer.cpp

File metadata and controls

330 lines (269 loc) · 10.7 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
25
26
27
28
29
30
31
32
/*
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
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// AudioFilePlayer.cpp
//
#include "AudioFilePlayer.h"
void ThrowResult (OSStatus result, const char* str)
{
Jan 4, 2004
Jan 4, 2004
33
SDL_SetError ("Error: %s %d", str, result);
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
throw result;
}
#if DEBUG
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
OSStatus AudioFileManager::FileInputProc (void *inRefCon,
AudioUnitRenderActionFlags inActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
AudioBuffer *ioData)
{
AudioFileManager* THIS = (AudioFileManager*)inRefCon;
return THIS->Render(*ioData);
}
OSStatus AudioFileManager::Render (AudioBuffer &ioData)
{
Aug 21, 2004
Aug 21, 2004
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
OSStatus result = noErr;
if (mBufferOffset >= mBufferSize) {
result = GetFileData(&mTmpBuffer, &mBufferSize);
if (result) {
SDL_SetError ("AudioConverterFillBuffer:%ld\n", result);
mParent.DoNotification (result);
return result;
}
mBufferOffset = 0;
}
if (ioData.mDataByteSize > mBufferSize - mBufferOffset)
ioData.mDataByteSize = mBufferSize - mBufferOffset;
ioData.mData = (char *)mTmpBuffer + mBufferOffset;
mBufferOffset += ioData.mDataByteSize;
mByteCounter += ioData.mDataByteSize;
AfterRender();
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
return result;
}
AudioFileManager::~AudioFileManager ()
{
if (mFileBuffer) {
free (mFileBuffer);
mFileBuffer = 0;
}
}
AudioFilePlayer::AudioFilePlayer (const FSRef *inFileRef)
: mConnected (false),
mAudioFileManager (0),
mNotifier (0),
mStartFrame (0)
{
SInt64 fileDataSize = 0;
OpenFile (inFileRef, fileDataSize);
Jan 4, 2004
Jan 4, 2004
111
112
// we want about 4 seconds worth of data for the buffer
int bytesPerSecond = UInt32 (4 * mFileDescription.mSampleRate * mFileDescription.mBytesPerFrame);
113
114
115
116
117
118
#if DEBUG
printf("File format:\n");
PrintStreamDesc (&mFileDescription);
#endif
Jan 4, 2004
Jan 4, 2004
119
120
mAudioFileManager = new AudioFileManager (*this,
mForkRefNum,
121
fileDataSize,
Jan 4, 2004
Jan 4, 2004
122
bytesPerSecond);
Aug 21, 2004
Aug 21, 2004
125
void AudioFilePlayer::SetDestination (AudioUnit &inDestUnit)
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
if (mConnected) throw static_cast<OSStatus>(-1); //can't set dest if already engaged
mPlayUnit = inDestUnit;
OSStatus result = noErr;
//we can "down" cast a component instance to a component
ComponentDescription desc;
result = GetComponentInfo ((Component)inDestUnit, &desc, 0, 0, 0);
THROW_RESULT("GetComponentInfo")
// 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.
Jan 4, 2004
Jan 4, 2004
142
if (desc.componentType != kAudioUnitComponentType) {
143
144
145
146
result = badComponentInstance;
THROW_RESULT("BAD COMPONENT")
}
Aug 21, 2004
Aug 21, 2004
147
148
149
150
151
152
153
154
/* Set the input format of the audio unit. */
result = AudioUnitSetProperty (inDestUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&mFileDescription,
sizeof (mFileDescription));
THROW_RESULT("AudioUnitSetProperty")
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
}
void AudioFilePlayer::SetStartFrame (int frame)
{
SInt64 position = frame * 2352;
mStartFrame = frame;
mAudioFileManager->SetPosition (position);
}
int AudioFilePlayer::GetCurrentFrame ()
{
return mStartFrame + (mAudioFileManager->GetByteCounter() / 2352);
}
void AudioFilePlayer::SetStopFrame (int frame)
{
SInt64 position = frame * 2352;
mAudioFileManager->SetEndOfFile (position);
}
AudioFilePlayer::~AudioFilePlayer()
{
Disconnect();
if (mAudioFileManager) {
delete mAudioFileManager;
mAudioFileManager = 0;
}
Jan 4, 2004
Jan 4, 2004
187
188
189
if (mForkRefNum) {
FSClose (mForkRefNum);
mForkRefNum = 0;
190
191
192
193
194
195
}
}
void AudioFilePlayer::Connect()
{
#if DEBUG
Aug 21, 2004
Aug 21, 2004
196
printf ("Connect:%x, engaged=%d\n", (int)mPlayUnit, (mConnected ? 1 : 0));
197
198
199
#endif
if (!mConnected)
{
Aug 21, 2004
Aug 21, 2004
200
201
202
mAudioFileManager->DoConnect();
203
// set the render callback for the file data to be supplied to the sound converter AU
Jan 4, 2004
Jan 4, 2004
204
205
206
207
208
209
mInputCallback.inputProc = AudioFileManager::FileInputProc;
mInputCallback.inputProcRefCon = mAudioFileManager;
OSStatus result = AudioUnitSetProperty (mPlayUnit,
kAudioUnitProperty_SetInputCallback,
kAudioUnitScope_Input,
Aug 21, 2004
Aug 21, 2004
210
0,
Jan 4, 2004
Jan 4, 2004
211
212
213
&mInputCallback,
sizeof(mInputCallback));
THROW_RESULT("AudioUnitSetProperty")
214
215
216
217
218
219
220
221
222
223
224
225
mConnected = true;
}
}
// warning noted, now please go away ;-)
// #warning This should redirect the calling of notification code to some other thread
void AudioFilePlayer::DoNotification (OSStatus inStatus) const
{
AudioFilePlayer* THIS = const_cast<AudioFilePlayer*>(this);
if (mNotifier) {
(*mNotifier) (mRefCon, inStatus);
Jan 4, 2004
Jan 4, 2004
226
} else {
227
228
229
230
231
232
233
234
235
236
237
238
SDL_SetError ("Notification posted with no notifier in place");
if (inStatus == kAudioFilePlay_FileIsFinished)
THIS->Disconnect();
else if (inStatus != kAudioFilePlayErr_FilePlayUnderrun)
THIS->Disconnect();
}
}
void AudioFilePlayer::Disconnect ()
{
#if DEBUG
Aug 21, 2004
Aug 21, 2004
239
printf ("Disconnect:%x,%ld, engaged=%d\n", (int)mPlayUnit, 0, (mConnected ? 1 : 0));
240
241
242
243
244
#endif
if (mConnected)
{
mConnected = false;
Jan 4, 2004
Jan 4, 2004
245
246
247
248
249
mInputCallback.inputProc = 0;
mInputCallback.inputProcRefCon = 0;
OSStatus result = AudioUnitSetProperty (mPlayUnit,
kAudioUnitProperty_SetInputCallback,
kAudioUnitScope_Input,
Aug 21, 2004
Aug 21, 2004
250
0,
Jan 4, 2004
Jan 4, 2004
251
252
253
254
255
&mInputCallback,
sizeof(mInputCallback));
if (result)
SDL_SetError ("AudioUnitSetProperty:RemoveInputCallback:%ld", result);
256
257
258
259
mAudioFileManager->Disconnect();
}
}
Jan 4, 2004
Jan 4, 2004
260
261
262
263
264
struct SSNDData {
UInt32 offset;
UInt32 blockSize;
};
265
void AudioFilePlayer::OpenFile (const FSRef *inRef, SInt64& outFileDataSize)
Jan 4, 2004
Jan 4, 2004
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
ContainerChunk chunkHeader;
ChunkHeader chunk;
SSNDData ssndData;
OSErr result;
HFSUniStr255 dfName;
ByteCount actual;
SInt64 offset;
// Open the data fork of the input file
result = FSGetDataForkName(&dfName);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSGetDataForkName")
result = FSOpenFork(inRef, dfName.length, dfName.unicode, fsRdPerm, &mForkRefNum);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSOpenFork")
// Read the file header, and check if it's indeed an AIFC file
result = FSReadFork(mForkRefNum, fsAtMark, 0, sizeof(chunkHeader), &chunkHeader, &actual);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")
if (chunkHeader.ckID != 'FORM') {
result = -1;
THROW_RESULT("AudioFilePlayer::OpenFile(): chunk id is not 'FORM'");
}
if (chunkHeader.formType != 'AIFC') {
result = -1;
THROW_RESULT("AudioFilePlayer::OpenFile(): file format is not 'AIFC'");
}
// 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.
offset = 0;
do {
result = FSReadFork(mForkRefNum, fsFromMark, offset, sizeof(chunk), &chunk, &actual);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")
// Skip the chunk data
offset = chunk.ckSize;
} while (chunk.ckID != 'SSND');
// Read the header of the SSND chunk. After this, we are positioned right
// at the start of the audio data.
result = FSReadFork(mForkRefNum, fsAtMark, 0, sizeof(ssndData), &ssndData, &actual);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSReadFork")
result = FSSetForkPosition(mForkRefNum, fsFromMark, ssndData.offset);
THROW_RESULT("AudioFilePlayer::OpenFile(): FSSetForkPosition")
// Data size
Jan 5, 2004
Jan 5, 2004
319
outFileDataSize = chunk.ckSize - ssndData.offset - 8;
Jan 4, 2004
Jan 4, 2004
320
321
322
323
324
325
326
327
328
329
330
// File format
mFileDescription.mSampleRate = 44100;
mFileDescription.mFormatID = kAudioFormatLinearPCM;
mFileDescription.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
mFileDescription.mBytesPerPacket = 4;
mFileDescription.mFramesPerPacket = 1;
mFileDescription.mBytesPerFrame = 4;
mFileDescription.mChannelsPerFrame = 2;
mFileDescription.mBitsPerChannel = 16;
}