|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +Minimal example: generate audio from a text prompt using Stable Audio Open. |
| 5 | +
|
| 6 | +Requires: pip install .[stable-audio] (no stable-audio-tools clone needed). |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python examples/inference/basic/stable_audio_basic.py |
| 10 | + python examples/inference/basic/stable_audio_basic.py --prompt "A gentle rain" --duration 8 |
| 11 | + python examples/inference/basic/stable_audio_basic.py --no-cpu-offload # higher GPU utilization |
| 12 | +""" |
| 13 | +import argparse |
| 14 | +import os |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import torch |
| 18 | + |
| 19 | +from fastvideo import VideoGenerator |
| 20 | + |
| 21 | + |
| 22 | +def save_audio_wav(audio: torch.Tensor, sample_rate: int, path: str) -> None: |
| 23 | + """Save audio tensor (B, C, T) to WAV file. Output is stereo interleaved.""" |
| 24 | + import wave |
| 25 | + |
| 26 | + if audio.ndim == 3: |
| 27 | + audio = audio[0] |
| 28 | + audio_np = audio.detach().cpu().float().numpy() |
| 29 | + audio_np = np.clip(audio_np, -1.0, 1.0) |
| 30 | + audio_int16 = (audio_np * 32767.0).astype(np.int16) |
| 31 | + if audio_int16.ndim == 1: |
| 32 | + audio_int16 = audio_int16[:, None] |
| 33 | + num_channels = audio_int16.shape[0] |
| 34 | + num_frames = audio_int16.shape[1] |
| 35 | + frames_bytes = audio_int16.T.tobytes() |
| 36 | + |
| 37 | + os.makedirs(os.path.dirname(path) or ".", exist_ok=True) |
| 38 | + with wave.open(path, "wb") as wav_file: |
| 39 | + wav_file.setnchannels(num_channels) |
| 40 | + wav_file.setsampwidth(2) |
| 41 | + wav_file.setframerate(sample_rate) |
| 42 | + wav_file.writeframes(frames_bytes) |
| 43 | + |
| 44 | + |
| 45 | +def main() -> None: |
| 46 | + parser = argparse.ArgumentParser(description="Stable Audio text-to-audio generation") |
| 47 | + parser.add_argument( |
| 48 | + "--model-path", |
| 49 | + type=str, |
| 50 | + default="stabilityai/stable-audio-open-1.0", |
| 51 | + help="Path to model or HuggingFace model ID (e.g. stabilityai/stable-audio-open-1.0)", |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--prompt", |
| 55 | + type=str, |
| 56 | + default="A beautiful piano arpeggio", |
| 57 | + help="Text description of the audio to generate", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--duration", |
| 61 | + type=float, |
| 62 | + default=10.0, |
| 63 | + help="Duration in seconds (default: 10)", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "--output", |
| 67 | + type=str, |
| 68 | + default="stable_audio_output.wav", |
| 69 | + help="Output WAV file path", |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + "--steps", |
| 73 | + type=int, |
| 74 | + default=100, |
| 75 | + help="Number of denoising steps (default: 100)", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--guidance-scale", |
| 79 | + type=float, |
| 80 | + default=6.0, |
| 81 | + help="Classifier-free guidance scale (default: 6.0)", |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + "--seed", |
| 85 | + type=int, |
| 86 | + default=42, |
| 87 | + help="Random seed", |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "--no-cpu-offload", |
| 91 | + action="store_true", |
| 92 | + help="Disable CPU offload for higher GPU utilization (requires more VRAM)", |
| 93 | + ) |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + offload_kwargs = {} |
| 97 | + if args.no_cpu_offload: |
| 98 | + offload_kwargs = dict( |
| 99 | + dit_cpu_offload=False, |
| 100 | + text_encoder_cpu_offload=False, |
| 101 | + vae_cpu_offload=False, |
| 102 | + ) |
| 103 | + |
| 104 | + generator = VideoGenerator.from_pretrained( |
| 105 | + args.model_path, |
| 106 | + num_gpus=1, |
| 107 | + **offload_kwargs, |
| 108 | + ) |
| 109 | + |
| 110 | + result = generator.generate_audio( |
| 111 | + prompt=args.prompt, |
| 112 | + duration_seconds=args.duration, |
| 113 | + num_inference_steps=args.steps, |
| 114 | + guidance_scale=args.guidance_scale, |
| 115 | + seed=args.seed, |
| 116 | + ) |
| 117 | + |
| 118 | + generator.shutdown() |
| 119 | + |
| 120 | + save_audio_wav(result["audio"], result["sample_rate"], args.output) |
| 121 | + print(f"Saved audio to {args.output}") |
| 122 | + print(f" Shape: {result['audio'].shape}, sample_rate: {result['sample_rate']} Hz") |
| 123 | + if result.get("generation_time"): |
| 124 | + print(f" Generation time: {result['generation_time']:.1f}s") |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments