]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/sound.cpp
add wxTLW::UseNativeDecorations[ByDefault]() to allow the programmer to control wheth...
[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) Obtain FSSpec
40 // 2) Call OpenMovieFile
41 // 3) Call NewMovieFromFile
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 //NB: RN: Stefan - I think the 10.3 path functions are broken if kQTNativeDefaultPathStyle is
390 //going to trigger a warning every time it is used - where its _supposed to be used_!!
391 //(kQTNativePathStyle is negative but the function argument is unsigned!)
392 //../src/mac/carbon/sound.cpp: In member function `virtual bool
393 // wxSound::DoPlay(unsigned int) const':
394 //../src/mac/carbon/sound.cpp:387: warning: passing negative value `
395 // kQTNativeDefaultPathStyle' for argument passing 2 of `OSErr
396 // QTNewDataReferenceFromFullPathCFString(const __CFString*, long unsigned int,
397 // long unsigned int, char***, OSType*)'
398 //../src/mac/carbon/sound.cpp:387: warning: argument of negative value `
399 // kQTNativeDefaultPathStyle' to `long unsigned int'
400 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
401 if ( UMAGetSystemVersion() >= 0x1030 )
402 {
403 Handle dataRef = NULL;
404 OSType dataRefType;
405
406 err = QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname,wxLocale::GetSystemEncoding()),
407 //FIXME: Why does this have to be casted?
408 (unsigned int)kQTNativeDefaultPathStyle,
409 //FIXME: End
410 0, &dataRef, &dataRefType);
411
412 wxASSERT(err == noErr);
413
414 if (NULL != dataRef || err != noErr)
415 {
416 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
417 wxASSERT(err == noErr);
418 DisposeHandle(dataRef);
419 }
420 }
421 else
422 #endif
423 {
424 short movieResFile;
425 FSSpec sfFile;
426 #ifdef __WXMAC__
427 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
428 #else
429 int nError;
430 if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
431 {
432 /*
433 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
434 m_sndname.c_str(), nError));
435 */
436 return false;
437 }
438 #endif
439 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
440 {
441 wxLogSysError(wxT("Quicktime couldn't open the file"));
442 return false;
443 }
444 short movieResID = 0;
445 Str255 movieName;
446
447 err = NewMovieFromFile (
448 &movie,
449 movieResFile,
450 &movieResID,
451 movieName,
452 newMovieActive,
453 NULL); //wasChanged
454
455 CloseMovieFile (movieResFile);
456 }
457
458 if (err != noErr)
459 {
460 wxLogSysError(
461 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
462 );
463 return false;
464 }
465 }
466 break;
467 default:
468 return false;
469 }//end switch(m_type)
470
471 //Start the movie!
472 StartMovie(movie);
473
474 if (flags & wxSOUND_ASYNC)
475 {
476 //Start timer and play movie asyncronously
477 lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
478 new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
479 &lastSoundIsPlaying);
480 lastSoundIsPlaying = true;
481 ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
482 }
483 else
484 {
485 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
486
487 //Play movie until it ends, then exit
488 //Note that due to quicktime caching this may not always
489 //work 100% correctly
490 while (!IsMovieDone(movie))
491 MoviesTask(movie, 1);
492
493 DisposeMovie(movie);
494 }
495
496 return true;
497 }
498
499 bool wxSound::IsPlaying()
500 {
501 return lastSoundIsPlaying;
502 }
503
504 void wxSound::Stop()
505 {
506 if (lastSoundIsPlaying)
507 {
508 delete (wxTimer*&) lastSoundTimer;
509 lastSoundIsPlaying = false;
510 lastSoundTimer = NULL;
511 }
512 }
513
514 void* wxSound::GetHandle()
515 {
516 if(m_type == wxSound_RESOURCE)
517 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
518
519 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
520 }
521
522 #endif //wxUSE_SOUND