summaryrefslogtreecommitdiff
path: root/src/libs/sound/mixer/sdl/audiodrv_sdl.c
blob: 7ef522ed0e33c2df3060c6e03486e9069169aaf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* SDL audio driver
 */

#include "audiodrv_sdl.h"
#include "../../sndintrn.h"
#include "libs/log.h"
#include "libs/memlib.h"
#include <stdlib.h>


/* SDL2 wants to talk to a specific device. We'll let SDL1 use the same
 * function names and just throw the device argument away. */
#if SDL_MAJOR_VERSION > 1
static SDL_AudioDeviceID dev;
#else
#define SDL_CloseAudioDevice(x) SDL_CloseAudio ()
#define SDL_PauseAudioDevice(x, y) SDL_PauseAudio (y)
#endif
static const audio_Driver mixSDL_Driver =
{
	mixSDL_Uninit,
	mixSDL_GetError,
	audio_DRIVER_MIXSDL,
	{
		/* Errors */
		MIX_NO_ERROR,
		MIX_INVALID_NAME,
		MIX_INVALID_ENUM,
		MIX_INVALID_VALUE,
		MIX_INVALID_OPERATION,
		MIX_OUT_OF_MEMORY,
		MIX_DRIVER_FAILURE,

		/* Source properties */
		MIX_POSITION,
		MIX_LOOPING,
		MIX_BUFFER,
		MIX_GAIN,
		MIX_SOURCE_STATE,
		MIX_BUFFERS_QUEUED,
		MIX_BUFFERS_PROCESSED,

		/* Source state information */
		MIX_INITIAL,
		MIX_STOPPED,
		MIX_PLAYING,
		MIX_PAUSED,

		/* Sound buffer properties */ 
		MIX_FREQUENCY,
		MIX_BITS,
		MIX_CHANNELS,
		MIX_SIZE,
		MIX_FORMAT_MONO16,
		MIX_FORMAT_STEREO16,
		MIX_FORMAT_MONO8,
		MIX_FORMAT_STEREO8
	},

	/* Sources */
	mixSDL_GenSources,
	mixSDL_DeleteSources,
	mixSDL_IsSource,
	mixSDL_Sourcei,
	mixSDL_Sourcef,
	mixSDL_Sourcefv,
	mixSDL_GetSourcei,
	mixSDL_GetSourcef,
	mixSDL_SourceRewind,
	mixSDL_SourcePlay,
	mixSDL_SourcePause,
	mixSDL_SourceStop,
	mixSDL_SourceQueueBuffers,
	mixSDL_SourceUnqueueBuffers,

	/* Buffers */
	mixSDL_GenBuffers,
	mixSDL_DeleteBuffers,
	mixSDL_IsBuffer,
	mixSDL_GetBufferi,
	mixSDL_BufferData
};


static void audioCallback (void *userdata, Uint8 *stream, int len);

/*
 * Initialization
 */

sint32 
mixSDL_Init (audio_Driver *driver, sint32 flags)
{
	int i;
	SDL_AudioSpec desired, obtained;
	mixer_Quality quality;
	TFB_DecoderFormats formats =
	{
		MIX_IS_BIG_ENDIAN, MIX_WANT_BIG_ENDIAN,
		audio_FORMAT_MONO8, audio_FORMAT_STEREO8,
		audio_FORMAT_MONO16, audio_FORMAT_STEREO16
	};

	log_add (log_Info, "Initializing SDL audio subsystem.");
	if ((SDL_InitSubSystem(SDL_INIT_AUDIO)) == -1)
	{
		log_add (log_Error, "Couldn't initialize audio subsystem: %s",
				SDL_GetError());
		return -1;
	}
	log_add (log_Info, "SDL audio subsystem initialized.");
		
	if (flags & audio_QUALITY_HIGH)
	{
		quality = MIX_QUALITY_HIGH;
		desired.freq = 44100;
		desired.samples = 4096;
	}
	else if (flags & audio_QUALITY_LOW)
	{
		quality = MIX_QUALITY_LOW;
#ifdef __SYMBIAN32__
		desired.freq = 11025;
		desired.samples = 4096;
#else
		desired.freq = 22050;
		desired.samples = 2048;
#endif		
	}
	else
	{
		quality = MIX_QUALITY_DEFAULT;
		desired.freq = 44100;
		desired.samples = 4096;
	}

	desired.format = AUDIO_S16SYS;
	desired.channels = 2;
	desired.callback = audioCallback;
	
	log_add (log_Info, "Opening SDL audio device.");
#if SDL_MAJOR_VERSION > 1
	dev = SDL_OpenAudioDevice (NULL, 0, &desired, &obtained,
			SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE
#ifdef SDL_AUDIO_ALLOW_SAMPLES_CHANGE
			| SDL_AUDIO_ALLOW_SAMPLES_CHANGE
#endif
			);
	if (dev != 0 && obtained.channels != 1 && obtained.channels != 2)
	{
		/* Try again without SDL_AUDIO_ALLOW_CHANNELS_CHANGE
		 * in case the device only supports >2 channels for some
		 * reason */
		SDL_CloseAudioDevice (dev);
		dev = SDL_OpenAudioDevice (NULL, 0, &desired, &obtained,
				SDL_AUDIO_ALLOW_FREQUENCY_CHANGE
#ifdef SDL_AUDIO_ALLOW_SAMPLES_CHANGE
				| SDL_AUDIO_ALLOW_SAMPLES_CHANGE
#endif
				);
	}
	if (dev == 0)
#else
	if (SDL_OpenAudio (&desired, &obtained) < 0)
#endif
	{
		log_add (log_Error, "Unable to open audio device: %s",
				SDL_GetError ());
		SDL_QuitSubSystem (SDL_INIT_AUDIO);
		return -1;
	}
	if (obtained.format != desired.format ||
		(obtained.channels != 1 && obtained.channels != 2))
	{
		log_add (log_Error, "Unable to obtain desired audio format.");
		SDL_CloseAudio ();
		SDL_QuitSubSystem (SDL_INIT_AUDIO);
		return -1;
	}

	{
#if SDL_MAJOR_VERSION == 1
		char devicename[256];
		SDL_AudioDriverName (devicename, sizeof (devicename));
#else
		const char *devicename = SDL_GetCurrentAudioDriver ();
#endif
		log_add (log_Info, "    using %s at %d Hz 16 bit %s, "
				"%d samples audio buffer",
				devicename, obtained.freq,
				obtained.channels > 1 ? "stereo" : "mono",
				obtained.samples);
	}

	log_add (log_Info, "Initializing mixer.");
	if (!mixer_Init (obtained.freq, MIX_FORMAT_MAKE (2, obtained.channels),
			quality, 0))
	{
		log_add (log_Error, "Mixer initialization failed: %x",
				mixer_GetError ());
		SDL_CloseAudioDevice (dev);
		SDL_QuitSubSystem (SDL_INIT_AUDIO);
		return -1;
	}
	log_add (log_Info, "Mixer initialized.");

	log_add (log_Info, "Initializing sound decoders.");
	if (SoundDecoder_Init (flags, &formats))
	{
		log_add (log_Error, "Sound decoders initialization failed.");
		SDL_CloseAudioDevice (dev);
		mixer_Uninit ();
		SDL_QuitSubSystem (SDL_INIT_AUDIO);
		return -1;
	}
	log_add (log_Info, "Sound decoders initialized.");

	*driver = mixSDL_Driver;
	for (i = 0; i < NUM_SOUNDSOURCES; ++i)
	{
		audio_GenSources (1, &soundSource[i].handle);		
		soundSource[i].stream_mutex = CreateMutex ("MixSDL stream mutex", SYNC_CLASS_AUDIO);
	}

	if (InitStreamDecoder ())
	{
		log_add (log_Error, "Stream decoder initialization failed.");
		// TODO: cleanup source mutexes [or is it "muti"? :) ]
		SDL_CloseAudioDevice (dev);
		SoundDecoder_Uninit ();
		mixer_Uninit ();
		SDL_QuitSubSystem (SDL_INIT_AUDIO);
		return -1;
	}

	atexit (unInitAudio);

	SetSFXVolume (sfxVolumeScale);
	SetSpeechVolume (speechVolumeScale);
	SetMusicVolume ((COUNT)musicVolume);
				
	SDL_PauseAudioDevice (dev, 0);
		
	return 0;
}

void
mixSDL_Uninit (void)
{
	int i;

	UninitStreamDecoder ();

	for (i = 0; i < NUM_SOUNDSOURCES; ++i)
	{
		if (soundSource[i].sample && soundSource[i].sample->decoder)
		{
			StopStream (i);
		}
		if (soundSource[i].sbuffer)
		{
			void *sbuffer = soundSource[i].sbuffer;
			soundSource[i].sbuffer = NULL;
			HFree (sbuffer);
		}
		DestroyMutex (soundSource[i].stream_mutex);
		soundSource[i].stream_mutex = 0;

		mixSDL_DeleteSources (1, &soundSource[i].handle);
	}

	SDL_CloseAudioDevice (dev);
	mixer_Uninit ();
	SoundDecoder_Uninit ();
	SDL_QuitSubSystem (SDL_INIT_AUDIO);
}

static void
audioCallback (void *userdata, Uint8 *stream, int len)
{
	mixer_MixChannels (userdata, stream, len);
}

/*
 * General
 */

sint32
mixSDL_GetError (void)
{
	sint32 value = mixer_GetError ();
	switch (value)
	{
		case MIX_NO_ERROR:
			return audio_NO_ERROR;
		case MIX_INVALID_NAME:
			return audio_INVALID_NAME;
		case MIX_INVALID_ENUM:
			return audio_INVALID_ENUM;
		case MIX_INVALID_VALUE:
			return audio_INVALID_VALUE;
		case MIX_INVALID_OPERATION:
			return audio_INVALID_OPERATION;
		case MIX_OUT_OF_MEMORY:
			return audio_OUT_OF_MEMORY;
		default:
			log_add (log_Debug, "mixSDL_GetError: unknown value %x", value);
			return audio_DRIVER_FAILURE;
			break;
	}
}


/*
 * Sources
 */

void
mixSDL_GenSources (uint32 n, audio_Object *psrcobj)
{
	mixer_GenSources (n, (mixer_Object *) psrcobj);
}

void
mixSDL_DeleteSources (uint32 n, audio_Object *psrcobj)
{
	mixer_DeleteSources (n, (mixer_Object *) psrcobj);
}

bool
mixSDL_IsSource (audio_Object srcobj)
{
	return mixer_IsSource ((mixer_Object) srcobj);
}

void
mixSDL_Sourcei (audio_Object srcobj, audio_SourceProp pname,
		audio_IntVal value)

{
	mixer_Sourcei ((mixer_Object) srcobj, (mixer_SourceProp) pname,
			(mixer_IntVal) value);
}

void
mixSDL_Sourcef (audio_Object srcobj, audio_SourceProp pname,
		float value)
{
	mixer_Sourcef ((mixer_Object) srcobj, (mixer_SourceProp) pname, value);
}

void
mixSDL_Sourcefv (audio_Object srcobj, audio_SourceProp pname,
		float *value)
{
	mixer_Sourcefv ((mixer_Object) srcobj, (mixer_SourceProp) pname, value);
}

void
mixSDL_GetSourcei (audio_Object srcobj, audio_SourceProp pname,
		audio_IntVal *value)
{
	mixer_GetSourcei ((mixer_Object) srcobj, (mixer_SourceProp) pname,
			(mixer_IntVal *) value);
	if (pname == MIX_SOURCE_STATE)
	{
		switch (*value)
		{
			case MIX_INITIAL:
				*value = audio_INITIAL;
				break;
			case MIX_STOPPED:
				*value = audio_STOPPED;
				break;
			case MIX_PLAYING:
				*value = audio_PLAYING;
				break;
			case MIX_PAUSED:
				*value = audio_PAUSED;
				break;
			default:
				*value = audio_DRIVER_FAILURE;
		}
	}
}

void
mixSDL_GetSourcef (audio_Object srcobj, audio_SourceProp pname,
		float *value)
{
	mixer_GetSourcef ((mixer_Object) srcobj, (mixer_SourceProp) pname, value);
}

void
mixSDL_SourceRewind (audio_Object srcobj)
{
	mixer_SourceRewind ((mixer_Object) srcobj);
}

void
mixSDL_SourcePlay (audio_Object srcobj)
{
	mixer_SourcePlay ((mixer_Object) srcobj);
}

void
mixSDL_SourcePause (audio_Object srcobj)
{
	mixer_SourcePause ((mixer_Object) srcobj);
}

void
mixSDL_SourceStop (audio_Object srcobj)
{
	mixer_SourceStop ((mixer_Object) srcobj);
}

void
mixSDL_SourceQueueBuffers (audio_Object srcobj, uint32 n,
		audio_Object* pbufobj)
{
	mixer_SourceQueueBuffers ((mixer_Object) srcobj, n,
			(mixer_Object *) pbufobj);
}

void
mixSDL_SourceUnqueueBuffers (audio_Object srcobj, uint32 n,
		audio_Object* pbufobj)
{
	mixer_SourceUnqueueBuffers ((mixer_Object) srcobj, n,
			(mixer_Object *) pbufobj);
}


/*
 * Buffers
 */

void
mixSDL_GenBuffers (uint32 n, audio_Object *pbufobj)
{
	mixer_GenBuffers (n, (mixer_Object *) pbufobj);
}

void
mixSDL_DeleteBuffers (uint32 n, audio_Object *pbufobj)
{
	mixer_DeleteBuffers (n, (mixer_Object *) pbufobj);
}

bool
mixSDL_IsBuffer (audio_Object bufobj)
{
	return mixer_IsBuffer ((mixer_Object) bufobj);
}

void
mixSDL_GetBufferi (audio_Object bufobj, audio_BufferProp pname,
		audio_IntVal *value)
{
	mixer_GetBufferi ((mixer_Object) bufobj, (mixer_BufferProp) pname,
			(mixer_IntVal *) value);
}

void
mixSDL_BufferData (audio_Object bufobj, uint32 format, void* data,
		uint32 size, uint32 freq)
{
	mixer_BufferData ((mixer_Object) bufobj, format, data, size, freq);
}