]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/sound.cpp
new capture handling, can be turned off
[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
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
315ebf68 13#pragma implementation "sound.h"
e9576ca5
SC
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
e9576ca5
SC
18#include "wx/object.h"
19#include "wx/string.h"
625d14ab
SC
20#include "wx/log.h"
21#include "wx/file.h"
315ebf68 22#include "wx/sound.h"
625d14ab 23#include "wx/timer.h"
c9528e9d 24#include "wx/intl.h"
e9576ca5 25
315ebf68 26#if wxUSE_SOUND
4a69b060 27
625d14ab
SC
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
76a5e5d2 46#ifdef __WXMAC__
a2b77260 47#include "wx/mac/uma.h"
768c6e8b 48#ifndef __DARWIN__
625d14ab
SC
49#include <Movies.h>
50#include <Gestalt.h>
51#endif
768c6e8b 52#endif
625d14ab
SC
53
54#if defined __WXMAC__ && defined __DARWIN__/*TARGET_CARBON*/
55#ifdef __APPLE_CC__
56#include <Carbon/Carbon.h>
57#else
58#include <Carbon.h>
66a09d47 59#endif
625d14ab
SC
60#else
61#include <Sound.h>
76a5e5d2
SC
62#endif
63
625d14ab
SC
64//quicktime media layer only required for mac emulation on pc
65#ifndef __WXMAC__
66#include <qtml.h>
67#endif
e9576ca5 68
768c6e8b 69#ifndef __DARWIN__
625d14ab 70#include <QuickTimeComponents.h>
768c6e8b
SC
71#else
72#include <QuickTime/QuickTimeComponents.h>
73#endif
e9576ca5 74
c22edec9 75//Time between timer calls
625d14ab 76#define MOVIE_DELAY 100
e9576ca5 77
c22edec9
RD
78static wxTimer* lastSoundTimer=NULL;
79static bool lastSoundIsPlaying=false;
80
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
625d14ab
SC
92 ~wxQTTimer()
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
625d14ab 158 ~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
625d14ab
SC
246wxSound::wxSound()
247: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
248{
249}
250
251wxSound::wxSound(const wxString& sFileName, bool isResource)
252: m_hSnd(NULL), m_waveLength(0), m_pTimer(NULL), m_type(wxSound_NONE)
253{
254 Create(sFileName, isResource);
255}
256
257wxSound::wxSound(int size, const wxByte* data)
258: m_hSnd((char*)data), m_waveLength(size), m_pTimer(NULL), m_type(wxSound_MEMORY)
259{
625d14ab
SC
260}
261
262wxSound::~wxSound()
263{
625d14ab
SC
264}
265
266bool wxSound::Create(const wxString& fileName, bool isResource)
267{
4b430ee1 268 Stop();
2116a0d1 269
625d14ab
SC
270 if (isResource)
271 {
272#ifdef __WXMAC__
273 m_type = wxSound_RESOURCE;
274
275 Str255 lpSnd ;
276
277 wxMacStringToPascal( fileName , lpSnd ) ;
278
7f0b95bb 279 m_sndname = fileName;
625d14ab
SC
280 m_hSnd = (char*) GetNamedResource('snd ', (const unsigned char *) lpSnd);
281#else
282 return false;
283#endif
4b430ee1
DS
284 }
285 else
286 {
625d14ab
SC
287 m_type = wxSound_FILE;
288 m_sndname = fileName;
e40298d5
JS
289 }
290
625d14ab 291 return true;
e9576ca5
SC
292}
293
315ebf68 294bool wxSound::DoPlay(unsigned flags) const
e9576ca5 295{
4b430ee1 296 Stop();
4a69b060 297
625d14ab
SC
298 Movie movie;
299
300 switch(m_type)
301 {
302 case wxSound_MEMORY:
303 {
4b430ee1
DS
304 if (!wxInitQT())
305 return false;
625d14ab 306 Handle myHandle, dataRef = nil;
4b430ee1 307 MovieImportComponent miComponent;
625d14ab
SC
308 Track targetTrack = nil;
309 TimeValue addedDuration = 0;
310 long outFlags = 0;
311 OSErr err;
312 ComponentResult result;
313
314 myHandle = NewHandleClear((Size)m_waveLength);
4b430ee1 315
625d14ab
SC
316 BlockMove(m_hSnd, *myHandle, m_waveLength);
317
318 err = PtrToHand(&myHandle, &dataRef, sizeof(Handle));
319
320 if (memcmp(&m_hSnd[8], "WAVE", 4) == 0)
321 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeWave);
322 else if (memcmp(&m_hSnd[8], "AIFF", 4) == 0)
323 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFF);
324 else if (memcmp(&m_hSnd[8], "AIFC", 4) == 0)
325 miComponent = OpenDefaultComponent(MovieImportType, kQTFileTypeAIFC);
326 else
4b430ee1 327 {
7f0b95bb 328 wxLogSysError(wxT("wxSound - Location in memory does not contain valid data"));
625d14ab
SC
329 return false;
330 }
331
332 movie = NewMovie(0);
4a69b060 333
625d14ab
SC
334 result = MovieImportDataRef(miComponent, dataRef,
335 HandleDataHandlerSubType, movie,
336 nil, &targetTrack,
337 nil, &addedDuration,
338 movieImportCreateTrack, &outFlags);
e9576ca5 339
625d14ab
SC
340 if (result != noErr)
341 {
342 wxLogSysError(wxString::Format(wxT("Couldn't import movie data\nError:%i"), (int)result));
4b430ee1 343 }
4a69b060 344
625d14ab
SC
345 SetMovieVolume(movie, kFullVolume);
346 GoToBeginningOfMovie(movie);
9e783cc0
RD
347
348 DisposeHandle(myHandle);
625d14ab
SC
349 }
350 break;
351 case wxSound_RESOURCE:
352 {
353 SoundComponentData data;
354 unsigned long numframes, offset;
e9576ca5 355
625d14ab
SC
356 ParseSndHeader((SndListHandle)m_hSnd, &data, &numframes, &offset);
357 //m_waveLength = numFrames * data.numChannels;
5b781a67 358
625d14ab
SC
359 SndChannelPtr pSndChannel;
360 SndNewChannel(&pSndChannel, sampledSynth,
4b430ee1
DS
361 initNoInterp
362 + (data.numChannels == 1 ? initMono : initStereo), NULL);
e40298d5 363
625d14ab 364 if(SndPlay(pSndChannel, (SndListHandle) m_hSnd, flags & wxSOUND_ASYNC ? 1 : 0) != noErr)
4b430ee1 365 return false;
625d14ab
SC
366
367 if (flags & wxSOUND_ASYNC)
4b430ee1
DS
368 {
369 lastSoundTimer = ((wxSMTimer*&)m_pTimer)
370 = new wxSMTimer(pSndChannel, m_hSnd, flags & wxSOUND_LOOP ? 1 : 0,
c22edec9
RD
371 &lastSoundIsPlaying);
372 lastSoundIsPlaying = true;
4b430ee1
DS
373
374 ((wxTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
375 }
376 else
625d14ab
SC
377 SndDisposeChannel(pSndChannel, TRUE);
378
379 return true;
380 }
381 break;
382 case wxSound_FILE:
383 {
4b430ee1
DS
384 if (!wxInitQT())
385 return false;
386
a2b77260 387 OSErr err = noErr ;
bc57786b
RN
388//NB: RN: Stefan - I think the 10.3 path functions are broken if kQTNativeDefaultPathStyle is
389//going to trigger a warning every time it is used - where its _supposed to be used_!!
390//(kQTNativePathStyle is negative but the function argument is unsigned!)
391//../src/mac/carbon/sound.cpp: In member function `virtual bool
392// wxSound::DoPlay(unsigned int) const':
393//../src/mac/carbon/sound.cpp:387: warning: passing negative value `
394// kQTNativeDefaultPathStyle' for argument passing 2 of `OSErr
395// QTNewDataReferenceFromFullPathCFString(const __CFString*, long unsigned int,
396// long unsigned int, char***, OSType*)'
397//../src/mac/carbon/sound.cpp:387: warning: argument of negative value `
398// kQTNativeDefaultPathStyle' to `long unsigned int'
a2b77260
SC
399#if defined( __WXMAC__ ) && TARGET_API_MAC_OSX && ( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 )
400 if ( UMAGetSystemVersion() >= 0x1030 )
4b430ee1 401 {
a2b77260
SC
402 Handle dataRef = NULL;
403 OSType dataRefType;
404
405 err = QTNewDataReferenceFromFullPathCFString(wxMacCFStringHolder(m_sndname,wxLocale::GetSystemEncoding()),
bc57786b
RN
406 //FIXME: Why does this have to be casted?
407 (unsigned int)kQTNativeDefaultPathStyle,
408 //FIXME: End
409 0, &dataRef, &dataRefType);
a2b77260 410
bc57786b
RN
411 wxASSERT(err == noErr);
412
413 if (NULL != dataRef || err != noErr)
a2b77260
SC
414 {
415 err = NewMovieFromDataRef( &movie, newMovieDontAskUnresolvedDataRefs , NULL, dataRef, dataRefType );
bc57786b 416 wxASSERT(err == noErr);
a2b77260
SC
417 DisposeHandle(dataRef);
418 }
4b430ee1 419 }
a2b77260 420 else
625d14ab 421#endif
625d14ab 422 {
a2b77260
SC
423 short movieResFile;
424 FSSpec sfFile;
425#ifdef __WXMAC__
426 wxMacFilename2FSSpec( m_sndname , &sfFile ) ;
427#else
428 int nError;
429 if ((nError = NativePathNameToFSSpec ((char*) m_sndname.c_str(), &sfFile, 0)) != noErr)
430 {
65442ab6 431/*
a2b77260
SC
432 wxLogSysError(wxString::Format(wxT("File:%s does not exist\nError:%i"),
433 m_sndname.c_str(), nError));
65442ab6 434*/
a2b77260
SC
435 return false;
436 }
437#endif
438 if (OpenMovieFile (&sfFile, &movieResFile, fsRdPerm) != noErr)
439 {
440 wxLogSysError(wxT("Quicktime couldn't open the file"));
441 return false;
442 }
443 short movieResID = 0;
444 Str255 movieName;
445
446 err = NewMovieFromFile (
447 &movie,
448 movieResFile,
449 &movieResID,
450 movieName,
451 newMovieActive,
452 NULL); //wasChanged
453
454 CloseMovieFile (movieResFile);
625d14ab 455 }
a2b77260 456
625d14ab 457 if (err != noErr)
4b430ee1 458 {
625d14ab
SC
459 wxLogSysError(
460 wxString::Format(wxT("wxSound - Could not open file: %s\nError:%i"), m_sndname.c_str(), err )
461 );
462 return false;
4b430ee1 463 }
625d14ab
SC
464 }
465 break;
466 default:
467 return false;
468 }//end switch(m_type)
e40298d5 469
625d14ab
SC
470 //Start the movie!
471 StartMovie(movie);
e40298d5 472
c22edec9
RD
473 if (flags & wxSOUND_ASYNC)
474 {
475 //Start timer and play movie asyncronously
476 lastSoundTimer = ((wxQTTimer*&)m_pTimer) =
477 new wxQTTimer(movie, flags & wxSOUND_LOOP ? 1 : 0,
478 &lastSoundIsPlaying);
479 lastSoundIsPlaying = true;
480 ((wxQTTimer*)m_pTimer)->Start(MOVIE_DELAY, wxTIMER_CONTINUOUS);
481 }
482 else
625d14ab 483 {
7f0b95bb 484 wxASSERT_MSG(!(flags & wxSOUND_LOOP), wxT("Can't loop and play syncronously at the same time"));
e40298d5 485
625d14ab 486 //Play movie until it ends, then exit
dcb68102
RN
487 //Note that due to quicktime caching this may not always
488 //work 100% correctly
625d14ab 489 while (!IsMovieDone(movie))
dcb68102 490 MoviesTask(movie, 1);
e40298d5 491
625d14ab
SC
492 DisposeMovie(movie);
493 }
625d14ab
SC
494
495 return true;
5b781a67
SC
496}
497
4b430ee1 498bool wxSound::IsPlaying()
625d14ab 499{
4b430ee1 500 return lastSoundIsPlaying;
625d14ab 501}
5b781a67 502
4b430ee1 503void wxSound::Stop()
5b781a67 504{
c22edec9 505 if (lastSoundIsPlaying)
e40298d5 506 {
4b430ee1
DS
507 delete (wxTimer*&) lastSoundTimer;
508 lastSoundIsPlaying = false;
c22edec9 509 lastSoundTimer = NULL;
e40298d5 510 }
625d14ab 511}
9e783cc0 512
4b430ee1
DS
513void* wxSound::GetHandle()
514{
515 if(m_type == wxSound_RESOURCE)
516 return (void*) ((wxSMTimer*)m_pTimer)->GetChannel();
9e783cc0 517
4b430ee1
DS
518 return (void*) ((wxQTTimer*) m_pTimer)->GetMovie();
519}
9e783cc0 520
4b430ee1 521#endif //wxUSE_SOUND