Speech recognition

Converting audio speech to text

Author

Thomas H. Simm

Resources / Sources

  1. using-speech-recognition-to-convert-speech-to-text-python
  2. create-a-voice-recorder-using-python
  3. i-cant-install-pyaudio-on-windows-how-to-solve-error-microsoft-visual-c-14
  4. examples/audio_transcribe.py

Installs


!pip install speechrecognition pydub
!pip3 install sounddevice
!pip3 install wavio
!pip install scipy

Create a recording

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv
  
# Sampling frequency
freq = 44100
  
# Recording duration
duration = 20
  
# Start recorder with the given values 
# of duration and sample frequency
recording = sd.rec(int(duration * freq), 
                   samplerate=freq, channels=2)
  
# Record audio for the given number of seconds
sd.wait()
  
# This will convert the NumPy array to an audio
# file with the given sampling frequency
# write("recording0.wav", freq, recording)
  
# Convert the NumPy array to audio file
fileo="recording1.wav"
wv.write(fileo, recording, freq, sampwidth=2)

To make it an arbitrary duration

https://python-sounddevice.readthedocs.io/en/0.3.12/examples.html#recording-with-arbitrary-duration

Convert Audio to text

import speech_recognition as sr

AUDIO_FILE = fileo
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
    audio = r.record(source)  # read the entire audio file


try:
    # we're just using the default API key
    print("->> " + r.recognize_google(audio))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))
->> I heard you have a compilation of every good song ever done by anybody every great song by The Beach Boys All the underground hits all the modern lovers tracks I heard you have a vinyl record on German imports I heard that you have a white label every cm

When reading out Losing My Edge by LCD Soundsystem

I heard you have a compilation of every good song ever done by anybody. Every great song by the Beach Boys. All the underground hits. All the Modern Lovers tracks. I heard you have a vinyl of every Niagra record on German import. I heard that you have a white label of every semina