cd3afac842f1719df960ee6878275a80f57ace20
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / nist.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU Lesser General Public License as published by
6 ** the Free Software Foundation; either version 2.1 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /*
20 ** Some of the information used to read NIST files was gleaned from
21 ** reading the code of Bill Schottstaedt's sndlib library
22 ** ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz
23 ** However, no code from that package was used.
24 */
25
26 #include "sfconfig.h"
27
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "sndfile.h"
34 #include "sfendian.h"
35 #include "common.h"
36
37 /*------------------------------------------------------------------------------
38 */
39
40 #define NIST_HEADER_LENGTH 1024
41
42 /*------------------------------------------------------------------------------
43 ** Private static functions.
44 */
45
46 static int nist_close (SF_PRIVATE *psf) ;
47 static int nist_write_header (SF_PRIVATE *psf, int calc_length) ;
48 static int nist_read_header (SF_PRIVATE *psf) ;
49
50 /*------------------------------------------------------------------------------
51 */
52
53 int
54 nist_open (SF_PRIVATE *psf)
55 { int error ;
56
57 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
58 { if ((error = nist_read_header (psf)))
59 return error ;
60 } ;
61
62 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
63 { if (psf->is_pipe)
64 return SFE_NO_PIPE_WRITE ;
65
66 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_NIST)
67 return SFE_BAD_OPEN_FORMAT ;
68
69 psf->endian = SF_ENDIAN (psf->sf.format) ;
70 if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
71 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
72
73 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
74 psf->sf.frames = 0 ;
75
76 if ((error = nist_write_header (psf, SF_FALSE)))
77 return error ;
78
79 psf->write_header = nist_write_header ;
80 } ;
81
82 psf->container_close = nist_close ;
83
84 switch (SF_CODEC (psf->sf.format))
85 { case SF_FORMAT_PCM_S8 :
86 error = pcm_init (psf) ;
87 break ;
88
89 case SF_FORMAT_PCM_16 :
90 case SF_FORMAT_PCM_24 :
91 case SF_FORMAT_PCM_32 :
92 error = pcm_init (psf) ;
93 break ;
94
95 case SF_FORMAT_ULAW :
96 error = ulaw_init (psf) ;
97 break ;
98
99 case SF_FORMAT_ALAW :
100 error = alaw_init (psf) ;
101 break ;
102
103 default : error = SFE_UNIMPLEMENTED ;
104 break ;
105 } ;
106
107 return error ;
108 } /* nist_open */
109
110 /*------------------------------------------------------------------------------
111 */
112
113 static char bad_header [] =
114 { 'N', 'I', 'S', 'T', '_', '1', 'A', 0x0d, 0x0a,
115 ' ', ' ', ' ', '1', '0', '2', '4', 0x0d, 0x0a,
116 0
117 } ;
118
119 static int
120 nist_read_header (SF_PRIVATE *psf)
121 { char *psf_header ;
122 int bitwidth = 0, count, encoding ;
123 unsigned bytes = 0 ;
124 char str [64], *cptr ;
125 long samples ;
126
127 psf_header = psf->u.cbuf ;
128
129 if (sizeof (psf->header) <= NIST_HEADER_LENGTH)
130 return SFE_INTERNAL ;
131
132 /* Go to start of file and read in the whole header. */
133 psf_binheader_readf (psf, "pb", 0, psf_header, NIST_HEADER_LENGTH) ;
134
135 /* Header is a string, so make sure it is null terminated. */
136 psf_header [NIST_HEADER_LENGTH] = 0 ;
137
138 /* Now trim the header after the end marker. */
139 if ((cptr = strstr (psf_header, "end_head")))
140 { cptr += strlen ("end_head") + 1 ;
141 cptr [0] = 0 ;
142 } ;
143
144 if (strstr (psf_header, bad_header) == psf_header)
145 return SFE_NIST_CRLF_CONVERISON ;
146
147 /* Make sure its a NIST file. */
148 if (strstr (psf_header, "NIST_1A\n") != psf_header)
149 { psf_log_printf (psf, "Not a NIST file.\n") ;
150 return SFE_NIST_BAD_HEADER ;
151 } ;
152
153 if (sscanf (psf_header, "NIST_1A\n%d\n", &count) == 1)
154 psf->dataoffset = count ;
155 else
156 { psf_log_printf (psf, "*** Suspicious header length.\n") ;
157 psf->dataoffset = NIST_HEADER_LENGTH ;
158 } ;
159
160 /* Determine sample encoding, start by assuming PCM. */
161 encoding = SF_FORMAT_PCM_U8 ;
162 if ((cptr = strstr (psf_header, "sample_coding -s")))
163 { sscanf (cptr, "sample_coding -s%d %63s", &count, str) ;
164
165 if (strcmp (str, "pcm") == 0)
166 { /* Correct this later when we find out the bitwidth. */
167 encoding = SF_FORMAT_PCM_U8 ;
168 }
169 else if (strcmp (str, "alaw") == 0)
170 encoding = SF_FORMAT_ALAW ;
171 else if ((strcmp (str, "ulaw") == 0) || (strcmp (str, "mu-law") == 0))
172 encoding = SF_FORMAT_ULAW ;
173 else
174 { psf_log_printf (psf, "*** Unknown encoding : %s\n", str) ;
175 encoding = 0 ;
176 } ;
177 } ;
178
179 if ((cptr = strstr (psf_header, "channel_count -i ")) != NULL)
180 sscanf (cptr, "channel_count -i %d", &(psf->sf.channels)) ;
181
182 if ((cptr = strstr (psf_header, "sample_rate -i ")) != NULL)
183 sscanf (cptr, "sample_rate -i %d", &(psf->sf.samplerate)) ;
184
185 if ((cptr = strstr (psf_header, "sample_count -i ")) != NULL)
186 { sscanf (cptr, "sample_count -i %ld", &samples) ;
187 psf->sf.frames = samples ;
188 } ;
189
190 if ((cptr = strstr (psf_header, "sample_n_bytes -i ")) != NULL)
191 sscanf (cptr, "sample_n_bytes -i %d", &(psf->bytewidth)) ;
192
193 /* Default endian-ness (for 8 bit, u-law, A-law. */
194 psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
195
196 /* This is where we figure out endian-ness. */
197 if ((cptr = strstr (psf_header, "sample_byte_format -s"))
198 && sscanf (cptr, "sample_byte_format -s%u %8s", &bytes, str) == 2)
199 {
200 if (bytes != strlen (str))
201 psf_log_printf (psf, "Weird sample_byte_format : strlen '%s' != %d\n", str, bytes) ;
202
203 if (bytes > 1)
204 { if (psf->bytewidth == 0)
205 psf->bytewidth = bytes ;
206 else if (psf->bytewidth - bytes != 0)
207 { psf_log_printf (psf, "psf->bytewidth (%d) != bytes (%d)\n", psf->bytewidth, bytes) ;
208 return SFE_NIST_BAD_ENCODING ;
209 } ;
210
211 if (strcmp (str, "01") == 0)
212 psf->endian = SF_ENDIAN_LITTLE ;
213 else if (strcmp (str, "10") == 0)
214 psf->endian = SF_ENDIAN_BIG ;
215 else
216 { psf_log_printf (psf, "Weird endian-ness : %s\n", str) ;
217 return SFE_NIST_BAD_ENCODING ;
218 } ;
219 } ;
220
221 psf->sf.format |= psf->endian ;
222 } ;
223
224 if ((cptr = strstr (psf_header, "sample_sig_bits -i ")))
225 sscanf (cptr, "sample_sig_bits -i %d", &bitwidth) ;
226
227 if (strstr (psf_header, "channels_interleaved -s5 FALSE"))
228 { psf_log_printf (psf, "Non-interleaved data unsupported.\n", str) ;
229 return SFE_NIST_BAD_ENCODING ;
230 } ;
231
232 psf->blockwidth = psf->sf.channels * psf->bytewidth ;
233 psf->datalength = psf->filelength - psf->dataoffset ;
234
235 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
236
237 if (encoding == SF_FORMAT_PCM_U8)
238 { switch (psf->bytewidth)
239 { case 1 :
240 psf->sf.format |= SF_FORMAT_PCM_S8 ;
241 break ;
242
243 case 2 :
244 psf->sf.format |= SF_FORMAT_PCM_16 ;
245 break ;
246
247 case 3 :
248 psf->sf.format |= SF_FORMAT_PCM_24 ;
249 break ;
250
251 case 4 :
252 psf->sf.format |= SF_FORMAT_PCM_32 ;
253 break ;
254
255 default : break ;
256 } ;
257 }
258 else if (encoding != 0)
259 psf->sf.format |= encoding ;
260 else
261 return SFE_UNIMPLEMENTED ;
262
263 /* Sanitize psf->sf.format. */
264 switch (SF_CODEC (psf->sf.format))
265 { case SF_FORMAT_ULAW :
266 case SF_FORMAT_ALAW :
267 case SF_FORMAT_PCM_U8 :
268 /* Blank out endian bits. */
269 psf->sf.format = SF_FORMAT_NIST | SF_CODEC (psf->sf.format) ;
270 break ;
271
272 default :
273 break ;
274 } ;
275
276 return 0 ;
277 } /* nist_read_header */
278
279 static int
280 nist_close (SF_PRIVATE *psf)
281 {
282 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
283 nist_write_header (psf, SF_TRUE) ;
284
285 return 0 ;
286 } /* nist_close */
287
288 /*=========================================================================
289 */
290
291 static int
292 nist_write_header (SF_PRIVATE *psf, int calc_length)
293 { const char *end_str ;
294 long samples ;
295 sf_count_t current ;
296
297 current = psf_ftell (psf) ;
298
299 if (calc_length)
300 { psf->filelength = psf_get_filelen (psf) ;
301
302 psf->datalength = psf->filelength - psf->dataoffset ;
303
304 if (psf->dataend)
305 psf->datalength -= psf->filelength - psf->dataend ;
306
307 if (psf->bytewidth > 0)
308 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
309 } ;
310
311 if (psf->endian == SF_ENDIAN_BIG)
312 end_str = "10" ;
313 else if (psf->endian == SF_ENDIAN_LITTLE)
314 end_str = "01" ;
315 else
316 end_str = "error" ;
317
318 /* Clear the whole header. */
319 memset (psf->header, 0, sizeof (psf->header)) ;
320 psf->headindex = 0 ;
321
322 psf_fseek (psf, 0, SEEK_SET) ;
323
324 psf_asciiheader_printf (psf, "NIST_1A\n 1024\n") ;
325 psf_asciiheader_printf (psf, "channel_count -i %d\n", psf->sf.channels) ;
326 psf_asciiheader_printf (psf, "sample_rate -i %d\n", psf->sf.samplerate) ;
327
328 switch (SF_CODEC (psf->sf.format))
329 { case SF_FORMAT_PCM_S8 :
330 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n") ;
331 psf_asciiheader_printf (psf, "sample_n_bytes -i 1\n"
332 "sample_sig_bits -i 8\n") ;
333 break ;
334
335 case SF_FORMAT_PCM_16 :
336 case SF_FORMAT_PCM_24 :
337 case SF_FORMAT_PCM_32 :
338 psf_asciiheader_printf (psf, "sample_n_bytes -i %d\n", psf->bytewidth) ;
339 psf_asciiheader_printf (psf, "sample_sig_bits -i %d\n", psf->bytewidth * 8) ;
340 psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n"
341 "sample_byte_format -s%d %s\n", psf->bytewidth, end_str) ;
342 break ;
343
344 case SF_FORMAT_ALAW :
345 psf_asciiheader_printf (psf, "sample_coding -s4 alaw\n") ;
346 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
347 break ;
348
349 case SF_FORMAT_ULAW :
350 psf_asciiheader_printf (psf, "sample_coding -s4 ulaw\n") ;
351 psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
352 break ;
353
354 default : return SFE_UNIMPLEMENTED ;
355 } ;
356
357 psf->dataoffset = NIST_HEADER_LENGTH ;
358
359 /* Fix this */
360 samples = psf->sf.frames ;
361 psf_asciiheader_printf (psf, "sample_count -i %ld\n", samples) ;
362 psf_asciiheader_printf (psf, "end_head\n") ;
363
364 /* Zero fill to dataoffset. */
365 psf_binheader_writef (psf, "z", (size_t) (NIST_HEADER_LENGTH - psf->headindex)) ;
366
367 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
368
369 if (psf->error)
370 return psf->error ;
371
372 if (current > 0)
373 psf_fseek (psf, current, SEEK_SET) ;
374
375 return psf->error ;
376 } /* nist_write_header */
377