org.xiph.speex.spi

Class Pcm2SpeexAudioInputStream


public class Pcm2SpeexAudioInputStream
extends FilteredAudioInputStream

Converts a PCM 16bits/sample mono audio stream to Ogg Speex
Version:
$Revision: 1.2 $
Author:
Marc Gimpel, Wimba S.A. (mgimpel@horizonwimba.com)

Field Summary

static int
DEFAULT_BUFFER_SIZE
The default size of the buffer (UWB stereo requires at least 2560b).
static int
DEFAULT_CHANNELS
The default number of channels if none is given in the constructor.
static int
DEFAULT_FRAMES_PER_PACKET
The default number of Speex frames that will be put in each Ogg packet.
static int
DEFAULT_PACKETS_PER_OGG_PAGE
The default number of Ogg packets that will be put in each Ogg page.
static int
DEFAULT_QUALITY
The default quality setting for the Speex encoder.
static int
DEFAULT_SAMPLERATE
The default sample rate if none is given in the constructor.
static int
UNKNOWN
Indicates the value is unknown or undetermined.
private int
channels
The number of audio channels (1=mono, 2=stereo).
private String
comment
The comment String that will appear in the Ogg comment packet.
private SpeexEncoder
encoder
The Speex Encoder class.
private boolean
first
Flag to indicate if this is the first time a encode method is called.
private int
frameSize
The size in bytes of PCM data that will be encoded into 1 Speex frame.
private int
framesPerPacket
The number of Speex frames that will be put in each Ogg packet.
private int
granulepos
A counter for the number of PCM samples that have been encoded.
private int
mode
The encoder mode (0=NB, 1=WB, 2=UWB).
private int
oggCount
Pointer in the buffer to the point where Ogg data is added.
private int
packetCount
The number of Ogg packets that have been encoded in the current page.
private int
packetsPerOggPage
The number of Ogg packets that will be put in each Ogg page.
private int
pageCount
The number of Ogg pages that have been written to the stream.
private int
streamSerialNumber
A unique serial number that identifies the Ogg stream.

Fields inherited from class org.xiph.speex.spi.FilteredAudioInputStream

DEFAULT_BUFFER_SIZE, buf, count, in, marklimit, markpos, pos, prebuf, precount, prepos, single

Constructor Summary

Pcm2SpeexAudioInputStream(InputStream in, AudioFormat format, long length)
Constructor
Pcm2SpeexAudioInputStream(InputStream in, AudioFormat format, long length, int size)
Constructor
Pcm2SpeexAudioInputStream(int mode, int quality, InputStream in, AudioFormat format, long length)
Constructor
Pcm2SpeexAudioInputStream(int mode, int quality, InputStream in, 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
fill()
Fills the buffer with more data, taking into account shuffling and other tricks for dealing with marks.
Encoder
getEncoder()
Returns the Encoder.
void
setComment(String comment, boolean appendVersion)
Sets the comment for the Ogg Comment Header.
void
setFramesPerPacket(int framesPerPacket)
Sets the number of Audio Frames that are to be put in every Speex Packet.
void
setPacketsPerOggPage(int packetsPerOggPage)
Sets the number of Speex Packets that are to be put in every Ogg Page.
void
setQuality(int quality)
Sets the Speex encoder Quality.
void
setSerialNumber(int serialNumber)
Sets the Stream Serial Number.
void
setVbr(boolean vbr)
Sets whether of not the encoder is to use VBR.
private void
writeHeaderFrames()
Write the OGG Speex header then the comment header.
private void
writeOggPageChecksum()
Calculate and write the OGG page checksum.
private void
writeOggPageHeader(int packets, int headertype)
Write an OGG page header.

Methods inherited from class org.xiph.speex.spi.FilteredAudioInputStream

available, checkIfStillOpen, close, fill, makeSpace, mark, markSupported, read, read, reset, skip

Field Details

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size of the buffer (UWB stereo requires at least 2560b).
Field Value:
2560

DEFAULT_CHANNELS

public static final int DEFAULT_CHANNELS
The default number of channels if none is given in the constructor.
Field Value:
1

DEFAULT_FRAMES_PER_PACKET

public static final int DEFAULT_FRAMES_PER_PACKET
The default number of Speex frames that will be put in each Ogg packet.
Field Value:
1

DEFAULT_PACKETS_PER_OGG_PAGE

public static final int DEFAULT_PACKETS_PER_OGG_PAGE
The default number of Ogg packets that will be put in each Ogg page.
Field Value:
20

DEFAULT_QUALITY

public static final int DEFAULT_QUALITY
The default quality setting for the Speex encoder.
Field Value:
3

DEFAULT_SAMPLERATE

public static final int DEFAULT_SAMPLERATE
The default sample rate if none is given in the constructor.
Field Value:
8000

UNKNOWN

public static final int UNKNOWN
Indicates the value is unknown or undetermined.
Field Value:
-1

channels

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

comment

private String comment
The comment String that will appear in the Ogg comment packet.

encoder

private SpeexEncoder encoder
The Speex Encoder class.

first

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

frameSize

private int frameSize
The size in bytes of PCM data that will be encoded into 1 Speex frame.

framesPerPacket

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

granulepos

private int granulepos
A counter for the number of PCM samples that have been encoded.

mode

private int mode
The encoder mode (0=NB, 1=WB, 2=UWB).

oggCount

private int oggCount
Pointer in the buffer to the point where Ogg data is added.

packetCount

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

packetsPerOggPage

private int packetsPerOggPage
The number of Ogg packets that will be put in each Ogg page.

pageCount

private int pageCount
The number of Ogg pages that have been written to the stream.

streamSerialNumber

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

Constructor Details

Pcm2SpeexAudioInputStream

public Pcm2SpeexAudioInputStream(InputStream in,
                                 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.

Pcm2SpeexAudioInputStream

public Pcm2SpeexAudioInputStream(InputStream in,
                                 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.

Pcm2SpeexAudioInputStream

public Pcm2SpeexAudioInputStream(int mode,
                                 int quality,
                                 InputStream in,
                                 AudioFormat format,
                                 long length)
Constructor
Parameters:
mode - the mode of the encoder (0=NB, 1=WB, 2=UWB).
quality - the quality setting of the encoder (between 0 and 10).
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.

Pcm2SpeexAudioInputStream

public Pcm2SpeexAudioInputStream(int mode,
                                 int quality,
                                 InputStream in,
                                 AudioFormat format,
                                 long length,
                                 int size)
Constructor
Parameters:
mode - the mode of the encoder (0=NB, 1=WB, 2=UWB).
quality - the quality setting of the encoder (between 0 and 10).
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.

Method Details

available

public int available()
            throws 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) and the result of calling the available method of the underlying inputstream.

Overrides:
available in interface FilteredAudioInputStream
Returns:
the number of bytes that can be read from this inputstream without blocking.

fill

protected void fill()
            throws 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 interface FilteredAudioInputStream

getEncoder

public Encoder getEncoder()
Returns the Encoder.
Returns:
the Encoder.

setComment

public void setComment(String comment,
                       boolean appendVersion)
Sets the comment for the Ogg Comment Header.
Parameters:
comment -
appendVersion -

setFramesPerPacket

public void setFramesPerPacket(int framesPerPacket)
Sets the number of Audio Frames that are to be put in every Speex Packet. An Audio Frame contains 160 samples for narrowband, 320 samples for wideband and 640 samples for ultra-wideband.
Parameters:
framesPerPacket -

setPacketsPerOggPage

public void setPacketsPerOggPage(int packetsPerOggPage)
Sets the number of Speex Packets that are to be put in every Ogg Page. This value must be less than 256 as the value is encoded in 1 byte in the Ogg Header (just before the array of packet sizes)
Parameters:
packetsPerOggPage -

setQuality

public void setQuality(int quality)
Sets the Speex encoder Quality.
Parameters:
quality -

setSerialNumber

public void setSerialNumber(int serialNumber)
Sets the Stream Serial Number. Must not be changed mid stream.
Parameters:
serialNumber -

setVbr

public void setVbr(boolean vbr)
Sets whether of not the encoder is to use VBR.
Parameters:
vbr -

writeHeaderFrames

private void writeHeaderFrames()
Write the OGG Speex header then the comment header.

writeOggPageChecksum

private void writeOggPageChecksum()
Calculate and write the OGG page checksum. This now closes the Ogg page.

writeOggPageHeader

private void writeOggPageHeader(int packets,
                                int headertype)
Write an OGG page header.
Parameters:
packets - - the number of packets in the Ogg Page (must be between 1 and 255)
headertype - - 2=bos: beginning of sream, 4=eos: end of sream

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