17 from porcupine
import Porcupine
26 return "%s_%s" % (s, language)
30 wav_file = wave.open(file_name, mode=
"rb")
31 channels = wav_file.getnchannels()
32 num_frames = wav_file.getnframes()
34 if wav_file.getframerate() != sample_rate:
36 "Audio file should have a sample rate of %d, got %d" % (sample_rate, wav_file.getframerate()))
38 samples = wav_file.readframes(num_frames)
41 frames = struct.unpack(
'h' * num_frames * channels, samples)
44 print(
"Picovoice processes single-channel audio but stereo file is provided. Processing left channel only.")
46 return frames[::channels]
50 model_path_subdir = cls.
__append_language(
'lib/common/porcupine_params', language)
51 return os.path.join(os.path.dirname(__file__), relative,
'%s.pv' % model_path_subdir)
60 for x
in os.listdir(keyword_files_dir):
61 res[x.rsplit(
'_')[0]] = os.path.join(keyword_files_dir, x)
65 def run_porcupine(self, language, keywords, ground_truth, audio_file_name=None):
66 if audio_file_name
is None:
68 audio_file_name =
'%s.wav' % _audio_file_name_prefix
69 keyword_paths = list()
74 access_key=sys.argv[1],
77 keyword_paths=keyword_paths,
78 sensitivities=[0.5] * len(keyword_paths))
81 os.path.join(os.path.dirname(__file__),
'../../resources/audio_samples/', audio_file_name),
82 porcupine.sample_rate)
84 num_frames = len(audio) // porcupine.frame_length
86 for i
in range(num_frames):
87 frame = audio[i * porcupine.frame_length:(i + 1) * porcupine.frame_length]
88 result = porcupine.process(frame)
90 results.append(result)
94 self.assertEqual(results, ground_truth)
99 keywords=[
'porcupine'],
101 audio_file_name=
'porcupine.wav')
105 'americano',
'blueberry',
'bumblebee',
'grapefruit',
106 'grasshopper',
'picovoice',
'porcupine',
'terminator'
111 ground_truth=[6, 0, 1, 2, 3, 4, 5, 6, 7])
116 keywords=[
'manzana'],
118 audio_file_name=
'manzana.wav')
123 keywords=[
'emparedado',
'leopardo',
'manzana'],
124 ground_truth=[0, 1, 2])
129 keywords=[
'heuschrecke'],
131 audio_file_name=
'heuschrecke.wav')
136 keywords=[
'ananas',
'heuschrecke',
'leguan',
'stachelschwein'],
137 ground_truth=[0, 1, 2, 3])
142 keywords=[
'mon chouchou'],
144 audio_file_name=
'mon_chouchou.wav')
149 keywords=[
'framboise',
'mon chouchou',
'parapluie'],
150 ground_truth=[0, 1, 0, 2])
153 if __name__ ==
'__main__':
154 if len(sys.argv) != 2:
155 print(
"usage: test_porcupine.py ${AccessKey}")
158 unittest.main(argv=sys.argv[:1])