For a class, I am trying to stream audio from a ChatGPT API response. The code below mostly works, and I am getting good quality when I play the saved file later, but if I try to do it live it is super choppy: # Initialize an empty AudioSegment for concatenation full_audio = AudioSegment.empty() stream_completion = client.chat.completions.create( model="gpt-4o-audio-preview", modalities=["text", "audio"], audio={"voice": "alloy", "format": "pcm16"}, messages=[ { "role": "user", "content": "Can you tell me a funny short story about a pickle?" } ], stream=True ) # Play the audio as it comes in and concatenate it for chunk in stream_completion: chunk_audio = getattr(chunk.choices[0].delta, 'audio', None) if chunk_audio is not None: pcm_bytes = base64.b64decode(chunk_audio.get('data', '')) if pcm_bytes: audio_segment = AudioSegment.from_raw( io.BytesIO(pcm_bytes), sample_width=2, # 16-bit PCM frame_rate=24000, # 24kHz sample rate channels=1 # Mono audio ) play(audio_segment) # Concatenate the audio segment full_audio += audio_segment # Save the concatenated audio to a file full_audio.export("assets/audio/full_audio.wav", format="wav") Any idea of how to smooth the audio out while I play it live via stream? Continue reading...