4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
45 #include <audio/audiolib.h>
49 #include "sounds/BuddyArrive.h"
50 #include "sounds/BuddyLeave.h"
51 #include "sounds/Send.h"
52 #include "sounds/Receive.h"
53 #include "sounds/RedAlert.h"
55 /* label and opt are null for the buddy pounce because it's configured *
56 * per pounce. NULL option means it doesn't get displayed in the sound *
58 struct sound_struct sounds
[NUM_SOUNDS
] = {
59 N_("Buddy logs in"), OPT_SOUND_LOGIN
, BuddyArrive
, sizeof(BuddyArrive
),
60 N_("Buddy logs out"), OPT_SOUND_LOGOUT
, BuddyLeave
, sizeof(BuddyLeave
),
61 N_("Message received"), OPT_SOUND_RECV
, Receive
, sizeof(Receive
),
62 N_("Message received begins conversation"), OPT_SOUND_FIRST_RCV
, Receive
, sizeof(Receive
),
63 N_("Message sent"), OPT_SOUND_SEND
, Send
, sizeof(Send
),
64 N_("Person enters chat"), OPT_SOUND_CHAT_JOIN
, BuddyArrive
, sizeof(BuddyArrive
),
65 N_("Person leaves chat"), OPT_SOUND_CHAT_PART
, BuddyLeave
, sizeof(BuddyLeave
),
66 N_("You talk in chat"), OPT_SOUND_CHAT_YOU_SAY
, Send
, sizeof(Send
),
67 N_("Others talk in chat"), OPT_SOUND_CHAT_SAY
, Receive
, sizeof(Receive
),
68 NULL
, 0, RedAlert
, sizeof(RedAlert
)
71 static int check_dev(char *dev
)
74 uid_t user
= getuid();
75 gid_t group
= getgid(), other_groups
[32];
78 if ((numgroups
= getgroups(32, other_groups
)) == -1)
80 if (stat(dev
, &stat_buf
))
82 if (user
== stat_buf
.st_uid
&& stat_buf
.st_mode
& S_IWUSR
)
84 if (stat_buf
.st_mode
& S_IWGRP
) {
85 if (group
== stat_buf
.st_gid
)
87 for (i
= 0; i
< numgroups
; i
++)
88 if (other_groups
[i
] == stat_buf
.st_gid
)
91 if (stat_buf
.st_mode
& S_IWOTH
)
97 static void play_audio(unsigned char *data
, int size
)
101 fd
= open("/dev/audio", O_WRONLY
| O_EXCL
| O_NDELAY
);
104 write(fd
, data
, size
);
108 static void play_audio_file(char *file
)
110 /* here we can assume that we can write to /dev/audio */
113 int fd
= open(file
, O_RDONLY
);
118 if (info
.st_size
< 24)
120 buf
= malloc(info
.st_size
+ 1);
122 read(fd
, buf
, info
.st_size
- 24);
125 fd
= open("/dev/audio", O_WRONLY
| O_EXCL
| O_NDELAY
);
130 write(fd
, buf
, info
.st_size
- 24);
135 static int can_play_audio()
137 return check_dev("/dev/audio");
141 #if defined(ESD_SOUND) || defined(ARTSC_SOUND)
144 ** This routine converts from ulaw to 16 bit linear.
146 ** Craig Reese: IDA/Supercomputing Research Center
150 ** 1) CCITT Recommendation G.711 (very difficult to follow)
151 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
152 ** for Analog-to_Digital Conversion Techniques,"
155 ** Input: 8 bit ulaw sample
156 ** Output: signed 16 bit linear sample
157 ** Z-note -- this is from libaudiofile. Thanks guys!
160 static int _af_ulaw2linear(unsigned char ulawbyte
)
162 static int exp_lut
[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
163 int sign
, exponent
, mantissa
, sample
;
165 ulawbyte
= ~ulawbyte
;
166 sign
= (ulawbyte
& 0x80);
167 exponent
= (ulawbyte
>> 4) & 0x07;
168 mantissa
= ulawbyte
& 0x0F;
169 sample
= exp_lut
[exponent
] + (mantissa
<< (exponent
+ 3));
182 static int play_esd(unsigned char *data
, int size
)
187 lineardata
= g_malloc(size
* 2);
189 for (i
= 0; i
< size
; i
++)
190 lineardata
[i
] = _af_ulaw2linear(data
[i
]);
192 write(esd_fd
, lineardata
, size
* 2);
201 static int can_play_esd()
203 esd_format_t format
= ESD_BITS16
| ESD_STREAM
| ESD_PLAY
| ESD_MONO
;
205 esd_fd
= esd_play_stream(format
, 8012, NULL
, "gaim");
218 static int play_artsc(unsigned char *data
, int size
)
220 arts_stream_t stream
;
226 lineardata
= g_malloc(size
* 2);
228 for (i
= 0; i
< size
; i
++) {
229 lineardata
[i
] = _af_ulaw2linear(data
[i
]);
232 stream
= arts_play_stream(8012, 16, 1, "gaim");
234 error
= arts_write(stream
, lineardata
, size
);
239 arts_close_stream(stream
);
248 static int can_play_artsc()
259 static int artsc_play_file(char *file
)
261 struct stat stat_buf
;
262 unsigned char *buf
= NULL
;
266 if (!can_play_artsc())
269 fd
= open(file
, O_RDONLY
);
273 if (fstat(fd
, &stat_buf
)) {
278 if (!stat_buf
.st_size
) {
283 buf
= g_malloc(stat_buf
.st_size
);
289 if (read(fd
, buf
, stat_buf
.st_size
) < 0) {
295 result
= play_artsc(buf
, stat_buf
.st_size
);
302 #endif /* ARTSC_SOUND */
306 char nas_server
[] = "localhost";
307 AuServer
*nas_serv
= NULL
;
309 static AuBool
NasEventHandler(AuServer
* aud
, AuEvent
* ev
, AuEventHandlerRec
* handler
)
311 AuElementNotifyEvent
*event
= (AuElementNotifyEvent
*) ev
;
313 if (ev
->type
== AuEventTypeElementNotify
) {
314 switch (event
->kind
) {
315 case AuElementNotifyKindState
:
316 switch (event
->cur_state
) {
327 static int play_nas(unsigned char *data
, int size
)
329 AuDeviceID device
= AuNone
;
331 AuElement elements
[3];
334 /* look for an output device */
335 for (i
= 0; i
< AuServerNumDevices(nas_serv
); i
++) {
336 if ((AuDeviceKind(AuServerDevice(nas_serv
, i
)) ==
337 AuComponentKindPhysicalOutput
) &&
338 AuDeviceNumTracks(AuServerDevice(nas_serv
, i
)) == 1) {
339 device
= AuDeviceIdentifier(AuServerDevice(nas_serv
, i
));
344 if (device
== AuNone
)
347 if (!(flow
= AuCreateFlow(nas_serv
, NULL
)))
351 AuMakeElementImportClient(&elements
[0], 8012, AuFormatULAW8
, 1, AuTrue
, size
, size
/ 2, 0, NULL
);
352 AuMakeElementExportDevice(&elements
[1], 0, device
, 8012, AuUnlimitedSamples
, 0, NULL
);
353 AuSetElements(nas_serv
, flow
, AuTrue
, 2, elements
, NULL
);
355 AuStartFlow(nas_serv
, flow
, NULL
);
357 AuWriteElement(nas_serv
, flow
, 0, size
, data
, AuTrue
, NULL
);
359 AuRegisterEventHandler(nas_serv
, AuEventHandlerIDMask
, 0, flow
, NasEventHandler
, NULL
);
362 AuHandleEvents(nas_serv
);
368 static int can_play_nas()
370 if ((nas_serv
= AuOpenServer(NULL
, 0, NULL
, 0, NULL
, NULL
)))
375 static int play_nas_file(char *file
)
377 struct stat stat_buf
;
380 int fd
= open(file
, O_RDONLY
);
387 if (stat(file
, &stat_buf
))
390 if (!stat_buf
.st_size
)
393 buf
= malloc(stat_buf
.st_size
);
394 read(fd
, buf
, stat_buf
.st_size
);
395 ret
= play_nas(buf
, stat_buf
.st_size
);
402 void play_file(char *filename
)
410 if (sound_options
& OPT_SOUND_BEEP
) {
415 else if (sound_options
& OPT_SOUND_NORMAL
) {
416 debug_printf("attempting to play audio file with internal method -- this is unlikely to work\n");
426 if ((sound_options
& OPT_SOUND_CMD
) && sound_cmd
[0]) {
430 g_snprintf(command
, sizeof(command
), sound_cmd
, filename
);
436 execvp(args
[0], args
);
440 else if (sound_options
& OPT_SOUND_ESD
) {
441 if (esd_play_file(NULL
, filename
, 1))
447 else if (sound_options
& OPT_SOUND_ARTSC
) {
448 if (artsc_play_file(filename
))
454 else if (sound_options
& OPT_SOUND_NAS
) {
455 if (play_nas_file(filename
))
459 else if ((sound_options
& OPT_SOUND_NORMAL
) &&
461 play_audio_file(filename
);
469 void play(unsigned char *data
, int size
)
477 if (sound_options
& OPT_SOUND_BEEP
) {
482 else if ((sound_options
& OPT_SOUND_CMD
) && sound_cmd
[0]) {
483 debug_printf("can't play internal sound with external command -- skipping\n");
495 /* ESD is our player of choice. Are we OK to
497 if (sound_options
& OPT_SOUND_ESD
) {
498 if (can_play_esd()) {
499 if (play_esd(data
, size
))
506 /* ArtsC is the new second choice. */
507 if (sound_options
& OPT_SOUND_ARTSC
) {
508 if (can_play_artsc()) {
509 if (play_artsc(data
, size
))
516 /* NAS is our second choice setup. */
517 if (sound_options
& OPT_SOUND_NAS
) {
518 if (can_play_nas()) {
519 if (play_nas(data
, size
))
525 /* Lastly, we can try just plain old /dev/audio */
526 if (sound_options
& OPT_SOUND_NORMAL
) {
527 if (can_play_audio()) {
528 play_audio(data
, size
);
537 extern int logins_not_muted
;
539 void play_sound(int sound
)
541 if (awaymessage
&& !(sound_options
& OPT_SOUND_WHEN_AWAY
))
544 if ((sound
== SND_BUDDY_ARRIVE
) && !logins_not_muted
)
547 if (sound
>= NUM_SOUNDS
) {
548 debug_printf("sorry old fruit... can't say I know that sound: ", sound
, "\n");
552 /* check NULL for sounds that don't have an option, ie buddy pounce */
553 if ((sound_options
& sounds
[sound
].opt
) || (sounds
[sound
].opt
== 0)) {
554 if (sound_file
[sound
]) {
555 play_file(sound_file
[sound
]);
557 play(sounds
[sound
].snd
, sounds
[sound
].snd_size
);