0

I would love to set the play my example.mp3 to 1.5 and let it play faster while the script is running. Is it possible to do? I googled and didn´t found a solution which worked.

from playsound import playsound
from gtts import gTTS


kompletterText = "Thats an example"
mytext = str(kompletterText)
language = 'de'
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("C:\\Users\\Sawa2\\Desktop\\example.mp3")
playsound("C:\\Users\\Sawa2\\Desktop\\example.mp3")

Cheers

Sawa
  • 3
  • 2
  • Nope. You can look at the [source code](https://github.com/TaylorSMarks/playsound/blob/master/playsound.py) and see that Playsound doesn't have any parameters for varying the speed. It looks like it's intended to do something very simple (play a sound) across multiple platforms (Win/Mac/Linux). gTTS supports [slowing down speech](https://gtts.readthedocs.io/en/latest/changelog.html?highlight=speed#id125) but not speeding it up. – djsumdog Aug 24 '22 at 16:49

1 Answers1

0

Yes, you need use mixer function from pygame and AudioSegment from pydub. First, save the audio in a mp3 file and change the frame rate of the file. The script bellow show this process:

import os, datetime, time
import glob
from gtts import gTTS
from pygame import mixer
from pydub import AudioSegment

def speed_swifter(sound, speed=1.0):
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={"frame_rate": int(sound.frame_rate * speed)})
    return sound_with_altered_frame_rate

def say(frase):
    if os.path.exists("oldsound.mp3"):
        try:
            os.remove("oldsound.mp3")
        except Exception as e:
            time.sleep(0)
    if os.path.exists("newsound.mp3"):
        try:
            os.remove("newsound.mp3")
        except Exception as e:
            time.sleep(0)

    voz = gTTS(frase, lang ="pt-br", slow=False).save("oldsound.mp3")
    mixer.init()
    mixer.music.load("oldsound.mp3")
    mixer.music.play()

    try:
        newSpeed = 1.3
        sound = AudioSegment.from_file("oldsound.mp3")    
        speed_sound = speed_swifter(sound, newSpeed)
        speed_sound.export(os.path.join("newsound.mp3"), format="mp3")
        mixer.init()
        mixer.music.load("newsound.mp3")
        mixer.music.play()
        while mixer.music.get_busy():
            time.sleep(0.1)
    except Exception as e:
        print(e)
say("Testando áudio")