(Metux) autogen.sh: not running ./configure anymore (breaks certain distro builders)
[mirror-ossqm-audiofile.git] / libaudiofile / format.c
blob6dd9ae6d927ae01b68bd46dc3f0d606a332c90ee
1 /*
2 Audio File Library
3 Copyright (C) 1998-2000, Michael Pruett <michael@68k.org>
4 Copyright (C) 2000, Silicon Graphics, Inc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307 USA.
23 audiofile.c
25 This file implements many of the main interface routines of the
26 Audio File Library.
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
33 #include <assert.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "audiofile.h"
40 #include "util.h"
41 #include "afinternal.h"
42 #include "afinternal.h"
43 #include "units.h"
44 #include "modules.h"
46 extern const _Unit _af_units[];
48 AFfileoffset afGetDataOffset (AFfilehandle file, int trackid)
50 _Track *track;
52 if (!_af_filehandle_ok(file))
53 return -1;
55 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
56 return -1;
58 return track->fpos_first_frame;
61 AFfileoffset afGetTrackBytes (AFfilehandle file, int trackid)
63 _Track *track;
65 if (!_af_filehandle_ok(file))
66 return -1;
68 track = _af_filehandle_get_track(file, trackid);
70 return track->data_size;
74 afGetFrameSize returns the size (in bytes) of a sample frame from
75 the specified track of an audio file.
77 stretch3to4 == true: size which user sees
78 stretch3to4 == false: size used in file
80 float afGetFrameSize (AFfilehandle file, int trackid, int stretch3to4)
82 _Track *track;
84 if (!_af_filehandle_ok(file))
85 return -1;
87 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
88 return -1;
90 return _af_format_frame_size(&track->f, stretch3to4);
93 float afGetVirtualFrameSize (AFfilehandle file, int trackid, int stretch3to4)
95 _Track *track;
97 if (!_af_filehandle_ok(file))
98 return -1;
100 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
101 return -1;
103 return _af_format_frame_size(&track->v, stretch3to4);
106 AFframecount afSeekFrame (AFfilehandle file, int trackid, AFframecount frame)
108 _Track *track;
110 if (!_af_filehandle_ok(file))
111 return -1;
113 if (!_af_filehandle_can_read(file))
114 return -1;
116 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
117 return -1;
119 if (track->ms.modulesdirty)
120 if (_AFsetupmodules(file, track) != AF_SUCCEED)
121 return -1;
123 if (frame < 0)
124 return track->nextvframe;
126 /* Optimize the case of seeking to the current position. */
127 if (frame == track->nextvframe)
128 return track->nextvframe;
130 /* Limit request to the number of frames in the file. */
131 if (track->totalvframes != -1)
132 if (frame > track->totalvframes)
133 frame = track->totalvframes - 1;
136 Now that the modules are not dirty and frame
137 represents a valid virtual frame, we call
138 _AFsetupmodules again after setting track->nextvframe.
140 _AFsetupmodules will look at track->nextvframe and
141 compute track->nextfframe in clever and mysterious
142 ways.
144 track->nextvframe = frame;
146 if (_AFsetupmodules(file, track) != AF_SUCCEED)
147 return -1;
149 return track->nextvframe;
152 AFfileoffset afTellFrame (AFfilehandle file, int trackid)
154 return afSeekFrame(file, trackid, -1);
157 int afSetVirtualByteOrder (AFfilehandle handle, int track, int byteorder)
159 _Track *currentTrack;
161 if (!_af_filehandle_ok(handle))
162 return -1;
164 if (NULL == (currentTrack = _af_filehandle_get_track(handle, track)))
165 return AF_FAIL;
167 if (byteorder != AF_BYTEORDER_BIGENDIAN &&
168 byteorder != AF_BYTEORDER_LITTLEENDIAN)
170 _af_error(AF_BAD_BYTEORDER, "invalid byte order %d", byteorder);
171 return AF_FAIL;
174 currentTrack->v.byteOrder = byteorder;
175 currentTrack->ms.modulesdirty = true;
177 return AF_SUCCEED;
180 int afGetByteOrder (AFfilehandle handle, int track)
182 _Track *currentTrack;
184 if (!_af_filehandle_ok(handle))
185 return -1;
187 if ((currentTrack = _af_filehandle_get_track(handle, track)) == NULL)
188 return -1;
190 return (currentTrack->f.byteOrder);
193 int afGetVirtualByteOrder (AFfilehandle handle, int track)
195 _Track *currentTrack;
197 if (!_af_filehandle_ok(handle))
198 return -1;
200 if ((currentTrack = _af_filehandle_get_track(handle, track)) == NULL)
201 return -1;
203 return (currentTrack->v.byteOrder);
206 AFframecount afGetFrameCount (AFfilehandle file, int trackid)
208 _Track *track;
210 if (!_af_filehandle_ok(file))
211 return -1;
213 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
214 return -1;
216 if (track->ms.modulesdirty)
218 if (_AFsetupmodules(file, track) != AF_SUCCEED)
219 return -1;
222 return track->totalvframes;
225 double afGetRate (AFfilehandle file, int trackid)
227 _Track *track;
229 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
230 return -1;
232 return track->f.sampleRate;
235 int afGetChannels (AFfilehandle file, int trackid)
237 _Track *track;
239 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
240 return -1;
242 return track->f.channelCount;
245 void afGetSampleFormat (AFfilehandle file, int trackid, int *sampleFormat, int *sampleWidth)
247 _Track *track;
249 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
250 return;
252 if (sampleFormat != NULL)
253 *sampleFormat = track->f.sampleFormat;
255 if (sampleFormat != NULL)
256 *sampleWidth = track->f.sampleWidth;
259 void afGetVirtualSampleFormat (AFfilehandle file, int trackid, int *sampleFormat, int *sampleWidth)
261 _Track *track;
263 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
264 return;
266 if (sampleFormat != NULL)
267 *sampleFormat = track->v.sampleFormat;
269 if (sampleFormat != NULL)
270 *sampleWidth = track->v.sampleWidth;
273 int afSetVirtualSampleFormat (AFfilehandle file, int trackid,
274 int sampleFormat, int sampleWidth)
276 _Track *track;
278 if (!_af_filehandle_ok(file))
279 return -1;
281 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
282 return -1;
284 if (_af_set_sample_format(&track->v, sampleFormat, sampleWidth) == AF_FAIL)
285 return -1;
287 track->ms.modulesdirty = true;
289 return 0;
292 /* XXXmpruett fix the version */
293 int afGetFileFormat (AFfilehandle file, int *version)
295 if (!_af_filehandle_ok(file))
296 return -1;
298 if (version != NULL)
300 if (_af_units[file->fileFormat].getversion)
301 *version = _af_units[file->fileFormat].getversion(file);
302 else
303 *version = 0;
306 return file->fileFormat;
309 int afSetVirtualChannels (AFfilehandle file, int trackid, int channelCount)
311 _Track *track;
313 if (!_af_filehandle_ok(file))
314 return -1;
316 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
317 return -1;
319 track->v.channelCount = channelCount;
320 track->ms.modulesdirty = true;
322 if (track->channelMatrix)
323 free(track->channelMatrix);
324 track->channelMatrix = NULL;
326 return 0;
329 double afGetVirtualRate (AFfilehandle file, int trackid)
331 _Track *track;
333 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
334 return -1;
336 return track->v.sampleRate;
339 int afSetVirtualRate (AFfilehandle file, int trackid, double rate)
341 _Track *track;
343 if (!_af_filehandle_ok(file))
344 return -1;
346 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
347 return -1;
349 if (rate < 0)
351 _af_error(AF_BAD_RATE, "invalid sampling rate %.30g", rate);
352 return -1;
355 track->v.sampleRate = rate;
356 track->ms.modulesdirty = true;
358 return 0;
361 void afSetChannelMatrix (AFfilehandle file, int trackid, double* matrix)
363 _Track *track;
365 if (!_af_filehandle_ok(file))
366 return;
368 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
369 return;
371 if (track->channelMatrix != NULL)
372 free(track->channelMatrix);
373 track->channelMatrix = NULL;
375 if (matrix != NULL)
377 int i, size;
379 size = track->v.channelCount * track->f.channelCount;
381 track->channelMatrix = (double *) malloc(size * sizeof (double));
383 for (i = 0; i < size; i++)
384 track->channelMatrix[i] = matrix[i];
388 int afGetVirtualChannels (AFfilehandle file, int trackid)
390 _Track *track;
392 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
393 return -1;
395 return track->v.channelCount;