mirror of
https://github.com/idootop/mi-gpt.git
synced 2025-04-07 19:21:30 +00:00
fix: 修复唤醒模式下小爱回答不完整的问题 fix #9
This commit is contained in:
parent
1ad10a95d2
commit
50353e587a
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user