-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
351 lines (302 loc) · 8.14 KB
/
script.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import Editor, { registerEvents } from './editor/editor.js'
import LoopNode from './loop-node.js'
import Shared32Array from './shared32array.js'
import * as api from './api.js'
const initial = `\
// guide:
//
// ctrl+enter = play/pause
//
// mod(measure=1) = [beat time] modulo(%) [measure] (loop)
// sin(hz) saw(hz) ramp(hz) sqr(hz) tri(hz) pulse(hz,width) noise(seed)
// gen+w = wavetable, i.e: sinw saww triw ...
// gen+t = time synced, i.e: sint sawt, trit ...
// val(x) = explicit value x
// push() = pushes value to spare to join later
// join() = joins/sums previous spare values
// exp(decay_speed=10) = reverse exponential curve (decay)
// pat('.1 .2 .5 1',measure=last_mod) = pattern of scalars
// pat('a4 f#5 c3 d3',measure=last_mod) = pattern of notes
// patv(...) = shortcut to pattern volumes
// slide('.1 .2 .5 1',measure=last_mod,speed=1) = slide pattern
// note: for sliding hz use wavetable oscillators
// slidev(...) = shortcut to slide volumes
// note('b#4') = note musical value in hz
// offt(time_offset) = shift time by time_offset (used with mod)
// vol(x)|mul(x) = multiply current value by x
// lp1(cut,amt=1) hp1(cut,amt=1)
// lp(cut,res=1,amt=1) hp(cut,res=1,amt=1)
// bp(cut,res=1,amt=1) bpp(cut,res=1,amt=1)
// not(cut,res=1,amt=1) ap(cut,res=1,amt=1)
// pk(cut,res=1,gain=1,amt=1)
// ls(cut,res=1,gain=1,amt=1) hs(cut,res=1,gain=1,amt=1)
// eq(bp(...),ls(...),...) = equalizer
// on(beat,measure,count=beat).[call]() = schedule next call
// use a second parenthesis group schedule many calls:
// i.e on(...).many().calls.grouped()()
// to bypass use an 'x' before, so xon() will passthrough
// delay(measure=1/16,feedback=.5,amt=.5)
// tanh(x=1) = tanh value multiplied by x (s-curve distortion)
// out(vol=1) = send value to speakers
// send('send_name',amt=1) = sends to send channel "send_name"
// val(send.send_name)...out() = process send "send_name"
// send.out is the output and it's chainable like everything
//
// all changes are saved immediately and refresh
// brings back the state as it was. to reset it
// type in devtools console:
// delete localStorage.last
//
// enjoy :)
var kick =
mod(1/4).sinm(mod(1/4).val(42.881).exp(.057))
.exp(8.82).tanh(15.18)
.out(.4)
var snare =
mod(1/2).sample('freesound:220752',-19025,.95)
.slidev('- - 1 -',1/8,.5)
.patv('- - 1 .3',1/8)
.tanh(2)
.daverb()
.out(.4)
var crash =
on(1,2,8)
.noisew(1)
.bp(6000)
.bp(14000)
.out(.07)
var sitar =
sample('freesound:350547').daverb().out()
var the_future =
sample('freesound:166834')
.daverb({
wet: .5,
preDelay: 1000,
bandwidth: .22,
inputDiffusion1: .8,
inputDiffusion2: .7,
decay: .6,
damping: .092,
decayDiffusion1: .89,
decayDiffusion2: .87,
excursionRate: .25,
excursionDepth: .12
})
.hs(5000,1,-15,.5)
.delay(16/(512+(420*sint(1/64))),.8).bpp(3000,.12,.5)
.out(.7)
// var hihat =
// mod(1/16).noisew(1).exp(30)
// .patv('.1 .4 1 .4')
// .on(8,1/4).patv('1.5 15',1/32)
// .hs(16000)
// // .bp(2000,3,1)
// .bp(500+mod(1/2).val(8000).exp(2.85),.5,.5)
// .on(8,2).vol(0)
// .out(.23)
// var clap =
// mod(1/4).noisew(8).exp(110)
// .push().offt(.986).noisew(10).exp(110).vol(1.25)
// .push().offt(.976).noisew(8).exp(110).vol(.9)
// .push().noisew(8).exp(8.5).vol(.1)
// .join()
// .patv('- 1')
// .bp(1200,1.7,1)
// .on(8,1/4).send('fx')
// .send('reverb',.5)
// .out(.45)
// var bass_melody =
// val(50)
// .on(8,1/8).val(70)
// .on(8,1/2,16).mul(1.5)
// .on(16,1/2).mul(2)
// var bass =
// mod(1/16).pulsew(bass_melody).exp(10)
// .patv('.1 .1 .5 1')
// .lp(700,1.2)
// .out(.35)
send.out
.on(2,1,32)
.slidev('1.1 - - - 1.1 - - - 1.1 - 1.2 - 1 - 1 -', 1/16, 5)
.on(8,2)
.vol(.65).bp(2000+sint(1/32)*1800,5)
()
`
const numberOfChannels = 1
const sampleRate = 44100
const bpm = 120
let audio, node, buffer, render = () => {}
let once = true
let editor
let currentWorker
let nextWorker
let playing = false
let updateInProgress = false
let hasChanged = false
let n = 0
const readyCallbacks = []
const methods = {
ready () {
readyCallbacks.splice(0).forEach(cb => cb())
},
play (worker, data) {
n = data.n
playing = true
updateInProgress = false
node.playBuffer(worker.buffer)
},
async fetchSample (worker, { url }) {
const sample = await api.fetchSample(audio, url)
return { sample }
}
}
const workerUrl = new URL('render-worker.js', import.meta.url)
const worker = new Worker(workerUrl, { type: 'module' })
worker.onmessage = async ({ data }) => {
let result
try {
result = await methods[data.call](worker, data)
} catch (error) {
result = { error }
}
if (data.callback) {
worker.postMessage({
call: 'callback',
callback: data.callback,
...result
})
}
}
worker.onerror = error => {
updateInProgress = false
console.error(error)
}
worker.onmessageerror = error => {
updateInProgress = false
console.error(error)
}
const requestNextBuffer = () => {
if (updateInProgress) return
updateInProgress = true
worker.postMessage({ call: 'play' })
}
const updateRenderFunction = (force = false) => {
if (updateInProgress && !force) return
hasChanged = false
updateInProgress = true
console.log('updating function')
worker.postMessage({
call: 'updateRenderFunction',
value: editor.value,
n: n //+node.bufferSize
})
}
let toggle = async () => {
audio = new AudioContext({
numberOfChannels,
sampleRate,
latencyHint: 'playback' // without this audio glitches
})
node = new LoopNode({ numberOfChannels, bpm })
node.connect(audio.destination)
worker.buffer = Array(numberOfChannels).fill(0).map(() =>
new Shared32Array(node.bufferSize))
if (offscreen) {
worker.postMessage({
call: 'setup',
buffer: worker.buffer,
sampleRate,
beatRate: node.beatRate,
canvas: offscreen,
width: window.innerWidth,
height: window.innerHeight,
pixelRatio: window.devicePixelRatio,
}, [offscreen])
} else {
worker.postMessage({
call: 'setup',
buffer: worker.buffer,
sampleRate,
beatRate: node.beatRate
})
}
offscreen = null
await new Promise(resolve => readyCallbacks.push(resolve))
console.log('received worker ready')
node.onbar = () => {
if (hasChanged) {
updateRenderFunction()
} else {
requestNextBuffer()
}
}
console.log('connected node')
const start = () => {
updateRenderFunction()
node.start()
document.body.onclick = () => {}
toggle = () => {
node.stop(0)
toggle = start
}
}
toggle = start
start()
}
document.body.onclick = () => {
document.body.onclick = () => {}
document.body.addEventListener('keydown', e => {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.stopPropagation()
e.preventDefault()
toggle()
return false
}
if (e.key === 's' && (e.ctrlKey || e.metaKey)) {
updateRenderFunction(true)
}
}, { capture: true })
}
let offscreen
const main = async () => {
const canvas = document.createElement('canvas')
canvas.className = 'back-canvas'
canvas.style.width = window.innerWidth + 'px'
canvas.style.height = window.innerHeight + 'px'
offscreen = canvas.transferControlToOffscreen()
container.appendChild(canvas)
editor = new Editor({
id: 'main',
title: 'new-project.js',
font: '/fonts/SpaceMono-Regular.woff2',
value: localStorage.last ?? initial,
fontSize: '11.5pt',
// fontSize: '16.4pt',
padding: 10,
titlebarHeight: 0,
width: window.innerWidth,
height: window.innerHeight,
})
editor.onchange = () => {
localStorage.last = editor.value
hasChanged = true
}
container.appendChild(editor.canvas)
editor.parent = document.body
editor.rect = editor.canvas.getBoundingClientRect()
registerEvents(document.body)
window.onresize = () => {
editor.resize({
width: window.innerWidth,
height: window.innerHeight
})
worker.postMessage({
call: 'resize',
width: window.innerWidth,
height: window.innerHeight
})
canvas.style.width = window.innerWidth + 'px'
canvas.style.height = window.innerHeight + 'px'
}
}
main()