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.
25 This file implements many of the main interface routines of the
39 #include "audiofile.h"
41 #include "afinternal.h"
42 #include "afinternal.h"
46 extern const _Unit _af_units
[];
48 AFfileoffset
afGetDataOffset (AFfilehandle file
, int trackid
)
52 if (!_af_filehandle_ok(file
))
55 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
58 return track
->fpos_first_frame
;
61 AFfileoffset
afGetTrackBytes (AFfilehandle file
, int trackid
)
65 if (!_af_filehandle_ok(file
))
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
)
84 if (!_af_filehandle_ok(file
))
87 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
90 return _af_format_frame_size(&track
->f
, stretch3to4
);
93 float afGetVirtualFrameSize (AFfilehandle file
, int trackid
, int stretch3to4
)
97 if (!_af_filehandle_ok(file
))
100 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
103 return _af_format_frame_size(&track
->v
, stretch3to4
);
106 AFframecount
afSeekFrame (AFfilehandle file
, int trackid
, AFframecount frame
)
110 if (!_af_filehandle_ok(file
))
113 if (!_af_filehandle_can_read(file
))
116 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
119 if (track
->ms
.modulesdirty
)
120 if (_AFsetupmodules(file
, track
) != AF_SUCCEED
)
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
144 track
->nextvframe
= frame
;
146 if (_AFsetupmodules(file
, track
) != AF_SUCCEED
)
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
))
164 if (NULL
== (currentTrack
= _af_filehandle_get_track(handle
, track
)))
167 if (byteorder
!= AF_BYTEORDER_BIGENDIAN
&&
168 byteorder
!= AF_BYTEORDER_LITTLEENDIAN
)
170 _af_error(AF_BAD_BYTEORDER
, "invalid byte order %d", byteorder
);
174 currentTrack
->v
.byteOrder
= byteorder
;
175 currentTrack
->ms
.modulesdirty
= true;
180 int afGetByteOrder (AFfilehandle handle
, int track
)
182 _Track
*currentTrack
;
184 if (!_af_filehandle_ok(handle
))
187 if ((currentTrack
= _af_filehandle_get_track(handle
, track
)) == NULL
)
190 return (currentTrack
->f
.byteOrder
);
193 int afGetVirtualByteOrder (AFfilehandle handle
, int track
)
195 _Track
*currentTrack
;
197 if (!_af_filehandle_ok(handle
))
200 if ((currentTrack
= _af_filehandle_get_track(handle
, track
)) == NULL
)
203 return (currentTrack
->v
.byteOrder
);
206 AFframecount
afGetFrameCount (AFfilehandle file
, int trackid
)
210 if (!_af_filehandle_ok(file
))
213 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
216 if (track
->ms
.modulesdirty
)
218 if (_AFsetupmodules(file
, track
) != AF_SUCCEED
)
222 return track
->totalvframes
;
225 double afGetRate (AFfilehandle file
, int trackid
)
229 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
232 return track
->f
.sampleRate
;
235 int afGetChannels (AFfilehandle file
, int trackid
)
239 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
242 return track
->f
.channelCount
;
245 void afGetSampleFormat (AFfilehandle file
, int trackid
, int *sampleFormat
, int *sampleWidth
)
249 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
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
)
263 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
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
)
278 if (!_af_filehandle_ok(file
))
281 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
284 if (_af_set_sample_format(&track
->v
, sampleFormat
, sampleWidth
) == AF_FAIL
)
287 track
->ms
.modulesdirty
= true;
292 /* XXXmpruett fix the version */
293 int afGetFileFormat (AFfilehandle file
, int *version
)
295 if (!_af_filehandle_ok(file
))
300 if (_af_units
[file
->fileFormat
].getversion
)
301 *version
= _af_units
[file
->fileFormat
].getversion(file
);
306 return file
->fileFormat
;
309 int afSetVirtualChannels (AFfilehandle file
, int trackid
, int channelCount
)
313 if (!_af_filehandle_ok(file
))
316 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
319 track
->v
.channelCount
= channelCount
;
320 track
->ms
.modulesdirty
= true;
322 if (track
->channelMatrix
)
323 free(track
->channelMatrix
);
324 track
->channelMatrix
= NULL
;
329 double afGetVirtualRate (AFfilehandle file
, int trackid
)
333 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
336 return track
->v
.sampleRate
;
339 int afSetVirtualRate (AFfilehandle file
, int trackid
, double rate
)
343 if (!_af_filehandle_ok(file
))
346 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
351 _af_error(AF_BAD_RATE
, "invalid sampling rate %.30g", rate
);
355 track
->v
.sampleRate
= rate
;
356 track
->ms
.modulesdirty
= true;
361 void afSetChannelMatrix (AFfilehandle file
, int trackid
, double* matrix
)
365 if (!_af_filehandle_ok(file
))
368 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
371 if (track
->channelMatrix
!= NULL
)
372 free(track
->channelMatrix
);
373 track
->channelMatrix
= NULL
;
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
)
392 if ((track
= _af_filehandle_get_track(file
, trackid
)) == NULL
)
395 return track
->v
.channelCount
;