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