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