Spaces:
Running
Running
feat: UTAU oto.ini 导出插件
Browse files- .gitignore +3 -0
- docs/TODO_utau导出.md +232 -8
- docs/公开部署方案.md +0 -322
- docs/流程文档_AI用.md +38 -0
- requirements.in +6 -2
- requirements.txt +4 -0
- src/export_plugins/base.py +42 -1
- src/export_plugins/loader.py +2 -1
- src/export_plugins/utau_oto_export.py +878 -0
- src/quality_scorer.py +270 -0
.gitignore
CHANGED
|
@@ -26,6 +26,9 @@ mfa_temp/
|
|
| 26 |
# 用户配置 (包含本地路径)
|
| 27 |
config.json
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# 用户数据目录 (保留目录结构)
|
| 30 |
bank/*/
|
| 31 |
export/*/
|
|
|
|
| 26 |
# 用户配置 (包含本地路径)
|
| 27 |
config.json
|
| 28 |
|
| 29 |
+
# 本地启动脚本 (包含本地路径)
|
| 30 |
+
run_local*.bat
|
| 31 |
+
|
| 32 |
# 用户数据目录 (保留目录结构)
|
| 33 |
bank/*/
|
| 34 |
export/*/
|
docs/TODO_utau导出.md
CHANGED
|
@@ -101,16 +101,240 @@ intervals [10]: xmin = 0.98, xmax = 1.04, text = "i" # 元音 60ms
|
|
| 101 |
- 或使用 phones 层的 IPA 转换为假名/拼音
|
| 102 |
- 支持 CV(辅音+元音)和 VCV 等不同录音方式
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
- [
|
| 108 |
-
- [
|
| 109 |
-
- [
|
| 110 |
-
- [
|
| 111 |
-
- [
|
| 112 |
-
- [
|
| 113 |
-
- [
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
## 参考资料
|
| 116 |
- [Wasteland UTAU - OTO Configuration](https://wastelandutau.neocities.org/en/config)
|
|
|
|
| 101 |
- 或使用 phones 层的 IPA 转换为假名/拼音
|
| 102 |
- 支持 CV(辅音+元音)和 VCV 等不同录音方式
|
| 103 |
|
| 104 |
+
## 音源质量评分方案
|
| 105 |
+
|
| 106 |
+
### 问题背景
|
| 107 |
+
简单导出仅按时长排序,但时长较长的音频可能存在以下问题:
|
| 108 |
+
- 多个字合并(MFA 对齐错误)
|
| 109 |
+
- 音调转变大(不适合 UTAU 拉伸)
|
| 110 |
+
- 音量波动大(录音不稳定)
|
| 111 |
+
|
| 112 |
+
对于 UTAU 音源,需要更精细的质量评估。
|
| 113 |
+
|
| 114 |
+
### 评分维度
|
| 115 |
+
|
| 116 |
+
| 维度 | 指标 | 计算方式 | 理想值 | 权重建议 |
|
| 117 |
+
|-----|------|---------|-------|---------|
|
| 118 |
+
| 时长 | duration | 音频时长(秒) | 适中(0.3~0.8s) | 0.3 |
|
| 119 |
+
| 音量稳定性 | rms_variance | RMS 能量的方差 | 越小越好 | 0.3 |
|
| 120 |
+
| 音高稳定性 | f0_variance | 基频的方差 | 越小越好 | 0.4 |
|
| 121 |
+
|
| 122 |
+
### 各维度详细说明
|
| 123 |
+
|
| 124 |
+
#### 1. 时长评分 (Duration Score)
|
| 125 |
+
```python
|
| 126 |
+
def duration_score(duration: float) -> float:
|
| 127 |
+
"""
|
| 128 |
+
时长评分:适中时长得分最高
|
| 129 |
+
|
| 130 |
+
- 过短(<0.2s):发音不完整
|
| 131 |
+
- 过长(>1.0s):可能包含多字或拖音
|
| 132 |
+
- 最佳范围:0.3~0.8s
|
| 133 |
+
"""
|
| 134 |
+
if duration < 0.2:
|
| 135 |
+
return duration / 0.2 * 0.5 # 0~0.5分
|
| 136 |
+
elif duration <= 0.8:
|
| 137 |
+
return 1.0 # 满分
|
| 138 |
+
elif duration <= 1.2:
|
| 139 |
+
return 1.0 - (duration - 0.8) / 0.4 * 0.3 # 0.7~1.0分
|
| 140 |
+
else:
|
| 141 |
+
return max(0.3, 0.7 - (duration - 1.2) * 0.2) # 递减
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
#### 2. 音量稳定性评分 (RMS Variance Score)
|
| 145 |
+
```python
|
| 146 |
+
def rms_variance_score(audio: np.ndarray, sr: int, frame_ms: int = 20) -> float:
|
| 147 |
+
"""
|
| 148 |
+
音量稳定性评分:RMS 方差越小越好
|
| 149 |
+
|
| 150 |
+
计算步骤:
|
| 151 |
+
1. 将音频分帧(默认20ms一帧)
|
| 152 |
+
2. 计算每帧的 RMS 能量
|
| 153 |
+
3. 计算 RMS 序列的方差
|
| 154 |
+
4. 归一化到 0~1 分数
|
| 155 |
+
"""
|
| 156 |
+
frame_size = int(sr * frame_ms / 1000)
|
| 157 |
+
frames = len(audio) // frame_size
|
| 158 |
+
|
| 159 |
+
rms_values = []
|
| 160 |
+
for i in range(frames):
|
| 161 |
+
frame = audio[i * frame_size : (i + 1) * frame_size]
|
| 162 |
+
rms = np.sqrt(np.mean(frame ** 2))
|
| 163 |
+
rms_values.append(rms)
|
| 164 |
+
|
| 165 |
+
if len(rms_values) < 2:
|
| 166 |
+
return 0.5 # 太短无法评估
|
| 167 |
+
|
| 168 |
+
variance = np.var(rms_values)
|
| 169 |
+
# 归一化:方差越小分数越高
|
| 170 |
+
# 经验阈值:方差 < 0.01 为优秀,> 0.1 为较差
|
| 171 |
+
score = max(0, 1.0 - variance * 10)
|
| 172 |
+
return score
|
| 173 |
+
```
|
| 174 |
+
|
| 175 |
+
#### 3. 音高稳定性评分 (F0 Variance Score)
|
| 176 |
+
```python
|
| 177 |
+
def f0_variance_score(audio: np.ndarray, sr: int) -> float:
|
| 178 |
+
"""
|
| 179 |
+
音高稳定性评分:F0 方差越小越好
|
| 180 |
+
|
| 181 |
+
计算步骤:
|
| 182 |
+
1. 使用 pyin/crepe/parselmouth 提取 F0
|
| 183 |
+
2. 过滤无声帧(F0=0 或 NaN)
|
| 184 |
+
3. 计算有效 F0 的方差
|
| 185 |
+
4. 归一化到 0~1 分数
|
| 186 |
+
|
| 187 |
+
依赖:librosa.pyin 或 parselmouth
|
| 188 |
+
"""
|
| 189 |
+
import librosa
|
| 190 |
+
|
| 191 |
+
# 提取 F0(使用 pyin 算法)
|
| 192 |
+
f0, voiced_flag, voiced_probs = librosa.pyin(
|
| 193 |
+
audio,
|
| 194 |
+
fmin=librosa.note_to_hz('C2'), # ~65Hz
|
| 195 |
+
fmax=librosa.note_to_hz('C6'), # ~1047Hz
|
| 196 |
+
sr=sr
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
# 过滤无效值
|
| 200 |
+
valid_f0 = f0[~np.isnan(f0)]
|
| 201 |
+
|
| 202 |
+
if len(valid_f0) < 3:
|
| 203 |
+
return 0.5 # 无法评估
|
| 204 |
+
|
| 205 |
+
# 转换为音分(cents)计算方差,避免频率绝对值影响
|
| 206 |
+
# cents = 1200 * log2(f / f_ref)
|
| 207 |
+
f0_cents = 1200 * np.log2(valid_f0 / np.median(valid_f0))
|
| 208 |
+
variance = np.var(f0_cents)
|
| 209 |
+
|
| 210 |
+
# 归一化:方差 < 100 cents² 为优秀,> 10000 cents² 为较差
|
| 211 |
+
# 100 cents ≈ 1个半音
|
| 212 |
+
score = max(0, 1.0 - variance / 10000)
|
| 213 |
+
return score
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
### 综合评分计算
|
| 217 |
+
|
| 218 |
+
```python
|
| 219 |
+
def calculate_quality_score(
|
| 220 |
+
audio: np.ndarray,
|
| 221 |
+
sr: int,
|
| 222 |
+
weights: dict = None,
|
| 223 |
+
enabled_metrics: list = None
|
| 224 |
+
) -> float:
|
| 225 |
+
"""
|
| 226 |
+
综合质量评分
|
| 227 |
+
|
| 228 |
+
参数:
|
| 229 |
+
audio: 音频数据
|
| 230 |
+
sr: 采样率
|
| 231 |
+
weights: 各维度权重,如 {"duration": 0.3, "rms": 0.3, "f0": 0.4}
|
| 232 |
+
enabled_metrics: 启用的评分维度,如 ["duration", "rms", "f0"]
|
| 233 |
+
|
| 234 |
+
返回:
|
| 235 |
+
0~1 的综合分数
|
| 236 |
+
"""
|
| 237 |
+
default_weights = {"duration": 0.3, "rms": 0.3, "f0": 0.4}
|
| 238 |
+
weights = weights or default_weights
|
| 239 |
+
enabled_metrics = enabled_metrics or ["duration", "rms", "f0"]
|
| 240 |
+
|
| 241 |
+
scores = {}
|
| 242 |
+
duration = len(audio) / sr
|
| 243 |
+
|
| 244 |
+
if "duration" in enabled_metrics:
|
| 245 |
+
scores["duration"] = duration_score(duration)
|
| 246 |
+
|
| 247 |
+
if "rms" in enabled_metrics:
|
| 248 |
+
scores["rms"] = rms_variance_score(audio, sr)
|
| 249 |
+
|
| 250 |
+
if "f0" in enabled_metrics:
|
| 251 |
+
scores["f0"] = f0_variance_score(audio, sr)
|
| 252 |
+
|
| 253 |
+
# 加权平均(仅计算启用的维度)
|
| 254 |
+
total_weight = sum(weights[k] for k in scores.keys())
|
| 255 |
+
final_score = sum(scores[k] * weights[k] for k in scores.keys()) / total_weight
|
| 256 |
+
|
| 257 |
+
return final_score
|
| 258 |
+
```
|
| 259 |
+
|
| 260 |
+
### 用户配置选项
|
| 261 |
+
|
| 262 |
+
```python
|
| 263 |
+
# 插件选项设计
|
| 264 |
+
PluginOption(
|
| 265 |
+
key="quality_metrics",
|
| 266 |
+
label="质量评估维度",
|
| 267 |
+
option_type=OptionType.MULTI_SELECT,
|
| 268 |
+
default=["duration"],
|
| 269 |
+
choices=[
|
| 270 |
+
("duration", "时长(快速)"),
|
| 271 |
+
("rms", "音量稳定性(中速)"),
|
| 272 |
+
("f0", "音高稳定性(较慢)")
|
| 273 |
+
],
|
| 274 |
+
description="选择用于排序的质量指标,多选时综合评分"
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
PluginOption(
|
| 278 |
+
key="duration_weight",
|
| 279 |
+
label="时长权重",
|
| 280 |
+
option_type=OptionType.SLIDER,
|
| 281 |
+
default=0.3,
|
| 282 |
+
min_value=0,
|
| 283 |
+
max_value=1,
|
| 284 |
+
step=0.1,
|
| 285 |
+
visible_when={"quality_metrics": "contains:duration"}
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
# 类似地添加 rms_weight 和 f0_weight
|
| 289 |
+
```
|
| 290 |
+
|
| 291 |
+
### 性能考虑
|
| 292 |
+
|
| 293 |
+
| 评估维度 | 耗时估算(每文件) | 依赖 |
|
| 294 |
+
|---------|------------------|------|
|
| 295 |
+
| duration | <1ms | 无 |
|
| 296 |
+
| rms | ~5ms | numpy |
|
| 297 |
+
| f0 | ~50-200ms | librosa 或 parselmouth |
|
| 298 |
+
|
| 299 |
+
建议:
|
| 300 |
+
- 默认仅启用 `duration`(兼容现有行为)
|
| 301 |
+
- UTAU 导出时推荐启用 `duration` + `f0`
|
| 302 |
+
- 完整评估启用全部三项
|
| 303 |
+
|
| 304 |
+
### 缓存策略
|
| 305 |
+
|
| 306 |
+
为避免重复计算,可将评分结果缓存到 JSON:
|
| 307 |
+
|
| 308 |
+
```json
|
| 309 |
+
// bank/{source}/quality_cache.json
|
| 310 |
+
{
|
| 311 |
+
"version": "1.0",
|
| 312 |
+
"metrics": ["duration", "rms", "f0"],
|
| 313 |
+
"scores": {
|
| 314 |
+
"segments/ba/1.wav": {
|
| 315 |
+
"duration": 0.85,
|
| 316 |
+
"rms": 0.72,
|
| 317 |
+
"f0": 0.91,
|
| 318 |
+
"combined": 0.83
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
}
|
| 322 |
+
```
|
| 323 |
+
|
| 324 |
## 待实现功能
|
| 325 |
|
| 326 |
+
- [x] 创建 `src/export_plugins/utau_oto_export.py`
|
| 327 |
+
- [x] 实现 IPA 音素分类器(辅音/元音判断)
|
| 328 |
+
- [x] 实现 TextGrid 解析与音素配对逻辑
|
| 329 |
+
- [x] 实现 oto.ini 参数计算
|
| 330 |
+
- [x] 支持中文和日语
|
| 331 |
+
- [x] 日语支持罗马音/平假名别名切换
|
| 332 |
+
- [x] 一个 wav 文件支持多条 oto 配置(不裁剪音频)
|
| 333 |
+
- [x] 每个别名最大样本数限制
|
| 334 |
+
- [x] 添加到 GUI 导出选项(通过 loader.py 自动注册)
|
| 335 |
+
- [x] 实现音源质量评分模块 `src/quality_scorer.py`
|
| 336 |
+
- [x] 在导出插件基类中集成质量评分接口
|
| 337 |
+
- [x] 为 UTAU 导出插件添加质量评分选项
|
| 338 |
|
| 339 |
## 参考资料
|
| 340 |
- [Wasteland UTAU - OTO Configuration](https://wastelandutau.neocities.org/en/config)
|
docs/公开部署方案.md
DELETED
|
@@ -1,322 +0,0 @@
|
|
| 1 |
-
# 人力V助手 在线部署方案
|
| 2 |
-
|
| 3 |
-
## 快速部署指南 (魔搭创空间)
|
| 4 |
-
|
| 5 |
-
### 部署文件清单
|
| 6 |
-
|
| 7 |
-
| 文件 | 说明 |
|
| 8 |
-
|------|------|
|
| 9 |
-
| `ms_deploy.json` | 魔搭创空间部署配置 |
|
| 10 |
-
| `requirements_cloud.txt` | 云端依赖文件 |
|
| 11 |
-
| `app.py` | 云端入口 (已就绪) |
|
| 12 |
-
|
| 13 |
-
### 部署步骤
|
| 14 |
-
|
| 15 |
-
1. **注册魔搭账号**: https://modelscope.cn
|
| 16 |
-
2. **创建创空间**:
|
| 17 |
-
- 进入「创空间」→「创建创空间」
|
| 18 |
-
- 选择「Gradio」类型
|
| 19 |
-
- 填写名称和描述
|
| 20 |
-
3. **上传代码**:
|
| 21 |
-
- 方式一: 直接上传 zip 包
|
| 22 |
-
- 方式二: 关联 GitHub/Gitee 仓库
|
| 23 |
-
4. **配置部署**:
|
| 24 |
-
- 上传 `ms_deploy.json` 或在界面配置
|
| 25 |
-
- 将 `requirements_cloud.txt` 重命名为 `requirements.txt` (或在部署时指定)
|
| 26 |
-
5. **启动应用**: 点击「部署」等待构建完成
|
| 27 |
-
|
| 28 |
-
### 注意事项
|
| 29 |
-
|
| 30 |
-
- 首次启动需要下载模型,可能需要 5-10 分钟
|
| 31 |
-
- 云端数据不持久,处理完成后请及时下载结果
|
| 32 |
-
- 免费配额为 2vCPU/16GB 内存,适合小规模处理
|
| 33 |
-
|
| 34 |
-
---
|
| 35 |
-
|
| 36 |
-
## 一、平台对比与选择
|
| 37 |
-
|
| 38 |
-
| 平台 | 免费配额 | GPU | 存储 | 国内访问 | 推荐度 |
|
| 39 |
-
|------|----------|-----|------|----------|--------|
|
| 40 |
-
| Hugging Face Spaces | 2vCPU/16GB | 付费 | 50GB | 较慢 | ⭐⭐⭐ |
|
| 41 |
-
| 魔塔社区 (ModelScope) | 2vCPU/16GB | 免费T4 | 50GB | 快 | ⭐⭐⭐⭐⭐ |
|
| 42 |
-
| 阿里云函数计算 | 按量付费 | 可选 | - | 快 | ⭐⭐ |
|
| 43 |
-
|
| 44 |
-
**推荐:魔塔社区** - 国内访问快、免费 GPU、对中文项目友好
|
| 45 |
-
|
| 46 |
-
---
|
| 47 |
-
|
| 48 |
-
## 二、核心问题与解决方案
|
| 49 |
-
|
| 50 |
-
### 问题1:MFA 引擎不兼容
|
| 51 |
-
|
| 52 |
-
当前 MFA 使用 Windows 外挂模式 (`tools/mfa_engine/python.exe`),云平台为 Linux。
|
| 53 |
-
|
| 54 |
-
**解决方案:使用 conda 安装 MFA**
|
| 55 |
-
|
| 56 |
-
```dockerfile
|
| 57 |
-
# 在 Linux 环境安装 MFA
|
| 58 |
-
RUN conda install -c conda-forge montreal-forced-aligner -y
|
| 59 |
-
```
|
| 60 |
-
|
| 61 |
-
需要修改 `src/mfa_runner.py` 支持 Linux 原生调用。
|
| 62 |
-
|
| 63 |
-
### 问题2:模型文件体积大
|
| 64 |
-
|
| 65 |
-
| 模型 | 大小 | 处理方式 |
|
| 66 |
-
|------|------|----------|
|
| 67 |
-
| Whisper small | ~500MB | 首次运行自动下载 |
|
| 68 |
-
| Whisper medium | ~1.5GB | 首次运行自动下载 |
|
| 69 |
-
| Silero VAD | ~2MB | 打包到仓库 |
|
| 70 |
-
| MFA 声学模型 | ~100MB | 打包或首次下载 |
|
| 71 |
-
|
| 72 |
-
### 问题3:用户数据存储
|
| 73 |
-
|
| 74 |
-
云平台重启后数据丢失,需要:
|
| 75 |
-
- 处理完成后提供下载链接
|
| 76 |
-
- 或集成云存储 (OSS/S3)
|
| 77 |
-
|
| 78 |
-
---
|
| 79 |
-
|
| 80 |
-
## 三、代码改造清单
|
| 81 |
-
|
| 82 |
-
### 3.1 MFA 运行器改造
|
| 83 |
-
|
| 84 |
-
创建 `src/mfa_runner_linux.py` 或修改现有文件支持双平台:
|
| 85 |
-
|
| 86 |
-
```python
|
| 87 |
-
import platform
|
| 88 |
-
import subprocess
|
| 89 |
-
|
| 90 |
-
def get_mfa_command():
|
| 91 |
-
"""根据平台返回 MFA 命令"""
|
| 92 |
-
if platform.system() == "Windows":
|
| 93 |
-
# Windows: 使用外挂 Python
|
| 94 |
-
return [str(MFA_PYTHON), "-m", "montreal_forced_aligner"]
|
| 95 |
-
else:
|
| 96 |
-
# Linux: 使用系统安装的 mfa
|
| 97 |
-
return ["mfa"]
|
| 98 |
-
|
| 99 |
-
def run_mfa_alignment(...):
|
| 100 |
-
cmd = get_mfa_command() + ["align", ...]
|
| 101 |
-
# ...
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
-
### 3.2 路径处理改造
|
| 105 |
-
|
| 106 |
-
```python
|
| 107 |
-
import tempfile
|
| 108 |
-
|
| 109 |
-
# 云环境使用临时目录
|
| 110 |
-
if os.environ.get("SPACE_ID"): # HF Spaces
|
| 111 |
-
BANK_DIR = tempfile.mkdtemp()
|
| 112 |
-
EXPORT_DIR = tempfile.mkdtemp()
|
| 113 |
-
```
|
| 114 |
-
|
| 115 |
-
### 3.3 GUI 改造
|
| 116 |
-
|
| 117 |
-
添加下载按钮,让用户下载处理结果:
|
| 118 |
-
|
| 119 |
-
```python
|
| 120 |
-
# 在导出完成后提供下载
|
| 121 |
-
output_zip = gr.File(label="下载结果")
|
| 122 |
-
```
|
| 123 |
-
|
| 124 |
-
---
|
| 125 |
-
|
| 126 |
-
## 四、Hugging Face Spaces 部署
|
| 127 |
-
|
| 128 |
-
### 4.1 目录结构
|
| 129 |
-
|
| 130 |
-
```
|
| 131 |
-
jinriki-helper/
|
| 132 |
-
├── app.py # 入口 (重命名自 main.py)
|
| 133 |
-
├── requirements.txt
|
| 134 |
-
├── packages.txt # 系统依赖
|
| 135 |
-
├── README.md
|
| 136 |
-
├── src/
|
| 137 |
-
├── models/
|
| 138 |
-
│ └── silero_vad/ # 预置小模型
|
| 139 |
-
└── ...
|
| 140 |
-
```
|
| 141 |
-
|
| 142 |
-
### 4.2 创建 packages.txt
|
| 143 |
-
|
| 144 |
-
```
|
| 145 |
-
ffmpeg
|
| 146 |
-
libsndfile1
|
| 147 |
-
```
|
| 148 |
-
|
| 149 |
-
### 4.3 创建 app.py
|
| 150 |
-
|
| 151 |
-
```python
|
| 152 |
-
# HF Spaces 入口
|
| 153 |
-
import os
|
| 154 |
-
os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
|
| 155 |
-
|
| 156 |
-
from src.gui import create_ui
|
| 157 |
-
|
| 158 |
-
app = create_ui()
|
| 159 |
-
app.launch()
|
| 160 |
-
```
|
| 161 |
-
|
| 162 |
-
### 4.4 修改 requirements.txt
|
| 163 |
-
|
| 164 |
-
移除 Windows 专用依赖,添加:
|
| 165 |
-
```
|
| 166 |
-
montreal-forced-aligner
|
| 167 |
-
```
|
| 168 |
-
|
| 169 |
-
### 4.5 部署步骤
|
| 170 |
-
|
| 171 |
-
1. 创建 HF 账号并新建 Space (选择 Gradio SDK)
|
| 172 |
-
2. 上传代码或连接 GitHub 仓库
|
| 173 |
-
3. 等待构建完成
|
| 174 |
-
|
| 175 |
-
---
|
| 176 |
-
|
| 177 |
-
## 五、魔塔社区部署 (推荐)
|
| 178 |
-
|
| 179 |
-
### 5.1 目录结构
|
| 180 |
-
|
| 181 |
-
```
|
| 182 |
-
jinriki-helper/
|
| 183 |
-
├── app.py
|
| 184 |
-
├── requirements.txt
|
| 185 |
-
├── README.md
|
| 186 |
-
├── src/
|
| 187 |
-
└── ...
|
| 188 |
-
```
|
| 189 |
-
|
| 190 |
-
### 5.2 创建 app.py
|
| 191 |
-
|
| 192 |
-
```python
|
| 193 |
-
# 魔塔社区入口
|
| 194 |
-
import os
|
| 195 |
-
import subprocess
|
| 196 |
-
|
| 197 |
-
# 安装 MFA (首次运行)
|
| 198 |
-
def setup_mfa():
|
| 199 |
-
try:
|
| 200 |
-
subprocess.run(["mfa", "version"], check=True, capture_output=True)
|
| 201 |
-
except:
|
| 202 |
-
print("正在安装 MFA...")
|
| 203 |
-
subprocess.run([
|
| 204 |
-
"pip", "install", "montreal-forced-aligner"
|
| 205 |
-
], check=True)
|
| 206 |
-
# 下载模型
|
| 207 |
-
subprocess.run(["mfa", "model", "download", "acoustic", "mandarin_mfa"])
|
| 208 |
-
subprocess.run(["mfa", "model", "download", "dictionary", "mandarin_china_mfa"])
|
| 209 |
-
|
| 210 |
-
setup_mfa()
|
| 211 |
-
|
| 212 |
-
from src.gui import create_ui
|
| 213 |
-
app = create_ui()
|
| 214 |
-
app.launch()
|
| 215 |
-
```
|
| 216 |
-
|
| 217 |
-
### 5.3 部署步骤
|
| 218 |
-
|
| 219 |
-
1. 注册魔塔社区账号: https://modelscope.cn
|
| 220 |
-
2. 创建新的创空间 (选择 Gradio)
|
| 221 |
-
3. 上传代码或关联 GitHub
|
| 222 |
-
4. 配置环境 (选择 GPU 实例可加速 Whisper)
|
| 223 |
-
|
| 224 |
-
---
|
| 225 |
-
|
| 226 |
-
## 六、需要修改的文件
|
| 227 |
-
|
| 228 |
-
### 6.1 src/mfa_runner.py
|
| 229 |
-
|
| 230 |
-
```python
|
| 231 |
-
# 添加跨平台支持
|
| 232 |
-
import platform
|
| 233 |
-
import shutil
|
| 234 |
-
|
| 235 |
-
def check_mfa_available() -> bool:
|
| 236 |
-
"""检查 MFA 是否可用"""
|
| 237 |
-
if platform.system() == "Windows":
|
| 238 |
-
return MFA_PYTHON.exists()
|
| 239 |
-
else:
|
| 240 |
-
# Linux: 检查系统命令
|
| 241 |
-
return shutil.which("mfa") is not None
|
| 242 |
-
|
| 243 |
-
def _get_mfa_cmd() -> list:
|
| 244 |
-
"""获取 MFA 命令前缀"""
|
| 245 |
-
if platform.system() == "Windows":
|
| 246 |
-
return [str(MFA_PYTHON), "-m", "montreal_forced_aligner"]
|
| 247 |
-
return ["mfa"]
|
| 248 |
-
```
|
| 249 |
-
|
| 250 |
-
### 6.2 src/gui.py
|
| 251 |
-
|
| 252 |
-
```python
|
| 253 |
-
# 添加结果下载功能
|
| 254 |
-
import zipfile
|
| 255 |
-
import tempfile
|
| 256 |
-
|
| 257 |
-
def create_download_zip(source_dir: str) -> str:
|
| 258 |
-
"""打包目录为 zip 供下载"""
|
| 259 |
-
zip_path = tempfile.mktemp(suffix=".zip")
|
| 260 |
-
with zipfile.ZipFile(zip_path, 'w') as zf:
|
| 261 |
-
for root, dirs, files in os.walk(source_dir):
|
| 262 |
-
for file in files:
|
| 263 |
-
file_path = os.path.join(root, file)
|
| 264 |
-
arcname = os.path.relpath(file_path, source_dir)
|
| 265 |
-
zf.write(file_path, arcname)
|
| 266 |
-
return zip_path
|
| 267 |
-
```
|
| 268 |
-
|
| 269 |
-
### 6.3 requirements.txt (云端版)
|
| 270 |
-
|
| 271 |
-
```
|
| 272 |
-
gradio==6.2.0
|
| 273 |
-
transformers>=4.25.0
|
| 274 |
-
torch
|
| 275 |
-
torchaudio
|
| 276 |
-
accelerate
|
| 277 |
-
silero-vad>=5.1
|
| 278 |
-
onnxruntime
|
| 279 |
-
textgrid
|
| 280 |
-
audiofile
|
| 281 |
-
tqdm
|
| 282 |
-
pypinyin
|
| 283 |
-
pykakasi
|
| 284 |
-
# Linux 环境直接 pip 安装 MFA
|
| 285 |
-
montreal-forced-aligner
|
| 286 |
-
```
|
| 287 |
-
|
| 288 |
-
---
|
| 289 |
-
|
| 290 |
-
## 七、功能限制说明
|
| 291 |
-
|
| 292 |
-
在线版相比本地版的限制:
|
| 293 |
-
|
| 294 |
-
| 功能 | 本地版 | 在线版 |
|
| 295 |
-
|------|--------|--------|
|
| 296 |
-
| 处理大文件 | ✅ 无限制 | ⚠️ 受内存限制 |
|
| 297 |
-
| 数据持久化 | ✅ 本地保存 | ❌ 需下载 |
|
| 298 |
-
| GPU 加速 | ✅ 本地显卡 | ⚠️ 取决于平台 |
|
| 299 |
-
| 批量处理 | ✅ 支持 | ⚠️ 建议限制数量 |
|
| 300 |
-
|
| 301 |
-
建议在 UI 中添加提示:
|
| 302 |
-
> 在线版适合试用和小规模处理,大规模制作建议下载本地版
|
| 303 |
-
|
| 304 |
-
---
|
| 305 |
-
|
| 306 |
-
## 八、执行步骤
|
| 307 |
-
|
| 308 |
-
### 第一步:改造 MFA 运行器
|
| 309 |
-
修改 `src/mfa_runner.py` 支持 Linux
|
| 310 |
-
|
| 311 |
-
### 第二步:创建云端入口
|
| 312 |
-
创建 `app.py`
|
| 313 |
-
|
| 314 |
-
### 第三步:添加下载功能
|
| 315 |
-
修改 `src/gui.py` 添加结果打包下载
|
| 316 |
-
|
| 317 |
-
### 第四步:部署测试
|
| 318 |
-
先在魔塔社区测试,成功后同步到 HF Spaces
|
| 319 |
-
|
| 320 |
-
---
|
| 321 |
-
|
| 322 |
-
需要我帮你执行代码改造吗?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
docs/流程文档_AI用.md
CHANGED
|
@@ -131,6 +131,18 @@
|
|
| 131 |
│ │ 4. 按时长排序,保留最佳样本 │ │
|
| 132 |
│ │ 5. 按命名规则导出 (如: ba.wav, ba1.wav, ba2.wav) │ │
|
| 133 |
│ └─────────────────────────────────────────────────────────────────────┘ │
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
│ │
|
| 135 |
│ 输出: export/[音源名称]/simple_export/ │
|
| 136 |
│ ├── ba.wav │
|
|
@@ -192,14 +204,40 @@ MFA 支持两种运行模式:
|
|
| 192 |
| 插件基类 | `export_plugins/base.py` | 定义插件接口和配置选项 |
|
| 193 |
| 插件加载器 | `export_plugins/loader.py` | 扫描和加载插件 |
|
| 194 |
| 简单导出 | `export_plugins/simple_export.py` | 按拼音分类导出单字音频 |
|
|
|
|
|
|
|
| 195 |
|
| 196 |
插件配置选项类型:
|
| 197 |
- `TEXT`: 文本输入
|
| 198 |
- `NUMBER`: 数字输入
|
| 199 |
- `SWITCH`: 开关
|
| 200 |
- `COMBO`: 下拉选择
|
|
|
|
| 201 |
- `FILE`/`FOLDER`: 文件/文件夹选择
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
### 5. MFA 跨平台支持
|
| 204 |
|
| 205 |
MFA 支持三种运行模式:
|
|
|
|
| 131 |
│ │ 4. 按时长排序,保留最佳样本 │ │
|
| 132 |
│ │ 5. 按命名规则导出 (如: ba.wav, ba1.wav, ba2.wav) │ │
|
| 133 |
│ └─────────────────────────────────────────────────────────────────────┘ │
|
| 134 |
+
│ ┌─────────────────────────────────────────────────────────────────────┐ │
|
| 135 |
+
│ │ UTAU oto.ini 导出插件 (UTAUOtoExportPlugin) │ │
|
| 136 |
+
│ │ 1. 从 TextGrid phones 层提取音素时间边界 │ │
|
| 137 |
+
│ │ 2. 识别辅音+元音对,计算 oto.ini 六参数 │ │
|
| 138 |
+
│ │ • Offset: 音频开始位置 │ │
|
| 139 |
+
│ │ • Consonant: 不被拉伸的区域 │ │
|
| 140 |
+
│ │ • Cutoff: 音频结束位置(负值从末尾算) │ │
|
| 141 |
+
│ │ • Preutterance: 与节拍对齐位置 │ │
|
| 142 |
+
│ │ • Overlap: 交叉淡化区域 │ │
|
| 143 |
+
│ │ 3. IPA 音素转换为拼音/罗马音别名 │ │
|
| 144 |
+
│ │ 4. 生成 oto.ini 配置文件 │ │
|
| 145 |
+
│ └─────────────────────────────────────────────────────────────────────┘ │
|
| 146 |
│ │
|
| 147 |
│ 输出: export/[音源名称]/simple_export/ │
|
| 148 |
│ ├── ba.wav │
|
|
|
|
| 204 |
| 插件基类 | `export_plugins/base.py` | 定义插件接口和配置选项 |
|
| 205 |
| 插件加载器 | `export_plugins/loader.py` | 扫描和加载插件 |
|
| 206 |
| 简单导出 | `export_plugins/simple_export.py` | 按拼音分类导出单字音频 |
|
| 207 |
+
| UTAU 导出 | `export_plugins/utau_oto_export.py` | 生成 UTAU 音源配置文件 (oto.ini) |
|
| 208 |
+
| 质量评分 | `quality_scorer.py` | 音频质量多维度评估 |
|
| 209 |
|
| 210 |
插件配置选项类型:
|
| 211 |
- `TEXT`: 文本输入
|
| 212 |
- `NUMBER`: 数字输入
|
| 213 |
- `SWITCH`: 开关
|
| 214 |
- `COMBO`: 下拉选择
|
| 215 |
+
- `MULTI_SELECT`: 多选框
|
| 216 |
- `FILE`/`FOLDER`: 文件/文件夹选择
|
| 217 |
|
| 218 |
+
### 5. 音源质量评分模块
|
| 219 |
+
|
| 220 |
+
`src/quality_scorer.py` 提供多维度音频质量评估:
|
| 221 |
+
|
| 222 |
+
| 评估维度 | 函数 | 说明 | 耗时 |
|
| 223 |
+
|---------|------|------|------|
|
| 224 |
+
| 时长 | `duration_score()` | 适中时长得分高 (0.3~0.8s 最佳) | <1ms |
|
| 225 |
+
| 音量稳定性 | `rms_variance_score()` | RMS 方差越小越好 | ~5ms |
|
| 226 |
+
| 音高稳定性 | `f0_variance_score()` | F0 方差越小越好 | ~50-200ms |
|
| 227 |
+
|
| 228 |
+
使用方式:
|
| 229 |
+
```python
|
| 230 |
+
from src.quality_scorer import QualityScorer
|
| 231 |
+
|
| 232 |
+
scorer = QualityScorer(enabled_metrics=["duration", "f0"])
|
| 233 |
+
scores = scorer.score_from_file("audio.wav")
|
| 234 |
+
# 返回: {"duration": 0.85, "f0": 0.91, "combined": 0.88}
|
| 235 |
+
```
|
| 236 |
+
|
| 237 |
+
导出插件基类已集成质量评分接口:
|
| 238 |
+
- `get_quality_scorer()`: 获取评分器实例
|
| 239 |
+
- `score_audio_quality()`: 直接评估音频文件
|
| 240 |
+
|
| 241 |
### 5. MFA 跨平台支持
|
| 242 |
|
| 243 |
MFA 支持三种运行模式:
|
requirements.in
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
# 直接依赖声明
|
| 2 |
# 使用 pip-compile requirements.in 生成 requirements.txt
|
| 3 |
|
| 4 |
-
# NumPy 版本
|
| 5 |
-
numpy
|
| 6 |
|
| 7 |
textgrid
|
| 8 |
audiofile
|
|
@@ -10,6 +10,7 @@ tqdm
|
|
| 10 |
|
| 11 |
# GUI框架
|
| 12 |
gradio==6.2.0
|
|
|
|
| 13 |
|
| 14 |
# Whisper 语音识别
|
| 15 |
transformers>=4.25.0
|
|
@@ -26,6 +27,9 @@ onnxruntime
|
|
| 26 |
pypinyin
|
| 27 |
pykakasi
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# MFA 强制对齐 (云端 Linux 环境使用)
|
| 30 |
# Windows 本地使用 tools/mfa_engine 外挂模式
|
| 31 |
# 注意: MFA 依赖 numba,不支持 Python 3.13,云端需单独安装
|
|
|
|
| 1 |
# 直接依赖声明
|
| 2 |
# 使用 pip-compile requirements.in 生成 requirements.txt
|
| 3 |
|
| 4 |
+
# NumPy 版本由其他依赖自动确定
|
| 5 |
+
# librosa 0.11+ 支持 numpy 1.x 和 2.x
|
| 6 |
|
| 7 |
textgrid
|
| 8 |
audiofile
|
|
|
|
| 10 |
|
| 11 |
# GUI框架
|
| 12 |
gradio==6.2.0
|
| 13 |
+
customtkinter>=5.2.0 # 本地桌面 GUI (gui_old.py)
|
| 14 |
|
| 15 |
# Whisper 语音识别
|
| 16 |
transformers>=4.25.0
|
|
|
|
| 27 |
pypinyin
|
| 28 |
pykakasi
|
| 29 |
|
| 30 |
+
# 音频分析 (质量评分模块)
|
| 31 |
+
librosa
|
| 32 |
+
|
| 33 |
# MFA 强制对齐 (云端 Linux 环境使用)
|
| 34 |
# Windows 本地使用 tools/mfa_engine 外挂模式
|
| 35 |
# 注意: MFA 依赖 numba,不支持 Python 3.13,云端需单独安装
|
requirements.txt
CHANGED
|
@@ -42,6 +42,10 @@ colorama==0.4.6
|
|
| 42 |
# via
|
| 43 |
# click
|
| 44 |
# tqdm
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
coloredlogs==15.0.1
|
| 46 |
# via onnxruntime
|
| 47 |
deprecated==1.3.1
|
|
|
|
| 42 |
# via
|
| 43 |
# click
|
| 44 |
# tqdm
|
| 45 |
+
customtkinter==5.2.2
|
| 46 |
+
# via -r requirements.in
|
| 47 |
+
darkdetect==0.8.0
|
| 48 |
+
# via customtkinter
|
| 49 |
coloredlogs==15.0.1
|
| 50 |
# via onnxruntime
|
| 51 |
deprecated==1.3.1
|
src/export_plugins/base.py
CHANGED
|
@@ -24,6 +24,7 @@ class OptionType(Enum):
|
|
| 24 |
FILE = "file" # 文件选择
|
| 25 |
FOLDER = "folder" # 文件夹选择
|
| 26 |
COMBO = "combo" # 下拉选择框
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
@dataclass
|
|
@@ -34,10 +35,12 @@ class PluginOption:
|
|
| 34 |
option_type: OptionType # 选项类型
|
| 35 |
default: Any = None # 默认值
|
| 36 |
description: str = "" # 描述说明
|
| 37 |
-
choices: List[
|
| 38 |
min_value: Optional[float] = None # 最小值(仅NUMBER类型)
|
| 39 |
max_value: Optional[float] = None # 最大值(仅NUMBER类型)
|
|
|
|
| 40 |
file_types: List[Tuple[str, str]] = field(default_factory=list) # 文件类型过滤
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
class ExportPlugin(ABC):
|
|
@@ -142,3 +145,41 @@ class ExportPlugin(ABC):
|
|
| 142 |
"slices_dir": os.path.join(source_dir, "slices"),
|
| 143 |
"textgrid_dir": os.path.join(source_dir, "textgrid")
|
| 144 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
FILE = "file" # 文件选择
|
| 25 |
FOLDER = "folder" # 文件夹选择
|
| 26 |
COMBO = "combo" # 下拉选择框
|
| 27 |
+
MULTI_SELECT = "multi_select" # 多选框
|
| 28 |
|
| 29 |
|
| 30 |
@dataclass
|
|
|
|
| 35 |
option_type: OptionType # 选项类型
|
| 36 |
default: Any = None # 默认值
|
| 37 |
description: str = "" # 描述说明
|
| 38 |
+
choices: List[Any] = field(default_factory=list) # 下拉/多选选项
|
| 39 |
min_value: Optional[float] = None # 最小值(仅NUMBER类型)
|
| 40 |
max_value: Optional[float] = None # 最大值(仅NUMBER类型)
|
| 41 |
+
step: Optional[float] = None # 步进值(仅NUMBER类型)
|
| 42 |
file_types: List[Tuple[str, str]] = field(default_factory=list) # 文件类型过滤
|
| 43 |
+
visible_when: Optional[Dict[str, Any]] = None # 条件显示规则
|
| 44 |
|
| 45 |
|
| 46 |
class ExportPlugin(ABC):
|
|
|
|
| 145 |
"slices_dir": os.path.join(source_dir, "slices"),
|
| 146 |
"textgrid_dir": os.path.join(source_dir, "textgrid")
|
| 147 |
}
|
| 148 |
+
|
| 149 |
+
def get_quality_scorer(
|
| 150 |
+
self,
|
| 151 |
+
enabled_metrics: Optional[List[str]] = None,
|
| 152 |
+
weights: Optional[Dict[str, float]] = None
|
| 153 |
+
):
|
| 154 |
+
"""
|
| 155 |
+
获取质量评分器实例
|
| 156 |
+
|
| 157 |
+
参数:
|
| 158 |
+
enabled_metrics: 启用的评分维度,如 ["duration", "rms", "f0"]
|
| 159 |
+
weights: 各维度权重
|
| 160 |
+
|
| 161 |
+
返回:
|
| 162 |
+
QualityScorer 实例
|
| 163 |
+
"""
|
| 164 |
+
from ..quality_scorer import QualityScorer
|
| 165 |
+
return QualityScorer(enabled_metrics=enabled_metrics, weights=weights)
|
| 166 |
+
|
| 167 |
+
def score_audio_quality(
|
| 168 |
+
self,
|
| 169 |
+
wav_path: str,
|
| 170 |
+
enabled_metrics: Optional[List[str]] = None,
|
| 171 |
+
weights: Optional[Dict[str, float]] = None
|
| 172 |
+
) -> Dict[str, float]:
|
| 173 |
+
"""
|
| 174 |
+
评估音频文件质量
|
| 175 |
+
|
| 176 |
+
参数:
|
| 177 |
+
wav_path: 音频文件路径
|
| 178 |
+
enabled_metrics: 启用的评分维度
|
| 179 |
+
weights: 各维度权重
|
| 180 |
+
|
| 181 |
+
返回:
|
| 182 |
+
包含各维度分数和综合分数的字典
|
| 183 |
+
"""
|
| 184 |
+
scorer = self.get_quality_scorer(enabled_metrics, weights)
|
| 185 |
+
return scorer.score_from_file(wav_path)
|
src/export_plugins/loader.py
CHANGED
|
@@ -13,13 +13,14 @@ from typing import Dict, List, Type
|
|
| 13 |
|
| 14 |
from .base import ExportPlugin
|
| 15 |
from .simple_export import SimpleExportPlugin
|
|
|
|
| 16 |
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
|
| 20 |
def get_builtin_plugins() -> List[Type[ExportPlugin]]:
|
| 21 |
"""获取内置插件列表"""
|
| 22 |
-
return [SimpleExportPlugin]
|
| 23 |
|
| 24 |
|
| 25 |
def load_plugins(plugins_dir: str = None) -> Dict[str, ExportPlugin]:
|
|
|
|
| 13 |
|
| 14 |
from .base import ExportPlugin
|
| 15 |
from .simple_export import SimpleExportPlugin
|
| 16 |
+
from .utau_oto_export import UTAUOtoExportPlugin
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
|
| 21 |
def get_builtin_plugins() -> List[Type[ExportPlugin]]:
|
| 22 |
"""获取内置插件列表"""
|
| 23 |
+
return [SimpleExportPlugin, UTAUOtoExportPlugin]
|
| 24 |
|
| 25 |
|
| 26 |
def load_plugins(plugins_dir: str = None) -> Dict[str, ExportPlugin]:
|
src/export_plugins/utau_oto_export.py
ADDED
|
@@ -0,0 +1,878 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
UTAU oto.ini 导出插件
|
| 4 |
+
|
| 5 |
+
从 TextGrid 提取音素时间边界,生成 UTAU 音源配置文件
|
| 6 |
+
一个 wav 文件可包含多条 oto 配置,无需裁剪音频
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import json
|
| 11 |
+
import glob
|
| 12 |
+
import shutil
|
| 13 |
+
import logging
|
| 14 |
+
from collections import defaultdict
|
| 15 |
+
from typing import Any, Dict, List, Optional, Tuple
|
| 16 |
+
|
| 17 |
+
from .base import ExportPlugin, PluginOption, OptionType
|
| 18 |
+
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ==================== IPA 音素分类 ====================
|
| 23 |
+
|
| 24 |
+
# 中文辅音(MFA 输出的 IPA 符号)
|
| 25 |
+
CHINESE_CONSONANTS = {
|
| 26 |
+
'p', 'pʰ', 'pʲ', 'b', 'm', 'f',
|
| 27 |
+
't', 'tʰ', 'd', 'n', 'l',
|
| 28 |
+
'k', 'kʰ', 'ɡ', 'g', 'ŋ', 'x', 'h',
|
| 29 |
+
'tɕ', 'tɕʰ', 'dʑ', 'ɕ', 'ʑ',
|
| 30 |
+
'ts', 'tsʰ', 'dz', 's', 'z',
|
| 31 |
+
'ʈʂ', 'ʈʂʰ', 'ɖʐ', 'ʂ', 'ʐ',
|
| 32 |
+
'ɲ', 'j', 'w', 'ɥ',
|
| 33 |
+
'ʔ', # 喉塞音
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# 中文元音(可能带声调标记)
|
| 37 |
+
CHINESE_VOWELS = {
|
| 38 |
+
'a', 'o', 'e', 'i', 'u', 'y', 'ü',
|
| 39 |
+
'ə', 'ɛ', 'ɔ', 'ɤ', 'ɨ', 'ʅ', 'ʉ',
|
| 40 |
+
'ai', 'ei', 'ao', 'ou',
|
| 41 |
+
'ia', 'ie', 'iu', 'iao', 'iou',
|
| 42 |
+
'ua', 'uo', 'ui', 'uai', 'uei',
|
| 43 |
+
'üe', 'üan', 'ün',
|
| 44 |
+
'an', 'en', 'in', 'un', 'ün',
|
| 45 |
+
'ang', 'eng', 'ing', 'ong',
|
| 46 |
+
'aw', 'ej', 'ow', # MFA 输出格式
|
| 47 |
+
'z̩', # 舌尖元音
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# 日语辅音
|
| 51 |
+
JAPANESE_CONSONANTS = {
|
| 52 |
+
'p', 'b', 'm', 'ɸ',
|
| 53 |
+
't', 'd', 'n', 's', 'z', 'ɾ', 'r',
|
| 54 |
+
'k', 'ɡ', 'g', 'ŋ', 'h',
|
| 55 |
+
'tɕ', 'dʑ', 'ɕ', 'ʑ',
|
| 56 |
+
'ts', 'dz',
|
| 57 |
+
'ɲ', 'j', 'w',
|
| 58 |
+
# 长辅音
|
| 59 |
+
'nː', 'sː', 'tː', 'kː', 'pː',
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# 日语元音
|
| 63 |
+
JAPANESE_VOWELS = {
|
| 64 |
+
'a', 'i', 'ɯ', 'u', 'e', 'o',
|
| 65 |
+
'aː', 'iː', 'ɯː', 'uː', 'eː', 'oː',
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
# 跳过的标记
|
| 69 |
+
SKIP_MARKS = {'', 'SP', 'AP', '<unk>', 'spn', 'sil'}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def is_consonant(phone: str, language: str) -> bool:
|
| 73 |
+
"""判断音素是否为辅音"""
|
| 74 |
+
base_phone = _strip_tone(phone)
|
| 75 |
+
|
| 76 |
+
if language in ('chinese', 'zh', 'mandarin'):
|
| 77 |
+
return base_phone in CHINESE_CONSONANTS
|
| 78 |
+
elif language in ('japanese', 'ja', 'jp'):
|
| 79 |
+
return base_phone in JAPANESE_CONSONANTS
|
| 80 |
+
return False
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def is_vowel(phone: str, language: str) -> bool:
|
| 84 |
+
"""判断音素是否为元音"""
|
| 85 |
+
base_phone = _strip_tone(phone)
|
| 86 |
+
|
| 87 |
+
if language in ('chinese', 'zh', 'mandarin'):
|
| 88 |
+
if base_phone in CHINESE_VOWELS:
|
| 89 |
+
return True
|
| 90 |
+
for v in ['a', 'o', 'e', 'i', 'u', 'y', 'ə', 'ɛ', 'ɔ', 'ɤ', 'ɨ', 'ʅ', 'ʉ']:
|
| 91 |
+
if base_phone.startswith(v):
|
| 92 |
+
return True
|
| 93 |
+
return False
|
| 94 |
+
elif language in ('japanese', 'ja', 'jp'):
|
| 95 |
+
return base_phone in JAPANESE_VOWELS or base_phone.rstrip('ː') in {'a', 'i', 'ɯ', 'u', 'e', 'o'}
|
| 96 |
+
return False
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _strip_tone(phone: str) -> str:
|
| 100 |
+
"""移除声调标记"""
|
| 101 |
+
tone_marks = '˥˦˧˨˩ˇˊˋ¯'
|
| 102 |
+
result = phone
|
| 103 |
+
for mark in tone_marks:
|
| 104 |
+
result = result.replace(mark, '')
|
| 105 |
+
return result
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ==================== IPA 到别名转换 ====================
|
| 109 |
+
|
| 110 |
+
# 中文 IPA 到拼音映射
|
| 111 |
+
CHINESE_IPA_TO_PINYIN = {
|
| 112 |
+
# 辅音
|
| 113 |
+
'p': 'b', 'pʰ': 'p', 'pʲ': 'p',
|
| 114 |
+
'm': 'm', 'f': 'f',
|
| 115 |
+
't': 'd', 'tʰ': 't',
|
| 116 |
+
'n': 'n', 'l': 'l',
|
| 117 |
+
'k': 'g', 'kʰ': 'k',
|
| 118 |
+
'x': 'h', 'h': 'h',
|
| 119 |
+
'tɕ': 'j', 'tɕʰ': 'q', 'ɕ': 'x',
|
| 120 |
+
'ts': 'z', 'tsʰ': 'c', 's': 's',
|
| 121 |
+
'ʈʂ': 'zh', 'ʈʂʰ': 'ch', 'ʂ': 'sh', 'ʐ': 'r',
|
| 122 |
+
'ɲ': 'n', 'ŋ': 'ng',
|
| 123 |
+
'j': 'y', 'w': 'w', 'ɥ': 'yu',
|
| 124 |
+
'ʔ': '',
|
| 125 |
+
# 元音
|
| 126 |
+
'a': 'a', 'o': 'o', 'e': 'e', 'i': 'i', 'u': 'u', 'y': 'v', 'ü': 'v',
|
| 127 |
+
'ə': 'e', 'ɛ': 'e', 'ɔ': 'o', 'ɤ': 'e',
|
| 128 |
+
'ai': 'ai', 'ei': 'ei', 'ao': 'ao', 'ou': 'ou',
|
| 129 |
+
'aw': 'ao', 'ej': 'ei', 'ow': 'ou',
|
| 130 |
+
'z̩': 'i',
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
# 日语 IPA 到罗马音映射
|
| 134 |
+
JAPANESE_IPA_TO_ROMAJI = {
|
| 135 |
+
# 辅音
|
| 136 |
+
'p': 'p', 'b': 'b', 'm': 'm', 'ɸ': 'f',
|
| 137 |
+
't': 't', 'd': 'd', 'n': 'n', 's': 's', 'z': 'z', 'ɾ': 'r', 'r': 'r',
|
| 138 |
+
'k': 'k', 'ɡ': 'g', 'g': 'g', 'h': 'h',
|
| 139 |
+
'tɕ': 'ch', 'dʑ': 'j', 'ɕ': 'sh', 'ʑ': 'j',
|
| 140 |
+
'ts': 'ts', 'dz': 'z',
|
| 141 |
+
'ɲ': 'ny', 'ŋ': 'ng', 'j': 'y', 'w': 'w',
|
| 142 |
+
# 长辅音(促音后)
|
| 143 |
+
'nː': 'n', 'sː': 's', 'tː': 't', 'kː': 'k', 'pː': 'p',
|
| 144 |
+
# 元音
|
| 145 |
+
'a': 'a', 'i': 'i', 'ɯ': 'u', 'u': 'u', 'e': 'e', 'o': 'o',
|
| 146 |
+
'aː': 'a', 'iː': 'i', 'ɯː': 'u', 'uː': 'u', 'eː': 'e', 'oː': 'o',
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
# 罗马音到平假名映射
|
| 150 |
+
ROMAJI_TO_HIRAGANA = {
|
| 151 |
+
# 基本元音
|
| 152 |
+
'a': 'あ', 'i': 'い', 'u': 'う', 'e': 'え', 'o': 'お',
|
| 153 |
+
# か行
|
| 154 |
+
'ka': 'か', 'ki': 'き', 'ku': 'く', 'ke': 'け', 'ko': 'こ',
|
| 155 |
+
# さ行
|
| 156 |
+
'sa': 'さ', 'shi': 'し', 'si': 'し', 'su': 'す', 'se': 'せ', 'so': 'そ',
|
| 157 |
+
# た行
|
| 158 |
+
'ta': 'た', 'chi': 'ち', 'ti': 'ち', 'tsu': 'つ', 'tu': 'つ', 'te': 'て', 'to': 'と',
|
| 159 |
+
# な行
|
| 160 |
+
'na': 'な', 'ni': 'に', 'nu': 'ぬ', 'ne': 'ね', 'no': 'の',
|
| 161 |
+
# は行
|
| 162 |
+
'ha': 'は', 'hi': 'ひ', 'fu': 'ふ', 'hu': 'ふ', 'he': 'へ', 'ho': 'ほ',
|
| 163 |
+
# ま行
|
| 164 |
+
'ma': 'ま', 'mi': 'み', 'mu': 'む', 'me': 'め', 'mo': 'も',
|
| 165 |
+
# や���
|
| 166 |
+
'ya': 'や', 'yu': 'ゆ', 'yo': 'よ',
|
| 167 |
+
# ら行
|
| 168 |
+
'ra': 'ら', 'ri': 'り', 'ru': 'る', 're': 'れ', 'ro': 'ろ',
|
| 169 |
+
# わ行
|
| 170 |
+
'wa': 'わ', 'wo': 'を', 'n': 'ん',
|
| 171 |
+
# が行
|
| 172 |
+
'ga': 'が', 'gi': 'ぎ', 'gu': 'ぐ', 'ge': 'げ', 'go': 'ご',
|
| 173 |
+
# ざ行
|
| 174 |
+
'za': 'ざ', 'ji': 'じ', 'zi': 'じ', 'zu': 'ず', 'ze': 'ぜ', 'zo': 'ぞ',
|
| 175 |
+
# だ行
|
| 176 |
+
'da': 'だ', 'di': 'ぢ', 'du': 'づ', 'de': 'で', 'do': 'ど',
|
| 177 |
+
# ば行
|
| 178 |
+
'ba': 'ば', 'bi': 'び', 'bu': 'ぶ', 'be': 'べ', 'bo': 'ぼ',
|
| 179 |
+
# ぱ行
|
| 180 |
+
'pa': 'ぱ', 'pi': 'ぴ', 'pu': 'ぷ', 'pe': 'ぺ', 'po': 'ぽ',
|
| 181 |
+
# 拗音
|
| 182 |
+
'kya': 'きゃ', 'kyu': 'きゅ', 'kyo': 'きょ',
|
| 183 |
+
'sha': 'しゃ', 'shu': 'しゅ', 'sho': 'しょ',
|
| 184 |
+
'cha': 'ちゃ', 'chu': 'ちゅ', 'cho': 'ちょ',
|
| 185 |
+
'nya': 'にゃ', 'nyu': 'にゅ', 'nyo': 'にょ',
|
| 186 |
+
'hya': 'ひゃ', 'hyu': 'ひゅ', 'hyo': 'ひょ',
|
| 187 |
+
'mya': 'みゃ', 'myu': 'みゅ', 'myo': 'みょ',
|
| 188 |
+
'rya': 'りゃ', 'ryu': 'りゅ', 'ryo': 'りょ',
|
| 189 |
+
'gya': 'ぎゃ', 'gyu': 'ぎゅ', 'gyo': 'ぎょ',
|
| 190 |
+
'ja': 'じゃ', 'ju': 'じゅ', 'jo': 'じょ',
|
| 191 |
+
'bya': 'びゃ', 'byu': 'びゅ', 'byo': 'びょ',
|
| 192 |
+
'pya': 'ぴゃ', 'pyu': 'ぴゅ', 'pyo': 'ぴょ',
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def ipa_to_alias(consonant: Optional[str], vowel: Optional[str], language: str, use_hiragana: bool = False) -> Optional[str]:
|
| 197 |
+
"""将 IPA 音素转换为别名"""
|
| 198 |
+
c_base = _strip_tone(consonant) if consonant else ''
|
| 199 |
+
v_base = _strip_tone(vowel) if vowel else ''
|
| 200 |
+
|
| 201 |
+
if language in ('chinese', 'zh', 'mandarin'):
|
| 202 |
+
c_alias = CHINESE_IPA_TO_PINYIN.get(c_base, c_base)
|
| 203 |
+
v_alias = CHINESE_IPA_TO_PINYIN.get(v_base, v_base)
|
| 204 |
+
alias = (c_alias or '') + (v_alias or '')
|
| 205 |
+
# 清理非 ASCII 字符
|
| 206 |
+
alias = ''.join(c for c in alias if c.isascii() and (c.isalnum() or c == '_'))
|
| 207 |
+
return alias.lower() if alias else None
|
| 208 |
+
else:
|
| 209 |
+
# 日语
|
| 210 |
+
c_alias = JAPANESE_IPA_TO_ROMAJI.get(c_base, c_base)
|
| 211 |
+
v_alias = JAPANESE_IPA_TO_ROMAJI.get(v_base, v_base)
|
| 212 |
+
romaji = (c_alias or '') + (v_alias or '')
|
| 213 |
+
# 清理非 ASCII
|
| 214 |
+
romaji = ''.join(c for c in romaji if c.isascii() and (c.isalnum() or c == '_'))
|
| 215 |
+
romaji = romaji.lower()
|
| 216 |
+
|
| 217 |
+
if not romaji:
|
| 218 |
+
return None
|
| 219 |
+
|
| 220 |
+
if use_hiragana:
|
| 221 |
+
# 尝试转换为平假名
|
| 222 |
+
return ROMAJI_TO_HIRAGANA.get(romaji, romaji)
|
| 223 |
+
return romaji
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
class UTAUOtoExportPlugin(ExportPlugin):
|
| 227 |
+
"""UTAU oto.ini 导出插件"""
|
| 228 |
+
|
| 229 |
+
name = "UTAU oto.ini 导出"
|
| 230 |
+
description = "从 TextGrid 生成 UTAU 音源配置文件,一个 wav 可包含多条配置"
|
| 231 |
+
version = "1.1.0"
|
| 232 |
+
author = "内置"
|
| 233 |
+
|
| 234 |
+
def get_options(self) -> List[PluginOption]:
|
| 235 |
+
return [
|
| 236 |
+
PluginOption(
|
| 237 |
+
key="info",
|
| 238 |
+
label="从 TextGrid phones 层提取音素,生成 oto.ini(音频不裁剪)",
|
| 239 |
+
option_type=OptionType.LABEL
|
| 240 |
+
),
|
| 241 |
+
PluginOption(
|
| 242 |
+
key="max_samples",
|
| 243 |
+
label="每个别名最大样本数",
|
| 244 |
+
option_type=OptionType.NUMBER,
|
| 245 |
+
default=5,
|
| 246 |
+
min_value=1,
|
| 247 |
+
max_value=100,
|
| 248 |
+
description="同一别名保留的最大条目数"
|
| 249 |
+
),
|
| 250 |
+
PluginOption(
|
| 251 |
+
key="quality_metrics",
|
| 252 |
+
label="质量评估维度",
|
| 253 |
+
option_type=OptionType.COMBO,
|
| 254 |
+
default="duration+rms",
|
| 255 |
+
choices=["duration", "duration+rms", "duration+f0", "all"],
|
| 256 |
+
description="duration=仅时长, +rms=音量稳定性, +f0=音高稳定性。选择 all 可能耗时较长"
|
| 257 |
+
),
|
| 258 |
+
PluginOption(
|
| 259 |
+
key="naming_rule",
|
| 260 |
+
label="别名命名规则",
|
| 261 |
+
option_type=OptionType.TEXT,
|
| 262 |
+
default="%p%%n%",
|
| 263 |
+
description="变量: %p%=拼音/罗马音, %n%=序号。示例: %p%_%n% → ba_1"
|
| 264 |
+
),
|
| 265 |
+
PluginOption(
|
| 266 |
+
key="first_naming_rule",
|
| 267 |
+
label="首个样本命名规则",
|
| 268 |
+
option_type=OptionType.TEXT,
|
| 269 |
+
default="%p%",
|
| 270 |
+
description="第0个样本的特殊规则,留空则使用通用规则。示例: %p% → ba"
|
| 271 |
+
),
|
| 272 |
+
PluginOption(
|
| 273 |
+
key="alias_style",
|
| 274 |
+
label="别名风格(日语)",
|
| 275 |
+
option_type=OptionType.COMBO,
|
| 276 |
+
default="hiragana",
|
| 277 |
+
choices=["romaji", "hiragana"],
|
| 278 |
+
description="日语音源的别名格式:罗马音或平假名"
|
| 279 |
+
),
|
| 280 |
+
PluginOption(
|
| 281 |
+
key="overlap_ratio",
|
| 282 |
+
label="Overlap 比例",
|
| 283 |
+
option_type=OptionType.NUMBER,
|
| 284 |
+
default=0.3,
|
| 285 |
+
min_value=0.1,
|
| 286 |
+
max_value=0.5,
|
| 287 |
+
description="Overlap = Preutterance × 此比例"
|
| 288 |
+
),
|
| 289 |
+
PluginOption(
|
| 290 |
+
key="encoding",
|
| 291 |
+
label="文件编码",
|
| 292 |
+
option_type=OptionType.COMBO,
|
| 293 |
+
default="shift_jis",
|
| 294 |
+
choices=["shift_jis", "utf-8", "gbk"],
|
| 295 |
+
description="oto.ini 和 character.txt 编码(UTAU 标准为 Shift_JIS)"
|
| 296 |
+
),
|
| 297 |
+
PluginOption(
|
| 298 |
+
key="sanitize_filename",
|
| 299 |
+
label="文件名转拼音",
|
| 300 |
+
option_type=OptionType.SWITCH,
|
| 301 |
+
default=False,
|
| 302 |
+
description="将中文文件名转为拼音,清理特殊字符,防止 UTAU 识别故障"
|
| 303 |
+
),
|
| 304 |
+
]
|
| 305 |
+
|
| 306 |
+
def export(
|
| 307 |
+
self,
|
| 308 |
+
source_name: str,
|
| 309 |
+
bank_dir: str,
|
| 310 |
+
options: Dict[str, Any]
|
| 311 |
+
) -> Tuple[bool, str]:
|
| 312 |
+
"""执行 UTAU oto.ini 导出"""
|
| 313 |
+
try:
|
| 314 |
+
# 加载语言设置
|
| 315 |
+
language = self._load_language_from_meta(bank_dir, source_name)
|
| 316 |
+
|
| 317 |
+
# 获取选项
|
| 318 |
+
max_samples = int(options.get("max_samples", 5))
|
| 319 |
+
quality_metrics = options.get("quality_metrics", "duration")
|
| 320 |
+
naming_rule = options.get("naming_rule", "%p%%n%")
|
| 321 |
+
first_naming_rule = options.get("first_naming_rule", "%p%")
|
| 322 |
+
alias_style = options.get("alias_style", "romaji")
|
| 323 |
+
overlap_ratio = float(options.get("overlap_ratio", 0.3))
|
| 324 |
+
encoding = options.get("encoding", "utf-8")
|
| 325 |
+
sanitize_filename = options.get("sanitize_filename", False)
|
| 326 |
+
use_hiragana = (alias_style == "hiragana") and language in ('japanese', 'ja', 'jp')
|
| 327 |
+
|
| 328 |
+
# 解析质量评估维度
|
| 329 |
+
enabled_metrics = self._parse_quality_metrics(quality_metrics)
|
| 330 |
+
|
| 331 |
+
paths = self.get_source_paths(bank_dir, source_name)
|
| 332 |
+
export_dir = self.get_export_dir(bank_dir, source_name, "utau_oto")
|
| 333 |
+
|
| 334 |
+
os.makedirs(export_dir, exist_ok=True)
|
| 335 |
+
|
| 336 |
+
# 步骤1: 解析 TextGrid 并生成 oto 条目
|
| 337 |
+
self._log("【解析 TextGrid 文件】")
|
| 338 |
+
oto_entries, wav_files = self._parse_textgrids(
|
| 339 |
+
paths["slices_dir"],
|
| 340 |
+
paths["textgrid_dir"],
|
| 341 |
+
language,
|
| 342 |
+
use_hiragana,
|
| 343 |
+
overlap_ratio
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
if not oto_entries:
|
| 347 |
+
return False, "未能从 TextGrid 提取有效音素"
|
| 348 |
+
|
| 349 |
+
self._log(f"提取到 {len(oto_entries)} 条原始 oto 配置")
|
| 350 |
+
|
| 351 |
+
# 步骤2: 按别名分组并限制数量,添加编号
|
| 352 |
+
self._log(f"\n【筛选最佳样本】评估维度: {enabled_metrics}")
|
| 353 |
+
filtered_entries, used_wavs = self._filter_by_alias(
|
| 354 |
+
oto_entries, max_samples, naming_rule, first_naming_rule,
|
| 355 |
+
paths["slices_dir"], enabled_metrics
|
| 356 |
+
)
|
| 357 |
+
self._log(f"筛选后保留 {len(filtered_entries)} 条配置,涉及 {len(used_wavs)} 个音频文件")
|
| 358 |
+
|
| 359 |
+
# 步骤3: 复制音频文件(可选文件名转拼音)
|
| 360 |
+
self._log("\n【复制音频文件】")
|
| 361 |
+
if sanitize_filename:
|
| 362 |
+
self._log("已启用文件名转拼音")
|
| 363 |
+
copied, filename_map = self._copy_wav_files(
|
| 364 |
+
used_wavs, paths["slices_dir"], export_dir, sanitize_filename
|
| 365 |
+
)
|
| 366 |
+
self._log(f"复制了 {copied} 个音频文件")
|
| 367 |
+
|
| 368 |
+
# 步骤4: 写入 oto.ini
|
| 369 |
+
self._log("\n【生成 oto.ini】")
|
| 370 |
+
oto_path = os.path.join(export_dir, "oto.ini")
|
| 371 |
+
self._write_oto_ini(filtered_entries, oto_path, encoding, filename_map)
|
| 372 |
+
self._log(f"写入: {oto_path}")
|
| 373 |
+
|
| 374 |
+
# 步骤5: 写入 character.txt
|
| 375 |
+
self._log("\n【生成 character.txt】")
|
| 376 |
+
char_path = os.path.join(export_dir, "character.txt")
|
| 377 |
+
self._write_character_txt(source_name, char_path, encoding)
|
| 378 |
+
self._log(f"写入: {char_path}")
|
| 379 |
+
|
| 380 |
+
# 统计别名数量
|
| 381 |
+
unique_aliases = set(e["alias"] for e in filtered_entries)
|
| 382 |
+
return True, f"导出完成: {export_dir}\n{len(unique_aliases)} 个别名,{len(filtered_entries)} 条配置,{copied} 个音频"
|
| 383 |
+
|
| 384 |
+
except Exception as e:
|
| 385 |
+
logger.error(f"UTAU oto.ini 导出失败: {e}", exc_info=True)
|
| 386 |
+
return False, str(e)
|
| 387 |
+
|
| 388 |
+
def _parse_quality_metrics(self, metrics_str: str) -> List[str]:
|
| 389 |
+
"""解析质量评估维度选项"""
|
| 390 |
+
if metrics_str == "all":
|
| 391 |
+
return ["duration", "rms", "f0"]
|
| 392 |
+
elif metrics_str == "duration+rms":
|
| 393 |
+
return ["duration", "rms"]
|
| 394 |
+
elif metrics_str == "duration+f0":
|
| 395 |
+
return ["duration", "f0"]
|
| 396 |
+
else:
|
| 397 |
+
return ["duration"]
|
| 398 |
+
|
| 399 |
+
def _load_language_from_meta(self, bank_dir: str, source_name: str) -> str:
|
| 400 |
+
"""从 meta.json 加载���言设置"""
|
| 401 |
+
meta_path = os.path.join(bank_dir, source_name, "meta.json")
|
| 402 |
+
try:
|
| 403 |
+
if os.path.exists(meta_path):
|
| 404 |
+
with open(meta_path, 'r', encoding='utf-8') as f:
|
| 405 |
+
meta = json.load(f)
|
| 406 |
+
language = meta.get("language", "chinese")
|
| 407 |
+
self._log(f"语言设置: {language}")
|
| 408 |
+
return language
|
| 409 |
+
except Exception as e:
|
| 410 |
+
logger.warning(f"读取 meta.json 失败: {e}")
|
| 411 |
+
return "chinese"
|
| 412 |
+
|
| 413 |
+
def _parse_textgrids(
|
| 414 |
+
self,
|
| 415 |
+
slices_dir: str,
|
| 416 |
+
textgrid_dir: str,
|
| 417 |
+
language: str,
|
| 418 |
+
use_hiragana: bool,
|
| 419 |
+
overlap_ratio: float
|
| 420 |
+
) -> Tuple[List[Dict], set]:
|
| 421 |
+
"""解析 TextGrid 文件,提取音素边界"""
|
| 422 |
+
import textgrid
|
| 423 |
+
import soundfile as sf
|
| 424 |
+
|
| 425 |
+
tg_files = glob.glob(os.path.join(textgrid_dir, '*.TextGrid'))
|
| 426 |
+
if not tg_files:
|
| 427 |
+
self._log("未找到 TextGrid 文件")
|
| 428 |
+
return [], set()
|
| 429 |
+
|
| 430 |
+
self._log(f"处理 {len(tg_files)} 个 TextGrid 文件")
|
| 431 |
+
|
| 432 |
+
oto_entries = []
|
| 433 |
+
wav_files = set()
|
| 434 |
+
|
| 435 |
+
for tg_path in tg_files:
|
| 436 |
+
basename = os.path.basename(tg_path).replace('.TextGrid', '')
|
| 437 |
+
wav_name = basename + '.wav'
|
| 438 |
+
wav_path = os.path.join(slices_dir, wav_name)
|
| 439 |
+
|
| 440 |
+
if not os.path.exists(wav_path):
|
| 441 |
+
continue
|
| 442 |
+
|
| 443 |
+
try:
|
| 444 |
+
info = sf.info(wav_path)
|
| 445 |
+
wav_duration_ms = info.duration * 1000
|
| 446 |
+
except Exception:
|
| 447 |
+
continue
|
| 448 |
+
|
| 449 |
+
wav_files.add(wav_name)
|
| 450 |
+
|
| 451 |
+
try:
|
| 452 |
+
tg = textgrid.TextGrid.fromFile(tg_path)
|
| 453 |
+
except Exception:
|
| 454 |
+
continue
|
| 455 |
+
|
| 456 |
+
# 查找 words 层和 phones 层
|
| 457 |
+
words_tier = None
|
| 458 |
+
phones_tier = None
|
| 459 |
+
for tier in tg:
|
| 460 |
+
name_lower = tier.name.lower()
|
| 461 |
+
if name_lower in ('words', 'word'):
|
| 462 |
+
words_tier = tier
|
| 463 |
+
elif name_lower in ('phones', 'phone'):
|
| 464 |
+
phones_tier = tier
|
| 465 |
+
|
| 466 |
+
# 如果没找到,按顺序取
|
| 467 |
+
if words_tier is None and len(tg) >= 1:
|
| 468 |
+
words_tier = tg[0]
|
| 469 |
+
if phones_tier is None and len(tg) >= 2:
|
| 470 |
+
phones_tier = tg[1]
|
| 471 |
+
|
| 472 |
+
if phones_tier is None:
|
| 473 |
+
continue
|
| 474 |
+
|
| 475 |
+
# 提取音素对,使用 words 层限制配对范围
|
| 476 |
+
entries = self._extract_cv_pairs(
|
| 477 |
+
words_tier, phones_tier, wav_name, wav_duration_ms,
|
| 478 |
+
language, use_hiragana, overlap_ratio
|
| 479 |
+
)
|
| 480 |
+
oto_entries.extend(entries)
|
| 481 |
+
|
| 482 |
+
return oto_entries, wav_files
|
| 483 |
+
|
| 484 |
+
def _extract_cv_pairs(
|
| 485 |
+
self,
|
| 486 |
+
words_tier,
|
| 487 |
+
phones_tier,
|
| 488 |
+
wav_name: str,
|
| 489 |
+
wav_duration_ms: float,
|
| 490 |
+
language: str,
|
| 491 |
+
use_hiragana: bool,
|
| 492 |
+
overlap_ratio: float
|
| 493 |
+
) -> List[Dict]:
|
| 494 |
+
"""
|
| 495 |
+
从 phones 层提取辅音+元音对
|
| 496 |
+
使用 words 层限制配对范围,确保辅音和元音属于同一个字
|
| 497 |
+
"""
|
| 498 |
+
entries = []
|
| 499 |
+
|
| 500 |
+
# 构建 word 时间范围列表
|
| 501 |
+
word_ranges = []
|
| 502 |
+
if words_tier:
|
| 503 |
+
for interval in words_tier:
|
| 504 |
+
text = interval.mark.strip()
|
| 505 |
+
if text and text not in SKIP_MARKS:
|
| 506 |
+
word_ranges.append((interval.minTime, interval.maxTime))
|
| 507 |
+
|
| 508 |
+
def get_word_range(time: float) -> Optional[Tuple[float, float]]:
|
| 509 |
+
"""获取某时间点所属的 word 范围"""
|
| 510 |
+
for start, end in word_ranges:
|
| 511 |
+
if start <= time < end:
|
| 512 |
+
return (start, end)
|
| 513 |
+
return None
|
| 514 |
+
|
| 515 |
+
def same_word(time1: float, time2: float) -> bool:
|
| 516 |
+
"""判断两个时间点是否在同一个 word 内"""
|
| 517 |
+
if not word_ranges:
|
| 518 |
+
return True # 没有 words 层时不限制
|
| 519 |
+
range1 = get_word_range(time1)
|
| 520 |
+
range2 = get_word_range(time2)
|
| 521 |
+
return range1 is not None and range1 == range2
|
| 522 |
+
|
| 523 |
+
intervals = list(phones_tier)
|
| 524 |
+
i = 0
|
| 525 |
+
|
| 526 |
+
while i < len(intervals):
|
| 527 |
+
interval = intervals[i]
|
| 528 |
+
phone = interval.mark.strip()
|
| 529 |
+
|
| 530 |
+
if phone in SKIP_MARKS:
|
| 531 |
+
i += 1
|
| 532 |
+
continue
|
| 533 |
+
|
| 534 |
+
start_ms = interval.minTime * 1000
|
| 535 |
+
end_ms = interval.maxTime * 1000
|
| 536 |
+
|
| 537 |
+
if is_consonant(phone, language):
|
| 538 |
+
consonant = phone
|
| 539 |
+
consonant_start = start_ms
|
| 540 |
+
consonant_end = end_ms
|
| 541 |
+
consonant_time = interval.minTime # 用于判断所属 word
|
| 542 |
+
|
| 543 |
+
vowel = None
|
| 544 |
+
vowel_end = end_ms
|
| 545 |
+
|
| 546 |
+
# 检查下一个音素是否是元音,且在同一个 word 内
|
| 547 |
+
if i + 1 < len(intervals):
|
| 548 |
+
next_interval = intervals[i + 1]
|
| 549 |
+
next_phone = next_interval.mark.strip()
|
| 550 |
+
next_time = next_interval.minTime
|
| 551 |
+
|
| 552 |
+
if (next_phone not in SKIP_MARKS and
|
| 553 |
+
is_vowel(next_phone, language) and
|
| 554 |
+
same_word(consonant_time, next_time)):
|
| 555 |
+
vowel = next_phone
|
| 556 |
+
vowel_end = next_interval.maxTime * 1000
|
| 557 |
+
i += 1
|
| 558 |
+
|
| 559 |
+
alias = ipa_to_alias(consonant, vowel, language, use_hiragana)
|
| 560 |
+
if not alias:
|
| 561 |
+
i += 1
|
| 562 |
+
continue
|
| 563 |
+
|
| 564 |
+
consonant_duration = consonant_end - consonant_start
|
| 565 |
+
|
| 566 |
+
entry = self._calculate_oto_params(
|
| 567 |
+
wav_name=wav_name,
|
| 568 |
+
alias=alias,
|
| 569 |
+
offset=consonant_start,
|
| 570 |
+
consonant_duration=consonant_duration,
|
| 571 |
+
segment_end=vowel_end,
|
| 572 |
+
wav_duration_ms=wav_duration_ms,
|
| 573 |
+
overlap_ratio=overlap_ratio
|
| 574 |
+
)
|
| 575 |
+
entries.append(entry)
|
| 576 |
+
|
| 577 |
+
elif is_vowel(phone, language):
|
| 578 |
+
alias = ipa_to_alias(None, phone, language, use_hiragana)
|
| 579 |
+
if not alias:
|
| 580 |
+
i += 1
|
| 581 |
+
continue
|
| 582 |
+
|
| 583 |
+
entry = self._calculate_oto_params(
|
| 584 |
+
wav_name=wav_name,
|
| 585 |
+
alias=alias,
|
| 586 |
+
offset=start_ms,
|
| 587 |
+
consonant_duration=min(30, (end_ms - start_ms) * 0.2),
|
| 588 |
+
segment_end=end_ms,
|
| 589 |
+
wav_duration_ms=wav_duration_ms,
|
| 590 |
+
overlap_ratio=overlap_ratio
|
| 591 |
+
)
|
| 592 |
+
entries.append(entry)
|
| 593 |
+
|
| 594 |
+
i += 1
|
| 595 |
+
|
| 596 |
+
return entries
|
| 597 |
+
|
| 598 |
+
def _calculate_oto_params(
|
| 599 |
+
self,
|
| 600 |
+
wav_name: str,
|
| 601 |
+
alias: str,
|
| 602 |
+
offset: float,
|
| 603 |
+
consonant_duration: float,
|
| 604 |
+
segment_end: float,
|
| 605 |
+
wav_duration_ms: float,
|
| 606 |
+
overlap_ratio: float
|
| 607 |
+
) -> Dict:
|
| 608 |
+
"""
|
| 609 |
+
计算 oto.ini 参数
|
| 610 |
+
|
| 611 |
+
oto.ini 格式: wav=alias,offset,consonant,cutoff,preutterance,overlap
|
| 612 |
+
|
| 613 |
+
- offset: 从音频开头跳过的毫秒数
|
| 614 |
+
- consonant: 不被拉伸的区域长度
|
| 615 |
+
- cutoff: 负值,表示这个音素的总时长(从 offset 开始)
|
| 616 |
+
- preutterance: 先行发声
|
| 617 |
+
- overlap: 与前一音符的交叉淡化区域
|
| 618 |
+
"""
|
| 619 |
+
segment_duration = segment_end - offset
|
| 620 |
+
preutterance = consonant_duration
|
| 621 |
+
overlap = preutterance * overlap_ratio
|
| 622 |
+
|
| 623 |
+
# cutoff 为负值,表示音素的总时长
|
| 624 |
+
cutoff = -segment_duration
|
| 625 |
+
|
| 626 |
+
return {
|
| 627 |
+
"wav_name": wav_name,
|
| 628 |
+
"alias": alias,
|
| 629 |
+
"offset": round(offset, 1),
|
| 630 |
+
"consonant": round(consonant_duration, 1),
|
| 631 |
+
"cutoff": round(cutoff, 1),
|
| 632 |
+
"preutterance": round(preutterance, 1),
|
| 633 |
+
"overlap": round(overlap, 1),
|
| 634 |
+
"segment_duration": segment_duration, # 用于排序
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
+
def _filter_by_alias(
|
| 638 |
+
self,
|
| 639 |
+
entries: List[Dict],
|
| 640 |
+
max_samples: int,
|
| 641 |
+
naming_rule: str,
|
| 642 |
+
first_naming_rule: str,
|
| 643 |
+
slices_dir: str,
|
| 644 |
+
enabled_metrics: List[str]
|
| 645 |
+
) -> Tuple[List[Dict], set]:
|
| 646 |
+
"""按别名分组,使用质量评分筛选最佳样本,并添加编号"""
|
| 647 |
+
# 过滤空别名
|
| 648 |
+
valid_entries = [e for e in entries if e.get("alias") and e["alias"].strip()]
|
| 649 |
+
|
| 650 |
+
# 按基础别名分组
|
| 651 |
+
alias_groups: Dict[str, List[Dict]] = defaultdict(list)
|
| 652 |
+
for entry in valid_entries:
|
| 653 |
+
alias_groups[entry["alias"]].append(entry)
|
| 654 |
+
|
| 655 |
+
# 判断是否需要加载音频计算质量分数
|
| 656 |
+
need_audio_scoring = any(m in enabled_metrics for m in ["rms", "f0"])
|
| 657 |
+
|
| 658 |
+
filtered = []
|
| 659 |
+
used_wavs = set()
|
| 660 |
+
|
| 661 |
+
for base_alias, group in alias_groups.items():
|
| 662 |
+
# 计算质量分数
|
| 663 |
+
if need_audio_scoring:
|
| 664 |
+
scored_group = self._score_entries(group, slices_dir, enabled_metrics)
|
| 665 |
+
else:
|
| 666 |
+
# 仅使用时长评分
|
| 667 |
+
from ..quality_scorer import duration_score
|
| 668 |
+
for entry in group:
|
| 669 |
+
duration = entry["segment_duration"] / 1000 # 转换为秒
|
| 670 |
+
entry["quality_score"] = duration_score(duration)
|
| 671 |
+
scored_group = group
|
| 672 |
+
|
| 673 |
+
# 按质量分数排序(降序)
|
| 674 |
+
sorted_group = sorted(scored_group, key=lambda x: -x.get("quality_score", 0))
|
| 675 |
+
|
| 676 |
+
# 保留前 N 个,并应用命名规则
|
| 677 |
+
for idx, entry in enumerate(sorted_group[:max_samples]):
|
| 678 |
+
# 生成带编号的别名
|
| 679 |
+
if idx == 0 and first_naming_rule:
|
| 680 |
+
final_alias = self._apply_naming_rule(first_naming_rule, base_alias, idx)
|
| 681 |
+
else:
|
| 682 |
+
final_alias = self._apply_naming_rule(naming_rule, base_alias, idx)
|
| 683 |
+
|
| 684 |
+
entry["alias"] = final_alias
|
| 685 |
+
filtered.append(entry)
|
| 686 |
+
used_wavs.add(entry["wav_name"])
|
| 687 |
+
|
| 688 |
+
return filtered, used_wavs
|
| 689 |
+
|
| 690 |
+
def _score_entries(
|
| 691 |
+
self,
|
| 692 |
+
entries: List[Dict],
|
| 693 |
+
slices_dir: str,
|
| 694 |
+
enabled_metrics: List[str]
|
| 695 |
+
) -> List[Dict]:
|
| 696 |
+
"""为条目计算质量分数"""
|
| 697 |
+
import soundfile as sf
|
| 698 |
+
from ..quality_scorer import QualityScorer
|
| 699 |
+
|
| 700 |
+
scorer = QualityScorer(enabled_metrics=enabled_metrics)
|
| 701 |
+
|
| 702 |
+
# 缓存已加载的音频
|
| 703 |
+
audio_cache: Dict[str, Tuple] = {}
|
| 704 |
+
|
| 705 |
+
for entry in entries:
|
| 706 |
+
wav_name = entry["wav_name"]
|
| 707 |
+
wav_path = os.path.join(slices_dir, wav_name)
|
| 708 |
+
|
| 709 |
+
try:
|
| 710 |
+
# 加载或使用缓存的音频
|
| 711 |
+
if wav_name not in audio_cache:
|
| 712 |
+
audio, sr = sf.read(wav_path)
|
| 713 |
+
if len(audio.shape) > 1:
|
| 714 |
+
audio = audio.mean(axis=1)
|
| 715 |
+
audio_cache[wav_name] = (audio, sr)
|
| 716 |
+
else:
|
| 717 |
+
audio, sr = audio_cache[wav_name]
|
| 718 |
+
|
| 719 |
+
# 提取片段(根据 offset 和 segment_duration)
|
| 720 |
+
offset_samples = int(entry["offset"] / 1000 * sr)
|
| 721 |
+
duration_samples = int(entry["segment_duration"] / 1000 * sr)
|
| 722 |
+
segment = audio[offset_samples:offset_samples + duration_samples]
|
| 723 |
+
|
| 724 |
+
if len(segment) > 0:
|
| 725 |
+
scores = scorer.score(segment, sr)
|
| 726 |
+
entry["quality_score"] = scores.get("combined", 0.5)
|
| 727 |
+
else:
|
| 728 |
+
entry["quality_score"] = 0.5
|
| 729 |
+
|
| 730 |
+
except Exception as e:
|
| 731 |
+
logger.warning(f"评分失败 {wav_name}: {e}")
|
| 732 |
+
entry["quality_score"] = 0.5
|
| 733 |
+
|
| 734 |
+
return entries
|
| 735 |
+
|
| 736 |
+
def _apply_naming_rule(self, rule: str, base_alias: str, index: int) -> str:
|
| 737 |
+
"""应用命名规则生成别名"""
|
| 738 |
+
return rule.replace("%p%", base_alias).replace("%n%", str(index))
|
| 739 |
+
|
| 740 |
+
def _copy_wav_files(
|
| 741 |
+
self,
|
| 742 |
+
wav_files: set,
|
| 743 |
+
slices_dir: str,
|
| 744 |
+
export_dir: str,
|
| 745 |
+
sanitize: bool = False
|
| 746 |
+
) -> Tuple[int, Dict[str, str]]:
|
| 747 |
+
"""
|
| 748 |
+
复制音频文件到导出目录
|
| 749 |
+
|
| 750 |
+
参数:
|
| 751 |
+
wav_files: 需要复制的文件名集合
|
| 752 |
+
slices_dir: 源目录
|
| 753 |
+
export_dir: 目标目录
|
| 754 |
+
sanitize: 是否对文件名进行转拼音和清理
|
| 755 |
+
|
| 756 |
+
返回:
|
| 757 |
+
(复制数量, 文件名映射表 {原文件名: 新文件名})
|
| 758 |
+
"""
|
| 759 |
+
copied = 0
|
| 760 |
+
filename_map: Dict[str, str] = {}
|
| 761 |
+
used_names: set = set()
|
| 762 |
+
|
| 763 |
+
for wav_name in wav_files:
|
| 764 |
+
src = os.path.join(slices_dir, wav_name)
|
| 765 |
+
if not os.path.exists(src):
|
| 766 |
+
continue
|
| 767 |
+
|
| 768 |
+
if sanitize:
|
| 769 |
+
new_name = self._sanitize_filename(wav_name, used_names)
|
| 770 |
+
used_names.add(new_name)
|
| 771 |
+
else:
|
| 772 |
+
new_name = wav_name
|
| 773 |
+
|
| 774 |
+
filename_map[wav_name] = new_name
|
| 775 |
+
dst = os.path.join(export_dir, new_name)
|
| 776 |
+
shutil.copyfile(src, dst)
|
| 777 |
+
copied += 1
|
| 778 |
+
|
| 779 |
+
return copied, filename_map
|
| 780 |
+
|
| 781 |
+
def _sanitize_filename(self, filename: str, used_names: set) -> str:
|
| 782 |
+
"""
|
| 783 |
+
清理文件名:中文转拼音 + 特殊字符清理 + 防冲突
|
| 784 |
+
|
| 785 |
+
参数:
|
| 786 |
+
filename: 原文件名
|
| 787 |
+
used_names: 已使用的文件名集合(用于防冲突)
|
| 788 |
+
|
| 789 |
+
返回:
|
| 790 |
+
清理后的文件名
|
| 791 |
+
"""
|
| 792 |
+
from pypinyin import lazy_pinyin
|
| 793 |
+
import re
|
| 794 |
+
|
| 795 |
+
# 分离文件名和扩展名
|
| 796 |
+
name, ext = os.path.splitext(filename)
|
| 797 |
+
|
| 798 |
+
# 中文转拼音
|
| 799 |
+
pinyin_parts = lazy_pinyin(name)
|
| 800 |
+
sanitized = ''.join(pinyin_parts)
|
| 801 |
+
|
| 802 |
+
# 清理特殊字符,只保留字母、数字、下划线、连字符
|
| 803 |
+
sanitized = re.sub(r'[^a-zA-Z0-9_\-]', '_', sanitized)
|
| 804 |
+
|
| 805 |
+
# 合并连续下划线
|
| 806 |
+
sanitized = re.sub(r'_+', '_', sanitized)
|
| 807 |
+
|
| 808 |
+
# 去除首尾下划线
|
| 809 |
+
sanitized = sanitized.strip('_')
|
| 810 |
+
|
| 811 |
+
# 如果为空,使用默认名
|
| 812 |
+
if not sanitized:
|
| 813 |
+
sanitized = 'audio'
|
| 814 |
+
|
| 815 |
+
# 防冲突:添加数字后缀
|
| 816 |
+
base_name = sanitized
|
| 817 |
+
counter = 1
|
| 818 |
+
while f"{sanitized}{ext}" in used_names:
|
| 819 |
+
sanitized = f"{base_name}_{counter}"
|
| 820 |
+
counter += 1
|
| 821 |
+
|
| 822 |
+
return f"{sanitized}{ext}"
|
| 823 |
+
|
| 824 |
+
def _write_oto_ini(
|
| 825 |
+
self,
|
| 826 |
+
entries: List[Dict],
|
| 827 |
+
output_path: str,
|
| 828 |
+
encoding: str,
|
| 829 |
+
filename_map: Optional[Dict[str, str]] = None
|
| 830 |
+
):
|
| 831 |
+
"""
|
| 832 |
+
写入 oto.ini 文件
|
| 833 |
+
|
| 834 |
+
参数:
|
| 835 |
+
entries: oto 条目列表
|
| 836 |
+
output_path: 输出路径
|
| 837 |
+
encoding: 文件编码
|
| 838 |
+
filename_map: 文件名映射表(原文件名 -> 新文件名)
|
| 839 |
+
"""
|
| 840 |
+
lines = []
|
| 841 |
+
for entry in entries:
|
| 842 |
+
# 跳过空别名
|
| 843 |
+
alias = entry.get("alias", "")
|
| 844 |
+
if not alias or not alias.strip():
|
| 845 |
+
logger.warning(f"跳过空别名: {entry.get('wav_name', 'unknown')}")
|
| 846 |
+
continue
|
| 847 |
+
|
| 848 |
+
# 应用文件名映射
|
| 849 |
+
wav_name = entry["wav_name"]
|
| 850 |
+
if filename_map and wav_name in filename_map:
|
| 851 |
+
wav_name = filename_map[wav_name]
|
| 852 |
+
|
| 853 |
+
line = "{wav}={alias},{offset},{consonant},{cutoff},{preutterance},{overlap}".format(
|
| 854 |
+
wav=wav_name,
|
| 855 |
+
alias=alias,
|
| 856 |
+
offset=entry["offset"],
|
| 857 |
+
consonant=entry["consonant"],
|
| 858 |
+
cutoff=entry["cutoff"],
|
| 859 |
+
preutterance=entry["preutterance"],
|
| 860 |
+
overlap=entry["overlap"]
|
| 861 |
+
)
|
| 862 |
+
lines.append(line)
|
| 863 |
+
|
| 864 |
+
# 按 wav 文件名 + 别名排序
|
| 865 |
+
lines.sort(key=lambda x: (x.split('=')[0], x.split('=')[1].split(',')[0]))
|
| 866 |
+
|
| 867 |
+
with open(output_path, 'w', encoding=encoding) as f:
|
| 868 |
+
f.write('\n'.join(lines))
|
| 869 |
+
|
| 870 |
+
def _write_character_txt(
|
| 871 |
+
self,
|
| 872 |
+
source_name: str,
|
| 873 |
+
output_path: str,
|
| 874 |
+
encoding: str
|
| 875 |
+
):
|
| 876 |
+
"""写入 character.txt 文件,用于 UTAU 识别音源名称"""
|
| 877 |
+
with open(output_path, 'w', encoding=encoding) as f:
|
| 878 |
+
f.write(f"name={source_name}")
|
src/quality_scorer.py
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
音源质量评分模块
|
| 4 |
+
|
| 5 |
+
提供多维度的音频质量评估,用于筛选最佳样本
|
| 6 |
+
支持时长、音量稳定性、音高稳定性三个评估维度
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import logging
|
| 10 |
+
import numpy as np
|
| 11 |
+
from typing import Dict, List, Optional, Tuple
|
| 12 |
+
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def duration_score(duration: float) -> float:
|
| 17 |
+
"""
|
| 18 |
+
时长评分:适中时长得分最高
|
| 19 |
+
|
| 20 |
+
参数:
|
| 21 |
+
duration: 音频时长(秒)
|
| 22 |
+
|
| 23 |
+
返回:
|
| 24 |
+
0~1 的分数
|
| 25 |
+
|
| 26 |
+
评分逻辑:
|
| 27 |
+
- 过短(<0.2s):发音不完整,低分
|
| 28 |
+
- 最佳范围(0.3~0.8s):满分
|
| 29 |
+
- 过长(>1.0s):可能包含多字或拖音,递减
|
| 30 |
+
"""
|
| 31 |
+
if duration < 0.2:
|
| 32 |
+
return duration / 0.2 * 0.5 # 0~0.5分
|
| 33 |
+
elif duration <= 0.8:
|
| 34 |
+
return 1.0 # 满分
|
| 35 |
+
elif duration <= 1.2:
|
| 36 |
+
return 1.0 - (duration - 0.8) / 0.4 * 0.3 # 0.7~1.0分
|
| 37 |
+
else:
|
| 38 |
+
return max(0.3, 0.7 - (duration - 1.2) * 0.2) # 递减,最低0.3
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def rms_variance_score(audio: np.ndarray, sr: int, frame_ms: int = 20) -> float:
|
| 42 |
+
"""
|
| 43 |
+
音量稳定性评分:RMS 方差越小越好
|
| 44 |
+
|
| 45 |
+
参数:
|
| 46 |
+
audio: 音频数据(numpy 数组)
|
| 47 |
+
sr: 采样率
|
| 48 |
+
frame_ms: 帧长度(毫秒)
|
| 49 |
+
|
| 50 |
+
返回:
|
| 51 |
+
0~1 的分数
|
| 52 |
+
|
| 53 |
+
计算步骤:
|
| 54 |
+
1. 将音频分帧
|
| 55 |
+
2. 计算每帧的 RMS 能量
|
| 56 |
+
3. 计算 RMS 序列的方差
|
| 57 |
+
4. 归一化到 0~1 分数
|
| 58 |
+
"""
|
| 59 |
+
frame_size = int(sr * frame_ms / 1000)
|
| 60 |
+
if frame_size <= 0:
|
| 61 |
+
return 0.5
|
| 62 |
+
|
| 63 |
+
frames = len(audio) // frame_size
|
| 64 |
+
if frames < 2:
|
| 65 |
+
return 0.5 # 太短无法评估
|
| 66 |
+
|
| 67 |
+
rms_values = []
|
| 68 |
+
for i in range(frames):
|
| 69 |
+
frame = audio[i * frame_size : (i + 1) * frame_size]
|
| 70 |
+
rms = np.sqrt(np.mean(frame.astype(np.float64) ** 2))
|
| 71 |
+
rms_values.append(rms)
|
| 72 |
+
|
| 73 |
+
if len(rms_values) < 2:
|
| 74 |
+
return 0.5
|
| 75 |
+
|
| 76 |
+
# 归一化 RMS 值(避免绝对值影响)
|
| 77 |
+
rms_array = np.array(rms_values)
|
| 78 |
+
mean_rms = np.mean(rms_array)
|
| 79 |
+
if mean_rms > 0:
|
| 80 |
+
rms_normalized = rms_array / mean_rms
|
| 81 |
+
variance = np.var(rms_normalized)
|
| 82 |
+
else:
|
| 83 |
+
variance = 0
|
| 84 |
+
|
| 85 |
+
# 归一化:方差越小分数越高
|
| 86 |
+
# 经验阈值:方差 < 0.01 为优秀,> 0.5 为较差
|
| 87 |
+
score = max(0, 1.0 - variance * 2)
|
| 88 |
+
return min(1.0, score)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def f0_variance_score(audio: np.ndarray, sr: int) -> float:
|
| 92 |
+
"""
|
| 93 |
+
音高稳定性评分:F0 方差越小越好
|
| 94 |
+
|
| 95 |
+
参数:
|
| 96 |
+
audio: 音频数据(numpy 数组)
|
| 97 |
+
sr: 采样率
|
| 98 |
+
|
| 99 |
+
返回:
|
| 100 |
+
0~1 的分数
|
| 101 |
+
|
| 102 |
+
计算步骤:
|
| 103 |
+
1. 使用 librosa.pyin 提取 F0
|
| 104 |
+
2. 过滤无声帧(F0=NaN)
|
| 105 |
+
3. 转换为音分计算方差
|
| 106 |
+
4. 归一化到 0~1 分数
|
| 107 |
+
"""
|
| 108 |
+
try:
|
| 109 |
+
import librosa
|
| 110 |
+
except ImportError:
|
| 111 |
+
logger.warning("librosa 未安装,无法计算 F0 方差")
|
| 112 |
+
return 0.5
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
# 提取 F0(使用 pyin 算法)
|
| 116 |
+
f0, voiced_flag, voiced_probs = librosa.pyin(
|
| 117 |
+
audio.astype(np.float32),
|
| 118 |
+
fmin=librosa.note_to_hz('C2'), # ~65Hz
|
| 119 |
+
fmax=librosa.note_to_hz('C6'), # ~1047Hz
|
| 120 |
+
sr=sr
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# 过滤无效值
|
| 124 |
+
valid_f0 = f0[~np.isnan(f0)]
|
| 125 |
+
|
| 126 |
+
if len(valid_f0) < 3:
|
| 127 |
+
return 0.5 # 无法评估
|
| 128 |
+
|
| 129 |
+
# 转换为音分(cents)计算方差,避免频率绝对值影响
|
| 130 |
+
# cents = 1200 * log2(f / f_ref)
|
| 131 |
+
median_f0 = np.median(valid_f0)
|
| 132 |
+
if median_f0 <= 0:
|
| 133 |
+
return 0.5
|
| 134 |
+
|
| 135 |
+
f0_cents = 1200 * np.log2(valid_f0 / median_f0)
|
| 136 |
+
variance = np.var(f0_cents)
|
| 137 |
+
|
| 138 |
+
# 归一化:方差 < 100 cents² 为优秀,> 10000 cents² 为较差
|
| 139 |
+
# 100 cents ≈ 1个半音
|
| 140 |
+
score = max(0, 1.0 - variance / 10000)
|
| 141 |
+
return min(1.0, score)
|
| 142 |
+
|
| 143 |
+
except Exception as e:
|
| 144 |
+
logger.warning(f"F0 计算失败: {e}")
|
| 145 |
+
return 0.5
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class QualityScorer:
|
| 150 |
+
"""
|
| 151 |
+
音频质量评分器
|
| 152 |
+
|
| 153 |
+
支持多维度评估和加权综合评分
|
| 154 |
+
"""
|
| 155 |
+
|
| 156 |
+
# 默认权重
|
| 157 |
+
DEFAULT_WEIGHTS = {
|
| 158 |
+
"duration": 0.3,
|
| 159 |
+
"rms": 0.3,
|
| 160 |
+
"f0": 0.4
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
def __init__(
|
| 164 |
+
self,
|
| 165 |
+
enabled_metrics: Optional[List[str]] = None,
|
| 166 |
+
weights: Optional[Dict[str, float]] = None
|
| 167 |
+
):
|
| 168 |
+
"""
|
| 169 |
+
初始化评分器
|
| 170 |
+
|
| 171 |
+
参数:
|
| 172 |
+
enabled_metrics: 启用的评分维度,如 ["duration", "rms", "f0"]
|
| 173 |
+
weights: 各维度权重
|
| 174 |
+
"""
|
| 175 |
+
self.enabled_metrics = enabled_metrics or ["duration"]
|
| 176 |
+
self.weights = weights or self.DEFAULT_WEIGHTS.copy()
|
| 177 |
+
|
| 178 |
+
def score(
|
| 179 |
+
self,
|
| 180 |
+
audio: np.ndarray,
|
| 181 |
+
sr: int,
|
| 182 |
+
duration: Optional[float] = None
|
| 183 |
+
) -> Dict[str, float]:
|
| 184 |
+
"""
|
| 185 |
+
计算音频质量分数
|
| 186 |
+
|
| 187 |
+
参数:
|
| 188 |
+
audio: 音频数据
|
| 189 |
+
sr: 采样率
|
| 190 |
+
duration: 音频时长(秒),如不提供则自动计算
|
| 191 |
+
|
| 192 |
+
返回:
|
| 193 |
+
包含各维度分数和综合分数的字典
|
| 194 |
+
"""
|
| 195 |
+
if duration is None:
|
| 196 |
+
duration = len(audio) / sr
|
| 197 |
+
|
| 198 |
+
scores = {}
|
| 199 |
+
|
| 200 |
+
if "duration" in self.enabled_metrics:
|
| 201 |
+
scores["duration"] = duration_score(duration)
|
| 202 |
+
|
| 203 |
+
if "rms" in self.enabled_metrics:
|
| 204 |
+
scores["rms"] = rms_variance_score(audio, sr)
|
| 205 |
+
|
| 206 |
+
if "f0" in self.enabled_metrics:
|
| 207 |
+
scores["f0"] = f0_variance_score(audio, sr)
|
| 208 |
+
|
| 209 |
+
# 计算加权综合分数
|
| 210 |
+
if scores:
|
| 211 |
+
total_weight = sum(self.weights.get(k, 0) for k in scores.keys())
|
| 212 |
+
if total_weight > 0:
|
| 213 |
+
combined = sum(
|
| 214 |
+
scores[k] * self.weights.get(k, 0)
|
| 215 |
+
for k in scores.keys()
|
| 216 |
+
) / total_weight
|
| 217 |
+
else:
|
| 218 |
+
combined = sum(scores.values()) / len(scores)
|
| 219 |
+
scores["combined"] = combined
|
| 220 |
+
else:
|
| 221 |
+
scores["combined"] = 0.5
|
| 222 |
+
|
| 223 |
+
return scores
|
| 224 |
+
|
| 225 |
+
def score_from_file(self, wav_path: str) -> Dict[str, float]:
|
| 226 |
+
"""
|
| 227 |
+
从文件计算质量分数
|
| 228 |
+
|
| 229 |
+
参数:
|
| 230 |
+
wav_path: 音频文件路径
|
| 231 |
+
|
| 232 |
+
返回:
|
| 233 |
+
包含各维度分数和综合分数的字典
|
| 234 |
+
"""
|
| 235 |
+
try:
|
| 236 |
+
import soundfile as sf
|
| 237 |
+
audio, sr = sf.read(wav_path)
|
| 238 |
+
|
| 239 |
+
# 转换为单声道
|
| 240 |
+
if len(audio.shape) > 1:
|
| 241 |
+
audio = audio.mean(axis=1)
|
| 242 |
+
|
| 243 |
+
return self.score(audio, sr)
|
| 244 |
+
|
| 245 |
+
except Exception as e:
|
| 246 |
+
logger.error(f"读取音频文件失败 {wav_path}: {e}")
|
| 247 |
+
return {"combined": 0.5}
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def calculate_quality_score(
|
| 251 |
+
audio: np.ndarray,
|
| 252 |
+
sr: int,
|
| 253 |
+
weights: Optional[Dict[str, float]] = None,
|
| 254 |
+
enabled_metrics: Optional[List[str]] = None
|
| 255 |
+
) -> float:
|
| 256 |
+
"""
|
| 257 |
+
便捷函数:计算综合质量评分
|
| 258 |
+
|
| 259 |
+
参数:
|
| 260 |
+
audio: 音频数据
|
| 261 |
+
sr: 采样率
|
| 262 |
+
weights: 各维度权重
|
| 263 |
+
enabled_metrics: 启用的评分维度
|
| 264 |
+
|
| 265 |
+
返回:
|
| 266 |
+
0~1 的综合分数
|
| 267 |
+
"""
|
| 268 |
+
scorer = QualityScorer(enabled_metrics=enabled_metrics, weights=weights)
|
| 269 |
+
scores = scorer.score(audio, sr)
|
| 270 |
+
return scores.get("combined", 0.5)
|