Mercurial > sdl-ios-xcode
comparison src/cdrom/macosx/AudioFilePlayer.h @ 613:9c6717a1c66f
Added MacOS X CD-ROM audio support (thanks Max and Darrell)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 15 Apr 2003 16:33:56 +0000 |
parents | |
children | 1bd056de5d1b |
comparison
equal
deleted
inserted
replaced
612:0648505b1f8b | 613:9c6717a1c66f |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 | |
22 This file based on Apple sample code. We haven't changed the file name, | |
23 so if you want to see the original search for it on apple.com/developer | |
24 */ | |
25 | |
26 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
27 // AudioFilePlayer.h | |
28 // | |
29 #ifndef __AudioFilePlayer_H__ | |
30 #define __AudioFilePlayer_H__ | |
31 | |
32 #include <CoreServices/CoreServices.h> | |
33 | |
34 #include <AudioToolbox/AudioToolbox.h> | |
35 #include <AudioUnit/AudioUnit.h> | |
36 | |
37 #include "SDL_Error.h" | |
38 | |
39 const char* AudioFilePlayerErrorStr (OSStatus error); | |
40 | |
41 void ThrowResult (OSStatus result, const char *str); | |
42 | |
43 #define THROW_RESULT(str) \ | |
44 if (result) { \ | |
45 ThrowResult (result, str); \ | |
46 } | |
47 | |
48 typedef void (*AudioFilePlayNotifier)(void *inRefCon, | |
49 OSStatus inStatus); | |
50 | |
51 enum { | |
52 kAudioFilePlayErr_FilePlayUnderrun = -10000, | |
53 kAudioFilePlay_FileIsFinished = -10001, | |
54 kAudioFilePlay_PlayerIsUninitialized = -10002 | |
55 }; | |
56 | |
57 | |
58 class AudioFileManager; | |
59 | |
60 #pragma mark __________ AudioFilePlayer | |
61 class AudioFilePlayer | |
62 { | |
63 public: | |
64 AudioFilePlayer (const FSRef *inFileRef); | |
65 | |
66 ~AudioFilePlayer(); | |
67 | |
68 void SetDestination (AudioUnit &inDestUnit, | |
69 int inBusNumber); | |
70 | |
71 void SetNotifier (AudioFilePlayNotifier inNotifier, void *inRefCon) | |
72 { | |
73 mNotifier = inNotifier; | |
74 mRefCon = inRefCon; | |
75 } | |
76 | |
77 void SetStartFrame (int frame); // seek in the file | |
78 | |
79 int GetCurrentFrame (); // get the current frame position | |
80 | |
81 void SetStopFrame (int frame); // set limit in the file | |
82 | |
83 void Connect(); | |
84 | |
85 void Disconnect(); | |
86 | |
87 void DoNotification (OSStatus inError) const; | |
88 | |
89 bool IsConnected () const { return mConnected; } | |
90 | |
91 UInt32 GetBusNumber () const { return mBusNumber; } | |
92 | |
93 AudioUnit GetDestUnit () const { return mPlayUnit; } | |
94 | |
95 AudioConverterRef GetAudioConverter() const { return mConverter; } | |
96 | |
97 #if DEBUG | |
98 void Print() const | |
99 { | |
100 CAShow (mAudioFileID); | |
101 printf ("Destination Bus:%ld\n", GetBusNumber()); | |
102 printf ("Is 'aunt' unit:%s\n", (mIsAUNTUnit ? "true" : "false")); | |
103 printf ("Is Connected:%s\n", (IsConnected() ? "true" : "false")); | |
104 if (mConverter) CAShow (mConverter); | |
105 printf ("- - - - - - - - - - - - - - \n"); | |
106 } | |
107 #endif | |
108 | |
109 const AudioStreamBasicDescription& GetFileFormat() const { return mFileDescription; } | |
110 | |
111 private: | |
112 AudioUnit mPlayUnit; | |
113 UInt32 mBusNumber; | |
114 AudioFileID mAudioFileID; | |
115 | |
116 AudioUnitInputCallback mInputCallback; | |
117 | |
118 AudioStreamBasicDescription mFileDescription; | |
119 | |
120 bool mConnected; | |
121 bool mIsAUNTUnit; | |
122 | |
123 AudioFileManager* mAudioFileManager; | |
124 AudioConverterRef mConverter; | |
125 | |
126 AudioFilePlayNotifier mNotifier; | |
127 void* mRefCon; | |
128 | |
129 int mStartFrame; | |
130 | |
131 #pragma mark __________ Private_Methods | |
132 | |
133 void OpenFile (const FSRef *inRef, SInt64& outFileSize); | |
134 }; | |
135 | |
136 #pragma mark __________ AudioFileManager | |
137 class AudioFileManager | |
138 { | |
139 public: | |
140 AudioFileManager (AudioFilePlayer& inParent, AudioFileID inFile) | |
141 : mParent (inParent), | |
142 mAudioFileID (inFile), | |
143 mFileBuffer (0), | |
144 mByteCounter (0) | |
145 {} | |
146 | |
147 virtual ~AudioFileManager(); | |
148 | |
149 | |
150 void Connect (AudioConverterRef inConverter) | |
151 { | |
152 mParentConverter = inConverter; | |
153 DoConnect(); | |
154 } | |
155 | |
156 // this method should NOT be called by an object of this class | |
157 // as it is called by the parent's Disconnect() method | |
158 virtual void Disconnect () {} | |
159 | |
160 const AudioFileID& GetFileID() const { return mAudioFileID; } | |
161 | |
162 const char* GetFileBuffer () { return mFileBuffer; } | |
163 | |
164 const AudioFilePlayer& GetParent () const { return mParent; } | |
165 | |
166 virtual void SetPosition (SInt64 pos) = 0; // seek/rewind in the file | |
167 | |
168 virtual int GetByteCounter () { return mByteCounter; } // return actual bytes streamed to audio hardware | |
169 | |
170 virtual void SetEndOfFile (SInt64 pos) = 0; // set the "EOF" (will behave just like it reached eof) | |
171 | |
172 protected: | |
173 AudioFilePlayer& mParent; | |
174 AudioConverterRef mParentConverter; | |
175 const AudioFileID mAudioFileID; | |
176 | |
177 char* mFileBuffer; | |
178 | |
179 OSStatus Render (AudioBuffer &ioData); | |
180 | |
181 int mByteCounter; | |
182 | |
183 virtual OSStatus GetFileData (void** inOutData, UInt32 *inOutDataSize) = 0; | |
184 | |
185 virtual void DoConnect () = 0; | |
186 | |
187 virtual void AfterRender () = 0; | |
188 | |
189 public: | |
190 static OSStatus FileInputProc (void *inRefCon, | |
191 AudioUnitRenderActionFlags inActionFlags, | |
192 const AudioTimeStamp *inTimeStamp, | |
193 UInt32 inBusNumber, | |
194 AudioBuffer *ioData); | |
195 static OSStatus ACInputProc (AudioConverterRef inAudioConverter, | |
196 UInt32* outDataSize, | |
197 void** outData, | |
198 void* inUserData); | |
199 }; | |
200 | |
201 | |
202 #pragma mark __________ AudioFileReaderThread | |
203 class AudioFileReaderThread | |
204 : public AudioFileManager | |
205 { | |
206 public: | |
207 const UInt32 mChunkSize; | |
208 SInt64 mFileLength; | |
209 SInt64 mReadFilePosition; | |
210 bool mWriteToFirstBuffer; | |
211 bool mFinishedReadingData; | |
212 | |
213 AudioFileReaderThread (AudioFilePlayer &inParent, | |
214 AudioFileID &inFile, | |
215 SInt64 inFileLength, | |
216 UInt32 inChunkSize); | |
217 | |
218 virtual void Disconnect (); | |
219 | |
220 virtual void SetPosition (SInt64 pos); // seek/rewind in the file | |
221 | |
222 virtual void SetEndOfFile (SInt64 pos); // set the "EOF" (will behave just like it reached eof) | |
223 | |
224 protected: | |
225 virtual void DoConnect (); | |
226 | |
227 virtual OSStatus GetFileData (void** inOutData, UInt32 *inOutDataSize); | |
228 | |
229 virtual void AfterRender (); | |
230 | |
231 private: | |
232 bool mReadFromFirstBuffer; | |
233 bool mLockUnsuccessful; | |
234 bool mIsEngaged; | |
235 | |
236 int mNumTimesAskedSinceFinished; | |
237 }; | |
238 | |
239 | |
240 #endif |