Virtualizer.java revision d2bebb3ab86177c0d27664af86b30b7dce2c9bcb
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.media.audiofx;
18
19import android.media.AudioDevice;
20import android.media.AudioFormat;
21import android.media.audiofx.AudioEffect;
22import android.util.Log;
23
24import java.nio.ByteBuffer;
25import java.nio.ByteOrder;
26import java.util.StringTokenizer;
27
28
29/**
30 * An audio virtualizer is a general name for an effect to spatialize audio channels. The exact
31 * behavior of this effect is dependent on the number of audio input channels and the types and
32 * number of audio output channels of the device. For example, in the case of a stereo input and
33 * stereo headphone output, a stereo widening effect is used when this effect is turned on.
34 * <p>An application creates a Virtualizer object to instantiate and control a virtualizer engine
35 * in the audio framework.
36 * <p>The methods, parameter types and units exposed by the Virtualizer implementation are directly
37 * mapping those defined by the OpenSL ES 1.0.1 Specification (http://www.khronos.org/opensles/)
38 * for the SLVirtualizerItf interface. Please refer to this specification for more details.
39 * <p>To attach the Virtualizer to a particular AudioTrack or MediaPlayer, specify the audio session
40 * ID of this AudioTrack or MediaPlayer when constructing the Virtualizer.
41 * <p>NOTE: attaching a Virtualizer to the global audio output mix by use of session 0 is
42 * deprecated.
43 * <p>See {@link android.media.MediaPlayer#getAudioSessionId()} for details on audio sessions.
44 * <p>See {@link android.media.audiofx.AudioEffect} class for more details on controlling
45 * audio effects.
46 */
47
48public class Virtualizer extends AudioEffect {
49
50    private final static String TAG = "Virtualizer";
51    private final static boolean DEBUG = false;
52
53    // These constants must be synchronized with those in
54    //        system/media/audio_effects/include/audio_effects/effect_virtualizer.h
55    /**
56     * Is strength parameter supported by virtualizer engine. Parameter ID for getParameter().
57     */
58    public static final int PARAM_STRENGTH_SUPPORTED = 0;
59    /**
60     * Virtualizer effect strength. Parameter ID for
61     * {@link android.media.audiofx.Virtualizer.OnParameterChangeListener}
62     */
63    public static final int PARAM_STRENGTH = 1;
64    /**
65     * @hide
66     * Parameter ID to query the virtual speaker angles for a channel mask / device configuration.
67     */
68    public static final int PARAM_VIRTUAL_SPEAKER_ANGLES = 2;
69    /**
70     * @hide
71     * Parameter ID to force the virtualization mode to be that of a specific device
72     */
73    public static final int PARAM_FORCE_VIRTUALIZATION_MODE = 3;
74    /**
75     * @hide
76     * Parameter ID to query the current virtualization mode.
77     */
78    public static final int PARAM_VIRTUALIZATION_MODE = 4;
79
80    /**
81     * Indicates if strength parameter is supported by the virtualizer engine
82     */
83    private boolean mStrengthSupported = false;
84
85    /**
86     * Registered listener for parameter changes.
87     */
88    private OnParameterChangeListener mParamListener = null;
89
90    /**
91     * Listener used internally to to receive raw parameter change event from AudioEffect super class
92     */
93    private BaseParameterListener mBaseParamListener = null;
94
95    /**
96     * Lock for access to mParamListener
97     */
98    private final Object mParamListenerLock = new Object();
99
100    /**
101     * Class constructor.
102     * @param priority the priority level requested by the application for controlling the Virtualizer
103     * engine. As the same engine can be shared by several applications, this parameter indicates
104     * how much the requesting application needs control of effect parameters. The normal priority
105     * is 0, above normal is a positive number, below normal a negative number.
106     * @param audioSession  system wide unique audio session identifier. The Virtualizer will
107     * be attached to the MediaPlayer or AudioTrack in the same audio session.
108     *
109     * @throws java.lang.IllegalStateException
110     * @throws java.lang.IllegalArgumentException
111     * @throws java.lang.UnsupportedOperationException
112     * @throws java.lang.RuntimeException
113     */
114    public Virtualizer(int priority, int audioSession)
115    throws IllegalStateException, IllegalArgumentException,
116           UnsupportedOperationException, RuntimeException {
117        super(EFFECT_TYPE_VIRTUALIZER, EFFECT_TYPE_NULL, priority, audioSession);
118
119        if (audioSession == 0) {
120            Log.w(TAG, "WARNING: attaching a Virtualizer to global output mix is deprecated!");
121        }
122
123        int[] value = new int[1];
124        checkStatus(getParameter(PARAM_STRENGTH_SUPPORTED, value));
125        mStrengthSupported = (value[0] != 0);
126    }
127
128    /**
129     * Indicates whether setting strength is supported. If this method returns false, only one
130     * strength is supported and the setStrength() method always rounds to that value.
131     * @return true is strength parameter is supported, false otherwise
132     */
133    public boolean getStrengthSupported() {
134       return mStrengthSupported;
135    }
136
137    /**
138     * Sets the strength of the virtualizer effect. If the implementation does not support per mille
139     * accuracy for setting the strength, it is allowed to round the given strength to the nearest
140     * supported value. You can use the {@link #getRoundedStrength()} method to query the
141     * (possibly rounded) value that was actually set.
142     * @param strength strength of the effect. The valid range for strength strength is [0, 1000],
143     * where 0 per mille designates the mildest effect and 1000 per mille designates the strongest.
144     * @throws IllegalStateException
145     * @throws IllegalArgumentException
146     * @throws UnsupportedOperationException
147     */
148    public void setStrength(short strength)
149    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
150        checkStatus(setParameter(PARAM_STRENGTH, strength));
151    }
152
153    /**
154     * Gets the current strength of the effect.
155     * @return the strength of the effect. The valid range for strength is [0, 1000], where 0 per
156     * mille designates the mildest effect and 1000 per mille the strongest
157     * @throws IllegalStateException
158     * @throws IllegalArgumentException
159     * @throws UnsupportedOperationException
160     */
161    public short getRoundedStrength()
162    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
163        short[] value = new short[1];
164        checkStatus(getParameter(PARAM_STRENGTH, value));
165        return value[0];
166    }
167
168    /**
169     * Checks if a configuration is supported, and query the virtual speaker angles.
170     * @param inputChannelMask
171     * @param deviceType
172     * @param angles if non-null: array in which the angles will be written. If null, no angles
173     *    are returned
174     * @return true if the combination of channel mask and output device type is supported, false
175     *    otherwise
176     * @throws IllegalStateException
177     * @throws IllegalArgumentException
178     * @throws UnsupportedOperationException
179     */
180    private boolean getAnglesInt(int inputChannelMask, int deviceType, int[] angles)
181            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
182        // parameter check
183        if (inputChannelMask == AudioFormat.CHANNEL_INVALID) {
184            throw (new IllegalArgumentException(
185                    "Virtualizer: illegal CHANNEL_INVALID channel mask"));
186        }
187        int channelMask = inputChannelMask == AudioFormat.CHANNEL_OUT_DEFAULT ?
188                AudioFormat.CHANNEL_OUT_STEREO : inputChannelMask;
189        int nbChannels = AudioFormat.channelCountFromOutChannelMask(channelMask);
190        if ((angles != null) && (angles.length < (nbChannels * 3))) {
191            Log.e(TAG, "Size of array for angles cannot accomodate number of channels in mask ("
192                    + nbChannels + ")");
193            throw (new IllegalArgumentException(
194                    "Virtualizer: array for channel / angle pairs is too small: is " + angles.length
195                    + ", should be " + (nbChannels * 3)));
196        }
197
198        ByteBuffer paramsConverter = ByteBuffer.allocate(3 /* param + mask + device*/ * 4);
199        paramsConverter.order(ByteOrder.nativeOrder());
200        paramsConverter.putInt(PARAM_VIRTUAL_SPEAKER_ANGLES);
201        // convert channel mask to internal native representation
202        paramsConverter.putInt(AudioFormat.convertChannelOutMaskToNativeMask(channelMask));
203        // convert Java device type to internal representation
204        paramsConverter.putInt(AudioDevice.convertDeviceTypeToInternalDevice(deviceType));
205        // allocate an array to store the results
206        byte[] result = new byte[nbChannels * 4/*int to byte*/ * 3/*for mask, azimuth, elevation*/];
207
208        // call into the effect framework
209        int status = getParameter(paramsConverter.array(), result);
210        if (DEBUG) {
211            Log.v(TAG, "getAngles(0x" + Integer.toHexString(inputChannelMask) + ", 0x"
212                    + Integer.toHexString(deviceType) + ") returns " + status);
213        }
214
215        if (status >= 0) {
216            if (angles != null) {
217                // convert and copy the results
218                ByteBuffer resultConverter = ByteBuffer.wrap(result);
219                resultConverter.order(ByteOrder.nativeOrder());
220                for (int i = 0 ; i < nbChannels ; i++) {
221                    // write the channel mask
222                    angles[3 * i] = AudioFormat.convertNativeChannelMaskToOutMask(
223                            resultConverter.getInt((i * 4 * 3)));
224                    // write the azimuth
225                    angles[3 * i + 1] = resultConverter.getInt(i * 4 * 3 + 4);
226                    // write the elevation
227                    angles[3 * i + 2] = resultConverter.getInt(i * 4 * 3 + 8);
228                    if (DEBUG) {
229                        Log.v(TAG, "channel 0x" + Integer.toHexString(angles[3*i]).toUpperCase()
230                                + " at az=" + angles[3*i+1] + "deg"
231                                + " elev="  + angles[3*i+2] + "deg");
232                    }
233                }
234            }
235            return true;
236        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
237            // a BAD_VALUE return from getParameter indicates the configuration is not supported
238            // don't throw an exception, just return false
239            return false;
240        } else {
241            // something wrong may have happened
242            checkStatus(status);
243        }
244        // unexpected virtualizer behavior
245        Log.e(TAG, "unexpected status code " + status
246                + " after getParameter(PARAM_VIRTUAL_SPEAKER_ANGLES)");
247        return false;
248    }
249
250    /**
251     * @hide
252     * CANDIDATE FOR PUBLIC API
253     * Checks if the combination of a channel mask and device type is supported by this virtualizer.
254     * Some virtualizer implementations may only support binaural processing (i.e. only support
255     * headphone output), some may support transaural processing (i.e. for speaker output) for the
256     * built-in speakers. Use this method to query the virtualizer implementation capabilities.
257     * @param inputChannelMask the channel mask of the content to virtualize.
258     * @param deviceType the device type for which virtualization processing is to be performed.
259     *    Valid values are the device types defined in {@link AudioDevice}.
260     * @return true if the combination of channel mask and output device type is supported, false
261     *    otherwise.
262     *    <br>An indication that a certain channel mask is not supported doesn't necessarily mean
263     *    you cannot play content with that channel mask, it more likely implies the content will
264     *    be downmixed before being virtualized. For instance a virtualizer that only supports a
265     *    mask such as {@link AudioFormat#CHANNEL_OUT_STEREO}
266     *    will still be able to process content with a mask of
267     *    {@link AudioFormat#CHANNEL_OUT_5POINT1}, but will downmix the content to stereo first, and
268     *    then will virtualize, as opposed to virtualizing each channel individually.
269     * @throws IllegalStateException
270     * @throws IllegalArgumentException
271     * @throws UnsupportedOperationException
272     */
273    public boolean canVirtualize(int inputChannelMask, int deviceType)
274            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
275        return getAnglesInt(inputChannelMask, deviceType, null);
276    }
277
278    /**
279     * @hide
280     * CANDIDATE FOR PUBLIC API
281     * Queries the virtual speaker angles (azimuth and elevation) for a combination of a channel
282     * mask and device type.
283     * If the virtualization configuration (mask and device) is supported (see
284     * {@link #canVirtualize(int, int)}, the array angles will contain upon return the
285     * definition of each virtual speaker and its azimuth and elevation angles relative to the
286     * listener.
287     * <br>Note that in some virtualizer implementations, the angles may be strength-dependent.
288     * @param inputChannelMask the channel mask of the content to virtualize.
289     * @param deviceType the device type for which virtualization processing is to be performed.
290     *    Valid values are the device types defined in {@link AudioDevice}.
291     * @param angles a non-null array whose length is 3 times the number of channels in the channel
292     *    mask.
293     *    If the method indicates the configuration is supported, the array will contain upon return
294     *    triplets of values: for each channel <code>i</code> among the channels of the mask:
295     *    <ul>
296     *      <li>the element at index <code>3*i</code> in the array contains the speaker
297     *          identification (e.g. {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT}),</li>
298     *      <li>the element at index <code>3*i+1</code> contains its corresponding azimuth angle
299     *          expressed in degrees, where 0 is the direction the listener faces, 180 is behind
300     *          the listener, and -90 is to her/his left,</li>
301     *      <li>the element at index <code>3*i+2</code> contains its corresponding elevation angle
302     *          where +90 is directly above the listener, 0 is the horizontal plane, and -90 is
303     *          directly below the listener.</li>
304     * @return true if the combination of channel mask and output device type is supported, false
305     *    otherwise.
306     * @throws IllegalStateException
307     * @throws IllegalArgumentException
308     * @throws UnsupportedOperationException
309     */
310    public boolean getSpeakerAngles(int inputChannelMask, int deviceType, int[] angles)
311            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
312        if (angles == null) {
313            throw (new IllegalArgumentException(
314                    "Virtualizer: illegal null channel / angle array"));
315        }
316
317        return getAnglesInt(inputChannelMask, deviceType, angles);
318    }
319
320    /**
321     * @hide
322     * CANDIDATE FOR PUBLIC API
323     * Forces the virtualizer effect to use the processing mode used for the given device type.
324     * The effect must be enabled for the forced mode to be applied.
325     * @param deviceType one of the device types defined in {@link AudioDevice}.
326     *     Use {@link AudioDevice#DEVICE_TYPE_UNKNOWN} to return to the non-forced mode.
327     * @return true if the processing mode for the device type is supported, and it is successfully
328     *     set, or forcing was successfully disabled with {@link AudioDevice#DEVICE_TYPE_UNKNOWN},
329     *     false otherwise.
330     * @throws IllegalStateException
331     * @throws IllegalArgumentException
332     * @throws UnsupportedOperationException
333     */
334    public boolean forceVirtualizationMode(int deviceType)
335            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
336        // convert Java device type to internal representation
337        int internalDevice = AudioDevice.convertDeviceTypeToInternalDevice(deviceType);
338
339        int status = setParameter(PARAM_FORCE_VIRTUALIZATION_MODE, internalDevice);
340
341        if (status >= 0) {
342            return true;
343        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
344            // a BAD_VALUE return from setParameter indicates the mode can't be forced to that
345            // of this device, don't throw an exception, just return false
346            return false;
347        } else {
348            // something wrong may have happened
349            checkStatus(status);
350        }
351        // unexpected virtualizer behavior
352        Log.e(TAG, "unexpected status code " + status
353                + " after setParameter(PARAM_FORCE_VIRTUALIZATION_MODE)");
354        return false;
355    }
356
357    /**
358     * @hide
359     * CANDIDATE FOR PUBLIC API
360     * Return the device type which reflects the virtualization mode being used, if any.
361     * @return a device type (as defined in {@link AudioDevice}) which reflects the virtualization
362     *     mode being used.
363     *     If virtualization is not active, the device type will be
364     *     {@link AudioDevice#DEVICE_TYPE_UNKNOWN}. Virtualization may not be active either because
365     *     the effect is not enabled or because the current output device is not compatible with
366     *     this virtualization implementation.
367     */
368    public int getVirtualizationMode() {
369        int[] value = new int[1];
370        int status = getParameter(PARAM_VIRTUALIZATION_MODE, value);
371        if (status >= 0) {
372            return AudioDevice.convertInternalDeviceToDeviceType(value[0]);
373        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
374            return AudioDevice.DEVICE_TYPE_UNKNOWN;
375        } else {
376            // something wrong may have happened
377            checkStatus(status);
378        }
379        // unexpected virtualizer behavior
380        Log.e(TAG, "unexpected status code " + status
381                + " after getParameter(PARAM_VIRTUALIZATION_MODE)");
382        return AudioDevice.DEVICE_TYPE_UNKNOWN;
383    }
384
385    /**
386     * The OnParameterChangeListener interface defines a method called by the Virtualizer when a
387     * parameter value has changed.
388     */
389    public interface OnParameterChangeListener  {
390        /**
391         * Method called when a parameter value has changed. The method is called only if the
392         * parameter was changed by another application having the control of the same
393         * Virtualizer engine.
394         * @param effect the Virtualizer on which the interface is registered.
395         * @param status status of the set parameter operation.
396         * @param param ID of the modified parameter. See {@link #PARAM_STRENGTH} ...
397         * @param value the new parameter value.
398         */
399        void onParameterChange(Virtualizer effect, int status, int param, short value);
400    }
401
402    /**
403     * Listener used internally to receive unformatted parameter change events from AudioEffect
404     * super class.
405     */
406    private class BaseParameterListener implements AudioEffect.OnParameterChangeListener {
407        private BaseParameterListener() {
408
409        }
410        public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) {
411            OnParameterChangeListener l = null;
412
413            synchronized (mParamListenerLock) {
414                if (mParamListener != null) {
415                    l = mParamListener;
416                }
417            }
418            if (l != null) {
419                int p = -1;
420                short v = -1;
421
422                if (param.length == 4) {
423                    p = byteArrayToInt(param, 0);
424                }
425                if (value.length == 2) {
426                    v = byteArrayToShort(value, 0);
427                }
428                if (p != -1 && v != -1) {
429                    l.onParameterChange(Virtualizer.this, status, p, v);
430                }
431            }
432        }
433    }
434
435    /**
436     * Registers an OnParameterChangeListener interface.
437     * @param listener OnParameterChangeListener interface registered
438     */
439    public void setParameterListener(OnParameterChangeListener listener) {
440        synchronized (mParamListenerLock) {
441            if (mParamListener == null) {
442                mParamListener = listener;
443                mBaseParamListener = new BaseParameterListener();
444                super.setParameterListener(mBaseParamListener);
445            }
446        }
447    }
448
449    /**
450     * The Settings class regroups all virtualizer parameters. It is used in
451     * conjuntion with getProperties() and setProperties() methods to backup and restore
452     * all parameters in a single call.
453     */
454    public static class Settings {
455        public short strength;
456
457        public Settings() {
458        }
459
460        /**
461         * Settings class constructor from a key=value; pairs formatted string. The string is
462         * typically returned by Settings.toString() method.
463         * @throws IllegalArgumentException if the string is not correctly formatted.
464         */
465        public Settings(String settings) {
466            StringTokenizer st = new StringTokenizer(settings, "=;");
467            int tokens = st.countTokens();
468            if (st.countTokens() != 3) {
469                throw new IllegalArgumentException("settings: " + settings);
470            }
471            String key = st.nextToken();
472            if (!key.equals("Virtualizer")) {
473                throw new IllegalArgumentException(
474                        "invalid settings for Virtualizer: " + key);
475            }
476            try {
477                key = st.nextToken();
478                if (!key.equals("strength")) {
479                    throw new IllegalArgumentException("invalid key name: " + key);
480                }
481                strength = Short.parseShort(st.nextToken());
482             } catch (NumberFormatException nfe) {
483                throw new IllegalArgumentException("invalid value for key: " + key);
484            }
485        }
486
487        @Override
488        public String toString() {
489            String str = new String (
490                    "Virtualizer"+
491                    ";strength="+Short.toString(strength)
492                    );
493            return str;
494        }
495    };
496
497
498    /**
499     * Gets the virtualizer properties. This method is useful when a snapshot of current
500     * virtualizer settings must be saved by the application.
501     * @return a Virtualizer.Settings object containing all current parameters values
502     * @throws IllegalStateException
503     * @throws IllegalArgumentException
504     * @throws UnsupportedOperationException
505     */
506    public Virtualizer.Settings getProperties()
507    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
508        Settings settings = new Settings();
509        short[] value = new short[1];
510        checkStatus(getParameter(PARAM_STRENGTH, value));
511        settings.strength = value[0];
512        return settings;
513    }
514
515    /**
516     * Sets the virtualizer properties. This method is useful when virtualizer settings have to
517     * be applied from a previous backup.
518     * @param settings a Virtualizer.Settings object containing the properties to apply
519     * @throws IllegalStateException
520     * @throws IllegalArgumentException
521     * @throws UnsupportedOperationException
522     */
523    public void setProperties(Virtualizer.Settings settings)
524    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
525        checkStatus(setParameter(PARAM_STRENGTH, settings.strength));
526    }
527}
528