VadFromMicWithNonStreamingMoonshine.java
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2024 Xiaomi Corporation
// This file shows how to use a silero_vad model with a non-streaming
// Moonshine tiny for speech recognition.
import com.k2fsa.sherpa.onnx.*;
import javax.sound.sampled.*;
public class VadFromMicNonStreamingMoonshine {
private static final int sampleRate = 16000;
private static final int windowSize = 512;
public static Vad createVad() {
// please download ./silero_vad.onnx from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
String model = "./silero_vad.onnx";
SileroVadModelConfig sileroVad =
SileroVadModelConfig.builder()
.setModel(model)
.setThreshold(0.5f)
.setMinSilenceDuration(0.25f)
.setMinSpeechDuration(0.5f)
.setWindowSize(windowSize)
.build();
VadModelConfig config =
VadModelConfig.builder()
.setSileroVadModelConfig(sileroVad)
.setSampleRate(sampleRate)
.setNumThreads(1)
.setDebug(true)
.setProvider("cpu")
.build();
return new Vad(config);
}
public static OfflineRecognizer createOfflineRecognizer() {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/moonshine/index.html
// to download model files
String preprocessor = "./sherpa-onnx-moonshine-tiny-en-int8/preprocess.onnx";
String encoder = "./sherpa-onnx-moonshine-tiny-en-int8/encode.int8.onnx";
String uncachedDecoder = "./sherpa-onnx-moonshine-tiny-en-int8/uncached_decode.int8.onnx";
String cachedDecoder = "./sherpa-onnx-moonshine-tiny-en-int8/cached_decode.int8.onnx";
String tokens = "./sherpa-onnx-moonshine-tiny-en-int8/tokens.txt";
OfflineMoonshineModelConfig moonshine =
OfflineMoonshineModelConfig.builder()
.setPreprocessor(preprocessor)
.setEncoder(encoder)
.setUncachedDecoder(uncachedDecoder)
.setCachedDecoder(cachedDecoder)
.build();
OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setMoonshine(moonshine)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();
OfflineRecognizerConfig config =
OfflineRecognizerConfig.builder()
.setOfflineModelConfig(modelConfig)
.setDecodingMethod("greedy_search")
.build();
return new OfflineRecognizer(config);
}
public static void main(String[] args) {
Vad vad = createVad();
OfflineRecognizer recognizer = createOfflineRecognizer();
// https://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/AudioFormat.html
// Linear PCM, 16000Hz, 16-bit, 1 channel, signed, little endian
AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false);
// https://docs.oracle.com/javase/8/docs/api/javax/sound/sampled/DataLine.Info.html#Info-java.lang.Class-javax.sound.sampled.AudioFormat-int-
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
TargetDataLine targetDataLine;
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(format);
targetDataLine.start();
} catch (LineUnavailableException e) {
System.out.println("Failed to open target data line: " + e.getMessage());
vad.release();
recognizer.release();
return;
}
boolean printed = false;
byte[] buffer = new byte[windowSize * 2];
float[] samples = new float[windowSize];
System.out.println("Started. Please speak");
boolean running = true;
while (targetDataLine.isOpen() && running) {
int n = targetDataLine.read(buffer, 0, buffer.length);
if (n <= 0) {
System.out.printf("Got %d bytes. Expected %d bytes.\n", n, buffer.length);
continue;
}
for (int i = 0; i != windowSize; ++i) {
short low = buffer[2 * i];
short high = buffer[2 * i + 1];
int s = (high << 8) + low;
samples[i] = (float) s / 32768;
}
vad.acceptWaveform(samples);
if (vad.isSpeechDetected() && !printed) {
System.out.println("Detected speech");
printed = true;
}
if (!vad.isSpeechDetected()) {
printed = false;
}
while (!vad.empty()) {
SpeechSegment segment = vad.front();
float startTime = segment.getStart() / (float) sampleRate;
float duration = segment.getSamples().length / (float) sampleRate;
OfflineStream stream = recognizer.createStream();
stream.acceptWaveform(segment.getSamples(), sampleRate);
recognizer.decode(stream);
String text = recognizer.getResult(stream).getText();
stream.release();
if (!text.isEmpty()) {
System.out.printf("%.3f--%.3f: %s\n", startTime, startTime + duration, text);
}
if (text.contains("exit the program")) {
running = false;
}
vad.pop();
}
}
vad.release();
recognizer.release();
}
}