Debian Codegraph @

rplayd/sound.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* $Id: sound.c,v 1.10 1999/06/09 06:27:44 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <sys/types.h>
28 #include <sys/uio.h>
29 #include <sys/stat.h>
30 #include <sys/file.h>
31 #include <sys/errno.h>
32 #include <sys/signal.h>
33 #include <sys/ioctl.h>
34 #ifdef ultrix
35 #include <fcntl.h>
36 #else
37 #include <sys/fcntl.h>
38 #endif
39 #include <sys/param.h>
40 #ifdef HAVE_MMAP
41 #include <sys/mman.h>
42 #endif
43 #include <stdio.h>
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <dirent.h>
50 #include "sound.h"
51 #include "rplayd.h"
52 #include "cache.h"
53 #include "ulaw.h"
54 #include "xhash.h"
55 #include "server.h"
56 #include "buffer.h"
57 #include "connection.h"
58 #include "misc.h"
59 #include "strdup.h"
60 #include "spool.h"
61 #ifdef HAVE_CDROM
62 #include "cdrom.h"
63 #endif /* HAVE_CDROM */
64 #ifdef HAVE_HELPERS
65 #include "helper.h"
66 #endif /* HAVE_HELPERS */
67 #ifdef HAVE_REGEX_H
68 #include <regex.h>
69 #else
70 #ifdef HAVE_RX_RXPOSIX_H
71 #include <rx/rxposix.h>
72 #else
73 #ifdef HAVE_RXPOSIX_H
74 #include <rxposix.h>
75 #else
76 #include "rxposix.h"
77 #endif
78 #endif
79 #endif
80
81 /* Make sure MAXPATHLEN is defined. */
82 #ifndef MAXPATHLEN
83 #define MAXPATHLEN 4096
84 #endif
85
86 SOUND *sounds = NULL;
87 int sound_count = 0;
88 static time_t sound_read_time;
89
90 int sound_cache_size = 0;
91 int sound_cache_max_sound_size = MEMORY_CACHE_SOUND_SIZE;
92 int sound_cache_max_size = MEMORY_CACHE_SIZE;
93
94 #ifdef BAD_DIRS
95 static regex_t bad_dirs;
96
97 /* Prepare `bad_dirs'. */
98 static void
99 bad_dirs_init()
100 {
101 static char *buf;
102 char *p;
103 char *dirs;
104 int first, length;
105
106 dirs = strdup(BAD_DIRS); /* XXX */
107
108 length = strlen("^\\(") + strlen("\\)") + strlen(dirs) + 1;
109
110 //length += strlen ("^");
111 for (p = dirs; *p; p++)
112 {
113 if (*p == ':')
114 {
115 length += strlen("\\|") - strlen(":");
116 }
117 }
118
119 if (buf)
120 {
121 free(buf);
122 }
123 buf = (char *) malloc(length);
124 if (buf == NULL)
125 {
126 report(REPORT_ERROR, "bad_dir_init: out of memory\n");
127 done(1);
128 }
129
130 first = 1;
131 strcpy(buf, "^\\(");
132 while (p = (char *) strtok(first ? dirs : 0, ":"))
133 {
134 if (first)
135 {
136 //strcat (buf, "^");
137 first = 0;
138 }
139 else
140 {
141 //strcat (buf, "\\|^");
142 strcat(buf, "\\|");
143 }
144 strcat(buf, p);
145 }
146 strcat(buf, "\\)");
147
148 #if 0
149 report(REPORT_DEBUG, "bad_dirs=%s, strlen=%d, length=%d\n",
150 buf, strlen(buf), length);
151 #endif
152
153 //memset ((char *) &bad_dirs, 0, sizeof (bad_dirs));
154
155 if (regcomp(&bad_dirs, buf, REG_ICASE | REG_NOSUB))
156 {
157 report(REPORT_ERROR, "bad_dirs: regcomp failed\n");
158 done(1);
159 }
160
161 free(dirs);
162 }
163
164 #ifdef __STDC__
165 static int
166 bad_dir(char *dir)
167 #else
168 static int
169 bad_dir(dir)
170 char *dir;
171 #endif
172 {
173 /* return 1 if bad */
174 return regexec(&bad_dirs, dir, 0, 0, 0) ? 0 : 1;
175 }
176
177 #endif /* BAD_DIRS */
178
179 #ifdef __STDC__
180 void
181 sound_read_directory(char *dirname)
182 #else
183 void
184 sound_read_directory(dirname)
185 char *dirname;
186 #endif
187 {
188 char soundname[MAXPATHLEN];
189 struct dirent *dp;
190 DIR *dir = opendir(dirname);
191 if (dir == NULL)
192 {
193 report(REPORT_ERROR, "opendir %s: %s", sys_err_str(errno));
194 return;
195 }
196
197 while ((dp = readdir(dir)) != NULL)
198 {
199 if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
200 {
201 continue;
202 }
203 sprintf(soundname, "%s/%s", dirname, dp->d_name);
204 sound_insert(soundname, SOUND_READY, SOUND_FILE);
205 }
206
207 closedir(dir);
208 }
209
210
211 /*
212 * read the rplay configuration file and load the hash table
213 */
214 #ifdef __STDC__
215 void
216 sound_read(char *filename)
217 #else
218 void
219 sound_read(filename)
220 char *filename;
221 #endif
222 {
223 FILE *fp;
224 char buf[MAXPATHLEN], *p;
225 struct stat st;
226
227 xhash_init(MAX_SOUNDS);
228
229 #ifdef BAD_DIRS
230 bad_dirs_init();
231 #endif
232
233 #ifdef HAVE_CDROM
234 {
235 /* Insert the CDROM devices into the sound list. */
236 int i;
237 for (i = 0; i < MAX_CDROMS; i++)
238 {
239 sound_insert(cdrom_table[i].name, SOUND_READY, SOUND_CDROM);
240 }
241 }
242 #endif /* HAVE_CDROM */
243
244 sound_read_time = time(0);
245
246 fp = fopen(filename, "r");
247 if (fp == NULL)
248 {
249 report(REPORT_NOTICE, "warning: cannot open %s\n", filename);
250 /*
251 * no local sounds
252 */
253 return;
254 }
255
256 while (fgets(buf, sizeof(buf), fp) != NULL)
257 {
258 switch (buf[0])
259 {
260 case '#':
261 case ' ':
262 case '\t':
263 case '\n':
264 continue;
265 }
266 p = strchr(buf, '\n');
267 if (p)
268 {
269 *p = '\0';
270 }
271
272 if (stat(buf, &st) < 0)
273 {
274 report(REPORT_ERROR, "sound_read: %s: %s\n", buf, sys_err_str(errno));
275 continue;
276 }
277
278 if (S_ISDIR(st.st_mode))
279 {
280 sound_read_directory(buf);
281 }
282 else
283 {
284 sound_insert(buf, SOUND_READY, SOUND_FILE);
285 }
286 }
287
288 fclose(fp);
289 }
290
291 #ifdef __STDC__
292 static char *
293 destroy(char *hash_string, char *hash_value)
294 #else
295 static char *
296 destroy(hash_string, hash_value)
297 char *hash_string;
298 char *hash_value;
299 #endif
300 {
301 SOUND *s = (SOUND *) hash_value;
302
303 sound_free(s);
304 free((char *) s);
305
306 return NULL;
307 }
308
309 /*
310 * Re-read the rplay configuration file and reload the hash table.
311 */
312 #ifdef __STDC__
313 void
314 sound_reread(char *filename)
315 #else
316 void
317 sound_reread(filename)
318 char *filename;
319 #endif
320 {
321 SOUND *s1, *s1_next, *s2, *s2_next;
322
323 report(REPORT_DEBUG, "re-reading sounds\n");
324
325 /*
326 * Delete all the sounds.
327 */
328 #if 1
329 for (s1 = sounds; s1; s1 = s1_next)
330 {
331 s1_next = s1->list;
332 for (s2 = s1; s2; s2 = s2_next)
333 {
334 s2_next = s2->next;
335 /* xhash_delete(s2->hash_key); */
336 sound_free(s2);
337 free((char *) s2);
338 }
339 }
340 #else
341 xhash_apply(destroy);
342 #endif
343
344 xhash_die();
345 xhash_init(MAX_SOUNDS);
346
347 sounds = NULL;
348 sound_count = 0;
349
350 /*
351 * and now load the sounds
352 */
353 sound_read(filename);
354
355 /*
356 * don't forget the cache...
357 */
358 cache_read();
359 }
360
361 #ifdef __STDC__
362 void
363 sound_stat(char *filename)
364 #else
365 void
366 sound_stat(filename)
367 char *filename;
368 #endif
369 {
370 if (modified(filename, sound_read_time))
371 {
372 sound_reread(filename);
373 }
374 }
375
376 BUFFER *
377 sound_list_create()
378 {
379 SOUND *s;
380 int n;
381 BUFFER *start, *b;
382 char line[RPTP_MAX_LINE];
383
384 b = buffer_create();
385 start = b;
386 SNPRINTF(SIZE(b->buf + strlen(b->buf), BUFFER_SIZE), "+message=\"sounds\"\r\n");
387 b->nbytes += strlen(b->buf);
388
389 for (s = sounds; s; s = s->list)
390 {
391 if (s->status != SOUND_READY)
392 {
393 continue;
394 }
395
396 if (s->mapped)
397 {
398 SNPRINTF(SIZE(line, sizeof(line)), "\
399 sound=\"%s\" size=%d bits=%g sample_rate=%d channels=%d samples=%d\r\n",
400 s->name, s->size, s->input_precision, s->sample_rate, s->channels,
401 s->samples);
402 }
403 else
404 {
405 SNPRINTF(SIZE(line, sizeof(line)), "sound=\"%s\"\r\n", s->name);
406 }
407
408 n = strlen(line);
409 if (b->nbytes + n > BUFFER_SIZE)
410 {
411 b->next = buffer_create();
412 b = b->next;
413 }
414 strncat(b->buf + b->nbytes, line, BUFFER_SIZE - b->nbytes);
415 b->nbytes += n;
416 }
417
418 if (b->nbytes + 3 > BUFFER_SIZE)
419 {
420 b->next = buffer_create();
421 b = b->next;
422 }
423
424 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), ".\r\n");
425 b->nbytes += 3;
426
427 return start;
428 }
429
430 #ifdef __STDC__
431 SOUND *
432 sound_insert(char *path, int status, int type)
433 #else
434 SOUND *
435 sound_insert(path, status, type)
436 char *path;
437 int status;
438 int type;
439 #endif
440 {
441 SOUND *s;
442 char *hash_val;
443
444 #ifdef BAD_DIRS
445 if (bad_dir(path))
446 {
447 report(REPORT_NOTICE, "bad_dir: attempt to load %s\n", path);
448 return NULL;
449 }
450 #endif
451
452 s = sound_create();
453 if (s == NULL)
454 {
455 report(REPORT_ERROR, "sound_insert: out of memory\n");
456 done(1);
457 }
458
459 s->type = type;
460
461 s->path = strdup(path);
462 if (s->path == NULL)
463 {
464 report(REPORT_ERROR, "sound_insert: out of memory\n");
465 done(1);
466 }
467 s->hash_key = strdup(xhash_name(s->path));
468 if (s->hash_key == NULL)
469 {
470 report(REPORT_ERROR, "sound_insert: out of memory\n");
471 done(1);
472 }
473 s->name = s->path[0] == '/' ? strrchr(s->path, '/') + 1 : s->path;
474 s->status = status;
475
476 hash_val = xhash_get(s->hash_key);
477 if (hash_val == NULL)
478 {
479 xhash_put(s->hash_key, (char *) s);
480
481 s->list_prev = NULL;
482 s->list = sounds;
483 if (sounds)
484 {
485 sounds->list_prev = s;
486 }
487 sounds = s;
488 }
489 else
490 {
491 SOUND *ss, *prev = NULL;
492
493 for (ss = (SOUND *) hash_val; ss; prev = ss, ss = ss->next)
494 {
495 if (strcmp(ss->path, s->path) == 0)
496 {
497 free((char *) s);
498 return ss;
499 }
500 }
501 prev->next = s;
502 s->prev = prev;
503 }
504
505 return s;
506 }
507
508 /*
509 * create a sound
510 */
511 #ifdef __STDC__
512 SOUND *
513 sound_create(void)
514 #else
515 SOUND *
516 sound_create()
517 #endif
518 {
519 SOUND *s;
520
521 s = (SOUND *) malloc(sizeof(SOUND));
522 if (s == NULL)
523 {
524 return s;
525 }
526 s->list = NULL;
527 s->list_prev = NULL;
528 s->next = NULL;
529 s->prev = NULL;
530 s->type = 0;
531 s->storage = SOUND_STORAGE_NULL;
532 s->path = NULL;
533 s->name = NULL;
534 s->hash_key = NULL;
535 s->status = SOUND_NULL;
536 s->count = 0;
537 s->format = RPLAY_FORMAT_NONE;
538 s->byte_order = 0;
539 s->sample_rate = 0;
540 s->input_precision = 0;
541 s->output_precision = 0;
542 s->channels = 0;
543 s->samples = 0;
544 s->input_sample_size = 0;
545 s->output_sample_size = 0;
546 s->offset = 0;
547 s->size = 0;
548 s->tail = 0;
549 s->chunk_size = 0;
550 s->mapped = 0;
551 s->cache = NULL;
552 s->flow = NULL;
553 s->flowp = &s->flow;
554 #ifdef HAVE_CDROM
555 s->starting_track = 0;
556 s->ending_track = 0;
557 #endif /* HAVE_CDROM */
558 #ifdef HAVE_HELPERS
559 s->needs_helper = 0;
560 #endif /* HAVE_HELPERS */
561
562 return s;
563 }
564
565 /*
566 * Lookup the sound name in the hash table and map the sound file if
567 * necessary.
568 */
569 #ifdef __STDC__
570 SOUND *
571 sound_lookup(char *name, int mode, SERVER *lookup_server)
572 #else
573 SOUND *
574 sound_lookup(name, mode, lookup_server)
575 char *name;
576 int mode;
577 SERVER *lookup_server;
578 #endif
579 {
580 SOUND *s = NULL;
581 int has_extension = 0, has_pathname = 0;
582 struct stat st;
583 int i;
584
585 #ifdef HAVE_CDROM
586 /* Map sounds with cdrom prefixes to the cdrom itself. */
587 for (i = 0; i < MAX_CDROMS; i++)
588 {
589 if (strncmp(name, cdrom_table[i].name, strlen(cdrom_table[i].name)) == 0)
590 {
591 break;
592 }
593 }
594 if (i < MAX_CDROMS)
595 {
596 s = (SOUND *) xhash_get(xhash_name(cdrom_table[i].name));
597 }
598 #endif /* HAVE_CDROM */
599
600 if (s == NULL)
601 {
602 s = (SOUND *) xhash_get(xhash_name(name));
603 }
604
605 if (s == NULL)
606 {
607 report(REPORT_DEBUG, "%s not in hash table\n", name);
608
609 if (mode == SOUND_DONT_FIND || mode == SOUND_DONT_COUNT)
610 {
611 return NULL;
612 }
613
614 /*
615 * see if a local file can be loaded (mode == SOUND_LOAD)
616 */
617 if (stat(name, &st) == 0)
618 {
619 report(REPORT_DEBUG, "loading local file %s\n", name);
620 s = sound_insert(name, SOUND_READY, SOUND_FILE);
621 if (s == NULL)
622 {
623 return NULL;
624 }
625 }
626 #if 0
627 /* virtual sounds cause searching problems */
628 #ifdef HAVE_HELPERS
629 else if (mode == SOUND_CREATE && helper_lookup(name))
630 {
631 s = sound_insert(name, SOUND_READY, SOUND_VIRTUAL);
632 }
633 #endif
634 #endif
635 else if (mode == SOUND_FIND)
636 {
637 if (name[0] == '/')
638 {
639 report(REPORT_DEBUG, "not searching for %s\n", name);
640 return NULL;
641 }
642 if (lookup_server)
643 {
644 report(REPORT_DEBUG, "searching for %s\n", name);
645 s = sound_insert(cache_name(name), SOUND_SEARCH, SOUND_FILE);
646 if (s == NULL)
647 {
648 return NULL;
649 }
650 connection_server_open(lookup_server, s);
651 }
652 else if (servers)
653 {
654 report(REPORT_DEBUG, "searching for %s\n", name);
655 s = sound_insert(cache_name(name), SOUND_SEARCH, SOUND_FILE);
656 if (s == NULL)
657 {
658 return NULL;
659 }
660 connection_server_open(servers, s);
661 }
662 else
663 {
664 return NULL;
665 }
666 }
667 else
668 {
669 return NULL;
670 }
671 }
672 else
673 {
674 /*
675 * Make sure the sound found has the correct path and dot
676 * extension.
677 */
678 if (name[0] == '/')
679 {
680 has_pathname++;
681 }
682 else if (strrchr(name, '.'))
683 {
684 has_extension++;
685 }
686 for (; s; s = s->next)
687 {
688 if (has_pathname && (!s->path || strcmp(name, s->path) != 0))
689 {
690 continue;
691 }
692 if (has_extension && (!s->path || strcmp(name, s->name) != 0))
693 {
694 continue;
695 }
696 break;
697 }
698 if (s == NULL)
699 {
700 if (has_pathname)
701 {
702 report(REPORT_DEBUG, "`%s' file name not in hash table\n", name);
703 if (stat(name, &st) == 0)
704 {
705 report(REPORT_DEBUG, "loading local file `%s'\n", name);
706 s = sound_insert(name, SOUND_READY, SOUND_FILE);
707 if (s == NULL)
708 {
709 return NULL;
710 }
711 }
712 else
713 {
714 return NULL;
715 }
716 }
717 else if (has_extension)
718 {
719 report(REPORT_DEBUG, "`%s' extension not in hash table\n", name);
720 return NULL;
721 }
722 }
723 }
724
725 if (mode == SOUND_DONT_COUNT)
726 {
727 return s;
728 }
729 else
730 {
731 sound_count++;
732 s->count = sound_count;
733 }
734
735 switch (s->status)
736 {
737 case SOUND_NOT_READY:
738 case SOUND_SEARCH:
739 return s;
740 }
741
742 if (!s->mapped)
743 {
744 return sound_map(s) == 0 ? s : NULL;
745 }
746 else
747 {
748 return s;
749 }
750 }
751
752 /*
753 * Map a sound using its header or file extension.
754 */
755 #ifdef __STDC__
756 int
757 sound_map(SOUND *s)
758 #else
759 int
760 sound_map(s)
761 SOUND *s;
762 #endif
763 {
764 SINDEX *si;
765 BUFFER *b;
766 int n, storage;
767 int optional_offset;
768 int optional_format;
769 int optional_byte_order;
770 int optional_sample_rate;
771 float optional_input_precision;
772 int optional_output_precision;
773 int optional_channels;
774 int optional_storage;
775 char buf[SOUND_MAX_HEADER_SIZE];
776 int sound_chunk_size = 0;
777 #ifdef HAVE_HELPERS
778 HELPER *helper = NULL;
779 #endif /* HAVE_HELPERS */
780
781 if (s->mapped)
782 {
783 return 0;
784 }
785
786 /* Save the optional settings. */
787 optional_offset = s->offset;
788 optional_format = s->format;
789 optional_byte_order = s->byte_order;
790 optional_sample_rate = s->sample_rate;
791 optional_input_precision = s->input_precision;
792 optional_output_precision = s->output_precision;
793 optional_channels = s->channels;
794 optional_storage = s->storage;
795
796 #ifdef HAVE_HELPERS
797 helper = helper_lookup(s->path);
798 if (helper)
799 {
800 s->needs_helper++;
801 strcpy(buf, "HELPER"); /* XXX: not used; see below */
802 }
803 else
804 #endif /* HAVE_HELPERS */
805 #ifdef HAVE_CDROM
806 if (s->type == SOUND_CDROM)
807 {
808 strcpy(buf, "CDROM");
809 }
810 else
811 #endif /* HAVE_CDROM */
812 {
813 si = sound_open(s, 1);
814 if (si == NULL)
815 {
816 return -1;
817 }
818
819 /* Trick sound_fill to not free any buffers used to determine what
820 sort of sound this is. */
821 storage = s->storage;
822 s->storage = SOUND_STORAGE_MEMORY;
823 b = buffer_alloc(SOUND_MAX_HEADER_SIZE, BUFFER_FREE);
824 n = sound_fill(si, b, 1);
825 s->storage = storage;
826 if (n <= 0)
827 {
828 report(REPORT_DEBUG, "sound_map: sound_fill %d\n", n);
829 buffer_dealloc(b, 1);
830 return -1;
831 }
832
833 sound_close(si);
834
835 /* Copy the buffer contents to `buf' so `b' can be destroyed. */
836 memcpy(buf, b->buf, MIN(sizeof(buf), b->nbytes));
837 buffer_dealloc(b, 1);
838 }
839
840 /* u-law */
841 if (strncmp(buf, ".snd", 4) == 0)
842 {
843 long ulaw_hdr_size;
844 int encoding;
845 int data_size;
846 char *p;
847
848 p = buf + 4;
849
850 ulaw_hdr_size = big_long(p);
851 p += 4;
852 if (ulaw_hdr_size < ULAW_HDRSIZE)
853 {
854 report(REPORT_DEBUG, "%s not a ulaw file\n", s->path);
855 return -1;
856 }
857
858 /*
859 * The audio data size which may be ~0.
860 * This value is ignored.
861 */
862 data_size = big_long(p);
863 p += 4;
864
865 encoding = big_long(p);
866 p += 4;
867 switch (encoding)
868 {
869 case ULAW_MULAW_8:
870 s->format = RPLAY_FORMAT_ULAW;
871 s->input_precision = 8;
872 s->output_precision = 8;
873 break;
874
875 case ULAW_LINEAR_8:
876 s->format = RPLAY_FORMAT_LINEAR_8;
877 s->input_precision = 8;
878 s->output_precision = 8;
879 break;
880
881 case ULAW_LINEAR_16:
882 s->format = RPLAY_FORMAT_LINEAR_16;
883 s->input_precision = 16;
884 s->output_precision = 16;
885 break;
886
887 #ifdef HAVE_ADPCM
888 case ULAW_G721:
889 s->format = RPLAY_FORMAT_G721;
890 s->input_precision = 4;
891 s->output_precision = 16; /* uncompressed to 16-bit */
892 break;
893
894 case ULAW_G723_3:
895 s->format = RPLAY_FORMAT_G723_3;
896 s->input_precision = 3;
897 s->output_precision = 16; /* uncompressed to 16-bit */
898 break;
899
900 case ULAW_G723_5:
901 s->format = RPLAY_FORMAT_G723_5;
902 s->input_precision = 5;
903 s->output_precision = 16; /* uncompressed to 16-bit */
904 break;
905 #endif /* HAVE_ADPCM */
906
907 default:
908 report(REPORT_DEBUG, "%s: %d - unsupported ulaw encoding\n",
909 s->path, encoding);
910 return -1;
911 }
912
913 /*
914 * Byte order.
915 */
916 s->byte_order = RPLAY_BIG_ENDIAN;
917
918 /*
919 * Sample rate.
920 */
921 s->sample_rate = big_long(p);
922 p += 4;
923
924 /*
925 * Number of channels.
926 */
927 s->channels = big_long(p);
928 p += 4;
929
930 s->offset = ulaw_hdr_size;
931
932 if (data_size == ~0)
933 {
934 sound_chunk_size = s->size - s->offset;
935 }
936 else
937 {
938 sound_chunk_size = data_size;
939 }
940 }
941 /* aiff */
942 else if (strncmp(buf, "FORM", 4) == 0)
943 {
944 unsigned long total_size;
945 unsigned long chunk_size;
946 unsigned long frames;
947 unsigned long offset;
948 unsigned long block_size;
949 double rate;
950 int channels;
951 int bits;
952 char id[5];
953 char *p;
954
955 p = buf + 4;
956
957 total_size = big_long(p);
958 p += 4;
959
960 if (memcmp(p, "AIFF", 4) != 0)
961 {
962 return -1;
963 }
964 p += 4;
965
966 /*
967 * Scan the chunks.
968 */
969 for (;;)
970 {
971 /*
972 * Chunk id.
973 */
974 memcpy(id, p, 4);
975 id[4] = '\0';
976 p += 4;
977
978 /*
979 * Chunk size.
980 */
981 chunk_size = big_long(p);
982 p += 4;
983
984 /*
985 * COMM Chunk.
986 */
987 if (memcmp(id, "COMM", 4) == 0)
988 {
989 channels = big_short(p);
990 p += 2;
991
992 frames = big_long(p);
993 p += 4;
994
995 bits = big_short(p);
996 p += 2;
997
998 rate = ConvertFromIeeeExtended((unsigned char *) p);
999 p += 10;
1000 }
1001 /*
1002 * SSN Chunk.
1003 */
1004 else if (memcmp(id, "SSND", 4) == 0)
1005 {
1006 offset = big_long(p);
1007 p += 4;
1008
1009 block_size = big_long(p);
1010 sound_chunk_size = block_size;
1011 p += 4;
1012 break;
1013 }
1014 /*
1015 * Ignore other chunks.
1016 */
1017 else
1018 {
1019 p += chunk_size;
1020 /*
1021 * skip the pad byte, if necessary
1022 */
1023 if (chunk_size % 2 == 1)
1024 p++;
1025 }
1026 }
1027
1028 /*
1029 * Byte order.
1030 */
1031 s->byte_order = RPLAY_BIG_ENDIAN;
1032
1033 s->offset = p - buf;
1034
1035 s->sample_rate = rate;
1036 s->input_precision = bits;
1037 s->output_precision = bits;
1038 s->channels = channels;
1039
1040 switch ((int) s->input_precision)
1041 {
1042 case 8:
1043 s->format = RPLAY_FORMAT_LINEAR_8;
1044 break;
1045
1046 case 16:
1047 s->format = RPLAY_FORMAT_LINEAR_16;
1048 break;
1049
1050 default:
1051 report(REPORT_ERROR, "unsupported aiff precision `%g'\n",
1052 s->input_precision);
1053 return -1;
1054 }
1055 }
1056 /* deadsnd files */
1057 else if (*((long *) &buf) == 0x4a02b6d2)
1058 {
1059 DSID *dsid = (DSID *) & buf;
1060
1061 s->offset = sizeof(DSID);
1062 s->sample_rate = dsid->freq;
1063 s->input_precision = s->output_precision = dsid->bps;
1064 s->channels = (dsid->mode + 1);
1065 s->byte_order = RPLAY_LITTLE_ENDIAN;
1066
1067 switch (dsid->bps)
1068 {
1069 case 8:
1070 s->format = RPLAY_FORMAT_ULINEAR_8;
1071 break;
1072
1073 case 16:
1074 s->format = RPLAY_FORMAT_LINEAR_16;
1075 break;
1076
1077 default:
1078 report(REPORT_ERROR, "unsupported deadsnd precision `%d'\n", dsid->bps);
1079 return -1;
1080 }
1081 }
1082 /* wave */
1083 else if (strncmp(buf, "RIFF", 4) == 0)
1084 {
1085 unsigned long chunk_size;
1086 short chunk_type;
1087 double rate;
1088 int channels;
1089 int bits;
1090 char id[5];
1091 char *p;
1092
1093 p = buf + 4;
1094 p += 4;
1095
1096 if (memcmp(p, "WAVE", 4) != 0)
1097 {
1098 report(REPORT_ERROR, "wave file missing `WAVE' header\n");
1099 return -1;
1100 }
1101 p += 4;
1102
1103 /*
1104 * Scan the chunks.
1105 */
1106 for (;;)
1107 {
1108 /*
1109 * Chunk id.
1110 */
1111 memcpy(id, p, 4);
1112 id[4] = '\0';
1113 p += 4;
1114
1115 /*
1116 * Chunk size.
1117 */
1118 chunk_size = little_long(p);
1119 p += 4;
1120
1121 /*
1122 * 'fmt ' Chunk.
1123 */
1124 if (memcmp(id, "fmt ", 4) == 0)
1125 {
1126 char *start_of_chunk = p;
1127 chunk_type = little_short(p);
1128 p += 2;
1129 /*
1130 * Only one wave format is supported.
1131 */
1132 if (chunk_type == 0x0001)
1133 {
1134 channels = little_short(p);
1135 p += 2;
1136 rate = little_long(p);
1137 p += 4;
1138 p += 4; /* bytes/second ? */
1139 p += 2; /* block align ? */
1140 bits = little_short(p);
1141 p += 2;
1142 }
1143 else
1144 {
1145 report(REPORT_ERROR, "unknown wave chunk `%x'\n", chunk_type);
1146 return -1;
1147 }
1148 p = start_of_chunk + chunk_size;
1149 }
1150 /*
1151 * data Chunk.
1152 */
1153 else if (memcmp(id, "data", 4) == 0)
1154 {
1155 /* size */
1156 sound_chunk_size = chunk_size;
1157 break;
1158 }
1159 /*
1160 * Ignore other chunks.
1161 */
1162 else
1163 {
1164 report(REPORT_DEBUG, "ignoring chunk - %s (%d)\n",
1165 id, chunk_size);
1166 p += chunk_size;
1167 /*
1168 * skip the pad byte, if necessary
1169 */
1170 if (chunk_size % 2 == 1)
1171 p++;
1172 }
1173 }
1174
1175 s->offset = p - buf;
1176 s->sample_rate = rate;
1177 s->input_precision = bits;
1178 s->output_precision = bits;
1179 s->channels = channels;
1180
1181 switch ((int) s->input_precision)
1182 {
1183 case 8:
1184 s->format = RPLAY_FORMAT_ULINEAR_8;
1185 break;
1186
1187 case 16:
1188 s->format = RPLAY_FORMAT_LINEAR_16;
1189 break;
1190
1191 default:
1192 report(REPORT_ERROR, "unsupported wave precision `%g'\n",
1193 s->input_precision);
1194 return -1;
1195 }
1196
1197 /*
1198 * Byte order.
1199 */
1200 s->byte_order = RPLAY_LITTLE_ENDIAN;
1201 }
1202 /* voc */
1203 else if (strncmp(buf, "Creative Voice File\032", 20) == 0)
1204 {
1205 char *p;
1206 short header_size;
1207 unsigned long block_length;
1208 unsigned char block_id, rate;
1209
1210 p = buf + 20;
1211
1212 header_size = little_short(p); /* sizeof header */
1213 p += 2;
1214
1215 /* major/minor version + checksum of version */
1216 p += 2 * sizeof(short);
1217
1218 for (;;)
1219 {
1220 block_id = *p++;
1221
1222 if (block_id == 0) /* VOC_TERM */
1223 {
1224 break;
1225 }
1226
1227 /* Block length */
1228 block_length = (unsigned char) *p++;
1229 block_length |= ((unsigned char) *p++ << 8);
1230 block_length |= ((unsigned char) *p++ << 16);
1231
1232 if (block_id == 1) /* VOC_DATA */
1233 {
1234 //sound_chunk_size = block_length;
1235
1236 rate = *p++;
1237 s->input_precision = 8;
1238 s->output_precision = 8;
1239 s->sample_rate = 1000000.0 / (256 - rate);
1240 s->channels = 1;
1241 break;
1242 }
1243 else if (block_id == 9) /* VOC_DATA_16 */
1244 {
1245 //sound_chunk_size = block_length;
1246
1247 s->sample_rate = little_long(p);
1248 p += 4;
1249
1250 switch (*p)
1251 {
1252 case 8:
1253 s->input_precision = 8;
1254 s->output_precision = 8;
1255 break;
1256
1257 case 16:
1258 s->input_precision = 16;
1259 s->output_precision = 16;
1260 break;
1261
1262 default:
1263 report(REPORT_ERROR, "unsupported voc_data_16 precision `%d'\n",
1264 *p);
1265 return -1;
1266 }
1267 *p++;
1268
1269 s->channels = *p++;
1270
1271 p++; /* unknown */
1272 p++; /* not used */
1273 p++; /* not used */
1274 p++; /* not used */
1275 p++; /* not used */
1276 p++; /* not used */
1277
1278 break;
1279 }
1280
1281 p += block_length;
1282 }
1283
1284 s->offset = p - buf;
1285 s->byte_order = RPLAY_LITTLE_ENDIAN;
1286
1287 switch ((int) s->input_precision)
1288 {
1289 case 8:
1290 s->format = RPLAY_FORMAT_ULINEAR_8;
1291 break;
1292
1293 case 16:
1294 s->format = RPLAY_FORMAT_ULINEAR_16;
1295 break;
1296
1297 default:
1298 report(REPORT_ERROR, "unsupported voc precision `%d'\n",
1299 s->input_precision);
1300 return -1;
1301 }
1302 }
1303 #ifdef HAVE_GSM
1304 /* gsm */
1305 else if (((*buf >> 4) & 0xf) == GSM_MAGIC)
1306 {
1307 s->offset = 0;
1308 s->format = RPLAY_FORMAT_GSM;
1309 s->byte_order = RPLAY_BIG_ENDIAN; /* ??? */
1310 s->sample_rate = 8000;
1311 s->input_precision = 1.65;
1312 s->output_precision = 16;
1313 s->channels = 1;
1314 }
1315 #endif /* HAVE_GSM */
1316 #ifdef HAVE_CDROM
1317 else if (strcmp(buf, "CDROM") == 0)
1318 {
1319 s->offset = 0;
1320 s->format = RPLAY_FORMAT_LINEAR_16;
1321 s->byte_order = RPLAY_LITTLE_ENDIAN;
1322 s->sample_rate = 44100;
1323 s->input_precision = 16;
1324 s->output_precision = 16;
1325 s->channels = 2;
1326 s->storage = SOUND_STORAGE_NONE;
1327 }
1328 #endif /* HAVE_CDROM */
1329 /* Unknown audio header -- try to use the extension. */
1330 else
1331 {
1332 char *p;
1333
1334 report(REPORT_DEBUG, "%s missing sound header; trying helper or extension\n", s->path);
1335 p = strrchr(s->path, '.');
1336 #ifdef HAVE_HELPERS
1337 if (s->needs_helper)
1338 {
1339 s->offset = 0;
1340 s->format = helper->format;
1341 s->byte_order = helper->byte_order;
1342 s->sample_rate = helper->sample_rate;
1343 s->input_precision = helper->precision;
1344 s->output_precision = helper->precision;
1345 s->channels = helper->channels;
1346 s->storage = SOUND_STORAGE_NONE;
1347 }
1348 else
1349 #endif
1350 if (p && strcmp(p, ".ub") == 0)
1351 {
1352 report(REPORT_DEBUG, "%s assuming ub\n", s->path);
1353 s->offset = 0;
1354 s->format = RPLAY_FORMAT_ULINEAR_8;
1355 s->byte_order = RPLAY_BIG_ENDIAN;
1356 s->sample_rate = 11025;
1357 s->input_precision = 8;
1358 s->output_precision = 8;
1359 s->channels = 1;
1360 }
1361 else if (p && (strcmp(p, ".au") == 0 || strcmp(p, ".ul") == 0))
1362 {
1363 report(REPORT_DEBUG, "%s assuming ulaw\n", s->path);
1364 s->offset = 0;
1365 s->format = RPLAY_FORMAT_ULAW;
1366 s->byte_order = RPLAY_BIG_ENDIAN;
1367 s->sample_rate = 8000;
1368 s->input_precision = 8;
1369 s->output_precision = 8;
1370 s->channels = 1;
1371 }
1372 #ifdef HAVE_GSM
1373 else if (p && (strcmp(p, ".gsm") == 0 || strcmp(p, ".GSM") == 0))
1374 {
1375 report(REPORT_DEBUG, "%s assuming gsm\n", s->path);
1376 s->offset = 0;
1377 s->format = RPLAY_FORMAT_GSM;
1378 s->byte_order = RPLAY_BIG_ENDIAN; /* ??? */
1379 s->sample_rate = 8000;
1380 s->input_precision = 1.65;
1381 s->output_precision = 16;
1382 s->channels = 1;
1383 }
1384 #endif /* HAVE_GSM */
1385 else if (!optional_format
1386 && !optional_byte_order
1387 && !optional_sample_rate
1388 && !optional_input_precision
1389 && !optional_channels)
1390 {
1391 report(REPORT_ERROR, "`%s' unknown audio file\n", s->path);
1392 return -1;
1393 }
1394 }
1395
1396 if (optional_offset)
1397 s->offset = optional_offset;
1398 if (optional_format)
1399 s->format = optional_format;
1400 if (optional_byte_order)
1401 s->byte_order = optional_byte_order;
1402 if (optional_sample_rate)
1403 s->sample_rate = optional_sample_rate;
1404 if (optional_input_precision)
1405 s->input_precision = optional_input_precision;
1406 if (optional_output_precision)
1407 s->output_precision = optional_output_precision;
1408 if (optional_channels)
1409 s->channels = optional_channels;
1410 if (optional_storage)
1411 s->storage = optional_storage;
1412
1413 switch (s->format)
1414 {
1415 #ifdef HAVE_ADPCM
1416 /* G.72X files *must* have 16-bit output. */
1417 case RPLAY_FORMAT_G721:
1418 case RPLAY_FORMAT_G723_3:
1419 case RPLAY_FORMAT_G723_5:
1420 s->output_precision = 16;
1421 break;
1422 #endif /* HAVE_ADPCM */
1423
1424 #ifdef HAVE_GSM
1425 /* Fix-up GSM info. */
1426 case RPLAY_FORMAT_GSM:
1427 s->output_precision = 16;
1428 s->input_precision = 1.65;
1429 s->byte_order = RPLAY_BIG_ENDIAN;
1430 s->channels = 1;
1431 s->offset = 0;
1432 break;
1433 #endif /* HAVE_GSM */
1434
1435 default:
1436 break;
1437 }
1438
1439 if (!s->format || !s->byte_order || !s->sample_rate
1440 || !s->input_precision || !s->output_precision || !s->channels)
1441 {
1442 report(REPORT_ERROR, "`%s' invalid audio parameters\n", s->path);
1443 return -1;
1444 }
1445
1446 /* The input sample size is a floating point number since compressed
1447 samples can be smaller than 8 bits. */
1448 /* XXX output_precision -> input_precision */
1449 s->input_sample_size = (s->input_precision / 8) * s->channels;
1450 s->output_sample_size = (s->output_precision >> 3) * s->channels;
1451
1452 if (sound_chunk_size > 0)
1453 {
1454 /* Make sure sound_chunk_size is valid */
1455 if (s->size == 0 || sound_chunk_size < s->size) /* size==0 for flows */
1456 {
1457 s->chunk_size = sound_chunk_size;
1458 }
1459 }
1460
1461 /* Calculate tail only if chunk_size is in sound header */
1462 if (s->chunk_size)
1463 {
1464 s->tail = s->offset + s->chunk_size;
1465 }
1466
1467 /* Calculate chunk_size */
1468 if (!s->chunk_size && s->size)
1469 {
1470 s->chunk_size = s->size - s->offset;
1471 }
1472
1473 #if 0
1474 printf("SOUND: %s size=%d chunk=%d tail=%d offset=%d\n",
1475 s->name, s->size, s->chunk_size, s->tail, s->offset);
1476 #endif
1477
1478 if (s->type != SOUND_FILE)
1479 {
1480 s->size = 0;
1481 s->samples = 0;
1482 }
1483 #ifdef HAVE_HELPERS
1484 /* helper sounds should never have size and samples set */
1485 else if (s->needs_helper)
1486 {
1487 s->size = 0;
1488 s->samples = 0;
1489 }
1490 #endif
1491 else
1492 {
1493 s->samples = number_of_samples(s, 0); // NUMBER_OF_SAMPLES (s);
1494
1495 }
1496
1497 s->mapped = 1;
1498
1499 report(REPORT_DEBUG, "\
1500 %s input=%s bits=%g sample-rate=%d channels=%d samples=%d format=%s byte-order=%s\n",
1501 s->name,
1502 input_to_string(s->type),
1503 s->input_precision,
1504 s->sample_rate,
1505 s->channels,
1506 s->samples,
1507 audio_format_to_string(s->format),
1508 byte_order_to_string(s->byte_order));
1509
1510 return 0;
1511 }
1512
1513 /*
1514 * unmap a sound -- forget about it
1515 */
1516 #ifdef __STDC__
1517 int
1518 sound_unmap(SOUND *s)
1519 #else
1520 int
1521 sound_unmap(s)
1522 SOUND *s;
1523 #endif
1524 {
1525 if (s->type == SOUND_FILE)
1526 {
1527 if (s->cache)
1528 {
1529 #ifdef HAVE_MMAP
1530 munmap(s->cache, s->size);
1531 #else /* not HAVE_MMAP */
1532 free((char *) s->cache);
1533 #endif /* not HAVE_MMAP */
1534 s->cache = NULL;
1535 sound_cache_size -= s->size;
1536 }
1537 }
1538
1539 if (s->type == SOUND_FLOW || s->needs_helper)
1540 {
1541 buffer_dealloc(s->flow, 1);
1542 s->flow = NULL;
1543 s->flowp = &s->flow;
1544 }
1545
1546 s->mapped = 0;
1547
1548 return 0;
1549 }
1550
1551 /* Free *all* memory used by a sound. */
1552 #ifdef __STDC__
1553 int
1554 sound_free(SOUND *s)
1555 #else
1556 int
1557 sound_free(s)
1558 SOUND *s;
1559 #endif
1560 {
1561 sound_unmap(s);
1562
1563 free((char *) s->path);
1564 s->path = NULL;
1565 free((char *) s->hash_key);
1566 s->hash_key = NULL;
1567
1568 return 0;
1569 }
1570
1571 /* Delete a sound from the list of sounds. */
1572 #ifdef __STDC__
1573 void
1574 sound_delete(SOUND *s, int remove)
1575 #else
1576 void
1577 sound_delete(s, remove)
1578 SOUND *s;
1579 int remove;
1580 #endif
1581 {
1582 /* No other sounds with the same hash key.
1583 This is the general case. */
1584 if (s->next == NULL && s->prev == NULL)
1585 {
1586 if (s->hash_key)
1587 {
1588 xhash_delete(s->hash_key);
1589 }
1590 if (s->list_prev)
1591 {
1592 s->list_prev->list = s->list;
1593 }
1594 else
1595 {
1596 sounds = s->list;
1597 }
1598 if (s->list)
1599 {
1600 s->list->list_prev = s->list_prev;
1601 }
1602 }
1603 /* Sounds with same hash key. */
1604 else
1605 {
1606 /* not the first node */
1607 if (s->prev)
1608 {
1609 s->prev->next = s->next;
1610 if (s->next)
1611 {
1612 s->next->prev = s->prev;
1613 }
1614 }
1615 /* the first node with a next node */
1616 else
1617 {
1618 if (s->list_prev)
1619 {
1620 s->list_prev->list = s->next;
1621 }
1622 else
1623 {
1624 sounds = s->next;
1625 }
1626 if (s->list)
1627 {
1628 s->list->list_prev = s->next;
1629 }
1630
1631 /* Copy the list links. */
1632 s->next->list = s->list;
1633 s->next->list_prev = s->list_prev;
1634
1635 xhash_replace(s->next->hash_key, (char *) s->next);
1636 }
1637 }
1638
1639 if (remove)
1640 {
1641 struct stat st;
1642
1643 if (stat(s->path, &st) == 0)
1644 {
1645 if (unlink(s->path) < 0)
1646 {
1647 report(REPORT_ERROR, "sound_delete: unlink %s: %s\n", s->path,
1648 sys_err_str(errno));
1649 }
1650 }
1651 }
1652
1653 sound_free(s);
1654 free((char *) s);
1655 }
1656
1657 /*
1658 * Free cached sounds and delete sound flows.
1659 */
1660 void
1661 sound_cleanup()
1662 {
1663 SOUND *s1, *s1_next, *s, *s_next;
1664 EVENT *e;
1665 CONNECTION *c;
1666 SPOOL *sp;
1667 int n;
1668
1669 report(REPORT_DEBUG, "cleaning up sounds\n");
1670
1671 for (s1 = sounds; s1; s1 = s1_next)
1672 {
1673 s1_next = s1->list;
1674
1675 for (s = s1; s; s = s_next)
1676 {
1677 s_next = s->next;
1678
1679 /* Cached sounds */
1680 if (s->type == SOUND_FILE && s->status == SOUND_READY && s->cache)
1681 {
1682 n = 0;
1683 for (c = connections; c && n == 0; c = c->next)
1684 {
1685 /* Don't clean sounds with events. */
1686 for (e = c->event; e && n == 0; e = e->next)
1687 {
1688 switch (e->type)
1689 {
1690 case EVENT_WRITE_FIND:
1691 case EVENT_READ_FIND_REPLY:
1692 case EVENT_WRITE_GET:
1693 case EVENT_READ_GET_REPLY:
1694 case EVENT_READ_SOUND:
1695 case EVENT_WRITE_SOUND:
1696 if (e->sound == s)
1697 {
1698 n++;
1699 }
1700 break;
1701 }
1702 }
1703 /* Don't clean sounds in the spool. */
1704 for (sp = spool; sp; sp = sp->next)
1705 {
1706 if (sp->sound[sp->curr_sound] == s)
1707 {
1708 n++;
1709 }
1710 }
1711 }
1712 if (n == 0)
1713 {
1714 sound_unmap(s);
1715 }
1716 }
1717 /* Flows */
1718 else if (s->type == SOUND_FLOW)
1719 {
1720 /* Don't cleanup flows in the spool. */
1721 n = 0;
1722 for (sp = spool; sp; sp = sp->next)
1723 {
1724 if (sp->sound[sp->curr_sound] == s)
1725 {
1726 n++;
1727 }
1728 }
1729 if (n != 0)
1730 {
1731 report(REPORT_DEBUG, "sound_clean: not cleaning flow `%s'\n", s->name);
1732 }
1733 else
1734 {
1735 sound_clean(s);
1736 }
1737 }
1738 #ifdef HAVE_CDROM
1739 else if (s->type == SOUND_CDROM)
1740 {
1741 n = 0;
1742 for (sp = spool; sp; sp = sp->next)
1743 {
1744 if (sp->sound[sp->curr_sound] == s)
1745 {
1746 n++;
1747 }
1748 }
1749 if (n != 0)
1750 {
1751 report(REPORT_DEBUG, "sound_clean: not cleaning cdrom `%s'\n", s->name);
1752 }
1753 else
1754 {
1755 sound_clean(s);
1756 }
1757 }
1758 #endif /* HAVE_CDROM */
1759 }
1760 }
1761
1762 sound_cache_size = 0;
1763 }
1764
1765 #ifdef __STDC__
1766 void
1767 sound_clean(SOUND *s)
1768 #else
1769 void
1770 sound_clean(s)
1771 SOUND *s;
1772 #endif
1773 {
1774 BUFFER *b;
1775 int n, fd;
1776
1777 #ifdef HAVE_HELPERS
1778 if (s->needs_helper)
1779 {
1780 s->samples = 0;
1781 s->offset = 0;
1782 s->size = 0;
1783 if (s->flow)
1784 {
1785 buffer_dealloc(s->flow, 1);
1786 }
1787 s->flow = NULL;
1788 s->flowp = &s->flow;
1789 return;
1790 }
1791 #endif /* HAVE_HELPERS */
1792
1793 if (s->type == SOUND_FILE)
1794 {
1795 return;
1796 }
1797 #ifdef HAVE_CDROM
1798 else if (s->type == SOUND_CDROM)
1799 {
1800 s->samples = 0;
1801 s->offset = 0;
1802 s->size = 0;
1803 if (s->flow)
1804 {
1805 buffer_dealloc(s->flow, 1);
1806 }
1807 s->flow = NULL;
1808 s->flowp = &s->flow;
1809 return;
1810 }
1811 #endif /* HAVE_CDROM */
1812
1813 switch (s->storage)
1814 {
1815 case SOUND_STORAGE_NONE:
1816 /* nothing to free -- delete it */
1817 report(REPORT_DEBUG, "sound_clean: deleting flow `%s'\n", s->name);
1818 sound_delete(s, 0);
1819 break;
1820
1821 case SOUND_STORAGE_MEMORY:
1822 /* keep it in memory? */
1823 report(REPORT_DEBUG, "sound_clean: keeping flow `%s' in memory\n", s->name);
1824 break;
1825
1826 case SOUND_STORAGE_DISK:
1827 /* Save the flow in the disk cache. */
1828 if (cache_free(s->size) < 0)
1829 {
1830 report(REPORT_ERROR, "sound_clean: can't cache_free %d bytes\n", s->size);
1831 sound_delete(s, 0); /* delete it */
1832 break;
1833 }
1834
1835 fd = cache_create(s->path, s->size);
1836 if (fd < 0)
1837 {
1838 report(REPORT_ERROR, "sound_clean: can't cache_create %d bytes\n", s->size);
1839 sound_delete(s, 0);
1840 break;
1841 }
1842
1843 /* Dump the flow buffers to disk. */
1844 report(REPORT_DEBUG, "sound_clean: storing %s\n", s->path);
1845 for (b = s->flow; b; b = b->next)
1846 {
1847 retry:
1848 n = write(fd, b->buf, b->nbytes);
1849 if (n < 0)
1850 {
1851 if (errno == EINTR || errno == EINTR)
1852 {
1853 goto retry;
1854 }
1855 else
1856 {
1857 report(REPORT_ERROR, "sound_clean: write: %s\n", sys_err_str(errno));
1858 sound_delete(s, 1);
1859 s = NULL;
1860 break;
1861 }
1862 }
1863 }
1864 close(fd);
1865
1866 if (s)
1867 {
1868 /* The flow is now a file. */
1869 sound_unmap(s);
1870 s->type = SOUND_FILE;
1871 }
1872 break;
1873 }
1874 }
1875
1876 #ifdef __STDC__
1877 SINDEX *
1878 sound_open(SOUND *s, int use_helper)
1879 #else
1880 SINDEX *
1881 sound_open(s, use_helper)
1882 SOUND *s;
1883 int use_helper;
1884 #endif
1885 {
1886 SINDEX *si;
1887 int n;
1888
1889 si = (SINDEX *) malloc(sizeof(SINDEX));
1890 if (si == NULL)
1891 {
1892 return NULL;
1893 }
1894
1895 si->sound = s;
1896 si->offset = 0;
1897 si->is_cached = 0;
1898 si->eof = 0;
1899 si->fd = 0;
1900 si->skip = 0;
1901 si->buffer_offset = 0;
1902 si->is_flow = 0;
1903 si->flowp = NULL;
1904 si->water_mark = 0;
1905 si->low_water_mark = 0;
1906 si->high_water_mark = 0;
1907 #if defined (HAVE_CDROM) || defined (HAVE_HELPERS)
1908 si->pid = -1;
1909 #endif
1910
1911 switch (s->format)
1912 {
1913 #ifdef HAVE_ADPCM
1914 case RPLAY_FORMAT_G721:
1915 case RPLAY_FORMAT_G723_3:
1916 case RPLAY_FORMAT_G723_5:
1917 g72x_init_state(&si->adpcm_state[0]);
1918 g72x_init_state(&si->adpcm_state[1]);
1919 si->adpcm_in_buffer = 0;
1920 si->adpcm_in_bits = 0;
1921 break;
1922 #endif /* HAVE_ADPCM */
1923
1924 #ifdef HAVE_GSM
1925 case RPLAY_FORMAT_GSM:
1926 si->gsm_object = gsm_create();
1927 si->gsm_bit_frame_bytes = 0;
1928 si->gsm_fixed_buffer_size = 0;
1929 break;
1930 #endif /* HAVE_GSM */
1931
1932 default:
1933 break;
1934 }
1935
1936 #ifdef HAVE_HELPERS
1937 if (use_helper && s->needs_helper)
1938 {
1939 int fds[2];
1940 HELPER *helper;
1941
1942 helper = helper_lookup(s->path);
1943 if (!helper)
1944 {
1945 report(REPORT_ERROR, "%s helper not found\n", s->path);
1946 done(1);
1947 }
1948
1949 si->is_flow = 1;
1950 si->flowp = &s->flow;
1951
1952 if (pipe(fds) < 0)
1953 {
1954 report(REPORT_ERROR, "helper pipe: %s\n", sys_err_str(errno));
1955 sound_close(si);
1956 return NULL;
1957 }
1958
1959 si->pid = fork();
1960 if (si->pid < 0)
1961 {
1962 report(REPORT_ERROR, "helper fork: %s\n", sys_err_str(errno));
1963 sound_close(si);
1964 return NULL;
1965 }
1966
1967 if (si->pid == 0) /* child */
1968 {
1969 int first;
1970 char *argv[64]; /* XXX */
1971 int argc = 0;
1972 char buf[MAXPATHLEN], *p;
1973 int input_fd = -1;
1974
1975 if (s->type == SOUND_FILE)
1976 {
1977 input_fd = open(s->path, O_RDONLY /* | O_NDELAY */ , 0);
1978 if (input_fd < 0)
1979 {
1980 report(REPORT_ERROR, "can't open %s: %s\n", s->path, sys_err_str(errno));
1981 exit(1);
1982 }
1983 }
1984 else if (s->type == SOUND_FLOW)
1985 {
1986 CONNECTION *c;
1987 EVENT *e;
1988 for (c = connections; c && input_fd == -1; c = c->next)
1989 {
1990 for (e = c->event; e; e = e->next)
1991 {
1992 if (e->type == EVENT_PIPE_FLOW && e->sound == s)
1993 {
1994 input_fd = c->fd;
1995 fd_block(input_fd);
1996 break;
1997 }
1998 }
1999 }
2000 }
2001
2002 if (input_fd == -1)
2003 {
2004 report(REPORT_ERROR, "No input for helper, type=%d.\n", s->type);
2005 exit(1);
2006 }
2007
2008 first = 1;
2009 while (p = (char *) strtok(first ? helper->program : NULL, " \t\n"))
2010 {
2011 first = 0;
2012 argv[argc++] = p;
2013 }
2014 argv[argc] = NULL;
2015
2016 close(fds[0]);
2017
2018 if (input_fd != -1)
2019 {
2020 if (dup2(input_fd, 0) != 0) /* input for the helper */
2021 {
2022 report(REPORT_ERROR, "can't dup2 stdin: %s\n", sys_err_str(errno));
2023 exit(1);
2024 }
2025 }
2026 if (dup2(fds[1], 1) != 1) /* output for rplayd */
2027 {
2028 report(REPORT_ERROR, "can't dup2 stdout: %s\n", sys_err_str(errno));
2029 exit(1);
2030 }
2031
2032 execv(argv[0], argv);
2033
2034 report(REPORT_ERROR, "can't execute %s: %s\n", argv[0], sys_err_str(errno));
2035 exit(1);
2036 }
2037
2038 report(REPORT_DEBUG, "forked helper process %d\n", si->pid);
2039 close(fds[1]);
2040 si->fd = fds[0];
2041 FD_SET(si->fd, &read_mask);
2042 }
2043 #else
2044 if (0)
2045 {
2046 /* hack */
2047 }
2048 #endif /* HAVE_HELPERS */
2049 else if (s->type == SOUND_FILE)
2050 {
2051 if (!s->cache)
2052 {
2053 struct stat st;
2054
2055 si->fd = open(s->path, O_RDONLY | O_NDELAY, 0);
2056 if (si->fd < 0)
2057 {
2058 report(REPORT_DEBUG, "sound_open: open %s: %s\n", s->path,
2059 sys_err_str(errno));
2060 sound_close(si);
2061 return NULL;
2062 }
2063
2064 fd_nonblock(si->fd);
2065
2066 if (!s->mapped || (s->mapped && !s->size))
2067 {
2068 if (fstat(si->fd, &st) < 0)
2069 {
2070 report(REPORT_ERROR, "sound_open: fstat %s: %s\n",
2071 s->path, sys_err_str(errno));
2072 sound_close(si);
2073 return NULL;
2074 }
2075
2076 if (S_ISDIR(st.st_mode))
2077 {
2078 report(REPORT_ERROR, "sound_open: %s is a directory\n",
2079 s->path);
2080 sound_close(si);
2081 return NULL;
2082 }
2083
2084 s->size = (int) st.st_size;
2085
2086 /* See if the entire sound can be cached. */
2087 if (sound_cache_max_size
2088 && !helper_lookup(s->path)
2089 && (sound_cache_max_sound_size == 0 || (s->size <= sound_cache_max_sound_size))
2090 && (sound_cache_size + s->size <= sound_cache_max_size))
2091 {
2092 /* Use mmap to load the entire sound. */
2093 #ifdef HAVE_MMAP
2094
2095 #ifndef MAP_FILE
2096 #define MAP_FILE 0
2097 #endif
2098 s->cache = mmap(0, s->size, PROT_READ, MAP_SHARED | MAP_FILE, si->fd, 0);
2099 if (s->cache == (caddr_t) - 1)
2100 {
2101 report(REPORT_ERROR, "sound_open: %s size=%d mmap: %s\n",
2102 s->path, s->size, sys_err_str(errno));
2103 sound_close(si);
2104 return NULL;
2105 }
2106 #else /* not HAVE_MMAP */
2107 /* Use malloc+read to load the entire sound. */
2108 s->cache = (char *) malloc(s->size);
2109 if (s->cache == NULL)
2110 {
2111 report(REPORT_ERROR, "sound_open: %s size=%d malloc: %s\n",
2112 s->path, s->size, sys_err_str(errno));
2113 sound_close(si);
2114 return NULL;
2115 }
2116 again:
2117 n = read(si->fd, s->cache, s->size);
2118 if (n < 0 && (errno == EINTR || errno == EAGAIN))
2119 {
2120 goto again;
2121 }
2122 if (n != s->size)
2123 {
2124 report(REPORT_ERROR, "sound_open: read %s %d: %s\n",
2125 s->path, s->size, sys_err_str(errno));
2126 free((char *) s->cache);
2127 s->cache = NULL;
2128 sound_close(si);
2129 return NULL;
2130 }
2131 #endif /* not HAVE_MMAP */
2132
2133 sound_cache_size += s->size;
2134 #if 0
2135 report(REPORT_DEBUG, "- cached %s %d, cache_size=%d\n",
2136 s->path, s->size, sound_cache_size);
2137 #endif
2138
2139 /* Cached sounds won't need the file descriptor. */
2140 close(si->fd);
2141 si->fd = 0;
2142 }
2143 }
2144 }
2145
2146 if (s->cache)
2147 {
2148 si->is_cached = 1;
2149 }
2150 else
2151 {
2152 si->is_cached = 0;
2153 }
2154 }
2155 else if (s->type == SOUND_FLOW)
2156 {
2157 si->is_flow = 1;
2158 si->flowp = &s->flow;
2159 }
2160 #ifdef HAVE_CDROM
2161 /* Fork the reader process and treat it like a flow, reading
2162 audio data from the pipe. */
2163 else if (s->type == SOUND_CDROM)
2164 {
2165 int fds[2];
2166
2167 si->is_flow = 1;
2168 si->flowp = &s->flow;
2169
2170 if (pipe(fds) < 0)
2171 {
2172 report(REPORT_ERROR, "cdrom pipe: %s\n", sys_err_str(errno));
2173 sound_close(si);
2174 return NULL;
2175 }
2176
2177 si->pid = fork();
2178 if (si->pid < 0)
2179 {
2180 report(REPORT_ERROR, "cdrom fork: %s\n", sys_err_str(errno));
2181 sound_close(si);
2182 return NULL;
2183 }
2184
2185 if (si->pid == 0) /* child */
2186 {
2187 int i;
2188
2189 close(fds[0]);
2190
2191 #ifdef HAVE_SIGSET
2192 sigset(SIGHUP, SIG_DFL);
2193 sigset(SIGINT, SIG_DFL);
2194 sigset(SIGCHLD, SIG_DFL);
2195 #else
2196 signal(SIGHUP, SIG_DFL);
2197 signal(SIGINT, SIG_DFL);
2198 signal(SIGCHLD, SIG_DFL);
2199 #endif
2200
2201 for (i = 0; i < MAX_CDROMS; i++)
2202 {
2203 if (strcmp(cdrom_table[i].name, si->sound->name) == 0)
2204 {
2205 cdrom_reader(i, si->sound->starting_track, si->sound->ending_track, fds[1]);
2206 exit(0);
2207 }
2208 }
2209
2210 report(REPORT_DEBUG, "cdrom `%s' not found\n", si->sound->name);
2211 exit(1);
2212 }
2213
2214 report(REPORT_DEBUG, "forked cdrom process %d\n", si->pid);
2215 close(fds[1]);
2216 si->fd = fds[0];
2217
2218 FD_SET(si->fd, &read_mask);
2219 }
2220 #endif /* HAVE_CDROM */
2221
2222 return si;
2223 }
2224
2225 #ifdef __STDC__
2226 int
2227 sound_close(SINDEX *si)
2228 #else
2229 int
2230 sound_close(si)
2231 SINDEX *si;
2232 #endif
2233 {
2234 #ifdef HAVE_HELPERS
2235 if (si->sound->needs_helper)
2236 {
2237 if (si->pid != -1)
2238 {
2239 report(REPORT_DEBUG, "killing helper process %d\n", si->pid);
2240 if (kill(si->pid, SIGKILL) < 0)
2241 {
2242 report(REPORT_DEBUG, "kill %d: %s\n", si->pid, sys_err_str(errno));
2243 }
2244 }
2245 if (si->fd != 0)
2246 {
2247 FD_CLR(si->fd, &read_mask);
2248 close(si->fd);
2249 }
2250 if (si->sound->type == SOUND_FLOW)
2251 {
2252 CONNECTION *c, *c_next;
2253 EVENT *e;
2254 for (c = connections; c; c = c_next)
2255 {
2256 c_next = c->next;
2257 for (e = c->event; e; e = e->next)
2258 {
2259 if (e->type == EVENT_PIPE_FLOW && e->sound == si->sound)
2260 {
2261 connection_close(c);
2262 break;
2263 }
2264 }
2265 }
2266 }
2267
2268 }
2269 #endif /* HAVE_HELPERS */
2270
2271 if (si->sound->type == SOUND_FILE)
2272 {
2273 if (!si->is_cached)
2274 {
2275 close(si->fd);
2276 }
2277 }
2278 #ifdef HAVE_CDROM
2279 else if (si->sound->type == SOUND_CDROM)
2280 {
2281 if (si->pid != -1)
2282 {
2283 report(REPORT_DEBUG, "killing cdrom process %d\n", si->pid);
2284 if (kill(si->pid, SIGKILL) < 0)
2285 {
2286 report(REPORT_DEBUG, "kill %d: %s\n", si->pid, sys_err_str(errno));
2287 }
2288 }
2289 if (si->fd != 0)
2290 {
2291 FD_CLR(si->fd, &read_mask);
2292 close(si->fd);
2293 }
2294 }
2295 #endif /* HAVE_CDROM */
2296
2297 switch (si->sound->format)
2298 {
2299 #ifdef HAVE_GSM
2300 case RPLAY_FORMAT_GSM:
2301 gsm_destroy(si->gsm_object);
2302 break;
2303 #endif /* HAVE_GSM */
2304
2305 default:
2306 break;
2307 }
2308
2309 free((char *) si);
2310
2311 return 0;
2312 }
2313
2314 #ifdef HAVE_ADPCM
2315 /* This routine is based on the unpack_input routine from
2316 Sun's CCITT ADPCM decoder. */
2317 static int
2318 adpcm_unpack(si, b, code, bits)
2319 SINDEX *si;
2320 BUFFER *b;
2321 unsigned char *code;
2322 int bits;
2323 {
2324 unsigned char in_byte;
2325
2326 if (si->adpcm_in_bits < bits)
2327 {
2328 if (b->offset >= b->nbytes)
2329 {
2330 *code = 0;
2331 return -1;
2332 }
2333 in_byte = b->buf[b->offset++];
2334 si->adpcm_in_buffer |= (in_byte << si->adpcm_in_bits);
2335 si->adpcm_in_bits += 8;
2336 }
2337
2338 *code = si->adpcm_in_buffer & ((1 << bits) - 1);
2339 si->adpcm_in_buffer >>= bits;
2340 si->adpcm_in_bits -= bits;
2341
2342 return si->adpcm_in_bits > 0;
2343 }
2344
2345 /* Uncompress the `data' buffers into 16-bit linear data. */
2346 static void
2347 adpcm_decode(si, data, decode_routine, decode_bits)
2348 SINDEX *si;
2349 BUFFER *data;
2350 int (*decode_routine) ();
2351 int decode_bits;
2352 {
2353 BUFFER *b = buffer_create();
2354 short *ptr;
2355 unsigned char code;
2356 int channel = 0;
2357
2358 for (; data && data->nbytes; data = data->next)
2359 {
2360 memcpy(b->buf, data->buf, data->nbytes);
2361 b->nbytes = data->nbytes;
2362 b->offset = 0;
2363
2364 ptr = (short *) data->buf;
2365 data->nbytes = 0;
2366
2367 while (adpcm_unpack(si, b, &code, decode_bits) >= 0)
2368 {
2369 *ptr++ = (*decode_routine) (code, AUDIO_ENCODING_LINEAR, &si->adpcm_state[channel]);
2370 if (si->sound->channels == 2)
2371 {
2372 channel ^= 1;
2373 }
2374 data->nbytes += 2; /* 16-bit */
2375 }
2376 #if 1
2377 report(REPORT_DEBUG, "adpcm_decode: uncompressed from %d to %d bytes\n",
2378 b->nbytes, data->nbytes);
2379 #endif
2380 }
2381
2382 buffer_destroy(b);
2383 }
2384 #endif /* HAVE_ADPCM */
2385
2386 #ifdef HAVE_GSM
2387 /* Uncompress the `data' buffers into 16-bit linear data. */
2388 static void
2389 gsm_uncompress(si, data)
2390 SINDEX *si;
2391 BUFFER *data;
2392 {
2393 BUFFER *b = buffer_create();
2394 gsm_signal sample[160];
2395 int nbytes;
2396
2397 for (; data; data = data->next)
2398 {
2399 memcpy(b->buf, data->buf, data->nbytes);
2400 b->nbytes = data->nbytes;
2401 b->offset = 0;
2402 data->nbytes = 0;
2403
2404 do
2405 {
2406 nbytes = MIN(b->nbytes - b->offset, sizeof(si->gsm_bit_frame) - si->gsm_bit_frame_bytes);
2407 memcpy(si->gsm_bit_frame, b->buf + b->offset, nbytes);
2408 b->offset += nbytes;
2409 si->gsm_bit_frame_bytes += nbytes;
2410
2411 if (si->gsm_bit_frame_bytes == sizeof(si->gsm_bit_frame)) /* Only decode complete frames */
2412 {
2413 gsm_decode(si->gsm_object, si->gsm_bit_frame, sample);
2414 memcpy(data->buf + data->nbytes, (char *) sample, sizeof(sample));
2415 data->nbytes += sizeof(sample);
2416 si->gsm_bit_frame_bytes = 0;
2417 }
2418
2419 #if 0
2420 printf("b->offset=%d data->nbytes=%d gsm_bit_frame_bytes=%d\n",
2421 b->offset, data->nbytes, si->gsm_bit_frame_bytes);
2422 #endif
2423
2424 }
2425 while (b->offset < b->nbytes);
2426
2427 #if 1
2428 report(REPORT_DEBUG, "gsm_uncompress: uncompressed from %d to %d bytes\n",
2429 b->nbytes, data->nbytes);
2430 #endif
2431 }
2432
2433 buffer_destroy(b);
2434 }
2435 #endif /* HAVE_GSM */
2436
2437 #ifdef __STDC__
2438 int
2439 sound_fill(SINDEX *si, BUFFER *data, int as_is)
2440 #else
2441 int
2442 sound_fill(si, data, as_is)
2443 SINDEX *si;
2444 BUFFER *data;
2445 int as_is;
2446 #endif
2447 {
2448 static struct iovec iov[UIO_MAXIOV];
2449 int i, n;
2450 int total = 0; /* The total number of bytes filled. */
2451 BUFFER *b;
2452 float bit_factor;
2453 int max_per_buffer;
2454
2455 if (si->eof && !si->is_flow)
2456 {
2457 return 0;
2458 }
2459
2460 if (si->sound->mapped)
2461 {
2462 /* ADPCM compression expands from 3/4/5 bit input to 16 bit output. */
2463 bit_factor = ((float) si->sound->input_precision / si->sound->output_precision);
2464 }
2465 else
2466 {
2467 bit_factor = 1.0;
2468 }
2469 max_per_buffer = BUFFER_SIZE * bit_factor;
2470
2471 /* Force the buffer size to be a multiple of the GSM bit frame size. */
2472 if (si->sound->format == RPLAY_FORMAT_GSM)
2473 {
2474 if (si->gsm_fixed_buffer_size == 0)
2475 {
2476 si->gsm_fixed_buffer_size = max_per_buffer;
2477 while (si->gsm_fixed_buffer_size % sizeof(si->gsm_bit_frame))
2478 {
2479 si->gsm_fixed_buffer_size--;
2480 }
2481 }
2482 max_per_buffer = si->gsm_fixed_buffer_size;
2483 }
2484
2485 /* Read from a flow. */
2486 if (si->is_flow)
2487 {
2488 BUFFER *f, *f_next;
2489
2490 if (!si->flowp)
2491 {
2492 return 0;
2493 }
2494
2495 if (!si->high_water_mark)
2496 {
2497 #ifdef HAVE_CDROM
2498 if (si->sound->type == SOUND_CDROM)
2499 {
2500 SOUND *s = si->sound;
2501 si->low_water_mark = (s->sample_rate * s->input_sample_size) / 2;
2502 si->high_water_mark = s->sample_rate * s->input_sample_size;
2503 }
2504 #endif
2505 #ifdef HAVE_HELPERS
2506 if (!si->high_water_mark && si->sound->needs_helper)
2507 {
2508 SOUND *s = si->sound;
2509 si->low_water_mark = (s->sample_rate * s->input_sample_size) / 4;
2510 si->high_water_mark = (s->sample_rate * s->input_sample_size) / 2;
2511 }
2512 #endif
2513 if (!si->high_water_mark)
2514 {
2515 #if 1
2516 /* XXX test */
2517 SOUND *s = si->sound;
2518 si->low_water_mark = (s->sample_rate * s->input_sample_size) / 4;
2519 si->high_water_mark = (s->sample_rate * s->input_sample_size) / 2;
2520 #else
2521 si->low_water_mark = LOW_WATER_MARK(si->sound);
2522 si->high_water_mark = HIGH_WATER_MARK(si->sound);
2523 #endif
2524 }
2525 }
2526
2527 f = *si->flowp;
2528 for (b = data; b && f; b = b->next)
2529 {
2530 while (f && b->nbytes < max_per_buffer)
2531 {
2532 /* Calculate the total number of bytes available. */
2533 n = f->nbytes - si->buffer_offset - si->skip;
2534 if (n < 0)
2535 {
2536 si->buffer_offset += si->skip;
2537 si->skip -= f->nbytes;
2538 }
2539 else
2540 {
2541 /* Calculate the number of bytes needed. */
2542 i = MIN(n, max_per_buffer - b->nbytes);
2543 memcpy(b->buf + b->nbytes, f->buf + si->buffer_offset + si->skip, i);
2544 si->skip = 0;
2545 b->nbytes += i;
2546 total += i;
2547 si->offset += i;
2548 si->buffer_offset += i;
2549 }
2550
2551 /* See if a new flow buffer is needed. */
2552 if (si->buffer_offset >= f->nbytes)
2553 {
2554 BUFFER *f_next = f->next;
2555
2556 si->buffer_offset = 0;
2557 si->flowp = &f->next;
2558
2559 /* Free the buffer. */
2560 if (si->sound->storage == SOUND_STORAGE_NONE
2561 || si->sound->storage == SOUND_STORAGE_NULL)
2562 {
2563 buffer_destroy(f);
2564 }
2565
2566 f = f_next;
2567 }
2568 }
2569 }
2570
2571 /* Update the flow pointers if the sound is not being stored. */
2572 if (si->sound->storage == SOUND_STORAGE_NONE
2573 || si->sound->storage == SOUND_STORAGE_NULL)
2574 {
2575 si->sound->flow = f;
2576 if (!f)
2577 {
2578 si->sound->flowp = &si->sound->flow;
2579 }
2580 si->flowp = &si->sound->flow;
2581 }
2582
2583 if (si->sound->tail && si->offset >= si->sound->tail)
2584 {
2585 si->eof = 1;
2586 }
2587 }
2588 /* Read from the cache. */
2589 else if (si->is_cached)
2590 {
2591 n = si->sound->size - si->offset;
2592 for (b = data; b; b = b->next)
2593 {
2594 i = MIN(n, max_per_buffer);
2595 if (i > 0)
2596 {
2597 #ifdef DEBUG
2598 report(REPORT_DEBUG, "- cache-read %s %d\n", si->sound->name, i);
2599 #endif
2600 memcpy(b->buf, si->sound->cache + si->offset, i);
2601 b->nbytes = i;
2602 si->offset += i;
2603 n -= i;
2604 total += i;
2605 }
2606 else
2607 {
2608 b->nbytes = 0;
2609 n -= i;
2610 }
2611 }
2612 if (si->offset >= si->sound->size)
2613 {
2614 si->eof = 1;
2615 }
2616 }
2617 /* Read from disk. */
2618 else
2619 {
2620 for (b = data, i = 0; b && i < UIO_MAXIOV; b = b->next, i++)
2621 {
2622 iov[i].iov_base = b->buf;
2623 iov[i].iov_len = max_per_buffer;
2624 }
2625
2626 n = readv(si->fd, iov, i);
2627 if (n < 0)
2628 {
2629 report(REPORT_ERROR, "sound_fill: readv: %s, fd=%d, %s\n",
2630 si->sound->name, si->fd, sys_err_str(errno));
2631 return -1;
2632 }
2633 else if (n == 0)
2634 {
2635 si->eof = 1;
2636 return 0;
2637 }
2638 else
2639 {
2640 si->offset += n;
2641 for (b = data, i = n; b; b = b->next, i -= max_per_buffer)
2642 {
2643 if (i < 0)
2644 {
2645 i = 0;
2646 }
2647 b->nbytes = MIN(i, max_per_buffer);
2648 }
2649 total = n;
2650 }
2651 }
2652
2653 /* uncompress/decode */
2654 if (total && si->sound->mapped && !as_is)
2655 {
2656 switch (si->sound->format)
2657 {
2658 #ifdef HAVE_ADPCM
2659 case RPLAY_FORMAT_G721:
2660 adpcm_decode(si, data, g721_decoder, 4);
2661 break;
2662
2663 case RPLAY_FORMAT_G723_3:
2664 adpcm_decode(si, data, g723_24_decoder, 3);
2665 break;
2666
2667 case RPLAY_FORMAT_G723_5:
2668 adpcm_decode(si, data, g723_40_decoder, 5);
2669 break;
2670 #endif /* HAVE_ADPCM */
2671
2672 #ifdef HAVE_GSM
2673 case RPLAY_FORMAT_GSM:
2674 gsm_uncompress(si, data);
2675 break;
2676 #endif /* HAVE_GSM */
2677 }
2678 }
2679
2680 return total;
2681 }
2682
2683 #ifdef __STDC__
2684 int
2685 sound_seek(SINDEX *si, int offset, int whence)
2686 #else
2687 int
2688 sound_seek(si, offset, whence)
2689 SINDEX *si;
2690 int offset;
2691 int whence;
2692 #endif
2693 {
2694 if (si->is_flow)
2695 {
2696 si->skip += offset; /* XXX: whence is ignored */
2697 return 0;
2698 }
2699 else if (si->is_cached)
2700 {
2701 si->offset += offset; /* XXX: whence is ignored */
2702 return 0;
2703 }
2704 #ifdef HAVE_CDROM
2705 else if (si->sound->type == SOUND_CDROM)
2706 {
2707 return 0;
2708 }
2709 #endif /* HAVE_CDROM */
2710 #ifdef HAVE_HELPERS
2711 else if (si->sound->needs_helper)
2712 {
2713 return 0;
2714 }
2715 #endif /* HAVE_HELPERS */
2716 else
2717 {
2718 return lseek(si->fd, offset, whence);
2719 }
2720 }
2721
2722 #if defined(HAVE_CDROM) || defined (HAVE_HELPERS)
2723 /* Read data from the pipe and return the data in a buffer list. */
2724 #ifdef __STDC__
2725 BUFFER *
2726 sound_pipe_read(SINDEX *si)
2727 #else
2728 BUFFER *
2729 sound_pipe_read(si)
2730 SINDEX *si;
2731 #endif
2732 {
2733 static struct iovec iov[UIO_MAXIOV];
2734 int i, n, nbytes;
2735 BUFFER *b;
2736 BUFFER *data;
2737
2738 /* Pause */
2739 if (si->water_mark - si->offset > si->high_water_mark)
2740 {
2741 FD_CLR(si->fd, &read_mask);
2742 return NULL;
2743 }
2744
2745 /* Determine the number of bytes available in the pipe. */
2746 #if defined (sun) && defined (SVR4) /* Solaris 2.x */
2747 {
2748 struct stat st;
2749 if (fstat(si->fd, &st) < 0)
2750 {
2751 report(REPORT_DEBUG, "sound_pipe_read: fstat: %s\n",
2752 sys_err_str(errno));
2753 return NULL;
2754 }
2755 nbytes = st.st_size;
2756 }
2757 #else /* not Solaris */
2758 #if 0 /* disable FIONREAD for now */
2759 #ifdef FIONREAD
2760 if (ioctl(si->fd, FIONREAD, &nbytes) < 0)
2761 {
2762 report(REPORT_DEBUG, "sound_pipe_read: ioctl FIONREAD: %s\n",
2763 sys_err_str(errno));
2764 return NULL;
2765 }
2766 #endif /* not FIONREAD */
2767 #endif
2768 /* guess */
2769 nbytes = (si->sound->sample_rate * si->sound->input_sample_size) / 2;
2770 #endif /* not Solaris */
2771
2772 data = buffer_alloc(nbytes, BUFFER_FREE);
2773
2774 for (b = data, i = 0; b && i < UIO_MAXIOV; b = b->next, i++)
2775 {
2776 iov[i].iov_base = b->buf;
2777 iov[i].iov_len = BUFFER_SIZE;
2778 }
2779
2780 n = readv(si->fd, iov, i);
2781 if (n <= 0)
2782 {
2783 buffer_dealloc(data, 1);
2784 spool_remove(si->sound); /* XXX */
2785 return NULL;
2786 }
2787
2788 for (b = data, i = n; b; b = b->next, i -= BUFFER_SIZE)
2789 {
2790 if (i < 0)
2791 {
2792 i = 0;
2793 }
2794 b->nbytes = MIN(i, BUFFER_SIZE);
2795 }
2796
2797 return data;
2798 }
2799 #endif
2800
2801 #ifdef __STDC__
2802 static int
2803 bytes_in_sound(SOUND *s)
2804 #else
2805 static int
2806 bytes_in_sound(s)
2807 SOUND *s;
2808 #endif
2809 {
2810 int bytes = 0;
2811
2812 if (s->chunk_size)
2813 {
2814 bytes = s->chunk_size;
2815 }
2816 else if (s->tail)
2817 {
2818 bytes = s->tail - s->offset;
2819 }
2820 else if (s->size)
2821 {
2822 bytes = s->size - s->offset;
2823 }
2824
2825 return bytes;
2826 }
2827
2828 #ifdef __STDC__
2829 int
2830 number_of_samples(SOUND *s, int bytes)
2831 #else
2832 int
2833 number_of_samples(s, bytes)
2834 SOUND *s;
2835 int bytes;
2836 #endif
2837 {
2838 if (!bytes)
2839 {
2840 bytes = bytes_in_sound(s);
2841 }
2842 return (bytes << 3) / (s->input_precision * s->channels);
2843 }