audio_int.h 8.0 KB

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