root / audio / esdaudio.c @ e6bb7d7e
History | View | Annotate | Download (14.3 kB)
1 | ca9cc28c | balrog | /*
|
---|---|---|---|
2 | ca9cc28c | balrog | * QEMU ESD audio driver
|
3 | ca9cc28c | balrog | *
|
4 | ca9cc28c | balrog | * Copyright (c) 2006 Frederick Reeve (brushed up by malc)
|
5 | ca9cc28c | balrog | *
|
6 | ca9cc28c | balrog | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ca9cc28c | balrog | * of this software and associated documentation files (the "Software"), to deal
|
8 | ca9cc28c | balrog | * in the Software without restriction, including without limitation the rights
|
9 | ca9cc28c | balrog | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ca9cc28c | balrog | * copies of the Software, and to permit persons to whom the Software is
|
11 | ca9cc28c | balrog | * furnished to do so, subject to the following conditions:
|
12 | ca9cc28c | balrog | *
|
13 | ca9cc28c | balrog | * The above copyright notice and this permission notice shall be included in
|
14 | ca9cc28c | balrog | * all copies or substantial portions of the Software.
|
15 | ca9cc28c | balrog | *
|
16 | ca9cc28c | balrog | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ca9cc28c | balrog | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ca9cc28c | balrog | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ca9cc28c | balrog | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ca9cc28c | balrog | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ca9cc28c | balrog | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ca9cc28c | balrog | * THE SOFTWARE.
|
23 | ca9cc28c | balrog | */
|
24 | ca9cc28c | balrog | #include <esd.h> |
25 | ca9cc28c | balrog | #include "qemu-common.h" |
26 | ca9cc28c | balrog | #include "audio.h" |
27 | ca9cc28c | balrog | #include <signal.h> |
28 | ca9cc28c | balrog | |
29 | ca9cc28c | balrog | #define AUDIO_CAP "esd" |
30 | ca9cc28c | balrog | #include "audio_int.h" |
31 | ca9cc28c | balrog | #include "audio_pt_int.h" |
32 | ca9cc28c | balrog | |
33 | ca9cc28c | balrog | typedef struct { |
34 | ca9cc28c | balrog | HWVoiceOut hw; |
35 | ca9cc28c | balrog | int done;
|
36 | ca9cc28c | balrog | int live;
|
37 | ca9cc28c | balrog | int decr;
|
38 | ca9cc28c | balrog | int rpos;
|
39 | ca9cc28c | balrog | void *pcm_buf;
|
40 | ca9cc28c | balrog | int fd;
|
41 | ca9cc28c | balrog | struct audio_pt pt;
|
42 | ca9cc28c | balrog | } ESDVoiceOut; |
43 | ca9cc28c | balrog | |
44 | ca9cc28c | balrog | typedef struct { |
45 | ca9cc28c | balrog | HWVoiceIn hw; |
46 | ca9cc28c | balrog | int done;
|
47 | ca9cc28c | balrog | int dead;
|
48 | ca9cc28c | balrog | int incr;
|
49 | ca9cc28c | balrog | int wpos;
|
50 | ca9cc28c | balrog | void *pcm_buf;
|
51 | ca9cc28c | balrog | int fd;
|
52 | ca9cc28c | balrog | struct audio_pt pt;
|
53 | ca9cc28c | balrog | } ESDVoiceIn; |
54 | ca9cc28c | balrog | |
55 | ca9cc28c | balrog | static struct { |
56 | ca9cc28c | balrog | int samples;
|
57 | ca9cc28c | balrog | int divisor;
|
58 | ca9cc28c | balrog | char *dac_host;
|
59 | ca9cc28c | balrog | char *adc_host;
|
60 | ca9cc28c | balrog | } conf = { |
61 | ca9cc28c | balrog | 1024,
|
62 | ca9cc28c | balrog | 2,
|
63 | ca9cc28c | balrog | NULL,
|
64 | ca9cc28c | balrog | NULL
|
65 | ca9cc28c | balrog | }; |
66 | ca9cc28c | balrog | |
67 | ca9cc28c | balrog | static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...) |
68 | ca9cc28c | balrog | { |
69 | ca9cc28c | balrog | va_list ap; |
70 | ca9cc28c | balrog | |
71 | ca9cc28c | balrog | va_start (ap, fmt); |
72 | ca9cc28c | balrog | AUD_vlog (AUDIO_CAP, fmt, ap); |
73 | ca9cc28c | balrog | va_end (ap); |
74 | ca9cc28c | balrog | |
75 | ca9cc28c | balrog | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
|
76 | ca9cc28c | balrog | } |
77 | ca9cc28c | balrog | |
78 | ca9cc28c | balrog | /* playback */
|
79 | ca9cc28c | balrog | static void *qesd_thread_out (void *arg) |
80 | ca9cc28c | balrog | { |
81 | ca9cc28c | balrog | ESDVoiceOut *esd = arg; |
82 | ca9cc28c | balrog | HWVoiceOut *hw = &esd->hw; |
83 | ca9cc28c | balrog | int threshold;
|
84 | ca9cc28c | balrog | |
85 | ca9cc28c | balrog | threshold = conf.divisor ? hw->samples / conf.divisor : 0;
|
86 | ca9cc28c | balrog | |
87 | deb9d2ee | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
88 | deb9d2ee | balrog | return NULL; |
89 | deb9d2ee | balrog | } |
90 | deb9d2ee | balrog | |
91 | ca9cc28c | balrog | for (;;) {
|
92 | ca9cc28c | balrog | int decr, to_mix, rpos;
|
93 | ca9cc28c | balrog | |
94 | ca9cc28c | balrog | for (;;) {
|
95 | ca9cc28c | balrog | if (esd->done) {
|
96 | ca9cc28c | balrog | goto exit;
|
97 | ca9cc28c | balrog | } |
98 | ca9cc28c | balrog | |
99 | ca9cc28c | balrog | if (esd->live > threshold) {
|
100 | ca9cc28c | balrog | break;
|
101 | ca9cc28c | balrog | } |
102 | ca9cc28c | balrog | |
103 | ca9cc28c | balrog | if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
|
104 | ca9cc28c | balrog | goto exit;
|
105 | ca9cc28c | balrog | } |
106 | ca9cc28c | balrog | } |
107 | ca9cc28c | balrog | |
108 | ca9cc28c | balrog | decr = to_mix = esd->live; |
109 | ca9cc28c | balrog | rpos = hw->rpos; |
110 | ca9cc28c | balrog | |
111 | ca9cc28c | balrog | if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
|
112 | ca9cc28c | balrog | return NULL; |
113 | ca9cc28c | balrog | } |
114 | ca9cc28c | balrog | |
115 | ca9cc28c | balrog | while (to_mix) {
|
116 | ca9cc28c | balrog | ssize_t written; |
117 | ca9cc28c | balrog | int chunk = audio_MIN (to_mix, hw->samples - rpos);
|
118 | ca9cc28c | balrog | st_sample_t *src = hw->mix_buf + rpos; |
119 | ca9cc28c | balrog | |
120 | ca9cc28c | balrog | hw->clip (esd->pcm_buf, src, chunk); |
121 | ca9cc28c | balrog | |
122 | ca9cc28c | balrog | again:
|
123 | ca9cc28c | balrog | written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift); |
124 | ca9cc28c | balrog | if (written == -1) { |
125 | ca9cc28c | balrog | if (errno == EINTR || errno == EAGAIN) {
|
126 | ca9cc28c | balrog | goto again;
|
127 | ca9cc28c | balrog | } |
128 | ca9cc28c | balrog | qesd_logerr (errno, "write failed\n");
|
129 | ca9cc28c | balrog | return NULL; |
130 | ca9cc28c | balrog | } |
131 | ca9cc28c | balrog | |
132 | ca9cc28c | balrog | if (written != chunk << hw->info.shift) {
|
133 | ca9cc28c | balrog | int wsamples = written >> hw->info.shift;
|
134 | ca9cc28c | balrog | int wbytes = wsamples << hw->info.shift;
|
135 | ca9cc28c | balrog | if (wbytes != written) {
|
136 | ca9cc28c | balrog | dolog ("warning: Misaligned write %d (requested %d), "
|
137 | ca9cc28c | balrog | "alignment %d\n",
|
138 | ca9cc28c | balrog | wbytes, written, hw->info.align + 1);
|
139 | ca9cc28c | balrog | } |
140 | ca9cc28c | balrog | to_mix -= wsamples; |
141 | ca9cc28c | balrog | rpos = (rpos + wsamples) % hw->samples; |
142 | ca9cc28c | balrog | break;
|
143 | ca9cc28c | balrog | } |
144 | ca9cc28c | balrog | |
145 | ca9cc28c | balrog | rpos = (rpos + chunk) % hw->samples; |
146 | ca9cc28c | balrog | to_mix -= chunk; |
147 | ca9cc28c | balrog | } |
148 | ca9cc28c | balrog | |
149 | ca9cc28c | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
150 | ca9cc28c | balrog | return NULL; |
151 | ca9cc28c | balrog | } |
152 | ca9cc28c | balrog | |
153 | ca9cc28c | balrog | esd->rpos = rpos; |
154 | ca9cc28c | balrog | esd->live -= decr; |
155 | ca9cc28c | balrog | esd->decr += decr; |
156 | ca9cc28c | balrog | } |
157 | ca9cc28c | balrog | |
158 | ca9cc28c | balrog | exit:
|
159 | ca9cc28c | balrog | audio_pt_unlock (&esd->pt, AUDIO_FUNC); |
160 | ca9cc28c | balrog | return NULL; |
161 | ca9cc28c | balrog | } |
162 | ca9cc28c | balrog | |
163 | ca9cc28c | balrog | static int qesd_run_out (HWVoiceOut *hw) |
164 | ca9cc28c | balrog | { |
165 | ca9cc28c | balrog | int live, decr;
|
166 | ca9cc28c | balrog | ESDVoiceOut *esd = (ESDVoiceOut *) hw; |
167 | ca9cc28c | balrog | |
168 | ca9cc28c | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
169 | ca9cc28c | balrog | return 0; |
170 | ca9cc28c | balrog | } |
171 | ca9cc28c | balrog | |
172 | ca9cc28c | balrog | live = audio_pcm_hw_get_live_out (hw); |
173 | ca9cc28c | balrog | decr = audio_MIN (live, esd->decr); |
174 | ca9cc28c | balrog | esd->decr -= decr; |
175 | ca9cc28c | balrog | esd->live = live - decr; |
176 | ca9cc28c | balrog | hw->rpos = esd->rpos; |
177 | ca9cc28c | balrog | if (esd->live > 0) { |
178 | ca9cc28c | balrog | audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC); |
179 | ca9cc28c | balrog | } |
180 | ca9cc28c | balrog | else {
|
181 | ca9cc28c | balrog | audio_pt_unlock (&esd->pt, AUDIO_FUNC); |
182 | ca9cc28c | balrog | } |
183 | ca9cc28c | balrog | return decr;
|
184 | ca9cc28c | balrog | } |
185 | ca9cc28c | balrog | |
186 | ca9cc28c | balrog | static int qesd_write (SWVoiceOut *sw, void *buf, int len) |
187 | ca9cc28c | balrog | { |
188 | ca9cc28c | balrog | return audio_pcm_sw_write (sw, buf, len);
|
189 | ca9cc28c | balrog | } |
190 | ca9cc28c | balrog | |
191 | ca9cc28c | balrog | static int qesd_init_out (HWVoiceOut *hw, audsettings_t *as) |
192 | ca9cc28c | balrog | { |
193 | ca9cc28c | balrog | ESDVoiceOut *esd = (ESDVoiceOut *) hw; |
194 | ca9cc28c | balrog | audsettings_t obt_as = *as; |
195 | ca9cc28c | balrog | int esdfmt = ESD_STREAM | ESD_PLAY;
|
196 | ca9cc28c | balrog | int err;
|
197 | ca9cc28c | balrog | sigset_t set, old_set; |
198 | ca9cc28c | balrog | |
199 | ca9cc28c | balrog | sigfillset (&set); |
200 | ca9cc28c | balrog | |
201 | ca9cc28c | balrog | esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
|
202 | ca9cc28c | balrog | switch (as->fmt) {
|
203 | ca9cc28c | balrog | case AUD_FMT_S8:
|
204 | ca9cc28c | balrog | case AUD_FMT_U8:
|
205 | ca9cc28c | balrog | esdfmt |= ESD_BITS8; |
206 | ca9cc28c | balrog | obt_as.fmt = AUD_FMT_U8; |
207 | ca9cc28c | balrog | break;
|
208 | ca9cc28c | balrog | |
209 | ca9cc28c | balrog | case AUD_FMT_S32:
|
210 | ca9cc28c | balrog | case AUD_FMT_U32:
|
211 | ca9cc28c | balrog | dolog ("Will use 16 instead of 32 bit samples\n");
|
212 | ca9cc28c | balrog | |
213 | ca9cc28c | balrog | case AUD_FMT_S16:
|
214 | ca9cc28c | balrog | case AUD_FMT_U16:
|
215 | ca9cc28c | balrog | deffmt:
|
216 | ca9cc28c | balrog | esdfmt |= ESD_BITS16; |
217 | ca9cc28c | balrog | obt_as.fmt = AUD_FMT_S16; |
218 | ca9cc28c | balrog | break;
|
219 | ca9cc28c | balrog | |
220 | ca9cc28c | balrog | default:
|
221 | ca9cc28c | balrog | dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
|
222 | ca9cc28c | balrog | goto deffmt;
|
223 | ca9cc28c | balrog | |
224 | ca9cc28c | balrog | } |
225 | deb9d2ee | balrog | obt_as.endianness = AUDIO_HOST_ENDIANNESS; |
226 | ca9cc28c | balrog | |
227 | ca9cc28c | balrog | audio_pcm_init_info (&hw->info, &obt_as); |
228 | ca9cc28c | balrog | |
229 | ca9cc28c | balrog | hw->samples = conf.samples; |
230 | ca9cc28c | balrog | esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
231 | ca9cc28c | balrog | if (!esd->pcm_buf) {
|
232 | ca9cc28c | balrog | dolog ("Could not allocate buffer (%d bytes)\n",
|
233 | ca9cc28c | balrog | hw->samples << hw->info.shift); |
234 | ca9cc28c | balrog | return -1; |
235 | ca9cc28c | balrog | } |
236 | ca9cc28c | balrog | |
237 | ca9cc28c | balrog | esd->fd = -1;
|
238 | ca9cc28c | balrog | err = pthread_sigmask (SIG_BLOCK, &set, &old_set); |
239 | ca9cc28c | balrog | if (err) {
|
240 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask failed\n");
|
241 | ca9cc28c | balrog | goto fail1;
|
242 | ca9cc28c | balrog | } |
243 | ca9cc28c | balrog | |
244 | ca9cc28c | balrog | esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
|
245 | ca9cc28c | balrog | if (esd->fd < 0) { |
246 | ca9cc28c | balrog | qesd_logerr (errno, "esd_play_stream failed\n");
|
247 | ca9cc28c | balrog | goto fail2;
|
248 | ca9cc28c | balrog | } |
249 | ca9cc28c | balrog | |
250 | ca9cc28c | balrog | if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
|
251 | ca9cc28c | balrog | goto fail3;
|
252 | ca9cc28c | balrog | } |
253 | ca9cc28c | balrog | |
254 | ca9cc28c | balrog | err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
|
255 | ca9cc28c | balrog | if (err) {
|
256 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask(restore) failed\n");
|
257 | ca9cc28c | balrog | } |
258 | ca9cc28c | balrog | |
259 | ca9cc28c | balrog | return 0; |
260 | ca9cc28c | balrog | |
261 | ca9cc28c | balrog | fail3:
|
262 | ca9cc28c | balrog | if (close (esd->fd)) {
|
263 | ca9cc28c | balrog | qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
|
264 | ca9cc28c | balrog | AUDIO_FUNC, esd->fd); |
265 | ca9cc28c | balrog | } |
266 | ca9cc28c | balrog | esd->fd = -1;
|
267 | ca9cc28c | balrog | |
268 | ca9cc28c | balrog | fail2:
|
269 | ca9cc28c | balrog | err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
|
270 | ca9cc28c | balrog | if (err) {
|
271 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask(restore) failed\n");
|
272 | ca9cc28c | balrog | } |
273 | ca9cc28c | balrog | |
274 | ca9cc28c | balrog | fail1:
|
275 | ca9cc28c | balrog | qemu_free (esd->pcm_buf); |
276 | ca9cc28c | balrog | esd->pcm_buf = NULL;
|
277 | ca9cc28c | balrog | return -1; |
278 | ca9cc28c | balrog | } |
279 | ca9cc28c | balrog | |
280 | ca9cc28c | balrog | static void qesd_fini_out (HWVoiceOut *hw) |
281 | ca9cc28c | balrog | { |
282 | ca9cc28c | balrog | void *ret;
|
283 | ca9cc28c | balrog | ESDVoiceOut *esd = (ESDVoiceOut *) hw; |
284 | ca9cc28c | balrog | |
285 | ca9cc28c | balrog | audio_pt_lock (&esd->pt, AUDIO_FUNC); |
286 | ca9cc28c | balrog | esd->done = 1;
|
287 | ca9cc28c | balrog | audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC); |
288 | ca9cc28c | balrog | audio_pt_join (&esd->pt, &ret, AUDIO_FUNC); |
289 | ca9cc28c | balrog | |
290 | ca9cc28c | balrog | if (esd->fd >= 0) { |
291 | ca9cc28c | balrog | if (close (esd->fd)) {
|
292 | ca9cc28c | balrog | qesd_logerr (errno, "failed to close esd socket\n");
|
293 | ca9cc28c | balrog | } |
294 | ca9cc28c | balrog | esd->fd = -1;
|
295 | ca9cc28c | balrog | } |
296 | ca9cc28c | balrog | |
297 | ca9cc28c | balrog | audio_pt_fini (&esd->pt, AUDIO_FUNC); |
298 | ca9cc28c | balrog | |
299 | ca9cc28c | balrog | qemu_free (esd->pcm_buf); |
300 | ca9cc28c | balrog | esd->pcm_buf = NULL;
|
301 | ca9cc28c | balrog | } |
302 | ca9cc28c | balrog | |
303 | ca9cc28c | balrog | static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...) |
304 | ca9cc28c | balrog | { |
305 | ca9cc28c | balrog | (void) hw;
|
306 | ca9cc28c | balrog | (void) cmd;
|
307 | ca9cc28c | balrog | return 0; |
308 | ca9cc28c | balrog | } |
309 | ca9cc28c | balrog | |
310 | ca9cc28c | balrog | /* capture */
|
311 | ca9cc28c | balrog | static void *qesd_thread_in (void *arg) |
312 | ca9cc28c | balrog | { |
313 | ca9cc28c | balrog | ESDVoiceIn *esd = arg; |
314 | ca9cc28c | balrog | HWVoiceIn *hw = &esd->hw; |
315 | ca9cc28c | balrog | int threshold;
|
316 | ca9cc28c | balrog | |
317 | ca9cc28c | balrog | threshold = conf.divisor ? hw->samples / conf.divisor : 0;
|
318 | ca9cc28c | balrog | |
319 | deb9d2ee | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
320 | deb9d2ee | balrog | return NULL; |
321 | deb9d2ee | balrog | } |
322 | deb9d2ee | balrog | |
323 | ca9cc28c | balrog | for (;;) {
|
324 | ca9cc28c | balrog | int incr, to_grab, wpos;
|
325 | ca9cc28c | balrog | |
326 | ca9cc28c | balrog | for (;;) {
|
327 | ca9cc28c | balrog | if (esd->done) {
|
328 | ca9cc28c | balrog | goto exit;
|
329 | ca9cc28c | balrog | } |
330 | ca9cc28c | balrog | |
331 | ca9cc28c | balrog | if (esd->dead > threshold) {
|
332 | ca9cc28c | balrog | break;
|
333 | ca9cc28c | balrog | } |
334 | ca9cc28c | balrog | |
335 | ca9cc28c | balrog | if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
|
336 | ca9cc28c | balrog | goto exit;
|
337 | ca9cc28c | balrog | } |
338 | ca9cc28c | balrog | } |
339 | ca9cc28c | balrog | |
340 | ca9cc28c | balrog | incr = to_grab = esd->dead; |
341 | ca9cc28c | balrog | wpos = hw->wpos; |
342 | ca9cc28c | balrog | |
343 | ca9cc28c | balrog | if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
|
344 | ca9cc28c | balrog | return NULL; |
345 | ca9cc28c | balrog | } |
346 | ca9cc28c | balrog | |
347 | ca9cc28c | balrog | while (to_grab) {
|
348 | ca9cc28c | balrog | ssize_t nread; |
349 | ca9cc28c | balrog | int chunk = audio_MIN (to_grab, hw->samples - wpos);
|
350 | ca9cc28c | balrog | void *buf = advance (esd->pcm_buf, wpos);
|
351 | ca9cc28c | balrog | |
352 | ca9cc28c | balrog | again:
|
353 | ca9cc28c | balrog | nread = read (esd->fd, buf, chunk << hw->info.shift); |
354 | ca9cc28c | balrog | if (nread == -1) { |
355 | ca9cc28c | balrog | if (errno == EINTR || errno == EAGAIN) {
|
356 | ca9cc28c | balrog | goto again;
|
357 | ca9cc28c | balrog | } |
358 | ca9cc28c | balrog | qesd_logerr (errno, "read failed\n");
|
359 | ca9cc28c | balrog | return NULL; |
360 | ca9cc28c | balrog | } |
361 | ca9cc28c | balrog | |
362 | ca9cc28c | balrog | if (nread != chunk << hw->info.shift) {
|
363 | ca9cc28c | balrog | int rsamples = nread >> hw->info.shift;
|
364 | ca9cc28c | balrog | int rbytes = rsamples << hw->info.shift;
|
365 | ca9cc28c | balrog | if (rbytes != nread) {
|
366 | ca9cc28c | balrog | dolog ("warning: Misaligned write %d (requested %d), "
|
367 | ca9cc28c | balrog | "alignment %d\n",
|
368 | ca9cc28c | balrog | rbytes, nread, hw->info.align + 1);
|
369 | ca9cc28c | balrog | } |
370 | ca9cc28c | balrog | to_grab -= rsamples; |
371 | ca9cc28c | balrog | wpos = (wpos + rsamples) % hw->samples; |
372 | ca9cc28c | balrog | break;
|
373 | ca9cc28c | balrog | } |
374 | ca9cc28c | balrog | |
375 | ca9cc28c | balrog | hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift, |
376 | ca9cc28c | balrog | &nominal_volume); |
377 | ca9cc28c | balrog | wpos = (wpos + chunk) % hw->samples; |
378 | ca9cc28c | balrog | to_grab -= chunk; |
379 | ca9cc28c | balrog | } |
380 | ca9cc28c | balrog | |
381 | ca9cc28c | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
382 | ca9cc28c | balrog | return NULL; |
383 | ca9cc28c | balrog | } |
384 | ca9cc28c | balrog | |
385 | ca9cc28c | balrog | esd->wpos = wpos; |
386 | ca9cc28c | balrog | esd->dead -= incr; |
387 | ca9cc28c | balrog | esd->incr += incr; |
388 | ca9cc28c | balrog | } |
389 | ca9cc28c | balrog | |
390 | ca9cc28c | balrog | exit:
|
391 | ca9cc28c | balrog | audio_pt_unlock (&esd->pt, AUDIO_FUNC); |
392 | ca9cc28c | balrog | return NULL; |
393 | ca9cc28c | balrog | } |
394 | ca9cc28c | balrog | |
395 | ca9cc28c | balrog | static int qesd_run_in (HWVoiceIn *hw) |
396 | ca9cc28c | balrog | { |
397 | ca9cc28c | balrog | int live, incr, dead;
|
398 | ca9cc28c | balrog | ESDVoiceIn *esd = (ESDVoiceIn *) hw; |
399 | ca9cc28c | balrog | |
400 | ca9cc28c | balrog | if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
|
401 | ca9cc28c | balrog | return 0; |
402 | ca9cc28c | balrog | } |
403 | ca9cc28c | balrog | |
404 | ca9cc28c | balrog | live = audio_pcm_hw_get_live_in (hw); |
405 | ca9cc28c | balrog | dead = hw->samples - live; |
406 | ca9cc28c | balrog | incr = audio_MIN (dead, esd->incr); |
407 | ca9cc28c | balrog | esd->incr -= incr; |
408 | ca9cc28c | balrog | esd->dead = dead - incr; |
409 | ca9cc28c | balrog | hw->wpos = esd->wpos; |
410 | ca9cc28c | balrog | if (esd->dead > 0) { |
411 | ca9cc28c | balrog | audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC); |
412 | ca9cc28c | balrog | } |
413 | ca9cc28c | balrog | else {
|
414 | ca9cc28c | balrog | audio_pt_unlock (&esd->pt, AUDIO_FUNC); |
415 | ca9cc28c | balrog | } |
416 | ca9cc28c | balrog | return incr;
|
417 | ca9cc28c | balrog | } |
418 | ca9cc28c | balrog | |
419 | ca9cc28c | balrog | static int qesd_read (SWVoiceIn *sw, void *buf, int len) |
420 | ca9cc28c | balrog | { |
421 | ca9cc28c | balrog | return audio_pcm_sw_read (sw, buf, len);
|
422 | ca9cc28c | balrog | } |
423 | ca9cc28c | balrog | |
424 | ca9cc28c | balrog | static int qesd_init_in (HWVoiceIn *hw, audsettings_t *as) |
425 | ca9cc28c | balrog | { |
426 | ca9cc28c | balrog | ESDVoiceIn *esd = (ESDVoiceIn *) hw; |
427 | ca9cc28c | balrog | audsettings_t obt_as = *as; |
428 | ca9cc28c | balrog | int esdfmt = ESD_STREAM | ESD_RECORD;
|
429 | ca9cc28c | balrog | int err;
|
430 | ca9cc28c | balrog | sigset_t set, old_set; |
431 | ca9cc28c | balrog | |
432 | ca9cc28c | balrog | sigfillset (&set); |
433 | ca9cc28c | balrog | |
434 | ca9cc28c | balrog | esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
|
435 | ca9cc28c | balrog | switch (as->fmt) {
|
436 | ca9cc28c | balrog | case AUD_FMT_S8:
|
437 | ca9cc28c | balrog | case AUD_FMT_U8:
|
438 | ca9cc28c | balrog | esdfmt |= ESD_BITS8; |
439 | ca9cc28c | balrog | obt_as.fmt = AUD_FMT_U8; |
440 | ca9cc28c | balrog | break;
|
441 | ca9cc28c | balrog | |
442 | ca9cc28c | balrog | case AUD_FMT_S16:
|
443 | ca9cc28c | balrog | case AUD_FMT_U16:
|
444 | ca9cc28c | balrog | esdfmt |= ESD_BITS16; |
445 | ca9cc28c | balrog | obt_as.fmt = AUD_FMT_S16; |
446 | ca9cc28c | balrog | break;
|
447 | ca9cc28c | balrog | |
448 | ca9cc28c | balrog | case AUD_FMT_S32:
|
449 | ca9cc28c | balrog | case AUD_FMT_U32:
|
450 | ca9cc28c | balrog | dolog ("Will use 16 instead of 32 bit samples\n");
|
451 | ca9cc28c | balrog | esdfmt |= ESD_BITS16; |
452 | ca9cc28c | balrog | obt_as.fmt = AUD_FMT_S16; |
453 | ca9cc28c | balrog | break;
|
454 | ca9cc28c | balrog | } |
455 | deb9d2ee | balrog | obt_as.endianness = AUDIO_HOST_ENDIANNESS; |
456 | ca9cc28c | balrog | |
457 | ca9cc28c | balrog | audio_pcm_init_info (&hw->info, &obt_as); |
458 | ca9cc28c | balrog | |
459 | ca9cc28c | balrog | hw->samples = conf.samples; |
460 | ca9cc28c | balrog | esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
461 | ca9cc28c | balrog | if (!esd->pcm_buf) {
|
462 | ca9cc28c | balrog | dolog ("Could not allocate buffer (%d bytes)\n",
|
463 | ca9cc28c | balrog | hw->samples << hw->info.shift); |
464 | ca9cc28c | balrog | return -1; |
465 | ca9cc28c | balrog | } |
466 | ca9cc28c | balrog | |
467 | ca9cc28c | balrog | esd->fd = -1;
|
468 | ca9cc28c | balrog | |
469 | ca9cc28c | balrog | err = pthread_sigmask (SIG_BLOCK, &set, &old_set); |
470 | ca9cc28c | balrog | if (err) {
|
471 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask failed\n");
|
472 | ca9cc28c | balrog | goto fail1;
|
473 | ca9cc28c | balrog | } |
474 | ca9cc28c | balrog | |
475 | ca9cc28c | balrog | esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
|
476 | ca9cc28c | balrog | if (esd->fd < 0) { |
477 | ca9cc28c | balrog | qesd_logerr (errno, "esd_record_stream failed\n");
|
478 | ca9cc28c | balrog | goto fail2;
|
479 | ca9cc28c | balrog | } |
480 | ca9cc28c | balrog | |
481 | ca9cc28c | balrog | if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
|
482 | ca9cc28c | balrog | goto fail3;
|
483 | ca9cc28c | balrog | } |
484 | ca9cc28c | balrog | |
485 | ca9cc28c | balrog | err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
|
486 | ca9cc28c | balrog | if (err) {
|
487 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask(restore) failed\n");
|
488 | ca9cc28c | balrog | } |
489 | ca9cc28c | balrog | |
490 | ca9cc28c | balrog | return 0; |
491 | ca9cc28c | balrog | |
492 | ca9cc28c | balrog | fail3:
|
493 | ca9cc28c | balrog | if (close (esd->fd)) {
|
494 | ca9cc28c | balrog | qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
|
495 | ca9cc28c | balrog | AUDIO_FUNC, esd->fd); |
496 | ca9cc28c | balrog | } |
497 | ca9cc28c | balrog | esd->fd = -1;
|
498 | ca9cc28c | balrog | |
499 | ca9cc28c | balrog | fail2:
|
500 | ca9cc28c | balrog | err = pthread_sigmask (SIG_SETMASK, &old_set, NULL);
|
501 | ca9cc28c | balrog | if (err) {
|
502 | ca9cc28c | balrog | qesd_logerr (err, "pthread_sigmask(restore) failed\n");
|
503 | ca9cc28c | balrog | } |
504 | ca9cc28c | balrog | |
505 | ca9cc28c | balrog | fail1:
|
506 | ca9cc28c | balrog | qemu_free (esd->pcm_buf); |
507 | ca9cc28c | balrog | esd->pcm_buf = NULL;
|
508 | ca9cc28c | balrog | return -1; |
509 | ca9cc28c | balrog | } |
510 | ca9cc28c | balrog | |
511 | ca9cc28c | balrog | static void qesd_fini_in (HWVoiceIn *hw) |
512 | ca9cc28c | balrog | { |
513 | ca9cc28c | balrog | void *ret;
|
514 | ca9cc28c | balrog | ESDVoiceIn *esd = (ESDVoiceIn *) hw; |
515 | ca9cc28c | balrog | |
516 | ca9cc28c | balrog | audio_pt_lock (&esd->pt, AUDIO_FUNC); |
517 | ca9cc28c | balrog | esd->done = 1;
|
518 | ca9cc28c | balrog | audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC); |
519 | ca9cc28c | balrog | audio_pt_join (&esd->pt, &ret, AUDIO_FUNC); |
520 | ca9cc28c | balrog | |
521 | ca9cc28c | balrog | if (esd->fd >= 0) { |
522 | ca9cc28c | balrog | if (close (esd->fd)) {
|
523 | ca9cc28c | balrog | qesd_logerr (errno, "failed to close esd socket\n");
|
524 | ca9cc28c | balrog | } |
525 | ca9cc28c | balrog | esd->fd = -1;
|
526 | ca9cc28c | balrog | } |
527 | ca9cc28c | balrog | |
528 | ca9cc28c | balrog | audio_pt_fini (&esd->pt, AUDIO_FUNC); |
529 | ca9cc28c | balrog | |
530 | ca9cc28c | balrog | qemu_free (esd->pcm_buf); |
531 | ca9cc28c | balrog | esd->pcm_buf = NULL;
|
532 | ca9cc28c | balrog | } |
533 | ca9cc28c | balrog | |
534 | ca9cc28c | balrog | static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...) |
535 | ca9cc28c | balrog | { |
536 | ca9cc28c | balrog | (void) hw;
|
537 | ca9cc28c | balrog | (void) cmd;
|
538 | ca9cc28c | balrog | return 0; |
539 | ca9cc28c | balrog | } |
540 | ca9cc28c | balrog | |
541 | ca9cc28c | balrog | /* common */
|
542 | ca9cc28c | balrog | static void *qesd_audio_init (void) |
543 | ca9cc28c | balrog | { |
544 | ca9cc28c | balrog | return &conf;
|
545 | ca9cc28c | balrog | } |
546 | ca9cc28c | balrog | |
547 | ca9cc28c | balrog | static void qesd_audio_fini (void *opaque) |
548 | ca9cc28c | balrog | { |
549 | ca9cc28c | balrog | (void) opaque;
|
550 | ca9cc28c | balrog | ldebug ("esd_fini");
|
551 | ca9cc28c | balrog | } |
552 | ca9cc28c | balrog | |
553 | ca9cc28c | balrog | struct audio_option qesd_options[] = {
|
554 | ca9cc28c | balrog | {"SAMPLES", AUD_OPT_INT, &conf.samples,
|
555 | ca9cc28c | balrog | "buffer size in samples", NULL, 0}, |
556 | ca9cc28c | balrog | |
557 | ca9cc28c | balrog | {"DIVISOR", AUD_OPT_INT, &conf.divisor,
|
558 | ca9cc28c | balrog | "threshold divisor", NULL, 0}, |
559 | ca9cc28c | balrog | |
560 | ca9cc28c | balrog | {"DAC_HOST", AUD_OPT_STR, &conf.dac_host,
|
561 | ca9cc28c | balrog | "playback host", NULL, 0}, |
562 | ca9cc28c | balrog | |
563 | ca9cc28c | balrog | {"ADC_HOST", AUD_OPT_STR, &conf.adc_host,
|
564 | ca9cc28c | balrog | "capture host", NULL, 0}, |
565 | ca9cc28c | balrog | |
566 | ca9cc28c | balrog | {NULL, 0, NULL, NULL, NULL, 0} |
567 | ca9cc28c | balrog | }; |
568 | ca9cc28c | balrog | |
569 | ca9cc28c | balrog | struct audio_pcm_ops qesd_pcm_ops = {
|
570 | ca9cc28c | balrog | qesd_init_out, |
571 | ca9cc28c | balrog | qesd_fini_out, |
572 | ca9cc28c | balrog | qesd_run_out, |
573 | ca9cc28c | balrog | qesd_write, |
574 | ca9cc28c | balrog | qesd_ctl_out, |
575 | ca9cc28c | balrog | |
576 | ca9cc28c | balrog | qesd_init_in, |
577 | ca9cc28c | balrog | qesd_fini_in, |
578 | ca9cc28c | balrog | qesd_run_in, |
579 | ca9cc28c | balrog | qesd_read, |
580 | ca9cc28c | balrog | qesd_ctl_in, |
581 | ca9cc28c | balrog | }; |
582 | ca9cc28c | balrog | |
583 | ca9cc28c | balrog | struct audio_driver esd_audio_driver = {
|
584 | ca9cc28c | balrog | INIT_FIELD (name = ) "esd",
|
585 | ca9cc28c | balrog | INIT_FIELD (descr = ) |
586 | ca9cc28c | balrog | "http://en.wikipedia.org/wiki/Esound",
|
587 | ca9cc28c | balrog | INIT_FIELD (options = ) qesd_options, |
588 | ca9cc28c | balrog | INIT_FIELD (init = ) qesd_audio_init, |
589 | ca9cc28c | balrog | INIT_FIELD (fini = ) qesd_audio_fini, |
590 | ca9cc28c | balrog | INIT_FIELD (pcm_ops = ) &qesd_pcm_ops, |
591 | ca9cc28c | balrog | INIT_FIELD (can_be_default = ) 0,
|
592 | ca9cc28c | balrog | INIT_FIELD (max_voices_out = ) INT_MAX, |
593 | ca9cc28c | balrog | INIT_FIELD (max_voices_in = ) INT_MAX, |
594 | ca9cc28c | balrog | INIT_FIELD (voice_size_out = ) sizeof (ESDVoiceOut),
|
595 | ca9cc28c | balrog | INIT_FIELD (voice_size_in = ) sizeof (ESDVoiceIn)
|
596 | ca9cc28c | balrog | }; |