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