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"
25 // Carbon QT Implementation Details -
28 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
30 // 3) MovieImportDataRef() //Pass Memory Location to this
32 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
36 // 2) Call OpenMovieFile
37 // 3) Call NewMovieFromFile
38 // 4) Call CloseMovieFile
40 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
44 #include "wx/mac/private.h"
49 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
51 #include <Carbon/Carbon.h>
59 //quicktime media layer only required for mac emulation on pc
64 #include <QuickTimeComponents.h>
66 //Time between timer calls
67 #define MOVIE_DELAY 100
69 static wxTimer
* lastSoundTimer
=NULL
;
70 static bool lastSoundIsPlaying
=false;
72 // ------------------------------------------------------------------
73 // wxQTTimer - Handle Asyncronous Playing
74 // ------------------------------------------------------------------
75 class wxQTTimer
: public wxTimer
78 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
79 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
89 DisposeMovie(m_movie
);
92 //Note that ExitMovies() is not neccessary, but
93 //the docs are fuzzy on whether or not TerminateQTML is
108 if (m_pbPlaying
&& !*m_pbPlaying
)
113 if(IsMovieDone(m_movie
))
120 GoToBeginningOfMovie(m_movie
);
125 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
129 Movie
& GetMovie() {return m_movie
;}
141 class wxSMTimer
: public wxTimer
144 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
145 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
152 *m_pbPlaying
= false;
153 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
159 if (m_pbPlaying
&& !*m_pbPlaying
)
166 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
169 //if the sound isn't playing anymore, see if it's looped,
170 //and if so play it again, otherwise close things up
171 if (stat
.scChannelBusy
== FALSE
)
175 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
188 void* GetChannel() {return m_pSndChannel
;}
199 // ------------------------------------------------------------------
201 // ------------------------------------------------------------------
203 //Determines whether version 4 of QT is installed
204 Boolean
wxIsQuickTime4Installed (void)
210 error
= Gestalt (gestaltQuickTime
, &result
);
211 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
217 inline bool wxInitQT ()
219 if (wxIsQuickTime4Installed())
224 if ((nError
= InitializeQTML(0)) != noErr
)
225 wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError
));
232 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
238 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
242 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
243 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
245 Create(sFileName
, isResource
);
248 wxSound::wxSound(int size
, const wxByte
* data
)
249 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
257 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
264 m_type
= wxSound_RESOURCE
;
268 wxMacStringToPascal( fileName
, lpSnd
) ;
270 m_sndname
= fileName
;
271 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
278 m_type
= wxSound_FILE
;
279 m_sndname
= fileName
;
285 bool wxSound::DoPlay(unsigned flags
) const
297 Handle myHandle
, dataRef
= nil
;
298 MovieImportComponent miComponent
;
299 Track targetTrack
= nil
;
300 TimeValue addedDuration
= 0;
303 ComponentResult result
;
305 myHandle
= NewHandleClear((Size
)m_waveLength
);
307 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
309 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
311 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
312 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
313 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
314 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
315 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
316 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
319 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
325 result
= MovieImportDataRef(miComponent
, dataRef
,
326 HandleDataHandlerSubType
, movie
,
329 movieImportCreateTrack
, &outFlags
);
333 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
336 SetMovieVolume(movie
, kFullVolume
);
337 GoToBeginningOfMovie(movie
);
339 DisposeHandle(myHandle
);
342 case wxSound_RESOURCE
:
344 SoundComponentData data
;
345 unsigned long numframes
, offset
;
347 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
348 //m_waveLength = numFrames * data.numChannels;
350 SndChannelPtr pSndChannel
;
351 SndNewChannel(&pSndChannel
, sampledSynth
,
353 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
355 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
358 if (flags
& wxSOUND_ASYNC
)
360 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
361 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
362 &lastSoundIsPlaying
);
363 lastSoundIsPlaying
= true;
365 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
368 SndDisposeChannel(pSndChannel
, TRUE
);
382 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
385 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
387 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
388 m_sndname
.c_str(), nError
));
393 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
395 wxLogSysError(wxT("Quicktime couldn't open the file"));
400 short movieResID
= 0;
404 err
= NewMovieFromFile (
412 CloseMovieFile (movieResFile
);
417 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
425 }//end switch(m_type)
430 if (flags
& wxSOUND_ASYNC
)
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
);
441 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
443 //Play movie until it ends, then exit
444 while (!IsMovieDone(movie
))
445 MoviesTask(movie
, 0);
453 bool wxSound::IsPlaying()
455 return lastSoundIsPlaying
;
460 if (lastSoundIsPlaying
)
462 delete (wxTimer
*&) lastSoundTimer
;
463 lastSoundIsPlaying
= false;
464 lastSoundTimer
= NULL
;
468 void* wxSound::GetHandle()
470 if(m_type
== wxSound_RESOURCE
)
471 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
473 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();