Skip to content

Instantly share code, notes, and snippets.

@doomuch
Created November 26, 2023 10:21
Show Gist options
  • Save doomuch/2df4f85d210d0ba80ff6335a78d872e5 to your computer and use it in GitHub Desktop.
Save doomuch/2df4f85d210d0ba80ff6335a78d872e5 to your computer and use it in GitHub Desktop.
TTS stream Openai
api_url = 'https://api.openai.com/v1/audio/speech'
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
}
payload = {
"model": "tts-1",
"input": """
Death is the irreversible cessation of all biological functions that sustain an organism.[1] For organisms with a brain, death can also be defined as the irreversible cessation of functioning of the whole brain, including the brainstem. [2][3] Brain death is sometimes used as a legal definition of death.[4] The remains of a former organism normally begins to decompose shortly after death.[5] Death is an inevitable process that eventually occurs in all organisms. Some organisms, such as Turritopsis dohrnii, are biologically immortal. However, they can still die from means other than aging.[6]
Determining when someone has definitively died has proven difficult. Initially, death was defined as occurring when breathing and the heartbeat ceased, a status still known as clinical death.[7] However, the development of CPR no longer means it was irreversible.[8] Brain death was the next option, but several definitions exist for this. Some people believe that all brain functions must cease. Some believe that even if the brainstem is still alive, the personality and identity are irretrievably lost, so therefore, the person should be entirely dead.[9]
Death is generally applied to whole organisms; the similar process seen in individual components of an organism, such as cells or tissues, is necrosis.[10] Something that is not considered an organism, such as a virus, can be physically destroyed but is not said to die, as a virus is not considered alive in the first place.[11] As of the early 21st century, 56 million people die per year. The most common reason is cardiovascular disease, which is a disease that affects the heart.[12]''',
""",
"voice": "alloy",
}
# Make the HTTP request with Transfer-Encoding: chunked
s = requests.session()
with s.post(api_url, headers=headers, data=json.dumps(payload), stream=True) as response:
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=44000,
output=True)
# Pre-buffer for smoother playback
try:
for chunk in response.iter_content(chunk_size=4000096):
if chunk:
audio_data = AudioSegment.from_mp3(io.BytesIO(chunk))
raw_data = audio_data.raw_data
stream.write(raw_data)
# Clear the buffer to ensure no old data is played
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Cleanup
stream.stop_stream()
stream.close()
p.terminate()
@chunyeah
Copy link

chunyeah commented Dec 9, 2023

how can i gent AudioSegment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment