Glätten Sie Streamed Audio aus Chatgpt ausPython

Python-Programme
Anonymous
 Glätten Sie Streamed Audio aus Chatgpt aus

Post by Anonymous »

Für eine Klasse versuche ich, Audio aus einer Chatgpt -API -Antwort zu streamen. Der folgende Code funktioniert hauptsächlich und ich bekomme eine gute Qualität, wenn ich die gespeicherte Datei später abspiele. Wenn ich aber live versuche, ist es super abgehackt: < /p>

Code: Select all

# 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")
Eine Idee, wie man das Audio ausgleicht, während ich es live über Stream spiele?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post