Class AudioFile
- java.lang.Object
-
- de.sciss.io.AudioFile
-
- All Implemented Interfaces:
InterleavedStreamFile
public class AudioFile extends java.lang.Object implements InterleavedStreamFile
TheAudioFileallows reading and writing of sound files. It wraps aRandomAccessFileand 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
AudioFileyou call one of its static methodsopenAsReadoropenAsWrite. The format description is handled by anAudioFileDescrobject. 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
AudioFileimplements the generic interfaceInterleavedStreamFile(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.- See Also:
AudioFileDescr
-
-
Field Summary
Fields Modifier and Type Field Description protected AudioFileDescrafdprotected java.nio.ByteBufferbyteBufprotected intbytesPerFrameprotected intchannelsprotected java.nio.channels.FileChannelfchprotected intframeBufCapacityprotected java.io.RandomAccessFileraf
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcleanUp()Flushes and closes the file.voidclose()Flushes and closes the filevoidcopyFrames(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 theInterleavedStreamFileinterface).voidflush()Flushes pending buffer content, and updates the sound file header information (i.e.intgetChannelNum()Returns the number of channels in the file.AudioFileDescrgetDescr()Returns a description of the audio file's format.java.io.FilegetFile()Returns the file that was used to open the audio file.longgetFrameNum()Returns the number of frames in the file.longgetFramePosition()Returns the current file pointer in sample framesprotected static java.lang.StringgetResourceString(java.lang.String key)static AudioFileopenAsRead(java.io.File f)Opens an audio file for reading.static AudioFileopenAsWrite(AudioFileDescr afd)Opens an audio file for reading/writing.voidreadAppCode()Reads application specific code into the audio file description if there is such code.voidreadFrames(float[][] data, int offset, int length)Reads sample frames from the current positionvoidreadMarkers()Reads markers into the audio file description if there are any.static intretrieveType(java.io.File f)Determines the type of audio file.voidseekFrame(long frame)Moves the file pointer to a specific frame.voidsetFrameNum(long frame)voidtruncate()Truncates the file to the size represented by the current file position.voidwriteFrames(float[][] data, int offset, int length)Writes sample frames to the file starting at the current position.
-
-
-
Field Detail
-
raf
protected final java.io.RandomAccessFile raf
-
fch
protected final java.nio.channels.FileChannel fch
-
afd
protected AudioFileDescr afd
-
byteBuf
protected java.nio.ByteBuffer byteBuf
-
bytesPerFrame
protected int bytesPerFrame
-
frameBufCapacity
protected int frameBufCapacity
-
channels
protected int channels
-
-
Method Detail
-
openAsRead
public static AudioFile openAsRead(java.io.File f) throws java.io.IOException
Opens an audio file for reading.- Parameters:
f- the path name of the file- Returns:
- a new
AudioFileobject whose header is already parsed and can be obtained through thegetDescrmethod. - Throws:
java.io.IOException- if the file was not found, could not be read or has an unknown or unsupported format
-
openAsWrite
public static AudioFile openAsWrite(AudioFileDescr afd) throws java.io.IOException
Opens an audio file for reading/writing. The pathname is determined by thefilefield of the providedAudioFileDescr. 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
AudioFileDescrbefore 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).- Parameters:
afd- format and resolution of the new audio file. the header is immediatly written to the harddisc- Throws:
java.io.IOException- if the file could not be created or the format is unsupported
-
retrieveType
public static int retrieveType(java.io.File f) throws java.io.IOExceptionDetermines the type of audio file.- Parameters:
f- the path name of the file- Returns:
- the type code as defined in
AudioFileDescr, e.g.TYPE_AIFF. ReturnsTYPE_UNKNOWNif the file could not be identified. - Throws:
java.io.IOException- if the file could not be read
-
getDescr
public AudioFileDescr getDescr()
Returns a description of the audio file's format. Fields which are guaranteed to be filled in, are the type (usegetType),channels,bitsPerSample,sampleFormat,rateandlength. Warning: the returned description is not immutable but should be considered read only, do not modify it. the fields may change dynamically if the file is modified, e.g. thelengthfield for a writable file.- Returns:
- an
AudioFileDescrdescribing this audio file.
-
getFile
public java.io.File getFile()
Returns the file that was used to open the audio file. Note that this simply returns getDescr().file, so it's not a good idea to modify this field after opening the audio file.- Specified by:
getFilein interfaceInterleavedStreamFile- Returns:
- the
Filethat was used in the static constructor methods. Can be used to query the pathname or to delete the file after it has been closed
-
seekFrame
public void seekFrame(long frame) throws java.io.IOExceptionMoves the file pointer to a specific frame.- Specified by:
seekFramein interfaceInterleavedStreamFile- Parameters:
frame- the sample frame which should be the new file position. this is really the sample index and not the physical file pointer.- Throws:
java.io.IOException- when a seek error occurs or you try to seek past the file's end.
-
flush
public void flush() throws java.io.IOExceptionFlushes pending buffer content, and updates the sound file header information (i.e. length fields). Usually you will not have to call this method directly, unless you pause writing for some time and want the file information to appear as accurate as possible.- Specified by:
flushin interfaceInterleavedStreamFile- Throws:
java.io.IOException
-
getFramePosition
public long getFramePosition() throws java.io.IOExceptionReturns the current file pointer in sample frames- Specified by:
getFramePositionin interfaceInterleavedStreamFile- Returns:
- the sample frame index which is the offset for the next read or write operation.
- Throws:
java.io.IOException- when the position cannot be queried
-
readFrames
public void readFrames(float[][] data, int offset, int length) throws java.io.IOExceptionReads sample frames from the current position- Specified by:
readFramesin interfaceInterleavedStreamFile- Parameters:
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.- Throws:
java.io.IOException- if a read error or end-of-file occurs.
-
writeFrames
public void writeFrames(float[][] data, int offset, int length) throws java.io.IOExceptionWrites sample frames to the file starting at the current position. If you write past the previous end of the file, thelengthfield of the internalAudioFileDescris updated. Since you get a reference fromgetDescrand not a copy, using this reference to the description will automatically give you the correct file length.- Specified by:
writeFramesin interfaceInterleavedStreamFile- Parameters:
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.- Throws:
java.io.IOException- if a write error occurs.
-
getFrameNum
public long getFrameNum() throws java.io.IOExceptionReturns the number of frames in the file.- Specified by:
getFrameNumin interfaceInterleavedStreamFile- Returns:
- the number of sample frames in the file. includes pending buffer content
- Throws:
java.io.IOException- this is never thrown but declared as of theInterleavedStreamFileinterface
-
setFrameNum
public void setFrameNum(long frame) throws java.io.IOException- Specified by:
setFrameNumin interfaceInterleavedStreamFile- Throws:
java.io.IOException
-
getChannelNum
public int getChannelNum()
Returns the number of channels in the file.- Specified by:
getChannelNumin interfaceInterleavedStreamFile- Returns:
- the number of channels
-
truncate
public void truncate() throws java.io.IOExceptionTruncates the file to the size represented by the current file position. The file must have been opened in write mode. Truncation occurs only if frames exist beyond the current file position, which implicates that you have set the position usingseekFrameto a location before the end of the file. The header information is immediately updated.- Specified by:
truncatein interfaceInterleavedStreamFile- Throws:
java.io.IOException- if truncation fails
-
copyFrames
public void copyFrames(InterleavedStreamFile target, long length) throws java.io.IOException
Copies sample frames from a source sound file to a target file (either another sound file or any other class implementing theInterleavedStreamFileinterface). Both files must have the same number of channels.- Specified by:
copyFramesin interfaceInterleavedStreamFile- Parameters:
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.- Throws:
java.io.IOException- if a read or write error occurs
-
close
public void close() throws java.io.IOExceptionFlushes and closes the file- Specified by:
closein interfaceInterleavedStreamFile- Throws:
java.io.IOException- if an error occurs during buffer flush or closing the file.
-
cleanUp
public void cleanUp()
Flushes and closes the file. As opposed toclose(), this does not throw any exceptions but simply ignores any errors.- See Also:
close()
-
readMarkers
public void readMarkers() throws java.io.IOExceptionReads markers into the audio file description if there are any. This method sets theKEY_MARKERSproperty of the afd, if markers are available. It sets theKEY_LOOPproperty if a loop span is available.- Throws:
java.io.IOException- if a read or parsing error occurs- See Also:
getDescr(),AudioFileDescr.KEY_MARKERS,AudioFileDescr.KEY_LOOP
-
readAppCode
public void readAppCode() throws java.io.IOExceptionReads application specific code into the audio file description if there is such code. This method sets theKEY_APPCODEproperty of the afd. The caller can check theappCodefield of the afd to ensure that potential app code is relevant to it.- Throws:
java.io.IOException- if a read or parsing error occurs- See Also:
getDescr(),AudioFileDescr.KEY_APPCODE,AudioFileDescr.appCode
-
getResourceString
protected static final java.lang.String getResourceString(java.lang.String key)
-
-