public class **TensorAudio** Defines a ring buffer and some utility functions to prepare the input audio samples.

It maintains a [Ring Buffer](https://en.wikipedia.org/wiki/Circular_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 `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_16BIT`) or Short (in `https://developer.android.com/reference/android/media/AudioFormat.html#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 `https://developer.android.com/reference/android/media/AudioRecord.html`

```
   val tensor = TensorAudio.create(format, modelInputLength)
   Timer().scheduleAtFixedRate(delay, period) {
     tensor.load(audioRecord)
     interpreter.run(tensor.getTensorBuffer(), outputBuffer)
   }
 
```

<br />

<br />

<br />

<br />

<br />

### Nested Classes

|---|---|---|---|
| class | [TensorAudio.TensorAudioFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/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](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio) | [create](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#create(AudioFormat,%20int))(AudioFormat format, int sampleCounts) Creates a `https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio` instance with a ring buffer whose size is `sampleCounts` \* `format.getChannelCount()`. |
| static [TensorAudio](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio) | [create](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#create(org.tensorflow.lite.support.audio.TensorAudio.TensorAudioFormat,%20int))([TensorAudio.TensorAudioFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio.TensorAudioFormat) format, int sampleCounts) Creates a `https://developer.android.com/reference/android/media/AudioRecord.html` instance with a ring buffer whose size is `sampleCounts` \* `format.getChannels()`. |
| [TensorAudio.TensorAudioFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio.TensorAudioFormat) | [getFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#getFormat())() |
| [TensorBuffer](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer) | [getTensorBuffer](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#getTensorBuffer())() Returns a float `https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer` holding all the available audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_FLOAT` i.e. |
| void | [load](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#load(short[]))(short\[\] src) Converts the input audio samples `src` to ENCODING_PCM_FLOAT, then stores it in the ring buffer. |
| void | [load](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#load(float[],%20int,%20int))(float\[\] src, int offsetInFloat, int sizeInFloat) Stores the input audio samples `src` in the ring buffer. |
| void | [load](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#load(short[],%20int,%20int))(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](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#load(AudioRecord))(AudioRecord record) Loads latest data from the `https://developer.android.com/reference/android/media/AudioRecord.html` in a non-blocking way. |
| void | [load](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio#load(float[]))(float\[\] src) Stores the input audio samples `src` in the ring buffer. |

### Inherited Methods

From class java.lang.Object

|---|---|
| boolean | equals([Object](https://developer.android.com/reference/java/lang/Object.html) arg0) |
| final [Class](https://developer.android.com/reference/java/lang/Class.html)\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| [String](https://developer.android.com/reference/java/lang/String.html) | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

## Public Methods

#### public static [TensorAudio](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio)
**create**
(AudioFormat format, int sampleCounts)

Creates a `https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio` instance with a ring buffer whose size is `sampleCounts` \*
`format.getChannelCount()`.

##### Parameters

| format | the `https://developer.android.com/reference/android/media/AudioFormat.html` required by the TFLite model. It defines the number of channels and sample rate. |
| sampleCounts | the number of samples to be fed into the model |
|---|---|

#### public static [TensorAudio](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio)
**create**
([TensorAudio.TensorAudioFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio.TensorAudioFormat) format, int sampleCounts)

Creates a `https://developer.android.com/reference/android/media/AudioRecord.html` instance with a ring buffer whose size is `sampleCounts` \* `format.getChannels()`.

##### Parameters

| format | the expected `https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio.TensorAudioFormat` of audio data loaded into this class. |
| sampleCounts | the number of samples to be fed into the model |
|---|---|

#### public [TensorAudio.TensorAudioFormat](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/audio/TensorAudio.TensorAudioFormat)
**getFormat**
()

<br />

#### public [TensorBuffer](https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer)
**getTensorBuffer**
()

Returns a float `https://developers.google.com/edge/api/tflite/java/org/tensorflow/lite/support/tensorbuffer/TensorBuffer` holding all the available audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_FLOAT` i.e. values are in the range of \[-1, 1\].

#### public void
**load**
(short\[\] src)

Converts the input audio samples `src` to ENCODING_PCM_FLOAT, then stores it in the ring
buffer.

##### Parameters

| src | input audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_16BIT`. For multi-channel input, the array is interleaved. |
|---|---|

#### public void
**load**
(float\[\] src, int offsetInFloat, int sizeInFloat)

Stores the input audio samples `src` in the ring buffer.

##### Parameters

| src | input audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_FLOAT`. For multi-channel input, the array is interleaved. |
| offsetInFloat | starting position in the `src` array |
| sizeInFloat | the number of float values to be copied |
|---|---|

##### Throws

| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | for incompatible audio format or incorrect input size |
|---|---|

#### public 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.

##### Parameters

| src | input audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_16BIT`. For multi-channel input, the array is interleaved. |
| offsetInShort | starting position in the src array |
| sizeInShort | the number of short values to be copied |
|---|---|

##### Throws

| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | if the source array can't be copied |
|---|---|

#### public int
**load**
(AudioRecord record)

Loads latest data from the `https://developer.android.com/reference/android/media/AudioRecord.html` in a non-blocking way. Only
supporting ENCODING_PCM_16BIT and ENCODING_PCM_FLOAT.

##### Parameters

| record | an instance of `https://developer.android.com/reference/android/media/AudioRecord.html` |
|---|---|

##### Returns

- number of captured audio values whose size is `channelCount * sampleCount`. If there was no new data in the AudioRecord or an error occurred, this method will return 0.

##### Throws

| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | for unsupported audio encoding format |
| [IllegalStateException](https://developer.android.com/reference/java/lang/IllegalStateException.html) | if reading from AudioRecord failed |
|---|---|

#### public void
**load**
(float\[\] src)

Stores the input audio samples `src` in the ring buffer.

##### Parameters

| src | input audio samples in `https://developer.android.com/reference/android/media/AudioFormat.html#ENCODING_PCM_FLOAT`. For multi-channel input, the array is interleaved. |
|---|---|