From 856937ddb1d640a13b023fdce445ecb631164cf2 Mon Sep 17 00:00:00 2001 From: km0 Date: Mon, 3 Apr 2023 01:24:19 +0200 Subject: [PATCH] t t t t looper sketch --- ] | 95 ++++++++++++++++++++++++++++++++++++++++++++++++ tap.js | 7 ++-- ttt.html | 31 ++++++++++++++++ ttt.js | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 ] create mode 100644 ttt.html create mode 100644 ttt.js diff --git a/] b/] new file mode 100644 index 0000000..ff204a5 --- /dev/null +++ b/] @@ -0,0 +1,95 @@ +// The surface where to tap +const ttt = document.querySelector("#ttt") + +// The input to store a sequence +const s = document.querySelector("#s") + + +let series = [] +let initial = null + +// The function of a tap +const tap = () => { + if (initial == null) { + initial = new Date() + series.push(0) + } else { + series.push((new Date() - initial)) + } + + ttt.innerHTML = series +} + + +const stap = () => { + if (initial == null) return + + // insert the final value + series.push((new Date() - initial)) + + // copy the series + let sequence = series + + // reset series and initial date + series = [] + initial = null + + return sequence +} + +const audioContext = new AudioContext() +const gain = new GainNode(audioContext, { + value: 0.05 +}) + + +const playNoise = (bandHz=1000, time=0) => { + + let noiseDuration = 0.05 + + const bufferSize = audioContext.sampleRate * noiseDuration + const noiseBuffer = new AudioBuffer({ + length: bufferSize, + sampleRate: audioContext.sampleRate + }) + + const data = noiseBuffer.getChannelData(0) + for (let i=0; i { + return new Promise((resolve)=>{ + const seq = (sequence = []) => { + if (!sequence.length) resolve() + setTimeout( ()=> playNoise(), sequence[0]) + sequence.shift() + seq(sequence) + } + seq(sequence) + }) +} + + + +ttt.addEventListener("click", () => tap()) + +s.addEventListener("click", () => { + let sequence = stap(); + promiSeq([...sequence]) +}) + diff --git a/tap.js b/tap.js index 8274d86..83a204d 100644 --- a/tap.js +++ b/tap.js @@ -41,14 +41,13 @@ const playNoise = (bandHz = 1000, time = 0) => { type: "square", frequency: freq }) - + const amp = new GainNode(audioContext, { - value: 0 + value: 0 }) const lfo = new OscillatorNode(audioContext, { - type: "sine", - frequency: lfoFreq + type: "sine" frequency: lfoFreq }) lfo.connect(amp.gain) diff --git a/ttt.html b/ttt.html new file mode 100644 index 0000000..5d1a0f1 --- /dev/null +++ b/ttt.html @@ -0,0 +1,31 @@ + + + + + t t t + + + + + + +
+ + diff --git a/ttt.js b/ttt.js new file mode 100644 index 0000000..ddb6672 --- /dev/null +++ b/ttt.js @@ -0,0 +1,107 @@ +// The surface where to tap +const ttt = document.querySelector("#ttt") + +// The input to store a sequence +const s = document.querySelector("#s") + + +let series = [] +let initial = null + +// The function of a tap +const tap = () => { + if (initial == null) { + initial = new Date() + series.push(0) + } else { + series.push((new Date() - initial)) + } + + ttt.innerHTML = series +} + + +const stap = () => { + if (initial == null) return + + // insert the final value + series.push((new Date() - initial)) + + // copy the series + let sequence = series + + // reset series and initial date + series = [] + initial = null + + return sequence +} + +const audioContext = new AudioContext() +const gain = new GainNode(audioContext, { + value: 0.05 +}) + + +const playNoise = (bandHz=1000, time=0) => { + + let noiseDuration = 0.05 + + const bufferSize = audioContext.sampleRate * noiseDuration + const noiseBuffer = new AudioBuffer({ + length: bufferSize, + sampleRate: audioContext.sampleRate + }) + + const data = noiseBuffer.getChannelData(0) + for (let i=0; i { + const osc = new OscillatorNode(audioContext, { + type: "sine", + frequency: freq + }) + osc.connect(gain).connect(audioContext.destination); + osc.start() + osc.stop(audioContext.currentTime + 0.1) +} + +const seq = (sequence, fn) => { + if (!sequence.length) return + setTimeout(fn, sequence[0]) + let copy = [...sequence] + copy.shift() + seq(copy,fn) + } + + +ttt.addEventListener("click", () => tap()) + + +s.addEventListener("click", () => { + let sequence = stap(); + let period = sequence.pop() + let freq = Math.random() * 1000 + seq(sequence, ()=> playOsc(freq)) + setInterval(seq, period, sequence, ()=>playOsc(freq)) + +}) + +