oops, more fixes needed
[wxWidgets.git] / src / unix / sound_sdl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound_sdl.cpp
3 // Purpose: wxSound backend using SDL
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2004/01/31
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004, Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/setup.h"
16
17 #if defined(__BORLANDC__)
18 #pragma hdrstop
19 #endif
20
21 #if wxUSE_WAVE && wxUSE_LIBSDL
22
23 #include <SDL.h>
24
25 #ifndef WX_PRECOMP
26 #include "wx/event.h"
27 #include "wx/intl.h"
28 #include "wx/log.h"
29 #include "wx/utils.h"
30 #endif
31
32 #include "wx/thread.h"
33 #include "wx/module.h"
34 #include "wx/sound.h"
35
36 // ----------------------------------------------------------------------------
37 // wxSoundBackendSDL, for Unix with libSDL
38 // ----------------------------------------------------------------------------
39
40 class wxSoundBackendSDLNotification : public wxEvent
41 {
42 public:
43 DECLARE_DYNAMIC_CLASS(wxSoundBackendSDLNotification)
44 wxSoundBackendSDLNotification();
45 wxEvent *Clone() const { return new wxSoundBackendSDLNotification(*this); }
46 };
47
48 typedef void (wxEvtHandler::*wxSoundBackendSDLNotificationFunction)
49 (wxSoundBackendSDLNotification&);
50
51 BEGIN_DECLARE_EVENT_TYPES()
52 DECLARE_LOCAL_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, -1)
53 END_DECLARE_EVENT_TYPES()
54
55 #define EVT_SOUND_BACKEND_SDL_NOTIFICATON(func) \
56 DECLARE_EVENT_TABLE_ENTRY(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION, \
57 -1, \
58 -1, \
59 (wxObjectEventFunction) \
60 (wxSoundBackendSDLNotificationFunction)& func, \
61 (wxObject *) NULL ),
62
63 IMPLEMENT_DYNAMIC_CLASS(wxSoundBackendSDLNotification, wxEvtHandler)
64 DEFINE_EVENT_TYPE(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION)
65
66 wxSoundBackendSDLNotification::wxSoundBackendSDLNotification()
67 {
68 SetEventType(wxEVT_SOUND_BACKEND_SDL_NOTIFICATION);
69 }
70
71 class wxSoundBackendSDLEvtHandler;
72
73 class wxSoundBackendSDL : public wxSoundBackend
74 {
75 public:
76 wxSoundBackendSDL()
77 : m_initialized(false), m_playing(false), m_audioOpen(false),
78 m_data(NULL), m_evtHandler(NULL) {}
79 virtual ~wxSoundBackendSDL();
80
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
87 void FillAudioBuffer(Uint8 *stream, int len);
88 void Stop();
89 bool IsPlaying() const { return m_playing; }
90
91 private:
92 bool OpenAudio();
93 void CloseAudio();
94
95 bool m_initialized;
96 bool m_playing, m_audioOpen;
97 // playback information:
98 wxSoundData *m_data;
99 unsigned m_pos;
100 SDL_AudioSpec m_spec;
101 bool m_loop;
102
103 wxSoundBackendSDLEvtHandler *m_evtHandler;
104 };
105
106 class wxSoundBackendSDLEvtHandler : public wxEvtHandler
107 {
108 public:
109 wxSoundBackendSDLEvtHandler(wxSoundBackendSDL *bk) : m_backend(bk) {}
110
111 private:
112 void OnNotify(wxSoundBackendSDLNotification& WXUNUSED(event))
113 {
114 wxLogTrace(_T("sound"),
115 _T("received playback status change notification"));
116 m_backend->Stop();
117 }
118 wxSoundBackendSDL *m_backend;
119
120 DECLARE_EVENT_TABLE()
121 };
122
123 BEGIN_EVENT_TABLE(wxSoundBackendSDLEvtHandler, wxEvtHandler)
124 EVT_SOUND_BACKEND_SDL_NOTIFICATON(wxSoundBackendSDLEvtHandler::OnNotify)
125 END_EVENT_TABLE()
126
127 wxSoundBackendSDL::~wxSoundBackendSDL()
128 {
129 Stop();
130 CloseAudio();
131 delete m_evtHandler;
132 }
133
134 bool wxSoundBackendSDL::IsAvailable() const
135 {
136 if (m_initialized)
137 return true;
138 if (SDL_WasInit(SDL_INIT_AUDIO) != SDL_INIT_AUDIO)
139 {
140 if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) == -1)
141 return false;
142 }
143 wxConstCast(this, wxSoundBackendSDL)->m_initialized = true;
144 wxLogTrace(_T("sound"), _T("initialized SDL audio subsystem"));
145 return true;
146 }
147
148 extern "C" void wx_sdl_audio_callback(void *userdata, Uint8 *stream, int len)
149 {
150 wxSoundBackendSDL *bk = (wxSoundBackendSDL*)userdata;
151 bk->FillAudioBuffer(stream, len);
152 }
153
154 void wxSoundBackendSDL::FillAudioBuffer(Uint8 *stream, int len)
155 {
156 if (m_playing)
157 {
158 // finished playing the sample
159 if (m_pos == m_data->m_dataBytes)
160 {
161 m_playing = false;
162 wxSoundBackendSDLNotification event;
163 m_evtHandler->AddPendingEvent(event);
164 }
165 // still something to play
166 else
167 {
168 unsigned size = ((len + m_pos) < m_data->m_dataBytes) ?
169 len :
170 (m_data->m_dataBytes - m_pos);
171 memcpy(stream, m_data->m_data + m_pos, size);
172 m_pos += size;
173 len -= size;
174 stream += size;
175 }
176 }
177 // the sample doesn't play, fill the buffer with silence and wait for
178 // the main thread to shut the playback down:
179 if (len > 0)
180 {
181 if (m_loop)
182 {
183 m_pos = 0;
184 FillAudioBuffer(stream, len);
185 return;
186 }
187 else
188 {
189 memset(stream, m_spec.silence, len);
190 }
191 }
192 }
193
194 bool wxSoundBackendSDL::OpenAudio()
195 {
196 if (!m_audioOpen)
197 {
198 if (!m_evtHandler)
199 m_evtHandler = new wxSoundBackendSDLEvtHandler(this);
200
201 m_spec.silence = 0;
202 m_spec.samples = 4096;
203 m_spec.size = 0;
204 m_spec.callback = wx_sdl_audio_callback;
205 m_spec.userdata = (void*)this;
206
207 wxLogTrace(_T("sound"), _T("opening SDL audio..."));
208 if (SDL_OpenAudio(&m_spec, NULL) >= 0)
209 {
210 #if wxUSE_LOG_DEBUG
211 char driver[256];
212 SDL_AudioDriverName(driver, 256);
213 wxLogTrace(_T("sound"), _T("opened audio, driver '%s'"),
214 wxString(driver, wxConvLocal).c_str());
215 #endif
216 m_audioOpen = true;
217 return true;
218 }
219 else
220 {
221 wxString err(SDL_GetError(), wxConvLocal);
222 wxLogError(_("Couldn't open audio: %s"), err.c_str());
223 return false;
224 }
225 }
226 return true;
227 }
228
229 void wxSoundBackendSDL::CloseAudio()
230 {
231 if (m_audioOpen)
232 {
233 SDL_CloseAudio();
234 wxLogTrace(_T("sound"), _T("closed audio"));
235 m_audioOpen = false;
236 }
237 }
238
239 bool wxSoundBackendSDL::Play(wxSoundData *data, unsigned flags)
240 {
241 Stop();
242
243 int format;
244 if (data->m_bitsPerSample == 8)
245 format = AUDIO_U8;
246 else if (data->m_bitsPerSample == 16)
247 format = AUDIO_S16LSB;
248 else
249 return false;
250
251 bool needsOpen = true;
252 if (m_audioOpen)
253 {
254 if (format == m_spec.format &&
255 m_spec.freq == (int)data->m_samplingRate &&
256 m_spec.channels == data->m_channels)
257 {
258 needsOpen = false;
259 }
260 else
261 {
262 CloseAudio();
263 }
264 }
265
266 if (needsOpen)
267 {
268 m_spec.format = format;
269 m_spec.freq = data->m_samplingRate;
270 m_spec.channels = data->m_channels;
271 if (!OpenAudio())
272 return false;
273 }
274
275 SDL_LockAudio();
276 m_playing = true;
277 m_pos = 0;
278 m_loop = (flags & wxSOUND_LOOP);
279 m_data = data;
280 data->IncRef();
281 SDL_UnlockAudio();
282
283 SDL_PauseAudio(0);
284
285 // wait until playback finishes if called in sync mode:
286 if (!(flags & wxSOUND_ASYNC))
287 {
288 wxLogTrace(_T("sound"), _T("waiting for sample to finish"));
289 while (m_playing)
290 {
291 #if wxUSE_THREADS
292 // give the playback thread a chance to add event to pending
293 // events queue, release GUI lock temporarily:
294 if (wxThread::IsMain())
295 wxMutexGuiLeave();
296 #endif
297 wxUsleep(10);
298 #if wxUSE_THREADS
299 if (wxThread::IsMain())
300 wxMutexGuiEnter();
301 #endif
302 }
303 wxLogTrace(_T("sound"), _T("sample finished"));
304 }
305
306 return true;
307 }
308
309 void wxSoundBackendSDL::Stop()
310 {
311 SDL_LockAudio();
312 SDL_PauseAudio(1);
313 m_playing = false;
314 if (m_data)
315 {
316 m_data->DecRef();
317 m_data = NULL;
318 }
319 SDL_UnlockAudio();
320 }
321
322 extern "C" wxSoundBackend *wxCreateSoundBackendSDL()
323 {
324 return new wxSoundBackendSDL();
325 }
326
327 #endif // wxUSE_WAVE && wxUSE_LIBSDL