sample source for WT

This commit is contained in:
Jade (Rose) Rowland
2025-09-26 21:47:45 -05:00
parent e1629e8383
commit 2b3b638943
5 changed files with 44 additions and 28 deletions
+27 -11
View File
@@ -1,5 +1,5 @@
import { noteToMidi, valueToMidi, getSoundIndex } from './util.mjs';
import { getAudioContext, registerSound } from './index.mjs';
import { getAudioContext, registerSound, registerWaveTable } from './index.mjs';
import { getADSRValues, getParamADSR, getPitchEnvelope, getVibratoOscillator } from './helpers.mjs';
import { logger } from './logger.mjs';
@@ -79,14 +79,14 @@ export const getSampleBufferSource = async (hapValue, bank, resolveUrl) => {
bufferSource.buffer = buffer;
bufferSource.playbackRate.value = playbackRate;
const { s, loopBegin = 0, loopEnd = 1, begin = 0, end = 1 } = hapValue;
const { loopBegin = 0, loopEnd = 1, begin = 0, end = 1 } = hapValue;
// "The computation of the offset into the sound is performed using the sound buffer's natural sample rate,
// rather than the current playback rate, so even if the sound is playing at twice its normal speed,
// the midway point through a 10-second audio buffer is still 5."
const offset = begin * bufferSource.buffer.duration;
const loop = s.startsWith('wt_') ? 1 : hapValue.loop;
const loop = hapValue.loop;
if (loop) {
bufferSource.loop = true;
bufferSource.loopStart = loopBegin * bufferSource.buffer.duration - offset;
@@ -267,16 +267,13 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option
return samples(json, baseUrl || base, options);
}
const { prebake, tag } = options;
processSampleMap(
sampleMap,
(key, bank) =>
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, bank), {
type: 'sample',
samples: bank,
baseUrl,
prebake,
tag,
}),
(key, bank) => {
registerSample(key, bank, { baseUrl, prebake, tag })
},
baseUrl,
);
};
@@ -366,3 +363,22 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
return handle;
}
function registerSample(key, bank, params) {
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) {
registerWaveTable(key,bank, params)
} else {
registerSample(key, bank, params)
}
}
+1
View File
@@ -76,3 +76,4 @@ export function cycleToSeconds(cycle, cps) {
export function secondsToCycle(t, cps) {
return t * cps;
}
+10 -7
View File
@@ -140,15 +140,18 @@ const _processTables = (json, baseUrl, frameLen) => {
baseUrl = githubPath(baseUrl, '');
}
value = value.map((v) => baseUrl + v);
registerSound(key, (t, hapValue, onended) => onTriggerSynth(t, hapValue, onended, value, frameLen), {
type: 'wavetable',
tables: value,
baseUrl,
frameLen,
});
registerWaveTable(key,value, {baseUrl, frameLen})
});
};
export function registerWaveTable(key, bank, params) {
registerSound(key, (t, hapValue, onended) => onTriggerSynth(t, hapValue, onended, bank, params?.frameLen ?? 2048), {
type: 'wavetable',
tables: bank,
...params
});
}
/**
* Loads a collection of wavetables to use with `s`
*
@@ -179,7 +182,7 @@ export const tables = async (url, frameLen, json) => {
});
};
async function onTriggerSynth(t, value, onended, bank, frameLen) {
export async function onTriggerSynth(t, value, onended, bank, frameLen) {
const { s, n = 0, duration } = value;
const ac = getAudioContext();
const [attack, decay, sustain, release] = getADSRValues([value.attack, value.decay, value.sustain, value.release]);
+3 -2
View File
@@ -1239,6 +1239,7 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
}
const outL = outputs[0][0];
const outR = outputs[0][1] || outputs[0][0];
const gainAdjustment = .15;
if (!this.tables) {
outL.fill(0);
@@ -1284,8 +1285,8 @@ class WavetableOscillatorProcessor extends AudioWorkletProcessor {
if (warpMode === WarpMode.FLIP && this.phase[n] < warpAmount) {
s = -s;
}
outL[i] += (s * gainL) / Math.sqrt(voices);
outR[i] += (s * gainR) / Math.sqrt(voices);
outL[i] += ((s * gainL) / Math.sqrt(voices)) * gainAdjustment;
outR[i] += ((s * gainR) / Math.sqrt(voices)) * gainAdjustment;
this.phase[n] = wrapPhase(this.phase[n] + dPhase);
}
}
+3 -8
View File
@@ -1,4 +1,4 @@
import { registerSound, onTriggerSample } from '@strudel/webaudio';
import { registerSampleSource } from '@strudel/webaudio';
import { isAudioFile } from './files.mjs';
import { logger } from '@strudel/core';
@@ -76,13 +76,8 @@ export function registerSamplesFromDB(config = userSamplesDBConfig, onComplete =
})
.map((title) => titlePathMap.get(title));
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value), {
type: 'sample',
samples: value,
baseUrl: undefined,
prebake: false,
tag: undefined,
});
registerSampleSource(key,value, {prebake: false})
});
logger('imported sounds registered!', 'success');