Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(voice): send SiliconFlow volume gain in decibels

Rudy Celeklidfef3c064858
diff --git a/app/services/voice.py b/app/services/voice.py
index fefa6f44..5f4344e0 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1627,11 +1627,15 @@ def siliconflow_tts(
logger.error("SiliconFlow API key is not set")
return None
- # 将voice_volume转换为硅基流动的增益范围
- # 默认voice_volume为1.0,对应gain为0
- gain = voice_volume - 1.0
- # 确保gain在[-10, 10]范围内
- gain = max(-10, min(10, gain))
+ # voice_volume 是线性倍数,硅基流动的 gain 单位是 dB,范围 [-10, 10]。
+ # 默认 1.0 对应 0 dB;2.0 约为 +6 dB,0.6 约为 -4.4 dB。直接相减会把
+ # 2 倍音量当作 +1 dB,试听音量与最终视频的线性混音结果明显不一致。
+ if voice_volume is None:
+ voice_volume = 1.0
+ if voice_volume <= 0:
+ gain = -10.0
+ else:
+ gain = max(-10.0, min(10.0, 20.0 * math.log10(voice_volume)))
url = "https://api.siliconflow.cn/v1/audio/speech"
diff --git a/test/services/test_siliconflow_volume_gain.py b/test/services/test_siliconflow_volume_gain.py
new file mode 100644
index 00000000..24c70ad0
--- /dev/null
+++ b/test/services/test_siliconflow_volume_gain.py
@@ -0,0 +1,50 @@
+import math
+from unittest.mock import patch
+
+import pytest
+
+from app.services import voice
+
+
+class _RejectedResponse:
+ status_code = 400
+ text = "rejected"
+ content = b""
+
+
+ "voice_volume, expected_gain",
+ [
+ (1.0, 0.0),
+ (2.0, 20 * math.log10(2.0)),
+ (0.6, 20 * math.log10(0.6)),
+ (5.0, 10.0),
+ (0.1, -10.0),
+ ],
+)
+def test_siliconflow_gain_is_decibels_for_linear_volume(
+ voice_volume, expected_gain, tmp_path
+):
+ # SiliconFlow's gain is in dB. The WebUI preview passes the same linear
+ # multiplier that the final mix applies, so the preview must match it.
+ payloads = []
+
+ def fake_post(url, json=None, **kwargs):
+ payloads.append(json)
+ return _RejectedResponse()
+
+ with (
+ patch.dict(voice.config.siliconflow, {"api_key": "test-key"}),
+ patch.object(voice.requests, "post", side_effect=fake_post),
+ ):
+ voice.siliconflow_tts(
+ "hello",
+ "FunAudioLLM/CosyVoice2-0.5B",
+ "FunAudioLLM/CosyVoice2-0.5B:alex",
+ 1.0,
+ str(tmp_path / "voice.mp3"),
+ voice_volume,
+ )
+
+ assert payloads
+ assert payloads[0]["gain"] == pytest.approx(expected_gain)