-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi_library.py
143 lines (110 loc) · 3.41 KB
/
midi_library.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
A library for all functions MIDI-related
"""
import vamp
import librosa
import numpy as np
from os import walk, path, mkdir, listdir
from scipy.signal import medfilt
from midiutil.MidiFile import MIDIFile
"""
Convert from MIDI format to an array of notes
"""
def midi_to_notes(midi, fs, hop, smooth, minduration):
# smooth midi pitch sequence first
if (smooth > 0):
filter_duration = smooth # in seconds
filter_size = int(filter_duration * fs / float(hop))
if filter_size % 2 == 0:
filter_size += 1
midi_filt = medfilt(midi, filter_size)
else:
midi_filt = midi
notes = []
p_prev = 0
duration = 0
onset = 0
for n, p in enumerate(midi_filt):
if p == p_prev:
duration += 1
else:
# treat 0 as silence
if p_prev > 0:
# add note
duration_sec = duration * hop / float(fs)
# only add notes that are long enough
if duration_sec >= minduration:
onset_sec = onset * hop / float(fs)
notes.append((onset_sec, duration_sec, p_prev))
# start new note
onset = n
duration = 1
p_prev = p
# add last note
if p_prev > 0:
# add note
duration_sec = duration * hop / float(fs)
onset_sec = onset * hop / float(fs)
notes.append((onset_sec, duration_sec, p_prev))
return notes
"""
Convert from frequency in Hz to MIDI format
"""
def hz2midi(hz):
# convert from Hz to midi note
hz_nonneg = hz.copy()
idx = hz_nonneg <= 0
hz_nonneg[idx] = 1
midi = 69 + 12*np.log2(hz_nonneg/440.)
midi[idx] = 0
# round
midi = np.round(midi)
return midi
"""
Saves an array of notes as a midi file
"""
def save_midi(outfile, notes, tempo):
track = 0
time = 0
midifile = MIDIFile(1)
# Add track name and tempo.
midifile.addTrackName(track, time, "MIDI TRACK")
midifile.addTempo(track, time, tempo)
channel = 0
volume = 100
for note in notes:
onset = note[0] * (tempo/60.)
duration = note[1] * (tempo/60.)
# duration = 1
pitch = int(note[2])
midifile.addNote(track, channel, pitch, onset, duration, volume)
# And write it to disk.
binfile = open(outfile, 'wb')
midifile.writeFile(binfile)
binfile.close()
"""
Convert all mp3 files to MIDI
"""
def create_midi_files():
smooth = 0.25
minduration = 0.1
fs = 44100
hop = 128
# Make the folder if it doesn't already exist
if not path.isdir('data/song_preview_clips'):
mkdir('data/song_preview_clips')
# If files are already in the folder we are done
if len(listdir('data/song_preview_clips')) != 0:
return
for (_, _, filenames) in walk("data/song_preview_clips"):
for file_name in filenames:
data, sr = librosa.load(
f"data/song_preview_clips/{file_name}", sr=fs, mono=True)
melody = vamp.collect(data, sr, "mtg-melodia:melodia",
parameters={"voicing": 0.2})
pitch = melody['vector'][1]
midi_pitch = hz2midi(pitch)
notes = midi_to_notes(midi_pitch, fs, hop, smooth, minduration)
save_midi(
f'data/midi_songs/{file_name.replace(".mp3", ".mid")}', notes, 115)
print(f"converted {file_name}")