comparison src/sound.c @ 4429:bf770f11132b

[gaim-migrate @ 4704] (15:41:59) Robot101: the hunk that got left out in the cold. its a good thing when things compile committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Sun, 26 Jan 2003 20:41:49 +0000
parents 4b04ecb3eb97
children 801830dec409
comparison
equal deleted inserted replaced
4428:b5669740e34c 4429:bf770f11132b
38 38
39 #include <fcntl.h> 39 #include <fcntl.h>
40 #include <sys/types.h> 40 #include <sys/types.h>
41 #include <sys/stat.h> 41 #include <sys/stat.h>
42 42
43 #ifdef ESD_SOUND 43 #ifdef USE_AO
44 #include <esd.h> 44 #include <ao/ao.h>
45 #endif 45 #include <audiofile.h>
46 46 #endif /* USE_AO */
47 #ifdef ARTSC_SOUND
48 #include <artsc.h>
49 #endif
50
51 #ifdef NAS_SOUND
52 #include <audio/audiolib.h>
53 #endif
54 47
55 #include "gaim.h" 48 #include "gaim.h"
56 49
57 #ifdef _WIN32 50 #ifdef _WIN32
58 #include "win32dep.h" 51 #include "win32dep.h"
59 #endif 52 #endif
53
54 #ifdef USE_AO
55 static gboolean ao_initialized = FALSE;
56 #endif /* USE_AO */
60 57
61 gboolean mute_sounds = 0; 58 gboolean mute_sounds = 0;
62 59
63 /* description, option bit, default sound file * 60 /* description, option bit, default sound file *
64 * set the option bit to 0 to have it not display in prefs * 61 * set the option bit to 0 to have it not display in prefs *
77 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ 74 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
78 {NULL, 0, "redalert.wav"}, 75 {NULL, 0, "redalert.wav"},
79 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"} 76 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"}
80 }; 77 };
81 78
82 #ifndef _WIN32
83 static int check_dev(char *dev)
84 {
85 struct stat stat_buf;
86 uid_t user = getuid();
87 gid_t group = getgid(), other_groups[32];
88 int i, numgroups;
89
90 if ((numgroups = getgroups(32, other_groups)) == -1)
91 return 0;
92 if (stat(dev, &stat_buf))
93 return 0;
94 if (user == stat_buf.st_uid && stat_buf.st_mode & S_IWUSR)
95 return 1;
96 if (stat_buf.st_mode & S_IWGRP) {
97 if (group == stat_buf.st_gid)
98 return 1;
99 for (i = 0; i < numgroups; i++)
100 if (other_groups[i] == stat_buf.st_gid)
101 return 1;
102 }
103 if (stat_buf.st_mode & S_IWOTH)
104 return 1;
105 return 0;
106 }
107
108 static void play_audio_file(char *file)
109 {
110 /* here we can assume that we can write to /dev/audio */
111 char *buf;
112 struct stat info;
113 int fd = open(file, O_RDONLY);
114 if (fd <= 0) {
115 return;
116 }
117 fstat(fd, &info);
118 if (info.st_size < 24)
119 return;
120 buf = malloc(info.st_size + 1);
121 read(fd, buf, 24);
122 read(fd, buf, info.st_size - 24);
123 close(fd);
124
125 fd = open("/dev/audio", O_WRONLY | O_EXCL | O_NDELAY);
126 if (fd < 0) {
127 free(buf);
128 return;
129 }
130 write(fd, buf, info.st_size - 24);
131 free(buf);
132 close(fd);
133 }
134
135 static int can_play_audio()
136 {
137 return check_dev("/dev/audio");
138 }
139
140 #ifdef ESD_SOUND
141
142 int esd_fd;
143
144 static int can_play_esd()
145 {
146 esd_format_t format = ESD_BITS16 | ESD_STREAM | ESD_PLAY | ESD_MONO;
147
148 esd_fd = esd_play_stream(format, 8012, NULL, "gaim");
149
150 if (esd_fd < 0) {
151 return 0;
152 }
153
154 return 1;
155 }
156
157 #endif /* ESD_SOUND */
158
159 #ifdef ARTSC_SOUND
160
161 /*
162 ** This routine converts from ulaw to 16 bit linear.
163 **
164 ** Craig Reese: IDA/Supercomputing Research Center
165 ** 29 September 1989
166 **
167 ** References:
168 ** 1) CCITT Recommendation G.711 (very difficult to follow)
169 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
170 ** for Analog-to_Digital Conversion Techniques,"
171 ** 17 February 1987
172 **
173 ** Input: 8 bit ulaw sample
174 ** Output: signed 16 bit linear sample
175 ** Z-note -- this is from libaudiofile. Thanks guys!
176 */
177
178 static int _af_ulaw2linear(unsigned char ulawbyte)
179 {
180 static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
181 int sign, exponent, mantissa, sample;
182
183 ulawbyte = ~ulawbyte;
184 sign = (ulawbyte & 0x80);
185 exponent = (ulawbyte >> 4) & 0x07;
186 mantissa = ulawbyte & 0x0F;
187 sample = exp_lut[exponent] + (mantissa << (exponent + 3));
188 if (sign != 0)
189 sample = -sample;
190
191 return (sample);
192 }
193
194 static int play_artsc(unsigned char *data, int size)
195 {
196 arts_stream_t stream;
197 guint16 *lineardata;
198 int result = 1;
199 int error;
200 int i;
201
202 lineardata = g_malloc(size * 2);
203
204 for (i = 0; i < size; i++) {
205 lineardata[i] = _af_ulaw2linear(data[i]);
206 }
207
208 stream = arts_play_stream(8012, 16, 1, "gaim");
209
210 error = arts_write(stream, lineardata, size);
211 if (error < 0) {
212 result = 0;
213 }
214
215 arts_close_stream(stream);
216
217 g_free(lineardata);
218
219 arts_free();
220
221 return result;
222 }
223
224 static int can_play_artsc()
225 {
226 int error;
227
228 error = arts_init();
229 if (error < 0)
230 return 0;
231
232 return 1;
233 }
234
235 static int artsc_play_file(char *file)
236 {
237 struct stat stat_buf;
238 unsigned char *buf = NULL;
239 int result = 0;
240 int fd = -1;
241
242 if (!can_play_artsc())
243 return 0;
244
245 fd = open(file, O_RDONLY);
246 if (fd < 0)
247 return 0;
248
249 if (fstat(fd, &stat_buf)) {
250 close(fd);
251 return 0;
252 }
253
254 if (!stat_buf.st_size) {
255 close(fd);
256 return 0;
257 }
258
259 buf = g_malloc(stat_buf.st_size);
260 if (!buf) {
261 close(fd);
262 return 0;
263 }
264
265 if (read(fd, buf, stat_buf.st_size) < 0) {
266 g_free(buf);
267 close(fd);
268 return 0;
269 }
270
271 result = play_artsc(buf, stat_buf.st_size);
272
273 g_free(buf);
274 close(fd);
275 return result;
276 }
277
278 #endif /* ARTSC_SOUND */
279
280 #ifdef NAS_SOUND
281
282 char nas_server[] = "localhost";
283 AuServer *nas_serv = NULL;
284
285 static AuBool NasEventHandler(AuServer * aud, AuEvent * ev, AuEventHandlerRec * handler)
286 {
287 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
288
289 if (ev->type == AuEventTypeElementNotify) {
290 switch (event->kind) {
291 case AuElementNotifyKindState:
292 switch (event->cur_state) {
293 case AuStateStop:
294 _exit(0);
295 }
296 break;
297 }
298 }
299 return AuTrue;
300 }
301
302
303 static int play_nas(unsigned char *data, int size)
304 {
305 AuDeviceID device = AuNone;
306 AuFlowID flow;
307 AuElement elements[3];
308 int i, n, w;
309
310 /* look for an output device */
311 for (i = 0; i < AuServerNumDevices(nas_serv); i++) {
312 if ((AuDeviceKind(AuServerDevice(nas_serv, i)) ==
313 AuComponentKindPhysicalOutput) &&
314 AuDeviceNumTracks(AuServerDevice(nas_serv, i)) == 1) {
315 device = AuDeviceIdentifier(AuServerDevice(nas_serv, i));
316 break;
317 }
318 }
319
320 if (device == AuNone)
321 return 0;
322
323 if (!(flow = AuCreateFlow(nas_serv, NULL)))
324 return 0;
325
326
327 AuMakeElementImportClient(&elements[0], 8012, AuFormatULAW8, 1, AuTrue, size, size / 2, 0, NULL);
328 AuMakeElementExportDevice(&elements[1], 0, device, 8012, AuUnlimitedSamples, 0, NULL);
329 AuSetElements(nas_serv, flow, AuTrue, 2, elements, NULL);
330
331 AuStartFlow(nas_serv, flow, NULL);
332
333 AuWriteElement(nas_serv, flow, 0, size, data, AuTrue, NULL);
334
335 AuRegisterEventHandler(nas_serv, AuEventHandlerIDMask, 0, flow, NasEventHandler, NULL);
336
337 while (1) {
338 AuHandleEvents(nas_serv);
339 }
340
341 return 1;
342 }
343
344 static int can_play_nas()
345 {
346 if ((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
347 return 1;
348 return 0;
349 }
350
351 static int play_nas_file(char *file)
352 {
353 struct stat stat_buf;
354 char *buf;
355 int ret;
356 int fd = open(file, O_RDONLY);
357 if (fd <= 0)
358 return 0;
359
360 if (!can_play_nas())
361 return 0;
362
363 if (stat(file, &stat_buf))
364 return 0;
365
366 if (!stat_buf.st_size)
367 return 0;
368
369 buf = malloc(stat_buf.st_size);
370 read(fd, buf, stat_buf.st_size);
371 ret = play_nas(buf, stat_buf.st_size);
372 free(buf);
373 return ret;
374 }
375
376 #endif /* NAS_SOUND */
377
378 #endif /* !_WIN32 */
379
380 void play_file(char *filename) 79 void play_file(char *filename)
381 { 80 {
382 #ifndef _WIN32 81 #ifndef _WIN32
383 int pid; 82 #ifdef USE_AO
384 #endif 83 int ao_driver;
84 #endif /* USE_AO */
85 pid_t pid;
86 #endif
87
385 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY)) 88 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY))
386 return; /* check here in case a buddy pounce plays a file while away */ 89 return; /* check here in case a buddy pounce plays a file while away */
387 90
388 if (sound_options & OPT_SOUND_BEEP) { 91 if (sound_options & OPT_SOUND_BEEP) {
389 gdk_beep(); 92 gdk_beep();
390 return; 93 return;
391 } 94 }
392 95
393 else if (sound_options & OPT_SOUND_NORMAL) {
394 debug_printf("attempting to play audio file with internal method -- this is unlikely to work\n");
395 }
396 #ifndef _WIN32 96 #ifndef _WIN32
97 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
98 gchar *tmp = g_strdup_printf(_("Unable to play sound because the chosen filename (%s) does not exist."), filename);
99 do_error_dialog(tmp, NULL, GAIM_ERROR);
100 g_free(tmp);
101 return;
102 }
103
104 if (sound_options & OPT_SOUND_CMD) {
105 gchar *command = NULL;
106 GError *error = NULL;
107
108 if (!sound_cmd[0]) {
109 do_error_dialog(_("Unable to play sound because the 'Command' sound method has been chosen, but no command has been set."), NULL, GAIM_ERROR);
110 return;
111 }
112
113 command = g_strdup_printf(sound_cmd, filename);
114
115 if (g_spawn_command_line_async(command, &error) == FALSE) {
116 gchar *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message);
117 do_error_dialog(tmp, NULL, GAIM_ERROR);
118 g_free(tmp);
119 g_error_free(error);
120 }
121
122 g_free(command);
123 return;
124 }
125
126 #ifdef USE_AO
127 if (!ao_initialized) {
128 ao_initialize();
129 }
130
131 ao_driver = ao_default_driver_id();
132
133 if (ao_driver == -1) {
134 do_error_dialog(_("Unable to play sound because no suitable driver could be found."), NULL, GAIM_ERROR);
135 return;
136 }
137
397 pid = fork(); 138 pid = fork();
398 139
399 if (pid < 0) 140 if (pid < 0)
400 return; 141 return;
401 else if (pid == 0) { 142 else if (pid == 0) {
402 alarm(30); 143 AFfilehandle file = afOpenFile(filename, "rb", NULL);
403 144 if(file) {
404 if ((sound_options & OPT_SOUND_CMD) && sound_cmd[0]) { 145 ao_device *device;
405 char *args[4]; 146 ao_sample_format format;
406 char command[4096]; 147
407 148 int in_fmt;
408 g_snprintf(command, sizeof(command), sound_cmd, filename); 149 int bytes_per_frame;
409 150
410 args[0] = "sh"; 151 format.rate = afGetRate(file, AF_DEFAULT_TRACK);
411 args[1] = "-c"; 152 format.channels = afGetChannels(file, AF_DEFAULT_TRACK);
412 args[2] = command; 153 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt,
413 args[3] = NULL; 154 &format.bits);
414 execvp(args[0], args); 155
415 _exit(0); 156 bytes_per_frame = format.bits * format.channels / 8;
416 } 157
417 #ifdef ESD_SOUND 158 device = ao_open_live(ao_driver, &format, NULL);
418 else if (sound_options & OPT_SOUND_ESD) { 159
419 if (esd_play_file(NULL, filename, 1)) 160 if (device) {
420 _exit(0); 161 int frames_read;
421 } 162 char buf[4096];
422 #endif 163 int buf_frames = sizeof(buf) / bytes_per_frame;
423 164
424 #ifdef ARTSC_SOUND 165 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK,
425 else if (sound_options & OPT_SOUND_ARTSC) { 166 buf, buf_frames))) {
426 if (artsc_play_file(filename)) 167 if(!ao_play(device, buf, frames_read * bytes_per_frame))
427 _exit(0); 168 break;
428 } 169 }
429 #endif 170 ao_close(device);
430 171 }
431 #ifdef NAS_SOUND 172
432 else if (sound_options & OPT_SOUND_NAS) { 173 ao_shutdown();
433 if (play_nas_file(filename)) 174 afCloseFile(file);
434 _exit(0); 175 }
435 }
436 #endif
437
438 else if ((sound_options & OPT_SOUND_NORMAL) &&
439 can_play_audio()) {
440 play_audio_file(filename);
441 _exit(0);
442 }
443
444 _exit(0); 176 _exit(0);
445 } 177 }
178 #else /* USE_AO */
179 gdk_beep();
180 #endif /* USE_AO */
181
446 #else /* _WIN32 */ 182 #else /* _WIN32 */
447 debug_printf("Playing %s\n", filename); 183 debug_printf("Playing %s\n", filename);
448 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) 184 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME))
449 debug_printf("Error playing sound."); 185 debug_printf("Error playing sound.");
450 #endif 186 #endif
451 } 187 }
452 188
189 void sound_quit() {
190 #ifdef USE_AO
191 if (ao_initialized) {
192 ao_shutdown();
193 }
194 #endif
195 }
196
453 extern int logins_not_muted; 197 extern int logins_not_muted;
454 198
455 void play_sound(int sound) 199 void play_sound(int sound)
456 { 200 {
457 if (mute_sounds) 201 if (mute_sounds)
458 return; 202 return;
459 203
460 if ((sound == SND_BUDDY_ARRIVE) && !logins_not_muted) 204 if ((sound == SND_BUDDY_ARRIVE) && !logins_not_muted)
461 return; 205 return;
462 206
463 if (sound >= NUM_SOUNDS) { 207 if (sound >= NUM_SOUNDS) {
464 debug_printf("got request for unknown sound: %d\n", sound); 208 debug_printf("got request for unknown sound: %d\n", sound);