FileDocCategorySizeDatePackage
AudioFormat.javaAPI DocJMF 2.1.1e16545Mon May 12 12:20:34 BST 2003javax.media.format

AudioFormat

public 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.
since
JMF 2.0

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
multiplier
For computing the duration of the sample.
int
margin
boolean
init
Constructors Summary
public AudioFormat(String encoding)
Constructs an AudioFormat with the specified encoding type.

param
encoding The audio encoding type.


                     

       
	super(encoding);
    
public AudioFormat(String encoding, double sampleRate, int sampleSizeInBits, int channels)
Constructs an AudioFormat with the specified attributes.

param
encoding A String that describes the encoding type for this AudioFormat.
param
sampleRate The sample rate.
param
sampleSizeInBits The sample size in bits.
param
channels The number of channels as an integer. For example, 1 for mono, 2 for stereo.

	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.

param
encoding A String that describes the encoding type for this AudioFormat.
param
sampleRate The sample rate.
param
sampleSizeInBits The sample size in bits.
param
channels The number of channels.
param
endian The sample byte ordering used for this AudioFormat--BIG_ENDIAN or LITTLE_ENDIAN.
param
signed Indicates whether the samples are stored in a signed or unsigned format. Specify true if the AudioFormat is signed, false if the AudioFormat is unsigned.

	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.

param
encoding A String that describes the encoding type for this AudioFormat.
param
sampleRate The sample rate.
param
sampleSizeInBits The sample size.
param
channels The number of channels.
param
endian The sample byte ordering used for this AudioFormat--BIG_ENDIAN or LITTLE_ENDIAN.
param
signed Indicates whether the samples are stored in a signed or unsigned format. Specify true if the AudioFormat is signed, false if the AudioFormat is unsigned.
param
frameSizeInBits The frame size.
param
frameRate The frame rate.
param
dataType The type of the data. For example, byte array.

	this(encoding, sampleRate, sampleSizeInBits, channels, endian, signed);
	this.frameSizeInBits = frameSizeInBits;
	this.frameRate = frameRate;
	this.dataType = dataType;
    
Methods Summary
public java.lang.Objectclone()
Creates a clone of this AudioFormat by copying each field to the clone.

return
A clone of this AudioFormat.

	AudioFormat f = new AudioFormat(encoding);
	f.copy(this);
	return f;
    
public longcomputeDuration(long length)
Returns the duration of the media based on the given length of the data.

param
length length of the data in this format.
return
the duration in nanoseconds computed from the length of the data in this format. Returns -1 if the duration cannot be computed.



                                                          
        

	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 voidcopy(javax.media.Format f)
Copies the attributes from the specified Format into this AudioFormat.

param
f The Format to copy the attributes from.

	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 booleanequals(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.

param
format The Format to compare with this one.
return
true if the specified Format is the same, false if it is not.

	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 intgetChannels()
Gets the number of channels.

return
The number of channels as an integer.

	return channels;
    
public intgetEndian()
Gets an integer that indicates whether the sample byte order is big endian or little endian.

return
The sample byte order of this AudioFormat, BIG_ENDIAN or LITTLE_ENDIAN.

	return endian;
    
public doublegetFrameRate()
Gets the frame rate of this AudioFormat.

return
The frame rate.

	return frameRate;
    
public intgetFrameSizeInBits()
Gets the frame size of this AudioFormat. This method is used primarily for compressed audio.

return
The frame size of this AudioFormat in bits.

	return frameSizeInBits;
    
public doublegetSampleRate()
Gets the audio sample rate.

return
The sample rate.

	return sampleRate;
    
public intgetSampleSizeInBits()
Gets the size of a sample.

return
The sample size in bits.

	return sampleSizeInBits;
    
public intgetSigned()
Gets a boolean that indicates whether the samples are stored in signed format or an unsigned format.

return
SIGNED if this VideoFormat is signed, UNSIGNED if it is not.

	return signed;
    
public javax.media.Formatintersects(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.

param
The matching Format to intersect with this AudioFormat.
return
A Format object with its attributes set to those attributes common to both Format objects.
see
#matches

	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 booleanmatches(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.)

param
format The Format to compare with this one.
return
true if the specified Format matches this one, false if it does not.

	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.StringtoString()
Gets a String representation of the attributes of this AudioFormat. For example: "PCM, 44.1 KHz, Stereo, Signed".

return
A String that describes the AudioFormat attributes.

	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
	                                                         : "");