/
sequence_decoding.py
92 lines (68 loc) · 2.79 KB
/
sequence_decoding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Provides the functionality to convert encoded sequences into midi files.
"""
import music21
import pandas as pd
import pygame
from tqdm import tqdm
def load_sequences_from_file(filename):
sequences = pd.read_csv(filename)
sequences = list(sequences["0"])
return sequences
def create_note(pitch, offset, duration):
duration = music21.duration.Duration(duration)
note = music21.note.Note(pitch, duration=duration)
note.offset = offset
return note
def create_midi_file(sequence, output_filename, step_size=(1/12), max_length=1):
sequence = sequence.split(" ")
notes = list()
current_notes = list()
steps = 0
for element in sequence:
if element == "xxbos" or element == "":
continue
elif element == "step":
steps += 1
delete_notes = list()
for i, note in enumerate(current_notes):
note[2] += 1
if note[2] >= max_length / step_size:
new_note = create_note(note[0] + 36, note[1] * step_size, note[2] * step_size)
notes.append(new_note)
delete_notes.append(note)
for note in delete_notes:
current_notes.remove(note)
elif "stop" in element:
pitch = int(element[4:])
current_pitches = [n[0] for n in current_notes]
if pitch in current_pitches:
index = current_pitches.index(pitch)
note = current_notes[index]
new_note = create_note(note[0] + 36, note[1] * step_size, note[2] * step_size)
notes.append(new_note)
del current_notes[index]
else:
pitch = int(element)
offset_steps = steps
current_notes.append([pitch, offset_steps, 0])
for note in current_notes:
new_note = create_note(note[0] + 36, note[1] * step_size, note[2] * step_size)
notes.append(new_note)
piano = music21.instrument.fromString("Piano")
notes.insert(0, piano)
stream = music21.stream.Stream(notes)
output_path = "data/generated_midi_files/{}".format(output_filename)
stream.write(fmt="midi", fp=output_path)
def convert_all_sequences_in_file(filename):
sequences = load_sequences_from_file(filename)
for i, sequence in tqdm(enumerate(sequences)):
create_midi_file(sequence, "generated_{}.mid".format(i))
def play_midi_file(filename):
pygame.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.wait(1000)
#convert_all_sequences_in_file("data/generated_sequences/major_v2/lr_reduction.csv")
#play_midi_file("data/generated_midi_files/generated_9.mid")
play_midi_file("data/exemplary_examples/major/300hs_10bs_001lr_0.mid")