From 50353e587a1bfecfd27cc42d57dbda9614dba88b Mon Sep 17 00:00:00 2001 From: WJG Date: Sun, 26 May 2024 21:28:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=94=A4=E9=86=92?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=B0=8F=E7=88=B1=E5=9B=9E=E7=AD=94?= =?UTF-8?q?=E4=B8=8D=E5=AE=8C=E6=95=B4=E7=9A=84=E9=97=AE=E9=A2=98=20fix=20?= =?UTF-8?q?#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/speaker/base.ts | 20 +++++++++++--------- src/services/speaker/speaker.ts | 16 +++++++++------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/services/speaker/base.ts b/src/services/speaker/base.ts index 00459ec..3758402 100644 --- a/src/services/speaker/base.ts +++ b/src/services/speaker/base.ts @@ -5,7 +5,7 @@ import { getMiIOT, getMiNA, } from "mi-service-lite"; -import { sleep } from "../../utils/base"; +import { clamp, sleep } from "../../utils/base"; import { Logger } from "../../utils/log"; import { Http } from "../http"; import { StreamResponse } from "./stream"; @@ -43,9 +43,9 @@ export type BaseSpeakerConfig = MiServiceConfig & { */ wakeUpCommand?: ActionCommand; /** - * 检测间隔(单位毫秒,默认 500 毫秒) + * 播放状态检测间隔(单位毫秒,最低 500 毫秒,默认 1 秒) */ - interval?: number; + checkInterval?: number; /** * TTS 开始/结束提示音 */ @@ -57,7 +57,7 @@ export class BaseSpeaker { MiNA?: MiNA; MiIOT?: MiIOT; - interval: number; + checkInterval: number; tts: TTSProvider; ttsCommand: ActionCommand; wakeUpCommand: ActionCommand; @@ -65,14 +65,14 @@ export class BaseSpeaker { constructor(config: BaseSpeakerConfig) { this.config = config; const { - interval = 500, + checkInterval = 1000, tts = "xiaoai", ttsCommand = [5, 1], wakeUpCommand = [5, 3], audioBeep = process.env.audioBeep, } = config; this.audioBeep = audioBeep; - this.interval = interval; + this.checkInterval = clamp(checkInterval, 500, Infinity); this.tts = tts; this.ttsCommand = ttsCommand; this.wakeUpCommand = wakeUpCommand; @@ -172,7 +172,7 @@ export class BaseSpeaker { // 播放完毕 break; } - await sleep(this.interval); + await sleep(this.checkInterval); } } else { res = await this._response(options); @@ -220,6 +220,8 @@ export class BaseSpeaker { await this.MiNA!.play(args); } this.logger.log("🔊 " + (ttsText ?? audio)); + // 等待 3 秒,确保本地设备状态已更新 + await sleep(3000); // 等待回答播放完毕 while (true) { const res = await this.MiNA!.getStatus(); @@ -230,10 +232,10 @@ export class BaseSpeaker { // 响应被中断 return "break"; } - if (res?.status && res.status !== "playing") { + if (res && res?.status !== "playing") { break; } - await sleep(this.interval); + await sleep(this.checkInterval); } // 播放结束提示音 if (playSFX) { diff --git a/src/services/speaker/speaker.ts b/src/services/speaker/speaker.ts index 453bcb0..fcb95ba 100644 --- a/src/services/speaker/speaker.ts +++ b/src/services/speaker/speaker.ts @@ -1,4 +1,4 @@ -import { firstOf, lastOf, sleep } from "../../utils/base"; +import { clamp, firstOf, lastOf, sleep } from "../../utils/base"; import { kAreYouOK } from "../../utils/string"; import { BaseSpeaker, BaseSpeakerConfig } from "./base"; import { StreamResponse } from "./stream"; @@ -28,7 +28,7 @@ export interface SpeakerCommand { export type SpeakerConfig = BaseSpeakerConfig & { /** - * 拉取消息心跳间隔(单位毫秒,默认1秒) + * 拉取消息心跳间隔(单位毫秒,最低 500 毫秒,默认 1 秒) */ heartbeat?: number; /** @@ -59,7 +59,7 @@ export class Speaker extends BaseSpeaker { } = config; this.audioSilent = audioSilent; this._commands = config.commands ?? []; - this.heartbeat = heartbeat; + this.heartbeat = clamp(heartbeat, 500, Infinity); this.exitKeepAliveAfter = exitKeepAliveAfter; } @@ -95,12 +95,14 @@ export class Speaker extends BaseSpeaker { // 唤醒中 if (!this.responding) { // 没有回复时,一直播放静音音频使小爱闭嘴 - await this.MiNA?.play( - this.audioSilent ? { url: this.audioSilent } : { tts: kAreYouOK } - ); + if (this.audioSilent) { + await this.MiNA?.play({ url: this.audioSilent }); + } else { + await this.MiIOT!.doAction(...this.ttsCommand, kAreYouOK); + } } } - await sleep(this.interval); + await sleep(this.checkInterval); } }