Skip to content

Commit

Permalink
fix: 修复唤醒模式下小爱回答不完整的问题 fix #9
Browse files Browse the repository at this point in the history
  • Loading branch information
idootop committed May 26, 2024
1 parent 1ad10a9 commit 50353e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/services/speaker/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -43,9 +43,9 @@ export type BaseSpeakerConfig = MiServiceConfig & {
*/
wakeUpCommand?: ActionCommand;
/**
* 检测间隔(单位毫秒,默认 500 毫秒)
* 播放状态检测间隔(单位毫秒,最低 500 毫秒,默认 1 秒
*/
interval?: number;
checkInterval?: number;
/**
* TTS 开始/结束提示音
*/
Expand All @@ -57,22 +57,22 @@ export class BaseSpeaker {
MiNA?: MiNA;
MiIOT?: MiIOT;

interval: number;
checkInterval: number;
tts: TTSProvider;
ttsCommand: ActionCommand;
wakeUpCommand: ActionCommand;
config: MiServiceConfig;
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;
Expand Down Expand Up @@ -172,7 +172,7 @@ export class BaseSpeaker {
// 播放完毕
break;
}
await sleep(this.interval);
await sleep(this.checkInterval);
}
} else {
res = await this._response(options);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions src/services/speaker/speaker.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface SpeakerCommand {

export type SpeakerConfig = BaseSpeakerConfig & {
/**
* 拉取消息心跳间隔(单位毫秒,默认1秒
* 拉取消息心跳间隔(单位毫秒,最低 500 毫秒,默认 1 秒
*/
heartbeat?: number;
/**
Expand Down Expand Up @@ -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;
}

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

Expand Down

0 comments on commit 50353e5

Please sign in to comment.