Transcribe a wav file using pocketsphinxΒΆ

In this example we take a WAV file recorded at 44100hz, we resample it to 16khz, then we bulkify it so it can be transcribed as a single file, then we send it to pocketsphinx.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import asyncio

from streamtotext import audio, transcriber


async def handle_event(event):
    """This is called whenever we have new transcription information."""
    print(event)


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())