语音服务接口
| 功能 | C++ | Python | C# | Java |
|---|---|---|---|---|
| 语音服务开关 | - | - | ✔ | - |
| ASR开关 | ✔ | ✔ | ✔ | ✔ |
| 获取ASR识别结果 | ✔ | ✔ | ✔ | ✔ |
| TTS开关 | ✔ | ✔ | ✔ | ✔ |
| TTS语音播放 | - | ✔ | ✔ | ✔ |
| 停止TTS播放 | ✔ | ✔ | ✔ | ✔ |
| 切换音源 | - | - | ✔ | - |
| 切换对话模式 | - | - | ✔ | - |
| 切换语音识别模式 | - | - | ✔ | - |
注意事项!!!
语音服务开关
函数原型
- C#:
(bool success, string message) EnableVoiceService(bool enable, int id = 301, int timeoutMs = 5000)
功能说明
开启或关闭语音服务。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
enable | bool | 必填 | true: 开启, false: 关闭 |
id | int | 选填 (C#) | 请求ID(可忽略) |
timeoutMs | int | 选填 (C#) | 超时时间 (毫秒) |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | 包含 success(bool) 和 message(string) |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
var (success, message) = _robotMutiMediaClient.EnableVoiceService(true);ASR开关
函数原型
- C#:
(bool success, string message) SpeechStartASR(bool enable) - C++:
CommReply enableAsr(bool enable) - Python:
async def enable_asr_engine(enable: bool, timeout: float = 30.0) -> Optional[Dict] - Java:
ServiceOperationResponse SpeechEnableAsr(boolean enable)
功能说明
开启或关闭自动语音识别(ASR)引擎。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
enable | bool | 必填 | true: 开启, false: 关闭 |
timeout | float | 选填 (Python) | 超时时间(秒) |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | 包含 success(bool) 和 message(string) |
| C++ | CommReply | 包含 success(bool) 和 message(string) |
| Python | Dict | 包含 success(bool) 和 message(string) |
| Java | ServiceOperationResponse | 包含 status 和 message |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
_robotMutiMediaClient.SpeechStartASRAsync(true, (success, message) =>
{
string resultText = $"SpeechStartASR(): success={success}, message={message}";
Debug.Log(resultText);
});python
# Python
from daystar_sdk import WebSocketRobotClient
with WebSocketRobotClient(host='localhost', port=5252) as client:
open_result_asr = await client.enable_asr_engine(True)
print(f"enable_asr_engine => {open_result_asr}")java
// Java
import com.lenovo.daystar_bot_sdk_client.client.SlamNaviClient;
import com.lenovo.daystar_bot_sdk_client.RobotSDKManager;
import com.lenovo.daystar_bot_sdk_client.DataEntities.RobotType;
import com.lenovo.daystar_bot_sdk_client.adapter.IRobotAdapter;
RobotSDKManager sdkManager = RobotSDKManager.getInstance();
IRobotAdapter adapter = sdkManager.createRobotAdapter(RobotType.HEXAPOD);
MultimediaClient task = new MultimediaClient(adapter);
ServiceOperationResponse response = media.SpeechStartASR(true);
if (response.success) {
logger.info("ASR enabled: " + response.message);
} else {
logger.error("Failed to enable ASR: " + response.message);
}cpp
// C++
Lenovo::Daystar::SDK sdk;
if (!sdk.isConnected()) {
std::cerr << "Can not connect SDK server with default settings"
<< std::endl;
return 1;
} else {
std::cout << "Connected with default settings" << std::endl;
}
auto &sysb = sdk.getSYSB();
bool enable = true;
auto result = sysb.enableAsr(enable);
if (result.success)
{
std::cout << "✓ ASR" << (enable ? "已启用" : "已禁用") << std::endl;
}
else
{
std::cout << "✗ 命令发送失败: " << result.message << std::endl;
}获取ASR识别结果
函数原型
- C#:
bool SpeechRegisterASRResult(Action<object> callback) - C++:
bool subscribeSpeechAsrResult() - Python:
async def subscribe_asr_result(callback: Callable[[Dict], None]) -> bool - Java:
void RegisterSpeechAsrResult(Consumer<SpeechAsrResult> callback)
功能说明
注册ASR识别结果的回调函数。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
callback | function | 必填 | 接收ASR结果的回调函数 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | bool | 是否注册成功 |
| C++ | bool | 是否订阅成功 |
| Python | bool | 是否订阅成功 |
| Java | void | 无返回值 |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
void OnASRResultReceived(object data)
{
try
{
string jsonStr = data.ToString();
Debug.Log($"ASR Result received: {jsonStr}");
// 尝试解析JSON数据(假设包含text字段和is_final字段)
// 示例: {"text": "你好", "is_final": true}
}
catch (Exception ex)
{
Debug.LogError($"Error processing ASR result: {ex.Message}");
}
}
bool success = _robotMutiMediaClient.SpeechRegisterASRResult(OnASRResultReceived);python
# Python
from daystar_sdk import WebSocketRobotClient
with WebSocketRobotClient(host='localhost', port=5252) as client:
result = await client.subscribe_asr_result(lambda data: print(f"ASR: {data['text']}, 最终结果: {data['is_final']}"))
print(f"订阅ASR结果:{result}")java
// Java
import com.lenovo.daystar_bot_sdk_client.client.SlamNaviClient;
import com.lenovo.daystar_bot_sdk_client.RobotSDKManager;
import com.lenovo.daystar_bot_sdk_client.DataEntities.RobotType;
import com.lenovo.daystar_bot_sdk_client.adapter.IRobotAdapter;
RobotSDKManager sdkManager = RobotSDKManager.getInstance();
IRobotAdapter adapter = sdkManager.createRobotAdapter(RobotType.HEXAPOD);
MultimediaClient task = new MultimediaClient(adapter);
media.SpeechRegisterASRResult(result -> {
logger.info("ASR Result: " + result.text + ", is_final: " + result.is_final);
if (result.is_final) {
// 处理最终识别结果
processCommand(result.text);
}
});cpp
// C++
Lenovo::Daystar::SDK sdk;
if (!sdk.isConnected()) {
std::cerr << "Can not connect SDK server with default settings"
<< std::endl;
return 1;
} else {
std::cout << "Connected with default settings" << std::endl;
}
auto &sysb = sdk.getSYSB();
auto result = sysb.subscribeSpeechAsrResult();
if (result)
{
std::cout << "✓ 已订阅ASR结果" << std::endl;
}
else
{
std::cout << "✗ 订阅ASR结果失败" << std::endl;
}TTS开关
函数原型
- C#:
(bool success, string message) SpeechInitTTS(bool enable) - C++:
CommReply enableTts(bool enable) - Python:
async def enable_tts_engine(enable: bool, timeout: float = 30.0) -> Optional[Dict] - Java:
ServiceOperationResponse SpeechEnableTts(boolean enable)
功能说明
开启或关闭语音合成(TTS)引擎。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
enable | bool | 必填 | true: 开启, false: 关闭 |
timeout | float | 选填 (Python) | 超时时间(秒) |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | 包含 success(bool) 和 message(string) |
| C++ | CommReply | 包含 success(bool) 和 message(string) |
| Python | Dict | 包含 success(bool) 和 message(string) |
| Java | ServiceOperationResponse | 包含 status 和 message |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
_robotMutiMediaClient.SpeechInitTTSAsync(enable, (success, message) =>
{
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
string resultText = $"SpeechInitTTS({enable}): success={success}, message={message}";
Debug.Log(resultText);
});
});python
# Python
from daystar_sdk import WebSocketRobotClient
with WebSocketRobotClient(host='localhost', port=5252) as client:
open_result_tts = await client.enable_tts_engine(True)
print(f"enable_tts_engine => {open_result_tts}")java
// Java
import com.lenovo.daystar_bot_sdk_client.client.SlamNaviClient;
import com.lenovo.daystar_bot_sdk_client.RobotSDKManager;
import com.lenovo.daystar_bot_sdk_client.DataEntities.RobotType;
import com.lenovo.daystar_bot_sdk_client.adapter.IRobotAdapter;
RobotSDKManager sdkManager = RobotSDKManager.getInstance();
IRobotAdapter adapter = sdkManager.createRobotAdapter(RobotType.HEXAPOD);
MultimediaClient task = new MultimediaClient(adapter);
ServiceOperationResponse response = media.SpeechInitTts(true);
if (response.success) {
logger.info("TTS enabled: " + response.message);
} else {
logger.error("Failed to enable TTS: " + response.message);
}cpp
// C++
Lenovo::Daystar::SDK sdk;
if (!sdk.isConnected()) {
std::cerr << "Can not connect SDK server with default settings"
<< std::endl;
return 1;
} else {
std::cout << "Connected with default settings" << std::endl;
}
auto &sysb = sdk.getSYSB();
bool enable = true;
auto result = sysb.enableTts(enable);
if (result.success)
{
std::cout << "✓ TTS" << (enable ? "已启用" : "已禁用") << std::endl;
}
else
{
std::cout << "✗ 命令发送失败: " << result.message << std::endl;
}TTS语音播放
函数原型
- C#:
(bool success, string message) SpeechPlayTTS(string text, int sid = 0, float speed = 1.0f) - Python:
async def generate_audio(text: str, sid: int = 0, speed: float = 1.0, timeout: float = 30.0) -> Optional[Dict] - Java:
ServiceOperationResponse SpeechGenerateAudio(String text, int sid, float speed, int timeout)
功能说明
将文本转换为语音并播放。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
text | string | 必填 | 需要转换的文本内容 |
sid | int | 0 | 说话人ID |
speed | float | 1.0 | 语速 (建议 0.7 ~ 1.3) |
timeout | int/float | 30 | 超时时间 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | 包含 success(bool) 和 message(string) |
| Python | Dict | 包含 success(bool) 和 message(string) |
| Java | ServiceOperationResponse | 包含 status 和 message |
调用示例
csharp
// C#
robot.SpeechPlayTTS("你好,我是晨星机器人");python
# Python
from daystar_sdk import WebSocketRobotClient
with WebSocketRobotClient(host='localhost', port=5252) as client:
generate_audio_result = await client.generate_audio("你好,我是晨星机器人")
print(f"generate_audio => {generate_audio_result}")java
// Java
import com.lenovo.daystar_bot_sdk_client.client.SlamNaviClient;
import com.lenovo.daystar_bot_sdk_client.RobotSDKManager;
import com.lenovo.daystar_bot_sdk_client.DataEntities.RobotType;
import com.lenovo.daystar_bot_sdk_client.adapter.IRobotAdapter;
RobotSDKManager sdkManager = RobotSDKManager.getInstance();
IRobotAdapter adapter = sdkManager.createRobotAdapter(RobotType.HEXAPOD);
MultimediaClient task = new MultimediaClient(adapter);
ServiceOperationResponse response = media.SpeechPlayTTS(
"你好,我是晨星机器人",
1, // 说话人ID
1.0f, // 正常语速
30 // 30秒超时
);
if (response.success) {
logger.info("Audio generation started");
}停止TTS播放
函数原型
- C#:
(bool success, string message) SpeechStopTTS() - C++:
CommReply stopPlayingTts(bool enable) - Python:
async def stop_playing_tts(timeout: float = 30.0) -> Optional[Dict] - Java:
ServiceOperationResponse SpeechStopPlayingTts()
功能说明
停止当前正在播放的TTS语音。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
enable | bool | 必填 (C++) | 标识位 (通常传true) |
timeout | float | 选填 (Python) | 超时时间 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | 包含 success(bool) 和 message(string) |
| C++ | CommReply | 包含 success(bool) 和 message(string) |
| Python | Dict | 包含 success(bool) 和 message(string) |
| Java | ServiceOperationResponse | 包含 status 和 message |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
var (success, message) = _robotMutiMediaClient.SpeechStopTTS();python
# Python
from daystar_sdk import WebSocketRobotClient
with WebSocketRobotClient(host='localhost', port=5252) as client:
stop_result = await client.stop_playing_tts()
print(f"stop_playing_tts => {stop_result}")java
// Java
import com.lenovo.daystar_bot_sdk_client.client.SlamNaviClient;
import com.lenovo.daystar_bot_sdk_client.RobotSDKManager;
import com.lenovo.daystar_bot_sdk_client.DataEntities.RobotType;
import com.lenovo.daystar_bot_sdk_client.adapter.IRobotAdapter;
RobotSDKManager sdkManager = RobotSDKManager.getInstance();
IRobotAdapter adapter = sdkManager.createRobotAdapter(RobotType.HEXAPOD);
MultimediaClient task = new MultimediaClient(adapter);
ServiceOperationResponse response = media.SpeechStopTTS();
if (response.success) {
logger.info("TTS stopped");
}cpp
// C++
Lenovo::Daystar::SDK sdk;
if (!sdk.isConnected()) {
std::cerr << "Can not connect SDK server with default settings"
<< std::endl;
return 1;
} else {
std::cout << "Connected with default settings" << std::endl;
}
auto &sysb = sdk.getSYSB();
bool enable = true;
auto result = sysb.stopPlayingTts(enable);
if (result.success)
{
std::cout << "✓ TTS播放" << (enable ? "已停止" : "已继续") << std::endl;
}
else
{
std::cout << "✗ 命令发送失败: " << result.message << std::endl;
}切换音源
函数原型
- C#:
(bool success, string message) SwitchAudioSource(int source, int id = 302, int timeoutMs = 5000)
功能说明
切换语音输入源。
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
source | int | 必填 | 0: 默认音源(机器狗), 1: 外部音源 |
timeoutMs | int | 选填 | 超时时间 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | success, message |
调用示例
csharp
// C#切换对话模式
功能说明
设置语音对话模式(单轮或多轮)。
函数原型
- C#:
(bool success, string message) SetDialogMode(int mode, int id = 303, int timeoutMs = 5000)
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
mode | int | 必填 | 0: 单轮对话, 1: 多轮对话 |
timeoutMs | int | 选填 | 超时时间 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | success, message |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
var (success, message) = _robotMutiMediaClient.SetDialogMode(0);切换语音识别模式
功能说明
切换语音识别模式(在线或离线)。
函数原型
- C#:
(bool success, string message) SetVoiceRecognitionMode(int mode, int id = 304, int timeoutMs = 5000)
参数说明
| 参数名 | 类型 | 必填/默认值 | 说明 |
|---|---|---|---|
mode | int | 必填 | 0: 在线模式, 1: 离线模式 |
timeoutMs | int | 选填 | 超时时间 |
返回值
| 语言 | 返回类型 | 说明 |
|---|---|---|
| C# | (bool, string) | success, message |
调用示例
csharp
// C#
private RobotSDKManager _robotSDKManager;
private RobotMutiMediaClient _robotMutiMediaClient;
_robotSDKManager = RobotSDKManager.Instance;
_robotMutiMediaClient = _robotSDKManager.CreateRobotMutiMediaClient();
var (success, message) = _robotMutiMediaClient.SetVoiceRecognitionMode(0);