6 * Copyright (c) 2009 wahrhaft
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #include <SDL/SDL_audio.h>
22 #include "aica/audio.h"
25 #define SDL_SAMPLES 512 //tweaking this value may help with audio dropouts
26 #define BYTES_PER_SAMPLE 4 //should be changed if samples are not S16 stereo
28 #define BUFFER_MIN_SIZE SDL_SAMPLES * BYTES_PER_SAMPLE * 4
29 #define BUFFER_MAX_SIZE SDL_SAMPLES * BYTES_PER_SAMPLE * 16
31 static char *audio_buffer;
32 static int buffer_pos;
34 static void mix_audio(void *userdata, Uint8 *stream, int len);
36 static gboolean audio_sdl_init( )
38 int rate = DEFAULT_SAMPLE_RATE;
39 int format = DEFAULT_SAMPLE_FORMAT;
43 if (format & AUDIO_FMT_16BIT)
44 fmt.format = AUDIO_S16;
46 fmt.format = AUDIO_U8;
47 if (format & AUDIO_FMT_STEREO)
52 fmt.samples = SDL_SAMPLES;
53 fmt.callback = mix_audio;
56 if (SDL_OpenAudio(&fmt, NULL) < 0)
58 ERROR("Unable to open audio output (SDL)");
62 audio_buffer = (char*)malloc(BUFFER_MAX_SIZE * sizeof(char));
63 if (audio_buffer == NULL)
65 ERROR("Could not allocate audio buffer (SDL)");
72 gboolean audio_sdl_process_buffer( audio_buffer_t buffer )
75 if (buffer_pos + buffer->length >= BUFFER_MAX_SIZE)
77 printf("Audio buffer full, dropping a chunk\n");
81 memcpy(audio_buffer, buffer->data, buffer->length);
82 buffer_pos += buffer->length;
89 static void mix_audio(void *userdata, Uint8 *stream, int len)
93 memcpy(stream, audio_buffer, len);
95 if (buffer_pos > BUFFER_MIN_SIZE)
97 memcpy(audio_buffer, &audio_buffer[len], buffer_pos - len);
102 //printf("Audio buffer low, repeating a chunk\n");
106 static gboolean audio_sdl_shutdown()
113 static void audio_sdl_start()
118 static void audio_sdl_stop()
123 static struct audio_driver audio_sdl_driver = {
125 N_("SDL sound driver"),
128 DEFAULT_SAMPLE_FORMAT,
131 audio_sdl_process_buffer,
136 AUDIO_DRIVER( "sdl", audio_sdl_driver );
.