mirror of
https://github.com/hanzoai/mugen.git
synced 2026-11-03 01:48:40 +00:00
7.7 KiB
7.7 KiB
In [1]:
from audiocraft.models import MusicGen
from audiocraft.models import MultiBandDiffusion
USE_DIFFUSION_DECODER = False
# Using small model, better results would be obtained with `medium` or `large`.
model = MusicGen.get_pretrained('facebook/musicgen-small')
if USE_DIFFUSION_DECODER:
mbd = MultiBandDiffusion.get_mbd_musicgen()In [ ]:
model.set_generation_params(
use_sampling=True,
top_k=250,
duration=30
)In [ ]:
import math
import torchaudio
import torch
from audiocraft.utils.notebook import display_audio
def get_bip_bip(bip_duration=0.125, frequency=440,
duration=0.5, sample_rate=32000, device="cuda"):
"""Generates a series of bip bip at the given frequency."""
t = torch.arange(
int(duration * sample_rate), device="cuda", dtype=torch.float) / sample_rate
wav = torch.cos(2 * math.pi * 440 * t)[None]
tp = (t % (2 * bip_duration)) / (2 * bip_duration)
envelope = (tp >= 0.5).float()
return wav * envelopeIn [ ]:
# Here we use a synthetic signal to prompt both the tonality and the BPM
# of the generated audio.
res = model.generate_continuation(
get_bip_bip(0.125).expand(2, -1, -1),
32000, ['Jazz jazz and only jazz',
'Heartful EDM with beautiful synths and chords'],
progress=True)
display_audio(res, 32000)In [ ]:
# You can also use any audio from a file. Make sure to trim the file if it is too long!
prompt_waveform, prompt_sr = torchaudio.load("../assets/bach.mp3")
prompt_duration = 2
prompt_waveform = prompt_waveform[..., :int(prompt_duration * prompt_sr)]
output = model.generate_continuation(prompt_waveform, prompt_sample_rate=prompt_sr, progress=True, return_tokens=True)
display_audio(output[0], sample_rate=32000)
if USE_DIFFUSION_DECODER:
out_diffusion = mbd.tokens_to_wav(output[1])
display_audio(out_diffusion, sample_rate=32000)In [ ]:
from audiocraft.utils.notebook import display_audio
output = model.generate(
descriptions=[
#'80s pop track with bassy drums and synth',
#'90s rock song with loud guitars and heavy drums',
#'Progressive rock drum and bass solo',
#'Punk Rock song with loud drum and power guitar',
#'Bluesy guitar instrumental with soulful licks and a driving rhythm section',
#'Jazz Funk song with slap bass and powerful saxophone',
'drum and bass beat with intense percussions'
],
progress=True, return_tokens=True
)
display_audio(output[0], sample_rate=32000)
if USE_DIFFUSION_DECODER:
out_diffusion = mbd.tokens_to_wav(output[1])
display_audio(out_diffusion, sample_rate=32000)In [ ]:
import torchaudio
from audiocraft.utils.notebook import display_audio
model = MusicGen.get_pretrained('facebook/musicgen-melody')
model.set_generation_params(duration=8)
melody_waveform, sr = torchaudio.load("../assets/bach.mp3")
melody_waveform = melody_waveform.unsqueeze(0).repeat(2, 1, 1)
output = model.generate_with_chroma(
descriptions=[
'80s pop track with bassy drums and synth',
'90s rock song with loud guitars and heavy drums',
],
melody_wavs=melody_waveform,
melody_sample_rate=sr,
progress=True, return_tokens=True
)
display_audio(output[0], sample_rate=32000)
if USE_DIFFUSION_DECODER:
out_diffusion = mbd.tokens_to_wav(output[1])
display_audio(out_diffusion, sample_rate=32000)