Debian Codegraph @

rplayd/sound.c

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