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