feat: Add SiliconFlow TTS API support with custom base URL and model selection

This commit adds comprehensive support for using SiliconFlow's TTS API as an alternative to OpenAI, including:

Features:
- Configurable API base URL (Settings > API Base URL)
- TTS model selection dropdown (CosyVoice2-0.5B, OpenAI compatible models)
- Dynamic voice options based on selected model
- Editable voice dropdown (Combobox) supporting custom voice IDs
- Automatic voice formatting for SiliconFlow (model:voice format)
- Debug logging for troubleshooting API calls
- Warning for incorrect base URL format

Changes:
- utils/settings_manager.py: Added api_base_url and tts_model settings
- utils/text_to_mic.py:
  - Added get_available_tts_models() for model options
  - Added get_siliconflow_voices() for SiliconFlow voices
  - Added change_api_base_url() method with validation
  - Added TTS model dropdown in GUI
  - Converted voice dropdown to Combobox for typing support
  - Added on_voice_exit() for validation
  - Updated API call to use selected model and formatted voice
- text-to-mic-cli.py: Added OPENAI_API_BASE_URL and OPENAI_TTS_MODEL env var support
- Readme.md: Updated documentation with SiliconFlow usage instructions

Supported Models:
- FunAudioLLM/CosyVoice2-0.5B (SiliconFlow - multi-language, emotional)
- tts-1, tts-1-hd (OpenAI compatible)
- gpt-4o-mini-tts (OpenAI default)

SiliconFlow Voices (CosyVoice2-0.5B):
- Male: alex, benjamin, charles, david
- Female: anna, bella, claire, diana
- Custom voices via voice ID entry

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Xin Wang
2026-01-27 17:12:34 +08:00
parent 20358adafb
commit 92d20e59e9
4 changed files with 365 additions and 30 deletions

View File

@@ -10,7 +10,14 @@ import os
load_dotenv()
# Set up your OpenAI API key from the environment variable
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
api_key = os.getenv('OPENAI_API_KEY')
api_base_url = os.getenv('OPENAI_API_BASE_URL', '').strip()
# Create client with custom base URL if provided
if api_base_url:
client = OpenAI(api_key=api_key, base_url=api_base_url)
else:
client = OpenAI(api_key=api_key)
def list_audio_devices():
p = pyaudio.PyAudio()
@@ -81,9 +88,13 @@ def play_audio_multiplexed(file_paths, device_indices):
p.terminate()
def stream_audio_to_virtual_mic(text, voice="fable", device_index=None, device_index_2=None):
def stream_audio_to_virtual_mic(text, voice="fable", model=None, device_index=None, device_index_2=None):
# Get model from environment variable or use default
if model is None:
model = os.getenv('OPENAI_TTS_MODEL', 'tts-1')
response = client.audio.speech.create(
model="tts-1",
model=model,
voice=voice,
input=text,
response_format='wav'
@@ -114,10 +125,27 @@ if __name__ == "__main__":
if arglen < 2:
print("Usage: python script.py 'text to convert'")
print("Environment variables:")
print(" OPENAI_API_KEY - Your API key (required)")
print(" OPENAI_API_BASE_URL - Custom API base URL (optional)")
print(" OPENAI_TTS_MODEL - TTS model to use (default: tts-1)")
print("")
print("Example models:")
print(" - tts-1 (OpenAI standard)")
print(" - tts-1-hd (OpenAI high quality)")
print(" - gpt-4o-mini-tts (OpenAI)")
print(" - FunAudioLLM/CosyVoice2-0.5B (SiliconFlow)")
print("")
print("For SiliconFlow voices with CosyVoice2:")
print(" The voice will be auto-formatted as: FunAudioLLM/CosyVoice2-0.5B:alex")
sys.exit(1)
print(f"arg count {arglen}")
# Get TTS model from environment
tts_model = os.getenv('OPENAI_TTS_MODEL', 'tts-1')
print(f"Using TTS model: {tts_model}")
if arglen == 4:
device_index = int(sys.argv[2])
device_index_2 = int(sys.argv[3])
@@ -129,5 +157,5 @@ if __name__ == "__main__":
device_index = int(input("Enter the device index: "))
device_index_2 = None
stream_audio_to_virtual_mic(sys.argv[1], voice="fable", device_index=device_index,device_index_2=device_index_2)
stream_audio_to_virtual_mic(sys.argv[1], voice="fable", model=tts_model, device_index=device_index,device_index_2=device_index_2)