1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
8 // Copyright: (c) Ryan Norton, Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sound.h"
16 #include "wx/object.h"
17 #include "wx/string.h"
26 // Carbon QT Implementation Details -
29 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
31 // 3) MovieImportDataRef() //Pass Memory Location to this
33 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
37 // 2) Call OpenMovieFile
38 // 3) Call NewMovieFromFile
39 // 4) Call CloseMovieFile
41 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
45 #include "wx/mac/uma.h"
50 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
52 #include <Carbon/Carbon.h>
60 //quicktime media layer only required for mac emulation on pc
65 #include <QuickTimeComponents.h>
67 //Time between timer calls
68 #define MOVIE_DELAY 100
70 static wxTimer
* lastSoundTimer
=NULL
;
71 static bool lastSoundIsPlaying
=false;
73 // ------------------------------------------------------------------
74 // wxQTTimer - Handle Asyncronous Playing
75 // ------------------------------------------------------------------
76 class wxQTTimer
: public wxTimer
79 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
80 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
90 DisposeMovie(m_movie
);
93 //Note that ExitMovies() is not neccessary, but
94 //the docs are fuzzy on whether or not TerminateQTML is
109 if (m_pbPlaying
&& !*m_pbPlaying
)
114 if(IsMovieDone(m_movie
))
121 GoToBeginningOfMovie(m_movie
);
126 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
130 Movie
& GetMovie() {return m_movie
;}
142 class wxSMTimer
: public wxTimer
145 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
146 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
153 *m_pbPlaying
= false;
154 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
160 if (m_pbPlaying
&& !*m_pbPlaying
)
167 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
170 //if the sound isn't playing anymore, see if it's looped,
171 //and if so play it again, otherwise close things up
172 if (stat
.scChannelBusy
== FALSE
)
176 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
189 void* GetChannel() {return m_pSndChannel
;}
200 // ------------------------------------------------------------------
202 // ------------------------------------------------------------------
204 //Determines whether version 4 of QT is installed
205 Boolean
wxIsQuickTime4Installed (void)
211 error
= Gestalt (gestaltQuickTime
, &result
);
212 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
218 inline bool wxInitQT ()
220 if (wxIsQuickTime4Installed())
225 if ((nError
= InitializeQTML(0)) != noErr
)
226 wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError
));
233 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
239 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
243 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
244 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
246 Create(sFileName
, isResource
);
249 wxSound::wxSound(int size
, const wxByte
* data
)
250 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
258 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
265 m_type
= wxSound_RESOURCE
;
269 wxMacStringToPascal( fileName
, lpSnd
) ;
271 m_sndname
= fileName
;
272 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
279 m_type
= wxSound_FILE
;
280 m_sndname
= fileName
;
286 bool wxSound::DoPlay(unsigned flags
) const
298 Handle myHandle
, dataRef
= nil
;
299 MovieImportComponent miComponent
;
300 Track targetTrack
= nil
;
301 TimeValue addedDuration
= 0;
304 ComponentResult result
;
306 myHandle
= NewHandleClear((Size
)m_waveLength
);
308 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
310 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
312 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
313 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
314 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
315 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
316 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
317 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
320 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
326 result
= MovieImportDataRef(miComponent
, dataRef
,
327 HandleDataHandlerSubType
, movie
,
330 movieImportCreateTrack
, &outFlags
);
334 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
337 SetMovieVolume(movie
, kFullVolume
);
338 GoToBeginningOfMovie(movie
);
340 DisposeHandle(myHandle
);
343 case wxSound_RESOURCE
:
345 SoundComponentData data
;
346 unsigned long numframes
, offset
;
348 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
349 //m_waveLength = numFrames * data.numChannels;
351 SndChannelPtr pSndChannel
;
352 SndNewChannel(&pSndChannel
, sampledSynth
,
354 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
356 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
359 if (flags
& wxSOUND_ASYNC
)
361 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
362 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
363 &lastSoundIsPlaying
);
364 lastSoundIsPlaying
= true;
366 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
369 SndDisposeChannel(pSndChannel
, TRUE
);
380 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
381 if ( UMAGetSystemVersion() >= 0x1030 )
383 Handle dataRef
= NULL
;
386 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
387 kQTNativeDefaultPathStyle
, 0, &dataRef
, &dataRefType
);
391 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
392 DisposeHandle(dataRef
);
401 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
404 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
406 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
407 m_sndname
.c_str(), nError
));
411 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
413 wxLogSysError(wxT("Quicktime couldn't open the file"));
416 short movieResID
= 0;
419 err
= NewMovieFromFile (
427 CloseMovieFile (movieResFile
);
433 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
441 }//end switch(m_type)
446 if (flags
& wxSOUND_ASYNC
)
448 //Start timer and play movie asyncronously
449 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
450 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
451 &lastSoundIsPlaying
);
452 lastSoundIsPlaying
= true;
453 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
457 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
459 //Play movie until it ends, then exit
460 while (!IsMovieDone(movie
))
461 MoviesTask(movie
, 0);
469 bool wxSound::IsPlaying()
471 return lastSoundIsPlaying
;
476 if (lastSoundIsPlaying
)
478 delete (wxTimer
*&) lastSoundTimer
;
479 lastSoundIsPlaying
= false;
480 lastSoundTimer
= NULL
;
484 void* wxSound::GetHandle()
486 if(m_type
== wxSound_RESOURCE
)
487 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
489 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();