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 inbetween timer calls
67 #define MOVIE_DELAY 100
69 // ------------------------------------------------------------------
70 // wxQTTimer - Handle Asyncronous Playing
71 // ------------------------------------------------------------------
72 class wxQTTimer
: public wxTimer
75 wxQTTimer(Movie movie
, bool bLoop
, bool& playing
) :
76 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(&playing
)
86 DisposeMovie(m_movie
);
89 //Note that ExitMovies() is not neccessary, but
90 //the docs are fuzzy on whether or not TerminateQTML is
105 if (m_pbPlaying
&& !*m_pbPlaying
)
110 if(IsMovieDone(m_movie
))
117 GoToBeginningOfMovie(m_movie
);
122 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
126 Movie
& GetMovie() {return m_movie
;}
138 class wxSMTimer
: public wxTimer
141 wxSMTimer(void* hSnd
, void* pSndChannel
, const bool& bLoop
, bool& playing
)
142 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(&playing
)
149 *m_pbPlaying
= false;
150 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
156 if (m_pbPlaying
&& !*m_pbPlaying
)
163 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
166 //if the sound isn't playing anymore, see if it's looped,
167 //and if so play it again, otherwise close things up
168 if (stat
.scChannelBusy
== FALSE
)
172 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
185 void* GetChannel() {return m_pSndChannel
;}
196 // ------------------------------------------------------------------
198 // ------------------------------------------------------------------
199 wxTimer
* lastSoundTimer
=NULL
;
200 bool lastSoundIsPlaying
=false;
202 //Determines whether version 4 of QT is installed
203 Boolean
wxIsQuickTime4Installed (void)
209 error
= Gestalt (gestaltQuickTime
, &result
);
210 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
216 inline bool wxInitQT ()
218 if (wxIsQuickTime4Installed())
223 if ((nError
= InitializeQTML(0)) != noErr
)
224 wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError
));
231 wxLogSysError("Quicktime is not installed, or Your Version of Quicktime is <= 4.");
237 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
241 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
242 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
244 Create(sFileName
, isResource
);
247 wxSound::wxSound(int size
, const wxByte
* data
)
248 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
254 if(lastSoundIsPlaying
)
256 if(m_type
== wxSound_RESOURCE
)
257 ((wxSMTimer
*)lastSoundTimer
)->m_pbPlaying
= NULL
;
259 ((wxQTTimer
*)lastSoundTimer
)->m_pbPlaying
= NULL
;
263 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
270 m_type
= wxSound_RESOURCE
;
274 wxMacStringToPascal( fileName
, lpSnd
) ;
277 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
284 m_type
= wxSound_FILE
;
285 m_sndname
= fileName
;
291 bool wxSound::DoPlay(unsigned flags
) const
303 Handle myHandle
, dataRef
= nil
;
304 MovieImportComponent miComponent
;
305 Track targetTrack
= nil
;
306 TimeValue addedDuration
= 0;
309 ComponentResult result
;
311 myHandle
= NewHandleClear((Size
)m_waveLength
);
313 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
315 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
317 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
318 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
319 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
320 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
321 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
322 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
325 wxLogSysError("wxSound - Location in memory does not contain valid data");
331 result
= MovieImportDataRef(miComponent
, dataRef
,
332 HandleDataHandlerSubType
, movie
,
335 movieImportCreateTrack
, &outFlags
);
339 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
342 SetMovieVolume(movie
, kFullVolume
);
343 GoToBeginningOfMovie(movie
);
345 DisposeHandle(myHandle
);
348 case wxSound_RESOURCE
:
350 SoundComponentData data
;
351 unsigned long numframes
, offset
;
353 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
354 //m_waveLength = numFrames * data.numChannels;
356 SndChannelPtr pSndChannel
;
357 SndNewChannel(&pSndChannel
, sampledSynth
,
359 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
361 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
364 if (flags
& wxSOUND_ASYNC
)
366 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
367 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
368 lastSoundIsPlaying
=true);
370 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
373 SndDisposeChannel(pSndChannel
, TRUE
);
387 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
390 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
392 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
393 m_sndname
.c_str(), nError
));
398 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
400 wxLogSysError(wxT("Quicktime couldn't open the file"));
405 short movieResID
= 0;
409 err
= NewMovieFromFile (
417 CloseMovieFile (movieResFile
);
422 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
430 }//end switch(m_type)
436 if (flags
& wxSOUND_SYNC
)
438 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), "Can't loop and play syncronously at the same time");
440 //Play movie until it ends, then exit
441 while (!IsMovieDone(movie
))
442 MoviesTask(movie
, 0);
448 //Start timer and play movie asyncronously
449 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) = new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,lastSoundIsPlaying
=true);
450 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
456 bool wxSound::IsPlaying()
458 return lastSoundIsPlaying
;
463 if(lastSoundIsPlaying
)
465 delete (wxTimer
*&) lastSoundTimer
;
466 lastSoundIsPlaying
= false;
470 void* wxSound::GetHandle()
472 if(m_type
== wxSound_RESOURCE
)
473 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
475 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();