org.xiph.speex.spi
Class Speex2PcmAudioInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjavax.sound.sampled.AudioInputStream
          extended byorg.xiph.speex.spi.FilteredAudioInputStream
              extended byorg.xiph.speex.spi.Speex2PcmAudioInputStream

public class Speex2PcmAudioInputStream
extends FilteredAudioInputStream

Converts an Ogg Speex bitstream into a PCM 16bits/sample audio stream.

Version:
$Revision: 1.9 $
Author:
Marc Gimpel, Wimba S.A. (marc@wimba.com)

Nested Class Summary
 
Nested classes inherited from class javax.sound.sampled.AudioInputStream
 
Field Summary
private  Bits bits
          Speex bit packing and unpacking class.
private  int channelCount
          The number of audio channels (1=mono, 2=stereo).
private  float[] decodedData
          Array containing the decoded audio samples.
private  Decoder decoder
          Speex Decoder.
private  boolean first
          Flag to indicate if this is the first time a decode method is called.
private  int frameSize
          The frame size, in samples.
private  int framesPerPacket
          The number of Speex frames that will be put in each Ogg packet.
private  byte[] outputData
          Array containing the decoded audio samples converted into bytes.
private  int packetCount
          The number of Ogg packets that have been decoded in the current page.
private  byte[] packetSizes
          Array containing the sizes of Ogg packets in the current page.
private  int packetsPerOggPage
          The number of Ogg packets that are in each Ogg page.
private  int sampleRate
          The sample rate of the audio, in samples per seconds (Hz).
private  int streamSerialNumber
          A unique serial number that identifies the Ogg stream.
 
Fields inherited from class org.xiph.speex.spi.FilteredAudioInputStream
buf, count, DEFAULT_BUFFER_SIZE, in, marklimit, markpos, pos, prebuf, precount, prepos
 
Fields inherited from class javax.sound.sampled.AudioInputStream
format, frameLength, framePos
 
Fields inherited from class java.io.InputStream
 
Constructor Summary
Speex2PcmAudioInputStream(java.io.InputStream in, javax.sound.sampled.AudioFormat format, long length)
          Constructor
Speex2PcmAudioInputStream(java.io.InputStream in, javax.sound.sampled.AudioFormat format, long length, int size)
          Constructor
 
Method Summary
 int available()
          Returns the number of bytes that can be read from this inputstream without blocking.
protected  void decode(byte[] data, int offset, int len)
          This is where the actual decoding takes place.
protected  void fill()
          Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks.
protected  void initDecoder()
          Initialise the Speex Decoder after reading the Ogg Header.
private static int readInt(byte[] data, int offset)
          Converts Little Endian (Windows) bytes to an int (Java uses Big Endian).
private  void readOggPageHeader()
          Read the Ogg Page header and extract the speex packet sizes.
 
Methods inherited from class org.xiph.speex.spi.FilteredAudioInputStream
close, makeSpace, mark, markSupported, read, read, reset, skip
 
Methods inherited from class javax.sound.sampled.AudioInputStream
getFormat, getFrameLength, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sampleRate

private int sampleRate
The sample rate of the audio, in samples per seconds (Hz).


channelCount

private int channelCount
The number of audio channels (1=mono, 2=stereo).


decodedData

private float[] decodedData
Array containing the decoded audio samples.


outputData

private byte[] outputData
Array containing the decoded audio samples converted into bytes.


bits

private Bits bits
Speex bit packing and unpacking class.


decoder

private Decoder decoder
Speex Decoder.


frameSize

private int frameSize
The frame size, in samples.


framesPerPacket

private int framesPerPacket
The number of Speex frames that will be put in each Ogg packet.


streamSerialNumber

private int streamSerialNumber
A unique serial number that identifies the Ogg stream.


packetsPerOggPage

private int packetsPerOggPage
The number of Ogg packets that are in each Ogg page.


packetCount

private int packetCount
The number of Ogg packets that have been decoded in the current page.


packetSizes

private byte[] packetSizes
Array containing the sizes of Ogg packets in the current page.


first

private boolean first
Flag to indicate if this is the first time a decode method is called.

Constructor Detail

Speex2PcmAudioInputStream

public Speex2PcmAudioInputStream(java.io.InputStream in,
                                 javax.sound.sampled.AudioFormat format,
                                 long length)
Constructor

Parameters:
in - the underlying input stream.
format - the target format of this stream's audio data.
length - the length in sample frames of the data in this stream.

Speex2PcmAudioInputStream

public Speex2PcmAudioInputStream(java.io.InputStream in,
                                 javax.sound.sampled.AudioFormat format,
                                 long length,
                                 int size)
Constructor

Parameters:
in - the underlying input stream.
format - the target format of this stream's audio data.
length - the length in sample frames of the data in this stream.
size - the buffer size.
Throws:
java.lang.IllegalArgumentException - if size <= 0.
Method Detail

initDecoder

protected void initDecoder()
                    throws java.io.IOException
Initialise the Speex Decoder after reading the Ogg Header.
 Ogg Header description
  0 -  3: capture_pattern
       4: stream_structure_version
       5: header_type_flag (2=bos: beginning of sream)
  6 - 13: absolute granule position
 14 - 17: stream serial number
 18 - 21: page sequence no
 22 - 25: page checksum
      26: page_segments
 27 -...: segment_table
 Speex Header description
  0 -  7: speex_string
  8 - 27: speex_version
 28 - 31: speex_version_id
 32 - 35: header_size
 36 - 39: rate
 40 - 43: mode (0=narrowband, 1=wb, 2=uwb)
 44 - 47: mode_bitstream_version
 48 - 51: nb_channels
 52 - 55: bitrate
 56 - 59: frame_size
 60 - 63: vbr
 64 - 67: frames_per_packet
 68 - 71: extra_headers
 72 - 75: reserved1
 76 - 79: reserved2
 

Throws:
java.io.IOException

fill

protected void fill()
             throws java.io.IOException
Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks. Assumes that it is being called by a synchronized method. This method also assumes that all data has already been read in, hence pos > count.

Overrides:
fill in class FilteredAudioInputStream
Throws:
java.io.IOException

decode

protected void decode(byte[] data,
                      int offset,
                      int len)
               throws java.io.StreamCorruptedException
This is where the actual decoding takes place.

Parameters:
data - the array of data to decode.
offset - the offset from which to start reading the data.
len - the length of data to read from the array.
Throws:
java.io.StreamCorruptedException - If the input stream not valid Ogg Speex data.

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from this inputstream without blocking.

The available method of FilteredAudioInputStream returns the sum of the the number of bytes remaining to be read in the buffer (count - pos). The result of calling the available method of the underlying inputstream is not used, as this data will have to be filtered, and thus may not be the same size after processing (although subclasses that do the filtering should override this method and use the amount of data available in the underlying inputstream).

Overrides:
available in class FilteredAudioInputStream
Returns:
the number of bytes that can be read from this inputstream without blocking.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilteredAudioInputStream.in

readOggPageHeader

private void readOggPageHeader()
                        throws java.io.IOException
Read the Ogg Page header and extract the speex packet sizes. Note: the checksum is ignores.

Throws:
java.io.IOException

readInt

private static int readInt(byte[] data,
                           int offset)
Converts Little Endian (Windows) bytes to an int (Java uses Big Endian).

Parameters:
data - the data to read.
offset - the offset from which to start reading.
Returns:
the integer value of the reassembled bytes.


Copyright © 1999-2004 Wimba S.A. All Rights Reserved.