1 /*------------ Telecommunications & Signal Processing Lab --------------
2                          McGill University
3 
4 Routine:
5   AFpar.h
6 
7 Description:
8   Declarations for the TSP audio file routines.
9 
10 Author / revision:
11   P. Kabal  Copyright (C) 2003
12   $Revision: 1.80 $  $Date: 2003/11/04 10:35:51 $
13 
14 ----------------------------------------------------------------------*/
15 
16 #ifndef AFpar_h_
17 #define AFpar_h_
18 
19 #include <float.h>		/* DBL_MAX */
20 #include <stdio.h>		/* typedef for FILE */
21 #include <libtsp/UTpar.h>	/* Byte swap codes DS_SWAP, etc. */
22 
23 #ifndef	AFILE_t_
24 #  define	AFILE_t_
25 typedef struct AF_filepar AFILE;	/* Audio file parameters */
26 #endif
27 
28 /* Loudspeaker locations */
29 /* The loudspeaker locations associated with the channels are stored in
30    a byte array.  The values in the byte array start at 1.  A null byte marks
31    the end of the list.
32 */
33 
34 #define AF_MAXN_SPKR	30	/* Maximum size for speaker position array */
35 #define AF_NC_SPKR	3	/* Maximum length of a speaker name */
36 
37 #define AF_SPKR_FL	1	/* Front left */
38 #define AF_SPKR_FR	2	/* Front right */
39 #define AF_SPKR_FC	3	/* Front centre */
40 #define AF_SPKR_LF	4	/* Low frequency */
41 #define AF_SPKR_BL	5	/* Back left */
42 #define AF_SPKR_BR	6	/* Back right */
43 #define AF_SPKR_FLC	7	/* Front left of centre */
44 #define AF_SPKR_FRC	8	/* Front right of centre */
45 #define AF_SPKR_BC	9	/* Back centre */
46 #define AF_SPKR_SL	10	/* Side left */
47 #define AF_SPKR_SR	11	/* Side left */
48 #define AF_SPKR_TC	12	/* Top centre */
49 #define AF_SPKR_TFL	13	/* Top front left */
50 #define AF_SPKR_TFC	14	/* Top front centre */
51 #define AF_SPKR_TFR	15	/* Top front right */
52 #define AF_SPKR_TBL	16	/* Top back left */
53 #define AF_SPKR_TBC	17	/* Top back centre */
54 #define AF_SPKR_TBR	18	/* Top back right */
55 #define AF_SPKR_X	19	/* No position */
56 
57 #define AF_N_SPKR_NAMES	19
58 #define AF_X_SPKR	(AF_N_SPKR_NAMES + 1)
59 
60 #ifdef AF_SPKR_NAMES
61 static const char *AF_Spkr_Names[] =
62 {
63   "FL",  "FR",  "FC",  "LF",
64   "BL",  "BR",  "FLC", "FRC",
65   "BC",  "SL",  "SR",  "TC",
66   "TFL", "TFC", "TFR", "TBL",
67   "TBC", "TBR", "-",   NULL	/* Terminating null string */
68 };
69 #endif
70 
71 /* Information records */
72 struct AF_infoX {
73   char *Info;		/* Pointer to string */
74   int N;		/* Number of characters (includes nulls) */
75   int Nmax;		/* Maximum number of characters (size of string) */
76 };
77 /* Structures for audio file information */
78 struct AF_dformat {
79   int Format;		/* Data format, FD_INT16, etc. */
80   int Swapb;		/* Swap code, DS_EB, DS_NATIVE, etc. */
81   int NbS;		/* Number of bits per sample */
82   double ScaleF;	/* Scaling factor applied to file data */
83   			/* (default value AF_SF_DEFAULT) */
84 };
85 struct AF_ndata {
86   long int Ldata;	/* Data space in bytes */
87   long int Nsamp;	/* Number of samples (all channels) */
88   long int Nchan;	/* Number of channels */
89   unsigned char SpkrConfig[AF_MAXN_SPKR+1];	/* Speaker configuration */
90 };
91 struct AF_read {
92   double Sfreq;
93   struct AF_dformat DFormat;
94   struct AF_ndata NData;
95   struct AF_infoX InfoX;
96 };
97 struct AF_write {
98   double Sfreq;
99   struct AF_dformat DFormat;
100   long int Nchan;
101   long int Nframe;	/* No. Frames: normally AF_NFRAME_UNDEF */
102   int Ftype;		/* FTW_WAVE, etc. */
103   unsigned char SpkrConfig[AF_MAXN_SPKR+1];	/* Speaker configuration */
104   struct AF_infoX InfoX;
105 };
106 
107 #define AF_READ_DEFAULT(x) \
108 	static const struct AF_read x = \
109 		{0.0, \
110 		 {FD_UNDEF, DS_NATIVE, 0, AF_SF_DEFAULT}, \
111 		 {AF_LDATA_UNDEF, AF_NSAMP_UNDEF, 1L, {AF_X_SPKR, 0}}, \
112 		 {NULL, 0, 0}}
113 
114 #define FM_AFSP		"AFsp"
115 #define AF_MAXINFO	10240
116 struct AF_info {
117   char *Info;		/* Pointer to string */
118   int N;		/* Number of characters (includes nulls) */
119 };
120 
121 /* Audio file parameter structure */
122 struct AF_filepar {
123   FILE *fp;		/* File pointer */
124   int Op;		/* Operation (read, write) */
125   int Error;		/* Error flag (0 for no error) */
126   long int Novld;	/* Number of points clipped */
127   double Sfreq;		/* Sampling rate */
128   int Ftype;		/* File type */
129   int Format;		/* Data format, FD_INT16, etc. */
130   int Swapb;		/* Swap code (DS_NATIVE or DS_SWAP) */
131   int NbS;		/* Number of bits per sample */
132   double ScaleF;	/* Scale factor applied to file data */
133   long int Nchan;	/* Number of channels */
134   unsigned char *SpkrConfig;	/* Speaker configuration mask */
135   long int Start;	/* Start byte */
136   long int Isamp;	/* Sample offset */
137   long int Nsamp;	/* Number of samples */
138   struct AF_info InfoS;	/* Information structure */
139 };
140 
141 /* Error codes for the Error field in the audio parameter structure */
142 #define AF_NOERR	0
143 #define AF_UEOF		-1	/* Unexpected end-of-file on read */
144 #define AF_IOERR	1	/* Read or write error */
145 #define AF_DECERR	2	/* Data decoding error on read */
146 
147 /* File operation types */
148 enum {
149   FO_NONE	= 0,	/* closed */
150   FO_RO 	= 1,	/* read */
151   FO_WO 	= 2	/* write */
152 };
153 
154 /* Data format types - must be sequential */
155 enum {
156   FD_UNDEF	= 0,	/* undefined file data format */
157   FD_MULAW8	= 1,	/* mu-law 8-bit data */
158   FD_ALAW8	= 2,	/* A-law 8-bit data */
159   FD_UINT8	= 3,	/* offset binary integer 8-bit data */
160   FD_INT8	= 4,	/* two's complement integer 8-bit data */
161   FD_INT16	= 5,	/* two's complement integer 16-bit data */
162   FD_INT24	= 6,	/* two's complement integer 24-bit data */
163   FD_INT32	= 7,	/* two's complement integer 32-bit data */
164   FD_FLOAT32	= 8,	/* 32-bit float data */
165   FD_FLOAT64	= 9,	/* 64-bit float data */
166   FD_TEXT	= 10	/* text data */
167 };
168 #define NFD		(FD_TEXT+1)
169 
170 /* Data format sizes (bytes) */
171 #define FDL_MULAW8	1
172 #define FDL_ALAW8	1
173 #define FDL_UINT8	1
174 #define FDL_INT8	1
175 #define FDL_INT16	2
176 #define FDL_INT24	3
177 #define FDL_INT32	4
178 #define	FDL_FLOAT32	4
179 #define FDL_FLOAT64	8
180 #define FDL_TEXT	0	/* Variable size */
181 
182 #ifdef AF_DATA_LENGTHS
183 static const int AF_DL[NFD] = {
184   0,
185   FDL_MULAW8,
186   FDL_ALAW8,
187   FDL_UINT8,
188   FDL_INT8,
189   FDL_INT16,
190   FDL_INT24,
191   FDL_INT32,
192   FDL_FLOAT32,
193   FDL_FLOAT64,
194   FDL_TEXT
195 };
196 #endif
197 
198 /* Default scale factors */
199 /* For the fixed-point formats, data is returned in the range
200    [-1, +1). An exception is 8-bit data which gives values
201    [-1/2,1/2).
202 */
203 #define AF_SF_UNDEF	(1.)
204 #define AF_SF_MULAW8	(1./32768.)
205 #define AF_SF_ALAW8	(1./32768.)
206 #define AF_SF_UINT8	(128./32768.)
207 #define AF_SF_INT8	(128./32768.)
208 #define AF_SF_INT16	(1./32768.)
209 #define AF_SF_INT24	(1./(256.*32768.))
210 #define AF_SF_INT32	(1./(65536.*32768.))
211 #define AF_SF_FLOAT32	(1.)
212 #define AF_SF_FLOAT64	(1.)
213 #define AF_SF_TEXT	(1.)
214 #define AF_SF_DEFAULT	(-(DBL_MAX))
215 
216 /* Scale factors */
217 /* These are the scale factors for reading, the scale factors for
218    writing are the inverses */
219 static const double AF_SF[NFD] = {
220   AF_SF_UNDEF,
221   AF_SF_MULAW8,
222   AF_SF_ALAW8,
223   AF_SF_UINT8,
224   AF_SF_INT8,
225   AF_SF_INT16,
226   AF_SF_INT24,
227   AF_SF_INT32,
228   AF_SF_FLOAT32,
229   AF_SF_FLOAT64,
230   AF_SF_TEXT
231 };
232 
233 #ifdef AF_DATA_TYPE_NAMES
234 static const char *AF_DTN[NFD] = {
235   "undefined",
236   "8-bit mu-law",
237   "8-bit A-law",
238   "offset-binary 8-bit integer",
239   "8-bit integer",
240   "16-bit integer",
241   "24-bit integer",
242   "32-bit integer",
243   "32-bit float",
244   "64-bit float",
245   "text data"
246 };
247 #endif
248 
249 #ifdef AF_DATA_TYPE_NAMES_X
250 static const char *AF_DTX[NFD] = {
251   NULL,
252   "%d-bit mu-law",
253   "%d-bit A-law",
254   "offset-binary %d-bit integer",
255   "%d-bit integer",
256   "%d-bit integer",
257   "%d-bit integer",
258   "%d-bit integer",
259   "%d-bit float",
260   "%d-bit float",
261   "text data"
262 };
263 static const char *AF_DTXX[NFD] = {
264   NULL,
265   "%d/%d-bit mu-law",
266   "%d/%d-bit A-law",
267   "offset-binary %d/%d-bit integer",
268   "%d/%d-bit integer",
269   "%d/%d-bit integer",
270   "%d/%d-bit integer",
271   "%d/%d-bit integer",
272   "%d/%d-bit float",
273   "%d/%d-bit float",
274   "text data"
275 };
276 #endif
277 
278 /* === Input audio files === */
279 /* Internal codes for input audio file types - must be sequential for the
280    standard file types
281 */
282 enum {
283   FT_UNKNOWN	= 0,	/* unknown audio file format */
284   FT_NH		= 1,	/* headerless (non-standard or no header) audio file */
285   FT_AU		= 2,	/* AU audio file */
286   FT_WAVE	= 3,	/* WAVE file */
287   FT_AIFF_C	= 4,	/* AIFF-C sound file */
288   FT_SPHERE	= 5,	/* NIST SPHERE audio file */
289   FT_ESPS	= 6,	/* ESPS sampled data feature file */
290   FT_SF		= 7,	/* IRCAM soundfile */
291   FT_SPPACK	= 8, 	/* SPPACK file */
292   FT_INRS	= 9,	/* INRS-Telecom audio file */
293   FT_AIFF	= 10,	/* AIFF sound file */
294   FT_SPW	= 11,	/* Comdisco SPW Signal file */
295   FT_NSP	= 12,	/* CSL NSP file */
296   FT_TXAUD	= 13	/* Text audio file */
297 };
298 #define NFT	(FT_TXAUD+1)
299 #define FT_ERROR	(-1)	/* error, file type cannot be determined */
300 #define FT_AUTO		255	/* automatic mode (check file header) */
301 #define FT_UNSUP	256	/* unsupported audio file type */
302 
303 #ifdef AF_FILE_TYPE_NAMES
304 static const char *AF_FTN[NFT] = {
305   NULL,
306   "Audio file",		/* Headerless or non-standard audio file */
307   "AU audio file",
308   "WAVE file",
309   "AIFF-C sound file",
310   "NIST SPHERE audio file",
311   "ESPS audio file",
312   "IRCAM soundfile",
313   "SPPACK file",
314   "INRS-Telecom audio file",
315   "AIFF sound file",
316   "Comdisco SPW Signal file",
317   "CSL NSP file",
318   "Text audio file"
319 };
320 #endif
321 
322 /* Structure for headerless input audio file parameters */
323 struct AF_NHpar {
324   int Format;		/* Data format */
325   long int Start;	/* Offset in bytes to the start of data */
326   double Sfreq;		/* Sampling frequency */
327   int Swapb;		/* Byte swap flag */
328   long int Nchan;	/* Number of channels */
329   double ScaleF;	/* Scale factor */
330 };
331 
332 /* Defines for data length checks */
333 /* Special values for Ldata, Nsamp and Nframe */
334 #define AF_LDATA_UNDEF	-1L
335 #define AF_NSAMP_UNDEF	-1L
336 #define AF_NFRAME_UNDEF	-1L
337 
338 /* Header data length fixup flags */
339 #define AF_NOFIX		0
340 #define AF_FIX_NSAMP_HIGH	1
341 #define AF_FIX_NSAMP_LOW	2
342 #define AF_FIX_LDATA_HIGH	4
343 
344 /* === Output audio files === */
345 /* Codes for output audio file types */
346 #define FTW_UNDEF	0	/* Undefined */
347 #define FTW_AU		1	/* AU audio file */
348 #define FTW_WAVE	2	/* WAVE file */
349 #define FTW_AIFF_C	3	/* AIFF-C sound file */
350 #define FTW_NH_X       	4	/* Headerless */
351 #define FTW_AIFF	5	/* AIFF sound file */
352 
353 #define FTW_SUBTYPE_MOD	16
354 
355 #define FTW_NH_EB	(FTW_NH_X + DS_EB * FTW_SUBTYPE_MOD)
356 #define FTW_NH_EL	(FTW_NH_X + DS_EL * FTW_SUBTYPE_MOD)
357 #define FTW_NH_NATIVE	(FTW_NH_X + DS_NATIVE * FTW_SUBTYPE_MOD)
358 #define FTW_NH_SWAP	(FTW_NH_X + DS_SWAP * FTW_SUBTYPE_MOD)
359 
360 #define FTW_WAVE_NOEX	(FTW_WAVE + 1 * FTW_SUBTYPE_MOD)
361 
362 #ifdef AF_OUTPUT_FILE_TYPE_NAMES
363 static const char *AF_FTWN[NFT] = {
364   NULL,
365   "AU audio file",
366   "WAVE file",
367   "AIFF-C sound file",
368   "Headerless audio file",
369   "AIFF sound file"
370 };
371 #endif
372 
373 /* Default sampling frequency if the sampling frequency is zero or invalid */
374 #define AF_SFREQ_DEFAULT	8000.0
375 
376 /* Structure for AF routine options */
377 struct AF_opt {
378   int ErrorHalt;		/* Error handling:
379 				   0 - continue on error,
380 				   1 - halt on error */
381 
382 /* Input files */
383   int NsampND;			/* Number of samples requirement
384 				   0 - Nsamp must be known
385 				   1 - Nsamp=AF_NSAMP_UNDEF allowed */
386   int RAccess;			/* Random access requirement:
387                                    0 - input file can be sequential access or
388 				       random access
389                                    1 - input file must be random access */
390   int FtypeI;			/* Input file type, FT_AUTO, FT_AU, etc. */
391   struct AF_NHpar NHpar;	/* Headerless input audio file parameters */
392 
393 /* Output files */
394   long int Nframe;		/* Number of frames
395 				   AF_NFRAME_UNDEF means this value is
396 				   undefined */
397   int NbS;			/* Number of bits per sample
398 				   0 means use full precision of the data */
399   unsigned char *SpkrConfig;	/* Speaker configuration */
400   char *Uinfo;			/* User supplied information string */
401 };
402 
403 /* Values for AFresetOptions category */
404 #define AF_OPT_GENERAL	1
405 #define AF_OPT_INPUT	2
406 #define AF_OPT_OUTPUT	4
407 
408 #ifdef __cplusplus
409 extern "C" {
410 #endif
411 
412 struct AF_opt *
413 AFoptions (void);
414 struct AF_opt *
415 AFresetOptions (unsigned int Cat);
416 
417 #ifdef __cplusplus
418 }
419 #endif
420 
421 #endif	/* AFpar_h_ */
422