silero-vad 官方新增了java 的demo

本文介绍了如何将AndroidGitHub上的SileroVADJava示例代码应用于Java中,简化了基于freeswitch获取PCM数据时的静音检测,利用OnnxRuntime执行模型预测。
该文章已生成可运行项目,

原来参考android GitHub - gkonovalov/android-vad: Android Voice Activity Detection (VAD) library. Supports WebRTC VAD GMM, Silero VAD DNN, Yamnet VAD DNN models.的kt改写java demo 可费劲了 

上个月  https://github.com/snakers4/silero-vad/tree/master/examples/java-example 官方新增了例子  在java判断pcm 的静音简单了 

package org.example;

import ai.onnxruntime.OrtException;
import javax.sound.sampled.*;
import java.util.Map;

public class App {

    private static final String MODEL_PATH = "src/main/resources/silero_vad.onnx";
    private static final int SAMPLE_RATE = 16000;
    private static final float START_THRESHOLD = 0.6f;
    private static final float END_THRESHOLD = 0.45f;
    private static final int MIN_SILENCE_DURATION_MS = 600;
    private static final int SPEECH_PAD_MS = 500;
    private static final int WINDOW_SIZE_SAMPLES = 2048;

    public static void main(String[] args) {
        // Initialize the Voice Activity Detector
        SlieroVadDetector vadDetector;
        try {
            vadDetector = new SlieroVadDetector(MODEL_PATH, START_THRESHOLD, END_THRESHOLD, SAMPLE_RATE, MIN_SILENCE_DURATION_MS, SPEECH_PAD_MS);
        } catch (OrtException e) {
            System.err.println("Error initializing the VAD detector: " + e.getMessage());
            return;
        }

        // Set audio format
        AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

        // Get the target data line and open it with the specified format
        TargetDataLine targetDataLine;
        try {
            targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
            targetDataLine.open(format);
            targetDataLine.start();
        } catch (LineUnavailableException e) {
            System.err.println("Error opening target data line: " + e.getMessage());
            return;
        }

        // Main loop to continuously read data and apply Voice Activity Detection
        while (targetDataLine.isOpen()) {
            byte[] data = new byte[WINDOW_SIZE_SAMPLES];

            int numBytesRead = targetDataLine.read(data, 0, data.length);
            if (numBytesRead <= 0) {
                System.err.println("Error reading data from target data line.");
                continue;
            }

            // Apply the Voice Activity Detector to the data and get the result
            Map<String, Double> detectResult;
            try {
                detectResult = vadDetector.apply(data, true);
            } catch (Exception e) {
                System.err.println("Error applying VAD detector: " + e.getMessage());
                continue;
            }

            if (!detectResult.isEmpty()) {
                System.out.println(detectResult);
            }
        }

        // Close the target data line to release audio resources
        targetDataLine.close();
    }
}

运行加下onnx的 dll git 下载下

System.load("F:\\jar\\onnxruntime-win-x64-1.16.3\\lib\\onnxruntime.dll");

对应基于freeswitch 获取到的pcm数据判断静音就简单了

vadDetector.apply(data, true); 主要方法就是get float值

// Call the model to get the prediction probability of speech
float speechProb = 0;
try {
    speechProb = model.call(new float[][]{audioData}, samplingRate)[0];
} catch (OrtException e) {
    throw new RuntimeException(e);
}

 

  有兴趣可以到https://item.taobao.com/item.htm?id=653611115230

本文章已经生成可运行项目
### Silero-VAD 语音活动检测工具使用指南 Silero-VAD 是一个功能强大且易于集成的语音活动检测工具,适用于多种语音处理场景[^2]。以下是关于 Silero-VAD 的安装、配置和使用的基本指南: #### 1. 安装 Silero-VAD 要开始使用 Silero-VAD,首先需要安装其依赖项和模型文件。以下是一个简单的安装步骤: ```bash # 使用 pip 安装 silero-vad 库 pip install torch pip install torchaudio pip install numpy pip install soundfile ``` 接下来,下载预训练的 Silero-VAD 模型: ```python import os import torch # 下载 Silero-VAD 模型 model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad', model='silero_vad', force_reload=True) (get_speech_timestamps, save_audio, read_audio, VADIterator, collect_chunks) = utils ``` #### 2. 配置 Silero-VAD Silero-VAD 提供了多种参数以调整其性能,例如灵敏度阈值和窗口大小。这些参数可以通过 `VADIterator` 或其他函数进行设置。 ```python vad_iterator = VADIterator(model) # 设置语音检测的灵敏度(默认为 0.5) vad_iterator.speech_threshold = 0.7 ``` #### 3. 使用 Silero-VAD 进行语音活动检测 Silero-VAD 可以用于实时或离线检测音频中的语音活动。以下是一个示例代码,展示如何从音频文件中提取语音片段的时间戳: ```python import soundfile as sf # 加载音频文件 audio_path = "example.wav" audio, sr = sf.read(audio_path) # 获取语音活动的时间戳 speech_timestamps = get_speech_timestamps(audio, model, sampling_rate=sr) print("语音活动的时间戳:", speech_timestamps) ``` 如果需要保存提取出的语音片段,可以使用以下代码: ```python # 合并所有语音片段 speech_chunks = collect_chunks(speech_timestamps, audio) # 保存合并后的语音片段 save_audio("output_speech.wav", speech_chunks, sr) ``` #### 4. 常见问题及解决方法 在安装或使用 Silero-VAD 的过程中可能会遇到一些问题。例如: - 如果模型加载失败,请确保 PyTorch 和 Torchaudio 的版本兼容。 - 如果音频格式不支持,请转换为支持的格式(如 WAV)后再进行处理。 更多详细信息可以参考 Silero-VAD官方 GitHub 页面[^1]或教程文档[^3]。 --- ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值