summaryrefslogtreecommitdiff
path: root/src/alsa/alsa.c
blob: 867ad8cb652bacf35e9e5e81852872cef8c1bf53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
 * ALSA Output Plugin for Audacious
 * Copyright 2009-2010 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

/*
 * Because ALSA is not thread-safe (despite claims to the contrary) we use non-
 * blocking output in the pump thread with the mutex locked, then unlock the
 * mutex and wait for more room in the buffer with poll() while other threads
 * lock the mutex and read the output time.  We poll a pipe of our own as well
 * as the ALSA file descriptors so that we can wake up the pump thread when
 * needed.
 *
 * When paused, or when it comes to the end of the data given it, the pump will
 * wait on alsa_cond for the signal to continue.  When it has more data waiting,
 * however, it will be sitting in poll() waiting for ALSA's signal that more
 * data can be written.
 *
 * * After adding more data to the buffer, and after resuming from pause,
 *   signal on alsa_cond to wake the pump.  (There is no need to signal when
 *   entering pause.)
 * * After setting the pump_quit flag, signal on alsa_cond AND the poll_pipe
 *   before joining the thread.
 */

#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>

#include <alsa/asoundlib.h>
#include <glib.h>

#include <audacious/audconfig.h>
#include <audacious/debug.h>

#include "alsa.h"

#define CHECK_VAL_RECOVER(value, function, ...) \
do { \
    (value) = function (__VA_ARGS__); \
    if ((value) < 0) { \
        CHECK (snd_pcm_recover, alsa_handle, (value), 0); \
        CHECK_VAL ((value), function, __VA_ARGS__); \
    } \
} while (0)

#define CHECK_RECOVER(function, ...) \
do { \
    int error2; \
    CHECK_VAL_RECOVER (error2, function, __VA_ARGS__); \
} while (0)

static snd_pcm_t * alsa_handle;
static char alsa_initted;
static pthread_mutex_t alsa_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t alsa_cond = PTHREAD_COND_INITIALIZER;

static snd_pcm_format_t alsa_format;
static int alsa_channels, alsa_rate;

static void * alsa_buffer;
static int alsa_buffer_length, alsa_buffer_data_start, alsa_buffer_data_length;

static int64_t alsa_written; /* frames */
static char alsa_prebuffer, alsa_paused;
static int alsa_paused_time; /* milliseconds */

static int poll_pipe[2];
static int poll_count;
static struct pollfd * poll_handles;

static char pump_quit;
static pthread_t pump_thread;

static snd_mixer_t * alsa_mixer;
static snd_mixer_elem_t * alsa_mixer_element;

static char poll_setup (void)
{
    if (pipe (poll_pipe))
    {
        ERROR ("Failed to create pipe: %s.\n", strerror (errno));
        return 0;
    }

    if (fcntl (poll_pipe[0], F_SETFL, O_NONBLOCK))
    {
        ERROR ("Failed to set O_NONBLOCK on pipe: %s.\n", strerror (errno));
        close (poll_pipe[0]);
        close (poll_pipe[1]);
        return 0;
    }

    poll_count = 1 + snd_pcm_poll_descriptors_count (alsa_handle);
    poll_handles = malloc (sizeof (struct pollfd) * poll_count);
    poll_handles[0].fd = poll_pipe[0];
    poll_handles[0].events = POLLIN;
    poll_count = 1 + snd_pcm_poll_descriptors (alsa_handle, poll_handles + 1,
     poll_count - 1);

    return 1;
}

static void poll_sleep (void)
{
    if (poll (poll_handles, poll_count, -1) < 0)
    {
        ERROR ("Failed to poll: %s.\n", strerror (errno));
        return;
    }

    if (poll_handles[0].revents & POLLIN)
    {
        char c;
        while (read (poll_pipe[0], & c, 1) == 1)
            ;
    }
}

static void poll_wake (void)
{
    const char c = 0;
    write (poll_pipe[1], & c, 1);
}

static void poll_cleanup (void)
{
    close (poll_pipe[0]);
    close (poll_pipe[1]);
    free (poll_handles);
}

static void * pump (void * unused)
{
    pthread_mutex_lock (& alsa_mutex);
    pthread_cond_broadcast (& alsa_cond); /* signal thread started */

    while (! pump_quit)
    {
        if (alsa_prebuffer || alsa_paused || ! alsa_buffer_data_length)
        {
            pthread_cond_wait (& alsa_cond, & alsa_mutex);
            continue;
        }

        int length;
        CHECK_VAL_RECOVER (length, snd_pcm_avail_update, alsa_handle);

        if (! length)
            goto WAIT;

        length = snd_pcm_frames_to_bytes (alsa_handle, length);
        length = MIN (length, alsa_buffer_data_length);
        length = MIN (length, alsa_buffer_length - alsa_buffer_data_start);
        length = snd_pcm_bytes_to_frames (alsa_handle, length);

        int written;
        CHECK_VAL_RECOVER (written, snd_pcm_writei, alsa_handle, (char *)
         alsa_buffer + alsa_buffer_data_start, length);

        written = snd_pcm_frames_to_bytes (alsa_handle, written);
        alsa_buffer_data_start += written;
        alsa_buffer_data_length -= written;

        pthread_cond_broadcast (& alsa_cond); /* signal write complete */

        if (alsa_buffer_data_start == alsa_buffer_length)
        {
            alsa_buffer_data_start = 0;
            continue;
        }

    WAIT:
        pthread_mutex_unlock (& alsa_mutex);
        poll_sleep ();
        pthread_mutex_lock (& alsa_mutex);
    }

FAILED:
    pthread_mutex_unlock (& alsa_mutex);
    return NULL;
}

static void pump_start (void)
{
    AUDDBG ("Starting pump.\n");
    pthread_create (& pump_thread, NULL, pump, NULL);
    pthread_cond_wait (& alsa_cond, & alsa_mutex);
}

static void pump_stop (void)
{
    AUDDBG ("Stopping pump.\n");
    pump_quit = 1;
    pthread_cond_broadcast (& alsa_cond);
    poll_wake ();
    pthread_mutex_unlock (& alsa_mutex);
    pthread_join (pump_thread, NULL);
    pthread_mutex_lock (& alsa_mutex);
    pump_quit = 0;
}

static void start_playback (void)
{
    AUDDBG ("Starting playback.\n");
    CHECK (snd_pcm_prepare, alsa_handle);

FAILED:
    alsa_prebuffer = 0;
    pthread_cond_broadcast (& alsa_cond);
}

static int get_delay (void)
{
    snd_pcm_sframes_t delay = 0;

    CHECK_RECOVER (snd_pcm_delay, alsa_handle, & delay);

FAILED:
    return delay;
}

static int get_output_time (void)
{
    return (int64_t) (alsa_written - snd_pcm_bytes_to_frames (alsa_handle,
     alsa_buffer_data_length) - get_delay ()) * 1000 / alsa_rate;
}

OutputPluginInitStatus alsa_init (void)
{
    alsa_handle = NULL;
    alsa_initted = 0;
    return OUTPUT_PLUGIN_INIT_FOUND_DEVICES;
}

void alsa_soft_init (void)
{
    pthread_mutex_lock (& alsa_mutex);

    if (! alsa_initted)
    {
        AUDDBG ("Initialize.\n");
        alsa_config_load ();
        alsa_open_mixer ();
        alsa_initted = 1;
    }

    pthread_mutex_unlock (& alsa_mutex);
}

void alsa_cleanup (void)
{
    if (alsa_initted)
    {
        AUDDBG ("Cleanup.\n");
        alsa_close_mixer ();
        alsa_config_save ();
    }
}

static int convert_aud_format (int aud_format)
{
    const struct
    {
        int aud_format, format;
    }
    table[] =
    {
        {FMT_FLOAT, SND_PCM_FORMAT_FLOAT},
        {FMT_S8, SND_PCM_FORMAT_S8},
        {FMT_U8, SND_PCM_FORMAT_U8},
        {FMT_S16_LE, SND_PCM_FORMAT_S16_LE},
        {FMT_S16_BE, SND_PCM_FORMAT_S16_BE},
        {FMT_U16_LE, SND_PCM_FORMAT_U16_LE},
        {FMT_U16_BE, SND_PCM_FORMAT_U16_BE},
        {FMT_S24_LE, SND_PCM_FORMAT_S24_LE},
        {FMT_S24_BE, SND_PCM_FORMAT_S24_BE},
        {FMT_U24_LE, SND_PCM_FORMAT_U24_LE},
        {FMT_U24_BE, SND_PCM_FORMAT_U24_BE},
        {FMT_S32_LE, SND_PCM_FORMAT_S32_LE},
        {FMT_S32_BE, SND_PCM_FORMAT_S32_BE},
        {FMT_U32_LE, SND_PCM_FORMAT_U32_LE},
        {FMT_U32_BE, SND_PCM_FORMAT_U32_BE},
    };

    for (int count = 0; count < G_N_ELEMENTS (table); count ++)
    {
        if (table[count].aud_format == aud_format)
            return table[count].format;
    }

    return SND_PCM_FORMAT_UNKNOWN;
}

int alsa_open_audio (int aud_format, int rate, int channels)
{
    alsa_soft_init ();
    pthread_mutex_lock (& alsa_mutex);

    assert (alsa_handle == NULL);

    int format = convert_aud_format (aud_format);
    AUDDBG ("Opening PCM device %s for %s, %d channels, %d Hz.\n",
     alsa_config_pcm, snd_pcm_format_name (format), channels, rate);
    CHECK_NOISY (snd_pcm_open, & alsa_handle, alsa_config_pcm,
     SND_PCM_STREAM_PLAYBACK, 0);

    snd_pcm_hw_params_t * params;
    snd_pcm_hw_params_alloca (& params);
    CHECK_NOISY (snd_pcm_hw_params_any, alsa_handle, params);
    CHECK_NOISY (snd_pcm_hw_params_set_access, alsa_handle, params,
     SND_PCM_ACCESS_RW_INTERLEAVED);

    CHECK_NOISY (snd_pcm_hw_params_set_format, alsa_handle, params, format);
    CHECK_NOISY (snd_pcm_hw_params_set_channels, alsa_handle, params, channels);
    CHECK_NOISY (snd_pcm_hw_params_set_rate, alsa_handle, params, rate, 0);

    alsa_format = format;
    alsa_channels = channels;
    alsa_rate = rate;

    unsigned int useconds = 1000 * MIN (1000, aud_cfg->output_buffer_size / 2);
    int direction = 0;
    CHECK_NOISY (snd_pcm_hw_params_set_buffer_time_near, alsa_handle, params,
     & useconds, & direction);
    int hard_buffer = useconds / 1000;

    useconds = 1000 * (hard_buffer / 4);
    direction = 0;
    CHECK_NOISY (snd_pcm_hw_params_set_period_time_near, alsa_handle, params,
     & useconds, & direction);
#ifdef DEBUG
    int period = useconds / 1000;
#endif

    CHECK_NOISY (snd_pcm_hw_params, alsa_handle, params);

    int soft_buffer = MAX (aud_cfg->output_buffer_size / 2,
     aud_cfg->output_buffer_size - hard_buffer);
    AUDDBG ("Buffer: hardware %d ms, software %d ms, period %d ms.\n",
     hard_buffer, soft_buffer, period);

    alsa_buffer_length = snd_pcm_frames_to_bytes (alsa_handle, (int64_t)
     soft_buffer * rate / 1000);
    alsa_buffer = malloc (alsa_buffer_length);
    alsa_buffer_data_start = 0;
    alsa_buffer_data_length = 0;

    alsa_written = 0;
    alsa_prebuffer = 1;
    alsa_paused = 0;
    alsa_paused_time = 0;

    if (! poll_setup ())
        goto FAILED;

    pump_start ();

    pthread_mutex_unlock (& alsa_mutex);
    return 1;

FAILED:
    if (alsa_handle != NULL)
    {
        snd_pcm_close (alsa_handle);
        alsa_handle = NULL;
    }

    pthread_mutex_unlock (& alsa_mutex);
    return 0;
}

void alsa_close_audio (void)
{
    AUDDBG ("Closing audio.\n");
    pthread_mutex_lock (& alsa_mutex);

    assert (alsa_handle != NULL);

    pump_stop ();
    CHECK (snd_pcm_drop, alsa_handle);

FAILED:
    free (alsa_buffer);
    poll_cleanup ();
    snd_pcm_close (alsa_handle);
    alsa_handle = NULL;

    pthread_mutex_unlock (& alsa_mutex);
}

int alsa_buffer_free (void)
{
    pthread_mutex_lock (& alsa_mutex);
    int avail = alsa_buffer_length - alsa_buffer_data_length;
    pthread_mutex_unlock (& alsa_mutex);
    return avail;
}

void alsa_write_audio (void * data, int length)
{
    pthread_mutex_lock (& alsa_mutex);

    int start = (alsa_buffer_data_start + alsa_buffer_data_length) %
     alsa_buffer_length;

    assert (length <= alsa_buffer_length - alsa_buffer_data_length);

    if (length > alsa_buffer_length - start)
    {
        int part = alsa_buffer_length - start;

        memcpy ((char *) alsa_buffer + start, data, part);
        memcpy (alsa_buffer, (char *) data + part, length - part);
    }
    else
        memcpy ((char *) alsa_buffer + start, data, length);

    alsa_buffer_data_length += length;
    alsa_written += snd_pcm_bytes_to_frames (alsa_handle, length);

    pthread_mutex_unlock (& alsa_mutex);
}

void alsa_period_wait (void)
{
    pthread_mutex_lock (& alsa_mutex);

    while (alsa_buffer_data_length == alsa_buffer_length)
    {
        if (! alsa_paused)
        {
            if (alsa_prebuffer)
                start_playback ();
            else
                pthread_cond_broadcast (& alsa_cond);
        }

        pthread_cond_wait (& alsa_cond, & alsa_mutex);
    }

    pthread_mutex_unlock (& alsa_mutex);
}

void alsa_drain (void)
{
    AUDDBG ("Drain.\n");
    pthread_mutex_lock (& alsa_mutex);

    assert (! alsa_paused);

    if (alsa_prebuffer)
        start_playback ();

    while (alsa_buffer_data_length > 0)
        pthread_cond_wait (& alsa_cond, & alsa_mutex);

    pump_stop ();

    if (alsa_config_drain_workaround)
    {
        int d = get_delay () * 1000 / alsa_rate;
        struct timespec delay = {.tv_sec = d / 1000, .tv_nsec = d % 1000 *
         1000000};

        pthread_mutex_unlock (& alsa_mutex);
        nanosleep (& delay, NULL);
        pthread_mutex_lock (& alsa_mutex);
    }
    else
    {
        while (1)
        {
            int state;
            CHECK_VAL (state, snd_pcm_state, alsa_handle);

            if (state != SND_PCM_STATE_RUNNING && state !=
             SND_PCM_STATE_DRAINING)
                break;

            pthread_mutex_unlock (& alsa_mutex);
            poll_sleep ();
            pthread_mutex_lock (& alsa_mutex);
        }
    }

    pump_start ();

FAILED:
    pthread_mutex_unlock (& alsa_mutex);
    return;
}

void alsa_set_written_time (int time)
{
    AUDDBG ("Setting time counter to %d.\n", time);
    pthread_mutex_lock (& alsa_mutex);
    alsa_written = (int64_t) time * alsa_rate / 1000;
    pthread_mutex_unlock (& alsa_mutex);
}

int alsa_written_time (void)
{
    pthread_mutex_lock (& alsa_mutex);
    int time = (int64_t) alsa_written * 1000 / alsa_rate;
    pthread_mutex_unlock (& alsa_mutex);
    return time;
}

int alsa_output_time (void)
{
    pthread_mutex_lock (& alsa_mutex);
    int time = (alsa_prebuffer || alsa_paused) ? alsa_paused_time :
     get_output_time ();
    pthread_mutex_unlock (& alsa_mutex);
    return time;
}

void alsa_flush (int time)
{
    AUDDBG ("Seek requested; discarding buffer.\n");
    pthread_mutex_lock (& alsa_mutex);

    pump_stop ();
    CHECK (snd_pcm_drop, alsa_handle);

FAILED:
    alsa_buffer_data_start = 0;
    alsa_buffer_data_length = 0;

    alsa_written = (int64_t) time * alsa_rate / 1000;
    alsa_prebuffer = 1;
    alsa_paused_time = time;

    pthread_cond_broadcast (& alsa_cond); /* interrupt period wait */

    pump_start ();

    pthread_mutex_unlock (& alsa_mutex);
}

void alsa_pause (short pause)
{
    AUDDBG ("%sause.\n", pause ? "P" : "Unp");
    pthread_mutex_lock (& alsa_mutex);

    alsa_paused = pause;

    if (! alsa_prebuffer)
    {
        if (pause)
            alsa_paused_time = get_output_time ();

        CHECK (snd_pcm_pause, alsa_handle, pause);
    }

FAILED:
    pthread_cond_broadcast (& alsa_cond);
    pthread_mutex_unlock (& alsa_mutex);
}

void alsa_open_mixer (void)
{
    snd_mixer_selem_id_t * selem_id;

    alsa_mixer = NULL;

    if (alsa_config_mixer_element == NULL)
        goto FAILED;

    AUDDBG ("Opening mixer card %s.\n", alsa_config_mixer);
    CHECK_NOISY (snd_mixer_open, & alsa_mixer, 0);
    CHECK_NOISY (snd_mixer_attach, alsa_mixer, alsa_config_mixer);
    CHECK_NOISY (snd_mixer_selem_register, alsa_mixer, NULL, NULL);
    CHECK_NOISY (snd_mixer_load, alsa_mixer);

    snd_mixer_selem_id_alloca (& selem_id);
    snd_mixer_selem_id_set_name (selem_id, alsa_config_mixer_element);
    alsa_mixer_element = snd_mixer_find_selem (alsa_mixer, selem_id);

    if (alsa_mixer_element == NULL)
    {
        ERROR_NOISY ("snd_mixer_find_selem failed.\n");
        goto FAILED;
    }

    CHECK (snd_mixer_selem_set_playback_volume_range, alsa_mixer_element, 0, 100);
    return;

FAILED:
    if (alsa_mixer != NULL)
    {
        snd_mixer_close (alsa_mixer);
        alsa_mixer = NULL;
    }
}

void alsa_close_mixer (void)
{
    if (alsa_mixer != NULL)
        snd_mixer_close (alsa_mixer);
}

void alsa_get_volume (int * left, int * right)
{
    alsa_soft_init ();
    pthread_mutex_lock (& alsa_mutex);

    long left_l = 0, right_l = 0;

    if (alsa_mixer == NULL)
        goto FAILED;

    CHECK (snd_mixer_handle_events, alsa_mixer);

    if (snd_mixer_selem_is_playback_mono (alsa_mixer_element))
    {
        CHECK (snd_mixer_selem_get_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_MONO, & left_l);
        right_l = left_l;
    }
    else
    {
        CHECK (snd_mixer_selem_get_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_FRONT_LEFT, & left_l);
        CHECK (snd_mixer_selem_get_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_FRONT_RIGHT, & right_l);
    }

FAILED:
    pthread_mutex_unlock (& alsa_mutex);

    * left = left_l;
    * right = right_l;
}

void alsa_set_volume (int left, int right)
{
    alsa_soft_init ();
    pthread_mutex_lock (& alsa_mutex);

    if (alsa_mixer == NULL)
        goto FAILED;

    if (snd_mixer_selem_is_playback_mono (alsa_mixer_element))
    {
        CHECK (snd_mixer_selem_set_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_MONO, MAX (left, right));

        if (snd_mixer_selem_has_playback_switch (alsa_mixer_element))
            CHECK (snd_mixer_selem_set_playback_switch, alsa_mixer_element,
             SND_MIXER_SCHN_MONO, MAX (left, right) != 0);
    }
    else
    {
        CHECK (snd_mixer_selem_set_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_FRONT_LEFT, left);
        CHECK (snd_mixer_selem_set_playback_volume, alsa_mixer_element,
         SND_MIXER_SCHN_FRONT_RIGHT, right);

        if (snd_mixer_selem_has_playback_switch (alsa_mixer_element))
        {
            if (snd_mixer_selem_has_playback_switch_joined (alsa_mixer_element))
                CHECK (snd_mixer_selem_set_playback_switch, alsa_mixer_element,
                 SND_MIXER_SCHN_FRONT_LEFT, MAX (left, right) != 0);
            else
            {
                CHECK (snd_mixer_selem_set_playback_switch, alsa_mixer_element,
                 SND_MIXER_SCHN_FRONT_LEFT, left != 0);
                CHECK (snd_mixer_selem_set_playback_switch, alsa_mixer_element,
                 SND_MIXER_SCHN_FRONT_RIGHT, right != 0);
            }
        }
    }

    CHECK (snd_mixer_handle_events, alsa_mixer);

FAILED:
    pthread_mutex_unlock (& alsa_mutex);
}