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