]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/sound.cpp
Applied patch to avoid g_lib wanrings.
[wxWidgets.git] / src / mac / carbon / sound.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
315ebf68
VS
2// Name: sound.cpp
3// Purpose: wxSound class implementation: optional
4b430ee1 4// Author: Ryan Norton
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
625d14ab 8// Copyright: (c) Ryan Norton, Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
315ebf68 13#pragma implementation "sound.h"
e9576ca5
SC
14#endif
15
16#include "wx/object.h"
17#include "wx/string.h"
625d14ab
SC
18#include "wx/log.h"
19#include "wx/file.h"
315ebf68 20#include "wx/sound.h"
625d14ab 21#include "wx/timer.h"
c9528e9d 22#include "wx/intl.h"
e9576ca5 23
315ebf68 24#if wxUSE_SOUND
4a69b060 25
625d14ab
SC
26// Carbon QT Implementation Details -
27//
28// Memory:
29// 1) OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
30// 2) NewMovie(0);
31// 3) MovieImportDataRef() //Pass Memory Location to this
32// 4) PlayMovie();
33// 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
34//
35// File:
36// 1) Obtain FSSpec
37// 2) Call OpenMovieFile
38// 3) Call NewMovieFromFile
39// 4) Call CloseMovieFile
40// 4) PlayMovie();
41// 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
42//
43
76a5e5d2 44#ifdef __WXMAC__
a2b77260 45#include "wx/mac/uma.h"
625d14ab
SC
46#include <Movies.h>
47#include <Gestalt.h>
48#endif
49
50#if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
51#ifdef __APPLE_CC__
52#include <Carbon/Carbon.h>
53#else
54#include <Carbon.h>
66a09d47 55#endif
625d14ab
SC
56#else
57#include <Sound.h>
76a5e5d2
SC
58#endif
59
625d14ab
SC
60//quicktime media layer only required for mac emulation on pc
61#ifndef __WXMAC__
62#include <qtml.h>
63#endif
e9576ca5 64
625d14ab 65#include <QuickTimeComponents.h>
e9576ca5 66
c22edec9 67//Time between timer calls
625d14ab 68#define MOVIE_DELAY 100
e9576ca5 69
c22edec9
RD
70static wxTimer* lastSoundTimer=NULL;
71static bool lastSoundIsPlaying=false;
72
625d14ab
SC
73// ------------------------------------------------------------------
74// wxQTTimer - Handle Asyncronous Playing
75// ------------------------------------------------------------------
76class wxQTTimer : public wxTimer
e9576ca5 77{
625d14ab 78public:
c22edec9
RD
79 wxQTTimer(Movie movie, bool bLoop, bool* playing) :
80 m_movie(movie), m_bLoop(bLoop), m_pbPlaying(playing)
625d14ab
SC
81 {
82 }
e9576ca5 83
625d14ab
SC
84 ~wxQTTimer()
85 {
4b430ee1
DS
86 if(m_pbPlaying)
87 *m_pbPlaying = false;
5b781a67 88
625d14ab
SC
89 StopMovie(m_movie);
90 DisposeMovie(m_movie);
625d14ab 91 Stop();
9e783cc0
RD
92
93 //Note that ExitMovies() is not neccessary, but
94 //the docs are fuzzy on whether or not TerminateQTML is
95 ExitMovies();
96
4b430ee1 97#ifndef __WXMAC__
9e783cc0 98 TerminateQTML();
4b430ee1
DS
99#endif
100 }
101
102 void Shutdown()
103 {
104 delete this;
625d14ab 105 }
e40298d5 106
625d14ab
SC
107 void Notify()
108 {
4b430ee1
DS
109 if (m_pbPlaying && !*m_pbPlaying)
110 {
111 Shutdown();
112 }
113
625d14ab
SC
114 if(IsMovieDone(m_movie))
115 {
116 if (!m_bLoop)
117 Shutdown();
118 else
119 {
120 StopMovie(m_movie);
4b430ee1 121 GoToBeginningOfMovie(m_movie);
625d14ab 122 StartMovie(m_movie);
4b430ee1 123 }
625d14ab
SC
124 }
125 else
126 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
127 }
e40298d5 128
e40298d5 129
625d14ab 130 Movie& GetMovie() {return m_movie;}
4b430ee1 131
625d14ab
SC
132protected:
133 Movie m_movie;
134 bool m_bLoop;
4b430ee1
DS
135
136public:
137 bool* m_pbPlaying;
138
625d14ab 139};
e40298d5 140
e40298d5 141
625d14ab
SC
142class wxSMTimer : public wxTimer
143{
144public:
c22edec9
RD
145 wxSMTimer(void* hSnd, void* pSndChannel, bool bLoop, bool* playing)
146 : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(playing)
4b430ee1
DS
147 {
148 }
e40298d5 149
625d14ab 150 ~wxSMTimer()
4b430ee1
DS
151 {
152 if(m_pbPlaying)
153 *m_pbPlaying = false;
154 SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
155 Stop();
156 }
e40298d5 157
625d14ab 158 void Notify()
4b430ee1
DS
159 {
160 if (m_pbPlaying && !*m_pbPlaying)
e40298d5 161 {
4b430ee1
DS
162 Shutdown();
163 }
164
165 SCStatus stat;
625d14ab
SC
166
167 if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
4b430ee1
DS
168 Shutdown();
169
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)
173 {
174 if (m_bLoop)
175 {
176 if(SndPlay((SndChannelPtr)m_pSndChannel, (SndListHandle) m_hSnd, true) != noErr)
177 Shutdown();
178 }
179 else
180 Shutdown();
e40298d5 181 }
625d14ab 182 }
e40298d5 183
625d14ab
SC
184 void Shutdown()
185 {
4b430ee1 186 delete this;
625d14ab 187 }
4b430ee1
DS
188
189 void* GetChannel() {return m_pSndChannel;}
190
625d14ab
SC
191protected:
192 void* m_hSnd;
193 void* m_pSndChannel;
194 bool m_bLoop;
4b430ee1
DS
195
196public:
197 bool* m_pbPlaying;
625d14ab
SC
198};
199
200// ------------------------------------------------------------------
201// wxSound
202// ------------------------------------------------------------------
203
204//Determines whether version 4 of QT is installed
205Boolean wxIsQuickTime4Installed (void)
206{
207#ifdef __WXMAC__
208 short error;
209 long result;
e40298d5 210
625d14ab 211 error = Gestalt (gestaltQuickTime, &result);
9e783cc0 212 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
625d14ab
SC
213#else
214 return true;
215#endif
216}
e40298d5 217
625d14ab
SC
218inline bool wxInitQT ()
219{
220 if (wxIsQuickTime4Installed())
221 {
222 #ifndef __WXMAC__
223 int nError;
224 //-2093 no dll
225 if ((nError = InitializeQTML(0)) != noErr)
226 wxLogSysError(wxString::Format("Couldn't Initialize Quicktime-%i", nError));
4b430ee1 227 #endif
625d14ab
SC
228 EnterMovies();
229 return true;
230 }
231 else
4b430ee1 232 {
7f0b95bb 233 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
4b430ee1
DS
234 return false;
235 }
625d14ab 236}
e40298d5 237
625d14ab
SC
238wxSound::wxSound()
239: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
240{
241}
242
243wxSound::wxSound(const wxString& sFileName, bool isResource)
244: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
245{
246 Create(sFileName, isResource);
247}
248
249wxSound::wxSound(int size, const wxByte* data)
250: m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
251{
625d14ab
SC
252}
253
254wxSound::~wxSound()
255{
625d14ab
SC
256}
257
258bool wxSound::Create(const wxString& fileName, bool isResource)
259{
4b430ee1 260 Stop();
2116a0d1 261
625d14ab
SC
262 if (isResource)
263 {
264#ifdef __WXMAC__
265 m_type = wxSound_RESOURCE;
266
267 Str255 lpSnd ;
268
269 wxMacStringToPascal( fileName , lpSnd ) ;
270
7f0b95bb 271 m_sndname = fileName;
625d14ab
SC
272 m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
273#else
274 return false;
275#endif
4b430ee1
DS
276 }
277 else
278 {
625d14ab
SC
279 m_type = wxSound_FILE;
280 m_sndname = fileName;
e40298d5
JS
281 }
282
625d14ab 283 return true;
e9576ca5
SC
284}
285
315ebf68 286bool wxSound::DoPlay(unsigned flags) const
e9576ca5 287{
4b430ee1 288 Stop();
4a69b060 289
625d14ab
SC
290 Movie movie;
291
292 switch(m_type)
293 {
294 case wxSound_MEMORY:
295 {
4b430ee1
DS
296 if (!wxInitQT())
297 return false;
625d14ab 298 Handle myHandle, dataRef = nil;
4b430ee1 299 MovieImportComponent miComponent;
625d14ab
SC
300 Track targetTrack = nil;
301 TimeValue addedDuration = 0;
302 long outFlags = 0;
303 OSErr err;
304 ComponentResult result;
305
306 myHandle = NewHandleClear((Size)m_waveLength);
4b430ee1 307
625d14ab
SC
308 BlockMove(m_hSnd, *myHandle, m_waveLength);
309
310 err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
311
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);
318 else
4b430ee1 319 {
7f0b95bb 320 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
625d14ab
SC
321 return false;
322 }
323
324 movie = NewMovie(0);
4a69b060 325
625d14ab
SC
326 result = MovieImportDataRef(miComponent, dataRef,
327 HandleDataHandlerSubType, movie,
328 nil, &targetTrack,
329 nil, &addedDuration,
330 movieImportCreateTrack, &outFlags);
e9576ca5 331
625d14ab
SC
332 if (result != noErr)
333 {
334 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
4b430ee1 335 }
4a69b060 336
625d14ab
SC
337 SetMovieVolume(movie, kFullVolume);
338 GoToBeginningOfMovie(movie);
9e783cc0
RD
339
340 DisposeHandle(myHandle);
625d14ab
SC
341 }
342 break;
343 case wxSound_RESOURCE:
344 {
345 SoundComponentData data;
346 unsigned long numframes, offset;
e9576ca5 347
625d14ab
SC
348 ParseSndHeader((SndListHandle)m_hSnd, &data, &numframes, &offset);
349 //m_waveLength = numFrames * data.numChannels;
5b781a67 350
625d14ab
SC
351 SndChannelPtr pSndChannel;
352 SndNewChannel(&pSndChannel, sampledSynth,
4b430ee1
DS
353 initNoInterp
354 + (data.numChannels == 1 ? initMono : initStereo), NULL);
e40298d5 355
625d14ab 356 if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
4b430ee1 357 return false;
625d14ab
SC
358
359 if (flags & wxSOUND_ASYNC)
4b430ee1
DS
360 {
361 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
362 = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
c22edec9
RD
363 &lastSoundIsPlaying);
364 lastSoundIsPlaying = true;
4b430ee1
DS
365
366 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
367 }
368 else
625d14ab
SC
369 SndDisposeChannel(pSndChannel, TRUE);
370
371 return true;
372 }
373 break;
374 case wxSound_FILE:
375 {
4b430ee1
DS
376 if (!wxInitQT())
377 return false;
378
a2b77260
SC
379 OSErr err = noErr ;
380#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
381 if ( UMAGetSystemVersion() >= 0x1030 )
4b430ee1 382 {
a2b77260
SC
383 Handle dataRef = NULL;
384 OSType dataRefType;
385
386 err = QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname,wxLocale::GetSystemEncoding()),
387 kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
388
389 if (NULL != dataRef)
390 {
391 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
392 DisposeHandle(dataRef);
393 }
4b430ee1 394 }
a2b77260 395 else
625d14ab 396#endif
625d14ab 397 {
a2b77260
SC
398 short movieResFile;
399 FSSpec sfFile;
400#ifdef __WXMAC__
401 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
402#else
403 int nError;
404 if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
405 {
406 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
407 m_sndname.c_str(), nError));
408 return false;
409 }
410#endif
411 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
412 {
413 wxLogSysError(wxT("Quicktime couldn't open the file"));
414 return false;
415 }
416 short movieResID = 0;
417 Str255 movieName;
418
419 err = NewMovieFromFile (
420 &movie,
421 movieResFile,
422 &movieResID,
423 movieName,
424 newMovieActive,
425 NULL); //wasChanged
426
427 CloseMovieFile (movieResFile);
625d14ab 428 }
a2b77260 429
625d14ab 430 if (err != noErr)
4b430ee1 431 {
625d14ab
SC
432 wxLogSysError(
433 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
434 );
435 return false;
4b430ee1 436 }
625d14ab
SC
437 }
438 break;
439 default:
440 return false;
441 }//end switch(m_type)
e40298d5 442
625d14ab
SC
443 //Start the movie!
444 StartMovie(movie);
e40298d5 445
c22edec9
RD
446 if (flags & wxSOUND_ASYNC)
447 {
448 //Start timer and play movie asyncronously
449 lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
450 new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
451 &lastSoundIsPlaying);
452 lastSoundIsPlaying = true;
453 ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
454 }
455 else
625d14ab 456 {
7f0b95bb 457 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
e40298d5 458
625d14ab
SC
459 //Play movie until it ends, then exit
460 while (!IsMovieDone(movie))
461 MoviesTask(movie, 0);
e40298d5 462
625d14ab
SC
463 DisposeMovie(movie);
464 }
625d14ab
SC
465
466 return true;
5b781a67
SC
467}
468
4b430ee1 469bool wxSound::IsPlaying()
625d14ab 470{
4b430ee1 471 return lastSoundIsPlaying;
625d14ab 472}
5b781a67 473
4b430ee1 474void wxSound::Stop()
5b781a67 475{
c22edec9 476 if (lastSoundIsPlaying)
e40298d5 477 {
4b430ee1
DS
478 delete (wxTimer*&) lastSoundTimer;
479 lastSoundIsPlaying = false;
c22edec9 480 lastSoundTimer = NULL;
e40298d5 481 }
625d14ab 482}
9e783cc0 483
4b430ee1
DS
484void* wxSound::GetHandle()
485{
486 if(m_type == wxSound_RESOURCE)
487 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
9e783cc0 488
4b430ee1
DS
489 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
490}
9e783cc0 491
4b430ee1 492#endif //wxUSE_SOUND