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