你的提交说明

This commit is contained in:
iiml33 2025-12-11 21:02:27 +08:00
parent c8c6462f61
commit 8fd4a4d648
4 changed files with 207 additions and 0 deletions

37
af Normal file
View file

@ -0,0 +1,37 @@
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=d:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=main
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
safe.directory=D:/Espressif/frameworks/esp-idf-v5.4.1
safe.directory=D:/Espressif/frameworks/esp-idf-v5.4.1/components/openthread/openthread
safe.directory=D:/Espressif/frameworks/esp-idf-v5.4.1
safe.directory=D:/Espressif/frameworks/esp-idf-v5.4.1/components/openthread/openthread
user.name=iiml33
user.email=zhendpan@gmail.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
remote.origin.url=https://github.com/iiml33/xiaozhi-esp32-server
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
branch.main.vscode-merge-base=origin/main
remote.upstream.url=https://github.com/xinnan-tech/xiaozhi-esp32-server.git
remote.upstream.fetch=+refs/heads/*:refs/remotes/upstream/*

39
docs/animal-sound-mode.md Normal file
View file

@ -0,0 +1,39 @@
# 动物互动音频模式
让大语言模型给出情绪标签(如“开心”“悲伤”“生气”“害怕”“无奈”“撒娇”),服务器根据情绪播放对应动物叫声,将结果推送给小智客户端。
## 启用步骤
1. 准备音频文件(建议 16k/mono 的 wav 或 mp3),放到 `config/assets/animal_sounds/`,文件名与情绪映射参考下方配置。
2. 在 `data/.config.yaml`(或智控台 TTS 配置)里选择新的 TTS 模块 `AnimalSound`,并配置情绪到音频文件的映射:
```yaml
selected_module:
TTS: AnimalSound
TTS:
AnimalSound:
type: animal_sound
base_path: config/assets/animal_sounds
emotion_files:
happy: cat_happy.wav
sad: cat_sad.wav
angry: cat_angry.wav
afraid: cat_afraid.wav
helpless: cat_helpless.wav
coquetry: cat_coquetry.wav
default: cat_neutral.wav
# 如果没有 default,可用绝对路径兜底
# default_file: /abs/path/to/animal_default.wav
```
3. 确保对话输出包含情绪关键词,上述关键词会被自动匹配(可通过 `emotion_keywords` 自定义)。
4. 重启服务后生效。
## 行为说明
- 不会做文本合成,直接播放对应音频;找不到音频时仅记录警告。
- 关键词默认映射(可通过配置覆盖):
- happy: 开心/高兴/快乐/喜悦
- sad: 悲伤/难过/伤心/沮丧
- angry: 生气/愤怒/气愤
- afraid: 害怕/恐惧/紧张
- helpless: 无奈/唉/叹气
- coquetry: 撒娇/卖萌/黏人

View file

@ -889,6 +889,21 @@ TTS:
voice: "zh_female_wanwanxiaohe_moon_bigtts" voice: "zh_female_wanwanxiaohe_moon_bigtts"
output_dir: tmp/ output_dir: tmp/
access_token: "你的机智云API key" access_token: "你的机智云API key"
AnimalSound:
# 动物互动模式:根据LLM输出的情绪标签播放预置动物叫声
# 需要提前在 base_path 下放入对应的音频文件(建议 16k/mono wav 或 mp3)
type: animal_sound
base_path: config/assets/animal_sounds
emotion_files:
happy: cat_happy.wav # 开心
sad: cat_sad.wav # 悲伤
angry: cat_angry.wav # 生气
afraid: cat_afraid.wav # 害怕
helpless: cat_helpless.wav # 无奈
coquetry: cat_coquetry.wav # 撒娇/卖萌
default: cat_neutral.wav # 兜底
# 可选:兜底文件(绝对路径),如 emotion_files 中未指定 default 时使用
# default_file: /abs/path/to/animal_default.wav
ACGNTTS: ACGNTTS:
#在线网址:https://acgn.ttson.cn/ #在线网址:https://acgn.ttson.cn/
#token购买:www.ttson.cn #token购买:www.ttson.cn

View file

@ -0,0 +1,116 @@
import os
import queue
from typing import Dict, List
from config.logger import setup_logging
from core.providers.tts.base import TTSProviderBase
from core.providers.tts.dto.dto import SentenceType, ContentType
TAG = __name__
logger = setup_logging()
class TTSProvider(TTSProviderBase):
"""
通过情绪标签播放预置的动物叫声。
适用于“动物互动”模式:LLM 返回情绪词(如 开心/悲伤/生气/害怕/无奈/撒娇),
本 TTS 直接选择对应音频文件推送给客户端,而不做文字合成。
"""
def __init__(self, config, delete_audio_file):
super().__init__(config, delete_audio_file)
self.base_path = config.get("base_path", "config/assets/animal_sounds")
# emotion -> filename 映射;支持 default 兜底
self.emotion_files: Dict[str, str] = config.get("emotion_files", {})
self.default_file = config.get("default_file")
self.keyword_map: Dict[str, List[str]] = config.get(
"emotion_keywords",
{
"happy": ["开心", "高兴", "快乐", "喜悦"],
"sad": ["悲伤", "难过", "伤心", "沮丧"],
"angry": ["生气", "愤怒", "气愤"],
"afraid": ["害怕", "恐惧", "紧张"],
"helpless": ["无奈", "唉", "叹气"],
"coquetry": ["撒娇", "卖萌", "黏人"],
},
)
self._session_started = False
async def text_to_speak(self, text, output_file):
"""
动物叫声模式不做 TTS 合成,直接使用预置音频。
保留空实现以兼容基类接口。
"""
return None
def tts_text_priority_thread(self):
"""
将文本情绪映射为音频文件并推送到播放队列。
"""
while not self.conn.stop_event.is_set():
try:
message = self.tts_text_queue.get(timeout=1)
if message.sentence_type == SentenceType.FIRST:
# 新一句开始,重置状态
self.conn.client_abort = False
self._session_started = False
self.tts_audio_first_sentence = True
continue
if self.conn.client_abort:
continue
if message.content_type == ContentType.TEXT:
emotion = self._detect_emotion(message.content_detail or "")
audio_path = self._resolve_audio_path(emotion)
label_for_log = emotion or (message.content_detail or "")
# 发送开场标记,便于客户端识别一句话的开始
if not self._session_started:
self.tts_audio_queue.put(
(SentenceType.FIRST, None, label_for_log)
)
self._session_started = True
if audio_path and os.path.exists(audio_path):
logger.bind(tag=TAG).info(
f"动物音频播放: emotion={emotion}, file={audio_path}"
)
self._process_audio_file_stream(
audio_path, callback=self.handle_opus
)
else:
logger.bind(tag=TAG).warning(
f"未找到对应的动物音频,emotion={emotion}, path={audio_path}"
)
if message.sentence_type == SentenceType.LAST:
# 一句话结束
self.tts_audio_queue.put(
(SentenceType.LAST, [], message.content_detail)
)
self._session_started = False
except queue.Empty:
continue
except Exception as e:
logger.bind(tag=TAG).error(
f"动物音频处理失败: {e.__class__.__name__}, {e}"
)
def _detect_emotion(self, text: str) -> str:
"""根据关键词粗略匹配情绪标签"""
for emotion, keywords in self.keyword_map.items():
if any(k in text for k in keywords):
return emotion
return "default"
def _resolve_audio_path(self, emotion: str) -> str:
filename = self.emotion_files.get(emotion) or self.emotion_files.get("default")
if not filename and self.default_file:
filename = self.default_file
if not filename:
return ""
return filename if os.path.isabs(filename) else os.path.join(self.base_path, filename)