2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Manuel Bachmann"
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.
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.
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/>.
19 #include "audio-api.h"
20 #include "audio-alsa.h"
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
33 PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
36 snd_pcm_hw_params_t *params;
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;
45 if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
48 snd_pcm_hw_params_malloc (¶ms);
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);
58 snd_pcm_prepare (dev);
60 snd_mixer_open (&mixer, 0);
61 if (snd_mixer_attach (mixer, name) < 0) {
62 snd_pcm_hw_params_free (params);
65 snd_mixer_selem_register (mixer, NULL, NULL);
66 snd_mixer_load (mixer);
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");
72 mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
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)
81 snd_mixer_selem_get_id (mixer_elm, mixer_sid);
82 if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {
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);
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);
99 /* allocate the global array if it hasn't been done */
101 dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
102 dev_ctx[0] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
103 dev_ctx[0]->name = NULL;
104 dev_ctx[0]->dev = NULL;
107 /* is a card with similar name already opened ? */
108 for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
109 if (dev_ctx[num]->name &&
110 !strcmp (dev_ctx[num]->name, name))
115 /* it's not... let us add it to the global array */
116 dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (num+1)*sizeof(dev_ctx_T));
117 dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T));
118 dev_ctx[num]->name = strdup (name);
119 dev_ctx[num]->dev = dev;
120 dev_ctx[num]->params = params;
121 dev_ctx[num]->mixer_elm = mixer_elm;
122 dev_ctx[num]->mixer_elm_m = mixer_elm_m;
123 dev_ctx[num]->vol_max = vol_max;
124 dev_ctx[num]->vol = vol;
125 dev_ctx[num]->thr_should_run = 0;
126 dev_ctx[num]->thr_finished = 0;
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);
137 PUBLIC void _alsa_free (const char *name) {
141 for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
142 if (dev_ctx[num]->name &&
143 !strcmp (dev_ctx[num]->name, name)) {
144 snd_pcm_close (dev_ctx[num]->dev);
145 snd_pcm_hw_params_free (dev_ctx[num]->params);
146 free (dev_ctx[num]->name);
147 dev_ctx[num]->name = NULL;
148 dev_ctx[num]->dev = NULL;
155 PUBLIC void _alsa_play (unsigned int num) {
157 if (!dev_ctx || !dev_ctx[num] || dev_ctx[num]->thr_should_run ||
158 access (AUDIO_BUFFER, F_OK) == -1)
161 dev_ctx[num]->thr_should_run = 1;
162 dev_ctx[num]->thr_finished = 0;
163 pthread_create(&dev_ctx[num]->thr, NULL, _play_thread_fn, (void*)dev_ctx[num]);
166 PUBLIC void _alsa_stop (unsigned int num) {
168 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->thr_should_run)
171 /* stop the "while" loop in thread */
172 dev_ctx[num]->thr_should_run = 0;
174 while (!dev_ctx[num]->thr_finished)
177 pthread_join(dev_ctx[num]->thr, NULL);
180 PUBLIC int _alsa_get_volume (unsigned int num, unsigned int channel) {
182 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
185 snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SCHANNELS[channel], &dev_ctx[num]->vol);
187 return (int)(dev_ctx[num]->vol*100)/dev_ctx[num]->vol_max;
190 PUBLIC void _alsa_set_volume (unsigned int num, unsigned int channel, int vol) {
192 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm ||
196 snd_mixer_selem_set_playback_volume (dev_ctx[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx[num]->vol_max)/100);
200 PUBLIC void _alsa_set_volume_all (unsigned int num, int vol) {
202 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm ||
205 snd_mixer_selem_set_playback_volume_all (dev_ctx[num]->mixer_elm, (vol*dev_ctx[num]->vol_max)/100);
208 PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
211 snd_mixer_elem_t *elm_m;
213 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
216 dev_ctx[num]->mixer_elm_m ? (elm_m = dev_ctx[num]->mixer_elm_m) :
217 (elm_m = dev_ctx[num]->mixer_elm);
219 if (snd_mixer_selem_has_playback_switch (elm_m)) {
220 snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
221 snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
224 if (dev_ctx[num]->mixer_elm_m)
225 return (unsigned char)mute;
227 return (unsigned char)!mute;
230 PUBLIC void _alsa_set_mute (unsigned int num, unsigned char tomute) {
232 snd_mixer_elem_t *elm_m;
235 if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || 1 < tomute < 0)
238 if (dev_ctx[num]->mixer_elm_m) {
239 elm_m = dev_ctx[num]->mixer_elm_m;
242 elm_m = dev_ctx[num]->mixer_elm;
246 if (snd_mixer_selem_has_playback_switch (elm_m))
247 snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
250 PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
252 if (!dev_ctx || !dev_ctx[num])
255 snd_pcm_hw_params_set_rate_near (dev_ctx[num]->dev, dev_ctx[num]->params, &rate, 0);
258 PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) {
260 if (!dev_ctx || !dev_ctx[num])
263 snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels);
266 /* ---- LOCAL THREADED FUNCTIONS ---- */
268 STATIC void* _play_thread_fn (void *ctx) {
270 dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
276 file = fopen (AUDIO_BUFFER, "rb");
278 while (dev_ctx->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
279 fseek (file, 0, SEEK_END);
281 buf = (char*) realloc (buf, size * sizeof(char));
282 frames = (size * sizeof(char)) / 4;
284 fseek (file, 0, SEEK_SET);
285 fread (buf, 1, size, file);
288 if ((res = snd_pcm_writei (dev_ctx->dev, buf, frames)) != frames) {
289 snd_pcm_recover (dev_ctx->dev, res, 0);
290 snd_pcm_prepare (dev_ctx->dev);
292 /* snd_pcm_drain (dev_ctx->dev); */
295 if (file) fclose(file);
297 dev_ctx->thr_finished = 1;