3

I am getting an error when writing to an audio file.

Basically, I am overwriting the data in the mp3 whenever my function gets called and then playing it.

It works the first time through, but then gives me [Errno 13] Permission denied: 'file.mp3' then on.

Here is the code:

def speech(self, response):
    audio_file = "response.mp3"
    tts = gTTS(text=str(response), lang="en")
    tts.save(audio_file)
    pygame.mixer.init()
    pygame.mixer.music.load(audio_file)
    pygame.mixer.music.play()

Error is on the tts.save line.

More info:

Traceback (most recent call last):

  File "myprojectpath", line 63, in speech
    tts.save(audio_file)

  File "C:\Python35\lib\site-packages\gtts\tts.py", line 93, in save
    with open(savefile, 'wb') as f:

Thanks!

furas
  • 134,197
  • 12
  • 106
  • 148
Jaromando
  • 309
  • 2
  • 3
  • 9
  • 1
    maybe program (which plays it) blocks access to file. – furas Oct 02 '16 at 16:14
  • would there be a way to test that? – Jaromando Oct 02 '16 at 16:18
  • Do you start python script as administrator? Does Windows user have access to this file? Here the way to verify process accessed to the file or folder http://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows Also do you open this file in script before? – nick_gabpe Oct 02 '16 at 16:32
  • I do not believe any of those are an issue as I tried running Pycharm in admin, it obviously can access the file and write to it if it does it at least once, and I do not ever really open it. The only time it is edited is in the code provided. – Jaromando Oct 02 '16 at 17:35
  • Do you ever call pygame.mixer.music.stop() before trying to rewrite the file? If pygame still has the file open to read from it might be interfering when you try to overwrite the file. – Chachmu Oct 03 '16 at 08:43
  • @Chachmu From what it states in the API pygame.mixer.music.stop() is only used to stop the playback if it is playing. – Jaromando Oct 03 '16 at 12:16
  • If you have the file open on your PC the program won't work do make sure it's closed when saving to it – coolio85 Feb 18 '18 at 14:15

9 Answers9

4
from gtts import gTTS
import playsound
import os

x = ['sunny', 'sagar', 'akhil']
tts = 'tts'
for i in range(0,3):
    tts = gTTS(text= x[i], lang = 'en')
    file1 = str("hello" + str(i) + ".mp3")
    tts.save(file1)
    playsound.playsound(file1,True)
    print 'after'
    os.remove(file1)

change the file name every time you save it, this worked for me.

Sunny
  • 179
  • 1
  • 8
3

You should name your file for a unique name each time , so the solution that I realized is to name your file with date like this ==>

    date_string = datetime.now().strftime("%d%m%Y%H%M%S")
    filename = "voice"+date_string+".mp3"
    tts.save(filename)
    playsound.playsound(filename)
3

try to save the fi open an other file whitout playing, then you will have the permission to delete it here is the talk function:

def Talk (mytext, language= "fr" ):
    myobj = gTTS(text=mytext, lang=language, slow=False)
    myobj.save("1.mp3")
    mixer.init()
    mixer.music.load("1.mp3")
    mixer.music.set_volume(1)
    mixer.music.play()
    mixer.music.get_endevent()
    while mixer.music.get_busy():
        continue
    mixer.music.load("Talker\empty.mp3")
    os.remove("1.mp3")
2

You can create another file in the same folder, just copy and rename it.

File you want to use: ("C:/.../file.mp3") copied file: ("C:/.../file_copy.mp3")

You can change the file loaded in mixer.music.load("C:/.../file.mp3") to mixer.music.load("C:/.../file_copy.mp3") and delete delete the ("C:/.../file.mp3") without problem using this:

from pygame import mixer
import os

mixer.music.load("C:/.../file.mp3")
mixer.music.play()
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")

but the code above will remove the file before even playing it, so you can use a method to await it play:

from pygame import mixer
import os

mixer.music.load("C:/.../file.mp3")
mixer.music.play()
while mixer.music.get_busy(): # check if the file is playing
    pass
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")
1

You are getting this error because the mixer is not releasing that audio file. If you want to mixer release that file you can load a useless or empty file into a mixer. Then you can overwrite that audio file. E. g.. Mixer.load("C:/...../empty.mp3") After this previously loaded file is released.

Ahmad
  • 1,618
  • 5
  • 24
  • 46
ClassHacker
  • 394
  • 3
  • 11
1

if you don't want the file before saved anymore, you can delete the audio file before saving the next file. so, you can save the new file with the same name. bellow is the code I ran successfully

import speech_recognition as ram
from playsound import playsound
from gtts import gTTS
import os
r=ram.Recognizer()
with ram.Microphone() as source:
    while True:
        r.adjust_for_ambient_noise(source,duration=1)
        print("I'm Listening....")
        playsound('audio/listen.mp3')
        audio=r.listen(source, timeout=5)
        text=r.recognize_google(audio)
        tts = gTTS("Boss. you said "+str(text)+". i'm opening it")
        tts.save('audio/text.mp3')
        print("you said "+text+".")
        playsound('audio/text.mp3')
        os.remove('audio/text.mp3')
Kurohige
  • 1,378
  • 2
  • 16
  • 24
Ramc_26
  • 11
  • 2
0

a much better solution is this, if i had the time i would also calculate the probability of ever landing a same file

from gtts import gTTS
from playsound import playsound
import random
import os

#creating a super random named file

r1 = random.randint(1,10000000)
r2 = random.randint(1,10000000)

randfile = str(r2)+"randomtext"+str(r1) +".mp3"

tts = gTTS(text='hey, STOP It!!', lang='en', slow=True)
tts.save(randfile)
playsound(randfile)

print(randfile)
os.remove(randfile)
Ahmed
  • 89
  • 10
0

I have faced the same problem, then figure it out that the file is pointing to the previous position so I have to release it. It works fine and important thing is that it does not create an mp3 file also. If you don't release it, it will work fine for the first time and save the audio file then the error occurs for the second time.

from gtts import gTTS
import  os
import playsound

def tts(text, lang):
     file = gTTS(text = text, lang = lang)
     file.save("audio/speak_func.mp3")
     playsound.playsound('audio/speak_func.mp3', True)
     os.remove("audio/speak_func.mp3")
Md.Rakibuz Sultan
  • 759
  • 1
  • 8
  • 13
0

I know this post is fairly old but the reason this error is thrown is because the mp3 file is loaded into pygames mixer, meaning you cant write to it. you can simply use the pygame.mixer.music.unload() method to unload the file before writing to it again.

def speak(text):
    pygame.mixer.music.unload()
    tts = gTTS(text, lang='en')
    tts.save('s.mp3')

    pygame.mixer.music.load("s.mp3")
    pygame.mixer.music.play()

    while pygame.mixer.get_busy():
        continue
Turb000
  • 11
  • 2