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