338477e07a147d3cdfe69faadfc3bec5413b836e
[Faustine.git] / interpretor / libsndfile-1.0.25 / src / mat5.c
1 /*
2 ** Copyright (C) 2002-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 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <math.h>
26
27 #include "sndfile.h"
28 #include "sfendian.h"
29 #include "common.h"
30
31 /*------------------------------------------------------------------------------
32 ** Information on how to decode and encode this file was obtained in a PDF
33 ** file which I found on http://www.wotsit.org/.
34 ** Also did a lot of testing with GNU Octave but do not have access to
35 ** Matlab (tm) and so could not test it there.
36 */
37
38 /*------------------------------------------------------------------------------
39 ** Macros to handle big/little endian issues.
40 */
41
42 #define MATL_MARKER (MAKE_MARKER ('M', 'A', 'T', 'L'))
43
44 #define IM_MARKER (('I' << 8) + 'M')
45 #define MI_MARKER (('M' << 8) + 'I')
46
47 /*------------------------------------------------------------------------------
48 ** Enums and typedefs.
49 */
50
51 enum
52 { MAT5_TYPE_SCHAR = 0x1,
53 MAT5_TYPE_UCHAR = 0x2,
54 MAT5_TYPE_INT16 = 0x3,
55 MAT5_TYPE_UINT16 = 0x4,
56 MAT5_TYPE_INT32 = 0x5,
57 MAT5_TYPE_UINT32 = 0x6,
58 MAT5_TYPE_FLOAT = 0x7,
59 MAT5_TYPE_DOUBLE = 0x9,
60 MAT5_TYPE_ARRAY = 0xE,
61
62 MAT5_TYPE_COMP_USHORT = 0x00020004,
63 MAT5_TYPE_COMP_UINT = 0x00040006
64 } ;
65
66 typedef struct
67 { sf_count_t size ;
68 int rows, cols ;
69 char name [32] ;
70 } MAT5_MATRIX ;
71
72 /*------------------------------------------------------------------------------
73 ** Private static functions.
74 */
75
76 static int mat5_close (SF_PRIVATE *psf) ;
77
78 static int mat5_write_header (SF_PRIVATE *psf, int calc_length) ;
79 static int mat5_read_header (SF_PRIVATE *psf) ;
80
81 /*------------------------------------------------------------------------------
82 ** Public function.
83 */
84
85 int
86 mat5_open (SF_PRIVATE *psf)
87 { int subformat, error = 0 ;
88
89 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
90 { if ((error = mat5_read_header (psf)))
91 return error ;
92 } ;
93
94 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_MAT5)
95 return SFE_BAD_OPEN_FORMAT ;
96
97 subformat = SF_CODEC (psf->sf.format) ;
98
99 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
100 { if (psf->is_pipe)
101 return SFE_NO_PIPE_WRITE ;
102
103 psf->endian = SF_ENDIAN (psf->sf.format) ;
104 if (CPU_IS_LITTLE_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
105 psf->endian = SF_ENDIAN_LITTLE ;
106 else if (CPU_IS_BIG_ENDIAN && (psf->endian == SF_ENDIAN_CPU || psf->endian == 0))
107 psf->endian = SF_ENDIAN_BIG ;
108
109 if ((error = mat5_write_header (psf, SF_FALSE)))
110 return error ;
111
112 psf->write_header = mat5_write_header ;
113 } ;
114
115 psf->container_close = mat5_close ;
116
117 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
118
119 switch (subformat)
120 { case SF_FORMAT_PCM_U8 :
121 case SF_FORMAT_PCM_16 :
122 case SF_FORMAT_PCM_32 :
123 error = pcm_init (psf) ;
124 break ;
125
126 case SF_FORMAT_FLOAT :
127 error = float32_init (psf) ;
128 break ;
129
130 case SF_FORMAT_DOUBLE :
131 error = double64_init (psf) ;
132 break ;
133
134 default : break ;
135 } ;
136
137 return error ;
138 } /* mat5_open */
139
140 /*------------------------------------------------------------------------------
141 */
142
143 static int
144 mat5_close (SF_PRIVATE *psf)
145 {
146 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
147 mat5_write_header (psf, SF_TRUE) ;
148
149 return 0 ;
150 } /* mat5_close */
151
152 /*------------------------------------------------------------------------------
153 */
154
155 static int
156 mat5_write_header (SF_PRIVATE *psf, int calc_length)
157 { static const char *filename = "MATLAB 5.0 MAT-file, written by " PACKAGE "-" VERSION ", " ;
158 static const char *sr_name = "samplerate\0\0\0\0\0\0\0\0\0\0\0" ;
159 static const char *wd_name = "wavedata\0" ;
160 sf_count_t current, datasize ;
161 int encoding ;
162
163 current = psf_ftell (psf) ;
164
165 if (calc_length)
166 { psf_fseek (psf, 0, SEEK_END) ;
167 psf->filelength = psf_ftell (psf) ;
168 psf_fseek (psf, 0, SEEK_SET) ;
169
170 psf->datalength = psf->filelength - psf->dataoffset ;
171 if (psf->dataend)
172 psf->datalength -= psf->filelength - psf->dataend ;
173
174 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
175 } ;
176
177 switch (SF_CODEC (psf->sf.format))
178 { case SF_FORMAT_PCM_U8 :
179 encoding = MAT5_TYPE_UCHAR ;
180 break ;
181
182 case SF_FORMAT_PCM_16 :
183 encoding = MAT5_TYPE_INT16 ;
184 break ;
185
186 case SF_FORMAT_PCM_32 :
187 encoding = MAT5_TYPE_INT32 ;
188 break ;
189
190 case SF_FORMAT_FLOAT :
191 encoding = MAT5_TYPE_FLOAT ;
192 break ;
193
194 case SF_FORMAT_DOUBLE :
195 encoding = MAT5_TYPE_DOUBLE ;
196 break ;
197
198 default :
199 return SFE_BAD_OPEN_FORMAT ;
200 } ;
201
202 /* Reset the current header length to zero. */
203 psf->header [0] = 0 ;
204 psf->headindex = 0 ;
205 psf_fseek (psf, 0, SEEK_SET) ;
206
207 psf_get_date_str (psf->u.cbuf, sizeof (psf->u.scbuf)) ;
208 psf_binheader_writef (psf, "bb", filename, strlen (filename), psf->u.cbuf, strlen (psf->u.cbuf) + 1) ;
209
210 memset (psf->u.scbuf, ' ', 124 - psf->headindex) ;
211 psf_binheader_writef (psf, "b", psf->u.scbuf, make_size_t (124 - psf->headindex)) ;
212
213 psf->rwf_endian = psf->endian ;
214
215 if (psf->rwf_endian == SF_ENDIAN_BIG)
216 psf_binheader_writef (psf, "2b", 0x0100, "MI", make_size_t (2)) ;
217 else
218 psf_binheader_writef (psf, "2b", 0x0100, "IM", make_size_t (2)) ;
219
220 psf_binheader_writef (psf, "444444", MAT5_TYPE_ARRAY, 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
221 psf_binheader_writef (psf, "4444", MAT5_TYPE_INT32, 8, 1, 1) ;
222 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (sr_name), sr_name, make_size_t (16)) ;
223
224 if (psf->sf.samplerate > 0xFFFF)
225 psf_binheader_writef (psf, "44", MAT5_TYPE_COMP_UINT, psf->sf.samplerate) ;
226 else
227 { unsigned short samplerate = psf->sf.samplerate ;
228
229 psf_binheader_writef (psf, "422", MAT5_TYPE_COMP_USHORT, samplerate, 0) ;
230 } ;
231
232 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
233
234 psf_binheader_writef (psf, "t484444", MAT5_TYPE_ARRAY, datasize + 64, MAT5_TYPE_UINT32, 8, 6, 0) ;
235 psf_binheader_writef (psf, "t4448", MAT5_TYPE_INT32, 8, psf->sf.channels, psf->sf.frames) ;
236 psf_binheader_writef (psf, "44b", MAT5_TYPE_SCHAR, strlen (wd_name), wd_name, strlen (wd_name)) ;
237
238 datasize = psf->sf.frames * psf->sf.channels * psf->bytewidth ;
239 if (datasize > 0x7FFFFFFF)
240 datasize = 0x7FFFFFFF ;
241
242 psf_binheader_writef (psf, "t48", encoding, datasize) ;
243
244 /* Header construction complete so write it out. */
245 psf_fwrite (psf->header, psf->headindex, 1, psf) ;
246
247 if (psf->error)
248 return psf->error ;
249
250 psf->dataoffset = psf->headindex ;
251
252 if (current > 0)
253 psf_fseek (psf, current, SEEK_SET) ;
254
255 return psf->error ;
256 } /* mat5_write_header */
257
258 static int
259 mat5_read_header (SF_PRIVATE *psf)
260 { char name [32] ;
261 short version, endian ;
262 int type, flags1, flags2, rows, cols ;
263 unsigned size ;
264
265 psf_binheader_readf (psf, "pb", 0, psf->u.cbuf, 124) ;
266
267 psf->u.scbuf [125] = 0 ;
268
269 if (strlen (psf->u.cbuf) >= 124)
270 return SFE_UNIMPLEMENTED ;
271
272 if (strstr (psf->u.cbuf, "MATLAB 5.0 MAT-file") == psf->u.cbuf)
273 psf_log_printf (psf, "%s\n", psf->u.scbuf) ;
274
275
276 psf_binheader_readf (psf, "E22", &version, &endian) ;
277
278 if (endian == MI_MARKER)
279 { psf->endian = psf->rwf_endian = SF_ENDIAN_BIG ;
280 if (CPU_IS_LITTLE_ENDIAN) version = ENDSWAP_SHORT (version) ;
281 }
282 else if (endian == IM_MARKER)
283 { psf->endian = psf->rwf_endian = SF_ENDIAN_LITTLE ;
284 if (CPU_IS_BIG_ENDIAN) version = ENDSWAP_SHORT (version) ;
285 }
286 else
287 return SFE_MAT5_BAD_ENDIAN ;
288
289 if ((CPU_IS_LITTLE_ENDIAN && endian == IM_MARKER) ||
290 (CPU_IS_BIG_ENDIAN && endian == MI_MARKER))
291 version = ENDSWAP_SHORT (version) ;
292
293 psf_log_printf (psf, "Version : 0x%04X\n", version) ;
294 psf_log_printf (psf, "Endian : 0x%04X => %s\n", endian,
295 (psf->endian == SF_ENDIAN_LITTLE) ? "Little" : "Big") ;
296
297 /*========================================================*/
298 psf_binheader_readf (psf, "44", &type, &size) ;
299 psf_log_printf (psf, "Block\n Type : %X Size : %d\n", type, size) ;
300
301 if (type != MAT5_TYPE_ARRAY)
302 return SFE_MAT5_NO_BLOCK ;
303
304 psf_binheader_readf (psf, "44", &type, &size) ;
305 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
306
307 if (type != MAT5_TYPE_UINT32)
308 return SFE_MAT5_NO_BLOCK ;
309
310 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
311 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
312
313 psf_binheader_readf (psf, "44", &type, &size) ;
314 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
315
316 if (type != MAT5_TYPE_INT32)
317 return SFE_MAT5_NO_BLOCK ;
318
319 psf_binheader_readf (psf, "44", &rows, &cols) ;
320 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
321
322 if (rows != 1 || cols != 1)
323 return SFE_MAT5_SAMPLE_RATE ;
324
325 psf_binheader_readf (psf, "4", &type) ;
326
327 if (type == MAT5_TYPE_SCHAR)
328 { psf_binheader_readf (psf, "4", &size) ;
329 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
330 if (size > SIGNED_SIZEOF (name) - 1)
331 { psf_log_printf (psf, "Error : Bad name length.\n") ;
332 return SFE_MAT5_NO_BLOCK ;
333 } ;
334
335 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
336 name [size] = 0 ;
337 }
338 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
339 { size = type >> 16 ;
340 if (size > 4)
341 { psf_log_printf (psf, "Error : Bad name length.\n") ;
342 return SFE_MAT5_NO_BLOCK ;
343 } ;
344
345 psf_log_printf (psf, " Type : %X\n", type) ;
346 psf_binheader_readf (psf, "4", &name) ;
347 name [size] = 0 ;
348 }
349 else
350 return SFE_MAT5_NO_BLOCK ;
351
352 psf_log_printf (psf, " Name : %s\n", name) ;
353
354 /*-----------------------------------------*/
355
356 psf_binheader_readf (psf, "44", &type, &size) ;
357
358 switch (type)
359 { case MAT5_TYPE_DOUBLE :
360 { double samplerate ;
361
362 psf_binheader_readf (psf, "d", &samplerate) ;
363 snprintf (name, sizeof (name), "%f\n", samplerate) ;
364 psf_log_printf (psf, " Val : %s\n", name) ;
365
366 psf->sf.samplerate = lrint (samplerate) ;
367 } ;
368 break ;
369
370 case MAT5_TYPE_COMP_USHORT :
371 { unsigned short samplerate ;
372
373 psf_binheader_readf (psf, "j2j", -4, &samplerate, 2) ;
374 psf_log_printf (psf, " Val : %u\n", samplerate) ;
375 psf->sf.samplerate = samplerate ;
376 }
377 break ;
378
379 case MAT5_TYPE_COMP_UINT :
380 psf_log_printf (psf, " Val : %u\n", size) ;
381 psf->sf.samplerate = size ;
382 break ;
383
384 default :
385 psf_log_printf (psf, " Type : %X Size : %d ***\n", type, size) ;
386 return SFE_MAT5_SAMPLE_RATE ;
387 } ;
388
389 /*-----------------------------------------*/
390
391
392 psf_binheader_readf (psf, "44", &type, &size) ;
393 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
394
395 if (type != MAT5_TYPE_ARRAY)
396 return SFE_MAT5_NO_BLOCK ;
397
398 psf_binheader_readf (psf, "44", &type, &size) ;
399 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
400
401 if (type != MAT5_TYPE_UINT32)
402 return SFE_MAT5_NO_BLOCK ;
403
404 psf_binheader_readf (psf, "44", &flags1, &flags2) ;
405 psf_log_printf (psf, " Flg1 : %X Flg2 : %d\n", flags1, flags2) ;
406
407 psf_binheader_readf (psf, "44", &type, &size) ;
408 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
409
410 if (type != MAT5_TYPE_INT32)
411 return SFE_MAT5_NO_BLOCK ;
412
413 psf_binheader_readf (psf, "44", &rows, &cols) ;
414 psf_log_printf (psf, " Rows : %X Cols : %d\n", rows, cols) ;
415
416 psf_binheader_readf (psf, "4", &type) ;
417
418 if (type == MAT5_TYPE_SCHAR)
419 { psf_binheader_readf (psf, "4", &size) ;
420 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
421 if (size > SIGNED_SIZEOF (name) - 1)
422 { psf_log_printf (psf, "Error : Bad name length.\n") ;
423 return SFE_MAT5_NO_BLOCK ;
424 } ;
425
426 psf_binheader_readf (psf, "bj", name, size, (8 - (size % 8)) % 8) ;
427 name [size] = 0 ;
428 }
429 else if ((type & 0xFFFF) == MAT5_TYPE_SCHAR)
430 { size = type >> 16 ;
431 if (size > 4)
432 { psf_log_printf (psf, "Error : Bad name length.\n") ;
433 return SFE_MAT5_NO_BLOCK ;
434 } ;
435
436 psf_log_printf (psf, " Type : %X\n", type) ;
437 psf_binheader_readf (psf, "4", &name) ;
438 name [size] = 0 ;
439 }
440 else
441 return SFE_MAT5_NO_BLOCK ;
442
443 psf_log_printf (psf, " Name : %s\n", name) ;
444
445 psf_binheader_readf (psf, "44", &type, &size) ;
446 psf_log_printf (psf, " Type : %X Size : %d\n", type, size) ;
447
448 /*++++++++++++++++++++++++++++++++++++++++++++++++++*/
449
450 if (rows == 0 && cols == 0)
451 { psf_log_printf (psf, "*** Error : zero channel count.\n") ;
452 return SFE_CHANNEL_COUNT_ZERO ;
453 } ;
454
455 psf->sf.channels = rows ;
456 psf->sf.frames = cols ;
457
458 psf->sf.format = psf->endian | SF_FORMAT_MAT5 ;
459
460 switch (type)
461 { case MAT5_TYPE_DOUBLE :
462 psf_log_printf (psf, "Data type : double\n") ;
463 psf->sf.format |= SF_FORMAT_DOUBLE ;
464 psf->bytewidth = 8 ;
465 break ;
466
467 case MAT5_TYPE_FLOAT :
468 psf_log_printf (psf, "Data type : float\n") ;
469 psf->sf.format |= SF_FORMAT_FLOAT ;
470 psf->bytewidth = 4 ;
471 break ;
472
473 case MAT5_TYPE_INT32 :
474 psf_log_printf (psf, "Data type : 32 bit PCM\n") ;
475 psf->sf.format |= SF_FORMAT_PCM_32 ;
476 psf->bytewidth = 4 ;
477 break ;
478
479 case MAT5_TYPE_INT16 :
480 psf_log_printf (psf, "Data type : 16 bit PCM\n") ;
481 psf->sf.format |= SF_FORMAT_PCM_16 ;
482 psf->bytewidth = 2 ;
483 break ;
484
485 case MAT5_TYPE_UCHAR :
486 psf_log_printf (psf, "Data type : unsigned 8 bit PCM\n") ;
487 psf->sf.format |= SF_FORMAT_PCM_U8 ;
488 psf->bytewidth = 1 ;
489 break ;
490
491 default :
492 psf_log_printf (psf, "*** Error : Bad marker %08X\n", type) ;
493 return SFE_UNIMPLEMENTED ;
494 } ;
495
496 psf->dataoffset = psf_ftell (psf) ;
497 psf->datalength = psf->filelength - psf->dataoffset ;
498
499 return 0 ;
500 } /* mat5_read_header */
501