Skip to content

Commit 5d155de

Browse files
authored
Changed example to use microphone and work with Vosk server (#251)
1 parent 38e1edd commit 5d155de

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

client-samples/java/src/main/java/VoiceClientWithMic.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import javax.sound.sampled.TargetDataLine;
77
import java.util.concurrent.*;
88
import java.util.ArrayList;
9+
import javax.sound.sampled.AudioFormat;
10+
import javax.sound.sampled.AudioInputStream;
11+
import javax.sound.sampled.AudioSystem;
912

1013
public class VoiceClientWithMic {
1114

@@ -28,18 +31,35 @@ public ArrayList<String> asr(TargetDataLine line) throws Exception {
2831
ws.addListener(new CustomWebSocketAdapter());
2932
ws.connect();
3033

31-
byte[] buf = new byte[8000];
32-
while (true) {
33-
line.read(buf, 0, buf.length);
34-
if (results.size() > 0 && results.get(results.size() - 1).contains("exit")) {
35-
disconnect(ws);
36-
break;
34+
try {
35+
AudioInputStream audioInputStream = new AudioInputStream(line);
36+
AudioFormat format = audioInputStream.getFormat();
37+
int bytesPerFrame = format.getFrameSize();
38+
if (bytesPerFrame == AudioSystem.NOT_SPECIFIED) {
39+
bytesPerFrame = 1;
3740
}
38-
recieveLatch = new CountDownLatch(1);
39-
ws.sendBinary(buf);
40-
recieveLatch.await();
41-
}
4241

42+
// Let Vosk server now the sample rate of sound file
43+
ws.sendText("{ \"config\" : { \"sample_rate\" : " + (int)format.getSampleRate() + " } }");
44+
45+
// Set an arbitrary buffer size of 1024 frames.
46+
int numBytes = 1024 * bytesPerFrame;
47+
byte[] audioBytes = new byte[numBytes];
48+
try {
49+
int numBytesRead = 0;
50+
// Try to read numBytes bytes from the file.
51+
while ((numBytesRead = audioInputStream.read(audioBytes)) != -1) {
52+
recieveLatch = new CountDownLatch(1);
53+
ws.sendBinary(audioBytes);
54+
recieveLatch.await();
55+
}
56+
disconnect(ws);
57+
} catch (Exception e) {
58+
e.printStackTrace();
59+
}
60+
} catch (Exception ex) {
61+
ex.printStackTrace();
62+
}
4363
return results;
4464
}
4565

@@ -59,9 +79,9 @@ private void disconnect(WebSocket ws) {
5979
private TargetDataLine getLine() {
6080
TargetDataLine line;
6181

62-
//It must be a 16 kHz (or 8 kHz, depending on the training data), 16bit Mono (= single channel) Little-Endian file
82+
//It must be a 16 kHz (or 8 kHz, depending on the training data), 16bit Mono (= single channel) Big-Endian
6383
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
64-
float rate = 8000.0f;
84+
float rate = 16000.0f;
6585
int channels = 1;
6686
int sampleSize = 16;
6787
boolean bigEndian = false;

0 commit comments

Comments
 (0)