root / audio / audio.c @ 46dc3881
History | View | Annotate | Download (48.4 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 85571bc7 | bellard | * QEMU Audio subsystem
|
3 | 1d14ffa9 | bellard | *
|
4 | 1d14ffa9 | bellard | * Copyright (c) 2003-2005 Vassili Karpov (malc)
|
5 | 1d14ffa9 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw/hw.h" |
25 | 87ecb68b | pbrook | #include "audio.h" |
26 | 87ecb68b | pbrook | #include "console.h" |
27 | 87ecb68b | pbrook | #include "qemu-timer.h" |
28 | 87ecb68b | pbrook | #include "sysemu.h" |
29 | 85571bc7 | bellard | |
30 | 1d14ffa9 | bellard | #define AUDIO_CAP "audio" |
31 | 1d14ffa9 | bellard | #include "audio_int.h" |
32 | 85571bc7 | bellard | |
33 | 1d14ffa9 | bellard | /* #define DEBUG_PLIVE */
|
34 | 1d14ffa9 | bellard | /* #define DEBUG_LIVE */
|
35 | 1d14ffa9 | bellard | /* #define DEBUG_OUT */
|
36 | 8ead62cf | bellard | /* #define DEBUG_CAPTURE */
|
37 | 85571bc7 | bellard | |
38 | c0fe3827 | bellard | #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown" |
39 | c0fe3827 | bellard | |
40 | 1d14ffa9 | bellard | static struct audio_driver *drvtab[] = { |
41 | 1d14ffa9 | bellard | #ifdef CONFIG_OSS
|
42 | 1d14ffa9 | bellard | &oss_audio_driver, |
43 | 1d14ffa9 | bellard | #endif
|
44 | 1d14ffa9 | bellard | #ifdef CONFIG_ALSA
|
45 | 1d14ffa9 | bellard | &alsa_audio_driver, |
46 | 1d14ffa9 | bellard | #endif
|
47 | 1d14ffa9 | bellard | #ifdef CONFIG_COREAUDIO
|
48 | 1d14ffa9 | bellard | &coreaudio_audio_driver, |
49 | 1d14ffa9 | bellard | #endif
|
50 | 1d14ffa9 | bellard | #ifdef CONFIG_DSOUND
|
51 | 1d14ffa9 | bellard | &dsound_audio_driver, |
52 | 1d14ffa9 | bellard | #endif
|
53 | 1d14ffa9 | bellard | #ifdef CONFIG_FMOD
|
54 | 1d14ffa9 | bellard | &fmod_audio_driver, |
55 | 1d14ffa9 | bellard | #endif
|
56 | 1d14ffa9 | bellard | #ifdef CONFIG_SDL
|
57 | 1d14ffa9 | bellard | &sdl_audio_driver, |
58 | 1d14ffa9 | bellard | #endif
|
59 | ca9cc28c | balrog | #ifdef CONFIG_ESD
|
60 | ca9cc28c | balrog | &esd_audio_driver, |
61 | ca9cc28c | balrog | #endif
|
62 | 1d14ffa9 | bellard | &no_audio_driver, |
63 | 1d14ffa9 | bellard | &wav_audio_driver |
64 | 1d14ffa9 | bellard | }; |
65 | 85571bc7 | bellard | |
66 | c0fe3827 | bellard | struct fixed_settings {
|
67 | c0fe3827 | bellard | int enabled;
|
68 | c0fe3827 | bellard | int nb_voices;
|
69 | c0fe3827 | bellard | int greedy;
|
70 | c0fe3827 | bellard | audsettings_t settings; |
71 | c0fe3827 | bellard | }; |
72 | c0fe3827 | bellard | |
73 | c0fe3827 | bellard | static struct { |
74 | c0fe3827 | bellard | struct fixed_settings fixed_out;
|
75 | c0fe3827 | bellard | struct fixed_settings fixed_in;
|
76 | c0fe3827 | bellard | union {
|
77 | c0fe3827 | bellard | int hz;
|
78 | c0fe3827 | bellard | int64_t ticks; |
79 | c0fe3827 | bellard | } period; |
80 | c0fe3827 | bellard | int plive;
|
81 | 541e0844 | bellard | int log_to_monitor;
|
82 | c0fe3827 | bellard | } conf = { |
83 | c0fe3827 | bellard | { /* DAC fixed settings */
|
84 | c0fe3827 | bellard | 1, /* enabled */ |
85 | c0fe3827 | bellard | 1, /* nb_voices */ |
86 | c0fe3827 | bellard | 1, /* greedy */ |
87 | c0fe3827 | bellard | { |
88 | c0fe3827 | bellard | 44100, /* freq */ |
89 | c0fe3827 | bellard | 2, /* nchannels */ |
90 | f941aa25 | ths | AUD_FMT_S16, /* fmt */
|
91 | f941aa25 | ths | AUDIO_HOST_ENDIANNESS |
92 | c0fe3827 | bellard | } |
93 | c0fe3827 | bellard | }, |
94 | c0fe3827 | bellard | |
95 | c0fe3827 | bellard | { /* ADC fixed settings */
|
96 | c0fe3827 | bellard | 1, /* enabled */ |
97 | c0fe3827 | bellard | 1, /* nb_voices */ |
98 | c0fe3827 | bellard | 1, /* greedy */ |
99 | c0fe3827 | bellard | { |
100 | c0fe3827 | bellard | 44100, /* freq */ |
101 | c0fe3827 | bellard | 2, /* nchannels */ |
102 | f941aa25 | ths | AUD_FMT_S16, /* fmt */
|
103 | f941aa25 | ths | AUDIO_HOST_ENDIANNESS |
104 | c0fe3827 | bellard | } |
105 | c0fe3827 | bellard | }, |
106 | c0fe3827 | bellard | |
107 | 1d14ffa9 | bellard | { 0 }, /* period */ |
108 | 541e0844 | bellard | 0, /* plive */ |
109 | 571ec3d6 | bellard | 0 /* log_to_monitor */ |
110 | 1d14ffa9 | bellard | }; |
111 | 1d14ffa9 | bellard | |
112 | c0fe3827 | bellard | static AudioState glob_audio_state;
|
113 | c0fe3827 | bellard | |
114 | 1d14ffa9 | bellard | volume_t nominal_volume = { |
115 | 1d14ffa9 | bellard | 0,
|
116 | 1d14ffa9 | bellard | #ifdef FLOAT_MIXENG
|
117 | 1d14ffa9 | bellard | 1.0, |
118 | 1d14ffa9 | bellard | 1.0 |
119 | 1d14ffa9 | bellard | #else
|
120 | 683efdcb | balrog | 1ULL << 32, |
121 | 683efdcb | balrog | 1ULL << 32 |
122 | 1d14ffa9 | bellard | #endif
|
123 | 85571bc7 | bellard | }; |
124 | 85571bc7 | bellard | |
125 | 85571bc7 | bellard | /* http://www.df.lth.se/~john_e/gems/gem002d.html */
|
126 | 85571bc7 | bellard | /* http://www.multi-platforms.com/Tips/PopCount.htm */
|
127 | 85571bc7 | bellard | uint32_t popcount (uint32_t u) |
128 | 85571bc7 | bellard | { |
129 | 85571bc7 | bellard | u = ((u&0x55555555) + ((u>>1)&0x55555555)); |
130 | 85571bc7 | bellard | u = ((u&0x33333333) + ((u>>2)&0x33333333)); |
131 | 85571bc7 | bellard | u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f)); |
132 | 85571bc7 | bellard | u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff)); |
133 | 85571bc7 | bellard | u = ( u&0x0000ffff) + (u>>16); |
134 | 85571bc7 | bellard | return u;
|
135 | 85571bc7 | bellard | } |
136 | 85571bc7 | bellard | |
137 | 85571bc7 | bellard | inline uint32_t lsbindex (uint32_t u)
|
138 | 85571bc7 | bellard | { |
139 | 85571bc7 | bellard | return popcount ((u&-u)-1); |
140 | 85571bc7 | bellard | } |
141 | 85571bc7 | bellard | |
142 | 1d14ffa9 | bellard | #ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
|
143 | 1d14ffa9 | bellard | #error No its not
|
144 | 1d14ffa9 | bellard | #else
|
145 | 1d14ffa9 | bellard | int audio_bug (const char *funcname, int cond) |
146 | 85571bc7 | bellard | { |
147 | 1d14ffa9 | bellard | if (cond) {
|
148 | 1d14ffa9 | bellard | static int shown; |
149 | 1d14ffa9 | bellard | |
150 | 8ead62cf | bellard | AUD_log (NULL, "A bug was just triggered in %s\n", funcname); |
151 | 1d14ffa9 | bellard | if (!shown) {
|
152 | 1d14ffa9 | bellard | shown = 1;
|
153 | 1d14ffa9 | bellard | AUD_log (NULL, "Save all your work and restart without audio\n"); |
154 | 1d14ffa9 | bellard | AUD_log (NULL, "Please send bug report to malc@pulsesoft.com\n"); |
155 | 1d14ffa9 | bellard | AUD_log (NULL, "I am sorry\n"); |
156 | 1d14ffa9 | bellard | } |
157 | 1d14ffa9 | bellard | AUD_log (NULL, "Context:\n"); |
158 | 1d14ffa9 | bellard | |
159 | 1d14ffa9 | bellard | #if defined AUDIO_BREAKPOINT_ON_BUG
|
160 | 1d14ffa9 | bellard | # if defined HOST_I386
|
161 | 1d14ffa9 | bellard | # if defined __GNUC__
|
162 | 1d14ffa9 | bellard | __asm__ ("int3");
|
163 | 1d14ffa9 | bellard | # elif defined _MSC_VER
|
164 | 1d14ffa9 | bellard | _asm _emit 0xcc;
|
165 | 1d14ffa9 | bellard | # else
|
166 | 1d14ffa9 | bellard | abort (); |
167 | 1d14ffa9 | bellard | # endif
|
168 | 1d14ffa9 | bellard | # else
|
169 | 1d14ffa9 | bellard | abort (); |
170 | 1d14ffa9 | bellard | # endif
|
171 | 1d14ffa9 | bellard | #endif
|
172 | 85571bc7 | bellard | } |
173 | 85571bc7 | bellard | |
174 | 1d14ffa9 | bellard | return cond;
|
175 | 85571bc7 | bellard | } |
176 | 1d14ffa9 | bellard | #endif
|
177 | 85571bc7 | bellard | |
178 | f941aa25 | ths | static inline int audio_bits_to_index (int bits) |
179 | f941aa25 | ths | { |
180 | f941aa25 | ths | switch (bits) {
|
181 | f941aa25 | ths | case 8: |
182 | f941aa25 | ths | return 0; |
183 | f941aa25 | ths | |
184 | f941aa25 | ths | case 16: |
185 | f941aa25 | ths | return 1; |
186 | f941aa25 | ths | |
187 | f941aa25 | ths | case 32: |
188 | f941aa25 | ths | return 2; |
189 | f941aa25 | ths | |
190 | f941aa25 | ths | default:
|
191 | f941aa25 | ths | audio_bug ("bits_to_index", 1); |
192 | f941aa25 | ths | AUD_log (NULL, "invalid bits %d\n", bits); |
193 | f941aa25 | ths | return 0; |
194 | f941aa25 | ths | } |
195 | f941aa25 | ths | } |
196 | f941aa25 | ths | |
197 | c0fe3827 | bellard | void *audio_calloc (const char *funcname, int nmemb, size_t size) |
198 | c0fe3827 | bellard | { |
199 | c0fe3827 | bellard | int cond;
|
200 | c0fe3827 | bellard | size_t len; |
201 | c0fe3827 | bellard | |
202 | c0fe3827 | bellard | len = nmemb * size; |
203 | c0fe3827 | bellard | cond = !nmemb || !size; |
204 | c0fe3827 | bellard | cond |= nmemb < 0;
|
205 | c0fe3827 | bellard | cond |= len < size; |
206 | c0fe3827 | bellard | |
207 | c0fe3827 | bellard | if (audio_bug ("audio_calloc", cond)) { |
208 | c0fe3827 | bellard | AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n", |
209 | c0fe3827 | bellard | funcname); |
210 | 541e0844 | bellard | AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len); |
211 | c0fe3827 | bellard | return NULL; |
212 | c0fe3827 | bellard | } |
213 | c0fe3827 | bellard | |
214 | c0fe3827 | bellard | return qemu_mallocz (len);
|
215 | c0fe3827 | bellard | } |
216 | c0fe3827 | bellard | |
217 | 1d14ffa9 | bellard | static char *audio_alloc_prefix (const char *s) |
218 | 85571bc7 | bellard | { |
219 | 1d14ffa9 | bellard | const char qemu_prefix[] = "QEMU_"; |
220 | 1d14ffa9 | bellard | size_t len; |
221 | 1d14ffa9 | bellard | char *r;
|
222 | 85571bc7 | bellard | |
223 | 1d14ffa9 | bellard | if (!s) {
|
224 | 1d14ffa9 | bellard | return NULL; |
225 | 1d14ffa9 | bellard | } |
226 | 85571bc7 | bellard | |
227 | 1d14ffa9 | bellard | len = strlen (s); |
228 | 1d14ffa9 | bellard | r = qemu_malloc (len + sizeof (qemu_prefix));
|
229 | 85571bc7 | bellard | |
230 | 1d14ffa9 | bellard | if (r) {
|
231 | 1d14ffa9 | bellard | size_t i; |
232 | 1d14ffa9 | bellard | char *u = r + sizeof (qemu_prefix) - 1; |
233 | 85571bc7 | bellard | |
234 | 1d14ffa9 | bellard | strcpy (r, qemu_prefix); |
235 | 1d14ffa9 | bellard | strcat (r, s); |
236 | 1d14ffa9 | bellard | |
237 | 1d14ffa9 | bellard | for (i = 0; i < len; ++i) { |
238 | 1d14ffa9 | bellard | u[i] = toupper (u[i]); |
239 | 1d14ffa9 | bellard | } |
240 | 85571bc7 | bellard | } |
241 | 1d14ffa9 | bellard | return r;
|
242 | 85571bc7 | bellard | } |
243 | 85571bc7 | bellard | |
244 | 9596ebb7 | pbrook | static const char *audio_audfmt_to_string (audfmt_e fmt) |
245 | 85571bc7 | bellard | { |
246 | 1d14ffa9 | bellard | switch (fmt) {
|
247 | 1d14ffa9 | bellard | case AUD_FMT_U8:
|
248 | 1d14ffa9 | bellard | return "U8"; |
249 | 85571bc7 | bellard | |
250 | 1d14ffa9 | bellard | case AUD_FMT_U16:
|
251 | 1d14ffa9 | bellard | return "U16"; |
252 | 85571bc7 | bellard | |
253 | 85571bc7 | bellard | case AUD_FMT_S8:
|
254 | 1d14ffa9 | bellard | return "S8"; |
255 | 85571bc7 | bellard | |
256 | 85571bc7 | bellard | case AUD_FMT_S16:
|
257 | 1d14ffa9 | bellard | return "S16"; |
258 | f941aa25 | ths | |
259 | f941aa25 | ths | case AUD_FMT_U32:
|
260 | f941aa25 | ths | return "U32"; |
261 | f941aa25 | ths | |
262 | f941aa25 | ths | case AUD_FMT_S32:
|
263 | f941aa25 | ths | return "S32"; |
264 | 85571bc7 | bellard | } |
265 | 85571bc7 | bellard | |
266 | 1d14ffa9 | bellard | dolog ("Bogus audfmt %d returning S16\n", fmt);
|
267 | 1d14ffa9 | bellard | return "S16"; |
268 | 85571bc7 | bellard | } |
269 | 85571bc7 | bellard | |
270 | 9596ebb7 | pbrook | static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval, |
271 | 9596ebb7 | pbrook | int *defaultp)
|
272 | 85571bc7 | bellard | { |
273 | 1d14ffa9 | bellard | if (!strcasecmp (s, "u8")) { |
274 | 1d14ffa9 | bellard | *defaultp = 0;
|
275 | 1d14ffa9 | bellard | return AUD_FMT_U8;
|
276 | 1d14ffa9 | bellard | } |
277 | 1d14ffa9 | bellard | else if (!strcasecmp (s, "u16")) { |
278 | 1d14ffa9 | bellard | *defaultp = 0;
|
279 | 1d14ffa9 | bellard | return AUD_FMT_U16;
|
280 | 1d14ffa9 | bellard | } |
281 | f941aa25 | ths | else if (!strcasecmp (s, "u32")) { |
282 | f941aa25 | ths | *defaultp = 0;
|
283 | f941aa25 | ths | return AUD_FMT_U32;
|
284 | f941aa25 | ths | } |
285 | 1d14ffa9 | bellard | else if (!strcasecmp (s, "s8")) { |
286 | 1d14ffa9 | bellard | *defaultp = 0;
|
287 | 1d14ffa9 | bellard | return AUD_FMT_S8;
|
288 | 1d14ffa9 | bellard | } |
289 | 1d14ffa9 | bellard | else if (!strcasecmp (s, "s16")) { |
290 | 1d14ffa9 | bellard | *defaultp = 0;
|
291 | 1d14ffa9 | bellard | return AUD_FMT_S16;
|
292 | 1d14ffa9 | bellard | } |
293 | f941aa25 | ths | else if (!strcasecmp (s, "s32")) { |
294 | f941aa25 | ths | *defaultp = 0;
|
295 | f941aa25 | ths | return AUD_FMT_S32;
|
296 | f941aa25 | ths | } |
297 | 1d14ffa9 | bellard | else {
|
298 | 1d14ffa9 | bellard | dolog ("Bogus audio format `%s' using %s\n",
|
299 | 1d14ffa9 | bellard | s, audio_audfmt_to_string (defval)); |
300 | 1d14ffa9 | bellard | *defaultp = 1;
|
301 | 1d14ffa9 | bellard | return defval;
|
302 | 1d14ffa9 | bellard | } |
303 | 85571bc7 | bellard | } |
304 | 85571bc7 | bellard | |
305 | 1d14ffa9 | bellard | static audfmt_e audio_get_conf_fmt (const char *envname, |
306 | 1d14ffa9 | bellard | audfmt_e defval, |
307 | 1d14ffa9 | bellard | int *defaultp)
|
308 | 85571bc7 | bellard | { |
309 | 1d14ffa9 | bellard | const char *var = getenv (envname); |
310 | 1d14ffa9 | bellard | if (!var) {
|
311 | 1d14ffa9 | bellard | *defaultp = 1;
|
312 | 1d14ffa9 | bellard | return defval;
|
313 | 85571bc7 | bellard | } |
314 | 1d14ffa9 | bellard | return audio_string_to_audfmt (var, defval, defaultp);
|
315 | 85571bc7 | bellard | } |
316 | 85571bc7 | bellard | |
317 | 1d14ffa9 | bellard | static int audio_get_conf_int (const char *key, int defval, int *defaultp) |
318 | 85571bc7 | bellard | { |
319 | 1d14ffa9 | bellard | int val;
|
320 | 1d14ffa9 | bellard | char *strval;
|
321 | 85571bc7 | bellard | |
322 | 1d14ffa9 | bellard | strval = getenv (key); |
323 | 1d14ffa9 | bellard | if (strval) {
|
324 | 1d14ffa9 | bellard | *defaultp = 0;
|
325 | 1d14ffa9 | bellard | val = atoi (strval); |
326 | 1d14ffa9 | bellard | return val;
|
327 | 1d14ffa9 | bellard | } |
328 | 1d14ffa9 | bellard | else {
|
329 | 1d14ffa9 | bellard | *defaultp = 1;
|
330 | 1d14ffa9 | bellard | return defval;
|
331 | 1d14ffa9 | bellard | } |
332 | 85571bc7 | bellard | } |
333 | 85571bc7 | bellard | |
334 | 1d14ffa9 | bellard | static const char *audio_get_conf_str (const char *key, |
335 | 1d14ffa9 | bellard | const char *defval, |
336 | 1d14ffa9 | bellard | int *defaultp)
|
337 | 85571bc7 | bellard | { |
338 | 1d14ffa9 | bellard | const char *val = getenv (key); |
339 | 1d14ffa9 | bellard | if (!val) {
|
340 | 1d14ffa9 | bellard | *defaultp = 1;
|
341 | 1d14ffa9 | bellard | return defval;
|
342 | 1d14ffa9 | bellard | } |
343 | 1d14ffa9 | bellard | else {
|
344 | 1d14ffa9 | bellard | *defaultp = 0;
|
345 | 1d14ffa9 | bellard | return val;
|
346 | 85571bc7 | bellard | } |
347 | 85571bc7 | bellard | } |
348 | 85571bc7 | bellard | |
349 | 541e0844 | bellard | void AUD_vlog (const char *cap, const char *fmt, va_list ap) |
350 | 85571bc7 | bellard | { |
351 | 541e0844 | bellard | if (conf.log_to_monitor) {
|
352 | 541e0844 | bellard | if (cap) {
|
353 | 541e0844 | bellard | term_printf ("%s: ", cap);
|
354 | 541e0844 | bellard | } |
355 | 541e0844 | bellard | |
356 | 541e0844 | bellard | term_vprintf (fmt, ap); |
357 | 541e0844 | bellard | } |
358 | 541e0844 | bellard | else {
|
359 | 541e0844 | bellard | if (cap) {
|
360 | 541e0844 | bellard | fprintf (stderr, "%s: ", cap);
|
361 | 541e0844 | bellard | } |
362 | 541e0844 | bellard | |
363 | 541e0844 | bellard | vfprintf (stderr, fmt, ap); |
364 | 85571bc7 | bellard | } |
365 | 85571bc7 | bellard | } |
366 | 85571bc7 | bellard | |
367 | 541e0844 | bellard | void AUD_log (const char *cap, const char *fmt, ...) |
368 | 85571bc7 | bellard | { |
369 | 541e0844 | bellard | va_list ap; |
370 | 541e0844 | bellard | |
371 | 541e0844 | bellard | va_start (ap, fmt); |
372 | 541e0844 | bellard | AUD_vlog (cap, fmt, ap); |
373 | 541e0844 | bellard | va_end (ap); |
374 | 85571bc7 | bellard | } |
375 | 85571bc7 | bellard | |
376 | 1d14ffa9 | bellard | static void audio_print_options (const char *prefix, |
377 | 1d14ffa9 | bellard | struct audio_option *opt)
|
378 | 85571bc7 | bellard | { |
379 | 1d14ffa9 | bellard | char *uprefix;
|
380 | 1d14ffa9 | bellard | |
381 | 1d14ffa9 | bellard | if (!prefix) {
|
382 | 1d14ffa9 | bellard | dolog ("No prefix specified\n");
|
383 | 1d14ffa9 | bellard | return;
|
384 | 1d14ffa9 | bellard | } |
385 | 1d14ffa9 | bellard | |
386 | 1d14ffa9 | bellard | if (!opt) {
|
387 | 1d14ffa9 | bellard | dolog ("No options\n");
|
388 | 85571bc7 | bellard | return;
|
389 | 1d14ffa9 | bellard | } |
390 | 85571bc7 | bellard | |
391 | 1d14ffa9 | bellard | uprefix = audio_alloc_prefix (prefix); |
392 | 85571bc7 | bellard | |
393 | 1d14ffa9 | bellard | for (; opt->name; opt++) {
|
394 | 1d14ffa9 | bellard | const char *state = "default"; |
395 | 1d14ffa9 | bellard | printf (" %s_%s: ", uprefix, opt->name);
|
396 | 85571bc7 | bellard | |
397 | fe8f096b | ths | if (opt->overriddenp && *opt->overriddenp) {
|
398 | 1d14ffa9 | bellard | state = "current";
|
399 | 1d14ffa9 | bellard | } |
400 | 85571bc7 | bellard | |
401 | 1d14ffa9 | bellard | switch (opt->tag) {
|
402 | 1d14ffa9 | bellard | case AUD_OPT_BOOL:
|
403 | 1d14ffa9 | bellard | { |
404 | 1d14ffa9 | bellard | int *intp = opt->valp;
|
405 | 1d14ffa9 | bellard | printf ("boolean, %s = %d\n", state, *intp ? 1 : 0); |
406 | 1d14ffa9 | bellard | } |
407 | 1d14ffa9 | bellard | break;
|
408 | 1d14ffa9 | bellard | |
409 | 1d14ffa9 | bellard | case AUD_OPT_INT:
|
410 | 1d14ffa9 | bellard | { |
411 | 1d14ffa9 | bellard | int *intp = opt->valp;
|
412 | 1d14ffa9 | bellard | printf ("integer, %s = %d\n", state, *intp);
|
413 | 1d14ffa9 | bellard | } |
414 | 1d14ffa9 | bellard | break;
|
415 | 1d14ffa9 | bellard | |
416 | 1d14ffa9 | bellard | case AUD_OPT_FMT:
|
417 | 1d14ffa9 | bellard | { |
418 | 1d14ffa9 | bellard | audfmt_e *fmtp = opt->valp; |
419 | 1d14ffa9 | bellard | printf ( |
420 | ca9cc28c | balrog | "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
|
421 | 1d14ffa9 | bellard | state, |
422 | 1d14ffa9 | bellard | audio_audfmt_to_string (*fmtp) |
423 | 1d14ffa9 | bellard | ); |
424 | 1d14ffa9 | bellard | } |
425 | 1d14ffa9 | bellard | break;
|
426 | 1d14ffa9 | bellard | |
427 | 1d14ffa9 | bellard | case AUD_OPT_STR:
|
428 | 1d14ffa9 | bellard | { |
429 | 1d14ffa9 | bellard | const char **strp = opt->valp; |
430 | 1d14ffa9 | bellard | printf ("string, %s = %s\n",
|
431 | 1d14ffa9 | bellard | state, |
432 | 1d14ffa9 | bellard | *strp ? *strp : "(not set)");
|
433 | 85571bc7 | bellard | } |
434 | 1d14ffa9 | bellard | break;
|
435 | 1d14ffa9 | bellard | |
436 | 1d14ffa9 | bellard | default:
|
437 | 1d14ffa9 | bellard | printf ("???\n");
|
438 | 1d14ffa9 | bellard | dolog ("Bad value tag for option %s_%s %d\n",
|
439 | 1d14ffa9 | bellard | uprefix, opt->name, opt->tag); |
440 | 1d14ffa9 | bellard | break;
|
441 | 85571bc7 | bellard | } |
442 | 1d14ffa9 | bellard | printf (" %s\n", opt->descr);
|
443 | 85571bc7 | bellard | } |
444 | 1d14ffa9 | bellard | |
445 | 1d14ffa9 | bellard | qemu_free (uprefix); |
446 | 85571bc7 | bellard | } |
447 | 85571bc7 | bellard | |
448 | 1d14ffa9 | bellard | static void audio_process_options (const char *prefix, |
449 | 1d14ffa9 | bellard | struct audio_option *opt)
|
450 | 85571bc7 | bellard | { |
451 | 1d14ffa9 | bellard | char *optname;
|
452 | 1d14ffa9 | bellard | const char qemu_prefix[] = "QEMU_"; |
453 | 1d14ffa9 | bellard | size_t preflen; |
454 | 85571bc7 | bellard | |
455 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, !prefix)) {
|
456 | 1d14ffa9 | bellard | dolog ("prefix = NULL\n");
|
457 | 1d14ffa9 | bellard | return;
|
458 | 1d14ffa9 | bellard | } |
459 | 85571bc7 | bellard | |
460 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, !opt)) {
|
461 | 1d14ffa9 | bellard | dolog ("opt = NULL\n");
|
462 | 1d14ffa9 | bellard | return;
|
463 | 85571bc7 | bellard | } |
464 | 85571bc7 | bellard | |
465 | 1d14ffa9 | bellard | preflen = strlen (prefix); |
466 | 85571bc7 | bellard | |
467 | 1d14ffa9 | bellard | for (; opt->name; opt++) {
|
468 | 1d14ffa9 | bellard | size_t len, i; |
469 | 1d14ffa9 | bellard | int def;
|
470 | 1d14ffa9 | bellard | |
471 | 1d14ffa9 | bellard | if (!opt->valp) {
|
472 | 1d14ffa9 | bellard | dolog ("Option value pointer for `%s' is not set\n",
|
473 | 1d14ffa9 | bellard | opt->name); |
474 | 1d14ffa9 | bellard | continue;
|
475 | 1d14ffa9 | bellard | } |
476 | 1d14ffa9 | bellard | |
477 | 1d14ffa9 | bellard | len = strlen (opt->name); |
478 | c0fe3827 | bellard | /* len of opt->name + len of prefix + size of qemu_prefix
|
479 | c0fe3827 | bellard | * (includes trailing zero) + zero + underscore (on behalf of
|
480 | c0fe3827 | bellard | * sizeof) */
|
481 | 1d14ffa9 | bellard | optname = qemu_malloc (len + preflen + sizeof (qemu_prefix) + 1); |
482 | 1d14ffa9 | bellard | if (!optname) {
|
483 | c0fe3827 | bellard | dolog ("Could not allocate memory for option name `%s'\n",
|
484 | 1d14ffa9 | bellard | opt->name); |
485 | 1d14ffa9 | bellard | continue;
|
486 | 1d14ffa9 | bellard | } |
487 | 1d14ffa9 | bellard | |
488 | 1d14ffa9 | bellard | strcpy (optname, qemu_prefix); |
489 | c0fe3827 | bellard | |
490 | c0fe3827 | bellard | /* copy while upper-casing, including trailing zero */
|
491 | 1d14ffa9 | bellard | for (i = 0; i <= preflen; ++i) { |
492 | 1d14ffa9 | bellard | optname[i + sizeof (qemu_prefix) - 1] = toupper (prefix[i]); |
493 | 1d14ffa9 | bellard | } |
494 | 1d14ffa9 | bellard | strcat (optname, "_");
|
495 | 1d14ffa9 | bellard | strcat (optname, opt->name); |
496 | 1d14ffa9 | bellard | |
497 | 1d14ffa9 | bellard | def = 1;
|
498 | 1d14ffa9 | bellard | switch (opt->tag) {
|
499 | 1d14ffa9 | bellard | case AUD_OPT_BOOL:
|
500 | 1d14ffa9 | bellard | case AUD_OPT_INT:
|
501 | 1d14ffa9 | bellard | { |
502 | 1d14ffa9 | bellard | int *intp = opt->valp;
|
503 | 1d14ffa9 | bellard | *intp = audio_get_conf_int (optname, *intp, &def); |
504 | 1d14ffa9 | bellard | } |
505 | 1d14ffa9 | bellard | break;
|
506 | 1d14ffa9 | bellard | |
507 | 1d14ffa9 | bellard | case AUD_OPT_FMT:
|
508 | 1d14ffa9 | bellard | { |
509 | 1d14ffa9 | bellard | audfmt_e *fmtp = opt->valp; |
510 | 1d14ffa9 | bellard | *fmtp = audio_get_conf_fmt (optname, *fmtp, &def); |
511 | 1d14ffa9 | bellard | } |
512 | 1d14ffa9 | bellard | break;
|
513 | 1d14ffa9 | bellard | |
514 | 1d14ffa9 | bellard | case AUD_OPT_STR:
|
515 | 1d14ffa9 | bellard | { |
516 | 1d14ffa9 | bellard | const char **strp = opt->valp; |
517 | 1d14ffa9 | bellard | *strp = audio_get_conf_str (optname, *strp, &def); |
518 | 1d14ffa9 | bellard | } |
519 | 1d14ffa9 | bellard | break;
|
520 | 1d14ffa9 | bellard | |
521 | 1d14ffa9 | bellard | default:
|
522 | 1d14ffa9 | bellard | dolog ("Bad value tag for option `%s' - %d\n",
|
523 | 1d14ffa9 | bellard | optname, opt->tag); |
524 | 85571bc7 | bellard | break;
|
525 | 85571bc7 | bellard | } |
526 | 85571bc7 | bellard | |
527 | fe8f096b | ths | if (!opt->overriddenp) {
|
528 | fe8f096b | ths | opt->overriddenp = &opt->overridden; |
529 | 1d14ffa9 | bellard | } |
530 | fe8f096b | ths | *opt->overriddenp = !def; |
531 | 1d14ffa9 | bellard | qemu_free (optname); |
532 | 1d14ffa9 | bellard | } |
533 | 85571bc7 | bellard | } |
534 | 85571bc7 | bellard | |
535 | c0fe3827 | bellard | static void audio_print_settings (audsettings_t *as) |
536 | c0fe3827 | bellard | { |
537 | c0fe3827 | bellard | dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
|
538 | c0fe3827 | bellard | |
539 | c0fe3827 | bellard | switch (as->fmt) {
|
540 | c0fe3827 | bellard | case AUD_FMT_S8:
|
541 | c0fe3827 | bellard | AUD_log (NULL, "S8"); |
542 | c0fe3827 | bellard | break;
|
543 | c0fe3827 | bellard | case AUD_FMT_U8:
|
544 | c0fe3827 | bellard | AUD_log (NULL, "U8"); |
545 | c0fe3827 | bellard | break;
|
546 | c0fe3827 | bellard | case AUD_FMT_S16:
|
547 | c0fe3827 | bellard | AUD_log (NULL, "S16"); |
548 | c0fe3827 | bellard | break;
|
549 | c0fe3827 | bellard | case AUD_FMT_U16:
|
550 | c0fe3827 | bellard | AUD_log (NULL, "U16"); |
551 | c0fe3827 | bellard | break;
|
552 | c0fe3827 | bellard | default:
|
553 | c0fe3827 | bellard | AUD_log (NULL, "invalid(%d)", as->fmt); |
554 | c0fe3827 | bellard | break;
|
555 | c0fe3827 | bellard | } |
556 | ec36b695 | bellard | |
557 | ec36b695 | bellard | AUD_log (NULL, " endianness="); |
558 | d929eba5 | bellard | switch (as->endianness) {
|
559 | d929eba5 | bellard | case 0: |
560 | d929eba5 | bellard | AUD_log (NULL, "little"); |
561 | d929eba5 | bellard | break;
|
562 | d929eba5 | bellard | case 1: |
563 | d929eba5 | bellard | AUD_log (NULL, "big"); |
564 | d929eba5 | bellard | break;
|
565 | d929eba5 | bellard | default:
|
566 | d929eba5 | bellard | AUD_log (NULL, "invalid"); |
567 | d929eba5 | bellard | break;
|
568 | d929eba5 | bellard | } |
569 | c0fe3827 | bellard | AUD_log (NULL, "\n"); |
570 | c0fe3827 | bellard | } |
571 | c0fe3827 | bellard | |
572 | ec36b695 | bellard | static int audio_validate_settings (audsettings_t *as) |
573 | c0fe3827 | bellard | { |
574 | c0fe3827 | bellard | int invalid;
|
575 | c0fe3827 | bellard | |
576 | c0fe3827 | bellard | invalid = as->nchannels != 1 && as->nchannels != 2; |
577 | d929eba5 | bellard | invalid |= as->endianness != 0 && as->endianness != 1; |
578 | c0fe3827 | bellard | |
579 | c0fe3827 | bellard | switch (as->fmt) {
|
580 | c0fe3827 | bellard | case AUD_FMT_S8:
|
581 | c0fe3827 | bellard | case AUD_FMT_U8:
|
582 | c0fe3827 | bellard | case AUD_FMT_S16:
|
583 | c0fe3827 | bellard | case AUD_FMT_U16:
|
584 | f941aa25 | ths | case AUD_FMT_S32:
|
585 | f941aa25 | ths | case AUD_FMT_U32:
|
586 | c0fe3827 | bellard | break;
|
587 | c0fe3827 | bellard | default:
|
588 | c0fe3827 | bellard | invalid = 1;
|
589 | c0fe3827 | bellard | break;
|
590 | c0fe3827 | bellard | } |
591 | c0fe3827 | bellard | |
592 | c0fe3827 | bellard | invalid |= as->freq <= 0;
|
593 | d929eba5 | bellard | return invalid ? -1 : 0; |
594 | c0fe3827 | bellard | } |
595 | c0fe3827 | bellard | |
596 | c0fe3827 | bellard | static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as) |
597 | 85571bc7 | bellard | { |
598 | 1d14ffa9 | bellard | int bits = 8, sign = 0; |
599 | 85571bc7 | bellard | |
600 | c0fe3827 | bellard | switch (as->fmt) {
|
601 | 1d14ffa9 | bellard | case AUD_FMT_S8:
|
602 | 1d14ffa9 | bellard | sign = 1;
|
603 | 1d14ffa9 | bellard | case AUD_FMT_U8:
|
604 | 1d14ffa9 | bellard | break;
|
605 | 1d14ffa9 | bellard | |
606 | 1d14ffa9 | bellard | case AUD_FMT_S16:
|
607 | 1d14ffa9 | bellard | sign = 1;
|
608 | 1d14ffa9 | bellard | case AUD_FMT_U16:
|
609 | 1d14ffa9 | bellard | bits = 16;
|
610 | 1d14ffa9 | bellard | break;
|
611 | f941aa25 | ths | |
612 | f941aa25 | ths | case AUD_FMT_S32:
|
613 | f941aa25 | ths | sign = 1;
|
614 | f941aa25 | ths | case AUD_FMT_U32:
|
615 | f941aa25 | ths | bits = 32;
|
616 | f941aa25 | ths | break;
|
617 | 85571bc7 | bellard | } |
618 | c0fe3827 | bellard | return info->freq == as->freq
|
619 | c0fe3827 | bellard | && info->nchannels == as->nchannels |
620 | 1d14ffa9 | bellard | && info->sign == sign |
621 | d929eba5 | bellard | && info->bits == bits |
622 | d929eba5 | bellard | && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS); |
623 | 1d14ffa9 | bellard | } |
624 | 85571bc7 | bellard | |
625 | d929eba5 | bellard | void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as) |
626 | 1d14ffa9 | bellard | { |
627 | f941aa25 | ths | int bits = 8, sign = 0, shift = 0; |
628 | 1d14ffa9 | bellard | |
629 | c0fe3827 | bellard | switch (as->fmt) {
|
630 | 85571bc7 | bellard | case AUD_FMT_S8:
|
631 | 85571bc7 | bellard | sign = 1;
|
632 | 85571bc7 | bellard | case AUD_FMT_U8:
|
633 | 85571bc7 | bellard | break;
|
634 | 85571bc7 | bellard | |
635 | 85571bc7 | bellard | case AUD_FMT_S16:
|
636 | 85571bc7 | bellard | sign = 1;
|
637 | 85571bc7 | bellard | case AUD_FMT_U16:
|
638 | 85571bc7 | bellard | bits = 16;
|
639 | f941aa25 | ths | shift = 1;
|
640 | f941aa25 | ths | break;
|
641 | f941aa25 | ths | |
642 | f941aa25 | ths | case AUD_FMT_S32:
|
643 | f941aa25 | ths | sign = 1;
|
644 | f941aa25 | ths | case AUD_FMT_U32:
|
645 | f941aa25 | ths | bits = 32;
|
646 | f941aa25 | ths | shift = 2;
|
647 | 85571bc7 | bellard | break;
|
648 | 85571bc7 | bellard | } |
649 | 85571bc7 | bellard | |
650 | c0fe3827 | bellard | info->freq = as->freq; |
651 | 1d14ffa9 | bellard | info->bits = bits; |
652 | 1d14ffa9 | bellard | info->sign = sign; |
653 | c0fe3827 | bellard | info->nchannels = as->nchannels; |
654 | f941aa25 | ths | info->shift = (as->nchannels == 2) + shift;
|
655 | 1d14ffa9 | bellard | info->align = (1 << info->shift) - 1; |
656 | 1d14ffa9 | bellard | info->bytes_per_second = info->freq << info->shift; |
657 | d929eba5 | bellard | info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS); |
658 | 85571bc7 | bellard | } |
659 | 85571bc7 | bellard | |
660 | 1d14ffa9 | bellard | void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len) |
661 | 85571bc7 | bellard | { |
662 | 1d14ffa9 | bellard | if (!len) {
|
663 | 1d14ffa9 | bellard | return;
|
664 | 1d14ffa9 | bellard | } |
665 | 1d14ffa9 | bellard | |
666 | 1d14ffa9 | bellard | if (info->sign) {
|
667 | e2f909be | bellard | memset (buf, 0x00, len << info->shift);
|
668 | 85571bc7 | bellard | } |
669 | 85571bc7 | bellard | else {
|
670 | f941aa25 | ths | switch (info->bits) {
|
671 | f941aa25 | ths | case 8: |
672 | e2f909be | bellard | memset (buf, 0x80, len << info->shift);
|
673 | f941aa25 | ths | break;
|
674 | 1d14ffa9 | bellard | |
675 | f941aa25 | ths | case 16: |
676 | f941aa25 | ths | { |
677 | f941aa25 | ths | int i;
|
678 | f941aa25 | ths | uint16_t *p = buf; |
679 | f941aa25 | ths | int shift = info->nchannels - 1; |
680 | f941aa25 | ths | short s = INT16_MAX;
|
681 | f941aa25 | ths | |
682 | f941aa25 | ths | if (info->swap_endianness) {
|
683 | f941aa25 | ths | s = bswap16 (s); |
684 | f941aa25 | ths | } |
685 | f941aa25 | ths | |
686 | f941aa25 | ths | for (i = 0; i < len << shift; i++) { |
687 | f941aa25 | ths | p[i] = s; |
688 | f941aa25 | ths | } |
689 | 1d14ffa9 | bellard | } |
690 | f941aa25 | ths | break;
|
691 | f941aa25 | ths | |
692 | f941aa25 | ths | case 32: |
693 | f941aa25 | ths | { |
694 | f941aa25 | ths | int i;
|
695 | f941aa25 | ths | uint32_t *p = buf; |
696 | f941aa25 | ths | int shift = info->nchannels - 1; |
697 | f941aa25 | ths | int32_t s = INT32_MAX; |
698 | f941aa25 | ths | |
699 | f941aa25 | ths | if (info->swap_endianness) {
|
700 | f941aa25 | ths | s = bswap32 (s); |
701 | f941aa25 | ths | } |
702 | 1d14ffa9 | bellard | |
703 | f941aa25 | ths | for (i = 0; i < len << shift; i++) { |
704 | f941aa25 | ths | p[i] = s; |
705 | f941aa25 | ths | } |
706 | 1d14ffa9 | bellard | } |
707 | f941aa25 | ths | break;
|
708 | f941aa25 | ths | |
709 | f941aa25 | ths | default:
|
710 | f941aa25 | ths | AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n", |
711 | f941aa25 | ths | info->bits); |
712 | f941aa25 | ths | break;
|
713 | 1d14ffa9 | bellard | } |
714 | 85571bc7 | bellard | } |
715 | 85571bc7 | bellard | } |
716 | 85571bc7 | bellard | |
717 | 1d14ffa9 | bellard | /*
|
718 | 8ead62cf | bellard | * Capture
|
719 | 8ead62cf | bellard | */
|
720 | 8ead62cf | bellard | static void noop_conv (st_sample_t *dst, const void *src, |
721 | 8ead62cf | bellard | int samples, volume_t *vol)
|
722 | 8ead62cf | bellard | { |
723 | 8ead62cf | bellard | (void) src;
|
724 | 8ead62cf | bellard | (void) dst;
|
725 | 8ead62cf | bellard | (void) samples;
|
726 | 8ead62cf | bellard | (void) vol;
|
727 | 8ead62cf | bellard | } |
728 | 8ead62cf | bellard | |
729 | 8ead62cf | bellard | static CaptureVoiceOut *audio_pcm_capture_find_specific (
|
730 | 8ead62cf | bellard | AudioState *s, |
731 | d929eba5 | bellard | audsettings_t *as |
732 | 8ead62cf | bellard | ) |
733 | 8ead62cf | bellard | { |
734 | 8ead62cf | bellard | CaptureVoiceOut *cap; |
735 | 8ead62cf | bellard | |
736 | 8ead62cf | bellard | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
737 | d929eba5 | bellard | if (audio_pcm_info_eq (&cap->hw.info, as)) {
|
738 | 8ead62cf | bellard | return cap;
|
739 | 8ead62cf | bellard | } |
740 | 8ead62cf | bellard | } |
741 | 8ead62cf | bellard | return NULL; |
742 | 8ead62cf | bellard | } |
743 | 8ead62cf | bellard | |
744 | ec36b695 | bellard | static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd) |
745 | 8ead62cf | bellard | { |
746 | ec36b695 | bellard | struct capture_callback *cb;
|
747 | ec36b695 | bellard | |
748 | ec36b695 | bellard | #ifdef DEBUG_CAPTURE
|
749 | ec36b695 | bellard | dolog ("notification %d sent\n", cmd);
|
750 | ec36b695 | bellard | #endif
|
751 | ec36b695 | bellard | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
752 | ec36b695 | bellard | cb->ops.notify (cb->opaque, cmd); |
753 | ec36b695 | bellard | } |
754 | ec36b695 | bellard | } |
755 | 8ead62cf | bellard | |
756 | ec36b695 | bellard | static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled) |
757 | ec36b695 | bellard | { |
758 | ec36b695 | bellard | if (cap->hw.enabled != enabled) {
|
759 | ec36b695 | bellard | audcnotification_e cmd; |
760 | 8ead62cf | bellard | cap->hw.enabled = enabled; |
761 | ec36b695 | bellard | cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE; |
762 | ec36b695 | bellard | audio_notify_capture (cap, cmd); |
763 | 8ead62cf | bellard | } |
764 | 8ead62cf | bellard | } |
765 | 8ead62cf | bellard | |
766 | 8ead62cf | bellard | static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap) |
767 | 8ead62cf | bellard | { |
768 | 8ead62cf | bellard | HWVoiceOut *hw = &cap->hw; |
769 | 8ead62cf | bellard | SWVoiceOut *sw; |
770 | 8ead62cf | bellard | int enabled = 0; |
771 | 8ead62cf | bellard | |
772 | ec36b695 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
773 | 8ead62cf | bellard | if (sw->active) {
|
774 | 8ead62cf | bellard | enabled = 1;
|
775 | 8ead62cf | bellard | break;
|
776 | 8ead62cf | bellard | } |
777 | 8ead62cf | bellard | } |
778 | ec36b695 | bellard | audio_capture_maybe_changed (cap, enabled); |
779 | 8ead62cf | bellard | } |
780 | 8ead62cf | bellard | |
781 | 8ead62cf | bellard | static void audio_detach_capture (HWVoiceOut *hw) |
782 | 8ead62cf | bellard | { |
783 | ec36b695 | bellard | SWVoiceCap *sc = hw->cap_head.lh_first; |
784 | ec36b695 | bellard | |
785 | ec36b695 | bellard | while (sc) {
|
786 | ec36b695 | bellard | SWVoiceCap *sc1 = sc->entries.le_next; |
787 | ec36b695 | bellard | SWVoiceOut *sw = &sc->sw; |
788 | ec36b695 | bellard | CaptureVoiceOut *cap = sc->cap; |
789 | ec36b695 | bellard | int was_active = sw->active;
|
790 | 8ead62cf | bellard | |
791 | 8ead62cf | bellard | if (sw->rate) {
|
792 | 8ead62cf | bellard | st_rate_stop (sw->rate); |
793 | 8ead62cf | bellard | sw->rate = NULL;
|
794 | 8ead62cf | bellard | } |
795 | 8ead62cf | bellard | |
796 | 8ead62cf | bellard | LIST_REMOVE (sw, entries); |
797 | ec36b695 | bellard | LIST_REMOVE (sc, entries); |
798 | ec36b695 | bellard | qemu_free (sc); |
799 | ec36b695 | bellard | if (was_active) {
|
800 | ec36b695 | bellard | /* We have removed soft voice from the capture:
|
801 | ec36b695 | bellard | this might have changed the overall status of the capture
|
802 | ec36b695 | bellard | since this might have been the only active voice */
|
803 | ec36b695 | bellard | audio_recalc_and_notify_capture (cap); |
804 | ec36b695 | bellard | } |
805 | ec36b695 | bellard | sc = sc1; |
806 | 8ead62cf | bellard | } |
807 | 8ead62cf | bellard | } |
808 | 8ead62cf | bellard | |
809 | 8ead62cf | bellard | static int audio_attach_capture (AudioState *s, HWVoiceOut *hw) |
810 | 8ead62cf | bellard | { |
811 | 8ead62cf | bellard | CaptureVoiceOut *cap; |
812 | 8ead62cf | bellard | |
813 | 8ead62cf | bellard | audio_detach_capture (hw); |
814 | 8ead62cf | bellard | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
815 | ec36b695 | bellard | SWVoiceCap *sc; |
816 | 8ead62cf | bellard | SWVoiceOut *sw; |
817 | ec36b695 | bellard | HWVoiceOut *hw_cap = &cap->hw; |
818 | 8ead62cf | bellard | |
819 | ec36b695 | bellard | sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc)); |
820 | ec36b695 | bellard | if (!sc) {
|
821 | 8ead62cf | bellard | dolog ("Could not allocate soft capture voice (%zu bytes)\n",
|
822 | ec36b695 | bellard | sizeof (*sc));
|
823 | 8ead62cf | bellard | return -1; |
824 | 8ead62cf | bellard | } |
825 | 8ead62cf | bellard | |
826 | ec36b695 | bellard | sc->cap = cap; |
827 | ec36b695 | bellard | sw = &sc->sw; |
828 | 8ead62cf | bellard | sw->hw = hw_cap; |
829 | ec36b695 | bellard | sw->info = hw->info; |
830 | 8ead62cf | bellard | sw->empty = 1;
|
831 | 8ead62cf | bellard | sw->active = hw->enabled; |
832 | 8ead62cf | bellard | sw->conv = noop_conv; |
833 | 8ead62cf | bellard | sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
|
834 | 8ead62cf | bellard | sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq); |
835 | 8ead62cf | bellard | if (!sw->rate) {
|
836 | 8ead62cf | bellard | dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
|
837 | 8ead62cf | bellard | qemu_free (sw); |
838 | 8ead62cf | bellard | return -1; |
839 | 8ead62cf | bellard | } |
840 | 8ead62cf | bellard | LIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries); |
841 | ec36b695 | bellard | LIST_INSERT_HEAD (&hw->cap_head, sc, entries); |
842 | ec36b695 | bellard | #ifdef DEBUG_CAPTURE
|
843 | ec36b695 | bellard | asprintf (&sw->name, "for %p %d,%d,%d",
|
844 | ec36b695 | bellard | hw, sw->info.freq, sw->info.bits, sw->info.nchannels); |
845 | ec36b695 | bellard | dolog ("Added %s active = %d\n", sw->name, sw->active);
|
846 | ec36b695 | bellard | #endif
|
847 | 8ead62cf | bellard | if (sw->active) {
|
848 | ec36b695 | bellard | audio_capture_maybe_changed (cap, 1);
|
849 | 8ead62cf | bellard | } |
850 | 8ead62cf | bellard | } |
851 | 8ead62cf | bellard | return 0; |
852 | 8ead62cf | bellard | } |
853 | 8ead62cf | bellard | |
854 | 8ead62cf | bellard | /*
|
855 | 1d14ffa9 | bellard | * Hard voice (capture)
|
856 | 1d14ffa9 | bellard | */
|
857 | c0fe3827 | bellard | static int audio_pcm_hw_find_min_in (HWVoiceIn *hw) |
858 | 85571bc7 | bellard | { |
859 | 1d14ffa9 | bellard | SWVoiceIn *sw; |
860 | 1d14ffa9 | bellard | int m = hw->total_samples_captured;
|
861 | 1d14ffa9 | bellard | |
862 | 1d14ffa9 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
863 | 1d14ffa9 | bellard | if (sw->active) {
|
864 | 1d14ffa9 | bellard | m = audio_MIN (m, sw->total_hw_samples_acquired); |
865 | 1d14ffa9 | bellard | } |
866 | 85571bc7 | bellard | } |
867 | 1d14ffa9 | bellard | return m;
|
868 | 85571bc7 | bellard | } |
869 | 85571bc7 | bellard | |
870 | 1d14ffa9 | bellard | int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
|
871 | 85571bc7 | bellard | { |
872 | 1d14ffa9 | bellard | int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
|
873 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
874 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
875 | 1d14ffa9 | bellard | return 0; |
876 | 85571bc7 | bellard | } |
877 | 1d14ffa9 | bellard | return live;
|
878 | 85571bc7 | bellard | } |
879 | 85571bc7 | bellard | |
880 | 1d14ffa9 | bellard | /*
|
881 | 1d14ffa9 | bellard | * Soft voice (capture)
|
882 | 1d14ffa9 | bellard | */
|
883 | 1d14ffa9 | bellard | static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw) |
884 | 1d14ffa9 | bellard | { |
885 | 1d14ffa9 | bellard | HWVoiceIn *hw = sw->hw; |
886 | 1d14ffa9 | bellard | int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
|
887 | 1d14ffa9 | bellard | int rpos;
|
888 | 1d14ffa9 | bellard | |
889 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
890 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
891 | 1d14ffa9 | bellard | return 0; |
892 | 1d14ffa9 | bellard | } |
893 | 1d14ffa9 | bellard | |
894 | 1d14ffa9 | bellard | rpos = hw->wpos - live; |
895 | 1d14ffa9 | bellard | if (rpos >= 0) { |
896 | 1d14ffa9 | bellard | return rpos;
|
897 | 85571bc7 | bellard | } |
898 | 85571bc7 | bellard | else {
|
899 | 1d14ffa9 | bellard | return hw->samples + rpos;
|
900 | 85571bc7 | bellard | } |
901 | 85571bc7 | bellard | } |
902 | 85571bc7 | bellard | |
903 | 1d14ffa9 | bellard | int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size) |
904 | 85571bc7 | bellard | { |
905 | 1d14ffa9 | bellard | HWVoiceIn *hw = sw->hw; |
906 | 1d14ffa9 | bellard | int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0; |
907 | 571ec3d6 | bellard | st_sample_t *src, *dst = sw->buf; |
908 | 1d14ffa9 | bellard | |
909 | 1d14ffa9 | bellard | rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples; |
910 | 1d14ffa9 | bellard | |
911 | 1d14ffa9 | bellard | live = hw->total_samples_captured - sw->total_hw_samples_acquired; |
912 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
913 | 1d14ffa9 | bellard | dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
|
914 | 1d14ffa9 | bellard | return 0; |
915 | 1d14ffa9 | bellard | } |
916 | 1d14ffa9 | bellard | |
917 | 1d14ffa9 | bellard | samples = size >> sw->info.shift; |
918 | 1d14ffa9 | bellard | if (!live) {
|
919 | 1d14ffa9 | bellard | return 0; |
920 | 1d14ffa9 | bellard | } |
921 | 85571bc7 | bellard | |
922 | 1d14ffa9 | bellard | swlim = (live * sw->ratio) >> 32;
|
923 | 1d14ffa9 | bellard | swlim = audio_MIN (swlim, samples); |
924 | 85571bc7 | bellard | |
925 | 1d14ffa9 | bellard | while (swlim) {
|
926 | 1d14ffa9 | bellard | src = hw->conv_buf + rpos; |
927 | 1d14ffa9 | bellard | isamp = hw->wpos - rpos; |
928 | 1d14ffa9 | bellard | /* XXX: <= ? */
|
929 | 1d14ffa9 | bellard | if (isamp <= 0) { |
930 | 1d14ffa9 | bellard | isamp = hw->samples - rpos; |
931 | 1d14ffa9 | bellard | } |
932 | 85571bc7 | bellard | |
933 | 1d14ffa9 | bellard | if (!isamp) {
|
934 | 1d14ffa9 | bellard | break;
|
935 | 1d14ffa9 | bellard | } |
936 | 1d14ffa9 | bellard | osamp = swlim; |
937 | 85571bc7 | bellard | |
938 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, osamp < 0)) { |
939 | 1d14ffa9 | bellard | dolog ("osamp=%d\n", osamp);
|
940 | c0fe3827 | bellard | return 0; |
941 | 1d14ffa9 | bellard | } |
942 | 85571bc7 | bellard | |
943 | 1d14ffa9 | bellard | st_rate_flow (sw->rate, src, dst, &isamp, &osamp); |
944 | 1d14ffa9 | bellard | swlim -= osamp; |
945 | 1d14ffa9 | bellard | rpos = (rpos + isamp) % hw->samples; |
946 | 1d14ffa9 | bellard | dst += osamp; |
947 | 1d14ffa9 | bellard | ret += osamp; |
948 | 1d14ffa9 | bellard | total += isamp; |
949 | 1d14ffa9 | bellard | } |
950 | 85571bc7 | bellard | |
951 | 571ec3d6 | bellard | sw->clip (buf, sw->buf, ret); |
952 | 1d14ffa9 | bellard | sw->total_hw_samples_acquired += total; |
953 | 1d14ffa9 | bellard | return ret << sw->info.shift;
|
954 | 85571bc7 | bellard | } |
955 | 85571bc7 | bellard | |
956 | 1d14ffa9 | bellard | /*
|
957 | 1d14ffa9 | bellard | * Hard voice (playback)
|
958 | 1d14ffa9 | bellard | */
|
959 | c0fe3827 | bellard | static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep) |
960 | 1d14ffa9 | bellard | { |
961 | c0fe3827 | bellard | SWVoiceOut *sw; |
962 | c0fe3827 | bellard | int m = INT_MAX;
|
963 | c0fe3827 | bellard | int nb_live = 0; |
964 | 85571bc7 | bellard | |
965 | c0fe3827 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
966 | c0fe3827 | bellard | if (sw->active || !sw->empty) {
|
967 | c0fe3827 | bellard | m = audio_MIN (m, sw->total_hw_samples_mixed); |
968 | c0fe3827 | bellard | nb_live += 1;
|
969 | c0fe3827 | bellard | } |
970 | 85571bc7 | bellard | } |
971 | c0fe3827 | bellard | |
972 | c0fe3827 | bellard | *nb_livep = nb_live; |
973 | c0fe3827 | bellard | return m;
|
974 | 1d14ffa9 | bellard | } |
975 | 85571bc7 | bellard | |
976 | 1d14ffa9 | bellard | int audio_pcm_hw_get_live_out2 (HWVoiceOut *hw, int *nb_live) |
977 | 1d14ffa9 | bellard | { |
978 | 1d14ffa9 | bellard | int smin;
|
979 | 85571bc7 | bellard | |
980 | 1d14ffa9 | bellard | smin = audio_pcm_hw_find_min_out (hw, nb_live); |
981 | 1d14ffa9 | bellard | |
982 | 1d14ffa9 | bellard | if (!*nb_live) {
|
983 | 1d14ffa9 | bellard | return 0; |
984 | 85571bc7 | bellard | } |
985 | 85571bc7 | bellard | else {
|
986 | 1d14ffa9 | bellard | int live = smin;
|
987 | 1d14ffa9 | bellard | |
988 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
989 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
990 | 1d14ffa9 | bellard | return 0; |
991 | 85571bc7 | bellard | } |
992 | 1d14ffa9 | bellard | return live;
|
993 | 85571bc7 | bellard | } |
994 | 1d14ffa9 | bellard | } |
995 | 1d14ffa9 | bellard | |
996 | 1d14ffa9 | bellard | int audio_pcm_hw_get_live_out (HWVoiceOut *hw)
|
997 | 1d14ffa9 | bellard | { |
998 | 1d14ffa9 | bellard | int nb_live;
|
999 | 1d14ffa9 | bellard | int live;
|
1000 | 85571bc7 | bellard | |
1001 | 1d14ffa9 | bellard | live = audio_pcm_hw_get_live_out2 (hw, &nb_live); |
1002 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
1003 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
1004 | 1d14ffa9 | bellard | return 0; |
1005 | 85571bc7 | bellard | } |
1006 | 1d14ffa9 | bellard | return live;
|
1007 | 85571bc7 | bellard | } |
1008 | 85571bc7 | bellard | |
1009 | 1d14ffa9 | bellard | /*
|
1010 | 1d14ffa9 | bellard | * Soft voice (playback)
|
1011 | 1d14ffa9 | bellard | */
|
1012 | 1d14ffa9 | bellard | int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size) |
1013 | 85571bc7 | bellard | { |
1014 | 1d14ffa9 | bellard | int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
|
1015 | 1d14ffa9 | bellard | int ret = 0, pos = 0, total = 0; |
1016 | 85571bc7 | bellard | |
1017 | 1d14ffa9 | bellard | if (!sw) {
|
1018 | 1d14ffa9 | bellard | return size;
|
1019 | 1d14ffa9 | bellard | } |
1020 | 85571bc7 | bellard | |
1021 | 1d14ffa9 | bellard | hwsamples = sw->hw->samples; |
1022 | 85571bc7 | bellard | |
1023 | 1d14ffa9 | bellard | live = sw->total_hw_samples_mixed; |
1024 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){ |
1025 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hwsamples);
|
1026 | 1d14ffa9 | bellard | return 0; |
1027 | 1d14ffa9 | bellard | } |
1028 | 85571bc7 | bellard | |
1029 | 1d14ffa9 | bellard | if (live == hwsamples) {
|
1030 | ec36b695 | bellard | #ifdef DEBUG_OUT
|
1031 | ec36b695 | bellard | dolog ("%s is full %d\n", sw->name, live);
|
1032 | ec36b695 | bellard | #endif
|
1033 | 1d14ffa9 | bellard | return 0; |
1034 | 1d14ffa9 | bellard | } |
1035 | 85571bc7 | bellard | |
1036 | 1d14ffa9 | bellard | wpos = (sw->hw->rpos + live) % hwsamples; |
1037 | 1d14ffa9 | bellard | samples = size >> sw->info.shift; |
1038 | 85571bc7 | bellard | |
1039 | 1d14ffa9 | bellard | dead = hwsamples - live; |
1040 | 1d14ffa9 | bellard | swlim = ((int64_t) dead << 32) / sw->ratio;
|
1041 | 1d14ffa9 | bellard | swlim = audio_MIN (swlim, samples); |
1042 | 1d14ffa9 | bellard | if (swlim) {
|
1043 | 1d14ffa9 | bellard | sw->conv (sw->buf, buf, swlim, &sw->vol); |
1044 | 1d14ffa9 | bellard | } |
1045 | 1d14ffa9 | bellard | |
1046 | 1d14ffa9 | bellard | while (swlim) {
|
1047 | 1d14ffa9 | bellard | dead = hwsamples - live; |
1048 | 1d14ffa9 | bellard | left = hwsamples - wpos; |
1049 | 1d14ffa9 | bellard | blck = audio_MIN (dead, left); |
1050 | 1d14ffa9 | bellard | if (!blck) {
|
1051 | 1d14ffa9 | bellard | break;
|
1052 | 1d14ffa9 | bellard | } |
1053 | 1d14ffa9 | bellard | isamp = swlim; |
1054 | 1d14ffa9 | bellard | osamp = blck; |
1055 | 1d14ffa9 | bellard | st_rate_flow_mix ( |
1056 | 1d14ffa9 | bellard | sw->rate, |
1057 | 1d14ffa9 | bellard | sw->buf + pos, |
1058 | 1d14ffa9 | bellard | sw->hw->mix_buf + wpos, |
1059 | 1d14ffa9 | bellard | &isamp, |
1060 | 1d14ffa9 | bellard | &osamp |
1061 | 1d14ffa9 | bellard | ); |
1062 | 1d14ffa9 | bellard | ret += isamp; |
1063 | 1d14ffa9 | bellard | swlim -= isamp; |
1064 | 1d14ffa9 | bellard | pos += isamp; |
1065 | 1d14ffa9 | bellard | live += osamp; |
1066 | 1d14ffa9 | bellard | wpos = (wpos + osamp) % hwsamples; |
1067 | 1d14ffa9 | bellard | total += osamp; |
1068 | 1d14ffa9 | bellard | } |
1069 | 1d14ffa9 | bellard | |
1070 | 1d14ffa9 | bellard | sw->total_hw_samples_mixed += total; |
1071 | 1d14ffa9 | bellard | sw->empty = sw->total_hw_samples_mixed == 0;
|
1072 | 1d14ffa9 | bellard | |
1073 | 1d14ffa9 | bellard | #ifdef DEBUG_OUT
|
1074 | 1d14ffa9 | bellard | dolog ( |
1075 | c0fe3827 | bellard | "%s: write size %d ret %d total sw %d\n",
|
1076 | c0fe3827 | bellard | SW_NAME (sw), |
1077 | 1d14ffa9 | bellard | size >> sw->info.shift, |
1078 | 1d14ffa9 | bellard | ret, |
1079 | c0fe3827 | bellard | sw->total_hw_samples_mixed |
1080 | 1d14ffa9 | bellard | ); |
1081 | 1d14ffa9 | bellard | #endif
|
1082 | 1d14ffa9 | bellard | |
1083 | 1d14ffa9 | bellard | return ret << sw->info.shift;
|
1084 | 85571bc7 | bellard | } |
1085 | 85571bc7 | bellard | |
1086 | 1d14ffa9 | bellard | #ifdef DEBUG_AUDIO
|
1087 | 1d14ffa9 | bellard | static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info) |
1088 | 85571bc7 | bellard | { |
1089 | 1d14ffa9 | bellard | dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
|
1090 | 1d14ffa9 | bellard | cap, info->bits, info->sign, info->freq, info->nchannels); |
1091 | 85571bc7 | bellard | } |
1092 | 1d14ffa9 | bellard | #endif
|
1093 | 85571bc7 | bellard | |
1094 | 1d14ffa9 | bellard | #define DAC
|
1095 | 1d14ffa9 | bellard | #include "audio_template.h" |
1096 | 1d14ffa9 | bellard | #undef DAC
|
1097 | 1d14ffa9 | bellard | #include "audio_template.h" |
1098 | 1d14ffa9 | bellard | |
1099 | 1d14ffa9 | bellard | int AUD_write (SWVoiceOut *sw, void *buf, int size) |
1100 | 85571bc7 | bellard | { |
1101 | 1d14ffa9 | bellard | int bytes;
|
1102 | 85571bc7 | bellard | |
1103 | 1d14ffa9 | bellard | if (!sw) {
|
1104 | 1d14ffa9 | bellard | /* XXX: Consider options */
|
1105 | 1d14ffa9 | bellard | return size;
|
1106 | 1d14ffa9 | bellard | } |
1107 | 85571bc7 | bellard | |
1108 | 1d14ffa9 | bellard | if (!sw->hw->enabled) {
|
1109 | c0fe3827 | bellard | dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
|
1110 | 85571bc7 | bellard | return 0; |
1111 | 85571bc7 | bellard | } |
1112 | 85571bc7 | bellard | |
1113 | 1d14ffa9 | bellard | bytes = sw->hw->pcm_ops->write (sw, buf, size); |
1114 | 1d14ffa9 | bellard | return bytes;
|
1115 | 1d14ffa9 | bellard | } |
1116 | 1d14ffa9 | bellard | |
1117 | 1d14ffa9 | bellard | int AUD_read (SWVoiceIn *sw, void *buf, int size) |
1118 | 1d14ffa9 | bellard | { |
1119 | 1d14ffa9 | bellard | int bytes;
|
1120 | 85571bc7 | bellard | |
1121 | 1d14ffa9 | bellard | if (!sw) {
|
1122 | 1d14ffa9 | bellard | /* XXX: Consider options */
|
1123 | 1d14ffa9 | bellard | return size;
|
1124 | 85571bc7 | bellard | } |
1125 | 1d14ffa9 | bellard | |
1126 | 1d14ffa9 | bellard | if (!sw->hw->enabled) {
|
1127 | c0fe3827 | bellard | dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
|
1128 | 1d14ffa9 | bellard | return 0; |
1129 | 85571bc7 | bellard | } |
1130 | 1d14ffa9 | bellard | |
1131 | 1d14ffa9 | bellard | bytes = sw->hw->pcm_ops->read (sw, buf, size); |
1132 | 1d14ffa9 | bellard | return bytes;
|
1133 | 85571bc7 | bellard | } |
1134 | 85571bc7 | bellard | |
1135 | 1d14ffa9 | bellard | int AUD_get_buffer_size_out (SWVoiceOut *sw)
|
1136 | 85571bc7 | bellard | { |
1137 | c0fe3827 | bellard | return sw->hw->samples << sw->hw->info.shift;
|
1138 | 1d14ffa9 | bellard | } |
1139 | 1d14ffa9 | bellard | |
1140 | 1d14ffa9 | bellard | void AUD_set_active_out (SWVoiceOut *sw, int on) |
1141 | 1d14ffa9 | bellard | { |
1142 | 1d14ffa9 | bellard | HWVoiceOut *hw; |
1143 | 85571bc7 | bellard | |
1144 | 1d14ffa9 | bellard | if (!sw) {
|
1145 | 85571bc7 | bellard | return;
|
1146 | 1d14ffa9 | bellard | } |
1147 | 85571bc7 | bellard | |
1148 | 85571bc7 | bellard | hw = sw->hw; |
1149 | 1d14ffa9 | bellard | if (sw->active != on) {
|
1150 | 1d14ffa9 | bellard | SWVoiceOut *temp_sw; |
1151 | ec36b695 | bellard | SWVoiceCap *sc; |
1152 | 1d14ffa9 | bellard | |
1153 | 1d14ffa9 | bellard | if (on) {
|
1154 | 1d14ffa9 | bellard | hw->pending_disable = 0;
|
1155 | 1d14ffa9 | bellard | if (!hw->enabled) {
|
1156 | 1d14ffa9 | bellard | hw->enabled = 1;
|
1157 | 1d14ffa9 | bellard | hw->pcm_ops->ctl_out (hw, VOICE_ENABLE); |
1158 | 1d14ffa9 | bellard | } |
1159 | 1d14ffa9 | bellard | } |
1160 | 1d14ffa9 | bellard | else {
|
1161 | 1d14ffa9 | bellard | if (hw->enabled) {
|
1162 | 1d14ffa9 | bellard | int nb_active = 0; |
1163 | 1d14ffa9 | bellard | |
1164 | 1d14ffa9 | bellard | for (temp_sw = hw->sw_head.lh_first; temp_sw;
|
1165 | 1d14ffa9 | bellard | temp_sw = temp_sw->entries.le_next) { |
1166 | 1d14ffa9 | bellard | nb_active += temp_sw->active != 0;
|
1167 | 1d14ffa9 | bellard | } |
1168 | 1d14ffa9 | bellard | |
1169 | 1d14ffa9 | bellard | hw->pending_disable = nb_active == 1;
|
1170 | 1d14ffa9 | bellard | } |
1171 | 85571bc7 | bellard | } |
1172 | ec36b695 | bellard | |
1173 | ec36b695 | bellard | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
1174 | ec36b695 | bellard | sc->sw.active = hw->enabled; |
1175 | 8ead62cf | bellard | if (hw->enabled) {
|
1176 | ec36b695 | bellard | audio_capture_maybe_changed (sc->cap, 1);
|
1177 | 8ead62cf | bellard | } |
1178 | 8ead62cf | bellard | } |
1179 | 1d14ffa9 | bellard | sw->active = on; |
1180 | 1d14ffa9 | bellard | } |
1181 | 1d14ffa9 | bellard | } |
1182 | 1d14ffa9 | bellard | |
1183 | 1d14ffa9 | bellard | void AUD_set_active_in (SWVoiceIn *sw, int on) |
1184 | 1d14ffa9 | bellard | { |
1185 | 1d14ffa9 | bellard | HWVoiceIn *hw; |
1186 | 1d14ffa9 | bellard | |
1187 | 1d14ffa9 | bellard | if (!sw) {
|
1188 | 1d14ffa9 | bellard | return;
|
1189 | 85571bc7 | bellard | } |
1190 | 85571bc7 | bellard | |
1191 | 1d14ffa9 | bellard | hw = sw->hw; |
1192 | 85571bc7 | bellard | if (sw->active != on) {
|
1193 | 1d14ffa9 | bellard | SWVoiceIn *temp_sw; |
1194 | 1d14ffa9 | bellard | |
1195 | 85571bc7 | bellard | if (on) {
|
1196 | 85571bc7 | bellard | if (!hw->enabled) {
|
1197 | 85571bc7 | bellard | hw->enabled = 1;
|
1198 | 1d14ffa9 | bellard | hw->pcm_ops->ctl_in (hw, VOICE_ENABLE); |
1199 | 85571bc7 | bellard | } |
1200 | 1d14ffa9 | bellard | sw->total_hw_samples_acquired = hw->total_samples_captured; |
1201 | 85571bc7 | bellard | } |
1202 | 85571bc7 | bellard | else {
|
1203 | 1d14ffa9 | bellard | if (hw->enabled) {
|
1204 | 85571bc7 | bellard | int nb_active = 0; |
1205 | 1d14ffa9 | bellard | |
1206 | 1d14ffa9 | bellard | for (temp_sw = hw->sw_head.lh_first; temp_sw;
|
1207 | 1d14ffa9 | bellard | temp_sw = temp_sw->entries.le_next) { |
1208 | 1d14ffa9 | bellard | nb_active += temp_sw->active != 0;
|
1209 | 85571bc7 | bellard | } |
1210 | 85571bc7 | bellard | |
1211 | 85571bc7 | bellard | if (nb_active == 1) { |
1212 | 1d14ffa9 | bellard | hw->enabled = 0;
|
1213 | 1d14ffa9 | bellard | hw->pcm_ops->ctl_in (hw, VOICE_DISABLE); |
1214 | 85571bc7 | bellard | } |
1215 | 85571bc7 | bellard | } |
1216 | 85571bc7 | bellard | } |
1217 | 85571bc7 | bellard | sw->active = on; |
1218 | 85571bc7 | bellard | } |
1219 | 85571bc7 | bellard | } |
1220 | 85571bc7 | bellard | |
1221 | 1d14ffa9 | bellard | static int audio_get_avail (SWVoiceIn *sw) |
1222 | 1d14ffa9 | bellard | { |
1223 | 1d14ffa9 | bellard | int live;
|
1224 | 1d14ffa9 | bellard | |
1225 | 1d14ffa9 | bellard | if (!sw) {
|
1226 | 1d14ffa9 | bellard | return 0; |
1227 | 1d14ffa9 | bellard | } |
1228 | 1d14ffa9 | bellard | |
1229 | 1d14ffa9 | bellard | live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; |
1230 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) { |
1231 | 1d14ffa9 | bellard | dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
1232 | 1d14ffa9 | bellard | return 0; |
1233 | 1d14ffa9 | bellard | } |
1234 | 1d14ffa9 | bellard | |
1235 | 1d14ffa9 | bellard | ldebug ( |
1236 | 26a76461 | bellard | "%s: get_avail live %d ret %" PRId64 "\n", |
1237 | c0fe3827 | bellard | SW_NAME (sw), |
1238 | 1d14ffa9 | bellard | live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
|
1239 | 1d14ffa9 | bellard | ); |
1240 | 1d14ffa9 | bellard | |
1241 | 1d14ffa9 | bellard | return (((int64_t) live << 32) / sw->ratio) << sw->info.shift; |
1242 | 1d14ffa9 | bellard | } |
1243 | 1d14ffa9 | bellard | |
1244 | 1d14ffa9 | bellard | static int audio_get_free (SWVoiceOut *sw) |
1245 | 1d14ffa9 | bellard | { |
1246 | 1d14ffa9 | bellard | int live, dead;
|
1247 | 1d14ffa9 | bellard | |
1248 | 1d14ffa9 | bellard | if (!sw) {
|
1249 | 1d14ffa9 | bellard | return 0; |
1250 | 1d14ffa9 | bellard | } |
1251 | 1d14ffa9 | bellard | |
1252 | 1d14ffa9 | bellard | live = sw->total_hw_samples_mixed; |
1253 | 1d14ffa9 | bellard | |
1254 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) { |
1255 | 1d14ffa9 | bellard | dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
|
1256 | c0fe3827 | bellard | return 0; |
1257 | 1d14ffa9 | bellard | } |
1258 | 1d14ffa9 | bellard | |
1259 | 1d14ffa9 | bellard | dead = sw->hw->samples - live; |
1260 | 1d14ffa9 | bellard | |
1261 | 1d14ffa9 | bellard | #ifdef DEBUG_OUT
|
1262 | 26a76461 | bellard | dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n", |
1263 | c0fe3827 | bellard | SW_NAME (sw), |
1264 | 1d14ffa9 | bellard | live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
|
1265 | 85571bc7 | bellard | #endif
|
1266 | 1d14ffa9 | bellard | |
1267 | 1d14ffa9 | bellard | return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift; |
1268 | 1d14ffa9 | bellard | } |
1269 | 1d14ffa9 | bellard | |
1270 | 8ead62cf | bellard | static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples) |
1271 | 8ead62cf | bellard | { |
1272 | 8ead62cf | bellard | int n;
|
1273 | 8ead62cf | bellard | |
1274 | 8ead62cf | bellard | if (hw->enabled) {
|
1275 | ec36b695 | bellard | SWVoiceCap *sc; |
1276 | 8ead62cf | bellard | |
1277 | ec36b695 | bellard | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
1278 | ec36b695 | bellard | SWVoiceOut *sw = &sc->sw; |
1279 | 8ead62cf | bellard | int rpos2 = rpos;
|
1280 | 8ead62cf | bellard | |
1281 | 8ead62cf | bellard | n = samples; |
1282 | 8ead62cf | bellard | while (n) {
|
1283 | 8ead62cf | bellard | int till_end_of_hw = hw->samples - rpos2;
|
1284 | 8ead62cf | bellard | int to_write = audio_MIN (till_end_of_hw, n);
|
1285 | 8ead62cf | bellard | int bytes = to_write << hw->info.shift;
|
1286 | 8ead62cf | bellard | int written;
|
1287 | 8ead62cf | bellard | |
1288 | 8ead62cf | bellard | sw->buf = hw->mix_buf + rpos2; |
1289 | 8ead62cf | bellard | written = audio_pcm_sw_write (sw, NULL, bytes);
|
1290 | 8ead62cf | bellard | if (written - bytes) {
|
1291 | ec36b695 | bellard | dolog ("Could not mix %d bytes into a capture "
|
1292 | ec36b695 | bellard | "buffer, mixed %d\n",
|
1293 | ec36b695 | bellard | bytes, written); |
1294 | 8ead62cf | bellard | break;
|
1295 | 8ead62cf | bellard | } |
1296 | 8ead62cf | bellard | n -= to_write; |
1297 | 8ead62cf | bellard | rpos2 = (rpos2 + to_write) % hw->samples; |
1298 | 8ead62cf | bellard | } |
1299 | 8ead62cf | bellard | } |
1300 | 8ead62cf | bellard | } |
1301 | 8ead62cf | bellard | |
1302 | 8ead62cf | bellard | n = audio_MIN (samples, hw->samples - rpos); |
1303 | 8ead62cf | bellard | mixeng_clear (hw->mix_buf + rpos, n); |
1304 | 8ead62cf | bellard | mixeng_clear (hw->mix_buf, samples - n); |
1305 | 8ead62cf | bellard | } |
1306 | 8ead62cf | bellard | |
1307 | c0fe3827 | bellard | static void audio_run_out (AudioState *s) |
1308 | 1d14ffa9 | bellard | { |
1309 | 1d14ffa9 | bellard | HWVoiceOut *hw = NULL;
|
1310 | 1d14ffa9 | bellard | SWVoiceOut *sw; |
1311 | 1d14ffa9 | bellard | |
1312 | c0fe3827 | bellard | while ((hw = audio_pcm_hw_find_any_enabled_out (s, hw))) {
|
1313 | 1d14ffa9 | bellard | int played;
|
1314 | 8ead62cf | bellard | int live, free, nb_live, cleanup_required, prev_rpos;
|
1315 | 1d14ffa9 | bellard | |
1316 | 1d14ffa9 | bellard | live = audio_pcm_hw_get_live_out2 (hw, &nb_live); |
1317 | 1d14ffa9 | bellard | if (!nb_live) {
|
1318 | 1d14ffa9 | bellard | live = 0;
|
1319 | 1d14ffa9 | bellard | } |
1320 | c0fe3827 | bellard | |
1321 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) { |
1322 | 1d14ffa9 | bellard | dolog ("live=%d hw->samples=%d\n", live, hw->samples);
|
1323 | c0fe3827 | bellard | continue;
|
1324 | 1d14ffa9 | bellard | } |
1325 | 1d14ffa9 | bellard | |
1326 | 1d14ffa9 | bellard | if (hw->pending_disable && !nb_live) {
|
1327 | ec36b695 | bellard | SWVoiceCap *sc; |
1328 | 1d14ffa9 | bellard | #ifdef DEBUG_OUT
|
1329 | 1d14ffa9 | bellard | dolog ("Disabling voice\n");
|
1330 | 85571bc7 | bellard | #endif
|
1331 | 1d14ffa9 | bellard | hw->enabled = 0;
|
1332 | 1d14ffa9 | bellard | hw->pending_disable = 0;
|
1333 | 1d14ffa9 | bellard | hw->pcm_ops->ctl_out (hw, VOICE_DISABLE); |
1334 | ec36b695 | bellard | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
1335 | ec36b695 | bellard | sc->sw.active = 0;
|
1336 | ec36b695 | bellard | audio_recalc_and_notify_capture (sc->cap); |
1337 | 8ead62cf | bellard | } |
1338 | 1d14ffa9 | bellard | continue;
|
1339 | 1d14ffa9 | bellard | } |
1340 | 1d14ffa9 | bellard | |
1341 | 1d14ffa9 | bellard | if (!live) {
|
1342 | 1d14ffa9 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
1343 | 1d14ffa9 | bellard | if (sw->active) {
|
1344 | 1d14ffa9 | bellard | free = audio_get_free (sw); |
1345 | 1d14ffa9 | bellard | if (free > 0) { |
1346 | 1d14ffa9 | bellard | sw->callback.fn (sw->callback.opaque, free); |
1347 | 1d14ffa9 | bellard | } |
1348 | 1d14ffa9 | bellard | } |
1349 | 1d14ffa9 | bellard | } |
1350 | 1d14ffa9 | bellard | continue;
|
1351 | 1d14ffa9 | bellard | } |
1352 | 1d14ffa9 | bellard | |
1353 | 8ead62cf | bellard | prev_rpos = hw->rpos; |
1354 | 1d14ffa9 | bellard | played = hw->pcm_ops->run_out (hw); |
1355 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
|
1356 | 1d14ffa9 | bellard | dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
|
1357 | 1d14ffa9 | bellard | hw->rpos, hw->samples, played); |
1358 | 1d14ffa9 | bellard | hw->rpos = 0;
|
1359 | 1d14ffa9 | bellard | } |
1360 | 1d14ffa9 | bellard | |
1361 | 1d14ffa9 | bellard | #ifdef DEBUG_OUT
|
1362 | c0fe3827 | bellard | dolog ("played=%d\n", played);
|
1363 | 85571bc7 | bellard | #endif
|
1364 | 1d14ffa9 | bellard | |
1365 | 1d14ffa9 | bellard | if (played) {
|
1366 | 1d14ffa9 | bellard | hw->ts_helper += played; |
1367 | 8ead62cf | bellard | audio_capture_mix_and_clear (hw, prev_rpos, played); |
1368 | 1d14ffa9 | bellard | } |
1369 | 1d14ffa9 | bellard | |
1370 | c0fe3827 | bellard | cleanup_required = 0;
|
1371 | 1d14ffa9 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
1372 | 1d14ffa9 | bellard | if (!sw->active && sw->empty) {
|
1373 | 1d14ffa9 | bellard | continue;
|
1374 | 1d14ffa9 | bellard | } |
1375 | 1d14ffa9 | bellard | |
1376 | 1d14ffa9 | bellard | if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
|
1377 | 1d14ffa9 | bellard | dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
|
1378 | 1d14ffa9 | bellard | played, sw->total_hw_samples_mixed); |
1379 | 1d14ffa9 | bellard | played = sw->total_hw_samples_mixed; |
1380 | 1d14ffa9 | bellard | } |
1381 | 1d14ffa9 | bellard | |
1382 | 1d14ffa9 | bellard | sw->total_hw_samples_mixed -= played; |
1383 | 1d14ffa9 | bellard | |
1384 | 1d14ffa9 | bellard | if (!sw->total_hw_samples_mixed) {
|
1385 | 1d14ffa9 | bellard | sw->empty = 1;
|
1386 | c0fe3827 | bellard | cleanup_required |= !sw->active && !sw->callback.fn; |
1387 | 1d14ffa9 | bellard | } |
1388 | 1d14ffa9 | bellard | |
1389 | 1d14ffa9 | bellard | if (sw->active) {
|
1390 | 1d14ffa9 | bellard | free = audio_get_free (sw); |
1391 | 1d14ffa9 | bellard | if (free > 0) { |
1392 | 1d14ffa9 | bellard | sw->callback.fn (sw->callback.opaque, free); |
1393 | 1d14ffa9 | bellard | } |
1394 | 1d14ffa9 | bellard | } |
1395 | 1d14ffa9 | bellard | } |
1396 | c0fe3827 | bellard | |
1397 | c0fe3827 | bellard | if (cleanup_required) {
|
1398 | ec36b695 | bellard | SWVoiceOut *sw1; |
1399 | ec36b695 | bellard | |
1400 | ec36b695 | bellard | sw = hw->sw_head.lh_first; |
1401 | ec36b695 | bellard | while (sw) {
|
1402 | ec36b695 | bellard | sw1 = sw->entries.le_next; |
1403 | c0fe3827 | bellard | if (!sw->active && !sw->callback.fn) {
|
1404 | c0fe3827 | bellard | #ifdef DEBUG_PLIVE
|
1405 | c0fe3827 | bellard | dolog ("Finishing with old voice\n");
|
1406 | c0fe3827 | bellard | #endif
|
1407 | c0fe3827 | bellard | audio_close_out (s, sw); |
1408 | c0fe3827 | bellard | } |
1409 | ec36b695 | bellard | sw = sw1; |
1410 | c0fe3827 | bellard | } |
1411 | c0fe3827 | bellard | } |
1412 | 1d14ffa9 | bellard | } |
1413 | 1d14ffa9 | bellard | } |
1414 | 1d14ffa9 | bellard | |
1415 | c0fe3827 | bellard | static void audio_run_in (AudioState *s) |
1416 | 1d14ffa9 | bellard | { |
1417 | 1d14ffa9 | bellard | HWVoiceIn *hw = NULL;
|
1418 | 1d14ffa9 | bellard | |
1419 | c0fe3827 | bellard | while ((hw = audio_pcm_hw_find_any_enabled_in (s, hw))) {
|
1420 | 1d14ffa9 | bellard | SWVoiceIn *sw; |
1421 | 1d14ffa9 | bellard | int captured, min;
|
1422 | 1d14ffa9 | bellard | |
1423 | 1d14ffa9 | bellard | captured = hw->pcm_ops->run_in (hw); |
1424 | 1d14ffa9 | bellard | |
1425 | 1d14ffa9 | bellard | min = audio_pcm_hw_find_min_in (hw); |
1426 | 1d14ffa9 | bellard | hw->total_samples_captured += captured - min; |
1427 | 1d14ffa9 | bellard | hw->ts_helper += captured; |
1428 | 1d14ffa9 | bellard | |
1429 | 1d14ffa9 | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
1430 | 1d14ffa9 | bellard | sw->total_hw_samples_acquired -= min; |
1431 | 1d14ffa9 | bellard | |
1432 | 1d14ffa9 | bellard | if (sw->active) {
|
1433 | 1d14ffa9 | bellard | int avail;
|
1434 | 1d14ffa9 | bellard | |
1435 | 1d14ffa9 | bellard | avail = audio_get_avail (sw); |
1436 | 1d14ffa9 | bellard | if (avail > 0) { |
1437 | 1d14ffa9 | bellard | sw->callback.fn (sw->callback.opaque, avail); |
1438 | 1d14ffa9 | bellard | } |
1439 | 1d14ffa9 | bellard | } |
1440 | 1d14ffa9 | bellard | } |
1441 | 1d14ffa9 | bellard | } |
1442 | 1d14ffa9 | bellard | } |
1443 | 1d14ffa9 | bellard | |
1444 | 8ead62cf | bellard | static void audio_run_capture (AudioState *s) |
1445 | 8ead62cf | bellard | { |
1446 | 8ead62cf | bellard | CaptureVoiceOut *cap; |
1447 | 8ead62cf | bellard | |
1448 | 8ead62cf | bellard | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
|
1449 | 8ead62cf | bellard | int live, rpos, captured;
|
1450 | 8ead62cf | bellard | HWVoiceOut *hw = &cap->hw; |
1451 | 8ead62cf | bellard | SWVoiceOut *sw; |
1452 | 8ead62cf | bellard | |
1453 | 8ead62cf | bellard | captured = live = audio_pcm_hw_get_live_out (hw); |
1454 | 8ead62cf | bellard | rpos = hw->rpos; |
1455 | 8ead62cf | bellard | while (live) {
|
1456 | 8ead62cf | bellard | int left = hw->samples - rpos;
|
1457 | 8ead62cf | bellard | int to_capture = audio_MIN (live, left);
|
1458 | 8ead62cf | bellard | st_sample_t *src; |
1459 | 8ead62cf | bellard | struct capture_callback *cb;
|
1460 | 8ead62cf | bellard | |
1461 | 8ead62cf | bellard | src = hw->mix_buf + rpos; |
1462 | 8ead62cf | bellard | hw->clip (cap->buf, src, to_capture); |
1463 | 8ead62cf | bellard | mixeng_clear (src, to_capture); |
1464 | 8ead62cf | bellard | |
1465 | 8ead62cf | bellard | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
1466 | 8ead62cf | bellard | cb->ops.capture (cb->opaque, cap->buf, |
1467 | 8ead62cf | bellard | to_capture << hw->info.shift); |
1468 | 8ead62cf | bellard | } |
1469 | 8ead62cf | bellard | rpos = (rpos + to_capture) % hw->samples; |
1470 | 8ead62cf | bellard | live -= to_capture; |
1471 | 8ead62cf | bellard | } |
1472 | 8ead62cf | bellard | hw->rpos = rpos; |
1473 | 8ead62cf | bellard | |
1474 | 8ead62cf | bellard | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
|
1475 | 8ead62cf | bellard | if (!sw->active && sw->empty) {
|
1476 | 8ead62cf | bellard | continue;
|
1477 | 8ead62cf | bellard | } |
1478 | 8ead62cf | bellard | |
1479 | 8ead62cf | bellard | if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
|
1480 | 8ead62cf | bellard | dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
|
1481 | 8ead62cf | bellard | captured, sw->total_hw_samples_mixed); |
1482 | 8ead62cf | bellard | captured = sw->total_hw_samples_mixed; |
1483 | 8ead62cf | bellard | } |
1484 | 8ead62cf | bellard | |
1485 | 8ead62cf | bellard | sw->total_hw_samples_mixed -= captured; |
1486 | 8ead62cf | bellard | sw->empty = sw->total_hw_samples_mixed == 0;
|
1487 | 8ead62cf | bellard | } |
1488 | 8ead62cf | bellard | } |
1489 | 8ead62cf | bellard | } |
1490 | 8ead62cf | bellard | |
1491 | 571ec3d6 | bellard | static void audio_timer (void *opaque) |
1492 | 571ec3d6 | bellard | { |
1493 | 571ec3d6 | bellard | AudioState *s = opaque; |
1494 | 571ec3d6 | bellard | |
1495 | 571ec3d6 | bellard | audio_run_out (s); |
1496 | 571ec3d6 | bellard | audio_run_in (s); |
1497 | 8ead62cf | bellard | audio_run_capture (s); |
1498 | 571ec3d6 | bellard | |
1499 | 571ec3d6 | bellard | qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks); |
1500 | 571ec3d6 | bellard | } |
1501 | 571ec3d6 | bellard | |
1502 | 1d14ffa9 | bellard | static struct audio_option audio_options[] = { |
1503 | 1d14ffa9 | bellard | /* DAC */
|
1504 | c0fe3827 | bellard | {"DAC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_out.enabled,
|
1505 | 1d14ffa9 | bellard | "Use fixed settings for host DAC", NULL, 0}, |
1506 | 1d14ffa9 | bellard | |
1507 | c0fe3827 | bellard | {"DAC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_out.settings.freq,
|
1508 | 1d14ffa9 | bellard | "Frequency for fixed host DAC", NULL, 0}, |
1509 | 1d14ffa9 | bellard | |
1510 | c0fe3827 | bellard | {"DAC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_out.settings.fmt,
|
1511 | 1d14ffa9 | bellard | "Format for fixed host DAC", NULL, 0}, |
1512 | 1d14ffa9 | bellard | |
1513 | c0fe3827 | bellard | {"DAC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_out.settings.nchannels,
|
1514 | 1d14ffa9 | bellard | "Number of channels for fixed DAC (1 - mono, 2 - stereo)", NULL, 0}, |
1515 | 1d14ffa9 | bellard | |
1516 | c0fe3827 | bellard | {"DAC_VOICES", AUD_OPT_INT, &conf.fixed_out.nb_voices,
|
1517 | 1d14ffa9 | bellard | "Number of voices for DAC", NULL, 0}, |
1518 | 1d14ffa9 | bellard | |
1519 | 1d14ffa9 | bellard | /* ADC */
|
1520 | c0fe3827 | bellard | {"ADC_FIXED_SETTINGS", AUD_OPT_BOOL, &conf.fixed_in.enabled,
|
1521 | 1d14ffa9 | bellard | "Use fixed settings for host ADC", NULL, 0}, |
1522 | 1d14ffa9 | bellard | |
1523 | c0fe3827 | bellard | {"ADC_FIXED_FREQ", AUD_OPT_INT, &conf.fixed_in.settings.freq,
|
1524 | c0fe3827 | bellard | "Frequency for fixed host ADC", NULL, 0}, |
1525 | 1d14ffa9 | bellard | |
1526 | c0fe3827 | bellard | {"ADC_FIXED_FMT", AUD_OPT_FMT, &conf.fixed_in.settings.fmt,
|
1527 | c0fe3827 | bellard | "Format for fixed host ADC", NULL, 0}, |
1528 | 1d14ffa9 | bellard | |
1529 | c0fe3827 | bellard | {"ADC_FIXED_CHANNELS", AUD_OPT_INT, &conf.fixed_in.settings.nchannels,
|
1530 | 1d14ffa9 | bellard | "Number of channels for fixed ADC (1 - mono, 2 - stereo)", NULL, 0}, |
1531 | 1d14ffa9 | bellard | |
1532 | c0fe3827 | bellard | {"ADC_VOICES", AUD_OPT_INT, &conf.fixed_in.nb_voices,
|
1533 | 1d14ffa9 | bellard | "Number of voices for ADC", NULL, 0}, |
1534 | 1d14ffa9 | bellard | |
1535 | 1d14ffa9 | bellard | /* Misc */
|
1536 | c0fe3827 | bellard | {"TIMER_PERIOD", AUD_OPT_INT, &conf.period.hz,
|
1537 | c0fe3827 | bellard | "Timer period in HZ (0 - use lowest possible)", NULL, 0}, |
1538 | 1d14ffa9 | bellard | |
1539 | c0fe3827 | bellard | {"PLIVE", AUD_OPT_BOOL, &conf.plive,
|
1540 | 1d14ffa9 | bellard | "(undocumented)", NULL, 0}, |
1541 | 1d14ffa9 | bellard | |
1542 | 541e0844 | bellard | {"LOG_TO_MONITOR", AUD_OPT_BOOL, &conf.log_to_monitor,
|
1543 | b60aee00 | aurel32 | "print logging messages to monitor instead of stderr", NULL, 0}, |
1544 | 541e0844 | bellard | |
1545 | 1d14ffa9 | bellard | {NULL, 0, NULL, NULL, NULL, 0} |
1546 | 85571bc7 | bellard | }; |
1547 | 85571bc7 | bellard | |
1548 | 571ec3d6 | bellard | static void audio_pp_nb_voices (const char *typ, int nb) |
1549 | 571ec3d6 | bellard | { |
1550 | 571ec3d6 | bellard | switch (nb) {
|
1551 | 571ec3d6 | bellard | case 0: |
1552 | 571ec3d6 | bellard | printf ("Does not support %s\n", typ);
|
1553 | 571ec3d6 | bellard | break;
|
1554 | 571ec3d6 | bellard | case 1: |
1555 | 571ec3d6 | bellard | printf ("One %s voice\n", typ);
|
1556 | 571ec3d6 | bellard | break;
|
1557 | 571ec3d6 | bellard | case INT_MAX:
|
1558 | 571ec3d6 | bellard | printf ("Theoretically supports many %s voices\n", typ);
|
1559 | 571ec3d6 | bellard | break;
|
1560 | 571ec3d6 | bellard | default:
|
1561 | 571ec3d6 | bellard | printf ("Theoretically supports upto %d %s voices\n", nb, typ);
|
1562 | 571ec3d6 | bellard | break;
|
1563 | 571ec3d6 | bellard | } |
1564 | 571ec3d6 | bellard | |
1565 | 571ec3d6 | bellard | } |
1566 | 571ec3d6 | bellard | |
1567 | 1d14ffa9 | bellard | void AUD_help (void) |
1568 | 1d14ffa9 | bellard | { |
1569 | 1d14ffa9 | bellard | size_t i; |
1570 | 1d14ffa9 | bellard | |
1571 | 1d14ffa9 | bellard | audio_process_options ("AUDIO", audio_options);
|
1572 | 1d14ffa9 | bellard | for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) { |
1573 | 1d14ffa9 | bellard | struct audio_driver *d = drvtab[i];
|
1574 | 1d14ffa9 | bellard | if (d->options) {
|
1575 | 1d14ffa9 | bellard | audio_process_options (d->name, d->options); |
1576 | 1d14ffa9 | bellard | } |
1577 | 1d14ffa9 | bellard | } |
1578 | 1d14ffa9 | bellard | |
1579 | 1d14ffa9 | bellard | printf ("Audio options:\n");
|
1580 | 1d14ffa9 | bellard | audio_print_options ("AUDIO", audio_options);
|
1581 | 1d14ffa9 | bellard | printf ("\n");
|
1582 | 1d14ffa9 | bellard | |
1583 | 1d14ffa9 | bellard | printf ("Available drivers:\n");
|
1584 | 1d14ffa9 | bellard | |
1585 | 1d14ffa9 | bellard | for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) { |
1586 | 1d14ffa9 | bellard | struct audio_driver *d = drvtab[i];
|
1587 | 1d14ffa9 | bellard | |
1588 | 1d14ffa9 | bellard | printf ("Name: %s\n", d->name);
|
1589 | 1d14ffa9 | bellard | printf ("Description: %s\n", d->descr);
|
1590 | 1d14ffa9 | bellard | |
1591 | 571ec3d6 | bellard | audio_pp_nb_voices ("playback", d->max_voices_out);
|
1592 | 571ec3d6 | bellard | audio_pp_nb_voices ("capture", d->max_voices_in);
|
1593 | 1d14ffa9 | bellard | |
1594 | 1d14ffa9 | bellard | if (d->options) {
|
1595 | 1d14ffa9 | bellard | printf ("Options:\n");
|
1596 | 1d14ffa9 | bellard | audio_print_options (d->name, d->options); |
1597 | 1d14ffa9 | bellard | } |
1598 | 1d14ffa9 | bellard | else {
|
1599 | 1d14ffa9 | bellard | printf ("No options\n");
|
1600 | 1d14ffa9 | bellard | } |
1601 | 1d14ffa9 | bellard | printf ("\n");
|
1602 | 1d14ffa9 | bellard | } |
1603 | 1d14ffa9 | bellard | |
1604 | 1d14ffa9 | bellard | printf ( |
1605 | 1d14ffa9 | bellard | "Options are settable through environment variables.\n"
|
1606 | 1d14ffa9 | bellard | "Example:\n"
|
1607 | 1d14ffa9 | bellard | #ifdef _WIN32
|
1608 | 1d14ffa9 | bellard | " set QEMU_AUDIO_DRV=wav\n"
|
1609 | 571ec3d6 | bellard | " set QEMU_WAV_PATH=c:\\tune.wav\n"
|
1610 | 1d14ffa9 | bellard | #else
|
1611 | 1d14ffa9 | bellard | " export QEMU_AUDIO_DRV=wav\n"
|
1612 | 1d14ffa9 | bellard | " export QEMU_WAV_PATH=$HOME/tune.wav\n"
|
1613 | 1d14ffa9 | bellard | "(for csh replace export with setenv in the above)\n"
|
1614 | 1d14ffa9 | bellard | #endif
|
1615 | 1d14ffa9 | bellard | " qemu ...\n\n"
|
1616 | 1d14ffa9 | bellard | ); |
1617 | 1d14ffa9 | bellard | } |
1618 | 1d14ffa9 | bellard | |
1619 | c0fe3827 | bellard | static int audio_driver_init (AudioState *s, struct audio_driver *drv) |
1620 | 85571bc7 | bellard | { |
1621 | 1d14ffa9 | bellard | if (drv->options) {
|
1622 | 1d14ffa9 | bellard | audio_process_options (drv->name, drv->options); |
1623 | 1d14ffa9 | bellard | } |
1624 | c0fe3827 | bellard | s->drv_opaque = drv->init (); |
1625 | 1d14ffa9 | bellard | |
1626 | c0fe3827 | bellard | if (s->drv_opaque) {
|
1627 | 571ec3d6 | bellard | audio_init_nb_voices_out (s, drv); |
1628 | 571ec3d6 | bellard | audio_init_nb_voices_in (s, drv); |
1629 | c0fe3827 | bellard | s->drv = drv; |
1630 | 1d14ffa9 | bellard | return 0; |
1631 | 85571bc7 | bellard | } |
1632 | 85571bc7 | bellard | else {
|
1633 | 1d14ffa9 | bellard | dolog ("Could not init `%s' audio driver\n", drv->name);
|
1634 | 1d14ffa9 | bellard | return -1; |
1635 | 85571bc7 | bellard | } |
1636 | 85571bc7 | bellard | } |
1637 | 85571bc7 | bellard | |
1638 | 541e0844 | bellard | static void audio_vm_change_state_handler (void *opaque, int running) |
1639 | 85571bc7 | bellard | { |
1640 | c0fe3827 | bellard | AudioState *s = opaque; |
1641 | 1d14ffa9 | bellard | HWVoiceOut *hwo = NULL;
|
1642 | 1d14ffa9 | bellard | HWVoiceIn *hwi = NULL;
|
1643 | 541e0844 | bellard | int op = running ? VOICE_ENABLE : VOICE_DISABLE;
|
1644 | 1d14ffa9 | bellard | |
1645 | 541e0844 | bellard | while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
|
1646 | 541e0844 | bellard | hwo->pcm_ops->ctl_out (hwo, op); |
1647 | 1d14ffa9 | bellard | } |
1648 | 85571bc7 | bellard | |
1649 | 541e0844 | bellard | while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
|
1650 | 541e0844 | bellard | hwi->pcm_ops->ctl_in (hwi, op); |
1651 | 85571bc7 | bellard | } |
1652 | 85571bc7 | bellard | } |
1653 | 85571bc7 | bellard | |
1654 | 85571bc7 | bellard | static void audio_atexit (void) |
1655 | 85571bc7 | bellard | { |
1656 | c0fe3827 | bellard | AudioState *s = &glob_audio_state; |
1657 | 1d14ffa9 | bellard | HWVoiceOut *hwo = NULL;
|
1658 | 1d14ffa9 | bellard | HWVoiceIn *hwi = NULL;
|
1659 | 1d14ffa9 | bellard | |
1660 | 571ec3d6 | bellard | while ((hwo = audio_pcm_hw_find_any_enabled_out (s, hwo))) {
|
1661 | ec36b695 | bellard | SWVoiceCap *sc; |
1662 | 8ead62cf | bellard | |
1663 | 571ec3d6 | bellard | hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE); |
1664 | 1d14ffa9 | bellard | hwo->pcm_ops->fini_out (hwo); |
1665 | 8ead62cf | bellard | |
1666 | ec36b695 | bellard | for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
1667 | ec36b695 | bellard | CaptureVoiceOut *cap = sc->cap; |
1668 | ec36b695 | bellard | struct capture_callback *cb;
|
1669 | ec36b695 | bellard | |
1670 | ec36b695 | bellard | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
1671 | ec36b695 | bellard | cb->ops.destroy (cb->opaque); |
1672 | ec36b695 | bellard | } |
1673 | 8ead62cf | bellard | } |
1674 | 1d14ffa9 | bellard | } |
1675 | 85571bc7 | bellard | |
1676 | 571ec3d6 | bellard | while ((hwi = audio_pcm_hw_find_any_enabled_in (s, hwi))) {
|
1677 | 571ec3d6 | bellard | hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE); |
1678 | 1d14ffa9 | bellard | hwi->pcm_ops->fini_in (hwi); |
1679 | 85571bc7 | bellard | } |
1680 | c0fe3827 | bellard | |
1681 | c0fe3827 | bellard | if (s->drv) {
|
1682 | c0fe3827 | bellard | s->drv->fini (s->drv_opaque); |
1683 | c0fe3827 | bellard | } |
1684 | 85571bc7 | bellard | } |
1685 | 85571bc7 | bellard | |
1686 | 85571bc7 | bellard | static void audio_save (QEMUFile *f, void *opaque) |
1687 | 85571bc7 | bellard | { |
1688 | 1d14ffa9 | bellard | (void) f;
|
1689 | 1d14ffa9 | bellard | (void) opaque;
|
1690 | 85571bc7 | bellard | } |
1691 | 85571bc7 | bellard | |
1692 | 85571bc7 | bellard | static int audio_load (QEMUFile *f, void *opaque, int version_id) |
1693 | 85571bc7 | bellard | { |
1694 | 1d14ffa9 | bellard | (void) f;
|
1695 | 1d14ffa9 | bellard | (void) opaque;
|
1696 | 1d14ffa9 | bellard | |
1697 | 1d14ffa9 | bellard | if (version_id != 1) { |
1698 | 85571bc7 | bellard | return -EINVAL;
|
1699 | 1d14ffa9 | bellard | } |
1700 | 85571bc7 | bellard | |
1701 | 85571bc7 | bellard | return 0; |
1702 | 85571bc7 | bellard | } |
1703 | 85571bc7 | bellard | |
1704 | c0fe3827 | bellard | void AUD_register_card (AudioState *s, const char *name, QEMUSoundCard *card) |
1705 | c0fe3827 | bellard | { |
1706 | c0fe3827 | bellard | card->audio = s; |
1707 | c0fe3827 | bellard | card->name = qemu_strdup (name); |
1708 | c0fe3827 | bellard | memset (&card->entries, 0, sizeof (card->entries)); |
1709 | c0fe3827 | bellard | LIST_INSERT_HEAD (&s->card_head, card, entries); |
1710 | c0fe3827 | bellard | } |
1711 | c0fe3827 | bellard | |
1712 | c0fe3827 | bellard | void AUD_remove_card (QEMUSoundCard *card)
|
1713 | c0fe3827 | bellard | { |
1714 | c0fe3827 | bellard | LIST_REMOVE (card, entries); |
1715 | c0fe3827 | bellard | card->audio = NULL;
|
1716 | c0fe3827 | bellard | qemu_free (card->name); |
1717 | c0fe3827 | bellard | } |
1718 | c0fe3827 | bellard | |
1719 | c0fe3827 | bellard | AudioState *AUD_init (void)
|
1720 | 85571bc7 | bellard | { |
1721 | 1d14ffa9 | bellard | size_t i; |
1722 | 85571bc7 | bellard | int done = 0; |
1723 | 85571bc7 | bellard | const char *drvname; |
1724 | c0fe3827 | bellard | AudioState *s = &glob_audio_state; |
1725 | 1d14ffa9 | bellard | |
1726 | 571ec3d6 | bellard | LIST_INIT (&s->hw_head_out); |
1727 | 571ec3d6 | bellard | LIST_INIT (&s->hw_head_in); |
1728 | 8ead62cf | bellard | LIST_INIT (&s->cap_head); |
1729 | 571ec3d6 | bellard | atexit (audio_atexit); |
1730 | 571ec3d6 | bellard | |
1731 | 571ec3d6 | bellard | s->ts = qemu_new_timer (vm_clock, audio_timer, s); |
1732 | 571ec3d6 | bellard | if (!s->ts) {
|
1733 | 571ec3d6 | bellard | dolog ("Could not create audio timer\n");
|
1734 | 571ec3d6 | bellard | return NULL; |
1735 | 571ec3d6 | bellard | } |
1736 | 571ec3d6 | bellard | |
1737 | 1d14ffa9 | bellard | audio_process_options ("AUDIO", audio_options);
|
1738 | 1d14ffa9 | bellard | |
1739 | c0fe3827 | bellard | s->nb_hw_voices_out = conf.fixed_out.nb_voices; |
1740 | c0fe3827 | bellard | s->nb_hw_voices_in = conf.fixed_in.nb_voices; |
1741 | c0fe3827 | bellard | |
1742 | 1d14ffa9 | bellard | if (s->nb_hw_voices_out <= 0) { |
1743 | 571ec3d6 | bellard | dolog ("Bogus number of playback voices %d, setting to 1\n",
|
1744 | 1d14ffa9 | bellard | s->nb_hw_voices_out); |
1745 | 1d14ffa9 | bellard | s->nb_hw_voices_out = 1;
|
1746 | 1d14ffa9 | bellard | } |
1747 | 1d14ffa9 | bellard | |
1748 | 1d14ffa9 | bellard | if (s->nb_hw_voices_in <= 0) { |
1749 | 571ec3d6 | bellard | dolog ("Bogus number of capture voices %d, setting to 0\n",
|
1750 | 1d14ffa9 | bellard | s->nb_hw_voices_in); |
1751 | 571ec3d6 | bellard | s->nb_hw_voices_in = 0;
|
1752 | 1d14ffa9 | bellard | } |
1753 | 85571bc7 | bellard | |
1754 | 1d14ffa9 | bellard | { |
1755 | 1d14ffa9 | bellard | int def;
|
1756 | 1d14ffa9 | bellard | drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def); |
1757 | 1d14ffa9 | bellard | } |
1758 | 85571bc7 | bellard | |
1759 | 85571bc7 | bellard | if (drvname) {
|
1760 | 85571bc7 | bellard | int found = 0; |
1761 | 1d14ffa9 | bellard | |
1762 | 85571bc7 | bellard | for (i = 0; i < sizeof (drvtab) / sizeof (drvtab[0]); i++) { |
1763 | 85571bc7 | bellard | if (!strcmp (drvname, drvtab[i]->name)) {
|
1764 | c0fe3827 | bellard | done = !audio_driver_init (s, drvtab[i]); |
1765 | 85571bc7 | bellard | found = 1;
|
1766 | 85571bc7 | bellard | break;
|
1767 | 85571bc7 | bellard | } |
1768 | 85571bc7 | bellard | } |
1769 | 1d14ffa9 | bellard | |
1770 | 85571bc7 | bellard | if (!found) {
|
1771 | 85571bc7 | bellard | dolog ("Unknown audio driver `%s'\n", drvname);
|
1772 | 1d14ffa9 | bellard | dolog ("Run with -audio-help to list available drivers\n");
|
1773 | 85571bc7 | bellard | } |
1774 | 85571bc7 | bellard | } |
1775 | 85571bc7 | bellard | |
1776 | 85571bc7 | bellard | if (!done) {
|
1777 | 85571bc7 | bellard | for (i = 0; !done && i < sizeof (drvtab) / sizeof (drvtab[0]); i++) { |
1778 | 1d14ffa9 | bellard | if (drvtab[i]->can_be_default) {
|
1779 | c0fe3827 | bellard | done = !audio_driver_init (s, drvtab[i]); |
1780 | 1d14ffa9 | bellard | } |
1781 | 85571bc7 | bellard | } |
1782 | 85571bc7 | bellard | } |
1783 | 85571bc7 | bellard | |
1784 | 85571bc7 | bellard | if (!done) {
|
1785 | c0fe3827 | bellard | done = !audio_driver_init (s, &no_audio_driver); |
1786 | c0fe3827 | bellard | if (!done) {
|
1787 | c0fe3827 | bellard | dolog ("Could not initialize audio subsystem\n");
|
1788 | 1d14ffa9 | bellard | } |
1789 | 1d14ffa9 | bellard | else {
|
1790 | c0fe3827 | bellard | dolog ("warning: Using timer based audio emulation\n");
|
1791 | 1d14ffa9 | bellard | } |
1792 | 85571bc7 | bellard | } |
1793 | 1d14ffa9 | bellard | |
1794 | c0fe3827 | bellard | if (done) {
|
1795 | 571ec3d6 | bellard | VMChangeStateEntry *e; |
1796 | 571ec3d6 | bellard | |
1797 | c0fe3827 | bellard | if (conf.period.hz <= 0) { |
1798 | c0fe3827 | bellard | if (conf.period.hz < 0) { |
1799 | c0fe3827 | bellard | dolog ("warning: Timer period is negative - %d "
|
1800 | c0fe3827 | bellard | "treating as zero\n",
|
1801 | c0fe3827 | bellard | conf.period.hz); |
1802 | c0fe3827 | bellard | } |
1803 | c0fe3827 | bellard | conf.period.ticks = 1;
|
1804 | 1d14ffa9 | bellard | } |
1805 | c0fe3827 | bellard | else {
|
1806 | c0fe3827 | bellard | conf.period.ticks = ticks_per_sec / conf.period.hz; |
1807 | c0fe3827 | bellard | } |
1808 | c0fe3827 | bellard | |
1809 | 571ec3d6 | bellard | e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s); |
1810 | 571ec3d6 | bellard | if (!e) {
|
1811 | 571ec3d6 | bellard | dolog ("warning: Could not register change state handler\n"
|
1812 | 571ec3d6 | bellard | "(Audio can continue looping even after stopping the VM)\n");
|
1813 | 571ec3d6 | bellard | } |
1814 | 1d14ffa9 | bellard | } |
1815 | 1d14ffa9 | bellard | else {
|
1816 | c0fe3827 | bellard | qemu_del_timer (s->ts); |
1817 | c0fe3827 | bellard | return NULL; |
1818 | 1d14ffa9 | bellard | } |
1819 | 1d14ffa9 | bellard | |
1820 | c0fe3827 | bellard | LIST_INIT (&s->card_head); |
1821 | c0fe3827 | bellard | register_savevm ("audio", 0, 1, audio_save, audio_load, s); |
1822 | c0fe3827 | bellard | qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + conf.period.ticks); |
1823 | c0fe3827 | bellard | return s;
|
1824 | 85571bc7 | bellard | } |
1825 | 8ead62cf | bellard | |
1826 | ec36b695 | bellard | CaptureVoiceOut *AUD_add_capture ( |
1827 | 8ead62cf | bellard | AudioState *s, |
1828 | 8ead62cf | bellard | audsettings_t *as, |
1829 | 8ead62cf | bellard | struct audio_capture_ops *ops,
|
1830 | 8ead62cf | bellard | void *cb_opaque
|
1831 | 8ead62cf | bellard | ) |
1832 | 8ead62cf | bellard | { |
1833 | 8ead62cf | bellard | CaptureVoiceOut *cap; |
1834 | 8ead62cf | bellard | struct capture_callback *cb;
|
1835 | 8ead62cf | bellard | |
1836 | 8ead62cf | bellard | if (!s) {
|
1837 | 8ead62cf | bellard | /* XXX suppress */
|
1838 | 8ead62cf | bellard | s = &glob_audio_state; |
1839 | 8ead62cf | bellard | } |
1840 | 8ead62cf | bellard | |
1841 | ec36b695 | bellard | if (audio_validate_settings (as)) {
|
1842 | 8ead62cf | bellard | dolog ("Invalid settings were passed when trying to add capture\n");
|
1843 | 8ead62cf | bellard | audio_print_settings (as); |
1844 | ec36b695 | bellard | goto err0;
|
1845 | 8ead62cf | bellard | } |
1846 | 8ead62cf | bellard | |
1847 | 8ead62cf | bellard | cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb)); |
1848 | 8ead62cf | bellard | if (!cb) {
|
1849 | 8ead62cf | bellard | dolog ("Could not allocate capture callback information, size %zu\n",
|
1850 | 8ead62cf | bellard | sizeof (*cb));
|
1851 | 8ead62cf | bellard | goto err0;
|
1852 | 8ead62cf | bellard | } |
1853 | 8ead62cf | bellard | cb->ops = *ops; |
1854 | 8ead62cf | bellard | cb->opaque = cb_opaque; |
1855 | 8ead62cf | bellard | |
1856 | d929eba5 | bellard | cap = audio_pcm_capture_find_specific (s, as); |
1857 | 8ead62cf | bellard | if (cap) {
|
1858 | 8ead62cf | bellard | LIST_INSERT_HEAD (&cap->cb_head, cb, entries); |
1859 | ec36b695 | bellard | return cap;
|
1860 | 8ead62cf | bellard | } |
1861 | 8ead62cf | bellard | else {
|
1862 | 8ead62cf | bellard | HWVoiceOut *hw; |
1863 | 8ead62cf | bellard | CaptureVoiceOut *cap; |
1864 | 8ead62cf | bellard | |
1865 | 8ead62cf | bellard | cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap)); |
1866 | 8ead62cf | bellard | if (!cap) {
|
1867 | 8ead62cf | bellard | dolog ("Could not allocate capture voice, size %zu\n",
|
1868 | 8ead62cf | bellard | sizeof (*cap));
|
1869 | 8ead62cf | bellard | goto err1;
|
1870 | 8ead62cf | bellard | } |
1871 | 8ead62cf | bellard | |
1872 | 8ead62cf | bellard | hw = &cap->hw; |
1873 | 8ead62cf | bellard | LIST_INIT (&hw->sw_head); |
1874 | 8ead62cf | bellard | LIST_INIT (&cap->cb_head); |
1875 | 8ead62cf | bellard | |
1876 | 8ead62cf | bellard | /* XXX find a more elegant way */
|
1877 | 8ead62cf | bellard | hw->samples = 4096 * 4; |
1878 | 8ead62cf | bellard | hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples, |
1879 | 8ead62cf | bellard | sizeof (st_sample_t));
|
1880 | 8ead62cf | bellard | if (!hw->mix_buf) {
|
1881 | 8ead62cf | bellard | dolog ("Could not allocate capture mix buffer (%d samples)\n",
|
1882 | 8ead62cf | bellard | hw->samples); |
1883 | 8ead62cf | bellard | goto err2;
|
1884 | 8ead62cf | bellard | } |
1885 | 8ead62cf | bellard | |
1886 | d929eba5 | bellard | audio_pcm_init_info (&hw->info, as); |
1887 | 8ead62cf | bellard | |
1888 | 8ead62cf | bellard | cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
1889 | 8ead62cf | bellard | if (!cap->buf) {
|
1890 | 8ead62cf | bellard | dolog ("Could not allocate capture buffer "
|
1891 | 8ead62cf | bellard | "(%d samples, each %d bytes)\n",
|
1892 | 8ead62cf | bellard | hw->samples, 1 << hw->info.shift);
|
1893 | 8ead62cf | bellard | goto err3;
|
1894 | 8ead62cf | bellard | } |
1895 | 8ead62cf | bellard | |
1896 | 8ead62cf | bellard | hw->clip = mixeng_clip |
1897 | 8ead62cf | bellard | [hw->info.nchannels == 2]
|
1898 | 8ead62cf | bellard | [hw->info.sign] |
1899 | d929eba5 | bellard | [hw->info.swap_endianness] |
1900 | f941aa25 | ths | [audio_bits_to_index (hw->info.bits)]; |
1901 | 8ead62cf | bellard | |
1902 | 8ead62cf | bellard | LIST_INSERT_HEAD (&s->cap_head, cap, entries); |
1903 | 8ead62cf | bellard | LIST_INSERT_HEAD (&cap->cb_head, cb, entries); |
1904 | 8ead62cf | bellard | |
1905 | 8ead62cf | bellard | hw = NULL;
|
1906 | 8ead62cf | bellard | while ((hw = audio_pcm_hw_find_any_out (s, hw))) {
|
1907 | 8ead62cf | bellard | audio_attach_capture (s, hw); |
1908 | 8ead62cf | bellard | } |
1909 | ec36b695 | bellard | return cap;
|
1910 | 8ead62cf | bellard | |
1911 | 8ead62cf | bellard | err3:
|
1912 | 8ead62cf | bellard | qemu_free (cap->hw.mix_buf); |
1913 | 8ead62cf | bellard | err2:
|
1914 | 8ead62cf | bellard | qemu_free (cap); |
1915 | 8ead62cf | bellard | err1:
|
1916 | 8ead62cf | bellard | qemu_free (cb); |
1917 | 8ead62cf | bellard | err0:
|
1918 | ec36b695 | bellard | return NULL; |
1919 | ec36b695 | bellard | } |
1920 | ec36b695 | bellard | } |
1921 | ec36b695 | bellard | |
1922 | ec36b695 | bellard | void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque) |
1923 | ec36b695 | bellard | { |
1924 | ec36b695 | bellard | struct capture_callback *cb;
|
1925 | ec36b695 | bellard | |
1926 | ec36b695 | bellard | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
|
1927 | ec36b695 | bellard | if (cb->opaque == cb_opaque) {
|
1928 | ec36b695 | bellard | cb->ops.destroy (cb_opaque); |
1929 | ec36b695 | bellard | LIST_REMOVE (cb, entries); |
1930 | ec36b695 | bellard | qemu_free (cb); |
1931 | ec36b695 | bellard | |
1932 | ec36b695 | bellard | if (!cap->cb_head.lh_first) {
|
1933 | ec36b695 | bellard | SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1; |
1934 | a3c25997 | bellard | |
1935 | ec36b695 | bellard | while (sw) {
|
1936 | a3c25997 | bellard | SWVoiceCap *sc = (SWVoiceCap *) sw; |
1937 | ec36b695 | bellard | #ifdef DEBUG_CAPTURE
|
1938 | ec36b695 | bellard | dolog ("freeing %s\n", sw->name);
|
1939 | ec36b695 | bellard | #endif
|
1940 | a3c25997 | bellard | |
1941 | ec36b695 | bellard | sw1 = sw->entries.le_next; |
1942 | ec36b695 | bellard | if (sw->rate) {
|
1943 | ec36b695 | bellard | st_rate_stop (sw->rate); |
1944 | ec36b695 | bellard | sw->rate = NULL;
|
1945 | ec36b695 | bellard | } |
1946 | ec36b695 | bellard | LIST_REMOVE (sw, entries); |
1947 | a3c25997 | bellard | LIST_REMOVE (sc, entries); |
1948 | a3c25997 | bellard | qemu_free (sc); |
1949 | ec36b695 | bellard | sw = sw1; |
1950 | ec36b695 | bellard | } |
1951 | ec36b695 | bellard | LIST_REMOVE (cap, entries); |
1952 | ec36b695 | bellard | qemu_free (cap); |
1953 | ec36b695 | bellard | } |
1954 | ec36b695 | bellard | return;
|
1955 | ec36b695 | bellard | } |
1956 | 8ead62cf | bellard | } |
1957 | 8ead62cf | bellard | } |
1958 | 683efdcb | balrog | |
1959 | 683efdcb | balrog | void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol) |
1960 | 683efdcb | balrog | { |
1961 | 683efdcb | balrog | if (sw) {
|
1962 | 683efdcb | balrog | sw->vol.mute = mute; |
1963 | 683efdcb | balrog | sw->vol.l = nominal_volume.l * lvol / 255;
|
1964 | 683efdcb | balrog | sw->vol.r = nominal_volume.r * rvol / 255;
|
1965 | 683efdcb | balrog | } |
1966 | 683efdcb | balrog | } |
1967 | 683efdcb | balrog | |
1968 | 683efdcb | balrog | void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol) |
1969 | 683efdcb | balrog | { |
1970 | 683efdcb | balrog | if (sw) {
|
1971 | 683efdcb | balrog | sw->vol.mute = mute; |
1972 | 683efdcb | balrog | sw->vol.l = nominal_volume.l * lvol / 255;
|
1973 | 683efdcb | balrog | sw->vol.r = nominal_volume.r * rvol / 255;
|
1974 | 683efdcb | balrog | } |
1975 | 683efdcb | balrog | } |