Spaces:
Running
on
T4
Running
on
T4
Update Melody conditioning with -1 segment
Browse files- audiocraft/utils/extend.py +16 -2
audiocraft/utils/extend.py
CHANGED
|
@@ -114,8 +114,9 @@ def generate_music_segments(text, melody, seed, MODEL, duration:int=10, overlap:
|
|
| 114 |
sr, verse = melody_segments[segment_idx][0], torch.from_numpy(melody_segments[segment_idx][1]).to(MODEL.device).float().t().unsqueeze(0)
|
| 115 |
|
| 116 |
print(f"shape:{verse.shape} dim:{verse.dim()}")
|
|
|
|
| 117 |
if verse.dim() == 2:
|
| 118 |
-
|
| 119 |
verse = verse[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]
|
| 120 |
|
| 121 |
# Append the segment to the melodys list
|
|
@@ -182,16 +183,29 @@ def generate_music_segments(text, melody, seed, MODEL, duration:int=10, overlap:
|
|
| 182 |
|
| 183 |
|
| 184 |
print(f"Generating New Melody Segment {idx + 1}: {text}\r")
|
| 185 |
-
output = MODEL.generate_with_all(
|
| 186 |
descriptions=[text],
|
| 187 |
melody_wavs=verse,
|
| 188 |
sample_rate=sr,
|
| 189 |
progress=True,
|
| 190 |
prompt=prompt_segment,
|
|
|
|
| 191 |
)
|
| 192 |
# If user selects a prompt segment, use the prompt segment for all segments
|
| 193 |
# Otherwise, use the previous segment as the prompt
|
| 194 |
if prompt_index < 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
prompt_segment = output
|
| 196 |
|
| 197 |
# Append the generated output to the list of segments
|
|
|
|
| 114 |
sr, verse = melody_segments[segment_idx][0], torch.from_numpy(melody_segments[segment_idx][1]).to(MODEL.device).float().t().unsqueeze(0)
|
| 115 |
|
| 116 |
print(f"shape:{verse.shape} dim:{verse.dim()}")
|
| 117 |
+
#if verse is 2D, add 3rd dimension
|
| 118 |
if verse.dim() == 2:
|
| 119 |
+
verse = verse[None]
|
| 120 |
verse = verse[..., :int(sr * MODEL.lm.cfg.dataset.segment_duration)]
|
| 121 |
|
| 122 |
# Append the segment to the melodys list
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
print(f"Generating New Melody Segment {idx + 1}: {text}\r")
|
| 186 |
+
output, tokens = MODEL.generate_with_all(
|
| 187 |
descriptions=[text],
|
| 188 |
melody_wavs=verse,
|
| 189 |
sample_rate=sr,
|
| 190 |
progress=True,
|
| 191 |
prompt=prompt_segment,
|
| 192 |
+
return_tokens = True
|
| 193 |
)
|
| 194 |
# If user selects a prompt segment, use the prompt segment for all segments
|
| 195 |
# Otherwise, use the previous segment as the prompt
|
| 196 |
if prompt_index < 0:
|
| 197 |
+
if harmony_only:
|
| 198 |
+
# REMOVE PERCUSION FROM MELODY
|
| 199 |
+
# Apply HPSS using librosa
|
| 200 |
+
verse_harmonic, verse_percussive = librosa.effects.hpss(output.detach().cpu().numpy())
|
| 201 |
+
# Convert the separated components back to torch.Tensor
|
| 202 |
+
#harmonic_tensor = torch.from_numpy(verse_harmonic)
|
| 203 |
+
#percussive_tensor = torch.from_numpy(verse_percussive)
|
| 204 |
+
verse = torch.from_numpy(verse_harmonic).to(MODEL.device).float()
|
| 205 |
+
# if verse is 2D, add extra dimension
|
| 206 |
+
if verse.dim() == 2:
|
| 207 |
+
verse = verse[None]
|
| 208 |
+
output = verse
|
| 209 |
prompt_segment = output
|
| 210 |
|
| 211 |
# Append the generated output to the list of segments
|