Commit 9375d2cc

Stainless Bot <107565488+stainless-bot@users.noreply.github.com>
2024-02-27 00:41:05
docs(examples): add pyaudio streaming example (#1194)
1 parent 9179a03
examples/audio.py
@@ -1,5 +1,6 @@
-#!/usr/bin/env python
+#!/usr/bin/env rye run python
 
+import time
 from pathlib import Path
 
 from openai import OpenAI
@@ -11,6 +12,8 @@ speech_file_path = Path(__file__).parent / "speech.mp3"
 
 
 def main() -> None:
+    stream_to_speakers()
+
     # Create text-to-speech audio file
     with openai.audio.speech.with_streaming_response.create(
         model="tts-1",
@@ -34,5 +37,28 @@ def main() -> None:
     print(translation.text)
 
 
+def stream_to_speakers() -> None:
+    import pyaudio
+
+    player_stream = pyaudio.PyAudio().open(format=pyaudio.paInt16, channels=1, rate=24000, output=True)
+
+    start_time = time.time()
+
+    with openai.audio.speech.with_streaming_response.create(
+        model="tts-1",
+        voice="alloy",
+        response_format="pcm",  # similar to WAV, but without a header chunk at the start.
+        input="""I see skies of blue and clouds of white
+                The bright blessed days, the dark sacred nights
+                And I think to myself
+                What a wonderful world""",
+    ) as response:
+        print(f"Time to first byte: {int((time.time() - start_time) * 1000)}ms")
+        for chunk in response.iter_bytes(chunk_size=1024):
+            player_stream.write(chunk)
+
+    print(f"Done in {int((time.time() - start_time) * 1000)}ms.")
+
+
 if __name__ == "__main__":
     main()
pyproject.toml
@@ -61,7 +61,8 @@ dev-dependencies = [
     "dirty-equals>=0.6.0",
     "importlib-metadata>=6.7.0",
     "azure-identity >=1.14.1",
-    "types-tqdm > 4"
+    "types-tqdm > 4",
+    "types-pyaudio > 0"
 ]
 
 [tool.rye.scripts]
requirements-dev.lock
@@ -126,6 +126,7 @@ tomli==2.0.1
     # via pytest
 tqdm==4.66.1
     # via openai
+types-pyaudio==0.2.16.20240106
 types-pytz==2024.1.0.20240203
     # via pandas-stubs
 types-tqdm==4.66.0.2