2 * $Id: audio_pulse.c 754 2008-07-14 07:44:42Z nkeynes $
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);
36 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)");
69 //hmm, this doesn't seem to get called externally...
75 gboolean audio_sdl_process_buffer( audio_buffer_t buffer )
78 if (buffer_pos + buffer->length >= BUFFER_MAX_SIZE)
80 printf("Audio buffer full, dropping a chunk\n");
84 memcpy(audio_buffer, buffer->data, buffer->length);
85 buffer_pos += buffer->length;
92 void mix_audio(void *userdata, Uint8 *stream, int len)
96 memcpy(stream, audio_buffer, len);
98 if (buffer_pos > BUFFER_MIN_SIZE)
100 memcpy(audio_buffer, &audio_buffer[len], buffer_pos - len);
105 //printf("Audio buffer low, repeating a chunk\n");
109 gboolean audio_sdl_shutdown()
116 void audio_sdl_start()
121 void audio_sdl_stop()
126 struct audio_driver audio_sdl_driver = {
128 N_("SDL sound driver"),
130 DEFAULT_SAMPLE_FORMAT,
133 audio_sdl_process_buffer,
.