Perform text-to-speech training and sampling with SpearTTS integration
mainTo use Voicebox for text-guided speech generation, you need to integrate it with SpearTTS components. This involves using HubertWithKmeans for audio processing, TextToSemantic for converting text to semantic tokens, and ConditionalFlowMatcherWrapper to manage the training and sampling process.
Key components:
HubertWithKmeans: Loads a Hubert checkpoint and Kmeans weights.TextToSemantic: Converts text to semantic tokens using a wav2vec model.VoiceBox: The core model, which can be configured with anaudio_enc_dec(likeEncodecVoco()).ConditionalFlowMatcherWrapper: Wraps the model and semantic converter to provide.sample()and training loss functionality.
import torch
from voicebox_pytorch import (
VoiceBox,
EncodecVoco,
ConditionalFlowMatcherWrapper,
HubertWithKmeans,
TextToSemantic
)
# 1. Setup Hubert with Kmeans
wav2vec = HubertWithKmeans(
checkpoint_path = '/path/to/hubert/checkpoint.pt',
kmeans_path = '/path/to/hubert/kmeans.bin'
)
# 2. Setup Text to Semantic conversion
text_to_semantic = TextToSemantic(
wav2vec = wav2vec,
dim = 512,
source_depth = 1,
target_depth = 1,
use_openai_tokenizer = True
)
text_to_semantic.load('/path/to/trained/spear-tts/model.pt')
# 3. Initialize VoiceBox model
model = VoiceBox(
dim = 512,
audio_enc_dec = EncodecVoco(),
num_cond_tokens = 500,
depth = 2,
dim_head = 64,
heads = 16
)
# 4. Wrap with ConditionalFlowMatcher
cfm_wrapper = ConditionalFlowMatcherWrapper(
voicebox = model,
text_to_semantic = text_to_semantic
)
# --- Training ---
audio = torch.randn(2, 12000)
loss = cfm_wrapper(audio)
loss.backward()
# --- Sampling ---
texts = [
'the rain in spain falls mainly in the plains',
'she sells sea shells by the seashore'
]
cond = torch.randn(2, 12000)
sampled = cfm_wrapper.sample(cond = cond, texts = texts) # (2, 1, <audio length>)