2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer
4 * This driver is under the same license as MPlayer.
5 * (http://www.mplayerhq.hu)
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
9 * Thanks to Arpi for nice ringbuffer-code!
16 #include "../config.h"
17 #include "../mp_msg.h"
19 #include "audio_out.h"
20 #include "audio_out_internal.h"
24 #include "../libvo/fastmemcpy.h"
26 static ao_info_t info
=
28 "SDLlib audio output",
30 "Felix Buenemann <atmosfear@users.sourceforge.net>",
36 // Samplesize used by the SDLlib AudioSpec struct
37 #define SAMPLESIZE 1024
39 // General purpose Ring-buffering routines
44 static unsigned char *buffer
[NUM_BUFS
];
46 static unsigned int buf_read
=0;
47 static unsigned int buf_write
=0;
48 static unsigned int buf_read_pos
=0;
49 static unsigned int buf_write_pos
=0;
50 static unsigned int volume
=127;
51 static int full_buffers
=0;
52 static int buffered_bytes
=0;
55 static int write_buffer(unsigned char* data
,int len
){
59 if(full_buffers
==NUM_BUFS
) break;
60 x
=BUFFSIZE
-buf_write_pos
;
62 memcpy(buffer
[buf_write
]+buf_write_pos
,data
+len2
,x
);
64 buffered_bytes
+=x
; buf_write_pos
+=x
;
65 if(buf_write_pos
>=BUFFSIZE
){
66 // block is full, find next!
67 buf_write
=(buf_write
+1)%NUM_BUFS
;
75 static int read_buffer(unsigned char* data
,int len
){
79 if(full_buffers
==0) break; // no more data buffered!
80 x
=BUFFSIZE
-buf_read_pos
;
82 memcpy(data
+len2
,buffer
[buf_read
]+buf_read_pos
,x
);
83 SDL_MixAudio(data
+len2
, data
+len2
, x
, volume
);
85 buffered_bytes
-=x
; buf_read_pos
+=x
;
86 if(buf_read_pos
>=BUFFSIZE
){
87 // block is empty, find next!
88 buf_read
=(buf_read
+1)%NUM_BUFS
;
96 // end ring buffer stuff
98 #if defined(HPUX) || defined(sgi) || (defined(sun) && defined(__svr4__))
99 /* setenv is missing on solaris, IRIX and HPUX */
100 static void setenv(const char *name
, const char *val
, int _xx
)
102 int len
= strlen(name
) + strlen(val
) + 2;
103 char *env
= malloc(len
);
115 // to set/get/query special features/parameters
116 static int control(int cmd
,void *arg
){
118 case AOCONTROL_GET_VOLUME
:
120 ao_control_vol_t
* vol
= (ao_control_vol_t
*)arg
;
121 vol
->left
= vol
->right
= (float)((volume
+ 127)/2.55);
124 case AOCONTROL_SET_VOLUME
:
127 ao_control_vol_t
* vol
= (ao_control_vol_t
*)arg
;
128 diff
= (vol
->left
+vol
->right
) / 2;
129 volume
= (int)(diff
* 2.55) - 127;
136 // SDL Callback function
137 void outputaudio(void *unused
, Uint8
*stream
, int len
) {
138 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
139 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
141 read_buffer(stream
, len
);
142 //printf("SDL: Full Buffers: %i\n", full_buffers);
145 // open & setup audio device
146 // return: 1=success 0=fail
147 static int init(int rate
,int channels
,int format
,int flags
){
149 /* SDL Audio Specifications */
150 SDL_AudioSpec aspec
, obtained
;
153 /* Allocate ring-buffer memory */
154 for(i
=0;i
<NUM_BUFS
;i
++) buffer
[i
]=(unsigned char *) malloc(BUFFSIZE
);
156 mp_msg(MSGT_AO
,MSGL_INFO
,"SDL: Samplerate: %iHz Channels: %s Format %s\n", rate
, (channels
> 1) ? "Stereo" : "Mono", audio_out_format_name(format
));
159 setenv("SDL_AUDIODRIVER", ao_subdevice
, 1);
160 mp_msg(MSGT_AO
,MSGL_INFO
,"SDL: using %s audio driver\n", ao_subdevice
);
163 ao_data
.channels
=channels
;
164 ao_data
.samplerate
=rate
;
165 ao_data
.format
=format
;
167 ao_data
.bps
=channels
*rate
;
168 if(format
!= AFMT_U8
&& format
!= AFMT_S8
)
171 /* The desired audio format (see SDL_AudioSpec) */
174 aspec
.format
= AUDIO_U8
;
177 aspec
.format
= AUDIO_S16LSB
;
180 aspec
.format
= AUDIO_S16MSB
;
183 aspec
.format
= AUDIO_S8
;
186 aspec
.format
= AUDIO_U16LSB
;
189 aspec
.format
= AUDIO_U16MSB
;
192 mp_msg(MSGT_AO
,MSGL_WARN
,"SDL: Unsupported audio format: 0x%x.\n", format
);
196 /* The desired audio frequency in samples-per-second. */
199 /* Number of channels (mono/stereo) */
200 aspec
.channels
= channels
;
202 /* 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 */
203 aspec
.samples
= SAMPLESIZE
;
205 /* 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:
206 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. */
207 aspec
.callback
= outputaudio
;
209 /* This pointer is passed as the first parameter to the callback function. */
210 aspec
.userdata
= NULL
;
212 /* initialize the SDL Audio system */
213 if (SDL_Init (SDL_INIT_AUDIO
/*|SDL_INIT_NOPARACHUTE*/)) {
214 mp_msg(MSGT_AO
,MSGL_ERR
,"SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError());
218 /* Open the audio device and start playing sound! */
219 if(SDL_OpenAudio(&aspec
, &obtained
) < 0) {
220 mp_msg(MSGT_AO
,MSGL_ERR
,"SDL: Unable to open audio: %s\n", SDL_GetError());
224 /* did we got what we wanted ? */
225 ao_data
.channels
=obtained
.channels
;
226 ao_data
.samplerate
=obtained
.freq
;
228 switch(obtained
.format
) {
230 ao_data
.format
= AFMT_U8
;
233 ao_data
.format
= AFMT_S16_LE
;
236 ao_data
.format
= AFMT_S16_BE
;
239 ao_data
.format
= AFMT_S8
;
242 ao_data
.format
= AFMT_U16_LE
;
245 ao_data
.format
= AFMT_U16_BE
;
248 mp_msg(MSGT_AO
,MSGL_WARN
,"SDL: Unsupported SDL audio format: 0x%x.\n", obtained
.format
);
252 mp_msg(MSGT_AO
,MSGL_V
,"SDL: buf size = %d\n",obtained
.size
);
253 ao_data
.buffersize
=obtained
.size
;
255 /* unsilence audio, if callback is ready */
261 // close audio device
262 static void uninit(){
263 mp_msg(MSGT_AO
,MSGL_V
,"SDL: Audio Subsystem shutting down!\n");
265 SDL_QuitSubSystem(SDL_INIT_AUDIO
);
268 // stop playing and empty buffers (for seeking/pause)
271 //printf("SDL: reset called!\n");
273 /* Reset ring-buffer state */
284 // stop playing, keep buffers (for pause)
285 static void audio_pause()
288 //printf("SDL: audio_pause called!\n");
293 // resume playing, after audio_pause()
294 static void audio_resume()
296 //printf("SDL: audio_resume called!\n");
301 // return: how many bytes can be played without blocking
302 static int get_space(){
303 return (NUM_BUFS
-full_buffers
)*BUFFSIZE
- buf_write_pos
;
306 // plays 'len' bytes of 'data'
307 // it should round it down to outburst*n
308 // return: number of bytes played
309 static int play(void* data
,int len
,int flags
){
314 /* Audio locking prohibits call of outputaudio */
316 // copy audio stream into ring-buffer
317 ret
= write_buffer(data
, len
);
322 return write_buffer(data
, len
);
326 // return: delay in seconds between first and last sample in buffer
327 static float get_delay(){
328 return (float)(buffered_bytes
+ ao_data
.buffersize
)/(float)ao_data
.bps
;