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
34 void mix_audio(void *userdata, Uint8 *stream, int len);
35 void audio_sdl_start();
37 gboolean audio_sdl_init( )
39 int rate = DEFAULT_SAMPLE_RATE;
40 int format = DEFAULT_SAMPLE_FORMAT;
44 if (format & AUDIO_FMT_16BIT)
45 fmt.format = AUDIO_S16;
47 fmt.format = AUDIO_U8;
48 if (format & AUDIO_FMT_STEREO)
53 fmt.samples = SDL_SAMPLES;
54 fmt.callback = mix_audio;
57 if (SDL_OpenAudio(&fmt, NULL) < 0)
59 ERROR("Unable to open audio output (SDL)");
63 audio_buffer = (char*)malloc(BUFFER_MAX_SIZE * sizeof(char));
64 if (audio_buffer == NULL)
66 ERROR("Could not allocate audio buffer (SDL)");
70 //hmm, this doesn't seem to get called externally...
76 gboolean audio_sdl_process_buffer( audio_buffer_t buffer )
79 if (buffer_pos + buffer->length >= BUFFER_MAX_SIZE)
81 printf("Audio buffer full, dropping a chunk\n");
85 memcpy(audio_buffer, buffer->data, buffer->length);
86 buffer_pos += buffer->length;
93 void mix_audio(void *userdata, Uint8 *stream, int len)
97 memcpy(stream, audio_buffer, len);
99 if (buffer_pos > BUFFER_MIN_SIZE)
101 memcpy(audio_buffer, &audio_buffer[len], buffer_pos - len);
106 //printf("Audio buffer low, repeating a chunk\n");
110 gboolean audio_sdl_shutdown()
117 void audio_sdl_start()
122 void audio_sdl_stop()
127 struct audio_driver audio_sdl_driver = {
129 N_("SDL sound driver"),
131 DEFAULT_SAMPLE_FORMAT,
134 audio_sdl_process_buffer,
.