Skip to content

Commit 2d124d1

Browse files
authored
Merge pull request #2470 from makermelissa/main
Add ability to specify audio output device to ChatGPT Bear
2 parents e05bc61 + 3234a04 commit 2d124d1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ChatGPT_Bear/assistant.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import threading
66
import os
7+
import sys
78

89
from datetime import datetime, timedelta
910
from queue import Queue
@@ -29,6 +30,7 @@
2930

3031
# Azure Parameters
3132
AZURE_SPEECH_VOICE = "en-GB-OliverNeural"
33+
DEVICE_ID = None
3234

3335
# Speech Recognition Parameters
3436
ENERGY_THRESHOLD = 1000 # Energy level for mic to detect
@@ -47,6 +49,12 @@
4749
speech_key = os.environ.get("SPEECH_KEY")
4850
service_region = os.environ.get("SPEECH_REGION")
4951

52+
if openai.api_key is None or speech_key is None or service_region is None:
53+
print(
54+
"Please set the OPENAI_API_KEY, SPEECH_KEY, and SPEECH_REGION environment variables first."
55+
)
56+
sys.exit(1)
57+
5058
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
5159
speech_config.speech_synthesis_voice_name = AZURE_SPEECH_VOICE
5260

@@ -157,10 +165,14 @@ def __init__(self, azure_speech_config):
157165
self.do_mouth_movement = False
158166
self._mouth_thread = threading.Thread(target=self.move_mouth, daemon=True)
159167
self._mouth_thread.start()
160-
168+
if DEVICE_ID is None:
169+
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
170+
else:
171+
audio_config = speechsdk.audio.AudioOutputConfig(device_name=DEVICE_ID)
161172
self._speech_synthesizer = speechsdk.SpeechSynthesizer(
162-
speech_config=azure_speech_config
173+
speech_config=azure_speech_config, audio_config=audio_config
163174
)
175+
164176
self._speech_synthesizer.synthesizing.connect(self.start_moving_mouth)
165177
self._speech_synthesizer.synthesis_completed.connect(self.stop_moving_mouth)
166178

0 commit comments

Comments
 (0)