TensorAudio

公開課程 TensorAudio

定義環形緩衝區和一些公用程式函式,用來準備輸入音訊樣本。

會保留鈴聲緩衝區來 輸入音訊資料用戶端可以透過「load」方法提供輸入音訊資料,並存取 透過 `getTensorBuffer` 方法匯總音訊樣本

請注意,此類別只能處理浮動 (AudioFormat.ENCODING_PCM_16BIT) 或 Shorts (AudioFormat.ENCODING_PCM_FLOAT 中) 的輸入音訊。它可在內部轉換並儲存所有音訊 PCM Float 編碼的範例。

Kotlin 的一般用法

   val tensor = TensorAudio.create(format, modelInputLength)
   tensor.load(newData)
   interpreter.run(tensor.getTensorBuffer(), outputBuffer);
 

使用 AudioRecord 的其他範例

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

巢狀類別

類別 TensorAudio.TensorAudioFormat 包含一些常數,用於說明傳入的音訊樣本格式,也就是 管道和取樣率

公用方法

靜態 TensorAudio
create(AudioFormat 格式、int sampleCounts)
建立具有環形緩衝區且大小為 sampleCountsTensorAudio 執行個體 * format.getChannelCount()
靜態 TensorAudio
create(TensorAudio.TensorAudioFormat 格式, int sampleCounts)
建立具有環形緩衝區的 AudioRecord 執行個體,大小為 sampleCounts * format.getChannels()
TensorAudio.TensorAudioFormat
TensorBuffer
getTensorBuffer()
傳回一個浮點數 TensorBuffer,保留 AudioFormat.ENCODING_PCM_FLOAT 中所有可用的音訊範例,即
void
load(short[] src)
將輸入音訊樣本 src 轉換為 ENCODING_PCM_FLOAT,然後儲存在環形中 緩衝區。
void
load(float[] src, int offsetInFloat, int sizeInFloat)
將輸入音訊樣本 src 儲存在環形緩衝區中。
void
load(short[] src、int offsetInShort、int sizeInShort)
將輸入音訊樣本 src 轉換為 ENCODING_PCM_FLOAT,然後儲存在環形中 緩衝區。
攔截
load(錄音)
以非封鎖的方式載入 AudioRecord 中的最新資料。
void
load(float[] src)
將輸入音訊樣本 src 儲存在環形緩衝區中。

繼承的方法

公用方法

公開 靜態 TensorAudio 建立 (AudioFormat 格式、 int sampleCounts)

建立具有環形緩衝區且大小為 sampleCountsTensorAudio 執行個體 * format.getChannelCount()

參數
format TFLite 模型所需的 AudioFormat。會定義 管道數量和取樣率
sampleCounts 提供給模型的樣本數

公開 靜態 TensorAudio 建立 (TensorAudio.TensorAudioFormat 格式, int sampleCounts)

建立具有環形緩衝區的 AudioRecord 執行個體,大小為 sampleCounts * format.getChannels()

參數
format 載入此類別的音訊資料預期 TensorAudio.TensorAudioFormat
sampleCounts 提供給模型的樣本數

公開 TensorAudio.TensorAudioFormat getFormat ()

公開 TensorBuffer getTensorBuffer ()

傳回存放 AudioFormat.ENCODING_PCM_FLOAT 中所有可用音訊樣本的浮點 TensorBuffer,意即值在 [-1, 1] 的範圍內。

公開 void 載入 (短 [] src)

將輸入音訊樣本 src 轉換為 ENCODING_PCM_FLOAT,然後儲存在環形中 緩衝區。

參數
src AudioFormat.ENCODING_PCM_16BIT 中輸入音訊樣本適用對象 多管道輸入,陣列就會交錯。

公開 void 載入 (float[] src、int offsetInFloat、int sizeInFloat)

將輸入音訊樣本 src 儲存在環形緩衝區中。

參數
src AudioFormat.ENCODING_PCM_FLOAT 中輸入音訊樣本適用對象 多管道輸入,陣列就會交錯。
offsetInFloat src 陣列中的起始位置
sizeInFloat 要複製的浮點值數量
擲回
IllegalArgumentException 表示不相容的音訊格式或輸入大小不正確

公開 void 載入 (short[] src、int offsetInShort、int sizeInShort)

將輸入音訊樣本 src 轉換為 ENCODING_PCM_FLOAT,然後儲存在環形中 緩衝區。

參數
src AudioFormat.ENCODING_PCM_16BIT 中輸入音訊樣本適用對象 多管道輸入,陣列就會交錯。
offsetInShort src 陣列中的起始位置
sizeInShort 要複製的短值數量
擲回
IllegalArgumentException 如果無法複製來源陣列

公開 攔截 載入 (錄音)

以非封鎖的方式載入 AudioRecord 中的最新資料。僅限 支援 ENCODING_PCM_16BIT 和 ENCODING_PCM_FLOAT。

參數
record AudioRecord 的執行個體
傳回
  • 擷取的音訊值數量,大小為 channelCount * sampleCount。如果 錄音檔中沒有任何新資料,或是發生錯誤,這個方法會傳回 0。
擲回
IllegalArgumentException 表示不支援的音訊編碼格式
IllegalStateException 如果從 AudioRecord 讀取失敗

公開 void 載入 (浮點值 [] src)

將輸入音訊樣本 src 儲存在環形緩衝區中。

參數
src AudioFormat.ENCODING_PCM_FLOAT 中輸入音訊樣本適用對象 多管道輸入,陣列就會交錯。