1 /*
2  * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3  * Universitaet Berlin.  See the accompanying file "COPYRIGHT" for
4  * details.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5  */
6 
7 /* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/toast_audio.c,v 1.6 1995/03/07 21:21:24 jutta Exp $ */
8 
9 #include	"toast.h"
10 
11 /*  toast_audio -- functions to manipulate SunOS audio files.
12  *
13  *  This is reverse engineered from our present soundfiles
14  *  and in no way portable, durable or aesthetically pleasing.
15  */
16 
17 extern FILE	* in, 	  * out;
18 extern char	* inname;
19 extern char	* progname;
20 
21 extern int	(*output) P((gsm_signal *)),
22 		(*input ) P((gsm_signal *));
23 
24 extern int	alaw_input   P((gsm_signal *)),
25 		ulaw_input   P((gsm_signal *)),
26 		linear_input P((gsm_signal *));
27 
28 extern int	ulaw_output P((gsm_signal *));
29 
30 static int put_u32 P2((f, u), FILE * f, unsigned long u)
31 {
32 	/*  Write a 32-bit unsigned value msb first.
33 	 */
34 	if (  putc( (char)((u>>24) & 0x0FF), f) == EOF
35 	   || putc( (char)((u>>16) & 0x0FF), f) == EOF
36 	   || putc( (char)((u>> 8) & 0x0FF), f) == EOF
37 	   || putc( (char)( u      & 0x0FF), f) == EOF) return -1;
38 
39 	return 0;
40 }
41 
42 static int get_u32 P2((f, up), FILE * f, unsigned long * up)
43 {
44 	/*  Read a 32-bit unsigned value msb first.
45 	 */
46 	int		i;
47 	unsigned long 	u;
48 
49 	if (  				      (i = getc(f)) == EOF
50 	   || ((u =        (unsigned char)i), (i = getc(f)) == EOF)
51 	   || ((u = (u<<8)|(unsigned char)i), (i = getc(f)) == EOF)
52 	   || ((u = (u<<8)|(unsigned char)i), (i = getc(f)) == EOF)) return -1;
53 	*up = 	    (u<<8)|(unsigned char)i;
54 	return 0;
55 }
56 
P0()57 int audio_init_input P0()
58 {
59 	unsigned long	len, enc;	/* unsigned 32 bits	*/
60 
61 	if (  fgetc(in) != '.'
62 	   || fgetc(in) != 's'
63 	   || fgetc(in) != 'n'
64 	   || fgetc(in) != 'd'
65 	   || get_u32( in, &len )
66 	   || get_u32( in, &enc )	/* skip this */
67 	   || get_u32( in, &enc )) {
68 		fprintf(stderr,
69 	"%s: bad (missing?) header in Sun audio file \"%s\";\n\
70 	Try one of -u, -a, -l instead (%s -h for help).\n",
71 			progname, inname ? inname : "stdin", progname);
72 		return -1;
73 	}
74 
75 	switch (enc) {
76 	case 1:	input = ulaw_input;  	break;
77 	case 2: input = alaw_input;  	break;
78 	case 3: input = linear_input; 	break;
79 	default:
80 		fprintf(stderr,
81 "%s: warning: file format #%lu for %s not implemented, defaulting to u-law.\n",
82 			progname, enc, inname);
83 		input = ulaw_input;
84 		break;
85 	}
86 
87 	while (len > 4*4)
88 		if (getc(in) == EOF) {
89 			fprintf(stderr,
90 			"%s: EOF in header of Sun audio file \"%s\";\n\
91 			Try one of -u, -a, -l instead (%s -h for help).\n",
92 				progname, inname ? inname : "stdin", progname);
93 			return -1;
94 		}
95 		else len--;
96 
97 	return 0;
98 }
99 
P0()100 int audio_init_output P0()
101 {
102 	if (  fputs(".snd", out) == EOF
103 	   || put_u32(out, 32)
104 	   || put_u32(out, ~(unsigned long)0)
105 	   || put_u32(out, 1)
106 	   || put_u32(out, 8000)
107 	   || put_u32(out, 1)
108 	   || put_u32(out, 0)
109 	   || put_u32(out, 0)) return -1;
110 
111 	return 0;
112 }
113 
114