MCPcopy
hub / github.com/openai/openai-python / send_audio_worker_sounddevice

Function send_audio_worker_sounddevice

examples/realtime/audio_util.py:95–142  ·  view source on GitHub ↗
(
    connection: AsyncRealtimeConnection,
    should_send: Callable[[], bool] | None = None,
    start_send: Callable[[], Awaitable[None]] | None = None,
)

Source from the content-addressed store, hash-verified

93
94
95async def send_audio_worker_sounddevice(
96 connection: AsyncRealtimeConnection,
97 should_send: Callable[[], bool] | None = None,
98 start_send: Callable[[], Awaitable[None]] | None = None,
99):
100 sent_audio = False
101
102 device_info = sd.query_devices()
103 print(device_info)
104
105 read_size = int(SAMPLE_RATE * 0.02)
106
107 stream = sd.InputStream(
108 channels=CHANNELS,
109 samplerate=SAMPLE_RATE,
110 dtype="int16",
111 )
112 stream.start()
113
114 try:
115 while True:
116 if stream.read_available < read_size:
117 await asyncio.sleep(0)
118 continue
119
120 data, _ = stream.read(read_size)
121
122 if should_send() if should_send else True:
123 if not sent_audio and start_send:
124 await start_send()
125 await connection.send(
126 {"type": "input_audio_buffer.append", "audio": base64.b64encode(data).decode("utf-8")}
127 )
128 sent_audio = True
129
130 elif sent_audio:
131 print("Done, triggering inference")
132 await connection.send({"type": "input_audio_buffer.commit"})
133 await connection.send({"type": "response.create", "response": {}})
134 sent_audio = False
135
136 await asyncio.sleep(0)
137
138 except KeyboardInterrupt:
139 pass
140 finally:
141 stream.stop()
142 stream.close()

Callers

nothing calls this directly

Calls 6

startMethod · 0.80
decodeMethod · 0.80
stopMethod · 0.80
readMethod · 0.45
sendMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected