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
40 // 2) Call OpenMovieFile
41 // 3) Call NewMovieFromFile
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 // ------------------------------------------------------------------
83 // wxQTTimer - Handle Asyncronous Playing
84 // ------------------------------------------------------------------
85 class wxQTTimer
: public wxTimer
88 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
89 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
99 DisposeMovie(m_movie
);
102 //Note that ExitMovies() is not necessary, but
103 //the docs are fuzzy on whether or not TerminateQTML is
118 if (m_pbPlaying
&& !*m_pbPlaying
)
123 if(IsMovieDone(m_movie
))
130 GoToBeginningOfMovie(m_movie
);
135 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
139 Movie
& GetMovie() {return m_movie
;}
151 class wxSMTimer
: public wxTimer
154 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
155 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
162 *m_pbPlaying
= false;
163 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
169 if (m_pbPlaying
&& !*m_pbPlaying
)
176 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
179 //if the sound isn't playing anymore, see if it's looped,
180 //and if so play it again, otherwise close things up
181 if (stat
.scChannelBusy
== FALSE
)
185 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
198 void* GetChannel() {return m_pSndChannel
;}
209 // ------------------------------------------------------------------
211 // ------------------------------------------------------------------
213 //Determines whether version 4 of QT is installed
214 Boolean
wxIsQuickTime4Installed (void)
220 error
= Gestalt (gestaltQuickTime
, &result
);
221 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
227 inline bool wxInitQT ()
229 if (wxIsQuickTime4Installed())
234 if ((nError
= InitializeQTML(0)) != noErr
)
235 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
242 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
248 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
252 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
253 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
255 Create(sFileName
, isResource
);
258 wxSound::wxSound(int size
, const wxByte
* data
)
259 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
267 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
274 m_type
= wxSound_RESOURCE
;
278 wxMacStringToPascal( fileName
, lpSnd
) ;
280 m_sndname
= fileName
;
281 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
288 m_type
= wxSound_FILE
;
289 m_sndname
= fileName
;
295 bool wxSound::DoPlay(unsigned flags
) const
307 Handle myHandle
, dataRef
= nil
;
308 MovieImportComponent miComponent
;
309 Track targetTrack
= nil
;
310 TimeValue addedDuration
= 0;
313 ComponentResult result
;
315 myHandle
= NewHandleClear((Size
)m_waveLength
);
317 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
319 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
321 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
322 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
323 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
324 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
325 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
326 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
329 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
335 result
= MovieImportDataRef(miComponent
, dataRef
,
336 HandleDataHandlerSubType
, movie
,
339 movieImportCreateTrack
, &outFlags
);
343 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
346 SetMovieVolume(movie
, kFullVolume
);
347 GoToBeginningOfMovie(movie
);
349 DisposeHandle(myHandle
);
352 case wxSound_RESOURCE
:
354 SoundComponentData data
;
355 unsigned long numframes
, offset
;
357 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
358 //m_waveLength = numFrames * data.numChannels;
360 SndChannelPtr pSndChannel
;
361 SndNewChannel(&pSndChannel
, sampledSynth
,
363 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
365 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
368 if (flags
& wxSOUND_ASYNC
)
370 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
371 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
372 &lastSoundIsPlaying
);
373 lastSoundIsPlaying
= true;
375 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
378 SndDisposeChannel(pSndChannel
, TRUE
);
389 //NB: RN: Stefan - I think the 10.3 path functions are broken if kQTNativeDefaultPathStyle is
390 //going to trigger a warning every time it is used - where its _supposed to be used_!!
391 //(kQTNativePathStyle is negative but the function argument is unsigned!)
392 //../src/mac/carbon/sound.cpp: In member function `virtual bool
393 // wxSound::DoPlay(unsigned int) const':
394 //../src/mac/carbon/sound.cpp:387: warning: passing negative value `
395 // kQTNativeDefaultPathStyle' for argument passing 2 of `OSErr
396 // QTNewDataReferenceFromFullPathCFString(const __CFString*, long unsigned int,
397 // long unsigned int, char***, OSType*)'
398 //../src/mac/carbon/sound.cpp:387: warning: argument of negative value `
399 // kQTNativeDefaultPathStyle' to `long unsigned int'
400 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
401 if ( UMAGetSystemVersion() >= 0x1030 )
403 Handle dataRef
= NULL
;
406 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
407 //FIXME: Why does this have to be casted?
408 (unsigned int)kQTNativeDefaultPathStyle
,
410 0, &dataRef
, &dataRefType
);
412 wxASSERT(err
== noErr
);
414 if (NULL
!= dataRef
|| err
!= noErr
)
416 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
417 wxASSERT(err
== noErr
);
418 DisposeHandle(dataRef
);
427 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
430 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
433 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
434 m_sndname.c_str(), nError));
439 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
441 wxLogSysError(wxT("Quicktime couldn't open the file"));
444 short movieResID
= 0;
447 err
= NewMovieFromFile (
455 CloseMovieFile (movieResFile
);
461 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
469 }//end switch(m_type)
474 if (flags
& wxSOUND_ASYNC
)
476 //Start timer and play movie asyncronously
477 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
478 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
479 &lastSoundIsPlaying
);
480 lastSoundIsPlaying
= true;
481 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
485 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
487 //Play movie until it ends, then exit
488 //Note that due to quicktime caching this may not always
489 //work 100% correctly
490 while (!IsMovieDone(movie
))
491 MoviesTask(movie
, 1);
499 bool wxSound::IsPlaying()
501 return lastSoundIsPlaying
;
506 if (lastSoundIsPlaying
)
508 delete (wxTimer
*&) lastSoundTimer
;
509 lastSoundIsPlaying
= false;
510 lastSoundTimer
= NULL
;
514 void* wxSound::GetHandle()
516 if(m_type
== wxSound_RESOURCE
)
517 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
519 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();