File: NIST.cpp

package info (click to toggle)
audiofile 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,192 kB
  • sloc: cpp: 31,691; sh: 11,006; ansic: 3,773; makefile: 271
file content (473 lines) | stat: -rw-r--r-- 11,543 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
	Audio File Library
	Copyright (C) 2004, Michael Pruett <michael@68k.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License as published by the Free Software Foundation; either
	version 2 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public
	License along with this library; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA  02111-1307  USA.
*/

/*
	NIST.cpp

	This file contains code for reading NIST SPHERE files.
*/

#include "config.h"
#include "NIST.h"

#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Setup.h"
#include "Track.h"
#include "File.h"
#include "afinternal.h"
#include "audiofile.h"
#include "byteorder.h"
#include "util.h"

#define NIST_SPHERE_HEADER_LENGTH 1024
#define NIST_SPHERE_MAX_FIELD_LENGTH 80

static _AFfilesetup nist_default_filesetup =
{
	_AF_VALID_FILESETUP,	/* valid */
	AF_FILE_NIST_SPHERE,	/* fileFormat */
	true,			/* trackSet */
	true,			/* instrumentSet */
	true,			/* miscellaneousSet */
	1,			/* trackCount */
	NULL,			/* tracks */
	0,			/* instrumentCount */
	NULL,			/* instruments */
	0,			/* miscellaneousCount */
	NULL			/* miscellaneous */
};

bool NISTFile::recognize(File *fh)
{
	uint8_t buffer[16];

	fh->seek(0, File::SeekFromBeginning);

	if (fh->read(buffer, 16) != 16)
		return false;

	/* Check to see if the file's magic number matches. */
	if (memcmp(buffer, "NIST_1A\n   1024\n", 16) == 0)
		return true;

	return false;
}

AFfilesetup NISTFile::completeSetup(AFfilesetup setup)
{
	TrackSetup	*track;

	if (setup->trackSet && setup->trackCount != 1)
	{
		_af_error(AF_BAD_NUMTRACKS, "NIST SPHERE file must have 1 track");
		return AF_NULL_FILESETUP;
	}

	track = &setup->tracks[0];

	if (track->sampleFormatSet)
	{
		/* XXXmpruett: Currently we allow only 1-16 bit sample width. */
		if (track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP &&
			(track->f.sampleWidth < 1 || track->f.sampleWidth > 16))
		{
			_af_error(AF_BAD_WIDTH,
				"invalid sample width %d bits for NIST SPHERE format");
			return AF_NULL_FILESETUP;
		}
		else if (track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
		{
			_af_error(AF_BAD_SAMPFMT,
				"NIST SPHERE format does not support unsigned data");
			return AF_NULL_FILESETUP;
		}
		else if (track->f.sampleFormat == AF_SAMPFMT_FLOAT ||
			track->f.sampleFormat == AF_SAMPFMT_DOUBLE)
		{
			_af_error(AF_BAD_SAMPFMT,
				"NIST SPHERE format does not support floating-point data");
			return AF_NULL_FILESETUP;
		}
	}

	if (track->rateSet && track->f.sampleRate <= 0.0)
	{
		_af_error(AF_BAD_RATE,
			"invalid sample rate %.30g for NIST SPHERE file",
			track->f.sampleRate);
		return AF_NULL_FILESETUP;
	}

	if (track->channelCountSet && track->f.channelCount < 1)
	{
		_af_error(AF_BAD_CHANNELS,
			"invalid channel count (%d) for NIST SPHERE format",
			track->f.channelCount);
		return AF_NULL_FILESETUP;
	}

	if (track->compressionSet && track->f.compressionType != AF_COMPRESSION_NONE &&
		track->f.compressionType != AF_COMPRESSION_G711_ULAW &&
		track->f.compressionType != AF_COMPRESSION_G711_ALAW)
	{
		_af_error(AF_BAD_NOT_IMPLEMENTED,
			"NIST SPHERE format supports only G.711 u-law or A-law compression");
		return AF_NULL_FILESETUP;
	}

	if (track->aesDataSet)
	{
		_af_error(AF_BAD_FILESETUP, "NIST SPHERE file cannot have AES data");
		return AF_NULL_FILESETUP;
	}

	if (track->markersSet && track->markerCount != 0)
	{
		_af_error(AF_BAD_NUMMARKS, "NIST SPHERE format does not support markers");
		return AF_NULL_FILESETUP;
	}

	if (setup->instrumentSet && setup->instrumentCount != 0)
	{
		_af_error(AF_BAD_NUMINSTS, "NIST SPHERE format does not support instruments");
		return AF_NULL_FILESETUP;
	}

	/* XXXmpruett: We don't support miscellaneous chunks for now. */
	if (setup->miscellaneousSet && setup->miscellaneousCount != 0)
	{
		_af_error(AF_BAD_NOT_IMPLEMENTED, "NIST SPHERE format does not currently support miscellaneous chunks");
		return AF_NULL_FILESETUP;
	}

	return _af_filesetup_copy(setup, &nist_default_filesetup, true);
}

static bool nist_header_read_int (const char *header, const char *key, int *val)
{
	const char *cp;
	char keystring[256], scanstring[256];

	snprintf(keystring, 256, "\n%s -i", key);

	if ((cp = strstr(header, keystring)) != NULL)
	{
		snprintf(scanstring, 256, "\n%s -i %%d", key);
		sscanf(cp, scanstring, val);
		return true;
	}

	return false;
}

static bool nist_header_read_string (const char *header, const char *key, int *length, char *val)
{
	const char *cp;
	char keystring[256], scanstring[256];

	snprintf(keystring, 256, "\n%s -s", key);

	if ((cp = strstr(header, keystring)) != NULL)
	{
		snprintf(scanstring, 256, "\n%s -s%%d %%79s", key);
		sscanf(cp, scanstring, length, val);
		return true;
	}

	return false;
}

status NISTFile::readInit(AFfilesetup setup)
{
	char header[NIST_SPHERE_HEADER_LENGTH + 1];
	int intval;
	char strval[NIST_SPHERE_MAX_FIELD_LENGTH];
	int sample_n_bytes;

	fh->seek(0, File::SeekFromBeginning);

	if (fh->read(header, NIST_SPHERE_HEADER_LENGTH) != NIST_SPHERE_HEADER_LENGTH)
	{
		_af_error(AF_BAD_READ, "Could not read NIST SPHERE file header");
		return AF_FAIL;
	}

	header[NIST_SPHERE_HEADER_LENGTH] = '\0';

	if (memcmp(header, "NIST_1A\n   1024\n", 16) != 0)
	{
		_af_error(AF_BAD_FILEFMT, "Bad NIST SPHERE file header");
		return AF_FAIL;
	}

	Track *track = allocateTrack();
	if (!track)
		return AF_FAIL;

	/* Read number of bytes per sample. */
	if (!nist_header_read_int(header, "sample_n_bytes", &sample_n_bytes))
	{
		_af_error(AF_BAD_HEADER, "bytes per sample not specified");
		return AF_FAIL;
	}

	/*
		Since some older NIST SPHERE files lack a sample_coding
		field, if sample_n_bytes is 1, assume mu-law;
		otherwise assume linear PCM.
	*/
	track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
	if (sample_n_bytes == 1)
	{
		track->f.compressionType = AF_COMPRESSION_G711_ULAW;
		track->f.sampleWidth = 16;
	}
	else
	{
		track->f.compressionType = AF_COMPRESSION_NONE;
		track->f.sampleWidth = sample_n_bytes * 8;
	}

	if (nist_header_read_string(header, "sample_coding", &intval, strval))
	{
		if (strcmp(strval, "pcm") == 0)
			;
		else if (strcmp(strval, "ulaw") == 0 || strcmp(strval, "mu-law") == 0)
		{
			track->f.compressionType = AF_COMPRESSION_G711_ULAW;
			track->f.sampleWidth = 16;
		}
		else if (strcmp(strval, "alaw") == 0)
		{
			track->f.compressionType = AF_COMPRESSION_G711_ALAW;
			track->f.sampleWidth = 16;
		}
		else
		{
			_af_error(AF_BAD_SAMPFMT,
				"unrecognized NIST SPHERE sample format %s", strval);
			return AF_FAIL;
		}
	}

	/* Read channel count. */
	if (nist_header_read_int(header, "channel_count", &intval))
	{
		if (intval < 1)
		{
			_af_error(AF_BAD_CHANNELS, "invalid number of channels %d",
				intval);
			return AF_FAIL;
		}
		track->f.channelCount = intval;
	}
	else
	{
		_af_error(AF_BAD_HEADER, "number of channels not specified");
		return AF_FAIL;
	}

	/* Read string representing byte order. */
	if (nist_header_read_string(header, "sample_byte_format", &intval, strval))
	{
		if (intval > 1)
		{
			if (strncmp(strval, "01", 2) == 0)
				track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
			else
				track->f.byteOrder = AF_BYTEORDER_BIGENDIAN;
		}
		else
			track->f.byteOrder = _AF_BYTEORDER_NATIVE;
	}
	else
	{
		/*
			Fail if this field is not present and sample
			width is more than one byte.
		*/
		if (track->f.compressionType == AF_COMPRESSION_NONE &&
			track->f.sampleWidth > 8)
		{
			_af_error(AF_BAD_HEADER, "sample byte order not specified");
			return AF_FAIL;
		}
	}

	/* Read significant bits per sample. */
	if (nist_header_read_int(header, "sample_sig_bits", &intval))
	{
		if (intval < 1 || intval > 32)
		{
			_af_error(AF_BAD_WIDTH, "invalid sample width %d bits\n",
				intval);
			return AF_FAIL;
		}

		/*
			Use specified significant bits value as the
			sample width for uncompressed data as long
			as the number of bytes per sample remains
			unchanged.
		*/
		if (track->f.compressionType == AF_COMPRESSION_NONE &&
			(intval + 7) / 8 == sample_n_bytes)
		{
			track->f.sampleWidth = intval;
		}
	}

	/* Read sample rate. */
	if (nist_header_read_int(header, "sample_rate", &intval))
	{
		if (intval <= 0)
		{
			_af_error(AF_BAD_RATE, "invalid sample rate %d Hz\n", intval);
			return AF_FAIL;
		}
		track->f.sampleRate = intval;
	}
	else
	{
		_af_error(AF_BAD_HEADER, "sample rate not specified");
		return AF_FAIL;
	}

	/* Read sample count. */
	if (nist_header_read_int(header, "sample_count", &intval))
	{
		track->totalfframes = intval;
	}
	else
	{
		_af_error(AF_BAD_HEADER, "number of samples not specified");
		return AF_FAIL;
	}

	if (_af_set_sample_format(&track->f, track->f.sampleFormat,
		track->f.sampleWidth) == AF_FAIL)
	{
		return AF_FAIL;
	}

	track->fpos_first_frame = NIST_SPHERE_HEADER_LENGTH;
	track->data_size = fh->length() - NIST_SPHERE_HEADER_LENGTH;
	track->nextfframe = 0;
	track->fpos_next_frame = track->fpos_first_frame;

	return AF_SUCCEED;
}

static const char *sample_byte_format (AudioFormat *fmt)
{
	int nbytes = _af_format_sample_size(fmt, false);

	assert(nbytes == 1 || nbytes == 2);

	if (nbytes == 1)
		return "0";
	else if (nbytes == 2)
	{
		if (fmt->byteOrder == AF_BYTEORDER_BIGENDIAN)
			return "10";
		else
			return "01";
	}

	/* NOTREACHED */
	return NULL;
}

static const char *sample_coding (AudioFormat *fmt)
{
	if (fmt->compressionType == AF_COMPRESSION_NONE)
		return "pcm";
	else if (fmt->compressionType == AF_COMPRESSION_G711_ULAW)
		return "ulaw";
	else if (fmt->compressionType == AF_COMPRESSION_G711_ALAW)
		return "alaw";

	/* NOTREACHED */
	return NULL;
}

status NISTFile::writeHeader()
{
	Track *track = getTrack();

	char header[NIST_SPHERE_HEADER_LENGTH];
	int printed = snprintf(header, NIST_SPHERE_HEADER_LENGTH,
		"NIST_1A\n   1024\n"
		"channel_count -i %d\n"
		"sample_count -i %d\n"
		"sample_rate -i %d\n"
		"sample_n_bytes -i %d\n"
		"sample_byte_format -s%d %s\n"
		"sample_sig_bits -i %d\n"
		"sample_coding -s%d %s\n"
		"end_head\n",
		track->f.channelCount,
		(int) track->totalfframes,
		(int) track->f.sampleRate,
		(int) _af_format_sample_size(&track->f, false),
		(int) _af_format_sample_size(&track->f, false), sample_byte_format(&track->f),
		track->f.sampleWidth,
		(int) strlen(sample_coding(&track->f)), sample_coding(&track->f));

	/* Fill the remaining space in the buffer with space characters. */
	if (printed < NIST_SPHERE_HEADER_LENGTH)
		memset(header + printed, ' ', NIST_SPHERE_HEADER_LENGTH - printed);

	return fh->write(header, NIST_SPHERE_HEADER_LENGTH) == NIST_SPHERE_HEADER_LENGTH ? AF_SUCCEED : AF_FAIL;
}

status NISTFile::writeInit(AFfilesetup setup)
{
	Track		*track;

	assert(fileFormat == AF_FILE_NIST_SPHERE);

	if (_af_filesetup_make_handle(setup, this) == AF_FAIL)
		return AF_FAIL;

	track = &tracks[0];
	track->totalfframes = 0;
	track->fpos_first_frame = NIST_SPHERE_HEADER_LENGTH;
	track->nextfframe = 0;
	track->fpos_next_frame = track->fpos_first_frame;

	fh->seek(0, File::SeekFromBeginning);
	writeHeader();

	return AF_SUCCEED;
}

status NISTFile::update()
{
	fh->seek(0, File::SeekFromBeginning);
	writeHeader();

	return AF_SUCCEED;
}