mirror of
https://codeberg.org/uzu/strudel
synced 2026-09-05 14:58:03 -04:00
simplify
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
export const laserclock = ({ onTick, audioContext }) => {
|
||||
const ready = new Promise((resolve) => {
|
||||
document.addEventListener('click', async function init() {
|
||||
document.removeEventListener('click', init);
|
||||
audioContext = audioContext || new AudioContext();
|
||||
audioContext.resume();
|
||||
const workletCode = `class LaserClock extends AudioWorkletProcessor {
|
||||
const ready = new Promise(async (resolve) => {
|
||||
const workletCode = `class LaserClock extends AudioWorkletProcessor {
|
||||
constructor() {
|
||||
super();
|
||||
this.block = 0;
|
||||
this.tick = 0;
|
||||
this.started = true;
|
||||
this.started = false;
|
||||
this.blocksPerCallback = 20;
|
||||
this.port.onmessage = (e) => {
|
||||
switch (e.data) {
|
||||
@@ -39,13 +35,12 @@ export const laserclock = ({ onTick, audioContext }) => {
|
||||
}
|
||||
}
|
||||
registerProcessor("laserclock-processor", LaserClock);`;
|
||||
const dataURL = `data:text/javascript;base64,${btoa(workletCode)}`;
|
||||
await audioContext.audioWorklet.addModule(dataURL);
|
||||
const clock = new AudioWorkletNode(audioContext, 'laserclock-processor');
|
||||
clock.port.onmessage = (e) => onTick(e.data);
|
||||
clock.connect(audioContext.destination);
|
||||
resolve({ audioContext, clock, start, stop });
|
||||
});
|
||||
const dataURL = `data:text/javascript;base64,${btoa(workletCode)}`;
|
||||
await audioContext.audioWorklet.addModule(dataURL);
|
||||
const clock = new AudioWorkletNode(audioContext, 'laserclock-processor');
|
||||
clock.port.onmessage = (e) => onTick(e.data);
|
||||
clock.connect(audioContext.destination);
|
||||
resolve({ audioContext, clock, start, stop });
|
||||
});
|
||||
const start = () => ready.then(({ clock }) => clock.port.postMessage('start'));
|
||||
const stop = () => ready.then(({ clock }) => clock.port.postMessage('stop'));
|
||||
|
||||
@@ -5,17 +5,19 @@
|
||||
import { laserclock } from '../laserclock.mjs';
|
||||
import { evaluate } from '@strudel/transpiler';
|
||||
import { evalScope } from '@strudel/core';
|
||||
import { superdough, setDefaultAudioContext, registerSynthSounds } from '@strudel/webaudio';
|
||||
import { superdough, getAudioContext, registerSynthSounds, initAudioOnFirstClick } from '@strudel/webaudio';
|
||||
const init = async () => {
|
||||
initAudioOnFirstClick();
|
||||
await evalScope(import('@strudel/core'), import('@strudel/mini'), import('@strudel/tonal'));
|
||||
};
|
||||
init().then(async () => {
|
||||
const ctx = getAudioContext();
|
||||
let { pattern } = await evaluate('chord("<Dm7 G7 C^7 A7b9>(3,8)").jux(rev).voicing()');
|
||||
let last;
|
||||
let minLatency = 0.1;
|
||||
const { start, stop, ready } = laserclock({
|
||||
onTick: async (data) => {
|
||||
await ready;
|
||||
const { start, stop } = laserclock({
|
||||
audioContext: ctx,
|
||||
onTick: (data) => {
|
||||
const { currentTime } = data;
|
||||
if (last) {
|
||||
const haps = pattern.queryArc(last, currentTime).filter((h) => h.hasOnset());
|
||||
@@ -27,7 +29,6 @@
|
||||
last = currentTime;
|
||||
},
|
||||
} as any);
|
||||
ready.then(({ audioContext }) => setDefaultAudioContext(audioContext));
|
||||
registerSynthSounds();
|
||||
|
||||
document.getElementById('playButton')?.addEventListener('click', () => start());
|
||||
|
||||
Reference in New Issue
Block a user