added wx_truncate_cast and use it (sometimes instead of wx_static_cast) to explicitel...
[wxWidgets.git] / src / unix / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: wxSound
4 // Author: Marcel Rasche, Vaclav Slavik
5 // Modified by:
6 // Created: 25/10/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Open Source Applications Foundation
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "sound.h"
14 #pragma implementation "soundbase.h"
15 #endif
16
17 // for compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #include "wx/setup.h"
21
22 #if defined(__BORLANDC__)
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_SOUND
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32
33 #ifdef HAVE_SYS_SOUNDCARD_H
34 #include <sys/soundcard.h>
35 #endif
36
37 #ifndef WX_PRECOMP
38 #include "wx/event.h"
39 #include "wx/intl.h"
40 #include "wx/log.h"
41 #endif
42
43 #include "wx/thread.h"
44 #include "wx/file.h"
45 #include "wx/module.h"
46 #include "wx/sound.h"
47 #include "wx/dynlib.h"
48
49
50 #if wxUSE_THREADS
51 // mutex for all wxSound's synchronization
52 static wxMutex gs_soundMutex;
53 #endif
54
55 // ----------------------------------------------------------------------------
56 // wxSoundData
57 // ----------------------------------------------------------------------------
58
59 void wxSoundData::IncRef()
60 {
61 #if wxUSE_THREADS
62 wxMutexLocker locker(gs_soundMutex);
63 #endif
64 m_refCnt++;
65 }
66
67 void wxSoundData::DecRef()
68 {
69 #if wxUSE_THREADS
70 wxMutexLocker locker(gs_soundMutex);
71 #endif
72 if (--m_refCnt == 0)
73 delete this;
74 }
75
76 wxSoundData::~wxSoundData()
77 {
78 delete[] m_dataWithHeader;
79 }
80
81
82 // ----------------------------------------------------------------------------
83 // wxSoundBackendNull, used in absence of audio API or card
84 // ----------------------------------------------------------------------------
85
86 class wxSoundBackendNull : public wxSoundBackend
87 {
88 public:
89 wxString GetName() const { return _("No sound"); }
90 int GetPriority() const { return 0; }
91 bool IsAvailable() const { return true; }
92 bool HasNativeAsyncPlayback() const { return true; }
93 bool Play(wxSoundData *WXUNUSED(data), unsigned WXUNUSED(flags),
94 volatile wxSoundPlaybackStatus *WXUNUSED(status))
95 { return true; }
96 void Stop() {}
97 bool IsPlaying() const { return false; }
98 };
99
100
101 // ----------------------------------------------------------------------------
102 // wxSoundBackendOSS, for Linux
103 // ----------------------------------------------------------------------------
104
105 #ifdef HAVE_SYS_SOUNDCARD_H
106
107 #ifndef AUDIODEV
108 #define AUDIODEV "/dev/dsp" // Default path for audio device
109 #endif
110
111 class wxSoundBackendOSS : public wxSoundBackend
112 {
113 public:
114 wxString GetName() const { return _T("Open Sound System"); }
115 int GetPriority() const { return 10; }
116 bool IsAvailable() const;
117 bool HasNativeAsyncPlayback() const { return false; }
118 bool Play(wxSoundData *data, unsigned flags,
119 volatile wxSoundPlaybackStatus *status);
120 void Stop() {}
121 bool IsPlaying() const { return false; }
122
123 private:
124 int OpenDSP(const wxSoundData *data);
125 bool InitDSP(int dev, const wxSoundData *data);
126
127 int m_DSPblkSize; // Size of the DSP buffer
128 bool m_needConversion;
129 };
130
131 bool wxSoundBackendOSS::IsAvailable() const
132 {
133 int fd;
134 fd = open(AUDIODEV, O_WRONLY | O_NONBLOCK);
135 if (fd < 0)
136 return false;
137 close(fd);
138 return true;
139 }
140
141 bool wxSoundBackendOSS::Play(wxSoundData *data, unsigned flags,
142 volatile wxSoundPlaybackStatus *status)
143 {
144 int dev = OpenDSP(data);
145
146 if (dev < 0)
147 return false;
148
149 ioctl(dev, SNDCTL_DSP_SYNC, 0);
150
151 do
152 {
153 bool play = true;
154 int i;
155 unsigned l = 0;
156 size_t datasize = data->m_dataBytes;
157
158 do
159 {
160 if (status->m_stopRequested)
161 {
162 wxLogTrace(_T("sound"), _T("playback stopped"));
163 close(dev);
164 return true;
165 }
166
167 i= (int)((l + m_DSPblkSize) < datasize ?
168 m_DSPblkSize : (datasize - l));
169 if (write(dev, &data->m_data[l], i) != i)
170 {
171 play = false;
172 }
173 l += i;
174 } while (play && l < datasize);
175 } while (flags & wxSOUND_LOOP);
176
177 close(dev);
178 return true;
179 }
180
181 int wxSoundBackendOSS::OpenDSP(const wxSoundData *data)
182 {
183 int dev = -1;
184
185 if ((dev = open(AUDIODEV, O_WRONLY, 0)) <0)
186 return -1;
187
188 if (!InitDSP(dev, data) || m_needConversion)
189 {
190 close(dev);
191 return -1;
192 }
193
194 return dev;
195 }
196
197
198 bool wxSoundBackendOSS::InitDSP(int dev, const wxSoundData *data)
199 {
200 unsigned tmp;
201
202 // Reset the dsp
203 if (ioctl(dev, SNDCTL_DSP_RESET, 0) < 0)
204 {
205 wxLogTrace(_T("sound"), _T("unable to reset dsp"));
206 return false;
207 }
208
209 m_needConversion = false;
210
211 tmp = data->m_bitsPerSample;
212 if (ioctl(dev, SNDCTL_DSP_SAMPLESIZE, &tmp) < 0)
213 {
214 wxLogTrace(_T("sound"), _T("IOCTL failure (SNDCTL_DSP_SAMPLESIZE)"));
215 return false;
216 }
217 if (tmp != data->m_bitsPerSample)
218 {
219 wxLogTrace(_T("sound"),
220 _T("Unable to set DSP sample size to %d (wants %d)"),
221 data->m_bitsPerSample, tmp);
222 m_needConversion = true;
223 }
224
225 unsigned stereo = data->m_channels == 1 ? 0 : 1;
226 tmp = stereo;
227 if (ioctl(dev, SNDCTL_DSP_STEREO, &tmp) < 0)
228 {
229 wxLogTrace(_T("sound"), _T("IOCTL failure (SNDCTL_DSP_STEREO)"));
230 return false;
231 }
232 if (tmp != stereo)
233 {
234 wxLogTrace(_T("sound"), _T("Unable to set DSP to %s."), stereo? _T("stereo"):_T("mono"));
235 m_needConversion = true;
236 }
237
238 tmp = data->m_samplingRate;
239 if (ioctl(dev, SNDCTL_DSP_SPEED, &tmp) < 0)
240 {
241 wxLogTrace(_T("sound"), _T("IOCTL failure (SNDCTL_DSP_SPEED)"));
242 return false;
243 }
244 if (tmp != data->m_samplingRate)
245 {
246 // If the rate the sound card is using is not within 1% of what the
247 // data specified then override the data setting. The only reason not
248 // to always override this is because of clock-rounding
249 // problems. Sound cards will sometimes use things like 44101 when you
250 // ask for 44100. No need overriding this and having strange output
251 // file rates for something that we can't hear anyways.
252 if (data->m_samplingRate - tmp > (tmp * .01) ||
253 tmp - data->m_samplingRate > (tmp * .01)) {
254 wxLogTrace(_T("sound"),
255 _T("Unable to set DSP sampling rate to %d (wants %d)"),
256 data->m_samplingRate, tmp);
257 m_needConversion = true;
258 }
259 }
260
261 // Do this last because some drivers can adjust the buffer sized based on
262 // the sampling rate, etc.
263 if (ioctl(dev, SNDCTL_DSP_GETBLKSIZE, &m_DSPblkSize) < 0)
264 {
265 wxLogTrace(_T("sound"), _T("IOCTL failure (SNDCTL_DSP_GETBLKSIZE)"));
266 return false;
267 }
268 return true;
269 }
270
271 #endif // HAVE_SYS_SOUNDCARD_H
272
273 // ----------------------------------------------------------------------------
274 // wxSoundSyncOnlyAdaptor
275 // ----------------------------------------------------------------------------
276
277 #if wxUSE_THREADS
278
279 class wxSoundSyncOnlyAdaptor;
280
281 // this class manages asynchronous playback of audio if the backend doesn't
282 // support it natively (e.g. OSS backend)
283 class wxSoundAsyncPlaybackThread : public wxThread
284 {
285 public:
286 wxSoundAsyncPlaybackThread(wxSoundSyncOnlyAdaptor *adaptor,
287 wxSoundData *data, unsigned flags)
288 : wxThread(), m_adapt(adaptor), m_data(data), m_flags(flags) {}
289 virtual ExitCode Entry();
290
291 protected:
292 wxSoundSyncOnlyAdaptor *m_adapt;
293 wxSoundData *m_data;
294 unsigned m_flags;
295 };
296
297 #endif // wxUSE_THREADS
298
299 // This class turns wxSoundBackend that doesn't support asynchronous playback
300 // into one that does
301 class wxSoundSyncOnlyAdaptor : public wxSoundBackend
302 {
303 public:
304 wxSoundSyncOnlyAdaptor(wxSoundBackend *backend)
305 : m_backend(backend), m_playing(false) {}
306 ~wxSoundSyncOnlyAdaptor()
307 {
308 delete m_backend;
309 }
310 wxString GetName() const
311 {
312 return m_backend->GetName();
313 }
314 int GetPriority() const
315 {
316 return m_backend->GetPriority();
317 }
318 bool IsAvailable() const
319 {
320 return m_backend->IsAvailable();
321 }
322 bool HasNativeAsyncPlayback() const
323 {
324 return true;
325 }
326 bool Play(wxSoundData *data, unsigned flags,
327 volatile wxSoundPlaybackStatus *status);
328 void Stop();
329 bool IsPlaying() const;
330
331 private:
332 friend class wxSoundAsyncPlaybackThread;
333
334 wxSoundBackend *m_backend;
335 bool m_playing;
336 #if wxUSE_THREADS
337 // player thread holds this mutex and releases it after it finishes
338 // playing, so that the main thread knows when it can play sound
339 wxMutex m_mutexRightToPlay;
340 wxSoundPlaybackStatus m_status;
341 #endif
342 };
343
344
345 #if wxUSE_THREADS
346 wxThread::ExitCode wxSoundAsyncPlaybackThread::Entry()
347 {
348 m_adapt->m_backend->Play(m_data, m_flags & ~wxSOUND_ASYNC,
349 &m_adapt->m_status);
350
351 m_data->DecRef();
352 m_adapt->m_playing = false;
353 m_adapt->m_mutexRightToPlay.Unlock();
354 wxLogTrace(_T("sound"), _T("terminated async playback thread"));
355 return 0;
356 }
357 #endif
358
359 bool wxSoundSyncOnlyAdaptor::Play(wxSoundData *data, unsigned flags,
360 volatile wxSoundPlaybackStatus *status)
361 {
362 Stop();
363 if (flags & wxSOUND_ASYNC)
364 {
365 #if wxUSE_THREADS
366 m_mutexRightToPlay.Lock();
367 m_status.m_playing = true;
368 m_status.m_stopRequested = false;
369 data->IncRef();
370 wxThread *th = new wxSoundAsyncPlaybackThread(this, data, flags);
371 th->Create();
372 th->Run();
373 wxLogTrace(_T("sound"), _T("launched async playback thread"));
374 return true;
375 #else
376 wxLogError(_("Unable to play sound asynchronously."));
377 return false;
378 #endif
379 }
380 else
381 {
382 #if wxUSE_THREADS
383 m_mutexRightToPlay.Lock();
384 #endif
385 bool rv = m_backend->Play(data, flags, status);
386 #if wxUSE_THREADS
387 m_mutexRightToPlay.Unlock();
388 #endif
389 return rv;
390 }
391 }
392
393 void wxSoundSyncOnlyAdaptor::Stop()
394 {
395 wxLogTrace(_T("sound"), _T("asking audio to stop"));
396
397 #if wxUSE_THREADS
398 // tell the player thread (if running) to stop playback ASAP:
399 m_status.m_stopRequested = true;
400
401 // acquire the mutex to be sure no sound is being played, then
402 // release it because we don't need it for anything (the effect of this
403 // is that calling thread will wait until playback thread reacts to
404 // our request to interrupt playback):
405 m_mutexRightToPlay.Lock();
406 m_mutexRightToPlay.Unlock();
407 wxLogTrace(_T("sound"), _T("audio was stopped"));
408 #endif
409 }
410
411 bool wxSoundSyncOnlyAdaptor::IsPlaying() const
412 {
413 #if wxUSE_THREADS
414 return m_status.m_playing;
415 #else
416 return false;
417 #endif
418 }
419
420
421 // ----------------------------------------------------------------------------
422 // wxSound
423 // ----------------------------------------------------------------------------
424
425 wxSoundBackend *wxSound::ms_backend = NULL;
426
427 // FIXME - temporary, until we have plugins architecture
428 #if wxUSE_LIBSDL
429 #if wxUSE_PLUGINS
430 wxDynamicLibrary *wxSound::ms_backendSDL = NULL;
431 #else
432 extern "C" wxSoundBackend *wxCreateSoundBackendSDL();
433 #endif
434 #endif
435
436 wxSound::wxSound() : m_data(NULL)
437 {
438 }
439
440 wxSound::wxSound(const wxString& sFileName, bool isResource) : m_data(NULL)
441 {
442 Create(sFileName, isResource);
443 }
444
445 wxSound::wxSound(int size, const wxByte* data) : m_data(NULL)
446 {
447 Create(size, data);
448 }
449
450 wxSound::~wxSound()
451 {
452 Free();
453 }
454
455 bool wxSound::Create(const wxString& fileName, bool isResource)
456 {
457 wxASSERT_MSG( !isResource,
458 _T("Loading sound from resources is only supported on Windows") );
459
460 Free();
461
462 wxFile fileWave;
463 if (!fileWave.Open(fileName, wxFile::read))
464 {
465 return false;
466 }
467
468 wxFileOffset lenOrig = fileWave.Length();
469 if ( lenOrig == wxInvalidOffset )
470 return false;
471
472 size_t len = wx_truncate_cast(size_t, lenOrig);
473 wxUint8 *data = new wxUint8[len];
474 if (fileWave.Read(data, len) != len)
475 {
476 wxLogError(_("Couldn't load sound data from '%s'."), fileName.c_str());
477 return false;
478 }
479
480 if (!LoadWAV(data, len, false))
481 {
482 wxLogError(_("Sound file '%s' is in unsupported format."),
483 fileName.c_str());
484 return false;
485 }
486
487 return true;
488 }
489
490 bool wxSound::Create(int size, const wxByte* data)
491 {
492 wxASSERT( data != NULL );
493
494 Free();
495 if (!LoadWAV(data, size, true))
496 {
497 wxLogError(_("Sound data are in unsupported format."));
498 return false;
499 }
500 return true;
501 }
502
503 /*static*/ void wxSound::EnsureBackend()
504 {
505 if (!ms_backend)
506 {
507 // FIXME -- make this fully dynamic when plugins architecture is in
508 // place
509 #if wxUSE_LIBSDL
510 //if (!ms_backend)
511 {
512 #if !wxUSE_PLUGINS
513 ms_backend = wxCreateSoundBackendSDL();
514 #else
515 wxString dllname;
516 dllname.Printf(_T("%s/%s"),
517 wxDynamicLibrary::GetPluginsDirectory().c_str(),
518 wxDynamicLibrary::CanonicalizePluginName(
519 _T("sound_sdl"), wxDL_PLUGIN_BASE).c_str());
520 wxLogTrace(_T("sound"),
521 _T("trying to load SDL plugin from '%s'..."),
522 dllname.c_str());
523 wxLogNull null;
524 ms_backendSDL = new wxDynamicLibrary(dllname, wxDL_NOW);
525 if (!ms_backendSDL->IsLoaded())
526 {
527 wxDELETE(ms_backendSDL);
528 }
529 else
530 {
531 typedef wxSoundBackend *(*wxCreateSoundBackend_t)();
532 wxDYNLIB_FUNCTION(wxCreateSoundBackend_t,
533 wxCreateSoundBackendSDL, *ms_backendSDL);
534 if (pfnwxCreateSoundBackendSDL)
535 {
536 ms_backend = (*pfnwxCreateSoundBackendSDL)();
537 }
538 }
539 #endif
540 if (ms_backend && !ms_backend->IsAvailable())
541 {
542 wxDELETE(ms_backend);
543 }
544 }
545 #endif
546
547 #ifdef HAVE_SYS_SOUNDCARD_H
548 if (!ms_backend)
549 {
550 ms_backend = new wxSoundBackendOSS();
551 if (!ms_backend->IsAvailable())
552 {
553 wxDELETE(ms_backend);
554 }
555 }
556 #endif
557
558 if (!ms_backend)
559 ms_backend = new wxSoundBackendNull();
560
561 if (!ms_backend->HasNativeAsyncPlayback())
562 ms_backend = new wxSoundSyncOnlyAdaptor(ms_backend);
563
564 wxLogTrace(_T("sound"),
565 _T("using backend '%s'"), ms_backend->GetName().c_str());
566 }
567 }
568
569 /*static*/ void wxSound::UnloadBackend()
570 {
571 if (ms_backend)
572 {
573 wxLogTrace(_T("sound"), _T("unloading backend"));
574
575 Stop();
576
577 delete ms_backend;
578 ms_backend = NULL;
579 #if wxUSE_LIBSDL && wxUSE_PLUGINS
580 delete ms_backendSDL;
581 #endif
582 }
583 }
584
585 bool wxSound::DoPlay(unsigned flags) const
586 {
587 wxCHECK_MSG( IsOk(), false, _T("Attempt to play invalid wave data") );
588
589 EnsureBackend();
590 wxSoundPlaybackStatus status;
591 status.m_playing = true;
592 status.m_stopRequested = false;
593 return ms_backend->Play(m_data, flags, &status);
594 }
595
596 /*static*/ void wxSound::Stop()
597 {
598 if (ms_backend)
599 ms_backend->Stop();
600 }
601
602 /*static*/ bool wxSound::IsPlaying()
603 {
604 if (ms_backend)
605 return ms_backend->IsPlaying();
606 else
607 return false;
608 }
609
610 void wxSound::Free()
611 {
612 if (m_data)
613 m_data->DecRef();
614 }
615
616 typedef struct
617 {
618 wxUint32 uiSize;
619 wxUint16 uiFormatTag;
620 wxUint16 uiChannels;
621 wxUint32 ulSamplesPerSec;
622 wxUint32 ulAvgBytesPerSec;
623 wxUint16 uiBlockAlign;
624 wxUint16 uiBitsPerSample;
625 } WAVEFORMAT;
626
627 #define WAVE_FORMAT_PCM 1
628 #define WAVE_INDEX 8
629 #define FMT_INDEX 12
630
631 bool wxSound::LoadWAV(const wxUint8 *data, size_t length, bool copyData)
632 {
633 WAVEFORMAT waveformat;
634 wxUint32 ul;
635
636 if (length < 32 + sizeof(WAVEFORMAT))
637 return false;
638
639 memcpy(&waveformat, &data[FMT_INDEX + 4], sizeof(WAVEFORMAT));
640 waveformat.uiSize = wxUINT32_SWAP_ON_BE(waveformat.uiSize);
641 waveformat.uiFormatTag = wxUINT16_SWAP_ON_BE(waveformat.uiFormatTag);
642 waveformat.uiChannels = wxUINT16_SWAP_ON_BE(waveformat.uiChannels);
643 waveformat.ulSamplesPerSec = wxUINT32_SWAP_ON_BE(waveformat.ulSamplesPerSec);
644 waveformat.ulAvgBytesPerSec = wxUINT32_SWAP_ON_BE(waveformat.ulAvgBytesPerSec);
645 waveformat.uiBlockAlign = wxUINT16_SWAP_ON_BE(waveformat.uiBlockAlign);
646 waveformat.uiBitsPerSample = wxUINT16_SWAP_ON_BE(waveformat.uiBitsPerSample);
647
648 if (memcmp(data, "RIFF", 4) != 0)
649 return false;
650 if (memcmp(&data[WAVE_INDEX], "WAVE", 4) != 0)
651 return false;
652 if (memcmp(&data[FMT_INDEX], "fmt ", 4) != 0)
653 return false;
654 if (memcmp(&data[FMT_INDEX + waveformat.uiSize + 8], "data", 4) != 0)
655 return false;
656 memcpy(&ul,&data[FMT_INDEX + waveformat.uiSize + 12], 4);
657 ul = wxUINT32_SWAP_ON_BE(ul);
658
659 //WAS: if (ul + FMT_INDEX + waveformat.uiSize + 16 != length)
660 if (ul + FMT_INDEX + waveformat.uiSize + 16 > length)
661 return false;
662
663 if (waveformat.uiFormatTag != WAVE_FORMAT_PCM)
664 return false;
665
666 if (waveformat.ulSamplesPerSec !=
667 waveformat.ulAvgBytesPerSec / waveformat.uiBlockAlign)
668 return false;
669
670 m_data = new wxSoundData;
671 m_data->m_channels = waveformat.uiChannels;
672 m_data->m_samplingRate = waveformat.ulSamplesPerSec;
673 m_data->m_bitsPerSample = waveformat.uiBitsPerSample;
674 m_data->m_samples = ul / (m_data->m_channels * m_data->m_bitsPerSample / 8);
675 m_data->m_dataBytes = ul;
676
677 if (copyData)
678 {
679 m_data->m_dataWithHeader = new wxUint8[length];
680 memcpy(m_data->m_dataWithHeader, data, length);
681 }
682 else
683 m_data->m_dataWithHeader = (wxUint8*)data;
684
685 m_data->m_data =
686 (&m_data->m_dataWithHeader[FMT_INDEX + waveformat.uiSize + 8]);
687
688 return true;
689 }
690
691
692 // ----------------------------------------------------------------------------
693 // wxSoundCleanupModule
694 // ----------------------------------------------------------------------------
695
696 class wxSoundCleanupModule: public wxModule
697 {
698 public:
699 bool OnInit() { return true; }
700 void OnExit() { wxSound::UnloadBackend(); }
701 DECLARE_DYNAMIC_CLASS(wxSoundCleanupModule)
702 };
703
704 IMPLEMENT_DYNAMIC_CLASS(wxSoundCleanupModule, wxModule)
705
706 #endif