File: maud.c

package info (click to toggle)
sox 12.16-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,180 kB
  • ctags: 1,466
  • sloc: ansic: 16,658; sh: 2,071; makefile: 126
file content (341 lines) | stat: -rw-r--r-- 8,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * July 5, 1991
 * Copyright 1991 Lance Norskog And Sundry Contributors
 * This source code is freely redistributable and may be used for
 * any purpose.  This copyright notice must be maintained. 
 * Lance Norskog And Sundry Contributors are not responsible for 
 * the consequences of using this software.
 */

/*
 * Sound Tools MAUD file format driver, by Lutz Vieweg 1993
 *
 * supports: mono and stereo, linear, a-law and u-law reading and writing
 *
 * March 3, 1999 - cbagwell
 *   Changed to use rawread for reading.
 *
 */

#include "st.h"
#include "libst.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>	/* For SEEK_* defines if not found in stdio */
#endif

/* Private data for MAUD file */
struct maudstuff { /* max. 100 bytes!!!! */
	ULONG nsamples;
};

void maudwriteheader(P1(ft_t));
LONG rawread(P3(ft_t, LONG *, LONG));
void rawwrite(P3(ft_t, LONG *, LONG));

/*
 * Do anything required before you start reading samples.
 * Read file header. 
 *	Find out sampling rate, 
 *	size and style of samples, 
 *	mono/stereo/quad.
 */
void maudstartread(ft) 
ft_t ft;
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	char buf[12];
	char *chunk_buf;
	
	unsigned short bitpersam;
	ULONG nom;
	unsigned short denom;
	unsigned short chaninf;
	
	ULONG chunksize;

	int littlendian = 1;
	char *endptr;

	/* Needed for rawread() */
	rawstartread(ft);

	endptr = (char *) &littlendian;
	/* maud is in big endian format.  Swap whats read in
	 * on little endian machines.
	 */
	if (*endptr)
	{
		ft->swap = ft->swap ? 0 : 1;
	}
	
	/* read FORM chunk */
	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
		fail("MAUD: header does not begin with magic word 'FORM'");
	
	rlong(ft); /* totalsize */
	
	if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "MAUD", 4) != 0)
		fail("MAUD: 'FORM' chunk does not specify 'MAUD' as type");
	
	/* read chunks until 'BODY' (or end) */
	
	while (fread(buf,1,4,ft->fp) == 4 && strncmp(buf,"MDAT",4) != 0) {
		
		/*
		buf[4] = 0;
		report("chunk %s",buf);
		*/
		
		if (strncmp(buf,"MHDR",4) == 0) {
			
			chunksize = rlong(ft);
			if (chunksize != 8*4) fail ("MAUD: MHDR chunk has bad size");
			
			/* fseek(ft->fp,12,SEEK_CUR); */
			
			p->nsamples = rlong(ft); /* number of samples stored in MDAT */
			bitpersam = rshort(ft);  /* number of bits per sample as stored in MDAT */
			rshort(ft);              /* number of bits per sample after decompression */
			nom = rlong(ft);         /* clock source frequency */
			denom = rshort(ft);       /* clock devide           */
			if (denom == 0) fail("MAUD: frequency denominator == 0, failed");
			
			ft->info.rate = nom / denom;
			
			chaninf = rshort(ft); /* channel information */
			switch (chaninf) {
			case 0:
				ft->info.channels = 1;
				break;
			case 1:
				ft->info.channels = 2;
				break;
			default:
				fail("MAUD: unsupported number of channels in file");
				break;
			}
			
			chaninf = rshort(ft); /* number of channels (mono: 1, stereo: 2, ...) */
			if (chaninf != ft->info.channels) fail("MAUD: unsupported number of channels in file");
			
			chaninf = rshort(ft); /* compression type */
			
			rlong(ft); /* rest of chunk, unused yet */
			rlong(ft);
			rlong(ft);
			
			if (bitpersam == 8 && chaninf == 0) {
				ft->info.size = BYTE;
				ft->info.style = UNSIGNED;
			}
			else if (bitpersam == 8 && chaninf == 2) {
				ft->info.size = BYTE;
				ft->info.style = ALAW;
			}
			else if (bitpersam == 8 && chaninf == 3) {
				ft->info.size = BYTE;
				ft->info.style = ULAW;
			}
			else if (bitpersam == 16 && chaninf == 0) {
				ft->info.size = WORD;
				ft->info.style = SIGN2;
			}
			else fail("MAUD: unsupported compression type detected");
			
			ft->comment = 0;
			
			continue;
		}
		
		if (strncmp(buf,"ANNO",4) == 0) {
			chunksize = rlong(ft);
			if (chunksize & 1)
				chunksize++;
			chunk_buf = (char *) malloc(chunksize + 1);
			if (fread(chunk_buf,1,(int)chunksize,ft->fp) 
					!= chunksize)
				fail("MAUD: Unexpected EOF in ANNO header");
			chunk_buf[chunksize] = '\0';
			report ("%s",chunk_buf);
			free(chunk_buf);
			
			continue;
		}
		
		/* some other kind of chunk */
		chunksize = rlong(ft);
		if (chunksize & 1)
			chunksize++;
		fseek(ft->fp,chunksize,SEEK_CUR);
		continue;
		
	}
	
	if (strncmp(buf,"MDAT",4) != 0) fail("MAUD: MDAT chunk not found");
	p->nsamples = rlong(ft);
}

/*
 * Read up to len samples from file.
 * Convert to signed longs.
 * Place in buf[].
 * Return number of samples read.
 */

LONG maudread(ft, buf, len) 
ft_t ft;
LONG *buf, len;
{
	return (rawread(ft, buf, len));
}

/*
 * Do anything required when you stop reading samples.  
 * Don't close input file! 
 */
void maudstopread(ft) 
ft_t ft;
{
	/* Needed because of rawread() */
	rawstopread(ft);
}

void maudstartwrite(ft) 
ft_t ft;
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;

	int littlendian = 1;
	char *endptr;

	/* Needed for rawwrite() */
	rawstartwrite(ft);

	endptr = (char *) &littlendian;
	/* maud is in big endian format.  Swap whats read in
	 * on little endian machines.
	 */
	if (*endptr)
	{
		ft->swap = ft->swap ? 0 : 1;
	}
	
	/* If you have to seek around the output file */
	if (! ft->seekable) fail("Output .maud file must be a file, not a pipe");
	
	if (ft->info.channels != 1 && ft->info.channels != 2) {
		fail("MAUD: unsupported number of channels, unable to store");
	}
	if (ft->info.size == WORD) ft->info.style = SIGN2;
	if (ft->info.style == ULAW || ft->info.style == ALAW) ft->info.size = BYTE;
	if (ft->info.size == BYTE && ft->info.style == SIGN2) ft->info.style = UNSIGNED;
	
	p->nsamples = 0x7f000000L;
	maudwriteheader(ft);
	p->nsamples = 0;
}

void maudwrite(ft, buf, len) 
ft_t ft;
LONG *buf, len;
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	p->nsamples += len;
	
	rawwrite(ft, buf, len);
}

void maudstopwrite(ft) 
ft_t ft;
{
	/* Flush out remaining samples*/
	rawstopwrite(ft);

	/* All samples are already written out. */
	
	if (fseek(ft->fp, 0L, 0) != 0) fail("can't rewind output file to rewrite MAUD header");
	
	maudwriteheader(ft);
}

#define MAUDHEADERSIZE (4+(4+4+32)+(4+4+32)+(4+4))
void maudwriteheader(ft)
ft_t ft;
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	fputs ("FORM", ft->fp);
	wlong(ft, (p->nsamples*ft->info.size) + MAUDHEADERSIZE);  /* size of file */
	fputs("MAUD", ft->fp); /* File type */
	
	fputs ("MHDR", ft->fp);
	wlong(ft, (LONG) 8*4); /* number of bytes to follow */
	wlong(ft, (LONG) (p->nsamples ));  /* number of samples stored in MDAT */
	
	switch (ft->info.style) {
		
	case UNSIGNED:
		wshort(ft, (int) 8); /* number of bits per sample as stored in MDAT */
		wshort(ft, (int) 8); /* number of bits per sample after decompression */
		break;
		
	case SIGN2:
		wshort(ft, (int) 16); /* number of bits per sample as stored in MDAT */
		wshort(ft, (int) 16); /* number of bits per sample after decompression */
		break;
		
	case ALAW:
	case ULAW:
		wshort(ft, (int) 8); /* number of bits per sample as stored in MDAT */
		wshort(ft, (int) 16); /* number of bits per sample after decompression */
		break;
		
	}
	
	wlong(ft, (LONG) ft->info.rate); /* clock source frequency */
	wshort(ft, (int) 1); /* clock devide */
	
	if (ft->info.channels == 1) {
		wshort(ft, (int) 0); /* channel information */
		wshort(ft, (int) 1); /* number of channels (mono: 1, stereo: 2, ...) */
	}
	else {
		wshort(ft, (int) 1);
		wshort(ft, (int) 2);
	}
	
	switch (ft->info.style) {
		
	case UNSIGNED:
	case SIGN2:
		wshort(ft, (int) 0); /* no compression */
		break;
		
	case ULAW:
		wshort(ft, (int) 3);
		break;
		
	case ALAW:
		wshort(ft, (int) 2);
		break;
		
	}
	
	wlong(ft, (LONG) 0); /* reserved */
	wlong(ft, (LONG) 0); /* reserved */
	wlong(ft, (LONG) 0); /* reserved */
	
	fputs ("ANNO", ft->fp);
	wlong(ft, (LONG) 32); /* length of block */
	fputs ("file written by SOX MAUD-export ", ft->fp);
	
	fputs ("MDAT", ft->fp);
	wlong(ft, p->nsamples * ft->info.size ); /* samples in file */
}