From 8f50c0c6e4c99b6852d111f7efb1f8bb73ff7acb Mon Sep 17 00:00:00 2001 From: WJG Date: Tue, 21 May 2024 22:46:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20TTS=20=E5=92=8C=E8=AE=BE=E5=A4=87=E5=94=A4=E9=86=92?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E9=80=89=E9=A1=B9=EF=BC=8Cfix=20#5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .migpt.example.js | 9 +++++++ README.md | 50 +++++++++++++++++++----------------- src/services/speaker/base.ts | 42 ++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 29 deletions(-) diff --git a/.migpt.example.js b/.migpt.example.js index 8b114ae..6841a2a 100644 --- a/.migpt.example.js +++ b/.migpt.example.js @@ -17,6 +17,15 @@ export default { userId: process.env.MI_USER, password: process.env.MI_PASS, did: process.env.MI_DID, + /** + * 小米音箱 TTS 指令 + * + * 比如:小爱音箱 Pro(lx06) -> [5, 1] + * + * 不同设备的具体指令可在此网站查询:https://home.miot-spec.com + */ + ttsCommand: [5, 1], + wakeUpCommand: [5, 3], }, bot: { name: botName, diff --git a/README.md b/README.md index 80b9037..8851e68 100644 --- a/README.md +++ b/README.md @@ -96,30 +96,32 @@ main(); 然后,将里面的配置参数修改成你自己的,参数含义如下: -| 参数名称 | 描述 | 示例 | -| -------------------- | ------------------------------------------------------------ | -------------------------------------------------- | -| **bot** | | | -| `name` | 对方名称(小爱音箱) | `"傻妞"` | -| `profile` | 对方的个人简介/人设 | `"性别女,性格乖巧可爱,喜欢搞怪,爱吃醋。"` | -| **master** | | | -| `name` | 主人名称(我自己) | `"陆小千"` | -| `profile` | 主人的个人简介/人设 | `"性别男,善良正直,总是舍己为人,是傻妞的主人。"` | -| **room** | | | -| `name` | 会话群名称 | `"魔幻手机"` | -| `description` | 会话群简介 | `"傻妞和陆小千的私聊"` | -| **speaker** | | | -| `userId` | 小米账户 | `"12345678901"` | -| `password` | 账户密码 | `"123456"` | -| `did` | 小爱音箱 ID 或名称 | `"小爱音箱 Pro"` | -| **其他(可选)** | | | -| `callAIPrefix` | 当消息以召唤关键词开头时,会调用 AI 来响应用户消息 | `["请","傻妞"]` | -| `wakeUpKeywords` | 当消息中包含唤醒关键词时,会进入 AI 唤醒状态 | `["召唤傻妞","打开傻妞"]` | -| `exitKeywords` | 当消息中包含退出关键词时,会退出 AI 唤醒状态 | `["退出傻妞","关闭傻妞"]` | -| `onEnterAI` | 进入 AI 模式的欢迎语 | `["你好,我是傻妞,请问有什么能够帮你的吗?"]` | -| `onExitAI` | 退出 AI 模式的提示语 | `["傻妞已退出"]` | -| `onAIAsking` | AI 开始回答时的提示语 | `["请稍等,让我想想"]` | -| `onAIError` | AI 回答异常时的提示语 | `["出错了,请稍后再试吧!"]` | -| `exitKeepAliveAfter` | 无响应一段时间后,多久自动退出唤醒模式(单位秒,默认 30 秒) | `30` | +| 参数名称 | 描述 | 示例 | +| -------------------- | ------------------------------------------------------------- | -------------------------------------------------- | +| **bot** | | | +| `name` | 对方名称(小爱音箱) | `"傻妞"` | +| `profile` | 对方的个人简介/人设 | `"性别女,性格乖巧可爱,喜欢搞怪,爱吃醋。"` | +| **master** | | | +| `name` | 主人名称(我自己) | `"陆小千"` | +| `profile` | 主人的个人简介/人设 | `"性别男,善良正直,总是舍己为人,是傻妞的主人。"` | +| **room** | | | +| `name` | 会话群名称 | `"魔幻手机"` | +| `description` | 会话群简介 | `"傻妞和陆小千的私聊"` | +| **speaker** | | | +| `userId` | 小米账户 | `"12345678901"` | +| `password` | 账户密码 | `"123456"` | +| `did` | 小爱音箱 ID 或名称 | `"小爱音箱 Pro"` | +| `ttsCommand` | 小米音箱 TTS 指令([可在此查询](https://home.miot-spec.com)) | `[5, 1]` | +| `wakeUpCommand` | 小米音箱唤醒指令([可在此查询](https://home.miot-spec.com)) | `[5, 3]` | +| **其他(可选)** | | | +| `callAIPrefix` | 当消息以召唤关键词开头时,会调用 AI 来响应用户消息 | `["请","傻妞"]` | +| `wakeUpKeywords` | 当消息中包含唤醒关键词时,会进入 AI 唤醒状态 | `["召唤傻妞","打开傻妞"]` | +| `exitKeywords` | 当消息中包含退出关键词时,会退出 AI 唤醒状态 | `["退出傻妞","关闭傻妞"]` | +| `onEnterAI` | 进入 AI 模式的欢迎语 | `["你好,我是傻妞,请问有什么能够帮你的吗?"]` | +| `onExitAI` | 退出 AI 模式的提示语 | `["傻妞已退出"]` | +| `onAIAsking` | AI 开始回答时的提示语 | `["请稍等,让我想想"]` | +| `onAIError` | AI 回答异常时的提示语 | `["出错了,请稍后再试吧!"]` | +| `exitKeepAliveAfter` | 无响应一段时间后,多久自动退出唤醒模式(单位秒,默认 30 秒) | `30` | ## 💬 常见问题 diff --git a/src/services/speaker/base.ts b/src/services/speaker/base.ts index 4f0db6a..b25887f 100644 --- a/src/services/speaker/base.ts +++ b/src/services/speaker/base.ts @@ -19,10 +19,32 @@ type Speaker = { speaker: string; }; +type ActionCommand = [number, number]; + export type BaseSpeakerConfig = MiServiceConfig & { - // 语音合成服务商 + /** + * 语音合成服务商 + */ tts?: TTSProvider; - // 检测间隔(单位毫秒,默认 100 毫秒) + /** + * 小米音箱 TTS command + * + * 比如:小爱音箱 Pro(lx06) -> [5, 1] + * + * 具体指令可在此网站查询:https://home.miot-spec.com + */ + ttsCommand?: ActionCommand; + /** + * 小米音箱唤醒 command + * + * 比如:小爱音箱 Pro(lx06) -> [5, 3] + * + * 具体指令可在此网站查询:https://home.miot-spec.com + */ + wakeUpCommand?: ActionCommand; + /** + * 检测间隔(单位毫秒,默认 100 毫秒) + */ interval?: number; /** * TTS 开始/结束提示音 @@ -37,17 +59,23 @@ export class BaseSpeaker { interval: number; tts: TTSProvider; + ttsCommand: ActionCommand; + wakeUpCommand: ActionCommand; config: MiServiceConfig; constructor(config: BaseSpeakerConfig) { this.config = config; const { interval = 100, tts = "xiaoai", + ttsCommand = [5, 1], + wakeUpCommand = [5, 3], audio_beep = process.env.AUDIO_BEEP, } = config; this.audio_beep = audio_beep; this.interval = interval; this.tts = tts; + this.ttsCommand = ttsCommand; + this.wakeUpCommand = wakeUpCommand; } async initMiServices() { @@ -57,13 +85,13 @@ export class BaseSpeaker { } wakeUp() { - return this.MiIOT!.doAction(5, 3); + return this.MiIOT!.doAction(...this.wakeUpCommand); } async unWakeUp() { // 通过 TTS 不发音文本,使小爱退出唤醒状态 await this.MiNA!.pause(); - await this.MiIOT!.doAction(5, 1, kAreYouOK); + await this.MiIOT!.doAction(...this.ttsCommand, kAreYouOK); } audio_beep?: string; @@ -186,7 +214,11 @@ export class BaseSpeaker { if (ttsNotXiaoai) { await this.unWakeUp(); } - await this.MiNA!.play(args); + if (args?.tts) { + await this.MiIOT!.doAction(...this.ttsCommand, args.tts); + } else { + await this.MiNA!.play(args); + } this.logger.log("🔊 " + (ttsText ?? audio)); // 等待回答播放完毕 while (true) {