rhino/demo/java/src/ai/picovoice/rhinodemo/FileDemo.java
Go to the documentation of this file.
1 /*
2  Copyright 2018-2020 Picovoice Inc.
3 
4  You may not use this file except in compliance with the license. A copy of the license is
5  located in the "LICENSE" file accompanying this source.
6 
7  Unless required by applicable law or agreed to in writing, software distributed under the
8  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
9  express or implied. See the License for the specific language governing permissions and
10  limitations under the License.
11 */
12 
13 package ai.picovoice.rhinodemo;
14 
15 import ai.picovoice.rhino.Rhino;
17 import org.apache.commons.cli.*;
18 
19 import javax.sound.sampled.AudioFormat;
20 import javax.sound.sampled.AudioInputStream;
21 import javax.sound.sampled.AudioSystem;
22 import javax.sound.sampled.UnsupportedAudioFileException;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.ByteBuffer;
26 import java.nio.ByteOrder;
27 import java.util.Map;
28 
29 public class FileDemo {
30 
31  public static void runDemo(String accessKey, File inputAudioFile, String libraryPath, String modelPath,
32  String contextPath, float sensitivity, boolean requireEndpoint) {
33 
34  AudioInputStream audioInputStream;
35  try {
36  audioInputStream = AudioSystem.getAudioInputStream(inputAudioFile);
37  } catch (UnsupportedAudioFileException e) {
38  System.err.println("Audio format not supported. Please provide an input file of .au, .aiff or .wav format");
39  return;
40  } catch (IOException e) {
41  System.err.println("Could not find input audio file at " + inputAudioFile);
42  return;
43  }
44 
45  Rhino rhino = null;
46  try {
47  rhino = new Rhino.Builder()
48  .setAccessKey(accessKey)
49  .setLibraryPath(libraryPath)
50  .setModelPath(modelPath)
51  .setContextPath(contextPath)
52  .setSensitivity(sensitivity)
53  .setRequireEndpoint(requireEndpoint)
54  .build();
55 
56  AudioFormat audioFormat = audioInputStream.getFormat();
57 
58  if (audioFormat.getSampleRate() != 16000.0f || audioFormat.getSampleSizeInBits() != 16) {
59  throw new IllegalArgumentException(String.format("Invalid input audio file format. " +
60  "Input file must be a %dkHz, 16-bit audio file.", rhino.getSampleRate()));
61  }
62 
63  if (audioFormat.getChannels() > 1) {
64  System.out.println("Picovoice processes single-channel audio, but a multi-channel file was provided. " +
65  "Processing leftmost channel only.");
66  }
67 
68  int frameIndex = 0;
69  short[] rhinoFrame = new short[rhino.getFrameLength()];
70 
71  ByteBuffer sampleBuffer = ByteBuffer.allocate(audioFormat.getFrameSize());
72  sampleBuffer.order(ByteOrder.LITTLE_ENDIAN);
73  while (audioInputStream.available() != 0) {
74 
75  int numBytesRead = audioInputStream.read(sampleBuffer.array());
76  if (numBytesRead < 2) {
77  break;
78  }
79 
80  rhinoFrame[frameIndex++] = sampleBuffer.getShort(0);
81 
82  if (frameIndex == rhinoFrame.length) {
83 
84  boolean isFinalized = rhino.process(rhinoFrame);
85  if (isFinalized) {
86 
87  RhinoInference inference = rhino.getInference();
88  if (inference.getIsUnderstood()) {
89 
90  System.out.println("{");
91  System.out.println(String.format(" intent : '%s'", inference.getIntent()));
92  System.out.println(" slots : {");
93  for (Map.Entry<String, String> slot : inference.getSlots().entrySet()) {
94  System.out.println(String.format(" %s : '%s'", slot.getKey(), slot.getValue()));
95  }
96  System.out.println(" }");
97  System.out.println("}");
98  } else {
99  System.out.println("Didn't understand the command.");
100  }
101  return;
102  }
103  frameIndex = 0;
104  }
105  }
106  System.out.println("Reached end of audio file before Rhino returned an inference.");
107  } catch (Exception e) {
108  System.out.println(e.toString());
109  } finally {
110  if (rhino != null) {
111  rhino.delete();
112  }
113  }
114  }
115 
116  public static void main(String[] args) {
117 
118  Options options = BuildCommandLineOptions();
119  CommandLineParser parser = new DefaultParser();
120  HelpFormatter formatter = new HelpFormatter();
121 
122  CommandLine cmd;
123  try {
124  cmd = parser.parse(options, args);
125  } catch (ParseException e) {
126  System.out.println(e.getMessage());
127  formatter.printHelp("rhinofiledemo", options);
128  System.exit(1);
129  return;
130  }
131 
132  if (cmd.hasOption("help")) {
133  formatter.printHelp("rhinofiledemo", options);
134  return;
135  }
136 
137  String accessKey = cmd.getOptionValue("access_key");
138  String inputAudioPath = cmd.getOptionValue("input_audio_path");
139  String libraryPath = cmd.getOptionValue("library_path");
140  String modelPath = cmd.getOptionValue("model_path");
141  String contextPath = cmd.getOptionValue("context_path");
142  String sensitivityStr = cmd.getOptionValue("sensitivity");
143  String requireEndpointValue = cmd.getOptionValue("require_endpoint");
144 
145  if (accessKey == null || accessKey.length() == 0) {
146  throw new IllegalArgumentException("AccessKey is required for Rhino.");
147  }
148  // parse sensitivity
149  float sensitivity = 0.5f;
150  if (sensitivityStr != null) {
151  try {
152  sensitivity = Float.parseFloat(sensitivityStr);
153  } catch (Exception e) {
154  throw new IllegalArgumentException("Failed to parse sensitivity value. " +
155  "Must be a floating-point number between [0,1].");
156  }
157 
158  if (sensitivity < 0 || sensitivity > 1) {
159  throw new IllegalArgumentException(String.format("Failed to parse sensitivity value (%s). " +
160  "Must be a floating-point number between [0,1].", sensitivity));
161  }
162  }
163 
164  if(inputAudioPath == null){
165  throw new IllegalArgumentException("No input audio file provided. This is a required argument.");
166  }
167  File inputAudioFile = new File(inputAudioPath);
168  if (!inputAudioFile.exists()) {
169  throw new IllegalArgumentException(String.format("Audio file at path %s does not exits.", inputAudioPath));
170  }
171 
172  if(contextPath == null){
173  throw new IllegalArgumentException("No context file provided. This is a required argument.");
174  }
175  File contextFile = new File(contextPath);
176  if (!contextFile.exists()) {
177  throw new IllegalArgumentException(String.format("Context file at path '%s' does not exist", contextPath));
178  }
179 
180  if (libraryPath == null) {
181  libraryPath = Rhino.LIBRARY_PATH;
182  }
183 
184  if (modelPath == null) {
185  modelPath = Rhino.MODEL_PATH;
186  }
187 
188  boolean requireEndpoint = true;
189  if (requireEndpointValue != null && requireEndpointValue.toLowerCase().equals("false")) {
190  requireEndpoint = false;
191  }
192 
193  runDemo(accessKey, inputAudioFile, libraryPath, modelPath, contextPath, sensitivity, requireEndpoint);
194  }
195 
196  private static Options BuildCommandLineOptions() {
197  Options options = new Options();
198 
199  options.addOption(Option.builder("a")
200  .longOpt("access_key")
201  .hasArg(true)
202  .desc("AccessKey obtained from Picovoice Console (https://picovoice.ai/console/).")
203  .build());
204 
205  options.addOption(Option.builder("i")
206  .longOpt("input_audio_path")
207  .hasArg(true)
208  .desc("Absolute path to input audio file.")
209  .build());
210 
211  options.addOption(Option.builder("c")
212  .longOpt("context_path")
213  .hasArg(true)
214  .desc("Absolute path to context file.")
215  .build());
216 
217  options.addOption(Option.builder("l")
218  .longOpt("library_path")
219  .hasArg(true)
220  .desc("Absolute path to the Rhino native runtime library.")
221  .build());
222 
223  options.addOption(Option.builder("m")
224  .longOpt("model_path")
225  .hasArg(true)
226  .desc("Absolute path to the file containing model parameters.")
227  .build());
228 
229  options.addOption(Option.builder("s")
230  .longOpt("sensitivity")
231  .hasArgs()
232  .desc("Inference sensitivity. It should be a number within [0, 1]. A higher sensitivity value results in " +
233  "fewer misses at the cost of (potentially) increasing the erroneous inference rate. " +
234  "If not set 0.5 will be used.")
235  .build());
236 
237  options.addOption(Option.builder("e")
238  .longOpt("require_endpoint")
239  .hasArg(true)
240  .desc("If set to `false`, Rhino does not require an endpoint (chunk of silence) before " +
241  "finishing inference.")
242  .build());
243 
244  options.addOption(new Option("h", "help", false, ""));
245 
246  return options;
247  }
248 }
ai.picovoice.rhino.Rhino.process
boolean process(short[] pcm)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:90
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:221
ai.picovoice.rhino.Rhino.MODEL_PATH
static final String MODEL_PATH
Definition: java/src/ai/picovoice/rhino/Rhino.java:36
ai.picovoice.rhino.RhinoInference.getIntent
String getIntent()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:30
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:216
ai.picovoice.rhino.Rhino.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:206
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.rhinodemo.FileDemo.BuildCommandLineOptions
static Options BuildCommandLineOptions()
Definition: rhino/demo/java/src/ai/picovoice/rhinodemo/FileDemo.java:196
ai.picovoice.rhino.Rhino.LIBRARY_PATH
static final String LIBRARY_PATH
Definition: java/src/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.Rhino.Builder.setLibraryPath
Builder setLibraryPath(String libraryPath)
Definition: java/src/ai/picovoice/rhino/Rhino.java:192
ai.picovoice.rhino.RhinoInference
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:15
ai
ai.picovoice.rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoActivationException.java:11
ai.picovoice.rhinodemo.FileDemo
Definition: rhino/demo/java/src/ai/picovoice/rhinodemo/FileDemo.java:29
ai.picovoice
ai.picovoice.rhino.Rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.Rhino.Builder.setRequireEndpoint
Builder setRequireEndpoint(boolean requireEndpoint)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:226
ai.picovoice.rhino.Rhino.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:211
ai.picovoice.rhino.Rhino.Builder
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:199
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:120
ai.picovoice.rhino.RhinoInference.getIsUnderstood
boolean getIsUnderstood()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:26
ai.picovoice.rhinodemo.FileDemo.main
static void main(String[] args)
Definition: rhino/demo/java/src/ai/picovoice/rhinodemo/FileDemo.java:116
args
cmd
string cmd
ai.picovoice.rhino.Rhino.Builder.build
Rhino build(Context context)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:266
ai.picovoice.rhino.Rhino.getFrameLength
native int getFrameLength()
ai.picovoice.rhino.Rhino.delete
void delete()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:71
ai.picovoice.rhino.RhinoInference.getSlots
Map< String, String > getSlots()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:34
ai.picovoice.rhinodemo.FileDemo.runDemo
static void runDemo(String accessKey, File inputAudioFile, String libraryPath, String modelPath, String contextPath, float sensitivity, boolean requireEndpoint)
Definition: rhino/demo/java/src/ai/picovoice/rhinodemo/FileDemo.java:31


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:55