-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
207 lines (170 loc) · 4.49 KB
/
index.html
File metadata and controls
207 lines (170 loc) · 4.49 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>wavepot live</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<textarea id="editor" autofocus spellcheck=false>
/**
*
* @title icecream
* @artist stagas
* @license mit
*
*/
var bpm = exports.bpm = 120;
var tuning = 440;
var transpose = 5;
// constants
var tau = 2 * Math.PI;
// time coefficients
var t, tt;
function Oscillator(type, size, alias){
if (!(this instanceof Oscillator)) return new Oscillator(type, size, alias);
this.pos = 0;
this.size = size || sampleRate;
this.coeff = this.size / sampleRate;
this.table = new Float32Array(this.size);
this.alias = alias === false ? false : true;
this.build(type);
}
Oscillator.prototype.build = function(type){
switch (type) {
case 'sin':
var scale = 2 * Math.PI / this.size;
for (var i = 0; i < this.size; i++) {
this.table[i] = Math.sin(i * scale);
}
break;
case 'saw':
for (var i = 0; i < this.size; i++) {
var x = (i / this.size);
this.table[i] = +2.0 * (x - Math.round(x));
}
break;
case 'ramp':
for (var i = 0; i < this.size; i++) {
var x = (i / this.size);
this.table[i] = -2.0 * (x - Math.round(x));
}
break;
case 'tri':
for (var i = 0; i < this.size; i++) {
var x = (i / this.size) - 0.25;
this.table[i] = 1.0 - 4.0 * Math.abs(Math.round(x) - x);
}
break;
case 'sqr':
var half = this.size / 2;
for (var i = 0; i < this.size; i++) {
this.table[i] = i < half ? +1 : -1;
}
break;
}
};
Oscillator.prototype.play = function(freq){
this.pos += freq * this.coeff;
if (this.pos >= this.size) this.pos -= this.size;
this.index = this.pos | 0;
if (!this.alias) return this.table[this.index];
this.alpha = this.pos - this.index;
this.next = this.table[this.index == this.size - 1 ? 0 : this.index + 1];
this.curr = this.table[this.index];
return this.curr + (this.next - this.curr) * this.alpha;
};
function clock(_t){
t = _t;
// t *= bpm / 120;
tt = tau * t;
}
function octave(o){
return function(n){
return n * o;
};
}
function slide(measure, seq){
var pos = (t / measure / 4) % seq.length;
var now = pos | 0;
var next = now + 1;
var alpha = pos - now;
if (next == seq.length) next = 0;
return seq[now] + ((seq[next] - seq[now]) * Math.pow(alpha, 20));
}
function sequence(measure, seq){
return seq[(t / measure / 4 | 0) % seq.length];
}
function arp(measure, x, y, z){
var ts = t / 4 % measure;
return Math.sin(x * (Math.exp(-ts * y))) * Math.exp(-ts * z);
}
function sin(freq, phase){
return Math.sin((t * freq + (2 - (phase || 0) / 2)) * tau);
}
function saw(freq){
return 1-2 * (t % (1 / freq)) * freq;
}
function tri(freq){
return Math.abs(1 - (2 * t * freq) % 2) * 2 - 1;
}
function sqr(freq){
return sin(freq, t) > 0 ? 1 : -1;
}
function Noise(){
return Math.random() * 2 - 1;
}
function note(n, octave){
return Math.pow(2, (
n + transpose - 33 + (12 * (octave || 0))
) / 12) * tuning; // A4 tuning
}
// patterns
var hat_pattern = [
0.6, 0.3, 1.2, 0.1, 0.3, 0.1, 1.1, 0.4,
0.6, 0.4, 1.1, 0.1, 0.3, 0.2, 1.2, 0.4,
];
var melody = [4, 7, 12, 15].map(function(n){
return note(n, 0);
});
melody = melody.concat(melody.map(octave(3)));
melody = melody.concat(melody.slice().reverse());
var progression = [1, 2];
var mprogression = [16, 32];
var hat_note = note(1, 0);
var osc = Oscillator('tri', 512);
var hat_osc = Oscillator('ramp', 512);
exports.dsp = function dsp(t){
// return 0;
// for (var i = 30000; i--;) {}
clock(t);
var noise = Noise();
var p = sequence(1/128, progression);
var m = sequence(1/2, mprogression);
var n = slide(1/m, melody) * p;
var synth_osc = osc.play(n/2);
var synth = arp(1/8, synth_osc, 32, 4);
var kick = arp(1/4, 60, 60, 100);
var hat = sequence(1/16, hat_pattern)
* arp(1/16, hat_osc.play(hat_note) + noise * 1.4, 18, 70);
return 0.7 * (
0.9 * synth
+ kick
+ 0.12 * hat
);
}
</textarea>
<pre id="debug"></pre>
<script src="app.js"></script>
<script src="debug.js"></script>
<script src="config.js"></script>
<script src="util.js"></script>
<script src="util.loopbuffer.js"></script>
<script src="keys.js"></script>
<script src="editor.js"></script>
<script src="engine.js"></script>
<script src="engine.audio.js"></script>
<script src="engine.audio.stream.js"></script>
<script src="boot.js"></script>
</body>
</html>