aic-sdk-wasm - 声学的Web大会装订SDK
(原始内容存档于2018-03-27) (英语). WebAssembly spainper for the ai-coustics Speech Information SDK. 此软件包允许您直接在浏览器中运行AI-声学模型.
全面文件,访问 docs.ai-coustics.com (中文(中国大陆) )。 。 。 .
[注] 这个SDK需要许可证密钥. 生成您的密钥 开发者.ai-coustics.io。 。 。 .
安装
npm install @ai-coustics/aic-sdk-wasm
软件包由WebAssembly SIMD(simd128),需要运行时间支持它.
快速启动
import init, { Model, Processor } from "@ai-coustics/aic-sdk-wasm";
// Initialize the WASM module
await init();
// Load a model
const response = await fetch("https://artifacts.ai-coustics.io/models/quail-vf-2-3-s-16khz/v8/quail_vf_2_3_s_16khz_398hl32w_v12.aicmodel");
const modelBytes = new Uint8Array(await response.arrayBuffer());
const model = Model.fromBytes(modelBytes);
// Get optimal configuration
const sampleRate = model.getOptimalSampleRate();
const blockSize = model.getOptimalBlockSize(sampleRate);
// Create and initialize processor
const processor = new Processor(model, "YOUR_LICENSE_KEY");
processor.initialize(sampleRate, blockSize, false);
const audioBuffer = new Float32Array(blockSize);
// Process a mono audio block in place
processor.process(audioBuffer);
使用量
SDK 资讯
import { getVersion, getCompatibleModelVersion } from "@ai-coustics/aic-sdk-wasm";
// Get SDK version
console.log(`SDK version: ${getVersion()}`);
// Get compatible model version
console.log(`Compatible model version: ${getCompatibleModelVersion()}`);
装入模型
下载模型并找到可用的ID 文物. ai-coustics.io。 。 。 .
由于浏览器无法直接访问文件系统,所以您必须将模型文件作为字节数组(Uint8Array)加载.
// Fetch the .aicmodel file
const response = await fetch("https://artifacts.ai-coustics.io/models/quail-vf-2-3-s-16khz/v8/quail_vf_2_3_s_16khz_398hl32w_v12.aicmodel");
const bytes = new Uint8Array(await response.arrayBuffer());
// Create Model instance
const model = Model.fromBytes(bytes);
示范信息
// Get model ID
const modelId = model.getId();
// Get optimal sample rate for the model
const optimalRate = model.getOptimalSampleRate();
// Get the optimal block size for a specific sample rate
const optimalBlockSize = model.getOptimalBlockSize(48000);
配置处理器
这个 Processor 只接受增强和绕行模型。 专门的 VAD 模式去
通过 Vad 分类和分析模型
Analyzer; 传递错误类型 ModelTypeUnsupported。 。 。 .
// Create processor
const processor = new Processor(model, "YOUR_LICENSE_KEY");
// Initialize with audio settings
processor.initialize(
sampleRate, // Sample rate in Hz (8000 - 192000)
blockSize, // Samples per processing call
variableBlockSize // Allow shorter blocks than blockSize (default: false)
);
处理音频
处理器为单相. 处理多通道音频,将其混为一,或 每个频道创建一个处理器.
const buffer = new Float32Array(blockSize);
// Process a mono audio block in place
processor.process(buffer);
处理器上下文
import { ProcessorParameter } from "@ai-coustics/aic-sdk-wasm";
// Get processor context
const procCtx = processor.getContext();
// Get the delay applied to the audio, in samples
const delay = procCtx.getAudioDelay();
// Reset processor state (clears internal buffers)
procCtx.reset();
// Set enhancement parameters
procCtx.setParameter(ProcessorParameter.EnhancementLevel, 0.8);
procCtx.setParameter(ProcessorParameter.Bypass, 0.0);
// Get parameter values
const level = procCtx.getParameter(ProcessorParameter.EnhancementLevel);
console.log(`Enhancement level: ${level}`);
语音活动检测( VAD)
语音活性探测是一等物体. 它运行一个专门的 VAD 模式通过
页:1 Vad 类,并被明确驱动,独立于任何处理器. new Vad(...)
掷出 ModelTypeUnsupported 对于任何不是VAD模型.
import { Vad, VadParameter } from "@ai-coustics/aic-sdk-wasm";
// Load a dedicated VAD model and create a Vad from it
const vadResponse = await fetch("https://artifacts.ai-coustics.io/models/vad-ms-2-1-xxs-16khz/v8/vad_ms_2_1_xxs_16khz_mw7jdprk_v36.aicmodel");
const vadModel = Model.fromBytes(new Uint8Array(await vadResponse.arrayBuffer()));
const vad = new Vad(vadModel, "YOUR_LICENSE_KEY");
const vadSampleRate = vadModel.getOptimalSampleRate();
const vadBlockSize = vadModel.getOptimalBlockSize(vadSampleRate);
vad.initialize(vadSampleRate, vadBlockSize, false);
const vadCtx = vad.getContext();
// Configure VAD parameters
vadCtx.setParameter(VadParameter.Sensitivity, 0.8); // probability threshold, 0.0 - 1.0
vadCtx.setParameter(VadParameter.SpeechHoldDuration, 0.03);
vadCtx.setParameter(VadParameter.MinimumSpeechDuration, 0.0);
// Get parameter values
const sensitivity = vadCtx.getParameter(VadParameter.Sensitivity);
console.log(`VAD sensitivity: ${sensitivity}`);
// The VAD is driven explicitly and does not modify the audio
vad.process(block);
if (vadCtx.isSpeechDetected()) {
console.log("Speech detected!");
}
// Raw model prediction, without speech hold / thresholding post-processing
console.log(vadCtx.getRawVadProbability());
在原音频上运行 VAD
如果你一起使用增强和VAD, 向 VAD 输入原始输入音频,而不是 处理器增强输出. 在同一个单块上同时运行两个对象 而不是把他们锁起来:
vad.process(block); // reads the block, does not modify it
processor.process(block); // enhances the block in place
增强是用来改变信号的,所以在输出上运行 VAD 意味着 检测音频中不再符合VAD模型预期的语音。 也堆起来 处理器在 VAD 本身的预测延迟之上的延迟,它作出语音决定 更难调整.
延迟
处理器会延迟音频, VAD 并不会, 所以两个查询会报告不同的事情和 彼此独立:
const audioDelay = procCtx.getAudioDelay(); // enhanced audio lags the input by this much
const predictionDelay = vadCtx.getPredictionDelay(); // the VAD prediction lags the same input by this much
getPredictionDelay() 实值 没有 适用于音频—— vad.process 离开缓冲器
无所未有. 用它来将语音决定和音频时间表排成一行.
// Clear VAD state on a stream discontinuity
vadCtx.reset();
// Refresh a JWT-form license without reconstructing the VAD
// vadCtx.updateBearerToken(newJwt);
能源语音活动检测
一个增强处理器可以在不运行单独的VAD模型的情况下报告语音活动. 处理前创建上下文, 并在每个音频块后读取其预测 :
const energyVadCtx = processor.getEnergyVadContext();
energyVadCtx.setParameter(VadParameter.Sensitivity, 6.0);
processor.process(block);
const speechDetected = energyVadCtx.isSpeechDetected();
EnergyVadContext 还提供 getParameter, (中文(简体) ), getPredictionDelay,以及 reset。 。 。 .
它能活过处理器。 重置其设置而不重置增强;
重置处理器也会重置其能动VAD.
灵敏度从1到15,更高的值能探测出更安静的信号.
创建上下文即使在绕过或完全干燥时仍保持活性推论.
分析
仅用于分析的模型(例如: Tyto)使用 Analyzer 类代替 Processor。解析器通过音频线程缓冲音频 buffer 并按需运行分析模型 analyze()。 。 。 .
import { Analyzer } from "@ai-coustics/aic-sdk-wasm";
// Load an analysis model and create an Analyzer from it
const analysisResponse = await fetch("https://artifacts.ai-coustics.io/models/tyto-1-2-l-16khz/v8/tyto_1_2_l_16khz_ef8fy2t4_v65.aicmodel");
const analysisModel = Model.fromBytes(new Uint8Array(await analysisResponse.arrayBuffer()));
const sampleRate = analysisModel.getOptimalSampleRate();
const blockSize = analysisModel.getOptimalBlockSize(sampleRate);
const analyzer = new Analyzer(analysisModel, "YOUR_LICENSE_KEY");
analyzer.initialize(sampleRate, blockSize, false);
// From the audio path:
analyzer.buffer(audioChunk);
// From any context:
const result = analyzer.analyze();
console.log(result.riskScore, result.speakerReverb, result.speakerLoudness,
result.interferingSpeech, result.noise, result.codecDegradation,
result.packetLoss);
result.free();
// Optional: clear buffered audio on stream discontinuity.
analyzer.reset();
// Optional: refresh a JWT-form license without reconstructing the analyzer.
// analyzer.updateBearerToken(newJwt);
analyzer.free();
analysisModel.free();
每个班都接受一个模范家庭,扔 ModelTypeUnsupported 其余部分:
| 类 | 接受模式 |
|---|---|
Processor | 增强, 绕行 |
Vad | 专用 VAD |
Analyzer | 分析 |
内存管理
在不再需要对象时,应当手动释放WebAssembly内存. 如果浏览器支持, 则不需要这样做 参考资料薄弱。 。 。 .
// When finished with the VAD
vadCtx.free();
vad.free();
// When finished with the processor
processor.free();
// When finished with the model (and you won't create new objects from it)
model.free();
早日结束遥测会话
当自有对象解脱后,一个遥测会话会自动被停止. 因为
JavaScript 对象处理位置由垃圾收集器决定,可以延迟
任意, Processor, (中文(简体) ), Vad 财务报告和审定财务报表 Analyzer 也暴露一个明确的
terminateSession() 寿命周期事件。 经处理后,该对象为无
用于处理音频.
processor.terminateSession();
vad.terminateSession();
analyzer.terminateSession();
实例
见 examples/ 完整工作实例目录,包括:
- 索引.html: Full 交互式演示,包含一个增强模型,一个VAD模型和一个分析模型,参数控制,以及实时VAD可视化的文件输入.
- 基准.html: 衡量处理延迟性的业绩基准工具.
- 基础.html: 演示所有核心SDK API.
文档
- 完整文档数字 : docs.ai-coustics.com (中文(中国大陆) )
- 可用模式数字 : 文物. ai-coustics.io