1fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent/*
217cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent * Copyright (C) 2010 The Android Open Source Project
3fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent *
4fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Licensed under the Apache License, Version 2.0 (the "License");
5fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * you may not use this file except in compliance with the License.
6fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * You may obtain a copy of the License at
7fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent *
8fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent *      http://www.apache.org/licenses/LICENSE-2.0
9fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent *
10fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Unless required by applicable law or agreed to in writing, software
11fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * distributed under the License is distributed on an "AS IS" BASIS,
12fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * See the License for the specific language governing permissions and
14fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * limitations under the License.
15fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */
16fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
171a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurentpackage android.media.audiofx;
18fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
19fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.app.Activity;
20fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.content.Context;
21fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.content.Intent;
221a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurentimport android.media.audiofx.AudioEffect;
23fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.os.Bundle;
24fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport android.util.Log;
25ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
26fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.ByteOrder;
27fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.ByteBuffer;
28fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentimport java.nio.CharBuffer;
29ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurentimport java.util.StringTokenizer;
30fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
31fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
32fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent/**
33fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * Bass boost is an audio effect to boost or amplify low frequencies of the sound. It is comparable
34ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent * to a simple equalizer but limited to one band amplification in the low frequency range.
351a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>An application creates a BassBoost object to instantiate and control a bass boost engine in
361a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * the audio framework.
37fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * <p>The methods, parameter types and units exposed by the BassBoost implementation are directly
38fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * mapping those defined by the OpenSL ES 1.0.1 Specification (http://www.khronos.org/opensles/)
39fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * for the SLBassBoostItf interface. Please refer to this specification for more details.
40fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent * <p>To attach the BassBoost to a particular AudioTrack or MediaPlayer, specify the audio session
411a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * ID of this AudioTrack or MediaPlayer when constructing the BassBoost.
4262f3617f2f4016ad2f59635d5156d64872989880Eric Laurent * <p>NOTE: attaching a BassBoost to the global audio output mix by use of session 0 is deprecated.
431a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>See {@link android.media.MediaPlayer#getAudioSessionId()} for details on audio sessions.
441a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * <p>See {@link android.media.audiofx.AudioEffect} class for more details on
451a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent * controlling audio effects.
46fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent */
47fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
48fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurentpublic class BassBoost extends AudioEffect {
49fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
50fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private final static String TAG = "BassBoost";
51fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
52fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    // These constants must be synchronized with those in
53fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    // frameworks/base/include/media/EffectBassBoostApi.h
54fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
55fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Is strength parameter supported by bass boost engine. Parameter ID for getParameter().
56fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
57fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public static final int PARAM_STRENGTH_SUPPORTED = 0;
58fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
59fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Bass boost effect strength. Parameter ID for
601a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * {@link android.media.audiofx.BassBoost.OnParameterChangeListener}
61fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
62fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public static final int PARAM_STRENGTH = 1;
63fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
64fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
65fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Indicates if strength parameter is supported by the bass boost engine
66fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
67fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private boolean mStrengthSupported = false;
68fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
69fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
70fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Registered listener for parameter changes.
71fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
72fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private OnParameterChangeListener mParamListener = null;
73fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
74fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
75fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Listener used internally to to receive raw parameter change event from AudioEffect super class
76fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
77fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private BaseParameterListener mBaseParamListener = null;
78fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
79fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
80fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Lock for access to mParamListener
81fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
82fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private final Object mParamListenerLock = new Object();
83fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
84fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
85fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Class constructor.
86fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @param priority the priority level requested by the application for controlling the BassBoost
87fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * engine. As the same engine can be shared by several applications, this parameter indicates
88fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * how much the requesting application needs control of effect parameters. The normal priority
89fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * is 0, above normal is a positive number, below normal a negative number.
9062f3617f2f4016ad2f59635d5156d64872989880Eric Laurent     * @param audioSession system wide unique audio session identifier. The BassBoost will be
9162f3617f2f4016ad2f59635d5156d64872989880Eric Laurent     * attached to the MediaPlayer or AudioTrack in the same audio session.
92fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     *
93fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws java.lang.IllegalStateException
94fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws java.lang.IllegalArgumentException
95fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws java.lang.UnsupportedOperationException
96fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws java.lang.RuntimeException
97fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
98fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public BassBoost(int priority, int audioSession)
99fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    throws IllegalStateException, IllegalArgumentException,
100fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent           UnsupportedOperationException, RuntimeException {
101fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        super(EFFECT_TYPE_BASS_BOOST, EFFECT_TYPE_NULL, priority, audioSession);
102fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
10362f3617f2f4016ad2f59635d5156d64872989880Eric Laurent        if (audioSession == 0) {
10462f3617f2f4016ad2f59635d5156d64872989880Eric Laurent            Log.w(TAG, "WARNING: attaching a BassBoost to global output mix is deprecated!");
10562f3617f2f4016ad2f59635d5156d64872989880Eric Laurent        }
10662f3617f2f4016ad2f59635d5156d64872989880Eric Laurent
107ba8da2e61b1d9ebb7a4758f1f7849ff8440bd20cEric Laurent        int[] value = new int[1];
108fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        checkStatus(getParameter(PARAM_STRENGTH_SUPPORTED, value));
109fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        mStrengthSupported = (value[0] != 0);
110fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
111fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
112fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
113fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Indicates whether setting strength is supported. If this method returns false, only one
114fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * strength is supported and the setStrength() method always rounds to that value.
115fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @return true is strength parameter is supported, false otherwise
116fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
117fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public boolean getStrengthSupported() {
118fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent       return mStrengthSupported;
119fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
120fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
121fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
122fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Sets the strength of the bass boost effect. If the implementation does not support per mille
123fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * accuracy for setting the strength, it is allowed to round the given strength to the nearest
124fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * supported value. You can use the {@link #getRoundedStrength()} method to query the
125fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * (possibly rounded) value that was actually set.
12617cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent     * @param strength strength of the effect. The valid range for strength strength is [0, 1000],
127fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * where 0 per mille designates the mildest effect and 1000 per mille designates the strongest.
128fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws IllegalStateException
129fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws IllegalArgumentException
130fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws UnsupportedOperationException
131fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
132fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public void setStrength(short strength)
133fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
134fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        checkStatus(setParameter(PARAM_STRENGTH, strength));
135fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
136fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
137fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
138fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Gets the current strength of the effect.
13917cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent     * @return the strength of the effect. The valid range for strength is [0, 1000], where 0 per
140fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * mille designates the mildest effect and 1000 per mille the strongest
141fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws IllegalStateException
142fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws IllegalArgumentException
143fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @throws UnsupportedOperationException
144fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
145fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public short getRoundedStrength()
146fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
147fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        short[] value = new short[1];
148fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        checkStatus(getParameter(PARAM_STRENGTH, value));
149fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        return value[0];
150fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
151fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
152fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
153fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * The OnParameterChangeListener interface defines a method called by the BassBoost when a
154fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * parameter value has changed.
155fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
156fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public interface OnParameterChangeListener  {
157fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        /**
158fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * Method called when a parameter value has changed. The method is called only if the
159fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * parameter was changed by another application having the control of the same
160fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * BassBoost engine.
161fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * @param effect the BassBoost on which the interface is registered.
162fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * @param status status of the set parameter operation.
163fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * @param param ID of the modified parameter. See {@link #PARAM_STRENGTH} ...
164fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         * @param value the new parameter value.
165fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent         */
166fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        void onParameterChange(BassBoost effect, int status, int param, short value);
167fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
168fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
169fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
170fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Listener used internally to receive unformatted parameter change events from AudioEffect
171fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * super class.
172fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
173fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    private class BaseParameterListener implements AudioEffect.OnParameterChangeListener {
174fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        private BaseParameterListener() {
175fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
176fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        }
177fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) {
178fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            OnParameterChangeListener l = null;
179fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
180fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            synchronized (mParamListenerLock) {
181fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                if (mParamListener != null) {
182fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                    l = mParamListener;
183fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                }
184fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            }
185fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            if (l != null) {
186fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                int p = -1;
187fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                short v = -1;
188fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
189fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                if (param.length == 4) {
190fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                    p = byteArrayToInt(param, 0);
191fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                }
192fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                if (value.length == 2) {
193fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                    v = byteArrayToShort(value, 0);
194fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                }
195fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                if (p != -1 && v != -1) {
196fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                    l.onParameterChange(BassBoost.this, status, p, v);
197fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                }
198fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            }
199fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        }
200fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
201fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent
202fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    /**
203fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * Registers an OnParameterChangeListener interface.
204fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     * @param listener OnParameterChangeListener interface registered
205fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent     */
206fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    public void setParameterListener(OnParameterChangeListener listener) {
207fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        synchronized (mParamListenerLock) {
208fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            if (mParamListener == null) {
209fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                mParamListener = listener;
210fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                mBaseParamListener = new BaseParameterListener();
211fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent                super.setParameterListener(mBaseParamListener);
212fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent            }
213fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent        }
214fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent    }
215ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
216ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    /**
217ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * The Settings class regroups all bass boost parameters. It is used in
218ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * conjuntion with getProperties() and setProperties() methods to backup and restore
219ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * all parameters in a single call.
220ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     */
221ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    public static class Settings {
222ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        public short strength;
223ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
224ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        public Settings() {
225ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        }
226ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
227ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        /**
228ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent         * Settings class constructor from a key=value; pairs formatted string. The string is
229ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent         * typically returned by Settings.toString() method.
230ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent         * @throws IllegalArgumentException if the string is not correctly formatted.
231ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent         */
232ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        public Settings(String settings) {
233ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            StringTokenizer st = new StringTokenizer(settings, "=;");
234ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            int tokens = st.countTokens();
235ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            if (st.countTokens() != 3) {
236ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                throw new IllegalArgumentException("settings: " + settings);
237ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            }
238ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            String key = st.nextToken();
239ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            if (!key.equals("BassBoost")) {
240ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                throw new IllegalArgumentException(
241ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                        "invalid settings for BassBoost: " + key);
242ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            }
243ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            try {
244ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                key = st.nextToken();
245ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                if (!key.equals("strength")) {
246ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                    throw new IllegalArgumentException("invalid key name: " + key);
247ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                }
248ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                strength = Short.parseShort(st.nextToken());
249ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent             } catch (NumberFormatException nfe) {
250ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                throw new IllegalArgumentException("invalid value for key: " + key);
251ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            }
252ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        }
253ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
254ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        @Override
255ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        public String toString() {
256ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            String str = new String (
257ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                    "BassBoost"+
258ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                    ";strength="+Short.toString(strength)
259ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent                    );
260ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent            return str;
261ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        }
262ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    };
263ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
264ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
265ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    /**
266ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * Gets the bass boost properties. This method is useful when a snapshot of current
267ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * bass boost settings must be saved by the application.
268ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @return a BassBoost.Settings object containing all current parameters values
269ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws IllegalStateException
270ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws IllegalArgumentException
271ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws UnsupportedOperationException
272ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     */
273ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    public BassBoost.Settings getProperties()
274ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
275ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        Settings settings = new Settings();
276ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        short[] value = new short[1];
277ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        checkStatus(getParameter(PARAM_STRENGTH, value));
278ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        settings.strength = value[0];
279ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        return settings;
280ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    }
281ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent
282ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    /**
283ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * Sets the bass boost properties. This method is useful when bass boost settings have to
284ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * be applied from a previous backup.
28517cb280e7f1ac3556eac90ab08263712e0348cb9Eric Laurent     * @param settings a BassBoost.Settings object containing the properties to apply
286ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws IllegalStateException
287ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws IllegalArgumentException
288ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     * @throws UnsupportedOperationException
289ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent     */
290ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    public void setProperties(BassBoost.Settings settings)
291ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
292ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent        checkStatus(setParameter(PARAM_STRENGTH, settings.strength));
293ca57d1cc89d65dfbd59c749c5736574cd08c7bd3Eric Laurent    }
294fd84f97af4b44d54bba53bb85b31a6dbce07f6e2Eric Laurent}
295