/src/libsndfile/src/ircam.c
Line | Count | Source |
1 | | /* |
2 | | ** Copyright (C) 2001-2017 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 | | |
26 | | #include "sndfile.h" |
27 | | #include "sfendian.h" |
28 | | #include "common.h" |
29 | | |
30 | | /*------------------------------------------------------------------------------ |
31 | | ** Macros to handle big/little endian issues. |
32 | | */ |
33 | | |
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. |
36 | | */ |
37 | | |
38 | 1.17k | #define IRCAM_BE_MASK (MAKE_MARKER (0xFF, 0xFF, 0x00, 0xFF)) |
39 | 1.17k | #define IRCAM_BE_MARKER (MAKE_MARKER (0x64, 0xA3, 0x00, 0x00)) |
40 | | |
41 | 292 | #define IRCAM_LE_MASK (MAKE_MARKER (0xFF, 0x00, 0xFF, 0xFF)) |
42 | 292 | #define IRCAM_LE_MARKER (MAKE_MARKER (0x00, 0x00, 0xA3, 0x64)) |
43 | | |
44 | | #define IRCAM_02B_MARKER (MAKE_MARKER (0x64, 0xA3, 0x02, 0x00)) |
45 | | #define IRCAM_03L_MARKER (MAKE_MARKER (0x64, 0xA3, 0x03, 0x00)) |
46 | | |
47 | 1.04k | #define IRCAM_DATA_OFFSET (1024) |
48 | | |
49 | | /*------------------------------------------------------------------------------ |
50 | | ** Typedefs. |
51 | | */ |
52 | | |
53 | | enum |
54 | | { IRCAM_PCM_16 = 0x00002, |
55 | | IRCAM_FLOAT = 0x00004, |
56 | | IRCAM_ALAW = 0x10001, |
57 | | IRCAM_ULAW = 0x20001, |
58 | | IRCAM_PCM_32 = 0x40004 |
59 | | } ; |
60 | | |
61 | | |
62 | | /*------------------------------------------------------------------------------ |
63 | | ** Private static functions. |
64 | | */ |
65 | | |
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) ; |
69 | | |
70 | | static int get_encoding (int subformat) ; |
71 | | |
72 | | static const char* get_encoding_str (int encoding) ; |
73 | | |
74 | | /*------------------------------------------------------------------------------ |
75 | | ** Public function. |
76 | | */ |
77 | | |
78 | | int |
79 | | ircam_open (SF_PRIVATE *psf) |
80 | 1.17k | { int subformat ; |
81 | 1.17k | int error = SFE_NO_ERROR ; |
82 | | |
83 | 1.17k | if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0)) |
84 | 1.17k | { if ((error = ircam_read_header (psf))) |
85 | 652 | return error ; |
86 | 1.17k | } ; |
87 | | |
88 | 523 | subformat = SF_CODEC (psf->sf.format) ; |
89 | | |
90 | 523 | if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR) |
91 | 0 | { if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_IRCAM) |
92 | 0 | return SFE_BAD_OPEN_FORMAT ; |
93 | | |
94 | 0 | psf->endian = SF_ENDIAN (psf->sf.format) ; |
95 | 0 | if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU) |
96 | 0 | psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ; |
97 | |
|
98 | 0 | psf->dataoffset = IRCAM_DATA_OFFSET ; |
99 | |
|
100 | 0 | if ((error = ircam_write_header (psf, SF_FALSE))) |
101 | 0 | return error ; |
102 | | |
103 | 0 | psf->write_header = ircam_write_header ; |
104 | 523 | } ; |
105 | | |
106 | 523 | psf->container_close = ircam_close ; |
107 | | |
108 | 523 | switch (subformat) |
109 | 523 | { case SF_FORMAT_ULAW : /* 8-bit Ulaw encoding. */ |
110 | 100 | error = ulaw_init (psf) ; |
111 | 100 | break ; |
112 | | |
113 | 126 | case SF_FORMAT_ALAW : /* 8-bit Alaw encoding. */ |
114 | 126 | error = alaw_init (psf) ; |
115 | 126 | break ; |
116 | | |
117 | 108 | case SF_FORMAT_PCM_16 : /* 16-bit linear PCM. */ |
118 | 135 | case SF_FORMAT_PCM_32 : /* 32-bit linear PCM. */ |
119 | 135 | error = pcm_init (psf) ; |
120 | 135 | break ; |
121 | | |
122 | 162 | case SF_FORMAT_FLOAT : /* 32-bit linear PCM. */ |
123 | 162 | error = float32_init (psf) ; |
124 | 162 | break ; |
125 | | |
126 | 0 | default : break ; |
127 | 523 | } ; |
128 | | |
129 | 523 | return error ; |
130 | 523 | } /* ircam_open */ |
131 | | |
132 | | /*------------------------------------------------------------------------------ |
133 | | */ |
134 | | |
135 | | static int |
136 | | ircam_read_header (SF_PRIVATE *psf) |
137 | 1.17k | { unsigned int marker, encoding ; |
138 | 1.17k | float samplerate ; |
139 | 1.17k | int error = SFE_NO_ERROR ; |
140 | | |
141 | 1.17k | psf_binheader_readf (psf, "epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ; |
142 | | |
143 | 1.17k | if (((marker & IRCAM_BE_MASK) != IRCAM_BE_MARKER) && ((marker & IRCAM_LE_MASK) != IRCAM_LE_MARKER)) |
144 | 0 | { psf_log_printf (psf, "marker: 0x%X\n", marker) ; |
145 | 0 | return SFE_IRCAM_NO_MARKER ; |
146 | 1.17k | } ; |
147 | | |
148 | 1.17k | psf->endian = SF_ENDIAN_LITTLE ; |
149 | | |
150 | 1.17k | if (psf->sf.channels > SF_MAX_CHANNELS) |
151 | 430 | { psf_binheader_readf (psf, "Epmf44", 0, &marker, &samplerate, &(psf->sf.channels), &encoding) ; |
152 | | |
153 | | /* Sanity checking for endian-ness detection. */ |
154 | 430 | if (psf->sf.channels > SF_MAX_CHANNELS) |
155 | 66 | { psf_log_printf (psf, "marker: 0x%X\n", marker) ; |
156 | 66 | return SFE_IRCAM_BAD_CHANNELS ; |
157 | 364 | } ; |
158 | | |
159 | 364 | psf->endian = SF_ENDIAN_BIG ; |
160 | 1.10k | } ; |
161 | | |
162 | 1.10k | psf_log_printf (psf, "marker: 0x%X\n", marker) ; |
163 | | |
164 | 1.10k | psf->sf.samplerate = psf_lrintf (samplerate) ; |
165 | | |
166 | 1.10k | psf_log_printf (psf, " Sample Rate : %d\n" |
167 | 1.10k | " Channels : %d\n" |
168 | 1.10k | " Encoding : %X => %s\n", |
169 | 1.10k | psf->sf.samplerate, psf->sf.channels, encoding, get_encoding_str (encoding)) ; |
170 | | |
171 | 1.10k | switch (encoding) |
172 | 1.10k | { case IRCAM_PCM_16 : |
173 | 108 | psf->bytewidth = 2 ; |
174 | 108 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
175 | | |
176 | 108 | psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_16 ; |
177 | 108 | break ; |
178 | | |
179 | 27 | case IRCAM_PCM_32 : |
180 | 27 | psf->bytewidth = 4 ; |
181 | 27 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
182 | | |
183 | 27 | psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_PCM_32 ; |
184 | 27 | break ; |
185 | | |
186 | 162 | case IRCAM_FLOAT : |
187 | 162 | psf->bytewidth = 4 ; |
188 | 162 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
189 | | |
190 | 162 | psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_FLOAT ; |
191 | 162 | break ; |
192 | | |
193 | 126 | case IRCAM_ALAW : |
194 | 126 | psf->bytewidth = 1 ; |
195 | 126 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
196 | | |
197 | 126 | psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ALAW ; |
198 | 126 | break ; |
199 | | |
200 | 100 | case IRCAM_ULAW : |
201 | 100 | psf->bytewidth = 1 ; |
202 | 100 | psf->blockwidth = (sf_count_t) psf->sf.channels * psf->bytewidth ; |
203 | | |
204 | 100 | psf->sf.format = SF_FORMAT_IRCAM | SF_FORMAT_ULAW ; |
205 | 100 | break ; |
206 | | |
207 | 586 | default : |
208 | 586 | error = SFE_IRCAM_UNKNOWN_FORMAT ; |
209 | 586 | break ; |
210 | 1.10k | } ; |
211 | | |
212 | 1.10k | if (psf->endian == SF_ENDIAN_BIG) |
213 | 364 | psf->sf.format |= SF_ENDIAN_BIG ; |
214 | 745 | else |
215 | 745 | psf->sf.format |= SF_ENDIAN_LITTLE ; |
216 | | |
217 | 1.10k | if (error) |
218 | 586 | return error ; |
219 | | |
220 | 523 | psf->dataoffset = IRCAM_DATA_OFFSET ; |
221 | 523 | psf->datalength = psf->filelength - psf->dataoffset ; |
222 | | |
223 | 523 | if (psf->sf.frames == 0 && psf->blockwidth) |
224 | 518 | psf->sf.frames = psf->datalength / psf->blockwidth ; |
225 | | |
226 | 523 | psf_log_printf (psf, " Samples : %d\n", psf->sf.frames) ; |
227 | | |
228 | 523 | psf_binheader_readf (psf, "p", IRCAM_DATA_OFFSET) ; |
229 | | |
230 | 523 | return 0 ; |
231 | 1.10k | } /* ircam_read_header */ |
232 | | |
233 | | static int |
234 | | ircam_close (SF_PRIVATE *psf) |
235 | 523 | { |
236 | 523 | psf_log_printf (psf, "close\n") ; |
237 | | |
238 | 523 | return 0 ; |
239 | 523 | } /* ircam_close */ |
240 | | |
241 | | static int |
242 | | ircam_write_header (SF_PRIVATE *psf, int UNUSED (calc_length)) |
243 | 0 | { int encoding ; |
244 | 0 | float samplerate ; |
245 | 0 | sf_count_t current ; |
246 | |
|
247 | 0 | if (psf->pipeoffset > 0) |
248 | 0 | return 0 ; |
249 | | |
250 | 0 | current = psf_ftell (psf) ; |
251 | | |
252 | | /* This also sets psf->endian. */ |
253 | 0 | encoding = get_encoding (SF_CODEC (psf->sf.format)) ; |
254 | |
|
255 | 0 | if (encoding == 0) |
256 | 0 | return SFE_BAD_OPEN_FORMAT ; |
257 | | |
258 | | /* Reset the current header length to zero. */ |
259 | 0 | psf->header.ptr [0] = 0 ; |
260 | 0 | psf->header.indx = 0 ; |
261 | |
|
262 | 0 | if (psf->is_pipe == SF_FALSE) |
263 | 0 | psf_fseek (psf, 0, SEEK_SET) ; |
264 | |
|
265 | 0 | samplerate = psf->sf.samplerate ; |
266 | |
|
267 | 0 | switch (psf->endian) |
268 | 0 | { case SF_ENDIAN_BIG : |
269 | 0 | psf_binheader_writef (psf, "Emf", BHWm (IRCAM_02B_MARKER), BHWf (samplerate)) ; |
270 | 0 | psf_binheader_writef (psf, "E44", BHW4 (psf->sf.channels), BHW4 (encoding)) ; |
271 | 0 | break ; |
272 | | |
273 | 0 | case SF_ENDIAN_LITTLE : |
274 | 0 | psf_binheader_writef (psf, "emf", BHWm (IRCAM_03L_MARKER), BHWf (samplerate)) ; |
275 | 0 | psf_binheader_writef (psf, "e44", BHW4 (psf->sf.channels), BHW4 (encoding)) ; |
276 | 0 | break ; |
277 | | |
278 | 0 | default : return SFE_BAD_OPEN_FORMAT ; |
279 | 0 | } ; |
280 | |
|
281 | 0 | psf_binheader_writef (psf, "z", BHWz ((size_t) (IRCAM_DATA_OFFSET - psf->header.indx))) ; |
282 | | |
283 | | /* Header construction complete so write it out. */ |
284 | 0 | psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ; |
285 | |
|
286 | 0 | if (psf->error) |
287 | 0 | return psf->error ; |
288 | | |
289 | 0 | if (current > 0) |
290 | 0 | psf_fseek (psf, current, SEEK_SET) ; |
291 | |
|
292 | 0 | return psf->error ; |
293 | 0 | } /* ircam_write_header */ |
294 | | |
295 | | static int |
296 | | get_encoding (int subformat) |
297 | 0 | { switch (subformat) |
298 | 0 | { case SF_FORMAT_PCM_16 : return IRCAM_PCM_16 ; |
299 | 0 | case SF_FORMAT_PCM_32 : return IRCAM_PCM_32 ; |
300 | | |
301 | 0 | case SF_FORMAT_FLOAT : return IRCAM_FLOAT ; |
302 | | |
303 | 0 | case SF_FORMAT_ULAW : return IRCAM_ULAW ; |
304 | 0 | case SF_FORMAT_ALAW : return IRCAM_ALAW ; |
305 | | |
306 | 0 | default : break ; |
307 | 0 | } ; |
308 | |
|
309 | 0 | return 0 ; |
310 | 0 | } /* get_encoding */ |
311 | | |
312 | | static const char* |
313 | | get_encoding_str (int encoding) |
314 | 1.10k | { switch (encoding) |
315 | 1.10k | { case IRCAM_PCM_16 : return "16 bit PCM" ; |
316 | 162 | case IRCAM_FLOAT : return "32 bit float" ; |
317 | 126 | case IRCAM_ALAW : return "A law" ; |
318 | 100 | case IRCAM_ULAW : return "u law" ; |
319 | 27 | case IRCAM_PCM_32 : return "32 bit PCM" ; |
320 | 1.10k | } ; |
321 | 586 | return "Unknown encoding" ; |
322 | 1.10k | } /* get_encoding_str */ |
323 | | |