Audio API: manage multiple-channel volume
[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     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 0;
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 0;
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 0;
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     if (!mixer_elm) {
73         /* no "Master" mixer ; we are probably on a board... search ! */
74         for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
75              mixer_elm = snd_mixer_elem_next (mixer_elm)) {
76             if (snd_mixer_elem_info (mixer_elm) < 0)
77                 continue;
78             snd_mixer_selem_get_id (mixer_elm, mixer_sid);
79             if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "Master") ||
80                 strstr (snd_mixer_selem_id_get_name (mixer_sid), "Playback"))
81                 break;
82         }
83     }
84
85     if (mixer_elm) {
86         snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
87         snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
88     }
89
90     /* allocate the global array if it hasn't been done */
91     if (!dev_ctx) {
92         dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
93         dev_ctx[0] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
94         dev_ctx[0]->name = NULL;
95         dev_ctx[0]->dev = NULL;
96     }
97
98     /* is a card with similar name already opened ? */
99     for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
100         if (dev_ctx[num]->name &&
101            !strcmp (dev_ctx[num]->name, name))
102             return 0;
103     }
104     num++;
105
106     /* it's not... let us add it to the global array */
107     dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (num+1)*sizeof(dev_ctx_T));
108     dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
109     dev_ctx[num]->name = strdup (name);
110     dev_ctx[num]->dev = dev;
111     dev_ctx[num]->params = params;
112     dev_ctx[num]->mixer_elm = mixer_elm;
113     dev_ctx[num]->vol_max = vol_max;
114     dev_ctx[num]->vol = vol;
115     dev_ctx[num]->thr_should_run = 0;
116     dev_ctx[num]->thr_finished = 0;
117
118     /* make the client context aware of current card state */
119     for (i = 0; i < 8; i++)
120         ctx->volume[i] = _alsa_get_volume (num, i);
121     ctx->mute = _alsa_get_mute (num);
122     ctx->idx = num;
123
124     return 1;
125 }
126
127 PUBLIC void _alsa_free (const char *name) {
128
129     int num;
130
131     for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
132         if (dev_ctx[num]->name &&
133            !strcmp (dev_ctx[num]->name, name)) {
134             snd_pcm_close (dev_ctx[num]->dev);
135             snd_pcm_hw_params_free (dev_ctx[num]->params);
136             free (dev_ctx[num]->name);
137             dev_ctx[num]->name = NULL;
138             dev_ctx[num]->dev = NULL;
139             free(dev_ctx[num]);
140             return;
141         }
142     }
143 }
144
145 PUBLIC void _alsa_play (unsigned int num) {
146
147     if (!dev_ctx || !dev_ctx[num] || dev_ctx[num]->thr_should_run ||
148         access (AUDIO_BUFFER, F_OK) == -1)
149         return;
150
151     dev_ctx[num]->thr_should_run = 1;
152     dev_ctx[num]->thr_finished = 0;
153     pthread_create(&dev_ctx[num]->thr, NULL, _play_thread_fn, (void*)dev_ctx[num]);
154 }
155
156 PUBLIC void _alsa_stop (unsigned int num) {
157
158     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->thr_should_run)
159         return;
160
161     /* stop the "while" loop in thread */
162     dev_ctx[num]->thr_should_run = 0;
163
164     while (!dev_ctx[num]->thr_finished)
165         usleep(100000);
166
167     pthread_join(dev_ctx[num]->thr, NULL);
168 }
169
170 PUBLIC int _alsa_get_volume (unsigned int num, unsigned int channel) {
171
172     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
173         return;
174
175     snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SCHANNELS[channel], &dev_ctx[num]->vol);
176
177     return (int)(dev_ctx[num]->vol*100)/dev_ctx[num]->vol_max;
178 }
179
180 PUBLIC void _alsa_set_volume (unsigned int num, unsigned int channel, int vol) {
181
182     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm ||
183         0 > vol > 100)
184         return;
185
186     snd_mixer_selem_set_playback_volume (dev_ctx[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx[num]->vol_max)/100);
187
188 }
189
190 PUBLIC void _alsa_set_volume_all (unsigned int num, int vol) {
191
192     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm ||
193         0 > vol > 100)
194
195     snd_mixer_selem_set_playback_volume_all (dev_ctx[num]->mixer_elm, (vol*dev_ctx[num]->vol_max)/100);
196 }
197
198 PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
199
200     int mute = 0;
201
202     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
203         return;
204
205     if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm)) {
206         snd_mixer_selem_get_playback_switch (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &mute); 
207         snd_mixer_selem_get_playback_switch (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_RIGHT, &mute); 
208
209     }
210
211     return (unsigned char)!mute;
212 }
213
214 PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) {
215
216     if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || 1 < mute < 0)
217         return;
218
219     if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm))
220         snd_mixer_selem_set_playback_switch_all (dev_ctx[num]->mixer_elm, !mute);
221 }
222
223 PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
224
225     if (!dev_ctx || !dev_ctx[num])
226         return;
227
228     snd_pcm_hw_params_set_rate_near (dev_ctx[num]->dev, dev_ctx[num]->params, &rate, 0);
229 }
230
231 PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) {
232
233     if (!dev_ctx || !dev_ctx[num])
234         return;
235
236     snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels);
237 }
238
239  /* ---- LOCAL THREADED FUNCTIONS ---- */
240
241 STATIC void* _play_thread_fn (void *ctx) {
242
243     dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
244     FILE *file = NULL;
245     char *buf = NULL;
246     long size;
247     int frames, res;
248
249     file = fopen (AUDIO_BUFFER, "rb");
250
251     while (dev_ctx->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
252         fseek (file, 0, SEEK_END);
253         size = ftell (file);
254         buf = (char*) realloc (buf, size * sizeof(char));
255         frames = (size * sizeof(char)) / 4;
256
257         fseek (file, 0, SEEK_SET);
258         fread (buf, 1, size, file);
259         fflush (file);
260
261         if ((res = snd_pcm_writei (dev_ctx->dev, buf, frames)) != frames) {
262             snd_pcm_recover (dev_ctx->dev, res, 0);
263             snd_pcm_prepare (dev_ctx->dev);
264         }
265         /* snd_pcm_drain (dev_ctx->dev); */
266     }
267     if (buf) free(buf);
268     if (file) fclose(file);
269
270     dev_ctx->thr_finished = 1;
271     return 0;
272 }