-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_equations.py
187 lines (147 loc) · 5.06 KB
/
music_equations.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
"""Music_Equations.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/asigalov61/Music-Equations/blob/main/Music_Equations.ipynb
# Music Equations (ver 0.3)
***
## Listen to the muse of mathematics!!!
***
### Project Los Angeles
### Tegridy Code 2021
***
# Setup Environment
"""
# Commented out IPython magic to ensure Python compatibility.
#@title Install dependencies
# %cd /content/
!git clone https://github.com/asigalov61/tegridy-tools
!pip install -U scikit-learn
# Commented out IPython magic to ensure Python compatibility.
#@title Import modules
# %cd /content/tegridy-tools/tegridy-tools
import os
import TMIDIX
from joblib import dump, load
from sklearn.preprocessing import StandardScaler, RobustScaler
import math
import numpy as np
# %cd /content/
"""# Process"""
#@title Load the source MIDI file
full_path_to_MIDI_file = "/content/tegridy-tools/tegridy-tools/seed2.mid" #@param {type:"string"}
perfect_timings = True #@param {type:"boolean"}
data = TMIDIX.Optimus_MIDI_TXT_Processor(full_path_to_MIDI_file, MIDI_patch=range(0, 127), MIDI_channel=-1, perfect_timings=perfect_timings)
#@title Encode to INTs
sts = []
pitches = []
durs = []
pe = data[2][0]
for d in data[2]:
sts.append(abs(d[1]-pe[1]))
durs.append(d[2])
pitches.append(d[4])
pe = d
print(sts[:5])
print(durs[:5])
print(pitches[:5])
# prepare data for standardization
sts1 = np.asarray(sts)
sts2 = sts1.reshape((len(sts1), 1))
# train the standardization
#scaler = RobustScaler(with_centering=False, with_scaling=True)
scaler = StandardScaler(with_mean=True)
scaler = scaler.fit(sts2)
print('Mean: %f, StandardDeviation: %f' % (scaler.mean_, math.sqrt(scaler.var_)))
# standardization the dataset and print the first 5 rows
sts_norm = scaler.transform(sts2)
sts_ints = []
for d in sts_norm.tolist():
#print(d * 10000)
#z = 5
#y = round(d[0], math.ceil(-math.log10(d[0])) + z)
#sts_ints.append(y)
sts_ints.append(abs(math.ceil(d[0])))
for i in range(15):
print(sts_ints[i])
dump(scaler, '/content/sts_scaler.bin', compress=True)
print('=====')
# prepare data for standardization
durs1 = np.asarray(durs)
durs2 = durs1.reshape((len(durs1), 1))
#scaler1 = RobustScaler(with_centering=False, with_scaling=True)
scaler1 = StandardScaler(with_mean=True)
scaler1 = scaler1.fit(durs2)
print('Mean: %f, StandardDeviation: %f' % (scaler1.mean_, math.sqrt(scaler1.var_)))
# standardization the dataset and print the first 5 rows
durs_norm = scaler1.transform(durs2)
durs_ints = []
for d in durs_norm:
#print(d * 10000)
#z = 5
#y = round(d, math.ceil(-math.log10(d)) + z)
#durs_ints.append(y)
durs_ints.append(abs(math.ceil(d[0])))
for i in range(15):
print(durs_ints[i])
dump(scaler1, '/content/sts_scaler1.bin', compress=True)
#@title Decode back to MIDI
floats_vs_ints = True #@param {type:"boolean"}
out_sts = []
if floats_vs_ints:
z = sts_norm
else:
z = np.asarray(sts_ints, dtype=float).reshape(-1, 1) # / 20000
# inverse transform and print the first 5 rows
inversed = scaler.inverse_transform(z)
for i in range(len(z)):
#print(int(inversed[i]))
out_sts.append(math.ceil(inversed[i]))
print('========')
out_durs = []
if floats_vs_ints:
z = durs_norm
else:
z = np.asarray(durs_ints, dtype=float).reshape(-1, 1) #/ 20000
# inverse transform and print the first 5 rows
inversed = scaler1.inverse_transform(z)
for i in range(len(z)):
#print(int(inversed[i]))
out_durs.append(math.ceil(inversed[i]))
song = []
time = 0
for i in range(len(pitches)):
song.append(['note', time, out_durs[i], 0, pitches[i], pitches[i]+15])
time += out_sts[i]
TMIDIX.Tegridy_SONG_to_MIDI_Converter(song, output_file_name='/content/Music-Equations-Composition',
number_of_ticks_per_quarter=500,
track_name='sklearn StandardScaler',
output_signature='Music Equations')
"""# Listen"""
#@title Install prerequisites
!apt install fluidsynth #Pip does not work for some reason. Only apt works
!pip install midi2audio
!pip install pretty_midi
#@title Plot and listen to the output
#@markdown NOTE: May be very slow with the long compositions
from midi2audio import FluidSynth
from IPython.display import display, Javascript, HTML, Audio
import pretty_midi
import librosa.display
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import numpy as np
print('Synthesizing the last output MIDI... ')
fname = '/content/Music-Equations-Composition'
fn = os.path.basename(fname + '.mid')
fn1 = fn.split('.')[0]
print('Plotting the composition. Please wait...')
pm = pretty_midi.PrettyMIDI(fname + '.mid')
# Retrieve piano roll of the MIDI file
piano_roll = pm.get_piano_roll()
plt.figure(figsize=(14, 5))
librosa.display.specshow(piano_roll, x_axis='time', y_axis='cqt_note', fmin=1, hop_length=160, sr=16000, cmap=plt.cm.hot)
plt.title(fn1)
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
Audio(str(fname + '.wav'), rate=16000)
"""# Congrats! You did it! :)"""