comparison libao2/ao_sdl.c @ 1066:aecab161d8d6

Better audio format handling.
author atmosfear
date Sat, 09 Jun 2001 07:36:32 +0000
parents b36fb1ae4b53
children e45a029db4c3
comparison
equal deleted inserted replaced
1065:504c6edf7813 1066:aecab161d8d6
13 #include <stdio.h> 13 #include <stdio.h>
14 #include <stdlib.h> 14 #include <stdlib.h>
15 15
16 #include "audio_out.h" 16 #include "audio_out.h"
17 #include "audio_out_internal.h" 17 #include "audio_out_internal.h"
18 #include "afmt.h"
18 19
19 #include "../libvo/fastmemcpy.h" 20 #include "../libvo/fastmemcpy.h"
20 21
21 static ao_info_t info = 22 static ao_info_t info =
22 { 23 {
44 #define SAMPLESIZE 1024 45 #define SAMPLESIZE 1024
45 46
46 // General purpose Ring-buffering routines 47 // General purpose Ring-buffering routines
47 48
48 #define BUFFSIZE 4096 49 #define BUFFSIZE 4096
49 #define NUM_BUFS 16 50 #define NUM_BUFS 16
50 51
51 static unsigned char *buffer[NUM_BUFS]; 52 static unsigned char *buffer[NUM_BUFS];
52 53
53 static unsigned int buf_read=0; 54 static unsigned int buf_read=0;
54 static unsigned int buf_write=0; 55 static unsigned int buf_write=0;
130 } 131 }
131 132
132 // SDL Callback function 133 // SDL Callback function
133 void outputaudio(void *unused, Uint8 *stream, int len) { 134 void outputaudio(void *unused, Uint8 *stream, int len) {
134 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); 135 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME);
136 //if(!full_buffers) printf("SDL: Buffer underrun!\n");
137
135 read_buffer(stream, len); 138 read_buffer(stream, len);
139 //printf("SDL: Full Buffers: %i\n", full_buffers);
136 } 140 }
137 141
138 // open & setup audio device 142 // open & setup audio device
139 // return: 1=success 0=fail 143 // return: 1=success 0=fail
140 static int init(int rate,int channels,int format,int flags){ 144 static int init(int rate,int channels,int format,int flags){
152 setenv("SDL_AUDIODRIVER", sdl_adriver, 1); 156 setenv("SDL_AUDIODRIVER", sdl_adriver, 1);
153 printf("SDL: using %s audio driver\n", sdl_adriver); 157 printf("SDL: using %s audio driver\n", sdl_adriver);
154 } 158 }
155 159
156 160
161 /* The desired audio format (see SDL_AudioSpec) */
162 switch(format) {
163 case AFMT_U8:
164 aspec.format = AUDIO_U8;
165 break;
166 case AFMT_S16_LE:
167 aspec.format = AUDIO_S16LSB;
168 break;
169 case AFMT_S16_BE:
170 aspec.format = AUDIO_S16MSB;
171 break;
172 case AFMT_S8:
173 aspec.format = AUDIO_S8;
174 break;
175 case AFMT_U16_LE:
176 aspec.format = AUDIO_U16LSB;
177 break;
178 case AFMT_U16_BE:
179 aspec.format = AUDIO_U16MSB;
180 break;
181 default:
182 printf("SDL: Unsupported audio format: 0x%x.\n", format);
183 return 0;
184 }
185
157 /* The desired audio frequency in samples-per-second. */ 186 /* The desired audio frequency in samples-per-second. */
158 aspec.freq = rate; 187 aspec.freq = rate;
159
160 /* The desired audio format (see SDL_AudioSpec) */
161 aspec.format = (format == 16) ? AUDIO_S16 : AUDIO_U8;
162 188
163 /* Number of channels (mono/stereo) */ 189 /* Number of channels (mono/stereo) */
164 aspec.channels = channels; 190 aspec.channels = channels;
165 191
166 /* 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 */ 192 /* 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 */
201 SDL_QuitSubSystem(SDL_INIT_AUDIO); 227 SDL_QuitSubSystem(SDL_INIT_AUDIO);
202 } 228 }
203 229
204 // stop playing and empty buffers (for seeking/pause) 230 // stop playing and empty buffers (for seeking/pause)
205 static void reset(){ 231 static void reset(){
206 232
233 //printf("SDL: reset called!\n");
234
207 /* Reset ring-buffer state */ 235 /* Reset ring-buffer state */
208 buf_read=0; 236 buf_read=0;
209 buf_write=0; 237 buf_write=0;
210 buf_read_pos=0; 238 buf_read_pos=0;
211 buf_write_pos=0; 239 buf_write_pos=0;