Files
military-tech-voice3.0/backend/app/services/fish_audio_service.py
T
2026-07-02 23:58:52 +08:00

54 lines
1.4 KiB
Python

import os
import requests
PROXY_URL = 'http://202.182.125.133:8443/fish-api/v1/tts'
PROXY_USER = 'fishproxy'
PROXY_PASS = 'AO7C6SiovkRydkSHsUPJaOZw0CBh9mp0'
def synthesize_fish_audio(text, speed=1.0, pitch=0, volume=1.0, emotion='neutral'):
api_key = os.getenv('FISH_AUDIO_API_KEY', '')
if not api_key:
raise ValueError('FISH_AUDIO_API_KEY未设置')
voice_id = os.getenv('FISH_AUDIO_VOICE_ID', '34e841f0f4ff44aca6e396debf2776d4')
emotion_tags = {
'happy': '[happy]',
'sad': '[sad]',
'angry': '[angry]',
'surprised': '[excited]',
'fearful': '[nervous]',
}
tag = emotion_tags.get(emotion, '')
if tag:
text = f"{tag}{text}"
headers = {
'X-Fish-Auth': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
payload = {
'text': text,
'reference_id': voice_id,
'format': 'mp3',
'speed': max(0.5, min(2.0, speed)),
'volume': max(-20, min(20, (volume - 1.0) * 20)),
}
response = requests.post(
PROXY_URL, json=payload, headers=headers,
auth=(PROXY_USER, PROXY_PASS), timeout=120
)
if response.status_code != 200:
raise Exception(f'Fish Audio API错误: {response.status_code} - {response.text[:200]}')
audio_bytes = response.content
if not audio_bytes:
raise Exception('Fish Audio返回空音频')
return audio_bytes