Spaces:
Sleeping
Sleeping
thecollabagepatch
commited on
Commit
•
49f36fd
1
Parent(s):
1aa5fa0
index out of range error fix
Browse files
app.py
CHANGED
@@ -31,7 +31,13 @@ def create_slices(song, sr, slice_duration, bpm, num_slices=5):
|
|
31 |
slices.append(first_slice_waveform)
|
32 |
|
33 |
for i in range(1, num_slices):
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
slice_end = random_start + int(slice_duration * sr)
|
36 |
|
37 |
if slice_end > song_length * sr:
|
@@ -65,105 +71,110 @@ def calculate_duration(bpm, min_duration=29, max_duration=30):
|
|
65 |
return duration
|
66 |
|
67 |
def generate_music(seed, use_chords, chord_progression, prompt_duration, musicgen_model, num_iterations, bpm):
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
chord_progression
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
# Check if CUDA is available
|
169 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
31 |
slices.append(first_slice_waveform)
|
32 |
|
33 |
for i in range(1, num_slices):
|
34 |
+
possible_start_indices = list(range(int(slice_duration * sr), int(song_length * sr), int(4 * 60 / bpm * sr)))
|
35 |
+
if not possible_start_indices:
|
36 |
+
# If there are no valid start indices, duplicate the first slice
|
37 |
+
slices.append(first_slice_waveform)
|
38 |
+
continue
|
39 |
+
|
40 |
+
random_start = random.choice(possible_start_indices)
|
41 |
slice_end = random_start + int(slice_duration * sr)
|
42 |
|
43 |
if slice_end > song_length * sr:
|
|
|
71 |
return duration
|
72 |
|
73 |
def generate_music(seed, use_chords, chord_progression, prompt_duration, musicgen_model, num_iterations, bpm):
|
74 |
+
while True:
|
75 |
+
try:
|
76 |
+
if seed == "":
|
77 |
+
seed = random.randint(1, 10000)
|
78 |
+
|
79 |
+
ml = MusicLangPredictor('musiclang/musiclang-v2')
|
80 |
+
|
81 |
+
try:
|
82 |
+
seed = int(seed)
|
83 |
+
except ValueError:
|
84 |
+
seed = random.randint(1, 10000)
|
85 |
+
|
86 |
+
nb_tokens = 1024
|
87 |
+
temperature = 0.9
|
88 |
+
top_p = 1.0
|
89 |
+
|
90 |
+
if use_chords and chord_progression.strip():
|
91 |
+
score = ml.predict_chords(
|
92 |
+
chord_progression,
|
93 |
+
time_signature=(4, 4),
|
94 |
+
temperature=temperature,
|
95 |
+
topp=top_p,
|
96 |
+
rng_seed=seed
|
97 |
+
)
|
98 |
+
else:
|
99 |
+
score = ml.predict(
|
100 |
+
nb_tokens=nb_tokens,
|
101 |
+
temperature=temperature,
|
102 |
+
topp=top_p,
|
103 |
+
rng_seed=seed
|
104 |
+
)
|
105 |
+
|
106 |
+
midi_filename = f"output_{seed}.mid"
|
107 |
+
wav_filename = midi_filename.replace(".mid", ".wav")
|
108 |
+
|
109 |
+
score.to_midi(midi_filename, tempo=bpm, time_signature=(4, 4))
|
110 |
+
|
111 |
+
subprocess.run(["fluidsynth", "-ni", "font.sf2", midi_filename, "-F", wav_filename, "-r", "44100"])
|
112 |
+
|
113 |
+
# Load the generated audio
|
114 |
+
song, sr = torchaudio.load(wav_filename)
|
115 |
+
song = song.to(device)
|
116 |
+
|
117 |
+
# Use the user-provided BPM value for duration calculation
|
118 |
+
duration = calculate_duration(bpm)
|
119 |
+
|
120 |
+
# Create slices from the song using the user-provided BPM value
|
121 |
+
slices = create_slices(song, sr, 35, bpm, num_slices=5)
|
122 |
+
|
123 |
+
# Load the model
|
124 |
+
model_name = musicgen_model.split(" ")[0]
|
125 |
+
model_continue = MusicGen.get_pretrained(model_name)
|
126 |
+
|
127 |
+
# Setting generation parameters
|
128 |
+
model_continue.set_generation_params(
|
129 |
+
use_sampling=True,
|
130 |
+
top_k=250,
|
131 |
+
top_p=0.0,
|
132 |
+
temperature=1.0,
|
133 |
+
duration=duration,
|
134 |
+
cfg_coef=3
|
135 |
+
)
|
136 |
+
|
137 |
+
all_audio_files = []
|
138 |
+
|
139 |
+
for i in range(num_iterations):
|
140 |
+
slice_idx = i % len(slices)
|
141 |
+
|
142 |
+
print(f"Running iteration {i + 1} using slice {slice_idx}...")
|
143 |
+
|
144 |
+
prompt_waveform = slices[slice_idx][..., :int(prompt_duration * sr)]
|
145 |
+
prompt_waveform = preprocess_audio(prompt_waveform)
|
146 |
+
|
147 |
+
output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
|
148 |
+
output = output.cpu() # Move the output tensor back to CPU
|
149 |
+
|
150 |
+
# Make sure the output tensor has at most 2 dimensions
|
151 |
+
if len(output.size()) > 2:
|
152 |
+
output = output.squeeze()
|
153 |
+
|
154 |
+
filename_without_extension = f'continue_{i}'
|
155 |
+
filename_with_extension = f'{filename_without_extension}.wav'
|
156 |
+
|
157 |
+
audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
|
158 |
+
all_audio_files.append(f'{filename_without_extension}.wav.wav') # Assuming the library appends an extra .wav
|
159 |
+
|
160 |
+
# Combine all audio files
|
161 |
+
combined_audio = AudioSegment.empty()
|
162 |
+
for filename in all_audio_files:
|
163 |
+
combined_audio += AudioSegment.from_wav(filename)
|
164 |
+
|
165 |
+
combined_audio_filename = f"combined_audio_{seed}.mp3"
|
166 |
+
combined_audio.export(combined_audio_filename, format="mp3")
|
167 |
+
|
168 |
+
# Clean up temporary files
|
169 |
+
os.remove(midi_filename)
|
170 |
+
os.remove(wav_filename)
|
171 |
+
for filename in all_audio_files:
|
172 |
+
os.remove(filename)
|
173 |
+
|
174 |
+
return combined_audio_filename
|
175 |
+
except IndexError:
|
176 |
+
# Retry with a new random seed if an IndexError is raised
|
177 |
+
seed = random.randint(1, 10000)
|
178 |
|
179 |
# Check if CUDA is available
|
180 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|