13 package ai.picovoice.porcupine;
15 import org.junit.jupiter.api.AfterEach;
16 import org.junit.jupiter.api.Test;
18 import javax.sound.sampled.AudioInputStream;
19 import javax.sound.sampled.AudioSystem;
20 import javax.sound.sampled.UnsupportedAudioFileException;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.ByteOrder;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.Arrays;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
38 private String
accessKey = System.getProperty(
"pvTestingAccessKey");
47 return s +
"_" + language;
51 return Paths.get(System.getProperty(
"user.dir"))
52 .resolve(
"../../resources")
60 return Paths.get(System.getProperty(
"user.dir"))
61 .resolve(
"../../lib/common")
67 return Paths.get(System.getProperty(
"user.dir"))
68 .resolve(
"../../resources/audio_samples")
69 .resolve(audioFileName)
74 String keywordPaths[] =
new String[
keywords.length];
75 for (
int i=0; i<
keywords.length; i++) {
87 void getVersion() throws PorcupineException {
96 void getFrameLength() throws PorcupineException {
104 @org.junit.jupiter.api.Test
105 void getSampleRate() throws PorcupineException {
116 File testAudioPath =
new File(audioFilePath);
118 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(testAudioPath);
119 assertEquals(audioInputStream.getFormat().getFrameRate(), 16000);
121 int byteDepth = audioInputStream.getFormat().getFrameSize();
122 int bufferSize = frameLen * byteDepth;
123 byte[] pcm =
new byte[bufferSize];
124 short[] porcupineFrame =
new short[frameLen];
125 int numBytesRead = 0;
126 ArrayList<Integer> results =
new ArrayList<>();
127 while ((numBytesRead = audioInputStream.read(pcm)) != -1) {
129 if (numBytesRead / byteDepth == frameLen) {
131 ByteBuffer.wrap(pcm).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(porcupineFrame);
133 assertTrue(result >= -1);
140 assertEquals(results, expectedResults);
144 void testSingleKeyword() throws IOException, UnsupportedAudioFileException,
PorcupineException {
152 new ArrayList<>(Arrays.asList(0)));
156 void testMultipleKeywords() throws IOException, UnsupportedAudioFileException, PorcupineException {
157 final Porcupine.BuiltInKeyword[]
keywords =
new Porcupine.BuiltInKeyword[]{
158 Porcupine.BuiltInKeyword.ALEXA,
159 Porcupine.BuiltInKeyword.AMERICANO,
160 Porcupine.BuiltInKeyword.BLUEBERRY,
161 Porcupine.BuiltInKeyword.BUMBLEBEE,
162 Porcupine.BuiltInKeyword.GRAPEFRUIT,
163 Porcupine.BuiltInKeyword.GRASSHOPPER,
164 Porcupine.BuiltInKeyword.PICOVOICE,
165 Porcupine.BuiltInKeyword.PORCUPINE,
166 Porcupine.BuiltInKeyword.TERMINATOR
175 "multiple_keywords.wav",
176 new ArrayList<>(Arrays.asList(7, 0, 1, 2, 3, 4, 5, 6, 7, 8)));
180 void testSingleKeywordDe() throws IOException, UnsupportedAudioFileException, PorcupineException {
181 final String language =
"de";
182 final String
keywords[] = {
"heuschrecke"};
190 new ArrayList<>(Arrays.asList(0)));
194 void testMultipleKeywordsDe() throws IOException, UnsupportedAudioFileException, PorcupineException {
195 final String language =
"de";
196 final String
keywords[] = {
"ananas",
"heuschrecke",
"leguan",
"stachelschwein"};
204 "multiple_keywords_de.wav",
205 new ArrayList<>(Arrays.asList(0, 1, 2, 3)));
209 void testSingleKeywordEs() throws IOException, UnsupportedAudioFileException, PorcupineException {
210 final String language =
"es";
211 final String
keywords[] = {
"manzana"};
220 new ArrayList<>(Arrays.asList(0)));
224 void testMultipleKeywordsEs() throws IOException, UnsupportedAudioFileException, PorcupineException {
225 final String language =
"es";
226 final String
keywords[] = {
"emparedado",
"leopardo",
"manzana"};
234 "multiple_keywords_es.wav",
235 new ArrayList<>(Arrays.asList(0, 1, 2)));
239 void testSingleKeywordFr() throws IOException, UnsupportedAudioFileException, PorcupineException {
240 final String language =
"fr";
241 final String
keywords[] = {
"mon chouchou"};
250 new ArrayList<>(Arrays.asList(0)));
254 void testMultipleKeywordsFr() throws IOException, UnsupportedAudioFileException, PorcupineException {
255 final String language =
"fr";
256 final String
keywords[] = {
"framboise",
"mon chouchou",
"parapluie"};
264 "multiple_keywords_fr.wav",
265 new ArrayList<>(Arrays.asList(0, 1, 0, 2)));