AudioTrack.java revision 1b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.media;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.ref.WeakReference;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.IllegalArgumentException;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.lang.IllegalStateException;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Handler;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Looper;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Message;
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.media.AudioManager;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The AudioTrack class manages and plays a single audio resource for Java applications.
325c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten * It allows streaming PCM audio buffers to the audio hardware for playback. This is
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * achieved by "pushing" the data to the AudioTrack object using one of the
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *  {@link #write(byte[], int, int)} and {@link #write(short[], int, int)} methods.
3588bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath *
36ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi * <p>An AudioTrack instance can operate under two modes: static or streaming.<br>
37ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi * In Streaming mode, the application writes a continuous stream of data to the AudioTrack, using
3888bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * one of the {@code write()} methods. These are blocking and return when the data has been
3988bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * transferred from the Java layer to the native layer and queued for playback. The streaming
4088bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * mode is most useful when playing blocks of audio data that for instance are:
4188bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath *
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <ul>
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *   <li>too big to fit in memory because of the duration of the sound to play,</li>
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *   <li>too big to fit in memory because of the characteristics of the audio data
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *         (high sampling rate, bits per sample ...)</li>
46ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi *   <li>received or generated while previously queued audio is playing.</li>
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </ul>
4888bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath *
495c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten * The static mode should be chosen when dealing with short sounds that fit in memory and
5088bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * that need to be played with the smallest latency possible. The static mode will
5188bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * therefore be preferred for UI and game sounds that are played often, and with the
5288bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath * smallest overhead possible.
5388bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath *
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>Upon creation, an AudioTrack object initializes its associated audio buffer.
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The size of this buffer, specified during the construction, determines how long an AudioTrack
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * can play before running out of data.<br>
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * For an AudioTrack using the static mode, this size is the maximum size of the sound that can
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * be played from it.<br>
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * For the streaming mode, data will be written to the hardware in chunks of
605c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten * sizes less than or equal to the total buffer size.
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class AudioTrack
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project{
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Constants
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Minimum value for a channel volume */
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final float VOLUME_MIN = 0.0f;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Maximum value for a channel volume */
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final float VOLUME_MAX = 1.0f;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
72ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi    /** indicates AudioTrack state is stopped */
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int PLAYSTATE_STOPPED = 1;  // matches SL_PLAYSTATE_STOPPED
74ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi    /** indicates AudioTrack state is paused */
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int PLAYSTATE_PAUSED  = 2;  // matches SL_PLAYSTATE_PAUSED
76ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi    /** indicates AudioTrack state is playing */
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int PLAYSTATE_PLAYING = 3;  // matches SL_PLAYSTATE_PLAYING
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
795c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten    // keep these values in sync with android_media_AudioTrack.cpp
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Creation mode where audio data is transferred from Java to the native layer
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * only once before the audio starts playing.
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_STATIC = 0;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Creation mode where audio data is streamed from Java to the native layer
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * as the audio is playing.
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_STREAM = 1;
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
92ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * State of an AudioTrack that was not successfully initialized upon creation.
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STATE_UNINITIALIZED = 0;
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * State of an AudioTrack that is ready to be used.
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STATE_INITIALIZED   = 1;
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * State of a successfully initialized AudioTrack that uses static data,
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * but that hasn't received that data yet.
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STATE_NO_STATIC_DATA = 2;
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Error codes:
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // to keep in sync with frameworks/base/core/jni/android_media_AudioTrack.cpp
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Denotes a successful operation.
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public  static final int SUCCESS                               = 0;
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Denotes a generic operation failure.
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public  static final int ERROR                                 = -1;
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Denotes a failure due to the use of an invalid value.
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public  static final int ERROR_BAD_VALUE                       = -2;
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Denotes a failure due to the improper use of a method.
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public  static final int ERROR_INVALID_OPERATION               = -3;
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int ERROR_NATIVESETUP_AUDIOSYSTEM         = -16;
125a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    private static final int ERROR_NATIVESETUP_INVALIDCHANNELMASK  = -17;
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int ERROR_NATIVESETUP_INVALIDFORMAT       = -18;
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int ERROR_NATIVESETUP_INVALIDSTREAMTYPE   = -19;
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int ERROR_NATIVESETUP_NATIVEINITFAILED    = -20;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Events:
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // to keep in sync with frameworks/base/include/media/AudioTrack.h
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
133ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Event id denotes when playback head has reached a previously set marker.
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int NATIVE_EVENT_MARKER  = 3;
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
137ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Event id denotes when previously set update period has elapsed during playback.
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static final int NATIVE_EVENT_NEW_POS = 4;
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final static String TAG = "AudioTrack-Java";
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------------------------------------------------------------
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Member variables
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
148ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Indicates the state of the AudioTrack instance.
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mState = STATE_UNINITIALIZED;
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
152ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Indicates the play state of the AudioTrack instance.
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mPlayState = PLAYSTATE_STOPPED;
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lock to make sure mPlayState updates are reflecting the actual state of the object.
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private final Object mPlayStateLock = new Object();
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1604df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * The listener the AudioTrack notifies when the playback position reaches a marker
1614df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * or for periodic updates during the progression of the playback head.
1624df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     *  @see #setPlaybackPositionUpdateListener(OnPlaybackPositionUpdateListener)
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1644df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    private OnPlaybackPositionUpdateListener mPositionListener = null;
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
166ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Lock to protect event listener updates against event notifications.
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1684df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    private final Object mPositionListenerLock = new Object();
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Size of the native audio buffer.
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNativeBufferSizeInBytes = 0;
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
174ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Handler for marker events coming from the native code.
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1764df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    private NativeEventHandlerDelegate mEventHandlerDelegate = null;
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
178ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Looper associated with the thread that creates the AudioTrack instance.
179105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     */
180105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    private Looper mInitializationLooper = null;
181105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project    /**
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The audio data sampling rate in Hz.
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1845c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten    private int mSampleRate; // initialized by all constructors
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1863026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent     * The number of audio output channels (1 is mono, 2 is stereo).
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mChannelCount = 1;
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1903026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent     * The audio channel mask.
1913026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent     */
1923026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    private int mChannels = AudioFormat.CHANNEL_OUT_MONO;
1933026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent
1943026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    /**
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The type of the audio stream to play. See
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
1975c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
198b282e08ab641d4fc74d0324b7a0ce30926638dd5Glenn Kasten     *   {@link AudioManager#STREAM_ALARM}, {@link AudioManager#STREAM_NOTIFICATION}, and
199b282e08ab641d4fc74d0324b7a0ce30926638dd5Glenn Kasten     *   {@link AudioManager#STREAM_DTMF}.
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mStreamType = AudioManager.STREAM_MUSIC;
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The way audio is consumed by the hardware, streaming or static.
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mDataLoadMode = MODE_STREAM;
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
207ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * The current audio channel configuration.
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
209a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    private int mChannelConfiguration = AudioFormat.CHANNEL_OUT_MONO;
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The encoding of the audio samples.
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see AudioFormat#ENCODING_PCM_8BIT
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see AudioFormat#ENCODING_PCM_16BIT
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
216619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    /**
217619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * Audio session ID
218619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     */
219619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    private int mSessionId = 0;
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------------------
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Used exclusively by native code
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
226ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Accessed by native methods: provides access to C++ AudioTrack object.
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SuppressWarnings("unused")
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNativeTrackInJavaObj;
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Accessed by native methods: provides access to the JNI data (i.e. resources used by
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the native AudioTrack object, but not stored in it).
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SuppressWarnings("unused")
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mJniData;
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------------------------------------------------------------
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Constructor, Finalize
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Class constructor.
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType the type of the audio stream. See
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
2455c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
2465c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     *   {@link AudioManager#STREAM_ALARM}, and {@link AudioManager#STREAM_NOTIFICATION}.
2475c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * @param sampleRateInHz the sample rate expressed in Hertz.
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param channelConfig describes the configuration of the audio channels.
249a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
250a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param audioFormat the format in which the audio data is represented.
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   {@link AudioFormat#ENCODING_PCM_8BIT}
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is read
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   from for playback. If using the AudioTrack in streaming mode, you can write data into
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   this buffer in smaller chunks than this size. If using the AudioTrack in static mode,
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   this is the maximum size of the sound that will be played for this instance.
258ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *   See {@link #getMinBufferSize(int, int, int)} to determine the minimum required buffer size
259ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *   for the successful creation of an AudioTrack instance in streaming mode. Using values
260ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *   smaller than getMinBufferSize() will result in an initialization failure.
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws java.lang.IllegalArgumentException
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int bufferSizeInBytes, int mode)
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    throws IllegalArgumentException {
267619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        this(streamType, sampleRateInHz, channelConfig, audioFormat,
268619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent                bufferSizeInBytes, mode, 0);
269619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
270619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
271619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    /**
272619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * Class constructor with audio session. Use this constructor when the AudioTrack must be
273619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * attached to a particular audio session. The primary use of the audio session ID is to
274619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * associate audio effects to a particular instance of AudioTrack: if an audio session ID
275619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * is provided when creating an AudioEffect, this effect will be applied only to audio tracks
276619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * and media players in the same session and not to the output mix.
277619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * When an AudioTrack is created without specifying a session, it will create its own session
278619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * which can be retreived by calling the {@link #getAudioSessionId()} method.
2795c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * If a non-zero session ID is provided, this AudioTrack will share effects attached to this
2805c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * session
2815c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * with all other media players or audio tracks in the same session, otherwise a new session
2825c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * will be created for this track if none is supplied.
283619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param streamType the type of the audio stream. See
284619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   {@link AudioManager#STREAM_VOICE_CALL}, {@link AudioManager#STREAM_SYSTEM},
2855c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     *   {@link AudioManager#STREAM_RING}, {@link AudioManager#STREAM_MUSIC},
2865c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     *   {@link AudioManager#STREAM_ALARM}, and {@link AudioManager#STREAM_NOTIFICATION}.
2875c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * @param sampleRateInHz the sample rate expressed in Hertz.
288619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param channelConfig describes the configuration of the audio channels.
289619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
290619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
291619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param audioFormat the format in which the audio data is represented.
292619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
293619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   {@link AudioFormat#ENCODING_PCM_8BIT}
294619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param bufferSizeInBytes the total size (in bytes) of the buffer where audio data is read
295619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   from for playback. If using the AudioTrack in streaming mode, you can write data into
296619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   this buffer in smaller chunks than this size. If using the AudioTrack in static mode,
297619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   this is the maximum size of the sound that will be played for this instance.
298619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   See {@link #getMinBufferSize(int, int, int)} to determine the minimum required buffer size
299619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   for the successful creation of an AudioTrack instance in streaming mode. Using values
300619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *   smaller than getMinBufferSize() will result in an initialization failure.
301619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param mode streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}
302619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @param sessionId Id of audio session the AudioTrack must be attached to
303619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @throws java.lang.IllegalArgumentException
304619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     */
305619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
306619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent            int bufferSizeInBytes, int mode, int sessionId)
307619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    throws IllegalArgumentException {
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mState = STATE_UNINITIALIZED;
3095c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten
3105c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten        // remember which looper is associated with the AudioTrack instantiation
311105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        if ((mInitializationLooper = Looper.myLooper()) == null) {
312105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project            mInitializationLooper = Looper.getMainLooper();
313105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project        }
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        audioParamCheck(streamType, sampleRateInHz, channelConfig, audioFormat, mode);
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        audioBuffSizeCheck(bufferSizeInBytes);
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
319619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        if (sessionId < 0) {
320619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent            throw (new IllegalArgumentException("Invalid audio session ID: "+sessionId));
321619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        }
322619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
323619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        int[] session = new int[1];
324619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        session[0] = sessionId;
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // native initialization
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int initResult = native_setup(new WeakReference<AudioTrack>(this),
3273026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent                mStreamType, mSampleRate, mChannels, mAudioFormat,
328619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent                mNativeBufferSizeInBytes, mDataLoadMode, session);
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (initResult != SUCCESS) {
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            loge("Error code "+initResult+" when initializing AudioTrack.");
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return; // with mState == STATE_UNINITIALIZED
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
334619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        mSessionId = session[0];
335619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mDataLoadMode == MODE_STATIC) {
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mState = STATE_NO_STATIC_DATA;
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mState = STATE_INITIALIZED;
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3431b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi    // mask of all the channels supported by this implementation
3441b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi    private static final int SUPPORTED_OUT_CHANNELS =
3451b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_FRONT_LEFT |
3461b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_FRONT_RIGHT |
3471b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_FRONT_CENTER |
3481b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_LOW_FREQUENCY |
3491b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_BACK_LEFT |
3501b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_BACK_RIGHT |
3511b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            AudioFormat.CHANNEL_OUT_BACK_CENTER;
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Convenience method for the constructor's parameter checks.
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // This is where constructor IllegalArgumentException-s are thrown
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // postconditions:
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mStreamType is valid
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mChannelCount is valid
3583026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent    //    mChannels is valid
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mAudioFormat is valid
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mSampleRate is valid
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mDataLoadMode is valid
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void audioParamCheck(int streamType, int sampleRateInHz,
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 int channelConfig, int audioFormat, int mode) {
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //--------------
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // stream type
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if( (streamType != AudioManager.STREAM_ALARM) && (streamType != AudioManager.STREAM_MUSIC)
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           && (streamType != AudioManager.STREAM_RING) && (streamType != AudioManager.STREAM_SYSTEM)
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           && (streamType != AudioManager.STREAM_VOICE_CALL)
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project           && (streamType != AudioManager.STREAM_NOTIFICATION)
371a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent           && (streamType != AudioManager.STREAM_BLUETOOTH_SCO)
372a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent           && (streamType != AudioManager.STREAM_DTMF)) {
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw (new IllegalArgumentException("Invalid stream type."));
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mStreamType = streamType;
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //--------------
3795c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten        // sample rate, note these values are subject to change
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) {
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw (new IllegalArgumentException(sampleRateInHz
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    + "Hz is not a supported sample rate."));
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mSampleRate = sampleRateInHz;
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //--------------
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // channel config
3893026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        mChannelConfiguration = channelConfig;
3903026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (channelConfig) {
3923026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        case AudioFormat.CHANNEL_OUT_DEFAULT: //AudioFormat.CHANNEL_CONFIGURATION_DEFAULT
393a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        case AudioFormat.CHANNEL_OUT_MONO:
3943026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mChannelCount = 1;
3963026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent            mChannels = AudioFormat.CHANNEL_OUT_MONO;
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
398a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        case AudioFormat.CHANNEL_OUT_STEREO:
3993026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mChannelCount = 2;
4013026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent            mChannels = AudioFormat.CHANNEL_OUT_STEREO;
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
4041b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            if ((channelConfig & SUPPORTED_OUT_CHANNELS) != channelConfig) {
4051b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                // input channel configuration features unsupported channels
4061b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                mChannelCount = 0;
4071b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                mChannels = AudioFormat.CHANNEL_INVALID;
4081b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                mChannelConfiguration = AudioFormat.CHANNEL_INVALID;
4091b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                throw(new IllegalArgumentException("Unsupported channel configuration."));
4101b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            } else {
4111b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                mChannels = channelConfig;
4121b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                mChannelCount = Integer.bitCount(channelConfig);
4131b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            }
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //--------------
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // audio format
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (audioFormat) {
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case AudioFormat.ENCODING_DEFAULT:
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case AudioFormat.ENCODING_PCM_16BIT:
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case AudioFormat.ENCODING_PCM_8BIT:
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mAudioFormat = audioFormat;
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mAudioFormat = AudioFormat.ENCODING_INVALID;
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw(new IllegalArgumentException("Unsupported sample encoding."
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                + " Should be ENCODING_PCM_8BIT or ENCODING_PCM_16BIT."));
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //--------------
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // audio load mode
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ( (mode != MODE_STREAM) && (mode != MODE_STATIC) ) {
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw(new IllegalArgumentException("Invalid mode."));
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mDataLoadMode = mode;
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Convenience method for the contructor's audio buffer size check.
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // preconditions:
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mChannelCount is valid
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mAudioFormat is valid
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // postcondition:
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //    mNativeBufferSizeInBytes is valid (multiple of frame size, positive)
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void audioBuffSizeCheck(int audioBufferSize) {
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // NB: this section is only valid with PCM data.
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //     To update when supporting compressed formats
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int frameSizeInBytes = mChannelCount
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                * (mAudioFormat == AudioFormat.ENCODING_PCM_8BIT ? 1 : 2);
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((audioBufferSize % frameSizeInBytes != 0) || (audioBufferSize < 1)) {
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw (new IllegalArgumentException("Invalid audio buffer size."));
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNativeBufferSizeInBytes = audioBufferSize;
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Releases the native AudioTrack resources.
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void release() {
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // even though native_release() stops the native AudioTrack, we need to stop
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // AudioTrack subclasses too.
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            stop();
469fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten        } catch(IllegalStateException ise) {
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // don't raise an exception, we're releasing the resources.
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        native_release();
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mState = STATE_UNINITIALIZED;
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Override
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void finalize() {
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        native_finalize();
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------------------------------------------------------------
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Getters
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the minimum valid volume value. Volume values set under this one will
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be clamped at this value.
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the minimum volume expressed as a linear attenuation.
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static public float getMinVolume() {
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AudioTrack.VOLUME_MIN;
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the maximum valid volume value. Volume values set above this one will
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be clamped at this value.
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the maximum volume expressed as a linear attenuation.
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static public float getMaxVolume() {
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return AudioTrack.VOLUME_MAX;
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the configured audio data sample rate in Hz
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getSampleRate() {
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mSampleRate;
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
508fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
51088e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent     * Returns the current playback rate in Hz.
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPlaybackRate() {
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_playback_rate();
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the configured audio data format. See {@link AudioFormat#ENCODING_PCM_16BIT}
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and {@link AudioFormat#ENCODING_PCM_8BIT}.
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getAudioFormat() {
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mAudioFormat;
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the type of audio stream this AudioTrack is configured for.
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Compare the result against {@link AudioManager#STREAM_VOICE_CALL},
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link AudioManager#STREAM_SYSTEM}, {@link AudioManager#STREAM_RING},
5285c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * {@link AudioManager#STREAM_MUSIC}, {@link AudioManager#STREAM_ALARM},
529b282e08ab641d4fc74d0324b7a0ce30926638dd5Glenn Kasten     * {@link AudioManager#STREAM_NOTIFICATION}, or {@link AudioManager#STREAM_DTMF}.
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getStreamType() {
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mStreamType;
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the configured channel configuration.
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
538a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * See {@link AudioFormat#CHANNEL_OUT_MONO}
539a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * and {@link AudioFormat#CHANNEL_OUT_STEREO}.
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getChannelConfiguration() {
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mChannelConfiguration;
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the configured number of channels.
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getChannelCount() {
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mChannelCount;
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the state of the AudioTrack instance. This is useful after the
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * AudioTrack instance has been created to check if it was initialized
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * properly. This ensures that the appropriate hardware resources have been
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * acquired.
557ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @see #STATE_INITIALIZED
558ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @see #STATE_NO_STATIC_DATA
559ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @see #STATE_UNINITIALIZED
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getState() {
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return mState;
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the playback state of the AudioTrack instance.
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #PLAYSTATE_STOPPED
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #PLAYSTATE_PAUSED
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #PLAYSTATE_PLAYING
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPlayState() {
57206e0333c7eb50533cc6e0f7b61812d2840844296Narayan Kamath        synchronized (mPlayStateLock) {
57306e0333c7eb50533cc6e0f7b61812d2840844296Narayan Kamath            return mPlayState;
57406e0333c7eb50533cc6e0f7b61812d2840844296Narayan Kamath        }
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
578ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *  Returns the native frame count used by the hardware.
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected int getNativeFrameCount() {
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_native_frame_count();
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
585ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Returns marker position expressed in frames.
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getNotificationMarkerPosition() {
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_marker_pos();
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
592ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Returns the notification update period expressed in frames.
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPositionNotificationPeriod() {
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_pos_update_period();
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
599ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Returns the playback head position expressed in frames
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getPlaybackHeadPosition() {
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_position();
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  Returns the hardware output sample rate
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static public int getNativeOutputSampleRate(int streamType) {
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_get_output_sample_rate(streamType);
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
611fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the minimum buffer size required for the successful creation of an AudioTrack
614ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * object to be created in the {@link #MODE_STREAM} mode. Note that this size doesn't
615ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * guarantee a smooth playback under load, and higher values should be chosen according to
616fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     * the expected frequency at which the buffer will be refilled with additional data to play.
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param sampleRateInHz the sample rate expressed in Hertz.
618fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     * @param channelConfig describes the configuration of the audio channels.
619a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and
620a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *   {@link AudioFormat#CHANNEL_OUT_STEREO}
621fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     * @param audioFormat the format in which the audio data is represented.
622fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *   {@link AudioFormat#ENCODING_PCM_8BIT}
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return {@link #ERROR_BAD_VALUE} if an invalid parameter was passed,
625fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     *   or {@link #ERROR} if the implementation was unable to query the hardware for its output
626fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten     *     properties,
627ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *   or the minimum buffer size expressed in bytes.
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int channelCount = 0;
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch(channelConfig) {
632a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        case AudioFormat.CHANNEL_OUT_MONO:
6333026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        case AudioFormat.CHANNEL_CONFIGURATION_MONO:
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            channelCount = 1;
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
636a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        case AudioFormat.CHANNEL_OUT_STEREO:
6373026a023b8979b7ddcb3fe97bbc45531c89fda92Eric Laurent        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            channelCount = 2;
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
6411b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            if ((channelConfig & SUPPORTED_OUT_CHANNELS) != channelConfig) {
6421b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                // input channel configuration features unsupported channels
6431b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                loge("getMinBufferSize(): Invalid channel configuration.");
6441b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                return AudioTrack.ERROR_BAD_VALUE;
6451b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            } else {
6461b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi                channelCount = Integer.bitCount(channelConfig);
6471b8f499a14a4340d3422d95c7f6fdc8c0c72b3a4Jean-Michel Trivi            }
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
649fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
650fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten        if ((audioFormat != AudioFormat.ENCODING_PCM_16BIT)
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            && (audioFormat != AudioFormat.ENCODING_PCM_8BIT)) {
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            loge("getMinBufferSize(): Invalid audio format.");
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return AudioTrack.ERROR_BAD_VALUE;
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
655fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
6565c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten        // sample rate, note these values are subject to change
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) {
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            loge("getMinBufferSize(): " + sampleRateInHz +"Hz is not a supported sample rate.");
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return AudioTrack.ERROR_BAD_VALUE;
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
661fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((size == -1) || (size == 0)) {
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            loge("getMinBufferSize(): error querying hardware");
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return AudioTrack.ERROR;
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        else {
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return size;
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
672619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    /**
673619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * Returns the audio session ID.
674619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     *
675619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     * @return the ID of the audio session this AudioTrack belongs to.
676619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent     */
677619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    public int getAudioSessionId() {
678619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent        return mSessionId;
679619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    }
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------------------------------------------------------------
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Initialization / configuration
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6854df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * Sets the listener the AudioTrack notifies when a previously set marker is reached or
6864df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * for each periodic playback head position update.
687ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Notifications will be received in the same thread as the one in which the AudioTrack
688ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * instance was created.
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param listener
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6914df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    public void setPlaybackPositionUpdateListener(OnPlaybackPositionUpdateListener listener) {
6924df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        setPlaybackPositionUpdateListener(listener, null);
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
694fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
695ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi    /**
696ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Sets the listener the AudioTrack notifies when a previously set marker is reached or
697ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * for each periodic playback head position update.
698ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Use this method to receive AudioTrack events in the Handler associated with another
699ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * thread than the one in which you created the AudioTrack instance.
700ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param listener
701ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param handler the Handler that will receive the event notification messages.
702ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     */
703fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten    public void setPlaybackPositionUpdateListener(OnPlaybackPositionUpdateListener listener,
7044df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                                                    Handler handler) {
7054df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        synchronized (mPositionListenerLock) {
7064df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            mPositionListener = listener;
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7084df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        if (listener != null) {
7094df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            mEventHandlerDelegate = new NativeEventHandlerDelegate(this, handler);
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
711fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7154df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     /**
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the specified left/right output volume values on the AudioTrack. Values are clamped
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to the ({@link #getMinVolume()}, {@link #getMaxVolume()}) interval if outside this range.
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param leftVolume output attenuation for the left channel. A value of 0.0f is silence,
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *      a value of 1.0f is no attenuation.
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param rightVolume output attenuation for the right channel
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS},
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    {@link #ERROR_INVALID_OPERATION}
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setStereoVolume(float leftVolume, float rightVolume) {
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // clamp the volumes
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (leftVolume < getMinVolume()) {
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            leftVolume = getMinVolume();
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (leftVolume > getMaxVolume()) {
7359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            leftVolume = getMaxVolume();
7369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (rightVolume < getMinVolume()) {
7389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            rightVolume = getMinVolume();
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (rightVolume > getMaxVolume()) {
7419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            rightVolume = getMaxVolume();
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        native_setVolume(leftVolume, rightVolume);
7459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return SUCCESS;
7479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the playback sample rate for this track. This sets the sampling rate at which
7529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the audio data will be consumed and played back, not the original sampling rate of the
7539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * content. Setting it to half the sample rate of the content will cause the playback to
75488e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent     * last twice as long, but will also result in a negative pitch shift.
7555c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * The valid sample rate range is from 1Hz to twice the value returned by
75688e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent     * {@link #getNativeOutputSampleRate(int)}.
757ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param sampleRateInHz the sample rate expressed in Hz
7589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
7599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    {@link #ERROR_INVALID_OPERATION}
7609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setPlaybackRate(int sampleRateInHz) {
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
7639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (sampleRateInHz <= 0) {
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_BAD_VALUE;
7679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
76888e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent        return native_set_playback_rate(sampleRateInHz);
7699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
773ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Sets the position of the notification marker.
7749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param markerInFrames marker in frames
7759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
7769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  {@link #ERROR_INVALID_OPERATION}
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setNotificationMarkerPosition(int markerInFrames) {
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_set_marker_pos(markerInFrames);
7839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
787ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * Sets the period for the periodic notification event.
788ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param periodInFrames update period expressed in frames
7899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_INVALID_OPERATION}
7909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setPositionNotificationPeriod(int periodInFrames) {
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_set_pos_update_period(periodInFrames);
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the playback head position. The track must be stopped for the position to be changed.
801ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param positionInFrames playback head position expressed in frames
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    {@link #ERROR_INVALID_OPERATION}
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setPlaybackHeadPosition(int positionInFrames) {
8069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized(mPlayStateLock) {
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((mPlayState == PLAYSTATE_STOPPED) || (mPlayState == PLAYSTATE_PAUSED)) {
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return native_set_position(positionInFrames);
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return ERROR_INVALID_OPERATION;
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the loop points and the loop count. The loop can be infinite.
817ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param startInFrames loop start marker expressed in frames
818ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param endInFrames loop end marker expressed in frames
8199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param loopCount the number of times the loop is looped.
8209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    A value of -1 means infinite looping.
8219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
8229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    {@link #ERROR_INVALID_OPERATION}
8239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int setLoopPoints(int startInFrames, int endInFrames, int loopCount) {
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mDataLoadMode == MODE_STREAM) {
8269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
8279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_set_loop(startInFrames, endInFrames, loopCount);
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the initialization state of the instance. To be used in an AudioTrack subclass
8339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * constructor to set a subclass-specific post-initialization state.
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param state the state of the AudioTrack instance
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    protected void setState(int state) {
8379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mState = state;
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Transport control methods
8439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
8449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Starts playing an AudioTrack.
84688bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     *
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void play()
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    throws IllegalStateException {
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw(new IllegalStateException("play() called on uninitialized AudioTrack."));
8539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized(mPlayStateLock) {
8569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            native_start();
8579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPlayState = PLAYSTATE_PLAYING;
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Stops playing the audio data.
863a701da8ebabb3d63ac57b7718055698abb21804aJean-Michel Trivi     * When used on an instance created in {@link #MODE_STREAM} mode, audio will stop playing
864a701da8ebabb3d63ac57b7718055698abb21804aJean-Michel Trivi     * after the last buffer that was written has been played. For an immediate stop, use
865a701da8ebabb3d63ac57b7718055698abb21804aJean-Michel Trivi     * {@link #pause()}, followed by {@link #flush()} to discard audio data that hasn't been played
866a701da8ebabb3d63ac57b7718055698abb21804aJean-Michel Trivi     * back yet.
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void stop()
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    throws IllegalStateException {
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw(new IllegalStateException("stop() called on uninitialized AudioTrack."));
8739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // stop playing
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized(mPlayStateLock) {
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            native_stop();
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPlayState = PLAYSTATE_STOPPED;
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
88388bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Pauses the playback of the audio data. Data that has not been played
88488bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * back will not be discarded. Subsequent calls to {@link #play} will play
885a701da8ebabb3d63ac57b7718055698abb21804aJean-Michel Trivi     * this data back. See {@link #flush()} to discard this data.
88688bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     *
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @throws IllegalStateException
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void pause()
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    throws IllegalStateException {
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throw(new IllegalStateException("pause() called on uninitialized AudioTrack."));
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //logd("pause()");
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // pause playback
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        synchronized(mPlayStateLock) {
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            native_pause();
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPlayState = PLAYSTATE_PAUSED;
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Audio data supply
9069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
9079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
90988bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Flushes the audio data currently queued for playback. Any data that has
91088bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * not been played back will be discarded.
9119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void flush() {
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState == STATE_INITIALIZED) {
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // flush the data in native layer
9159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            native_flush();
9169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
92188bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Writes the audio data to the audio hardware for playback. Will block until
92288bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * all data has been written to the audio mixer.
92388bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Note that the actual playback of this data might occur after this function
92488bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * returns. This function is thread safe with respect to {@link #stop} calls,
92588bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * in which case all of the specified data might not be written to the mixer.
92688bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     *
9279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param audioData the array that holds the data to play.
92888bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * @param offsetInBytes the offset expressed in bytes in audioData where the data to play
929ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *    starts.
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param sizeInBytes the number of bytes to read in audioData after the offset.
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the number of bytes that were written or {@link #ERROR_INVALID_OPERATION}
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *    the parameters don't resolve to valid data and indexes.
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9365c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten    public int write(byte[] audioData, int offsetInBytes, int sizeInBytes) {
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mDataLoadMode == MODE_STATIC)
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && (mState == STATE_NO_STATIC_DATA)
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && (sizeInBytes > 0)) {
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mState = STATE_INITIALIZED;
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
947fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten        if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || (offsetInBytes + sizeInBytes > audioData.length)) {
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_BAD_VALUE;
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_write_byte(audioData, offsetInBytes, sizeInBytes, mAudioFormat);
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
95788bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Writes the audio data to the audio hardware for playback. Will block until
95888bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * all data has been written to the audio mixer.
95988bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * Note that the actual playback of this data might occur after this function
96088bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * returns. This function is thread safe with respect to {@link #stop} calls,
96188bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * in which case all of the specified data might not be written to the mixer.
96288bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     *
9639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param audioData the array that holds the data to play.
964ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     * @param offsetInShorts the offset expressed in shorts in audioData where the data to play
965ff14c25339da3f6cd86edd66ef42640e6d0c0787Jean-Michel Trivi     *     starts.
9669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param sizeInShorts the number of bytes to read in audioData after the offset.
9679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the number of shorts that were written or {@link #ERROR_INVALID_OPERATION}
9689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      *    if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
9699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      *    the parameters don't resolve to valid data and indexes.
9709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int write(short[] audioData, int offsetInShorts, int sizeInShorts) {
9739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((mDataLoadMode == MODE_STATIC)
9749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && (mState == STATE_NO_STATIC_DATA)
9759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && (sizeInShorts > 0)) {
9769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mState = STATE_INITIALIZED;
9779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
978fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
9799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mState != STATE_INITIALIZED) {
9809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
9819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
983fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten        if ( (audioData == null) || (offsetInShorts < 0 ) || (sizeInShorts < 0)
9849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                || (offsetInShorts + sizeInShorts > audioData.length)) {
9859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_BAD_VALUE;
9869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_write_short(audioData, offsetInShorts, sizeInShorts, mAudioFormat);
9899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
9939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Notifies the native resource to reuse the audio data already loaded in the native
9949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * layer. This call is only valid with AudioTrack instances that don't use the streaming
9959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * model.
9969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return error code or success, see {@link #SUCCESS}, {@link #ERROR_BAD_VALUE},
9979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  {@link #ERROR_INVALID_OPERATION}
9989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int reloadStaticData() {
10009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mDataLoadMode == MODE_STREAM) {
10019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return ERROR_INVALID_OPERATION;
10029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return native_reload_static();
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10067070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    //--------------------------------------------------------------------------
10077070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    // Audio effects management
10087070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    //--------------------
10097070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
10107070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    /**
10111a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * Attaches an auxiliary effect to the audio track. A typical auxiliary
10121a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * effect is a reverberation effect which can be applied on any sound source
10131a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * that directs a certain amount of its energy to this effect. This amount
10141a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * is defined by setAuxEffectSendLevel().
10157070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * {@see #setAuxEffectSendLevel(float)}.
10161a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * <p>After creating an auxiliary effect (e.g.
10171a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with
10181a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling
10197070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * this method to attach the audio track to the effect.
10201a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * <p>To detach the effect from the audio track, call this method with a
10211a5149e5d7f2dddc8b324f7695e69fd89af73c52Eric Laurent     * null effect id.
10227070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     *
10237070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * @param effectId system wide unique id of the effect to attach
10247070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * @return error code or success, see {@link #SUCCESS},
10257070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     *    {@link #ERROR_INVALID_OPERATION}, {@link #ERROR_BAD_VALUE}
10267070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     */
10277070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    public int attachAuxEffect(int effectId) {
10287070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        if (mState != STATE_INITIALIZED) {
10297070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            return ERROR_INVALID_OPERATION;
10307070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        }
10317070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return native_attachAuxEffect(effectId);
10327070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
10337070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
10347070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    /**
10357070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * Sets the send level of the audio track to the attached auxiliary effect
103688bde0ce3799c47530ea42ae3665bfa12ae38d11Narayan Kamath     * {@link #attachAuxEffect(int)}. The level value range is 0 to 1.0.
10377070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * <p>By default the send level is 0, so even if an effect is attached to the player
10387070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * this method must be called for the effect to be applied.
10397070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * <p>Note that the passed level value is a raw scalar. UI controls should be scaled
10407070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * logarithmically: the gain applied by audio framework ranges from -72dB to 0dB,
10417070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * so an appropriate conversion from linear UI input x to level is:
10425c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * x == 0 -&gt; level = 0
10435c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     * 0 &lt; x &lt;= R -&gt; level = 10^(72*(x-R)/20/R)
10447070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     *
10457070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * @param level send level scalar
10467070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     * @return error code or success, see {@link #SUCCESS},
10477070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     *    {@link #ERROR_INVALID_OPERATION}
10487070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent     */
10497070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    public int setAuxEffectSendLevel(float level) {
10507070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        if (mState != STATE_INITIALIZED) {
10517070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            return ERROR_INVALID_OPERATION;
10527070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        }
10537070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        // clamp the level
10547070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        if (level < getMinVolume()) {
10557070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            level = getMinVolume();
10567070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        }
10577070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        if (level > getMaxVolume()) {
10587070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent            level = getMaxVolume();
10597070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        }
10607070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        native_setAuxEffectSendLevel(level);
10617070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent        return SUCCESS;
10627070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    }
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Interface definitions
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10684df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * Interface definition for a callback to be invoked when the playback head position of
10694df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * an AudioTrack has reached a notification marker or has increased by a certain period.
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10714df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    public interface OnPlaybackPositionUpdateListener  {
10729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Called on the listener to notify it that the previously set marker has been reached
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * by the playback head.
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        void onMarkerReached(AudioTrack track);
1077fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Called on the listener to periodically notify it that the playback head has reached
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * a multiple of the notification period.
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
10829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        void onPeriodicNotification(AudioTrack track);
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Inner classes
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10904df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * Helper class to handle the forwarding of native events to the appropriate listener
10914df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project     * (potentially) handled in a different thread
10925c17a820f9e46e0756c11795b3e6f89105f2f539Glenn Kasten     */
10934df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project    private class NativeEventHandlerDelegate {
10944df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        private final AudioTrack mAudioTrack;
10954df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        private final Handler mHandler;
1096fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
10974df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        NativeEventHandlerDelegate(AudioTrack track, Handler handler) {
10984df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            mAudioTrack = track;
10994df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            // find the looper for our new event handler
11004df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            Looper looper;
11014df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            if (handler != null) {
11024df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                looper = handler.getLooper();
11034df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            } else {
1104105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project                // no given handler, use the looper the AudioTrack was created in
1105105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project                looper = mInitializationLooper;
11069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1107fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
11084df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            // construct the event handler with this looper
11094df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            if (looper != null) {
11104df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                // implement the event handler delegate
11114df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                mHandler = new Handler(looper) {
11124df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                    @Override
11134df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                    public void handleMessage(Message msg) {
11144df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        if (mAudioTrack == null) {
11154df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            return;
11164df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        }
11174df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        OnPlaybackPositionUpdateListener listener = null;
11184df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        synchronized (mPositionListenerLock) {
11194df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            listener = mAudioTrack.mPositionListener;
11204df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        }
11214df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        switch(msg.what) {
11224df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        case NATIVE_EVENT_MARKER:
11234df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            if (listener != null) {
11244df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                                listener.onMarkerReached(mAudioTrack);
11254df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            }
11264df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            break;
11274df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        case NATIVE_EVENT_NEW_POS:
11284df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            if (listener != null) {
11294df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                                listener.onPeriodicNotification(mAudioTrack);
11304df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            }
11314df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            break;
11324df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        default:
11334df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            Log.e(TAG, "[ android.media.AudioTrack.NativeEventHandler ] " +
11344df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                                    "Unknown event type: " + msg.what);
11354df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                            break;
11364df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                        }
11374df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                    }
11384df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                };
11394df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            } else {
11404df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                mHandler = null;
1141fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten            }
11424df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        }
1143fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten
11444df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        Handler getHandler() {
11454df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            return mHandler;
11469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
11519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Java methods called from the native side
11529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
11539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SuppressWarnings("unused")
11549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void postEventFromNative(Object audiotrack_ref,
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int what, int arg1, int arg2, Object obj) {
11569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2);
11579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AudioTrack track = (AudioTrack)((WeakReference)audiotrack_ref).get();
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (track == null) {
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11624df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project        if (track.mEventHandlerDelegate != null) {
1163fb2ab9efc3805c81521afb9ff91a58ff5097a36eGlenn Kasten            Message m =
11644df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project                track.mEventHandlerDelegate.getHandler().obtainMessage(what, arg1, arg2, obj);
11654df2423a947bcd3f024cc3d3a1a315a8dc428598The Android Open Source Project            track.mEventHandlerDelegate.getHandler().sendMessage(m);
11669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
11729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Native methods called from the Java side
11739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //--------------------
11749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_setup(Object audiotrack_this,
11769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int streamType, int sampleRate, int nbChannels, int audioFormat,
1177619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent            int buffSizeInBytes, int mode, int[] sessionId);
11789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_finalize();
11809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_release();
11829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_start();
11849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_stop();
11869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_pause();
11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_flush();
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_write_byte(byte[] audioData,
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                               int offsetInBytes, int sizeInBytes, int format);
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_write_short(short[] audioData,
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                int offsetInShorts, int sizeInShorts, int format);
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_reload_static();
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_get_native_frame_count();
12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final void native_setVolume(float leftVolume, float rightVolume);
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
120388e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent    private native final int native_set_playback_rate(int sampleRateInHz);
120488e209dcf8c2ebddda5c272f46d1bd5478bc639cEric Laurent    private native final int native_get_playback_rate();
12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_set_marker_pos(int marker);
12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_get_marker_pos();
12089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_set_pos_update_period(int updatePeriod);
12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_get_pos_update_period();
12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_set_position(int position);
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_get_position();
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private native final int native_set_loop(int start, int end, int loopCount);
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static private native final int native_get_output_sample_rate(int streamType);
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static private native final int native_get_min_buff_size(
12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int sampleRateInHz, int channelConfig, int audioFormat);
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1221619346f902241736d933657a4fe10f10c50a1ba8Eric Laurent    private native final int native_get_session_id();
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12237070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    private native final int native_attachAuxEffect(int effectId);
12247070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent    private native final void native_setAuxEffectSendLevel(float level);
12257070b36549d511d6627538c73dfbab23fbae5b43Eric Laurent
12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //---------------------------------------------------------
12279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Utility methods
12289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    //------------------
12299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void logd(String msg) {
12319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Log.d(TAG, "[ android.media.AudioTrack ] " + msg);
12329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void loge(String msg) {
12359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Log.e(TAG, "[ android.media.AudioTrack ] " + msg);
12369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1239