Audio Diffusion is by Robert Dargavel Smith, and it leverages the recent advances in image generation from diffusion models by converting audio samples to and from Mel spectrogram images.
The original codebase, training scripts and example notebooks can be found at teticio/audio-diffusion.
Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.
( vqvae: AutoencoderKL unet: UNet2DConditionModel mel: Mel scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_ddpm.DDPMScheduler] )
Parameters
UNet2DConditionModel
to denoise the encoded image latents. unet
to denoise the encoded image latents. Can be one of
DDIMScheduler or DDPMScheduler. Pipeline for audio diffusion.
This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
( batch_size: int = 1 audio_file: str = None raw_audio: ndarray = None slice: int = 0 start_step: int = 0 steps: int = None generator: Generator = None mask_start_secs: float = 0 mask_end_secs: float = 0 step_generator: Generator = None eta: float = 0 noise: Tensor = None encoding: Tensor = None return_dict = True ) → List[PIL Image]
Parameters
int
) —
Number of samples to generate. str
) —
An audio file that must be on disk due to Librosa limitation. np.ndarray
) —
The raw audio file as a NumPy array. int
) —
Slice number of audio to convert. int
) —
Number of denoising steps (defaults to 50
for DDIM and 1000
for DDPM). torch.Generator
) —
A torch.Generator
to make
generation deterministic. float
) —
Number of seconds of audio to mask (not generate) at start. float
) —
Number of seconds of audio to mask (not generate) at end. torch.Generator
) —
A torch.Generator
used to denoise.
None float
) —
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the DDIMScheduler, and is ignored in other schedulers. torch.Tensor
) —
A noise tensor of shape (batch_size, 1, height, width)
or None
. torch.Tensor
) —
A tensor for UNet2DConditionModel of shape (batch_size, seq_length, cross_attention_dim)
. bool
) —
Whether or not to return a AudioPipelineOutput, ImagePipelineOutput or a plain tuple. Returns
List[PIL Image]
A list of Mel spectrograms (float
, List[np.ndarray]
) with the sample rate and raw audio.
The call function to the pipeline for generation.
Examples:
For audio diffusion:
import torch
from IPython.display import Audio
from diffusers import DiffusionPipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = DiffusionPipeline.from_pretrained("teticio/audio-diffusion-256").to(device)
output = pipe()
display(output.images[0])
display(Audio(output.audios[0], rate=mel.get_sample_rate()))
For latent audio diffusion:
import torch
from IPython.display import Audio
from diffusers import DiffusionPipeline
device = "cuda" if torch.cuda.is_available() else "cpu"
pipe = DiffusionPipeline.from_pretrained("teticio/latent-audio-diffusion-256").to(device)
output = pipe()
display(output.images[0])
display(Audio(output.audios[0], rate=pipe.mel.get_sample_rate()))
( images: typing.List[PIL.Image.Image] steps: int = 50 ) → np.ndarray
Reverse the denoising step process to recover a noisy image from the generated image.
Returns default number of steps recommended for inference.
( x0: Tensor x1: Tensor alpha: float ) → torch.Tensor
Spherical Linear intERPolation.
( audios: ndarray )
Output class for audio pipelines.
( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Output class for image pipelines.
( x_res: int = 256 y_res: int = 256 sample_rate: int = 22050 n_fft: int = 2048 hop_length: int = 512 top_db: int = 80 n_iter: int = 32 )
Parameters
int
) —
x resolution of spectrogram (time). int
) —
y resolution of spectrogram (frequency bins). int
) —
Sample rate of audio. int
) —
Number of Fast Fourier Transforms. int
) —
Hop length (a higher number is recommended if y_res
< 256). int
) —
Loudest decibel value. int
) —
Number of iterations for Griffin-Lim Mel inversion. ( slice: int ) → PIL Image
Convert slice of audio to spectrogram.
( slice: int = 0 ) → np.ndarray
Get slice of audio.
( ) → int
Returns
int
Number of spectograms audio can be sliced into.
Get number of slices in audio.
Get sample rate.
( image: Image ) → audio (np.ndarray
)
Converts spectrogram to audio.
( audio_file: str = None raw_audio: ndarray = None )
Parameters
str
) —
An audio file that must be on disk due to Librosa limitation. np.ndarray
) —
The raw audio file as a NumPy array. Load audio.
( x_res: int y_res: int )
Set resolution.