xref: /qemu/audio/audio.h (revision f917eed3)
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 
25 #ifndef QEMU_AUDIO_H
26 #define QEMU_AUDIO_H
27 
28 #include "qemu/queue.h"
29 #include "qapi/qapi-types-audio.h"
30 #include "hw/qdev-properties.h"
31 
32 typedef void (*audio_callback_fn) (void *opaque, int avail);
33 
34 #ifdef HOST_WORDS_BIGENDIAN
35 #define AUDIO_HOST_ENDIANNESS 1
36 #else
37 #define AUDIO_HOST_ENDIANNESS 0
38 #endif
39 
40 typedef struct audsettings {
41     int freq;
42     int nchannels;
43     AudioFormat fmt;
44     int endianness;
45 } audsettings;
46 
47 audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo);
48 int audioformat_bytes_per_sample(AudioFormat fmt);
49 int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
50                         audsettings *as, int def_usecs);
51 int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
52                          audsettings *as, int def_usecs);
53 int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
54                        audsettings *as, int def_usecs);
55 
56 typedef enum {
57     AUD_CNOTIFY_ENABLE,
58     AUD_CNOTIFY_DISABLE
59 } audcnotification_e;
60 
61 struct audio_capture_ops {
62     void (*notify) (void *opaque, audcnotification_e cmd);
63     void (*capture) (void *opaque, const void *buf, int size);
64     void (*destroy) (void *opaque);
65 };
66 
67 struct capture_ops {
68     void (*info) (void *opaque);
69     void (*destroy) (void *opaque);
70 };
71 
72 typedef struct CaptureState {
73     void *opaque;
74     struct capture_ops ops;
75     QLIST_ENTRY (CaptureState) entries;
76 } CaptureState;
77 
78 typedef struct SWVoiceOut SWVoiceOut;
79 typedef struct CaptureVoiceOut CaptureVoiceOut;
80 typedef struct SWVoiceIn SWVoiceIn;
81 
82 typedef struct AudioState AudioState;
83 typedef struct QEMUSoundCard {
84     char *name;
85     AudioState *state;
86     QLIST_ENTRY (QEMUSoundCard) entries;
87 } QEMUSoundCard;
88 
89 typedef struct QEMUAudioTimeStamp {
90     uint64_t old_ts;
91 } QEMUAudioTimeStamp;
92 
93 void AUD_vlog (const char *cap, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
94 void AUD_log (const char *cap, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
95 
96 void AUD_register_card (const char *name, QEMUSoundCard *card);
97 void AUD_remove_card (QEMUSoundCard *card);
98 CaptureVoiceOut *AUD_add_capture(
99     AudioState *s,
100     struct audsettings *as,
101     struct audio_capture_ops *ops,
102     void *opaque
103     );
104 void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque);
105 
106 SWVoiceOut *AUD_open_out (
107     QEMUSoundCard *card,
108     SWVoiceOut *sw,
109     const char *name,
110     void *callback_opaque,
111     audio_callback_fn callback_fn,
112     struct audsettings *settings
113     );
114 
115 void AUD_close_out (QEMUSoundCard *card, SWVoiceOut *sw);
116 size_t AUD_write (SWVoiceOut *sw, void *pcm_buf, size_t size);
117 int  AUD_get_buffer_size_out (SWVoiceOut *sw);
118 void AUD_set_active_out (SWVoiceOut *sw, int on);
119 int  AUD_is_active_out (SWVoiceOut *sw);
120 
121 void     AUD_init_time_stamp_out (SWVoiceOut *sw, QEMUAudioTimeStamp *ts);
122 uint64_t AUD_get_elapsed_usec_out (SWVoiceOut *sw, QEMUAudioTimeStamp *ts);
123 
124 void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol);
125 void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol);
126 
127 #define AUDIO_MAX_CHANNELS 16
128 typedef struct Volume {
129     bool mute;
130     int channels;
131     uint8_t vol[AUDIO_MAX_CHANNELS];
132 } Volume;
133 
134 void audio_set_volume_out(SWVoiceOut *sw, Volume *vol);
135 void audio_set_volume_in(SWVoiceIn *sw, Volume *vol);
136 
137 SWVoiceIn *AUD_open_in (
138     QEMUSoundCard *card,
139     SWVoiceIn *sw,
140     const char *name,
141     void *callback_opaque,
142     audio_callback_fn callback_fn,
143     struct audsettings *settings
144     );
145 
146 void AUD_close_in (QEMUSoundCard *card, SWVoiceIn *sw);
147 size_t AUD_read (SWVoiceIn *sw, void *pcm_buf, size_t size);
148 void AUD_set_active_in (SWVoiceIn *sw, int on);
149 int  AUD_is_active_in (SWVoiceIn *sw);
150 
151 void     AUD_init_time_stamp_in (SWVoiceIn *sw, QEMUAudioTimeStamp *ts);
152 uint64_t AUD_get_elapsed_usec_in (SWVoiceIn *sw, QEMUAudioTimeStamp *ts);
153 
154 static inline void *advance (void *p, int incr)
155 {
156     uint8_t *d = p;
157     return (d + incr);
158 }
159 
160 int wav_start_capture(AudioState *state, CaptureState *s, const char *path,
161                       int freq, int bits, int nchannels);
162 
163 void audio_cleanup(void);
164 
165 void audio_sample_to_uint64(const void *samples, int pos,
166                             uint64_t *left, uint64_t *right);
167 void audio_sample_from_uint64(void *samples, int pos,
168                             uint64_t left, uint64_t right);
169 
170 void audio_parse_option(const char *opt);
171 void audio_init_audiodevs(void);
172 void audio_legacy_help(void);
173 
174 AudioState *audio_state_by_name(const char *name);
175 const char *audio_get_id(QEMUSoundCard *card);
176 
177 #define DEFINE_AUDIO_PROPERTIES(_s, _f)         \
178     DEFINE_PROP_AUDIODEV("audiodev", _s, _f)
179 
180 #endif /* QEMU_AUDIO_H */
181