AudioFormatpublic class AudioFormat extends Format Encapsulates format information for audio data.
The attributes of an AudioFormat include the sample rate,
bits per sample, and number of channels. |
Fields Summary |
---|
public static final int | BIG_ENDIAN | public static final int | LITTLE_ENDIAN | public static final int | SIGNED | public static final int | UNSIGNED | protected double | sampleRate | protected int | sampleSizeInBits | protected int | channels | protected int | endian | protected int | signed | protected double | frameRate | protected int | frameSizeInBits | public static final String | LINEAR | public static final String | ULAW | public static final String | ULAW_RTP | public static final String | ALAW | public static final String | IMA4 | public static final String | IMA4_MS | public static final String | MSADPCM | public static final String | DVI | public static final String | DVI_RTP | public static final String | G723 | public static final String | G723_RTP | public static final String | G728 | public static final String | G728_RTP | public static final String | G729 | public static final String | G729_RTP | public static final String | G729A | public static final String | G729A_RTP | public static final String | GSM | public static final String | GSM_MS | public static final String | GSM_RTP | public static final String | MAC3 | public static final String | MAC6 | public static final String | TRUESPEECH | public static final String | MSNAUDIO | public static final String | MPEGLAYER3 | public static final String | VOXWAREAC8 | public static final String | VOXWAREAC10 | public static final String | VOXWAREAC16 | public static final String | VOXWAREAC20 | public static final String | VOXWAREMETAVOICE | public static final String | VOXWAREMETASOUND | public static final String | VOXWARERT29H | public static final String | VOXWAREVR12 | public static final String | VOXWAREVR18 | public static final String | VOXWARETQ40 | public static final String | VOXWARETQ60 | public static final String | MSRT24 | public static final String | MPEG | public static final String | MPEG_RTP | public static final String | DOLBYAC3 | double | multiplierFor computing the duration of the sample. | int | margin | boolean | init |
Constructors Summary |
---|
public AudioFormat(String encoding)Constructs an AudioFormat with the specified encoding type.
super(encoding);
| public AudioFormat(String encoding, double sampleRate, int sampleSizeInBits, int channels)Constructs an AudioFormat with the specified attributes.
this(encoding);
this.sampleRate = sampleRate;
this.sampleSizeInBits = sampleSizeInBits;
this.channels = channels;
| public AudioFormat(String encoding, double sampleRate, int sampleSizeInBits, int channels, int endian, int signed)Constructs an AudioFormat with the specified attributes.
this(encoding, sampleRate, sampleSizeInBits, channels);
this.endian = endian;
this.signed = signed;
| public AudioFormat(String encoding, double sampleRate, int sampleSizeInBits, int channels, int endian, int signed, int frameSizeInBits, double frameRate, Class dataType)Constructs an AudioFormat with the specified attributes.
this(encoding, sampleRate, sampleSizeInBits, channels, endian, signed);
this.frameSizeInBits = frameSizeInBits;
this.frameRate = frameRate;
this.dataType = dataType;
|
Methods Summary |
---|
public java.lang.Object | clone()Creates a clone of this AudioFormat by copying each
field to the clone.
AudioFormat f = new AudioFormat(encoding);
f.copy(this);
return f;
| public long | computeDuration(long length)Returns the duration of the media based on the given length
of the data.
if (init) {
// We don't know how to compute this format
if (multiplier < 0)
return -1;
return (long)((length - margin) * multiplier) * 1000;
}
if (encoding == null) {
init = true;
return -1;
} else if (encoding.equalsIgnoreCase(AudioFormat.LINEAR) ||
encoding.equalsIgnoreCase(AudioFormat.ULAW)) {
if (sampleSizeInBits > 0 && channels > 0 && sampleRate > 0)
multiplier = (1000000 * 8)/sampleSizeInBits/channels/sampleRate;
} else if (encoding.equalsIgnoreCase(AudioFormat.ULAW_RTP)) {
if (sampleSizeInBits > 0 && channels > 0 && sampleRate > 0)
multiplier = (1000000 * 8)/sampleSizeInBits/channels/sampleRate;
} else if (encoding.equalsIgnoreCase(AudioFormat.DVI_RTP)) {
if (sampleSizeInBits > 0 && sampleRate > 0)
multiplier = (1000000 * 8)/sampleSizeInBits/sampleRate;
margin = 4;
} else if (encoding.equalsIgnoreCase(AudioFormat.GSM_RTP)) {
if (sampleRate > 0)
multiplier = (160 * 1000000 / 33) / sampleRate;
} else if (encoding.equalsIgnoreCase(AudioFormat.G723_RTP)) {
if (sampleRate > 0)
multiplier = (240/24 * 1000000) / sampleRate;
} else if (frameSizeInBits != Format.NOT_SPECIFIED &&
frameRate != Format.NOT_SPECIFIED) {
// We don't know this codec, but we can compute the
// rate by using the frame rate and size.
if (frameSizeInBits > 0 && frameRate > 0)
multiplier = (1000000 * 8)/frameSizeInBits/frameRate;
}
init = true;
if (multiplier > 0)
return (long)((length - margin) * multiplier) * 1000;
else
return -1;
| protected void | copy(javax.media.Format f)Copies the attributes from the specified
Format into this AudioFormat .
super.copy(f);
AudioFormat other = (AudioFormat) f;
sampleRate = other.sampleRate;
sampleSizeInBits = other.sampleSizeInBits;
channels = other.channels;
endian = other.endian;
signed = other.signed;
frameSizeInBits = other.frameSizeInBits;
frameRate = other.frameRate;
| public boolean | equals(java.lang.Object format)Compares the specified Format with this AudioFormat .
Returns true
only if the specified Format is an AudioFormat and
all of its attributes are
identical to this AudioFormat .
if (format instanceof AudioFormat) {
AudioFormat other = (AudioFormat) format;
return super.equals(format) &&
sampleRate == other.sampleRate &&
sampleSizeInBits == other.sampleSizeInBits &&
channels == other.channels &&
endian == other.endian &&
signed == other.signed &&
frameSizeInBits == other.frameSizeInBits &&
frameRate == other.frameRate;
}
return false;
| public int | getChannels()Gets the number of channels.
return channels;
| public int | getEndian()Gets an integer that indicates whether the sample byte order is big endian
or little endian.
return endian;
| public double | getFrameRate()Gets the frame rate of this AudioFormat .
return frameRate;
| public int | getFrameSizeInBits()Gets the frame size of this AudioFormat . This method is
used primarily for compressed audio.
return frameSizeInBits;
| public double | getSampleRate()Gets the audio sample rate.
return sampleRate;
| public int | getSampleSizeInBits()Gets the size of a sample.
return sampleSizeInBits;
| public int | getSigned()Gets a boolean that indicates whether the samples are stored in signed format
or an unsigned format.
return signed;
| public javax.media.Format | intersects(javax.media.Format format)Finds the attributes shared by two matching Format objects.
If the specified Format does not match this one, the result is
undefined.
Format fmt;
if ((fmt = super.intersects(format)) == null)
return null;
if (!(fmt instanceof AudioFormat))
return fmt;
AudioFormat other = (AudioFormat)format;
AudioFormat res = (AudioFormat)fmt;
res.sampleRate = (sampleRate != NOT_SPECIFIED ?
sampleRate : other.sampleRate);
res.sampleSizeInBits = (sampleSizeInBits != NOT_SPECIFIED ?
sampleSizeInBits : other.sampleSizeInBits);
res.channels = (channels != NOT_SPECIFIED ?
channels : other.channels);
res.endian = (endian != NOT_SPECIFIED ?
endian : other.endian);
res.signed = (signed != NOT_SPECIFIED ?
signed : other.signed);
res.frameSizeInBits = (frameSizeInBits != NOT_SPECIFIED ?
frameSizeInBits : other.frameSizeInBits);
res.frameRate = (frameRate != NOT_SPECIFIED ?
frameRate : other.frameRate);
return res;
| public boolean | matches(javax.media.Format format)Checks whether or not the specified Format matches
this AudioFormat .
Matches only compares the attributes that are defined in the specified
Format , unspecified attributes are ignored.
The two Format objects do not have to be of the same class to
match. For example, if "A" are "B" are being compared, a
match is possible if "A" is derived from "B"
or "B" is derived from "A". (The compared attributes must still match, or
matches fails.)
if (!super.matches(format))
return false;
if (!(format instanceof AudioFormat))
return true;
AudioFormat other = (AudioFormat) format;
return
(sampleRate == NOT_SPECIFIED || other.sampleRate == NOT_SPECIFIED ||
sampleRate == other.sampleRate) &&
(sampleSizeInBits == NOT_SPECIFIED || other.sampleSizeInBits == NOT_SPECIFIED ||
sampleSizeInBits == other.sampleSizeInBits) &&
(channels == NOT_SPECIFIED || other.channels == NOT_SPECIFIED ||
channels == other.channels) &&
(endian == NOT_SPECIFIED || other.endian == NOT_SPECIFIED ||
endian == other.endian) &&
(signed == NOT_SPECIFIED || other.signed == NOT_SPECIFIED ||
signed == other.signed) &&
(frameSizeInBits == NOT_SPECIFIED || other.frameSizeInBits == NOT_SPECIFIED ||
frameSizeInBits == other.frameSizeInBits) &&
(frameRate == NOT_SPECIFIED || other.frameRate == NOT_SPECIFIED ||
frameRate == other.frameRate);
| public java.lang.String | toString()Gets a String representation of the attributes of this
AudioFormat . For example: "PCM, 44.1 KHz, Stereo, Signed".
String strChannels = "";
String strEndian = "";
if (channels == 1)
strChannels = ", Mono";
else if (channels == 2)
strChannels = ", Stereo";
else if (channels != NOT_SPECIFIED)
strChannels = ", " + channels + "-channel";
if (sampleSizeInBits > 8) {
if (endian == BIG_ENDIAN)
strEndian = ", BigEndian";
else if (endian == LITTLE_ENDIAN)
strEndian = ", LittleEndian";
}
return getEncoding() +
( (sampleRate != NOT_SPECIFIED) ? (", " + sampleRate + " Hz")
: ", Unknown Sample Rate" )+
( (sampleSizeInBits != NOT_SPECIFIED) ? (", " + sampleSizeInBits + "-bit")
: "" ) +
strChannels +
strEndian +
( (signed != NOT_SPECIFIED) ? ((signed == SIGNED ? ", Signed"
: ", Unsigned"))
: "" ) +
( (frameRate != NOT_SPECIFIED) ? (", " + frameRate + " frame rate")
: "" )+
( (frameSizeInBits != NOT_SPECIFIED) ? (", FrameSize=" + frameSizeInBits + " bits")
: "" )+
( (dataType != Format.byteArray && dataType != null) ? ", " + dataType
: "");
|
|