Instructions to use FormalZz/AudioX with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Stable Audio Tools
How to use FormalZz/AudioX with Stable Audio Tools:
import torch import torchaudio from einops import rearrange from stable_audio_tools import get_pretrained_model from stable_audio_tools.inference.generation import generate_diffusion_cond device = "cuda" if torch.cuda.is_available() else "cpu" # Download model model, model_config = get_pretrained_model("FormalZz/AudioX") sample_rate = model_config["sample_rate"] sample_size = model_config["sample_size"] model = model.to(device) # Set up text and timing conditioning conditioning = [{ "prompt": "128 BPM tech house drum loop", }] # Generate stereo audio output = generate_diffusion_cond( model, conditioning=conditioning, sample_size=sample_size, device=device ) # Rearrange audio batch to a single sequence output = rearrange(output, "b d n -> d (b n)") # Peak normalize, clip, convert to int16, and save to file output = output.to(torch.float32).div(torch.max(torch.abs(output))).clamp(-1, 1).mul(32767).to(torch.int16).cpu() torchaudio.save("output.wav", output, sample_rate) - Notebooks
- Google Colab
- Kaggle
| from ..data.utils import PadCrop | |
| from torchaudio import transforms as T | |
| def set_audio_channels(audio, target_channels): | |
| if target_channels == 1: | |
| # Convert to mono | |
| audio = audio.mean(1, keepdim=True) | |
| elif target_channels == 2: | |
| # Convert to stereo | |
| if audio.shape[1] == 1: | |
| audio = audio.repeat(1, 2, 1) | |
| elif audio.shape[1] > 2: | |
| audio = audio[:, :2, :] | |
| return audio | |
| def prepare_audio(audio, in_sr, target_sr, target_length, target_channels, device): | |
| audio = audio.to(device) | |
| if in_sr != target_sr: | |
| resample_tf = T.Resample(in_sr, target_sr).to(device) | |
| audio = resample_tf(audio) | |
| audio = PadCrop(target_length, randomize=False)(audio) | |
| # Add batch dimension | |
| if audio.dim() == 1: | |
| audio = audio.unsqueeze(0).unsqueeze(0) | |
| elif audio.dim() == 2: | |
| audio = audio.unsqueeze(0) | |
| audio = set_audio_channels(audio, target_channels) | |
| return audio |