4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 #include <audiofile.h>
46 #include <audio/audiolib.h>
47 #include <audio/soundlib.h>
48 #endif /* USE_NAS_AUDIO */
58 struct gaim_sound_event
{
65 static gboolean ao_initialized
=FALSE
;
66 static int ao_driver
= -1;
70 static gboolean mute_login_sounds
= FALSE
;
71 static gboolean mute_sounds
= FALSE
;
72 static char *sound_cmd
= NULL
;
74 /* description, option bit, default sound file *
75 * set the option bit to 0 to have it not display in prefs *
76 * the order here has to match the defines in gaim.h. *
78 static struct gaim_sound_event sounds
[GAIM_NUM_SOUNDS
] = {
79 {N_("Buddy logs in"), OPT_SOUND_LOGIN
, "arrive.wav"},
80 {N_("Buddy logs out"), OPT_SOUND_LOGOUT
, "leave.wav"},
81 {N_("Message received"), OPT_SOUND_RECV
, "receive.wav"},
82 {N_("Message received begins conversation"), OPT_SOUND_FIRST_RCV
, "receive.wav"},
83 {N_("Message sent"), OPT_SOUND_SEND
, "send.wav"},
84 {N_("Person enters chat"), OPT_SOUND_CHAT_JOIN
, "arrive.wav"},
85 {N_("Person leaves chat"), OPT_SOUND_CHAT_PART
, "leave.wav"},
86 {N_("You talk in chat"), OPT_SOUND_CHAT_YOU_SAY
, "send.wav"},
87 {N_("Others talk in chat"), OPT_SOUND_CHAT_SAY
, "receive.wav"},
88 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */
89 {NULL
, 0, "redalert.wav"},
90 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK
, "redalert.wav"}
93 static char *sound_file
[GAIM_NUM_SOUNDS
];
97 static void check_ao_init()
100 gaim_debug(GAIM_DEBUG_INFO
, "sound",
101 "Initializing sound output drivers.\n");
103 ao_initialized
= TRUE
;
108 void gaim_sound_change_output_method() {
112 if ((sound_options
& OPT_SOUND_ESD
) || (sound_options
& OPT_SOUND_ARTS
) ||
113 (sound_options
& OPT_SOUND_NORMAL
)) {
115 if (ao_driver
== -1 && (sound_options
& OPT_SOUND_ESD
)) {
116 ao_driver
= ao_driver_id("esd");
118 if(ao_driver
== -1 && (sound_options
& OPT_SOUND_ARTS
)) {
119 ao_driver
= ao_driver_id("arts");
121 if (ao_driver
== -1) {
122 ao_driver
= ao_default_driver_id();
125 if(ao_driver
!= -1) {
126 ao_info
*info
= ao_driver_info(ao_driver
);
127 gaim_debug(GAIM_DEBUG_INFO
, "sound",
128 "Sound output driver loaded: %s\n", info
->name
);
132 if((sound_options
& OPT_SOUND_NAS
))
133 gaim_debug(GAIM_DEBUG_INFO
, "sound",
134 "Sound output driver loaded: NAS output\n");
138 void gaim_sound_quit()
148 static gboolean
play_file_nas(const char *filename
)
151 gboolean ret
= FALSE
;
153 if((nas_serv
= AuOpenServer(NULL
, 0, NULL
, 0, NULL
, NULL
))) {
154 ret
= AuSoundPlaySynchronousFromFile(nas_serv
, filename
, 100);
155 AuCloseServer(nas_serv
);
161 #endif /* USE_NAS_AUDIO */
163 void gaim_sound_play_file(char *filename
)
165 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
175 if (awaymessage
&& !(sound_options
& OPT_SOUND_WHEN_AWAY
))
176 return; /* check here in case a buddy pounce plays a file while away */
178 if (sound_options
& OPT_SOUND_BEEP
) {
183 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
184 char *tmp
= g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename
);
185 gaim_notify_error(NULL
, NULL
, tmp
, NULL
);
191 if ((sound_options
& OPT_SOUND_CMD
)) {
193 GError
*error
= NULL
;
196 gaim_notify_error(NULL
, NULL
,
197 _("Unable to play sound because the "
198 "'Command' sound method has been chosen, "
199 "but no command has been set."), NULL
);
203 command
= g_strdup_printf(sound_cmd
, filename
);
205 if(!g_spawn_command_line_async(command
, &error
)) {
206 char *tmp
= g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error
->message
);
207 gaim_notify_error(NULL
, NULL
, tmp
, NULL
);
215 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
221 if ((sound_options
& OPT_SOUND_NAS
)) {
222 if (play_file_nas(filename
))
225 #endif /* USE_NAS_AUDIO */
228 file
= afOpenFile(filename
, "rb", NULL
);
231 ao_sample_format format
;
235 format
.rate
= afGetRate(file
, AF_DEFAULT_TRACK
);
236 format
.channels
= afGetChannels(file
, AF_DEFAULT_TRACK
);
237 afGetSampleFormat(file
, AF_DEFAULT_TRACK
, &in_fmt
,
240 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll
241 * let libaudiofile make them a bit better for us */
245 afSetVirtualSampleFormat(file
, AF_DEFAULT_TRACK
,
246 AF_SAMPFMT_TWOSCOMP
, format
.bits
);
248 #if __BYTE_ORDER == __BIG_ENDIAN
249 format
.byte_format
= AO_FMT_BIG
;
250 afSetVirtualByteOrder(file
, AF_DEFAULT_TRACK
,
251 AF_BYTEORDER_BIGENDIAN
);
252 #elif __BYTE_ORDER == __LITTLE_ENDIAN
253 format
.byte_format
= AO_FMT_LITTLE
;
254 afSetVirtualByteOrder(file
, AF_DEFAULT_TRACK
,
255 AF_BYTEORDER_LITTLEENDIAN
);
258 bytes_per_frame
= format
.bits
* format
.channels
/ 8;
260 device
= ao_open_live(ao_driver
, &format
, NULL
);
265 int buf_frames
= sizeof(buf
) / bytes_per_frame
;
267 while((frames_read
= afReadFrames(file
, AF_DEFAULT_TRACK
,
269 if(!ao_play(device
, buf
, frames_read
* bytes_per_frame
))
280 #else /* USE_NAS_AUDIO || USE_AO */
283 #endif /* USE_NAS_AUDIO || USE_AO */
285 gaim_debug(GAIM_DEBUG_INFO
, "sound", "Playing %s\n", filename
);
287 if (!PlaySound(filename
, 0, SND_ASYNC
| SND_FILENAME
))
288 gaim_debug(GAIM_DEBUG_ERROR
, "sound", "Error playing sound.\n");
292 void gaim_sound_play_event(GaimSoundEventID event
)
294 if ((event
== GAIM_SOUND_BUDDY_ARRIVE
) && mute_login_sounds
)
297 if (event
>= GAIM_NUM_SOUNDS
) {
298 gaim_debug(GAIM_DEBUG_MISC
, "sound",
299 "got request for unknown sound: %d\n", event
);
303 /* check NULL for sounds that don't have an option, ie buddy pounce */
304 if ((sound_options
& sounds
[event
].opt
) || (sounds
[event
].opt
== 0)) {
305 if (sound_file
[event
]) {
306 gaim_sound_play_file(sound_file
[event
]);
308 gchar
*filename
= NULL
;
310 filename
= g_build_filename(DATADIR
, "sounds", "gaim", sounds
[event
].def
, NULL
);
311 gaim_sound_play_file(filename
);
317 void gaim_sound_set_mute(gboolean mute
)
322 gboolean
gaim_sound_get_mute()
327 void gaim_sound_set_login_mute(gboolean mute
)
329 mute_login_sounds
= mute
;
332 void gaim_sound_set_event_file(GaimSoundEventID event
, const char *filename
)
334 if(event
>= GAIM_NUM_SOUNDS
)
337 if(sound_file
[event
])
338 g_free(sound_file
[event
]);
340 sound_file
[event
] = g_strdup(filename
);
344 char *gaim_sound_get_event_file(GaimSoundEventID event
)
346 if(event
>= GAIM_NUM_SOUNDS
)
349 return sound_file
[event
];
352 guint
gaim_sound_get_event_option(GaimSoundEventID event
)
354 if(event
>= GAIM_NUM_SOUNDS
)
357 return sounds
[event
].opt
;
360 char *gaim_sound_get_event_label(GaimSoundEventID event
)
362 if(event
>= GAIM_NUM_SOUNDS
)
365 return sounds
[event
].label
;
369 void gaim_sound_set_command(const char *cmd
)
374 sound_cmd
= g_strdup(cmd
);
379 char *gaim_sound_get_command()