Mercurial > pidgin
annotate src/sound.c @ 550:6bd2bbc1b6c8
[gaim-migrate @ 560]
Always show top of info
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Fri, 28 Jul 2000 21:18:24 +0000 |
parents | 7b49c67e561c |
children | eaab8abda2c0 |
rev | line source |
---|---|
1 | 1 /* |
2 * gaim | |
3 * | |
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
349
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
23 #include "../config.h" |
b402a23f35df
[gaim-migrate @ 359]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
338
diff
changeset
|
24 #endif |
1 | 25 #include <stdio.h> |
26 #include <string.h> | |
27 #include <sys/time.h> | |
28 #include <unistd.h> | |
29 #include <gtk/gtk.h> | |
30 #include <fcntl.h> | |
31 #include <sys/wait.h> | |
32 #include <sys/signal.h> | |
63 | 33 #include <unistd.h> |
34 #include <sys/types.h> | |
35 #include <sys/stat.h> | |
1 | 36 |
37 #ifdef ESD_SOUND | |
38 #include <esd.h> | |
39 #endif | |
40 | |
41 #ifdef NAS_SOUND | |
42 #include <audio/audiolib.h> | |
43 #endif | |
44 | |
45 #include "gaim.h" | |
46 #include "sounds/BuddyArrive.h" | |
47 #include "sounds/BuddyLeave.h" | |
48 #include "sounds/Send.h" | |
49 #include "sounds/Receive.h" | |
50 | |
51 | |
52 static void play_audio(char *data, int size) | |
53 { | |
54 int fd; | |
55 | |
56 fd = open("/dev/audio", O_WRONLY | O_EXCL); | |
57 if (fd < 0) | |
58 return; | |
59 write(fd, data, size); | |
60 close(fd); | |
61 } | |
62 | |
63 static int can_play_audio() | |
64 { | |
79 | 65 struct stat stat_buf; |
63 | 66 uid_t user = getuid(); |
67 gid_t group = getgid(); | |
79 | 68 if (stat("/dev/audio", &stat_buf)) |
63 | 69 return 0; |
79 | 70 if (user == stat_buf.st_uid && stat_buf.st_mode & S_IWUSR) |
63 | 71 return 1; |
79 | 72 if (group == stat_buf.st_gid && stat_buf.st_mode & S_IWGRP) |
63 | 73 return 1; |
79 | 74 if (stat_buf.st_mode & S_IWOTH) |
63 | 75 return 1; |
76 return 0; | |
1 | 77 } |
78 | |
79 | |
80 #ifdef ESD_SOUND | |
81 /* | |
82 ** This routine converts from ulaw to 16 bit linear. | |
83 ** | |
84 ** Craig Reese: IDA/Supercomputing Research Center | |
85 ** 29 September 1989 | |
86 ** | |
87 ** References: | |
88 ** 1) CCITT Recommendation G.711 (very difficult to follow) | |
89 ** 2) MIL-STD-188-113,"Interoperability and Performance Standards | |
90 ** for Analog-to_Digital Conversion Techniques," | |
91 ** 17 February 1987 | |
92 ** | |
93 ** Input: 8 bit ulaw sample | |
94 ** Output: signed 16 bit linear sample | |
95 ** Z-note -- this is from libaudiofile. Thanks guys! | |
96 */ | |
97 | |
98 int _af_ulaw2linear (unsigned char ulawbyte) | |
99 { | |
100 static int exp_lut[8] = {0,132,396,924,1980,4092,8316,16764}; | |
101 int sign, exponent, mantissa, sample; | |
102 | |
103 ulawbyte = ~ulawbyte; | |
104 sign = (ulawbyte & 0x80); | |
105 exponent = (ulawbyte >> 4) & 0x07; | |
106 mantissa = ulawbyte & 0x0F; | |
107 sample = exp_lut[exponent] + (mantissa << (exponent + 3)); | |
108 if (sign != 0) sample = -sample; | |
109 | |
110 return(sample); | |
111 } | |
112 | |
113 | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
114 int esd_fd; |
1 | 115 |
116 static int play_esd(unsigned char *data, int size) | |
117 { | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
118 int i; |
79 | 119 guint16 *lineardata; |
1 | 120 |
79 | 121 lineardata = g_malloc(size * 2); |
1 | 122 |
123 for (i=0; i<size; i++) | |
124 lineardata[i] = _af_ulaw2linear(data[i]); | |
125 | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
126 write(esd_fd, lineardata, size * 2); |
1 | 127 |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
128 close(esd_fd); |
1 | 129 g_free(lineardata); |
130 | |
131 return 1; | |
132 | |
133 } | |
134 | |
135 static int can_play_esd() | |
136 { | |
354
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
137 esd_format_t format = ESD_BITS16 | ESD_STREAM | ESD_PLAY | ESD_MONO; |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
138 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
139 esd_fd = esd_play_stream(format, 8012, NULL, "gaim"); |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
140 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
141 if (esd_fd < 0) { |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
142 return 0; |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
143 } |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
144 |
2b91e42bff70
[gaim-migrate @ 364]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
349
diff
changeset
|
145 return 1; |
1 | 146 } |
147 | |
148 #endif | |
149 | |
150 #ifdef NAS_SOUND | |
151 | |
152 char nas_server[] = "localhost"; | |
153 AuServer *nas_serv = NULL; | |
154 | |
155 static AuBool | |
156 NasEventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler) | |
157 { | |
158 AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev; | |
159 | |
160 if (ev->type == AuEventTypeElementNotify) { | |
161 switch (event->kind) { | |
162 case AuElementNotifyKindState: | |
163 switch (event->cur_state) { | |
164 case AuStateStop: | |
165 _exit(0); | |
166 } | |
167 break; | |
168 } | |
169 } | |
170 return AuTrue; | |
171 } | |
172 | |
173 | |
174 static int play_nas(unsigned char *data, int size) | |
175 { | |
176 AuDeviceID device = AuNone; | |
177 AuFlowID flow; | |
178 AuElement elements[3]; | |
179 int i, n, w; | |
180 | |
181 /* look for an output device */ | |
182 for (i = 0; i < AuServerNumDevices(nas_serv); i++) { | |
183 if ((AuDeviceKind(AuServerDevice(nas_serv, i)) == | |
184 AuComponentKindPhysicalOutput) && | |
185 AuDeviceNumTracks(AuServerDevice(nas_serv, i)) == 1) { | |
186 device = AuDeviceIdentifier(AuServerDevice(nas_serv, i)); | |
187 break; | |
188 } | |
189 } | |
190 | |
191 if (device == AuNone) | |
192 return 0; | |
193 | |
194 if (!(flow = AuCreateFlow(nas_serv, NULL))) | |
195 return 0; | |
196 | |
197 | |
198 AuMakeElementImportClient(&elements[0], 8012, AuFormatULAW8, | |
199 1, AuTrue, size, size/2, 0, NULL); | |
200 AuMakeElementExportDevice(&elements[1], 0, device, 8012, | |
201 AuUnlimitedSamples, 0, NULL); | |
202 AuSetElements(nas_serv, flow, AuTrue, 2, elements, NULL); | |
203 | |
204 AuStartFlow(nas_serv, flow, NULL); | |
205 | |
206 AuWriteElement(nas_serv, flow, 0, size, data, AuTrue, NULL); | |
207 | |
208 AuRegisterEventHandler(nas_serv, AuEventHandlerIDMask, 0, flow, | |
209 NasEventHandler, NULL); | |
210 | |
211 while(1) { | |
212 AuHandleEvents(nas_serv); | |
213 } | |
214 | |
215 return 1; | |
216 } | |
217 | |
218 static int can_play_nas() | |
219 { | |
220 if ((nas_serv = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL))) | |
221 return 1; | |
222 return 0; | |
223 } | |
224 | |
225 #endif | |
226 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
227 void play(unsigned char *data, int size) |
1 | 228 { |
229 int pid; | |
230 | |
231 #ifdef _WIN32 | |
232 return; | |
233 #endif | |
234 | |
235 pid = fork(); | |
236 | |
237 if (pid < 0) | |
238 return; | |
239 else if (pid == 0) { | |
240 #ifdef ESD_SOUND | |
241 /* ESD is our player of choice. Are we OK to | |
242 * go there? */ | |
243 if (can_play_esd()) { | |
244 if (play_esd(data, size)) | |
245 _exit(0); | |
246 } | |
247 #endif | |
248 | |
249 #ifdef NAS_SOUND | |
250 /* NAS is our second choice setup. */ | |
251 if (can_play_nas()) { | |
252 if (play_nas(data, size)) | |
253 _exit(0); | |
254 } | |
255 #endif | |
256 | |
257 /* Lastly, we can try just plain old /dev/audio */ | |
258 if (can_play_audio()) { | |
259 play_audio(data, size); | |
260 _exit(0); | |
261 } | |
262 } else { | |
263 gtk_timeout_add(100, (GtkFunction)clean_pid, NULL); | |
264 } | |
265 } | |
266 | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
267 extern int logins_not_muted; |
1 | 268 |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
269 #ifdef USE_APPLET |
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
270 void applet_play_sound(int sound) |
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
271 #else |
1 | 272 void play_sound(int sound) |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
273 #endif |
1 | 274 { |
275 | |
276 switch(sound) { | |
277 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
278 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
1 | 279 play(BuddyArrive, sizeof(BuddyArrive)); |
280 break; | |
281 case BUDDY_LEAVE: | |
282 if (sound_options & OPT_SOUND_LOGOUT) | |
283 play(BuddyLeave, sizeof(BuddyLeave)); | |
284 break; | |
285 case SEND: | |
286 if (sound_options & OPT_SOUND_SEND) | |
287 play(Send, sizeof(Send)); | |
288 break; | |
289 case FIRST_RECEIVE: | |
290 if (sound_options & OPT_SOUND_FIRST_RCV) | |
291 play(Receive, sizeof(Receive)); | |
292 break; | |
293 case RECEIVE: | |
294 if (sound_options & OPT_SOUND_RECV) | |
295 play(Receive, sizeof(Receive)); | |
296 break; | |
297 case AWAY: | |
298 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
299 play(Receive, sizeof(Receive)); | |
300 break; | |
301 } | |
64 | 302 } |
1 | 303 |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
304 #ifdef USE_APPLET |
64 | 305 |
82 | 306 #include <gnome.h> |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
307 void play_sound(int sound) |
64 | 308 { |
309 | |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
310 if (!(sound_options & OPT_SOUND_THROUGH_GNOME)) { |
451
7b49c67e561c
[gaim-migrate @ 461]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
354
diff
changeset
|
311 applet_play_sound(sound); |
338
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
312 return; |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
313 } |
9d258a0aa560
[gaim-migrate @ 348]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
178
diff
changeset
|
314 |
64 | 315 switch(sound) { |
316 case BUDDY_ARRIVE: | |
133
e277d5f0c1dd
[gaim-migrate @ 143]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
108
diff
changeset
|
317 if ((sound_options & OPT_SOUND_LOGIN) && logins_not_muted) |
64 | 318 gnome_triggers_do("", "program", "gaim_applet", "login", NULL); |
319 break; | |
320 case BUDDY_LEAVE: | |
321 if (sound_options & OPT_SOUND_LOGOUT) | |
322 gnome_triggers_do("", "program", "gaim_applet", "leave", NULL); | |
323 break; | |
324 case SEND: | |
325 if (sound_options & OPT_SOUND_SEND) | |
326 gnome_triggers_do("", "program", "gaim_applet", "send", NULL); | |
327 break; | |
328 case FIRST_RECEIVE: | |
329 if (sound_options & OPT_SOUND_FIRST_RCV) | |
330 gnome_triggers_do("", "program", "gaim_applet", "recv", NULL); | |
331 break; | |
332 case RECEIVE: | |
333 if (sound_options & OPT_SOUND_RECV) | |
334 gnome_triggers_do("", "program", "gaim_applet", "recv", NULL); | |
335 break; | |
336 case AWAY: | |
337 if (sound_options & OPT_SOUND_WHEN_AWAY) | |
338 gnome_triggers_do("", "program", "gaim_applet", "recv", NULL); | |
339 break; | |
340 } | |
1 | 341 } |
64 | 342 |
343 #endif /* USE_APPLET */ |