DEV Community

Cover image for Run MusicGen-stereo on Google Colab (Free-tier)
0xkoji
0xkoji

Posted on

Run MusicGen-stereo on Google Colab (Free-tier)

MusicGen Stereo
https://medium.com/r/?url=https%3A%2F%2Fhuggingface.co%2Ffacebook%2Fmusicgen-stereo-medium

Run MusicGen on Google Colab T4

# install packages
!python3 -m pip install -U git+https://github.com/facebookresearch/audiocraft#egg=audiocraft

# import packages
from audiocraft.models import musicgen
from audiocraft.utils.notebook import display_audio
import torch

# generate music
# In this case, we'll try to generate a couple of music
prompts = ['futuristic rock music', 'futuristic jazz music'] # describe the music you want to generate

model = musicgen.MusicGen.get_pretrained('facebook/musicgen-stereo-medium')
model.set_generation_params(duration=60)

res = model.generate(prompts, 
    progress=True)
display_audio(res, 32000)
Enter fullscreen mode Exit fullscreen mode

export music as a wav file.

from audiocraft.data.audio import audio_write

for idx, one_wav in enumerate(res):
    audio_write(f'generated_music_{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness")
Enter fullscreen mode Exit fullscreen mode

The following is what I generated. Both of prompts were generated by ChatGPT๐Ÿ˜

Cosmic Rhythm
https://soundcloud.com/koji-kanao/cosmic-rhythm

Fusion Flow
https://soundcloud.com/koji-kanao/fusion-flow

Top comments (0)