I am trying to thread a speech_recognition function to run in the background continuously and the checkingAudio function to see what text was spoken and take actions accordingly, I tried to thread the 2 functions to run in parallel but the speech recon function is getting called over and over again, I have never worked with threading and followed a tutorial on youtube to thread my functions, I get that I could have made a very stupid mistake so I request the person who answers the question to be a little elaborate in their answer and my mistake. Thank you.
Edit
So I deleted a while loop in my listening function which was causing this error making the whole program redundant, but now I am getting TypeError: checkingAudio() missing 1 required positional argument: 'self' which I
as explained here requires me to instantiate a class but I did that and the same error still.
class listen(threading.Thread):
def __init__(self):
self.playmusicobject = playmusic()
self.r = sr.Recognizer()
self.listening()
def listening(self):
self.objectspeak = speak()
self.apiobject = googleAPI()
print("say something")
time.sleep(2.0)
with sr.Microphone() as source:
# self.objectspeak.speaking("say something")
self.audio = self.r.listen(source)
def checkingAudio(self):
time.sleep(0.5)
try:
a = str(self.r.recognize_google(self.audio))
a = str(self.r.recognize_google(self.audio))
print(a)
if a in greetings:
self.objectspeak.speaking("I am good how are you?")
if a in music:
print("playing music")
self.playmusicobject.play()
if a in stop:
print("stopping")
self.playmusicobject.b()
if a in api:
self.apiobject.distance()
else:
print("error")
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))
class speak:
THIS IS A PYTTS class
class googleAPI:
GOOGLE DISTANCE API function calculates distance between 2 places
class playmusic:
def play(self):
self.objectspeak = speak()
playsound.playsound('C:\\Users\legion\Downloads\Music\merimeri.mp3')
def b(self):
self.objectspeak.speaking("music stopped")
while 1:
a = listen
t1 = threading.Thread(target=listen())
t2 = threading.Thread(target= a.checkingAudio())
t1.join()
t2.join()