### Environment Setup for SongBloom (Bash) Source: https://github.com/tencent-ailab/songbloom/blob/master/README.md This script sets up a Python environment using Conda and installs necessary packages, including PyTorch with CUDA support, for running the SongBloom project. It assumes a Linux-like environment with Conda and yum package manager available. ```bash conda create -n SongBloom python==3.8.12 conda activate SongBloom # yum install libsndfile # pip install torch==2.2.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cu118 # For different CUDA version pip install -r requirements.txt ``` -------------------------------- ### Configure SongBloom Generation Pipeline Source: https://context7.com/tencent-ailab/songbloom/llms.txt This Python code demonstrates downloading model weights from Hugging Face and setting up the SongBloom generation pipeline. It includes configuration for custom parameters, hardware settings (GPU/CPU, data types), and generation specifics like guidance scale and diffusion steps. ```python from huggingface_hub import hf_hub_download from omegaconf import OmegaConf import torch from SongBloom.models.songbloom.songbloom_pl import SongBloom_Sampler # Download model files from Hugging Face model_name = "songbloom_full_150s_dpo" # Options: songbloom_full_150s, songbloom_full_150s_dpo, songbloom_full_240s local_dir = "./cache" # Download configuration and checkpoint repo_id = "CypressYang/SongBloom" cfg_path = hf_hub_download(repo_id=repo_id, filename=f"{model_name}.yaml", local_dir=local_dir) ckpt_path = hf_hub_download(repo_id=repo_id, filename=f"{model_name}.pt", local_dir=local_dir) # Download VAE weights vae_cfg_path = hf_hub_download(repo_id=repo_id, filename="stable_audio_1920_vae.json", local_dir=local_dir) vae_ckpt_path = hf_hub_download(repo_id=repo_id, filename="autoencoder_music_dsp1920.ckpt", local_dir=local_dir) # Download G2P vocabulary g2p_path = hf_hub_download(repo_id=repo_id, filename="vocab_g2p.yaml", local_dir=local_dir) # Load configuration cfg = OmegaConf.load(cfg_path) cfg.max_dur = cfg.max_dur + 10 # Add buffer for generation # Build model with specific hardware settings device = torch.device('cuda:0') # or 'cpu' for CPU inference dtype = torch.bfloat16 # Use bfloat16 for RTX4090 and similar GPUs, float32 for higher precision model = SongBloom_Sampler.build_from_trainer( cfg=cfg, strict=False, dtype=dtype, device=device ) # Configure generation parameters model.set_generation_params( cfg_coef=1.5, # Classifier-free guidance coefficient (higher = more faithful to conditions) steps=50, # Number of diffusion steps (higher = better quality, slower) dit_cfg_type='h', # CFG type for diffusion transformer use_sampling=True, # Use sampling instead of greedy decoding top_k=200, # Top-k sampling parameter max_frames=160 * 25 # Maximum generation length in frames (160s * 25 fps) ) print(f"Model loaded on {device} with dtype {dtype}") print(f"Sample rate: {model.sample_rate}Hz") print(f"Frame rate: {model.frame_rate}fps") ``` -------------------------------- ### Batch Song Generation from JSONL with SongBloom Source: https://context7.com/tencent-ailab/songbloom/llms.txt Processes multiple song generation requests from a JSONL file for batch processing. It requires PyTorch, torchaudio, OmegaConf, and argparse. The input file contains dictionaries with 'idx', 'lyrics', and 'prompt_wav' paths. Outputs are saved to a specified directory. ```python import os import json import torch import torchaudio import argparse from omegaconf import OmegaConf from SongBloom.models.songbloom.songbloom_pl import SongBloom_Sampler # Setup model_name = "songbloom_full_150s" local_dir = "./cache" input_jsonl = "example/test.jsonl" output_dir = "./output" n_samples = 2 # Download and load model from infer import hf_download hf_download(model_name, local_dir) cfg = OmegaConf.load(f"{local_dir}/{model_name}.yaml") cfg.max_dur = cfg.max_dur + 10 device = torch.device('cuda:0') dtype = torch.bfloat16 # Use bfloat16 for GPUs with low VRAM model = SongBloom_Sampler.build_from_trainer(cfg, strict=False, dtype=dtype, device=device) model.set_generation_params(**cfg.inference) os.makedirs(output_dir, exist_ok=True) # Process JSONL input file # Each line: {"idx": "test", "lyrics": "[verse] ...", "prompt_wav": "path/to/audio.wav"} input_lines = open(input_jsonl, 'r').readlines() input_lines = [json.loads(l.strip()) for l in input_lines] for test_sample in input_lines: idx = test_sample["idx"] lyrics = test_sample["lyrics"] prompt_wav_path = test_sample["prompt_wav"] # Load and resample prompt audio prompt_wav, sr = torchaudio.load(prompt_wav_path) if sr != model.sample_rate: prompt_wav = torchaudio.functional.resample(prompt_wav, sr, model.sample_rate) prompt_wav = prompt_wav.mean(dim=0, keepdim=True).to(dtype) prompt_wav = prompt_wav[..., :10*model.sample_rate] # Generate n_samples variations for i in range(n_samples): wav = model.generate(lyrics, prompt_wav) torchaudio.save(f'{output_dir}/{idx}_s{i}.flac', wav[0].cpu().float(), model.sample_rate) print(f"Generated {output_dir}/{idx}_s{i}.flac") ``` -------------------------------- ### Print Phoneme Symbol Counts and Samples Source: https://context7.com/tencent-ailab/songbloom/llms.txt This Python snippet demonstrates how to print the total count of available phoneme symbols and display the first 20 sample symbols. It assumes a 'symbols' variable is already defined and accessible. ```python print(f"Total symbols: {len(symbols)}") print(f"Sample symbols: {symbols[:20]}") ``` -------------------------------- ### Generate Song with SongBloom_Sampler.generate() Source: https://context7.com/tencent-ailab/songbloom/llms.txt Generates a complete song using provided lyrics and a 10-second audio prompt for musical style. It requires PyTorch, torchaudio, and OmegaConf. The function returns the generated audio as a torch tensor at a 48kHz sample rate. ```python import torch import torchaudio from omegaconf import OmegaConf from SongBloom.models.songbloom.songbloom_pl import SongBloom_Sampler # Load model configuration and build sampler cfg = OmegaConf.load("cache/songbloom_full_150s.yaml") cfg.max_dur = cfg.max_dur + 10 device = torch.device('cuda:0') dtype = torch.float32 model = SongBloom_Sampler.build_from_trainer(cfg, strict=False, dtype=dtype, device=device) # Set generation parameters model.set_generation_params( cfg_coef=1.5, steps=50, dit_cfg_type='h', use_sampling=True, top_k=200, max_frames=160 * 25 ) # Prepare lyrics with structure tags lyrics = "[intro] [intro] [intro] [intro] , [verse] City lights flicker through the car window. Dreams pass fast where the lost ones go , [chorus] Run with me down the empty street. Where silence and heartbeat always meet , [outro] [outro]" # Load and prepare 10-second prompt audio prompt_wav, sr = torchaudio.load("example/test.wav") if sr != model.sample_rate: prompt_wav = torchaudio.functional.resample(prompt_wav, sr, model.sample_rate) prompt_wav = prompt_wav.mean(dim=0, keepdim=True).to(dtype) prompt_wav = prompt_wav[..., :10*model.sample_rate] # Generate song generated_audio = model.generate(lyrics, prompt_wav) # Save output torchaudio.save('output/song.flac', generated_audio[0].cpu().float(), model.sample_rate) ``` -------------------------------- ### BibTeX Citation for SongBloom Source: https://github.com/tencent-ailab/songbloom/blob/master/README.md Provides the BibTeX formatted citation for the SongBloom paper, including title, authors, journal, and year, for academic referencing. ```bibtex @article{yang2025songbloom, title={SongBloom: Coherent Song Generation via Interleaved Autoregressive Sketching and Diffusion Refinement}, author={Yang, Chenyu and Wang, Shuai and Chen, Hangting and Tan, Wei and Yu, Jianwei and Li, Haizhou}, journal={arXiv preprint arXiv:2506.07634}, year={2025} } ``` -------------------------------- ### Inference Script for SongBloom (Bash) Source: https://github.com/tencent-ailab/songbloom/blob/master/README.md Executes the SongBloom inference script to generate audio samples. It supports various configurations, including specifying the model, input data, output directory, number of samples, and GPU memory optimization (bfloat16) for low VRAM GPUs. Flash-attention can also be enabled. ```bash source set_env.sh python3 infer.py --input-jsonl example/test.jsonl # For GPUs with low VRAM like RTX4090, you should set the dtype as bfloat16 python3 infer.py --input-jsonl example/test.jsonl --dtype bfloat16 # SongBloom also supports flash-attn (optional). To enable it, please install flash-attn (v2.6.3 is used during training) manually and set os.environ['DISABLE_FLASH_ATTN'] = "0" in infer.py:8 ``` -------------------------------- ### Data Preparation Format (JSON) Source: https://github.com/tencent-ailab/songbloom/blob/master/README.md Defines the expected JSON format for input data used in SongBloom's data preparation. Each line in the .jsonl file represents a sample with an index, lyrics, and a path to a prompt audio file. ```json { "idx": "The index of each sample", "lyrics": "The lyrics to be generated", "prompt_wav": "The path of the style prompt audio" } ``` -------------------------------- ### Process Structured Lyrics to Phonemes in Python Source: https://context7.com/tencent-ailab/songbloom/llms.txt This Python code processes a structured lyric string, converting words into phonemes while preserving structural tags and punctuation. It relies on the 'SongBloom.g2p.lyric_common' module for labels and a 'g2p_phoneme' function for conversion. The output is a space-joined string of processed lyrical parts. ```python from SongBloom.g2p.lyric_common import LABELS full_lyric = "[verse] 风轻轻吹过古道 , [chorus] 想见你在晨曦中" processed_parts = [] for word in full_lyric.split(): if word in LABELS.keys(): processed_parts.append(word) elif word in [',', '.']: processed_parts.append(word) else: processed_parts.append(g2p_phoneme(word)) processed_lyric = " ".join(processed_parts) print(f"Processed: {processed_lyric}") ``` -------------------------------- ### Convert Lyrics to Phonemes with SongBloom G2P Source: https://context7.com/tencent-ailab/songbloom/llms.txt This Python code snippet shows how to use the SongBloom Grapheme-to-Phoneme (G2P) module to convert Chinese and English lyrics into phoneme representations. It supports both phoneme-based and pinyin-based processing, essential for accurate singing synthesis. ```python from SongBloom.g2p.lyric_common import key2processor, symbols # Available processors print("Available G2P processors:", list(key2processor.keys())) # ['pinyin', 'phoneme'] # Phoneme-based processing (supports mixed Chinese-English) g2p_phoneme = key2processor['phoneme'] # Process Chinese text chinese_text = "想见你在晨曦中" chinese_phonemes = g2p_phoneme(chinese_text) print(f"Chinese: {chinese_text}") print(f"Phonemes: {chinese_phonemes}") # Process English text english_text = "Run with me down the empty street" english_phonemes = g2p_phoneme(english_text) print(f"English: {english_text}") print(f"Phonemes: {english_phonemes}") # Pinyin-based processing (primarily for Chinese) g2p_pinyin = key2processor['pinyin'] pinyin_output = g2p_pinyin(chinese_text) print(f"Pinyin: {pinyin_output}") ``` -------------------------------- ### Clean and Normalize Lyrics with clean_lyrics() Source: https://context7.com/tencent-ailab/songbloom/llms.txt Normalizes and formats raw lyrics text to meet SongBloom's input requirements. This function handles structure tags, punctuation, and spacing. It requires the 'normalize_lyrics' module. ```python from normalize_lyrics import clean_lyrics # Input lyrics with various formatting issues raw_lyrics = """ [intro], [intro] [intro] [Verse] 风轻轻吹过古道!岁月在墙上刻下记号? 梦中你笑得多甜...醒来却只剩下寂寥 繁花似锦的春天,少了你的色彩也失了妖娆 [Chorus] 想见你,在晨曦中 在月光下!! 每个瞬间都渴望 """ # Clean and normalize cleaned = clean_lyrics(raw_lyrics) print(cleaned) ``` -------------------------------- ### Validate Lyric Structure with SongBloom Source: https://context7.com/tencent-ailab/songbloom/llms.txt This Python function checks if lyric structure tags (like '[verse]', '[chorus]', '[intro]') are legally formatted according to SongBloom's requirements. It verifies that vocal sections contain text and non-vocal sections are empty. ```python from SongBloom.g2p.lyric_common import is_struct_legal, LABELS, valid_struct_type # Structure tags mapping print(LABELS) # {'[intro]': 0, '[outro]': 1, '[bridge]': 2, '[inst]': 3, '[verse]': 4, '[chorus]': 5, '[silence]': 6} # Valid vocal structure types print(valid_struct_type) # ['[chorus]', '[verse]', '[bridge]'] # Validation examples struct1 = "[verse]" text1 = "City lights flicker through the car window" is_valid1 = is_struct_legal(struct1, text1) print(f"{struct1} with text: {is_valid1}") # True - vocal sections need text struct2 = "[intro]" text2 = "" is_valid2 = is_struct_legal(struct2, text2) print(f"{struct2} without text: {is_valid2}") # True - non-vocal sections should be empty struct3 = "[chorus]" text3 = "" struct3 = "[chorus]" text3 = "" is_valid3 = is_struct_legal(struct3, text3) print(f"{struct3} without text: {is_valid3}") # False - vocal sections must have text struct4 = "[inst]" text4 = "Some lyrics" is_valid4 = is_struct_legal(struct4, text4) print(f"{struct4} with text: {is_valid4}") # False - instrumental sections should not have text ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.