From 496d60dc1cd83cbb7901996632879f058cfdae8a Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Fri, 3 May 2024 14:56:16 -0400 Subject: [PATCH 01/17] testing --- packages/core/controls.mjs | 11 +++++ packages/core/cyclist.mjs | 2 +- packages/core/neocyclist.mjs | 11 ++++- packages/core/repl.mjs | 6 +-- packages/superdough/superdough.mjs | 5 ++- packages/superdough/worklets.mjs | 65 ++++++++++++++++++++++++++++++ packages/webaudio/webaudio.mjs | 7 ++-- 7 files changed, 98 insertions(+), 9 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 166f0ac23..2364cd994 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -430,6 +430,17 @@ export const { crush } = registerControl('crush'); */ export const { coarse } = registerControl('coarse'); +/** + * modulate the output gain of a sound with a continuous wave + * + * @name gainmod + * @param {number | Pattern} factor 1 for original 2 for half, 3 for a third and so on. + * @example + * s("triangle").gainmod("2:1:0") + * + */ +export const { gainmod } = registerControl('gainmod'); + /** * Allows you to set the output channels on the interface * diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 08b893752..d61267475 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -56,7 +56,7 @@ export class Cyclist { // the following line is dumb and only here for backwards compatibility // see https://github.com/tidalcycles/strudel/pull/1004 const deadline = targetTime - phase; - onTrigger?.(hap, deadline, duration, this.cps, targetTime); + onTrigger?.(hap, deadline, duration, this.cps, targetTime, end); } }); } catch (e) { diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 4600d0ef9..6bc0a8404 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -4,6 +4,7 @@ Copyright (C) 2022 Strudel contributors - see . */ +import Fraction from 'fraction.js'; import { logger } from './logger.mjs'; export class NeoCyclist { @@ -86,7 +87,15 @@ export class NeoCyclist { this.latency + this.worker_time_dif; const duration = hap.duration / this.cps; - onTrigger?.(hap, 0, duration, this.cps, targetTime); + onTrigger?.( + hap, + 0, + duration, + this.cps, + targetTime, + this.cycle, + // + Fraction(this.latency).div(this.cps) + ); } }); }; diff --git a/packages/core/repl.mjs b/packages/core/repl.mjs index 5b7bcd71b..af5935787 100644 --- a/packages/core/repl.mjs +++ b/packages/core/repl.mjs @@ -178,15 +178,15 @@ export function repl({ export const getTrigger = ({ getTime, defaultOutput }) => - async (hap, deadline, duration, cps, t) => { + async (hap, deadline, duration, cps, t, cycle = 0) => { // TODO: get rid of deadline after https://github.com/tidalcycles/strudel/pull/1004 try { if (!hap.context.onTrigger || !hap.context.dominantTrigger) { - await defaultOutput(hap, deadline, duration, cps, t); + await defaultOutput(hap, deadline, duration, cps, t, cycle); } if (hap.context.onTrigger) { // call signature of output / onTrigger is different... - await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps, t); + await hap.context.onTrigger(getTime() + deadline, hap, getTime(), cps, t, cycle); } } catch (err) { logger(`[cyclist] error: ${err.message}`, 'error'); diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index c0ba96e06..138a3a38d 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -260,7 +260,8 @@ export function resetGlobalEffects() { analysersData = {}; } -export const superdough = async (value, t, hapDuration) => { +export const superdough = async (value, t, hapDuration, cps, cycle) => { + console.log({ cps, cycle }); const ac = getAudioContext(); if (typeof value !== 'object') { throw new Error( @@ -322,6 +323,7 @@ export const superdough = async (value, t, hapDuration) => { phasercenter, // coarse, + gainmod, crush, shape, shapevol = 1, @@ -470,6 +472,7 @@ export const superdough = async (value, t, hapDuration) => { crush !== undefined && chain.push(getWorklet(ac, 'crush-processor', { crush })); shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', { shape, postgain: shapevol })); distort !== undefined && chain.push(getWorklet(ac, 'distort-processor', { distort, postgain: distortvol })); + gainmod !== undefined && chain.push(getWorklet(ac, 'gainmod-processor', { speed: gainmod, cps, cycle })); compressorThreshold !== undefined && chain.push( diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index bab28987e..f29d1028f 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1,6 +1,36 @@ // coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js // LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE +const linearEnvelope = (startVal, EndVal, startTime, endTime, currentTime, hold = 0) => { + let min = 0.001; + currentTime = currentTime - hold; + if (startTime > currentTime) { + return 1; + } + if (currentTime > endTime) { + return min; + } + // change relative start time to 0 to prevent numeric overflow + currentTime = currentTime - startTime; + endTime = endTime - startTime; + startTime = 0; + + let x1 = startTime; + let y1 = startVal; + let x2 = endTime; + let y2 = EndVal; + + // Calculate the growth or decay rate (b) + let b = y1 / y2 / (x1 - x2); + + // Calculate the initial value (a) + let a = y1 / (b * x1); + + let x = currentTime; + // calculate y for any x + return a * (b * x); +}; + class CoarseProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [{ name: 'coarse', defaultValue: 1 }]; @@ -62,6 +92,41 @@ class CrushProcessor extends AudioWorkletProcessor { } registerProcessor('crush-processor', CrushProcessor); +class GainModProcessor extends AudioWorkletProcessor { + static get parameterDescriptors() { + return [ + { name: 'cps', defaultValue: 0.5 }, + { name: 'speed', defaultValue: 0.5 }, + { name: 'cycle', defaultValue: 0 }, + ]; + } + + constructor() { + super(); + } + + process(inputs, outputs, parameters) { + const input = inputs[0]; + const output = outputs[0]; + const blockSize = 128; + + let crush = parameters.crush[0] ?? 8; + crush = Math.max(1, crush); + + if (input[0] == null || output[0] == null) { + return false; + } + for (let n = 0; n < blockSize; n++) { + for (let i = 0; i < input.length; i++) { + const x = Math.pow(2, crush - 1); + output[i][n] = Math.round(input[i][n] * x) / x; + } + } + return true; + } +} +registerProcessor('gainmod-processor', GainModProcessor); + class ShapeProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [ diff --git a/packages/webaudio/webaudio.mjs b/packages/webaudio/webaudio.mjs index f82a436a9..36d11b096 100644 --- a/packages/webaudio/webaudio.mjs +++ b/packages/webaudio/webaudio.mjs @@ -17,9 +17,9 @@ const hap2value = (hap) => { export const webaudioOutputTrigger = (t, hap, ct, cps) => superdough(hap2value(hap), t - ct, hap.duration / cps, cps); // uses more precise, absolute t if available, see https://github.com/tidalcycles/strudel/pull/1004 -export const webaudioOutput = (hap, deadline, hapDuration, cps, t) => - superdough(hap2value(hap), t ? `=${t}` : deadline, hapDuration); - +export const webaudioOutput = (hap, deadline, hapDuration, cps, t, cycle) => { + return superdough(hap2value(hap), t ? `=${t}` : deadline, hapDuration, cps, cycle); +}; Pattern.prototype.webaudio = function () { return this.onTrigger(webaudioOutputTrigger); }; @@ -30,6 +30,7 @@ export function webaudioScheduler(options = {}) { defaultOutput: webaudioOutput, ...options, }; + const { defaultOutput, getTime } = options; return new strudel.Cyclist({ ...options, From 66a27a609ca3bc26c9f6bd962fad9429d2e686b5 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Fri, 3 May 2024 14:58:20 -0400 Subject: [PATCH 02/17] implementing --- packages/superdough/worklets.mjs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index f29d1028f..9800a8ed1 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -106,23 +106,21 @@ class GainModProcessor extends AudioWorkletProcessor { } process(inputs, outputs, parameters) { - const input = inputs[0]; - const output = outputs[0]; - const blockSize = 128; - - let crush = parameters.crush[0] ?? 8; - crush = Math.max(1, crush); - - if (input[0] == null || output[0] == null) { - return false; - } - for (let n = 0; n < blockSize; n++) { - for (let i = 0; i < input.length; i++) { - const x = Math.pow(2, crush - 1); - output[i][n] = Math.round(input[i][n] * x) / x; - } - } - return true; + // const input = inputs[0]; + // const output = outputs[0]; + // const blockSize = 128; + // let crush = parameters.crush[0] ?? 8; + // crush = Math.max(1, crush); + // if (input[0] == null || output[0] == null) { + // return false; + // } + // for (let n = 0; n < blockSize; n++) { + // for (let i = 0; i < input.length; i++) { + // const x = Math.pow(2, crush - 1); + // output[i][n] = Math.round(input[i][n] * x) / x; + // } + // } + // return true; } } registerProcessor('gainmod-processor', GainModProcessor); From 0d900c01342d0d6d860b67b2ea8a40867de21fb3 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sun, 5 May 2024 12:02:51 -0400 Subject: [PATCH 03/17] sine test --- packages/superdough/worklets.mjs | 47 ++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 9800a8ed1..be4d92c41 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -91,6 +91,14 @@ class CrushProcessor extends AudioWorkletProcessor { } } registerProcessor('crush-processor', CrushProcessor); +const sine = (phase, dt) => { + return Math.sin(Math.PI * 2 * phase); + + // return Math.sin(phase); +}; +const getModulator = (frequency, values, ct) => { + Math.sin(); +}; class GainModProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { @@ -103,23 +111,44 @@ class GainModProcessor extends AudioWorkletProcessor { constructor() { super(); + this.phase; + this.inc = 0; + } + + incrementPhase(dt) { + this.phase += dt; + if (this.phase > 1.0) { + this.phase = this.phase - 1; + } } process(inputs, outputs, parameters) { - // const input = inputs[0]; - // const output = outputs[0]; - // const blockSize = 128; + const speed = parameters['speed'][0]; + const cps = parameters['cps'][0]; + const cycle = parameters['cycle'][0]; + const frequency = speed * cps; + if (this.phase == null) { + this.phase = (cycle * cps * frequency) % 1; + } + + const input = inputs[0]; + const output = outputs[0]; + const blockSize = 128; // let crush = parameters.crush[0] ?? 8; // crush = Math.max(1, crush); // if (input[0] == null || output[0] == null) { // return false; // } - // for (let n = 0; n < blockSize; n++) { - // for (let i = 0; i < input.length; i++) { - // const x = Math.pow(2, crush - 1); - // output[i][n] = Math.round(input[i][n] * x) / x; - // } - // } + const dt = frequency / sampleRate; + for (let n = 0; n < blockSize; n++) { + for (let i = 0; i < input.length; i++) { + const modval = sine(this.phase, dt); + + output[i][n] = input[i][n] * modval; + } + this.incrementPhase(dt); + this.inc += 1; + } // return true; } } From 3159f665604089b4233b140441e1630e7a872b12 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sun, 5 May 2024 22:52:18 -0400 Subject: [PATCH 04/17] working --- packages/superdough/superdough.mjs | 15 ++- packages/superdough/worklets.mjs | 142 ++++++++++++++--------------- 2 files changed, 83 insertions(+), 74 deletions(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 138a3a38d..b6e06e635 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -261,7 +261,6 @@ export function resetGlobalEffects() { } export const superdough = async (value, t, hapDuration, cps, cycle) => { - console.log({ cps, cycle }); const ac = getAudioContext(); if (typeof value !== 'object') { throw new Error( @@ -282,6 +281,8 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { } // destructure let { + amdepth = 1, + amshape = 'tri', s = 'triangle', bank, source, @@ -326,6 +327,7 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { gainmod, crush, shape, + shapevol = 1, distort, distortvol = 1, @@ -472,7 +474,16 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { crush !== undefined && chain.push(getWorklet(ac, 'crush-processor', { crush })); shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', { shape, postgain: shapevol })); distort !== undefined && chain.push(getWorklet(ac, 'distort-processor', { distort, postgain: distortvol })); - gainmod !== undefined && chain.push(getWorklet(ac, 'gainmod-processor', { speed: gainmod, cps, cycle })); + gainmod !== undefined && + chain.push( + getWorklet(ac, 'gainmod-processor', { + speed: gainmod, + // depth: amdepth, shape: amshape, + + cps, + cycle, + }), + ); compressorThreshold !== undefined && chain.push( diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index be4d92c41..2f1375f01 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1,34 +1,70 @@ // coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js // LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE -const linearEnvelope = (startVal, EndVal, startTime, endTime, currentTime, hold = 0) => { - let min = 0.001; - currentTime = currentTime - hold; - if (startTime > currentTime) { - return 1; - } - if (currentTime > endTime) { - return min; +const clamp = (num, min, max) => Math.min(Math.max(num, min), max); +// adjust waveshape to remove frequencies above nyquist to prevent aliasing +// referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 +function polyBlep(phase, dt) { + // 0 <= phase < 1 + if (phase < dt) { + phase /= dt; + // 2 * (phase - phase^2/2 - 0.5) + return phase + phase - phase * phase - 1; } - // change relative start time to 0 to prevent numeric overflow - currentTime = currentTime - startTime; - endTime = endTime - startTime; - startTime = 0; - - let x1 = startTime; - let y1 = startVal; - let x2 = endTime; - let y2 = EndVal; - // Calculate the growth or decay rate (b) - let b = y1 / y2 / (x1 - x2); + // -1 < phase < 0 + else if (phase > 1 - dt) { + phase = (phase - 1) / dt; + // 2 * (phase^2/2 + phase + 0.5) + return phase * phase + phase + phase + 1; + } - // Calculate the initial value (a) - let a = y1 / (b * x1); + // 0 otherwise + else { + return 0; + } +} - let x = currentTime; - // calculate y for any x - return a * (b * x); +const waveshapes = { + sine(phase) { + return Math.sin(Math.PI * 2 * phase); + }, + custom(phase, values = [0, 1]) { + const numParts = values.length - 1; + const currPart = Math.floor(phase * numParts); + + const partLength = 1 / numParts; + const startVal = clamp(values[currPart], 0, 1); + const endVal = clamp(values[currPart + 1], 0, 1); + const y2 = endVal; + const y1 = startVal; + const x1 = 0; + const x2 = partLength; + const slope = (y2 - y1) / (x2 - x1); + return slope * (phase - partLength * currPart) + startVal; + }, + sawblep(phase, dt) { + const v = 2 * phase - 1; + return v - polyBlep(phase, dt); + }, + ramp(phase) { + return phase; + }, + saw(phase) { + return 1 - phase; + }, + tri(phase, skew = 0.5) { + if (phase >= skew) { + return 1 - ((phase / (1 - skew)) % 1); + } + return (phase / skew) % 1; + }, + square(phase, skew = 0.5) { + if (phase >= skew) { + return 1; + } + return 0; + }, }; class CoarseProcessor extends AudioWorkletProcessor { @@ -91,14 +127,6 @@ class CrushProcessor extends AudioWorkletProcessor { } } registerProcessor('crush-processor', CrushProcessor); -const sine = (phase, dt) => { - return Math.sin(Math.PI * 2 * phase); - - // return Math.sin(phase); -}; -const getModulator = (frequency, values, ct) => { - Math.sin(); -}; class GainModProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { @@ -106,13 +134,14 @@ class GainModProcessor extends AudioWorkletProcessor { { name: 'cps', defaultValue: 0.5 }, { name: 'speed', defaultValue: 0.5 }, { name: 'cycle', defaultValue: 0 }, + // { name: 'shape', defaultValue: 'tri' }, + // { name: 'depth', defaultValue: 1 }, ]; } constructor() { super(); this.phase; - this.inc = 0; } incrementPhase(dt) { @@ -126,30 +155,27 @@ class GainModProcessor extends AudioWorkletProcessor { const speed = parameters['speed'][0]; const cps = parameters['cps'][0]; const cycle = parameters['cycle'][0]; + + const blockSize = 128; const frequency = speed * cps; if (this.phase == null) { - this.phase = (cycle * cps * frequency) % 1; + const secondsPassed = cycle / cps; + this.phase = Math.max(0, (secondsPassed * frequency) % 1); } const input = inputs[0]; const output = outputs[0]; - const blockSize = 128; - // let crush = parameters.crush[0] ?? 8; - // crush = Math.max(1, crush); - // if (input[0] == null || output[0] == null) { - // return false; - // } + const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - const modval = sine(this.phase, dt); + const modval = waveshapes.tri(this.phase, 0.99); output[i][n] = input[i][n] * modval; } this.incrementPhase(dt); - this.inc += 1; } - // return true; + return true; } } registerProcessor('gainmod-processor', GainModProcessor); @@ -222,34 +248,6 @@ class DistortProcessor extends AudioWorkletProcessor { } registerProcessor('distort-processor', DistortProcessor); -// adjust waveshape to remove frequencies above nyquist to prevent aliasing -// referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 -const polyBlep = (phase, dt) => { - // 0 <= phase < 1 - if (phase < dt) { - phase /= dt; - // 2 * (phase - phase^2/2 - 0.5) - return phase + phase - phase * phase - 1; - } - - // -1 < phase < 0 - else if (phase > 1 - dt) { - phase = (phase - 1) / dt; - // 2 * (phase^2/2 + phase + 0.5) - return phase * phase + phase + phase + 1; - } - - // 0 otherwise - else { - return 0; - } -}; - -const saw = (phase, dt) => { - const v = 2 * phase - 1; - return v - polyBlep(phase, dt); -}; - function lerp(a, b, n) { return n * (b - a) + a; } @@ -349,7 +347,7 @@ class SuperSawOscillatorProcessor extends AudioWorkletProcessor { for (let i = 0; i < output[0].length; i++) { this.phase[n] = this.phase[n] ?? Math.random(); - const v = saw(this.phase[n], dt); + const v = waveshapes.sawblep(this.phase[n], dt); output[0][i] = output[0][i] + v * gainL; output[1][i] = output[1][i] + v * gainR; From 7df663e87dac1c28220fc2cac21ab68ab976b5f9 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Sun, 5 May 2024 23:54:30 -0400 Subject: [PATCH 05/17] memory leaking --- packages/core/controls.mjs | 31 +++++++++++++++++++++++++----- packages/superdough/superdough.mjs | 11 ++++++----- packages/superdough/worklets.mjs | 9 +++++---- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 2364cd994..b99f83564 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -431,16 +431,37 @@ export const { crush } = registerControl('crush'); export const { coarse } = registerControl('coarse'); /** - * modulate the output gain of a sound with a continuous wave + * modulate the amplitude of a sound with a continuous waveform * - * @name gainmod - * @param {number | Pattern} factor 1 for original 2 for half, 3 for a third and so on. + * @name am + * @param {number | Pattern} speed modulation speed in cycles + * @example + * s("triangle").am("2").amshape("").amdepth(.5) + * + */ +export const { am } = registerControl(['am', 'amdepth', 'amshape']); + +/** + * depth of amplitude modulation + * + * @name amdepth + * @param {number | Pattern} depth * @example - * s("triangle").gainmod("2:1:0") + * s("triangle").am(1).amdepth("1") * */ -export const { gainmod } = registerControl('gainmod'); +export const { amdepth } = registerControl('amdepth'); +/** + * shape of amplitude modulation + * + * @name amshape + * @param {number | Pattern} shape tri | square | sine | saw | ramp + * @example + * note("{f g c d}%16").am(4).amshape("ramp").s("sawtooth") + * + */ +export const { amshape } = registerControl('amshape'); /** * Allows you to set the output channels on the interface * diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index b6e06e635..13c138dcc 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -324,7 +324,7 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { phasercenter, // coarse, - gainmod, + am, crush, shape, @@ -474,11 +474,12 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { crush !== undefined && chain.push(getWorklet(ac, 'crush-processor', { crush })); shape !== undefined && chain.push(getWorklet(ac, 'shape-processor', { shape, postgain: shapevol })); distort !== undefined && chain.push(getWorklet(ac, 'distort-processor', { distort, postgain: distortvol })); - gainmod !== undefined && + am !== undefined && chain.push( - getWorklet(ac, 'gainmod-processor', { - speed: gainmod, - // depth: amdepth, shape: amshape, + getWorklet(ac, 'am-processor', { + speed: am, + depth: amdepth, + // shape: amshape, cps, cycle, diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 2f1375f01..b3e32576a 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -128,14 +128,14 @@ class CrushProcessor extends AudioWorkletProcessor { } registerProcessor('crush-processor', CrushProcessor); -class GainModProcessor extends AudioWorkletProcessor { +class AMProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [ { name: 'cps', defaultValue: 0.5 }, { name: 'speed', defaultValue: 0.5 }, { name: 'cycle', defaultValue: 0 }, // { name: 'shape', defaultValue: 'tri' }, - // { name: 'depth', defaultValue: 1 }, + { name: 'depth', defaultValue: 1 }, ]; } @@ -155,6 +155,7 @@ class GainModProcessor extends AudioWorkletProcessor { const speed = parameters['speed'][0]; const cps = parameters['cps'][0]; const cycle = parameters['cycle'][0]; + const depth = clamp(parameters['depth'][0], 0, 1); const blockSize = 128; const frequency = speed * cps; @@ -169,7 +170,7 @@ class GainModProcessor extends AudioWorkletProcessor { const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - const modval = waveshapes.tri(this.phase, 0.99); + const modval = waveshapes.tri(this.phase, 0.99) * depth + (1 - depth); output[i][n] = input[i][n] * modval; } @@ -178,7 +179,7 @@ class GainModProcessor extends AudioWorkletProcessor { return true; } } -registerProcessor('gainmod-processor', GainModProcessor); +registerProcessor('am-processor', AMProcessor); class ShapeProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { From 16472b59ddef33bc7fdb332cbcf23ecc6e09be48 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Wed, 8 May 2024 00:05:05 -0400 Subject: [PATCH 06/17] fix cyclist clock cycle --- packages/core/cyclist.mjs | 4 +- packages/core/neocyclist.mjs | 11 +-- packages/superdough/worklets.mjs | 140 +++++++++++++++++-------------- 3 files changed, 83 insertions(+), 72 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index d61267475..b33e9cc36 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -34,6 +34,8 @@ export class Cyclist { try { const begin = this.lastEnd; + const cycle = this.now(); + this.lastBegin = begin; const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; this.lastEnd = end; @@ -56,7 +58,7 @@ export class Cyclist { // the following line is dumb and only here for backwards compatibility // see https://github.com/tidalcycles/strudel/pull/1004 const deadline = targetTime - phase; - onTrigger?.(hap, deadline, duration, this.cps, targetTime, end); + onTrigger?.(hap, deadline, duration, this.cps, targetTime, cycle); } }); } catch (e) { diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 6bc0a8404..8f9b269e0 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -4,7 +4,6 @@ Copyright (C) 2022 Strudel contributors - see . */ -import Fraction from 'fraction.js'; import { logger } from './logger.mjs'; export class NeoCyclist { @@ -87,15 +86,7 @@ export class NeoCyclist { this.latency + this.worker_time_dif; const duration = hap.duration / this.cps; - onTrigger?.( - hap, - 0, - duration, - this.cps, - targetTime, - this.cycle, - // + Fraction(this.latency).div(this.cps) - ); + onTrigger?.(hap, 0, duration, this.cps, targetTime, this.cycle); } }); }; diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index b3e32576a..107e9bf7f 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -2,6 +2,7 @@ // LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE const clamp = (num, min, max) => Math.min(Math.max(num, min), max); +const blockSize = 128; // adjust waveshape to remove frequencies above nyquist to prevent aliasing // referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 function polyBlep(phase, dt) { @@ -67,119 +68,129 @@ const waveshapes = { }, }; -class CoarseProcessor extends AudioWorkletProcessor { +class AMProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { - return [{ name: 'coarse', defaultValue: 1 }]; + return [ + { name: 'cps', defaultValue: 0.5 }, + { name: 'speed', defaultValue: 0.5 }, + { name: 'cycle', defaultValue: 0 }, + // { name: 'shape', defaultValue: 'tri' }, + { name: 'depth', defaultValue: 1 }, + ]; } constructor() { super(); + this.phase; + this.started = false; + } + + incrementPhase(dt) { + this.phase += dt; + if (this.phase > 1.0) { + this.phase = this.phase - 1; + } } process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; - const blockSize = 128; - - let coarse = parameters.coarse[0] ?? 0; - coarse = Math.max(1, coarse); - - if (input[0] == null || output[0] == null) { + const hasInput = !(input[0] === undefined); + if (this.started && !hasInput) { return false; } + this.started = hasInput; + + const speed = parameters['speed'][0]; + const cps = parameters['cps'][0]; + const cycle = parameters['cycle'][0]; + const depth = clamp(parameters['depth'][0], 0, 1); + const blockSize = 128; + const frequency = speed * cps; + if (this.phase == null) { + const secondsPassed = cycle / cps; + this.phase = Math.max(0, (secondsPassed * frequency) % 1); + } + + const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - output[i][n] = n % coarse === 0 ? input[i][n] : output[i][n - 1]; + const modval = waveshapes.tri(this.phase, 0.99) * depth + (1 - depth); + + output[i][n] = input[i][n] * modval; } + this.incrementPhase(dt); } return true; } } -registerProcessor('coarse-processor', CoarseProcessor); +registerProcessor('am-processor', AMProcessor); -class CrushProcessor extends AudioWorkletProcessor { +class CoarseProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { - return [{ name: 'crush', defaultValue: 0 }]; + return [{ name: 'coarse', defaultValue: 1 }]; } constructor() { super(); + this.started = false; } process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; - const blockSize = 128; - - let crush = parameters.crush[0] ?? 8; - crush = Math.max(1, crush); - if (input[0] == null || output[0] == null) { + const hasInput = !(input[0] === undefined); + if (this.started && !hasInput) { return false; } + this.started = hasInput; + + let coarse = parameters.coarse[0] ?? 0; + coarse = Math.max(1, coarse); for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - const x = Math.pow(2, crush - 1); - output[i][n] = Math.round(input[i][n] * x) / x; + output[i][n] = n % coarse === 0 ? input[i][n] : output[i][n - 1]; } } return true; } } -registerProcessor('crush-processor', CrushProcessor); +registerProcessor('coarse-processor', CoarseProcessor); -class AMProcessor extends AudioWorkletProcessor { +class CrushProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { - return [ - { name: 'cps', defaultValue: 0.5 }, - { name: 'speed', defaultValue: 0.5 }, - { name: 'cycle', defaultValue: 0 }, - // { name: 'shape', defaultValue: 'tri' }, - { name: 'depth', defaultValue: 1 }, - ]; + return [{ name: 'crush', defaultValue: 0 }]; } constructor() { super(); - this.phase; - } - - incrementPhase(dt) { - this.phase += dt; - if (this.phase > 1.0) { - this.phase = this.phase - 1; - } + this.started = false; } process(inputs, outputs, parameters) { - const speed = parameters['speed'][0]; - const cps = parameters['cps'][0]; - const cycle = parameters['cycle'][0]; - const depth = clamp(parameters['depth'][0], 0, 1); + const input = inputs[0]; + const output = outputs[0]; - const blockSize = 128; - const frequency = speed * cps; - if (this.phase == null) { - const secondsPassed = cycle / cps; - this.phase = Math.max(0, (secondsPassed * frequency) % 1); + const hasInput = !(input[0] === undefined); + if (this.started && !hasInput) { + return false; } + this.started = hasInput; - const input = inputs[0]; - const output = outputs[0]; + let crush = parameters.crush[0] ?? 8; + crush = Math.max(1, crush); - const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - const modval = waveshapes.tri(this.phase, 0.99) * depth + (1 - depth); - - output[i][n] = input[i][n] * modval; + const x = Math.pow(2, crush - 1); + output[i][n] = Math.round(input[i][n] * x) / x; } - this.incrementPhase(dt); } return true; } } -registerProcessor('am-processor', AMProcessor); +registerProcessor('crush-processor', CrushProcessor); class ShapeProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { @@ -191,21 +202,24 @@ class ShapeProcessor extends AudioWorkletProcessor { constructor() { super(); + this.started = false; } process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; - const blockSize = 128; + + const hasInput = !(input[0] === undefined); + if (this.started && !hasInput) { + return false; + } + this.started = hasInput; let shape = parameters.shape[0]; shape = shape < 1 ? shape : 1.0 - 4e-10; shape = (2.0 * shape) / (1.0 - shape); const postgain = Math.max(0.001, Math.min(1, parameters.postgain[0])); - if (input[0] == null || output[0] == null) { - return false; - } for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { output[i][n] = (((1 + shape) * input[i][n]) / (1 + shape * Math.abs(input[i][n]))) * postgain; @@ -226,19 +240,22 @@ class DistortProcessor extends AudioWorkletProcessor { constructor() { super(); + this.started = false; } process(inputs, outputs, parameters) { const input = inputs[0]; const output = outputs[0]; - const blockSize = 128; + + const hasInput = !(input[0] === undefined); + if (this.started && !hasInput) { + return false; + } + this.started = hasInput; const shape = Math.expm1(parameters.distort[0]); const postgain = Math.max(0.001, Math.min(1, parameters.postgain[0])); - if (input[0] == null || output[0] == null) { - return false; - } for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { output[i][n] = (((1 + shape) * input[i][n]) / (1 + shape * Math.abs(input[i][n]))) * postgain; @@ -249,6 +266,7 @@ class DistortProcessor extends AudioWorkletProcessor { } registerProcessor('distort-processor', DistortProcessor); +// SUPERSAW function lerp(a, b, n) { return n * (b - a) + a; } From 6c05a4eb7becfb47e322eff6b6a00d351d5dc888 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 9 May 2024 00:21:21 -0400 Subject: [PATCH 07/17] fixed tri shape --- packages/core/controls.mjs | 23 ++++++++++++++++++++++- packages/core/cyclist.mjs | 5 ++++- packages/superdough/superdough.mjs | 8 ++++++-- packages/superdough/worklets.mjs | 24 ++++++++++++++---------- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index b99f83564..46523588a 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -439,7 +439,7 @@ export const { coarse } = registerControl('coarse'); * s("triangle").am("2").amshape("").amdepth(.5) * */ -export const { am } = registerControl(['am', 'amdepth', 'amshape']); +export const { am } = registerControl(['am', 'amdepth', 'amskew', 'amphase']); /** * depth of amplitude modulation @@ -451,6 +451,27 @@ export const { am } = registerControl(['am', 'amdepth', 'amshape']); * */ export const { amdepth } = registerControl('amdepth'); +/** + * alter the shape of the modulation waveform + * + * @name amskew + * @param {number | Pattern} amount between 0 & 1, the shape of the waveform + * @example + * note("{f a c e}%16").am(4).amskew("<.5 0 1>") + * + */ +export const { amskew } = registerControl('amskew'); + +/** + * alter the phase of the modulation waveform + * + * @name amphase + * @param {number | Pattern} offset the offset in cycles of the modulation + * @example + * note("{f a c e}%16").am(4).amphase("<0 .25 .66>") + * + */ +export const { amphase } = registerControl('amskew'); /** * shape of amplitude modulation diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index b33e9cc36..41e31ce5c 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -34,13 +34,16 @@ export class Cyclist { try { const begin = this.lastEnd; - const cycle = this.now(); this.lastBegin = begin; const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; + this.lastEnd = end; + this.lastTick = phase; + const cycle = this.now(); + if (phase < t) { // avoid querying haps that are in the past anyway console.log(`skip query: too late`); diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 13c138dcc..25859278d 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -281,8 +281,10 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { } // destructure let { + am, amdepth = 1, - amshape = 'tri', + amskew = 0.5, + amphase = 0, s = 'triangle', bank, source, @@ -324,7 +326,7 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { phasercenter, // coarse, - am, + crush, shape, @@ -479,6 +481,8 @@ export const superdough = async (value, t, hapDuration, cps, cycle) => { getWorklet(ac, 'am-processor', { speed: am, depth: amdepth, + skew: amskew, + phaseoffset: amphase, // shape: amshape, cps, diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 107e9bf7f..a3a5eaf5c 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -1,7 +1,7 @@ // coarse, crush, and shape processors adapted from dktr0's webdirt: https://github.com/dktr0/WebDirt/blob/5ce3d698362c54d6e1b68acc47eb2955ac62c793/dist/AudioWorklets.js // LICENSE GNU General Public License v3.0 see https://github.com/dktr0/WebDirt/blob/main/LICENSE - -const clamp = (num, min, max) => Math.min(Math.max(num, min), max); +import { clamp, _mod } from './util.mjs'; +// const clamp = (num, min, max) => Math.min(Math.max(num, min), max); const blockSize = 128; // adjust waveshape to remove frequencies above nyquist to prevent aliasing // referenced from https://www.kvraudio.com/forum/viewtopic.php?t=375517 @@ -55,16 +55,17 @@ const waveshapes = { return 1 - phase; }, tri(phase, skew = 0.5) { + const x = 1 - skew; if (phase >= skew) { - return 1 - ((phase / (1 - skew)) % 1); + return 1 / x - phase / x; } - return (phase / skew) % 1; + return phase / skew; }, square(phase, skew = 0.5) { if (phase >= skew) { - return 1; + return 0; } - return 0; + return 1; }, }; @@ -74,8 +75,9 @@ class AMProcessor extends AudioWorkletProcessor { { name: 'cps', defaultValue: 0.5 }, { name: 'speed', defaultValue: 0.5 }, { name: 'cycle', defaultValue: 0 }, - // { name: 'shape', defaultValue: 'tri' }, + { name: 'skew', defaultValue: 0.5 }, { name: 'depth', defaultValue: 1 }, + { name: 'phaseoffset', defaultValue: 0 }, ]; } @@ -104,18 +106,20 @@ class AMProcessor extends AudioWorkletProcessor { const speed = parameters['speed'][0]; const cps = parameters['cps'][0]; const cycle = parameters['cycle'][0]; - const depth = clamp(parameters['depth'][0], 0, 1); + const depth = parameters['depth'][0]; + const skew = parameters['skew'][0]; + const phaseoffset = parameters['phaseoffset'][0]; const blockSize = 128; const frequency = speed * cps; if (this.phase == null) { const secondsPassed = cycle / cps; - this.phase = Math.max(0, (secondsPassed * frequency) % 1); + this.phase = _mod(secondsPassed * frequency + phaseoffset, 1); } const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { - const modval = waveshapes.tri(this.phase, 0.99) * depth + (1 - depth); + const modval = clamp(waveshapes.tri(this.phase, skew) * depth + (1 - depth), 0, 1); output[i][n] = input[i][n] * modval; } From 6a33032da75daac665357ad460f3066e7a1b92a9 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Thu, 9 May 2024 23:26:04 -0400 Subject: [PATCH 08/17] formatting --- packages/core/controls.mjs | 2 +- packages/superdough/worklets.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 46523588a..b8cdb6ed7 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -471,7 +471,7 @@ export const { amskew } = registerControl('amskew'); * note("{f a c e}%16").am(4).amphase("<0 .25 .66>") * */ -export const { amphase } = registerControl('amskew'); +export const { amphase } = registerControl('amphase'); /** * shape of amplitude modulation diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index a3a5eaf5c..3fa89ca63 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -109,7 +109,7 @@ class AMProcessor extends AudioWorkletProcessor { const depth = parameters['depth'][0]; const skew = parameters['skew'][0]; const phaseoffset = parameters['phaseoffset'][0]; - const blockSize = 128; + const frequency = speed * cps; if (this.phase == null) { const secondsPassed = cycle / cps; From cba96049a3de9eb1baa9982bf5873831870b4595 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Fri, 10 May 2024 00:03:52 -0400 Subject: [PATCH 09/17] fixed duplicate variable --- packages/superdough/worklets.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 99eca92b5..3fa89ca63 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -130,7 +130,6 @@ class AMProcessor extends AudioWorkletProcessor { } registerProcessor('am-processor', AMProcessor); -const blockSize = 128; class CoarseProcessor extends AudioWorkletProcessor { static get parameterDescriptors() { return [{ name: 'coarse', defaultValue: 1 }]; From 19df19375d146f037107358d5f5222f0217d6747 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 10:33:18 -0400 Subject: [PATCH 10/17] kinda fixed it, need to find cause of incorrect cps calculation --- packages/core/cyclist.mjs | 17 +++++++++-------- packages/core/neocyclist.mjs | 3 --- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 41e31ce5c..046b6ff7d 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -8,7 +8,7 @@ import createClock from './zyklus.mjs'; import { logger } from './logger.mjs'; export class Cyclist { - constructor({ interval, onTrigger, onToggle, onError, getTime, latency = 0.1, setInterval, clearInterval }) { + constructor({ interval = 0.05, onTrigger, onToggle, onError, getTime, latency = 0.1, setInterval, clearInterval }) { this.started = false; this.cps = 0.5; this.num_ticks_since_cps_change = 0; @@ -35,14 +35,11 @@ export class Cyclist { try { const begin = this.lastEnd; - this.lastBegin = begin; const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; - - this.lastEnd = end; - - this.lastTick = phase; - - const cycle = this.now(); + let cycle = this.now(); + //magic number that fixes cycle calcuation, cycle is probably not being calculated correctly + const modifier = this.cps * -interval; + cycle = cycle + modifier; if (phase < t) { // avoid querying haps that are in the past anyway @@ -60,10 +57,14 @@ export class Cyclist { const duration = hap.duration / this.cps; // the following line is dumb and only here for backwards compatibility // see https://github.com/tidalcycles/strudel/pull/1004 + const deadline = targetTime - phase; onTrigger?.(hap, deadline, duration, this.cps, targetTime, cycle); } }); + this.lastBegin = begin; + this.lastEnd = end; + this.lastTick = phase; } catch (e) { logger(`[cyclist] error: ${e.message}`); onError?.(e); diff --git a/packages/core/neocyclist.mjs b/packages/core/neocyclist.mjs index 8f9b269e0..6a92050c5 100644 --- a/packages/core/neocyclist.mjs +++ b/packages/core/neocyclist.mjs @@ -10,11 +10,8 @@ export class NeoCyclist { constructor({ onTrigger, onToggle, getTime }) { this.started = false; this.cps = 0.5; - this.lastTick = 0; // absolute time when last tick (clock callback) happened this.getTime = getTime; // get absolute time this.time_at_last_tick_message = 0; - - this.num_cycles_at_cps_change = 0; this.onToggle = onToggle; this.latency = 0.1; // fixed trigger time offset this.cycle = 0; From e024ae2142797ea8a31aa4dea229003514640b5b Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 20:42:59 -0400 Subject: [PATCH 11/17] almost figured it out --- packages/core/cyclist.mjs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 046b6ff7d..75f92090c 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -36,11 +36,13 @@ export class Cyclist { const begin = this.lastEnd; const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; - let cycle = this.now(); + let cycle2 = this.now(); //magic number that fixes cycle calcuation, cycle is probably not being calculated correctly const modifier = this.cps * -interval; - cycle = cycle + modifier; - + cycle2 = cycle2 + modifier; + // let cycle = begin + (phase - t) * this.cps; + // cycle = begin t + // console.log({ phase, t, cycle2 }); if (phase < t) { // avoid querying haps that are in the past anyway console.log(`skip query: too late`); @@ -49,7 +51,9 @@ export class Cyclist { // query the pattern for events const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); - + let mod2 = t - phase - this.latency; + mod2 = mod2 * this.cps; + console.log(this.clock.duration); haps.forEach((hap) => { if (hap.hasOnset()) { const targetTime = @@ -59,9 +63,14 @@ export class Cyclist { // see https://github.com/tidalcycles/strudel/pull/1004 const deadline = targetTime - phase; + // console.log(); + let cycle = hap.whole.begin.valueOf() + mod2; + + // console.log({ cycle, cycle2, mod2 }); onTrigger?.(hap, deadline, duration, this.cps, targetTime, cycle); } }); + this.lastBegin = begin; this.lastEnd = end; this.lastTick = phase; From c6c5a3035c90f4a19e5cbd747bae5ccbff77806c Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 21:06:24 -0400 Subject: [PATCH 12/17] fixed non synced clock --- packages/core/controls.mjs | 4 ++-- packages/core/cyclist.mjs | 23 +++++------------------ 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index b8cdb6ed7..06e808be4 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -434,12 +434,13 @@ export const { coarse } = registerControl('coarse'); * modulate the amplitude of a sound with a continuous waveform * * @name am + * @synonyms tremelo * @param {number | Pattern} speed modulation speed in cycles * @example * s("triangle").am("2").amshape("").amdepth(.5) * */ -export const { am } = registerControl(['am', 'amdepth', 'amskew', 'amphase']); +export const { am, tremolo } = registerControl(['am', 'amdepth', 'amskew', 'amphase'], 'tremolo'); /** * depth of amplitude modulation @@ -1582,7 +1583,6 @@ export const { zmod } = registerControl('zmod'); // like crush but scaled differently export const { zcrush } = registerControl('zcrush'); export const { zdelay } = registerControl('zdelay'); -export const { tremolo } = registerControl('tremolo'); export const { zzfx } = registerControl('zzfx'); /** diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 75f92090c..5e2ee378a 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -34,15 +34,7 @@ export class Cyclist { try { const begin = this.lastEnd; - const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; - let cycle2 = this.now(); - //magic number that fixes cycle calcuation, cycle is probably not being calculated correctly - const modifier = this.cps * -interval; - cycle2 = cycle2 + modifier; - // let cycle = begin + (phase - t) * this.cps; - // cycle = begin t - // console.log({ phase, t, cycle2 }); if (phase < t) { // avoid querying haps that are in the past anyway console.log(`skip query: too late`); @@ -51,23 +43,18 @@ export class Cyclist { // query the pattern for events const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); - let mod2 = t - phase - this.latency; - mod2 = mod2 * this.cps; - console.log(this.clock.duration); + const cycle_gap = (t - phase - this.latency) * this.cps; + haps.forEach((hap) => { if (hap.hasOnset()) { const targetTime = (hap.whole.begin - this.num_cycles_at_cps_change) / this.cps + this.seconds_at_cps_change + latency; const duration = hap.duration / this.cps; + const cycle = hap.whole.begin.valueOf() + cycle_gap; // the following line is dumb and only here for backwards compatibility // see https://github.com/tidalcycles/strudel/pull/1004 - - const deadline = targetTime - phase; - // console.log(); - let cycle = hap.whole.begin.valueOf() + mod2; - - // console.log({ cycle, cycle2, mod2 }); - onTrigger?.(hap, deadline, duration, this.cps, targetTime, cycle); + const _deadline = targetTime - phase; + onTrigger?.(hap, _deadline, duration, this.cps, targetTime, cycle); } }); From 4ba9d47bfffe26246c2836e816bfaf6006f9060a Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 21:10:43 -0400 Subject: [PATCH 13/17] fix test --- packages/superdough/worklets.mjs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/superdough/worklets.mjs b/packages/superdough/worklets.mjs index 3fa89ca63..81a9c9086 100644 --- a/packages/superdough/worklets.mjs +++ b/packages/superdough/worklets.mjs @@ -115,12 +115,11 @@ class AMProcessor extends AudioWorkletProcessor { const secondsPassed = cycle / cps; this.phase = _mod(secondsPassed * frequency + phaseoffset, 1); } - + // eslint-disable-next-line no-undef const dt = frequency / sampleRate; for (let n = 0; n < blockSize; n++) { for (let i = 0; i < input.length; i++) { const modval = clamp(waveshapes.tri(this.phase, skew) * depth + (1 - depth), 0, 1); - output[i][n] = input[i][n] * modval; } this.incrementPhase(dt); From d5ab6b7211201ac0437ec1d9ace20a48de9d3015 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 22:38:20 -0400 Subject: [PATCH 14/17] fix tests --- test/__snapshots__/examples.test.mjs.snap | 375 +++++++++++++--------- 1 file changed, 225 insertions(+), 150 deletions(-) diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 30db05a20..4c98231e5 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -779,6 +779,24 @@ exports[`runs examples > example "always" example index 0 1`] = ` ] `; +exports[`runs examples > example "am" example index 0 1`] = ` +[ + "[ 0/1 → 1/1 | s:triangle am:2 amshape:tri amdepth:0.5 ]", + "[ 1/1 → 2/1 | s:triangle am:2 amshape:saw amdepth:0.5 ]", + "[ 2/1 → 3/1 | s:triangle am:2 amshape:ramp amdepth:0.5 ]", + "[ 3/1 → 4/1 | s:triangle am:2 amshape:square amdepth:0.5 ]", +] +`; + +exports[`runs examples > example "amdepth" example index 0 1`] = ` +[ + "[ 0/1 → 1/1 | s:triangle am:1 amdepth:1 ]", + "[ 1/1 → 2/1 | s:triangle am:1 amdepth:1 ]", + "[ 2/1 → 3/1 | s:triangle am:1 amdepth:1 ]", + "[ 3/1 → 4/1 | s:triangle am:1 amdepth:1 ]", +] +`; + exports[`runs examples > example "amp" example index 0 1`] = ` [ "[ (0/1 → 1/12) ⇝ 1/8 | s:bd amp:0.1 ]", @@ -840,6 +858,213 @@ exports[`runs examples > example "amp" example index 0 1`] = ` ] `; +exports[`runs examples > example "amphase" example index 0 1`] = ` +[ + "[ 0/1 → 1/16 | note:f am:4 amphase:0 ]", + "[ 1/16 → 1/8 | note:a am:4 amphase:0 ]", + "[ 1/8 → 3/16 | note:c am:4 amphase:0 ]", + "[ 3/16 → 1/4 | note:e am:4 amphase:0 ]", + "[ 1/4 → 5/16 | note:f am:4 amphase:0 ]", + "[ 5/16 → 3/8 | note:a am:4 amphase:0 ]", + "[ 3/8 → 7/16 | note:c am:4 amphase:0 ]", + "[ 7/16 → 1/2 | note:e am:4 amphase:0 ]", + "[ 1/2 → 9/16 | note:f am:4 amphase:0 ]", + "[ 9/16 → 5/8 | note:a am:4 amphase:0 ]", + "[ 5/8 → 11/16 | note:c am:4 amphase:0 ]", + "[ 11/16 → 3/4 | note:e am:4 amphase:0 ]", + "[ 3/4 → 13/16 | note:f am:4 amphase:0 ]", + "[ 13/16 → 7/8 | note:a am:4 amphase:0 ]", + "[ 7/8 → 15/16 | note:c am:4 amphase:0 ]", + "[ 15/16 → 1/1 | note:e am:4 amphase:0 ]", + "[ 1/1 → 17/16 | note:f am:4 amphase:0.25 ]", + "[ 17/16 → 9/8 | note:a am:4 amphase:0.25 ]", + "[ 9/8 → 19/16 | note:c am:4 amphase:0.25 ]", + "[ 19/16 → 5/4 | note:e am:4 amphase:0.25 ]", + "[ 5/4 → 21/16 | note:f am:4 amphase:0.25 ]", + "[ 21/16 → 11/8 | note:a am:4 amphase:0.25 ]", + "[ 11/8 → 23/16 | note:c am:4 amphase:0.25 ]", + "[ 23/16 → 3/2 | note:e am:4 amphase:0.25 ]", + "[ 3/2 → 25/16 | note:f am:4 amphase:0.25 ]", + "[ 25/16 → 13/8 | note:a am:4 amphase:0.25 ]", + "[ 13/8 → 27/16 | note:c am:4 amphase:0.25 ]", + "[ 27/16 → 7/4 | note:e am:4 amphase:0.25 ]", + "[ 7/4 → 29/16 | note:f am:4 amphase:0.25 ]", + "[ 29/16 → 15/8 | note:a am:4 amphase:0.25 ]", + "[ 15/8 → 31/16 | note:c am:4 amphase:0.25 ]", + "[ 31/16 → 2/1 | note:e am:4 amphase:0.25 ]", + "[ 2/1 → 33/16 | note:f am:4 amphase:0.66 ]", + "[ 33/16 → 17/8 | note:a am:4 amphase:0.66 ]", + "[ 17/8 → 35/16 | note:c am:4 amphase:0.66 ]", + "[ 35/16 → 9/4 | note:e am:4 amphase:0.66 ]", + "[ 9/4 → 37/16 | note:f am:4 amphase:0.66 ]", + "[ 37/16 → 19/8 | note:a am:4 amphase:0.66 ]", + "[ 19/8 → 39/16 | note:c am:4 amphase:0.66 ]", + "[ 39/16 → 5/2 | note:e am:4 amphase:0.66 ]", + "[ 5/2 → 41/16 | note:f am:4 amphase:0.66 ]", + "[ 41/16 → 21/8 | note:a am:4 amphase:0.66 ]", + "[ 21/8 → 43/16 | note:c am:4 amphase:0.66 ]", + "[ 43/16 → 11/4 | note:e am:4 amphase:0.66 ]", + "[ 11/4 → 45/16 | note:f am:4 amphase:0.66 ]", + "[ 45/16 → 23/8 | note:a am:4 amphase:0.66 ]", + "[ 23/8 → 47/16 | note:c am:4 amphase:0.66 ]", + "[ 47/16 → 3/1 | note:e am:4 amphase:0.66 ]", + "[ 3/1 → 49/16 | note:f am:4 amphase:0 ]", + "[ 49/16 → 25/8 | note:a am:4 amphase:0 ]", + "[ 25/8 → 51/16 | note:c am:4 amphase:0 ]", + "[ 51/16 → 13/4 | note:e am:4 amphase:0 ]", + "[ 13/4 → 53/16 | note:f am:4 amphase:0 ]", + "[ 53/16 → 27/8 | note:a am:4 amphase:0 ]", + "[ 27/8 → 55/16 | note:c am:4 amphase:0 ]", + "[ 55/16 → 7/2 | note:e am:4 amphase:0 ]", + "[ 7/2 → 57/16 | note:f am:4 amphase:0 ]", + "[ 57/16 → 29/8 | note:a am:4 amphase:0 ]", + "[ 29/8 → 59/16 | note:c am:4 amphase:0 ]", + "[ 59/16 → 15/4 | note:e am:4 amphase:0 ]", + "[ 15/4 → 61/16 | note:f am:4 amphase:0 ]", + "[ 61/16 → 31/8 | note:a am:4 amphase:0 ]", + "[ 31/8 → 63/16 | note:c am:4 amphase:0 ]", + "[ 63/16 → 4/1 | note:e am:4 amphase:0 ]", +] +`; + +exports[`runs examples > example "amshape" example index 0 1`] = ` +[ + "[ 0/1 → 1/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 1/16 → 1/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 1/8 → 3/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 3/16 → 1/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 1/4 → 5/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 5/16 → 3/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 3/8 → 7/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 7/16 → 1/2 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 1/2 → 9/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 9/16 → 5/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 5/8 → 11/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 11/16 → 3/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 3/4 → 13/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 13/16 → 7/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 7/8 → 15/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 15/16 → 1/1 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 1/1 → 17/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 17/16 → 9/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 9/8 → 19/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 19/16 → 5/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 5/4 → 21/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 21/16 → 11/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 11/8 → 23/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 23/16 → 3/2 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 3/2 → 25/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 25/16 → 13/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 13/8 → 27/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 27/16 → 7/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 7/4 → 29/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 29/16 → 15/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 15/8 → 31/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 31/16 → 2/1 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 2/1 → 33/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 33/16 → 17/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 17/8 → 35/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 35/16 → 9/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 9/4 → 37/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 37/16 → 19/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 19/8 → 39/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 39/16 → 5/2 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 5/2 → 41/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 41/16 → 21/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 21/8 → 43/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 43/16 → 11/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 11/4 → 45/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 45/16 → 23/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 23/8 → 47/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 47/16 → 3/1 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 3/1 → 49/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 49/16 → 25/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 25/8 → 51/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 51/16 → 13/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 13/4 → 53/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 53/16 → 27/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 27/8 → 55/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 55/16 → 7/2 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 7/2 → 57/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 57/16 → 29/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 29/8 → 59/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 59/16 → 15/4 | note:d am:4 amshape:ramp s:sawtooth ]", + "[ 15/4 → 61/16 | note:f am:4 amshape:ramp s:sawtooth ]", + "[ 61/16 → 31/8 | note:g am:4 amshape:ramp s:sawtooth ]", + "[ 31/8 → 63/16 | note:c am:4 amshape:ramp s:sawtooth ]", + "[ 63/16 → 4/1 | note:d am:4 amshape:ramp s:sawtooth ]", +] +`; + +exports[`runs examples > example "amskew" example index 0 1`] = ` +[ + "[ 0/1 → 1/16 | note:f am:4 amskew:0.5 ]", + "[ 1/16 → 1/8 | note:a am:4 amskew:0.5 ]", + "[ 1/8 → 3/16 | note:c am:4 amskew:0.5 ]", + "[ 3/16 → 1/4 | note:e am:4 amskew:0.5 ]", + "[ 1/4 → 5/16 | note:f am:4 amskew:0.5 ]", + "[ 5/16 → 3/8 | note:a am:4 amskew:0.5 ]", + "[ 3/8 → 7/16 | note:c am:4 amskew:0.5 ]", + "[ 7/16 → 1/2 | note:e am:4 amskew:0.5 ]", + "[ 1/2 → 9/16 | note:f am:4 amskew:0.5 ]", + "[ 9/16 → 5/8 | note:a am:4 amskew:0.5 ]", + "[ 5/8 → 11/16 | note:c am:4 amskew:0.5 ]", + "[ 11/16 → 3/4 | note:e am:4 amskew:0.5 ]", + "[ 3/4 → 13/16 | note:f am:4 amskew:0.5 ]", + "[ 13/16 → 7/8 | note:a am:4 amskew:0.5 ]", + "[ 7/8 → 15/16 | note:c am:4 amskew:0.5 ]", + "[ 15/16 → 1/1 | note:e am:4 amskew:0.5 ]", + "[ 1/1 → 17/16 | note:f am:4 amskew:0 ]", + "[ 17/16 → 9/8 | note:a am:4 amskew:0 ]", + "[ 9/8 → 19/16 | note:c am:4 amskew:0 ]", + "[ 19/16 → 5/4 | note:e am:4 amskew:0 ]", + "[ 5/4 → 21/16 | note:f am:4 amskew:0 ]", + "[ 21/16 → 11/8 | note:a am:4 amskew:0 ]", + "[ 11/8 → 23/16 | note:c am:4 amskew:0 ]", + "[ 23/16 → 3/2 | note:e am:4 amskew:0 ]", + "[ 3/2 → 25/16 | note:f am:4 amskew:0 ]", + "[ 25/16 → 13/8 | note:a am:4 amskew:0 ]", + "[ 13/8 → 27/16 | note:c am:4 amskew:0 ]", + "[ 27/16 → 7/4 | note:e am:4 amskew:0 ]", + "[ 7/4 → 29/16 | note:f am:4 amskew:0 ]", + "[ 29/16 → 15/8 | note:a am:4 amskew:0 ]", + "[ 15/8 → 31/16 | note:c am:4 amskew:0 ]", + "[ 31/16 → 2/1 | note:e am:4 amskew:0 ]", + "[ 2/1 → 33/16 | note:f am:4 amskew:1 ]", + "[ 33/16 → 17/8 | note:a am:4 amskew:1 ]", + "[ 17/8 → 35/16 | note:c am:4 amskew:1 ]", + "[ 35/16 → 9/4 | note:e am:4 amskew:1 ]", + "[ 9/4 → 37/16 | note:f am:4 amskew:1 ]", + "[ 37/16 → 19/8 | note:a am:4 amskew:1 ]", + "[ 19/8 → 39/16 | note:c am:4 amskew:1 ]", + "[ 39/16 → 5/2 | note:e am:4 amskew:1 ]", + "[ 5/2 → 41/16 | note:f am:4 amskew:1 ]", + "[ 41/16 → 21/8 | note:a am:4 amskew:1 ]", + "[ 21/8 → 43/16 | note:c am:4 amskew:1 ]", + "[ 43/16 → 11/4 | note:e am:4 amskew:1 ]", + "[ 11/4 → 45/16 | note:f am:4 amskew:1 ]", + "[ 45/16 → 23/8 | note:a am:4 amskew:1 ]", + "[ 23/8 → 47/16 | note:c am:4 amskew:1 ]", + "[ 47/16 → 3/1 | note:e am:4 amskew:1 ]", + "[ 3/1 → 49/16 | note:f am:4 amskew:0.5 ]", + "[ 49/16 → 25/8 | note:a am:4 amskew:0.5 ]", + "[ 25/8 → 51/16 | note:c am:4 amskew:0.5 ]", + "[ 51/16 → 13/4 | note:e am:4 amskew:0.5 ]", + "[ 13/4 → 53/16 | note:f am:4 amskew:0.5 ]", + "[ 53/16 → 27/8 | note:a am:4 amskew:0.5 ]", + "[ 27/8 → 55/16 | note:c am:4 amskew:0.5 ]", + "[ 55/16 → 7/2 | note:e am:4 amskew:0.5 ]", + "[ 7/2 → 57/16 | note:f am:4 amskew:0.5 ]", + "[ 57/16 → 29/8 | note:a am:4 amskew:0.5 ]", + "[ 29/8 → 59/16 | note:c am:4 amskew:0.5 ]", + "[ 59/16 → 15/4 | note:e am:4 amskew:0.5 ]", + "[ 15/4 → 61/16 | note:f am:4 amskew:0.5 ]", + "[ 61/16 → 31/8 | note:a am:4 amskew:0.5 ]", + "[ 31/8 → 63/16 | note:c am:4 amskew:0.5 ]", + "[ 63/16 → 4/1 | note:e am:4 amskew:0.5 ]", +] +`; + exports[`runs examples > example "apply" example index 0 1`] = ` [ "[ 0/1 → 1/1 | note:C3 ]", @@ -5035,72 +5260,6 @@ exports[`runs examples > example "ply" example index 0 1`] = ` ] `; -exports[`runs examples > example "polymeter" example index 0 1`] = ` -[ - "[ 0/1 → 1/3 | c ]", - "[ 0/1 → 1/3 | c2 ]", - "[ 1/3 → 2/3 | eb ]", - "[ 1/3 → 2/3 | g2 ]", - "[ 2/3 → 1/1 | g ]", - "[ 2/3 → 1/1 | c2 ]", - "[ 1/1 → 4/3 | c ]", - "[ 1/1 → 4/3 | g2 ]", - "[ 4/3 → 5/3 | eb ]", - "[ 4/3 → 5/3 | c2 ]", - "[ 5/3 → 2/1 | g ]", - "[ 5/3 → 2/1 | g2 ]", - "[ 2/1 → 7/3 | c ]", - "[ 2/1 → 7/3 | c2 ]", - "[ 7/3 → 8/3 | eb ]", - "[ 7/3 → 8/3 | g2 ]", - "[ 8/3 → 3/1 | g ]", - "[ 8/3 → 3/1 | c2 ]", - "[ 3/1 → 10/3 | c ]", - "[ 3/1 → 10/3 | g2 ]", - "[ 10/3 → 11/3 | eb ]", - "[ 10/3 → 11/3 | c2 ]", - "[ 11/3 → 4/1 | g ]", - "[ 11/3 → 4/1 | g2 ]", -] -`; - -exports[`runs examples > example "polymeterSteps" example index 0 1`] = ` -[ - "[ 0/1 → 1/4 | c ]", - "[ 0/1 → 1/4 | e ]", - "[ 1/4 → 1/2 | d ]", - "[ 1/4 → 1/2 | f ]", - "[ 1/2 → 3/4 | c ]", - "[ 1/2 → 3/4 | g ]", - "[ 3/4 → 1/1 | d ]", - "[ 3/4 → 1/1 | e ]", - "[ 1/1 → 5/4 | c ]", - "[ 1/1 → 5/4 | f ]", - "[ 5/4 → 3/2 | d ]", - "[ 5/4 → 3/2 | g ]", - "[ 3/2 → 7/4 | c ]", - "[ 3/2 → 7/4 | e ]", - "[ 7/4 → 2/1 | d ]", - "[ 7/4 → 2/1 | f ]", - "[ 2/1 → 9/4 | c ]", - "[ 2/1 → 9/4 | g ]", - "[ 9/4 → 5/2 | d ]", - "[ 9/4 → 5/2 | e ]", - "[ 5/2 → 11/4 | c ]", - "[ 5/2 → 11/4 | f ]", - "[ 11/4 → 3/1 | d ]", - "[ 11/4 → 3/1 | g ]", - "[ 3/1 → 13/4 | c ]", - "[ 3/1 → 13/4 | e ]", - "[ 13/4 → 7/2 | d ]", - "[ 13/4 → 7/2 | f ]", - "[ 7/2 → 15/4 | c ]", - "[ 7/2 → 15/4 | g ]", - "[ 15/4 → 4/1 | d ]", - "[ 15/4 → 4/1 | e ]", -] -`; - exports[`runs examples > example "postgain" example index 0 1`] = ` [ "[ 0/1 → 1/8 | s:hh compressor:-20 compressorRatio:20 compressorKnee:10 compressorAttack:0.002 compressorRelease:0.02 postgain:1.5 ]", @@ -7216,31 +7375,6 @@ exports[`runs examples > example "stack" example index 0 2`] = ` ] `; -exports[`runs examples > example "stepcat" example index 0 1`] = ` -[ - "[ 0/1 → 1/5 | s:bd ]", - "[ 1/5 → 2/5 | s:cp ]", - "[ 2/5 → 3/5 | s:bd ]", - "[ 3/5 → 4/5 | s:mt ]", - "[ 4/5 → 1/1 | s:bd ]", - "[ 1/1 → 6/5 | s:bd ]", - "[ 6/5 → 7/5 | s:cp ]", - "[ 7/5 → 8/5 | s:bd ]", - "[ 8/5 → 9/5 | s:mt ]", - "[ 9/5 → 2/1 | s:bd ]", - "[ 2/1 → 11/5 | s:bd ]", - "[ 11/5 → 12/5 | s:cp ]", - "[ 12/5 → 13/5 | s:bd ]", - "[ 13/5 → 14/5 | s:mt ]", - "[ 14/5 → 3/1 | s:bd ]", - "[ 3/1 → 16/5 | s:bd ]", - "[ 16/5 → 17/5 | s:cp ]", - "[ 17/5 → 18/5 | s:bd ]", - "[ 18/5 → 19/5 | s:mt ]", - "[ 19/5 → 4/1 | s:bd ]", -] -`; - exports[`runs examples > example "steps" example index 0 1`] = ` [ "[ 0/1 → 1/4 | s:bd ]", @@ -7554,65 +7688,6 @@ exports[`runs examples > example "swingBy" example index 0 1`] = ` ] `; -exports[`runs examples > example "timecat" example index 0 1`] = ` -[ - "[ 0/1 → 3/4 | note:e3 ]", - "[ 3/4 → 1/1 | note:g3 ]", - "[ 1/1 → 7/4 | note:e3 ]", - "[ 7/4 → 2/1 | note:g3 ]", - "[ 2/1 → 11/4 | note:e3 ]", - "[ 11/4 → 3/1 | note:g3 ]", - "[ 3/1 → 15/4 | note:e3 ]", - "[ 15/4 → 4/1 | note:g3 ]", -] -`; - -exports[`runs examples > example "timecat" example index 1 1`] = ` -[ - "[ 0/1 → 1/5 | s:bd ]", - "[ 1/5 → 2/5 | s:sd ]", - "[ 2/5 → 3/5 | s:cp ]", - "[ 3/5 → 4/5 | s:hh ]", - "[ 4/5 → 1/1 | s:hh ]", - "[ 1/1 → 6/5 | s:bd ]", - "[ 6/5 → 7/5 | s:sd ]", - "[ 7/5 → 8/5 | s:cp ]", - "[ 8/5 → 9/5 | s:hh ]", - "[ 9/5 → 2/1 | s:hh ]", - "[ 2/1 → 11/5 | s:bd ]", - "[ 11/5 → 12/5 | s:sd ]", - "[ 12/5 → 13/5 | s:cp ]", - "[ 13/5 → 14/5 | s:hh ]", - "[ 14/5 → 3/1 | s:hh ]", - "[ 3/1 → 16/5 | s:bd ]", - "[ 16/5 → 17/5 | s:sd ]", - "[ 17/5 → 18/5 | s:cp ]", - "[ 18/5 → 19/5 | s:hh ]", - "[ 19/5 → 4/1 | s:hh ]", -] -`; - -exports[`runs examples > example "toTactus" example index 0 1`] = ` -[ - "[ 0/1 → 1/4 | s:bd ]", - "[ 1/4 → 1/2 | s:sd ]", - "[ 1/2 → 3/4 | s:cp ]", - "[ 3/4 → 1/1 | s:bd ]", - "[ 1/1 → 5/4 | s:sd ]", - "[ 5/4 → 3/2 | s:cp ]", - "[ 3/2 → 7/4 | s:bd ]", - "[ 7/4 → 2/1 | s:sd ]", - "[ 2/1 → 9/4 | s:cp ]", - "[ 9/4 → 5/2 | s:bd ]", - "[ 5/2 → 11/4 | s:sd ]", - "[ 11/4 → 3/1 | s:cp ]", - "[ 3/1 → 13/4 | s:bd ]", - "[ 13/4 → 7/2 | s:sd ]", - "[ 7/2 → 15/4 | s:cp ]", - "[ 15/4 → 4/1 | s:bd ]", -] -`; - exports[`runs examples > example "transpose" example index 0 1`] = ` [ "[ 0/1 → 1/4 | note:C2 ]", From 4a181d5b335611833b1b38dbe039ae4f520224d1 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 23:02:36 -0400 Subject: [PATCH 15/17] updated non synced clock to work more like synced clock --- packages/core/cyclist.mjs | 89 ++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 5e2ee378a..893a7619e 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -11,60 +11,53 @@ export class Cyclist { constructor({ interval = 0.05, onTrigger, onToggle, onError, getTime, latency = 0.1, setInterval, clearInterval }) { this.started = false; this.cps = 0.5; - this.num_ticks_since_cps_change = 0; - this.lastTick = 0; // absolute time when last tick (clock callback) happened - this.lastBegin = 0; // query begin of last tick - this.lastEnd = 0; // query end of last tick + this.time_at_last_tick_message = 0; + this.cycle = 0; this.getTime = getTime; // get absolute time this.num_cycles_at_cps_change = 0; this.seconds_at_cps_change; // clock phase when cps was changed + this.num_ticks_since_cps_change = 0; this.onToggle = onToggle; this.latency = latency; // fixed trigger time offset + + this.interval = interval; + this.clock = createClock( getTime, // called slightly before each cycle - (phase, duration, _, t) => { - if (this.num_ticks_since_cps_change === 0) { - this.num_cycles_at_cps_change = this.lastEnd; - this.seconds_at_cps_change = phase; + (phase, duration, _, time) => { + const num_seconds_since_cps_change = this.num_ticks_since_cps_change * duration; + const tickdeadline = phase - time; + const lastTick = time + tickdeadline; + const num_cycles_since_cps_change = num_seconds_since_cps_change * this.cps; + const begin = this.num_cycles_at_cps_change + num_cycles_since_cps_change; + const secondsSinceLastTick = time - lastTick - duration; + const eventLength = duration * this.cps; + const end = begin + eventLength; + this.cycle = begin + secondsSinceLastTick * this.cps; + const num_seconds_at_cps_change = this.num_cycles_at_cps_change / this.cps; + const time_dif = time - (num_seconds_at_cps_change + num_seconds_since_cps_change) + tickdeadline; + + if (this.started === false) { + return; } - this.num_ticks_since_cps_change++; - const seconds_since_cps_change = this.num_ticks_since_cps_change * duration; - const num_cycles_since_cps_change = seconds_since_cps_change * this.cps; - try { - const begin = this.lastEnd; - const end = this.num_cycles_at_cps_change + num_cycles_since_cps_change; - if (phase < t) { - // avoid querying haps that are in the past anyway - console.log(`skip query: too late`); - return; - } + const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); - // query the pattern for events - const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); - const cycle_gap = (t - phase - this.latency) * this.cps; + //account for latency and tick duration when using cycle calculations for audio downstream + const cycle_gap = (this.latency - duration) * this.cps; - haps.forEach((hap) => { - if (hap.hasOnset()) { - const targetTime = - (hap.whole.begin - this.num_cycles_at_cps_change) / this.cps + this.seconds_at_cps_change + latency; - const duration = hap.duration / this.cps; - const cycle = hap.whole.begin.valueOf() + cycle_gap; - // the following line is dumb and only here for backwards compatibility - // see https://github.com/tidalcycles/strudel/pull/1004 - const _deadline = targetTime - phase; - onTrigger?.(hap, _deadline, duration, this.cps, targetTime, cycle); - } - }); + haps.forEach((hap) => { + if (hap.hasOnset()) { + let targetTime = (hap.whole.begin - this.num_cycles_at_cps_change) / this.cps; + targetTime = targetTime + num_seconds_at_cps_change + this.latency + time_dif; + const duration = hap.duration / this.cps; + onTrigger?.(hap, tickdeadline, duration, this.cps, targetTime, this.cycle - cycle_gap); + } + }); - this.lastBegin = begin; - this.lastEnd = end; - this.lastTick = phase; - } catch (e) { - logger(`[cyclist] error: ${e.message}`); - onError?.(e); - } + this.time_at_last_tick_message = this.getTime(); + this.num_ticks_since_cps_change++; }, interval, // duration of each cycle 0.1, @@ -77,16 +70,24 @@ export class Cyclist { if (!this.started) { return 0; } - const secondsSinceLastTick = this.getTime() - this.lastTick - this.clock.duration; - return this.lastBegin + secondsSinceLastTick * this.cps; // + this.clock.minLatency; + const gap = (this.getTime() - this.time_at_last_tick_message) * this.cps; + return this.cycle + gap; } + + setCycle = (cycle) => { + this.num_ticks_since_cps_change = 0; + this.num_cycles_at_cps_change = cycle; + }; setStarted(v) { this.started = v; + + this.setCycle(0); this.onToggle?.(v); } start() { this.num_ticks_since_cps_change = 0; this.num_cycles_at_cps_change = 0; + if (!this.pattern) { throw new Error('Scheduler: no pattern set! call .setPattern first.'); } @@ -115,6 +116,8 @@ export class Cyclist { if (this.cps === cps) { return; } + const num_seconds_since_cps_change = this.num_ticks_since_cps_change * this.interval; + this.num_cycles_at_cps_change = this.num_cycles_at_cps_change + num_seconds_since_cps_change * cps; this.cps = cps; this.num_ticks_since_cps_change = 0; } From 8ad48e1633ff5c26e1d29b7257c96f855f4764f0 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 23:02:48 -0400 Subject: [PATCH 16/17] remove unesseccary time call --- packages/core/cyclist.mjs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 893a7619e..55dcb35b6 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -26,6 +26,9 @@ export class Cyclist { getTime, // called slightly before each cycle (phase, duration, _, time) => { + if (this.started === false) { + return; + } const num_seconds_since_cps_change = this.num_ticks_since_cps_change * duration; const tickdeadline = phase - time; const lastTick = time + tickdeadline; @@ -38,14 +41,9 @@ export class Cyclist { const num_seconds_at_cps_change = this.num_cycles_at_cps_change / this.cps; const time_dif = time - (num_seconds_at_cps_change + num_seconds_since_cps_change) + tickdeadline; - if (this.started === false) { - return; - } - - const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); - //account for latency and tick duration when using cycle calculations for audio downstream const cycle_gap = (this.latency - duration) * this.cps; + const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); haps.forEach((hap) => { if (hap.hasOnset()) { @@ -55,8 +53,7 @@ export class Cyclist { onTrigger?.(hap, tickdeadline, duration, this.cps, targetTime, this.cycle - cycle_gap); } }); - - this.time_at_last_tick_message = this.getTime(); + this.time_at_last_tick_message = time; this.num_ticks_since_cps_change++; }, interval, // duration of each cycle From bee8c812548e795b8ce04324b3929ec1afe52b89 Mon Sep 17 00:00:00 2001 From: "Jade (Rose) Rowland" Date: Mon, 13 May 2024 23:14:30 -0400 Subject: [PATCH 17/17] simplify targettime --- packages/core/cyclist.mjs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/core/cyclist.mjs b/packages/core/cyclist.mjs index 55dcb35b6..a170ca3a5 100644 --- a/packages/core/cyclist.mjs +++ b/packages/core/cyclist.mjs @@ -38,17 +38,15 @@ export class Cyclist { const eventLength = duration * this.cps; const end = begin + eventLength; this.cycle = begin + secondsSinceLastTick * this.cps; - const num_seconds_at_cps_change = this.num_cycles_at_cps_change / this.cps; - const time_dif = time - (num_seconds_at_cps_change + num_seconds_since_cps_change) + tickdeadline; //account for latency and tick duration when using cycle calculations for audio downstream const cycle_gap = (this.latency - duration) * this.cps; - const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); + const haps = this.pattern.queryArc(begin, end, { _cps: this.cps }); haps.forEach((hap) => { if (hap.hasOnset()) { let targetTime = (hap.whole.begin - this.num_cycles_at_cps_change) / this.cps; - targetTime = targetTime + num_seconds_at_cps_change + this.latency + time_dif; + targetTime = targetTime + this.latency + tickdeadline + time - num_seconds_since_cps_change; const duration = hap.duration / this.cps; onTrigger?.(hap, tickdeadline, duration, this.cps, targetTime, this.cycle - cycle_gap); }