audio_template.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * QEMU Audio subsystem header
  3. *
  4. * Copyright (c) 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. #ifdef DAC
  25. #define NAME "playback"
  26. #define HWBUF hw->mix_buf
  27. #define TYPE out
  28. #define HW HWVoiceOut
  29. #define SW SWVoiceOut
  30. #else
  31. #define NAME "capture"
  32. #define TYPE in
  33. #define HW HWVoiceIn
  34. #define SW SWVoiceIn
  35. #define HWBUF hw->conv_buf
  36. #endif
  37. static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,
  38. struct audio_driver *drv, int min_voices)
  39. {
  40. int max_voices = glue (drv->max_voices_, TYPE);
  41. size_t voice_size = glue(drv->voice_size_, TYPE);
  42. glue (s->nb_hw_voices_, TYPE) = glue(audio_get_pdo_, TYPE)(s->dev)->voices;
  43. if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
  44. if (!max_voices) {
  45. #ifdef DAC
  46. dolog ("Driver `%s' does not support " NAME "\n", drv->name);
  47. #endif
  48. } else {
  49. dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
  50. drv->name,
  51. glue (s->nb_hw_voices_, TYPE),
  52. max_voices);
  53. }
  54. glue (s->nb_hw_voices_, TYPE) = max_voices;
  55. }
  56. if (glue (s->nb_hw_voices_, TYPE) < min_voices) {
  57. dolog ("Bogus number of " NAME " voices %d, setting to %d\n",
  58. glue (s->nb_hw_voices_, TYPE),
  59. min_voices);
  60. }
  61. if (audio_bug(__func__, !voice_size && max_voices)) {
  62. dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
  63. drv->name, max_voices);
  64. glue (s->nb_hw_voices_, TYPE) = 0;
  65. }
  66. if (audio_bug(__func__, voice_size && !max_voices)) {
  67. dolog("drv=`%s' voice_size=%zu max_voices=0\n",
  68. drv->name, voice_size);
  69. }
  70. }
  71. static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
  72. {
  73. g_free(hw->buf_emul);
  74. g_free(HWBUF.buffer);
  75. HWBUF.buffer = NULL;
  76. HWBUF.size = 0;
  77. }
  78. static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw)
  79. {
  80. if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) {
  81. size_t samples = hw->samples;
  82. if (audio_bug(__func__, samples == 0)) {
  83. dolog("Attempted to allocate empty buffer\n");
  84. }
  85. HWBUF.buffer = g_new0(st_sample, samples);
  86. HWBUF.size = samples;
  87. HWBUF.pos = 0;
  88. } else {
  89. HWBUF.buffer = NULL;
  90. HWBUF.size = 0;
  91. }
  92. }
  93. static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
  94. {
  95. g_free(sw->resample_buf.buffer);
  96. sw->resample_buf.buffer = NULL;
  97. sw->resample_buf.size = 0;
  98. if (sw->rate) {
  99. st_rate_stop (sw->rate);
  100. }
  101. sw->rate = NULL;
  102. }
  103. static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
  104. {
  105. HW *hw = sw->hw;
  106. uint64_t samples;
  107. if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) {
  108. return 0;
  109. }
  110. samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq);
  111. if (samples == 0) {
  112. uint64_t f_fe_min;
  113. uint64_t f_be = (uint32_t)hw->info.freq;
  114. /* f_fe_min = ceil(1 [frames] * f_be [Hz] / size_be [frames]) */
  115. f_fe_min = (f_be + HWBUF.size - 1) / HWBUF.size;
  116. qemu_log_mask(LOG_UNIMP,
  117. AUDIO_CAP ": The guest selected a " NAME " sample rate"
  118. " of %d Hz for %s. Only sample rates >= %" PRIu64 " Hz"
  119. " are supported.\n",
  120. sw->info.freq, sw->name, f_fe_min);
  121. return -1;
  122. }
  123. /*
  124. * Allocate one additional audio frame that is needed for upsampling
  125. * if the resample buffer size is small. For large buffer sizes take
  126. * care of overflows and truncation.
  127. */
  128. samples = samples < SIZE_MAX ? samples + 1 : SIZE_MAX;
  129. sw->resample_buf.buffer = g_new0(st_sample, samples);
  130. sw->resample_buf.size = samples;
  131. sw->resample_buf.pos = 0;
  132. #ifdef DAC
  133. sw->rate = st_rate_start(sw->info.freq, hw->info.freq);
  134. #else
  135. sw->rate = st_rate_start(hw->info.freq, sw->info.freq);
  136. #endif
  137. return 0;
  138. }
  139. static int glue (audio_pcm_sw_init_, TYPE) (
  140. SW *sw,
  141. HW *hw,
  142. const char *name,
  143. struct audsettings *as
  144. )
  145. {
  146. int err;
  147. audio_pcm_init_info (&sw->info, as);
  148. sw->hw = hw;
  149. sw->active = 0;
  150. #ifdef DAC
  151. sw->total_hw_samples_mixed = 0;
  152. sw->empty = 1;
  153. #endif
  154. if (sw->info.is_float) {
  155. #ifdef DAC
  156. sw->conv = mixeng_conv_float[sw->info.nchannels == 2];
  157. #else
  158. sw->clip = mixeng_clip_float[sw->info.nchannels == 2];
  159. #endif
  160. } else {
  161. #ifdef DAC
  162. sw->conv = mixeng_conv
  163. #else
  164. sw->clip = mixeng_clip
  165. #endif
  166. [sw->info.nchannels == 2]
  167. [sw->info.is_signed]
  168. [sw->info.swap_endianness]
  169. [audio_bits_to_index(sw->info.bits)];
  170. }
  171. sw->name = g_strdup (name);
  172. err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
  173. if (err) {
  174. g_free (sw->name);
  175. sw->name = NULL;
  176. }
  177. return err;
  178. }
  179. static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
  180. {
  181. glue (audio_pcm_sw_free_resources_, TYPE) (sw);
  182. g_free (sw->name);
  183. sw->name = NULL;
  184. }
  185. static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
  186. {
  187. QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
  188. }
  189. static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
  190. {
  191. QLIST_REMOVE (sw, entries);
  192. }
  193. static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
  194. {
  195. HW *hw = *hwp;
  196. AudioState *s = hw->s;
  197. if (!hw->sw_head.lh_first) {
  198. #ifdef DAC
  199. audio_detach_capture(hw);
  200. #endif
  201. QLIST_REMOVE(hw, entries);
  202. glue(hw->pcm_ops->fini_, TYPE) (hw);
  203. glue(s->nb_hw_voices_, TYPE) += 1;
  204. glue(audio_pcm_hw_free_resources_ , TYPE) (hw);
  205. g_free(hw);
  206. *hwp = NULL;
  207. }
  208. }
  209. static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw)
  210. {
  211. return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
  212. }
  213. static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw)
  214. {
  215. while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
  216. if (hw->enabled) {
  217. return hw;
  218. }
  219. }
  220. return NULL;
  221. }
  222. static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw,
  223. struct audsettings *as)
  224. {
  225. while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
  226. if (audio_pcm_info_eq (&hw->info, as)) {
  227. return hw;
  228. }
  229. }
  230. return NULL;
  231. }
  232. static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s,
  233. struct audsettings *as)
  234. {
  235. HW *hw;
  236. struct audio_driver *drv = s->drv;
  237. if (!glue (s->nb_hw_voices_, TYPE)) {
  238. return NULL;
  239. }
  240. if (audio_bug(__func__, !drv)) {
  241. dolog ("No host audio driver\n");
  242. return NULL;
  243. }
  244. if (audio_bug(__func__, !drv->pcm_ops)) {
  245. dolog ("Host audio driver without pcm_ops\n");
  246. return NULL;
  247. }
  248. /*
  249. * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE)
  250. * is guaranteed to be != 0. See the audio_init_nb_voices_* functions.
  251. */
  252. hw = g_malloc0(glue(drv->voice_size_, TYPE));
  253. hw->s = s;
  254. hw->pcm_ops = drv->pcm_ops;
  255. QLIST_INIT (&hw->sw_head);
  256. #ifdef DAC
  257. QLIST_INIT (&hw->cap_head);
  258. #endif
  259. if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
  260. goto err0;
  261. }
  262. if (audio_bug(__func__, hw->samples <= 0)) {
  263. dolog("hw->samples=%zd\n", hw->samples);
  264. goto err1;
  265. }
  266. if (hw->info.is_float) {
  267. #ifdef DAC
  268. hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
  269. #else
  270. hw->conv = mixeng_conv_float[hw->info.nchannels == 2];
  271. #endif
  272. } else {
  273. #ifdef DAC
  274. hw->clip = mixeng_clip
  275. #else
  276. hw->conv = mixeng_conv
  277. #endif
  278. [hw->info.nchannels == 2]
  279. [hw->info.is_signed]
  280. [hw->info.swap_endianness]
  281. [audio_bits_to_index(hw->info.bits)];
  282. }
  283. glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
  284. QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
  285. glue (s->nb_hw_voices_, TYPE) -= 1;
  286. #ifdef DAC
  287. audio_attach_capture (hw);
  288. #endif
  289. return hw;
  290. err1:
  291. glue (hw->pcm_ops->fini_, TYPE) (hw);
  292. err0:
  293. g_free (hw);
  294. return NULL;
  295. }
  296. AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev)
  297. {
  298. switch (dev->driver) {
  299. case AUDIODEV_DRIVER_NONE:
  300. return dev->u.none.TYPE;
  301. #ifdef CONFIG_AUDIO_ALSA
  302. case AUDIODEV_DRIVER_ALSA:
  303. return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE);
  304. #endif
  305. #ifdef CONFIG_AUDIO_COREAUDIO
  306. case AUDIODEV_DRIVER_COREAUDIO:
  307. return qapi_AudiodevCoreaudioPerDirectionOptions_base(
  308. dev->u.coreaudio.TYPE);
  309. #endif
  310. #ifdef CONFIG_DBUS_DISPLAY
  311. case AUDIODEV_DRIVER_DBUS:
  312. return dev->u.dbus.TYPE;
  313. #endif
  314. #ifdef CONFIG_AUDIO_DSOUND
  315. case AUDIODEV_DRIVER_DSOUND:
  316. return dev->u.dsound.TYPE;
  317. #endif
  318. #ifdef CONFIG_AUDIO_JACK
  319. case AUDIODEV_DRIVER_JACK:
  320. return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE);
  321. #endif
  322. #ifdef CONFIG_AUDIO_OSS
  323. case AUDIODEV_DRIVER_OSS:
  324. return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE);
  325. #endif
  326. #ifdef CONFIG_AUDIO_PA
  327. case AUDIODEV_DRIVER_PA:
  328. return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE);
  329. #endif
  330. #ifdef CONFIG_AUDIO_PIPEWIRE
  331. case AUDIODEV_DRIVER_PIPEWIRE:
  332. return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE);
  333. #endif
  334. #ifdef CONFIG_AUDIO_SDL
  335. case AUDIODEV_DRIVER_SDL:
  336. return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE);
  337. #endif
  338. #ifdef CONFIG_AUDIO_SNDIO
  339. case AUDIODEV_DRIVER_SNDIO:
  340. return dev->u.sndio.TYPE;
  341. #endif
  342. #ifdef CONFIG_SPICE
  343. case AUDIODEV_DRIVER_SPICE:
  344. return dev->u.spice.TYPE;
  345. #endif
  346. case AUDIODEV_DRIVER_WAV:
  347. return dev->u.wav.TYPE;
  348. case AUDIODEV_DRIVER__MAX:
  349. break;
  350. }
  351. abort();
  352. }
  353. static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as)
  354. {
  355. HW *hw;
  356. AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
  357. if (!pdo->mixing_engine || pdo->fixed_settings) {
  358. hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
  359. if (!pdo->mixing_engine || hw) {
  360. return hw;
  361. }
  362. }
  363. hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as);
  364. if (hw) {
  365. return hw;
  366. }
  367. hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
  368. if (hw) {
  369. return hw;
  370. }
  371. return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL);
  372. }
  373. static SW *glue(audio_pcm_create_voice_pair_, TYPE)(
  374. AudioState *s,
  375. const char *sw_name,
  376. struct audsettings *as
  377. )
  378. {
  379. SW *sw;
  380. HW *hw;
  381. struct audsettings hw_as;
  382. AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
  383. if (pdo->fixed_settings) {
  384. hw_as = audiodev_to_audsettings(pdo);
  385. } else {
  386. hw_as = *as;
  387. }
  388. sw = g_new0(SW, 1);
  389. sw->s = s;
  390. hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as);
  391. if (!hw) {
  392. dolog("Could not create a backend for voice `%s'\n", sw_name);
  393. goto err1;
  394. }
  395. glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
  396. if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
  397. goto err2;
  398. }
  399. return sw;
  400. err2:
  401. glue (audio_pcm_hw_del_sw_, TYPE) (sw);
  402. glue (audio_pcm_hw_gc_, TYPE) (&hw);
  403. err1:
  404. g_free(sw);
  405. return NULL;
  406. }
  407. static void glue (audio_close_, TYPE) (SW *sw)
  408. {
  409. glue (audio_pcm_sw_fini_, TYPE) (sw);
  410. glue (audio_pcm_hw_del_sw_, TYPE) (sw);
  411. glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
  412. g_free (sw);
  413. }
  414. void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
  415. {
  416. if (sw) {
  417. if (audio_bug(__func__, !card)) {
  418. dolog ("card=%p\n", card);
  419. return;
  420. }
  421. glue (audio_close_, TYPE) (sw);
  422. }
  423. }
  424. SW *glue (AUD_open_, TYPE) (
  425. QEMUSoundCard *card,
  426. SW *sw,
  427. const char *name,
  428. void *callback_opaque ,
  429. audio_callback_fn callback_fn,
  430. struct audsettings *as
  431. )
  432. {
  433. AudioState *s;
  434. AudiodevPerDirectionOptions *pdo;
  435. if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
  436. dolog ("card=%p name=%p callback_fn=%p as=%p\n",
  437. card, name, callback_fn, as);
  438. goto fail;
  439. }
  440. s = card->state;
  441. pdo = glue(audio_get_pdo_, TYPE)(s->dev);
  442. ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
  443. name, as->freq, as->nchannels, as->fmt);
  444. if (audio_bug(__func__, audio_validate_settings(as))) {
  445. audio_print_settings (as);
  446. goto fail;
  447. }
  448. if (audio_bug(__func__, !s->drv)) {
  449. dolog ("Can not open `%s' (no host audio driver)\n", name);
  450. goto fail;
  451. }
  452. if (sw && audio_pcm_info_eq (&sw->info, as)) {
  453. return sw;
  454. }
  455. if (!pdo->fixed_settings && sw) {
  456. glue (AUD_close_, TYPE) (card, sw);
  457. sw = NULL;
  458. }
  459. if (sw) {
  460. HW *hw = sw->hw;
  461. if (!hw) {
  462. dolog("Internal logic error: voice `%s' has no backend\n",
  463. SW_NAME(sw));
  464. goto fail;
  465. }
  466. glue (audio_pcm_sw_fini_, TYPE) (sw);
  467. if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
  468. goto fail;
  469. }
  470. } else {
  471. sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as);
  472. if (!sw) {
  473. return NULL;
  474. }
  475. }
  476. sw->card = card;
  477. sw->vol = nominal_volume;
  478. sw->callback.fn = callback_fn;
  479. sw->callback.opaque = callback_opaque;
  480. #ifdef DEBUG_AUDIO
  481. dolog ("%s\n", name);
  482. audio_pcm_print_info ("hw", &sw->hw->info);
  483. audio_pcm_print_info ("sw", &sw->info);
  484. #endif
  485. return sw;
  486. fail:
  487. glue (AUD_close_, TYPE) (card, sw);
  488. return NULL;
  489. }
  490. int glue (AUD_is_active_, TYPE) (SW *sw)
  491. {
  492. return sw ? sw->active : 0;
  493. }
  494. void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
  495. {
  496. if (!sw) {
  497. return;
  498. }
  499. ts->old_ts = sw->hw->ts_helper;
  500. }
  501. uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
  502. {
  503. uint64_t delta, cur_ts, old_ts;
  504. if (!sw) {
  505. return 0;
  506. }
  507. cur_ts = sw->hw->ts_helper;
  508. old_ts = ts->old_ts;
  509. /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
  510. if (cur_ts >= old_ts) {
  511. delta = cur_ts - old_ts;
  512. } else {
  513. delta = UINT64_MAX - old_ts + cur_ts;
  514. }
  515. if (!delta) {
  516. return 0;
  517. }
  518. return muldiv64 (delta, sw->hw->info.freq, 1000000);
  519. }
  520. #undef TYPE
  521. #undef HW
  522. #undef SW
  523. #undef HWBUF
  524. #undef NAME