Mercurial > pidgin.yaz
annotate src/sound.c @ 5473:794d81c10d0a
[gaim-migrate @ 5865]
Zoltan Sutto updated the hungarian translation
committer: Tailor Script <tailor@pidgin.im>
author | Luke Schierer <lschiere@pidgin.im> |
---|---|
date | Wed, 21 May 2003 17:08:43 +0000 |
parents | ad445074d239 |
children | 7a64114641c3 |
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" |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
52 #include "notify.h" |
1 | 53 |
4019
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
54 #ifdef _WIN32 |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
55 #include "win32dep.h" |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
56 #endif |
e53d9f9969d0
[gaim-migrate @ 4219]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4014
diff
changeset
|
57 |
4561 | 58 struct gaim_sound_event { |
59 char *label; | |
60 guint opt; | |
61 char *def; | |
62 }; | |
63 | |
64 #ifdef USE_AO | |
4581 | 65 static gboolean ao_initialized=FALSE; |
66 static int ao_driver = -1; | |
4561 | 67 #endif /* USE_AO */ |
68 | |
69 | |
70 static gboolean mute_login_sounds = FALSE; | |
71 static gboolean mute_sounds = FALSE; | |
72 static char *sound_cmd = NULL; | |
3319 | 73 |
4082 | 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 */ | |
4561 | 78 static struct gaim_sound_event sounds[GAIM_NUM_SOUNDS] = { |
4013 | 79 {N_("Buddy logs in"), OPT_SOUND_LOGIN, "arrive.wav"}, |
80 {N_("Buddy logs out"), OPT_SOUND_LOGOUT, "leave.wav"}, | |
4012 | 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"}, | |
4013 | 84 {N_("Person enters chat"), OPT_SOUND_CHAT_JOIN, "arrive.wav"}, |
85 {N_("Person leaves chat"), OPT_SOUND_CHAT_PART, "leave.wav"}, | |
4012 | 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"}, | |
3326 | 88 /* this isn't a terminator, it's the buddy pounce default sound event ;-) */ |
4012 | 89 {NULL, 0, "redalert.wav"}, |
90 {N_("Someone says your name in chat"), OPT_SOUND_CHAT_NICK, "redalert.wav"} | |
3060 | 91 }; |
92 | |
4561 | 93 static char *sound_file[GAIM_NUM_SOUNDS]; |
4430 | 94 |
4581 | 95 |
96 #ifdef USE_AO | |
97 static void check_ao_init() | |
4430 | 98 { |
4581 | 99 if(!ao_initialized) { |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
100 gaim_debug(GAIM_DEBUG_INFO, "sound", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
101 "Initializing sound output drivers.\n"); |
4581 | 102 ao_initialize(); |
103 ao_initialized = TRUE; | |
104 } | |
105 } | |
106 #endif /* USE_AO */ | |
4430 | 107 |
4581 | 108 void gaim_sound_change_output_method() { |
109 #ifdef USE_AO | |
110 ao_driver = -1; | |
4430 | 111 |
4581 | 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"); | |
117 } | |
118 if(ao_driver == -1 && (sound_options & OPT_SOUND_ARTS)) { | |
119 ao_driver = ao_driver_id("arts"); | |
120 } | |
121 if (ao_driver == -1) { | |
122 ao_driver = ao_default_driver_id(); | |
123 } | |
124 } | |
125 if(ao_driver != -1) { | |
4561 | 126 ao_info *info = ao_driver_info(ao_driver); |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
127 gaim_debug(GAIM_DEBUG_INFO, "sound", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
128 "Sound output driver loaded: %s\n", info->name); |
4430 | 129 } |
4581 | 130 #endif /* USE_AO */ |
131 #ifdef USE_NAS | |
132 if((sound_options & OPT_SOUND_NAS)) | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
133 gaim_debug(GAIM_DEBUG_INFO, "sound", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
134 "Sound output driver loaded: NAS output\n"); |
4581 | 135 #endif /* USE_NAS */ |
4430 | 136 } |
137 | |
4561 | 138 void gaim_sound_quit() |
4430 | 139 { |
4561 | 140 #ifdef USE_AO |
4581 | 141 if(ao_initialized) |
142 ao_shutdown(); | |
4561 | 143 #endif |
4430 | 144 } |
145 | |
4581 | 146 |
147 #ifdef USE_NAS_AUDIO | |
148 static gboolean play_file_nas(const char *filename) | |
149 { | |
150 AuServer *nas_serv; | |
151 gboolean ret = FALSE; | |
152 | |
153 if((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) { | |
154 ret = AuSoundPlaySynchronousFromFile(nas_serv, filename, 100); | |
155 AuCloseServer(nas_serv); | |
156 } | |
157 | |
158 return ret; | |
159 } | |
160 | |
161 #endif /* USE_NAS_AUDIO */ | |
162 | |
4561 | 163 void gaim_sound_play_file(char *filename) |
4430 | 164 { |
4581 | 165 #if defined(USE_NAS_AUDIO) || defined(USE_AO) |
4561 | 166 pid_t pid; |
4581 | 167 #ifdef USE_AO |
168 AFfilehandle file; | |
169 #endif | |
170 #endif | |
4430 | 171 |
4561 | 172 if (mute_sounds) |
173 return; | |
4430 | 174 |
3901 | 175 if (awaymessage && !(sound_options & OPT_SOUND_WHEN_AWAY)) |
176 return; /* check here in case a buddy pounce plays a file while away */ | |
4561 | 177 |
3004 | 178 if (sound_options & OPT_SOUND_BEEP) { |
179 gdk_beep(); | |
180 return; | |
181 } | |
182 | |
4561 | 183 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { |
5281 | 184 char *tmp = g_strdup_printf(_("Unable to play sound because the chosen file (%s) does not exist."), filename); |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
185 gaim_notify_error(NULL, NULL, tmp, NULL); |
4561 | 186 g_free(tmp); |
187 return; | |
3060 | 188 } |
4561 | 189 |
4430 | 190 #ifndef _WIN32 |
4561 | 191 if ((sound_options & OPT_SOUND_CMD)) { |
192 char *command; | |
193 GError *error = NULL; | |
194 | |
195 if(!sound_cmd) { | |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
196 gaim_notify_error(NULL, NULL, |
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
197 _("Unable to play sound because the " |
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
198 "'Command' sound method has been chosen, " |
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
199 "but no command has been set."), NULL); |
4561 | 200 return; |
201 } | |
202 | |
203 command = g_strdup_printf(sound_cmd, filename); | |
204 | |
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); | |
5436
ad445074d239
[gaim-migrate @ 5818]
Christian Hammond <chipx86@chipx86.com>
parents:
5281
diff
changeset
|
207 gaim_notify_error(NULL, NULL, tmp, NULL); |
4561 | 208 g_free(tmp); |
209 g_error_free(error); | |
210 } | |
211 | |
212 g_free(command); | |
213 return; | |
214 } | |
4581 | 215 #if defined(USE_NAS_AUDIO) || defined(USE_AO) |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
216 pid = fork(); |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
217 if (pid < 0) |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
218 return; |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
219 else if (pid == 0) { |
4581 | 220 #ifdef USE_NAS_AUDIO |
221 if ((sound_options & OPT_SOUND_NAS)) { | |
222 if (play_file_nas(filename)) | |
223 _exit(0); | |
224 } | |
225 #endif /* USE_NAS_AUDIO */ | |
226 | |
227 #ifdef USE_AO | |
228 file = afOpenFile(filename, "rb", NULL); | |
4561 | 229 if(file) { |
230 ao_device *device; | |
231 ao_sample_format format; | |
232 int in_fmt; | |
233 int bytes_per_frame; | |
4429 | 234 |
4561 | 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); | |
239 | |
4878 | 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; | |
244 | |
4561 | 245 afSetVirtualSampleFormat(file, AF_DEFAULT_TRACK, |
246 AF_SAMPFMT_TWOSCOMP, format.bits); | |
4429 | 247 |
4592 | 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 | |
4561 | 257 |
258 bytes_per_frame = format.bits * format.channels / 8; | |
259 | |
260 device = ao_open_live(ao_driver, &format, NULL); | |
261 | |
262 if(device) { | |
263 int frames_read; | |
264 char buf[4096]; | |
265 int buf_frames = sizeof(buf) / bytes_per_frame; | |
266 | |
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; | |
271 } | |
272 ao_close(device); | |
273 } | |
274 afCloseFile(file); | |
4430 | 275 } |
4581 | 276 ao_shutdown(); |
277 #endif /* USE_AO */ | |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
278 _exit(0); |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
279 } |
4581 | 280 #else /* USE_NAS_AUDIO || USE_AO */ |
4561 | 281 gdk_beep(); |
282 return; | |
4581 | 283 #endif /* USE_NAS_AUDIO || USE_AO */ |
3630 | 284 #else /* _WIN32 */ |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
285 gaim_debug(GAIM_DEBUG_INFO, "sound", "Playing %s\n", filename); |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
286 |
3630 | 287 if (!PlaySound(filename, 0, SND_ASYNC | SND_FILENAME)) |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
288 gaim_debug(GAIM_DEBUG_ERROR, "sound", "Error playing sound.\n"); |
4561 | 289 #endif /* _WIN32 */ |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
290 } |
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
291 |
4561 | 292 void gaim_sound_play_event(GaimSoundEventID event) |
1 | 293 { |
4561 | 294 if ((event == GAIM_SOUND_BUDDY_ARRIVE) && mute_login_sounds) |
3060 | 295 return; |
296 | |
4561 | 297 if (event >= GAIM_NUM_SOUNDS) { |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
298 gaim_debug(GAIM_DEBUG_MISC, "sound", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
4878
diff
changeset
|
299 "got request for unknown sound: %d\n", event); |
3060 | 300 return; |
301 } | |
302 | |
303 /* check NULL for sounds that don't have an option, ie buddy pounce */ | |
4561 | 304 if ((sound_options & sounds[event].opt) || (sounds[event].opt == 0)) { |
305 if (sound_file[event]) { | |
306 gaim_sound_play_file(sound_file[event]); | |
3060 | 307 } else { |
4004 | 308 gchar *filename = NULL; |
309 | |
4561 | 310 filename = g_build_filename(DATADIR, "sounds", "gaim", sounds[event].def, NULL); |
311 gaim_sound_play_file(filename); | |
4004 | 312 g_free(filename); |
1006
0a4d0ed65e17
[gaim-migrate @ 1016]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
899
diff
changeset
|
313 } |
1252
46c09828e929
[gaim-migrate @ 1262]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1235
diff
changeset
|
314 } |
64 | 315 } |
4561 | 316 |
317 void gaim_sound_set_mute(gboolean mute) | |
318 { | |
319 mute_sounds = mute; | |
320 } | |
321 | |
322 gboolean gaim_sound_get_mute() | |
323 { | |
324 return mute_sounds; | |
325 } | |
326 | |
327 void gaim_sound_set_login_mute(gboolean mute) | |
328 { | |
329 mute_login_sounds = mute; | |
330 } | |
331 | |
332 void gaim_sound_set_event_file(GaimSoundEventID event, const char *filename) | |
333 { | |
334 if(event >= GAIM_NUM_SOUNDS) | |
335 return; | |
336 | |
337 if(sound_file[event]) | |
338 g_free(sound_file[event]); | |
339 | |
340 sound_file[event] = g_strdup(filename); | |
341 } | |
342 | |
343 | |
344 char *gaim_sound_get_event_file(GaimSoundEventID event) | |
345 { | |
346 if(event >= GAIM_NUM_SOUNDS) | |
347 return NULL; | |
348 | |
349 return sound_file[event]; | |
350 } | |
351 | |
352 guint gaim_sound_get_event_option(GaimSoundEventID event) | |
353 { | |
354 if(event >= GAIM_NUM_SOUNDS) | |
355 return 0; | |
356 | |
357 return sounds[event].opt; | |
358 } | |
359 | |
360 char *gaim_sound_get_event_label(GaimSoundEventID event) | |
361 { | |
362 if(event >= GAIM_NUM_SOUNDS) | |
363 return NULL; | |
364 | |
365 return sounds[event].label; | |
366 } | |
367 | |
368 | |
369 void gaim_sound_set_command(const char *cmd) | |
370 { | |
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; | |
377 } | |
378 | |
379 char *gaim_sound_get_command() | |
380 { | |
381 return sound_cmd; | |
382 } | |
383 |