AudioClassifier

public final class AudioClassifier extends BaseAudioTaskApi

Performs audio classification on audio clips or audio stream.

This API expects a TFLite model with mandatory TFLite Model Metadata that contains the mandatory AudioProperties of the solo input audio tensor and the optional (but recommended) label items as AssociatedFiles with type TENSOR_AXIS_LABELS per output classification tensor.

Input tensor: (kTfLiteFloat32)

  • input audio buffer of size `[batch * samples]`.
  • batch inference is not supported (`batch` is required to be 1).
  • for multi-channel models, the channels need be interleaved.

At least one output tensor with: (kTfLiteFloat32)

  • `[1 x N]` array with `N` represents the number of categories.
  • optional (but recommended) label items as AssociatedFiles with type TENSOR_AXIS_LABELS, containing one label per line. The first such AssociatedFile (if any) is used to fill the `category_name` field of the results. The `display_name` field is filled from the AssociatedFile (if any) whose locale matches the `display_names_locale` field of the `AudioClassifierOptions` used at creation time ("en" by default, i.e. English). If none of these are available, only the `index` field of the results will be filled.

Summary

Nested types

Options for setting up and AudioClassifier.

Builder for AudioClassifierOptions.

Public methods

AudioClassifierResult
classify(AudioData audioClip)

Performs audio classification on the provided audio clip.

void
classifyAsync(AudioData audioBlock, long timestampMs)

Sends audio data (a block in a continuous audio stream) to perform audio classification, and the results will be available via the ResultListener provided in the AudioClassifierOptions.

static AudioClassifier
createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates an AudioClassifier instance from a model buffer and default AudioClassifierOptions.

static AudioClassifier
createFromFile(Context context, File modelFile)

Creates an AudioClassifier instance from a model file and default AudioClassifierOptions.

static AudioClassifier
createFromFile(Context context, String modelPath)

Creates an AudioClassifier instance from a model file and default AudioClassifierOptions.

static AudioClassifier

Creates an AudioClassifier instance from an AudioClassifierOptions instance.

Inherited methods

From java.lang.AutoCloseable
abstract void
From com.google.mediapipe.tasks.audio.core.BaseAudioTaskApi
void
checkOrSetSampleRate(double sampleRate)

Checks or sets the audio sample rate in the audio stream mode.

void

Closes and cleans up the MediaPipe audio task.

AudioRecord

Creates an android.media.AudioRecord instance to record audio stream that has mono channel at sample rate at sample rate 16kHz, the sample rate required for models like Yamnet.

AudioRecord
createAudioRecord(
    int numChannels,
    int sampleRate,
    int requiredInputBufferSize
)

Creates an android.media.AudioRecord instance to record audio stream.

TaskResult

A synchronous method to process audio clips.

void
sendAudioStreamData(AudioData audioClip, long timestampMs)

An asynchronous method to send audio stream data to the TaskRunner.

Public methods

classify

public AudioClassifierResult classify(AudioData audioClip)

Performs audio classification on the provided audio clip. Only use this method when the AudioClassifier is created with the audio clips mode.

The audio clip is represented as a MediaPipe AudioData object The method accepts audio clips with various length and audio sample rate. It's required to provide the corresponding audio sample rate within the AudioData object.

The input audio clip may be longer than what the model is able to process in a single inference. When this occurs, the input audio clip is split into multiple chunks starting at different timestamps. For this reason, this function returns a vector of ClassificationResult objects, each associated with a timestamp corresponding to the start (in milliseconds) of the chunk data that was classified, e.g.: ClassificationResult #0 (first chunk of data): timestamp_ms: 0 (starts at 0ms) classifications #0 (single head model): category #0: category_name: "Speech" score: 0.6 category #1: category_name: "Music" score: 0.2 ClassificationResult #1 (second chunk of data): timestamp_ms: 800 (starts at 800ms) classifications #0 (single head model): category #0: category_name: "Speech" score: 0.5 category #1: category_name: "Silence" score: 0.1

Parameters
AudioData audioClip

a MediaPipe AudioData object for processing.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

classifyAsync

public void classifyAsync(AudioData audioBlock, long timestampMs)

Sends audio data (a block in a continuous audio stream) to perform audio classification, and the results will be available via the ResultListener provided in the AudioClassifierOptions. Only use this method when the AudioClassifier is created with the audio stream mode.

The audio block is represented as a MediaPipe AudioData object. The audio data will be resampled, accumulated, and framed to the proper size for the underlying model to consume. It's required to provide the corresponding audio sample rate within AudioData object as well as a timestamp (in milliseconds) to indicate the start time of the input audio block. The timestamps must be monotonically increasing. This method will return immediately after the input audio data is accepted. The results will be available in the `resultListener` provided in the `AudioClassifierOptions`. The `classifyAsync` method is designed to process auido stream data such as microphone input.

The input audio block may be longer than what the model is able to process in a single inference. When this occurs, the input audio block is split into multiple chunks. For this reason, the callback may be called multiple times (once per chunk) for each call to this function.

Parameters
AudioData audioBlock

a MediaPipe AudioData object for processing.

long timestampMs

the input timestamp (in milliseconds).

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an internal error.

createFromBuffer

public static AudioClassifier createFromBuffer(Context context, ByteBuffer modelBuffer)

Creates an AudioClassifier instance from a model buffer and default AudioClassifierOptions.

Parameters
Context context

an Android Context.

ByteBuffer modelBuffer

a direct ByteBuffer or a MappedByteBuffer of the classification model.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during AudioClassifier creation.

createFromFile

public static AudioClassifier createFromFile(Context context, File modelFile)

Creates an AudioClassifier instance from a model file and default AudioClassifierOptions.

Parameters
Context context

an Android Context.

File modelFile

the classification model File instance.

Throws
java.io.IOException

if an I/O error occurs when opening the tflite model file.

com.google.mediapipe.framework.MediaPipeException

if there is an error during AudioClassifier creation.

createFromFile

public static AudioClassifier createFromFile(Context context, String modelPath)

Creates an AudioClassifier instance from a model file and default AudioClassifierOptions.

Parameters
Context context

an Android Context.

String modelPath

path to the classification model in the assets.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during AudioClassifier creation.

createFromOptions

public static AudioClassifier createFromOptions(
    Context context,
    AudioClassifier.AudioClassifierOptions options
)

Creates an AudioClassifier instance from an AudioClassifierOptions instance.

Parameters
Context context

an Android Context.

AudioClassifier.AudioClassifierOptions options

an AudioClassifierOptions instance.

Throws
com.google.mediapipe.framework.MediaPipeException

if there is an error during AudioClassifier creation.