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,
|
getMiIOT,
|
||||||
getMiNA,
|
getMiNA,
|
||||||
} from "mi-service-lite";
|
} from "mi-service-lite";
|
||||||
import { sleep } from "../../utils/base";
|
import { clamp, sleep } from "../../utils/base";
|
||||||
import { Logger } from "../../utils/log";
|
import { Logger } from "../../utils/log";
|
||||||
import { Http } from "../http";
|
import { Http } from "../http";
|
||||||
import { StreamResponse } from "./stream";
|
import { StreamResponse } from "./stream";
|
||||||
|
@ -43,9 +43,9 @@ export type BaseSpeakerConfig = MiServiceConfig & {
|
||||||
*/
|
*/
|
||||||
wakeUpCommand?: ActionCommand;
|
wakeUpCommand?: ActionCommand;
|
||||||
/**
|
/**
|
||||||
* 检测间隔(单位毫秒,默认 500 毫秒)
|
* 播放状态检测间隔(单位毫秒,最低 500 毫秒,默认 1 秒)
|
||||||
*/
|
*/
|
||||||
interval?: number;
|
checkInterval?: number;
|
||||||
/**
|
/**
|
||||||
* TTS 开始/结束提示音
|
* TTS 开始/结束提示音
|
||||||
*/
|
*/
|
||||||
|
@ -57,7 +57,7 @@ export class BaseSpeaker {
|
||||||
MiNA?: MiNA;
|
MiNA?: MiNA;
|
||||||
MiIOT?: MiIOT;
|
MiIOT?: MiIOT;
|
||||||
|
|
||||||
interval: number;
|
checkInterval: number;
|
||||||
tts: TTSProvider;
|
tts: TTSProvider;
|
||||||
ttsCommand: ActionCommand;
|
ttsCommand: ActionCommand;
|
||||||
wakeUpCommand: ActionCommand;
|
wakeUpCommand: ActionCommand;
|
||||||
|
@ -65,14 +65,14 @@ export class BaseSpeaker {
|
||||||
constructor(config: BaseSpeakerConfig) {
|
constructor(config: BaseSpeakerConfig) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
const {
|
const {
|
||||||
interval = 500,
|
checkInterval = 1000,
|
||||||
tts = "xiaoai",
|
tts = "xiaoai",
|
||||||
ttsCommand = [5, 1],
|
ttsCommand = [5, 1],
|
||||||
wakeUpCommand = [5, 3],
|
wakeUpCommand = [5, 3],
|
||||||
audioBeep = process.env.audioBeep,
|
audioBeep = process.env.audioBeep,
|
||||||
} = config;
|
} = config;
|
||||||
this.audioBeep = audioBeep;
|
this.audioBeep = audioBeep;
|
||||||
this.interval = interval;
|
this.checkInterval = clamp(checkInterval, 500, Infinity);
|
||||||
this.tts = tts;
|
this.tts = tts;
|
||||||
this.ttsCommand = ttsCommand;
|
this.ttsCommand = ttsCommand;
|
||||||
this.wakeUpCommand = wakeUpCommand;
|
this.wakeUpCommand = wakeUpCommand;
|
||||||
|
@ -172,7 +172,7 @@ export class BaseSpeaker {
|
||||||
// 播放完毕
|
// 播放完毕
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await sleep(this.interval);
|
await sleep(this.checkInterval);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res = await this._response(options);
|
res = await this._response(options);
|
||||||
|
@ -220,6 +220,8 @@ export class BaseSpeaker {
|
||||||
await this.MiNA!.play(args);
|
await this.MiNA!.play(args);
|
||||||
}
|
}
|
||||||
this.logger.log("🔊 " + (ttsText ?? audio));
|
this.logger.log("🔊 " + (ttsText ?? audio));
|
||||||
|
// 等待 3 秒,确保本地设备状态已更新
|
||||||
|
await sleep(3000);
|
||||||
// 等待回答播放完毕
|
// 等待回答播放完毕
|
||||||
while (true) {
|
while (true) {
|
||||||
const res = await this.MiNA!.getStatus();
|
const res = await this.MiNA!.getStatus();
|
||||||
|
@ -230,10 +232,10 @@ export class BaseSpeaker {
|
||||||
// 响应被中断
|
// 响应被中断
|
||||||
return "break";
|
return "break";
|
||||||
}
|
}
|
||||||
if (res?.status && res.status !== "playing") {
|
if (res && res?.status !== "playing") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await sleep(this.interval);
|
await sleep(this.checkInterval);
|
||||||
}
|
}
|
||||||
// 播放结束提示音
|
// 播放结束提示音
|
||||||
if (playSFX) {
|
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 { kAreYouOK } from "../../utils/string";
|
||||||
import { BaseSpeaker, BaseSpeakerConfig } from "./base";
|
import { BaseSpeaker, BaseSpeakerConfig } from "./base";
|
||||||
import { StreamResponse } from "./stream";
|
import { StreamResponse } from "./stream";
|
||||||
|
@ -28,7 +28,7 @@ export interface SpeakerCommand {
|
||||||
|
|
||||||
export type SpeakerConfig = BaseSpeakerConfig & {
|
export type SpeakerConfig = BaseSpeakerConfig & {
|
||||||
/**
|
/**
|
||||||
* 拉取消息心跳间隔(单位毫秒,默认1秒)
|
* 拉取消息心跳间隔(单位毫秒,最低 500 毫秒,默认 1 秒)
|
||||||
*/
|
*/
|
||||||
heartbeat?: number;
|
heartbeat?: number;
|
||||||
/**
|
/**
|
||||||
|
@ -59,7 +59,7 @@ export class Speaker extends BaseSpeaker {
|
||||||
} = config;
|
} = config;
|
||||||
this.audioSilent = audioSilent;
|
this.audioSilent = audioSilent;
|
||||||
this._commands = config.commands ?? [];
|
this._commands = config.commands ?? [];
|
||||||
this.heartbeat = heartbeat;
|
this.heartbeat = clamp(heartbeat, 500, Infinity);
|
||||||
this.exitKeepAliveAfter = exitKeepAliveAfter;
|
this.exitKeepAliveAfter = exitKeepAliveAfter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,12 +95,14 @@ export class Speaker extends BaseSpeaker {
|
||||||
// 唤醒中
|
// 唤醒中
|
||||||
if (!this.responding) {
|
if (!this.responding) {
|
||||||
// 没有回复时,一直播放静音音频使小爱闭嘴
|
// 没有回复时,一直播放静音音频使小爱闭嘴
|
||||||
await this.MiNA?.play(
|
if (this.audioSilent) {
|
||||||
this.audioSilent ? { url: this.audioSilent } : { tts: kAreYouOK }
|
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