1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/sound.cpp
3 // Purpose: wxSound class implementation: optional
5 // Modified by: Stefan Csomor
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
18 #include "wx/object.h"
21 #include "wx/string.h"
28 // Carbon QT Implementation Details -
31 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
33 // 3) MovieImportDataRef() //Pass Memory Location to this
35 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
39 // 2) Call OpenMovieFile
40 // 3) Call NewMovieFromFile
41 // 4) Call CloseMovieFile
43 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
47 #include "wx/mac/uma.h"
54 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
56 #include <Carbon/Carbon.h>
64 //quicktime media layer only required for mac emulation on pc
70 #include <QuickTimeComponents.h>
72 #include <QuickTime/QuickTimeComponents.h>
75 //Time between timer calls
76 #define MOVIE_DELAY 100
78 static wxTimer
* lastSoundTimer
=NULL
;
79 static bool lastSoundIsPlaying
=false;
81 // ------------------------------------------------------------------
82 // wxQTTimer - Handle Asyncronous Playing
83 // ------------------------------------------------------------------
84 class wxQTTimer
: public wxTimer
87 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
88 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
98 DisposeMovie(m_movie
);
101 //Note that ExitMovies() is not necessary, but
102 //the docs are fuzzy on whether or not TerminateQTML is
117 if (m_pbPlaying
&& !*m_pbPlaying
)
122 if(IsMovieDone(m_movie
))
129 GoToBeginningOfMovie(m_movie
);
134 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
138 Movie
& GetMovie() {return m_movie
;}
150 class wxSMTimer
: public wxTimer
153 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
154 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
161 *m_pbPlaying
= false;
162 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
168 if (m_pbPlaying
&& !*m_pbPlaying
)
175 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
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
)
184 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
197 void* GetChannel() {return m_pSndChannel
;}
208 // ------------------------------------------------------------------
210 // ------------------------------------------------------------------
212 //Determines whether version 4 of QT is installed
213 Boolean
wxIsQuickTime4Installed (void)
219 error
= Gestalt (gestaltQuickTime
, &result
);
220 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
226 inline bool wxInitQT ()
228 if (wxIsQuickTime4Installed())
233 if ((nError
= InitializeQTML(0)) != noErr
)
234 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
241 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
247 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
251 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
252 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
254 Create(sFileName
, isResource
);
257 wxSound::wxSound(int size
, const wxByte
* data
)
258 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
266 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
273 m_type
= wxSound_RESOURCE
;
277 wxMacStringToPascal( fileName
, lpSnd
) ;
279 m_sndname
= fileName
;
280 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
287 m_type
= wxSound_FILE
;
288 m_sndname
= fileName
;
294 bool wxSound::DoPlay(unsigned flags
) const
306 Handle myHandle
, dataRef
= nil
;
307 MovieImportComponent miComponent
;
308 Track targetTrack
= nil
;
309 TimeValue addedDuration
= 0;
312 ComponentResult result
;
314 myHandle
= NewHandleClear((Size
)m_waveLength
);
316 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
318 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
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
);
328 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
334 result
= MovieImportDataRef(miComponent
, dataRef
,
335 HandleDataHandlerSubType
, movie
,
338 movieImportCreateTrack
, &outFlags
);
342 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
345 SetMovieVolume(movie
, kFullVolume
);
346 GoToBeginningOfMovie(movie
);
348 DisposeHandle(myHandle
);
351 case wxSound_RESOURCE
:
353 SoundComponentData data
;
354 unsigned long numframes
, offset
;
356 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
357 //m_waveLength = numFrames * data.numChannels;
359 SndChannelPtr pSndChannel
;
360 SndNewChannel(&pSndChannel
, sampledSynth
,
362 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
364 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
367 if (flags
& wxSOUND_ASYNC
)
369 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
370 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
371 &lastSoundIsPlaying
);
372 lastSoundIsPlaying
= true;
374 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
377 SndDisposeChannel(pSndChannel
, TRUE
);
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 )
402 Handle dataRef
= NULL
;
405 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
406 //FIXME: Why does this have to be casted?
407 (unsigned int)kQTNativeDefaultPathStyle
,
409 0, &dataRef
, &dataRefType
);
411 wxASSERT(err
== noErr
);
413 if (NULL
!= dataRef
|| err
!= noErr
)
415 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
416 wxASSERT(err
== noErr
);
417 DisposeHandle(dataRef
);
426 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
429 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
432 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
433 m_sndname.c_str(), nError));
438 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
440 wxLogSysError(wxT("Quicktime couldn't open the file"));
443 short movieResID
= 0;
446 err
= NewMovieFromFile (
454 CloseMovieFile (movieResFile
);
460 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
468 }//end switch(m_type)
473 if (flags
& wxSOUND_ASYNC
)
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
);
484 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
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);
498 bool wxSound::IsPlaying()
500 return lastSoundIsPlaying
;
505 if (lastSoundIsPlaying
)
507 delete (wxTimer
*&) lastSoundTimer
;
508 lastSoundIsPlaying
= false;
509 lastSoundTimer
= NULL
;
513 void* wxSound::GetHandle()
515 if(m_type
== wxSound_RESOURCE
)
516 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
518 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();