[gaim-migrate @ 5891]
[pidgin-git.git] / src / sound.c
blob7045cc198abe48c18d2a3d56d2f913e74f2a069b
1 /*
2 * gaim
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #ifndef _WIN32
31 #include <unistd.h>
32 #else
33 #include <windows.h>
34 #include <mmsystem.h>
35 #endif
37 #ifdef HAVE_ENDIAN_H
38 #include <endian.h>
39 #endif
41 #ifdef USE_AO
42 #include <ao/ao.h>
43 #include <audiofile.h>
44 #endif /* USE_AO */
45 #ifdef USE_NAS_AUDIO
46 #include <audio/audiolib.h>
47 #include <audio/soundlib.h>
48 #endif /* USE_NAS_AUDIO */
50 #include "gaim.h"
51 #include "sound.h"
52 #include "notify.h"
54 #ifdef _WIN32
55 #include "win32dep.h"
56 #endif
58 struct gaim_sound_event {
59 char *label;
60 guint opt;
61 char *def;
64 #ifdef USE_AO
65 static gboolean ao_initialized=FALSE;
66 static int ao_driver = -1;
67 #endif /* USE_AO */
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. *
77 * -Robot101 */
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];
96 #ifdef USE_AO
97 static void check_ao_init()
99 if(!ao_initialized) {
100 gaim_debug(GAIM_DEBUG_INFO, "sound",
101 "Initializing sound output drivers.\n");
102 ao_initialize();
103 ao_initialized = TRUE;
106 #endif /* USE_AO */
108 void gaim_sound_change_output_method() {
109 #ifdef USE_AO
110 ao_driver = -1;
112 if ((sound_options & OPT_SOUND_ESD) || (sound_options & OPT_SOUND_ARTS) ||
113 (sound_options & OPT_SOUND_NORMAL)) {
114 check_ao_init();
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);
130 #endif /* USE_AO */
131 #ifdef USE_NAS
132 if((sound_options & OPT_SOUND_NAS))
133 gaim_debug(GAIM_DEBUG_INFO, "sound",
134 "Sound output driver loaded: NAS output\n");
135 #endif /* USE_NAS */
138 void gaim_sound_quit()
140 #ifdef USE_AO
141 if(ao_initialized)
142 ao_shutdown();
143 #endif
147 #ifdef USE_NAS_AUDIO
148 static gboolean play_file_nas(const char *filename)
150 AuServer *nas_serv;
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);
158 return ret;
161 #endif /* USE_NAS_AUDIO */
163 void gaim_sound_play_file(char *filename)
165 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
166 pid_t pid;
167 #ifdef USE_AO
168 AFfilehandle file;
169 #endif
170 #endif
172 if (mute_sounds)
173 return;
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) {
179 gdk_beep();
180 return;
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);
186 g_free(tmp);
187 return;
190 #ifndef _WIN32
191 if ((sound_options & OPT_SOUND_CMD)) {
192 char *command;
193 GError *error = NULL;
195 if(!sound_cmd) {
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);
200 return;
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);
208 g_free(tmp);
209 g_error_free(error);
212 g_free(command);
213 return;
215 #if defined(USE_NAS_AUDIO) || defined(USE_AO)
216 pid = fork();
217 if (pid < 0)
218 return;
219 else if (pid == 0) {
220 #ifdef USE_NAS_AUDIO
221 if ((sound_options & OPT_SOUND_NAS)) {
222 if (play_file_nas(filename))
223 _exit(0);
225 #endif /* USE_NAS_AUDIO */
227 #ifdef USE_AO
228 file = afOpenFile(filename, "rb", NULL);
229 if(file) {
230 ao_device *device;
231 ao_sample_format format;
232 int in_fmt;
233 int bytes_per_frame;
235 format.rate = afGetRate(file, AF_DEFAULT_TRACK);
236 format.channels = afGetChannels(file, AF_DEFAULT_TRACK);
237 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt,
238 &format.bits);
240 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll
241 * let libaudiofile make them a bit better for us */
242 if(format.bits == 8)
243 format.bits = 16;
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);
256 #endif
258 bytes_per_frame = format.bits * format.channels / 8;
260 device = ao_open_live(ao_driver, &format, NULL);
262 if(device) {
263 int frames_read;
264 char buf[4096];
265 int buf_frames = sizeof(buf) / bytes_per_frame;
267 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK,
268 buf, buf_frames))) {
269 if(!ao_play(device, buf, frames_read * bytes_per_frame))
270 break;
272 ao_close(device);
274 afCloseFile(file);
276 ao_shutdown();
277 #endif /* USE_AO */
278 _exit(0);
280 #else /* USE_NAS_AUDIO || USE_AO */
281 gdk_beep();
282 return;
283 #endif /* USE_NAS_AUDIO || USE_AO */
284 #else /* _WIN32 */
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");
289 #endif /* _WIN32 */
292 void gaim_sound_play_event(GaimSoundEventID event)
294 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
295 return;
297 if (event >= GAIM_NUM_SOUNDS) {
298 gaim_debug(GAIM_DEBUG_MISC, "sound",
299 "got request for unknown sound: %d\n", event);
300 return;
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]);
307 } else {
308 gchar *filename = NULL;
310 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL);
311 gaim_sound_play_file(filename);
312 g_free(filename);
317 void gaim_sound_set_mute(gboolean mute)
319 mute_sounds = mute;
322 gboolean gaim_sound_get_mute()
324 return mute_sounds;
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)
335 return;
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)
347 return NULL;
349 return sound_file[event];
352 guint gaim_sound_get_event_option(GaimSoundEventID event)
354 if(event >= GAIM_NUM_SOUNDS)
355 return 0;
357 return sounds[event].opt;
360 char *gaim_sound_get_event_label(GaimSoundEventID event)
362 if(event >= GAIM_NUM_SOUNDS)
363 return NULL;
365 return sounds[event].label;
369 void gaim_sound_set_command(const char *cmd)
371 if(sound_cmd)
372 g_free(sound_cmd);
373 if(strlen(cmd) > 0)
374 sound_cmd = g_strdup(cmd);
375 else
376 sound_cmd = NULL;
379 char *gaim_sound_get_command()
381 return sound_cmd;