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