FileDocCategorySizeDatePackage
AudioFormat.javaAPI DocAndroid 5.1 API19744Thu Mar 12 22:22:30 GMT 2015android.media

AudioFormat

public class AudioFormat extends Object
The AudioFormat class is used to access a number of audio format and channel configuration constants. They are for instance used in {@link AudioTrack} and {@link AudioRecord}.

Fields Summary
public static final int
ENCODING_INVALID
Invalid audio data format
public static final int
ENCODING_DEFAULT
Default audio data format
public static final int
ENCODING_PCM_16BIT
Audio data format: PCM 16 bit per sample. Guaranteed to be supported by devices.
public static final int
ENCODING_PCM_8BIT
Audio data format: PCM 8 bit per sample. Not guaranteed to be supported by devices.
public static final int
ENCODING_PCM_FLOAT
Audio data format: single-precision floating-point per sample
public static final int
ENCODING_AC3
Audio data format: AC-3 compressed
public static final int
ENCODING_E_AC3
Audio data format: E-AC-3 compressed
public static final int
CHANNEL_CONFIGURATION_INVALID
public static final int
CHANNEL_CONFIGURATION_DEFAULT
public static final int
CHANNEL_CONFIGURATION_MONO
public static final int
CHANNEL_CONFIGURATION_STEREO
public static final int
CHANNEL_INVALID
Invalid audio channel mask
public static final int
CHANNEL_OUT_DEFAULT
Default audio channel mask
public static final int
CHANNEL_OUT_FRONT_LEFT
public static final int
CHANNEL_OUT_FRONT_RIGHT
public static final int
CHANNEL_OUT_FRONT_CENTER
public static final int
CHANNEL_OUT_LOW_FREQUENCY
public static final int
CHANNEL_OUT_BACK_LEFT
public static final int
CHANNEL_OUT_BACK_RIGHT
public static final int
CHANNEL_OUT_FRONT_LEFT_OF_CENTER
public static final int
CHANNEL_OUT_FRONT_RIGHT_OF_CENTER
public static final int
CHANNEL_OUT_BACK_CENTER
public static final int
CHANNEL_OUT_SIDE_LEFT
public static final int
CHANNEL_OUT_SIDE_RIGHT
public static final int
CHANNEL_OUT_TOP_CENTER
public static final int
CHANNEL_OUT_TOP_FRONT_LEFT
public static final int
CHANNEL_OUT_TOP_FRONT_CENTER
public static final int
CHANNEL_OUT_TOP_FRONT_RIGHT
public static final int
CHANNEL_OUT_TOP_BACK_LEFT
public static final int
CHANNEL_OUT_TOP_BACK_CENTER
public static final int
CHANNEL_OUT_TOP_BACK_RIGHT
public static final int
CHANNEL_OUT_MONO
public static final int
CHANNEL_OUT_STEREO
public static final int
CHANNEL_OUT_QUAD
public static final int
CHANNEL_OUT_QUAD_SIDE
public static final int
CHANNEL_OUT_SURROUND
public static final int
CHANNEL_OUT_5POINT1
public static final int
CHANNEL_OUT_5POINT1_SIDE
public static final int
CHANNEL_OUT_7POINT1
public static final int
CHANNEL_OUT_7POINT1_SURROUND
public static final int
CHANNEL_IN_DEFAULT
public static final int
CHANNEL_IN_LEFT
public static final int
CHANNEL_IN_RIGHT
public static final int
CHANNEL_IN_FRONT
public static final int
CHANNEL_IN_BACK
public static final int
CHANNEL_IN_LEFT_PROCESSED
public static final int
CHANNEL_IN_RIGHT_PROCESSED
public static final int
CHANNEL_IN_FRONT_PROCESSED
public static final int
CHANNEL_IN_BACK_PROCESSED
public static final int
CHANNEL_IN_PRESSURE
public static final int
CHANNEL_IN_X_AXIS
public static final int
CHANNEL_IN_Y_AXIS
public static final int
CHANNEL_IN_Z_AXIS
public static final int
CHANNEL_IN_VOICE_UPLINK
public static final int
CHANNEL_IN_VOICE_DNLINK
public static final int
CHANNEL_IN_MONO
public static final int
CHANNEL_IN_STEREO
public static final int
CHANNEL_IN_FRONT_BACK
public static final int
AUDIO_FORMAT_HAS_PROPERTY_NONE
public static final int
AUDIO_FORMAT_HAS_PROPERTY_ENCODING
public static final int
AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE
public static final int
AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK
private int
mEncoding
private int
mSampleRate
private int
mChannelMask
private int
mPropertySetMask
Constructors Summary
private AudioFormat(int ignoredArgument)
Private constructor with an ignored argument to differentiate from the removed default ctor

param
ignoredArgument

    
private AudioFormat(int encoding, int sampleRate, int channelMask)
Constructor used by the JNI

        mEncoding = encoding;
        mSampleRate = sampleRate;
        mChannelMask = channelMask;
        mPropertySetMask = AUDIO_FORMAT_HAS_PROPERTY_ENCODING |
                AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE |
                AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK;
    
public AudioFormat()

removed

        throw new UnsupportedOperationException("There is no valid usage of this constructor");
    
Methods Summary
public static intchannelCountFromInChannelMask(int mask)

hide
Return the number of channels from an input channel mask
param
mask a combination of the CHANNEL_IN_* definitions, even CHANNEL_IN_DEFAULT
return
number of channels for the mask

        return Integer.bitCount(mask);
    
public static intchannelCountFromOutChannelMask(int mask)

hide
Return the number of channels from an output channel mask
param
mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
return
number of channels for the mask

        return Integer.bitCount(mask);
    
public static intconvertChannelOutMaskToNativeMask(int javaMask)

hide
Return a channel mask ready to be used by native code
param
mask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
return
a native channel mask

        return (javaMask >> 2);
    
public static intconvertNativeChannelMaskToOutMask(int nativeMask)

hide
Return a java output channel mask
param
mask a native channel mask
return
a combination of the CHANNEL_OUT_* definitions

        return (nativeMask << 2);
    
public static intgetBytesPerSample(int audioFormat)

hide

    // CHANNEL_IN_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_IN_ALL

      
        
    
        switch (audioFormat) {
        case ENCODING_PCM_8BIT:
            return 1;
        case ENCODING_PCM_16BIT:
        case ENCODING_DEFAULT:
            return 2;
        case ENCODING_PCM_FLOAT:
            return 4;
        case ENCODING_INVALID:
        default:
            throw new IllegalArgumentException("Bad audio format " + audioFormat);
        }
    
public intgetChannelMask()
Return the channel mask.

return
one of the values that can be set in {@link Builder#setChannelMask(int)} or {@link AudioFormat#CHANNEL_INVALID} if not set.

        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_CHANNEL_MASK) == 0) {
            return CHANNEL_INVALID;
        }
        return mChannelMask;
    
public intgetEncoding()
Return the encoding.

return
one of the values that can be set in {@link Builder#setEncoding(int)} or {@link AudioFormat#ENCODING_INVALID} if not set.


                              
       
        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_ENCODING) == 0) {
            return ENCODING_INVALID;
        }
        return mEncoding;
    
public intgetPropertySetMask()

hide

        return mPropertySetMask;
    
public intgetSampleRate()
Return the sample rate.

return
one of the values that can be set in {@link Builder#setSampleRate(int)} or 0 if not set.

        if ((mPropertySetMask & AUDIO_FORMAT_HAS_PROPERTY_SAMPLE_RATE) == 0) {
            return 0;
        }
        return mSampleRate;
    
public static intinChannelMaskFromOutChannelMask(int outMask)

hide
Return the input channel mask corresponding to an output channel mask. This can be used for submix rerouting for the mask of the recorder to map to that of the mix.
param
outMask a combination of the CHANNEL_OUT_* definitions, but not CHANNEL_OUT_DEFAULT
return
a combination of CHANNEL_IN_* definitions matching an output channel mask
throws
IllegalArgumentException

    // CHANNEL_OUT_ALL is not yet defined; if added then it should match AUDIO_CHANNEL_OUT_ALL

                                                                 
           
        if (outMask == CHANNEL_OUT_DEFAULT) {
            throw new IllegalArgumentException(
                    "Illegal CHANNEL_OUT_DEFAULT channel mask for input.");
        }
        switch (channelCountFromOutChannelMask(outMask)) {
            case 1:
                return CHANNEL_IN_MONO;
            case 2:
                return CHANNEL_IN_STEREO;
            default:
                throw new IllegalArgumentException("Unsupported channel configuration for input.");
        }
    
public static booleanisEncodingLinearPcm(int audioFormat)

hide

        switch (audioFormat) {
        case ENCODING_PCM_8BIT:
        case ENCODING_PCM_16BIT:
        case ENCODING_PCM_FLOAT:
        case ENCODING_DEFAULT:
            return true;
        case ENCODING_AC3:
        case ENCODING_E_AC3:
            return false;
        case ENCODING_INVALID:
        default:
            throw new IllegalArgumentException("Bad audio format " + audioFormat);
        }
    
public static booleanisValidEncoding(int audioFormat)

hide

        switch (audioFormat) {
        case ENCODING_PCM_8BIT:
        case ENCODING_PCM_16BIT:
        case ENCODING_PCM_FLOAT:
        case ENCODING_AC3:
        case ENCODING_E_AC3:
            return true;
        default:
            return false;
        }
    
public java.lang.StringtoString()

        return new String("AudioFormat:"
                + " props=" + mPropertySetMask
                + " enc=" + mEncoding
                + " chan=0x" + Integer.toHexString(mChannelMask)
                + " rate=" + mSampleRate);