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 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)