rename "samples" to "bank"

This commit is contained in:
Felix Roos 2024-05-17 09:42:03 +02:00
parent 226cf356d6
commit 69a5d62027
3 changed files with 23 additions and 23 deletions

View File

@ -24,28 +24,28 @@ function humanFileSize(bytes, si) {
// deduces relevant info for sample loading from hap.value and sample definition // deduces relevant info for sample loading from hap.value and sample definition
// it encapsulates the core sampler logic into a pure and synchronous function // it encapsulates the core sampler logic into a pure and synchronous function
// hapValue: Hap.value, samples: sample definition for sound "s" (values in strudel.json format) // hapValue: Hap.value, bank: sample bank definition for sound "s" (values in strudel.json format)
export function getSampleInfo(hapValue, samples) { export function getSampleInfo(hapValue, bank) {
const { s, n = 0, speed = 1.0 } = hapValue; const { s, n = 0, speed = 1.0 } = hapValue;
let midi = valueToMidi(hapValue, 36); let midi = valueToMidi(hapValue, 36);
let transpose = midi - 36; // C3 is middle C; let transpose = midi - 36; // C3 is middle C;
let sampleUrl; let sampleUrl;
let index = 0; let index = 0;
if (Array.isArray(samples)) { if (Array.isArray(bank)) {
index = getSoundIndex(n, samples.length); index = getSoundIndex(n, bank.length);
sampleUrl = samples[index]; sampleUrl = bank[index];
} else { } else {
const midiDiff = (noteA) => noteToMidi(noteA) - midi; const midiDiff = (noteA) => noteToMidi(noteA) - midi;
// object format will expect keys as notes // object format will expect keys as notes
const closest = Object.keys(samples) const closest = Object.keys(bank)
.filter((k) => !k.startsWith('_')) .filter((k) => !k.startsWith('_'))
.reduce( .reduce(
(closest, key, j) => (!closest || Math.abs(midiDiff(key)) < Math.abs(midiDiff(closest)) ? key : closest), (closest, key, j) => (!closest || Math.abs(midiDiff(key)) < Math.abs(midiDiff(closest)) ? key : closest),
null, null,
); );
transpose = -midiDiff(closest); // semitones to repitch transpose = -midiDiff(closest); // semitones to repitch
index = getSoundIndex(n, samples[closest].length); index = getSoundIndex(n, bank[closest].length);
sampleUrl = samples[closest][index]; sampleUrl = bank[closest][index];
} }
const label = `${s}:${index}`; const label = `${s}:${index}`;
let playbackRate = Math.abs(speed) * Math.pow(2, transpose / 12); let playbackRate = Math.abs(speed) * Math.pow(2, transpose / 12);
@ -53,8 +53,8 @@ export function getSampleInfo(hapValue, samples) {
} }
// takes hapValue and returns buffer + playbackRate. // takes hapValue and returns buffer + playbackRate.
export const getSampleBuffer = async (hapValue, samples, resolveUrl) => { export const getSampleBuffer = async (hapValue, bank, resolveUrl) => {
let { sampleUrl, label, playbackRate } = getSampleInfo(hapValue, samples); let { sampleUrl, label, playbackRate } = getSampleInfo(hapValue, bank);
if (resolveUrl) { if (resolveUrl) {
sampleUrl = await resolveUrl(sampleUrl); sampleUrl = await resolveUrl(sampleUrl);
} }
@ -68,8 +68,8 @@ export const getSampleBuffer = async (hapValue, samples, resolveUrl) => {
}; };
// creates playback ready AudioBufferSourceNode from hapValue // creates playback ready AudioBufferSourceNode from hapValue
export const getSampleBufferSource = async (hapValue, samples, resolveUrl) => { export const getSampleBufferSource = async (hapValue, bank, resolveUrl) => {
let { buffer, playbackRate } = await getSampleBuffer(hapValue, samples, resolveUrl); let { buffer, playbackRate } = await getSampleBuffer(hapValue, bank, resolveUrl);
if (hapValue.speed < 0) { if (hapValue.speed < 0) {
// should this be cached? // should this be cached?
buffer = reverseBuffer(buffer); buffer = reverseBuffer(buffer);
@ -264,10 +264,10 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option
const { prebake, tag } = options; const { prebake, tag } = options;
processSampleMap( processSampleMap(
sampleMap, sampleMap,
(key, value) => (key, bank) =>
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value), { registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, bank), {
type: 'sample', type: 'sample',
samples: value, samples: bank,
baseUrl, baseUrl,
prebake, prebake,
tag, tag,
@ -278,7 +278,7 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option
const cutGroups = []; const cutGroups = [];
export async function onTriggerSample(t, value, onended, samples, resolveUrl) { export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
let { let {
s, s,
nudge = 0, // TODO: is this in seconds? nudge = 0, // TODO: is this in seconds?
@ -300,7 +300,7 @@ export async function onTriggerSample(t, value, onended, samples, resolveUrl) {
// destructure adsr here, because the default should be different for synths and samples // 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]); let [attack, decay, sustain, release] = getADSRValues([value.attack, value.decay, value.sustain, value.release]);
const { bufferSource, sliceDuration, offset } = await getSampleBufferSource(value, samples, resolveUrl); const { bufferSource, sliceDuration, offset } = await getSampleBufferSource(value, bank, resolveUrl);
// asny stuff above took too long? // asny stuff above took too long?
if (ac.currentTime > t) { if (ac.currentTime > t) {

View File

@ -23,10 +23,10 @@ async function hasStrudelJson(subpath) {
async function loadStrudelJson(subpath) { async function loadStrudelJson(subpath) {
const contents = await readTextFile(subpath + '/strudel.json', { dir }); const contents = await readTextFile(subpath + '/strudel.json', { dir });
const sampleMap = JSON.parse(contents); const sampleMap = JSON.parse(contents);
processSampleMap(sampleMap, (key, value) => { processSampleMap(sampleMap, (key, bank) => {
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value, fileResolver(subpath)), { registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, bank, fileResolver(subpath)), {
type: 'sample', type: 'sample',
samples: value, samples: bank,
fileSystem: true, fileSystem: true,
tag: 'local', tag: 'local',
}); });

View File

@ -49,10 +49,10 @@ export const registerSamplesFromDB = (config = userSamplesDBConfig, onComplete =
}); });
sounds.forEach((soundPaths, key) => { sounds.forEach((soundPaths, key) => {
const value = Array.from(soundPaths); const bank = Array.from(soundPaths);
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value), { registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, bank), {
type: 'sample', type: 'sample',
samples: value, samples: bank,
baseUrl: undefined, baseUrl: undefined,
prebake: false, prebake: false,
tag: undefined, tag: undefined,