Path: blob/master/examples/java-example/src/main/java/org/example/App.java
1171 views
package org.example;12import ai.onnxruntime.OrtException;3import javax.sound.sampled.*;4import java.util.Map;56public class App {78private static final String MODEL_PATH = "src/main/resources/silero_vad.onnx";9private static final int SAMPLE_RATE = 16000;10private static final float START_THRESHOLD = 0.6f;11private static final float END_THRESHOLD = 0.45f;12private static final int MIN_SILENCE_DURATION_MS = 600;13private static final int SPEECH_PAD_MS = 500;14private static final int WINDOW_SIZE_SAMPLES = 2048;1516public static void main(String[] args) {17// Initialize the Voice Activity Detector18SlieroVadDetector vadDetector;19try {20vadDetector = new SlieroVadDetector(MODEL_PATH, START_THRESHOLD, END_THRESHOLD, SAMPLE_RATE, MIN_SILENCE_DURATION_MS, SPEECH_PAD_MS);21} catch (OrtException e) {22System.err.println("Error initializing the VAD detector: " + e.getMessage());23return;24}2526// Set audio format27AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);28DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);2930// Get the target data line and open it with the specified format31TargetDataLine targetDataLine;32try {33targetDataLine = (TargetDataLine) AudioSystem.getLine(info);34targetDataLine.open(format);35targetDataLine.start();36} catch (LineUnavailableException e) {37System.err.println("Error opening target data line: " + e.getMessage());38return;39}4041// Main loop to continuously read data and apply Voice Activity Detection42while (targetDataLine.isOpen()) {43byte[] data = new byte[WINDOW_SIZE_SAMPLES];4445int numBytesRead = targetDataLine.read(data, 0, data.length);46if (numBytesRead <= 0) {47System.err.println("Error reading data from target data line.");48continue;49}5051// Apply the Voice Activity Detector to the data and get the result52Map<String, Double> detectResult;53try {54detectResult = vadDetector.apply(data, true);55} catch (Exception e) {56System.err.println("Error applying VAD detector: " + e.getMessage());57continue;58}5960if (!detectResult.isEmpty()) {61System.out.println(detectResult);62}63}6465// Close the target data line to release audio resources66targetDataLine.close();67}68}697071