This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
80 lines (68 loc) · 2.73 KB
/
utils.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
import os
import numpy as np
from sequence import EventSeq, ControlSeq
def find_files_by_extensions(root, exts=[]):
def _has_ext(name):
if not exts:
return True
name = name.lower()
for ext in exts:
if name.endswith(ext):
return True
return False
for path, _, files in os.walk(root):
for name in files:
if _has_ext(name):
yield os.path.join(path, name)
def event_indeces_to_midi_file(event_indeces, midi_file_name, velocity_scale=0.8):
event_seq = EventSeq.from_array(event_indeces)
note_seq = event_seq.to_note_seq()
for note in note_seq.notes:
note.velocity = int((note.velocity - 64) * velocity_scale + 64)
note_seq.to_midi_file(midi_file_name)
return len(note_seq.notes)
def transposition(events, controls, offset=0):
# events [steps, batch_size, event_dim]
# return events, controls
events = np.array(events, dtype=np.int64)
controls = np.array(controls, dtype=np.float32)
event_feat_ranges = EventSeq.feat_ranges()
on = event_feat_ranges['note_on']
off = event_feat_ranges['note_off']
if offset > 0:
indeces0 = (((on.start <= events) & (events < on.stop - offset)) |
((off.start <= events) & (events < off.stop - offset)))
indeces1 = (((on.stop - offset <= events) & (events < on.stop)) |
((off.stop - offset <= events) & (events < off.stop)))
events[indeces0] += offset
events[indeces1] += offset - 12
elif offset < 0:
indeces0 = (((on.start - offset <= events) & (events < on.stop)) |
((off.start - offset <= events) & (events < off.stop)))
indeces1 = (((on.start <= events) & (events < on.start - offset)) |
((off.start <= events) & (events < off.start - offset)))
events[indeces0] += offset
events[indeces1] += offset + 12
assert ((0 <= events) & (events < EventSeq.dim())).all()
histr = ControlSeq.feat_ranges()['pitch_histogram']
controls[:, :, histr.start:histr.stop] = np.roll(
controls[:, :, histr.start:histr.stop], offset, -1)
return events, controls
def dict2params(d, f=','):
return f.join(f'{k}={v}' for k, v in d.items())
def params2dict(p, f=',', e='='):
d = {}
for item in p.split(f):
item = item.split(e)
if len(item) < 2:
continue
k, *v = item
d[k] = eval('='.join(v))
return d
def compute_gradient_norm(parameters, norm_type=2):
total_norm = 0
for p in parameters:
param_norm = p.grad.data.norm(norm_type)
total_norm += param_norm ** norm_type
total_norm = total_norm ** (1. / norm_type)
return total_norm