Add support for VFS caching.
[herrie-working.git] / herrie / src / audio_output_alsa.c
blobe1988d31a8597b98f51d8e89b3fee960f2200cbd
1 /*
2 * Copyright (c) 2006-2011 Ed Schouten <ed@80386.nl>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 /**
27 * @file audio_output_alsa.c
28 * @brief ALSA audio output driver.
31 #include "stdinc.h"
33 #include <alsa/asoundlib.h>
35 #include "audio_file.h"
36 #include "audio_output.h"
37 #include "config.h"
38 #include "gui.h"
40 /**
41 * @brief Handle to the audio device obtained from ALSA.
43 static snd_pcm_t *devhnd;
44 /**
45 * @brief the amount of channels that is currently used for playback.
47 static unsigned int channels = 0;
48 /**
49 * @brief the sample rate that is currently used for playback.
51 static unsigned int srate = 0;
52 #ifdef BUILD_VOLUME
53 /**
54 * @brief Handle to the ALSA mixer.
56 static snd_mixer_t *mixhnd = NULL;
57 /**
58 * @brief ALSA mixer element for the audio device.
60 static snd_mixer_elem_t *elem;
61 #endif /* BUILD_VOLUME */
63 /**
64 * @brief Alter the audio output parameters of the audio output device.
66 static int
67 audio_output_apply_hwparams(void)
69 snd_pcm_hw_params_t *devparam;
71 snd_pcm_hw_params_alloca(&devparam);
73 /* Acquire the standard parameters */
74 if (snd_pcm_hw_params_any(devhnd, devparam) != 0)
75 return (-1);
77 /* Set the access method - XXX: mmap */
78 if (snd_pcm_hw_params_set_access(devhnd, devparam,
79 SND_PCM_ACCESS_RW_INTERLEAVED) != 0)
80 return (-1);
82 /* Output format */
83 if (snd_pcm_hw_params_set_format(devhnd, devparam,
84 SND_PCM_FORMAT_S16) != 0)
85 return (-1);
86 /* Sample rate */
87 if (snd_pcm_hw_params_set_rate_near(devhnd, devparam, &srate, NULL) != 0)
88 return (-1);
89 /* Channels */
90 if (snd_pcm_hw_params_set_channels(devhnd, devparam, channels) != 0)
91 return (-1);
93 /* Drain current data and make sure we aren't underrun */
94 snd_pcm_drain(devhnd);
96 /* Apply values */
97 if (snd_pcm_hw_params(devhnd, devparam) != 0)
98 return (-1);
100 return (0);
103 #ifdef BUILD_VOLUME
105 * @brief Open the ALSA mixer device, making it possible to adjust the
106 * volume.
108 static void
109 audio_output_volume_open(void)
111 snd_mixer_selem_id_t *mixer;
113 /* Open mixer device */
114 if (snd_mixer_open(&mixhnd, 0) != 0)
115 return;
116 if (snd_mixer_attach(mixhnd,
117 config_getopt("audio.output.alsa.device")) != 0)
118 goto bad;
119 if (snd_mixer_selem_register(mixhnd, NULL, NULL) < 0)
120 goto bad;
121 if (snd_mixer_load(mixhnd) < 0)
122 goto bad;
124 /* Obtain mixer element */
125 snd_mixer_selem_id_alloca(&mixer);
126 snd_mixer_selem_id_set_name(mixer, config_getopt("audio.output.alsa.mixer"));
128 elem = snd_mixer_find_selem(mixhnd, mixer);
129 if (elem != NULL)
130 return;
131 bad:
132 snd_mixer_close(mixhnd);
133 mixhnd = NULL;
135 #endif /* BUILD_VOLUME */
138 audio_output_open(void)
140 /* Open the device */
141 if (snd_pcm_open(&devhnd, config_getopt("audio.output.alsa.device"),
142 SND_PCM_STREAM_PLAYBACK, 0) != 0)
143 goto error;
145 #ifdef BUILD_VOLUME
146 audio_output_volume_open();
147 #endif /* BUILD_VOLUME */
149 return (0);
150 error:
151 g_printerr("%s\n", _("Cannot open the audio device."));
152 return (-1);
156 audio_output_play(struct audio_file *fd)
158 int16_t buf[8192];
159 snd_pcm_sframes_t ret, len, done = 0;
161 if ((len = audio_file_read(fd, buf, sizeof buf / sizeof(int16_t))) == 0)
162 return (-1);
164 if (fd->channels != channels || fd->srate != srate) {
165 /* Apply the new values */
166 channels = fd->channels;
167 srate = fd->srate;
168 if (audio_output_apply_hwparams() != 0) {
169 gui_msgbar_warn(_("Sample rate or amount of channels not supported."));
170 /* Invalidate the old settings */
171 srate = 0;
172 return (-1);
176 /* ALSA measures in sample lengths */
177 len /= fd->channels;
179 /* Our complex error handling for snd_pcm_writei() */
180 while (done < len) {
181 ret = snd_pcm_writei(devhnd, buf + (done * fd->channels), len - done);
182 if (ret == -EPIPE) {
183 /* Buffer underrun. Try again. */
184 if (snd_pcm_prepare(devhnd) != 0)
185 return (-1);
186 continue;
187 } else if (ret <= 0) {
188 /* Some other strange error. */
189 return (-1);
192 done += ret;
195 return (0);
198 void
199 audio_output_close(void)
201 snd_pcm_close(devhnd);
202 #ifdef BUILD_VOLUME
203 if (mixhnd != NULL)
204 snd_mixer_close(mixhnd);
205 #endif /* BUILD_VOLUME */
208 #ifdef BUILD_VOLUME
210 * @brief Adjust the audio output by a certain percentage and return the
211 * new value.
213 static int
214 audio_output_volume_adjust(int n)
216 int delta;
217 long min, max;
218 long right, left;
220 if (mixhnd == NULL)
221 return (-1);
223 /* Allow other applications to change mixer */
224 snd_mixer_handle_events(mixhnd);
226 /* Obtain volume range boundaries */
227 if (snd_mixer_selem_get_playback_volume_range(elem, &min, &max) != 0)
228 return (-1);
229 if (min >= max)
230 return (-1);
232 /* Convert delta from percent to steps */
233 delta = (n * (max - min)) / 100;
234 if (delta == 0)
235 delta = n > 0 ? 1 : -1;
237 /* Obtain current volume */
238 if (snd_mixer_selem_get_playback_volume(elem,
239 SND_MIXER_SCHN_FRONT_LEFT, &left) != 0)
240 return (-1);
241 if (snd_mixer_selem_get_playback_volume(elem,
242 SND_MIXER_SCHN_FRONT_RIGHT, &right) != 0)
243 return (-1);
245 left = left + delta;
246 left = CLAMP(left, min, max);
247 right = right + delta;
248 right = CLAMP(right, min, max);
250 /* Set new volume */
251 if (snd_mixer_selem_set_playback_volume(elem,
252 SND_MIXER_SCHN_FRONT_LEFT, left) != 0)
253 return (-1);
254 if (snd_mixer_selem_set_playback_volume(elem,
255 SND_MIXER_SCHN_FRONT_RIGHT, right) != 0)
256 return (-1);
258 /* Convert volume back to a percentage */
259 return (((left + right) / 2 - min) * 100) / (max - min);
263 audio_output_volume_up(void)
265 return audio_output_volume_adjust(4);
269 audio_output_volume_down(void)
271 return audio_output_volume_adjust(-4);
273 #endif /* BUILD_VOLUME */