thecollabagepatch commited on
Commit
49f36fd
1 Parent(s): 1aa5fa0

index out of range error fix

Browse files
Files changed (1) hide show
  1. app.py +111 -100
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
- random_start = random.choice(range(int(slice_duration * sr), int(song_length * sr), int(4 * 60 / bpm * sr)))
 
 
 
 
 
 
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
- if seed == "":
69
- seed = random.randint(1, 10000)
70
-
71
- ml = MusicLangPredictor('musiclang/musiclang-v2')
72
-
73
- try:
74
- seed = int(seed)
75
- except ValueError:
76
- seed = random.randint(1, 10000)
77
-
78
- nb_tokens = 2048
79
- temperature = 0.9
80
- top_p = 1.0
81
-
82
- if use_chords and chord_progression.strip():
83
- score = ml.predict_chords(
84
- chord_progression,
85
- time_signature=(4, 4),
86
- temperature=temperature,
87
- topp=top_p,
88
- rng_seed=seed
89
- )
90
- else:
91
- score = ml.predict(
92
- nb_tokens=nb_tokens,
93
- temperature=temperature,
94
- topp=top_p,
95
- rng_seed=seed
96
- )
97
-
98
- midi_filename = f"output_{seed}.mid"
99
- wav_filename = midi_filename.replace(".mid", ".wav")
100
-
101
- score.to_midi(midi_filename, tempo=bpm, time_signature=(4, 4))
102
-
103
- subprocess.run(["fluidsynth", "-ni", "font.sf2", midi_filename, "-F", wav_filename, "-r", "44100"])
104
-
105
- # Load the generated audio
106
- song, sr = torchaudio.load(wav_filename)
107
- song = song.to(device)
108
-
109
- # Use the user-provided BPM value for duration calculation
110
- duration = calculate_duration(bpm)
111
-
112
- # Create slices from the song using the user-provided BPM value
113
- slices = create_slices(song, sr, 35, bpm, num_slices=5)
114
-
115
- # Load the model
116
- model_name = musicgen_model.split(" ")[0]
117
- model_continue = MusicGen.get_pretrained(model_name)
118
-
119
- # Setting generation parameters
120
- model_continue.set_generation_params(
121
- use_sampling=True,
122
- top_k=250,
123
- top_p=0.0,
124
- temperature=1.0,
125
- duration=duration,
126
- cfg_coef=3
127
- )
128
-
129
- all_audio_files = []
130
-
131
- for i in range(num_iterations):
132
- slice_idx = i % len(slices)
133
-
134
- print(f"Running iteration {i + 1} using slice {slice_idx}...")
135
-
136
- prompt_waveform = slices[slice_idx][..., :int(prompt_duration * sr)]
137
- prompt_waveform = preprocess_audio(prompt_waveform)
138
-
139
- output = model_continue.generate_continuation(prompt_waveform, prompt_sample_rate=sr, progress=True)
140
- output = output.cpu() # Move the output tensor back to CPU
141
-
142
- # Make sure the output tensor has at most 2 dimensions
143
- if len(output.size()) > 2:
144
- output = output.squeeze()
145
-
146
- filename_without_extension = f'continue_{i}'
147
- filename_with_extension = f'{filename_without_extension}.wav'
148
-
149
- audio_write(filename_with_extension, output, model_continue.sample_rate, strategy="loudness", loudness_compressor=True)
150
- all_audio_files.append(f'{filename_without_extension}.wav.wav') # Assuming the library appends an extra .wav
151
-
152
- # Combine all audio files
153
- combined_audio = AudioSegment.empty()
154
- for filename in all_audio_files:
155
- combined_audio += AudioSegment.from_wav(filename)
156
-
157
- combined_audio_filename = f"combined_audio_{seed}.mp3"
158
- combined_audio.export(combined_audio_filename, format="mp3")
159
-
160
- # Clean up temporary files
161
- os.remove(midi_filename)
162
- os.remove(wav_filename)
163
- for filename in all_audio_files:
164
- os.remove(filename)
165
-
166
- return combined_audio_filename
 
 
 
 
 
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")