chiark / gitweb /
be716cfcde8688fc5eff6d42bed01917f8f41f2b
[disorder] / lib / uaudio-alsa.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2009 Richard Kettlewell
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 /** @file lib/uaudio-alsa.c
19  * @brief Support for ALSA backend */
20 #include "common.h"
21
22 #if HAVE_ALSA_ASOUNDLIB_H
23
24 #include <alsa/asoundlib.h>
25
26 #include "mem.h"
27 #include "log.h"
28 #include "uaudio.h"
29
30 /** @brief The current PCM handle */
31 static snd_pcm_t *alsa_pcm;
32
33 static const char *const alsa_options[] = {
34   "device",
35   "mixer-control",
36   "mixer-channel",
37   NULL
38 };
39
40 /** @brief Mixer handle */
41 snd_mixer_t *alsa_mixer_handle;
42
43 /** @brief Mixer control */
44 static snd_mixer_elem_t *alsa_mixer_elem;
45
46 /** @brief Left channel */
47 static snd_mixer_selem_channel_id_t alsa_mixer_left;
48
49 /** @brief Right channel */
50 static snd_mixer_selem_channel_id_t alsa_mixer_right;
51
52 /** @brief Minimum level */
53 static long alsa_mixer_min;
54
55 /** @brief Maximum level */
56 static long alsa_mixer_max;
57
58 /** @brief Actually play sound via ALSA */
59 static size_t alsa_play(void *buffer, size_t samples) {
60   int err;
61   /* ALSA wants 'frames', where frame = several concurrently played samples */
62   const snd_pcm_uframes_t frames = samples / uaudio_channels;
63
64   snd_pcm_sframes_t rc = snd_pcm_writei(alsa_pcm, buffer, frames);
65   if(rc < 0) {
66     switch(rc) {
67     case -EPIPE:
68       if((err = snd_pcm_prepare(alsa_pcm)))
69         fatal(0, "error calling snd_pcm_prepare: %d", err);
70       return 0;
71     case -EAGAIN:
72       return 0;
73     default:
74       fatal(0, "error calling snd_pcm_writei: %d", (int)rc);
75     }
76   }
77   return rc * uaudio_channels;
78 }
79
80 /** @brief Open the ALSA sound device */
81 static void alsa_open(void) {
82   const char *device = uaudio_get("device", "default");
83   int err;
84
85   if((err = snd_pcm_open(&alsa_pcm,
86                          device,
87                          SND_PCM_STREAM_PLAYBACK,
88                          0)))
89     fatal(0, "error from snd_pcm_open: %d", err);
90   snd_pcm_hw_params_t *hwparams;
91   snd_pcm_hw_params_alloca(&hwparams);
92   if((err = snd_pcm_hw_params_any(alsa_pcm, hwparams)) < 0)
93     fatal(0, "error from snd_pcm_hw_params_any: %d", err);
94   if((err = snd_pcm_hw_params_set_access(alsa_pcm, hwparams,
95                                          SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
96     fatal(0, "error from snd_pcm_hw_params_set_access: %d", err);
97   int sample_format;
98   if(uaudio_bits == 16)
99     sample_format = uaudio_signed ? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U16;
100   else
101     sample_format = uaudio_signed ? SND_PCM_FORMAT_S8 : SND_PCM_FORMAT_U8;
102   if((err = snd_pcm_hw_params_set_format(alsa_pcm, hwparams,
103                                          sample_format)) < 0)
104     fatal(0, "error from snd_pcm_hw_params_set_format (%d): %d",
105           sample_format, err);
106   unsigned rate = uaudio_rate;
107   if((err = snd_pcm_hw_params_set_rate_near(alsa_pcm, hwparams, &rate, 0)) < 0)
108     fatal(0, "error from snd_pcm_hw_params_set_rate_near (%d): %d",
109           rate, err);
110   if((err = snd_pcm_hw_params_set_channels(alsa_pcm, hwparams,
111                                            uaudio_channels)) < 0)
112     fatal(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
113           uaudio_channels, err);
114   if((err = snd_pcm_hw_params(alsa_pcm, hwparams)) < 0)
115     fatal(0, "error calling snd_pcm_hw_params: %d", err);
116   
117 }
118
119 static void alsa_activate(void) {
120   uaudio_thread_activate();
121 }
122
123 static void alsa_deactivate(void) {
124   uaudio_thread_deactivate();
125 }
126   
127 static void alsa_start(uaudio_callback *callback,
128                       void *userdata) {
129   if(uaudio_channels != 1 && uaudio_channels != 2)
130     fatal(0, "asked for %d channels but only support 1 or 2",
131           uaudio_channels); 
132   if(uaudio_bits != 8 && uaudio_bits != 16)
133     fatal(0, "asked for %d bits/channel but only support 8 or 16",
134           uaudio_bits); 
135   alsa_open();
136   uaudio_thread_start(callback, userdata, alsa_play,
137                       32 / uaudio_sample_size,
138                       4096 / uaudio_sample_size);
139 }
140
141 static void alsa_stop(void) {
142   uaudio_thread_stop();
143   snd_pcm_close(alsa_pcm);
144   alsa_pcm = 0;
145 }
146
147 /** @brief Convert a level to a percentage */
148 static int to_percent(long n) {
149   return (n - alsa_mixer_min) * 100 / (alsa_mixer_max - alsa_mixer_min);
150 }
151
152 static void alsa_open_mixer(void) {
153   int err;
154   snd_mixer_selem_id_t *id;
155   const char *device = uaudio_get("device", "default");
156   const char *mixer = uaudio_get("mixer-control", "0");
157   const char *channel = uaudio_get("mixer-channel", "PCM");
158
159   snd_mixer_selem_id_alloca(&id);
160   if((err = snd_mixer_open(&alsa_mixer_handle, 0)))
161     fatal(0, "snd_mixer_open: %s", snd_strerror(err));
162   if((err = snd_mixer_attach(alsa_mixer_handle, config->device)))
163     fatal(0, "snd_mixer_attach %s: %s", config->device, snd_strerror(err));
164   if((err = snd_mixer_selem_register(alsa_mixer_handle,
165                                      0/*options*/, 0/*classp*/)))
166     fatal(0, "snd_mixer_selem_register %s: %s",
167           device, snd_strerror(err));
168   if((err = snd_mixer_load(alsa_mixer_handle)))
169     fatal(0, "snd_mixer_load %s: %s", device, snd_strerror(err));
170   snd_mixer_selem_id_set_name(id, channel);
171   snd_mixer_selem_id_set_index(id, atoi(mixer));
172   if(!(alsa_mixer_elem = snd_mixer_find_selem(alsa_mixer_handle, id)))
173     fatal(0, "device '%s' mixer control '%s,%s' does not exist",
174           device, channel, mixer);
175   if(!snd_mixer_selem_has_playback_volume(alsa_mixer_elem))
176     fatal(0, "device '%s' mixer control '%s,%s' has no playback volume",
177           device, channel, mixer);
178   if(snd_mixer_selem_is_playback_mono(alsa_mixer_elem)) {
179     alsa_mixer_left = alsa_mixer_right = SND_MIXER_SCHN_MONO;
180   } else {
181     alsa_mixer_left = SND_MIXER_SCHN_FRONT_LEFT;
182     alsa_mixer_right = SND_MIXER_SCHN_FRONT_RIGHT;
183   }
184   if(!snd_mixer_selem_has_playback_channel(alsa_mixer_elem,
185                                            alsa_mixer_left)
186      || !snd_mixer_selem_has_playback_channel(alsa_mixer_elem,
187                                               alsa_mixer_right))
188     fatal(0, "device '%s' mixer control '%s,%s' lacks required playback channels",
189           device, channel, mixer);
190   snd_mixer_selem_get_playback_volume_range(alsa_mixer_elem,
191                                             &alsa_mixer_min, &alsa_mixer_max);
192
193 }
194
195 static void alsa_close_mixer(void) {
196   /* TODO alsa_mixer_elem */
197   if(alsa_mixer_handle)
198     snd_mixer_close(alsa_mixer_handle);
199 }
200
201 static void alsa_get_volume(int *left, int *right) {
202   long l, r;
203   int err;
204   
205   if((err = snd_mixer_selem_get_playback_volume(alsa_mixer_elem,
206                                                 alsa_mixer_left, &l))
207      || (err = snd_mixer_selem_get_playback_volume(alsa_mixer_elem,
208                                                    alsa_mixer_right, &r)))
209     fatal(0, "snd_mixer_selem_get_playback_volume: %s", snd_strerror(err));
210   *left = to_percent(l);
211   *right = to_percent(r);
212 }
213
214 static void alsa_set_volume(int *left, int *right) {
215   long l, r;
216   int err;
217   
218   /* Set the volume */
219   if(alsa_mixer_left == alsa_mixer_right) {
220     /* Mono output - just use the loudest */
221     if((err = snd_mixer_selem_set_playback_volume
222         (alsa_mixer_elem, alsa_mixer_left,
223          from_percent(*left > *right ? *left : *right))))
224       fatal(0, "snd_mixer_selem_set_playback_volume: %s", snd_strerror(err));
225   } else {
226     /* Stereo output */
227     if((err = snd_mixer_selem_set_playback_volume
228         (alsa_mixer_elem, alsa_mixer_left, from_percent(*left)))
229        || (err = snd_mixer_selem_set_playback_volume
230            (alsa_mixer_elem, alsa_mixer_right, from_percent(*right))))
231       fatal(0, "snd_mixer_selem_set_playback_volume: %s", snd_strerror(err));
232   }
233   /* Read it back to see what we ended up at */
234   if((err = snd_mixer_selem_get_playback_volume(alsa_mixer_elem,
235                                                 alsa_mixer_left, &l))
236      || (err = snd_mixer_selem_get_playback_volume(alsa_mixer_elem,
237                                                    alsa_mixer_right, &r)))
238     fatal(0, "snd_mixer_selem_get_playback_volume: %s", snd_strerror(err));
239   *left = to_percent(l);
240   *right = to_percent(r);
241 }
242
243 const struct uaudio uaudio_alsa = {
244   .name = "alsa",
245   .options = alsa_options,
246   .start = alsa_start,
247   .stop = alsa_stop,
248   .activate = alsa_activate,
249   .deactivate = alsa_deactivate,
250   .open_mixer = alsa_open_mixer,
251   .close_mixer = alsa_close_mixer,
252   .get_volume = alsa_get_volume,
253   .set_volume = alsa_set_volume,
254 };
255
256 #endif
257
258 /*
259 Local Variables:
260 c-basic-offset:2
261 comment-column:40
262 fill-column:79
263 indent-tabs-mode:nil
264 End:
265 */