-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIDI (Update).js
85 lines (84 loc) · 10.3 KB
/
MIDI (Update).js
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
//The update is that it can play more complex melodies. It had problems with note durations before.
importScripts("https://gist.githubusercontent.com/AnastasiaDunbar/d75e19d93a5c439d8185343f1fd54da1/raw/93ae0d764488038143723ccc1ccb01f6320e48f7/TinyRave%2520functions.js")
//Where the melody came from: https://www.youtube.com/watch?v=ZpNbSz1y428
//Array MIDI visualizer: https://jsfiddle.net/uujnovyf/
//[note,start,end]
var midi={
melody: [[59,0,12],[36,0,48],[60,12,108],[58,108,120],[59,120,192],[59,192,216],[26,192,240],[60,216,240],[59,240,252],[57,252,288],[55,288,336],[57,336,384],[59,384,408],[31,384,432],[60,408,432],[59,432,456],[57,456,480],[26,576,624],[59,480,768],[31,768,816],[50,816,864],[54,864,912],[55,912,960],[26,960,1008],[57,960,1008],[59,1008,1056],[60,1056,1104],[61,1104,1164],[31,1152,1344],[62,1164,1404],[64,1404,1428],[62,1428,1464],[62,1464,1476],[59,1476,1488],[33,1344,1536],[60,1488,1536],[60,1536,1596],[35,1536,1728],[62,1596,1764],[36,1728,1776],[65,1764,1800],[64,1800,1822],[65,1822,1844],[64,1844,1864],[65,1864,1883],[64,1883,1903],[65,1903,1922],[64,1922,1942],[65,1942,1961],[24,1920,1968],[64,1961,1981],[65,1981,2000],[64,2000,2020],[36,2112,2160],[50,2160,2196],[54,2196,2233],[55,2233,2269],[57,2269,2305],[24,2304,2352],[59,2304,2352],[62,2352,2376],[64,2376,2400],[64,2400,2424],[65,2424,2448],[66,2448,2472],[67,2472,2496],[28,2496,2544],[67,2496,2640],[69,2640,2664],[67,2664,2688],[65,2688,2820],[67,2820,2844],[32,2688,2880],[65,2844,2880],[64,2880,2928],[62,2928,2952],[33,2880,3072],[64,2952,3072],[62,3072,3168],[64,3168,3216],[35,3072,3264],[62,3216,3264],[36,3264,3312],[60,3264,3360],[55,3360,3372],[59,3372,3456],[27,3456,3504],[60,3456,3504],[59,3504,3540],[57,3540,3576],[50,3576,3612],[55,3612,3648],[57,3648,3672],[34,3648,3696],[59,3672,3696],[55,3696,3744],[60,3744,3840],[60,3840,3872],[59,3872,3904],[57,3904,3936],[57,3936,3952],[50,3952,3965],[39,3840,4032],[57,3965,4032],[58,4032,4128],[54,4080,4128],[38,4032,4224],[36,4224,4416],[57,4128,4512],[33,4416,4608],[55,4656,4704],[60,4704,4752],[30,4608,4800],[62,4752,4800],[63,4800,4824],[31,4800,4848],[63,4824,4848],[62,4848,4872],[63,4872,4896],[62,4896,4920],[60,4944,4956],[62,4956,4980],[60,4980,4992],[59,4992,5040],[60,5040,5088],[62,5088,5112],[55,5136,5160],[35,4992,5184],[59,5184,5220],[36,5184,5232],[60,5220,5256],[59,5256,5268],[57,5268,5280],[55,5280,5292],[57,5292,5328],[50,5328,5376],[59,5376,5388],[27,5376,5424],[60,5388,5472],[62,5472,5520],[55,5520,5556],[60,5568,5580],[33,5568,5616],[59,5580,5664],[58,5664,5688],[57,5688,5760],[27,5760,5808],[55,5760,5808],[55,5808,5856],[59,5856,5904],[50,5904,5952],[59,5952,5964],[60,5964,5988],[26,5952,6000],[59,5988,6018],[57,6018,6049],[57,6048,6096],[50,6096,6144],[33,6144,6192],[57,6144,6192],[59,6192,6240],[60,6240,6288],[62,6288,6336],[63,6336,6360],[31,6336,6384],[63,6360,6384],[62,6384,6432],[60,6432,6468],[62,6468,6480],[60,6480,6504],[62,6504,6528],[35,6528,6576],[59,6528,6612],[60,6612,6636],[62,6636,6672],[55,6672,6720],[60,6720,6744],[36,6720,6768],[62,6744,6768],[60,6768,6792],[59,6792,6816],[57,6816,6864],[55,6864,6912],[59,6912,6924],[27,6912,6960],[60,6924,6960],[59,6960,6984],[60,6984,7008],[62,7008,7056],[55,7056,7104],[59,7104,7128],[33,7104,7152],[60,7128,7152],[59,7152,7176],[33,7176,7200],[58,7176,7200],[58,7200,7212],[33,7200,7248],[57,7212,7248],[55,7248,7296],[55,7296,7320],[35,7296,7344],[54,7320,7344],[55,7344,7368],[57,7368,7392],[59,7392,7428],[37,7392,7440],[50,7428,7452],[58,7488,7500],[57,7500,7519],[56,7519,7524],[57,7524,7632],[38,7488,7680],[55,7632,7680],[50,7680,7728],[59,7728,7752],[55,7740,7776],[60,7776,7800],[57,7788,7824],[62,7824,7848],[59,7836,7872],[63,7872,7884],[62,7884,7896],[63,7896,7901],[62,7901,8016],[31,7872,8064],[60,8016,8064],[59,8064,8112],[60,8112,8160],[62,8160,8208],[35,8064,8256],[55,8208,8256],[60,8256,8288],[62,8288,8321],[60,8321,8353],[58,8352,8364],[50,8400,8424],[36,8256,8448],[57,8364,8448],[59,8448,8460],[28,8448,8496],[60,8460,8544],[61,8544,8556],[62,8556,8640],[60,8640,8688],[33,8640,8688],[59,8688,8700],[58,8700,8712],[59,8712,8720],[58,8720,8728],[58,8728,8748],[57,8748,8784],[54,8832,8856],[27,8832,8880],[55,8856,8880],[54,8880,8904],[54,8904,8928],[59,8928,8976],[50,8976,9024],[57,9024,9036],[59,9036,9048],[57,9048,9064],[26,9024,9072],[58,9064,9072],[57,9072,9120],[54,9120,9168],[50,9168,9216],[55,9216,9228],[30,9216,9264],[54,9228,9264],[59,9264,9312],[60,9312,9360],[62,9360,9408],[63,9408,9420],[62,9420,9427],[31,9408,9456],[63,9427,9504],[55,9540,9546],[57,9546,9552],[59,9552,9564],[60,9564,9576],[59,9576,9600],[59,9600,9624],[35,9600,9648],[60,9648,9696],[62,9696,9744],[55,9744,9768],[59,9792,9807],[36,9792,9840],[60,9807,9840],[59,9840,9864],[57,9864,9888],[58,9888,9896],[57,9896,9936],[50,9936,9984],[60,9984,9996],[59,9996,10008],[31,9984,10032],[60,10008,10080],[62,10080,10128],[50,10128,10176],[59,10176,10188],[33,10176,10224],[60,10188,10224],[57,10224,10240],[58,10240,10256],[57,10256,10272],[57,10272,10320],[50,10320,10368],[55,10368,10384],[54,10384,10401],[55,10401,10417],[54,10416,10440],[35,10368,10464],[55,10440,10464],[59,10464,10512],[50,10512,10536],[36,10464,10560],[59,10560,10567],[57,10567,10574],[58,10574,10582],[59,10582,10589],[57,10589,10596],[55,10596,10600],[57,10600,10668],[38,10560,10752],[50,10800,10848],[52,10848,10896],[33,10752,10944],[54,10896,10944],[55,10944,10968],[57,10968,10980],[31,10944,10992],[55,10980,10998],[54,10998,11003],[55,11003,11040],[50,11088,11136],[54,11136,11232],[55,11232,11280],[33,11136,11328],[59,11328,11337],[61,11337,11341],[55,11341,11355],[54,11355,11361],[55,11361,11364],[26,11328,11376],[58,11364,11376],[57,11376,11436],[50,11472,11520],[30,11520,11568],[54,11520,11568],[55,11568,11616],[57,11616,11660],[55,11660,11664],[59,11664,11712],[59,11712,11729],[60,11729,11760],[57,11760,11808],[55,11808,11825],[57,11825,11829],[59,11829,11856],[31,11712,11904],[50,11856,11904],[59,11904,11908],[60,11908,11928],[59,11928,11952],[55,11952,11976],[57,11976,11982],[58,11982,11988],[57,11988,12012],[35,11904,12096],[50,12048,12096],[55,12096,12108],[57,12108,12120],[36,12096,12144],[59,12120,12168],[51,12168,12192],[60,12192,12252],[55,12252,12258],[59,12258,12264],[59,12288,12300],[25,12288,12336],[60,12300,12342],[59,12342,12366],[57,12366,12384],[55,12384,12396],[54,12396,12432],[54,12432,12480],[58,12480,12492],[61,12492,12504],[58,12504,12516],[57,12516,12528],[57,12528,12576],[38,12480,12672],[57,12576,12720],[54,12768,12816],[55,12816,12832],[33,12672,12864],[56,12832,12864],[30,12864,12912],[57,12864,12912],[50,12912,12960],[60,12960,12990],[59,12990,13008],[60,13008,13038],[59,13038,13056],[26,13056,13104],[57,13056,13104],[55,13104,13152],[54,13152,13200],[55,13200,13206],[57,13206,13212],[55,13212,13248],[55,13248,13392],[50,13392,13440],[54,13440,13536],[33,13248,13632],[55,13536,13632],[57,13632,13638],[60,13638,13644],[59,13644,13650],[58,13650,13668],[57,13668,13674],[58,13674,13704],[57,13704,13752],[26,13632,13824],[50,13776,13824],[54,13824,13872],[55,13872,13920],[57,13920,13968],[31,13824,14016],[58,13968,14016],[59,14016,14028],[31,14016,14064],[60,14028,14070],[55,14070,14094],[50,14094,14112],[57,14112,14118],[59,14118,14160],[50,14160,14208],[27,14208,14256],[59,14208,14256],[60,14256,14280],[59,14280,14304],[57,14304,14352],[55,14352,14400],[57,14400,14412],[58,14412,14418],[33,14400,14448],[59,14418,14448],[60,14448,14496],[55,14496,14544],[60,14496,14562],[60,14562,14568],[62,14568,14586],[60,14586,14592],[62,14592,14610],[60,14610,14616],[62,14616,14634],[25,14592,14640],[60,14634,14652],[59,14652,14670],[58,14670,14688],[58,14688,14736],[57,14736,14784],[58,14784,14820],[26,14784,14832],[57,14820,14832],[61,14832,14838],[58,14838,14880],[55,14880,14922],[57,14922,14976],[30,14976,15024],[57,14976,15168],[33,15168,15216],[50,15216,15264],[54,15264,15312],[55,15312,15336],[56,15336,15339],[57,15339,15360],[57,15360,15381],[54,15408,15456],[55,15456,15504],[57,15504,15510],[58,15510,15516],[35,15360,15552],[59,15516,15552]],
melodyduration: 15552
};
var speed=1/384;
function wave(time,freq,dur){
return tri(time*freq);
}
var maxPoly=10;
var at=0;
var time=0;
function buildSample(globalTime) {
time+=1/SAMPLE_RATE;
if(time>midi.melodyduration*speed){
at=0;
time=0;
}
var master=0;
//for(var i=0;i<midi.melody.length;i++){ //Too slow.
for(var i=max(at-(maxPoly/2),0);i<min(at+(maxPoly/2),midi.melody.length);i++){
if(time>=midi.melody[i][1]*speed&&time<midi.melody[i][2]*speed){
var t=time-(midi.melody[i][1]*speed);
master+=wave(t,noteHz(midi.melody[i][0]),(midi.melody[i][2]-midi.melody[i][1])*speed);
}
if(time>=midi.melody[i][2]*speed){
at++;
}
}
master+=noiseSmooth(time,1000)*pow(fract(-time),4)*.7;master+=noiseSmooth(time,2000)*pow(fract(.5-time),4)*.7;
master/=5;
return master;
}
/* Python. MIDI to array
import mido
import math
filename = 'yourMidi.mid'
velocity = False #Put velocity in array
interval = False #0 to 1
endAtBar = True #Set melody duration to end of bar
arrayName = "melody"
durationName = "melodyduration"
def toAbsoluteTime(track):
messages = []
now = 0
for msg in track:
messages.append(msg.copy(time=now+msg.time))
now += msg.time
return messages
t = ['note_off','note_on']
print('//[note'+(',vel'if velocity else'')+',start,end]') #Tip
arrayString = 'var '+arrayName+'=['
myMidi = mido.MidiFile(filename)
midiStack = toAbsoluteTime(myMidi.tracks[1])[1:][:-1]
endtime = midiStack[-1].time #Length of MIDI
if endAtBar:
endtime=math.ceil(endtime/384.)*384
stack = {} #Output JS array stack
if interval: #0 to 1
for message in midiStack:
key = str(message.note)
if not key in stack:
stack[key]=[]
if(t.index(message.type)): #Note on
stack[key].append('['+str(message.note)+(','+str(message.velocity)if velocity else'')+','+str(message.time/float(endtime))+',')
else: #Note off
arrayString += stack[key].pop(0)+str(time/float(endtime))+'],'
print(arrayString[:-1]+'];')
else: #Not interval
for message in midiStack:
key = str(message.note)
if not key in stack:
stack[key]=[]
if(t.index(message.type)): #Note on
stack[key].append('['+str(message.note)+(','+str(message.velocity)if velocity else'')+','+str(message.time)+',')
else: #Note off
arrayString += stack[key].pop(0)+str(message.time)+'],'
print(arrayString[:-1]+'];')
print('var '+durationName+'='+str(endtime)+';')
*/