From c20a6673af5547f1668b6f50cfd8387b1dce9132 Mon Sep 17 00:00:00 2001 From: tyow Date: Sun, 15 Feb 2026 16:58:35 +0100 Subject: [PATCH] Added getDuration, need to fix failing test --- packages/superdough/superdough.mjs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 4ce8164bc..26be6e5c6 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -25,7 +25,7 @@ import { import { map } from 'nanostores'; import { logger } from './logger.mjs'; import { connectLFO, connectEnvelope, connectBusModulator } from './modulators.mjs'; -import { loadBuffer } from './sampler.mjs'; +import { getSampleBufferSource, loadBuffer } from './sampler.mjs'; import { getAudioContext } from './audioContext.mjs'; import { SuperdoughAudioController } from './superdoughoutput.mjs'; import { resetSeenKeys } from './wavetable.mjs'; @@ -85,6 +85,26 @@ export function setGainCurve(newGainCurveFunc) { gainCurveFunc = newGainCurveFunc; } +/** + * Returns the duration, in seconds, of the given sample. + * Has optional param `n` (for instance, the `2` in `s("casio:2")`) + * + * Note: `must` be called with await, otherwise you'll get a pending Promise object. + * + * @name getDuration + * @param {string} sampleName + * @param {number} (optional) n + * + * @example + * // Set a patterns cycle length to exactly the length of the sample + * samples('github:tidalcycles/dirt-samples') + * let k = await getDuration('sax') + * s("sax").cps(1/k) + */ +export const getDuration = (s, n = 0) => { + return getSampleBufferSource({ s, n }, soundMap.get(s)[s].data.samples).then((x) => x.bufferDuration); +}; + function aliasBankMap(aliasMap) { // Make all bank keys lower case for case insensitivity for (const key in aliasMap) {