Skip to content

Commit a0c5abc

Browse files
authored
fix(examples): make sure audio playback finishes (#340)
Previously the stream was closing as soon as all the audio was added but didn't wait for it to be finished. Additionally the audio might seem chopped off if there is no additional silence so this PR also adds one second of silence before exiting the program.
2 parents ad020b7 + 2104f54 commit a0c5abc

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Diff for: examples/voice/static/main.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import random
33

4+
import numpy as np
5+
46
from agents import Agent, function_tool
57
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
68
from agents.voice import (
@@ -78,6 +80,9 @@ async def main():
7880
elif event.type == "voice_stream_event_lifecycle":
7981
print(f"Received lifecycle event: {event.event}")
8082

83+
# Add 1 second of silence to the end of the stream to avoid cutting off the last audio.
84+
player.add_audio(np.zeros(24000 * 1, dtype=np.int16))
85+
8186

8287
if __name__ == "__main__":
8388
asyncio.run(main())

Diff for: examples/voice/static/util.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __enter__(self):
6262
return self
6363

6464
def __exit__(self, exc_type, exc_value, traceback):
65+
self.stream.stop() # wait for the stream to finish
6566
self.stream.close()
6667

6768
def add_audio(self, audio_data: npt.NDArray[np.int16]):

0 commit comments

Comments
 (0)