Debian Codegraph @

rplayd/sound.c

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