javax.sound.sampled

Class AudioInputStream

Implemented Interfaces:
Closeable

public class AudioInputStream
extends InputStream

An audio input stream is an input stream with a specified audio format and length. The length is expressed in sample frames, not bytes. Several methods are provided for reading a certain number of bytes from the stream, or an unspecified number of bytes. The audio input stream keeps track of the last byte that was read. You can skip over an arbitrary number of bytes to get to a later position for reading. An audio input stream may support marks. When you set a mark, the current position is remembered so that you can return to it later.

The AudioSystem class includes many methods that manipulate AudioInputStream objects. For example, the methods let you:

Since:
1.3
See Also:
AudioSystem, Clip.open(AudioInputStream)

Field Summary

protected AudioFormat
format
The format of the audio data contained in the stream.
protected long
frameLength
This stream's length, in sample frames.
protected long
framePos
The current position in this stream, in sample frames (zero-based).
protected int
frameSize
The size of each frame, in bytes.

Constructor Summary

AudioInputStream(InputStream stream, AudioFormat format, long length)
Constructs an audio input stream that has the requested format and length in sample frames, using audio data from the specified input stream.
AudioInputStream(TargetDataLine line)
Constructs an audio input stream that reads its data from the target data line indicated.

Method Summary

int
available()
Returns the maximum number of bytes that can be read (or skipped over) from this audio input stream without blocking.
void
close()
Closes this audio input stream and releases any system resources associated with the stream.
AudioFormat
getFormat()
Obtains the audio format of the sound data in this audio input stream.
long
getFrameLength()
Obtains the length of the stream, expressed in sample frames rather than bytes.
void
mark(int readlimit)
Marks the current position in this audio input stream.
boolean
markSupported()
Tests whether this audio input stream supports the mark and reset methods.
int
read()
Reads the next byte of data from the audio input stream.
int
read(byte[] b)
Reads some number of bytes from the audio input stream and stores them into the buffer array b.
int
read(byte[] b, int off, int len)
Reads up to a specified maximum number of bytes of data from the audio stream, putting them into the given byte array.
void
reset()
Repositions this audio input stream to the position it had at the time its mark method was last invoked.
long
skip(long n)
Skips over and discards a specified number of bytes from this audio input stream.

Methods inherited from class java.io.InputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

format

protected AudioFormat format
The format of the audio data contained in the stream.

frameLength

protected long frameLength
This stream's length, in sample frames.

framePos

protected long framePos
The current position in this stream, in sample frames (zero-based).

frameSize

protected int frameSize
The size of each frame, in bytes.

Constructor Details

AudioInputStream

public AudioInputStream(InputStream stream,
                        AudioFormat format,
                        long length)
Constructs an audio input stream that has the requested format and length in sample frames, using audio data from the specified input stream.
Parameters:
stream - the stream on which this AudioInputStream object is based
format - the format of this stream's audio data
length - the length in sample frames of the data in this stream
Usages and Demos :

View More Examples of AudioInputStream(InputStream stream,AudioFormat format,long length)
   1:          final InputStream is = new SequenceInputStream(Collections.enumeration(outputList));
   2:          final AudioInputStream ais = new AudioInputStream(is, currentFormat, totBytes / currentFormat.getFrameSize());
   3:          AudioSystem.write(ais, outputType, byteArrayOutputStream);
   4:          }
   5:       catch (IOException ioe)

View Full Code Here
   1:             ByteArrayInputStream inputStream = new ByteArrayInputStream(audioData);
   2:             AudioInputStream audioStream = new AudioInputStream(inputStream, audioFormat, audioData.length/audioFormat.getFrameSize());
   3:             try {
   4:             AudioSystem.write(audioStream, fileType, audioFile);
   5:             } catch (IOException iox) {

View Full Code Here

AudioInputStream

public AudioInputStream(TargetDataLine line)
Constructs an audio input stream that reads its data from the target data line indicated. The format of the stream is the same as that of the target data line, and the length is AudioSystem#NOT_SPECIFIED.
Parameters:
line - the target data line from which this stream obtains its data.

Method Details

available

public int available()
            throws IOException
Returns the maximum number of bytes that can be read (or skipped over) from this audio input stream without blocking. This limit applies only to the next invocation of a read or skip method for this audio input stream; the limit can vary each time these methods are invoked. Depending on the underlying stream,an IOException may be thrown if this stream is closed.
Overrides:
available in interface InputStream
Returns:
the number of bytes that can be read from this audio input stream without blocking
Throws:
IOException - if an input or output error occurs

close

public void close()
            throws IOException
Closes this audio input stream and releases any system resources associated with the stream.
Specified by:
close in interface Closeable
Overrides:
close in interface InputStream
Throws:
IOException - if an input or output error occurs
Usages and Demos :

View More Examples of close()
   1: import java.util.Map;
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:                {
   6:                final AudioInputStream ain = AudioSystem.getAudioInputStream(new ByteArrayInputStream(command.sound));
   7:                try
   8:         ...
   9:                      {
  10:                      ain.close();
  11:                      }
  12:         ...
  13:                      {
  14:                      LOG.warn("Ignoring IOException while trying to close the AudioInputStream", e);

View Full Code Here
   1:     if (sourceLine != null) {
   2:         try { sourceLine.close(); }
   3:         catch (Exception e) { out("Warning: couldn't close output line"); }
   4:         ...
   5:     if (txStream != null) {
   6:         try { txStream.close(); }
   7:         catch (IOException e) { out("Warning: couldn't close transcoder"); }
   8:         ...
   9:     if (sourceStream != null) {
  10:         try { sourceStream.close(); }
  11:         catch (IOException e) { out("Warning: couldn't close source"); }

View Full Code Here
   1: 
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:         SourceDataLine destination;
   6:         AudioInputStream sourceStream;
   7:         
   8:         ...
   9:         destination.stop();
  10:         destination.close();
  11:         
  12:         ...
  13:         try {
  14:             sourceStream.close();

View Full Code Here
   1:         midi = false;
   2:         AudioInputStream ain = AudioSystem.getAudioInputStream(f);
   3:         try {
   4:         ...
   5:         }
   6:         ain.close();
   7:         }
   8:         audioLength = (int)(clip.getMicrosecondLength()/1000);
   9:     }

View Full Code Here
   1: import javax.sound.sampled.AudioFormat;
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:     private Object                    m_dataSource;
   6:     private AudioInputStream        m_audioInputStream;
   7:     private AudioFileFormat            m_audioFileFormat;
   8:         ...
   9:             {
  10:                 m_line.close();
  11:                 openLine();
  12:         ...
  13:                                 m_line.stop();
  14:                                 m_audioInputStream.close();

View Full Code Here

getFormat

public AudioFormat getFormat()
Obtains the audio format of the sound data in this audio input stream.
Returns:
an audio format object describing this stream's format
Usages and Demos :

View More Examples of getFormat()
   1: import javax.sound.sampled.AudioInputStream;
   2: 
   3:         ...
   4: 
   5:     private AudioInputStream audioInputStream;
   6:     private int[][] samplesContainer;
   7:         ...
   8:     public int getNumberOfChannels(){
   9:         int numBytesPerSample = audioInputStream.getFormat().getSampleSizeInBits() / NUM_BITS_PER_BYTE;
  10:         ...
  11:         return audioInputStream.getFormat().getFrameSize() / numBytesPerSample;

View Full Code Here
   1: import java.util.Map;
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:                {
   6:                final AudioInputStream ain = AudioSystem.getAudioInputStream(new ByteArrayInputStream(command.sound));
   7:                try
   8:         ...
   9:                   {
  10:                   final DataLine.Info info = new DataLine.Info(Clip.class, ain.getFormat());
  11:                   final Clip clip = (Clip)AudioSystem.getLine(info);
  12:         ...
  13:                      {
  14:                      LOG.warn("Ignoring IOException while trying to close the AudioInputStream", e);

View Full Code Here
   1:         OyoahaLookAndFeel lnf = (OyoahaLookAndFeel)UIManager.getLookAndFeel();
   2:         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(lnf.getOyoahaTheme().getInputStream(pressed));
   3: 
   4:         ...
   5:         if(audioInputStream!=null)
   6:         {
   7:         ...
   8:           AudioFormat format = audioInputStream.getFormat();
   9:           if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW))
  10:         ...
  11:           }
  12:           DataLine.Info info = new DataLine.Info(Clip.class,  audioInputStream.getFormat(), ((int) audioInputStream.getFrameLength()*format.getFrameSize()));

View Full Code Here
   1: 
   2:     public Sound getSound(AudioInputStream audioStream) {
   3:         if (audioStream == null) {
   4:         ...
   5:         int length = (int)(audioStream.getFrameLength() *
   6:             audioStream.getFormat().getFrameSize());
   7: 
   8:         ...
   9: 
  10:     public AudioInputStream getAudioInputStream(String filename) {
  11:         try {
  12:         ...
  13: 
  14:     public AudioInputStream getAudioInputStream(InputStream is) {

View Full Code Here
   1:         midi = false;
   2:         AudioInputStream ain = AudioSystem.getAudioInputStream(f);
   3:         try {
   4:         ...
   5:         DataLine.Info info =
   6:             new DataLine.Info(Clip.class,ain.getFormat());
   7:         clip = (Clip) AudioSystem.getLine(info);
   8:         clip.open(ain);
   9:         }

View Full Code Here

getFrameLength

public long getFrameLength()
Obtains the length of the stream, expressed in sample frames rather than bytes.
Returns:
the length in sample frames
Usages and Demos :

View More Examples of getFrameLength()
   1:         OyoahaLookAndFeel lnf = (OyoahaLookAndFeel)UIManager.getLookAndFeel();
   2:         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(lnf.getOyoahaTheme().getInputStream(pressed));
   3: 
   4:         ...
   5:         if(audioInputStream!=null)
   6:         {
   7:         ...
   8:           }
   9:           DataLine.Info info = new DataLine.Info(Clip.class,  audioInputStream.getFormat(), ((int) audioInputStream.getFrameLength()*format.getFrameSize()));
  10:           clip2 = (Clip)AudioSystem.getLine(info);
  11:         ...
  12:           }
  13:           DataLine.Info info = new DataLine.Info(Clip.class,  audioInputStream.getFormat(), ((int) audioInputStream.getFrameLength()*format.getFrameSize()));

View Full Code Here
   1: 
   2:     long nFrames = sourceStream.getFrameLength();
   3:     float srcrate = srcfmt.getFrameRate();
   4:     float ttotal =  (nFrames < 1 || srcrate < 0.0) ? -1.0F : ((float)nFrames) / srcrate;
   5:     av.mg.setTimeTotal(ttotal);

View Full Code Here
   1: 
   2:     public Sound getSound(AudioInputStream audioStream) {
   3:         if (audioStream == null) {
   4:         ...
   5: 
   6:         int length = (int)(audioStream.getFrameLength() *
   7:             audioStream.getFormat().getFrameSize());
   8:         ...
   9: 
  10:     public AudioInputStream getAudioInputStream(String filename) {
  11:         try {
  12:         ...
  13: 
  14:     public AudioInputStream getAudioInputStream(InputStream is) {

View Full Code Here

mark

public void mark(int readlimit)
Marks the current position in this audio input stream.
Overrides:
mark in interface InputStream
Parameters:
readlimit - the maximum number of bytes that can be read before the mark position becomes invalid.
Usages and Demos :

View More Examples of mark(int readlimit)
   1: import javax.sound.sampled.AudioInputStream;
   2: 
   3:         ...
   4: 
   5:     private AudioInputStream audioInputStream;
   6:     private int[][] samplesContainer;
   7:         ...
   8:         try {
   9:             audioInputStream.mark(Integer.MAX_VALUE);
  10:             audioInputStream.reset();

View Full Code Here

markSupported

public boolean markSupported()
Tests whether this audio input stream supports the mark and reset methods.
Overrides:
markSupported in interface InputStream
Returns:
true if this stream supports the mark and reset methods; false otherwise
See Also:
mark(int), reset()

read

public int read()
            throws IOException
Reads the next byte of data from the audio input stream. The audio input stream's frame size must be one byte, or an IOException will be thrown.
Overrides:
read in interface InputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached
Throws:
IOException - if an input or output error occurs

read

public int read(byte[] b)
            throws IOException
Reads some number of bytes from the audio input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

This method will always read an integral number of frames. If the length of the array is not an integral number of frames, a maximum of b.length - (b.length % frameSize) bytes will be read.

Parameters:
b - the buffer into which the data is read
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached
Throws:
IOException - if an input or output error occurs
Usages and Demos :

View More Examples of read(byte[] b)
   1: import javax.sound.sampled.AudioInputStream;
   2: 
   3:         ...
   4: 
   5:     private AudioInputStream audioInputStream;
   6:     private int[][] samplesContainer;
   7:         ...
   8:             try {
   9:                 result = audioInputStream.read(bytes);
  10:             } catch (Exception e) {

View Full Code Here

read

public int read(byte[] b,
                int off,
                int len)
            throws IOException
Reads up to a specified maximum number of bytes of data from the audio stream, putting them into the given byte array.

This method will always read an integral number of frames. If len does not specify an integral number of frames, a maximum of len - (len % frameSize) bytes will be read.

Parameters:
b - the buffer into which the data is read
off - the offset, from the beginning of array b, at which the data will be written
len - the maximum number of bytes to read
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached
Throws:
IOException - if an input or output error occurs
Usages and Demos :

View More Examples of read(byte[] b,int off,int len)
   1:     private List playList;
   2:     private AudioInputStream audioInputStream;
   3:     private SourceDataLine sourceDataLine;
   4:         ...
   5:     public Player(AudioFormat format) {
   6:     this((AudioInputStream)null, format);
   7:     }
   8:         ...
   9:                 int cnt;
  10:                 while ((cnt = audioInputStream.read(playBuffer, 0, playBuffer.length)) != -1){
  11:                 if (cnt > 0){

View Full Code Here
   1:     {                                        
   2:         AudioInputStream audioIn = AudioSystem.getAudioInputStream( audioFile );
   3:         
   4:         ...
   5:                 
   6:                 while( (read = audioIn.read( b, read, frameSize-read )) < frameSize && read != -1 );
   7:                 
   8:         ...
   9:     {
  10:         AudioInputStream audioIn = AudioSystem.getAudioInputStream( new BufferedInputStream( audioFile ));
  11:         

View Full Code Here
   1: 
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:         SourceDataLine destination;
   6:         AudioInputStream sourceStream;
   7:         
   8:         ...
   9:         MinuetoSound source;
  10:         AudioInputStream sourceStream;
  11:         SourceDataLine destination;
  12:         ...
  13:                 toTransfer = toTransfer - (toTransfer % destination.getFormat().getFrameSize());
  14:                 transfered = sourceStream.read(tempBuffer, 0, toTransfer);

View Full Code Here
   1: import javax.sound.sampled.AudioFormat;
   2: import javax.sound.sampled.AudioInputStream;
   3: import javax.sound.sampled.AudioSystem;
   4:         ...
   5:     private Object                    m_dataSource;
   6:     private AudioInputStream        m_audioInputStream;
   7:     private AudioFileFormat            m_audioFileFormat;
   8:         ...
   9:                                 {
  10:                                     if (toSkip > abData.length) nBytesRead = m_audioInputStream.read(abData, 0, abData.length);
  11:         ...
  12:                                     else nBytesRead = m_audioInputStream.read(abData, 0, toSkip);

View Full Code Here
   1:   
   2:   public AudioInputStream makeAIS()
   3:   {
   4:         ...
   5:     
   6:     AudioInputStream audioInputStream = 
   7:       new AudioInputStream(bais, audioFileFormat.getFormat(), 
   8:         ...
   9:         numBytesRead = 
  10:           audioInputStream.read(buffer, offset, bufferSize);
  11:           break;

View Full Code Here

reset

public void reset()
            throws IOException
Repositions this audio input stream to the position it had at the time its mark method was last invoked.
Overrides:
reset in interface InputStream
Throws:
IOException - if an input or output error occurs.
Usages and Demos :

View More Examples of reset()
   1: import javax.sound.sampled.AudioInputStream;
   2: 
   3:         ...
   4: 
   5:     private AudioInputStream audioInputStream;
   6:     private int[][] samplesContainer;
   7:         ...
   8:             audioInputStream.mark(Integer.MAX_VALUE);
   9:             audioInputStream.reset();
  10:             byte[] bytes = new byte[(int) (audioInputStream.getFrameLength()) * ((int) audioInputStream.getFormat().getFrameSize())];

View Full Code Here
   1:     
   2:     AudioInputStream audioInputStream = sound.makeAIS();
   3:         ...
   4:     if(audioInputStream == null)
   5:     {
   6:         ...
   7:     try {
   8:       audioInputStream.reset();
   9:     } catch(Exception e) {

View Full Code Here
   1:   
   2:   public AudioInputStream makeAIS()
   3:   {
   4:         ...
   5:     
   6:     AudioInputStream audioInputStream = 
   7:       new AudioInputStream(bais, audioFileFormat.getFormat(), 
   8:         ...
   9:     try {
  10:       audioInputStream.reset();
  11:     catch(Exception e)

View Full Code Here

skip

public long skip(long n)
            throws IOException
Skips over and discards a specified number of bytes from this audio input stream.
Overrides:
skip in interface InputStream
Parameters:
n - the requested number of bytes to be skipped
Returns:
the actual number of bytes skipped
Throws:
IOException - if an input or output error occurs
See Also:
read, available()