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