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