aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/audio/audio-alsa.c
blob: 6240d39d08ab9ce13b225a93242257f44c596dc6 (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
/*
 * Copyright (C) 2015 "IoT.bzh"
 * Author "Manuel Bachmann"
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "audio-api.h"
#include "audio-alsa.h"

snd_mixer_selem_channel_id_t SCHANNELS[8] = {
 SND_MIXER_SCHN_FRONT_LEFT,
 SND_MIXER_SCHN_FRONT_RIGHT,
 SND_MIXER_SCHN_FRONT_CENTER,
 SND_MIXER_SCHN_REAR_LEFT,
 SND_MIXER_SCHN_REAR_RIGHT,
 SND_MIXER_SCHN_REAR_CENTER,
 SND_MIXER_SCHN_SIDE_LEFT,
 SND_MIXER_SCHN_SIDE_RIGHT
};

PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {

    snd_pcm_t *dev;
    snd_pcm_hw_params_t *params;
    snd_mixer_t *mixer;
    snd_mixer_selem_id_t *mixer_sid;
    snd_mixer_elem_t *mixer_elm;
    snd_mixer_elem_t *mixer_elm_m;
    unsigned int rate = 22050;
    long vol, vol_min, vol_max;
    int num, i;

    if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
        return 0;

    snd_pcm_hw_params_malloc (&params);
    snd_pcm_hw_params_any (dev, params);
    snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
    snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
    snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0);
    snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
    if (snd_pcm_hw_params (dev, params) < 0) {
        snd_pcm_hw_params_free (params);
        return 0;
    }
    snd_pcm_prepare (dev);

    snd_mixer_open (&mixer, 0);
    if (snd_mixer_attach (mixer, name) < 0) {
        snd_pcm_hw_params_free (params);
        return 0;
    }
    snd_mixer_selem_register (mixer, NULL, NULL);
    snd_mixer_load (mixer);

    snd_mixer_selem_id_alloca (&mixer_sid);
    snd_mixer_selem_id_set_index (mixer_sid, 0);
    snd_mixer_selem_id_set_name (mixer_sid, "Master");

    mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
    mixer_elm_m = NULL;

    if (!mixer_elm) {
        /* no "Master" mixer ; we are probably on a board... search ! */
        for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
             mixer_elm = snd_mixer_elem_next (mixer_elm)) {
            if (snd_mixer_elem_info (mixer_elm) < 0)
                continue;
            snd_mixer_selem_get_id (mixer_elm, mixer_sid);
            if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {

                /* this is Porter... let us found the specific mute switch */
                snd_mixer_selem_id_set_index (mixer_sid, 0);
                snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute");
                mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid);

                break;
            }
        }
    }

    if (mixer_elm) {
        snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
        snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
    }

    /* allocate the global array if it hasn't been done */
    if (!dev_ctx_a) {
        dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T));
        dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
        dev_ctx_a[0]->name = NULL;
        dev_ctx_a[0]->dev = NULL;
    }

    /* is a card with similar name already opened ? */
    for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); num++) {
        if (dev_ctx_a[num]->name &&
           !strcmp (dev_ctx_a[num]->name, name))
            return 0;
    }
    num++;

    /* it's not... let us add it to the global array */
    dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T));
    dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
    dev_ctx_a[num]->name = strdup (name);
    dev_ctx_a[num]->dev = dev;
    dev_ctx_a[num]->params = params;
    dev_ctx_a[num]->mixer_elm = mixer_elm;
    dev_ctx_a[num]->mixer_elm_m = mixer_elm_m;
    dev_ctx_a[num]->vol_max = vol_max;
    dev_ctx_a[num]->vol = vol;
    dev_ctx_a[num]->thr_should_run = 0;
    dev_ctx_a[num]->thr_finished = 0;

    /* make the client context aware of current card state */
    for (i = 0; i < 8; i++)
        ctx->volume[i] = _alsa_get_volume (num, i);
    ctx->mute = _alsa_get_mute (num);
    ctx->idx = num;
    ctx->name = strdup (name);

    if (verbose) fprintf (stderr, "Successfully initialized ALSA backend.\n");

    return 1;
}

PUBLIC void _alsa_free (const char *name) {

    int num;

    for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); num++) {
        if (dev_ctx_a[num]->name &&
           !strcmp (dev_ctx_a[num]->name, name)) {
            snd_pcm_close (dev_ctx_a[num]->dev);
            snd_pcm_hw_params_free (dev_ctx_a[num]->params);
            free (dev_ctx_a[num]->name);
            dev_ctx_a[num]->dev = NULL;
            dev_ctx_a[num]->name = NULL;
            free (dev_ctx_a[num]);
            return;
        }
    }
}

PUBLIC void _alsa_play (int num) {

    if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run ||
        access (AUDIO_BUFFER, F_OK) == -1)
        return;

    dev_ctx_a[num]->thr_should_run = 1;
    dev_ctx_a[num]->thr_finished = 0;
    pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]);
}

PUBLIC void _alsa_stop (int num) {

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run)
        return;

    /* stop the "while" loop in thread */
    dev_ctx_a[num]->thr_should_run = 0;

    while (!dev_ctx_a[num]->thr_finished)
        usleep(100000);

    pthread_join (dev_ctx_a[num]->thr, NULL);
}

PUBLIC unsigned int _alsa_get_volume (int num, unsigned int channel) {

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
        return 0;

    snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol);

    return (int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max;
}

PUBLIC void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) {

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
        vol > 100)
        return;

    snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100);

}

PUBLIC void _alsa_set_volume_all (int num, unsigned int vol) {

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
        vol > 100)

    snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100);
}

PUBLIC unsigned char _alsa_get_mute (int num) {

    int mute = 0;
    snd_mixer_elem_t *elm_m;

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
        return 0;

    dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) :
                                  (elm_m = dev_ctx_a[num]->mixer_elm);

    if (snd_mixer_selem_has_playback_switch (elm_m)) {
        snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
        snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
    }

    if (dev_ctx_a[num]->mixer_elm_m)
        return (unsigned char)mute;
    else
        return (unsigned char)!mute;
}

PUBLIC void _alsa_set_mute (int num, unsigned char tomute) {

    snd_mixer_elem_t *elm_m;
    int mute;

    if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute)
        return;

    if (dev_ctx_a[num]->mixer_elm_m) {
        elm_m = dev_ctx_a[num]->mixer_elm_m;
        mute = (int)!tomute;
    } else {
        elm_m = dev_ctx_a[num]->mixer_elm;
        mute = (int)tomute;
    }

    if (snd_mixer_selem_has_playback_switch (elm_m))
        snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
}

PUBLIC void _alsa_set_rate (int num, unsigned int rate) {

    if (!dev_ctx_a || !dev_ctx_a[num])
        return;

    snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0);
}

PUBLIC void _alsa_set_channels (int num, unsigned int channels) {

    if (!dev_ctx_a || !dev_ctx_a[num])
        return;

    snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels);
}

 /* ---- LOCAL THREADED FUNCTIONS ---- */

STATIC void* _alsa_play_thread_fn (void *ctx) {

    dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx;
    FILE *file = NULL;
    char *buf = NULL;
    long size;
    int frames, res;

    file = fopen (AUDIO_BUFFER, "rb");

    while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
        fseek (file, 0, SEEK_END);
        size = ftell (file);
        buf = (char*) realloc (buf, size * sizeof(char));
        frames = (size * sizeof(char)) / 4;

        fseek (file, 0, SEEK_SET);
        fread (buf, 1, size, file);
        fflush (file);

        if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) {
            snd_pcm_recover (dev_ctx_a->dev, res, 0);
            snd_pcm_prepare (dev_ctx_a->dev);
        }
        /* snd_pcm_drain (dev_ctx->dev); */
    }
    if (buf) free(buf);
    if (file) fclose(file);

    dev_ctx_a->thr_finished = 1;
    return 0;
}