1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound backend using SDL
4 // Author: Vaclav Slavik
8 // Copyright: (c) 2004, Open Source Applications Foundation
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #if defined(__BORLANDC__)
21 #if wxUSE_SOUND && wxUSE_LIBSDL
32 #include "wx/thread.h"
33 #include "wx/module.h"
36 // ----------------------------------------------------------------------------
37 // wxSoundBackendSDL, for Unix with libSDL
38 // ----------------------------------------------------------------------------
40 class wxSoundBackendSDLNotification
: public wxEvent
43 DECLARE_DYNAMIC_CLASS(wxSoundBackendSDLNotification
)
44 wxSoundBackendSDLNotification();
45 wxEvent
*Clone() const { return new wxSoundBackendSDLNotification(*this); }
48 typedef void (wxEvtHandler::*wxSoundBackendSDLNotificationFunction
)
49 (wxSoundBackendSDLNotification
&);
51 BEGIN_DECLARE_EVENT_TYPES()
52 DECLARE_LOCAL_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION
, -1)
53 END_DECLARE_EVENT_TYPES()
55 #define EVT_SOUND_BACKEND_SDL_NOTIFICATON(func) \
56 DECLARE_EVENT_TABLE_ENTRY(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, \
59 (wxObjectEventFunction) \
60 (wxSoundBackendSDLNotificationFunction)& func, \
63 IMPLEMENT_DYNAMIC_CLASS(wxSoundBackendSDLNotification
, wxEvtHandler
)
64 DEFINE_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION
)
66 wxSoundBackendSDLNotification::wxSoundBackendSDLNotification()
68 SetEventType(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION
);
71 class wxSoundBackendSDLEvtHandler
;
73 class wxSoundBackendSDL
: public wxSoundBackend
77 : m_initialized(false), m_playing(false), m_audioOpen(false),
78 m_data(NULL
), m_evtHandler(NULL
) {}
79 virtual ~wxSoundBackendSDL();
81 wxString
GetName() const { return _T("Simple DirectMedia Layer"); }
82 int GetPriority() const { return 9; }
83 bool IsAvailable() const;
84 bool HasNativeAsyncPlayback() const { return true; }
85 bool Play(wxSoundData
*data
, unsigned flags
,
86 volatile wxSoundPlaybackStatus
*status
);
88 void FillAudioBuffer(Uint8
*stream
, int len
);
89 void FinishedPlayback();
92 bool IsPlaying() const { return m_playing
; }
99 bool m_playing
, m_audioOpen
;
100 // playback information:
103 SDL_AudioSpec m_spec
;
106 wxSoundBackendSDLEvtHandler
*m_evtHandler
;
109 class wxSoundBackendSDLEvtHandler
: public wxEvtHandler
112 wxSoundBackendSDLEvtHandler(wxSoundBackendSDL
*bk
) : m_backend(bk
) {}
115 void OnNotify(wxSoundBackendSDLNotification
& WXUNUSED(event
))
117 wxLogTrace(_T("sound"),
118 _T("received playback status change notification"));
119 m_backend
->FinishedPlayback();
121 wxSoundBackendSDL
*m_backend
;
123 DECLARE_EVENT_TABLE()
126 BEGIN_EVENT_TABLE(wxSoundBackendSDLEvtHandler
, wxEvtHandler
)
127 EVT_SOUND_BACKEND_SDL_NOTIFICATON(wxSoundBackendSDLEvtHandler::OnNotify
)
130 wxSoundBackendSDL::~wxSoundBackendSDL()
137 bool wxSoundBackendSDL::IsAvailable() const
141 if (SDL_WasInit(SDL_INIT_AUDIO
) != SDL_INIT_AUDIO
)
143 if (SDL_Init(SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) == -1)
146 wxConstCast(this, wxSoundBackendSDL
)->m_initialized
= true;
147 wxLogTrace(_T("sound"), _T("initialized SDL audio subsystem"));
151 extern "C" void wx_sdl_audio_callback(void *userdata
, Uint8
*stream
, int len
)
153 wxSoundBackendSDL
*bk
= (wxSoundBackendSDL
*)userdata
;
154 bk
->FillAudioBuffer(stream
, len
);
157 void wxSoundBackendSDL::FillAudioBuffer(Uint8
*stream
, int len
)
161 // finished playing the sample
162 if (m_pos
== m_data
->m_dataBytes
)
165 wxSoundBackendSDLNotification event
;
166 m_evtHandler
->AddPendingEvent(event
);
168 // still something to play
171 unsigned size
= ((len
+ m_pos
) < m_data
->m_dataBytes
) ?
173 (m_data
->m_dataBytes
- m_pos
);
174 memcpy(stream
, m_data
->m_data
+ m_pos
, size
);
180 // the sample doesn't play, fill the buffer with silence and wait for
181 // the main thread to shut the playback down:
187 FillAudioBuffer(stream
, len
);
192 memset(stream
, m_spec
.silence
, len
);
197 void wxSoundBackendSDL::FinishedPlayback()
203 bool wxSoundBackendSDL::OpenAudio()
208 m_evtHandler
= new wxSoundBackendSDLEvtHandler(this);
211 m_spec
.samples
= 4096;
213 m_spec
.callback
= wx_sdl_audio_callback
;
214 m_spec
.userdata
= (void*)this;
216 wxLogTrace(_T("sound"), _T("opening SDL audio..."));
217 if (SDL_OpenAudio(&m_spec
, NULL
) >= 0)
221 SDL_AudioDriverName(driver
, 256);
222 wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
223 wxString(driver
, wxConvLocal
).c_str());
230 wxString
err(SDL_GetError(), wxConvLocal
);
231 wxLogError(_("Couldn't open audio: %s"), err
.c_str());
238 void wxSoundBackendSDL::CloseAudio()
243 wxLogTrace(_T("sound"), _T("closed audio"));
248 bool wxSoundBackendSDL::Play(wxSoundData
*data
, unsigned flags
,
249 volatile wxSoundPlaybackStatus
*WXUNUSED(status
))
254 if (data
->m_bitsPerSample
== 8)
256 else if (data
->m_bitsPerSample
== 16)
257 format
= AUDIO_S16LSB
;
261 bool needsOpen
= true;
264 if (format
== m_spec
.format
&&
265 m_spec
.freq
== (int)data
->m_samplingRate
&&
266 m_spec
.channels
== data
->m_channels
)
278 m_spec
.format
= format
;
279 m_spec
.freq
= data
->m_samplingRate
;
280 m_spec
.channels
= data
->m_channels
;
286 wxLogTrace(_T("sound"), _T("playing new sound"));
289 m_loop
= (flags
& wxSOUND_LOOP
);
296 // wait until playback finishes if called in sync mode:
297 if (!(flags
& wxSOUND_ASYNC
))
299 wxLogTrace(_T("sound"), _T("waiting for sample to finish"));
300 while (m_playing
&& m_data
== data
)
303 // give the playback thread a chance to add event to pending
304 // events queue, release GUI lock temporarily:
305 if (wxThread::IsMain())
310 if (wxThread::IsMain())
314 wxLogTrace(_T("sound"), _T("sample finished"));
320 void wxSoundBackendSDL::Stop()
333 extern "C" wxSoundBackend
*wxCreateSoundBackendSDL()
335 return new wxSoundBackendSDL();
338 #endif // wxUSE_SOUND && wxUSE_LIBSDL