public class Visualizer extends Object
The audio session ID passed to the constructor indicates which audio content should be
visualized:
MediaPlayer
or
AudioTrack
using this audio session is visualized Two types of representation of audio content can be captured:
getWaveForm(byte[])
methodgetFft(byte[])
methodThe length of the capture can be retrieved or specified by calling respectively
getCaptureSize()
and setCaptureSize(int)
methods. The capture size must be a
power of 2 in the range returned by getCaptureSizeRange()
.
In addition to the polling capture mode described above with getWaveForm(byte[])
and
getFft(byte[])
methods, a callback mode is also available by installing a listener by
use of the setDataCaptureListener(OnDataCaptureListener, int, boolean, boolean)
method.
The rate at which the listener capture method is called as well as the type of data returned is
specified.
Before capturing data, the Visualizer must be enabled by calling the
setEnabled(boolean)
method.
When data capture is not needed any more, the Visualizer should be disabled.
It is good practice to call the release()
method when the Visualizer is not used
anymore to free up native resources associated to the Visualizer instance.
Creating a Visualizer on the output mix (audio session 0) requires permission
android.Manifest.permission#MODIFY_AUDIO_SETTINGS
The Visualizer class can also be used to perform measurements on the audio being played back.
The measurements to perform are defined by setting a mask of the requested measurement modes with
setMeasurementMode(int)
. Supported values are MEASUREMENT_MODE_NONE
to cancel
any measurement, and MEASUREMENT_MODE_PEAK_RMS
for peak and RMS monitoring.
Measurements can be retrieved through getMeasurementPeakRms(MeasurementPeakRms)
.
Modifier and Type | Class and Description |
---|---|
static class |
Visualizer.MeasurementPeakRms
A class to store peak and RMS values.
|
static interface |
Visualizer.OnDataCaptureListener
The OnDataCaptureListener interface defines methods called by the Visualizer to periodically
update the audio visualization capture.
|
static interface |
Visualizer.OnServerDiedListener |
Modifier and Type | Field and Description |
---|---|
static int |
ALREADY_EXISTS
Internal operation status.
|
static int |
ERROR
Unspecified error.
|
static int |
ERROR_BAD_VALUE
Operation failed due to bad parameter value.
|
static int |
ERROR_DEAD_OBJECT
Operation failed due to dead remote object.
|
static int |
ERROR_INVALID_OPERATION
Operation failed because it was requested in wrong state.
|
static int |
ERROR_NO_INIT
Operation failed due to bad object initialization.
|
static int |
ERROR_NO_MEMORY
Operation failed due to lack of memory.
|
static int |
MEASUREMENT_MODE_NONE
Defines a measurement mode in which no measurements are performed.
|
static int |
MEASUREMENT_MODE_PEAK_RMS
Defines a measurement mode which computes the peak and RMS value in mB, where 0mB is the
maximum sample value, and -9600mB is the minimum value.
|
static int |
SCALING_MODE_AS_PLAYED
Defines a capture mode where the playback volume will affect (scale) the range of the
captured data.
|
static int |
SCALING_MODE_NORMALIZED
Defines a capture mode where amplification is applied based on the content of the captured
data.
|
static int |
STATE_ENABLED
State of a Visualizer object that is active.
|
static int |
STATE_INITIALIZED
State of a Visualizer object that is ready to be used.
|
static int |
STATE_UNINITIALIZED
State of a Visualizer object that was not successfully initialized upon creation
|
static int |
SUCCESS
Successful operation.
|
Constructor and Description |
---|
Visualizer(int audioSession)
Class constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
int |
getCaptureSize()
Returns current capture size.
|
static int[] |
getCaptureSizeRange()
Returns the capture size range.
|
boolean |
getEnabled()
Get current activation state of the visualizer.
|
int |
getFft(byte[] fft)
Returns a frequency capture of currently playing audio content.
|
static int |
getMaxCaptureRate()
Returns the maximum capture rate for the callback capture method.
|
int |
getMeasurementMode()
Returns the current measurement modes performed by this audio effect
|
int |
getMeasurementPeakRms(Visualizer.MeasurementPeakRms measurement)
Retrieves the latest peak and RMS measurement.
|
int |
getSamplingRate()
Returns the sampling rate of the captured audio.
|
int |
getScalingMode()
Returns the current scaling mode on the captured visualization data.
|
int |
getWaveForm(byte[] waveform)
Returns a waveform capture of currently playing audio content.
|
void |
release()
Releases the native Visualizer resources.
|
int |
setCaptureSize(int size)
Sets the capture size, i.e. the number of bytes returned by
getWaveForm(byte[]) and
getFft(byte[]) methods. |
int |
setDataCaptureListener(Visualizer.OnDataCaptureListener listener,
int rate,
boolean waveform,
boolean fft)
Registers an OnDataCaptureListener interface and specifies the rate at which the capture
should be updated as well as the type of capture requested.
|
int |
setEnabled(boolean enabled)
Enable or disable the visualization engine.
|
int |
setMeasurementMode(int mode)
Sets the combination of measurement modes to be performed by this audio effect.
|
int |
setScalingMode(int mode)
Set the type of scaling applied on the captured visualization data.
|
int |
setServerDiedListener(Visualizer.OnServerDiedListener listener) |
public static final int STATE_UNINITIALIZED
public static final int STATE_INITIALIZED
public static final int STATE_ENABLED
public static final int SCALING_MODE_NORMALIZED
public static final int SCALING_MODE_AS_PLAYED
public static final int MEASUREMENT_MODE_NONE
public static final int MEASUREMENT_MODE_PEAK_RMS
getMeasurementPeakRms(MeasurementPeakRms)
.public static final int SUCCESS
public static final int ERROR
public static final int ALREADY_EXISTS
public static final int ERROR_NO_INIT
public static final int ERROR_BAD_VALUE
public static final int ERROR_INVALID_OPERATION
public static final int ERROR_NO_MEMORY
public static final int ERROR_DEAD_OBJECT
public Visualizer(int audioSession) throws UnsupportedOperationException, RuntimeException
audioSession
- system wide unique audio session identifier. If audioSession
is not 0, the visualizer will be attached to the MediaPlayer or AudioTrack in the
same audio session. Otherwise, the Visualizer will apply to the output mix.UnsupportedOperationException
RuntimeException
public void release()
protected void finalize()
Object
finalize
method to dispose of
system resources or to perform other cleanup.
The general contract of finalize
is that it is invoked
if and when the JavaTM virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize
method may take any action, including
making this object available again to other threads; the usual purpose
of finalize
, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize
method of class Object
performs no
special action; it simply returns normally. Subclasses of
Object
may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize
method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize
method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize
method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize
method causes
the finalization of this object to be halted, but is otherwise
ignored.
public int setEnabled(boolean enabled) throws IllegalStateException
enabled
- requested enable stateSUCCESS
in case of success,
ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException
public boolean getEnabled()
public static int[] getCaptureSizeRange()
public static int getMaxCaptureRate()
setDataCaptureListener(OnDataCaptureListener, int, boolean, boolean)
method.public int setCaptureSize(int size) throws IllegalStateException
getWaveForm(byte[])
and
getFft(byte[])
methods. The capture size must be a power of 2 in the range returned
by getCaptureSizeRange()
.
This method must not be called when the Visualizer is enabled.size
- requested capture sizeSUCCESS
in case of success,
ERROR_BAD_VALUE
in case of failure.IllegalStateException
public int getCaptureSize() throws IllegalStateException
IllegalStateException
public int setScalingMode(int mode) throws IllegalStateException
mode
- see SCALING_MODE_NORMALIZED
and SCALING_MODE_AS_PLAYED
SUCCESS
in case of success,
ERROR_BAD_VALUE
in case of failure.IllegalStateException
public int getScalingMode() throws IllegalStateException
SCALING_MODE_NORMALIZED
and SCALING_MODE_AS_PLAYED
.IllegalStateException
public int setMeasurementMode(int mode) throws IllegalStateException
mode
- a mask of the measurements to perform. The valid values are
MEASUREMENT_MODE_NONE
(to cancel any measurement)
or MEASUREMENT_MODE_PEAK_RMS
.SUCCESS
in case of success, ERROR_BAD_VALUE
in case of failure.IllegalStateException
public int getMeasurementMode() throws IllegalStateException
MEASUREMENT_MODE_NONE
(when no measurements are performed)
or MEASUREMENT_MODE_PEAK_RMS
.IllegalStateException
public int getSamplingRate() throws IllegalStateException
IllegalStateException
public int getWaveForm(byte[] waveform) throws IllegalStateException
getCaptureSize()
.
This method must be called when the Visualizer is enabled.
waveform
- array of bytes where the waveform should be returnedSUCCESS
in case of success,
ERROR_NO_MEMORY
, ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException
public int getFft(byte[] fft) throws IllegalStateException
This method must be called when the Visualizer is enabled.
The capture is an 8-bit magnitude FFT, the frequency range covered being 0 (DC) to half of
the sampling rate returned by getSamplingRate()
. The capture returns the real and
imaginary parts of a number of frequency points equal to half of the capture size plus one.
Note: only the real part is returned for the first point (DC) and the last point (sampling frequency / 2).
The layout in the returned byte array is as follows:
Index | 0 | 1 | 2 | 3 | 4 | 5 | ... | n - 2 | n - 1 |
Data | Rf0 | Rf(n/2) | Rf1 | If1 | Rf2 | If2 | ... | Rf(n-1)/2 | If(n-1)/2 |
fft
- array of bytes where the FFT should be returnedSUCCESS
in case of success,
ERROR_NO_MEMORY
, ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.IllegalStateException
public int getMeasurementPeakRms(Visualizer.MeasurementPeakRms measurement)
Visualizer.MeasurementPeakRms
to the
latest measured values.measurement
- a non-null Visualizer.MeasurementPeakRms
instance to store
the measurement values.SUCCESS
in case of success, ERROR_BAD_VALUE
,
ERROR_NO_MEMORY
, ERROR_INVALID_OPERATION
or ERROR_DEAD_OBJECT
in case of failure.public int setDataCaptureListener(Visualizer.OnDataCaptureListener listener, int rate, boolean waveform, boolean fft)
Call this method with a null listener to stop receiving the capture updates.
listener
- OnDataCaptureListener registeredrate
- rate in milliHertz at which the capture should be updatedwaveform
- true if a waveform capture is requested: the onWaveFormDataCapture()
method will be called on the OnDataCaptureListener interface.fft
- true if a frequency capture is requested: the onFftDataCapture() method will be
called on the OnDataCaptureListener interface.SUCCESS
in case of success,
ERROR_NO_INIT
or ERROR_BAD_VALUE
in case of failure.public int setServerDiedListener(Visualizer.OnServerDiedListener listener)
SUCCESS
in case of success,