1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
4 // Author: Ryan Norton, Stefan Csomor
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
) :
76 m_movie(movie
), m_bLoop(bLoop
)
88 DisposeMovie(m_movie
);
95 if(IsMovieDone(m_movie
))
102 GoToBeginningOfMovie(m_movie
);
107 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
111 Movie
& GetMovie() {return m_movie
;}
118 class wxSMTimer
: public wxTimer
121 wxSMTimer(void* hSnd
, void* pSndChannel
, const bool& bLoop
)
122 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
)
135 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
138 //if the sound isn't playing anymore, see if it's looped,
139 //and if so play it again, otherwise close things up
140 if (stat
.scChannelBusy
== FALSE
)
144 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
154 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
164 // ------------------------------------------------------------------
166 // ------------------------------------------------------------------
168 //Determines whether version 4 of QT is installed
169 Boolean
wxIsQuickTime4Installed (void)
175 error
= Gestalt (gestaltQuickTime
, &result
);
176 return (error
== noErr
) && (result
>= 4); //result >= 4 correct?
182 inline bool wxInitQT ()
184 if (wxIsQuickTime4Installed())
189 if ((nError
= InitializeQTML(0)) != noErr
)
190 wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError
));
197 wxLogSysError("Quicktime is not installed, or Your Version of Quicktime is <= 4.");
203 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
207 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
208 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
210 Create(sFileName
, isResource
);
213 wxSound::wxSound(int size
, const wxByte
* data
)
214 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
217 m_type
= wxSound_NONE
;
225 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
233 m_type
= wxSound_RESOURCE
;
237 wxMacStringToPascal( fileName
, lpSnd
) ;
240 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
247 m_type
= wxSound_FILE
;
248 m_sndname
= fileName
;
254 bool wxSound::DoPlay(unsigned flags
) const
256 wxASSERT(m_pTimer
== NULL
|| !((wxTimer
*)m_pTimer
)->IsRunning() );
264 Handle myHandle
, dataRef
= nil
;
265 MovieImportComponent miComponent
;
266 Track targetTrack
= nil
;
267 TimeValue addedDuration
= 0;
270 ComponentResult result
;
272 myHandle
= NewHandleClear((Size
)m_waveLength
);
274 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
276 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
278 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
279 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
280 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
281 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
282 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
283 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
286 wxLogSysError("wxSound - Location in memory does not contain valid data");
292 result
= MovieImportDataRef(miComponent
, dataRef
,
293 HandleDataHandlerSubType
, movie
,
296 movieImportCreateTrack
, &outFlags
);
300 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
303 SetMovieVolume(movie
, kFullVolume
);
304 GoToBeginningOfMovie(movie
);
307 case wxSound_RESOURCE
:
309 SoundComponentData data
;
310 unsigned long numframes
, offset
;
312 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
313 //m_waveLength = numFrames * data.numChannels;
315 SndChannelPtr pSndChannel
;
316 SndNewChannel(&pSndChannel
, sampledSynth
,
318 (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
320 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
323 if (flags
& wxSOUND_ASYNC
)
325 ((wxSMTimer
*&)m_pTimer
) = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0);
327 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
330 SndDisposeChannel(pSndChannel
, TRUE
);
341 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
344 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
346 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
347 m_sndname
.c_str(), nError
));
352 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
354 wxLogSysError(wxT("Quicktime couldn't open the file"));
359 short movieResID
= 0;
363 err
= NewMovieFromFile (
371 CloseMovieFile (movieResFile
);
376 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
384 }//end switch(m_type)
390 if (flags
& wxSOUND_SYNC
)
392 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), "Can't loop and play syncronously at the same time");
394 //Play movie until it ends, then exit
395 while (!IsMovieDone(movie
))
396 MoviesTask(movie
, 0);
402 //Start timer and play movie asyncronously
403 ((wxQTTimer
*&)m_pTimer
) = new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0);
404 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
410 void* wxSound::GetHandle()
412 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();
415 bool wxSound::FreeData()
417 if (m_pTimer
!= NULL
)
419 delete (wxQTTimer
*) m_pTimer
;
423 //Note that ExitMovies() is not neccessary, but
424 //the docs are fuzzy on whether or not TerminateQTML is