Search
lxdream.org :: lxdream/src/drivers/audio_sdl.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/audio_sdl.c
changeset 989:7baf5ecd8e98
next994:a92d433f1be8
author nkeynes
date Sat Feb 28 06:26:48 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add SDL audio driver, thanks to Wahrhaft!
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/audio_sdl.c Sat Feb 28 06:26:48 2009 +0000
1.3 @@ -0,0 +1,137 @@
1.4 +/**
1.5 + * $Id: audio_pulse.c 754 2008-07-14 07:44:42Z nkeynes $
1.6 + *
1.7 + * The SDL sound driver
1.8 + *
1.9 + * Copyright (c) 2009 wahrhaft
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +#include <stdio.h>
1.22 +#include <unistd.h>
1.23 +#include <SDL/SDL.h>
1.24 +#include <SDL/SDL_audio.h>
1.25 +#include "aica/audio.h"
1.26 +#include "lxdream.h"
1.27 +
1.28 +#define SDL_SAMPLES 512 //tweaking this value may help with audio dropouts
1.29 +#define BYTES_PER_SAMPLE 4 //should be changed if samples are not S16 stereo
1.30 +
1.31 +#define BUFFER_MIN_SIZE SDL_SAMPLES * BYTES_PER_SAMPLE * 4
1.32 +#define BUFFER_MAX_SIZE SDL_SAMPLES * BYTES_PER_SAMPLE * 16
1.33 +
1.34 +char *audio_buffer;
1.35 +int buffer_pos;
1.36 +
1.37 +void mix_audio(void *userdata, Uint8 *stream, int len);
1.38 +
1.39 +gboolean audio_sdl_init( )
1.40 +{
1.41 + int rate = DEFAULT_SAMPLE_RATE;
1.42 + int format = DEFAULT_SAMPLE_FORMAT;
1.43 +
1.44 + SDL_AudioSpec fmt;
1.45 + fmt.freq = rate;
1.46 + if (format & AUDIO_FMT_16BIT)
1.47 + fmt.format = AUDIO_S16;
1.48 + else
1.49 + fmt.format = AUDIO_U8;
1.50 + if (format & AUDIO_FMT_STEREO)
1.51 + fmt.channels = 2;
1.52 + else
1.53 + fmt.channels = 1;
1.54 +
1.55 + fmt.samples = SDL_SAMPLES;
1.56 + fmt.callback = mix_audio;
1.57 + fmt.userdata = NULL;
1.58 +
1.59 + if (SDL_OpenAudio(&fmt, NULL) < 0)
1.60 + {
1.61 + ERROR("Unable to open audio output (SDL)");
1.62 + return FALSE;
1.63 + }
1.64 + buffer_pos = 0;
1.65 + audio_buffer = (char*)malloc(BUFFER_MAX_SIZE * sizeof(char));
1.66 + if (audio_buffer == NULL)
1.67 + {
1.68 + ERROR("Could not allocate audio buffer (SDL)");
1.69 + return FALSE;
1.70 + }
1.71 +
1.72 + //hmm, this doesn't seem to get called externally...
1.73 + audio_sdl_start();
1.74 +
1.75 + return TRUE;
1.76 +}
1.77 +
1.78 +gboolean audio_sdl_process_buffer( audio_buffer_t buffer )
1.79 +{
1.80 + SDL_LockAudio();
1.81 + if (buffer_pos + buffer->length >= BUFFER_MAX_SIZE)
1.82 + {
1.83 + printf("Audio buffer full, dropping a chunk\n");
1.84 + }
1.85 + else
1.86 + {
1.87 + memcpy(audio_buffer, buffer->data, buffer->length);
1.88 + buffer_pos += buffer->length;
1.89 + }
1.90 + SDL_UnlockAudio();
1.91 +
1.92 + return TRUE;
1.93 +}
1.94 +
1.95 +void mix_audio(void *userdata, Uint8 *stream, int len)
1.96 +{
1.97 + if (len < buffer_pos)
1.98 + {
1.99 + memcpy(stream, audio_buffer, len);
1.100 + }
1.101 + if (buffer_pos > BUFFER_MIN_SIZE)
1.102 + {
1.103 + memcpy(audio_buffer, &audio_buffer[len], buffer_pos - len);
1.104 + buffer_pos -= len;
1.105 + }
1.106 + else
1.107 + {
1.108 + //printf("Audio buffer low, repeating a chunk\n");
1.109 + }
1.110 +}
1.111 +
1.112 +gboolean audio_sdl_shutdown()
1.113 +{
1.114 + SDL_CloseAudio();
1.115 + free(audio_buffer);
1.116 + return TRUE;
1.117 +}
1.118 +
1.119 +void audio_sdl_start()
1.120 +{
1.121 + SDL_PauseAudio(0);
1.122 +}
1.123 +
1.124 +void audio_sdl_stop()
1.125 +{
1.126 + SDL_PauseAudio(1);
1.127 +}
1.128 +
1.129 +struct audio_driver audio_sdl_driver = {
1.130 + "sdl",
1.131 + N_("SDL sound driver"),
1.132 + DEFAULT_SAMPLE_RATE,
1.133 + DEFAULT_SAMPLE_FORMAT,
1.134 + audio_sdl_init,
1.135 + audio_sdl_start,
1.136 + audio_sdl_process_buffer,
1.137 + audio_sdl_stop,
1.138 + audio_sdl_shutdown
1.139 +};
1.140 +
.