]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/sound.cpp
Corrected wxRTTI for wxNotebook so dynamic casting to wxBookCtrlBase works
[wxWidgets.git] / src / mac / carbon / sound.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/sound.cpp
3// Purpose: wxSound class implementation: optional
4// Author: Ryan Norton
5// Modified by: Stefan Csomor
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Ryan Norton
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if wxUSE_SOUND
16
17#include "wx/sound.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/object.h"
21 #include "wx/string.h"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24 #include "wx/timer.h"
25#endif
26
27#include "wx/file.h"
28
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) Path as CFString
40// 2) Call QTNewDataReferenceFromFullPathCFString
41// 3) Call NewMovieFromDataRef
42// 4) Call CloseMovieFile
43// 4) PlayMovie();
44// 5) IsMovieDone(), MoviesTask() //2nd param is minimum wait time to allocate to quicktime
45//
46
47#ifdef __WXMAC__
48#include "wx/mac/uma.h"
49#ifndef __DARWIN__
50#include <Movies.h>
51#include <Gestalt.h>
52#endif
53#endif
54
55#include <Carbon/Carbon.h>
56
57//quicktime media layer only required for mac emulation on pc
58#ifndef __WXMAC__
59#include <qtml.h>
60#endif
61
62#ifndef __DARWIN__
63#include <QuickTimeComponents.h>
64#else
65#include <QuickTime/QuickTimeComponents.h>
66#endif
67
68//Time between timer calls
69#define MOVIE_DELAY 100
70
71static wxTimer* lastSoundTimer=NULL;
72static bool lastSoundIsPlaying=false;
73
74#if !defined(__LP64__)
75#define USE_QUICKTIME 1
76#else
77#define USE_QUICKTIME 0
78#endif
79
80#if USE_QUICKTIME
81// ------------------------------------------------------------------
82// wxQTTimer - Handle Asyncronous Playing
83// ------------------------------------------------------------------
84class wxQTTimer : public wxTimer
85{
86public:
87 wxQTTimer(Movie movie, bool bLoop, bool* playing) :
88 m_movie(movie), m_bLoop(bLoop), m_pbPlaying(playing)
89 {
90 }
91
92 virtual ~wxQTTimer()
93 {
94 if(m_pbPlaying)
95 *m_pbPlaying = false;
96
97 StopMovie(m_movie);
98 DisposeMovie(m_movie);
99 Stop();
100
101 //Note that ExitMovies() is not necessary, but
102 //the docs are fuzzy on whether or not TerminateQTML is
103 ExitMovies();
104
105#ifndef __WXMAC__
106 TerminateQTML();
107#endif
108 }
109
110 void Shutdown()
111 {
112 delete this;
113 }
114
115 void Notify()
116 {
117 if (m_pbPlaying && !*m_pbPlaying)
118 {
119 Shutdown();
120 }
121
122 if(IsMovieDone(m_movie))
123 {
124 if (!m_bLoop)
125 Shutdown();
126 else
127 {
128 StopMovie(m_movie);
129 GoToBeginningOfMovie(m_movie);
130 StartMovie(m_movie);
131 }
132 }
133 else
134 MoviesTask(m_movie, MOVIE_DELAY); //Give QT time to play movie
135 }
136
137
138 Movie& GetMovie() {return m_movie;}
139
140protected:
141 Movie m_movie;
142 bool m_bLoop;
143
144public:
145 bool* m_pbPlaying;
146
147};
148
149
150class wxSMTimer : public wxTimer
151{
152public:
153 wxSMTimer(void* hSnd, void* pSndChannel, bool bLoop, bool* playing)
154 : m_hSnd(hSnd), m_pSndChannel(pSndChannel), m_bLoop(bLoop), m_pbPlaying(playing)
155 {
156 }
157
158 virtual ~wxSMTimer()
159 {
160 if(m_pbPlaying)
161 *m_pbPlaying = false;
162 SndDisposeChannel((SndChannelPtr)m_pSndChannel, TRUE);
163 Stop();
164 }
165
166 void Notify()
167 {
168 if (m_pbPlaying && !*m_pbPlaying)
169 {
170 Shutdown();
171 }
172
173 SCStatus stat;
174
175 if (SndChannelStatus((SndChannelPtr)m_pSndChannel, sizeof(SCStatus), &stat) != 0)
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();
189 }
190 }
191
192 void Shutdown()
193 {
194 delete this;
195 }
196
197 void* GetChannel() {return m_pSndChannel;}
198
199protected:
200 void* m_hSnd;
201 void* m_pSndChannel;
202 bool m_bLoop;
203
204public:
205 bool* m_pbPlaying;
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;
218
219 error = Gestalt (gestaltQuickTime, &result);
220 return (error == noErr) && (((result >> 16) & 0xffff) >= 0x0400);
221#else
222 return true;
223#endif
224}
225
226inline bool wxInitQT ()
227{
228 if (wxIsQuickTime4Installed())
229 {
230 #ifndef __WXMAC__
231 int nError;
232 //-2093 no dll
233 if ((nError = InitializeQTML(0)) != noErr)
234 wxLogSysError(wxString::Format(wxT("Couldn't Initialize Quicktime-%i"), nError));
235 #endif
236 EnterMovies();
237 return true;
238 }
239 else
240 {
241 wxLogSysError(wxT("Quicktime is not installed, or Your Version of Quicktime is <= 4."));
242 return false;
243 }
244}
245
246#endif
247
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{
262}
263
264wxSound::~wxSound()
265{
266}
267
268bool wxSound::Create(const wxString& fileName, bool isResource)
269{
270 Stop();
271
272 if (isResource)
273 {
274#ifdef __WXMAC__
275 m_type = wxSound_RESOURCE;
276
277 Str255 lpSnd ;
278
279 wxMacStringToPascal( fileName , lpSnd ) ;
280
281 m_sndname = fileName;
282 m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
283#else
284 return false;
285#endif
286 }
287 else
288 {
289 m_type = wxSound_FILE;
290 m_sndname = fileName;
291 }
292
293 return true;
294}
295
296bool wxSound::DoPlay(unsigned flags) const
297{
298 Stop();
299
300#if USE_QUICKTIME
301
302 Movie movie;
303
304 switch(m_type)
305 {
306 case wxSound_MEMORY:
307 {
308 if (!wxInitQT())
309 return false;
310 Handle myHandle, dataRef = nil;
311 MovieImportComponent miComponent;
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);
319
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
331 {
332 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
333 return false;
334 }
335
336 movie = NewMovie(0);
337
338 result = MovieImportDataRef(miComponent, dataRef,
339 HandleDataHandlerSubType, movie,
340 nil, &targetTrack,
341 nil, &addedDuration,
342 movieImportCreateTrack, &outFlags);
343
344 if (result != noErr)
345 {
346 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
347 }
348
349 SetMovieVolume(movie, kFullVolume);
350 GoToBeginningOfMovie(movie);
351
352 DisposeHandle(myHandle);
353 }
354 break;
355 case wxSound_RESOURCE:
356 {
357 SoundComponentData data;
358 unsigned long numframes, offset;
359
360 ParseSndHeader((SndListHandle)m_hSnd, &data, &numframes, &offset);
361 //m_waveLength = numFrames * data.numChannels;
362
363 SndChannelPtr pSndChannel;
364 SndNewChannel(&pSndChannel, sampledSynth,
365 initNoInterp
366 + (data.numChannels == 1 ? initMono : initStereo), NULL);
367
368 if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
369 return false;
370
371 if (flags & wxSOUND_ASYNC)
372 {
373 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
374 = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
375 &lastSoundIsPlaying);
376 lastSoundIsPlaying = true;
377
378 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
379 }
380 else
381 SndDisposeChannel(pSndChannel, TRUE);
382
383 return true;
384 }
385 break;
386 case wxSound_FILE:
387 {
388 if (!wxInitQT())
389 return false;
390
391 OSErr err = noErr ;
392
393 Handle dataRef = NULL;
394 OSType dataRefType;
395
396 err = QTNewDataReferenceFromFullPathCFString(wxCFStringRef(m_sndname,wxLocale::GetSystemEncoding()),
397 (UInt32)kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType);
398
399 wxASSERT(err == noErr);
400
401 if (NULL != dataRef || err != noErr)
402 {
403 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
404 wxASSERT(err == noErr);
405 DisposeHandle(dataRef);
406 }
407
408 if (err != noErr)
409 {
410 wxLogSysError(
411 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
412 );
413 return false;
414 }
415 }
416 break;
417 default:
418 return false;
419 }//end switch(m_type)
420
421 //Start the movie!
422 StartMovie(movie);
423
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
434 {
435 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
436
437 //Play movie until it ends, then exit
438 //Note that due to quicktime caching this may not always
439 //work 100% correctly
440 while (!IsMovieDone(movie))
441 MoviesTask(movie, 1);
442
443 DisposeMovie(movie);
444 }
445#endif
446
447 return true;
448}
449
450bool wxSound::IsPlaying()
451{
452 return lastSoundIsPlaying;
453}
454
455void wxSound::Stop()
456{
457 if (lastSoundIsPlaying)
458 {
459 delete (wxTimer*&) lastSoundTimer;
460 lastSoundIsPlaying = false;
461 lastSoundTimer = NULL;
462 }
463}
464
465void* wxSound::GetHandle()
466{
467#if USE_QUICKTIME
468 if(m_type == wxSound_RESOURCE)
469 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
470
471 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
472#endif
473 return NULL;
474}
475
476#endif //wxUSE_SOUND