def dataAugment(self,bottom = 40, top = 85): print("start to augment data") #print("Be sure you get the chord functions before!") augment_data = [] if self.datasetName == "Nottingham": cl = Chord_Loader(recogLevel = self.recogLevel) for i in range(-5,7,1): for x in self.midi_files: midi_file = copy.deepcopy(x) is_add = True for j in range(len(midi_file["notes"])): if midi_file["notes"][j] <= 127: midi_file["notes"][j] += i if midi_file["notes"][j] > top or midi_file["notes"][j] < bottom: is_add = False break for j in range(len(midi_file["chord_seq"])): midi_file["chord_seq"][j] = cl.chord_alu(x = midi_file["chord_seq"][j],scalar = i) if is_add: midi_file["name"] += "-switch(" + str(i) + ")" augment_data.append(midi_file) print("finish augment %d data" % i) self.midi_files = augment_data # random.shuffle(self.midi_files) print("data augment success! %d files in total" % len(self.midi_files)) return self.midi_files print("Error: No dataset called " + self.datasetName) return False
def vae_make_one_hot_data(train_data): print("convert data to one-hot...", flush=True) train_size = min(len(train_data), 1001) train_x = np.zeros((train_size, total_len, pitch_num), dtype=np.int32) train_cond = np.zeros((train_size, total_len, chord_num), dtype=np.int32) # train_gd = np.zeros((train_size,total_len), dtype = np.int32) cl = Chord_Loader() # process with bi-directional issue for i, data in enumerate(train_data): if i >= train_size: break mi = data["notes"] ci = data["chords"] prev = rest_pitch for j, value in enumerate(mi): train_x[i, j, value] = 1 for j, value in enumerate(ci): cname = cl.index2name(j) cnotes = cl.name2note(cname) # print(cnotes) if cnotes is None: continue for k in cnotes: train_cond[i, j, k % 12] = 1 print("convert success!", flush=True) return [train_x, train_cond]
def getChordSeq(self, recogLevel = "Mm"): print("start to get chord sequences") self.recogLevel = recogLevel if self.datasetName == "Nottingham": # 25 one hot vectors # 0-11 for major # 12-23 for minor # 24 for NC for i in range(len(self.midi_files)): midi_data = self.midi_files[i]["raw"] cl = Chord_Loader(recogLevel = self.recogLevel) chord_set = [] chord_time = [0.0, 0.0] last_time = 0.0 # Define the chord recognition system chord_file = [] if len(midi_data.instruments) == 1: self.midi_files[i]["chords"] = {} self.midi_files[i]["chord_seq"] = [] continue self.midi_files[i]["chords"] = [] for note in midi_data.instruments[1].notes: if len(chord_set) == 0: chord_set.append(note.pitch) chord_time[0] = note.start chord_time[1] = note.end else: if note.start == chord_time[0] and note.end == chord_time[1]: chord_set.append(note.pitch) else: if last_time < chord_time[0]: self.midi_files[i]["chords"].append({"start":last_time ,"end": chord_time[0], "chord" : "NC"}) self.midi_files[i]["chords"].append({"start":chord_time[0],"end":chord_time[1],"chord": cl.note2name(chord_set)}) last_time = chord_time[1] chord_set = [] chord_set.append(note.pitch) chord_time[0] = note.start chord_time[1] = note.end if chord_set: if last_time < chord_time[0]: self.midi_files[i]["chords"].append({"start":last_time ,"end": chord_time[0], "chord" : "NC"}) self.midi_files[i]["chords"].append({"start":chord_time[0],"end":chord_time[1],"chord": cl.note2name(chord_set)}) last_time = chord_time[1] for c in self.midi_files[i]["chords"]: c_index = cl.name2index(c["chord"]) steps = int((c["end"] - c["start"]) / self.minStep) for j in range(steps): chord_file.append(c_index) self.midi_files[i]["chord_seq"] = chord_file print("calc chords success! %d files in total" % len(self.midi_files)) return self.midi_files if self.datasetName == "Irish": print("Error:Irish Folk Song dataset has no chord") return None print("Error: No dataset called " + self.datasetName) return None
def vae_make_one_hot_data(train_data): print("convert data to one-hot...",flush = True) train_size = min(len(train_data),200) train_x = np.zeros((train_size,total_len,pitch_num), dtype = np.int32) train_cond = np.zeros((train_size,total_len,chord_num), dtype = np.int32) # train_gd = np.zeros((train_size,total_len), dtype = np.int32) cl = Chord_Loader() # process with bi-directional issue for i,data in enumerate(train_data): if i >= train_size: break mi = data["notes"] ci = data["chords"] prev = rest_pitch for j,value in enumerate(mi): train_x[i, j, value] = 1 for j,value in enumerate(ci): cname = cl.index2name(j) cnotes = cl.name2note(cname) # print(cnotes) if cnotes is None: continue for k in cnotes: train_cond[i,j,k % 12] = 1 # for j, value in enumerate(mi): # if j < known_len: # if value != hold_pitch: # prev = value # if value == hold_pitch and mi[j + 1] != hold_pitch: # train_x[i,j,prev] = 1 # elif j + 1 == known_len and value == hold_pitch: # train_x[i,j,prev] = 1 # else: # train_x[i,j,value] = 1 # for j, value in enumerate(ci): # train_x[i,j, value + pitch_num] = 1 # prev = rest_pitch # for j, value in enumerate(mi): # if value != hold_pitch: # prev = value # if j + 1 == len(mi): # train_gd[i,j] = prev # elif value == hold_pitch and mi[j + 1] != hold_pitch: # train_gd[i,j] = prev # else: # train_gd[i,j] = value print("convert success!",flush = True) return [train_x,train_cond]
def text2midi(self, text_ad, recogLevel = "Mm",output = "test.mid"): gen_midi = pyd.PrettyMIDI() melodies = pyd.Instrument(program = pyd.instrument_name_to_program('Acoustic Grand Piano')) chords = pyd.Instrument(program = pyd.instrument_name_to_program('Acoustic Grand Piano')) if self.datasetName == "Nottingham": # 130 one hot vectors # 0-127 for pitch # 128 for hold 129 for rest rest_pitch = 129 hold_pitch = 128 with open(text_ad,"r") as f: lines = f.readlines() read_flag = "none" for line in lines: line = line.strip() # if line == "Chord:": # continue if line == "Chord Sequence:": read_flag = "chord_seq" continue if line == "Notes:": read_flag = "notes" continue if read_flag == "chord_seq": cl = Chord_Loader(recogLevel = recogLevel) elements = line.split(" ") time_shift = 0.0 local_duration = 0 prev = "NC" for chord in elements: if chord == "": continue chord = cl.index2name(x = int(chord)) if chord == prev: local_duration += 1 else: if prev == "NC": prev = chord time_shift += local_duration * self.minStep local_duration = 1 else: i_notes = cl.name2note(name = prev, stage = 4) for i_note in i_notes: i_note = pyd.Note(velocity = 100, pitch = i_note, start = time_shift, end = time_shift + local_duration * self.minStep) chords.notes.append(i_note) prev = chord time_shift += local_duration * self.minStep local_duration = 1 if prev != "NC": i_notes = cl.name2note(name = prev, stage = 4) for i_note in i_notes: i_note = pyd.Note(velocity = 100, pitch = i_note, start = time_shift, end = time_shift + local_duration * self.minStep) chords.notes.append(i_note) gen_midi.instruments.append(chords) continue if read_flag == "notes": elements = line.split(" ") time_shift = 0.0 local_duration = 0 prev = rest_pitch for note in elements: note = int(note) if note < 0 or note > 129: continue if note == hold_pitch: local_duration += 1 elif note == rest_pitch: time_shift += self.minStep else: if prev == rest_pitch: prev = note local_duration = 1 else: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) prev = note time_shift += local_duration * self.minStep local_duration = 1 if prev != rest_pitch: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) gen_midi.instruments.append(melodies) continue gen_midi.write(output) print("finish render midi on " + output)
def data2midi(self, data, recogLevel = "Mm", output = "test.mid"): gen_midi = pyd.PrettyMIDI() melodies = pyd.Instrument(program = pyd.instrument_name_to_program('Acoustic Grand Piano')) chords = pyd.Instrument(program = pyd.instrument_name_to_program('Acoustic Grand Piano')) if self.datasetName == "Nottingham": # 130 one hot vectors # 0-127 for pitch # 128 for hold 129 for rest rest_pitch = 129 hold_pitch = 128 cl = Chord_Loader(recogLevel = recogLevel) time_shift = 0.0 local_duration = 0 prev = "NC" for chord in data["chords"]: if chord == "": continue chord = cl.index2name(x = int(chord)) if chord == prev: local_duration += 1 else: if prev == "NC": prev = chord time_shift += local_duration * self.minStep local_duration = 1 else: i_notes = cl.name2note(name = prev, stage = 4) for i_note in i_notes: i_note = pyd.Note(velocity = 100, pitch = i_note, start = time_shift, end = time_shift + local_duration * self.minStep) chords.notes.append(i_note) prev = chord time_shift += local_duration * self.minStep local_duration = 1 if prev != "NC": i_notes = cl.name2note(name = prev, stage = 4) for i_note in i_notes: i_note = pyd.Note(velocity = 100, pitch = i_note, start = time_shift, end = time_shift + local_duration * self.minStep) chords.notes.append(i_note) gen_midi.instruments.append(chords) time_shift = 0.0 local_duration = 0 prev = rest_pitch for note in data["notes"]: note = int(note) if note < 0 or note > 129: continue if note == hold_pitch: local_duration += 1 elif note == rest_pitch: time_shift += self.minStep else: if prev == rest_pitch: prev = note local_duration = 1 else: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) prev = note time_shift += local_duration * self.minStep local_duration = 1 if prev != rest_pitch: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) gen_midi.instruments.append(melodies) gen_midi.write(output) print("finish render midi on " + output) if self.datasetName == "Irish": # 130 one hot vectors # 0-127 for pitch # 128 for hold 129 for rest rest_pitch = 129 hold_pitch = 128 local_duration = 0 time_shift = 0.0 local_duration = 0 prev = rest_pitch for note in data["notes"]: note = int(note) if note < 0 or note > 129: continue if note == hold_pitch: local_duration += 1 elif note == rest_pitch: time_shift += self.minStep else: if prev == rest_pitch: prev = note local_duration = 1 else: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) prev = note time_shift += local_duration * self.minStep local_duration = 1 if prev != rest_pitch: i_note = pyd.Note(velocity = 100, pitch = prev, start = time_shift, end = time_shift + local_duration * self.minStep) melodies.notes.append(i_note) gen_midi.instruments.append(melodies) gen_midi.write(output) print("finish render midi on " + output)