13 package ai.picovoice.rhino;
15 import org.junit.jupiter.api.AfterEach;
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
19 import javax.sound.sampled.AudioInputStream;
20 import javax.sound.sampled.AudioSystem;
21 import javax.sound.sampled.UnsupportedAudioFileException;
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;
31 import static org.junit.jupiter.api.Assertions.*;
37 private String
accessKey = System.getProperty(
"pvTestingAccessKey");
46 return s +
"_" + language;
50 return Paths.get(System.getProperty(
"user.dir"))
51 .resolve(
"../../resources")
59 return Paths.get(System.getProperty(
"user.dir"))
60 .resolve(
"../../lib/common")
66 return Paths.get(System.getProperty(
"user.dir"))
67 .resolve(
"../../resources/audio_samples")
68 .resolve(audioFileName)
78 void getVersion() throws RhinoException {
87 void getFrameLength() throws RhinoException {
95 @org.junit.jupiter.api.Test
96 void getSampleRate() throws RhinoException {
104 void runTestCase(String audioFileName,
boolean isWithinContext, String expectedIntent, Map<String, String> expectedSlots)
throws IOException, UnsupportedAudioFileException, RhinoException {
107 File testAudioPath =
new File(audioFilePath);
109 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(testAudioPath);
110 assertEquals(audioInputStream.getFormat().getFrameRate(), 16000);
112 int byteDepth = audioInputStream.getFormat().getFrameSize();
113 byte[] pcm =
new byte[frameLen * byteDepth];
114 short[] rhinoFrame =
new short[frameLen];
116 int numBytesRead = 0;
117 boolean isFinalized =
false;
118 while ((numBytesRead = audioInputStream.read(pcm)) != -1) {
119 if (numBytesRead / byteDepth == frameLen) {
121 ByteBuffer.wrap(pcm).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(rhinoFrame);
128 assertTrue(isFinalized);
131 assertEquals(inference.getIsUnderstood(), isWithinContext);
133 if(isWithinContext) {
134 assertEquals(inference.getIntent(), expectedIntent);
135 assertEquals(inference.getSlots(), expectedSlots);
140 void testWithinContext() throws IOException, UnsupportedAudioFileException, RhinoException {
146 Map<String, String> expectedSlotValues =
new HashMap<String, String>() {{
147 put(
"size",
"medium");
148 put(
"numberOfShots",
"double shot");
149 put(
"beverage",
"americano");
153 "test_within_context.wav",
161 void testOutOfContext() throws IOException, UnsupportedAudioFileException, RhinoException {
168 "test_out_of_context.wav",
176 void testWithinContextDe() throws IOException, UnsupportedAudioFileException, RhinoException {
178 final String language =
"de";
185 Map<String, String> expectedSlotValues =
new HashMap<String, String>() {{
189 "test_within_context_de.wav",
197 void testOutOfContextDe() throws IOException, UnsupportedAudioFileException, RhinoException {
198 final String language =
"de";
206 "test_out_of_context_de.wav",
214 void testWithinContextEs() throws IOException, UnsupportedAudioFileException, RhinoException {
216 final String language =
"es";
223 Map<String, String> expectedSlotValues =
new HashMap<String, String>() {{
224 put(
"location",
"habitación");
225 put(
"color",
"rosado");
228 "test_within_context_es.wav",
236 void testOutOfContextEs() throws IOException, UnsupportedAudioFileException, RhinoException {
237 final String language =
"es";
245 "test_out_of_context_es.wav",
253 void testWithinContextFr() throws IOException, UnsupportedAudioFileException, RhinoException {
255 final String language =
"fr";
262 Map<String, String> expectedSlotValues =
new HashMap<String, String>() {{
263 put(
"color",
"violet");
266 "test_within_context_fr.wav",
274 void testOutOfContextFr() throws IOException, UnsupportedAudioFileException, RhinoException {
275 final String language =
"fr";
283 "test_out_of_context_fr.wav",