-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmelodies.html
190 lines (153 loc) · 6.13 KB
/
melodies.html
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
<html>
<head>
<meta charset="utf-8">
<script src="gpu-browser.min.js"></script>
<!--<script src="ins.js"></script>-->
<script src="MyLib.js"></script>
</head>
<body>
<img src="const.png" style="position:fixed;bottom:0;right:0;width:100%;height:100%;z-index:10">
<script>
const gpu = new GPU();
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var scale = new Array(15);
function makeScale() {
let sc = [0,2,4,5,7,9,11];
sc = [0,2,3,6,7,8,11];
switch(rand(4)) {
case 0: sc = [0,2,4,5,7,9,11];break;
case 1: sc = [0,2,3,6,7,8,11];break;
case 2: sc = [0,1,4,5,7,8,11];break;
case 3: sc = [0,2,3,5,7,8,11];break;
}
let sc0 = [];
let r = new GeneralRandom();
for(let i = 0;i < sc.length - 1;++i) sc0.push(sc[i + 1] - sc[i]);
sc0.push(12 + sc[0] - sc[sc.length - 1]);
let m = Math.trunc(r.random() * sc0.length);
console.log(sc0);
console.log(m);
for(let i = 0;i < m;++i) {
let swp = sc0[0];
for(let j = 1;j < sc0.length;++j) sc0[j - 1] = sc0[j];
sc0[sc0.length - 1] = swp;
}
sc = [0];
for(let j = 0;j < sc0.length - 1;++j) sc.push(sc0[j] + sc[j]);
console.log(sc0);
console.log(sc);
let n = rand(6);
let k = 0;
for(let j = 0;j < 5;++j)
for(let i = 0;i < sc.length;++i)
if(k < scale.length) {
scale[k] = sc[i] + 12 * j + n - 12;
k += 1;
}
console.log(scale);
}
gpu.addFunction(function mypow(x,y) {
return Math.sign(x) * Math.pow(Math.abs(x),y);
});
gpu.addFunction(function myexp(x) {
return Math.sign(x) * Math.exp(Math.abs(x));
});
const add2 = gpu.createKernel(function (xs) {
let p = xs[7] > 0.5 ? 3.0 * (xs[7] - 0.5) : 1.0 / (3.0 * xs[7]);
let a = Math.pow(xs[0],p);
let b = Math.pow(xs[1],p);
let c = Math.pow(xs[2],p);
let d = Math.pow(xs[3],p);
let e = Math.pow(xs[4],p);
let f = Math.pow(xs[5],p);
let g = Math.pow(xs[6],p);
let h = Math.pow(xs[8],p);
let p2 = 3 * xs[9];
let p3 = 3 * xs[10];
let x = 441.0 * Math.PI * this.thread.x / 44100.0 * Math.PI * Math.pow(2.0,-24 / 12.0);
return Math.atan(
(Math.atan((this.thread.x - 1000) / 100.0) + Math.PI / 2) *
mypow(Math.sin(x),a > 0.5 ? p2 * (a - 0.5) : 1.0 / (p2 * a)) *
(mypow(Math.cos(this.thread.x / 4000 * (1.0 + 1.0 * (1.0 / (10 * c + 1)))),b > 0.5 ? p3 * (b - 0.5) : 1.0 / (p3 * b)) + (2 * e + 1)) *
(Math.atan(-0.001 * (4 / (10 * f + 1)) * (x - 40 - 50 * g)) + Math.PI / 2)
);
//return buffer[this.thread.x + offset] + vol * Math.atan(1.5 * y) / 5;
}).setOutput([200000]);
class Melody {
constructor(instrument) {
this.r = new GeneralRandom();
this.instrument = instrument;
this.tones = new Array(32);
this.index = 0;
for(let i = 0;i < this.tones.length;++i) this.tones[i] = 0;
this.tones[0] = scale.length * this.r.random();
this.tones[this.tones.length - 1] = scale.length * this.r.random();
this.fractal(0,this.tones.length - 1,0.85,scale.length * (this.r.random() < 0.5 ? 1.0 : -1.0));
let _min = 100000;
let _max = -100000;
for(let i = 0;i < this.tones.length;++i) if(this.tones[i] > _max) _max = this.tones[i];
for(let i = 0;i < this.tones.length;++i) if(this.tones[i] < _min) _min = this.tones[i];
let min = Math.trunc(this.r.random() * (scale.length / 2) );
let max = (scale.length - 1) - Math.trunc(this.r.random() * (scale.length - min - 5));
//min = 0;
//max = scale.length - 1;
for(let i = 0;i < this.tones.length;++i) this.tones[i] = Math.round((max - min) * (this.tones[i] - _min) / (_max - _min) + min);
console.log(min + " / " + max);
this.a = Math.random() * 0.5 + 0.5;
this.b = Math.random() * 0.5 + 0.5;
}
fractal(start,end,decay,amp) {
let m = Math.round((start + end) / 2);
if(this.tones[m] == 0) {
this.tones[m] = (this.tones[start] + this.tones[end]) / 2.0 + this.r.random() * amp;
this.fractal(start,m,decay,-decay * amp);
this.fractal(m,end,decay,-decay * amp);
}
}
play(buffer,offset,sampleRate,len) {
if(this.r.random() < this.a) {
this.instrument.render(buffer,offset,scale[this.tones[this.index]],sampleRate,1.75,len);
if(this.r.random() < this.b) {
this.index += 1;
if(this.index >= this.tones.length) {
this.index = 0;
}
}
}
}
}
var channels = 2;
var seconds = 60;
var frameCount = audioCtx.sampleRate * seconds;
let bl = blur.setOutput([frameCount]);
function sound() {
makeScale();
var myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);
var instruments0 = shuffle(instrumentsF(add2));
let ms = [];
for(let i = 0;i < 6;++i) {
ms.push(new Melody(new Instrument(instruments0.shift(),add)));
}
let n = 1;//rand(2) + 1;
ms.forEach((m,j) => {
for(let i = 0;i < frameCount;i += Math.round(0.125 * n * audioCtx.sampleRate * (j % 3 + 2))) {
m.play(myArrayBuffer,i,audioCtx.sampleRate,shuffle([3,4,5,6]).pop());
}
});
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
for(let i = 0;i < channels;++i) myArrayBuffer.copyToChannel(bl(myArrayBuffer.getChannelData(i)),i);
var source = audioCtx.createBufferSource();
source.onended = sound;
// set the buffer in the AudioBufferSourceNode
source.buffer = myArrayBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the source playing
source.start();
}
sound();
</script>
</body>
</html>