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