Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active April 27, 2024 13:58
Show Gist options
  • Save twobob/f4b9fb0b42e9a4f1128ded662f0fb41b to your computer and use it in GitHub Desktop.
Save twobob/f4b9fb0b42e9a4f1128ded662f0fb41b to your computer and use it in GitHub Desktop.
860s
import os
import torchaudio
import string
import time
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write
from datetime import datetime
import demucs.separate
import shlex
# Start timing the operation
start_time = time.time()
# Generate a timestamped folder name
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
folder_name = f"generated\\{timestamp}"
# Ensure the directory exists
os.makedirs(folder_name, exist_ok=True)
#model = MusicGen.get_pretrained('facebook/musicgen-stereo-large') # and other models on huggingface
model = MusicGen.get_pretrained('facebook/musicgen-stereo-melody-large')
model.set_generation_params(duration=860, # 860 is seconds... lower it to your needs. 860 is cap on 128 GB machine with 24GB VRAM
temperature=1.08,
cfg_coef=4.0)
#descriptions = ['bassline house oldschool EDM IDM']
descriptions = [ 'solemn imperial virtuoso Budhism, classical string and melodic operatic symphony Budhist mozart' ]
melody, sr = torchaudio.load("c:\somepath\someReferenceMp3orWavTrack.wav")
wav = model.generate_with_chroma(descriptions, melody[None].expand(len(descriptions), -1, -1), sr)
def sanitize_filename(name):
"""Sanitize the string to be safe for filenames."""
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
sanitized = ''.join(c for c in name if c in valid_chars)
return sanitized[:200]
for idx, one_wav in enumerate(wav):
description_sanitized = sanitize_filename(descriptions[idx])
file_path = os.path.join(folder_name, f'{idx}_{timestamp}_{description_sanitized}')
# Will save under {folder_name}/{idx}_{description_sanitized}.wav, with loudness normalization at -14 db LUFS.
audio_write(file_path, one_wav.cpu(), model.sample_rate, strategy="loudness")
# splits the wave into optional chunks for remixing.
demucs.separate.main(shlex.split(f'-n mdx_extra --shifts 1 --overlap 0.15 --float32 --clip-mode clamp \'{file_path}\'.wav'))
end_time = time.time()
total_time = end_time - start_time
print(f"Total time taken for the operation: {total_time:.2f} seconds.")
@twobob
Copy link
Author

twobob commented Apr 27, 2024

return sanitized[:200]
prevent vlc and windows freaking out over long file names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment