comparison libao2/ao_sdl.c @ 972:b49f89b3ab2e

Yea, it worksss!
author atmosfear
date Sun, 03 Jun 2001 20:54:46 +0000
parents 69b4f944ce08
children 0d4e4da7c126
comparison
equal deleted inserted replaced
971:dcf8d81eed48 972:b49f89b3ab2e
1 /*
2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer
3 *
4 * This driver is under the same license as MPlayer.
5 * (http://mplayer.sf.net)
6 *
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net)
8 *
9 * Thanks to Arpi for nice ringbuffer-code!
10 *
11 */
12
1 #include <stdio.h> 13 #include <stdio.h>
2 #include <stdlib.h>
3 14
4 #include "audio_out.h" 15 #include "audio_out.h"
5 #include "audio_out_internal.h" 16 #include "audio_out_internal.h"
17
18 #include "../libvo/fastmemcpy.h"
6 19
7 static ao_info_t info = 20 static ao_info_t info =
8 { 21 {
9 "SDLlib audio output", 22 "SDLlib audio output",
10 "sdl", 23 "sdl",
20 // ao_format 33 // ao_format
21 // ao_bps 34 // ao_bps
22 // ao_outburst 35 // ao_outburst
23 // ao_buffersize 36 // ao_buffersize
24 37
25 int audiolen = 0; 38 // Samplesize used by the SDLlib AudioSpec struct
26 int audioplayed = 0; 39 #define SAMPLESIZE 512
27 int audiobuffer = 0; 40
41 // General purpose Ring-buffering routines
42
43 #define BUFFSIZE 1024
44 #define NUM_BUFS 64
45
46 static unsigned char *buffer[NUM_BUFS];
47
48 static unsigned int buf_read=0;
49 static unsigned int buf_write=0;
50 static unsigned int buf_read_pos=0;
51 static unsigned int buf_write_pos=0;
52
53 static int full_buffers=0;
54 static int buffered_bytes=0;
55
56 static int write_buffer(unsigned char* data,int len){
57 int len2=0;
58 int x;
59 while(len>0){
60 if(full_buffers==NUM_BUFS) break;
61 x=BUFFSIZE-buf_write_pos;
62 if(x>len) x=len;
63 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x);
64 len2+=x; len-=x;
65 buffered_bytes+=x; buf_write_pos+=x;
66 if(buf_write_pos>=BUFFSIZE){
67 // block is full, find next!
68 buf_write=(buf_write+1)%NUM_BUFS;
69 ++full_buffers;
70 buf_write_pos=0;
71 }
72 }
73 return len2;
74 }
75
76 static int read_buffer(unsigned char* data,int len){
77 int len2=0;
78 int x;
79 while(len>0){
80 if(full_buffers==0) break; // no more data buffered!
81 x=BUFFSIZE-buf_read_pos;
82 if(x>len) x=len;
83 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x);
84 len2+=x; len-=x;
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;
89 --full_buffers;
90 buf_read_pos=0;
91 }
92 }
93 return len2;
94 }
95
96 // end ring buffer stuff
28 97
29 #ifdef __FreeBSD__ 98 #ifdef __FreeBSD__
30 #include <SDL11/SDL.h> 99 #include <SDL11/SDL.h>
31 #else 100 #else
32 #include <SDL/SDL.h> 101 #include <SDL/SDL.h>
33 #endif 102 #endif
34 103
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 104 // to set/get/query special features/parameters
58 static int control(int cmd,int arg){ 105 static int control(int cmd,int arg){
59 return -1; 106 return -1;
60 } 107 }
61 108
62 // Callback function 109 // SDL Callback function
63 void mixaudio(void *datastream, Uint8 *stream, int len) { 110 void outputaudio(void *unused, Uint8 *stream, int len) {
64 //printf("SDL: mixaudio called!\n"); 111 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
65 //printf("SDL pts: %u %u\n", aspec.userdata, stream); 112 read_buffer(stream, len);
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 } 113 }
72 114
73 // open & setup audio device 115 // open & setup audio device
74 // return: 1=success 0=fail 116 // return: 1=success 0=fail
75 static int init(int rate,int channels,int format,int flags){ 117 static int init(int rate,int channels,int format,int flags){
76 118
77 printf("SDL: Audio Out - This driver is early alpha, do not use!\n"); 119 /* SDL Audio Specifications */
78 printf("SDL: rate: %iHz channels %i format %i Bit flags %i\n", rate, channels, format, flags); 120 SDL_AudioSpec aspec;
79 // ao_outburst=4096; 121
122 int i;
123 /* Allocate ring-buffer memory */
124 for(i=0;i<NUM_BUFS;i++) buffer[i]=malloc(BUFFSIZE);
125
126 printf("SDL: Samplerate: %iHz Channels: %s Format %iBit\n", rate, (channels > 1) ? "Stereo" : "Mono", format);
80 127
81 /* The desired audio frequency in samples-per-second. */ 128 /* The desired audio frequency in samples-per-second. */
82 aspec.freq = rate; 129 aspec.freq = rate;
83 130
84 /* The desired audio format (see SDL_AudioSpec) */ 131 /* The desired audio format (see SDL_AudioSpec) */
85 aspec.format = (format == 16) ? AUDIO_S16 : AUDIO_S8; 132 aspec.format = (format == 16) ? AUDIO_S16 : AUDIO_U8;
86 133
87 /* Number of channels (mono/stereo) */ 134 /* Number of channels (mono/stereo) */
88 aspec.channels = channels; 135 aspec.channels = channels;
89 136
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 */ 137 /* 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; 138 aspec.samples = SAMPLESIZE;
92 139
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: 140 /* 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. */ 141 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; 142 aspec.callback = outputaudio;
96 143
97 /* This pointer is passed as the first parameter to the callback function. */ 144 /* This pointer is passed as the first parameter to the callback function. */
98 aspec.userdata = NULL; 145 aspec.userdata = NULL;
146
147 /* initialize the SDL Audio system */
148 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) {
149 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError());
150 return 0;
151 }
99 152
100 /* Open the audio device and start playing sound! */ 153 /* Open the audio device and start playing sound! */
101 if(SDL_OpenAudio(&aspec, NULL) < 0) { 154 if(SDL_OpenAudio(&aspec, NULL) < 0) {
102 printf("SDL: Unable to open audio: %s\n", SDL_GetError()); 155 printf("SDL: Unable to open audio: %s\n", SDL_GetError());
103 return(0); 156 return(0);
104 } 157 }
105 158
106 /* unsilence audio, if callback is ready */ 159 /* unsilence audio, if callback is ready */
107 SDL_PauseAudio(0); 160 SDL_PauseAudio(0);
108
109
110
111 161
112 return 1; 162 return 1;
113 } 163 }
114 164
115 // close audio device 165 // close audio device
116 static void uninit(){ 166 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(); 167 SDL_CloseAudio();
168 SDL_QuitSubSystem(SDL_INIT_AUDIO);
122 } 169 }
123 170
124 // stop playing and empty buffers (for seeking/pause) 171 // stop playing and empty buffers (for seeking/pause)
125 static void reset(){ 172 static void reset(){
173
174 /* Reset ring-buffer state */
175 buf_read=0;
176 buf_write=0;
177 buf_read_pos=0;
178 buf_write_pos=0;
179
180 full_buffers=0;
181 buffered_bytes=0;
126 182
127 } 183 }
128 184
129 // return: how many bytes can be played without blocking 185 // return: how many bytes can be played without blocking
130 static int get_space(){ 186 static int get_space(){
131 187 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos;
132 return aspec.samples;
133 } 188 }
134 189
135 // plays 'len' bytes of 'data' 190 // plays 'len' bytes of 'data'
136 // it should round it down to outburst*n 191 // it should round it down to outburst*n
137 // return: number of bytes played 192 // return: number of bytes played
138 static int play(void* data,int len,int flags){ 193 static int play(void* data,int len,int flags){
139 194
140 //printf("SDL: play called!\n"); 195 #if 0
141 196 int ret;
142 audiolen = len; 197
143 audiobuffer = len; 198 /* Audio locking prohibits call of outputaudio */
144
145 SDL_LockAudio(); 199 SDL_LockAudio();
146 // copy audio stream into mixaudio stream here 200 // copy audio stream into ring-buffer
147 aspec.userdata = data; 201 ret = write_buffer(data, len);
148 //printf("SDL pt: %u %u\n", data, aspec.userdata);
149
150 SDL_UnlockAudio(); 202 SDL_UnlockAudio();
151 203
152 return audioplayed; 204 return ret;
205 #else
206 return write_buffer(data, len);
207 #endif
153 } 208 }
154 209
155 // return: how many unplayed bytes are in the buffer 210 // return: how many unplayed bytes are in the buffer
156 static int get_delay(){ 211 static int get_delay(){
157 //printf("SDL: get_delay called (%i)!\n", audiobuffer); 212 return buffered_bytes;
158 return audiobuffer; 213 }
159 } 214
160 215
161 216
162 217
163 218
164 219
165