I want to stream audio, but there is a clack clack effect and like void space between chunks, the sound is not smooth as I want. Investigate based on you professional knowledge, make a strong solution and edit the code to make it work as I want.

import asyncio
import pyaudio
import edge_tts

from io import BytesIO
from pydub import AudioSegment

# Configure PyAudio parameters
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024

# Global PyAudio instance
p = pyaudio.PyAudio()

async def generate_and_play_sound(text, voice, speed=0, pitch=0, volume=0):
    # Initialize the audio stream with default settings
    stream = p.open(
        format=p.get_format_from_width(2),  # Assuming 16-bit audio
        channels=1,  # Mono audio
        rate=24000,  # Sample rate (adjust if needed to match TTS)
        output=True
    )

    # Fetch audio data from edge_tts
    communicate = edge_tts.Communicate(text, voice, rate=format_percent(speed), pitch=format_pitch(pitch), volume=format_percent(volume))

    # Process and play audio chunks as they arrive
    async for chunk in communicate.stream():
        if chunk["type"] == "audio":
            try:
                # Decode the chunk data to raw audio and play it
                audio_segment = AudioSegment.from_file(BytesIO(chunk["data"]), format="mp3")
                stream.write(audio_segment.raw_data)
            except Exception as e:
                print("Error processing audio chunk:", e)

    # End and close the stream once all audio has been played
    stream.stop_stream()
    stream.close()
        
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, -200, 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! 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! 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! 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)

    # Terminate the PyAudio instance
    p.terminate()

if __name__ == "__main__":
    asyncio.run(main())