File: maud.c

package info (click to toggle)
sox 12.17.3-4woody2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,048 kB
  • ctags: 2,183
  • sloc: ansic: 24,796; sh: 3,005; makefile: 228; perl: 133
file content (388 lines) | stat: -rw-r--r-- 9,298 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * 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_i.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#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!!!! */
	uint32_t nsamples;
};

static void maudwriteheader(ft_t);

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

	/* Needed for rawread() */
	rc = st_rawstartread(ft);
	if (rc)
	    return rc;

	/* maud is in big endian format.  Swap whats read in
	 * on little endian machines.
	 */
	if (ST_IS_LITTLEENDIAN)
	{
		ft->swap = ft->swap ? 0 : 1;
	}
	
	/* read FORM chunk */
	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "FORM", 4) != 0)
	{
		st_fail_errno(ft,ST_EHDR,"MAUD: header does not begin with magic word 'FORM'");
		return (ST_EOF);
	}
	
	st_readdw(ft, &trash32); /* totalsize */
	
	if (st_reads(ft, buf, 4) == ST_EOF || strncmp(buf, "MAUD", 4) != 0)
	{
		st_fail_errno(ft,ST_EHDR,"MAUD: 'FORM' chunk does not specify 'MAUD' as type");
		return(ST_EOF);
	}
	
	/* read chunks until 'BODY' (or end) */
	
	while (st_reads(ft, buf, 4) == ST_SUCCESS && strncmp(buf,"MDAT",4) != 0) {
		
		/*
		buf[4] = 0;
		st_report("chunk %s",buf);
		*/
		
		if (strncmp(buf,"MHDR",4) == 0) {
			
			st_readdw(ft, &chunksize);
			if (chunksize != 8*4) 
			{
			    st_fail_errno(ft,ST_EHDR,"MAUD: MHDR chunk has bad size");
			    return(ST_EOF);
			}
			
			/* fseek(ft->fp,12,SEEK_CUR); */

			/* number of samples stored in MDAT */
			st_readdw(ft, &(p->nsamples));

			/* number of bits per sample as stored in MDAT */
			st_readw(ft, &bitpersam);

			/* number of bits per sample after decompression */
			st_readw(ft, (unsigned short *)&trash32);

			st_readdw(ft, &nom);         /* clock source frequency */
			st_readw(ft, &denom);       /* clock devide           */
			if (denom == 0) 
			{
			    st_fail_errno(ft,ST_EHDR,"MAUD: frequency denominator == 0, failed");
			    return (ST_EOF);
			}
			
			ft->info.rate = nom / denom;
			
			st_readw(ft, &chaninf); /* channel information */
			switch (chaninf) {
			case 0:
				ft->info.channels = 1;
				break;
			case 1:
				ft->info.channels = 2;
				break;
			default:
				st_fail_errno(ft,ST_EFMT,"MAUD: unsupported number of channels in file");
				return (ST_EOF);
			}
			
			st_readw(ft, &chaninf); /* number of channels (mono: 1, stereo: 2, ...) */
			if (chaninf != ft->info.channels) 
			{
				st_fail_errno(ft,ST_EFMT,"MAUD: unsupported number of channels in file");
			    return(ST_EOF);
			}
			
			st_readw(ft, &chaninf); /* compression type */
			
			st_readdw(ft, &trash32); /* rest of chunk, unused yet */
			st_readdw(ft, &trash32);
			st_readdw(ft, &trash32);
			
			if (bitpersam == 8 && chaninf == 0) {
				ft->info.size = ST_SIZE_BYTE;
				ft->info.encoding = ST_ENCODING_UNSIGNED;
			}
			else if (bitpersam == 8 && chaninf == 2) {
				ft->info.size = ST_SIZE_BYTE;
				ft->info.encoding = ST_ENCODING_ALAW;
			}
			else if (bitpersam == 8 && chaninf == 3) {
				ft->info.size = ST_SIZE_BYTE;
				ft->info.encoding = ST_ENCODING_ULAW;
			}
			else if (bitpersam == 16 && chaninf == 0) {
				ft->info.size = ST_SIZE_WORD;
				ft->info.encoding = ST_ENCODING_SIGN2;
			}
			else 
			{
				st_fail_errno(ft,ST_EFMT,"MAUD: unsupported compression type detected");
				return(ST_EOF);
			}
			
			ft->comment = 0;
			
			continue;
		}
		
		if (strncmp(buf,"ANNO",4) == 0) {
			st_readdw(ft, &chunksize);
			if (chunksize & 1)
				chunksize++;
			chunk_buf = (char *) malloc(chunksize + 1);
			if (!chunk_buf)
			{
			    st_fail_errno(ft,ST_ENOMEM,"Couldn't alloc resources");
			    return(ST_EOF);
			}
			if (fread(chunk_buf,1,(int)chunksize,ft->fp) 
					!= chunksize)
			{
				st_fail_errno(ft,ST_EOF,"MAUD: Unexpected EOF in ANNO header");
				return(ST_EOF);
			}
			chunk_buf[chunksize] = '\0';
			st_report("%s",chunk_buf);
			free(chunk_buf);
			
			continue;
		}
		
		/* some other kind of chunk */
		st_readdw(ft, &chunksize);
		if (chunksize & 1)
			chunksize++;
		fseek(ft->fp,chunksize,SEEK_CUR);
		continue;
		
	}
	
	if (strncmp(buf,"MDAT",4) != 0) 
	{
	    st_fail_errno(ft,ST_EFMT,"MAUD: MDAT chunk not found");
	    return(ST_EOF);
	}
	st_readdw(ft, &(p->nsamples));
	return(ST_SUCCESS);
}

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

st_ssize_t st_maudread(ft_t ft, st_sample_t *buf, st_ssize_t len) 
{
	return (st_rawread(ft, buf, len));
}

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

int st_maudstartwrite(ft_t ft) 
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	int rc;

	/* Needed for rawwrite() */
	rc = st_rawstartwrite(ft);
	if (rc)
	    return rc;

	/* maud is in big endian format.  Swap whats read in
	 * on little endian machines.
	 */
	if (ST_IS_LITTLEENDIAN)
	{
		ft->swap = ft->swap ? 0 : 1;
	}
	
	/* If you have to seek around the output file */
	if (! ft->seekable) 
	{
	    st_fail_errno(ft,ST_EOF,"Output .maud file must be a file, not a pipe");
	    return (ST_EOF);
	}
	
	if (ft->info.channels != 1 && ft->info.channels != 2) {
		st_fail_errno(ft,ST_EFMT,"MAUD: unsupported number of channels, unable to store");
		return(ST_EOF);
	}
	if (ft->info.size == ST_SIZE_WORD) ft->info.encoding = ST_ENCODING_SIGN2;
	if (ft->info.encoding == ST_ENCODING_ULAW || 
	    ft->info.encoding == ST_ENCODING_ALAW) ft->info.size = ST_SIZE_BYTE;
	if (ft->info.size == ST_SIZE_BYTE && 
	    ft->info.encoding == ST_ENCODING_SIGN2) 
	    ft->info.encoding = ST_ENCODING_UNSIGNED;
	
	p->nsamples = 0x7f000000L;
	maudwriteheader(ft);
	p->nsamples = 0;
	return (ST_SUCCESS);
}

st_ssize_t st_maudwrite(ft_t ft, st_sample_t *buf, st_ssize_t len) 
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	p->nsamples += len;
	
	return st_rawwrite(ft, buf, len);
}

int st_maudstopwrite(ft_t ft) 
{
        int rc;

	/* Flush out remaining samples*/
	rc = st_rawstopwrite(ft);
	if (rc)
	    return rc;

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

#define MAUDHEADERSIZE (4+(4+4+32)+(4+4+32)+(4+4))
static void maudwriteheader(ft_t ft)
{
	struct maudstuff * p = (struct maudstuff *) ft->priv;
	
	st_writes(ft, "FORM");
	st_writedw(ft, (p->nsamples*ft->info.size) + MAUDHEADERSIZE);  /* size of file */
	st_writes(ft, "MAUD"); /* File type */
	
	st_writes(ft, "MHDR");
	st_writedw(ft,  8*4); /* number of bytes to follow */
	st_writedw(ft, p->nsamples);  /* number of samples stored in MDAT */
	
	switch (ft->info.encoding) {
		
	case ST_ENCODING_UNSIGNED:
		st_writew(ft, (int) 8); /* number of bits per sample as stored in MDAT */
		st_writew(ft, (int) 8); /* number of bits per sample after decompression */
		break;
		
	case ST_ENCODING_SIGN2:
		st_writew(ft, (int) 16); /* number of bits per sample as stored in MDAT */
		st_writew(ft, (int) 16); /* number of bits per sample after decompression */
		break;
		
	case ST_ENCODING_ALAW:
	case ST_ENCODING_ULAW:
		st_writew(ft, (int) 8); /* number of bits per sample as stored in MDAT */
		st_writew(ft, (int) 16); /* number of bits per sample after decompression */
		break;
		
	}
	
	st_writedw(ft, ft->info.rate); /* clock source frequency */
	st_writew(ft, (int) 1); /* clock devide */
	
	if (ft->info.channels == 1) {
		st_writew(ft, (int) 0); /* channel information */
		st_writew(ft, (int) 1); /* number of channels (mono: 1, stereo: 2, ...) */
	}
	else {
		st_writew(ft, (int) 1);
		st_writew(ft, (int) 2);
	}
	
	switch (ft->info.encoding) {
		
	case ST_ENCODING_UNSIGNED:
	case ST_ENCODING_SIGN2:
		st_writew(ft, (int) 0); /* no compression */
		break;
		
	case ST_ENCODING_ULAW:
		st_writew(ft, (int) 3);
		break;
		
	case ST_ENCODING_ALAW:
		st_writew(ft, (int) 2);
		break;
		
	}
	
	st_writedw(ft, 0); /* reserved */
	st_writedw(ft, 0); /* reserved */
	st_writedw(ft, 0); /* reserved */
	
	st_writes(ft, "ANNO");
	st_writedw(ft, 30); /* length of block */
	st_writes(ft, "file create by Sound eXchange ");
	
	st_writes(ft, "MDAT");
	st_writedw(ft, p->nsamples * ft->info.size ); /* samples in file */
}