966
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #include "audio_out.h"
|
|
5 #include "audio_out_internal.h"
|
|
6
|
|
7 static ao_info_t info =
|
|
8 {
|
|
9 "SDLlib audio output",
|
|
10 "sdl",
|
|
11 "Felix Buenemann <atmosfear@users.sourceforge.net>",
|
|
12 ""
|
|
13 };
|
|
14
|
|
15 LIBAO_EXTERN(sdl)
|
|
16
|
|
17 // there are some globals:
|
|
18 // ao_samplerate
|
|
19 // ao_channels
|
|
20 // ao_format
|
|
21 // ao_bps
|
|
22 // ao_outburst
|
|
23 // ao_buffersize
|
|
24
|
|
25 int audiolen = 0;
|
|
26 int audioplayed = 0;
|
|
27 int audiobuffer = 0;
|
|
28
|
|
29 #ifdef __FreeBSD__
|
|
30 #include <SDL11/SDL.h>
|
|
31 #else
|
|
32 #include <SDL/SDL.h>
|
|
33 #endif
|
|
34
|
|
35 /*
|
|
36
|
|
37 typedef struct{
|
|
38 int freq;
|
|
39 Uint16 format;
|
|
40 Uint8 channels;
|
|
41 Uint8 silence;
|
|
42 Uint16 samples;
|
|
43 Uint32 size;
|
|
44 void (*callback)(void *userdata, Uint8 *stream, int len);
|
|
45 void *userdata;
|
|
46 } SDL_AudioSpec;
|
|
47
|
|
48 */
|
|
49
|
|
50 //static struct sdl_priv_s {
|
|
51
|
|
52 /* SDL Audio Specifications */
|
|
53 SDL_AudioSpec aspec;
|
|
54
|
|
55 //} sdl_priv;
|
|
56
|
|
57 // to set/get/query special features/parameters
|
|
58 static int control(int cmd,int arg){
|
|
59 return -1;
|
|
60 }
|
|
61
|
|
62 // Callback function
|
|
63 void mixaudio(void *datastream, Uint8 *stream, int len) {
|
|
64 //printf("SDL: mixaudio called!\n");
|
|
65 //printf("SDL pts: %u %u\n", aspec.userdata, stream);
|
|
66 if(audiolen == 0) return;
|
|
67 len = (len > audiolen ? audiolen : len);
|
|
68 SDL_MixAudio(stream, aspec.userdata, len, SDL_MIX_MAXVOLUME);
|
|
69 audiobuffer -= len;
|
|
70 audioplayed = len;
|
|
71 }
|
|
72
|
|
73 // open & setup audio device
|
|
74 // return: 1=success 0=fail
|
|
75 static int init(int rate,int channels,int format,int flags){
|
|
76
|
|
77 printf("SDL: Audio Out - This driver is early alpha, do not use!\n");
|
|
78 printf("SDL: rate: %iHz channels %i format %i Bit flags %i\n", rate, channels, format, flags);
|
|
79 // ao_outburst=4096;
|
|
80
|
|
81 /* The desired audio frequency in samples-per-second. */
|
|
82 aspec.freq = rate;
|
|
83
|
|
84 /* The desired audio format (see SDL_AudioSpec) */
|
|
85 aspec.format = (format == 16) ? AUDIO_S16 : AUDIO_S8;
|
|
86
|
|
87 /* Number of channels (mono/stereo) */
|
|
88 aspec.channels = channels;
|
|
89
|
|
90 /* The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq */
|
|
91 aspec.samples = 4096;
|
|
92
|
|
93 /* This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code. The callback prototype is:
|
|
94 void callback(void *userdata, Uint8 *stream, int len); userdata is the pointer stored in userdata field of the SDL_AudioSpec. stream is a pointer to the audio buffer you want to fill with information and len is the length of the audio buffer in bytes. */
|
|
95 aspec.callback = mixaudio;
|
|
96
|
|
97 /* This pointer is passed as the first parameter to the callback function. */
|
|
98 aspec.userdata = NULL;
|
|
99
|
|
100 /* Open the audio device and start playing sound! */
|
|
101 if(SDL_OpenAudio(&aspec, NULL) < 0) {
|
|
102 printf("SDL: Unable to open audio: %s\n", SDL_GetError());
|
|
103 return(0);
|
|
104 }
|
|
105
|
|
106 /* unsilence audio, if callback is ready */
|
|
107 SDL_PauseAudio(0);
|
|
108
|
|
109
|
|
110
|
|
111
|
|
112 return 1;
|
|
113 }
|
|
114
|
|
115 // close audio device
|
|
116 static void uninit(){
|
|
117 /* Wait for sound to complete */
|
|
118 while ( audiolen > 0 ) {
|
|
119 SDL_Delay(100); /* Sleep 1/10 second */
|
|
120 }
|
|
121 SDL_CloseAudio();
|
|
122 }
|
|
123
|
|
124 // stop playing and empty buffers (for seeking/pause)
|
|
125 static void reset(){
|
|
126
|
|
127 }
|
|
128
|
|
129 // return: how many bytes can be played without blocking
|
|
130 static int get_space(){
|
|
131
|
|
132 return aspec.samples;
|
|
133 }
|
|
134
|
|
135 // plays 'len' bytes of 'data'
|
|
136 // it should round it down to outburst*n
|
|
137 // return: number of bytes played
|
|
138 static int play(void* data,int len,int flags){
|
|
139
|
|
140 //printf("SDL: play called!\n");
|
|
141
|
|
142 audiolen = len;
|
|
143 audiobuffer = len;
|
|
144
|
|
145 SDL_LockAudio();
|
|
146 // copy audio stream into mixaudio stream here
|
|
147 aspec.userdata = data;
|
|
148 //printf("SDL pt: %u %u\n", data, aspec.userdata);
|
|
149
|
|
150 SDL_UnlockAudio();
|
|
151
|
|
152 return audioplayed;
|
|
153 }
|
|
154
|
|
155 // return: how many unplayed bytes are in the buffer
|
|
156 static int get_delay(){
|
|
157 //printf("SDL: get_delay called (%i)!\n", audiobuffer);
|
|
158 return audiobuffer;
|
|
159 }
|
|
160
|
|
161
|
|
162
|
|
163
|
|
164
|
|
165
|