audio_int.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * QEMU Audio subsystem header
  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. #ifndef QEMU_AUDIO_INT_H
  25. #define QEMU_AUDIO_INT_H
  26. #ifdef CONFIG_AUDIO_COREAUDIO
  27. #define FLOAT_MIXENG
  28. /* #define RECIPROCAL */
  29. #endif
  30. #include "mixeng.h"
  31. struct audio_pcm_ops;
  32. struct audio_callback {
  33. void *opaque;
  34. audio_callback_fn fn;
  35. };
  36. struct audio_pcm_info {
  37. int bits;
  38. bool is_signed;
  39. bool is_float;
  40. int freq;
  41. int nchannels;
  42. int bytes_per_frame;
  43. int bytes_per_second;
  44. int swap_endianness;
  45. };
  46. typedef struct AudioState AudioState;
  47. typedef struct SWVoiceCap SWVoiceCap;
  48. typedef struct STSampleBuffer {
  49. size_t pos, size;
  50. st_sample samples[];
  51. } STSampleBuffer;
  52. typedef struct HWVoiceOut {
  53. AudioState *s;
  54. int enabled;
  55. int poll_mode;
  56. int pending_disable;
  57. struct audio_pcm_info info;
  58. f_sample *clip;
  59. uint64_t ts_helper;
  60. STSampleBuffer *mix_buf;
  61. void *buf_emul;
  62. size_t pos_emul, pending_emul, size_emul;
  63. size_t samples;
  64. QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
  65. QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
  66. struct audio_pcm_ops *pcm_ops;
  67. QLIST_ENTRY (HWVoiceOut) entries;
  68. } HWVoiceOut;
  69. typedef struct HWVoiceIn {
  70. AudioState *s;
  71. int enabled;
  72. int poll_mode;
  73. struct audio_pcm_info info;
  74. t_sample *conv;
  75. size_t total_samples_captured;
  76. uint64_t ts_helper;
  77. STSampleBuffer *conv_buf;
  78. void *buf_emul;
  79. size_t pos_emul, pending_emul, size_emul;
  80. size_t samples;
  81. QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
  82. struct audio_pcm_ops *pcm_ops;
  83. QLIST_ENTRY (HWVoiceIn) entries;
  84. } HWVoiceIn;
  85. struct SWVoiceOut {
  86. QEMUSoundCard *card;
  87. AudioState *s;
  88. struct audio_pcm_info info;
  89. t_sample *conv;
  90. int64_t ratio;
  91. struct st_sample *buf;
  92. void *rate;
  93. size_t total_hw_samples_mixed;
  94. int active;
  95. int empty;
  96. HWVoiceOut *hw;
  97. char *name;
  98. struct mixeng_volume vol;
  99. struct audio_callback callback;
  100. QLIST_ENTRY (SWVoiceOut) entries;
  101. };
  102. struct SWVoiceIn {
  103. QEMUSoundCard *card;
  104. AudioState *s;
  105. int active;
  106. struct audio_pcm_info info;
  107. int64_t ratio;
  108. void *rate;
  109. size_t total_hw_samples_acquired;
  110. struct st_sample *buf;
  111. f_sample *clip;
  112. HWVoiceIn *hw;
  113. char *name;
  114. struct mixeng_volume vol;
  115. struct audio_callback callback;
  116. QLIST_ENTRY (SWVoiceIn) entries;
  117. };
  118. typedef struct audio_driver audio_driver;
  119. struct audio_driver {
  120. const char *name;
  121. const char *descr;
  122. void *(*init) (Audiodev *);
  123. void (*fini) (void *);
  124. struct audio_pcm_ops *pcm_ops;
  125. int can_be_default;
  126. int max_voices_out;
  127. int max_voices_in;
  128. int voice_size_out;
  129. int voice_size_in;
  130. QLIST_ENTRY(audio_driver) next;
  131. };
  132. struct audio_pcm_ops {
  133. int (*init_out)(HWVoiceOut *hw, audsettings *as, void *drv_opaque);
  134. void (*fini_out)(HWVoiceOut *hw);
  135. size_t (*write) (HWVoiceOut *hw, void *buf, size_t size);
  136. void (*run_buffer_out)(HWVoiceOut *hw);
  137. /*
  138. * get a buffer that after later can be passed to put_buffer_out; optional
  139. * returns the buffer, and writes it's size to size (in bytes)
  140. * this is unrelated to the above buffer_size_out function
  141. */
  142. void *(*get_buffer_out)(HWVoiceOut *hw, size_t *size);
  143. /*
  144. * put back the buffer returned by get_buffer_out; optional
  145. * buf must be equal the pointer returned by get_buffer_out,
  146. * size may be smaller
  147. */
  148. size_t (*put_buffer_out)(HWVoiceOut *hw, void *buf, size_t size);
  149. void (*enable_out)(HWVoiceOut *hw, bool enable);
  150. void (*volume_out)(HWVoiceOut *hw, Volume *vol);
  151. int (*init_in) (HWVoiceIn *hw, audsettings *as, void *drv_opaque);
  152. void (*fini_in) (HWVoiceIn *hw);
  153. size_t (*read) (HWVoiceIn *hw, void *buf, size_t size);
  154. void *(*get_buffer_in)(HWVoiceIn *hw, size_t *size);
  155. void (*put_buffer_in)(HWVoiceIn *hw, void *buf, size_t size);
  156. void (*enable_in)(HWVoiceIn *hw, bool enable);
  157. void (*volume_in)(HWVoiceIn *hw, Volume *vol);
  158. };
  159. void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size);
  160. void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size);
  161. void audio_generic_run_buffer_out(HWVoiceOut *hw);
  162. void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size);
  163. size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size);
  164. size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size);
  165. size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size);
  166. struct capture_callback {
  167. struct audio_capture_ops ops;
  168. void *opaque;
  169. QLIST_ENTRY (capture_callback) entries;
  170. };
  171. struct CaptureVoiceOut {
  172. HWVoiceOut hw;
  173. void *buf;
  174. QLIST_HEAD (cb_listhead, capture_callback) cb_head;
  175. QLIST_ENTRY (CaptureVoiceOut) entries;
  176. };
  177. struct SWVoiceCap {
  178. SWVoiceOut sw;
  179. CaptureVoiceOut *cap;
  180. QLIST_ENTRY (SWVoiceCap) entries;
  181. };
  182. typedef struct AudioState {
  183. struct audio_driver *drv;
  184. Audiodev *dev;
  185. void *drv_opaque;
  186. QEMUTimer *ts;
  187. QLIST_HEAD (card_listhead, QEMUSoundCard) card_head;
  188. QLIST_HEAD (hw_in_listhead, HWVoiceIn) hw_head_in;
  189. QLIST_HEAD (hw_out_listhead, HWVoiceOut) hw_head_out;
  190. QLIST_HEAD (cap_listhead, CaptureVoiceOut) cap_head;
  191. int nb_hw_voices_out;
  192. int nb_hw_voices_in;
  193. int vm_running;
  194. int64_t period_ticks;
  195. bool timer_running;
  196. uint64_t timer_last;
  197. QTAILQ_ENTRY(AudioState) list;
  198. } AudioState;
  199. extern const struct mixeng_volume nominal_volume;
  200. extern const char *audio_prio_list[];
  201. void audio_driver_register(audio_driver *drv);
  202. audio_driver *audio_driver_lookup(const char *name);
  203. void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
  204. void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
  205. int audio_bug (const char *funcname, int cond);
  206. void *audio_calloc (const char *funcname, int nmemb, size_t size);
  207. void audio_run(AudioState *s, const char *msg);
  208. typedef struct RateCtl {
  209. int64_t start_ticks;
  210. int64_t bytes_sent;
  211. } RateCtl;
  212. void audio_rate_start(RateCtl *rate);
  213. size_t audio_rate_get_bytes(struct audio_pcm_info *info, RateCtl *rate,
  214. size_t bytes_avail);
  215. static inline size_t audio_ring_dist(size_t dst, size_t src, size_t len)
  216. {
  217. return (dst >= src) ? (dst - src) : (len - src + dst);
  218. }
  219. #define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
  220. #ifdef DEBUG
  221. #define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
  222. #else
  223. #define ldebug(fmt, ...) (void)0
  224. #endif
  225. #define AUDIO_STRINGIFY_(n) #n
  226. #define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
  227. typedef struct AudiodevListEntry {
  228. Audiodev *dev;
  229. QSIMPLEQ_ENTRY(AudiodevListEntry) next;
  230. } AudiodevListEntry;
  231. typedef QSIMPLEQ_HEAD(, AudiodevListEntry) AudiodevListHead;
  232. AudiodevListHead audio_handle_legacy_opts(void);
  233. void audio_free_audiodev_list(AudiodevListHead *head);
  234. void audio_create_pdos(Audiodev *dev);
  235. AudiodevPerDirectionOptions *audio_get_pdo_in(Audiodev *dev);
  236. AudiodevPerDirectionOptions *audio_get_pdo_out(Audiodev *dev);
  237. #endif /* QEMU_AUDIO_INT_H */