Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyaudio
#1
here is my code what i am trying to do is
when in text noting or something else than mentioned in my code do noting and continue till in text something has been mentioned
like if in text aloha do noting and continue running till how are you in text also i dont want the program close at all . i couldnt find out how to do it tnx for help in advance



def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)

        except Exception as e:
            print("Expection:"+str(e))
    return said


text = get_audio()

if "DC" in text:
    print("https://discord.gg/RjnpzX3")


if "how are you" in text:
    speak("I am fine ")
Reply
#2
When posting code, please include everything needed to run your snippet.
Reply
#3
here my code with imports tnx for advice


import speech_recognition as sr
import pyaudio
import playsound
import os
import time
from gtts import gTTS
from selenium import webdriver
from getpass import getpass
from pynput.keyboard import Key, Controller





def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)

        except Exception as e:
            print("Expection:" + str(e))
    return said


text = get_audio()

if "DC" in text:
    print("https://discord.gg/RjnpzX3")

if "how are you" in text:
    speak("I am fine ")

for sure line 32 is the key point but i dont know what to do there
except Exception as e:
            print("Expection:" + str(e))
Reply
#4
I am not a speech recognition expert, so:
I refer you to this document: https://realpython.com/python-speech-recognition/

There are many others available, this one chosen after a quick glance.
Reply
#5
lets think like this in other subjects when u code expect how you do expect do noting what you think of time.sleep maybe ?

i want to make the whole scenario in loop that continues
Reply
#6
you might be able to do it with delays, but what works for one session may not work for another,
nor for a different user.
The blog whose URL I posted in the post above mentions 7 different packages, and has lots of example code.
It might be possible to get a copy in your native language using google translate.
Reply
#7
I fixed the audio now its not closing after i speak but i have code there 'if 'how are you' in text
speak i am fine that works very well but when i say second time how are you only prints to terminal but doesnt speak back again also there is other if 'DC' in text print 'dc link' this also works in first time but second time when i say Dc doesnt do anything i dont know why can you help me
import source as source
import speech_recognition as sr
import pyaudio
import playsound
import os
import time
from gtts import gTTS
from selenium import webdriver
from getpass import getpass
from pynput.keyboard import Key, Controller


def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)

        except Exception as e:
            print("Expection:" + str(e))
    return said



text = get_audio()

while "DC" in text:
    print("https://discord.gg/RjnpzX3")
    time.sleep(4)

while "how are you" in text:
    speak("I am fine ")
    while True:
     if "" in text:
        get_audio()
Reply
#8
if text ever reaches the condition where it is equal to "",
your code will loop forever between lines 44 - 46, never going back to line 42.
if you add a break statement after line 46, flow will go back to line 42 and continue outer loop,
it will then exit the first loop because text will = "", the process will then terminate.

while "how are you" in text:
    speak("I am fine ")
    while True:
     if "" in text:
        get_audio()
        break
This code seems convoluted. I'm on the way out the door, but if no one else does so before I get back, I'll take a closer look.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 362 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Not getting response from pyaudio OceansBlue 1 2,645 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 1,937 Jan-18-2021, 09:56 AM
Last Post: DT2000
  pyAudio playing variable Talking2442 3 3,026 Dec-01-2020, 06:20 PM
Last Post: Talking2442
  PyAudio [Errorno -9999] Unanticipated Host Error iMuny 5 5,639 Sep-21-2020, 06:58 PM
Last Post: jefsummers
  PyAudio Buffer Data Calculation MclarenF1 0 2,128 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  PyAudio throwing Input overflowed anthares 3 4,762 Jun-14-2020, 03:37 PM
Last Post: anthares
  Using pyaudio to stop recording under certain sound threshold Twanski94 2 6,433 Jun-13-2020, 11:35 AM
Last Post: Twanski94
  update imhow in callback from pyaudio markB 0 2,342 May-28-2020, 06:01 PM
Last Post: markB
  When piping a FFMPEG stream to PyAudio, I get a "click" on every loop klehman 0 4,828 Dec-15-2019, 04:22 AM
Last Post: klehman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020