FileDocCategorySizeDatePackage
Equalizer.javaAPI DocAndroid 5.1 API21617Thu Mar 12 22:22:30 GMT 2015android.media.audiofx

Equalizer

public class Equalizer extends android.media.audiofx.AudioEffect
An Equalizer is used to alter the frequency response of a particular music source or of the main output mix.

An application creates an Equalizer object to instantiate and control an Equalizer engine in the audio framework. The application can either simply use predefined presets or have a more precise control of the gain in each frequency band controlled by the equalizer.

The methods, parameter types and units exposed by the Equalizer implementation are directly mapping those defined by the OpenSL ES 1.0.1 Specification (http://www.khronos.org/opensles/) for the SLEqualizerItf interface. Please refer to this specification for more details.

To attach the Equalizer to a particular AudioTrack or MediaPlayer, specify the audio session ID of this AudioTrack or MediaPlayer when constructing the Equalizer.

NOTE: attaching an Equalizer to the global audio output mix by use of session 0 is deprecated.

See {@link android.media.MediaPlayer#getAudioSessionId()} for details on audio sessions.

See {@link android.media.audiofx.AudioEffect} class for more details on controlling audio effects.

Fields Summary
private static final String
TAG
public static final int
PARAM_NUM_BANDS
Number of bands. Parameter ID for OnParameterChangeListener
public static final int
PARAM_LEVEL_RANGE
Band level range. Parameter ID for OnParameterChangeListener
public static final int
PARAM_BAND_LEVEL
Band level. Parameter ID for OnParameterChangeListener
public static final int
PARAM_CENTER_FREQ
Band center frequency. Parameter ID for OnParameterChangeListener
public static final int
PARAM_BAND_FREQ_RANGE
Band frequency range. Parameter ID for {@link android.media.audiofx.Equalizer.OnParameterChangeListener}
public static final int
PARAM_GET_BAND
Band for a given frequency. Parameter ID for OnParameterChangeListener
public static final int
PARAM_CURRENT_PRESET
Current preset. Parameter ID for OnParameterChangeListener
public static final int
PARAM_GET_NUM_OF_PRESETS
Request number of presets. Parameter ID for OnParameterChangeListener
public static final int
PARAM_GET_PRESET_NAME
Request preset name. Parameter ID for OnParameterChangeListener
private static final int
PARAM_PROPERTIES
public static final int
PARAM_STRING_SIZE_MAX
Maximum size for preset name
private short
mNumBands
Number of bands implemented by Equalizer engine
private int
mNumPresets
Number of presets implemented by Equalizer engine
private String[]
mPresetNames
Names of presets implemented by Equalizer engine
private OnParameterChangeListener
mParamListener
Registered listener for parameter changes.
private BaseParameterListener
mBaseParamListener
Listener used internally to to receive raw parameter change event from AudioEffect super class
private final Object
mParamListenerLock
Lock for access to mParamListener
Constructors Summary
public Equalizer(int priority, int audioSession)
Class constructor.

param
priority the priority level requested by the application for controlling the Equalizer engine. As the same engine can be shared by several applications, this parameter indicates how much the requesting application needs control of effect parameters. The normal priority is 0, above normal is a positive number, below normal a negative number.
param
audioSession system wide unique audio session identifier. The Equalizer will be attached to the MediaPlayer or AudioTrack in the same audio session.
throws
java.lang.IllegalStateException
throws
java.lang.IllegalArgumentException
throws
java.lang.UnsupportedOperationException
throws
java.lang.RuntimeException


                                                                                                
        
      
             
        super(EFFECT_TYPE_EQUALIZER, EFFECT_TYPE_NULL, priority, audioSession);

        if (audioSession == 0) {
            Log.w(TAG, "WARNING: attaching an Equalizer to global output mix is deprecated!");
        }

        getNumberOfBands();

        mNumPresets = (int)getNumberOfPresets();

        if (mNumPresets != 0) {
            mPresetNames = new String[mNumPresets];
            byte[] value = new byte[PARAM_STRING_SIZE_MAX];
            int[] param = new int[2];
            param[0] = PARAM_GET_PRESET_NAME;
            for (int i = 0; i < mNumPresets; i++) {
                param[1] = i;
                checkStatus(getParameter(param, value));
                int length = 0;
                while (value[length] != 0) length++;
                try {
                    mPresetNames[i] = new String(value, 0, length, "ISO-8859-1");
                } catch (java.io.UnsupportedEncodingException e) {
                    Log.e(TAG, "preset name decode error");
                }
            }
        }
    
Methods Summary
public shortgetBand(int frequency)
Gets the band that has the most effect on the given frequency.

param
frequency frequency in milliHertz which is to be equalized via the returned band.
return
the frequency band that has most effect on the given frequency.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        int[] param = new int[2];
        short[] result = new short[1];

        param[0] = PARAM_GET_BAND;
        param[1] = frequency;
        checkStatus(getParameter(param, result));

        return result[0];
    
public int[]getBandFreqRange(short band)
Gets the frequency range of the given frequency band.

param
band frequency band whose frequency range is requested. The numbering of the bands starts from 0 and ends at (number of bands - 1).
return
the frequency range in millHertz in an array of integers. The first element is the lower limit of the range, the second element the upper limit.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        int[] param = new int[2];
        int[] result = new int[2];
        param[0] = PARAM_BAND_FREQ_RANGE;
        param[1] = (int)band;
        checkStatus(getParameter(param, result));

        return result;
    
public shortgetBandLevel(short band)
Gets the gain set for the given equalizer band.

param
band frequency band whose gain is requested. The numbering of the bands starts from 0 and ends at (number of bands - 1).
return
the gain in millibels of the given band.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        int[] param = new int[2];
        short[] result = new short[1];

        param[0] = PARAM_BAND_LEVEL;
        param[1] = (int)band;
        checkStatus(getParameter(param, result));

        return result[0];
    
public short[]getBandLevelRange()
Gets the level range for use by {@link #setBandLevel(short,short)}. The level is expressed in milliBel.

return
the band level range in an array of short integers. The first element is the lower limit of the range, the second element the upper limit.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        short[] result = new short[2];
        checkStatus(getParameter(PARAM_LEVEL_RANGE, result));
        return result;
    
public intgetCenterFreq(short band)
Gets the center frequency of the given band.

param
band frequency band whose center frequency is requested. The numbering of the bands starts from 0 and ends at (number of bands - 1).
return
the center frequency in milliHertz
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        int[] param = new int[2];
        int[] result = new int[1];

        param[0] = PARAM_CENTER_FREQ;
        param[1] = (int)band;
        checkStatus(getParameter(param, result));

        return result[0];
    
public shortgetCurrentPreset()
Gets current preset.

return
the preset that is set at the moment.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        short[] result = new short[1];
        checkStatus(getParameter(PARAM_CURRENT_PRESET, result));
        return result[0];
    
public shortgetNumberOfBands()
Gets the number of frequency bands supported by the Equalizer engine.

return
the number of bands
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        if (mNumBands != 0) {
            return mNumBands;
        }
        int[] param = new int[1];
        param[0] = PARAM_NUM_BANDS;
        short[] result = new short[1];
        checkStatus(getParameter(param, result));
        mNumBands = result[0];
        return mNumBands;
    
public shortgetNumberOfPresets()
Gets the total number of presets the equalizer supports. The presets will have indices [0, number of presets-1].

return
the number of presets the equalizer supports.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        short[] result = new short[1];
        checkStatus(getParameter(PARAM_GET_NUM_OF_PRESETS, result));
        return result[0];
    
public java.lang.StringgetPresetName(short preset)
Gets the preset name based on the index.

param
preset index of the preset. The valid range is [0, number of presets-1].
return
a string containing the name of the given preset.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        if (preset >= 0 && preset < mNumPresets) {
            return mPresetNames[preset];
        } else {
            return "";
        }
    
public android.media.audiofx.Equalizer$SettingsgetProperties()
Gets the equalizer properties. This method is useful when a snapshot of current equalizer settings must be saved by the application.

return
an Equalizer.Settings object containing all current parameters values
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        byte[] param = new byte[4 + mNumBands * 2];
        checkStatus(getParameter(PARAM_PROPERTIES, param));
        Settings settings = new Settings();
        settings.curPreset = byteArrayToShort(param, 0);
        settings.numBands = byteArrayToShort(param, 2);
        settings.bandLevels = new short[mNumBands];
        for (int i = 0; i < mNumBands; i++) {
            settings.bandLevels[i] = byteArrayToShort(param, 4 + 2*i);
        }
        return settings;
    
public voidsetBandLevel(short band, short level)
Sets the given equalizer band to the given gain value.

param
band frequency band that will have the new gain. The numbering of the bands starts from 0 and ends at (number of bands - 1).
param
level new gain in millibels that will be set to the given band. getBandLevelRange() will define the maximum and minimum values.
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException
see
#getNumberOfBands()

        int[] param = new int[2];
        short[] value = new short[1];

        param[0] = PARAM_BAND_LEVEL;
        param[1] = (int)band;
        value[0] = level;
        checkStatus(setParameter(param, value));
    
public voidsetParameterListener(android.media.audiofx.Equalizer$OnParameterChangeListener listener)
Registers an OnParameterChangeListener interface.

param
listener OnParameterChangeListener interface registered

        synchronized (mParamListenerLock) {
            if (mParamListener == null) {
                mParamListener = listener;
                mBaseParamListener = new BaseParameterListener();
                super.setParameterListener(mBaseParamListener);
            }
        }
    
public voidsetProperties(android.media.audiofx.Equalizer$Settings settings)
Sets the equalizer properties. This method is useful when equalizer settings have to be applied from a previous backup.

param
settings an Equalizer.Settings object containing the properties to apply
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException

        if (settings.numBands != settings.bandLevels.length ||
            settings.numBands != mNumBands) {
            throw new IllegalArgumentException("settings invalid band count: " +settings.numBands);
        }

        byte[] param = concatArrays(shortToByteArray(settings.curPreset),
                                    shortToByteArray(mNumBands));
        for (int i = 0; i < mNumBands; i++) {
            param = concatArrays(param,
                                 shortToByteArray(settings.bandLevels[i]));
        }
        checkStatus(setParameter(PARAM_PROPERTIES, param));
    
public voidusePreset(short preset)
Sets the equalizer according to the given preset.

param
preset new preset that will be taken into use. The valid range is [0, number of presets-1].
throws
IllegalStateException
throws
IllegalArgumentException
throws
UnsupportedOperationException
see
#getNumberOfPresets()

        checkStatus(setParameter(PARAM_CURRENT_PRESET, preset));