public class
TensorAudio
Defines a ring buffer and some utility functions to prepare the input audio samples.
It maintains a Ring Buffer to hold input audio data. Clients could feed input audio data via `load` methods and access the aggregated audio samples via `getTensorBuffer` method.
Note that this class can only handle input audio in Float (in AudioFormat.ENCODING_PCM_16BIT
) or Short (in AudioFormat.ENCODING_PCM_FLOAT
). Internally it converts and stores all the audio
samples in PCM Float encoding.
Typical usage in Kotlin
val tensor = TensorAudio.create(format, modelInputLength) tensor.load(newData) interpreter.run(tensor.getTensorBuffer(), outputBuffer);
Another sample usage with AudioRecord
val tensor = TensorAudio.create(format, modelInputLength) Timer().scheduleAtFixedRate(delay, period) { tensor.load(audioRecord) interpreter.run(tensor.getTensorBuffer(), outputBuffer) }
Nested Classes
class | TensorAudio.TensorAudioFormat | Wraps a few constants describing the format of the incoming audio samples, namely number of channels and the sample rate. |
Public Methods
static TensorAudio |
create(AudioFormat format, int sampleCounts)
Creates a
TensorAudio instance with a ring buffer whose size is sampleCounts *
format.getChannelCount() . |
static TensorAudio |
create(TensorAudio.TensorAudioFormat format, int sampleCounts)
Creates a
AudioRecord instance with a ring buffer whose size is sampleCounts * format.getChannels() . |
TensorAudio.TensorAudioFormat | |
TensorBuffer |
getTensorBuffer()
Returns a float
TensorBuffer holding all the available audio samples in AudioFormat.ENCODING_PCM_FLOAT i.e. |
void |
load(short[] src)
Converts the input audio samples
src to ENCODING_PCM_FLOAT, then stores it in the ring
buffer. |
void |
load(float[] src, int offsetInFloat, int sizeInFloat)
Stores the input audio samples
src in the ring buffer. |
void |
load(short[] src, int offsetInShort, int sizeInShort)
Converts the input audio samples
src to ENCODING_PCM_FLOAT, then stores it in the ring
buffer. |
int |
load(AudioRecord record)
Loads latest data from the
AudioRecord in a non-blocking way. |
void |
load(float[] src)
Stores the input audio samples
src in the ring buffer. |