mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-25 20:48:27 +00:00
move sampler to webaudio
+ add basic sample playback to .out
This commit is contained in:
parent
44574af3eb
commit
d532f8007e
@ -7,3 +7,4 @@ This program is free software: you can redistribute it and/or modify it under th
|
|||||||
export * from './clockworker.mjs';
|
export * from './clockworker.mjs';
|
||||||
export * from './scheduler.mjs';
|
export * from './scheduler.mjs';
|
||||||
export * from './webaudio.mjs';
|
export * from './webaudio.mjs';
|
||||||
|
export * from './sampler.mjs';
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
const bufferCache = {}; // string: Promise<ArrayBuffer>
|
const bufferCache = {}; // string: Promise<ArrayBuffer>
|
||||||
const loadCache = {}; // string: Promise<ArrayBuffer>
|
const loadCache = {}; // string: Promise<ArrayBuffer>
|
||||||
|
|
||||||
|
export const getCachedBuffer = (url) => bufferCache[url];
|
||||||
|
|
||||||
export const loadBuffer = (url, ac) => {
|
export const loadBuffer = (url, ac) => {
|
||||||
if (!loadCache[url]) {
|
if (!loadCache[url]) {
|
||||||
loadCache[url] = fetch(url)
|
loadCache[url] = fetch(url)
|
||||||
@ -7,6 +7,7 @@ This program is free software: you can redistribute it and/or modify it under th
|
|||||||
// import { Pattern, getFrequency, patternify2 } from '@strudel.cycles/core';
|
// import { Pattern, getFrequency, patternify2 } from '@strudel.cycles/core';
|
||||||
import * as strudel from '@strudel.cycles/core';
|
import * as strudel from '@strudel.cycles/core';
|
||||||
import { fromMidi } from '@strudel.cycles/core';
|
import { fromMidi } from '@strudel.cycles/core';
|
||||||
|
import { loadBuffer } from './sampler.mjs';
|
||||||
const { Pattern } = strudel;
|
const { Pattern } = strudel;
|
||||||
|
|
||||||
// export const getAudioContext = () => Tone.getContext().rawContext;
|
// export const getAudioContext = () => Tone.getContext().rawContext;
|
||||||
@ -39,7 +40,7 @@ const getADSR = (attack, decay, sustain, release, velocity, begin, end) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Pattern.prototype.out = function () {
|
Pattern.prototype.out = function () {
|
||||||
return this.onTrigger((t, hap, ct) => {
|
return this.onTrigger(async (t, hap, ct) => {
|
||||||
const ac = getAudioContext();
|
const ac = getAudioContext();
|
||||||
// calculate correct time (tone.js workaround)
|
// calculate correct time (tone.js workaround)
|
||||||
t = ac.currentTime + t - ct;
|
t = ac.currentTime + t - ct;
|
||||||
@ -47,7 +48,7 @@ Pattern.prototype.out = function () {
|
|||||||
let {
|
let {
|
||||||
freq,
|
freq,
|
||||||
s,
|
s,
|
||||||
n,
|
n = 0,
|
||||||
gain = 1,
|
gain = 1,
|
||||||
cutoff,
|
cutoff,
|
||||||
resonance = 1,
|
resonance = 1,
|
||||||
@ -61,26 +62,49 @@ Pattern.prototype.out = function () {
|
|||||||
sustain = 1,
|
sustain = 1,
|
||||||
release = 0.001,
|
release = 0.001,
|
||||||
} = hap.value;
|
} = hap.value;
|
||||||
if (!n && !freq) {
|
|
||||||
console.warn('unplayable value:', hap.value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// get frequency
|
|
||||||
if (!freq && typeof n === 'number') {
|
|
||||||
freq = fromMidi(n); // + 48);
|
|
||||||
}
|
|
||||||
if (!freq && typeof n === 'string') {
|
|
||||||
freq = fromMidi(toMidi(n));
|
|
||||||
}
|
|
||||||
// the chain will hold all audio nodes that connect to each other
|
// the chain will hold all audio nodes that connect to each other
|
||||||
const chain = [];
|
const chain = [];
|
||||||
// make oscillator
|
if (!s || ['sine', 'square', 'triangle', 'sawtooth'].includes(s)) {
|
||||||
const o = ac.createOscillator();
|
// get frequency
|
||||||
o.type = s || 'triangle';
|
if (!freq && typeof n === 'number') {
|
||||||
o.frequency.value = Number(freq);
|
freq = fromMidi(n); // + 48);
|
||||||
o.start(t);
|
}
|
||||||
o.stop(t + hap.duration + release);
|
if (!freq && typeof n === 'string') {
|
||||||
chain.push(o);
|
freq = fromMidi(toMidi(n));
|
||||||
|
}
|
||||||
|
// make oscillator
|
||||||
|
const o = ac.createOscillator();
|
||||||
|
o.type = s || 'triangle';
|
||||||
|
o.frequency.value = Number(freq);
|
||||||
|
o.start(t);
|
||||||
|
o.stop(t + hap.duration + release);
|
||||||
|
chain.push(o);
|
||||||
|
} else {
|
||||||
|
// load sample
|
||||||
|
const samples = getLoadedSamples();
|
||||||
|
if (!samples) {
|
||||||
|
console.warn('no samples loaded');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const bank = samples?.[s];
|
||||||
|
if (!bank) {
|
||||||
|
console.warn('sample not found:', s, 'try one of ' + Object.keys(samples));
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
const bank = samples[s];
|
||||||
|
const sampleUrl = bank[n % bank.length];
|
||||||
|
let buffer = await loadBuffer(sampleUrl, ac);
|
||||||
|
if (ac.currentTime > t) {
|
||||||
|
console.warn('sample still loading:', s);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const src = ac.createBufferSource();
|
||||||
|
src.buffer = buffer;
|
||||||
|
src.start(t);
|
||||||
|
src.stop(t + hap.duration + release);
|
||||||
|
chain.push(src);
|
||||||
|
}
|
||||||
|
}
|
||||||
// envelope
|
// envelope
|
||||||
const adsr = getADSR(attack, decay, sustain, release, 1, t, t + hap.duration);
|
const adsr = getADSR(attack, decay, sustain, release, 1, t, t + hap.duration);
|
||||||
chain.push(adsr);
|
chain.push(adsr);
|
||||||
@ -98,7 +122,7 @@ Pattern.prototype.out = function () {
|
|||||||
}
|
}
|
||||||
// master out
|
// master out
|
||||||
const master = ac.createGain();
|
const master = ac.createGain();
|
||||||
master.gain.value = 0.1 * gain;
|
master.gain.value = 0.8 * gain;
|
||||||
chain.push(master);
|
chain.push(master);
|
||||||
chain.push(ac.destination);
|
chain.push(ac.destination);
|
||||||
// connect chain elements together
|
// connect chain elements together
|
||||||
|
|||||||
@ -1,2 +1 @@
|
|||||||
export * from './webdirt.mjs';
|
export * from './webdirt.mjs';
|
||||||
export * from './sampler.mjs';
|
|
||||||
|
|||||||
@ -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, getLoadedBuffer } from './sampler.mjs';
|
import { getLoadedSamples, loadBuffer, getLoadedBuffer } from '@strudel.cycles/webaudio';
|
||||||
|
|
||||||
let webDirt;
|
let webDirt;
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,8 @@ import './App.css';
|
|||||||
import logo from './logo.svg';
|
import logo from './logo.svg';
|
||||||
import * as tunes from './tunes.mjs';
|
import * as tunes from './tunes.mjs';
|
||||||
import * as WebDirt from 'WebDirt';
|
import * as WebDirt from 'WebDirt';
|
||||||
import { loadWebDirt, resetLoadedSamples } from '@strudel.cycles/webdirt';
|
import { loadWebDirt } from '@strudel.cycles/webdirt';
|
||||||
|
import { resetLoadedSamples } from '@strudel.cycles/webaudio';
|
||||||
|
|
||||||
evalScope(
|
evalScope(
|
||||||
Tone,
|
Tone,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user