mirror of
https://codeberg.org/uzu/strudel
synced 2026-09-15 10:16:55 -04:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 401491fdba | |||
| ef13c45496 | |||
| 1a279ef536 | |||
| dc62fea714 | |||
| 940d36436b | |||
| 3a483c0669 | |||
| 0df20ca5bf | |||
| 39b9d2b9b5 | |||
| 91d7a09c06 |
@@ -396,6 +396,18 @@ export const { velocity } = registerControl('velocity');
|
||||
*
|
||||
*/
|
||||
export const { gain } = registerControl('gain');
|
||||
|
||||
/**
|
||||
* Applies a linear gain adjustment
|
||||
*
|
||||
* @name gainlinear
|
||||
* @synonyms gainlin
|
||||
* @param {number | Pattern} amount gainlin
|
||||
* @example
|
||||
* s("hh*8").gainlin(".4!2 1 .4!2 1 .4 1").fast(2)
|
||||
*
|
||||
*/
|
||||
export const { gainlinear, gainlin } = registerControl('gainlinear', 'gainlin');
|
||||
/**
|
||||
* Gain applied after all effects have been processed.
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getCommonSampleInfoFromBank } from './util.mjs';
|
||||
import { getCommonSampleInfo } from './util.mjs';
|
||||
import { registerSound, registerWaveTable } from './index.mjs';
|
||||
import { getAudioContext } from './audioContext.mjs';
|
||||
import { getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs';
|
||||
@@ -23,34 +23,35 @@ function humanFileSize(bytes, si) {
|
||||
return bytes.toFixed(1) + ' ' + units[u];
|
||||
}
|
||||
|
||||
export function getSampleInfo(hapValue, bank) {
|
||||
const { speed = 1.0 } = hapValue;
|
||||
const { transpose, url, index, midi, label } = getCommonSampleInfo(hapValue, bank);
|
||||
let playbackRate = Math.abs(speed) * Math.pow(2, transpose / 12);
|
||||
return { transpose, url, index, midi, label, playbackRate };
|
||||
}
|
||||
|
||||
export const getSampleBuffer = async (label, sampleUrl, resolveUrl) => {
|
||||
// takes hapValue and returns buffer + playbackRate.
|
||||
export const getSampleBuffer = async (hapValue, bank, resolveUrl) => {
|
||||
let { url: sampleUrl, label, playbackRate } = getSampleInfo(hapValue, bank);
|
||||
if (resolveUrl) {
|
||||
sampleUrl = await resolveUrl(sampleUrl);
|
||||
}
|
||||
const ac = getAudioContext();
|
||||
const buffer = await loadBuffer(sampleUrl, ac, label);
|
||||
return buffer
|
||||
};
|
||||
|
||||
function getBufferPlaybackRate(hapValue, buffer, transpose) {
|
||||
const { speed = 1.0 } = hapValue;
|
||||
let playbackRate = Math.abs(speed) * Math.pow(2, transpose / 12);
|
||||
if (hapValue.unit === 'c') {
|
||||
playbackRate = playbackRate * buffer.duration;
|
||||
}
|
||||
return playbackRate;
|
||||
}
|
||||
|
||||
|
||||
return { buffer, playbackRate };
|
||||
};
|
||||
|
||||
// creates playback ready AudioBufferSourceNode from hapValue
|
||||
export function getSampleBufferSource(hapValue, buffer,transpose) {
|
||||
export const getSampleBufferSource = async (hapValue, bank, resolveUrl) => {
|
||||
let { buffer, playbackRate } = await getSampleBuffer(hapValue, bank, resolveUrl);
|
||||
if (hapValue.speed < 0) {
|
||||
// should this be cached?
|
||||
buffer = reverseBuffer(buffer);
|
||||
}
|
||||
const playbackRate = getBufferPlaybackRate(hapValue, buffer, transpose)
|
||||
const ac = getAudioContext();
|
||||
const bufferSource = ac.createBufferSource();
|
||||
bufferSource.buffer = buffer;
|
||||
@@ -263,7 +264,7 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option
|
||||
|
||||
const cutGroups = [];
|
||||
|
||||
export async function onTriggerSample(t, value, onended, bufferSrc) {
|
||||
export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
|
||||
let {
|
||||
s,
|
||||
nudge = 0, // TODO: is this in seconds?
|
||||
@@ -285,7 +286,7 @@ export async function onTriggerSample(t, value, onended, bufferSrc) {
|
||||
// destructure adsr here, because the default should be different for synths and samples
|
||||
let [attack, decay, sustain, release] = getADSRValues([value.attack, value.decay, value.sustain, value.release]);
|
||||
|
||||
const { bufferSource, sliceDuration, offset } = bufferSrc
|
||||
const { bufferSource, sliceDuration, offset } = await getSampleBufferSource(value, bank, resolveUrl);
|
||||
|
||||
// asny stuff above took too long?
|
||||
if (ac.currentTime > t) {
|
||||
@@ -347,21 +348,14 @@ export async function onTriggerSample(t, value, onended, bufferSrc) {
|
||||
return handle;
|
||||
}
|
||||
|
||||
async function getBufferSrcFromBank(hapValue, bank, resolveUrl = undefined) {
|
||||
const { transpose, url, label } = getCommonSampleInfoFromBank(hapValue, bank)
|
||||
let buffer = await getSampleBuffer(label,url, resolveUrl);
|
||||
return getSampleBufferSource(hapValue, buffer, transpose)
|
||||
}
|
||||
|
||||
function registerSample(key, bank, params) {
|
||||
registerSound(key, async (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, await getBufferSrcFromBank(hapValue, bank, undefined)), {
|
||||
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, bank), {
|
||||
type: 'sample',
|
||||
samples: bank,
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function registerSampleSource(key, bank, params) {
|
||||
const isWavetable = key.startsWith('wt_');
|
||||
if (isWavetable) {
|
||||
|
||||
@@ -37,7 +37,7 @@ export function registerSound(key, onTrigger, data = {}) {
|
||||
soundMap.setKey(key, { onTrigger, data });
|
||||
}
|
||||
|
||||
let gainCurveFunc = (val) => val;
|
||||
let gainCurveFunc = (val) => Math.pow(val, 2);
|
||||
|
||||
export function applyGainCurve(val) {
|
||||
return gainCurveFunc(val);
|
||||
@@ -141,9 +141,9 @@ export const getAudioDevices = async () => {
|
||||
return devicesMap;
|
||||
};
|
||||
|
||||
let defaultDefaultValues = {
|
||||
const defaultDefaultValues = {
|
||||
s: 'triangle',
|
||||
gain: 0.8,
|
||||
gain: 1,
|
||||
postgain: 1,
|
||||
density: '.03',
|
||||
ftype: '12db',
|
||||
@@ -166,17 +166,6 @@ let defaultDefaultValues = {
|
||||
fft: 8,
|
||||
};
|
||||
|
||||
const defaultDefaultDefaultValues = Object.freeze({ ...defaultDefaultValues });
|
||||
|
||||
export function setDefault(control, value) {
|
||||
// const main = getControlName(control); // we cant do this because superdough is independent of strudel/core
|
||||
defaultDefaultValues[control] = value;
|
||||
}
|
||||
|
||||
export function resetDefaults() {
|
||||
defaultDefaultValues = { ...defaultDefaultDefaultValues };
|
||||
}
|
||||
|
||||
let defaultControls = new Map(Object.entries(defaultDefaultValues));
|
||||
|
||||
export function setDefaultValue(key, value) {
|
||||
@@ -195,9 +184,17 @@ export function resetDefaultValues() {
|
||||
}
|
||||
export function setVersionDefaults(version) {
|
||||
resetDefaultValues();
|
||||
if (version === '1.0') {
|
||||
const v = Number(version);
|
||||
if (v <= 1.0) {
|
||||
setDefaultValue('fanchor', 0.5);
|
||||
}
|
||||
if (v <= 1.2) {
|
||||
setDefaultValue('gain', 0.8);
|
||||
setGainCurve((v) => v);
|
||||
} else {
|
||||
setDefaultValue('gain', 1);
|
||||
setGainCurve((v) => Math.pow(v, 2));
|
||||
}
|
||||
}
|
||||
|
||||
export const resetLoadedSounds = () => soundMap.set({});
|
||||
@@ -406,6 +403,7 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
||||
bank,
|
||||
source,
|
||||
gain = getDefaultValue('gain'),
|
||||
gainlinear,
|
||||
postgain = getDefaultValue('postgain'),
|
||||
density = getDefaultValue('density'),
|
||||
duckorbit,
|
||||
@@ -503,6 +501,9 @@ export const superdough = async (value, t, hapDuration, cps = 0.5, cycle = 0.5)
|
||||
velocity = applyGainCurve(velocity);
|
||||
tremolodepth = applyGainCurve(tremolodepth);
|
||||
gain *= velocity; // velocity currently only multiplies with gain. it might do other things in the future
|
||||
if (gainlinear != null) {
|
||||
gain *= gainlinear;
|
||||
}
|
||||
|
||||
const end = t + hapDuration;
|
||||
const endWithRelease = end + release;
|
||||
|
||||
@@ -80,13 +80,12 @@ export function secondsToCycle(t, cps) {
|
||||
// deduces relevant info for sample loading from hap.value and sample definition
|
||||
// it encapsulates the core sampler logic into a pure and synchronous function
|
||||
// hapValue: Hap.value, bank: sample bank definition for sound "s" (values in strudel.json format)
|
||||
export function getCommonSampleInfoFromBank(hapValue, bank) {
|
||||
export function getCommonSampleInfo(hapValue, bank) {
|
||||
const { s, n = 0 } = hapValue;
|
||||
let midi = valueToMidi(hapValue, 36);
|
||||
let transpose = midi - 36; // C3 is middle C;
|
||||
let url;
|
||||
let index = 0;
|
||||
let {transpose, label} = getCommonSampleInfo(hapValue)
|
||||
|
||||
if (Array.isArray(bank)) {
|
||||
index = getSoundIndex(n, bank.length);
|
||||
url = bank[index];
|
||||
@@ -103,20 +102,6 @@ export function getCommonSampleInfoFromBank(hapValue, bank) {
|
||||
index = getSoundIndex(n, bank[closest].length);
|
||||
url = bank[closest][index];
|
||||
}
|
||||
label = `${s}:${index}`;
|
||||
return { transpose, url, label };
|
||||
const label = `${s}:${index}`;
|
||||
return { transpose, url, index, midi, label };
|
||||
}
|
||||
|
||||
|
||||
export function getCommonSampleInfo(hapValue) {
|
||||
const { s, n = 0 } = hapValue;
|
||||
let midi = valueToMidi(hapValue, 36);
|
||||
let transpose = midi - 36; // C3 is middle C;
|
||||
const label = `${s}:${n}`;
|
||||
return { transpose, label };
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { getAudioContext, registerSound } from './index.mjs';
|
||||
import { getCommonSampleInfoFromBank } from './util.mjs';
|
||||
import { getCommonSampleInfo } from './util.mjs';
|
||||
import {
|
||||
applyFM,
|
||||
applyParameterModulators,
|
||||
@@ -216,7 +216,7 @@ export async function onTriggerSynth(t, value, onended, tables, cps, frameLen) {
|
||||
warpmode = Warpmode[warpmode.toUpperCase()] ?? Warpmode.NONE;
|
||||
}
|
||||
const frequency = getFrequencyFromValue(value);
|
||||
const { url, label } = getCommonSampleInfoFromBank(value, tables);
|
||||
const { url, label } = getCommonSampleInfo(value, tables);
|
||||
const payload = await getPayload(url, label, frameLen);
|
||||
let holdEnd = t + duration;
|
||||
if (clip !== undefined) {
|
||||
|
||||
@@ -4527,6 +4527,75 @@ exports[`runs examples > example "gain" example index 0 1`] = `
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`runs examples > example "gainlinear" example index 0 1`] = `
|
||||
[
|
||||
"[ 0/1 → 1/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 1/16 → 1/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 1/8 → 3/16 | s:hh gainlinear:1 ]",
|
||||
"[ 3/16 → 1/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 1/4 → 5/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 5/16 → 3/8 | s:hh gainlinear:1 ]",
|
||||
"[ 3/8 → 7/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 7/16 → 1/2 | s:hh gainlinear:1 ]",
|
||||
"[ 1/2 → 9/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 9/16 → 5/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 5/8 → 11/16 | s:hh gainlinear:1 ]",
|
||||
"[ 11/16 → 3/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 3/4 → 13/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 13/16 → 7/8 | s:hh gainlinear:1 ]",
|
||||
"[ 7/8 → 15/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 15/16 → 1/1 | s:hh gainlinear:1 ]",
|
||||
"[ 1/1 → 17/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 17/16 → 9/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 9/8 → 19/16 | s:hh gainlinear:1 ]",
|
||||
"[ 19/16 → 5/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 5/4 → 21/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 21/16 → 11/8 | s:hh gainlinear:1 ]",
|
||||
"[ 11/8 → 23/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 23/16 → 3/2 | s:hh gainlinear:1 ]",
|
||||
"[ 3/2 → 25/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 25/16 → 13/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 13/8 → 27/16 | s:hh gainlinear:1 ]",
|
||||
"[ 27/16 → 7/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 7/4 → 29/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 29/16 → 15/8 | s:hh gainlinear:1 ]",
|
||||
"[ 15/8 → 31/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 31/16 → 2/1 | s:hh gainlinear:1 ]",
|
||||
"[ 2/1 → 33/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 33/16 → 17/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 17/8 → 35/16 | s:hh gainlinear:1 ]",
|
||||
"[ 35/16 → 9/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 9/4 → 37/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 37/16 → 19/8 | s:hh gainlinear:1 ]",
|
||||
"[ 19/8 → 39/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 39/16 → 5/2 | s:hh gainlinear:1 ]",
|
||||
"[ 5/2 → 41/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 41/16 → 21/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 21/8 → 43/16 | s:hh gainlinear:1 ]",
|
||||
"[ 43/16 → 11/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 11/4 → 45/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 45/16 → 23/8 | s:hh gainlinear:1 ]",
|
||||
"[ 23/8 → 47/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 47/16 → 3/1 | s:hh gainlinear:1 ]",
|
||||
"[ 3/1 → 49/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 49/16 → 25/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 25/8 → 51/16 | s:hh gainlinear:1 ]",
|
||||
"[ 51/16 → 13/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 13/4 → 53/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 53/16 → 27/8 | s:hh gainlinear:1 ]",
|
||||
"[ 27/8 → 55/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 55/16 → 7/2 | s:hh gainlinear:1 ]",
|
||||
"[ 7/2 → 57/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 57/16 → 29/8 | s:hh gainlinear:0.4 ]",
|
||||
"[ 29/8 → 59/16 | s:hh gainlinear:1 ]",
|
||||
"[ 59/16 → 15/4 | s:hh gainlinear:0.4 ]",
|
||||
"[ 15/4 → 61/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 61/16 → 31/8 | s:hh gainlinear:1 ]",
|
||||
"[ 31/8 → 63/16 | s:hh gainlinear:0.4 ]",
|
||||
"[ 63/16 → 4/1 | s:hh gainlinear:1 ]",
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`runs examples > example "gap" example index 0 1`] = `[]`;
|
||||
|
||||
exports[`runs examples > example "grow" example index 0 1`] = `
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { getSampleBufferSource, onTriggerSample, registerSampleSource } from '@strudel/webaudio';
|
||||
import { registerSampleSource } from '@strudel/webaudio';
|
||||
import { isAudioFile } from './files.mjs';
|
||||
import { getSoundIndex, logger } from '@strudel/core';
|
||||
import { registerSound } from '@strudel/webaudio';
|
||||
import { getCommonSampleInfo } from '../../../packages/superdough/util.mjs';
|
||||
import { logger } from '@strudel/core';
|
||||
|
||||
//utilites for writing and reading to the indexdb
|
||||
|
||||
@@ -27,15 +25,6 @@ function clearAllIDB() {
|
||||
|
||||
export function clearIDB(dbName) {
|
||||
return window.indexedDB.deleteDatabase(dbName);
|
||||
}
|
||||
|
||||
|
||||
function registerSampleFromIdb(key, bank, params) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// queries the DB, and registers the sounds so they can be played
|
||||
@@ -63,76 +52,31 @@ export function registerSamplesFromDB(config = userSamplesDBConfig, onComplete =
|
||||
if (!isAudioFile(title)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const splitRelativePath = soundFile.id.split('/');
|
||||
let parentDirectory =
|
||||
//fallback to file name before period and seperator if no parent directory
|
||||
splitRelativePath[splitRelativePath.length - 2] ?? soundFile.id.split(/\W+/)[0] ?? 'user';
|
||||
|
||||
// const blob = soundFile.blob;
|
||||
|
||||
const blob = soundFile.blob;
|
||||
|
||||
return blobToDataUrl(blob).then((soundPath) => {
|
||||
const titlePathMap = sounds.get(parentDirectory) ?? new Map();
|
||||
|
||||
titlePathMap.set(title, soundFile.id);
|
||||
titlePathMap.set(title, soundPath);
|
||||
|
||||
sounds.set(parentDirectory, titlePathMap);
|
||||
|
||||
|
||||
|
||||
|
||||
// return blobToDataUrl(blob).then((soundPath) => {
|
||||
// const titlePathMap = sounds.get(parentDirectory) ?? new Map();
|
||||
|
||||
// titlePathMap.set(title, soundPath);
|
||||
|
||||
// sounds.set(parentDirectory, titlePathMap);
|
||||
// return;
|
||||
// });
|
||||
return;
|
||||
});
|
||||
}),
|
||||
)
|
||||
.then(() => {
|
||||
sounds.forEach((titlePathMap, key) => {
|
||||
const bank = Array.from(titlePathMap.keys())
|
||||
const value = Array.from(titlePathMap.keys())
|
||||
.sort((a, b) => {
|
||||
return a.localeCompare(b);
|
||||
})
|
||||
.map((title) => titlePathMap.get(title));
|
||||
|
||||
|
||||
|
||||
|
||||
registerSound(key, async (t, hapValue, onended) => {
|
||||
const { s, n = 0 } = hapValue;
|
||||
const index = getSoundIndex(n, bank.length);
|
||||
let {transpose, label} = getCommonSampleInfo(hapValue)
|
||||
const storeKey = bank[index];
|
||||
|
||||
openDB(config, (objectStore) => {
|
||||
const getRequest = objectStore.get(storeKey);
|
||||
getRequest.onsuccess = async (event) => {
|
||||
const result = event.target.result;
|
||||
let buffer = result?.blob.arrayBuffer ? await result.blob.arrayBuffer() : null;
|
||||
console.info(buffer)
|
||||
if (buffer) {
|
||||
const bufferSource = getSampleBufferSource(hapValue,buffer,transpose)
|
||||
onTriggerSample(t, hapValue, onended, bufferSource)
|
||||
} else {
|
||||
logger(`Could not load sample for ${storeKey}`, 'error');
|
||||
}
|
||||
};
|
||||
})
|
||||
|
||||
// const buffer = objectStore.getKey(key)
|
||||
// const bufferSource = getSampleBufferSource(hapValue,buffer,transpose)
|
||||
|
||||
// onTriggerSample(t, hapValue, onended, await getBufferSrcFromBank(hapValue, bank, undefined))
|
||||
|
||||
}, {
|
||||
type: 'sample',
|
||||
samples: bank,
|
||||
|
||||
});
|
||||
|
||||
// registerSampleSource(key, value, { prebake: false });
|
||||
registerSampleSource(key, value, { prebake: false });
|
||||
});
|
||||
|
||||
logger('imported sounds registered!', 'success');
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
resetGlobalEffects,
|
||||
resetLoadedSounds,
|
||||
initAudioOnFirstClick,
|
||||
resetDefaults,
|
||||
} from '@strudel/webaudio';
|
||||
import { setVersionDefaultsFrom } from './util.mjs';
|
||||
import { StrudelMirror, defaultSettings } from '@strudel/codemirror';
|
||||
@@ -182,7 +181,6 @@ export function useReplContext() {
|
||||
|
||||
const resetEditor = async () => {
|
||||
(await getModule('@strudel/tonal'))?.resetVoicings();
|
||||
resetDefaults();
|
||||
resetGlobalEffects();
|
||||
clearCanvas();
|
||||
clearHydra();
|
||||
|
||||
Reference in New Issue
Block a user