def load_files(self): self.buf = [] done = 0 bad_files = [] for filename in self.filenames: # update the progress bar to show it's working on the current file bothoven_globals.progress_bar(done, self.filenames.size, "Buffering " + filename + "...") # for progress tracking done += 1 try: file_buf = self.array_builder_type(filename).mid_to_array() except Exception as e: bad_files.append(done - 1) # done - 1 is the index print("\nError!", e) print() continue # slap this file's buffer onto the back of our running buffer self.buf.append(file_buf) # finish off the progress bar bothoven_globals.progress_bar(done, self.filenames.size, "Buffering complete!") self.filenames = np.delete(self.filenames, bad_files)
def build_csv(cache_dir): files = get_filenames(cache_dir, [".pkl"]) done = 0 csv_path = os.path.join(cache_dir, "key_signatures.csv") if os.path.exists(csv_path) and os.path.getsize(csv_path) > 0: df = pd.read_csv(csv_path, index_col="path") else: df = pd.DataFrame(columns=["tonic", "mode"]) df.index.name = "path" csv_f = open(csv_path, "a") for file in files: progress_bar(done, len(files), file) done += 1 if file in df.index.values: continue score = pickle_load(file, verbose=False) key = score.analyze('key') df.loc[file] = [key.tonic.name, key.mode] s = ",".join([f'"{file}"', key.tonic.name, key.mode]) + "\n" csv_f.write(s) csv_f.flush() csv_f.close() progress_bar(done, len(files), "Done!") return df
def cache(in_dir, out_dir): files = get_filenames(in_dir, [".mid", ".midi", ".smf"]) done = 0 csv_path = os.path.join(out_dir, "key_signatures.csv") os.makedirs(out_dir, exist_ok=True) if os.path.exists(csv_path) and os.path.getsize(csv_path) > 0: df = pd.read_csv(csv_path, index_col="path") needs_column_header = False else: df = pd.DataFrame(columns=["tonic", "mode"]) df.index.name = "path" needs_column_header = True csv_f = open(csv_path, "a") if needs_column_header: csv_f.write("path,tonic,mode\n") for file in files: progress_bar(done, len(files), "Loading:".ljust(11) + file + "...") done += 1 relative_path = file[len(in_dir):] if relative_path[0] == "/": relative_path = relative_path[1:] new_file = os.path.join(out_dir, relative_path) + ".pkl" if new_file in df.index.values: continue new_dir = os.path.dirname(new_file) os.makedirs(new_dir, exist_ok=True) try: score = m21.converter.parse(file) except Exception as e: continue progress_bar(done - 1, len(files), "Analyzing:".ljust(12) + file + "...") key = score.analyze('key') csv_f.write(",".join([f'"{new_file}"', key.tonic.name, key.mode]) + "\n") csv_f.flush() m21.converter.freeze(score, fp=new_file) csv_f.close() progress_bar(done, len(files), "Done!")
def doit(seed_file): print(" -> Loading seed song...") seed = song_buf_to_onehot( Music21ArrayBuilder("midi/format0_maj/" + seed_file).mid_to_array(), dataset) note_temp = .1 while note_temp <= 1.5: duration_temp = .1 while duration_temp <= 1.5: output_file = f"output/{seed_file[:-12]}/note_temp-{float(round(note_temp, 1))}__duration_temp-{float(round(duration_temp, 1))}.mid" print(f"Saving '{output_file}'...") new_song = np.vstack([ seed[start_i:start_i + NUM_STEPS], np.zeros((num_predictions, dataset.num_features)) ]) score = music21.stream.Score() part = music21.stream.Part() part.id = 'part1' current_list = part notes_in_chord = 0 for i in range(num_predictions): p = model.predict( np.array([seed[i + start_i:i + start_i + NUM_STEPS]])) p_note = sample(p[0][0], temperature=note_temp) p_duration = sample(p[1][0], temperature=duration_temp) p_offset = np.argmax(p[2][0]) new_song[i + NUM_STEPS, p_note] = 1 new_song[i + NUM_STEPS, p_duration + p[0].shape[1]] = 1 new_song[i + NUM_STEPS, p_offset + p[0].shape[1] + p[1].shape[1]] = 1 note = dataset.one_hot_to_note[p_note] duration = dataset.one_hot_to_duration[p_duration] offset = dataset.one_hot_to_offset[p_offset] if offset != -1 and offset != -2: if note == -3 or duration == -3 or offset == -3: if notes_in_chord: part.append(music21.chord.Chord(current_list)) notes_in_chord = 0 current_list = [] elif note == -4 or duration == -4 or offset == -4: if notes_in_chord: part.append(music21.chord.Chord(current_list)) notes_in_chord = 0 current_list = part elif note >= 0 and duration >= 0: if notes_in_chord >= 4: part.append(music21.chord.Chord(current_list)) notes_in_chord = 0 current_list = part current_list.append( music21.note.Note(note, quarterLength=duration)) if not current_list is part: notes_in_chord += 1 progress_bar(i + 1, num_predictions, output_file, clear_when_done=True) score.insert(0, part) os.makedirs(os.path.dirname(output_file), exist_ok=True) score.write('midi', fp=output_file) # upload_file(output_file) duration_temp += 0.1 note_temp += 0.1
import os from bothoven_globals import progress_bar from functions.file_functions import get_filenames in_dir = "/home/mark/Documents/raw_midi" out_dir = "/media/mark/Data/Documents/python/bothoven/midi/monophonic" good_programs = set(range(56, 80)) filenames = get_filenames(in_dir, [".mid", ".midi", ".smf"]) total = len(filenames) done = 0 for filename in filenames: progress_bar(done, total, filename) done += 1 mid = None try: mid = mido.MidiFile(filename) except KeyboardInterrupt as e: exit(1) except Exception as e: print("There was an error reading", filename) print(e) continue good_tracks = [] for track in mid.tracks: