Aldan
Aldan

Reputation: 715

Overwrite pre-existing .mp3 file in Python using gtts

I have a constraint with my syntax. I just tried python some time ago and I started it from AI. I make a bot like Jarvis to help me like opening google or youtube. From the tutorial available in Pythonspot.com it shows for Ubuntu tutorial but I use Windows. And there are some tools or plugins that do not work in windows is mpg321. I have found a replacement with mixer.music to play the sound of the AI. This works but I have constraints on the second sound, ie i make the first sound with audio.mp3 and it works then when my second voice uses the same filename ie audio.mp3 and i have constraints like this

Traceback (most recent call last):
File "D:\@AI Projects\Jarvis\Jarvis.py", line 71, in jarvis(data)
File "D:\@AI Projects\Jarvis\Jarvis.py", line 53, in jarvis speak(ctime())
File "D:\@AI Projects\Jarvis\Jarvis.py", line 17, in speak tts.save("audio.mp3")
File "C:\Users\inialdan\AppData\Local\Programs\Python\Python36\lib\site-packages\gtts\tts.py", line 110, in save with open(savefile, 'wb') as f: PermissionError: [Errno 13] Permission denied: 'audio.mp3'

This is my code

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
from time import ctime
import time
import os
import subprocess
from gtts import gTTS
from pygame import mixer 



def speak(audioString):
    print(audioString)
    tts = gTTS(text=audioString, lang='en')
    tts.save("audio.mp3")
    mixer.init()
    mixer.music.load('D:/@AI Projects/Jarvis/audio.mp3')
    mixer.music.play()

def recordAudio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Try to say something!")
        audio = r.listen(source)

    data = ""

    try:
        data = r.recognize_google(audio)
        print("You said : " + data)
    except sr.UnknownValueError:
        print("I'm Sorry, i couldn't understand what you mean ...")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

    return data

def jarvis(data):

    CHROME = os.path.join('C:\\', 'Program Files (x86)', 'Google', 'Chrome', 'Application', 'chrome.exe')

    if "jarvis" in data:
        speak("Yes, sir ?")
    if "what is your name" in data:
        speak("You can call me, Jarvis")
    if "where do you leave" in data:
        speak("In your heart.")
    if "how are you" in data:
        speak("I am fine")
    if "what time is it" in data:
        speak(ctime())
    if "where is" in data:
        data = data.split(" ")
        location = data[2]
        speak("Hold on Aldan, I will show you where " + location + " is.")
        os.system('taskkill /im chrome.exe')
        subprocess.call([CHROME, "https://www.google.nl/maps/place/" + location + "/&"])
    if "open" in data:
        data = data.split(" ")
        application = data[1]
        speak("Hold on Aldan, I will show you " + application)
        os.system('taskkill /im chrome.exe')
        subprocess.call([CHROME, "https://www." + application + ".com"])

time.sleep(2)
speak("Hi Aldan, How may I assist you?")
while 1:
    data = recordAudio()
    jarvis(data)

I have tried it with os.remove (); to remove the audio.mp3 and rewrite it. but still failed

Upvotes: 0

Views: 2792

Answers (3)

Jesscralett
Jesscralett

Reputation: 39

I did this, it worked. You may have to guess the length of the sound. For mine, 2 sec is long enough.

    voice = gtts.gTTS(item)
    voice.save("temp.mp3")
    sound = SoundLoader.load("temp.mp3")
    sound.play()
    time.sleep(2)
    sound.stop()
    os.remove("temp.mp3")

Upvotes: 0

Jonathan Shelton
Jonathan Shelton

Reputation: 26

Just create a second def that removes the filename and run that command after your speak()

def complete():
os.remove('audio.mp3')
speak("blah blah")
complete()

Upvotes: 0

Tanusree Roy
Tanusree Roy

Reputation: 64

I have modified Speak method with the help of TemporaryFile.
Click on the icon to check the Code

from googletrans import Translator
import pygame, time
import tempfile
from gtts import gTTS
from pygame import mixer
from tempfile import TemporaryFile

def speak(text, lang='en'):
    """Text to speech. For funp."""
    try:
        translator = Translator()
        tts = gTTS(text=translator.translate(text, dest=lang).text, lang=lang)
        mixer.init()
        sf = TemporaryFile()
        tts.write_to_fp(sf)
        sf.seek(0)
        mixer.music.load(sf)
        mixer.music.play()
    except Exception:
        raise

Upvotes: 4

Related Questions