Mercurial > mplayer.hg
annotate libao2/ao_sdl.c @ 15219:c931a567a395
Translation synced with 1.52
author | jheryan |
---|---|
date | Wed, 20 Apr 2005 07:41:24 +0000 |
parents | d313f591d1a4 |
children | 44c24de55f9d |
rev | line source |
---|---|
972 | 1 /* |
2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer | |
3 * | |
4 * This driver is under the same license as MPlayer. | |
11750 | 5 * (http://www.mplayerhq.hu) |
972 | 6 * |
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net) | |
8 * | |
9 * Thanks to Arpi for nice ringbuffer-code! | |
10 * | |
11 */ | |
12 | |
966 | 13 #include <stdio.h> |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
14 #include <stdlib.h> |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
15 #include <string.h> |
966 | 16 |
14123 | 17 #include "config.h" |
18 #include "mp_msg.h" | |
19 #include "help_mp.h" | |
8027 | 20 |
966 | 21 #include "audio_out.h" |
22 #include "audio_out_internal.h" | |
14245 | 23 #include "libaf/af_format.h" |
6184 | 24 #include <SDL.h> |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
25 #include "osdep/timer.h" |
966 | 26 |
14123 | 27 #include "libvo/fastmemcpy.h" |
972 | 28 |
966 | 29 static ao_info_t info = |
30 { | |
31 "SDLlib audio output", | |
32 "sdl", | |
33 "Felix Buenemann <atmosfear@users.sourceforge.net>", | |
34 "" | |
35 }; | |
36 | |
37 LIBAO_EXTERN(sdl) | |
38 | |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
39 // turn this on if you want to use the slower SDL_MixAudio |
12908
7b9b4f07d2c4
automatic loading of af_volume, original patch by Dan Christiansen (danchr (at) daimi (dot) au (dot) dk)
reimar
parents:
12714
diff
changeset
|
40 #undef USE_SDL_INTERNAL_MIXER |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
41 |
972 | 42 // Samplesize used by the SDLlib AudioSpec struct |
12113 | 43 #ifdef WIN32 |
12019
6ede5366bc47
fix compilation with sdl on mingw patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
11750
diff
changeset
|
44 #define SAMPLESIZE 2048 |
12113 | 45 #else |
46 #define SAMPLESIZE 1024 | |
47 #endif | |
972 | 48 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
49 #define CHUNK_SIZE 4096 |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
50 #define NUM_CHUNKS 8 |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
51 // This type of ring buffer may never fill up completely, at least |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
52 // one byte must always be unused. |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
53 // For performance reasons (alignment etc.) one whole chunk always stays |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
54 // empty, not only one byte. |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
55 #define BUFFSIZE ((NUM_CHUNKS + 1) * CHUNK_SIZE) |
972 | 56 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
57 static unsigned char *buffer; |
972 | 58 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
59 // may only be modified by SDL's playback thread or while it is stopped |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
60 static volatile int read_pos; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
61 // may only be modified by mplayer's thread |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
62 static volatile int write_pos; |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
63 #ifdef USE_SDL_INTERNAL_MIXER |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
64 static unsigned char volume=SDL_MIX_MAXVOLUME; |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
65 #endif |
972 | 66 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
67 // may only be called by mplayer's thread |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
68 // return value may change between immediately following two calls, |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
69 // and the real number of free bytes might be larger! |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
70 static int buf_free() { |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
71 int free = read_pos - write_pos - CHUNK_SIZE; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
72 if (free < 0) free += BUFFSIZE; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
73 return free; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
74 } |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
75 |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
76 // may only be called by SDL's playback thread |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
77 // return value may change between immediately following two calls, |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
78 // and the real number of buffered bytes might be larger! |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
79 static int buf_used() { |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
80 int used = write_pos - read_pos; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
81 if (used < 0) used += BUFFSIZE; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
82 return used; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
83 } |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
84 |
972 | 85 static int write_buffer(unsigned char* data,int len){ |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
86 int first_len = BUFFSIZE - write_pos; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
87 int free = buf_free(); |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
88 if (len > free) len = free; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
89 if (first_len > len) first_len = len; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
90 // till end of buffer |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
91 memcpy (&buffer[write_pos], data, first_len); |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
92 if (len > first_len) { // we have to wrap around |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
93 // remaining part from beginning of buffer |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
94 memcpy (buffer, &data[first_len], len - first_len); |
972 | 95 } |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
96 write_pos = (write_pos + len) % BUFFSIZE; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
97 return len; |
972 | 98 } |
99 | |
100 static int read_buffer(unsigned char* data,int len){ | |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
101 int first_len = BUFFSIZE - read_pos; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
102 int buffered = buf_used(); |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
103 if (len > buffered) len = buffered; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
104 if (first_len > len) first_len = len; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
105 // till end of buffer |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
106 #ifdef USE_SDL_INTERNAL_MIXER |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
107 SDL_MixAudio (data, &buffer[read_pos], first_len, volume); |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
108 #else |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
109 memcpy (data, &buffer[read_pos], first_len); |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
110 #endif |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
111 if (len > first_len) { // we have to wrap around |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
112 // remaining part from beginning of buffer |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
113 #ifdef USE_SDL_INTERNAL_MIXER |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
114 SDL_MixAudio (&data[first_len], buffer, len - first_len, volume); |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
115 #else |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
116 memcpy (&data[first_len], buffer, len - first_len); |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
117 #endif |
972 | 118 } |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
119 read_pos = (read_pos + len) % BUFFSIZE; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
120 return len; |
972 | 121 } |
122 | |
123 // end ring buffer stuff | |
966 | 124 |
12027
210e474436d3
Cygwin provides setenv. Fixes compilation on Cygwin.
diego
parents:
12019
diff
changeset
|
125 #if defined(__MINGW32__) || defined(HPUX) || defined(sgi) || (defined(sun) && defined(__svr4__)) |
12019
6ede5366bc47
fix compilation with sdl on mingw patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
11750
diff
changeset
|
126 /* setenv is missing on win32, solaris, IRIX and HPUX */ |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
127 static void setenv(const char *name, const char *val, int _xx) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
128 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
129 int len = strlen(name) + strlen(val) + 2; |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
130 char *env = malloc(len); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
131 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
132 if (env != NULL) { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
133 strcpy(env, name); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
134 strcat(env, "="); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
135 strcat(env, val); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
136 putenv(env); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
137 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
138 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
139 #endif |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
140 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
141 |
966 | 142 // to set/get/query special features/parameters |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
8027
diff
changeset
|
143 static int control(int cmd,void *arg){ |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
144 #ifdef USE_SDL_INTERNAL_MIXER |
6184 | 145 switch (cmd) { |
146 case AOCONTROL_GET_VOLUME: | |
147 { | |
148 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
149 vol->left = vol->right = volume * 100 / SDL_MIX_MAXVOLUME; |
6184 | 150 return CONTROL_OK; |
151 } | |
152 case AOCONTROL_SET_VOLUME: | |
153 { | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
154 int diff; |
6184 | 155 ao_control_vol_t* vol = (ao_control_vol_t*)arg; |
156 diff = (vol->left+vol->right) / 2; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
12027
diff
changeset
|
157 volume = diff * SDL_MIX_MAXVOLUME / 100; |
6184 | 158 return CONTROL_OK; |
159 } | |
160 } | |
12673
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
161 #endif |
c1371fce7267
make the internal sdl mixer optional, idea by Reimar Doffinger
alex
parents:
12440
diff
changeset
|
162 return CONTROL_UNKNOWN; |
966 | 163 } |
164 | |
972 | 165 // SDL Callback function |
166 void outputaudio(void *unused, Uint8 *stream, int len) { | |
167 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); | |
1066 | 168 //if(!full_buffers) printf("SDL: Buffer underrun!\n"); |
169 | |
972 | 170 read_buffer(stream, len); |
1066 | 171 //printf("SDL: Full Buffers: %i\n", full_buffers); |
966 | 172 } |
173 | |
174 // open & setup audio device | |
175 // return: 1=success 0=fail | |
176 static int init(int rate,int channels,int format,int flags){ | |
177 | |
972 | 178 /* SDL Audio Specifications */ |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
179 SDL_AudioSpec aspec, obtained; |
972 | 180 |
181 /* Allocate ring-buffer memory */ | |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
182 buffer = (unsigned char *) malloc(BUFFSIZE); |
972 | 183 |
14264 | 184 mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_SDL_INFO, rate, (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format)); |
983 | 185 |
1189 | 186 if(ao_subdevice) { |
187 setenv("SDL_AUDIODRIVER", ao_subdevice, 1); | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12908
diff
changeset
|
188 mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_SDL_DriverInfo, ao_subdevice); |
1189 | 189 } |
3095 | 190 |
7660 | 191 ao_data.channels=channels; |
192 ao_data.samplerate=rate; | |
193 ao_data.format=format; | |
194 | |
3137 | 195 ao_data.bps=channels*rate; |
14245 | 196 if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8) |
3095 | 197 ao_data.bps*=2; |
966 | 198 |
1066 | 199 /* The desired audio format (see SDL_AudioSpec) */ |
200 switch(format) { | |
14245 | 201 case AF_FORMAT_U8: |
1066 | 202 aspec.format = AUDIO_U8; |
203 break; | |
14245 | 204 case AF_FORMAT_S16_LE: |
1066 | 205 aspec.format = AUDIO_S16LSB; |
206 break; | |
14245 | 207 case AF_FORMAT_S16_BE: |
1066 | 208 aspec.format = AUDIO_S16MSB; |
209 break; | |
14245 | 210 case AF_FORMAT_S8: |
1066 | 211 aspec.format = AUDIO_S8; |
212 break; | |
14245 | 213 case AF_FORMAT_U16_LE: |
1066 | 214 aspec.format = AUDIO_U16LSB; |
215 break; | |
14245 | 216 case AF_FORMAT_U16_BE: |
1066 | 217 aspec.format = AUDIO_U16MSB; |
218 break; | |
219 default: | |
12440
56bdb9b7a4bc
use fallback for unsupported formats instead of quitting
reimar
parents:
12151
diff
changeset
|
220 aspec.format = AUDIO_S16LSB; |
14245 | 221 ao_data.format = AF_FORMAT_S16_LE; |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12908
diff
changeset
|
222 mp_msg(MSGT_AO,MSGL_WARN,MSGTR_AO_SDL_UnsupportedAudioFmt, format); |
1066 | 223 } |
224 | |
966 | 225 /* The desired audio frequency in samples-per-second. */ |
226 aspec.freq = rate; | |
227 | |
228 /* Number of channels (mono/stereo) */ | |
229 aspec.channels = channels; | |
230 | |
231 /* 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 */ | |
972 | 232 aspec.samples = SAMPLESIZE; |
966 | 233 |
234 /* 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: | |
235 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. */ | |
972 | 236 aspec.callback = outputaudio; |
966 | 237 |
238 /* This pointer is passed as the first parameter to the callback function. */ | |
239 aspec.userdata = NULL; | |
240 | |
972 | 241 /* initialize the SDL Audio system */ |
242 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) { | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12908
diff
changeset
|
243 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_SDL_CantInit, SDL_GetError()); |
972 | 244 return 0; |
245 } | |
246 | |
966 | 247 /* Open the audio device and start playing sound! */ |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
248 if(SDL_OpenAudio(&aspec, &obtained) < 0) { |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12908
diff
changeset
|
249 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_SDL_CantOpenAudio, SDL_GetError()); |
966 | 250 return(0); |
251 } | |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
252 |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
253 /* did we got what we wanted ? */ |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
254 ao_data.channels=obtained.channels; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
255 ao_data.samplerate=obtained.freq; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
256 |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
257 switch(obtained.format) { |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
258 case AUDIO_U8 : |
14245 | 259 ao_data.format = AF_FORMAT_U8; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
260 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
261 case AUDIO_S16LSB : |
14245 | 262 ao_data.format = AF_FORMAT_S16_LE; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
263 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
264 case AUDIO_S16MSB : |
14245 | 265 ao_data.format = AF_FORMAT_S16_BE; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
266 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
267 case AUDIO_S8 : |
14245 | 268 ao_data.format = AF_FORMAT_S8; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
269 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
270 case AUDIO_U16LSB : |
14245 | 271 ao_data.format = AF_FORMAT_U16_LE; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
272 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
273 case AUDIO_U16MSB : |
14245 | 274 ao_data.format = AF_FORMAT_U16_BE; |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
275 break; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
276 default: |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12908
diff
changeset
|
277 mp_msg(MSGT_AO,MSGL_WARN,MSGTR_AO_SDL_UnsupportedAudioFmt, obtained.format); |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
278 return 0; |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
279 } |
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
280 |
8027 | 281 mp_msg(MSGT_AO,MSGL_V,"SDL: buf size = %d\n",obtained.size); |
7908
dc96a3eb9fab
Check what we obtain in SDL_OpenAudio() - allows to build the
colin
parents:
7897
diff
changeset
|
282 ao_data.buffersize=obtained.size; |
13887
48ce49693b3c
respect immed uninit flag, initialize ao_data.outburst.
reimar
parents:
13832
diff
changeset
|
283 ao_data.outburst = CHUNK_SIZE; |
974 | 284 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
285 reset(); |
966 | 286 /* unsilence audio, if callback is ready */ |
287 SDL_PauseAudio(0); | |
288 | |
289 return 1; | |
290 } | |
291 | |
292 // close audio device | |
12145 | 293 static void uninit(int immed){ |
8027 | 294 mp_msg(MSGT_AO,MSGL_V,"SDL: Audio Subsystem shutting down!\n"); |
13887
48ce49693b3c
respect immed uninit flag, initialize ao_data.outburst.
reimar
parents:
13832
diff
changeset
|
295 if (!immed) |
14849
d313f591d1a4
aos should respect the immed uninit flag (quit immediatly vs waiting till file
reimar
parents:
14264
diff
changeset
|
296 usec_sleep(get_delay() * 1000 * 1000); |
966 | 297 SDL_CloseAudio(); |
972 | 298 SDL_QuitSubSystem(SDL_INIT_AUDIO); |
966 | 299 } |
300 | |
301 // stop playing and empty buffers (for seeking/pause) | |
302 static void reset(){ | |
1066 | 303 |
1091 | 304 //printf("SDL: reset called!\n"); |
1066 | 305 |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
306 SDL_PauseAudio(1); |
972 | 307 /* Reset ring-buffer state */ |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
308 read_pos = 0; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
309 write_pos = 0; |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
310 SDL_PauseAudio(0); |
966 | 311 } |
312 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
313 // stop playing, keep buffers (for pause) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
314 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
315 { |
1070 | 316 |
1091 | 317 //printf("SDL: audio_pause called!\n"); |
7897
7674e94baff7
Change SDL_(Un)lockAudio to PauseAudio() (works better)
colin
parents:
7660
diff
changeset
|
318 SDL_PauseAudio(1); |
1070 | 319 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
320 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
321 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
322 // resume playing, after audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
323 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
324 { |
1091 | 325 //printf("SDL: audio_resume called!\n"); |
7897
7674e94baff7
Change SDL_(Un)lockAudio to PauseAudio() (works better)
colin
parents:
7660
diff
changeset
|
326 SDL_PauseAudio(0); |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
327 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
328 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
329 |
966 | 330 // return: how many bytes can be played without blocking |
331 static int get_space(){ | |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
332 return buf_free(); |
966 | 333 } |
334 | |
335 // plays 'len' bytes of 'data' | |
336 // it should round it down to outburst*n | |
337 // return: number of bytes played | |
338 static int play(void* data,int len,int flags){ | |
972 | 339 |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12145
diff
changeset
|
340 len = (len/ao_data.outburst)*ao_data.outburst; |
985 | 341 #if 0 |
972 | 342 int ret; |
966 | 343 |
972 | 344 /* Audio locking prohibits call of outputaudio */ |
966 | 345 SDL_LockAudio(); |
972 | 346 // copy audio stream into ring-buffer |
347 ret = write_buffer(data, len); | |
966 | 348 SDL_UnlockAudio(); |
349 | |
972 | 350 return ret; |
351 #else | |
352 return write_buffer(data, len); | |
353 #endif | |
966 | 354 } |
355 | |
3095 | 356 // return: delay in seconds between first and last sample in buffer |
357 static float get_delay(){ | |
13832
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
358 int buffered = BUFFSIZE - CHUNK_SIZE - buf_free(); // could be less |
2f8cfe66dbfd
Different buffering scheme, avoiding possible races (SDL is using threads!).
reimar
parents:
13383
diff
changeset
|
359 return (float)(buffered + ao_data.buffersize)/(float)ao_data.bps; |
966 | 360 } |
361 | |
362 | |
363 | |
364 | |
365 | |
366 |