Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
snakers4
GitHub Repository: snakers4/silero-vad
Path: blob/master/examples/java-example/src/main/java/org/example/App.java
1171 views
1
package org.example;
2
3
import ai.onnxruntime.OrtException;
4
import javax.sound.sampled.*;
5
import java.util.Map;
6
7
public class App {
8
9
private static final String MODEL_PATH = "src/main/resources/silero_vad.onnx";
10
private static final int SAMPLE_RATE = 16000;
11
private static final float START_THRESHOLD = 0.6f;
12
private static final float END_THRESHOLD = 0.45f;
13
private static final int MIN_SILENCE_DURATION_MS = 600;
14
private static final int SPEECH_PAD_MS = 500;
15
private static final int WINDOW_SIZE_SAMPLES = 2048;
16
17
public static void main(String[] args) {
18
// Initialize the Voice Activity Detector
19
SlieroVadDetector vadDetector;
20
try {
21
vadDetector = new SlieroVadDetector(MODEL_PATH, START_THRESHOLD, END_THRESHOLD, SAMPLE_RATE, MIN_SILENCE_DURATION_MS, SPEECH_PAD_MS);
22
} catch (OrtException e) {
23
System.err.println("Error initializing the VAD detector: " + e.getMessage());
24
return;
25
}
26
27
// Set audio format
28
AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
29
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
30
31
// Get the target data line and open it with the specified format
32
TargetDataLine targetDataLine;
33
try {
34
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
35
targetDataLine.open(format);
36
targetDataLine.start();
37
} catch (LineUnavailableException e) {
38
System.err.println("Error opening target data line: " + e.getMessage());
39
return;
40
}
41
42
// Main loop to continuously read data and apply Voice Activity Detection
43
while (targetDataLine.isOpen()) {
44
byte[] data = new byte[WINDOW_SIZE_SAMPLES];
45
46
int numBytesRead = targetDataLine.read(data, 0, data.length);
47
if (numBytesRead <= 0) {
48
System.err.println("Error reading data from target data line.");
49
continue;
50
}
51
52
// Apply the Voice Activity Detector to the data and get the result
53
Map<String, Double> detectResult;
54
try {
55
detectResult = vadDetector.apply(data, true);
56
} catch (Exception e) {
57
System.err.println("Error applying VAD detector: " + e.getMessage());
58
continue;
59
}
60
61
if (!detectResult.isEmpty()) {
62
System.out.println(detectResult);
63
}
64
}
65
66
// Close the target data line to release audio resources
67
targetDataLine.close();
68
}
69
}
70
71