(Metux) autogen.sh: not running ./configure anymore (breaks certain distro builders)
[mirror-ossqm-audiofile.git] / libaudiofile / ircam.c
blob0e0e5dd58d2772937295d4ef978efd9b9adafaf7
1 /*
2 Audio File Library
3 Copyright (C) 2001, Silicon Graphics, Inc.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307 USA.
22 ircam.c
24 This file contains routines for parsing Berkeley/IRCAM/CARL
25 format files.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include <assert.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "afinternal.h"
38 #include "audiofile.h"
39 #include "util.h"
40 #include "byteorder.h"
41 #include "setup.h"
42 #include "track.h"
43 #include "marker.h"
45 #include "ircam.h"
48 Here ircam_mips_magic refers to little-endian MIPS, not SGI IRIX,
49 which uses big-endian MIPS.
51 const uint8_t _af_ircam_vax_magic[4] = {0x64, 0xa3, 0x01, 0x00},
52 _af_ircam_sun_magic[4] = {0x64, 0xa3, 0x02, 0x00},
53 _af_ircam_mips_magic[4] = {0x64, 0xa3, 0x03, 0x00},
54 _af_ircam_next_magic[4] = {0x64, 0xa3, 0x04, 0x00};
56 _AFfilesetup _af_ircam_default_filesetup =
58 _AF_VALID_FILESETUP, /* valid */
59 AF_FILE_IRCAM, /* fileFormat */
60 true, /* trackSet */
61 true, /* instrumentSet */
62 true, /* miscellaneousSet */
63 1, /* trackCount */
64 NULL, /* tracks */
65 0, /* instrumentCount */
66 NULL, /* instruments */
67 0, /* miscellaneousCount */
68 NULL /* miscellaneous */
71 bool _af_ircam_recognize (AFvirtualfile *fh)
73 uint8_t buffer[4];
75 af_fseek(fh, 0, SEEK_SET);
77 if (af_fread(buffer, 4, 1, fh) != 1)
78 return false;
80 /* Check to see if the file's magic number matches. */
81 if (memcmp(buffer, _af_ircam_vax_magic, 4) == 0 ||
82 memcmp(buffer, _af_ircam_sun_magic, 4) == 0 ||
83 memcmp(buffer, _af_ircam_mips_magic, 4) == 0 ||
84 memcmp(buffer, _af_ircam_next_magic, 4) == 0)
86 return true;
89 return false;
92 AFfilesetup _af_ircam_complete_setup (AFfilesetup setup)
94 _TrackSetup *track;
96 if (setup->trackSet && setup->trackCount != 1)
98 _af_error(AF_BAD_NUMTRACKS, "BICSF file must have 1 track");
99 return AF_NULL_FILESETUP;
102 track = &setup->tracks[0];
104 if (track->sampleFormatSet)
106 if (track->f.sampleFormat == AF_SAMPFMT_UNSIGNED)
108 _af_error(AF_BAD_SAMPFMT,
109 "BICSF format does not support unsigned data");
110 return AF_NULL_FILESETUP;
113 if (track->f.sampleFormat == AF_SAMPFMT_TWOSCOMP &&
114 track->f.sampleWidth != 16)
116 _af_error(AF_BAD_WIDTH,
117 "BICSF format supports only 16-bit width for "
118 "two's complement audio data");
119 return AF_NULL_FILESETUP;
122 if (track->f.sampleFormat == AF_SAMPFMT_DOUBLE)
124 _af_error(AF_BAD_SAMPFMT,
125 "BICSF format does not support "
126 "double-precision floating-point data");
127 return AF_NULL_FILESETUP;
131 if (track->rateSet && track->f.sampleRate <= 0.0)
133 _af_error(AF_BAD_RATE,
134 "invalid sample rate %.30g for BICSF file",
135 track->f.sampleRate);
136 return AF_NULL_FILESETUP;
139 if (track->channelCountSet && track->f.channelCount != 1 &&
140 track->f.channelCount != 2 && track->f.channelCount != 4)
142 _af_error(AF_BAD_CHANNELS,
143 "invalid channel count (%d) for BICSF format "
144 "(1, 2, or 4 channels only)",
145 track->f.channelCount);
146 return AF_NULL_FILESETUP;
149 if (track->compressionSet &&
150 track->f.compressionType != AF_COMPRESSION_NONE)
152 _af_error(AF_BAD_NOT_IMPLEMENTED,
153 "BICSF format does not support compression");
154 return AF_NULL_FILESETUP;
157 if (track->aesDataSet)
159 _af_error(AF_BAD_FILESETUP, "BICSF file cannot have AES data");
160 return AF_NULL_FILESETUP;
163 if (track->markersSet && track->markerCount != 0)
165 _af_error(AF_BAD_NUMMARKS, "BICSF format does not support markers");
166 return AF_NULL_FILESETUP;
169 if (setup->instrumentSet && setup->instrumentCount != 0)
171 _af_error(AF_BAD_NUMINSTS, "BICSF format does not support instruments");
172 return AF_NULL_FILESETUP;
175 /* XXXmpruett: We don't support miscellaneous chunks for now. */
176 if (setup->miscellaneousSet && setup->miscellaneousCount != 0)
178 _af_error(AF_BAD_NOT_IMPLEMENTED, "BICSF format does not currently support miscellaneous chunks");
179 return AF_NULL_FILESETUP;
182 return _af_filesetup_copy(setup, &_af_ircam_default_filesetup, true);
185 status _af_ircam_read_init (AFfilesetup setup, AFfilehandle handle)
187 _Track *track;
188 uint8_t magic[4];
189 float rate;
190 uint32_t channels;
191 uint32_t packMode;
193 float maxAmp = 1.0;
195 bool isSwapped, isLittleEndian;
197 handle->instruments = NULL;
198 handle->instrumentCount = 0 ;
199 handle->miscellaneous = NULL;
200 handle->miscellaneousCount = 0;
202 handle->tracks = NULL;
203 handle->trackCount = 1;
205 af_fseek(handle->fh, 0, SEEK_SET);
207 if (af_fread(magic, 4, 1, handle->fh) != 1)
209 _af_error(AF_BAD_READ, "Could not read BICSF file header");
210 return AF_FAIL;
213 if (memcmp(magic, _af_ircam_vax_magic, 4) != 0 &&
214 memcmp(magic, _af_ircam_sun_magic, 4) != 0 &&
215 memcmp(magic, _af_ircam_mips_magic, 4) != 0 &&
216 memcmp(magic, _af_ircam_next_magic, 4) != 0)
218 _af_error(AF_BAD_FILEFMT,
219 "file is not a BICSF file (bad magic number)");
220 return AF_FAIL;
224 If the file's magic number is that for VAX or MIPS,
225 the file is little endian.
227 isLittleEndian = (memcmp(magic, _af_ircam_vax_magic, 4) == 0 ||
228 memcmp(magic, _af_ircam_mips_magic, 4) == 0);
230 #ifdef WORDS_BIGENDIAN
231 isSwapped = isLittleEndian;
232 #else
233 isSwapped = !isLittleEndian;
234 #endif
236 af_fread(&rate, 4, 1, handle->fh);
237 af_fread(&channels, 4, 1, handle->fh);
238 af_fread(&packMode, 4, 1, handle->fh);
240 if (isSwapped)
242 rate = _af_byteswap_float32(rate);
243 channels = _af_byteswap_int32(channels);
244 packMode = _af_byteswap_int32(packMode);
247 if ((handle->tracks = _af_track_new()) == NULL)
248 return AF_FAIL;
250 track = &handle->tracks[0];
252 track->f.sampleRate = rate;
253 track->f.compressionType = AF_COMPRESSION_NONE;
255 switch (packMode)
257 case SF_SHORT:
258 track->f.sampleFormat = AF_SAMPFMT_TWOSCOMP;
259 track->f.sampleWidth = 16;
260 break;
261 case SF_FLOAT:
262 track->f.sampleFormat = AF_SAMPFMT_FLOAT;
263 track->f.sampleWidth = 32;
264 break;
265 default:
266 _af_error(AF_BAD_NOT_IMPLEMENTED,
267 "BICSF data format %d not supported", packMode);
268 return AF_FAIL;
271 track->f.channelCount = channels;
272 if (channels != 1 && channels != 2 && channels != 4)
274 _af_error(AF_BAD_FILEFMT, "invalid channel count (%d) "
275 "for BICSF format (1, 2, or 4 only)",
276 channels);
277 return AF_FAIL;
280 if (isLittleEndian)
281 track->f.byteOrder = AF_BYTEORDER_LITTLEENDIAN;
282 else
283 track->f.byteOrder = AF_BYTEORDER_BIGENDIAN;
285 if (_af_set_sample_format(&track->f, track->f.sampleFormat,
286 track->f.sampleWidth) == AF_FAIL)
288 return AF_FAIL;
291 if (track->f.sampleFormat == AF_SAMPFMT_FLOAT)
292 track->f.pcm.slope = maxAmp;
294 track->data_size = af_flength(handle->fh) - SIZEOF_BSD_HEADER;
297 Only uncompressed data formats are supported for IRCAM
298 files right now. The following line would need to be
299 changed if compressed data formats were supported.
301 track->totalfframes = track->data_size /
302 (int) _af_format_frame_size(&track->f, false);
304 track->fpos_first_frame = SIZEOF_BSD_HEADER;
305 track->nextfframe = 0;
306 track->fpos_next_frame = track->fpos_first_frame;
308 handle->formatSpecific = NULL;
310 return AF_SUCCEED;