Modify this script, i want to stream.write after the loop, in the loop i want to…
- User
Modify this script, i want to stream.write after the loop, in the loop i want to accumulate all audio segment in one segment and then add it in stream.write :
import asyncio import edge_tts import pyaudio from io import BytesIO from pydub import AudioSegment
TEXT = "aHello World!" VOICE = "en-GB-SoniaNeural"
p = pyaudio.PyAudio()
async def stream_tts(text: str, voice: str) -> None:
# We're assuming a certain format, channels, and rate # This will need to be dynamic based on the actual audio data from TTS stream = p.open( format=pyaudio.paInt16, channels=1, rate=26000, output=True ) communicate = edge_tts.Communicate(text, voice) # This will act as our chunk size chunk_size = 1024 # Process and play audio chunks as they arrive async for chunk in communicate.stream(): if chunk["type"] == "audio": try: audio_segment = AudioSegment.from_file(BytesIO(chunk["data"]), format="mp3") # Write data to the stream directly without extra buffering stream.write(audio_segment.raw_data) # If this is the last chunk, break after playing if chunk.get('end', False): break except Exception as e: print("Error processing audio chunk:", e) # Cleanup stream.stop_stream() stream.close() p.terminate()if name == "main": # Run the asyncio event loop asyncio.run(stream_tts(TEXT*100, VOICE))
- Assistant
To accumulate all audio segments into one before writing to the stream, you can modify the script by creating an
AudioSegmentvariable to collect all of the audio data. Write to the stream only after you exit the loop. Here's how you can do it:import asyncio import edge_tts import pyaudio from io import BytesIO from pydub import AudioSegment TEXT = "Hello World!" VOICE = "en-GB-SoniaNeural" p = pyaudio.PyAudio() async def stream_tts(text: str, voice: str) -> None: stream = p.open( format=pyaudio.paInt16, channels=1, rate=26000, output=True ) communicate = edge_tts.Communicate(text, voice) # Initialize an empty AudioSegment to accumulate audio accumulated_segment = AudioSegment.empty() # Process and accumulate audio chunks async for chunk in communicate.stream(): if chunk["type"] == "audio": try: audio_segment = AudioSegment.from_file(BytesIO(chunk["data"]), format="mp3") # Instead of writing to the stream, accumulate the audio accumulated_segment += audio_segment # If this is the last chunk, break after processing if chunk.get('end', False): break except Exception as e: print("Error processing audio chunk:", e) # After accumulating, write the full audio segment to the stream stream.write(accumulated_segment.raw_data) # Cleanup stream.stop_stream() stream.close() p.terminate() if __name__ == "__main__": # Run the asyncio event loop asyncio.run(stream_tts(TEXT*100, VOICE))In this updated script, we initialize
accumulated_segmentas an emptyAudioSegment. We then add each incomingaudio_segmentto it, enlarging it with each received chunk of audio. After the loop, we write the entireaccumulated_segmentto the pyaudio stream with a singlestream.writecall. This means that you won't hear the audio as it's being processed, but only after the full text has been accumulated and sent to the stream at once.