refresh.h: fix build with asm core enabled
[rofl0r-gnuboy.git] / sys / sdl / sdl-audio.c
bloba9f7963913879c321ae508e448ba1fa1208ad1a1
1 /*
2 * sdl-audio.c
3 * sdl audio interface
5 * (C) 2001 Laguna
7 * Licensed under the GPLv2, or later.
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
13 #include <SDL/SDL.h>
15 #include "rc.h"
16 #include "pcm.h"
19 struct pcm pcm;
22 static int sound = 1;
23 static int samplerate = 44100;
24 static int stereo = 1;
25 static volatile int audio_done;
27 rcvar_t pcm_exports[] =
29 RCV_BOOL("sound", &sound, "enable sound"),
30 RCV_INT("stereo", &stereo, "enable stereo"),
31 RCV_INT("samplerate", &samplerate, "samplerate, recommended: 32768"),
32 RCV_END
36 static void audio_callback(void *blah, byte *stream, int len)
38 memcpy(stream, pcm.buf, len);
39 audio_done = 1;
43 void pcm_init()
45 int i;
46 SDL_AudioSpec as = {0}, ob;
48 if (!sound) return;
50 SDL_InitSubSystem(SDL_INIT_AUDIO);
51 as.freq = samplerate;
52 as.format = AUDIO_U8;
53 as.channels = 1 + stereo;
54 as.samples = samplerate / 60;
55 for (i = 1; i < as.samples; i<<=1);
56 as.samples = i;
57 as.callback = audio_callback;
58 as.userdata = 0;
59 if (SDL_OpenAudio(&as, &ob) == -1) {
60 sound = 0;
61 return;
64 pcm.hz = ob.freq;
65 pcm.stereo = ob.channels - 1;
66 pcm.len = ob.size;
67 pcm.buf = malloc(pcm.len);
68 pcm.pos = 0;
69 memset(pcm.buf, 0, pcm.len);
71 SDL_PauseAudio(0);
74 int pcm_submit()
76 if (!pcm.buf) return 0;
77 if (pcm.pos < pcm.len) return 1;
78 while (!audio_done)
79 SDL_Delay(4);
80 audio_done = 0;
81 pcm.pos = 0;
82 return 1;
85 void pcm_close()
87 if (sound) SDL_CloseAudio();