forked from italoadler/fluX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.js
133 lines (104 loc) · 3.25 KB
/
sequencer.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
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
class Sequencer {
get audioContext() {
return Sequencer.ac;
}
static get notePlaying() {
return Sequencer.snotePlaying;
}
static set notePlaying(value){
Sequencer.snotePlaying = value;
}
static set audioContext(value) {
Sequencer.ac = value;
}
get synth() {
return Sequencer.synth;
}
get midiOutNamePort() {
return Sequencer.midiOut;
}
static set midiOutNamePort(value) {
Sequencer.midiOut = eval('new MidiOut("'+value+'")');
}
get generator() {
return Sequencer.gen;
}
static set generator(scale) {
Sequencer.gen = eval('new Generator('+JSON.stringify(scale)+','+f+')');
}
get bpm() {
return Sequencer.bpm;
}
static set bpm(value) {
Sequencer.sbpm = value;
}
get pattern() {
return Sequencer.pattern;
}
static set pattern(value) {
Sequencer.spattern = value;
}
static beatToMiliSeconds(noteObject){
const beat = {128: 31.25, 64: 62.5, 32: 125, 16: 250, 8: 500, 6: 666, 3: 333, 4: 1000, 2: 2000, 1: 4000};
if (Object.keys(noteObject).includes('dur')) {
noteObject.dur = (beat[noteObject.dur < 128 ? noteObject.dur : 128]/1000)/(Sequencer.sbpm/60)*1000;
} else {
Object.assign(noteObject, {dur:(beat[Sequencer.spattern[Sequencer.counter%Sequencer.spattern.length]]/1000)/(Sequencer.sbpm/60)*1000});
}
return noteObject;
}
static synth(value, adsr={a:0,d:0,r:0}) {
if (value != undefined && value != null) {
Sequencer.synth = value + '('+JSON.stringify(adsr)+')';
Sequencer.play = function() {
const asynth = eval('new '+Sequencer.synth);
if (!Sequencer.ac) {
throw 'Sequencer has no audio context.';
}
asynth.audioContext = Sequencer.ac;
const noteObject = Sequencer.beatToMiliSeconds(Sequencer.gen.note);
asynth.waveType = 'sawtooth';
asynth.note = noteObject.note;
asynth.vel = noteObject.vel;
asynth.adsr = adsr;
asynth.adsr.s = noteObject.dur/1000;
Sequencer.notePlaying = noteObject;
// asynth.connect(scope); // fix
asynth.connect(Sequencer.ac.destination);
asynth.start();
setTimeout(Sequencer.start, noteObject.dur, Sequencer.sbpm, Sequencer.spattern);
}
return this;
}
return null;
}
static midiOut(value) {
if (value != undefined && value != null) {
Sequencer.midiOut = eval('new MidiOut("'+value+'")');
Sequencer.play = function() {
const noteObject = Sequencer.gen.note;
Sequencer.notePlaying = noteObject;
Sequencer.midiOut.sendNote(Sequencer.beatToMiliSeconds(noteObject));
setTimeout(Sequencer.start, Sequencer.beatToMiliSeconds(noteObject.dur), Sequencer.sbpm, Sequencer.spattern);
}
return this;
}
Sequencer.midiOut = null;
return null;
}
static generator(scale, f) {
Sequencer.counter = 0;
Sequencer.gen = eval('new Generator('+JSON.stringify(scale)+','+f+')');
return this;
}
static start(bpm=90, pattern=[4]) {
Sequencer.sbpm = bpm;
Sequencer.spattern = pattern;
if (!Sequencer.play || Sequencer.play == null) {
throw 'No synth or MIDI output set.'
}
Sequencer.play();
Sequencer.counter++;
Sequencer.gen.counter = Sequencer.counter;
}
}