javax.sound.sampled
Class AudioInputStream
- Closeable
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:
- obtain an
audio input stream from an external audio file, stream, or URL
- write an external file from an audio input stream
- convert an audio input stream to a different audio format
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.
|
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.
|
clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait |
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.
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.
stream - the stream on which this AudioInputStream
object is basedformat - the format of this stream's audio datalength - the length in sample frames of the data in this stream
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.
line - the target data line from which this stream obtains its data.
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.
- available in interface InputStream
- the number of bytes that can be read from this audio input stream without blocking
View More Examples of close()
1: import ;
2: import ;
3: import ;
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: 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 ;
2: import ;
3: import ;
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.
- an audio format object describing this stream's format
View More Examples of getFormat()
1: import ;
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 ;
2: import ;
3: import ;
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.
- the length in sample frames
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.
- mark in interface InputStream
readlimit - the maximum number of bytes that can be read before
the mark position becomes invalid.
View More Examples of mark(int readlimit)
1: import ;
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.
- markSupported in interface InputStream
true if this stream supports the mark
and reset methods; false otherwise
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.
- read in interface InputStream
- the next byte of data, or -1 if the end of the stream is reached
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.
b - the buffer into which the data is read
- 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
View More Examples of read(byte[] b)
1: import ;
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.
b - the buffer into which the data is readoff - the offset, from the beginning of array b, at which
the data will be writtenlen - the maximum number of bytes to read
- 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
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 ;
3: import ;
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 ;
2: import ;
3: import ;
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.
- reset in interface InputStream
View More Examples of reset()
1: import ;
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.
- skip in interface InputStream
n - the requested number of bytes to be skipped
- the actual number of bytes skipped