add samples helper
+ dont play samples after load
This commit is contained in:
parent
e27b8aeb66
commit
5c6c5c9608
2 changed files with 49 additions and 9 deletions
|
|
@ -1,11 +1,20 @@
|
||||||
const bufferCache = {}; // string: Promise<ArrayBuffer>
|
const bufferCache = {}; // string: Promise<ArrayBuffer>
|
||||||
|
const loadCache = {}; // string: Promise<ArrayBuffer>
|
||||||
|
|
||||||
export const loadBuffer = (url, ac) => {
|
export const loadBuffer = (url, ac) => {
|
||||||
if (!bufferCache[url]) {
|
if (!loadCache[url]) {
|
||||||
bufferCache[url] = fetch(url)
|
loadCache[url] = fetch(url)
|
||||||
.then((res) => res.arrayBuffer())
|
.then((res) => res.arrayBuffer())
|
||||||
.then((res) => ac.decodeAudioData(res));
|
.then(async (res) => {
|
||||||
|
const decoded = await ac.decodeAudioData(res);
|
||||||
|
bufferCache[url] = decoded;
|
||||||
|
return decoded;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
return loadCache[url];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getLoadedBuffer = (url) => {
|
||||||
return bufferCache[url];
|
return bufferCache[url];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -70,6 +79,32 @@ export const loadGithubSamples = async (path, nameFn) => {
|
||||||
return githubCache[path];
|
return githubCache[path];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* load the given sample map for webdirt
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* loadSamples({
|
||||||
|
* bd: '808bd/BD0000.WAV',
|
||||||
|
* sd: ['808sd/SD0000.WAV','808sd/SD0010.WAV','808sd/SD0050.WAV']
|
||||||
|
* }, 'https://raw.githubusercontent.com/tidalcycles/Dirt-Samples/master/');
|
||||||
|
* s("bd <sd!7 sd(3,4,2)>").n(2).webdirt()
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const samples = (sampleMap, baseUrl = '') => {
|
||||||
|
sampleCache.current = {
|
||||||
|
...sampleCache.current,
|
||||||
|
...Object.fromEntries(
|
||||||
|
Object.entries(sampleMap).map(([key, value]) => [
|
||||||
|
key,
|
||||||
|
(typeof value === 'string' ? [value] : value).map((v) =>
|
||||||
|
(baseUrl + v).replace('github:', 'https://raw.githubusercontent.com/'),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const resetLoadedSamples = () => {
|
export const resetLoadedSamples = () => {
|
||||||
sampleCache.current = undefined;
|
sampleCache.current = undefined;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as strudel from '@strudel.cycles/core';
|
import * as strudel from '@strudel.cycles/core';
|
||||||
const { Pattern } = strudel;
|
const { Pattern } = strudel;
|
||||||
import * as WebDirt from 'WebDirt';
|
import * as WebDirt from 'WebDirt';
|
||||||
import { getLoadedSamples, loadBuffer } from './sampler.mjs';
|
import { getLoadedSamples, loadBuffer, getLoadedBuffer } from './sampler.mjs';
|
||||||
|
|
||||||
let webDirt;
|
let webDirt;
|
||||||
|
|
||||||
|
|
@ -70,7 +70,7 @@ Pattern.prototype.webdirt = function () {
|
||||||
const deadline = time - currentTime;
|
const deadline = time - currentTime;
|
||||||
const { s, n = 0, ...rest } = e.value || {};
|
const { s, n = 0, ...rest } = e.value || {};
|
||||||
if (!s) {
|
if (!s) {
|
||||||
console.warn('webdirt: no "s" was set!');
|
console.warn('Pattern.webdirt: no "s" was set!');
|
||||||
}
|
}
|
||||||
const samples = getLoadedSamples();
|
const samples = getLoadedSamples();
|
||||||
if (!samples?.[s]) {
|
if (!samples?.[s]) {
|
||||||
|
|
@ -79,13 +79,18 @@ Pattern.prototype.webdirt = function () {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!samples?.[s]) {
|
if (!samples?.[s]) {
|
||||||
console.warn(`webdirt: sample "${s}" not found in loaded samples`, samples);
|
console.warn(`Pattern.webdirt: sample "${s}" not found in loaded samples`, samples);
|
||||||
} else {
|
} else {
|
||||||
const bank = samples[s];
|
const bank = samples[s];
|
||||||
const sampleUrl = bank[n % bank.length];
|
const sampleUrl = bank[n % bank.length];
|
||||||
const buffer = await loadBuffer(sampleUrl, webDirt.ac);
|
const buffer = getLoadedBuffer(sampleUrl);
|
||||||
const msg = { buffer: { buffer }, ...rest };
|
if (!buffer) {
|
||||||
webDirt.playSample(msg, deadline);
|
console.log(`Pattern.webdirt: load ${s}:${n} from ${sampleUrl}`);
|
||||||
|
loadBuffer(sampleUrl, webDirt.ac);
|
||||||
|
} else {
|
||||||
|
const msg = { buffer: { buffer }, ...rest };
|
||||||
|
webDirt.playSample(msg, deadline);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return hap.setContext({ ...hap.context, onTrigger });
|
return hap.setContext({ ...hap.context, onTrigger });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue