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