1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound class implementation: optional
5 // Modified by: Stefan Csomor
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/object.h"
15 #include "wx/string.h"
24 // Carbon QT Implementation Details -
27 // 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
29 // 3) MovieImportDataRef() //Pass Memory Location to this
31 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
35 // 2) Call OpenMovieFile
36 // 3) Call NewMovieFromFile
37 // 4) Call CloseMovieFile
39 // 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
43 #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
66 #include <QuickTimeComponents.h>
68 #include <QuickTime/QuickTimeComponents.h>
71 //Time between timer calls
72 #define MOVIE_DELAY 100
74 static wxTimer
* lastSoundTimer
=NULL
;
75 static bool lastSoundIsPlaying
=false;
77 // ------------------------------------------------------------------
78 // wxQTTimer - Handle Asyncronous Playing
79 // ------------------------------------------------------------------
80 class wxQTTimer
: public wxTimer
83 wxQTTimer(Movie movie
, bool bLoop
, bool* playing
) :
84 m_movie(movie
), m_bLoop(bLoop
), m_pbPlaying(playing
)
94 DisposeMovie(m_movie
);
97 //Note that ExitMovies() is not necessary, but
98 //the docs are fuzzy on whether or not TerminateQTML is
113 if (m_pbPlaying
&& !*m_pbPlaying
)
118 if(IsMovieDone(m_movie
))
125 GoToBeginningOfMovie(m_movie
);
130 MoviesTask(m_movie
, MOVIE_DELAY
); //Give QT time to play movie
134 Movie
& GetMovie() {return m_movie
;}
146 class wxSMTimer
: public wxTimer
149 wxSMTimer(void* hSnd
, void* pSndChannel
, bool bLoop
, bool* playing
)
150 : m_hSnd(hSnd
), m_pSndChannel(pSndChannel
), m_bLoop(bLoop
), m_pbPlaying(playing
)
157 *m_pbPlaying
= false;
158 SndDisposeChannel((SndChannelPtr
)m_pSndChannel
, TRUE
);
164 if (m_pbPlaying
&& !*m_pbPlaying
)
171 if (SndChannelStatus((SndChannelPtr
)m_pSndChannel
, sizeof(SCStatus
), &stat
) != 0)
174 //if the sound isn't playing anymore, see if it's looped,
175 //and if so play it again, otherwise close things up
176 if (stat
.scChannelBusy
== FALSE
)
180 if(SndPlay((SndChannelPtr
)m_pSndChannel
, (SndListHandle
) m_hSnd
, true) != noErr
)
193 void* GetChannel() {return m_pSndChannel
;}
204 // ------------------------------------------------------------------
206 // ------------------------------------------------------------------
208 //Determines whether version 4 of QT is installed
209 Boolean
wxIsQuickTime4Installed (void)
215 error
= Gestalt (gestaltQuickTime
, &result
);
216 return (error
== noErr
) && (((result
>> 16) & 0xffff) >= 0x0400);
222 inline bool wxInitQT ()
224 if (wxIsQuickTime4Installed())
229 if ((nError
= InitializeQTML(0)) != noErr
)
230 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError
));
237 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
243 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
247 wxSound::wxSound(const wxString
& sFileName
, bool isResource
)
248 : m_hSnd(NULL
), m_waveLength(0), m_pTimer(NULL
), m_type(wxSound_NONE
)
250 Create(sFileName
, isResource
);
253 wxSound::wxSound(int size
, const wxByte
* data
)
254 : m_hSnd((char*)data
), m_waveLength(size
), m_pTimer(NULL
), m_type(wxSound_MEMORY
)
262 bool wxSound::Create(const wxString
& fileName
, bool isResource
)
269 m_type
= wxSound_RESOURCE
;
273 wxMacStringToPascal( fileName
, lpSnd
) ;
275 m_sndname
= fileName
;
276 m_hSnd
= (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd
);
283 m_type
= wxSound_FILE
;
284 m_sndname
= fileName
;
290 bool wxSound::DoPlay(unsigned flags
) const
302 Handle myHandle
, dataRef
= nil
;
303 MovieImportComponent miComponent
;
304 Track targetTrack
= nil
;
305 TimeValue addedDuration
= 0;
308 ComponentResult result
;
310 myHandle
= NewHandleClear((Size
)m_waveLength
);
312 BlockMove(m_hSnd
, *myHandle
, m_waveLength
);
314 err
= PtrToHand(&myHandle
, &dataRef
, sizeof(Handle
));
316 if (memcmp(&m_hSnd
[8], "WAVE", 4) == 0)
317 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeWave
);
318 else if (memcmp(&m_hSnd
[8], "AIFF", 4) == 0)
319 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFF
);
320 else if (memcmp(&m_hSnd
[8], "AIFC", 4) == 0)
321 miComponent
= OpenDefaultComponent(MovieImportType
, kQTFileTypeAIFC
);
324 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
330 result
= MovieImportDataRef(miComponent
, dataRef
,
331 HandleDataHandlerSubType
, movie
,
334 movieImportCreateTrack
, &outFlags
);
338 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result
));
341 SetMovieVolume(movie
, kFullVolume
);
342 GoToBeginningOfMovie(movie
);
344 DisposeHandle(myHandle
);
347 case wxSound_RESOURCE
:
349 SoundComponentData data
;
350 unsigned long numframes
, offset
;
352 ParseSndHeader((SndListHandle
)m_hSnd
, &data
, &numframes
, &offset
);
353 //m_waveLength = numFrames * data.numChannels;
355 SndChannelPtr pSndChannel
;
356 SndNewChannel(&pSndChannel
, sampledSynth
,
358 + (data
.numChannels
== 1 ? initMono
: initStereo
), NULL
);
360 if(SndPlay(pSndChannel
, (SndListHandle
) m_hSnd
, flags
& wxSOUND_ASYNC
? 1 : 0) != noErr
)
363 if (flags
& wxSOUND_ASYNC
)
365 lastSoundTimer
= ((wxSMTimer
*&)m_pTimer
)
366 = new wxSMTimer(pSndChannel
, m_hSnd
, flags
& wxSOUND_LOOP
? 1 : 0,
367 &lastSoundIsPlaying
);
368 lastSoundIsPlaying
= true;
370 ((wxTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
373 SndDisposeChannel(pSndChannel
, TRUE
);
384 //NB: RN: Stefan - I think the 10.3 path functions are broken if kQTNativeDefaultPathStyle is
385 //going to trigger a warning every time it is used - where its _supposed to be used_!!
386 //(kQTNativePathStyle is negative but the function argument is unsigned!)
387 //../src/mac/carbon/sound.cpp: In member function `virtual bool
388 // wxSound::DoPlay(unsigned int) const':
389 //../src/mac/carbon/sound.cpp:387: warning: passing negative value `
390 // kQTNativeDefaultPathStyle' for argument passing 2 of `OSErr
391 // QTNewDataReferenceFromFullPathCFString(const __CFString*, long unsigned int,
392 // long unsigned int, char***, OSType*)'
393 //../src/mac/carbon/sound.cpp:387: warning: argument of negative value `
394 // kQTNativeDefaultPathStyle' to `long unsigned int'
395 #if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
396 if ( UMAGetSystemVersion() >= 0x1030 )
398 Handle dataRef
= NULL
;
401 err
= QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname
,wxLocale::GetSystemEncoding()),
402 //FIXME: Why does this have to be casted?
403 (unsigned int)kQTNativeDefaultPathStyle
,
405 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
);
422 wxMacFilename2FSSpec( m_sndname
, &sfFile
) ;
425 if ((nError
= NativePathNameToFSSpec ((char*) m_sndname
.c_str(), &sfFile
, 0)) != noErr
)
428 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
429 m_sndname.c_str(), nError));
434 if (OpenMovieFile (&sfFile
, &movieResFile
, fsRdPerm
) != noErr
)
436 wxLogSysError(wxT("Quicktime couldn't open the file"));
439 short movieResID
= 0;
442 err
= NewMovieFromFile (
450 CloseMovieFile (movieResFile
);
456 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname
.c_str(), err
)
464 }//end switch(m_type)
469 if (flags
& wxSOUND_ASYNC
)
471 //Start timer and play movie asyncronously
472 lastSoundTimer
= ((wxQTTimer
*&)m_pTimer
) =
473 new wxQTTimer(movie
, flags
& wxSOUND_LOOP
? 1 : 0,
474 &lastSoundIsPlaying
);
475 lastSoundIsPlaying
= true;
476 ((wxQTTimer
*)m_pTimer
)->Start(MOVIE_DELAY
, wxTIMER_CONTINUOUS
);
480 wxASSERT_MSG(!(flags
& wxSOUND_LOOP
), wxT("Can't loop and play syncronously at the same time"));
482 //Play movie until it ends, then exit
483 //Note that due to quicktime caching this may not always
484 //work 100% correctly
485 while (!IsMovieDone(movie
))
486 MoviesTask(movie
, 1);
494 bool wxSound::IsPlaying()
496 return lastSoundIsPlaying
;
501 if (lastSoundIsPlaying
)
503 delete (wxTimer
*&) lastSoundTimer
;
504 lastSoundIsPlaying
= false;
505 lastSoundTimer
= NULL
;
509 void* wxSound::GetHandle()
511 if(m_type
== wxSound_RESOURCE
)
512 return (void*) ((wxSMTimer
*)m_pTimer
)->GetChannel();
514 return (void*) ((wxQTTimer
*) m_pTimer
)->GetMovie();