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")