- encapsulate sample map handling

- add resolveUrl param
This commit is contained in:
Felix Roos 2023-06-29 02:00:59 +02:00
parent b0a8d12112
commit cc6997baec

View File

@ -21,7 +21,7 @@ function humanFileSize(bytes, si) {
return bytes.toFixed(1) + ' ' + units[u]; return bytes.toFixed(1) + ' ' + units[u];
} }
export const getSampleBufferSource = async (s, n, note, speed, freq, bank) => { export const getSampleBufferSource = async (s, n, note, speed, freq, bank, resolveUrl) => {
let transpose = 0; let transpose = 0;
if (freq !== undefined && note !== undefined) { if (freq !== undefined && note !== undefined) {
logger('[sampler] hap has note and freq. ignoring note', 'warning'); logger('[sampler] hap has note and freq. ignoring note', 'warning');
@ -45,6 +45,9 @@ export const getSampleBufferSource = async (s, n, note, speed, freq, bank) => {
transpose = -midiDiff(closest); // semitones to repitch transpose = -midiDiff(closest); // semitones to repitch
sampleUrl = bank[closest][n % bank[closest].length]; sampleUrl = bank[closest][n % bank[closest].length];
} }
if (resolveUrl) {
sampleUrl = await resolveUrl(sampleUrl);
}
let buffer = await loadBuffer(sampleUrl, ac, s, n); let buffer = await loadBuffer(sampleUrl, ac, s, n);
if (speed < 0) { if (speed < 0) {
// should this be cached? // should this be cached?
@ -91,6 +94,31 @@ export const getLoadedBuffer = (url) => {
return bufferCache[url]; return bufferCache[url];
}; };
export const processSampleMap = (sampleMap, fn, baseUrl = sampleMap._base || '') => {
return Object.entries(sampleMap).forEach(([key, value]) => {
if (typeof value === 'string') {
value = [value];
}
if (typeof value !== 'object') {
throw new Error('wrong sample map format for ' + key);
}
baseUrl = value._base || baseUrl;
const replaceUrl = (v) => (baseUrl + v).replace('github:', 'https://raw.githubusercontent.com/');
if (Array.isArray(value)) {
//return [key, value.map(replaceUrl)];
value = value.map(replaceUrl);
} else {
// must be object
value = Object.fromEntries(
Object.entries(value).map(([note, samples]) => {
return [note, (typeof samples === 'string' ? [samples] : samples).map(replaceUrl)];
}),
);
}
fn(key, value);
});
};
/** /**
* Loads a collection of samples to use with `s` * Loads a collection of samples to use with `s`
* @example * @example
@ -130,39 +158,23 @@ export const samples = async (sampleMap, baseUrl = sampleMap._base || '', option
}); });
} }
const { prebake, tag } = options; const { prebake, tag } = options;
Object.entries(sampleMap).forEach(([key, value]) => { processSampleMap(
if (typeof value === 'string') { sampleMap,
value = [value]; (key, value) =>
} registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value), {
if (typeof value !== 'object') { type: 'sample',
throw new Error('wrong sample map format for ' + key); samples: value,
} baseUrl,
baseUrl = value._base || baseUrl; prebake,
const replaceUrl = (v) => (baseUrl + v).replace('github:', 'https://raw.githubusercontent.com/'); tag,
if (Array.isArray(value)) { }),
//return [key, value.map(replaceUrl)]; baseUrl,
value = value.map(replaceUrl); );
} else {
// must be object
value = Object.fromEntries(
Object.entries(value).map(([note, samples]) => {
return [note, (typeof samples === 'string' ? [samples] : samples).map(replaceUrl)];
}),
);
}
registerSound(key, (t, hapValue, onended) => onTriggerSample(t, hapValue, onended, value), {
type: 'sample',
samples: value,
baseUrl,
prebake,
tag,
});
});
}; };
const cutGroups = []; const cutGroups = [];
export async function onTriggerSample(t, value, onended, bank) { export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
const { const {
s, s,
freq, freq,
@ -188,7 +200,7 @@ export async function onTriggerSample(t, value, onended, bank) {
//const soundfont = getSoundfontKey(s); //const soundfont = getSoundfontKey(s);
const time = t + nudge; const time = t + nudge;
const bufferSource = await getSampleBufferSource(s, n, note, speed, freq, bank); const bufferSource = await getSampleBufferSource(s, n, note, speed, freq, bank, resolveUrl);
// asny stuff above took too long? // asny stuff above took too long?
if (ac.currentTime > t) { if (ac.currentTime > t) {