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"
20 #include "wx/object.h"
21 #include "wx/string.h"
29 // Carbon QT Implementation Details -
32 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
34 // 3) MovieImportDataRef() //Pass Memory Location to this
36 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
39 // 1) Path as CFString
40 // 2) Call QTNewDataReferenceFromFullPathCFString
41 // 3) Call NewMovieFromDataRef
42 // 4) Call CloseMovieFile
44 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
48 #include "wx/mac/uma.h"
55 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
57 #include <Carbon/Carbon.h>
65 //quicktime media layer only required for mac emulation on pc
71 #include <QuickTimeComponents.h>
73 #include <QuickTime/QuickTimeComponents.h>
76 //Time between timer calls
77 #define MOVIE_DELAY 100
79 static wxTimer
* lastSoundTimer
=NULL
;
80 static bool lastSoundIsPlaying
=false;
82 #if !defined(__DARWIN__) || !defined(__LP64__)
83 #define USE_QUICKTIME 1
85 #define USE_QUICKTIME 0
89 // ------------------------------------------------------------------
90 // wxQTTimer - Handle Asyncronous Playing
91 // ------------------------------------------------------------------
92 class wxQTTimer
: public wxTimer
95 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
96 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
103 *m_pbPlaying
= false;
106 DisposeMovie(m_movie
);
109 //Note that ExitMovies() is not necessary, but
110 //the docs are fuzzy on whether or not TerminateQTML is
125 if (m_pbPlaying
&& !*m_pbPlaying
)
130 if(IsMovieDone(m_movie
))
137 GoToBeginningOfMovie(m_movie
);
142 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
146 Movie
& GetMovie() {return m_movie
;}
158 class wxSMTimer
: public wxTimer
161 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
162 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
169 *m_pbPlaying
= false;
170 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
176 if (m_pbPlaying
&& !*m_pbPlaying
)
183 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
186 //if the sound isn't playing anymore, see if it's looped,
187 //and if so play it again, otherwise close things up
188 if (stat
.scChannelBusy
== FALSE
)
192 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
205 void* GetChannel() {return m_pSndChannel
;}
216 // ------------------------------------------------------------------
218 // ------------------------------------------------------------------
220 //Determines whether version 4 of QT is installed
221 Boolean
wxIsQuickTime4Installed (void)
227 error
= Gestalt (gestaltQuickTime
, &result
);
228 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
234 inline bool wxInitQT ()
236 if (wxIsQuickTime4Installed())
241 if ((nError
= InitializeQTML(0)) != noErr
)
242 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
249 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
257 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
261 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
262 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
264 Create(sFileName
, isResource
);
267 wxSound::wxSound(int size
, const wxByte
* data
)
268 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
276 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
283 m_type
= wxSound_RESOURCE
;
287 wxMacStringToPascal( fileName
, lpSnd
) ;
289 m_sndname
= fileName
;
290 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
297 m_type
= wxSound_FILE
;
298 m_sndname
= fileName
;
304 bool wxSound::DoPlay(unsigned flags
) const
318 Handle myHandle
, dataRef
= nil
;
319 MovieImportComponent miComponent
;
320 Track targetTrack
= nil
;
321 TimeValue addedDuration
= 0;
324 ComponentResult result
;
326 myHandle
= NewHandleClear((Size
)m_waveLength
);
328 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
330 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
332 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
333 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
334 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
335 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
336 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
337 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
340 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
346 result
= MovieImportDataRef(miComponent
, dataRef
,
347 HandleDataHandlerSubType
, movie
,
350 movieImportCreateTrack
, &outFlags
);
354 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
357 SetMovieVolume(movie
, kFullVolume
);
358 GoToBeginningOfMovie(movie
);
360 DisposeHandle(myHandle
);
363 case wxSound_RESOURCE
:
365 SoundComponentData data
;
366 unsigned long numframes
, offset
;
368 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
369 //m_waveLength = numFrames * data.numChannels;
371 SndChannelPtr pSndChannel
;
372 SndNewChannel(&pSndChannel
, sampledSynth
,
374 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
376 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
379 if (flags
& wxSOUND_ASYNC
)
381 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
382 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
383 &lastSoundIsPlaying
);
384 lastSoundIsPlaying
= true;
386 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
389 SndDisposeChannel(pSndChannel
, TRUE
);
401 Handle dataRef
= NULL
;
404 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
405 (UInt32
)kQTNativeDefaultPathStyle
, 0, &dataRef
, &dataRefType
);
407 wxASSERT(err
== noErr
);
409 if (NULL
!= dataRef
|| err
!= noErr
)
411 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
412 wxASSERT(err
== noErr
);
413 DisposeHandle(dataRef
);
419 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
427 }//end switch(m_type)
432 if (flags
& wxSOUND_ASYNC
)
434 //Start timer and play movie asyncronously
435 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
436 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
437 &lastSoundIsPlaying
);
438 lastSoundIsPlaying
= true;
439 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
443 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
445 //Play movie until it ends, then exit
446 //Note that due to quicktime caching this may not always
447 //work 100% correctly
448 while (!IsMovieDone(movie
))
449 MoviesTask(movie
, 1);
458 bool wxSound::IsPlaying()
460 return lastSoundIsPlaying
;
465 if (lastSoundIsPlaying
)
467 delete (wxTimer
*&) lastSoundTimer
;
468 lastSoundIsPlaying
= false;
469 lastSoundTimer
= NULL
;
473 void* wxSound::GetHandle()
476 if(m_type
== wxSound_RESOURCE
)
477 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
479 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();