snowboy_ros
Demo.java
Go to the documentation of this file.
1 import ai.kitt.snowboy.*;
2 
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5 
6 import javax.sound.sampled.AudioFormat;
7 import javax.sound.sampled.AudioSystem;
8 import javax.sound.sampled.DataLine;
9 import javax.sound.sampled.SourceDataLine;
10 import javax.sound.sampled.TargetDataLine;
11 
12 public class Demo {
13  static {
14  System.loadLibrary("snowboy-detect-java");
15  }
16 
17  public static void main(String[] args) {
18  // Sets up audio.
19  AudioFormat format = new AudioFormat(16000, 16, 1, true, false);
20  DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
21 
22  // Sets up Snowboy.
23  SnowboyDetect detector = new SnowboyDetect("resources/common.res",
24  "resources/snowboy.umdl");
25  detector.SetSensitivity("0.5");
26  detector.SetAudioGain(1);
27 
28  try {
29  TargetDataLine targetLine =
30  (TargetDataLine) AudioSystem.getLine(targetInfo);
31  targetLine.open(format);
32  targetLine.start();
33 
34  // Reads 0.1 second of audio in each call.
35  byte[] targetData = new byte[3200];
36  short[] snowboyData = new short[1600];
37  int numBytesRead;
38 
39  while (true) {
40  // Reads the audio data in the blocking mode. If you are on a very slow
41  // machine such that the hotword detector could not process the audio
42  // data in real time, this will cause problem...
43  numBytesRead = targetLine.read(targetData, 0, targetData.length);
44 
45  if (numBytesRead == -1) {
46  System.out.print("Fails to read audio data.");
47  break;
48  }
49 
50  // Converts bytes into int16 that Snowboy will read.
51  ByteBuffer.wrap(targetData).order(
52  ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(snowboyData);
53 
54  // Detection.
55  int result = detector.RunDetection(snowboyData, snowboyData.length);
56  if (result > 0) {
57  System.out.print("Hotword " + result + " detected!\n");
58  }
59  }
60  } catch (Exception e) {
61  System.err.println(e);
62  }
63  }
64 }
Demo.main
static void main(String[] args)
Definition: Demo.java:17
Demo
Definition: Demo.java:12
SnowboyDetect
Definition: snowboy.cc:5
demo.detector
detector
Definition: demo.py:27