]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/sound.cpp
listctrl header cleanup
[wxWidgets.git] / src / mac / carbon / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/sound.cpp
3 // Purpose: wxSound class implementation: optional
4 // Author: Ryan Norton
5 // Modified by: Stefan Csomor
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_SOUND
16
17 #include "wx/sound.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/timer.h"
25 #endif
26
27 #include "wx/file.h"
28
29 // Carbon QT Implementation Details -
30 //
31 // Memory:
32 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
33 // 2) NewMovie(0);
34 // 3) MovieImportDataRef() //Pass Memory Location to this
35 // 4) PlayMovie();
36 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
37 //
38 // File:
39 // 1) Path as CFString
40 // 2) Call QTNewDataReferenceFromFullPathCFString
41 // 3) Call NewMovieFromDataRef
42 // 4) Call CloseMovieFile
43 // 4) PlayMovie();
44 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
45 //
46
47 #ifdef __WXMAC__
48 #include "wx/mac/uma.h"
49 #ifndef __DARWIN__
50 #include <Movies.h>
51 #include <Gestalt.h>
52 #endif
53 #endif
54
55 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
56 #ifdef __APPLE_CC__
57 #include <Carbon/Carbon.h>
58 #else
59 #include <Carbon.h>
60 #endif
61 #else
62 #include <Sound.h>
63 #endif
64
65 //quicktime media layer only required for mac emulation on pc
66 #ifndef __WXMAC__
67 #include <qtml.h>
68 #endif
69
70 #ifndef __DARWIN__
71 #include <QuickTimeComponents.h>
72 #else
73 #include <QuickTime/QuickTimeComponents.h>
74 #endif
75
76 //Time between timer calls
77 #define MOVIE_DELAY 100
78
79 static wxTimer* lastSoundTimer=NULL;
80 static bool lastSoundIsPlaying=false;
81
82 // ------------------------------------------------------------------
83 // wxQTTimer - Handle Asyncronous Playing
84 // ------------------------------------------------------------------
85 class wxQTTimer : public wxTimer
86 {
87 public:
88 wxQTTimer(Movie movie, bool bLoop, bool* playing) :
89 m_movie(movie), m_bLoop(bLoop), m_pbPlaying(playing)
90 {
91 }
92
93 virtual ~wxQTTimer()
94 {
95 if(m_pbPlaying)
96 *m_pbPlaying = false;
97
98 StopMovie(m_movie);
99 DisposeMovie(m_movie);
100 Stop();
101
102 //Note that ExitMovies() is not necessary, but
103 //the docs are fuzzy on whether or not TerminateQTML is
104 ExitMovies();
105
106 #ifndef __WXMAC__
107 TerminateQTML();
108 #endif
109 }
110
111 void Shutdown()
112 {
113 delete this;
114 }
115
116 void Notify()
117 {
118 if (m_pbPlaying && !*m_pbPlaying)
119 {
120 Shutdown();
121 }
122
123 if(IsMovieDone(m_movie))
124 {
125 if (!m_bLoop)
126 Shutdown();
127 else
128 {
129 StopMovie(m_movie);
130 GoToBeginningOfMovie(m_movie);
131 StartMovie(m_movie);
132 }
133 }
134 else
135 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
136 }
137
138
139 Movie& GetMovie() {return m_movie;}
140
141 protected:
142 Movie m_movie;
143 bool m_bLoop;
144
145 public:
146 bool* m_pbPlaying;
147
148 };
149
150
151 class wxSMTimer : public wxTimer
152 {
153 public:
154 wxSMTimer(void* hSnd, void* pSndChannel, bool bLoop, bool* playing)
155 : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(playing)
156 {
157 }
158
159 virtual ~wxSMTimer()
160 {
161 if(m_pbPlaying)
162 *m_pbPlaying = false;
163 SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
164 Stop();
165 }
166
167 void Notify()
168 {
169 if (m_pbPlaying && !*m_pbPlaying)
170 {
171 Shutdown();
172 }
173
174 SCStatus stat;
175
176 if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
177 Shutdown();
178
179 //if the sound isn't playing anymore, see if it's looped,
180 //and if so play it again, otherwise close things up
181 if (stat.scChannelBusy == FALSE)
182 {
183 if (m_bLoop)
184 {
185 if(SndPlay((SndChannelPtr)m_pSndChannel, (SndListHandle) m_hSnd, true) != noErr)
186 Shutdown();
187 }
188 else
189 Shutdown();
190 }
191 }
192
193 void Shutdown()
194 {
195 delete this;
196 }
197
198 void* GetChannel() {return m_pSndChannel;}
199
200 protected:
201 void* m_hSnd;
202 void* m_pSndChannel;
203 bool m_bLoop;
204
205 public:
206 bool* m_pbPlaying;
207 };
208
209 // ------------------------------------------------------------------
210 // wxSound
211 // ------------------------------------------------------------------
212
213 //Determines whether version 4 of QT is installed
214 Boolean wxIsQuickTime4Installed (void)
215 {
216 #ifdef __WXMAC__
217 short error;
218 long result;
219
220 error = Gestalt (gestaltQuickTime, &result);
221 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
222 #else
223 return true;
224 #endif
225 }
226
227 inline bool wxInitQT ()
228 {
229 if (wxIsQuickTime4Installed())
230 {
231 #ifndef __WXMAC__
232 int nError;
233 //-2093 no dll
234 if ((nError = InitializeQTML(0)) != noErr)
235 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
236 #endif
237 EnterMovies();
238 return true;
239 }
240 else
241 {
242 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
243 return false;
244 }
245 }
246
247 wxSound::wxSound()
248 : m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
249 {
250 }
251
252 wxSound::wxSound(const wxString& sFileName, bool isResource)
253 : m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
254 {
255 Create(sFileName, isResource);
256 }
257
258 wxSound::wxSound(int size, const wxByte* data)
259 : m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
260 {
261 }
262
263 wxSound::~wxSound()
264 {
265 }
266
267 bool wxSound::Create(const wxString& fileName, bool isResource)
268 {
269 Stop();
270
271 if (isResource)
272 {
273 #ifdef __WXMAC__
274 m_type = wxSound_RESOURCE;
275
276 Str255 lpSnd ;
277
278 wxMacStringToPascal( fileName , lpSnd ) ;
279
280 m_sndname = fileName;
281 m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
282 #else
283 return false;
284 #endif
285 }
286 else
287 {
288 m_type = wxSound_FILE;
289 m_sndname = fileName;
290 }
291
292 return true;
293 }
294
295 bool wxSound::DoPlay(unsigned flags) const
296 {
297 Stop();
298
299 Movie movie;
300
301 switch(m_type)
302 {
303 case wxSound_MEMORY:
304 {
305 if (!wxInitQT())
306 return false;
307 Handle myHandle, dataRef = nil;
308 MovieImportComponent miComponent;
309 Track targetTrack = nil;
310 TimeValue addedDuration = 0;
311 long outFlags = 0;
312 OSErr err;
313 ComponentResult result;
314
315 myHandle = NewHandleClear((Size)m_waveLength);
316
317 BlockMove(m_hSnd, *myHandle, m_waveLength);
318
319 err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
320
321 if (memcmp(&m_hSnd[8], "WAVE", 4) == 0)
322 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
323 else if (memcmp(&m_hSnd[8], "AIFF", 4) == 0)
324 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFF);
325 else if (memcmp(&m_hSnd[8], "AIFC", 4) == 0)
326 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFC);
327 else
328 {
329 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
330 return false;
331 }
332
333 movie = NewMovie(0);
334
335 result = MovieImportDataRef(miComponent, dataRef,
336 HandleDataHandlerSubType, movie,
337 nil, &targetTrack,
338 nil, &addedDuration,
339 movieImportCreateTrack, &outFlags);
340
341 if (result != noErr)
342 {
343 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
344 }
345
346 SetMovieVolume(movie, kFullVolume);
347 GoToBeginningOfMovie(movie);
348
349 DisposeHandle(myHandle);
350 }
351 break;
352 case wxSound_RESOURCE:
353 {
354 SoundComponentData data;
355 unsigned long numframes, offset;
356
357 ParseSndHeader((SndListHandle)m_hSnd, &data, &numframes, &offset);
358 //m_waveLength = numFrames * data.numChannels;
359
360 SndChannelPtr pSndChannel;
361 SndNewChannel(&pSndChannel, sampledSynth,
362 initNoInterp
363 + (data.numChannels == 1 ? initMono : initStereo), NULL);
364
365 if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
366 return false;
367
368 if (flags & wxSOUND_ASYNC)
369 {
370 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
371 = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
372 &lastSoundIsPlaying);
373 lastSoundIsPlaying = true;
374
375 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
376 }
377 else
378 SndDisposeChannel(pSndChannel, TRUE);
379
380 return true;
381 }
382 break;
383 case wxSound_FILE:
384 {
385 if (!wxInitQT())
386 return false;
387
388 OSErr err = noErr ;
389
390 Handle dataRef = NULL;
391 OSType dataRefType;
392
393 err = QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname,wxLocale::GetSystemEncoding()),
394 (UInt32)kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
395
396 wxASSERT(err == noErr);
397
398 if (NULL != dataRef || err != noErr)
399 {
400 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
401 wxASSERT(err == noErr);
402 DisposeHandle(dataRef);
403 }
404
405 if (err != noErr)
406 {
407 wxLogSysError(
408 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
409 );
410 return false;
411 }
412 }
413 break;
414 default:
415 return false;
416 }//end switch(m_type)
417
418 //Start the movie!
419 StartMovie(movie);
420
421 if (flags & wxSOUND_ASYNC)
422 {
423 //Start timer and play movie asyncronously
424 lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
425 new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
426 &lastSoundIsPlaying);
427 lastSoundIsPlaying = true;
428 ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
429 }
430 else
431 {
432 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
433
434 //Play movie until it ends, then exit
435 //Note that due to quicktime caching this may not always
436 //work 100% correctly
437 while (!IsMovieDone(movie))
438 MoviesTask(movie, 1);
439
440 DisposeMovie(movie);
441 }
442
443 return true;
444 }
445
446 bool wxSound::IsPlaying()
447 {
448 return lastSoundIsPlaying;
449 }
450
451 void wxSound::Stop()
452 {
453 if (lastSoundIsPlaying)
454 {
455 delete (wxTimer*&) lastSoundTimer;
456 lastSoundIsPlaying = false;
457 lastSoundTimer = NULL;
458 }
459 }
460
461 void* wxSound::GetHandle()
462 {
463 if(m_type == wxSound_RESOURCE)
464 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
465
466 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
467 }
468
469 #endif //wxUSE_SOUND