-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.py
More file actions
64 lines (52 loc) · 1.92 KB
/
Copy pathtokenizer.py
File metadata and controls
64 lines (52 loc) · 1.92 KB
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
import pandas as pd
import os
class Notes(object):
def __init__(self):
self.ntoi = {}
self.iton = []
def add_note(self, note):
if note not in self.ntoi:
self.iton.append(note)
self.ntoi[note] = len(self.iton) - 1
return self.ntoi[note]
def __len__(self):
return len(self.iton)
class Tokenizer(object):
def __init__(self, base_path):
self.notes = Notes()
self.end_of_song_token = "<EOS>"
self.base_path = base_path
self.max_duration = None
self.max_velocity = None
self.notes.add_note(self.end_of_song_token)
def tokenize(self, path):
assert os.path.exists(self.base_path + path)
data = pd.read_csv(self.base_path + path)
ids = []
durations = []
velocities = []
for _, row in data.iterrows():
ids.append(self.notes.add_note(row['note_name']))
durations.append(row['duration'])
velocities.append(row['velocity'])
ids.append(self.notes.ntoi[self.end_of_song_token])
durations.append(0.0) # EOS
velocities.append(0.0) # EOS
return ids, durations, velocities
def tokenize_multiple_files(self, paths):
combined_ids = []
combined_durations = []
combined_velocities = []
for path in paths:
if '.csv' not in path or 'midi_notes' in path:
continue
song_ids, durations, velocities = self.tokenize(path)
combined_ids.extend(song_ids)
combined_durations.extend(durations)
combined_velocities.extend(velocities)
self.max_duration = max(combined_durations)
self.max_velocity = 127
return combined_ids, combined_durations, combined_velocities
def decode(self, ids):
notes = [self.notes.iton[i] for i in ids]
return notes