6240d39d08ab9ce13b225a93242257f44c596dc6
[src/app-framework-binder.git] / plugins / audio / audio-alsa.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "audio-api.h"
20 #include "audio-alsa.h"
21
22 snd_mixer_selem_channel_id_t SCHANNELS[8] = {
23  SND_MIXER_SCHN_FRONT_LEFT,
24  SND_MIXER_SCHN_FRONT_RIGHT,
25  SND_MIXER_SCHN_FRONT_CENTER,
26  SND_MIXER_SCHN_REAR_LEFT,
27  SND_MIXER_SCHN_REAR_RIGHT,
28  SND_MIXER_SCHN_REAR_CENTER,
29  SND_MIXER_SCHN_SIDE_LEFT,
30  SND_MIXER_SCHN_SIDE_RIGHT
31 };
32
33 PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
34
35     snd_pcm_t *dev;
36     snd_pcm_hw_params_t *params;
37     snd_mixer_t *mixer;
38     snd_mixer_selem_id_t *mixer_sid;
39     snd_mixer_elem_t *mixer_elm;
40     snd_mixer_elem_t *mixer_elm_m;
41     unsigned int rate = 22050;
42     long vol, vol_min, vol_max;
43     int num, i;
44
45     if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
46         return 0;
47
48     snd_pcm_hw_params_malloc (&params);
49     snd_pcm_hw_params_any (dev, params);
50     snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
51     snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
52     snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0);
53     snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
54     if (snd_pcm_hw_params (dev, params) < 0) {
55         snd_pcm_hw_params_free (params);
56         return 0;
57     }
58     snd_pcm_prepare (dev);
59
60     snd_mixer_open (&mixer, 0);
61     if (snd_mixer_attach (mixer, name) < 0) {
62         snd_pcm_hw_params_free (params);
63         return 0;
64     }
65     snd_mixer_selem_register (mixer, NULL, NULL);
66     snd_mixer_load (mixer);
67
68     snd_mixer_selem_id_alloca (&mixer_sid);
69     snd_mixer_selem_id_set_index (mixer_sid, 0);
70     snd_mixer_selem_id_set_name (mixer_sid, "Master");
71
72     mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
73     mixer_elm_m = NULL;
74
75     if (!mixer_elm) {
76         /* no "Master" mixer ; we are probably on a board... search ! */
77         for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
78              mixer_elm = snd_mixer_elem_next (mixer_elm)) {
79             if (snd_mixer_elem_info (mixer_elm) < 0)
80                 continue;
81             snd_mixer_selem_get_id (mixer_elm, mixer_sid);
82             if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {
83
84                 /* this is Porter... let us found the specific mute switch */
85                 snd_mixer_selem_id_set_index (mixer_sid, 0);
86                 snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute");
87                 mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid);
88
89                 break;
90             }
91         }
92     }
93
94     if (mixer_elm) {
95         snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
96         snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
97     }
98
99     /* allocate the global array if it hasn't been done */
100     if (!dev_ctx_a) {
101         dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T));
102         dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
103         dev_ctx_a[0]->name = NULL;
104         dev_ctx_a[0]->dev = NULL;
105     }
106
107     /* is a card with similar name already opened ? */
108     for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); num++) {
109         if (dev_ctx_a[num]->name &&
110            !strcmp (dev_ctx_a[num]->name, name))
111             return 0;
112     }
113     num++;
114
115     /* it's not... let us add it to the global array */
116     dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T));
117     dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
118     dev_ctx_a[num]->name = strdup (name);
119     dev_ctx_a[num]->dev = dev;
120     dev_ctx_a[num]->params = params;
121     dev_ctx_a[num]->mixer_elm = mixer_elm;
122     dev_ctx_a[num]->mixer_elm_m = mixer_elm_m;
123     dev_ctx_a[num]->vol_max = vol_max;
124     dev_ctx_a[num]->vol = vol;
125     dev_ctx_a[num]->thr_should_run = 0;
126     dev_ctx_a[num]->thr_finished = 0;
127
128     /* make the client context aware of current card state */
129     for (i = 0; i < 8; i++)
130         ctx->volume[i] = _alsa_get_volume (num, i);
131     ctx->mute = _alsa_get_mute (num);
132     ctx->idx = num;
133     ctx->name = strdup (name);
134
135     if (verbose) fprintf (stderr, "Successfully initialized ALSA backend.\n");
136
137     return 1;
138 }
139
140 PUBLIC void _alsa_free (const char *name) {
141
142     int num;
143
144     for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); num++) {
145         if (dev_ctx_a[num]->name &&
146            !strcmp (dev_ctx_a[num]->name, name)) {
147             snd_pcm_close (dev_ctx_a[num]->dev);
148             snd_pcm_hw_params_free (dev_ctx_a[num]->params);
149             free (dev_ctx_a[num]->name);
150             dev_ctx_a[num]->dev = NULL;
151             dev_ctx_a[num]->name = NULL;
152             free (dev_ctx_a[num]);
153             return;
154         }
155     }
156 }
157
158 PUBLIC void _alsa_play (int num) {
159
160     if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run ||
161         access (AUDIO_BUFFER, F_OK) == -1)
162         return;
163
164     dev_ctx_a[num]->thr_should_run = 1;
165     dev_ctx_a[num]->thr_finished = 0;
166     pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]);
167 }
168
169 PUBLIC void _alsa_stop (int num) {
170
171     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run)
172         return;
173
174     /* stop the "while" loop in thread */
175     dev_ctx_a[num]->thr_should_run = 0;
176
177     while (!dev_ctx_a[num]->thr_finished)
178         usleep(100000);
179
180     pthread_join (dev_ctx_a[num]->thr, NULL);
181 }
182
183 PUBLIC unsigned int _alsa_get_volume (int num, unsigned int channel) {
184
185     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
186         return 0;
187
188     snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol);
189
190     return (int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max;
191 }
192
193 PUBLIC void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) {
194
195     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
196         vol > 100)
197         return;
198
199     snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100);
200
201 }
202
203 PUBLIC void _alsa_set_volume_all (int num, unsigned int vol) {
204
205     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
206         vol > 100)
207
208     snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100);
209 }
210
211 PUBLIC unsigned char _alsa_get_mute (int num) {
212
213     int mute = 0;
214     snd_mixer_elem_t *elm_m;
215
216     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
217         return 0;
218
219     dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) :
220                                   (elm_m = dev_ctx_a[num]->mixer_elm);
221
222     if (snd_mixer_selem_has_playback_switch (elm_m)) {
223         snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
224         snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
225     }
226
227     if (dev_ctx_a[num]->mixer_elm_m)
228         return (unsigned char)mute;
229     else
230         return (unsigned char)!mute;
231 }
232
233 PUBLIC void _alsa_set_mute (int num, unsigned char tomute) {
234
235     snd_mixer_elem_t *elm_m;
236     int mute;
237
238     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute)
239         return;
240
241     if (dev_ctx_a[num]->mixer_elm_m) {
242         elm_m = dev_ctx_a[num]->mixer_elm_m;
243         mute = (int)!tomute;
244     } else {
245         elm_m = dev_ctx_a[num]->mixer_elm;
246         mute = (int)tomute;
247     }
248
249     if (snd_mixer_selem_has_playback_switch (elm_m))
250         snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
251 }
252
253 PUBLIC void _alsa_set_rate (int num, unsigned int rate) {
254
255     if (!dev_ctx_a || !dev_ctx_a[num])
256         return;
257
258     snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0);
259 }
260
261 PUBLIC void _alsa_set_channels (int num, unsigned int channels) {
262
263     if (!dev_ctx_a || !dev_ctx_a[num])
264         return;
265
266     snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels);
267 }
268
269  /* ---- LOCAL THREADED FUNCTIONS ---- */
270
271 STATIC void* _alsa_play_thread_fn (void *ctx) {
272
273     dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx;
274     FILE *file = NULL;
275     char *buf = NULL;
276     long size;
277     int frames, res;
278
279     file = fopen (AUDIO_BUFFER, "rb");
280
281     while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
282         fseek (file, 0, SEEK_END);
283         size = ftell (file);
284         buf = (char*) realloc (buf, size * sizeof(char));
285         frames = (size * sizeof(char)) / 4;
286
287         fseek (file, 0, SEEK_SET);
288         fread (buf, 1, size, file);
289         fflush (file);
290
291         if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) {
292             snd_pcm_recover (dev_ctx_a->dev, res, 0);
293             snd_pcm_prepare (dev_ctx_a->dev);
294         }
295         /* snd_pcm_drain (dev_ctx->dev); */
296     }
297     if (buf) free(buf);
298     if (file) fclose(file);
299
300     dev_ctx_a->thr_finished = 1;
301     return 0;
302 }