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