fix: 修复唤醒模式下小爱回答不完整的问题 fix #9

This commit is contained in:
WJG 2024-05-26 21:28:38 +08:00
parent 1ad10a95d2
commit 50353e587a
No known key found for this signature in database
GPG Key ID: 258474EF8590014A
2 changed files with 20 additions and 16 deletions

View File

@ -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) {

View File

@ -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);
}
}