语音控制
本模块提供机器人语音相关的所有功能,包括语音识别、语音合成、语音控制等。
voice 高层接口(推荐)
底层接口(精细控制)
单例与基础
- static Speech.get_instance()
Get the singleton instance of Speech.
- Returns:
Singleton instance.
- Return type:
Speech
Examples:
speech = Speech.get_instance()
AFE (音频前处理)
- Speech.enable_afe_engine(enable, block=True, timeout=5)
Enable or disable Audio Front End (AFE) engine.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableAfeEngineResponse.
Examples:
# Enable AFE enable_afe_engine(True) # Disable AFE with custom timeout enable_afe_engine(False, timeout=10)
- Parameters:
block (
bool) –
- Speech.enable_aec(enable, block=True, timeout=5)
Enable or disable Acoustic Echo Cancellation (AEC).
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableAecResponse.
Examples:
enable_aec(True) enable_aec(False, timeout=8)
- Parameters:
block (
bool) –
- Speech.enable_speech_enhance(enable, block=True, timeout=5)
Enable or disable speech enhancement.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableSpeechEnhanceResponse.
Examples:
enable_speech_enhance(True) enable_speech_enhance(False)
- Parameters:
block (
bool) –
- Speech.set_bypass(bypass, block=True, timeout=5)
Set AFE bypass mode.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
SetBypassResponse.
Examples:
set_bypass(True) set_bypass(False, timeout=10)
- Parameters:
block (
bool) –
ASR (语音识别)
- Speech.enable_asr(enable, block=True, timeout=30)
Enable or disable Automatic Speech Recognition (ASR).
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableAsrResponse.
Examples:
enable_asr(True) enable_asr(False, timeout=8)
- Parameters:
block (
bool) –
- Speech.switch_asr_device(device_name, timeout=5)
Switch ASR input device.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
SwitchAsrDeviceResponse.
Examples:
switch_asr_device("default") switch_asr_device("mic_array", timeout=10)
KWS (关键词唤醒)
- Speech.enable_hexaware(enable, block=True, timeout=5)
Enable or disable Hexaware wake word detection.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableHexawareResponse.
Examples:
enable_hexaware(True) enable_hexaware(False, timeout=8)
- Parameters:
block (
bool) –
TTS (语音合成)
- Speech.enable_tts(enable, block=True, timeout=30)
Enable or disable Text-to-Speech (TTS) engine.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
EnableTtsResponse.
Examples:
enable_tts(True) enable_tts(False, timeout=8)
- Parameters:
block (
bool) –
- Speech.stop_playing_tts(enable, block=True, timeout=5)
Stop playing current TTS audio.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
StopPlayingTtsResponse.
Examples:
stop_playing_tts(True) stop_playing_tts(True, timeout=8)
- Parameters:
block (
bool) –
- Speech.switch_tts_device(device_name, timeout=5)
Switch TTS output device.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
SwitchTtsDeviceResponse.
Examples:
switch_tts_device("default") switch_tts_device("speaker", timeout=10)
- Speech.set_volume(volume, timeout=5)
Set TTS playback volume.
- Parameters:
- Return type:
EnableServiceResponse- Returns:
SetVolumeResponse.
Examples:
set_volume(0.5) set_volume(1.0, timeout=10)
- Speech.generate_audio(text, sid=0, speed=1.0, block=True, timeout=60, complete_cb=None, feedback_cb=None)
Generate and play audio from text.
- Parameters:
text (
str) – Text to convert to speech.sid (
int) – Speaker ID (default: 0).speed (
float) – Speech speed (default: 1.0).block (
bool) – Whether to block until completion (default: True).timeout (
int) – Timeout in seconds (default: 60).complete_cb (
callable) – Callback for completion (default: None).feedback_cb (
callable) – Callback for progress updates (default: None).
- Return type:
GenerateAudioResponse- Returns:
GenerateAudioResponse.
Examples:
# Simple usage generate_audio("Hello, world!") # Customize speaker and speed generate_audio("Hello", sid=1, speed=1.2) # Non-blocking generate_audio(text="Hello", block=False) # With callbacks def on_complete(response): print(f"Done: {response.result.message}") def on_progress(event): print(f"Progress: {event.progress * 100:.1f}%") generate_audio("Hello", complete_cb=on_complete, feedback_cb=on_progress)
- Speech.cancel_generate_audio(timeout=5)
Cancel ongoing audio generation.
- Parameters:
timeout (
int) – Timeout in seconds (default: 5).- Return type:
EnableServiceResponse- Returns:
CancelGenerateAudioResponse.
Examples:
# Cancel current audio generation cancel_generate_audio() # Cancel with custom timeout cancel_generate_audio(timeout=8)
事件回调
- Speech.register_asr_callback(callback=None)
Register callback for ASR (speech recognition) results.
- Parameters:
callback (
callable) – Callback function that receives AsrResultEvent. Pass None to unregister.- Return type:
- Returns:
None.
Examples:
def on_asr(event): print(f"Recognized: {event.text}") register_asr_callback(on_asr) # Unregister register_asr_callback(None)
- Speech.register_hexaware_callback(callback=None)
Register callback for wake word detection.
- Parameters:
callback (
callable) – Callback function that receives HexawareAwakenEvent. Pass None to unregister.- Return type:
- Returns:
None.
Examples:
def on_wake_word(event): print(f"Wake word detected: {event.keyword}") register_hexaware_callback(on_wake_word) register_hexaware_callback(None)
- Speech.register_kws_message_callback(callback=None)
Register callback for keyword spotting messages.
- Parameters:
callback (
callable) – Callback function that receives KwsMessageEvent. Pass None to unregister.- Return type:
- Returns:
None.
Examples:
def on_kws_message(event): print(f"KWS message: {event.message}") register_kws_message_callback(on_kws_message) register_kws_message_callback(None)
- Speech.register_nlu_format_callback(callback=None)
Register callback for NLU format messages.
- Parameters:
callback (
callable) – Callback function that receives NluFormatEvent. Pass None to unregister.- Return type:
- Returns:
None.
Examples:
def on_nlu_format(event): print(f"Input: {event.input_text}, Format: {event.format_text}") register_nlu_format_callback(on_nlu_format) register_nlu_format_callback(None)
- Speech.register_generate_audio_callbacks(complete_cb, feedback_cb)
Register callbacks for audio generation progress and completion.
- Parameters:
complete_cb (
callable) – Callback for completion, receives GenerateAudioResponse.feedback_cb (
callable) – Callback for progress updates, receives GenerateAudioProgressEvent.
- Return type:
- Returns:
None.
Examples:
def on_complete(response): print(f"Audio generation complete: {response.result.message}") def on_progress(event): print(f"Progress: {event.progress * 100:.1f}%") register_generate_audio_callbacks(on_complete, on_progress)