]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/sound.cpp
Include missing.h to find O_* flags.
[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
bc57786b 5// Modified by: Stefan Csomor
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
bc57786b 8// Copyright: (c) Ryan Norton
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)
6d3ef225 226 wxLogSysError(wxString::Format(wxT("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 379 OSErr err = noErr ;
bc57786b
RN
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'
a2b77260
SC
391#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
392 if ( UMAGetSystemVersion() >= 0x1030 )
4b430ee1 393 {
a2b77260
SC
394 Handle dataRef = NULL;
395 OSType dataRefType;
396
397 err = QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname,wxLocale::GetSystemEncoding()),
bc57786b
RN
398 //FIXME: Why does this have to be casted?
399 (unsigned int)kQTNativeDefaultPathStyle,
400 //FIXME: End
401 0, &dataRef, &dataRefType);
a2b77260 402
bc57786b
RN
403 wxASSERT(err == noErr);
404
405 if (NULL != dataRef || err != noErr)
a2b77260
SC
406 {
407 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
bc57786b 408 wxASSERT(err == noErr);
a2b77260
SC
409 DisposeHandle(dataRef);
410 }
4b430ee1 411 }
a2b77260 412 else
625d14ab 413#endif
625d14ab 414 {
a2b77260
SC
415 short movieResFile;
416 FSSpec sfFile;
417#ifdef __WXMAC__
418 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
419#else
420 int nError;
421 if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
422 {
423 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
424 m_sndname.c_str(), nError));
425 return false;
426 }
427#endif
428 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
429 {
430 wxLogSysError(wxT("Quicktime couldn't open the file"));
431 return false;
432 }
433 short movieResID = 0;
434 Str255 movieName;
435
436 err = NewMovieFromFile (
437 &movie,
438 movieResFile,
439 &movieResID,
440 movieName,
441 newMovieActive,
442 NULL); //wasChanged
443
444 CloseMovieFile (movieResFile);
625d14ab 445 }
a2b77260 446
625d14ab 447 if (err != noErr)
4b430ee1 448 {
625d14ab
SC
449 wxLogSysError(
450 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
451 );
452 return false;
4b430ee1 453 }
625d14ab
SC
454 }
455 break;
456 default:
457 return false;
458 }//end switch(m_type)
e40298d5 459
625d14ab
SC
460 //Start the movie!
461 StartMovie(movie);
e40298d5 462
c22edec9
RD
463 if (flags & wxSOUND_ASYNC)
464 {
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);
471 }
472 else
625d14ab 473 {
7f0b95bb 474 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
e40298d5 475
625d14ab
SC
476 //Play movie until it ends, then exit
477 while (!IsMovieDone(movie))
478 MoviesTask(movie, 0);
e40298d5 479
625d14ab
SC
480 DisposeMovie(movie);
481 }
625d14ab
SC
482
483 return true;
5b781a67
SC
484}
485
4b430ee1 486bool wxSound::IsPlaying()
625d14ab 487{
4b430ee1 488 return lastSoundIsPlaying;
625d14ab 489}
5b781a67 490
4b430ee1 491void wxSound::Stop()
5b781a67 492{
c22edec9 493 if (lastSoundIsPlaying)
e40298d5 494 {
4b430ee1
DS
495 delete (wxTimer*&) lastSoundTimer;
496 lastSoundIsPlaying = false;
c22edec9 497 lastSoundTimer = NULL;
e40298d5 498 }
625d14ab 499}
9e783cc0 500
4b430ee1
DS
501void* wxSound::GetHandle()
502{
503 if(m_type == wxSound_RESOURCE)
504 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
9e783cc0 505
4b430ee1
DS
506 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
507}
9e783cc0 508
4b430ee1 509#endif //wxUSE_SOUND