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