comparison src/alsaplay.c @ 3308:34cfe24248f6

[xemacs-hg @ 2006-03-27 17:40:45 by james] Add support for sound using the ALSA library. See xemacs-patches message with ID <m3slp77hui.fsf@jerrypc.cs.usu.edu>.
author james
date Mon, 27 Mar 2006 17:40:59 +0000
parents
children f63530aa6c68
comparison
equal deleted inserted replaced
3307:d81b1754aab1 3308:34cfe24248f6
1 /* Play sound with the ALSA library
2 Copyright (C) 2006 Jerry James.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Synched up with: Not in FSF. */
22
23 /* TODO: Support asynchronous sound playing; see the NAS support in sound.c */
24
25 #include <config.h>
26 #include "lisp.h"
27 #include "sound.h"
28 #include "sysfile.h"
29
30 /* We can't just include <alsa/asoundlib.h> because it tries to redefine
31 * several symbols defined by the previous header files.
32 */
33 #include <alsa/input.h>
34 #include <alsa/output.h>
35 #include <alsa/conf.h>
36 #include <alsa/global.h>
37 #include <alsa/pcm.h>
38 #include <alsa/error.h>
39 #include <alsa/hwdep.h>
40 #include <alsa/rawmidi.h>
41 #include <alsa/control.h>
42 #include <alsa/mixer.h>
43
44 struct mixer_state
45 {
46 snd_mixer_t *mixer;
47 snd_mixer_elem_t *vol_ctl;
48
49 /* Which channels need the old volume restored */
50 int reset_front_left;
51 int reset_front_center;
52 int reset_front_right;
53 int reset_rear_left;
54 int reset_rear_center;
55 int reset_rear_right;
56 int reset_side_left;
57 int reset_side_right;
58 int reset_woofer;
59
60 /* Old volumes for the channels */
61 long front_left_vol;
62 long front_center_vol;
63 long front_right_vol;
64 long rear_left_vol;
65 long rear_center_vol;
66 long rear_right_vol;
67 long side_left_vol;
68 long side_right_vol;
69 long woofer_vol;
70 };
71
72 /* Assemble a machine half-word in little-endian order */
73 #define HALF_LE(arr,start) (arr[start] + (arr[start + 1] << 8))
74
75 /* Assemble a machine word in little-endian order */
76 #define WORD_LE(arr,start) (arr[start] + (arr[start + 1] << 8) + \
77 (arr[start + 2] << 16) + (arr[start + 3] << 24))
78
79 /* Assemble a machine word in big-endian order */
80 #define WORD_BE(arr,start) ((arr[start] << 24) + (arr[start + 1] << 16) + \
81 (arr[start + 2] << 8) + arr[start + 3])
82
83 /* This function was inspired by miscplay.c.
84 * Examine sound data to determine its format.
85 *
86 * TODO: Detect other formats that ALSA can play, such as GSM and MPEG.
87 */
88 static snd_pcm_format_t
89 analyze_format (const Binbyte *format, int *speed, int *tracks)
90 {
91 if (!memcmp (format, "Creative Voice File\x1A\x1A\x00", 22) &&
92 HALF_LE (format, 22) == ((0x1233 - HALF_LE (format, 24)) & 0xFFFF))
93 {
94 /* VOC */
95 *speed = 8000;
96 *tracks = 2;
97 return SND_PCM_FORMAT_U8;
98 }
99 else if (!memcmp (format, "RIFF", 4) && !memcmp (format + 8, "WAVEfmt ", 8))
100 {
101 /* WAVE */
102 *speed = WORD_LE (format, 24);
103 *tracks = format[22];
104 return format[32] / format[22] == 1
105 ? SND_PCM_FORMAT_U8
106 : SND_PCM_FORMAT_S16_LE;
107 }
108 else if (!memcmp (format, ".snd", 4))
109 {
110 /* Sun/NeXT Audio (big endian) */
111 if (WORD_BE (format, 4) < 24)
112 {
113 *speed = 8000;
114 *tracks = 1;
115 return SND_PCM_FORMAT_MU_LAW;
116 }
117 *speed = WORD_BE (format, 16);
118 *tracks = format[23];
119 if (!memcmp (format + 12, "\000\000\000", 3))
120 {
121 switch (format[15])
122 {
123 case 1:
124 case 17:
125 case 29:
126 return SND_PCM_FORMAT_MU_LAW;
127 case 2:
128 return SND_PCM_FORMAT_S8;
129 case 3:
130 return SND_PCM_FORMAT_S16_BE;
131 case 4:
132 return SND_PCM_FORMAT_S24_BE;
133 case 5:
134 return SND_PCM_FORMAT_S32_BE;
135 case 23:
136 case 24:
137 case 25:
138 case 26:
139 return SND_PCM_FORMAT_IMA_ADPCM;
140 case 27:
141 return SND_PCM_FORMAT_A_LAW;
142 default:
143 break;
144 }
145 }
146 return SND_PCM_FORMAT_UNKNOWN;
147 }
148 else if (!memcmp (format, ".sd", 4))
149 {
150 /* DEC Audio (little endian) */
151 if (WORD_LE (format, 4) < 24)
152 {
153 *speed = 8000;
154 *tracks = 1;
155 return SND_PCM_FORMAT_MU_LAW;
156 }
157 *speed = WORD_LE (format, 16);
158 *tracks = format[20];
159 if (!memcmp (format + 13, "\000\000\000", 3))
160 {
161 switch (format[12])
162 {
163 case 1:
164 case 17:
165 case 29:
166 return SND_PCM_FORMAT_MU_LAW;
167 case 2:
168 return SND_PCM_FORMAT_S8;
169 case 3:
170 return SND_PCM_FORMAT_S16_LE;
171 case 4:
172 return SND_PCM_FORMAT_S24_LE;
173 case 5:
174 return SND_PCM_FORMAT_S32_LE;
175 case 23:
176 case 24:
177 case 25:
178 case 26:
179 return SND_PCM_FORMAT_IMA_ADPCM;
180 case 27:
181 return SND_PCM_FORMAT_A_LAW;
182 default:
183 break;
184 }
185 }
186 return SND_PCM_FORMAT_UNKNOWN;
187 }
188 else
189 {
190 /* We don't know what it is. Guess that it is mono audio in unsigned
191 * byte format. Maybe we should error if we reach this point.
192 */
193 *speed = 8000;
194 *tracks = 1;
195 return SND_PCM_FORMAT_U8;
196 }
197 }
198
199 /* Set the volume: if any errors occur, we accept the existing volume */
200 static void
201 set_volume (struct mixer_state *mix, int volume)
202 {
203 snd_mixer_selem_id_t *volume_id;
204 long min_vol, max_vol, dev_vol;
205
206 if (snd_mixer_open (&mix->mixer, 0) < 0)
207 return;
208
209 if (snd_mixer_attach (mix->mixer, "default") < 0)
210 return;
211
212 if (snd_mixer_selem_register (mix->mixer, NULL, NULL) < 0)
213 return;
214
215 if (snd_mixer_load (mix->mixer) < 0)
216 return;
217
218 snd_mixer_selem_id_alloca (&volume_id);
219 snd_mixer_selem_id_set_name (volume_id, "PCM");
220
221 if ((mix->vol_ctl = snd_mixer_find_selem (mix->mixer, volume_id)) == NULL)
222 {
223 snd_mixer_selem_id_set_name (volume_id, "Master");
224 if ((mix->vol_ctl = snd_mixer_find_selem (mix->mixer, volume_id))
225 == NULL)
226 return;
227 }
228
229 /* Translate the Lisp volume range to the device volume range */
230 if (snd_mixer_selem_get_playback_volume_range (mix->vol_ctl, &min_vol,
231 &max_vol) < 0)
232 return;
233
234 dev_vol = volume * (max_vol - min_vol) / 100 + min_vol;
235
236 /* Record the old volumes */
237 if (snd_mixer_selem_get_playback_volume
238 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_LEFT, &mix->front_left_vol) >= 0)
239 mix->reset_front_left = 1;
240
241 if (snd_mixer_selem_get_playback_volume
242 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_CENTER, &mix->front_center_vol) >= 0)
243 mix->reset_front_center = 1;
244
245 if (snd_mixer_selem_get_playback_volume
246 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_RIGHT, &mix->front_right_vol) >= 0)
247 mix->reset_front_right = 1;
248
249 if (snd_mixer_selem_get_playback_volume
250 (mix->vol_ctl, SND_MIXER_SCHN_REAR_LEFT, &mix->rear_left_vol) >= 0)
251 mix->reset_rear_left = 1;
252
253 if (snd_mixer_selem_get_playback_volume
254 (mix->vol_ctl, SND_MIXER_SCHN_REAR_CENTER, &mix->rear_center_vol) >= 0)
255 mix->reset_rear_center = 1;
256
257 if (snd_mixer_selem_get_playback_volume
258 (mix->vol_ctl, SND_MIXER_SCHN_REAR_RIGHT, &mix->rear_right_vol) >= 0)
259 mix->reset_rear_right = 1;
260
261 if (snd_mixer_selem_get_playback_volume
262 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_LEFT, &mix->side_left_vol) >= 0)
263 mix->reset_side_left = 1;
264
265 if (snd_mixer_selem_get_playback_volume
266 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_RIGHT, &mix->side_right_vol) >= 0)
267 mix->reset_side_right = 1;
268
269 if (snd_mixer_selem_get_playback_volume
270 (mix->vol_ctl, SND_MIXER_SCHN_WOOFER, &mix->woofer_vol) >= 0)
271 mix->reset_woofer = 1;
272
273 /* Set the volume */
274 snd_mixer_selem_set_playback_volume_all (mix->vol_ctl, dev_vol);
275 }
276
277 static void
278 reset_volume (const struct mixer_state *mix)
279 {
280 if (mix->reset_front_left)
281 snd_mixer_selem_set_playback_volume
282 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_LEFT, mix->front_left_vol);
283
284 if (mix->reset_front_center)
285 snd_mixer_selem_set_playback_volume
286 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_CENTER, mix->front_center_vol);
287
288 if (mix->reset_front_right)
289 snd_mixer_selem_set_playback_volume
290 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_RIGHT, mix->front_right_vol);
291
292 if (mix->reset_rear_left)
293 snd_mixer_selem_set_playback_volume
294 (mix->vol_ctl, SND_MIXER_SCHN_REAR_LEFT, mix->rear_left_vol);
295
296 if (mix->reset_rear_center)
297 snd_mixer_selem_set_playback_volume
298 (mix->vol_ctl, SND_MIXER_SCHN_REAR_CENTER, mix->rear_center_vol);
299
300 if (mix->reset_rear_right)
301 snd_mixer_selem_set_playback_volume
302 (mix->vol_ctl, SND_MIXER_SCHN_REAR_RIGHT, mix->rear_right_vol);
303
304 if (mix->reset_side_left)
305 snd_mixer_selem_set_playback_volume
306 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_LEFT, mix->side_left_vol);
307
308 if (mix->reset_side_right)
309 snd_mixer_selem_set_playback_volume
310 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_RIGHT, mix->side_right_vol);
311
312 if (mix->reset_woofer)
313 snd_mixer_selem_set_playback_volume
314 (mix->vol_ctl, SND_MIXER_SCHN_WOOFER, mix->woofer_vol);
315
316 snd_mixer_close (mix->mixer);
317 }
318
319 int
320 alsa_play_sound_data (const Binbyte *data, int length, int volume)
321 {
322 snd_pcm_t *pcm_handle;
323 snd_pcm_hw_params_t *hwparams;
324 snd_pcm_format_t format;
325 struct mixer_state mix;
326 int speed, tracks, err;
327
328 /* Set the PCM parameters */
329 if ((err = snd_pcm_open (&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK,
330 0)) < 0)
331 goto error_pcm_open;
332
333 snd_pcm_hw_params_alloca (&hwparams);
334
335 if ((err = snd_pcm_hw_params_any (pcm_handle, hwparams)) < 0)
336 goto error_pcm;
337
338 format = analyze_format (data, &speed, &tracks);
339
340 if ((err = snd_pcm_hw_params_set_access (pcm_handle, hwparams,
341 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
342 goto error_pcm;
343
344 if ((err = snd_pcm_hw_params_set_format (pcm_handle, hwparams, format)) < 0)
345 goto error_pcm;
346
347 if ((err = snd_pcm_hw_params_set_rate (pcm_handle, hwparams, speed, 0)) < 0)
348 goto error_pcm;
349
350 if ((err = snd_pcm_hw_params_set_channels (pcm_handle, hwparams, tracks))
351 < 0)
352 goto error_pcm;
353
354 if ((err = snd_pcm_hw_params (pcm_handle, hwparams)) < 0)
355 goto error_pcm;
356
357 /* Set the volume */
358 memset (&mix, 0, sizeof (mix));
359 set_volume (&mix, volume);
360
361 /* Play the sound */
362 if ((err = snd_pcm_writei (pcm_handle, data, length)) < 0)
363 goto error_mixer;
364
365 /* Put the volume back the way it used to be */
366 reset_volume (&mix);
367
368 /* Release resources */
369 snd_pcm_close (pcm_handle);
370 return 1;
371
372 error_mixer:
373 reset_volume (&mix);
374 error_pcm:
375 snd_pcm_close (pcm_handle);
376 error_pcm_open:
377 sound_perror (snd_strerror (err));
378 return 0;
379 }
380
381 /* Read the sound file into an internal buffer, then call
382 * alsa_play_sound_data.
383 */
384 int
385 alsa_play_sound_file (const Extbyte *sound_file, int volume)
386 {
387 Binbyte *data;
388 int fd, retval;
389 struct stat st;
390
391 fd = retry_open (sound_file, O_RDONLY, 0);
392 if (fd < 0) {
393 sound_perror (sound_file);
394 return 0;
395 }
396
397 qxe_fstat (fd, &st);
398 data = xnew_array (Binbyte, st.st_size);
399 retry_read (fd, data, st.st_size);
400 retry_close (fd);
401 retval = alsa_play_sound_data (data, st.st_size, volume);
402 xfree (data, Binbyte);
403 return retval;
404 }