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"
56 #include "wx/osx/private.h"
59 #include <Carbon/Carbon.h>
61 //quicktime media layer only required for mac emulation on pc
67 #include <QuickTimeComponents.h>
69 #include <QuickTime/QuickTimeComponents.h>
72 //Time between timer calls
73 #define MOVIE_DELAY 100
75 static wxTimer
* lastSoundTimer
=NULL
;
76 static bool lastSoundIsPlaying
=false;
78 #if !defined(__LP64__)
79 #define USE_QUICKTIME 1
81 #define USE_QUICKTIME 0
85 // ------------------------------------------------------------------
86 // wxQTTimer - Handle Asyncronous Playing
87 // ------------------------------------------------------------------
88 class wxQTTimer
: public wxTimer
91 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
92 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
102 DisposeMovie(m_movie
);
105 //Note that ExitMovies() is not necessary, but
106 //the docs are fuzzy on whether or not TerminateQTML is
121 if (m_pbPlaying
&& !*m_pbPlaying
)
126 if(IsMovieDone(m_movie
))
133 GoToBeginningOfMovie(m_movie
);
138 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
142 Movie
& GetMovie() {return m_movie
;}
154 class wxSMTimer
: public wxTimer
157 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
158 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
165 *m_pbPlaying
= false;
166 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
172 if (m_pbPlaying
&& !*m_pbPlaying
)
179 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
182 //if the sound isn't playing anymore, see if it's looped,
183 //and if so play it again, otherwise close things up
184 if (stat
.scChannelBusy
== FALSE
)
188 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
201 void* GetChannel() {return m_pSndChannel
;}
212 // ------------------------------------------------------------------
214 // ------------------------------------------------------------------
216 //Determines whether version 4 of QT is installed
217 Boolean
wxIsQuickTime4Installed (void)
223 error
= Gestalt (gestaltQuickTime
, &result
);
224 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
230 inline bool wxInitQT ()
232 if (wxIsQuickTime4Installed())
237 if ((nError
= InitializeQTML(0)) != noErr
)
239 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
247 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
255 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
259 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
260 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
262 Create(sFileName
, isResource
);
265 wxSound::wxSound(int size
, const wxByte
* data
)
266 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
274 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
281 m_type
= wxSound_RESOURCE
;
285 wxMacStringToPascal( fileName
, lpSnd
) ;
287 m_sndname
= fileName
;
288 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
295 m_type
= wxSound_FILE
;
296 m_sndname
= fileName
;
302 bool wxSound::DoPlay(unsigned flags
) const
316 Handle myHandle
, dataRef
= nil
;
317 MovieImportComponent miComponent
;
318 Track targetTrack
= nil
;
319 TimeValue addedDuration
= 0;
322 ComponentResult result
;
324 myHandle
= NewHandleClear((Size
)m_waveLength
);
326 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
328 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
330 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
331 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
332 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
333 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
334 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
335 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
338 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
344 result
= MovieImportDataRef(miComponent
, dataRef
,
345 HandleDataHandlerSubType
, movie
,
348 movieImportCreateTrack
, &outFlags
);
352 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
355 SetMovieVolume(movie
, kFullVolume
);
356 GoToBeginningOfMovie(movie
);
358 DisposeHandle(myHandle
);
361 case wxSound_RESOURCE
:
363 SoundComponentData data
;
364 unsigned long numframes
, offset
;
366 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
367 //m_waveLength = numFrames * data.numChannels;
369 SndChannelPtr pSndChannel
;
370 SndNewChannel(&pSndChannel
, sampledSynth
,
372 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
374 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
377 if (flags
& wxSOUND_ASYNC
)
379 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
380 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
381 &lastSoundIsPlaying
);
382 lastSoundIsPlaying
= true;
384 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
387 SndDisposeChannel(pSndChannel
, TRUE
);
399 Handle dataRef
= NULL
;
402 err
= QTNewDataReferenceFromFullPathCFString(wxCFStringRef(m_sndname
,wxLocale::GetSystemEncoding()),
403 (UInt32
)kQTNativeDefaultPathStyle
, 0, &dataRef
, &dataRefType
);
405 wxASSERT(err
== noErr
);
407 if (NULL
!= dataRef
|| err
!= noErr
)
409 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
410 wxASSERT(err
== noErr
);
411 DisposeHandle(dataRef
);
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 //Note that due to quicktime caching this may not always
445 //work 100% correctly
446 while (!IsMovieDone(movie
))
447 MoviesTask(movie
, 1);
456 bool wxSound::IsPlaying()
458 return lastSoundIsPlaying
;
463 if (lastSoundIsPlaying
)
465 delete (wxTimer
*&) lastSoundTimer
;
466 lastSoundIsPlaying
= false;
467 lastSoundTimer
= NULL
;
471 void* wxSound::GetHandle()
474 if(m_type
== wxSound_RESOURCE
)
475 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
477 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();