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 loadCache = {}; // string: Promise<ArrayBuffer>
|
||||
|
||||
export const loadBuffer = (url, ac) => {
|
||||
if (!bufferCache[url]) {
|
||||
bufferCache[url] = fetch(url)
|
||||
if (!loadCache[url]) {
|
||||
loadCache[url] = fetch(url)
|
||||
.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];
|
||||
};
|
||||
|
||||
|
|
@ -70,6 +79,32 @@ export const loadGithubSamples = async (path, nameFn) => {
|
|||
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 = () => {
|
||||
sampleCache.current = undefined;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue