Virtualizer.java revision 6a8eeda65f5e290365029672ef01f9039a83754f
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     * FIXME: replace deviceType by virtualization mode
180     */
181    private boolean getAnglesInt(int inputChannelMask, int deviceType, int[] angles)
182            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
183        // parameter check
184        if (inputChannelMask == AudioFormat.CHANNEL_INVALID) {
185            throw (new IllegalArgumentException(
186                    "Virtualizer: illegal CHANNEL_INVALID channel mask"));
187        }
188        int channelMask = inputChannelMask == AudioFormat.CHANNEL_OUT_DEFAULT ?
189                AudioFormat.CHANNEL_OUT_STEREO : inputChannelMask;
190        int nbChannels = AudioFormat.channelCountFromOutChannelMask(channelMask);
191        if ((angles != null) && (angles.length < (nbChannels * 3))) {
192            Log.e(TAG, "Size of array for angles cannot accomodate number of channels in mask ("
193                    + nbChannels + ")");
194            throw (new IllegalArgumentException(
195                    "Virtualizer: array for channel / angle pairs is too small: is " + angles.length
196                    + ", should be " + (nbChannels * 3)));
197        }
198
199        ByteBuffer paramsConverter = ByteBuffer.allocate(3 /* param + mask + device*/ * 4);
200        paramsConverter.order(ByteOrder.nativeOrder());
201        paramsConverter.putInt(PARAM_VIRTUAL_SPEAKER_ANGLES);
202        // convert channel mask to internal native representation
203        paramsConverter.putInt(AudioFormat.convertChannelOutMaskToNativeMask(channelMask));
204        // convert Java device type to internal representation
205        paramsConverter.putInt(AudioDevice.convertDeviceTypeToInternalDevice(deviceType));
206        // allocate an array to store the results
207        byte[] result = new byte[nbChannels * 4/*int to byte*/ * 3/*for mask, azimuth, elevation*/];
208
209        // call into the effect framework
210        int status = getParameter(paramsConverter.array(), result);
211        if (DEBUG) {
212            Log.v(TAG, "getAngles(0x" + Integer.toHexString(inputChannelMask) + ", 0x"
213                    + Integer.toHexString(deviceType) + ") returns " + status);
214        }
215
216        if (status >= 0) {
217            if (angles != null) {
218                // convert and copy the results
219                ByteBuffer resultConverter = ByteBuffer.wrap(result);
220                resultConverter.order(ByteOrder.nativeOrder());
221                for (int i = 0 ; i < nbChannels ; i++) {
222                    // write the channel mask
223                    angles[3 * i] = AudioFormat.convertNativeChannelMaskToOutMask(
224                            resultConverter.getInt((i * 4 * 3)));
225                    // write the azimuth
226                    angles[3 * i + 1] = resultConverter.getInt(i * 4 * 3 + 4);
227                    // write the elevation
228                    angles[3 * i + 2] = resultConverter.getInt(i * 4 * 3 + 8);
229                    if (DEBUG) {
230                        Log.v(TAG, "channel 0x" + Integer.toHexString(angles[3*i]).toUpperCase()
231                                + " at az=" + angles[3*i+1] + "deg"
232                                + " elev="  + angles[3*i+2] + "deg");
233                    }
234                }
235            }
236            return true;
237        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
238            // a BAD_VALUE return from getParameter indicates the configuration is not supported
239            // don't throw an exception, just return false
240            return false;
241        } else {
242            // something wrong may have happened
243            checkStatus(status);
244        }
245        // unexpected virtualizer behavior
246        Log.e(TAG, "unexpected status code " + status
247                + " after getParameter(PARAM_VIRTUAL_SPEAKER_ANGLES)");
248        return false;
249    }
250
251    /**
252     * Checks if the combination of a channel mask and device type is supported by this virtualizer.
253     * Some virtualizer implementations may only support binaural processing (i.e. only support
254     * headphone output), some may support transaural processing (i.e. for speaker output) for the
255     * built-in speakers. Use this method to query the virtualizer implementation capabilities.
256     * @param inputChannelMask the channel mask of the content to virtualize.
257     * @param deviceType the device type for which virtualization processing is to be performed.
258     * @return true if the combination of channel mask and output device type is supported, false
259     *    otherwise.
260     *    <br>An indication that a certain channel mask is not supported doesn't necessarily mean
261     *    you cannot play content with that channel mask, it more likely implies the content will
262     *    be downmixed before being virtualized. For instance a virtualizer that only supports a
263     *    mask such as {@link AudioFormat#CHANNEL_OUT_STEREO}
264     *    will still be able to process content with a mask of
265     *    {@link AudioFormat#CHANNEL_OUT_5POINT1}, but will downmix the content to stereo first, and
266     *    then will virtualize, as opposed to virtualizing each channel individually.
267     * @throws IllegalStateException
268     * @throws IllegalArgumentException
269     * @throws UnsupportedOperationException
270     * FIXME: replace deviceType by virtualization mode
271     */
272    public boolean canVirtualize(int inputChannelMask, int deviceType)
273            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
274        return getAnglesInt(inputChannelMask, deviceType, null);
275    }
276
277    /**
278     * Queries the virtual speaker angles (azimuth and elevation) for a combination of a channel
279     * mask and device type.
280     * If the virtualization configuration (mask and device) is supported (see
281     * {@link #canVirtualize(int, int)}, the array angles will contain upon return the
282     * definition of each virtual speaker and its azimuth and elevation angles relative to the
283     * listener.
284     * <br>Note that in some virtualizer implementations, the angles may be strength-dependent.
285     * @param inputChannelMask the channel mask of the content to virtualize.
286     * @param deviceType the device type for which virtualization processing is to be performed.
287     * @param angles a non-null array whose length is 3 times the number of channels in the channel
288     *    mask.
289     *    If the method indicates the configuration is supported, the array will contain upon return
290     *    triplets of values: for each channel <code>i</code> among the channels of the mask:
291     *    <ul>
292     *      <li>the element at index <code>3*i</code> in the array contains the speaker
293     *          identification (e.g. {@link AudioFormat#CHANNEL_OUT_FRONT_LEFT}),</li>
294     *      <li>the element at index <code>3*i+1</code> contains its corresponding azimuth angle
295     *          expressed in degrees, where 0 is the direction the listener faces, 180 is behind
296     *          the listener, and -90 is to her/his left,</li>
297     *      <li>the element at index <code>3*i+2</code> contains its corresponding elevation angle
298     *          where +90 is directly above the listener, 0 is the horizontal plane, and -90 is
299     *          directly below the listener.</li>
300     * @return true if the combination of channel mask and output device type is supported, false
301     *    otherwise.
302     * @throws IllegalStateException
303     * @throws IllegalArgumentException
304     * @throws UnsupportedOperationException
305     * FIXME: replace deviceType by virtualization mode
306     */
307    public boolean getSpeakerAngles(int inputChannelMask, int deviceType, int[] angles)
308            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
309        if (angles == null) {
310            throw (new IllegalArgumentException(
311                    "Virtualizer: illegal null channel / angle array"));
312        }
313
314        return getAnglesInt(inputChannelMask, deviceType, angles);
315    }
316
317    /**
318     * Forces the virtualizer effect to use the processing mode used for the given device type.
319     * The effect must be enabled for the forced mode to be applied.
320     * @param deviceType one of the device types defined.
321     * @return true if the processing mode for the device type is supported, and it is successfully
322     *     set, or forcing was successfully disabled, false otherwise.
323     * @throws IllegalStateException
324     * @throws IllegalArgumentException
325     * @throws UnsupportedOperationException
326     * FIXME: replace deviceType by virtualization mode
327     */
328    public boolean forceVirtualizationMode(int deviceType)
329            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
330        // convert Java device type to internal representation
331        int internalDevice = AudioDevice.convertDeviceTypeToInternalDevice(deviceType);
332
333        int status = setParameter(PARAM_FORCE_VIRTUALIZATION_MODE, internalDevice);
334
335        if (status >= 0) {
336            return true;
337        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
338            // a BAD_VALUE return from setParameter indicates the mode can't be forced to that
339            // of this device, don't throw an exception, just return false
340            return false;
341        } else {
342            // something wrong may have happened
343            checkStatus(status);
344        }
345        // unexpected virtualizer behavior
346        Log.e(TAG, "unexpected status code " + status
347                + " after setParameter(PARAM_FORCE_VIRTUALIZATION_MODE)");
348        return false;
349    }
350
351    /**
352     * Return the device type which reflects the virtualization mode being used, if any.
353     * @return a device type which reflects the virtualization mode being used.
354     *     If virtualization is not active, the device type will be TBD
355     *     Virtualization may not be active either because
356     *     the effect is not enabled or because the current output device is not compatible with
357     *     this virtualization implementation.
358     *     <p>Note that the return value may differ from a device type successfully set with
359     *     {@link #forceVirtualizationMode(int)} as the implementation
360     *     may use a single mode for multiple devices.
361     * @throws IllegalStateException
362     * @throws IllegalArgumentException
363     * @throws UnsupportedOperationException
364     * FIXME: replace deviceType by virtualization mode
365     */
366    public int getVirtualizationMode()
367            throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
368        int[] value = new int[1];
369        int status = getParameter(PARAM_VIRTUALIZATION_MODE, value);
370        if (status >= 0) {
371            return AudioDevice.convertInternalDeviceToDeviceType(value[0]);
372        } else if (status == AudioEffect.ERROR_BAD_VALUE) {
373            return AudioDevice.TYPE_UNKNOWN;
374        } else {
375            // something wrong may have happened
376            checkStatus(status);
377        }
378        // unexpected virtualizer behavior
379        Log.e(TAG, "unexpected status code " + status
380                + " after getParameter(PARAM_VIRTUALIZATION_MODE)");
381        return AudioDevice.TYPE_UNKNOWN;
382    }
383
384    /**
385     * The OnParameterChangeListener interface defines a method called by the Virtualizer when a
386     * parameter value has changed.
387     */
388    public interface OnParameterChangeListener  {
389        /**
390         * Method called when a parameter value has changed. The method is called only if the
391         * parameter was changed by another application having the control of the same
392         * Virtualizer engine.
393         * @param effect the Virtualizer on which the interface is registered.
394         * @param status status of the set parameter operation.
395         * @param param ID of the modified parameter. See {@link #PARAM_STRENGTH} ...
396         * @param value the new parameter value.
397         */
398        void onParameterChange(Virtualizer effect, int status, int param, short value);
399    }
400
401    /**
402     * Listener used internally to receive unformatted parameter change events from AudioEffect
403     * super class.
404     */
405    private class BaseParameterListener implements AudioEffect.OnParameterChangeListener {
406        private BaseParameterListener() {
407
408        }
409        public void onParameterChange(AudioEffect effect, int status, byte[] param, byte[] value) {
410            OnParameterChangeListener l = null;
411
412            synchronized (mParamListenerLock) {
413                if (mParamListener != null) {
414                    l = mParamListener;
415                }
416            }
417            if (l != null) {
418                int p = -1;
419                short v = -1;
420
421                if (param.length == 4) {
422                    p = byteArrayToInt(param, 0);
423                }
424                if (value.length == 2) {
425                    v = byteArrayToShort(value, 0);
426                }
427                if (p != -1 && v != -1) {
428                    l.onParameterChange(Virtualizer.this, status, p, v);
429                }
430            }
431        }
432    }
433
434    /**
435     * Registers an OnParameterChangeListener interface.
436     * @param listener OnParameterChangeListener interface registered
437     */
438    public void setParameterListener(OnParameterChangeListener listener) {
439        synchronized (mParamListenerLock) {
440            if (mParamListener == null) {
441                mParamListener = listener;
442                mBaseParamListener = new BaseParameterListener();
443                super.setParameterListener(mBaseParamListener);
444            }
445        }
446    }
447
448    /**
449     * The Settings class regroups all virtualizer parameters. It is used in
450     * conjuntion with getProperties() and setProperties() methods to backup and restore
451     * all parameters in a single call.
452     */
453    public static class Settings {
454        public short strength;
455
456        public Settings() {
457        }
458
459        /**
460         * Settings class constructor from a key=value; pairs formatted string. The string is
461         * typically returned by Settings.toString() method.
462         * @throws IllegalArgumentException if the string is not correctly formatted.
463         */
464        public Settings(String settings) {
465            StringTokenizer st = new StringTokenizer(settings, "=;");
466            int tokens = st.countTokens();
467            if (st.countTokens() != 3) {
468                throw new IllegalArgumentException("settings: " + settings);
469            }
470            String key = st.nextToken();
471            if (!key.equals("Virtualizer")) {
472                throw new IllegalArgumentException(
473                        "invalid settings for Virtualizer: " + key);
474            }
475            try {
476                key = st.nextToken();
477                if (!key.equals("strength")) {
478                    throw new IllegalArgumentException("invalid key name: " + key);
479                }
480                strength = Short.parseShort(st.nextToken());
481             } catch (NumberFormatException nfe) {
482                throw new IllegalArgumentException("invalid value for key: " + key);
483            }
484        }
485
486        @Override
487        public String toString() {
488            String str = new String (
489                    "Virtualizer"+
490                    ";strength="+Short.toString(strength)
491                    );
492            return str;
493        }
494    };
495
496
497    /**
498     * Gets the virtualizer properties. This method is useful when a snapshot of current
499     * virtualizer settings must be saved by the application.
500     * @return a Virtualizer.Settings object containing all current parameters values
501     * @throws IllegalStateException
502     * @throws IllegalArgumentException
503     * @throws UnsupportedOperationException
504     */
505    public Virtualizer.Settings getProperties()
506    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
507        Settings settings = new Settings();
508        short[] value = new short[1];
509        checkStatus(getParameter(PARAM_STRENGTH, value));
510        settings.strength = value[0];
511        return settings;
512    }
513
514    /**
515     * Sets the virtualizer properties. This method is useful when virtualizer settings have to
516     * be applied from a previous backup.
517     * @param settings a Virtualizer.Settings object containing the properties to apply
518     * @throws IllegalStateException
519     * @throws IllegalArgumentException
520     * @throws UnsupportedOperationException
521     */
522    public void setProperties(Virtualizer.Settings settings)
523    throws IllegalStateException, IllegalArgumentException, UnsupportedOperationException {
524        checkStatus(setParameter(PARAM_STRENGTH, settings.strength));
525    }
526}
527