2
0

audio.c 51 KB

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