1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSound backend using SDL
4 // Author: Vaclav Slavik
8 // Copyright: (c) 2004, Vaclav Slavik
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_WAVE && 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
);
87 void FillAudioBuffer(Uint8
*stream
, int len
);
89 bool IsPlaying() const { return m_playing
; }
96 bool m_playing
, m_audioOpen
;
97 // playback information:
100 SDL_AudioSpec m_spec
;
103 wxSoundBackendSDLEvtHandler
*m_evtHandler
;
106 class wxSoundBackendSDLEvtHandler
: public wxEvtHandler
109 wxSoundBackendSDLEvtHandler(wxSoundBackendSDL
*bk
) : m_backend(bk
) {}
112 void OnNotify(wxSoundBackendSDLNotification
& WXUNUSED(event
))
114 wxLogTrace(_T("sound"),
115 _T("received playback status change notification"));
118 wxSoundBackendSDL
*m_backend
;
120 DECLARE_EVENT_TABLE()
123 BEGIN_EVENT_TABLE(wxSoundBackendSDLEvtHandler
, wxEvtHandler
)
124 EVT_SOUND_BACKEND_SDL_NOTIFICATON(wxSoundBackendSDLEvtHandler::OnNotify
)
127 wxSoundBackendSDL::~wxSoundBackendSDL()
134 bool wxSoundBackendSDL::IsAvailable() const
138 if (SDL_WasInit(SDL_INIT_AUDIO
) != SDL_INIT_AUDIO
)
140 if (SDL_Init(SDL_INIT_AUDIO
| SDL_INIT_NOPARACHUTE
) == -1)
143 wxConstCast(this, wxSoundBackendSDL
)->m_initialized
= true;
144 wxLogTrace(_T("sound"), _T("initialized SDL audio subsystem"));
148 extern "C" void wx_sdl_audio_callback(void *userdata
, Uint8
*stream
, int len
)
150 wxSoundBackendSDL
*bk
= (wxSoundBackendSDL
*)userdata
;
151 bk
->FillAudioBuffer(stream
, len
);
154 void wxSoundBackendSDL::FillAudioBuffer(Uint8
*stream
, int len
)
158 // finished playing the sample
159 if (m_pos
== m_data
->m_dataBytes
)
162 wxSoundBackendSDLNotification event
;
163 m_evtHandler
->AddPendingEvent(event
);
165 // still something to play
168 unsigned size
= ((len
+ m_pos
) < m_data
->m_dataBytes
) ?
170 (m_data
->m_dataBytes
- m_pos
);
171 memcpy(stream
, m_data
->m_data
+ m_pos
, size
);
177 // the sample doesn't play, fill the buffer with silence and wait for
178 // the main thread to shut the playback down:
184 FillAudioBuffer(stream
, len
);
189 memset(stream
, m_spec
.silence
, len
);
194 bool wxSoundBackendSDL::OpenAudio()
199 m_evtHandler
= new wxSoundBackendSDLEvtHandler(this);
202 m_spec
.samples
= 4096;
204 m_spec
.callback
= wx_sdl_audio_callback
;
205 m_spec
.userdata
= (void*)this;
207 wxLogTrace(_T("sound"), _T("opening SDL audio..."));
208 if (SDL_OpenAudio(&m_spec
, NULL
) >= 0)
212 SDL_AudioDriverName(driver
, 256);
213 wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
214 wxString(driver
, wxConvLocal
).c_str());
221 wxString
err(SDL_GetError(), wxConvLocal
);
222 wxLogError(_("Couldn't open audio: %s"), err
.c_str());
229 void wxSoundBackendSDL::CloseAudio()
234 wxLogTrace(_T("sound"), _T("closed audio"));
239 bool wxSoundBackendSDL::Play(wxSoundData
*data
, unsigned flags
)
242 if (data
->m_bitsPerSample
== 8)
244 else if (data
->m_bitsPerSample
== 16)
245 format
= AUDIO_S16LSB
;
251 bool needsOpen
= true;
254 if (format
== m_spec
.format
&&
255 m_spec
.freq
== (int)data
->m_samplingRate
&&
256 m_spec
.channels
== data
->m_channels
)
270 m_spec
.format
= format
;
271 m_spec
.freq
= data
->m_samplingRate
;
272 m_spec
.channels
= data
->m_channels
;
282 m_loop
= (flags
& wxSOUND_LOOP
);
289 // wait until playback finishes if called in sync mode:
290 if (!(flags
& wxSOUND_ASYNC
))
292 wxLogTrace(_T("sound"), _T("waiting for sample to finish"));
296 // give the playback thread a chance to add event to pending
297 // events queue, release GUI lock temporarily:
298 if (wxThread::IsMain())
303 if (wxThread::IsMain())
307 wxLogTrace(_T("sound"), _T("sample finished"));
313 void wxSoundBackendSDL::Stop()
325 extern "C" wxSoundBackend
*wxCreateSoundBackendSDL()
327 return new wxSoundBackendSDL();
330 #endif // wxUSE_WAVE && wxUSE_LIBSDL