The audio played is alterning between vol 1 & 0, like if the chunk are not linked due to…
- User
The audio played is alterning between vol 1 & 0, like if the chunk are not linked due to audio streaming. Find a solution to have a smooth audio with all chunked linked, maybe inspire on Youtube, which have little wait time before playing audio to link the first chunks : import asyncio import pyaudio import edge_tts
from io import BytesIO from pydub import AudioSegment
Global PyAudio instance
p = pyaudio.PyAudio()
Stream handle
stream = None
async def generate_and_play_sound(text, voice, speed = 0, pitch = 0, volume = 0): global stream, p
# If a stream is active, stop and close it if stream is not None and not stream.is_stopped(): stream.stop_stream() stream.close() full_audio_data = bytearray() # Fetch audio data from edge_tts communicate = edge_tts.Communicate(text, voice, rate = format_percent(speed), pitch = format_pitch(pitch), volume = format_percent(volume)) async for chunk in communicate.stream(): if chunk["type"] == "audio": try: audio_segment = AudioSegment.from_file(BytesIO(chunk["data"]), format="mp3") raw_data = audio_segment.raw_data stream = p.open(format=p.get_format_from_width(audio_segment.sample_width), channels=audio_segment.channels, rate=audio_segment.frame_rate, output=True) stream.write(raw_data) except: print("woot")def format_pitch(pitch): return f"{pitch:+.0f}Hz"
def format_percent(percent): return f"{percent:+.0f}%"
async def main(): # Test text and voices test_texts = [ ("Hello World! How are you today? I'm super good!", "en-GB-SoniaNeural", 15, -20, 0), ("Another text to synthesize and play. Hello World! How are you today? I'm super good! Hello World! How are you today? I'm super good! Hello World! How are you today? I'm super good! Hello World! How are you today? I'm super good! Hello World! How are you today? I'm super good!", "en-GB-SoniaNeural", 5, -20, 0), ("Yet another sound test.", "en-GB-SoniaNeural", 5, -20, 0), ("Final sound test!", "en-GB-SoniaNeural", 5, -20, 0), ]
for text, voice, speed, pitch, volume in test_texts: await generate_and_play_sound(text, voice, speed, pitch, volume) # Stop and close the last stream if stream is not None: stream.stop_stream() stream.close() # Terminate the PyAudio instance p.terminate()if name == "main": asyncio.run(main())
- Assistant
- User
It's better, but now I have a click click effect on the sound
- Assistant
- User
I hear nothing now, it's worse and it crash after some seconds
- Assistant