For asynchronous workflows, use the voicevox_core.asyncio module instead of voicevox_core.blocking.
Important Considerations:
- Concurrency Limits: You cannot perform simultaneous synthesis using the same voice model instance (it is protected by a Mutex).
- Performance: Because the underlying ONNX Runtime performs its own optimizations, using
asyncio for performance gains is often ineffective. However, it can help with responsiveness if you reduce synthesizer.cpu_num_threads to allow other tasks to run while a long synthesis is in progress.
Example:
from voicevox_core.asyncio import Onnxruntime, OpenJtalk, Synthesizer, VoiceModelFile
# 1. Synthesizer initialization (async)
open_jtalk_dict_dir = "dict/open_jtalk_dic_utf_8-1.11"
synthesizer = Synthesizer(await Onnxruntime.load_once(), await OpenJtalk.new(open_jtalk_dict_dir))
# 2. Load voice model (async context manager)
async with await VoiceModelFile.open("models/vvms/0.vvm") as model:
await synthesizer.load_voice_model(model)
# 3. Text-to-speech (async)
wav = await synthesizer.tts("サンプル音声です", 0)
from voicevox_core.asyncio import Onnxruntime, OpenJtalk, Synthesizer, VoiceModelFile
# 1. Synthesizerの初期化
open_jtalk_dict_dir = "dict/open_jtalk_dic_utf_8-1.11"
synthesizer = Synthesizer(await Onnxruntime.load_once(), await OpenJtalk.new(open_jtalk_dict_dir))
# 2. 音声モデルの読み込み
async with await VoiceModelFile.open("models/vvms/0.vvm") as model:
await synthesizer.load_voice_model(model)
# 3. テキスト音声合成
wav = await synthesizer.tts("サンプル音声です", 0)