Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Modify this script, i want to stream.write after the loop, in the loop i want to…

  1. 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))

  2. Assistant

    To accumulate all audio segments into one before writing to the stream, you can modify the script by creating an AudioSegment variable 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_segment as an empty AudioSegment. We then add each incoming audio_segment to it, enlarging it with each received chunk of audio. After the loop, we write the entire accumulated_segment to the pyaudio stream with a single stream.write call. 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.