Visualizer
open class Visualizer
kotlin.Any | |
↳ | android.media.audiofx.Visualizer |
The Visualizer class enables application to retrieve part of the currently playing audio for visualization purpose. It is not an audio recording interface and only returns partial and low quality audio content. However, to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO.
The audio session ID passed to the constructor indicates which audio content should be visualized:
- If the session is 0, the audio output mix is visualized
- If the session is not 0, the audio from a particular
android.media.MediaPlayer
orandroid.media.AudioTrack
using this audio session is visualized
Two types of representation of audio content can be captured:
- Waveform data: consecutive 8-bit (unsigned) mono samples by using the
getWaveForm(byte[])
method - Frequency data: 8-bit magnitude FFT by using the
getFft(byte[])
method
The 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(android.media.audiofx.Visualizer.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(android.media.audiofx.Visualizer.MeasurementPeakRms)
.
Summary
Nested classes | |
---|---|
A class to store peak and RMS values. |
|
abstract |
The OnDataCaptureListener interface defines methods called by the Visualizer to periodically update the audio visualization capture. |
Constants | |
---|---|
static Int |
Internal operation status. |
static Int |
Unspecified error. |
static Int |
Operation failed due to bad parameter value. |
static Int |
Operation failed due to dead remote object. |
static Int |
Operation failed because it was requested in wrong state. |
static Int |
Operation failed due to bad object initialization. |
static Int |
Operation failed due to lack of memory. |
static Int |
Defines a measurement mode in which no measurements are performed. |
static Int |
Defines a measurement mode which computes the peak and RMS value in mB below the "full scale", where 0mB is normally the maximum sample value (but see the note below). |
static Int |
Defines a capture mode where the playback volume will affect (scale) the range of the captured data. |
static Int |
Defines a capture mode where amplification is applied based on the content of the captured data. |
static Int |
State of a Visualizer object that is active. |
static Int |
State of a Visualizer object that is ready to be used. |
static Int |
State of a Visualizer object that was not successfully initialized upon creation |
static Int |
Successful operation. |
Public constructors | |
---|---|
Visualizer(audioSession: Int) Class constructor. |
Public methods | |
---|---|
open Int |
Returns current capture size. |
open static IntArray! |
Returns the capture size range. |
open Boolean |
Get current activation state of the visualizer. |
open Int |
Returns a frequency capture of currently playing audio content. |
open static Int |
Returns the maximum capture rate for the callback capture method. |
open Int |
Returns the current measurement modes performed by this audio effect |
open Int |
getMeasurementPeakRms(measurement: Visualizer.MeasurementPeakRms!) Retrieves the latest peak and RMS measurement. |
open Int |
Returns the sampling rate of the captured audio. |
open Int |
Returns the current scaling mode on the captured visualization data. |
open Int |
getWaveForm(waveform: ByteArray!) Returns a waveform capture of currently playing audio content. |
open Unit |
release() Releases the native Visualizer resources. |
open Int |
setCaptureSize(size: Int) Sets the capture size, i. |
open Int |
setDataCaptureListener(listener: Visualizer.OnDataCaptureListener?, rate: Int, waveform: Boolean, fft: Boolean) Registers an OnDataCaptureListener interface and specifies the rate at which the capture should be updated as well as the type of capture requested. |
open Int |
setEnabled(enabled: Boolean) Enable or disable the visualization engine. |
open Int |
setMeasurementMode(mode: Int) Sets the combination of measurement modes to be performed by this audio effect. |
open Int |
setScalingMode(mode: Int) Set the type of scaling applied on the captured visualization data. |
Protected methods | |
---|---|
open Unit |
finalize() |
Constants
ALREADY_EXISTS
static val ALREADY_EXISTS: Int
Internal operation status. Not returned by any method.
Value: -2
ERROR_BAD_VALUE
static val ERROR_BAD_VALUE: Int
Operation failed due to bad parameter value.
Value: -4
ERROR_DEAD_OBJECT
static val ERROR_DEAD_OBJECT: Int
Operation failed due to dead remote object.
Value: -7
ERROR_INVALID_OPERATION
static val ERROR_INVALID_OPERATION: Int
Operation failed because it was requested in wrong state.
Value: -5
ERROR_NO_INIT
static val ERROR_NO_INIT: Int
Operation failed due to bad object initialization.
Value: -3
ERROR_NO_MEMORY
static val ERROR_NO_MEMORY: Int
Operation failed due to lack of memory.
Value: -6
MEASUREMENT_MODE_NONE
static val MEASUREMENT_MODE_NONE: Int
Defines a measurement mode in which no measurements are performed.
Value: 0
MEASUREMENT_MODE_PEAK_RMS
static val MEASUREMENT_MODE_PEAK_RMS: Int
Defines a measurement mode which computes the peak and RMS value in mB below the "full scale", where 0mB is normally the maximum sample value (but see the note below). Minimum value depends on the resolution of audio samples used by the audio framework. The value of -9600mB is the minimum value for 16-bit audio systems and -14400mB or below for "high resolution" systems. Values for peak and RMS can be retrieved with getMeasurementPeakRms(android.media.audiofx.Visualizer.MeasurementPeakRms)
.
Note: when Visualizer effect is attached to the global session (with session ID 0), it is possible to observe RMS peaks higher than 0 dBFS, for example in the case when there are multiple audio sources playing simultaneously. In this case getMeasurementPeakRms(android.media.audiofx.Visualizer.MeasurementPeakRms)
method can return a positive value.
Value: 1
SCALING_MODE_AS_PLAYED
static val SCALING_MODE_AS_PLAYED: Int
Defines a capture mode where the playback volume will affect (scale) the range of the captured data. A low playback volume will lead to low sample and fft values, and vice-versa.
Value: 1
SCALING_MODE_NORMALIZED
static val SCALING_MODE_NORMALIZED: Int
Defines a capture mode where amplification is applied based on the content of the captured data. This is the default Visualizer mode, and is suitable for music visualization.
Value: 0
STATE_ENABLED
static val STATE_ENABLED: Int
State of a Visualizer object that is active.
Value: 2
STATE_INITIALIZED
static val STATE_INITIALIZED: Int
State of a Visualizer object that is ready to be used.
Value: 1
STATE_UNINITIALIZED
static val STATE_UNINITIALIZED: Int
State of a Visualizer object that was not successfully initialized upon creation
Value: 0
Public constructors
Visualizer
Visualizer(audioSession: Int)
Class constructor.
Parameters | |
---|---|
audioSession |
Int: 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. |
Exceptions | |
---|---|
java.lang.UnsupportedOperationException |
|
java.lang.RuntimeException |
Public methods
getCaptureSize
open fun getCaptureSize(): Int
Returns current capture size.
Return | |
---|---|
Int |
the capture size in bytes. |
getCaptureSizeRange
open static fun getCaptureSizeRange(): IntArray!
Returns the capture size range.
Return | |
---|---|
IntArray! |
the mininum capture size is returned in first array element and the maximum in second array element. |
getEnabled
open fun getEnabled(): Boolean
Get current activation state of the visualizer.
Return | |
---|---|
Boolean |
true if the visualizer is active, false otherwise |
getFft
open fun getFft(fft: ByteArray!): Int
Returns a frequency capture of currently playing audio content.
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:
- n is the capture size returned by getCaptureSize()
- Rfk, Ifk are respectively the real and imaginary parts of the kth frequency component
- If Fs is the sampling frequency retuned by getSamplingRate() the kth frequency is: k * Fs / n
Index | 0 | 1 | 2 | 3 | 4 | 5 | ... | n - 2 | n - 1 |
Data | Rf0 | Rf(n/2) | Rf1 | If1 | Rf2 | If2 | ... | Rf(n/2-1) | If(n/2-1) |
In order to obtain magnitude and phase values the following code can be used:
int n = fft.size(); float[] magnitudes = new float[n / 2 + 1]; float[] phases = new float[n / 2 + 1]; magnitudes[0] = (float)Math.abs(fft[0]); // DC magnitudes[n / 2] = (float)Math.abs(fft[1]); // Nyquist phases[0] = phases[n / 2] = 0; for (int k = 1; k < n / 2; k++) { int i = k * 2; magnitudes[k] = (float)Math.hypot(fft[i], fft[i + 1]); phases[k] = (float)Math.atan2(fft[i + 1], fft[i]); }
Parameters | |
---|---|
fft |
ByteArray!: array of bytes where the FFT should be returned |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_NO_MEMORY , ERROR_INVALID_OPERATION or ERROR_DEAD_OBJECT in case of failure. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
getMaxCaptureRate
open static fun getMaxCaptureRate(): Int
Returns the maximum capture rate for the callback capture method. This is the maximum value for the rate parameter of the setDataCaptureListener(android.media.audiofx.Visualizer.OnDataCaptureListener,int,boolean,boolean)
method.
Return | |
---|---|
Int |
the maximum capture rate expressed in milliHertz |
getMeasurementMode
open fun getMeasurementMode(): Int
Returns the current measurement modes performed by this audio effect
Return | |
---|---|
Int |
the mask of the measurements, MEASUREMENT_MODE_NONE (when no measurements are performed) or MEASUREMENT_MODE_PEAK_RMS . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
getMeasurementPeakRms
open fun getMeasurementPeakRms(measurement: Visualizer.MeasurementPeakRms!): Int
Retrieves the latest peak and RMS measurement. Sets the peak and RMS fields of the supplied Visualizer.MeasurementPeakRms
to the latest measured values.
Parameters | |
---|---|
measurement |
Visualizer.MeasurementPeakRms!: a non-null Visualizer.MeasurementPeakRms instance to store the measurement values. |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_BAD_VALUE , ERROR_NO_MEMORY , ERROR_INVALID_OPERATION or ERROR_DEAD_OBJECT in case of failure. |
getSamplingRate
open fun getSamplingRate(): Int
Returns the sampling rate of the captured audio.
Return | |
---|---|
Int |
the sampling rate in milliHertz. |
getScalingMode
open fun getScalingMode(): Int
Returns the current scaling mode on the captured visualization data.
Return | |
---|---|
Int |
the scaling mode, see SCALING_MODE_NORMALIZED and SCALING_MODE_AS_PLAYED . |
Exceptions | |
---|---|
java.lang.IllegalStateException |
getWaveForm
open fun getWaveForm(waveform: ByteArray!): Int
Returns a waveform capture of currently playing audio content. The capture consists in a number of consecutive 8-bit (unsigned) mono PCM samples equal to the capture size returned by getCaptureSize()
.
This method must be called when the Visualizer is enabled.
Parameters | |
---|---|
waveform |
ByteArray!: array of bytes where the waveform should be returned, array length must be at least equals to the capture size returned by getCaptureSize() . |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_NO_MEMORY , ERROR_INVALID_OPERATION or ERROR_DEAD_OBJECT in case of failure. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
|
java.lang.IllegalArgumentException |
release
open fun release(): Unit
Releases the native Visualizer resources. It is a good practice to release the visualization engine when not in use.
setCaptureSize
open fun setCaptureSize(size: Int): Int
Sets the capture size, i.e. the number of bytes returned by 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.
Parameters | |
---|---|
size |
Int: requested capture size |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_INVALID_OPERATION if Visualizer effect enginer not enabled. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
if the effect is not in proper state. |
java.lang.IllegalArgumentException |
if the size parameter is invalid (out of supported range). |
setDataCaptureListener
open fun setDataCaptureListener(
listener: Visualizer.OnDataCaptureListener?,
rate: Int,
waveform: Boolean,
fft: Boolean
): Int
Registers an OnDataCaptureListener interface and specifies the rate at which the capture should be updated as well as the type of capture requested.
Call this method with a null listener to stop receiving the capture updates.
Parameters | |
---|---|
listener |
Visualizer.OnDataCaptureListener?: OnDataCaptureListener registered This value may be null . |
rate |
Int: rate in milliHertz at which the capture should be updated |
waveform |
Boolean: true if a waveform capture is requested: the onWaveFormDataCapture() method will be called on the OnDataCaptureListener interface. |
fft |
Boolean: true if a frequency capture is requested: the onFftDataCapture() method will be called on the OnDataCaptureListener interface. |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_NO_INIT or ERROR_BAD_VALUE in case of failure. |
setEnabled
open fun setEnabled(enabled: Boolean): Int
Enable or disable the visualization engine.
Parameters | |
---|---|
enabled |
Boolean: requested enable state |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_INVALID_OPERATION or ERROR_DEAD_OBJECT in case of failure. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
setMeasurementMode
open fun setMeasurementMode(mode: Int): Int
Sets the combination of measurement modes to be performed by this audio effect.
Parameters | |
---|---|
mode |
Int: a mask of the measurements to perform. The valid values are MEASUREMENT_MODE_NONE (to cancel any measurement) or MEASUREMENT_MODE_PEAK_RMS . |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_BAD_VALUE in case of failure. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
setScalingMode
open fun setScalingMode(mode: Int): Int
Set the type of scaling applied on the captured visualization data.
Parameters | |
---|---|
mode |
Int: see SCALING_MODE_NORMALIZED and SCALING_MODE_AS_PLAYED |
Return | |
---|---|
Int |
SUCCESS in case of success, ERROR_BAD_VALUE in case of failure. |
Exceptions | |
---|---|
java.lang.IllegalStateException |
Protected methods
finalize
protected open fun finalize(): Unit
Exceptions | |
---|---|
java.lang.Throwable |
the Exception raised by this method |