binding/java/test/ai/picovoice/rhino/RhinoTest.java
Go to the documentation of this file.
1 /*
2  Copyright 2018-2021 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.rhino;
14 
15 import org.junit.jupiter.api.AfterEach;
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
18 
19 import javax.sound.sampled.AudioInputStream;
20 import javax.sound.sampled.AudioSystem;
21 import javax.sound.sampled.UnsupportedAudioFileException;
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.nio.ByteOrder;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 import static org.junit.jupiter.api.Assertions.*;
32 
33 public class RhinoTest {
34 
35  private static final String ENVIRONMENT_NAME;
36  private Rhino rhino;
37  private String accessKey = System.getProperty("pvTestingAccessKey");
38 
39  static {
40  ENVIRONMENT_NAME = Utils.getEnvironmentName();
41  }
42 
43  private static String appendLanguage(String s, String language) {
44  if (language == "en")
45  return s;
46  return s + "_" + language;
47  }
48 
49  private static String getTestContextPath(String language, String context) {
50  return Paths.get(System.getProperty("user.dir"))
51  .resolve("../../resources")
52  .resolve(appendLanguage("contexts", language))
53  .resolve(ENVIRONMENT_NAME)
54  .resolve(context + "_" + ENVIRONMENT_NAME + ".rhn")
55  .toString();
56  }
57 
58  private static String getTestModelPath(String language) {
59  return Paths.get(System.getProperty("user.dir"))
60  .resolve("../../lib/common")
61  .resolve(appendLanguage("rhino_params", language)+".pv")
62  .toString();
63  }
64 
65  private static String getAudioFilePath(String audioFileName) {
66  return Paths.get(System.getProperty("user.dir"))
67  .resolve("../../resources/audio_samples")
68  .resolve(audioFileName)
69  .toString();
70  }
71 
72  @AfterEach
73  void tearDown() {
74  rhino.delete();
75  }
76 
77  @Test
78  void getVersion() throws RhinoException {
79  rhino = new Rhino.Builder()
81  .setContextPath(getTestContextPath("en", "coffee_maker"))
82  .build();
83  assertTrue(rhino.getVersion() != null && !rhino.getVersion().equals(""));
84  }
85 
86  @Test
87  void getFrameLength() throws RhinoException {
88  rhino = new Rhino.Builder()
90  .setContextPath(getTestContextPath("en", "coffee_maker"))
91  .build();
92  assertTrue(rhino.getFrameLength() > 0);
93  }
94 
95  @org.junit.jupiter.api.Test
96  void getSampleRate() throws RhinoException {
97  rhino = new Rhino.Builder()
99  .setContextPath(getTestContextPath("en", "coffee_maker"))
100  .build();
101  assertTrue(rhino.getSampleRate() > 0);
102  }
103 
104  void runTestCase(String audioFileName, boolean isWithinContext, String expectedIntent, Map<String, String> expectedSlots) throws IOException, UnsupportedAudioFileException, RhinoException {
105  int frameLen = rhino.getFrameLength();
106  String audioFilePath = getAudioFilePath(audioFileName);
107  File testAudioPath = new File(audioFilePath);
108 
109  AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(testAudioPath);
110  assertEquals(audioInputStream.getFormat().getFrameRate(), 16000);
111 
112  int byteDepth = audioInputStream.getFormat().getFrameSize();
113  byte[] pcm = new byte[frameLen * byteDepth];
114  short[] rhinoFrame = new short[frameLen];
115 
116  int numBytesRead = 0;
117  boolean isFinalized = false;
118  while ((numBytesRead = audioInputStream.read(pcm)) != -1) {
119  if (numBytesRead / byteDepth == frameLen) {
120 
121  ByteBuffer.wrap(pcm).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(rhinoFrame);
122  isFinalized = rhino.process(rhinoFrame);
123  if (isFinalized) {
124  break;
125  }
126  }
127  }
128  assertTrue(isFinalized);
129 
130  RhinoInference inference = rhino.getInference();
131  assertEquals(inference.getIsUnderstood(), isWithinContext);
132 
133  if(isWithinContext) {
134  assertEquals(inference.getIntent(), expectedIntent);
135  assertEquals(inference.getSlots(), expectedSlots);
136  }
137  }
138 
139  @Test
140  void testWithinContext() throws IOException, UnsupportedAudioFileException, RhinoException {
141  rhino = new Rhino.Builder()
143  .setContextPath(getTestContextPath("en", "coffee_maker"))
144  .build();
145 
146  Map<String, String> expectedSlotValues = new HashMap<String, String>() {{
147  put("size", "medium");
148  put("numberOfShots", "double shot");
149  put("beverage", "americano");
150  }};
151 
152  runTestCase(
153  "test_within_context.wav",
154  true,
155  "orderBeverage",
156  expectedSlotValues
157  );
158  }
159 
160  @Test
161  void testOutOfContext() throws IOException, UnsupportedAudioFileException, RhinoException {
162  rhino = new Rhino.Builder()
164  .setContextPath(getTestContextPath("en", "coffee_maker"))
165  .build();
166 
167  runTestCase(
168  "test_out_of_context.wav",
169  false,
170  null,
171  null
172  );
173  }
174 
175  @Test
176  void testWithinContextDe() throws IOException, UnsupportedAudioFileException, RhinoException {
177 
178  final String language = "de";
179  rhino = new Rhino.Builder()
181  .setContextPath(getTestContextPath(language, "beleuchtung"))
182  .setModelPath(getTestModelPath(language))
183  .build();
184 
185  Map<String, String> expectedSlotValues = new HashMap<String, String>() {{
186  put("state", "aus");
187  }};
188  runTestCase(
189  "test_within_context_de.wav",
190  true,
191  "changeState",
192  expectedSlotValues
193  );
194  }
195 
196  @Test
197  void testOutOfContextDe() throws IOException, UnsupportedAudioFileException, RhinoException {
198  final String language = "de";
199  rhino = new Rhino.Builder()
201  .setContextPath(getTestContextPath(language, "beleuchtung"))
202  .setModelPath(getTestModelPath(language))
203  .build();
204 
205  runTestCase(
206  "test_out_of_context_de.wav",
207  false,
208  null,
209  null
210  );
211  }
212 
213  @Test
214  void testWithinContextEs() throws IOException, UnsupportedAudioFileException, RhinoException {
215 
216  final String language = "es";
217  rhino = new Rhino.Builder()
219  .setContextPath(getTestContextPath(language, "iluminación_inteligente"))
220  .setModelPath(getTestModelPath(language))
221  .build();
222 
223  Map<String, String> expectedSlotValues = new HashMap<String, String>() {{
224  put("location", "habitación");
225  put("color", "rosado");
226  }};
227  runTestCase(
228  "test_within_context_es.wav",
229  true,
230  "changeColor",
231  expectedSlotValues
232  );
233  }
234 
235  @Test
236  void testOutOfContextEs() throws IOException, UnsupportedAudioFileException, RhinoException {
237  final String language = "es";
238  rhino = new Rhino.Builder()
240  .setContextPath(getTestContextPath(language, "iluminación_inteligente"))
241  .setModelPath(getTestModelPath(language))
242  .build();
243 
244  runTestCase(
245  "test_out_of_context_es.wav",
246  false,
247  null,
248  null
249  );
250  }
251 
252  @Test
253  void testWithinContextFr() throws IOException, UnsupportedAudioFileException, RhinoException {
254 
255  final String language = "fr";
256  rhino = new Rhino.Builder()
258  .setContextPath(getTestContextPath(language, "éclairage_intelligent"))
259  .setModelPath(getTestModelPath(language))
260  .build();
261 
262  Map<String, String> expectedSlotValues = new HashMap<String, String>() {{
263  put("color", "violet");
264  }};
265  runTestCase(
266  "test_within_context_fr.wav",
267  true,
268  "changeColor",
269  expectedSlotValues
270  );
271  }
272 
273  @Test
274  void testOutOfContextFr() throws IOException, UnsupportedAudioFileException, RhinoException {
275  final String language = "fr";
276  rhino = new Rhino.Builder()
278  .setContextPath(getTestContextPath(language, "éclairage_intelligent"))
279  .setModelPath(getTestModelPath(language))
280  .build();
281 
282  runTestCase(
283  "test_out_of_context_fr.wav",
284  false,
285  null,
286  null
287  );
288  }
289 }
ai.picovoice.rhino.RhinoTest.getAudioFilePath
static String getAudioFilePath(String audioFileName)
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:65
ai.picovoice.rhino.Rhino.process
boolean process(short[] pcm)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:90
context
ma_context context
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/tests/test_deviceio/ma_test_deviceio.c:56
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:216
s
XmlRpcServer s
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.RhinoTest.appendLanguage
static String appendLanguage(String s, String language)
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:43
ai.picovoice.rhino.RhinoTest.rhino
Rhino rhino
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:36
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.rhino.RhinoTest.ENVIRONMENT_NAME
static final String ENVIRONMENT_NAME
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:35
ai.picovoice.rhino.RhinoTest.accessKey
String accessKey
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:37
ai.picovoice.rhino.Rhino.getVersion
native String getVersion()
ai.picovoice.rhino.Rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.RhinoTest
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:33
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.RhinoTest.getTestModelPath
static String getTestModelPath(String language)
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:58
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.RhinoTest.getTestContextPath
static String getTestContextPath(String language, String context)
Definition: binding/java/test/ai/picovoice/rhino/RhinoTest.java:49


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50