2 ** Copyright (C) 2001-2003 Erik de Castro Lopo <erikd@zip.com.au>
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.
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.
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.
30 /*------------------------------------------------------------------------------
31 ** Macros to handle big/little endian issues.
34 /* The IRCAM magic number is weird in that one byte in the number can have
35 ** values of 0x1, 0x2, 0x03 or 0x04. Hence the need for a marker and a mask.
38 #define IRCAM_BE_MASK (MAKE_MARKER (0xFF, 0xFF, 0x00, 0xFF))
39 #define IRCAM_BE_MARKER (MAKE_MARKER (0x64, 0xA3, 0x00, 0x00))
41 #define IRCAM_LE_MASK (MAKE_MARKER (0xFF, 0x00, 0xFF, 0xFF))
42 #define IRCAM_LE_MARKER (MAKE_MARKER (0x00, 0x00, 0xA3, 0x64))
44 #define IRCAM_02B_MARKER (MAKE_MARKER (0x00, 0x02, 0xA3, 0x64))
45 #define IRCAM_03L_MARKER (MAKE_MARKER (0x64, 0xA3, 0x03, 0x00))
47 #define IRCAM_DATA_OFFSET (1024)
49 /*------------------------------------------------------------------------------
54 { IRCAM_PCM_16
= 0x00002,
55 IRCAM_FLOAT
= 0x00004,
58 IRCAM_PCM_32
= 0x40004
62 /*------------------------------------------------------------------------------
63 ** Private static functions.
66 static int ircam_close (SF_PRIVATE
*psf
) ;
67 static int ircam_write_header (SF_PRIVATE
*psf
, int calc_length
) ;
68 static int ircam_read_header (SF_PRIVATE
*psf
) ;
70 static int get_encoding (int subformat
) ;
72 static char* get_encoding_str (int encoding
) ;
74 /*------------------------------------------------------------------------------
79 ircam_open (SF_PRIVATE
*psf
)
81 int error
= SFE_NO_ERROR
;
83 if (psf
->mode
== SFM_READ
|| (psf
->mode
== SFM_RDWR
&& psf
->filelength
> 0))
84 { if ((error
= ircam_read_header (psf
)))
88 subformat
= psf
->sf
.format
& SF_FORMAT_SUBMASK
;
90 if (psf
->mode
== SFM_WRITE
|| psf
->mode
== SFM_RDWR
)
91 { if ((psf
->sf
.format
& SF_FORMAT_TYPEMASK
) != SF_FORMAT_IRCAM
)
92 return SFE_BAD_OPEN_FORMAT
;
94 psf
->endian
= psf
->sf
.format
& SF_FORMAT_ENDMASK
;
95 if (psf
->endian
== 0 || psf
->endian
== SF_ENDIAN_CPU
)
96 psf
->endian
= (CPU_IS_BIG_ENDIAN
) ? SF_ENDIAN_BIG
: SF_ENDIAN_LITTLE
;
98 psf
->dataoffset
= IRCAM_DATA_OFFSET
;
100 if ((error
= ircam_write_header (psf
, SF_FALSE
)))
103 psf
->write_header
= ircam_write_header
;
106 psf
->close
= ircam_close
;
109 { case SF_FORMAT_ULAW
: /* 8-bit Ulaw encoding. */
110 error
= ulaw_init (psf
) ;
113 case SF_FORMAT_ALAW
: /* 8-bit Alaw encoding. */
114 error
= alaw_init (psf
) ;
117 case SF_FORMAT_PCM_16
: /* 16-bit linear PCM. */
118 case SF_FORMAT_PCM_32
: /* 32-bit linear PCM. */
119 error
= pcm_init (psf
) ;
122 case SF_FORMAT_FLOAT
: /* 32-bit linear PCM. */
123 error
= float32_init (psf
) ;
132 /*------------------------------------------------------------------------------
136 ircam_read_header (SF_PRIVATE
*psf
)
137 { unsigned int marker
, encoding
;
139 int error
= SFE_NO_ERROR
;
141 psf_binheader_readf (psf
, "epmf44", 0, &marker
, &samplerate
, &(psf
->sf
.channels
), &encoding
) ;
143 if (((marker
& IRCAM_LE_MASK
) != IRCAM_LE_MARKER
) &&
144 ((marker
& IRCAM_BE_MASK
) != IRCAM_BE_MARKER
))
145 { psf_log_printf (psf
, "marker: 0x%X\n", marker
) ;
146 return SFE_IRCAM_NO_MARKER
;
149 psf
->endian
= SF_ENDIAN_LITTLE
;
151 if (psf
->sf
.channels
> 256)
152 { psf_binheader_readf (psf
, "Epmf44", 0, &marker
, &samplerate
, &(psf
->sf
.channels
), &encoding
) ;
154 /* Sanity checking for endian-ness detection. */
155 if (psf
->sf
.channels
> 256)
156 { psf_log_printf (psf
, "marker: 0x%X\n", marker
) ;
157 return SFE_IRCAM_BAD_CHANNELS
;
160 psf
->endian
= SF_ENDIAN_BIG
;
163 psf_log_printf (psf
, "marker: 0x%X\n", marker
) ;
165 psf
->sf
.samplerate
= (int) samplerate
;
167 psf_log_printf (psf
, " Sample Rate : %d\n"
169 " Encoding : %X => %s\n", psf
->sf
.samplerate
, psf
->sf
.channels
, encoding
, get_encoding_str (encoding
)) ;
172 { case IRCAM_PCM_16
:
174 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
176 psf
->sf
.format
= SF_FORMAT_IRCAM
| SF_FORMAT_PCM_16
;
181 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
183 psf
->sf
.format
= SF_FORMAT_IRCAM
| SF_FORMAT_PCM_32
;
188 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
190 psf
->sf
.format
= SF_FORMAT_IRCAM
| SF_FORMAT_FLOAT
;
195 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
197 psf
->sf
.format
= SF_FORMAT_IRCAM
| SF_FORMAT_ALAW
;
202 psf
->blockwidth
= psf
->sf
.channels
* psf
->bytewidth
;
204 psf
->sf
.format
= SF_FORMAT_IRCAM
| SF_FORMAT_ULAW
;
208 error
= SFE_IRCAM_UNKNOWN_FORMAT
;
212 if (psf
->endian
== SF_ENDIAN_BIG
)
213 psf
->sf
.format
|= SF_ENDIAN_BIG
;
215 psf
->sf
.format
|= SF_ENDIAN_LITTLE
;
220 psf
->dataoffset
= IRCAM_DATA_OFFSET
;
221 psf
->datalength
= psf
->filelength
- psf
->dataoffset
;
223 if (! psf
->sf
.frames
&& psf
->blockwidth
)
224 psf
->sf
.frames
= psf
->datalength
/ psf
->blockwidth
;
226 psf_log_printf (psf
, " Samples : %d\n", psf
->sf
.frames
) ;
228 psf_binheader_readf (psf
, "p", IRCAM_DATA_OFFSET
) ;
231 } /* ircam_read_header */
234 ircam_close (SF_PRIVATE
*psf
)
236 psf_log_printf (psf
, "close\n") ;
242 ircam_write_header (SF_PRIVATE
*psf
, int calc_length
)
247 current
= psf_ftell (psf
) ;
249 calc_length
= calc_length
;
251 /* This also sets psf->endian. */
252 encoding
= get_encoding (psf
->sf
.format
& SF_FORMAT_SUBMASK
) ;
255 return SFE_BAD_OPEN_FORMAT
;
257 /* Reset the current header length to zero. */
258 psf
->header
[0] = 0 ;
260 psf_fseek (psf
, 0, SEEK_SET
) ;
262 samplerate
= psf
->sf
.samplerate
;
264 if (psf
->endian
== SF_ENDIAN_BIG
)
265 { psf_binheader_writef (psf
, "Emf", IRCAM_02B_MARKER
, samplerate
) ;
266 psf_binheader_writef (psf
, "E44", psf
->sf
.channels
, encoding
) ;
268 else if (psf
->endian
== SF_ENDIAN_LITTLE
)
269 { psf_binheader_writef (psf
, "emf", IRCAM_03L_MARKER
, samplerate
) ;
270 psf_binheader_writef (psf
, "e44", psf
->sf
.channels
, encoding
) ;
273 return SFE_BAD_OPEN_FORMAT
;
275 psf_binheader_writef (psf
, "z", IRCAM_DATA_OFFSET
- psf
->headindex
) ;
277 /* Header construction complete so write it out. */
278 psf_fwrite (psf
->header
, psf
->headindex
, 1, psf
) ;
284 psf_fseek (psf
, current
, SEEK_SET
) ;
287 } /* ircam_write_header */
290 get_encoding (int subformat
)
292 { case SF_FORMAT_PCM_16
: return IRCAM_PCM_16
;
293 case SF_FORMAT_PCM_32
: return IRCAM_PCM_32
;
295 case SF_FORMAT_FLOAT
: return IRCAM_FLOAT
;
297 case SF_FORMAT_ULAW
: return IRCAM_ULAW
;
298 case SF_FORMAT_ALAW
: return IRCAM_ALAW
;
307 get_encoding_str (int encoding
)
309 { case IRCAM_PCM_16
: return "16 bit PCM" ;
310 case IRCAM_FLOAT
: return "32 bit float" ;
311 case IRCAM_ALAW
: return "A law" ;
312 case IRCAM_ULAW
: return "u law" ;
313 case IRCAM_PCM_32
: return "32 bit PCM" ;
315 return "Unknown encoding" ;
316 } /* get_encoding_str */