Merge branch 'webaudio-rewrite' into starter-template

This commit is contained in:
Felix Roos
2022-06-18 17:39:43 +02:00
9 changed files with 116 additions and 34 deletions
+1
View File
@@ -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 './scheduler.mjs';
export * from './webaudio.mjs';
export * from './sampler.mjs';
@@ -1,6 +1,8 @@
const bufferCache = {}; // string: Promise<ArrayBuffer>
const loadCache = {}; // string: Promise<ArrayBuffer>
export const getCachedBuffer = (url) => bufferCache[url];
export const loadBuffer = (url, ac) => {
if (!loadCache[url]) {
loadCache[url] = fetch(url)
+77 -31
View File
@@ -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 * as strudel from '@strudel.cycles/core';
import { fromMidi } from '@strudel.cycles/core';
import { loadBuffer } from './sampler.mjs';
const { Pattern } = strudel;
// export const getAudioContext = () => Tone.getContext().rawContext;
@@ -19,16 +20,16 @@ export const getAudioContext = () => {
return audioContext;
};
const getFilter = (ac, type, frequency, Q) => {
const filter = ac.createBiquadFilter();
const getFilter = (type, frequency, Q) => {
const filter = getAudioContext().createBiquadFilter();
filter.type = type;
filter.frequency.value = frequency;
filter.Q.value = Q;
return filter;
};
const getADSR = (ac, attack, decay, sustain, release, velocity, begin, end) => {
const gainNode = ac.createGain();
const getADSR = (attack, decay, sustain, release, velocity, begin, end) => {
const gainNode = getAudioContext().createGain();
gainNode.gain.setValueAtTime(0, begin);
gainNode.gain.linearRampToValueAtTime(velocity, begin + attack); // attack
gainNode.gain.linearRampToValueAtTime(sustain * velocity, begin + attack + decay); // sustain start
@@ -39,7 +40,7 @@ const getADSR = (ac, attack, decay, sustain, release, velocity, begin, end) => {
};
Pattern.prototype.out = function () {
return this.onTrigger((t, hap, ct) => {
return this.onTrigger(async (t, hap, ct) => {
const ac = getAudioContext();
// calculate correct time (tone.js workaround)
t = ac.currentTime + t - ct;
@@ -47,7 +48,7 @@ Pattern.prototype.out = function () {
let {
freq,
s,
n,
n = 0,
gain = 1,
cutoff,
resonance = 1,
@@ -60,34 +61,79 @@ Pattern.prototype.out = function () {
decay = 0,
sustain = 1,
release = 0.001,
speed = 1, // sample playback speed
begin = 0,
end = 1,
} = 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
const chain = [];
// 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);
// envelope
const adsr = getADSR(ac, attack, decay, sustain, release, 1, t, t + hap.duration);
chain.push(adsr);
if (!s || ['sine', 'square', 'triangle', 'sawtooth'].includes(s)) {
// get frequency
if (!freq && typeof n === 'number') {
freq = fromMidi(n); // + 48);
}
if (!freq && typeof n === 'string') {
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);
// level down oscillators as they are really loud compared to samples i've tested
const g = ac.createGain();
g.gain.value = 0.5;
chain.push(g);
// TODO: make adsr work with samples without pops
// envelope
const adsr = getADSR(attack, decay, sustain, release, 1, t, t + hap.duration);
chain.push(adsr);
} 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 {
if (speed === 0) {
// no playback
return;
}
if (!s) {
console.warn('no sample specified');
return;
}
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, n);
return;
}
const src = ac.createBufferSource();
src.buffer = buffer;
src.playbackRate.value = Math.abs(speed);
// TODO: nudge, unit, cut, loop
let duration = src.buffer.duration;
const offset = begin * duration;
duration = ((end - begin) * duration) / Math.abs(speed);
src.start(t, offset, duration);
src.stop(t + duration);
chain.push(src);
}
}
// filters
cutoff !== undefined && chain.push(getFilter(ac, 'lowpass', cutoff, resonance));
hcutoff !== undefined && chain.push(getFilter(ac, 'highpass', hcutoff, hresonance));
bandf !== undefined && chain.push(getFilter(ac, 'bandpass', bandf, bandq));
cutoff !== undefined && chain.push(getFilter('lowpass', cutoff, resonance));
hcutoff !== undefined && chain.push(getFilter('highpass', hcutoff, hresonance));
bandf !== undefined && chain.push(getFilter('bandpass', bandf, bandq));
// TODO vowel
// TODO delay / delaytime / delayfeedback
// panning
@@ -98,7 +144,7 @@ Pattern.prototype.out = function () {
}
// master out
const master = ac.createGain();
master.gain.value = 0.1 * gain;
master.gain.value = 0.8 * gain;
chain.push(master);
chain.push(ac.destination);
// connect chain elements together
-1
View File
@@ -1,2 +1 @@
export * from './webdirt.mjs';
export * from './sampler.mjs';
+1 -1
View File
@@ -1,7 +1,7 @@
import * as strudel from '@strudel.cycles/core';
const { Pattern } = strudel;
import * as WebDirt from 'WebDirt';
import { getLoadedSamples, loadBuffer, getLoadedBuffer } from './sampler.mjs';
import { getLoadedSamples, loadBuffer, getLoadedBuffer } from '@strudel.cycles/webaudio';
let webDirt;
+2 -1
View File
@@ -13,7 +13,8 @@ import './App.css';
import logo from './logo.svg';
import * as tunes from './tunes.mjs';
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(
Tone,
+2
View File
@@ -6,6 +6,7 @@
import { evaluate } from '@strudel.cycles/eval';
import { extend } from '@strudel.cycles/eval';
import * as strudel from '@strudel.cycles/core';
import * as webaudio from '@strudel.cycles/webaudio';
import controls from '@strudel.cycles/core/controls.mjs';
// import gist from '@strudel.cycles/core/gist.js';
import { mini } from '@strudel.cycles/mini/mini.mjs';
@@ -129,6 +130,7 @@ extend(
toneHelpersMocked,
uiHelpersMocked,
controls,
webaudio,
/* controls,
toneHelpers,
voicingHelpers,
+30
View File
@@ -865,3 +865,33 @@ export const bornagain = `stack(
.jux(rev)
.out()
.stack(s("bd(3,8),hh*4,~ sd").webdirt())`;
export const meltingsubmarine = `samples({
clubkick: 'clubkick/2.wav',
sd: ['808sd/SD0010.WAV','808sd/SD0050.WAV'],
hh: 'hh/000_hh3closedhh.wav',
clak: 'clak/000_clak1.wav',
jvbass: ['jvbass/000_01.wav','jvbass/001_02.wav','jvbass/003_04.wav','jvbass/004_05.wav','jvbass/005_06.wav']
}, 'https://raw.githubusercontent.com/tidalcycles/Dirt-Samples/master/');
stack(
"<clubkick*2>,[~ <sd!3 sd(3,4,2)>],hh(3,4)".s().n("<0 1 2>").speed(perlin.range(.7,.9)),
"<a1 b1*2 a1(3,8) e2>"
.off(1/8,x=>x.add(12).degradeBy(.5))
.add(perlin.range(0,.5))
.n().decay(.15).sustain(0).s("sawtooth")
.superimpose(x=>x.add(.08)).gain(.4)
.cutoff(sine.slow(7).range(300,5000)),
"<Am7!3 <Em7 E7b13 Em7 Ebm7b5>>".voicings().superimpose(x=>x.add(.04))
.add(perlin.range(0,.5))
.n().s('sawtooth')
.gain(.16)
.cutoff(500)
.attack(1),
"a4 c5 <e6 a6>".struct("x(5,8)")
.superimpose(x=>x.add(.04))
.add(perlin.range(0,.5)).n()
.decay(.1).sustain(0).s('triangle')
.degradeBy(perlin.range(0,.5)).echoWith(4,.125,(x,n)=>x.gain(.15*1/(n+1)))
)
.out()
.slow(3/2)`;
File diff suppressed because one or more lines are too long