72 KiB
72 KiB
import music21
from music21.midi import MidiFile
import numpy as np
import matplotlib.pyplot as plt
import mido
midi messages
filepath = '/home/altarin/praca-magisterska/docs/images/seq2seq_generated_midi_7.mid'
mid = mido.MidiFile(filepath)
for i, track in enumerate(mid.tracks):
for msg in track:
print(msg)
<meta message set_tempo tempo=500000 time=0> <meta message time_signature numerator=4 denominator=4 clocks_per_click=24 notated_32nd_notes_per_beat=8 time=0> <meta message end_of_track time=1> <meta message track_name name='Guitar' time=0> program_change channel=0 program=30 time=0 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=48 velocity=0 time=220 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=55 velocity=100 time=0 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=48 velocity=0 time=220 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=55 velocity=0 time=0 note_on channel=0 note=55 velocity=100 time=0 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=48 velocity=0 time=440 note_on channel=0 note=55 velocity=0 time=0 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=48 velocity=0 time=110 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=45 velocity=100 time=770 note_on channel=0 note=50 velocity=100 time=0 note_on channel=0 note=57 velocity=100 time=0 note_on channel=0 note=45 velocity=0 time=220 note_on channel=0 note=46 velocity=100 time=0 note_on channel=0 note=50 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=57 velocity=0 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=46 velocity=0 time=220 note_on channel=0 note=46 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=46 velocity=0 time=220 note_on channel=0 note=46 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=46 velocity=0 time=440 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=48 velocity=0 time=440 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=48 velocity=0 time=220 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=48 velocity=0 time=220 note_on channel=0 note=51 velocity=100 time=0 note_on channel=0 note=48 velocity=100 time=220 note_on channel=0 note=51 velocity=0 time=0 note_on channel=0 note=48 velocity=0 time=220 note_on channel=0 note=60 velocity=100 time=0 note_on channel=0 note=51 velocity=100 time=220 note_on channel=0 note=60 velocity=0 time=0 note_on channel=0 note=51 velocity=0 time=220 note_on channel=0 note=51 velocity=100 time=0 note_on channel=0 note=51 velocity=0 time=220 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=46 velocity=100 time=440 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=46 velocity=0 time=990 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=46 velocity=100 time=110 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=46 velocity=0 time=220 note_on channel=0 note=46 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=58 velocity=100 time=0 note_on channel=0 note=41 velocity=100 time=220 note_on channel=0 note=46 velocity=0 time=0 note_on channel=0 note=48 velocity=100 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=53 velocity=100 time=0 note_on channel=0 note=57 velocity=100 time=0 note_on channel=0 note=58 velocity=0 time=0 note_on channel=0 note=41 velocity=0 time=220 note_on channel=0 note=48 velocity=0 time=0 note_on channel=0 note=53 velocity=0 time=0 note_on channel=0 note=57 velocity=0 time=0 <meta message end_of_track time=1> <meta message track_name name='Melody' time=0> program_change channel=1 program=1 time=0 note_on channel=1 note=60 velocity=100 time=0 note_on channel=1 note=60 velocity=0 time=220 note_on channel=1 note=62 velocity=100 time=0 note_on channel=1 note=62 velocity=0 time=220 note_on channel=1 note=63 velocity=100 time=0 note_on channel=1 note=63 velocity=0 time=220 note_on channel=1 note=62 velocity=100 time=220 note_on channel=1 note=62 velocity=0 time=220 note_on channel=1 note=75 velocity=100 time=0 note_on channel=1 note=58 velocity=100 time=220 note_on channel=1 note=75 velocity=0 time=0 note_on channel=1 note=75 velocity=100 time=0 note_on channel=1 note=58 velocity=0 time=220 note_on channel=1 note=75 velocity=0 time=0 note_on channel=1 note=75 velocity=100 time=0 note_on channel=1 note=75 velocity=0 time=220 note_on channel=1 note=75 velocity=100 time=0 note_on channel=1 note=75 velocity=0 time=220 note_on channel=1 note=72 velocity=100 time=220 note_on channel=1 note=72 velocity=0 time=220 note_on channel=1 note=75 velocity=100 time=440 note_on channel=1 note=75 velocity=0 time=220 note_on channel=1 note=72 velocity=100 time=220 note_on channel=1 note=72 velocity=0 time=220 note_on channel=1 note=75 velocity=100 time=0 note_on channel=1 note=75 velocity=0 time=220 note_on channel=1 note=72 velocity=100 time=220 note_on channel=1 note=72 velocity=0 time=220 note_on channel=1 note=75 velocity=100 time=440 note_on channel=1 note=75 velocity=0 time=220 note_on channel=1 note=72 velocity=100 time=220 note_on channel=1 note=72 velocity=0 time=220 note_on channel=1 note=70 velocity=100 time=220 note_on channel=1 note=70 velocity=0 time=220 note_on channel=1 note=70 velocity=100 time=0 note_on channel=1 note=70 velocity=0 time=220 note_on channel=1 note=70 velocity=100 time=0 note_on channel=1 note=70 velocity=0 time=220 note_on channel=1 note=70 velocity=100 time=0 note_on channel=1 note=70 velocity=0 time=440 note_on channel=1 note=71 velocity=100 time=0 note_on channel=1 note=71 velocity=0 time=440 <meta message end_of_track time=1> <meta message track_name name='Bass' time=0> program_change channel=2 program=35 time=0 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=22 velocity=100 time=0 note_on channel=2 note=22 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=29 velocity=100 time=0 note_on channel=2 note=29 velocity=0 time=220 note_on channel=2 note=31 velocity=100 time=0 note_on channel=2 note=31 velocity=0 time=220 note_on channel=2 note=31 velocity=100 time=0 note_on channel=2 note=31 velocity=0 time=220 note_on channel=2 note=32 velocity=100 time=220 note_on channel=2 note=32 velocity=0 time=220 note_on channel=2 note=32 velocity=100 time=0 note_on channel=2 note=32 velocity=0 time=220 note_on channel=2 note=34 velocity=100 time=220 note_on channel=2 note=34 velocity=0 time=220 note_on channel=2 note=34 velocity=100 time=0 note_on channel=2 note=24 velocity=100 time=220 note_on channel=2 note=34 velocity=0 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 note_on channel=2 note=24 velocity=100 time=0 note_on channel=2 note=24 velocity=0 time=220 <meta message end_of_track time=1> <meta message track_name name='Drums' time=0> program_change channel=9 program=1 time=0 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=110 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=49 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=49 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=110 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=110 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=110 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=49 velocity=100 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=49 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=36 velocity=100 time=0 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 note_on channel=9 note=38 velocity=100 time=0 note_on channel=9 note=36 velocity=100 time=220 note_on channel=9 note=38 velocity=0 time=0 note_on channel=9 note=36 velocity=0 time=220 <meta message end_of_track time=1>
Regresja liniowa
x = np.arange(0,10)
y = np.arange(0,10) + np.random.random(10)-0.5
y_hat = np.arange(0,10)
plt.scatter(x, y, c='k')
plt.plot(x,y_hat, c='r')
# plt.labels
# plt.savefig('linear_reg.png')
[<matplotlib.lines.Line2D at 0x7fe0d90a1580>]
# Gradient descent
fx = x^2
dx = 2x + c
func = lambda x: x**2
func_dx = lambda x:2*x
x = np.arange(-20,20,0.1)
y = func(x)
point_x = -6
point_y = func(point_x)
dx = styczna(x)
learning_points_xs = np.arange(point_x, 0, 0.8)
learning_points_ys = func(learning_points_xs)
fig, ax = plt.subplots()
ax.plot(x, y, c='k')
for px in learning_points_xs[0:1]:
slope = func_dx(px)
intercept = -px**2
styczna = lambda x: slope*x + intercept
dx = styczna(x)
ax.plot(x, dx, c='r', zorder=1)
ax.scatter(x=point_x, y=point_y, c='r', zorder=6) #start
# ax.scatter(x=0, y=0, c='g', zorder=6) #min
# ax.scatter(x=learning_points_xs, y=learning_points_ys, c='y', zorder=5)
plt.ylim((-20,80))
plt.xlim((-20,20))
# plt.xlabel('x')
# plt.ylabel('f(x) = x^2')
plt.savefig('gradient_descent_1.png')
# https://towardsdatascience.com/understanding-the-mathematics-behind-gradient-descent-dde5dc9be06e?
# https://medium.com/code-heroku/gradient-descent-for-machine-learning-3d871fa48b4c