Statistics
| Branch: | Revision:

root / audio / esdaudio.c @ 4b7c0418

History | View | Annotate | Download (13.5 kB)

1
/*
2
 * QEMU ESD audio driver
3
 *
4
 * Copyright (c) 2006 Frederick Reeve (brushed up by 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
#include <esd.h>
25
#include "qemu-common.h"
26
#include "audio.h"
27

    
28
#define AUDIO_CAP "esd"
29
#include "audio_int.h"
30
#include "audio_pt_int.h"
31

    
32
typedef struct {
33
    HWVoiceOut hw;
34
    int done;
35
    int live;
36
    int decr;
37
    int rpos;
38
    void *pcm_buf;
39
    int fd;
40
    struct audio_pt pt;
41
} ESDVoiceOut;
42

    
43
typedef struct {
44
    HWVoiceIn hw;
45
    int done;
46
    int dead;
47
    int incr;
48
    int wpos;
49
    void *pcm_buf;
50
    int fd;
51
    struct audio_pt pt;
52
} ESDVoiceIn;
53

    
54
static struct {
55
    int samples;
56
    int divisor;
57
    char *dac_host;
58
    char *adc_host;
59
} conf = {
60
    .samples = 1024,
61
    .divisor = 2,
62
};
63

    
64
static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...)
65
{
66
    va_list ap;
67

    
68
    va_start (ap, fmt);
69
    AUD_vlog (AUDIO_CAP, fmt, ap);
70
    va_end (ap);
71

    
72
    AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
73
}
74

    
75
/* playback */
76
static void *qesd_thread_out (void *arg)
77
{
78
    ESDVoiceOut *esd = arg;
79
    HWVoiceOut *hw = &esd->hw;
80
    int threshold;
81

    
82
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
83

    
84
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
85
        return NULL;
86
    }
87

    
88
    for (;;) {
89
        int decr, to_mix, rpos;
90

    
91
        for (;;) {
92
            if (esd->done) {
93
                goto exit;
94
            }
95

    
96
            if (esd->live > threshold) {
97
                break;
98
            }
99

    
100
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
101
                goto exit;
102
            }
103
        }
104

    
105
        decr = to_mix = esd->live;
106
        rpos = hw->rpos;
107

    
108
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
109
            return NULL;
110
        }
111

    
112
        while (to_mix) {
113
            ssize_t written;
114
            int chunk = audio_MIN (to_mix, hw->samples - rpos);
115
            struct st_sample *src = hw->mix_buf + rpos;
116

    
117
            hw->clip (esd->pcm_buf, src, chunk);
118

    
119
        again:
120
            written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
121
            if (written == -1) {
122
                if (errno == EINTR || errno == EAGAIN) {
123
                    goto again;
124
                }
125
                qesd_logerr (errno, "write failed\n");
126
                return NULL;
127
            }
128

    
129
            if (written != chunk << hw->info.shift) {
130
                int wsamples = written >> hw->info.shift;
131
                int wbytes = wsamples << hw->info.shift;
132
                if (wbytes != written) {
133
                    dolog ("warning: Misaligned write %d (requested %zd), "
134
                           "alignment %d\n",
135
                           wbytes, written, hw->info.align + 1);
136
                }
137
                to_mix -= wsamples;
138
                rpos = (rpos + wsamples) % hw->samples;
139
                break;
140
            }
141

    
142
            rpos = (rpos + chunk) % hw->samples;
143
            to_mix -= chunk;
144
        }
145

    
146
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
147
            return NULL;
148
        }
149

    
150
        esd->rpos = rpos;
151
        esd->live -= decr;
152
        esd->decr += decr;
153
    }
154

    
155
 exit:
156
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
157
    return NULL;
158
}
159

    
160
static int qesd_run_out (HWVoiceOut *hw, int live)
161
{
162
    int decr;
163
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
164

    
165
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
166
        return 0;
167
    }
168

    
169
    decr = audio_MIN (live, esd->decr);
170
    esd->decr -= decr;
171
    esd->live = live - decr;
172
    hw->rpos = esd->rpos;
173
    if (esd->live > 0) {
174
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
175
    }
176
    else {
177
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
178
    }
179
    return decr;
180
}
181

    
182
static int qesd_write (SWVoiceOut *sw, void *buf, int len)
183
{
184
    return audio_pcm_sw_write (sw, buf, len);
185
}
186

    
187
static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
188
{
189
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
190
    struct audsettings obt_as = *as;
191
    int esdfmt = ESD_STREAM | ESD_PLAY;
192

    
193
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
194
    switch (as->fmt) {
195
    case AUD_FMT_S8:
196
    case AUD_FMT_U8:
197
        esdfmt |= ESD_BITS8;
198
        obt_as.fmt = AUD_FMT_U8;
199
        break;
200

    
201
    case AUD_FMT_S32:
202
    case AUD_FMT_U32:
203
        dolog ("Will use 16 instead of 32 bit samples\n");
204

    
205
    case AUD_FMT_S16:
206
    case AUD_FMT_U16:
207
    deffmt:
208
        esdfmt |= ESD_BITS16;
209
        obt_as.fmt = AUD_FMT_S16;
210
        break;
211

    
212
    default:
213
        dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
214
        goto deffmt;
215

    
216
    }
217
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
218

    
219
    audio_pcm_init_info (&hw->info, &obt_as);
220

    
221
    hw->samples = conf.samples;
222
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
223
    if (!esd->pcm_buf) {
224
        dolog ("Could not allocate buffer (%d bytes)\n",
225
               hw->samples << hw->info.shift);
226
        return -1;
227
    }
228

    
229
    esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
230
    if (esd->fd < 0) {
231
        qesd_logerr (errno, "esd_play_stream failed\n");
232
        goto fail1;
233
    }
234

    
235
    if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
236
        goto fail2;
237
    }
238

    
239
    return 0;
240

    
241
 fail2:
242
    if (close (esd->fd)) {
243
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
244
                     AUDIO_FUNC, esd->fd);
245
    }
246
    esd->fd = -1;
247

    
248
 fail1:
249
    qemu_free (esd->pcm_buf);
250
    esd->pcm_buf = NULL;
251
    return -1;
252
}
253

    
254
static void qesd_fini_out (HWVoiceOut *hw)
255
{
256
    void *ret;
257
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
258

    
259
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
260
    esd->done = 1;
261
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
262
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
263

    
264
    if (esd->fd >= 0) {
265
        if (close (esd->fd)) {
266
            qesd_logerr (errno, "failed to close esd socket\n");
267
        }
268
        esd->fd = -1;
269
    }
270

    
271
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
272

    
273
    qemu_free (esd->pcm_buf);
274
    esd->pcm_buf = NULL;
275
}
276

    
277
static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
278
{
279
    (void) hw;
280
    (void) cmd;
281
    return 0;
282
}
283

    
284
/* capture */
285
static void *qesd_thread_in (void *arg)
286
{
287
    ESDVoiceIn *esd = arg;
288
    HWVoiceIn *hw = &esd->hw;
289
    int threshold;
290

    
291
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
292

    
293
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
294
        return NULL;
295
    }
296

    
297
    for (;;) {
298
        int incr, to_grab, wpos;
299

    
300
        for (;;) {
301
            if (esd->done) {
302
                goto exit;
303
            }
304

    
305
            if (esd->dead > threshold) {
306
                break;
307
            }
308

    
309
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
310
                goto exit;
311
            }
312
        }
313

    
314
        incr = to_grab = esd->dead;
315
        wpos = hw->wpos;
316

    
317
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
318
            return NULL;
319
        }
320

    
321
        while (to_grab) {
322
            ssize_t nread;
323
            int chunk = audio_MIN (to_grab, hw->samples - wpos);
324
            void *buf = advance (esd->pcm_buf, wpos);
325

    
326
        again:
327
            nread = read (esd->fd, buf, chunk << hw->info.shift);
328
            if (nread == -1) {
329
                if (errno == EINTR || errno == EAGAIN) {
330
                    goto again;
331
                }
332
                qesd_logerr (errno, "read failed\n");
333
                return NULL;
334
            }
335

    
336
            if (nread != chunk << hw->info.shift) {
337
                int rsamples = nread >> hw->info.shift;
338
                int rbytes = rsamples << hw->info.shift;
339
                if (rbytes != nread) {
340
                    dolog ("warning: Misaligned write %d (requested %zd), "
341
                           "alignment %d\n",
342
                           rbytes, nread, hw->info.align + 1);
343
                }
344
                to_grab -= rsamples;
345
                wpos = (wpos + rsamples) % hw->samples;
346
                break;
347
            }
348

    
349
            hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift,
350
                      &nominal_volume);
351
            wpos = (wpos + chunk) % hw->samples;
352
            to_grab -= chunk;
353
        }
354

    
355
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
356
            return NULL;
357
        }
358

    
359
        esd->wpos = wpos;
360
        esd->dead -= incr;
361
        esd->incr += incr;
362
    }
363

    
364
 exit:
365
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
366
    return NULL;
367
}
368

    
369
static int qesd_run_in (HWVoiceIn *hw)
370
{
371
    int live, incr, dead;
372
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
373

    
374
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
375
        return 0;
376
    }
377

    
378
    live = audio_pcm_hw_get_live_in (hw);
379
    dead = hw->samples - live;
380
    incr = audio_MIN (dead, esd->incr);
381
    esd->incr -= incr;
382
    esd->dead = dead - incr;
383
    hw->wpos = esd->wpos;
384
    if (esd->dead > 0) {
385
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
386
    }
387
    else {
388
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
389
    }
390
    return incr;
391
}
392

    
393
static int qesd_read (SWVoiceIn *sw, void *buf, int len)
394
{
395
    return audio_pcm_sw_read (sw, buf, len);
396
}
397

    
398
static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
399
{
400
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
401
    struct audsettings obt_as = *as;
402
    int esdfmt = ESD_STREAM | ESD_RECORD;
403

    
404
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
405
    switch (as->fmt) {
406
    case AUD_FMT_S8:
407
    case AUD_FMT_U8:
408
        esdfmt |= ESD_BITS8;
409
        obt_as.fmt = AUD_FMT_U8;
410
        break;
411

    
412
    case AUD_FMT_S16:
413
    case AUD_FMT_U16:
414
        esdfmt |= ESD_BITS16;
415
        obt_as.fmt = AUD_FMT_S16;
416
        break;
417

    
418
    case AUD_FMT_S32:
419
    case AUD_FMT_U32:
420
        dolog ("Will use 16 instead of 32 bit samples\n");
421
        esdfmt |= ESD_BITS16;
422
        obt_as.fmt = AUD_FMT_S16;
423
        break;
424
    }
425
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
426

    
427
    audio_pcm_init_info (&hw->info, &obt_as);
428

    
429
    hw->samples = conf.samples;
430
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
431
    if (!esd->pcm_buf) {
432
        dolog ("Could not allocate buffer (%d bytes)\n",
433
               hw->samples << hw->info.shift);
434
        return -1;
435
    }
436

    
437
    esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
438
    if (esd->fd < 0) {
439
        qesd_logerr (errno, "esd_record_stream failed\n");
440
        goto fail1;
441
    }
442

    
443
    if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
444
        goto fail2;
445
    }
446

    
447
    return 0;
448

    
449
 fail2:
450
    if (close (esd->fd)) {
451
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
452
                     AUDIO_FUNC, esd->fd);
453
    }
454
    esd->fd = -1;
455

    
456
 fail1:
457
    qemu_free (esd->pcm_buf);
458
    esd->pcm_buf = NULL;
459
    return -1;
460
}
461

    
462
static void qesd_fini_in (HWVoiceIn *hw)
463
{
464
    void *ret;
465
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
466

    
467
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
468
    esd->done = 1;
469
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
470
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
471

    
472
    if (esd->fd >= 0) {
473
        if (close (esd->fd)) {
474
            qesd_logerr (errno, "failed to close esd socket\n");
475
        }
476
        esd->fd = -1;
477
    }
478

    
479
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
480

    
481
    qemu_free (esd->pcm_buf);
482
    esd->pcm_buf = NULL;
483
}
484

    
485
static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
486
{
487
    (void) hw;
488
    (void) cmd;
489
    return 0;
490
}
491

    
492
/* common */
493
static void *qesd_audio_init (void)
494
{
495
    return &conf;
496
}
497

    
498
static void qesd_audio_fini (void *opaque)
499
{
500
    (void) opaque;
501
    ldebug ("esd_fini");
502
}
503

    
504
struct audio_option qesd_options[] = {
505
    {
506
        .name  = "SAMPLES",
507
        .tag   = AUD_OPT_INT,
508
        .valp  = &conf.samples,
509
        .descr = "buffer size in samples"
510
    },
511
    {
512
        .name  = "DIVISOR",
513
        .tag   = AUD_OPT_INT,
514
        .valp  = &conf.divisor,
515
        .descr = "threshold divisor"
516
    },
517
    {
518
        .name  = "DAC_HOST",
519
        .tag   = AUD_OPT_STR,
520
        .valp  = &conf.dac_host,
521
        .descr = "playback host"
522
    },
523
    {
524
        .name  = "ADC_HOST",
525
        .tag   = AUD_OPT_STR,
526
        .valp  = &conf.adc_host,
527
        .descr = "capture host"
528
    },
529
    { /* End of list */ }
530
};
531

    
532
static struct audio_pcm_ops qesd_pcm_ops = {
533
    .init_out = qesd_init_out,
534
    .fini_out = qesd_fini_out,
535
    .run_out  = qesd_run_out,
536
    .write    = qesd_write,
537
    .ctl_out  = qesd_ctl_out,
538

    
539
    .init_in  = qesd_init_in,
540
    .fini_in  = qesd_fini_in,
541
    .run_in   = qesd_run_in,
542
    .read     = qesd_read,
543
    .ctl_in   = qesd_ctl_in,
544
};
545

    
546
struct audio_driver esd_audio_driver = {
547
    .name           = "esd",
548
    .descr          = "http://en.wikipedia.org/wiki/Esound",
549
    .options        = qesd_options,
550
    .init           = qesd_audio_init,
551
    .fini           = qesd_audio_fini,
552
    .pcm_ops        = &qesd_pcm_ops,
553
    .can_be_default = 0,
554
    .max_voices_out = INT_MAX,
555
    .max_voices_in  = INT_MAX,
556
    .voice_size_out = sizeof (ESDVoiceOut),
557
    .voice_size_in  = sizeof (ESDVoiceIn)
558
};