1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
5 // Modified by: Stefan Csomor
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "sound.h"
16 #include "wx/object.h"
17 #include "wx/string.h"
26 // Carbon QT Implementation Details -
29 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
31 // 3) MovieImportDataRef() //Pass Memory Location to this
33 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
37 // 2) Call OpenMovieFile
38 // 3) Call NewMovieFromFile
39 // 4) Call CloseMovieFile
41 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
45 #include "wx/mac/uma.h"
50 #if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
52 #include <Carbon/Carbon.h>
60 //quicktime media layer only required for mac emulation on pc
65 #include <QuickTimeComponents.h>
67 //Time between timer calls
68 #define MOVIE_DELAY 100
70 static wxTimer
* lastSoundTimer
=NULL
;
71 static bool lastSoundIsPlaying
=false;
73 // ------------------------------------------------------------------
74 // wxQTTimer - Handle Asyncronous Playing
75 // ------------------------------------------------------------------
76 class wxQTTimer
: public wxTimer
79 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
80 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
90 DisposeMovie(m_movie
);
93 //Note that ExitMovies() is not neccessary, but
94 //the docs are fuzzy on whether or not TerminateQTML is
109 if (m_pbPlaying
&& !*m_pbPlaying
)
114 if(IsMovieDone(m_movie
))
121 GoToBeginningOfMovie(m_movie
);
126 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
130 Movie
& GetMovie() {return m_movie
;}
142 class wxSMTimer
: public wxTimer
145 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
146 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
153 *m_pbPlaying
= false;
154 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
160 if (m_pbPlaying
&& !*m_pbPlaying
)
167 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
170 //if the sound isn't playing anymore, see if it's looped,
171 //and if so play it again, otherwise close things up
172 if (stat
.scChannelBusy
== FALSE
)
176 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
189 void* GetChannel() {return m_pSndChannel
;}
200 // ------------------------------------------------------------------
202 // ------------------------------------------------------------------
204 //Determines whether version 4 of QT is installed
205 Boolean
wxIsQuickTime4Installed (void)
211 error
= Gestalt (gestaltQuickTime
, &result
);
212 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
218 inline bool wxInitQT ()
220 if (wxIsQuickTime4Installed())
225 if ((nError
= InitializeQTML(0)) != noErr
)
226 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
233 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
239 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
243 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
244 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
246 Create(sFileName
, isResource
);
249 wxSound::wxSound(int size
, const wxByte
* data
)
250 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
258 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
265 m_type
= wxSound_RESOURCE
;
269 wxMacStringToPascal( fileName
, lpSnd
) ;
271 m_sndname
= fileName
;
272 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
279 m_type
= wxSound_FILE
;
280 m_sndname
= fileName
;
286 bool wxSound::DoPlay(unsigned flags
) const
298 Handle myHandle
, dataRef
= nil
;
299 MovieImportComponent miComponent
;
300 Track targetTrack
= nil
;
301 TimeValue addedDuration
= 0;
304 ComponentResult result
;
306 myHandle
= NewHandleClear((Size
)m_waveLength
);
308 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
310 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
312 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
313 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
314 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
315 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
316 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
317 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
320 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
326 result
= MovieImportDataRef(miComponent
, dataRef
,
327 HandleDataHandlerSubType
, movie
,
330 movieImportCreateTrack
, &outFlags
);
334 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
337 SetMovieVolume(movie
, kFullVolume
);
338 GoToBeginningOfMovie(movie
);
340 DisposeHandle(myHandle
);
343 case wxSound_RESOURCE
:
345 SoundComponentData data
;
346 unsigned long numframes
, offset
;
348 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
349 //m_waveLength = numFrames * data.numChannels;
351 SndChannelPtr pSndChannel
;
352 SndNewChannel(&pSndChannel
, sampledSynth
,
354 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
356 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
359 if (flags
& wxSOUND_ASYNC
)
361 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
362 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
363 &lastSoundIsPlaying
);
364 lastSoundIsPlaying
= true;
366 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
369 SndDisposeChannel(pSndChannel
, TRUE
);
380 //NB: RN: Stefan - I think the 10.3 path functions are broken if kQTNativeDefaultPathStyle is
381 //going to trigger a warning every time it is used - where its _supposed to be used_!!
382 //(kQTNativePathStyle is negative but the function argument is unsigned!)
383 //../src/mac/carbon/sound.cpp: In member function `virtual bool
384 // wxSound::DoPlay(unsigned int) const':
385 //../src/mac/carbon/sound.cpp:387: warning: passing negative value `
386 // kQTNativeDefaultPathStyle' for argument passing 2 of `OSErr
387 // QTNewDataReferenceFromFullPathCFString(const __CFString*, long unsigned int,
388 // long unsigned int, char***, OSType*)'
389 //../src/mac/carbon/sound.cpp:387: warning: argument of negative value `
390 // kQTNativeDefaultPathStyle' to `long unsigned int'
391 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
392 if ( UMAGetSystemVersion() >= 0x1030 )
394 Handle dataRef
= NULL
;
397 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
398 //FIXME: Why does this have to be casted?
399 (unsigned int)kQTNativeDefaultPathStyle
,
401 0, &dataRef
, &dataRefType
);
403 wxASSERT(err
== noErr
);
405 if (NULL
!= dataRef
|| err
!= noErr
)
407 err
= NewMovieFromDataRef( &movie
, newMovieDontAskUnresolvedDataRefs
, NULL
, dataRef
, dataRefType
);
408 wxASSERT(err
== noErr
);
409 DisposeHandle(dataRef
);
418 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
421 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
423 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
424 m_sndname
.c_str(), nError
));
428 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
430 wxLogSysError(wxT("Quicktime couldn't open the file"));
433 short movieResID
= 0;
436 err
= NewMovieFromFile (
444 CloseMovieFile (movieResFile
);
450 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
458 }//end switch(m_type)
463 if (flags
& wxSOUND_ASYNC
)
465 //Start timer and play movie asyncronously
466 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
467 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
468 &lastSoundIsPlaying
);
469 lastSoundIsPlaying
= true;
470 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
474 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
476 //Play movie until it ends, then exit
477 //Note that due to quicktime caching this may not always
478 //work 100% correctly
479 while (!IsMovieDone(movie
))
480 MoviesTask(movie
, 1);
488 bool wxSound::IsPlaying()
490 return lastSoundIsPlaying
;
495 if (lastSoundIsPlaying
)
497 delete (wxTimer
*&) lastSoundTimer
;
498 lastSoundIsPlaying
= false;
499 lastSoundTimer
= NULL
;
503 void* wxSound::GetHandle()
505 if(m_type
== wxSound_RESOURCE
)
506 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
508 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();