3308
|
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
|
3335
|
44 #define ALSA_VERSION(major,minor,sub) (((major)<<16) | ((minor)<<8)| (sub))
|
|
45
|
3308
|
46 struct mixer_state
|
|
47 {
|
|
48 snd_mixer_t *mixer;
|
|
49 snd_mixer_elem_t *vol_ctl;
|
|
50
|
|
51 /* Which channels need the old volume restored */
|
|
52 int reset_front_left;
|
|
53 int reset_front_center;
|
|
54 int reset_front_right;
|
|
55 int reset_rear_left;
|
3335
|
56 int reset_rear_right;
|
|
57 int reset_woofer;
|
|
58 #if SND_LIB_VERSION >= ALSA_VERSION (1, 0, 10)
|
3308
|
59 int reset_rear_center;
|
|
60 int reset_side_left;
|
|
61 int reset_side_right;
|
3335
|
62 #endif
|
3308
|
63
|
|
64 /* Old volumes for the channels */
|
|
65 long front_left_vol;
|
|
66 long front_center_vol;
|
|
67 long front_right_vol;
|
|
68 long rear_left_vol;
|
3335
|
69 long rear_right_vol;
|
|
70 long woofer_vol;
|
|
71 #if SND_LIB_VERSION >= ALSA_VERSION (1, 0, 10)
|
3308
|
72 long rear_center_vol;
|
|
73 long side_left_vol;
|
|
74 long side_right_vol;
|
3335
|
75 #endif
|
3308
|
76 };
|
|
77
|
|
78 /* Assemble a machine half-word in little-endian order */
|
|
79 #define HALF_LE(arr,start) (arr[start] + (arr[start + 1] << 8))
|
|
80
|
|
81 /* Assemble a machine word in little-endian order */
|
|
82 #define WORD_LE(arr,start) (arr[start] + (arr[start + 1] << 8) + \
|
|
83 (arr[start + 2] << 16) + (arr[start + 3] << 24))
|
|
84
|
|
85 /* Assemble a machine word in big-endian order */
|
|
86 #define WORD_BE(arr,start) ((arr[start] << 24) + (arr[start + 1] << 16) + \
|
|
87 (arr[start + 2] << 8) + arr[start + 3])
|
|
88
|
|
89 /* This function was inspired by miscplay.c.
|
|
90 * Examine sound data to determine its format.
|
|
91 *
|
|
92 * TODO: Detect other formats that ALSA can play, such as GSM and MPEG.
|
|
93 */
|
|
94 static snd_pcm_format_t
|
|
95 analyze_format (const Binbyte *format, int *speed, int *tracks)
|
|
96 {
|
|
97 if (!memcmp (format, "Creative Voice File\x1A\x1A\x00", 22) &&
|
|
98 HALF_LE (format, 22) == ((0x1233 - HALF_LE (format, 24)) & 0xFFFF))
|
|
99 {
|
|
100 /* VOC */
|
|
101 *speed = 8000;
|
|
102 *tracks = 2;
|
|
103 return SND_PCM_FORMAT_U8;
|
|
104 }
|
|
105 else if (!memcmp (format, "RIFF", 4) && !memcmp (format + 8, "WAVEfmt ", 8))
|
|
106 {
|
|
107 /* WAVE */
|
|
108 *speed = WORD_LE (format, 24);
|
|
109 *tracks = format[22];
|
|
110 return format[32] / format[22] == 1
|
|
111 ? SND_PCM_FORMAT_U8
|
|
112 : SND_PCM_FORMAT_S16_LE;
|
|
113 }
|
|
114 else if (!memcmp (format, ".snd", 4))
|
|
115 {
|
|
116 /* Sun/NeXT Audio (big endian) */
|
|
117 if (WORD_BE (format, 4) < 24)
|
|
118 {
|
|
119 *speed = 8000;
|
|
120 *tracks = 1;
|
|
121 return SND_PCM_FORMAT_MU_LAW;
|
|
122 }
|
|
123 *speed = WORD_BE (format, 16);
|
|
124 *tracks = format[23];
|
|
125 if (!memcmp (format + 12, "\000\000\000", 3))
|
|
126 {
|
|
127 switch (format[15])
|
|
128 {
|
|
129 case 1:
|
|
130 case 17:
|
|
131 case 29:
|
|
132 return SND_PCM_FORMAT_MU_LAW;
|
|
133 case 2:
|
|
134 return SND_PCM_FORMAT_S8;
|
|
135 case 3:
|
|
136 return SND_PCM_FORMAT_S16_BE;
|
|
137 case 4:
|
|
138 return SND_PCM_FORMAT_S24_BE;
|
|
139 case 5:
|
|
140 return SND_PCM_FORMAT_S32_BE;
|
|
141 case 23:
|
|
142 case 24:
|
|
143 case 25:
|
|
144 case 26:
|
|
145 return SND_PCM_FORMAT_IMA_ADPCM;
|
|
146 case 27:
|
|
147 return SND_PCM_FORMAT_A_LAW;
|
|
148 default:
|
|
149 break;
|
|
150 }
|
|
151 }
|
|
152 return SND_PCM_FORMAT_UNKNOWN;
|
|
153 }
|
|
154 else if (!memcmp (format, ".sd", 4))
|
|
155 {
|
|
156 /* DEC Audio (little endian) */
|
|
157 if (WORD_LE (format, 4) < 24)
|
|
158 {
|
|
159 *speed = 8000;
|
|
160 *tracks = 1;
|
|
161 return SND_PCM_FORMAT_MU_LAW;
|
|
162 }
|
|
163 *speed = WORD_LE (format, 16);
|
|
164 *tracks = format[20];
|
|
165 if (!memcmp (format + 13, "\000\000\000", 3))
|
|
166 {
|
|
167 switch (format[12])
|
|
168 {
|
|
169 case 1:
|
|
170 case 17:
|
|
171 case 29:
|
|
172 return SND_PCM_FORMAT_MU_LAW;
|
|
173 case 2:
|
|
174 return SND_PCM_FORMAT_S8;
|
|
175 case 3:
|
|
176 return SND_PCM_FORMAT_S16_LE;
|
|
177 case 4:
|
|
178 return SND_PCM_FORMAT_S24_LE;
|
|
179 case 5:
|
|
180 return SND_PCM_FORMAT_S32_LE;
|
|
181 case 23:
|
|
182 case 24:
|
|
183 case 25:
|
|
184 case 26:
|
|
185 return SND_PCM_FORMAT_IMA_ADPCM;
|
|
186 case 27:
|
|
187 return SND_PCM_FORMAT_A_LAW;
|
|
188 default:
|
|
189 break;
|
|
190 }
|
|
191 }
|
|
192 return SND_PCM_FORMAT_UNKNOWN;
|
|
193 }
|
|
194 else
|
|
195 {
|
|
196 /* We don't know what it is. Guess that it is mono audio in unsigned
|
|
197 * byte format. Maybe we should error if we reach this point.
|
|
198 */
|
|
199 *speed = 8000;
|
|
200 *tracks = 1;
|
|
201 return SND_PCM_FORMAT_U8;
|
|
202 }
|
|
203 }
|
|
204
|
|
205 /* Set the volume: if any errors occur, we accept the existing volume */
|
|
206 static void
|
|
207 set_volume (struct mixer_state *mix, int volume)
|
|
208 {
|
|
209 snd_mixer_selem_id_t *volume_id;
|
|
210 long min_vol, max_vol, dev_vol;
|
|
211
|
|
212 if (snd_mixer_open (&mix->mixer, 0) < 0)
|
|
213 return;
|
|
214
|
|
215 if (snd_mixer_attach (mix->mixer, "default") < 0)
|
|
216 return;
|
|
217
|
|
218 if (snd_mixer_selem_register (mix->mixer, NULL, NULL) < 0)
|
|
219 return;
|
|
220
|
|
221 if (snd_mixer_load (mix->mixer) < 0)
|
|
222 return;
|
|
223
|
|
224 snd_mixer_selem_id_alloca (&volume_id);
|
|
225 snd_mixer_selem_id_set_name (volume_id, "PCM");
|
|
226
|
|
227 if ((mix->vol_ctl = snd_mixer_find_selem (mix->mixer, volume_id)) == NULL)
|
|
228 {
|
|
229 snd_mixer_selem_id_set_name (volume_id, "Master");
|
|
230 if ((mix->vol_ctl = snd_mixer_find_selem (mix->mixer, volume_id))
|
|
231 == NULL)
|
|
232 return;
|
|
233 }
|
|
234
|
|
235 /* Translate the Lisp volume range to the device volume range */
|
3335
|
236 #if SND_LIB_VERSION < ALSA_VERSION (1, 0, 10)
|
|
237 snd_mixer_selem_get_playback_volume_range (mix->vol_ctl, &min_vol, &max_vol);
|
|
238 #else
|
3308
|
239 if (snd_mixer_selem_get_playback_volume_range (mix->vol_ctl, &min_vol,
|
|
240 &max_vol) < 0)
|
|
241 return;
|
3335
|
242 #endif
|
3308
|
243
|
|
244 dev_vol = volume * (max_vol - min_vol) / 100 + min_vol;
|
|
245
|
|
246 /* Record the old volumes */
|
|
247 if (snd_mixer_selem_get_playback_volume
|
|
248 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_LEFT, &mix->front_left_vol) >= 0)
|
|
249 mix->reset_front_left = 1;
|
|
250
|
|
251 if (snd_mixer_selem_get_playback_volume
|
|
252 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_CENTER, &mix->front_center_vol) >= 0)
|
|
253 mix->reset_front_center = 1;
|
|
254
|
|
255 if (snd_mixer_selem_get_playback_volume
|
|
256 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_RIGHT, &mix->front_right_vol) >= 0)
|
|
257 mix->reset_front_right = 1;
|
|
258
|
|
259 if (snd_mixer_selem_get_playback_volume
|
|
260 (mix->vol_ctl, SND_MIXER_SCHN_REAR_LEFT, &mix->rear_left_vol) >= 0)
|
|
261 mix->reset_rear_left = 1;
|
|
262
|
|
263 if (snd_mixer_selem_get_playback_volume
|
3335
|
264 (mix->vol_ctl, SND_MIXER_SCHN_REAR_RIGHT, &mix->rear_right_vol) >= 0)
|
|
265 mix->reset_rear_right = 1;
|
|
266
|
|
267 if (snd_mixer_selem_get_playback_volume
|
|
268 (mix->vol_ctl, SND_MIXER_SCHN_WOOFER, &mix->woofer_vol) >= 0)
|
|
269 mix->reset_woofer = 1;
|
|
270
|
|
271 #if SND_LIB_VERSION >= ALSA_VERSION (1, 0, 10)
|
|
272 if (snd_mixer_selem_get_playback_volume
|
3308
|
273 (mix->vol_ctl, SND_MIXER_SCHN_REAR_CENTER, &mix->rear_center_vol) >= 0)
|
|
274 mix->reset_rear_center = 1;
|
|
275
|
|
276 if (snd_mixer_selem_get_playback_volume
|
|
277 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_LEFT, &mix->side_left_vol) >= 0)
|
|
278 mix->reset_side_left = 1;
|
|
279
|
|
280 if (snd_mixer_selem_get_playback_volume
|
|
281 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_RIGHT, &mix->side_right_vol) >= 0)
|
|
282 mix->reset_side_right = 1;
|
3335
|
283 #endif
|
3308
|
284
|
|
285 /* Set the volume */
|
|
286 snd_mixer_selem_set_playback_volume_all (mix->vol_ctl, dev_vol);
|
|
287 }
|
|
288
|
|
289 static void
|
|
290 reset_volume (const struct mixer_state *mix)
|
|
291 {
|
|
292 if (mix->reset_front_left)
|
|
293 snd_mixer_selem_set_playback_volume
|
|
294 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_LEFT, mix->front_left_vol);
|
|
295
|
|
296 if (mix->reset_front_center)
|
|
297 snd_mixer_selem_set_playback_volume
|
|
298 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_CENTER, mix->front_center_vol);
|
|
299
|
|
300 if (mix->reset_front_right)
|
|
301 snd_mixer_selem_set_playback_volume
|
|
302 (mix->vol_ctl, SND_MIXER_SCHN_FRONT_RIGHT, mix->front_right_vol);
|
|
303
|
|
304 if (mix->reset_rear_left)
|
|
305 snd_mixer_selem_set_playback_volume
|
|
306 (mix->vol_ctl, SND_MIXER_SCHN_REAR_LEFT, mix->rear_left_vol);
|
|
307
|
3335
|
308 if (mix->reset_rear_right)
|
|
309 snd_mixer_selem_set_playback_volume
|
|
310 (mix->vol_ctl, SND_MIXER_SCHN_REAR_RIGHT, mix->rear_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 #if SND_LIB_VERSION >= ALSA_VERSION (1, 0, 10)
|
3308
|
317 if (mix->reset_rear_center)
|
|
318 snd_mixer_selem_set_playback_volume
|
|
319 (mix->vol_ctl, SND_MIXER_SCHN_REAR_CENTER, mix->rear_center_vol);
|
|
320
|
|
321 if (mix->reset_side_left)
|
|
322 snd_mixer_selem_set_playback_volume
|
|
323 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_LEFT, mix->side_left_vol);
|
|
324
|
|
325 if (mix->reset_side_right)
|
|
326 snd_mixer_selem_set_playback_volume
|
|
327 (mix->vol_ctl, SND_MIXER_SCHN_SIDE_RIGHT, mix->side_right_vol);
|
3335
|
328 #endif
|
3308
|
329
|
|
330 snd_mixer_close (mix->mixer);
|
|
331 }
|
|
332
|
|
333 int
|
|
334 alsa_play_sound_data (const Binbyte *data, int length, int volume)
|
|
335 {
|
|
336 snd_pcm_t *pcm_handle;
|
|
337 snd_pcm_hw_params_t *hwparams;
|
|
338 snd_pcm_format_t format;
|
|
339 struct mixer_state mix;
|
|
340 int speed, tracks, err;
|
|
341
|
|
342 /* Set the PCM parameters */
|
|
343 if ((err = snd_pcm_open (&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK,
|
|
344 0)) < 0)
|
|
345 goto error_pcm_open;
|
|
346
|
|
347 snd_pcm_hw_params_alloca (&hwparams);
|
|
348
|
|
349 if ((err = snd_pcm_hw_params_any (pcm_handle, hwparams)) < 0)
|
|
350 goto error_pcm;
|
|
351
|
|
352 format = analyze_format (data, &speed, &tracks);
|
|
353
|
|
354 if ((err = snd_pcm_hw_params_set_access (pcm_handle, hwparams,
|
|
355 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
356 goto error_pcm;
|
|
357
|
|
358 if ((err = snd_pcm_hw_params_set_format (pcm_handle, hwparams, format)) < 0)
|
|
359 goto error_pcm;
|
|
360
|
|
361 if ((err = snd_pcm_hw_params_set_rate (pcm_handle, hwparams, speed, 0)) < 0)
|
|
362 goto error_pcm;
|
|
363
|
|
364 if ((err = snd_pcm_hw_params_set_channels (pcm_handle, hwparams, tracks))
|
|
365 < 0)
|
|
366 goto error_pcm;
|
|
367
|
|
368 if ((err = snd_pcm_hw_params (pcm_handle, hwparams)) < 0)
|
|
369 goto error_pcm;
|
|
370
|
|
371 /* Set the volume */
|
|
372 memset (&mix, 0, sizeof (mix));
|
|
373 set_volume (&mix, volume);
|
|
374
|
|
375 /* Play the sound */
|
|
376 if ((err = snd_pcm_writei (pcm_handle, data, length)) < 0)
|
|
377 goto error_mixer;
|
|
378
|
|
379 /* Put the volume back the way it used to be */
|
|
380 reset_volume (&mix);
|
|
381
|
|
382 /* Release resources */
|
|
383 snd_pcm_close (pcm_handle);
|
|
384 return 1;
|
|
385
|
|
386 error_mixer:
|
|
387 reset_volume (&mix);
|
|
388 error_pcm:
|
|
389 snd_pcm_close (pcm_handle);
|
|
390 error_pcm_open:
|
|
391 sound_perror (snd_strerror (err));
|
|
392 return 0;
|
|
393 }
|
|
394
|
|
395 /* Read the sound file into an internal buffer, then call
|
|
396 * alsa_play_sound_data.
|
|
397 */
|
|
398 int
|
|
399 alsa_play_sound_file (const Extbyte *sound_file, int volume)
|
|
400 {
|
|
401 Binbyte *data;
|
|
402 int fd, retval;
|
|
403 struct stat st;
|
|
404
|
|
405 fd = retry_open (sound_file, O_RDONLY, 0);
|
|
406 if (fd < 0) {
|
|
407 sound_perror (sound_file);
|
|
408 return 0;
|
|
409 }
|
|
410
|
|
411 qxe_fstat (fd, &st);
|
|
412 data = xnew_array (Binbyte, st.st_size);
|
|
413 retry_read (fd, data, st.st_size);
|
|
414 retry_close (fd);
|
|
415 retval = alsa_play_sound_data (data, st.st_size, volume);
|
|
416 xfree (data, Binbyte);
|
|
417 return retval;
|
|
418 }
|