22
22
#include < string.h>
23
23
#include < sys/time.h>
24
24
25
+ #if HAVE_LIBSDL3
26
+ #include < SDL3/SDL.h>
27
+ #else
25
28
#include < SDL.h>
26
- #include < SDL_audio.h >
29
+ #endif
27
30
28
31
#include < libaudcore/audstrings.h>
29
32
#include < libaudcore/i18n.h>
@@ -73,9 +76,10 @@ const char SDLOutput::about[] =
73
76
" Copyright 2010 John Lindgren" );
74
77
75
78
const char * const SDLOutput::defaults[] = {
76
- " vol_left" , " 100" ,
77
- " vol_right" , " 100" ,
78
- nullptr };
79
+ " vol_left" , " 100" ,
80
+ " vol_right" , " 100" ,
81
+ nullptr
82
+ };
79
83
80
84
static pthread_mutex_t sdlout_mutex = PTHREAD_MUTEX_INITIALIZER;
81
85
static pthread_cond_t sdlout_cond = PTHREAD_COND_INITIALIZER;
@@ -84,6 +88,10 @@ static volatile int vol_left, vol_right;
84
88
85
89
static int sdlout_chan, sdlout_rate;
86
90
91
+ #if HAVE_LIBSDL3
92
+ static SDL_AudioStream * sdlout_stream;
93
+ #endif
94
+
87
95
static RingBuf<unsigned char > buffer;
88
96
89
97
static bool prebuffer_flag, paused_flag;
@@ -98,7 +106,11 @@ bool SDLOutput::init ()
98
106
vol_left = aud_get_int (" sdlout" , " vol_left" );
99
107
vol_right = aud_get_int (" sdlout" , " vol_right" );
100
108
109
+ #if HAVE_LIBSDL3
110
+ if (! SDL_Init (SDL_INIT_AUDIO))
111
+ #else
101
112
if (SDL_Init (SDL_INIT_AUDIO) < 0 )
113
+ #endif
102
114
{
103
115
AUDERR (" Failed to init SDL: %s.\n " , SDL_GetError ());
104
116
return false ;
@@ -186,6 +198,23 @@ static void callback (void * user, unsigned char * buf, int len)
186
198
pthread_mutex_unlock (& sdlout_mutex);
187
199
}
188
200
201
+ #if HAVE_LIBSDL3
202
+ static void SDLCALL sdl3_callback (void * user, SDL_AudioStream * stream,
203
+ int additional_amount, int total_amount)
204
+ {
205
+ if (additional_amount <= 0 )
206
+ return ;
207
+
208
+ Uint8 * data = SDL_stack_alloc (Uint8, additional_amount);
209
+ if (! data)
210
+ return ;
211
+
212
+ callback (user, data, additional_amount);
213
+ SDL_PutAudioStreamData (stream, data, additional_amount);
214
+ SDL_stack_free (data);
215
+ }
216
+ #endif
217
+
189
218
bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
190
219
{
191
220
if (format != FMT_S16_NE)
@@ -205,15 +234,22 @@ bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
205
234
prebuffer_flag = true ;
206
235
paused_flag = false ;
207
236
208
- SDL_AudioSpec spec = {0 };
237
+ #if HAVE_LIBSDL3
238
+ const SDL_AudioSpec spec = { SDL_AUDIO_S16, chan, rate };
239
+ sdlout_stream = SDL_OpenAudioDeviceStream (
240
+ SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, & spec, sdl3_callback, nullptr );
209
241
242
+ if (! sdlout_stream)
243
+ #else
244
+ SDL_AudioSpec spec = {0 };
210
245
spec.freq = rate;
211
246
spec.format = AUDIO_S16;
212
247
spec.channels = chan;
213
248
spec.samples = 4096 ;
214
249
spec.callback = callback;
215
250
216
251
if (SDL_OpenAudio (& spec, nullptr ) < 0 )
252
+ #endif
217
253
{
218
254
error = String (str_printf
219
255
(" SDL error: Failed to open audio stream: %s." , SDL_GetError ()));
@@ -227,7 +263,14 @@ bool SDLOutput::open_audio (int format, int rate, int chan, String & error)
227
263
void SDLOutput::close_audio ()
228
264
{
229
265
AUDDBG (" Closing audio.\n " );
266
+
267
+ #if HAVE_LIBSDL3
268
+ SDL_DestroyAudioStream (sdlout_stream);
269
+ sdlout_stream = nullptr ;
270
+ #else
230
271
SDL_CloseAudio ();
272
+ #endif
273
+
231
274
buffer.destroy ();
232
275
}
233
276
@@ -239,7 +282,12 @@ static void check_started ()
239
282
AUDDBG (" Starting playback.\n " );
240
283
prebuffer_flag = false ;
241
284
block_delay = 0 ;
285
+
286
+ #if HAVE_LIBSDL3
287
+ SDL_ResumeAudioStreamDevice (sdlout_stream);
288
+ #else
242
289
SDL_PauseAudio (0 );
290
+ #endif
243
291
}
244
292
245
293
void SDLOutput::period_wait ()
@@ -311,7 +359,16 @@ void SDLOutput::pause (bool pause)
311
359
paused_flag = pause ;
312
360
313
361
if (! prebuffer_flag)
362
+ {
363
+ #if HAVE_LIBSDL3
364
+ if (pause )
365
+ SDL_PauseAudioStreamDevice (sdlout_stream);
366
+ else
367
+ SDL_ResumeAudioStreamDevice (sdlout_stream);
368
+ #else
314
369
SDL_PauseAudio (pause );
370
+ #endif
371
+ }
315
372
316
373
pthread_cond_broadcast (& sdlout_cond); /* wake up period wait */
317
374
pthread_mutex_unlock (& sdlout_mutex);
0 commit comments