Click here to Skip to main content
15,891,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Jarvis should say every instruction comes on screen but here first statement is spoken & none other than that gets audio voice. And while saving the audio only the first statement gets stored as audio file. how to delete the previous audio file after process & save new audio file as instruction.How to get print statements in audio form, like there's a print statement ("Say something!") , it should be there on the screen as well as audio of same should be spoken.

What I have tried:

import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
from pygame import mixer
mixer.init()

def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    os.system("mpg321 audio.mp3")

def speak_out():
    mixer.music.load('audio.mp3')
    mixer.music.play()
    time.sleep(2)

def recordAudio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Say something!")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration=1)
        audio = r.listen(source)
    data = ""
    try:
        data = r.recognize_google(audio)
        print("You said: " + data)        
    except sr.UnknownValueError:
        print("I could not understand")
    except sr.RequestError as e:
        print("Server error. Check your internet connection: {0}".format(e)) 
    return data

def jarvis(data):
    if "how are you" in data:
        speak("I am fine")

    if "what time is it" in data:
        speak(ctime())

    if 'mail' in data:
        speak('who is recipient')
        recipient = speak()

        if 'John' in data:
            speak('What should I say')
            content = recordAudio()
            mail = smtplib.SMTP('smtp.gmail.com', 587)
            mail.ehlo()
            mail.starttls()
            mail.login('abc@gmail.com','xyz')
            mail.sendmail('Person name', 'mail_id', content)
            mail.close()
            speak('Email sent')


time.sleep(2)
speak("Hi Ajay, what can I do for you?")
speak_out()
while 1:
    data = recordAudio()
    jarvis(data)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900