async def test_hello_44100_get_chunk(self): path = os.path.join(os.path.dirname(__file__), 'test_data/hello_44100.wav') wav = audio.WaveSource(path, chunk_frames=1000) a_s = audio.SquelchedSource(wav, squelch_level=200) chunks = [] async with a_s.listen(): async for chunk in a_s.chunks: chunks.append(chunk)
async def test_transcribe(self): hello_path = os.path.join(os.path.dirname(__file__), 'test_data/hello_44100.wav') wav = audio.WaveSource(hello_path) sq_wav = audio.SquelchedSource(wav, squelch_level=200) cv_wav = audio.RateConvert(sq_wav, 1, 16000) ts = transcriber.PocketSphinxTranscriber.default_config(cv_wav) handler = EvHandler(ts) ts.register_event_handler(handler.handle) async with ts: await handler.called.wait() self.assertEqual(handler.events[0].results[0].transcript, 'hello')
def transcribe_wav_file(path, watson_user, watson_pass): # Create an audio source from the wav file wav_src = audio.WaveSource(path) # Create a transcriber for watson which will read from our audio source ts = transcriber.WatsonTranscriber(wav_src, 44100, watson_user, watson_pass) # Register our handle_event method to be called when transcription occurs ts.register_event_handler(handle_event) # Run transcription loop = asyncio.get_event_loop() loop.run_until_complete(ts.transcribe())
async def test_hello_44100_get_chunk(self): path = os.path.join(os.path.dirname(__file__), 'test_data/hello_44100.wav') wav = audio.WaveSource(path, chunk_frames=1000) a_s = audio.SquelchedSource(wav, squelch_level=200) block_cnt = 0 chunks = [] async with a_s.listen(): async for block in a_s: block_cnt += 1 async for chunk in block: chunks.append(chunk) self.assertEqual(1, block_cnt) self.assertEqual(15, len(chunks))
async def test_hello_44100_wave_get_chunk(self): path = os.path.join(os.path.dirname(__file__), 'test_data/hello_44100.wav') src = audio.WaveSource(path, chunk_frames=1000) chunks = [] async with src.listen(): async for chunk in src.chunks: chunks.append(chunk) for chunk in chunks[:-1]: self.assertEqual(2000, len(chunk.audio)) self.assertEqual(2, chunk.width) self.assertEqual(44100, chunk.freq) full_chunk = audio.merge_chunks(chunks) self.assertEqual(2, full_chunk.width) self.assertEqual(44100, full_chunk.freq)
def transcribe_wav_file(path, watson_user, watson_pass): # Create an audio source from the wav file wav_src = audio.WaveSource(path) # Resample the audio to 16000hz which is what pocketsphinx expects cv_wav = audio.RateConvert(wav_src, 1, 16000) # Bulkify the wav audio source. See `Bulk Transcription` doc. bulk_wav = audio.Bulkify(cv_wav) # Create a transcriber for pocketsphinx which will read from our audio # source ts = transcriber.PocketSphinxTranscriber.default_config(bulk_wav) # Register our handle_event method to be called when transcription occurs ts.register_event_handler(handle_event) # Run transcription loop = asyncio.get_event_loop() loop.run_until_complete(ts.transcribe())
def hello_wave_source(): wav_path = os.path.join(utils.wav_dir(), 'hello_44100.wav') return audio.WaveSource(wav_path, chunk_frames=1000)