2 * Copyright (C) 2015, 2016 "IoT.bzh"
3 * Author "Manuel Bachmann"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "audio-api.h"
19 #include "audio-alsa.h"
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
32 static struct dev_ctx_alsa **dev_ctx_a = NULL;
35 unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
38 snd_pcm_hw_params_t *params;
40 snd_mixer_selem_id_t *mixer_sid;
41 snd_mixer_elem_t *mixer_elm;
42 snd_mixer_elem_t *mixer_elm_m;
43 unsigned int rate = 22050;
44 long vol, vol_min, vol_max;
47 if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
50 snd_pcm_hw_params_malloc (¶ms);
51 snd_pcm_hw_params_any (dev, params);
52 snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
53 snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
54 snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0);
55 snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
56 if (snd_pcm_hw_params (dev, params) < 0) {
57 snd_pcm_hw_params_free (params);
60 snd_pcm_prepare (dev);
62 snd_mixer_open (&mixer, 0);
63 if (snd_mixer_attach (mixer, name) < 0) {
64 snd_pcm_hw_params_free (params);
67 snd_mixer_selem_register (mixer, NULL, NULL);
68 snd_mixer_load (mixer);
70 snd_mixer_selem_id_alloca (&mixer_sid);
71 snd_mixer_selem_id_set_index (mixer_sid, 0);
72 snd_mixer_selem_id_set_name (mixer_sid, "Master");
74 mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
78 /* no "Master" mixer ; we are probably on a board... search ! */
79 for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
80 mixer_elm = snd_mixer_elem_next (mixer_elm)) {
81 if (snd_mixer_elem_info (mixer_elm) < 0)
83 snd_mixer_selem_get_id (mixer_elm, mixer_sid);
84 if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {
86 /* this is Porter... let us found the specific mute switch */
87 snd_mixer_selem_id_set_index (mixer_sid, 0);
88 snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute");
89 mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid);
97 snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
98 snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
101 /* allocate the global array if it hasn't been done */
103 dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T*));
104 dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
105 dev_ctx_a[0]->name = NULL;
106 dev_ctx_a[0]->dev = NULL;
109 /* is a card with similar name already opened ? */
110 for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) {
111 if (dev_ctx_a[num]->name &&
112 !strcmp (dev_ctx_a[num]->name, name))
117 /* it's not... let us add it to the global array */
118 dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T*));
119 dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
120 dev_ctx_a[num]->name = strdup (name);
121 dev_ctx_a[num]->dev = dev;
122 dev_ctx_a[num]->params = params;
123 dev_ctx_a[num]->mixer_elm = mixer_elm;
124 dev_ctx_a[num]->mixer_elm_m = mixer_elm_m;
125 dev_ctx_a[num]->vol_max = vol_max;
126 dev_ctx_a[num]->vol = vol;
127 dev_ctx_a[num]->thr_should_run = 0;
128 dev_ctx_a[num]->thr_finished = 0;
130 /* make the client context aware of current card state */
131 for (i = 0; i < 8; i++)
132 ctx->volume[i] = _alsa_get_volume (num, i);
133 ctx->mute = _alsa_get_mute (num);
135 ctx->name = strdup (name);
137 fprintf (stderr, "Successfully initialized ALSA backend.\n");
142 void _alsa_free (const char *name) {
146 for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) {
147 if (dev_ctx_a[num]->name &&
148 !strcmp (dev_ctx_a[num]->name, name)) {
149 snd_pcm_close (dev_ctx_a[num]->dev);
150 snd_pcm_hw_params_free (dev_ctx_a[num]->params);
151 free (dev_ctx_a[num]->name);
152 dev_ctx_a[num]->dev = NULL;
153 dev_ctx_a[num]->name = NULL;
154 free (dev_ctx_a[num]);
160 void _alsa_play (int num) {
162 if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run ||
163 access (AUDIO_BUFFER, F_OK) == -1)
166 dev_ctx_a[num]->thr_should_run = 1;
167 dev_ctx_a[num]->thr_finished = 0;
168 pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]);
171 void _alsa_stop (int num) {
173 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run)
176 /* stop the "while" loop in thread */
177 dev_ctx_a[num]->thr_should_run = 0;
179 while (!dev_ctx_a[num]->thr_finished)
182 pthread_join (dev_ctx_a[num]->thr, NULL);
185 unsigned int _alsa_get_volume (int num, unsigned int channel) {
187 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
190 snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol);
192 return (unsigned int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max;
195 void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) {
197 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
201 snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100);
204 void _alsa_set_volume_all (int num, unsigned int vol) {
206 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
209 snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100);
212 unsigned char _alsa_get_mute (int num) {
215 snd_mixer_elem_t *elm_m;
217 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
220 dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) :
221 (elm_m = dev_ctx_a[num]->mixer_elm);
223 if (snd_mixer_selem_has_playback_switch (elm_m)) {
224 snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
225 snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
228 if (dev_ctx_a[num]->mixer_elm_m)
229 return (unsigned char)mute;
231 return (unsigned char)!mute;
234 void _alsa_set_mute (int num, unsigned char tomute) {
236 snd_mixer_elem_t *elm_m;
239 if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute)
242 if (dev_ctx_a[num]->mixer_elm_m) {
243 elm_m = dev_ctx_a[num]->mixer_elm_m;
246 elm_m = dev_ctx_a[num]->mixer_elm;
250 if (snd_mixer_selem_has_playback_switch (elm_m))
251 snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
254 void _alsa_set_rate (int num, unsigned int rate) {
256 if (!dev_ctx_a || !dev_ctx_a[num])
259 snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0);
262 void _alsa_set_channels (int num, unsigned int channels) {
264 if (!dev_ctx_a || !dev_ctx_a[num])
267 snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels);
270 /* ---- LOCAL THREADED FUNCTIONS ---- */
272 void* _alsa_play_thread_fn (void *ctx) {
274 dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx;
280 file = fopen (AUDIO_BUFFER, "rb");
282 while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
283 fseek (file, 0, SEEK_END);
285 buf = (char*) realloc (buf, size * sizeof(char));
286 frames = (size * sizeof(char)) / 4;
288 fseek (file, 0, SEEK_SET);
289 fread (buf, 1, size, file);
292 if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) {
293 snd_pcm_recover (dev_ctx_a->dev, res, 0);
294 snd_pcm_prepare (dev_ctx_a->dev);
296 /* snd_pcm_drain (dev_ctx->dev); */
299 if (file) fclose(file);
301 dev_ctx_a->thr_finished = 1;