Mercurial > pidgin
annotate src/sound.c @ 5232:2d58a9a46292
[gaim-migrate @ 5602]
Fix a problem with people without perl. Er, FOR people without perl. We
can't do anything to fix them.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Sat, 26 Apr 2003 19:46:34 +0000 |
| parents | 0241d6b6702d |
| children | eae93f3e46bd |
| rev | line source |
|---|---|
| 1 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4562 | 4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
| 4561 | 5 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> |
| 1 | 6 * |
| 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. | |
| 11 * | |
| 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. | |
| 16 * | |
| 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 | |
| 20 * | |
| 21 */ | |
| 22 | |
|
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
|
2090
b66aca8e8dce
[gaim-migrate @ 2100]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2048
diff
changeset
|
24 #include <config.h> |
|
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
25 #endif |
| 1 | 26 #include <stdio.h> |
|
1057
d50d3abb9eb7
[gaim-migrate @ 1067]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1006
diff
changeset
|
27 #include <stdlib.h> |
| 1 | 28 #include <string.h> |
| 3630 | 29 |
| 30 #ifndef _WIN32 | |
| 63 | 31 #include <unistd.h> |
| 3630 | 32 #else |
| 33 #include <windows.h> | |
| 34 #include <mmsystem.h> | |
| 35 #endif | |
| 36 | |
| 4592 | 37 #ifdef HAVE_ENDIAN_H |
| 38 #include <endian.h> | |
| 39 #endif | |
| 40 | |
| 4561 | 41 #ifdef USE_AO |
| 42 #include <ao/ao.h> | |
| 43 #include <audiofile.h> | |
| 44 #endif /* USE_AO */ | |
| 4581 | 45 #ifdef USE_NAS_AUDIO |
| 46 #include <audio/audiolib.h> | |
| 47 #include <audio/soundlib.h> | |
| 48 #endif /* USE_NAS_AUDIO */ | |
| 4012 | 49 |
| 1 | 50 #include "gaim.h" |
| 4561 | 51 #include "sound.h" |
| 1 | 52 |
|
4019
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
53 #ifdef _WIN32 |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
54 #include "win32dep.h" |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
55 #endif |
|
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
56 |
| 4561 | 57 struct gaim_sound_event { |
| 58 char *label; | |
| 59 guint opt; | |
| 60 char *def; | |
| 61 }; | |
| 62 | |
| 63 #ifdef USE_AO | |
| 4581 | 64 static gboolean ao_initialized=FALSE; |
| 65 static int ao_driver = -1; | |
| 4561 | 66 #endif /* USE_AO */ |
| 67 | |
| 68 | |
| 69 static gboolean mute_login_sounds = FALSE; | |
| 70 static gboolean mute_sounds = FALSE; | |
| 71 static char *sound_cmd = NULL; | |
| 3319 | 72 |
| 4082 | 73 /* description, option bit, default sound file * |
| 74 * set the option bit to 0 to have it not display in prefs * | |
| 75 * the order here has to match the defines in gaim.h. * | |
| 76 * -Robot101 */ | |
| 4561 | 77 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { |
| 4013 | 78 {N_("Buddy logs in"), OPT_SOUND_LOGIN, "arrive.wav"}, |
| 79 {N_("Buddy logs out"), OPT_SOUND_LOGOUT, "leave.wav"}, | |
| 4012 | 80 {N_("Message received"), OPT_SOUND_RECV, "receive.wav"}, |
| 81 {N_("Message received begins conversation"), OPT_SOUND_FIRST_RCV, "receive.wav"}, | |
| 82 {N_("Message sent"), OPT_SOUND_SEND, "send.wav"}, | |
| 4013 | 83 {N_("Person enters chat"), OPT_SOUND_CHAT_JOIN, "arrive.wav"}, |
| 84 {N_("Person leaves chat"), OPT_SOUND_CHAT_PART, "leave.wav"}, | |
| 4012 | 85 {N_("You talk in chat"), OPT_SOUND_CHAT_YOU_SAY, "send.wav"}, |
| 86 {N_("Others talk in chat"), OPT_SOUND_CHAT_SAY, "receive.wav"}, | |
| 3326 | 87 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ |
| 4012 | 88 {NULL, 0, "redalert.wav"}, |
| 89 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"} | |
| 3060 | 90 }; |
| 91 | |
| 4561 | 92 static char *sound_file[GAIM_NUM_SOUNDS]; |
| 4430 | 93 |
| 4581 | 94 |
| 95 #ifdef USE_AO | |
| 96 static void check_ao_init() | |
| 4430 | 97 { |
| 4581 | 98 if(!ao_initialized) { |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
99 gaim_debug(GAIM_DEBUG_INFO, "sound", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
100 "Initializing sound output drivers.\n"); |
| 4581 | 101 ao_initialize(); |
| 102 ao_initialized = TRUE; | |
| 103 } | |
| 104 } | |
| 105 #endif /* USE_AO */ | |
| 4430 | 106 |
| 4581 | 107 void gaim_sound_change_output_method() { |
| 108 #ifdef USE_AO | |
| 109 ao_driver = -1; | |
| 4430 | 110 |
| 4581 | 111 if ((sound_options & OPT_SOUND_ESD) || (sound_options & OPT_SOUND_ARTS) || |
| 112 (sound_options & OPT_SOUND_NORMAL)) { | |
| 113 check_ao_init(); | |
| 114 if (ao_driver == -1 && (sound_options & OPT_SOUND_ESD)) { | |
| 115 ao_driver = ao_driver_id("esd"); | |
| 116 } | |
| 117 if(ao_driver == -1 && (sound_options & OPT_SOUND_ARTS)) { | |
| 118 ao_driver = ao_driver_id("arts"); | |
| 119 } | |
| 120 if (ao_driver == -1) { | |
| 121 ao_driver = ao_default_driver_id(); | |
| 122 } | |
| 123 } | |
| 124 if(ao_driver != -1) { | |
| 4561 | 125 ao_info *info = ao_driver_info(ao_driver); |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
126 gaim_debug(GAIM_DEBUG_INFO, "sound", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
127 "Sound output driver loaded: %s\n", info->name); |
| 4430 | 128 } |
| 4581 | 129 #endif /* USE_AO */ |
| 130 #ifdef USE_NAS | |
| 131 if((sound_options & OPT_SOUND_NAS)) | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
132 gaim_debug(GAIM_DEBUG_INFO, "sound", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
133 "Sound output driver loaded: NAS output\n"); |
| 4581 | 134 #endif /* USE_NAS */ |
| 4430 | 135 } |
| 136 | |
| 4561 | 137 void gaim_sound_quit() |
| 4430 | 138 { |
| 4561 | 139 #ifdef USE_AO |
| 4581 | 140 if(ao_initialized) |
| 141 ao_shutdown(); | |
| 4561 | 142 #endif |
| 4430 | 143 } |
| 144 | |
| 4581 | 145 |
| 146 #ifdef USE_NAS_AUDIO | |
| 147 static gboolean play_file_nas(const char *filename) | |
| 148 { | |
| 149 AuServer *nas_serv; | |
| 150 gboolean ret = FALSE; | |
| 151 | |
| 152 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) { | |
| 153 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100); | |
| 154 AuCloseServer(nas_serv); | |
| 155 } | |
| 156 | |
| 157 return ret; | |
| 158 } | |
| 159 | |
| 160 #endif /* USE_NAS_AUDIO */ | |
| 161 | |
| 4561 | 162 void gaim_sound_play_file(char *filename) |
| 4430 | 163 { |
| 4581 | 164 #if defined(USE_NAS_AUDIO) || defined(USE_AO) |
| 4561 | 165 pid_t pid; |
| 4581 | 166 #ifdef USE_AO |
| 167 AFfilehandle file; | |
| 168 #endif | |
| 169 #endif | |
| 4430 | 170 |
| 4561 | 171 if (mute_sounds) |
| 172 return; | |
| 4430 | 173 |
| 3901 | 174 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY)) |
| 175 return; /* check here in case a buddy pounce plays a file while away */ | |
| 4561 | 176 |
| 3004 | 177 if (sound_options & OPT_SOUND_BEEP) { |
| 178 gdk_beep(); | |
| 179 return; | |
| 180 } | |
| 181 | |
| 4561 | 182 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { |
| 183 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen filename (%s) does not exist."), filename); | |
| 184 do_error_dialog(tmp, NULL, GAIM_ERROR); | |
| 185 g_free(tmp); | |
| 186 return; | |
| 3060 | 187 } |
| 4561 | 188 |
| 4430 | 189 #ifndef _WIN32 |
| 4561 | 190 if ((sound_options & OPT_SOUND_CMD)) { |
| 191 char *command; | |
| 192 GError *error = NULL; | |
| 193 | |
| 194 if(!sound_cmd) { | |
| 195 do_error_dialog(_("Unable to play sound because the 'Command' sound method has been chosen, but no command has been set."), NULL, GAIM_ERROR); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 command = g_strdup_printf(sound_cmd, filename); | |
| 200 | |
| 201 if(!g_spawn_command_line_async(command, &error)) { | |
| 202 char *tmp = g_strdup_printf(_("Unable to play sound because the configured sound command could not be launched: %s"), error->message); | |
| 203 do_error_dialog(tmp, NULL, GAIM_ERROR); | |
| 204 g_free(tmp); | |
| 205 g_error_free(error); | |
| 206 } | |
| 207 | |
| 208 g_free(command); | |
| 209 return; | |
| 210 } | |
| 4581 | 211 #if defined(USE_NAS_AUDIO) || defined(USE_AO) |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
212 pid = fork(); |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
213 if (pid < 0) |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
214 return; |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
215 else if (pid == 0) { |
| 4581 | 216 #ifdef USE_NAS_AUDIO |
| 217 if ((sound_options & OPT_SOUND_NAS)) { | |
| 218 if (play_file_nas(filename)) | |
| 219 _exit(0); | |
| 220 } | |
| 221 #endif /* USE_NAS_AUDIO */ | |
| 222 | |
| 223 #ifdef USE_AO | |
| 224 file = afOpenFile(filename, "rb", NULL); | |
| 4561 | 225 if(file) { |
| 226 ao_device *device; | |
| 227 ao_sample_format format; | |
| 228 int in_fmt; | |
| 229 int bytes_per_frame; | |
| 4429 | 230 |
| 4561 | 231 format.rate = afGetRate(file, AF_DEFAULT_TRACK); |
| 232 format.channels = afGetChannels(file, AF_DEFAULT_TRACK); | |
| 233 afGetSampleFormat(file, AF_DEFAULT_TRACK, &in_fmt, | |
| 234 &format.bits); | |
| 235 | |
| 4878 | 236 /* XXX: libao doesn't seem to like 8-bit sounds, so we'll |
| 237 * let libaudiofile make them a bit better for us */ | |
| 238 if(format.bits == 8) | |
| 239 format.bits = 16; | |
| 240 | |
| 4561 | 241 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, |
| 242 AF_SAMPFMT_TWOSCOMP, format.bits); | |
| 4429 | 243 |
| 4592 | 244 #if __BYTE_ORDER == __BIG_ENDIAN |
| 245 format.byte_format = AO_FMT_BIG; | |
| 246 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
| 247 AF_BYTEORDER_BIGENDIAN); | |
| 248 #elif __BYTE_ORDER == __LITTLE_ENDIAN | |
| 249 format.byte_format = AO_FMT_LITTLE; | |
| 250 afSetVirtualByteOrder(file, AF_DEFAULT_TRACK, | |
| 251 AF_BYTEORDER_LITTLEENDIAN); | |
| 252 #endif | |
| 4561 | 253 |
| 254 bytes_per_frame = format.bits * format.channels / 8; | |
| 255 | |
| 256 device = ao_open_live(ao_driver, &format, NULL); | |
| 257 | |
| 258 if(device) { | |
| 259 int frames_read; | |
| 260 char buf[4096]; | |
| 261 int buf_frames = sizeof(buf) / bytes_per_frame; | |
| 262 | |
| 263 while((frames_read = afReadFrames(file, AF_DEFAULT_TRACK, | |
| 264 buf, buf_frames))) { | |
| 265 if(!ao_play(device, buf, frames_read * bytes_per_frame)) | |
| 266 break; | |
| 267 } | |
| 268 ao_close(device); | |
| 269 } | |
| 270 afCloseFile(file); | |
| 4430 | 271 } |
| 4581 | 272 ao_shutdown(); |
| 273 #endif /* USE_AO */ | |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
274 _exit(0); |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
275 } |
| 4581 | 276 #else /* USE_NAS_AUDIO || USE_AO */ |
| 4561 | 277 gdk_beep(); |
| 278 return; | |
| 4581 | 279 #endif /* USE_NAS_AUDIO || USE_AO */ |
| 3630 | 280 #else /* _WIN32 */ |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
281 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename); |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
282 |
| 3630 | 283 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
284 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); |
| 4561 | 285 #endif /* _WIN32 */ |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
286 } |
|
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
287 |
| 4561 | 288 void gaim_sound_play_event(GaimSoundEventID event) |
| 1 | 289 { |
| 4561 | 290 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) |
| 3060 | 291 return; |
| 292 | |
| 4561 | 293 if (event >= GAIM_NUM_SOUNDS) { |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
294 gaim_debug(GAIM_DEBUG_MISC, "sound", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
295 "got request for unknown sound: %d\n", event); |
| 3060 | 296 return; |
| 297 } | |
| 298 | |
| 299 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
| 4561 | 300 if ((sound_options & sounds[event].opt) || (sounds[event].opt == 0)) { |
| 301 if (sound_file[event]) { | |
| 302 gaim_sound_play_file(sound_file[event]); | |
| 3060 | 303 } else { |
| 4004 | 304 gchar *filename = NULL; |
| 305 | |
| 4561 | 306 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); |
| 307 gaim_sound_play_file(filename); | |
| 4004 | 308 g_free(filename); |
|
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
309 } |
|
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
310 } |
| 64 | 311 } |
| 4561 | 312 |
| 313 void gaim_sound_set_mute(gboolean mute) | |
| 314 { | |
| 315 mute_sounds = mute; | |
| 316 } | |
| 317 | |
| 318 gboolean gaim_sound_get_mute() | |
| 319 { | |
| 320 return mute_sounds; | |
| 321 } | |
| 322 | |
| 323 void gaim_sound_set_login_mute(gboolean mute) | |
| 324 { | |
| 325 mute_login_sounds = mute; | |
| 326 } | |
| 327 | |
| 328 void gaim_sound_set_event_file(GaimSoundEventID event, const char *filename) | |
| 329 { | |
| 330 if(event >= GAIM_NUM_SOUNDS) | |
| 331 return; | |
| 332 | |
| 333 if(sound_file[event]) | |
| 334 g_free(sound_file[event]); | |
| 335 | |
| 336 sound_file[event] = g_strdup(filename); | |
| 337 } | |
| 338 | |
| 339 | |
| 340 char *gaim_sound_get_event_file(GaimSoundEventID event) | |
| 341 { | |
| 342 if(event >= GAIM_NUM_SOUNDS) | |
| 343 return NULL; | |
| 344 | |
| 345 return sound_file[event]; | |
| 346 } | |
| 347 | |
| 348 guint gaim_sound_get_event_option(GaimSoundEventID event) | |
| 349 { | |
| 350 if(event >= GAIM_NUM_SOUNDS) | |
| 351 return 0; | |
| 352 | |
| 353 return sounds[event].opt; | |
| 354 } | |
| 355 | |
| 356 char *gaim_sound_get_event_label(GaimSoundEventID event) | |
| 357 { | |
| 358 if(event >= GAIM_NUM_SOUNDS) | |
| 359 return NULL; | |
| 360 | |
| 361 return sounds[event].label; | |
| 362 } | |
| 363 | |
| 364 | |
| 365 void gaim_sound_set_command(const char *cmd) | |
| 366 { | |
| 367 if(sound_cmd) | |
| 368 g_free(sound_cmd); | |
| 369 if(strlen(cmd) > 0) | |
| 370 sound_cmd = g_strdup(cmd); | |
| 371 else | |
| 372 sound_cmd = NULL; | |
| 373 } | |
| 374 | |
| 375 char *gaim_sound_get_command() | |
| 376 { | |
| 377 return sound_cmd; | |
| 378 } | |
| 379 |
