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