-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from uezo/support-custom-listeners
Support Azure Speech Services
- Loading branch information
Showing
10 changed files
with
420 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# pip install azure-cognitiveservices-speech | ||
from logging import getLogger, NullHandler | ||
import azure.cognitiveservices.speech as speechsdk | ||
from azure.cognitiveservices.speech import PropertyId | ||
from . import RequestListenerBase | ||
|
||
class AzureVoiceRequestListener(RequestListenerBase): | ||
def __init__(self, api_key: str, region: str, timeout: float=0.5, detection_timeout: float=10.0, lang: str="ja-JP", device_name: str=None): | ||
self.logger = getLogger(__name__) | ||
self.logger.addHandler(NullHandler()) | ||
|
||
self.speech_config = speechsdk.SpeechConfig(subscription=api_key, region=region) | ||
self.speech_config.set_property(PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, str(detection_timeout * 1000)) | ||
self.speech_config.set_property(PropertyId.Speech_SegmentationSilenceTimeoutMs, str(timeout * 1000)) | ||
|
||
if device_name: | ||
# NOTE: You can see the way to check the device_name at Microsoft Learn. | ||
# https://learn.microsoft.com/ja-jp/azure/ai-services/speech-service/how-to-select-audio-input-devices | ||
self.audio_config = speechsdk.AudioConfig(device_name=device_name) | ||
else: | ||
self.audio_config = speechsdk.AudioConfig(use_default_microphone=True) | ||
|
||
self.speech_recognizer = speechsdk.SpeechRecognizer(speech_config=self.speech_config, audio_config=self.audio_config, language=lang) | ||
|
||
self.on_start_listening = None | ||
|
||
async def get_request(self): | ||
if self.on_start_listening: | ||
await self.on_start_listening() | ||
|
||
self.logger.info(f"Listening... ({self.__class__.__name__})") | ||
result = self.speech_recognizer.recognize_once() | ||
|
||
if result.text: | ||
self.logger.info(f"AzureVoiceRequestListener: {result.text}") | ||
else: | ||
self.logger.info(f"AzureVoiceRequestListener: No speech recognized.") | ||
|
||
return result.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# pip install azure-cognitiveservices-speech | ||
import asyncio | ||
from logging import getLogger, NullHandler | ||
from threading import Thread | ||
from typing import Callable | ||
import azure.cognitiveservices.speech as speechsdk | ||
|
||
class AzureWakewordListener: | ||
def __init__(self, api_key: str, region: str, wakewords: list, on_wakeword: Callable, lang: str="ja-JP", device_name: str=None, verbose: bool=False): | ||
self.logger = getLogger(__name__) | ||
self.logger.addHandler(NullHandler()) | ||
|
||
self.speech_config = speechsdk.SpeechConfig(subscription=api_key, region=region) | ||
|
||
if device_name: | ||
# NOTE: You can see the way to check the device_name at Microsoft Learn. | ||
# https://learn.microsoft.com/ja-jp/azure/ai-services/speech-service/how-to-select-audio-input-devices | ||
self.audio_config = speechsdk.AudioConfig(device_name=device_name) | ||
else: | ||
self.audio_config = speechsdk.AudioConfig(use_default_microphone=True) | ||
|
||
self.speech_recognizer = speechsdk.SpeechRecognizer(speech_config=self.speech_config, audio_config=self.audio_config, language=lang) | ||
self.speech_recognizer.recognized.connect(lambda evt: self.on_recognized(evt)) | ||
|
||
self.wakewords = wakewords | ||
self.on_wakeword = on_wakeword | ||
self.verbose = verbose | ||
|
||
def on_recognized(self, evt): | ||
recognized_text = evt.result.text.replace("。", "").replace("、", "").replace("!", "").replace("!", "").replace("?", "").replace("?", "").strip() | ||
|
||
if self.verbose: | ||
self.logger.info(f"AzureWakeWordListener: {recognized_text}") | ||
|
||
if recognized_text in self.wakewords: | ||
asyncio.run(self.on_wakeword(recognized_text)) | ||
|
||
async def start_listening(self): | ||
self.logger.info(f"Listening... ({self.__class__.__name__})") | ||
self.speech_recognizer.start_continuous_recognition() | ||
while True: | ||
await asyncio.sleep(0.1) | ||
|
||
def start(self): | ||
th = Thread(target=asyncio.run, args=(self.start_listening(),), daemon=True) | ||
th.start() | ||
return th |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.