audio.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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 "audio.h"
  26. #include "migration/vmstate.h"
  27. #include "monitor/monitor.h"
  28. #include "qemu/timer.h"
  29. #include "qapi/error.h"
  30. #include "qapi/clone-visitor.h"
  31. #include "qapi/qobject-input-visitor.h"
  32. #include "qapi/qapi-visit-audio.h"
  33. #include "qapi/qapi-commands-audio.h"
  34. #include "qobject/qdict.h"
  35. #include "qemu/cutils.h"
  36. #include "qemu/error-report.h"
  37. #include "qemu/log.h"
  38. #include "qemu/module.h"
  39. #include "qemu/help_option.h"
  40. #include "system/system.h"
  41. #include "system/replay.h"
  42. #include "system/runstate.h"
  43. #include "ui/qemu-spice.h"
  44. #include "trace.h"
  45. #define AUDIO_CAP "audio"
  46. #include "audio_int.h"
  47. /* #define DEBUG_LIVE */
  48. /* #define DEBUG_OUT */
  49. /* #define DEBUG_CAPTURE */
  50. /* #define DEBUG_POLL */
  51. #define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
  52. /* Order of CONFIG_AUDIO_DRIVERS is import.
  53. The 1st one is the one used by default, that is the reason
  54. that we generate the list.
  55. */
  56. const char *audio_prio_list[] = {
  57. "spice",
  58. CONFIG_AUDIO_DRIVERS
  59. "none",
  60. NULL
  61. };
  62. static QLIST_HEAD(, audio_driver) audio_drivers;
  63. static AudiodevListHead audiodevs =
  64. QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
  65. static AudiodevListHead default_audiodevs =
  66. QSIMPLEQ_HEAD_INITIALIZER(default_audiodevs);
  67. void audio_driver_register(audio_driver *drv)
  68. {
  69. QLIST_INSERT_HEAD(&audio_drivers, drv, next);
  70. }
  71. static audio_driver *audio_driver_lookup(const char *name)
  72. {
  73. struct audio_driver *d;
  74. Error *local_err = NULL;
  75. int rv;
  76. QLIST_FOREACH(d, &audio_drivers, next) {
  77. if (strcmp(name, d->name) == 0) {
  78. return d;
  79. }
  80. }
  81. rv = audio_module_load(name, &local_err);
  82. if (rv > 0) {
  83. QLIST_FOREACH(d, &audio_drivers, next) {
  84. if (strcmp(name, d->name) == 0) {
  85. return d;
  86. }
  87. }
  88. } else if (rv < 0) {
  89. error_report_err(local_err);
  90. }
  91. return NULL;
  92. }
  93. static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
  94. QTAILQ_HEAD_INITIALIZER(audio_states);
  95. static AudioState *default_audio_state;
  96. const struct mixeng_volume nominal_volume = {
  97. .mute = 0,
  98. #ifdef FLOAT_MIXENG
  99. .r = 1.0,
  100. .l = 1.0,
  101. #else
  102. .r = 1ULL << 32,
  103. .l = 1ULL << 32,
  104. #endif
  105. };
  106. int audio_bug (const char *funcname, int cond)
  107. {
  108. if (cond) {
  109. static int shown;
  110. AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
  111. if (!shown) {
  112. shown = 1;
  113. AUD_log (NULL, "Save all your work and restart without audio\n");
  114. AUD_log (NULL, "I am sorry\n");
  115. }
  116. AUD_log (NULL, "Context:\n");
  117. }
  118. return cond;
  119. }
  120. static inline int audio_bits_to_index (int bits)
  121. {
  122. switch (bits) {
  123. case 8:
  124. return 0;
  125. case 16:
  126. return 1;
  127. case 32:
  128. return 2;
  129. default:
  130. audio_bug ("bits_to_index", 1);
  131. AUD_log (NULL, "invalid bits %d\n", bits);
  132. return 0;
  133. }
  134. }
  135. void AUD_vlog (const char *cap, const char *fmt, va_list ap)
  136. {
  137. if (cap) {
  138. fprintf(stderr, "%s: ", cap);
  139. }
  140. vfprintf(stderr, fmt, ap);
  141. }
  142. void AUD_log (const char *cap, const char *fmt, ...)
  143. {
  144. va_list ap;
  145. va_start (ap, fmt);
  146. AUD_vlog (cap, fmt, ap);
  147. va_end (ap);
  148. }
  149. static void audio_print_settings (struct audsettings *as)
  150. {
  151. dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
  152. switch (as->fmt) {
  153. case AUDIO_FORMAT_S8:
  154. AUD_log (NULL, "S8");
  155. break;
  156. case AUDIO_FORMAT_U8:
  157. AUD_log (NULL, "U8");
  158. break;
  159. case AUDIO_FORMAT_S16:
  160. AUD_log (NULL, "S16");
  161. break;
  162. case AUDIO_FORMAT_U16:
  163. AUD_log (NULL, "U16");
  164. break;
  165. case AUDIO_FORMAT_S32:
  166. AUD_log (NULL, "S32");
  167. break;
  168. case AUDIO_FORMAT_U32:
  169. AUD_log (NULL, "U32");
  170. break;
  171. case AUDIO_FORMAT_F32:
  172. AUD_log (NULL, "F32");
  173. break;
  174. default:
  175. AUD_log (NULL, "invalid(%d)", as->fmt);
  176. break;
  177. }
  178. AUD_log (NULL, " endianness=");
  179. switch (as->endianness) {
  180. case 0:
  181. AUD_log (NULL, "little");
  182. break;
  183. case 1:
  184. AUD_log (NULL, "big");
  185. break;
  186. default:
  187. AUD_log (NULL, "invalid");
  188. break;
  189. }
  190. AUD_log (NULL, "\n");
  191. }
  192. static int audio_validate_settings (struct audsettings *as)
  193. {
  194. int invalid;
  195. invalid = as->nchannels < 1;
  196. invalid |= as->endianness != 0 && as->endianness != 1;
  197. switch (as->fmt) {
  198. case AUDIO_FORMAT_S8:
  199. case AUDIO_FORMAT_U8:
  200. case AUDIO_FORMAT_S16:
  201. case AUDIO_FORMAT_U16:
  202. case AUDIO_FORMAT_S32:
  203. case AUDIO_FORMAT_U32:
  204. case AUDIO_FORMAT_F32:
  205. break;
  206. default:
  207. invalid = 1;
  208. break;
  209. }
  210. invalid |= as->freq <= 0;
  211. return invalid ? -1 : 0;
  212. }
  213. static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
  214. {
  215. int bits = 8;
  216. bool is_signed = false, is_float = false;
  217. switch (as->fmt) {
  218. case AUDIO_FORMAT_S8:
  219. is_signed = true;
  220. /* fall through */
  221. case AUDIO_FORMAT_U8:
  222. break;
  223. case AUDIO_FORMAT_S16:
  224. is_signed = true;
  225. /* fall through */
  226. case AUDIO_FORMAT_U16:
  227. bits = 16;
  228. break;
  229. case AUDIO_FORMAT_F32:
  230. is_float = true;
  231. /* fall through */
  232. case AUDIO_FORMAT_S32:
  233. is_signed = true;
  234. /* fall through */
  235. case AUDIO_FORMAT_U32:
  236. bits = 32;
  237. break;
  238. default:
  239. abort();
  240. }
  241. return info->freq == as->freq
  242. && info->nchannels == as->nchannels
  243. && info->is_signed == is_signed
  244. && info->is_float == is_float
  245. && info->bits == bits
  246. && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
  247. }
  248. void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
  249. {
  250. int bits = 8, mul;
  251. bool is_signed = false, is_float = false;
  252. switch (as->fmt) {
  253. case AUDIO_FORMAT_S8:
  254. is_signed = true;
  255. /* fall through */
  256. case AUDIO_FORMAT_U8:
  257. mul = 1;
  258. break;
  259. case AUDIO_FORMAT_S16:
  260. is_signed = true;
  261. /* fall through */
  262. case AUDIO_FORMAT_U16:
  263. bits = 16;
  264. mul = 2;
  265. break;
  266. case AUDIO_FORMAT_F32:
  267. is_float = true;
  268. /* fall through */
  269. case AUDIO_FORMAT_S32:
  270. is_signed = true;
  271. /* fall through */
  272. case AUDIO_FORMAT_U32:
  273. bits = 32;
  274. mul = 4;
  275. break;
  276. default:
  277. abort();
  278. }
  279. info->freq = as->freq;
  280. info->bits = bits;
  281. info->is_signed = is_signed;
  282. info->is_float = is_float;
  283. info->nchannels = as->nchannels;
  284. info->bytes_per_frame = as->nchannels * mul;
  285. info->bytes_per_second = info->freq * info->bytes_per_frame;
  286. info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
  287. }
  288. void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
  289. {
  290. if (!len) {
  291. return;
  292. }
  293. if (info->is_signed || info->is_float) {
  294. memset(buf, 0x00, len * info->bytes_per_frame);
  295. } else {
  296. switch (info->bits) {
  297. case 8:
  298. memset(buf, 0x80, len * info->bytes_per_frame);
  299. break;
  300. case 16:
  301. {
  302. int i;
  303. uint16_t *p = buf;
  304. short s = INT16_MAX;
  305. if (info->swap_endianness) {
  306. s = bswap16 (s);
  307. }
  308. for (i = 0; i < len * info->nchannels; i++) {
  309. p[i] = s;
  310. }
  311. }
  312. break;
  313. case 32:
  314. {
  315. int i;
  316. uint32_t *p = buf;
  317. int32_t s = INT32_MAX;
  318. if (info->swap_endianness) {
  319. s = bswap32 (s);
  320. }
  321. for (i = 0; i < len * info->nchannels; i++) {
  322. p[i] = s;
  323. }
  324. }
  325. break;
  326. default:
  327. AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
  328. info->bits);
  329. break;
  330. }
  331. }
  332. }
  333. /*
  334. * Capture
  335. */
  336. static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
  337. struct audsettings *as)
  338. {
  339. CaptureVoiceOut *cap;
  340. for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
  341. if (audio_pcm_info_eq (&cap->hw.info, as)) {
  342. return cap;
  343. }
  344. }
  345. return NULL;
  346. }
  347. static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
  348. {
  349. struct capture_callback *cb;
  350. #ifdef DEBUG_CAPTURE
  351. dolog ("notification %d sent\n", cmd);
  352. #endif
  353. for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
  354. cb->ops.notify (cb->opaque, cmd);
  355. }
  356. }
  357. static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
  358. {
  359. if (cap->hw.enabled != enabled) {
  360. audcnotification_e cmd;
  361. cap->hw.enabled = enabled;
  362. cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
  363. audio_notify_capture (cap, cmd);
  364. }
  365. }
  366. static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
  367. {
  368. HWVoiceOut *hw = &cap->hw;
  369. SWVoiceOut *sw;
  370. int enabled = 0;
  371. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  372. if (sw->active) {
  373. enabled = 1;
  374. break;
  375. }
  376. }
  377. audio_capture_maybe_changed (cap, enabled);
  378. }
  379. static void audio_detach_capture (HWVoiceOut *hw)
  380. {
  381. SWVoiceCap *sc = hw->cap_head.lh_first;
  382. while (sc) {
  383. SWVoiceCap *sc1 = sc->entries.le_next;
  384. SWVoiceOut *sw = &sc->sw;
  385. CaptureVoiceOut *cap = sc->cap;
  386. int was_active = sw->active;
  387. if (sw->rate) {
  388. st_rate_stop (sw->rate);
  389. sw->rate = NULL;
  390. }
  391. QLIST_REMOVE (sw, entries);
  392. QLIST_REMOVE (sc, entries);
  393. g_free (sc);
  394. if (was_active) {
  395. /* We have removed soft voice from the capture:
  396. this might have changed the overall status of the capture
  397. since this might have been the only active voice */
  398. audio_recalc_and_notify_capture (cap);
  399. }
  400. sc = sc1;
  401. }
  402. }
  403. static int audio_attach_capture (HWVoiceOut *hw)
  404. {
  405. AudioState *s = hw->s;
  406. CaptureVoiceOut *cap;
  407. audio_detach_capture (hw);
  408. for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
  409. SWVoiceCap *sc;
  410. SWVoiceOut *sw;
  411. HWVoiceOut *hw_cap = &cap->hw;
  412. sc = g_malloc0(sizeof(*sc));
  413. sc->cap = cap;
  414. sw = &sc->sw;
  415. sw->hw = hw_cap;
  416. sw->info = hw->info;
  417. sw->empty = 1;
  418. sw->active = hw->enabled;
  419. sw->vol = nominal_volume;
  420. sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
  421. QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
  422. QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
  423. #ifdef DEBUG_CAPTURE
  424. sw->name = g_strdup_printf ("for %p %d,%d,%d",
  425. hw, sw->info.freq, sw->info.bits,
  426. sw->info.nchannels);
  427. dolog ("Added %s active = %d\n", sw->name, sw->active);
  428. #endif
  429. if (sw->active) {
  430. audio_capture_maybe_changed (cap, 1);
  431. }
  432. }
  433. return 0;
  434. }
  435. /*
  436. * Hard voice (capture)
  437. */
  438. static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
  439. {
  440. SWVoiceIn *sw;
  441. size_t m = hw->total_samples_captured;
  442. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  443. if (sw->active) {
  444. m = MIN (m, sw->total_hw_samples_acquired);
  445. }
  446. }
  447. return m;
  448. }
  449. static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
  450. {
  451. size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
  452. if (audio_bug(__func__, live > hw->conv_buf.size)) {
  453. dolog("live=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
  454. return 0;
  455. }
  456. return live;
  457. }
  458. static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples)
  459. {
  460. size_t conv = 0;
  461. STSampleBuffer *conv_buf = &hw->conv_buf;
  462. while (samples) {
  463. uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame);
  464. size_t proc = MIN(samples, conv_buf->size - conv_buf->pos);
  465. hw->conv(conv_buf->buffer + conv_buf->pos, src, proc);
  466. conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size;
  467. samples -= proc;
  468. conv += proc;
  469. }
  470. return conv;
  471. }
  472. /*
  473. * Soft voice (capture)
  474. */
  475. static void audio_pcm_sw_resample_in(SWVoiceIn *sw,
  476. size_t frames_in_max, size_t frames_out_max,
  477. size_t *total_in, size_t *total_out)
  478. {
  479. HWVoiceIn *hw = sw->hw;
  480. struct st_sample *src, *dst;
  481. size_t live, rpos, frames_in, frames_out;
  482. live = hw->total_samples_captured - sw->total_hw_samples_acquired;
  483. rpos = audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size);
  484. /* resample conv_buf from rpos to end of buffer */
  485. src = hw->conv_buf.buffer + rpos;
  486. frames_in = MIN(frames_in_max, hw->conv_buf.size - rpos);
  487. dst = sw->resample_buf.buffer;
  488. frames_out = frames_out_max;
  489. st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
  490. rpos += frames_in;
  491. *total_in = frames_in;
  492. *total_out = frames_out;
  493. /* resample conv_buf from start of buffer if there are input frames left */
  494. if (frames_in_max - frames_in && rpos == hw->conv_buf.size) {
  495. src = hw->conv_buf.buffer;
  496. frames_in = frames_in_max - frames_in;
  497. dst += frames_out;
  498. frames_out = frames_out_max - frames_out;
  499. st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
  500. *total_in += frames_in;
  501. *total_out += frames_out;
  502. }
  503. }
  504. static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len)
  505. {
  506. HWVoiceIn *hw = sw->hw;
  507. size_t live, frames_out_max, total_in, total_out;
  508. live = hw->total_samples_captured - sw->total_hw_samples_acquired;
  509. if (!live) {
  510. return 0;
  511. }
  512. if (audio_bug(__func__, live > hw->conv_buf.size)) {
  513. dolog("live_in=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
  514. return 0;
  515. }
  516. frames_out_max = MIN(buf_len / sw->info.bytes_per_frame,
  517. sw->resample_buf.size);
  518. audio_pcm_sw_resample_in(sw, live, frames_out_max, &total_in, &total_out);
  519. if (!hw->pcm_ops->volume_in) {
  520. mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol);
  521. }
  522. sw->clip(buf, sw->resample_buf.buffer, total_out);
  523. sw->total_hw_samples_acquired += total_in;
  524. return total_out * sw->info.bytes_per_frame;
  525. }
  526. /*
  527. * Hard voice (playback)
  528. */
  529. static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
  530. {
  531. SWVoiceOut *sw;
  532. size_t m = SIZE_MAX;
  533. int nb_live = 0;
  534. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  535. if (sw->active || !sw->empty) {
  536. m = MIN (m, sw->total_hw_samples_mixed);
  537. nb_live += 1;
  538. }
  539. }
  540. *nb_livep = nb_live;
  541. return m;
  542. }
  543. static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
  544. {
  545. size_t smin;
  546. int nb_live1;
  547. smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
  548. if (nb_live) {
  549. *nb_live = nb_live1;
  550. }
  551. if (nb_live1) {
  552. size_t live = smin;
  553. if (audio_bug(__func__, live > hw->mix_buf.size)) {
  554. dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
  555. return 0;
  556. }
  557. return live;
  558. }
  559. return 0;
  560. }
  561. static size_t audio_pcm_hw_get_free(HWVoiceOut *hw)
  562. {
  563. return (hw->pcm_ops->buffer_get_free ? hw->pcm_ops->buffer_get_free(hw) :
  564. INT_MAX) / hw->info.bytes_per_frame;
  565. }
  566. static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len)
  567. {
  568. size_t clipped = 0;
  569. size_t pos = hw->mix_buf.pos;
  570. while (len) {
  571. st_sample *src = hw->mix_buf.buffer + pos;
  572. uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame);
  573. size_t samples_till_end_of_buf = hw->mix_buf.size - pos;
  574. size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
  575. hw->clip(dst, src, samples_to_clip);
  576. pos = (pos + samples_to_clip) % hw->mix_buf.size;
  577. len -= samples_to_clip;
  578. clipped += samples_to_clip;
  579. }
  580. }
  581. /*
  582. * Soft voice (playback)
  583. */
  584. static void audio_pcm_sw_resample_out(SWVoiceOut *sw,
  585. size_t frames_in_max, size_t frames_out_max,
  586. size_t *total_in, size_t *total_out)
  587. {
  588. HWVoiceOut *hw = sw->hw;
  589. struct st_sample *src, *dst;
  590. size_t live, wpos, frames_in, frames_out;
  591. live = sw->total_hw_samples_mixed;
  592. wpos = (hw->mix_buf.pos + live) % hw->mix_buf.size;
  593. /* write to mix_buf from wpos to end of buffer */
  594. src = sw->resample_buf.buffer;
  595. frames_in = frames_in_max;
  596. dst = hw->mix_buf.buffer + wpos;
  597. frames_out = MIN(frames_out_max, hw->mix_buf.size - wpos);
  598. st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
  599. wpos += frames_out;
  600. *total_in = frames_in;
  601. *total_out = frames_out;
  602. /* write to mix_buf from start of buffer if there are input frames left */
  603. if (frames_in_max - frames_in > 0 && wpos == hw->mix_buf.size) {
  604. src += frames_in;
  605. frames_in = frames_in_max - frames_in;
  606. dst = hw->mix_buf.buffer;
  607. frames_out = frames_out_max - frames_out;
  608. st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
  609. *total_in += frames_in;
  610. *total_out += frames_out;
  611. }
  612. }
  613. static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len)
  614. {
  615. HWVoiceOut *hw = sw->hw;
  616. size_t live, dead, hw_free, sw_max, fe_max;
  617. size_t frames_in_max, frames_out_max, total_in, total_out;
  618. live = sw->total_hw_samples_mixed;
  619. if (audio_bug(__func__, live > hw->mix_buf.size)) {
  620. dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
  621. return 0;
  622. }
  623. if (live == hw->mix_buf.size) {
  624. #ifdef DEBUG_OUT
  625. dolog ("%s is full %zu\n", sw->name, live);
  626. #endif
  627. return 0;
  628. }
  629. dead = hw->mix_buf.size - live;
  630. hw_free = audio_pcm_hw_get_free(hw);
  631. hw_free = hw_free > live ? hw_free - live : 0;
  632. frames_out_max = MIN(dead, hw_free);
  633. sw_max = st_rate_frames_in(sw->rate, frames_out_max);
  634. fe_max = MIN(buf_len / sw->info.bytes_per_frame + sw->resample_buf.pos,
  635. sw->resample_buf.size);
  636. frames_in_max = MIN(sw_max, fe_max);
  637. if (!frames_in_max) {
  638. return 0;
  639. }
  640. if (frames_in_max > sw->resample_buf.pos) {
  641. sw->conv(sw->resample_buf.buffer + sw->resample_buf.pos,
  642. buf, frames_in_max - sw->resample_buf.pos);
  643. if (!sw->hw->pcm_ops->volume_out) {
  644. mixeng_volume(sw->resample_buf.buffer + sw->resample_buf.pos,
  645. frames_in_max - sw->resample_buf.pos, &sw->vol);
  646. }
  647. }
  648. audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max,
  649. &total_in, &total_out);
  650. sw->total_hw_samples_mixed += total_out;
  651. sw->empty = sw->total_hw_samples_mixed == 0;
  652. /*
  653. * Upsampling may leave one audio frame in the resample buffer. Decrement
  654. * total_in by one if there was a leftover frame from the previous resample
  655. * pass in the resample buffer. Increment total_in by one if the current
  656. * resample pass left one frame in the resample buffer.
  657. */
  658. if (frames_in_max - total_in == 1) {
  659. /* copy one leftover audio frame to the beginning of the buffer */
  660. *sw->resample_buf.buffer = *(sw->resample_buf.buffer + total_in);
  661. total_in += 1 - sw->resample_buf.pos;
  662. sw->resample_buf.pos = 1;
  663. } else if (total_in >= sw->resample_buf.pos) {
  664. total_in -= sw->resample_buf.pos;
  665. sw->resample_buf.pos = 0;
  666. }
  667. #ifdef DEBUG_OUT
  668. dolog (
  669. "%s: write size %zu written %zu total mixed %zu\n",
  670. SW_NAME(sw),
  671. buf_len / sw->info.bytes_per_frame,
  672. total_in,
  673. sw->total_hw_samples_mixed
  674. );
  675. #endif
  676. return total_in * sw->info.bytes_per_frame;
  677. }
  678. #ifdef DEBUG_AUDIO
  679. static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
  680. {
  681. dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
  682. cap, info->bits, info->is_signed, info->is_float, info->freq,
  683. info->nchannels);
  684. }
  685. #endif
  686. #define DAC
  687. #include "audio_template.h"
  688. #undef DAC
  689. #include "audio_template.h"
  690. /*
  691. * Timer
  692. */
  693. static int audio_is_timer_needed(AudioState *s)
  694. {
  695. HWVoiceIn *hwi = NULL;
  696. HWVoiceOut *hwo = NULL;
  697. while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
  698. if (!hwo->poll_mode) {
  699. return 1;
  700. }
  701. }
  702. while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
  703. if (!hwi->poll_mode) {
  704. return 1;
  705. }
  706. }
  707. return 0;
  708. }
  709. static void audio_reset_timer (AudioState *s)
  710. {
  711. if (audio_is_timer_needed(s)) {
  712. timer_mod_anticipate_ns(s->ts,
  713. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
  714. if (!s->timer_running) {
  715. s->timer_running = true;
  716. s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  717. trace_audio_timer_start(s->period_ticks / SCALE_MS);
  718. }
  719. } else {
  720. timer_del(s->ts);
  721. if (s->timer_running) {
  722. s->timer_running = false;
  723. trace_audio_timer_stop();
  724. }
  725. }
  726. }
  727. static void audio_timer (void *opaque)
  728. {
  729. int64_t now, diff;
  730. AudioState *s = opaque;
  731. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  732. diff = now - s->timer_last;
  733. if (diff > s->period_ticks * 3 / 2) {
  734. trace_audio_timer_delayed(diff / SCALE_MS);
  735. }
  736. s->timer_last = now;
  737. audio_run(s, "timer");
  738. audio_reset_timer(s);
  739. }
  740. /*
  741. * Public API
  742. */
  743. size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size)
  744. {
  745. HWVoiceOut *hw;
  746. if (!sw) {
  747. /* XXX: Consider options */
  748. return size;
  749. }
  750. hw = sw->hw;
  751. if (!hw->enabled) {
  752. dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
  753. return 0;
  754. }
  755. if (audio_get_pdo_out(hw->s->dev)->mixing_engine) {
  756. return audio_pcm_sw_write(sw, buf, size);
  757. } else {
  758. return hw->pcm_ops->write(hw, buf, size);
  759. }
  760. }
  761. size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)
  762. {
  763. HWVoiceIn *hw;
  764. if (!sw) {
  765. /* XXX: Consider options */
  766. return size;
  767. }
  768. hw = sw->hw;
  769. if (!hw->enabled) {
  770. dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
  771. return 0;
  772. }
  773. if (audio_get_pdo_in(hw->s->dev)->mixing_engine) {
  774. return audio_pcm_sw_read(sw, buf, size);
  775. } else {
  776. return hw->pcm_ops->read(hw, buf, size);
  777. }
  778. }
  779. int AUD_get_buffer_size_out(SWVoiceOut *sw)
  780. {
  781. return sw->hw->samples * sw->hw->info.bytes_per_frame;
  782. }
  783. void AUD_set_active_out (SWVoiceOut *sw, int on)
  784. {
  785. HWVoiceOut *hw;
  786. if (!sw) {
  787. return;
  788. }
  789. hw = sw->hw;
  790. if (sw->active != on) {
  791. AudioState *s = sw->s;
  792. SWVoiceOut *temp_sw;
  793. SWVoiceCap *sc;
  794. if (on) {
  795. hw->pending_disable = 0;
  796. if (!hw->enabled) {
  797. hw->enabled = 1;
  798. if (s->vm_running) {
  799. if (hw->pcm_ops->enable_out) {
  800. hw->pcm_ops->enable_out(hw, true);
  801. }
  802. audio_reset_timer (s);
  803. }
  804. }
  805. } else {
  806. if (hw->enabled) {
  807. int nb_active = 0;
  808. for (temp_sw = hw->sw_head.lh_first; temp_sw;
  809. temp_sw = temp_sw->entries.le_next) {
  810. nb_active += temp_sw->active != 0;
  811. }
  812. hw->pending_disable = nb_active == 1;
  813. }
  814. }
  815. for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
  816. sc->sw.active = hw->enabled;
  817. if (hw->enabled) {
  818. audio_capture_maybe_changed (sc->cap, 1);
  819. }
  820. }
  821. sw->active = on;
  822. }
  823. }
  824. void AUD_set_active_in (SWVoiceIn *sw, int on)
  825. {
  826. HWVoiceIn *hw;
  827. if (!sw) {
  828. return;
  829. }
  830. hw = sw->hw;
  831. if (sw->active != on) {
  832. AudioState *s = sw->s;
  833. SWVoiceIn *temp_sw;
  834. if (on) {
  835. if (!hw->enabled) {
  836. hw->enabled = 1;
  837. if (s->vm_running) {
  838. if (hw->pcm_ops->enable_in) {
  839. hw->pcm_ops->enable_in(hw, true);
  840. }
  841. audio_reset_timer (s);
  842. }
  843. }
  844. sw->total_hw_samples_acquired = hw->total_samples_captured;
  845. } else {
  846. if (hw->enabled) {
  847. int nb_active = 0;
  848. for (temp_sw = hw->sw_head.lh_first; temp_sw;
  849. temp_sw = temp_sw->entries.le_next) {
  850. nb_active += temp_sw->active != 0;
  851. }
  852. if (nb_active == 1) {
  853. hw->enabled = 0;
  854. if (hw->pcm_ops->enable_in) {
  855. hw->pcm_ops->enable_in(hw, false);
  856. }
  857. }
  858. }
  859. }
  860. sw->active = on;
  861. }
  862. }
  863. static size_t audio_get_avail (SWVoiceIn *sw)
  864. {
  865. size_t live;
  866. if (!sw) {
  867. return 0;
  868. }
  869. live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
  870. if (audio_bug(__func__, live > sw->hw->conv_buf.size)) {
  871. dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live,
  872. sw->hw->conv_buf.size);
  873. return 0;
  874. }
  875. ldebug (
  876. "%s: get_avail live %zu frontend frames %u\n",
  877. SW_NAME (sw),
  878. live, st_rate_frames_out(sw->rate, live)
  879. );
  880. return live;
  881. }
  882. static size_t audio_get_free(SWVoiceOut *sw)
  883. {
  884. size_t live, dead;
  885. if (!sw) {
  886. return 0;
  887. }
  888. live = sw->total_hw_samples_mixed;
  889. if (audio_bug(__func__, live > sw->hw->mix_buf.size)) {
  890. dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live,
  891. sw->hw->mix_buf.size);
  892. return 0;
  893. }
  894. dead = sw->hw->mix_buf.size - live;
  895. #ifdef DEBUG_OUT
  896. dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
  897. SW_NAME(sw), live, dead, st_rate_frames_in(sw->rate, dead));
  898. #endif
  899. return dead;
  900. }
  901. static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
  902. size_t samples)
  903. {
  904. size_t n;
  905. if (hw->enabled) {
  906. SWVoiceCap *sc;
  907. for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
  908. SWVoiceOut *sw = &sc->sw;
  909. size_t rpos2 = rpos;
  910. n = samples;
  911. while (n) {
  912. size_t till_end_of_hw = hw->mix_buf.size - rpos2;
  913. size_t to_read = MIN(till_end_of_hw, n);
  914. size_t live, frames_in, frames_out;
  915. sw->resample_buf.buffer = hw->mix_buf.buffer + rpos2;
  916. sw->resample_buf.size = to_read;
  917. live = sw->total_hw_samples_mixed;
  918. audio_pcm_sw_resample_out(sw,
  919. to_read, sw->hw->mix_buf.size - live,
  920. &frames_in, &frames_out);
  921. sw->total_hw_samples_mixed += frames_out;
  922. sw->empty = sw->total_hw_samples_mixed == 0;
  923. if (to_read - frames_in) {
  924. dolog("Could not mix %zu frames into a capture "
  925. "buffer, mixed %zu\n",
  926. to_read, frames_in);
  927. break;
  928. }
  929. n -= to_read;
  930. rpos2 = (rpos2 + to_read) % hw->mix_buf.size;
  931. }
  932. }
  933. }
  934. n = MIN(samples, hw->mix_buf.size - rpos);
  935. mixeng_clear(hw->mix_buf.buffer + rpos, n);
  936. mixeng_clear(hw->mix_buf.buffer, samples - n);
  937. }
  938. static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
  939. {
  940. size_t clipped = 0;
  941. while (live) {
  942. size_t size = live * hw->info.bytes_per_frame;
  943. size_t decr, proc;
  944. void *buf = hw->pcm_ops->get_buffer_out(hw, &size);
  945. if (size == 0) {
  946. break;
  947. }
  948. decr = MIN(size / hw->info.bytes_per_frame, live);
  949. if (buf) {
  950. audio_pcm_hw_clip_out(hw, buf, decr);
  951. }
  952. proc = hw->pcm_ops->put_buffer_out(hw, buf,
  953. decr * hw->info.bytes_per_frame) /
  954. hw->info.bytes_per_frame;
  955. live -= proc;
  956. clipped += proc;
  957. hw->mix_buf.pos = (hw->mix_buf.pos + proc) % hw->mix_buf.size;
  958. if (proc == 0 || proc < decr) {
  959. break;
  960. }
  961. }
  962. if (hw->pcm_ops->run_buffer_out) {
  963. hw->pcm_ops->run_buffer_out(hw);
  964. }
  965. return clipped;
  966. }
  967. static void audio_run_out (AudioState *s)
  968. {
  969. HWVoiceOut *hw = NULL;
  970. SWVoiceOut *sw;
  971. while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
  972. size_t played, live, prev_rpos;
  973. size_t hw_free = audio_pcm_hw_get_free(hw);
  974. int nb_live;
  975. if (!audio_get_pdo_out(s->dev)->mixing_engine) {
  976. /* there is exactly 1 sw for each hw with no mixeng */
  977. sw = hw->sw_head.lh_first;
  978. if (hw->pending_disable) {
  979. hw->enabled = 0;
  980. hw->pending_disable = 0;
  981. if (hw->pcm_ops->enable_out) {
  982. hw->pcm_ops->enable_out(hw, false);
  983. }
  984. }
  985. if (sw->active) {
  986. sw->callback.fn(sw->callback.opaque,
  987. hw_free * sw->info.bytes_per_frame);
  988. }
  989. if (hw->pcm_ops->run_buffer_out) {
  990. hw->pcm_ops->run_buffer_out(hw);
  991. }
  992. continue;
  993. }
  994. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  995. if (sw->active) {
  996. size_t sw_free = audio_get_free(sw);
  997. size_t free;
  998. if (hw_free > sw->total_hw_samples_mixed) {
  999. free = st_rate_frames_in(sw->rate,
  1000. MIN(sw_free, hw_free - sw->total_hw_samples_mixed));
  1001. } else {
  1002. free = 0;
  1003. }
  1004. if (free > sw->resample_buf.pos) {
  1005. free = MIN(free, sw->resample_buf.size)
  1006. - sw->resample_buf.pos;
  1007. sw->callback.fn(sw->callback.opaque,
  1008. free * sw->info.bytes_per_frame);
  1009. }
  1010. }
  1011. }
  1012. live = audio_pcm_hw_get_live_out (hw, &nb_live);
  1013. if (!nb_live) {
  1014. live = 0;
  1015. }
  1016. if (audio_bug(__func__, live > hw->mix_buf.size)) {
  1017. dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
  1018. continue;
  1019. }
  1020. if (hw->pending_disable && !nb_live) {
  1021. SWVoiceCap *sc;
  1022. #ifdef DEBUG_OUT
  1023. dolog ("Disabling voice\n");
  1024. #endif
  1025. hw->enabled = 0;
  1026. hw->pending_disable = 0;
  1027. if (hw->pcm_ops->enable_out) {
  1028. hw->pcm_ops->enable_out(hw, false);
  1029. }
  1030. for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
  1031. sc->sw.active = 0;
  1032. audio_recalc_and_notify_capture (sc->cap);
  1033. }
  1034. continue;
  1035. }
  1036. if (!live) {
  1037. if (hw->pcm_ops->run_buffer_out) {
  1038. hw->pcm_ops->run_buffer_out(hw);
  1039. }
  1040. continue;
  1041. }
  1042. prev_rpos = hw->mix_buf.pos;
  1043. played = audio_pcm_hw_run_out(hw, live);
  1044. replay_audio_out(&played);
  1045. if (audio_bug(__func__, hw->mix_buf.pos >= hw->mix_buf.size)) {
  1046. dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
  1047. hw->mix_buf.pos, hw->mix_buf.size, played);
  1048. hw->mix_buf.pos = 0;
  1049. }
  1050. #ifdef DEBUG_OUT
  1051. dolog("played=%zu\n", played);
  1052. #endif
  1053. if (played) {
  1054. hw->ts_helper += played;
  1055. audio_capture_mix_and_clear (hw, prev_rpos, played);
  1056. }
  1057. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  1058. if (!sw->active && sw->empty) {
  1059. continue;
  1060. }
  1061. if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
  1062. dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
  1063. played, sw->total_hw_samples_mixed);
  1064. played = sw->total_hw_samples_mixed;
  1065. }
  1066. sw->total_hw_samples_mixed -= played;
  1067. if (!sw->total_hw_samples_mixed) {
  1068. sw->empty = 1;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
  1074. {
  1075. size_t conv = 0;
  1076. if (hw->pcm_ops->run_buffer_in) {
  1077. hw->pcm_ops->run_buffer_in(hw);
  1078. }
  1079. while (samples) {
  1080. size_t proc;
  1081. size_t size = samples * hw->info.bytes_per_frame;
  1082. void *buf = hw->pcm_ops->get_buffer_in(hw, &size);
  1083. assert(size % hw->info.bytes_per_frame == 0);
  1084. if (size == 0) {
  1085. break;
  1086. }
  1087. proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame);
  1088. samples -= proc;
  1089. conv += proc;
  1090. hw->pcm_ops->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame);
  1091. }
  1092. return conv;
  1093. }
  1094. static void audio_run_in (AudioState *s)
  1095. {
  1096. HWVoiceIn *hw = NULL;
  1097. if (!audio_get_pdo_in(s->dev)->mixing_engine) {
  1098. while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
  1099. /* there is exactly 1 sw for each hw with no mixeng */
  1100. SWVoiceIn *sw = hw->sw_head.lh_first;
  1101. if (sw->active) {
  1102. sw->callback.fn(sw->callback.opaque, INT_MAX);
  1103. }
  1104. }
  1105. return;
  1106. }
  1107. while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
  1108. SWVoiceIn *sw;
  1109. size_t captured = 0, min;
  1110. if (replay_mode != REPLAY_MODE_PLAY) {
  1111. captured = audio_pcm_hw_run_in(
  1112. hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw));
  1113. }
  1114. replay_audio_in(&captured, hw->conv_buf.buffer, &hw->conv_buf.pos,
  1115. hw->conv_buf.size);
  1116. min = audio_pcm_hw_find_min_in (hw);
  1117. hw->total_samples_captured += captured - min;
  1118. hw->ts_helper += captured;
  1119. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  1120. sw->total_hw_samples_acquired -= min;
  1121. if (sw->active) {
  1122. size_t sw_avail = audio_get_avail(sw);
  1123. size_t avail;
  1124. avail = st_rate_frames_out(sw->rate, sw_avail);
  1125. if (avail > 0) {
  1126. avail = MIN(avail, sw->resample_buf.size);
  1127. sw->callback.fn(sw->callback.opaque,
  1128. avail * sw->info.bytes_per_frame);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. static void audio_run_capture (AudioState *s)
  1135. {
  1136. CaptureVoiceOut *cap;
  1137. for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
  1138. size_t live, rpos, captured;
  1139. HWVoiceOut *hw = &cap->hw;
  1140. SWVoiceOut *sw;
  1141. captured = live = audio_pcm_hw_get_live_out (hw, NULL);
  1142. rpos = hw->mix_buf.pos;
  1143. while (live) {
  1144. size_t left = hw->mix_buf.size - rpos;
  1145. size_t to_capture = MIN(live, left);
  1146. struct st_sample *src;
  1147. struct capture_callback *cb;
  1148. src = hw->mix_buf.buffer + rpos;
  1149. hw->clip (cap->buf, src, to_capture);
  1150. mixeng_clear (src, to_capture);
  1151. for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
  1152. cb->ops.capture (cb->opaque, cap->buf,
  1153. to_capture * hw->info.bytes_per_frame);
  1154. }
  1155. rpos = (rpos + to_capture) % hw->mix_buf.size;
  1156. live -= to_capture;
  1157. }
  1158. hw->mix_buf.pos = rpos;
  1159. for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
  1160. if (!sw->active && sw->empty) {
  1161. continue;
  1162. }
  1163. if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
  1164. dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
  1165. captured, sw->total_hw_samples_mixed);
  1166. captured = sw->total_hw_samples_mixed;
  1167. }
  1168. sw->total_hw_samples_mixed -= captured;
  1169. sw->empty = sw->total_hw_samples_mixed == 0;
  1170. }
  1171. }
  1172. }
  1173. void audio_run(AudioState *s, const char *msg)
  1174. {
  1175. audio_run_out(s);
  1176. audio_run_in(s);
  1177. audio_run_capture(s);
  1178. #ifdef DEBUG_POLL
  1179. {
  1180. static double prevtime;
  1181. double currtime;
  1182. struct timeval tv;
  1183. if (gettimeofday (&tv, NULL)) {
  1184. perror ("audio_run: gettimeofday");
  1185. return;
  1186. }
  1187. currtime = tv.tv_sec + tv.tv_usec * 1e-6;
  1188. dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
  1189. prevtime = currtime;
  1190. }
  1191. #endif
  1192. }
  1193. void audio_generic_run_buffer_in(HWVoiceIn *hw)
  1194. {
  1195. if (unlikely(!hw->buf_emul)) {
  1196. hw->size_emul = hw->samples * hw->info.bytes_per_frame;
  1197. hw->buf_emul = g_malloc(hw->size_emul);
  1198. hw->pos_emul = hw->pending_emul = 0;
  1199. }
  1200. while (hw->pending_emul < hw->size_emul) {
  1201. size_t read_len = MIN(hw->size_emul - hw->pos_emul,
  1202. hw->size_emul - hw->pending_emul);
  1203. size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul,
  1204. read_len);
  1205. hw->pending_emul += read;
  1206. hw->pos_emul = (hw->pos_emul + read) % hw->size_emul;
  1207. if (read < read_len) {
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
  1213. {
  1214. size_t start;
  1215. start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
  1216. assert(start < hw->size_emul);
  1217. *size = MIN(*size, hw->pending_emul);
  1218. *size = MIN(*size, hw->size_emul - start);
  1219. return hw->buf_emul + start;
  1220. }
  1221. void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
  1222. {
  1223. assert(size <= hw->pending_emul);
  1224. hw->pending_emul -= size;
  1225. }
  1226. size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
  1227. {
  1228. if (hw->buf_emul) {
  1229. return hw->size_emul - hw->pending_emul;
  1230. } else {
  1231. return hw->samples * hw->info.bytes_per_frame;
  1232. }
  1233. }
  1234. void audio_generic_run_buffer_out(HWVoiceOut *hw)
  1235. {
  1236. while (hw->pending_emul) {
  1237. size_t write_len, written, start;
  1238. start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
  1239. assert(start < hw->size_emul);
  1240. write_len = MIN(hw->pending_emul, hw->size_emul - start);
  1241. written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len);
  1242. hw->pending_emul -= written;
  1243. if (written < write_len) {
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
  1249. {
  1250. if (unlikely(!hw->buf_emul)) {
  1251. hw->size_emul = hw->samples * hw->info.bytes_per_frame;
  1252. hw->buf_emul = g_malloc(hw->size_emul);
  1253. hw->pos_emul = hw->pending_emul = 0;
  1254. }
  1255. *size = MIN(hw->size_emul - hw->pending_emul,
  1256. hw->size_emul - hw->pos_emul);
  1257. return hw->buf_emul + hw->pos_emul;
  1258. }
  1259. size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
  1260. {
  1261. assert(buf == hw->buf_emul + hw->pos_emul &&
  1262. size + hw->pending_emul <= hw->size_emul);
  1263. hw->pending_emul += size;
  1264. hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
  1265. return size;
  1266. }
  1267. size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
  1268. {
  1269. size_t total = 0;
  1270. if (hw->pcm_ops->buffer_get_free) {
  1271. size_t free = hw->pcm_ops->buffer_get_free(hw);
  1272. size = MIN(size, free);
  1273. }
  1274. while (total < size) {
  1275. size_t dst_size = size - total;
  1276. size_t copy_size, proc;
  1277. void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size);
  1278. if (dst_size == 0) {
  1279. break;
  1280. }
  1281. copy_size = MIN(size - total, dst_size);
  1282. if (dst) {
  1283. memcpy(dst, (char *)buf + total, copy_size);
  1284. }
  1285. proc = hw->pcm_ops->put_buffer_out(hw, dst, copy_size);
  1286. total += proc;
  1287. if (proc == 0 || proc < copy_size) {
  1288. break;
  1289. }
  1290. }
  1291. return total;
  1292. }
  1293. size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
  1294. {
  1295. size_t total = 0;
  1296. if (hw->pcm_ops->run_buffer_in) {
  1297. hw->pcm_ops->run_buffer_in(hw);
  1298. }
  1299. while (total < size) {
  1300. size_t src_size = size - total;
  1301. void *src = hw->pcm_ops->get_buffer_in(hw, &src_size);
  1302. if (src_size == 0) {
  1303. break;
  1304. }
  1305. memcpy((char *)buf + total, src, src_size);
  1306. hw->pcm_ops->put_buffer_in(hw, src, src_size);
  1307. total += src_size;
  1308. }
  1309. return total;
  1310. }
  1311. static int audio_driver_init(AudioState *s, struct audio_driver *drv,
  1312. Audiodev *dev, Error **errp)
  1313. {
  1314. Error *local_err = NULL;
  1315. s->drv_opaque = drv->init(dev, &local_err);
  1316. if (s->drv_opaque) {
  1317. if (!drv->pcm_ops->get_buffer_in) {
  1318. drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in;
  1319. drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in;
  1320. }
  1321. if (!drv->pcm_ops->get_buffer_out) {
  1322. drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out;
  1323. drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out;
  1324. }
  1325. audio_init_nb_voices_out(s, drv, 1);
  1326. audio_init_nb_voices_in(s, drv, 0);
  1327. s->drv = drv;
  1328. return 0;
  1329. } else {
  1330. if (local_err) {
  1331. error_propagate(errp, local_err);
  1332. } else {
  1333. error_setg(errp, "Could not init `%s' audio driver", drv->name);
  1334. }
  1335. return -1;
  1336. }
  1337. }
  1338. static void audio_vm_change_state_handler (void *opaque, bool running,
  1339. RunState state)
  1340. {
  1341. AudioState *s = opaque;
  1342. HWVoiceOut *hwo = NULL;
  1343. HWVoiceIn *hwi = NULL;
  1344. s->vm_running = running;
  1345. while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
  1346. if (hwo->pcm_ops->enable_out) {
  1347. hwo->pcm_ops->enable_out(hwo, running);
  1348. }
  1349. }
  1350. while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
  1351. if (hwi->pcm_ops->enable_in) {
  1352. hwi->pcm_ops->enable_in(hwi, running);
  1353. }
  1354. }
  1355. audio_reset_timer (s);
  1356. }
  1357. static void free_audio_state(AudioState *s)
  1358. {
  1359. HWVoiceOut *hwo, *hwon;
  1360. HWVoiceIn *hwi, *hwin;
  1361. QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
  1362. SWVoiceCap *sc;
  1363. if (hwo->enabled && hwo->pcm_ops->enable_out) {
  1364. hwo->pcm_ops->enable_out(hwo, false);
  1365. }
  1366. hwo->pcm_ops->fini_out (hwo);
  1367. for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
  1368. CaptureVoiceOut *cap = sc->cap;
  1369. struct capture_callback *cb;
  1370. for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
  1371. cb->ops.destroy (cb->opaque);
  1372. }
  1373. }
  1374. QLIST_REMOVE(hwo, entries);
  1375. }
  1376. QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
  1377. if (hwi->enabled && hwi->pcm_ops->enable_in) {
  1378. hwi->pcm_ops->enable_in(hwi, false);
  1379. }
  1380. hwi->pcm_ops->fini_in (hwi);
  1381. QLIST_REMOVE(hwi, entries);
  1382. }
  1383. if (s->drv) {
  1384. s->drv->fini (s->drv_opaque);
  1385. s->drv = NULL;
  1386. }
  1387. if (s->dev) {
  1388. qapi_free_Audiodev(s->dev);
  1389. s->dev = NULL;
  1390. }
  1391. if (s->ts) {
  1392. timer_free(s->ts);
  1393. s->ts = NULL;
  1394. }
  1395. g_free(s);
  1396. }
  1397. void audio_cleanup(void)
  1398. {
  1399. default_audio_state = NULL;
  1400. while (!QTAILQ_EMPTY(&audio_states)) {
  1401. AudioState *s = QTAILQ_FIRST(&audio_states);
  1402. QTAILQ_REMOVE(&audio_states, s, list);
  1403. free_audio_state(s);
  1404. }
  1405. }
  1406. static bool vmstate_audio_needed(void *opaque)
  1407. {
  1408. /*
  1409. * Never needed, this vmstate only exists in case
  1410. * an old qemu sends it to us.
  1411. */
  1412. return false;
  1413. }
  1414. static const VMStateDescription vmstate_audio = {
  1415. .name = "audio",
  1416. .version_id = 1,
  1417. .minimum_version_id = 1,
  1418. .needed = vmstate_audio_needed,
  1419. .fields = (const VMStateField[]) {
  1420. VMSTATE_END_OF_LIST()
  1421. }
  1422. };
  1423. void audio_create_default_audiodevs(void)
  1424. {
  1425. for (int i = 0; audio_prio_list[i]; i++) {
  1426. if (audio_driver_lookup(audio_prio_list[i])) {
  1427. QDict *dict = qdict_new();
  1428. Audiodev *dev = NULL;
  1429. Visitor *v;
  1430. qdict_put_str(dict, "driver", audio_prio_list[i]);
  1431. qdict_put_str(dict, "id", "#default");
  1432. v = qobject_input_visitor_new_keyval(QOBJECT(dict));
  1433. qobject_unref(dict);
  1434. visit_type_Audiodev(v, NULL, &dev, &error_fatal);
  1435. visit_free(v);
  1436. audio_define_default(dev, &error_abort);
  1437. }
  1438. }
  1439. }
  1440. /*
  1441. * if we have dev, this function was called because of an -audiodev argument =>
  1442. * initialize a new state with it
  1443. * if dev == NULL => legacy implicit initialization, return the already created
  1444. * state or create a new one
  1445. */
  1446. static AudioState *audio_init(Audiodev *dev, Error **errp)
  1447. {
  1448. static bool atexit_registered;
  1449. int done = 0;
  1450. const char *drvname;
  1451. VMChangeStateEntry *vmse;
  1452. AudioState *s;
  1453. struct audio_driver *driver;
  1454. s = g_new0(AudioState, 1);
  1455. QLIST_INIT (&s->hw_head_out);
  1456. QLIST_INIT (&s->hw_head_in);
  1457. QLIST_INIT (&s->cap_head);
  1458. if (!atexit_registered) {
  1459. atexit(audio_cleanup);
  1460. atexit_registered = true;
  1461. }
  1462. s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
  1463. if (dev) {
  1464. /* -audiodev option */
  1465. s->dev = dev;
  1466. drvname = AudiodevDriver_str(dev->driver);
  1467. driver = audio_driver_lookup(drvname);
  1468. if (driver) {
  1469. done = !audio_driver_init(s, driver, dev, errp);
  1470. } else {
  1471. error_setg(errp, "Unknown audio driver `%s'", drvname);
  1472. }
  1473. if (!done) {
  1474. goto out;
  1475. }
  1476. } else {
  1477. assert(!default_audio_state);
  1478. for (;;) {
  1479. AudiodevListEntry *e = QSIMPLEQ_FIRST(&default_audiodevs);
  1480. if (!e) {
  1481. error_setg(errp, "no default audio driver available");
  1482. goto out;
  1483. }
  1484. s->dev = dev = e->dev;
  1485. QSIMPLEQ_REMOVE_HEAD(&default_audiodevs, next);
  1486. g_free(e);
  1487. drvname = AudiodevDriver_str(dev->driver);
  1488. driver = audio_driver_lookup(drvname);
  1489. if (!audio_driver_init(s, driver, dev, NULL)) {
  1490. break;
  1491. }
  1492. qapi_free_Audiodev(dev);
  1493. s->dev = NULL;
  1494. }
  1495. }
  1496. if (dev->timer_period <= 0) {
  1497. s->period_ticks = 1;
  1498. } else {
  1499. s->period_ticks = dev->timer_period * (int64_t)SCALE_US;
  1500. }
  1501. vmse = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
  1502. if (!vmse) {
  1503. dolog ("warning: Could not register change state handler\n"
  1504. "(Audio can continue looping even after stopping the VM)\n");
  1505. }
  1506. QTAILQ_INSERT_TAIL(&audio_states, s, list);
  1507. QLIST_INIT (&s->card_head);
  1508. vmstate_register_any(NULL, &vmstate_audio, s);
  1509. return s;
  1510. out:
  1511. free_audio_state(s);
  1512. return NULL;
  1513. }
  1514. AudioState *audio_get_default_audio_state(Error **errp)
  1515. {
  1516. if (!default_audio_state) {
  1517. default_audio_state = audio_init(NULL, errp);
  1518. if (!default_audio_state) {
  1519. if (!QSIMPLEQ_EMPTY(&audiodevs)) {
  1520. error_append_hint(errp, "Perhaps you wanted to use -audio or set audiodev=%s?\n",
  1521. QSIMPLEQ_FIRST(&audiodevs)->dev->id);
  1522. }
  1523. }
  1524. }
  1525. return default_audio_state;
  1526. }
  1527. bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp)
  1528. {
  1529. if (!card->state) {
  1530. card->state = audio_get_default_audio_state(errp);
  1531. if (!card->state) {
  1532. return false;
  1533. }
  1534. }
  1535. card->name = g_strdup (name);
  1536. memset (&card->entries, 0, sizeof (card->entries));
  1537. QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
  1538. return true;
  1539. }
  1540. void AUD_remove_card (QEMUSoundCard *card)
  1541. {
  1542. QLIST_REMOVE (card, entries);
  1543. g_free (card->name);
  1544. }
  1545. static struct audio_pcm_ops capture_pcm_ops;
  1546. CaptureVoiceOut *AUD_add_capture(
  1547. AudioState *s,
  1548. struct audsettings *as,
  1549. struct audio_capture_ops *ops,
  1550. void *cb_opaque
  1551. )
  1552. {
  1553. CaptureVoiceOut *cap;
  1554. struct capture_callback *cb;
  1555. if (!s) {
  1556. error_report("Capturing without setting an audiodev is not supported");
  1557. abort();
  1558. }
  1559. if (!audio_get_pdo_out(s->dev)->mixing_engine) {
  1560. dolog("Can't capture with mixeng disabled\n");
  1561. return NULL;
  1562. }
  1563. if (audio_validate_settings (as)) {
  1564. dolog ("Invalid settings were passed when trying to add capture\n");
  1565. audio_print_settings (as);
  1566. return NULL;
  1567. }
  1568. cb = g_malloc0(sizeof(*cb));
  1569. cb->ops = *ops;
  1570. cb->opaque = cb_opaque;
  1571. cap = audio_pcm_capture_find_specific(s, as);
  1572. if (cap) {
  1573. QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
  1574. } else {
  1575. HWVoiceOut *hw;
  1576. cap = g_malloc0(sizeof(*cap));
  1577. hw = &cap->hw;
  1578. hw->s = s;
  1579. hw->pcm_ops = &capture_pcm_ops;
  1580. QLIST_INIT (&hw->sw_head);
  1581. QLIST_INIT (&cap->cb_head);
  1582. /* XXX find a more elegant way */
  1583. hw->samples = 4096 * 4;
  1584. audio_pcm_hw_alloc_resources_out(hw);
  1585. audio_pcm_init_info (&hw->info, as);
  1586. cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame);
  1587. if (hw->info.is_float) {
  1588. hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
  1589. } else {
  1590. hw->clip = mixeng_clip
  1591. [hw->info.nchannels == 2]
  1592. [hw->info.is_signed]
  1593. [hw->info.swap_endianness]
  1594. [audio_bits_to_index(hw->info.bits)];
  1595. }
  1596. QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
  1597. QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
  1598. QLIST_FOREACH(hw, &s->hw_head_out, entries) {
  1599. audio_attach_capture (hw);
  1600. }
  1601. }
  1602. return cap;
  1603. }
  1604. void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
  1605. {
  1606. struct capture_callback *cb;
  1607. for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
  1608. if (cb->opaque == cb_opaque) {
  1609. cb->ops.destroy (cb_opaque);
  1610. QLIST_REMOVE (cb, entries);
  1611. g_free (cb);
  1612. if (!cap->cb_head.lh_first) {
  1613. SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
  1614. while (sw) {
  1615. SWVoiceCap *sc = (SWVoiceCap *) sw;
  1616. #ifdef DEBUG_CAPTURE
  1617. dolog ("freeing %s\n", sw->name);
  1618. #endif
  1619. sw1 = sw->entries.le_next;
  1620. if (sw->rate) {
  1621. st_rate_stop (sw->rate);
  1622. sw->rate = NULL;
  1623. }
  1624. QLIST_REMOVE (sw, entries);
  1625. QLIST_REMOVE (sc, entries);
  1626. g_free (sc);
  1627. sw = sw1;
  1628. }
  1629. QLIST_REMOVE (cap, entries);
  1630. g_free(cap->hw.mix_buf.buffer);
  1631. g_free (cap->buf);
  1632. g_free (cap);
  1633. }
  1634. return;
  1635. }
  1636. }
  1637. }
  1638. void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
  1639. {
  1640. Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
  1641. audio_set_volume_out(sw, &vol);
  1642. }
  1643. void audio_set_volume_out(SWVoiceOut *sw, Volume *vol)
  1644. {
  1645. if (sw) {
  1646. HWVoiceOut *hw = sw->hw;
  1647. sw->vol.mute = vol->mute;
  1648. sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
  1649. sw->vol.r = nominal_volume.l * vol->vol[vol->channels > 1 ? 1 : 0] /
  1650. 255;
  1651. if (hw->pcm_ops->volume_out) {
  1652. hw->pcm_ops->volume_out(hw, vol);
  1653. }
  1654. }
  1655. }
  1656. void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
  1657. {
  1658. Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
  1659. audio_set_volume_in(sw, &vol);
  1660. }
  1661. void audio_set_volume_in(SWVoiceIn *sw, Volume *vol)
  1662. {
  1663. if (sw) {
  1664. HWVoiceIn *hw = sw->hw;
  1665. sw->vol.mute = vol->mute;
  1666. sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
  1667. sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
  1668. 255;
  1669. if (hw->pcm_ops->volume_in) {
  1670. hw->pcm_ops->volume_in(hw, vol);
  1671. }
  1672. }
  1673. }
  1674. void audio_create_pdos(Audiodev *dev)
  1675. {
  1676. switch (dev->driver) {
  1677. #define CASE(DRIVER, driver, pdo_name) \
  1678. case AUDIODEV_DRIVER_##DRIVER: \
  1679. if (!dev->u.driver.in) { \
  1680. dev->u.driver.in = g_malloc0( \
  1681. sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
  1682. } \
  1683. if (!dev->u.driver.out) { \
  1684. dev->u.driver.out = g_malloc0( \
  1685. sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
  1686. } \
  1687. break
  1688. CASE(NONE, none, );
  1689. #ifdef CONFIG_AUDIO_ALSA
  1690. CASE(ALSA, alsa, Alsa);
  1691. #endif
  1692. #ifdef CONFIG_AUDIO_COREAUDIO
  1693. CASE(COREAUDIO, coreaudio, Coreaudio);
  1694. #endif
  1695. #ifdef CONFIG_DBUS_DISPLAY
  1696. CASE(DBUS, dbus, );
  1697. #endif
  1698. #ifdef CONFIG_AUDIO_DSOUND
  1699. CASE(DSOUND, dsound, );
  1700. #endif
  1701. #ifdef CONFIG_AUDIO_JACK
  1702. CASE(JACK, jack, Jack);
  1703. #endif
  1704. #ifdef CONFIG_AUDIO_OSS
  1705. CASE(OSS, oss, Oss);
  1706. #endif
  1707. #ifdef CONFIG_AUDIO_PA
  1708. CASE(PA, pa, Pa);
  1709. #endif
  1710. #ifdef CONFIG_AUDIO_PIPEWIRE
  1711. CASE(PIPEWIRE, pipewire, Pipewire);
  1712. #endif
  1713. #ifdef CONFIG_AUDIO_SDL
  1714. CASE(SDL, sdl, Sdl);
  1715. #endif
  1716. #ifdef CONFIG_AUDIO_SNDIO
  1717. CASE(SNDIO, sndio, );
  1718. #endif
  1719. #ifdef CONFIG_SPICE
  1720. CASE(SPICE, spice, );
  1721. #endif
  1722. CASE(WAV, wav, );
  1723. case AUDIODEV_DRIVER__MAX:
  1724. abort();
  1725. };
  1726. }
  1727. static void audio_validate_per_direction_opts(
  1728. AudiodevPerDirectionOptions *pdo, Error **errp)
  1729. {
  1730. if (!pdo->has_mixing_engine) {
  1731. pdo->has_mixing_engine = true;
  1732. pdo->mixing_engine = true;
  1733. }
  1734. if (!pdo->has_fixed_settings) {
  1735. pdo->has_fixed_settings = true;
  1736. pdo->fixed_settings = pdo->mixing_engine;
  1737. }
  1738. if (!pdo->fixed_settings &&
  1739. (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
  1740. error_setg(errp,
  1741. "You can't use frequency, channels or format with fixed-settings=off");
  1742. return;
  1743. }
  1744. if (!pdo->mixing_engine && pdo->fixed_settings) {
  1745. error_setg(errp, "You can't use fixed-settings without mixeng");
  1746. return;
  1747. }
  1748. if (!pdo->has_frequency) {
  1749. pdo->has_frequency = true;
  1750. pdo->frequency = 44100;
  1751. }
  1752. if (!pdo->has_channels) {
  1753. pdo->has_channels = true;
  1754. pdo->channels = 2;
  1755. }
  1756. if (!pdo->has_voices) {
  1757. pdo->has_voices = true;
  1758. pdo->voices = pdo->mixing_engine ? 1 : INT_MAX;
  1759. }
  1760. if (!pdo->has_format) {
  1761. pdo->has_format = true;
  1762. pdo->format = AUDIO_FORMAT_S16;
  1763. }
  1764. }
  1765. static void audio_validate_opts(Audiodev *dev, Error **errp)
  1766. {
  1767. Error *err = NULL;
  1768. audio_create_pdos(dev);
  1769. audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
  1770. if (err) {
  1771. error_propagate(errp, err);
  1772. return;
  1773. }
  1774. audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
  1775. if (err) {
  1776. error_propagate(errp, err);
  1777. return;
  1778. }
  1779. if (!dev->has_timer_period) {
  1780. dev->has_timer_period = true;
  1781. dev->timer_period = 10000; /* 100Hz -> 10ms */
  1782. }
  1783. }
  1784. void audio_help(void)
  1785. {
  1786. int i;
  1787. printf("Available audio drivers:\n");
  1788. for (i = 0; i < AUDIODEV_DRIVER__MAX; i++) {
  1789. audio_driver *driver = audio_driver_lookup(AudiodevDriver_str(i));
  1790. if (driver) {
  1791. printf("%s\n", driver->name);
  1792. }
  1793. }
  1794. }
  1795. void audio_parse_option(const char *opt)
  1796. {
  1797. Audiodev *dev = NULL;
  1798. if (is_help_option(opt)) {
  1799. audio_help();
  1800. exit(EXIT_SUCCESS);
  1801. }
  1802. Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
  1803. visit_type_Audiodev(v, NULL, &dev, &error_fatal);
  1804. visit_free(v);
  1805. audio_define(dev);
  1806. }
  1807. void audio_define(Audiodev *dev)
  1808. {
  1809. AudiodevListEntry *e;
  1810. audio_validate_opts(dev, &error_fatal);
  1811. e = g_new0(AudiodevListEntry, 1);
  1812. e->dev = dev;
  1813. QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
  1814. }
  1815. void audio_define_default(Audiodev *dev, Error **errp)
  1816. {
  1817. AudiodevListEntry *e;
  1818. audio_validate_opts(dev, errp);
  1819. e = g_new0(AudiodevListEntry, 1);
  1820. e->dev = dev;
  1821. QSIMPLEQ_INSERT_TAIL(&default_audiodevs, e, next);
  1822. }
  1823. void audio_init_audiodevs(void)
  1824. {
  1825. AudiodevListEntry *e;
  1826. QSIMPLEQ_FOREACH(e, &audiodevs, next) {
  1827. audio_init(e->dev, &error_fatal);
  1828. }
  1829. }
  1830. audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
  1831. {
  1832. return (audsettings) {
  1833. .freq = pdo->frequency,
  1834. .nchannels = pdo->channels,
  1835. .fmt = pdo->format,
  1836. .endianness = AUDIO_HOST_ENDIANNESS,
  1837. };
  1838. }
  1839. int audioformat_bytes_per_sample(AudioFormat fmt)
  1840. {
  1841. switch (fmt) {
  1842. case AUDIO_FORMAT_U8:
  1843. case AUDIO_FORMAT_S8:
  1844. return 1;
  1845. case AUDIO_FORMAT_U16:
  1846. case AUDIO_FORMAT_S16:
  1847. return 2;
  1848. case AUDIO_FORMAT_U32:
  1849. case AUDIO_FORMAT_S32:
  1850. case AUDIO_FORMAT_F32:
  1851. return 4;
  1852. case AUDIO_FORMAT__MAX:
  1853. ;
  1854. }
  1855. abort();
  1856. }
  1857. /* frames = freq * usec / 1e6 */
  1858. int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
  1859. audsettings *as, int def_usecs)
  1860. {
  1861. uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
  1862. return (as->freq * usecs + 500000) / 1000000;
  1863. }
  1864. /* samples = channels * frames = channels * freq * usec / 1e6 */
  1865. int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
  1866. audsettings *as, int def_usecs)
  1867. {
  1868. return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
  1869. }
  1870. /*
  1871. * bytes = bytes_per_sample * samples =
  1872. * bytes_per_sample * channels * freq * usec / 1e6
  1873. */
  1874. int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
  1875. audsettings *as, int def_usecs)
  1876. {
  1877. return audio_buffer_samples(pdo, as, def_usecs) *
  1878. audioformat_bytes_per_sample(as->fmt);
  1879. }
  1880. AudioState *audio_state_by_name(const char *name, Error **errp)
  1881. {
  1882. AudioState *s;
  1883. QTAILQ_FOREACH(s, &audio_states, list) {
  1884. assert(s->dev);
  1885. if (strcmp(name, s->dev->id) == 0) {
  1886. return s;
  1887. }
  1888. }
  1889. error_setg(errp, "audiodev '%s' not found", name);
  1890. return NULL;
  1891. }
  1892. const char *audio_get_id(QEMUSoundCard *card)
  1893. {
  1894. if (card->state) {
  1895. assert(card->state->dev);
  1896. return card->state->dev->id;
  1897. } else {
  1898. return "";
  1899. }
  1900. }
  1901. const char *audio_application_name(void)
  1902. {
  1903. const char *vm_name;
  1904. vm_name = qemu_get_vm_name();
  1905. return vm_name ? vm_name : "qemu";
  1906. }
  1907. void audio_rate_start(RateCtl *rate)
  1908. {
  1909. memset(rate, 0, sizeof(RateCtl));
  1910. rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1911. }
  1912. size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
  1913. {
  1914. int64_t now;
  1915. int64_t ticks;
  1916. int64_t bytes;
  1917. int64_t frames;
  1918. now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1919. ticks = now - rate->start_ticks;
  1920. bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
  1921. frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
  1922. if (frames < 0 || frames > 65536) {
  1923. AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames);
  1924. audio_rate_start(rate);
  1925. frames = 0;
  1926. }
  1927. return frames * info->bytes_per_frame;
  1928. }
  1929. void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
  1930. {
  1931. rate->bytes_sent += bytes_used;
  1932. }
  1933. size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
  1934. size_t bytes_avail)
  1935. {
  1936. size_t bytes;
  1937. bytes = audio_rate_peek_bytes(rate, info);
  1938. bytes = MIN(bytes, bytes_avail);
  1939. audio_rate_add_bytes(rate, bytes);
  1940. return bytes;
  1941. }
  1942. AudiodevList *qmp_query_audiodevs(Error **errp)
  1943. {
  1944. AudiodevList *ret = NULL;
  1945. AudiodevListEntry *e;
  1946. QSIMPLEQ_FOREACH(e, &audiodevs, next) {
  1947. QAPI_LIST_PREPEND(ret, QAPI_CLONE(Audiodev, e->dev));
  1948. }
  1949. return ret;
  1950. }