1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/uma.h"
55 #include <Carbon/Carbon.h>
57 //quicktime media layer only required for mac emulation on pc
63 #include <QuickTimeComponents.h>
65 #include <QuickTime/QuickTimeComponents.h>
68 //Time between timer calls
69 #define MOVIE_DELAY 100
71 static wxTimer
* lastSoundTimer
=NULL
;
72 static bool lastSoundIsPlaying
=false;
74 #if !defined(__LP64__)
75 #define USE_QUICKTIME 1
77 #define USE_QUICKTIME 0
81 // ------------------------------------------------------------------
82 // wxQTTimer - Handle Asyncronous Playing
83 // ------------------------------------------------------------------
84 class wxQTTimer
: public wxTimer
87 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
88 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
98 DisposeMovie(m_movie
);
101 //Note that ExitMovies() is not necessary, but
102 //the docs are fuzzy on whether or not TerminateQTML is
117 if (m_pbPlaying
&& !*m_pbPlaying
)
122 if(IsMovieDone(m_movie
))
129 GoToBeginningOfMovie(m_movie
);
134 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
138 Movie
& GetMovie() {return m_movie
;}
150 class wxSMTimer
: public wxTimer
153 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
154 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
161 *m_pbPlaying
= false;
162 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
168 if (m_pbPlaying
&& !*m_pbPlaying
)
175 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
178 //if the sound isn't playing anymore, see if it's looped,
179 //and if so play it again, otherwise close things up
180 if (stat
.scChannelBusy
== FALSE
)
184 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
197 void* GetChannel() {return m_pSndChannel
;}
208 // ------------------------------------------------------------------
210 // ------------------------------------------------------------------
212 //Determines whether version 4 of QT is installed
213 Boolean
wxIsQuickTime4Installed (void)
219 error
= Gestalt (gestaltQuickTime
, &result
);
220 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
226 inline bool wxInitQT ()
228 if (wxIsQuickTime4Installed())
233 if ((nError
= InitializeQTML(0)) != noErr
)
234 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
241 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
249 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
253 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
254 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
256 Create(sFileName
, isResource
);
259 wxSound::wxSound(int size
, const wxByte
* data
)
260 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
268 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
275 m_type
= wxSound_RESOURCE
;
279 wxMacStringToPascal( fileName
, lpSnd
) ;
281 m_sndname
= fileName
;
282 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
289 m_type
= wxSound_FILE
;
290 m_sndname
= fileName
;
296 bool wxSound::DoPlay(unsigned flags
) const
310 Handle myHandle
, dataRef
= nil
;
311 MovieImportComponent miComponent
;
312 Track targetTrack
= nil
;
313 TimeValue addedDuration
= 0;
316 ComponentResult result
;
318 myHandle
= NewHandleClear((Size
)m_waveLength
);
320 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
322 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
324 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
325 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
326 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
327 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
328 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
329 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
332 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
338 result
= MovieImportDataRef(miComponent
, dataRef
,
339 HandleDataHandlerSubType
, movie
,
342 movieImportCreateTrack
, &outFlags
);
346 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
349 SetMovieVolume(movie
, kFullVolume
);
350 GoToBeginningOfMovie(movie
);
352 DisposeHandle(myHandle
);
355 case wxSound_RESOURCE
:
357 SoundComponentData data
;
358 unsigned long numframes
, offset
;
360 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
361 //m_waveLength = numFrames * data.numChannels;
363 SndChannelPtr pSndChannel
;
364 SndNewChannel(&pSndChannel
, sampledSynth
,
366 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
368 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
371 if (flags
& wxSOUND_ASYNC
)
373 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
374 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
375 &lastSoundIsPlaying
);
376 lastSoundIsPlaying
= true;
378 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
381 SndDisposeChannel(pSndChannel
, TRUE
);
393 Handle dataRef
= NULL
;
396 err
= QTNewDataReferenceFromFullPathCFString(wxCFStringRef(m_sndname
,wxLocale::GetSystemEncoding()),
397 (UInt32
)kQTNativeDefaultPathStyle
, 0, &dataRef
, &dataRefType
);
399 wxASSERT(err
== noErr
);
401 if (NULL
!= dataRef
|| err
!= noErr
)
403 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
404 wxASSERT(err
== noErr
);
405 DisposeHandle(dataRef
);
411 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
419 }//end switch(m_type)
424 if (flags
& wxSOUND_ASYNC
)
426 //Start timer and play movie asyncronously
427 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
428 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
429 &lastSoundIsPlaying
);
430 lastSoundIsPlaying
= true;
431 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
435 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
437 //Play movie until it ends, then exit
438 //Note that due to quicktime caching this may not always
439 //work 100% correctly
440 while (!IsMovieDone(movie
))
441 MoviesTask(movie
, 1);
450 bool wxSound::IsPlaying()
452 return lastSoundIsPlaying
;
457 if (lastSoundIsPlaying
)
459 delete (wxTimer
*&) lastSoundTimer
;
460 lastSoundIsPlaying
= false;
461 lastSoundTimer
= NULL
;
465 void* wxSound::GetHandle()
468 if(m_type
== wxSound_RESOURCE
)
469 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
471 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();