4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
9 * See the COPYING file in the top-level directory.
13 #include "sndout_sdl.h"
16 // set this to power of 2
18 #define BUF_MASK (BUF_LEN - 1)
20 static short buf[BUF_LEN];
21 static int buf_w, buf_r;
24 static void callback(void *userdata, Uint8 *stream, int len)
26 int have = (buf_w - buf_r) & BUF_MASK;
27 int buf_left = BUF_LEN - buf_r;
33 if (have > buf_left) {
34 memcpy(stream, buf + buf_r, buf_left * 2);
35 stream += buf_left * 2;
40 memcpy(stream, buf + buf_r, have * 2);
42 buf_r = (buf_r + have) & BUF_MASK;
47 // put in some silence..
48 memset(stream, 0, len * 2);
52 int sndout_sdl_init(void)
56 ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
63 int sndout_sdl_start(int rate, int stereo)
65 SDL_AudioSpec desired;
73 desired.format = AUDIO_S16LSB;
74 desired.channels = stereo ? 2 : 1;
75 desired.callback = callback;
76 desired.userdata = NULL;
78 samples = rate * 4 * 16 / 1000;
79 for (shift = 8; (1 << shift) < samples; shift++)
81 desired.samples = 1 << shift;
83 ret = SDL_OpenAudio(&desired, NULL);
85 fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
97 void sndout_sdl_stop(void)
104 void sndout_sdl_wait(void)
110 left = (buf_r - buf_w - 2) & BUF_MASK;
111 if (left >= BUF_LEN / 2)
118 int sndout_sdl_write_nb(const void *samples, int len)
120 int maxlen = (buf_r - buf_w - 2) & BUF_MASK;
121 int buf_left = BUF_LEN - buf_w;
134 if (len > buf_left) {
135 memcpy(buf + buf_w, samples, buf_left * 2);
136 samples = (const short *)samples + buf_left;
140 memcpy(buf + buf_w, samples, len * 2);
141 buf_w = (buf_w + len) & BUF_MASK;
146 void sndout_sdl_exit(void)
150 SDL_QuitSubSystem(SDL_INIT_AUDIO);