|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.sciss.io.AudioFile
public class AudioFile
The AudioFile
allows reading and writing
of sound files. It wraps a RandomAccessFile
and delegates the I/O to subclasses which deal with
the specific sample format and endianess.
Currently supported formats are: AIFF, IRCAM, NeXT/Sun (.au), WAVE, and Wave64. Supported resolutions are 8/16/24/32 bit integer and 32/64 bit floating point. However not all audio formats support all bit depths.
Not all format combinations are supported, for example the rather exotic little-endian AIFF, but also little-endian SND, WAVE 8-bit.
In order to simplify communication with CSound, raw output files are supported, raw input files however are not recognized.
To create a new AudioFile
you call
one of its static methods openAsRead
or
openAsWrite
. The format description
is handled by an AudioFileDescr
object.
This object also contains information about what special
tags are read/written for which format. For example,
AIFF can read/write markers, and application-specific
chunk, and a gain tag. WAVE can read/write markers and
regions, and a gain tag, etc.
The AudioFile
implements the generic
interface InterleavedStreamFile
(which
is likely to be modified in the future) to allow
clients to deal more easily with different sorts
of streaming files, not just audio files.
AudioFileDescr
Field Summary | |
---|---|
protected AudioFileDescr |
afd
|
protected java.nio.ByteBuffer |
byteBuf
|
protected int |
bytesPerFrame
|
protected int |
channels
|
protected java.nio.channels.FileChannel |
fch
|
protected int |
frameBufCapacity
|
protected java.io.RandomAccessFile |
raf
|
Method Summary | |
---|---|
void |
cleanUp()
Flushes and closes the file. |
void |
close()
Flushes and closes the file |
void |
copyFrames(InterleavedStreamFile target,
long length)
Copies sample frames from a source sound file to a target file (either another sound file or any other class implementing the InterleavedStreamFile interface). |
void |
flush()
Flushes pending buffer content, and updates the sound file header information (i.e. length fields). |
int |
getChannelNum()
Returns the number of channels in the file. |
AudioFileDescr |
getDescr()
Returns a description of the audio file's format. |
java.io.File |
getFile()
Returns the file that was used to open the audio file. |
long |
getFrameNum()
Returns the number of frames in the file. |
long |
getFramePosition()
Returns the current file pointer in sample frames |
protected static java.lang.String |
getResourceString(java.lang.String key)
|
static AudioFile |
openAsRead(java.io.File f)
Opens an audio file for reading. |
static AudioFile |
openAsWrite(AudioFileDescr afd)
Opens an audio file for reading/writing. |
void |
readAppCode()
Reads application specific code into the audio file description if there is such code. |
void |
readFrames(float[][] data,
int offset,
int length)
Reads sample frames from the current position |
void |
readMarkers()
Reads markers into the audio file description if there are any. |
static int |
retrieveType(java.io.File f)
Determines the type of audio file. |
void |
seekFrame(long frame)
Moves the file pointer to a specific frame. |
void |
setFrameNum(long frame)
|
void |
truncate()
Truncates the file to the size represented by the current file position. |
void |
writeFrames(float[][] data,
int offset,
int length)
Writes sample frames to the file starting at the current position. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final java.io.RandomAccessFile raf
protected final java.nio.channels.FileChannel fch
protected AudioFileDescr afd
protected java.nio.ByteBuffer byteBuf
protected int bytesPerFrame
protected int frameBufCapacity
protected int channels
Method Detail |
---|
public static AudioFile openAsRead(java.io.File f) throws java.io.IOException
f
- the path name of the file
AudioFile
object
whose header is already parsed and can
be obtained through the getDescr
method.
java.io.IOException
- if the file was not found, could not be read
or has an unknown or unsupported formatpublic static AudioFile openAsWrite(AudioFileDescr afd) throws java.io.IOException
file
field of the provided AudioFileDescr
.
If a file denoted by this path already exists, it will be
deleted before opening.
Note that the initial audio file header is written immediately.
Special tags for the header thus need to be set in the AudioFileDescr
before calling this method, including markers and regions. It is not
possible to write markers and regions after the file has been opened
(since the header size has to be constant).
afd
- format and resolution of the new audio file.
the header is immediatly written to the harddisc
java.io.IOException
- if the file could not be created or the
format is unsupportedpublic static int retrieveType(java.io.File f) throws java.io.IOException
f
- the path name of the file
AudioFileDescr
,
e.g. TYPE_AIFF
. Returns TYPE_UNKNOWN
if the file could not be identified.
java.io.IOException
- if the file could not be readpublic AudioFileDescr getDescr()
getType
), channels
,
bitsPerSample
, sampleFormat
,
rate
and length
.
AudioFileDescr
describing
this audio file.length
field
for a writable file.public java.io.File getFile()
getFile
in interface InterleavedStreamFile
File
that was used in
the static constructor methods. Can be used
to query the pathname or to delete the file after
it has been closedpublic void seekFrame(long frame) throws java.io.IOException
seekFrame
in interface InterleavedStreamFile
frame
- the sample frame which should be
the new file position. this is really
the sample index and not the physical file pointer.
java.io.IOException
- when a seek error occurs or you try to
seek past the file's end.public void flush() throws java.io.IOException
flush
in interface InterleavedStreamFile
java.io.IOException
public long getFramePosition() throws java.io.IOException
getFramePosition
in interface InterleavedStreamFile
java.io.IOException
- when the position cannot be queriedpublic void readFrames(float[][] data, int offset, int length) throws java.io.IOException
readFrames
in interface InterleavedStreamFile
data
- buffer to hold the frames read from harddisc.
the samples will be deinterleaved such that
data[0][] holds the first channel, data[1][]
holds the second channel etc.
; it is allowed to have null arrays in the data
(e.g. data[0] == null), in which case these channels
are skipped when readingoffset
- offset in the buffer in sample frames, such
that he first frame of the first channel will
be placed in data[0][offset] etc.length
- number of continuous frames to read.
java.io.IOException
- if a read error or end-of-file occurs.public void writeFrames(float[][] data, int offset, int length) throws java.io.IOException
length
field of the internal AudioFileDescr
is updated.
Since you get a reference from getDescr
and not
a copy, using this reference to the description will automatically
give you the correct file length.
writeFrames
in interface InterleavedStreamFile
data
- buffer holding the frames to write to harddisc.
the samples must be deinterleaved such that
data[0][] holds the first channel, data[1][]
holds the second channel etc.offset
- offset in the buffer in sample frames, such
that he first frame of the first channel will
be read from data[0][offset] etc.length
- number of continuous frames to write.
java.io.IOException
- if a write error occurs.public long getFrameNum() throws java.io.IOException
getFrameNum
in interface InterleavedStreamFile
java.io.IOException
- this is never thrown
but declared as of the InterleavedStreamFile
interfacepublic void setFrameNum(long frame) throws java.io.IOException
setFrameNum
in interface InterleavedStreamFile
java.io.IOException
public int getChannelNum()
getChannelNum
in interface InterleavedStreamFile
public void truncate() throws java.io.IOException
seekFrame
to a location before the end of the file.
The header information is immediately updated.
truncate
in interface InterleavedStreamFile
java.io.IOException
- if truncation failspublic void copyFrames(InterleavedStreamFile target, long length) throws java.io.IOException
InterleavedStreamFile
interface).
Both files must have the same number of channels.
copyFrames
in interface InterleavedStreamFile
target
- to file to copy to from this audio filelength
- the number of frames to copy. Reading
and writing begins at the current positions
of both files.
java.io.IOException
- if a read or write error occurspublic void close() throws java.io.IOException
close
in interface InterleavedStreamFile
java.io.IOException
- if an error occurs during buffer flush
or closing the file.public void cleanUp()
close()
, this does not
throw any exceptions but simply ignores any errors.
close()
public void readMarkers() throws java.io.IOException
KEY_MARKERS
property of the afd, if markers are available. It sets
the KEY_LOOP
property if a loop span is available.
java.io.IOException
- if a read or parsing error occursgetDescr()
,
AudioFileDescr.KEY_MARKERS
,
AudioFileDescr.KEY_LOOP
public void readAppCode() throws java.io.IOException
KEY_APPCODE
property of the afd. The caller can check the appCode
field of the afd to ensure that potential app code is relevant to it.
java.io.IOException
- if a read or parsing error occursgetDescr()
,
AudioFileDescr.KEY_APPCODE
,
AudioFileDescr.appCode
protected static final java.lang.String getResourceString(java.lang.String key)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |