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