# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# !pip install SpeechRecognition

import speech_recognition as sp
sp.__version__

r = sp.Recognizer()

# +
#Example Recognizer methods for recognizing speech
#recognize_bing() Bing speech
#recognize_google() Google speech
#recognize_google_cloud(): Google Cloud Speech - requires installation of the google-cloud-speech package
#recognize_houndify(): Houndify by SoundHound
#recognize_ibm(): IBM Speech to Text
#recognize_sphinx(): CMU Sphinx - requires installing PocketSphinx
#recognize_wit(): Wit.ai
# -



harvard = sp.AudioFile('harvard.wav')
with harvard as source:
    audio = r.record(source)

type(audio)

r.recognize_google(audio)

#audio with duration 
with harvard as source:
    audio1 = r.record(source , duration = 4)

r.recognize_google(audio1) #recognizing first  four seconds of audio

with harvard as source: #splitting audio files into different segments
     audio2 = r.record(source, duration=5)
     audio3 = r.record(source, duration=8)

r.recognize_google(audio2)

r.recognize_google(audio3)

# +
#offset in audio files - offset delays when the audio should start recording
with harvard as source:
     audio6 = r.record(source, offset=4, duration=3)

r.recognize_google(audio6)
# -

# using a noisy audio source
jackhammer = sp.AudioFile('jackhammer.wav')

with jackhammer as source:
     audio7 = r.record(source)
r.recognize_google(audio7)

# +
with jackhammer as source:
     r.adjust_for_ambient_noise(source)
     audio8 = r.record(source)

r.recognize_google(audio8)
# -

r.recognize_google(audio, show_all=True)


########################### Working with microphones ######################


# !pip install PyAudio


mic = sp.Microphone()

sp.Microphone.list_microphone_names() #list microphone available note the device id of the microphone

with mic as source:
...     r.adjust_for_ambient_noise(source)
...     audio = r.listen(source)
r.recognize_google(audio)

StarcoderdataJupyterScriptsDedupFiltered