]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/sound.cpp
fix compilation after r50329
[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:
fe8712b5
SC
39// 1) Path as CFString
40// 2) Call QTNewDataReferenceFromFullPathCFString
41// 3) Call NewMovieFromDataRef
625d14ab
SC
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 54
625d14ab 55#include <Carbon/Carbon.h>
76a5e5d2 56
625d14ab
SC
57//quicktime media layer only required for mac emulation on pc
58#ifndef __WXMAC__
59#include <qtml.h>
60#endif
e9576ca5 61
768c6e8b 62#ifndef __DARWIN__
625d14ab 63#include <QuickTimeComponents.h>
768c6e8b
SC
64#else
65#include <QuickTime/QuickTimeComponents.h>
66#endif
e9576ca5 67
c22edec9 68//Time between timer calls
625d14ab 69#define MOVIE_DELAY 100
e9576ca5 70
c22edec9
RD
71static wxTimer* lastSoundTimer=NULL;
72static bool lastSoundIsPlaying=false;
73
03561a3c 74#if !defined(__LP64__)
6239ee05
SC
75#define USE_QUICKTIME 1
76#else
77#define USE_QUICKTIME 0
78#endif
79
80#if USE_QUICKTIME
625d14ab
SC
81// ------------------------------------------------------------------
82// wxQTTimer - Handle Asyncronous Playing
83// ------------------------------------------------------------------
84class wxQTTimer : public wxTimer
e9576ca5 85{
625d14ab 86public:
c22edec9
RD
87 wxQTTimer(Movie movie, bool bLoop, bool* playing) :
88 m_movie(movie), m_bLoop(bLoop), m_pbPlaying(playing)
625d14ab
SC
89 {
90 }
e9576ca5 91
d3c7fc99 92 virtual ~wxQTTimer()
625d14ab 93 {
4b430ee1
DS
94 if(m_pbPlaying)
95 *m_pbPlaying = false;
5b781a67 96
625d14ab
SC
97 StopMovie(m_movie);
98 DisposeMovie(m_movie);
625d14ab 99 Stop();
9e783cc0 100
3103e8a9 101 //Note that ExitMovies() is not necessary, but
9e783cc0
RD
102 //the docs are fuzzy on whether or not TerminateQTML is
103 ExitMovies();
104
4b430ee1 105#ifndef __WXMAC__
9e783cc0 106 TerminateQTML();
4b430ee1
DS
107#endif
108 }
109
110 void Shutdown()
111 {
112 delete this;
625d14ab 113 }
e40298d5 114
625d14ab
SC
115 void Notify()
116 {
4b430ee1
DS
117 if (m_pbPlaying && !*m_pbPlaying)
118 {
119 Shutdown();
120 }
121
625d14ab
SC
122 if(IsMovieDone(m_movie))
123 {
124 if (!m_bLoop)
125 Shutdown();
126 else
127 {
128 StopMovie(m_movie);
4b430ee1 129 GoToBeginningOfMovie(m_movie);
625d14ab 130 StartMovie(m_movie);
4b430ee1 131 }
625d14ab
SC
132 }
133 else
134 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
135 }
e40298d5 136
e40298d5 137
625d14ab 138 Movie& GetMovie() {return m_movie;}
4b430ee1 139
625d14ab
SC
140protected:
141 Movie m_movie;
142 bool m_bLoop;
4b430ee1
DS
143
144public:
145 bool* m_pbPlaying;
146
625d14ab 147};
e40298d5 148
e40298d5 149
625d14ab
SC
150class wxSMTimer : public wxTimer
151{
152public:
c22edec9
RD
153 wxSMTimer(void* hSnd, void* pSndChannel, bool bLoop, bool* playing)
154 : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(playing)
4b430ee1
DS
155 {
156 }
e40298d5 157
d3c7fc99 158 virtual ~wxSMTimer()
4b430ee1
DS
159 {
160 if(m_pbPlaying)
161 *m_pbPlaying = false;
162 SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
163 Stop();
164 }
e40298d5 165
625d14ab 166 void Notify()
4b430ee1
DS
167 {
168 if (m_pbPlaying && !*m_pbPlaying)
e40298d5 169 {
4b430ee1
DS
170 Shutdown();
171 }
172
173 SCStatus stat;
625d14ab
SC
174
175 if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
4b430ee1
DS
176 Shutdown();
177
178 //if the sound isn't playing anymore, see if it's looped,
179 //and if so play it again, otherwise close things up
180 if (stat.scChannelBusy == FALSE)
181 {
182 if (m_bLoop)
183 {
184 if(SndPlay((SndChannelPtr)m_pSndChannel, (SndListHandle) m_hSnd, true) != noErr)
185 Shutdown();
186 }
187 else
188 Shutdown();
e40298d5 189 }
625d14ab 190 }
e40298d5 191
625d14ab
SC
192 void Shutdown()
193 {
4b430ee1 194 delete this;
625d14ab 195 }
4b430ee1
DS
196
197 void* GetChannel() {return m_pSndChannel;}
198
625d14ab
SC
199protected:
200 void* m_hSnd;
201 void* m_pSndChannel;
202 bool m_bLoop;
4b430ee1
DS
203
204public:
205 bool* m_pbPlaying;
625d14ab
SC
206};
207
208// ------------------------------------------------------------------
209// wxSound
210// ------------------------------------------------------------------
211
212//Determines whether version 4 of QT is installed
213Boolean wxIsQuickTime4Installed (void)
214{
215#ifdef __WXMAC__
216 short error;
217 long result;
e40298d5 218
625d14ab 219 error = Gestalt (gestaltQuickTime, &result);
9e783cc0 220 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
625d14ab
SC
221#else
222 return true;
223#endif
224}
e40298d5 225
625d14ab
SC
226inline bool wxInitQT ()
227{
228 if (wxIsQuickTime4Installed())
229 {
230 #ifndef __WXMAC__
231 int nError;
232 //-2093 no dll
233 if ((nError = InitializeQTML(0)) != noErr)
6d3ef225 234 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
4b430ee1 235 #endif
625d14ab
SC
236 EnterMovies();
237 return true;
238 }
239 else
4b430ee1 240 {
7f0b95bb 241 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
4b430ee1
DS
242 return false;
243 }
625d14ab 244}
e40298d5 245
6239ee05
SC
246#endif
247
625d14ab
SC
248wxSound::wxSound()
249: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
250{
251}
252
253wxSound::wxSound(const wxString& sFileName, bool isResource)
254: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
255{
256 Create(sFileName, isResource);
257}
258
259wxSound::wxSound(int size, const wxByte* data)
260: m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
261{
625d14ab
SC
262}
263
264wxSound::~wxSound()
265{
625d14ab
SC
266}
267
268bool wxSound::Create(const wxString& fileName, bool isResource)
269{
4b430ee1 270 Stop();
2116a0d1 271
625d14ab
SC
272 if (isResource)
273 {
274#ifdef __WXMAC__
275 m_type = wxSound_RESOURCE;
276
277 Str255 lpSnd ;
278
279 wxMacStringToPascal( fileName , lpSnd ) ;
280
7f0b95bb 281 m_sndname = fileName;
625d14ab
SC
282 m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
283#else
284 return false;
285#endif
4b430ee1
DS
286 }
287 else
288 {
625d14ab
SC
289 m_type = wxSound_FILE;
290 m_sndname = fileName;
e40298d5
JS
291 }
292
625d14ab 293 return true;
e9576ca5
SC
294}
295
315ebf68 296bool wxSound::DoPlay(unsigned flags) const
e9576ca5 297{
4b430ee1 298 Stop();
4a69b060 299
6239ee05
SC
300#if USE_QUICKTIME
301
625d14ab
SC
302 Movie movie;
303
304 switch(m_type)
305 {
306 case wxSound_MEMORY:
307 {
4b430ee1
DS
308 if (!wxInitQT())
309 return false;
625d14ab 310 Handle myHandle, dataRef = nil;
4b430ee1 311 MovieImportComponent miComponent;
625d14ab
SC
312 Track targetTrack = nil;
313 TimeValue addedDuration = 0;
314 long outFlags = 0;
315 OSErr err;
316 ComponentResult result;
317
318 myHandle = NewHandleClear((Size)m_waveLength);
4b430ee1 319
625d14ab
SC
320 BlockMove(m_hSnd, *myHandle, m_waveLength);
321
322 err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
323
324 if (memcmp(&m_hSnd[8], "WAVE", 4) == 0)
325 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
326 else if (memcmp(&m_hSnd[8], "AIFF", 4) == 0)
327 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFF);
328 else if (memcmp(&m_hSnd[8], "AIFC", 4) == 0)
329 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFC);
330 else
4b430ee1 331 {
7f0b95bb 332 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
625d14ab
SC
333 return false;
334 }
335
336 movie = NewMovie(0);
4a69b060 337
625d14ab
SC
338 result = MovieImportDataRef(miComponent, dataRef,
339 HandleDataHandlerSubType, movie,
340 nil, &targetTrack,
341 nil, &addedDuration,
342 movieImportCreateTrack, &outFlags);
e9576ca5 343
625d14ab
SC
344 if (result != noErr)
345 {
346 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
4b430ee1 347 }
4a69b060 348
625d14ab
SC
349 SetMovieVolume(movie, kFullVolume);
350 GoToBeginningOfMovie(movie);
9e783cc0
RD
351
352 DisposeHandle(myHandle);
625d14ab
SC
353 }
354 break;
355 case wxSound_RESOURCE:
356 {
357 SoundComponentData data;
358 unsigned long numframes, offset;
e9576ca5 359
625d14ab
SC
360 ParseSndHeader((SndListHandle)m_hSnd, &data, &numframes, &offset);
361 //m_waveLength = numFrames * data.numChannels;
5b781a67 362
625d14ab
SC
363 SndChannelPtr pSndChannel;
364 SndNewChannel(&pSndChannel, sampledSynth,
4b430ee1
DS
365 initNoInterp
366 + (data.numChannels == 1 ? initMono : initStereo), NULL);
e40298d5 367
625d14ab 368 if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
4b430ee1 369 return false;
625d14ab
SC
370
371 if (flags & wxSOUND_ASYNC)
4b430ee1
DS
372 {
373 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
374 = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
c22edec9
RD
375 &lastSoundIsPlaying);
376 lastSoundIsPlaying = true;
4b430ee1
DS
377
378 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
379 }
380 else
625d14ab
SC
381 SndDisposeChannel(pSndChannel, TRUE);
382
383 return true;
384 }
385 break;
386 case wxSound_FILE:
387 {
4b430ee1
DS
388 if (!wxInitQT())
389 return false;
390
a2b77260 391 OSErr err = noErr ;
8e3f3880 392
fe8712b5
SC
393 Handle dataRef = NULL;
394 OSType dataRefType;
a2b77260 395
dbe4a80c 396 err = QTNewDataReferenceFromFullPathCFString(wxCFStringRef(m_sndname,wxLocale::GetSystemEncoding()),
fe8712b5 397 (UInt32)kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
8e3f3880 398
fe8712b5
SC
399 wxASSERT(err == noErr);
400
401 if (NULL != dataRef || err != noErr)
625d14ab 402 {
fe8712b5
SC
403 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
404 wxASSERT(err == noErr);
405 DisposeHandle(dataRef);
625d14ab 406 }
8e3f3880 407
625d14ab 408 if (err != noErr)
4b430ee1 409 {
625d14ab
SC
410 wxLogSysError(
411 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
412 );
413 return false;
4b430ee1 414 }
625d14ab
SC
415 }
416 break;
417 default:
418 return false;
419 }//end switch(m_type)
e40298d5 420
625d14ab
SC
421 //Start the movie!
422 StartMovie(movie);
e40298d5 423
c22edec9
RD
424 if (flags & wxSOUND_ASYNC)
425 {
426 //Start timer and play movie asyncronously
427 lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
428 new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
429 &lastSoundIsPlaying);
430 lastSoundIsPlaying = true;
431 ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
432 }
433 else
625d14ab 434 {
7f0b95bb 435 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
e40298d5 436
625d14ab 437 //Play movie until it ends, then exit
8e3f3880 438 //Note that due to quicktime caching this may not always
dcb68102 439 //work 100% correctly
625d14ab 440 while (!IsMovieDone(movie))
dcb68102 441 MoviesTask(movie, 1);
e40298d5 442
625d14ab
SC
443 DisposeMovie(movie);
444 }
6239ee05 445#endif
625d14ab
SC
446
447 return true;
5b781a67
SC
448}
449
4b430ee1 450bool wxSound::IsPlaying()
625d14ab 451{
4b430ee1 452 return lastSoundIsPlaying;
625d14ab 453}
5b781a67 454
4b430ee1 455void wxSound::Stop()
5b781a67 456{
c22edec9 457 if (lastSoundIsPlaying)
e40298d5 458 {
4b430ee1
DS
459 delete (wxTimer*&) lastSoundTimer;
460 lastSoundIsPlaying = false;
c22edec9 461 lastSoundTimer = NULL;
e40298d5 462 }
625d14ab 463}
9e783cc0 464
4b430ee1
DS
465void* wxSound::GetHandle()
466{
6239ee05 467#if USE_QUICKTIME
4b430ee1
DS
468 if(m_type == wxSound_RESOURCE)
469 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
9e783cc0 470
4b430ee1 471 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
6239ee05
SC
472#endif
473 return NULL;
4b430ee1 474}
9e783cc0 475
4b430ee1 476#endif //wxUSE_SOUND