APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.sound.sampled

public Class AudioFormat

extends Object
Class Inheritance
Imports
java.util.Collections, .HashMap, .Map, .Objects

AudioFormat is the class that specifies a particular arrangement of data in a sound stream. By examining the information stored in the audio format, you can discover how to interpret the bits in the binary sound data.

Every data line has an audio format associated with its data stream. The audio format of a source (playback) data line indicates what kind of data the data line expects to receive for output. For a target (capture) data line, the audio format specifies the kind of the data that can be read from the line.

Sound files also have audio formats, of course. The AudioFileFormat class encapsulates an AudioFormat in addition to other, file-specific information. Similarly, an AudioInputStream has an AudioFormat.

The AudioFormat class accommodates a number of common sound-file encoding techniques, including pulse-code modulation (PCM), mu-law encoding, and a-law encoding. These encoding techniques are predefined, but service providers can create new encoding types. The encoding that a specific format uses is named by its encoding field.

In addition to the encoding, the audio format includes other properties that further specify the exact arrangement of the data. These include the number of channels, sample rate, sample size, byte order, frame rate, and frame size. Sounds may have different numbers of audio channels: one for mono, two for stereo. The sample rate measures how many "snapshots" (samples) of the sound pressure are taken per second, per channel. (If the sound is stereo rather than mono, two samples are actually measured at each instant of time: one for the left channel, and another for the right channel; however, the sample rate still measures the number per channel, so the rate is the same regardless of the number of channels. This is the standard use of the term.) The sample size indicates how many bits are used to store each snapshot; 8 and 16 are typical values. For 16-bit samples (or any other sample size larger than a byte), byte order is important; the bytes in each sample are arranged in either the "little-endian" or "big-endian" style. For encodings like PCM, a frame consists of the set of samples for all channels at a given point in time, and so the size of a frame (in bytes) is always equal to the size of a sample (in bytes) times the number of channels. However, with some other sorts of encodings a frame can contain a bundle of compressed data for a whole series of samples, as well as additional, non-sample data. For such encodings, the sample rate and sample size refer to the data after it is decoded into PCM, and so they are completely different from the frame rate and frame size.

An AudioFormat object can include a set of properties. A property is a pair of key and value: the key is of type String, the associated property value is an arbitrary object. Properties specify additional format specifications, like the bit rate for compressed formats. Properties are mainly used as a means to transport additional information of the audio format to and from the service providers. Therefore, properties are ignored in the matches(AudioFormat) method. However, methods which rely on the installed service providers, like (AudioFormat, AudioFormat) isConversionSupported may consider properties, depending on the respective service provider implementation.

The following table lists some common properties which service providers should use, if applicable:

Audio Format Properties
Property key Value type Description
"bitrate" Integer average bit rate in bits per second
"vbr" Boolean true, if the file is encoded in variable bit rate (VBR)
"quality" Integer encoding/conversion quality, 1..100

Vendors of service providers (plugins) are encouraged to seek information about other already established properties in third party plugins, and follow the same conventions.

Authors
Kara Kytle, Florian Bomers
Since
1.3
See Also
DataLine#getFormat, AudioInputStream#getFormat, AudioFileFormat, javax.sound.sampled.spi.FormatConversionProvider

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
AudioFormat.Encoding

The Encoding class names the specific type of data representation used for an audio stream.

Field Summary

Modifier and TypeField and Description
protected boolean
bigEndian

Indicates whether the audio data is stored in big-endian or little-endian order.

protected int
channels

The number of audio channels in this format (1 for mono, 2 for stereo).

protected AudioFormat.Encoding
encoding

The audio encoding technique used by this format.

protected float
frameRate

The number of frames played or recorded per second, for sounds that have this format.

protected int
frameSize

The number of bytes in each frame of a sound that has this format.

private HashMap<String, Object>
properties

The set of properties.

protected float
sampleRate

The number of samples played or recorded per second, for sounds that have this format.

protected int
sampleSizeInBits

The number of bits in each sample of a sound that has this format.

Constructor Summary

AccessConstructor and Description
public
AudioFormat(AudioFormat.Encoding
the audio encoding technique
encoding
,
float
the number of samples per second
sampleRate
,
int
the number of bits in each sample
sampleSizeInBits
,
int
the number of channels (1 for mono, 2 for stereo, and so on)
channels
,
int
the number of bytes in each frame
frameSize
,
float
the number of frames per second
frameRate
,
boolean
indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)
bigEndian
)

Constructs an AudioFormat with the given parameters.

public
AudioFormat(AudioFormat.Encoding
the audio encoding technique
encoding
,
float
the number of samples per second
sampleRate
,
int
the number of bits in each sample
sampleSizeInBits
,
int
the number of channels (1 for mono, 2 for stereo, and so on)
channels
,
int
the number of bytes in each frame
frameSize
,
float
the number of frames per second
frameRate
,
boolean
indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)
bigEndian
,
Map<String, Object>
a Map<String, Object> object containing format properties
properties
)

Constructs an AudioFormat with the given parameters.

public
AudioFormat(float
the number of samples per second
sampleRate
,
int
the number of bits in each sample
sampleSizeInBits
,
int
the number of channels (1 for mono, 2 for stereo, and so on)
channels
,
boolean
indicates whether the data is signed or unsigned
signed
,
boolean
indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)
bigEndian
)

Constructs an AudioFormat with a linear PCM encoding and the given parameters.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The number of channels (1 for mono, 2 for stereo, etc.), or AudioSystem.NOT_SPECIFIED
getChannels
()

Obtains the number of channels.

public AudioFormat.Encoding

Returns:

the encoding type
getEncoding
()

Obtains the type of encoding for sounds in this format.

public float

Returns:

the number of frames per second, or AudioSystem.NOT_SPECIFIED
getFrameRate
()

Obtains the frame rate in frames per second.

public int

Returns:

the number of bytes per frame, or AudioSystem.NOT_SPECIFIED
getFrameSize
()

Obtains the frame size in bytes.

public Object

Returns:

the value of the property with the specified key, or null if the property does not exist
getProperty
(String
the key of the desired property
key
)

Obtain the property value specified by the key.

public float

Returns:

the number of samples per second, or AudioSystem.NOT_SPECIFIED
getSampleRate
()

Obtains the sample rate.

public int

Returns:

the number of bits in each sample, or AudioSystem.NOT_SPECIFIED
getSampleSizeInBits
()

Obtains the size of a sample.

public boolean

Returns:

true if the data is stored in big-endian byte order, false if little-endian
isBigEndian
()

Indicates whether the audio data is stored in big-endian or little-endian byte order.

public boolean

Returns:

true if this format matches the one specified, false otherwise
matches
(AudioFormat
format to test for match
format
)

Indicates whether this format matches the one specified.

public Map<String, Object>

Returns:

a Map<String, Object> object containing all properties. If no properties are recognized, an empty map is returned.
properties
()

Obtain an unmodifiable map of properties.

public String

Returns:

a string representation of the audio format
toString
()

Overrides java.lang.Object.toString.

Returns a string that describes the audio format, such as: "PCM SIGNED 22050 Hz 16 bit mono big-endian".

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bigEndianback to summary
protected boolean bigEndian

Indicates whether the audio data is stored in big-endian or little-endian order.

channelsback to summary
protected int channels

The number of audio channels in this format (1 for mono, 2 for stereo).

encodingback to summary
protected AudioFormat.Encoding encoding

The audio encoding technique used by this format.

frameRateback to summary
protected float frameRate

The number of frames played or recorded per second, for sounds that have this format.

frameSizeback to summary
protected int frameSize

The number of bytes in each frame of a sound that has this format.

propertiesback to summary
private HashMap<String, Object> properties

The set of properties.

sampleRateback to summary
protected float sampleRate

The number of samples played or recorded per second, for sounds that have this format.

sampleSizeInBitsback to summary
protected int sampleSizeInBits

The number of bits in each sample of a sound that has this format.

Constructor Detail

AudioFormatback to summary
public AudioFormat(AudioFormat.Encoding encoding, float sampleRate, int sampleSizeInBits, int channels, int frameSize, float frameRate, boolean bigEndian)

Constructs an AudioFormat with the given parameters. The encoding specifies the convention used to represent the data. The other parameters are further explained in the class description.

Parameters
encoding:AudioFormat.Encoding

the audio encoding technique

sampleRate:float

the number of samples per second

sampleSizeInBits:int

the number of bits in each sample

channels:int

the number of channels (1 for mono, 2 for stereo, and so on)

frameSize:int

the number of bytes in each frame

frameRate:float

the number of frames per second

bigEndian:boolean

indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)

AudioFormatback to summary
public AudioFormat(AudioFormat.Encoding encoding, float sampleRate, int sampleSizeInBits, int channels, int frameSize, float frameRate, boolean bigEndian, Map<String, Object> properties)

Constructs an AudioFormat with the given parameters. The encoding specifies the convention used to represent the data. The other parameters are further explained in the class description.

Parameters
encoding:AudioFormat.Encoding

the audio encoding technique

sampleRate:float

the number of samples per second

sampleSizeInBits:int

the number of bits in each sample

channels:int

the number of channels (1 for mono, 2 for stereo, and so on)

frameSize:int

the number of bytes in each frame

frameRate:float

the number of frames per second

bigEndian:boolean

indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)

properties:Map<String, Object>

a Map<String, Object> object containing format properties

Since
1.5
AudioFormatback to summary
public AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)

Constructs an AudioFormat with a linear PCM encoding and the given parameters. The frame size is set to the number of bytes required to contain one sample from each channel, and the frame rate is set to the sample rate.

Parameters
sampleRate:float

the number of samples per second

sampleSizeInBits:int

the number of bits in each sample

channels:int

the number of channels (1 for mono, 2 for stereo, and so on)

signed:boolean

indicates whether the data is signed or unsigned

bigEndian:boolean

indicates whether the data for a single sample is stored in big-endian byte order (false means little-endian)

Method Detail

getChannelsback to summary
public int getChannels()

Obtains the number of channels. When this AudioFormat is used for queries (e.g. AudioSystem.isConversionSupported) or capabilities (e.g. DataLine.Info.getFormats), a return value of AudioSystem.NOT_SPECIFIED means that any (positive) number of channels is acceptable.

Returns:int

The number of channels (1 for mono, 2 for stereo, etc.), or AudioSystem.NOT_SPECIFIED

See Also
AudioSystem#NOT_SPECIFIED
getEncodingback to summary
public AudioFormat.Encoding getEncoding()

Obtains the type of encoding for sounds in this format.

Returns:AudioFormat.Encoding

the encoding type

See Also
Encoding#PCM_SIGNED, Encoding#PCM_UNSIGNED, Encoding#ULAW, Encoding#ALAW
getFrameRateback to summary
public float getFrameRate()

Obtains the frame rate in frames per second. When this AudioFormat is used for queries (e.g. AudioSystem.isConversionSupported) or capabilities (e.g. DataLine.Info.getFormats), a frame rate of AudioSystem.NOT_SPECIFIED means that any frame rate is acceptable. AudioSystem.NOT_SPECIFIED is also returned when the frame rate is not defined for this audio format.

Returns:float

the number of frames per second, or AudioSystem.NOT_SPECIFIED

See Also
getSampleRate(), AudioSystem#NOT_SPECIFIED
getFrameSizeback to summary
public int getFrameSize()

Obtains the frame size in bytes. When this AudioFormat is used for queries (e.g. AudioSystem.isConversionSupported) or capabilities (e.g. DataLine.Info.getFormats), a frame size of AudioSystem.NOT_SPECIFIED means that any frame size is acceptable. AudioSystem.NOT_SPECIFIED is also returned when the frame size is not defined for this audio format.

Returns:int

the number of bytes per frame, or AudioSystem.NOT_SPECIFIED

See Also
getSampleSizeInBits(), AudioSystem#NOT_SPECIFIED
getPropertyback to summary
public Object getProperty(String key)

Obtain the property value specified by the key. The concept of properties is further explained in the class description.

If the specified property is not defined for a particular file format, this method returns null.

Parameters
key:String

the key of the desired property

Returns:Object

the value of the property with the specified key, or null if the property does not exist

Since
1.5
See Also
properties()
getSampleRateback to summary
public float getSampleRate()

Obtains the sample rate. For compressed formats, the return value is the sample rate of the uncompressed audio data. When this AudioFormat is used for queries (e.g. AudioSystem.isConversionSupported) or capabilities (e.g. DataLine.Info.getFormats), a sample rate of AudioSystem.NOT_SPECIFIED means that any sample rate is acceptable. AudioSystem.NOT_SPECIFIED is also returned when the sample rate is not defined for this audio format.

Returns:float

the number of samples per second, or AudioSystem.NOT_SPECIFIED

See Also
getFrameRate(), AudioSystem#NOT_SPECIFIED
getSampleSizeInBitsback to summary
public int getSampleSizeInBits()

Obtains the size of a sample. For compressed formats, the return value is the sample size of the uncompressed audio data. When this AudioFormat is used for queries (e.g. AudioSystem.isConversionSupported) or capabilities (e.g. DataLine.Info.getFormats), a sample size of AudioSystem.NOT_SPECIFIED means that any sample size is acceptable. AudioSystem.NOT_SPECIFIED is also returned when the sample size is not defined for this audio format.

Returns:int

the number of bits in each sample, or AudioSystem.NOT_SPECIFIED

See Also
getFrameSize(), AudioSystem#NOT_SPECIFIED
isBigEndianback to summary
public boolean isBigEndian()

Indicates whether the audio data is stored in big-endian or little-endian byte order. If the sample size is not more than one byte, the return value is irrelevant.

Returns:boolean

true if the data is stored in big-endian byte order, false if little-endian

matchesback to summary
public boolean matches(AudioFormat format)

Indicates whether this format matches the one specified. To match, two formats must have the same encoding, and consistent values of the number of channels, sample rate, sample size, frame rate, and frame size. The values of the property are consistent if they are equal or the specified format has the property value AudioSystem.NOT_SPECIFIED. The byte order (big-endian or little-endian) must be the same if the sample size is greater than one byte.

Parameters
format:AudioFormat

format to test for match

Returns:boolean

true if this format matches the one specified, false otherwise

propertiesback to summary
public Map<String, Object> properties()

Obtain an unmodifiable map of properties. The concept of properties is further explained in the class description.

Returns:Map<String, Object>

a Map<String, Object> object containing all properties. If no properties are recognized, an empty map is returned.

Annotations
@SuppressWarnings:unchecked
Since
1.5
See Also
getProperty(String)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string that describes the audio format, such as: "PCM SIGNED 22050 Hz 16 bit mono big-endian". The contents of the string may vary between implementations of Java Sound.

Returns:String

a string representation of the audio format

Annotations
@Override
javax.sound.sampled back to summary

public Class AudioFormat.Encoding

extends Object
Class Inheritance

The Encoding class names the specific type of data representation used for an audio stream. The encoding includes aspects of the sound format other than the number of channels, sample rate, sample size, frame rate, frame size, and byte order.

One ubiquitous type of audio encoding is pulse-code modulation (PCM), which is simply a linear (proportional) representation of the sound waveform. With PCM, the number stored in each sample is proportional to the instantaneous amplitude of the sound pressure at that point in time. The numbers may be signed or unsigned integers or floats. Besides PCM, other encodings include mu-law and a-law, which are nonlinear mappings of the sound amplitude that are often used for recording speech.

You can use a predefined encoding by referring to one of the static objects created by this class, such as PCM_SIGNED or PCM_UNSIGNED. Service providers can create new encodings, such as compressed audio formats, and make these available through the AudioSystem class.

The Encoding class is static, so that all AudioFormat objects that have the same encoding will refer to the same object (rather than different instances of the same class). This allows matches to be made by checking that two format's encodings are equal.

Author
Kara Kytle
Since
1.3
See Also
AudioFormat, javax.sound.sampled.spi.FormatConversionProvider

Field Summary

Modifier and TypeField and Description
public static final AudioFormat.Encoding
ALAW

Specifies a-law encoded data.

private final String
name

Encoding name.

public static final AudioFormat.Encoding
PCM_FLOAT

Specifies floating-point PCM data.

public static final AudioFormat.Encoding
PCM_SIGNED

Specifies signed, linear PCM data.

public static final AudioFormat.Encoding
PCM_UNSIGNED

Specifies unsigned, linear PCM data.

public static final AudioFormat.Encoding
ULAW

Specifies u-law encoded data.

Constructor Summary

AccessConstructor and Description
public
Encoding(final String
the name of the new type of encoding
name
)

Constructs a new encoding.

Method Summary

Modifier and TypeMethod and Description
public final boolean

Returns:

true if the specified object is equal to this encoding; false otherwise
equals
(final Object
the reference object with which to compare
obj
)

Overrides java.lang.Object.equals.

Indicates whether the specified object is equal to this encoding, returning true if the objects are equal.

public final int

Returns:

a hash code value for this encoding
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this encoding.

public final String

Returns:

a string representation of the encoding
toString
()

Overrides java.lang.Object.toString.

Returns encoding's name as the string representation of the encoding.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ALAWback to summary
public static final AudioFormat.Encoding ALAW

Specifies a-law encoded data.

nameback to summary
private final String name

Encoding name.

PCM_FLOATback to summary
public static final AudioFormat.Encoding PCM_FLOAT

Specifies floating-point PCM data.

Since
1.7
PCM_SIGNEDback to summary
public static final AudioFormat.Encoding PCM_SIGNED

Specifies signed, linear PCM data.

PCM_UNSIGNEDback to summary
public static final AudioFormat.Encoding PCM_UNSIGNED

Specifies unsigned, linear PCM data.

ULAWback to summary
public static final AudioFormat.Encoding ULAW

Specifies u-law encoded data.

Constructor Detail

Encodingback to summary
public Encoding(final String name)

Constructs a new encoding.

Parameters
name:String

the name of the new type of encoding

Method Detail

equalsback to summary
public final boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Indicates whether the specified object is equal to this encoding, returning true if the objects are equal.

Parameters
obj:Object

the reference object with which to compare

Returns:boolean

true if the specified object is equal to this encoding; false otherwise

Annotations
@Override
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this encoding.

Returns:int

a hash code value for this encoding

Annotations
@Override
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Returns encoding's name as the string representation of the encoding. For the predefined encodings, the name is similar to the encoding's variable (field) name. For example, PCM_SIGNED.toString() returns the name "PCM_SIGNED".

Returns:String

a string representation of the encoding

Annotations
@Override