Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。
tf.data.microphone() 函數用於生成迭代器,該迭代器使用瀏覽器的本機 FFT 從麥克風音頻流創建 frequency-domain 頻譜圖張量。
注意:
- 此代碼僅在設備帶有麥克風時有效。在運行此 API 時,它會要求允許打開麥克風。
- 此 API 僅適用於瀏覽器環境。
用法:
tf.data.microphone (microphoneConfig)
參數:此函數接受如下所示的參數:
- microphoneConfig:MicrophoneConfig 對象包含從麥克風讀取音頻數據的配置。它是一個可選參數。
該對象包含一些在下麵指定的配置:
- sampleRateHz:它的範圍在 44100 和 48000 之間。
- fftSize:它是一個數值,必須是 2 到 4 和 2 到 14 之間的 2 的冪。
- columnTruncateLength:它是一個數值。
- numFramesPerSpectrogram:它是一個數值。
- audioTrackConstraints:它是 MediaTrackConstraints。
- smoothingTimeConstant:它是一個數值。
- includeSpectrogram:它是一個布爾值。
- includeWaveform:它是一個布爾值。
返回值:它返回一個 MicrophoneIterator。
範例1:運行以下代碼後,它會請求啟動麥克風的權限。授予權限後,下麵的代碼將返回並給出輸出。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling the .data.microphone() function
// with its parameters
const mic = await tf.data.microphone({
fftSize:1024,
columnTruncateLength:32,
numFramesPerSpectrogram:10,
sampleRateHz:43000,
includeSpectrogram:true,
includeWaveform:false
});
// Capturing the data recorded by mircophone
const audioData = await mic.capture();
const spectrogramTensor = audioData.spectrogram;
// Printing the data like sampling rate
// expected and actual
spectrogramTensor.print();
const waveformTensor = audioData.waveform;
waveformTensor.print();
// Stopping the microphone
mic.stop();
輸出:它給出了一個錯誤,因為這裏預期的采樣率為 43000,但實際記錄的值為 48000。
An error occured Mismatch in sampling rate:Expected:43000; Actual:48000
範例2:運行以下代碼後,它會請求啟動麥克風的權限。授予權限後,下麵的代碼將返回並給出輸出。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Initializing the configurations of
// reading audio data from microphone
const x = {
fftSize:1024,
columnTruncateLength:32,
numFramesPerSpectrogram:10,
sampleRateHz:48000,
includeSpectrogram:true,
includeWaveform:false
};
// Calling the .data.microphone() function
// with the parameter specified above
const mic = await tf.data.microphone(x);
// Capturing the data recorded by mircophone
const audioData = await mic.capture();
const spectrogramTensor = audioData.spectrogram;
// Creating an iterator that generate frequency-domain
// spectrogram Tensors from the microphone
spectrogramTensor.print();
const waveformTensor = audioData.waveform;
// Stopping the microphone
mic.stop();
輸出:
Tensor [[[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[0 ], [0 ], [0 ], ..., [0 ], [0 ], [0 ]], [[-Infinity], [-Infinity], [-Infinity], ..., [-Infinity], [-Infinity], [-Infinity]]]
參考:https://js.tensorflow.org/api/latest/#data.microphone
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Tensorflow.js tf.data.microphone() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。