move things around in init
+ shorten things
This commit is contained in:
parent
181e51ef0e
commit
73ef43dd2c
1 changed files with 79 additions and 147 deletions
|
|
@ -211,7 +211,7 @@ function lerp(x, y0, y1, exponent = 1) {
|
||||||
|
|
||||||
export class ADSR {
|
export class ADSR {
|
||||||
constructor(props = {}) {
|
constructor(props = {}) {
|
||||||
this.state = 'off'
|
this.state = 'off';
|
||||||
this.startTime = 0;
|
this.startTime = 0;
|
||||||
this.startVal = 0;
|
this.startVal = 0;
|
||||||
this.decayCurve = props.decayCurve ?? 1;
|
this.decayCurve = props.decayCurve ?? 1;
|
||||||
|
|
@ -398,7 +398,7 @@ export class Distort {
|
||||||
// distortion could be expressed as a function, because it's stateless
|
// distortion could be expressed as a function, because it's stateless
|
||||||
|
|
||||||
export class BufferPlayer {
|
export class BufferPlayer {
|
||||||
static samples = new Map();
|
static samples = new Map(); // string -> { channels, sampleRate }
|
||||||
buffer; // { channels: Float32Array, sampleRate: number }
|
buffer; // { channels: Float32Array, sampleRate: number }
|
||||||
pos = 0;
|
pos = 0;
|
||||||
sampleFreq = 261.626; // middle c
|
sampleFreq = 261.626; // middle c
|
||||||
|
|
@ -421,7 +421,7 @@ export function _rangex(sig, min, max) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// duplicate
|
// duplicate
|
||||||
export const getADSRValues = (params, curve = 'linear', defaultValues) => {
|
export const getADSR = (params, curve = 'linear', defaultValues) => {
|
||||||
const envmin = curve === 'exponential' ? 0.001 : 0.001;
|
const envmin = curve === 'exponential' ? 0.001 : 0.001;
|
||||||
const releaseMin = 0.01;
|
const releaseMin = 0.01;
|
||||||
const envmax = 1;
|
const envmax = 1;
|
||||||
|
|
@ -517,167 +517,99 @@ export class DoughVoice {
|
||||||
l = 0;
|
l = 0;
|
||||||
r = 0;
|
r = 0;
|
||||||
constructor(value) {
|
constructor(value) {
|
||||||
// params without defaults:
|
|
||||||
/*
|
|
||||||
bank,
|
|
||||||
source,
|
|
||||||
cutoff,
|
|
||||||
lpenv,
|
|
||||||
lpattack,
|
|
||||||
lpdecay,
|
|
||||||
lpsustain,
|
|
||||||
lprelease,
|
|
||||||
hpenv,
|
|
||||||
hcutoff,
|
|
||||||
hpattack,
|
|
||||||
hpdecay,
|
|
||||||
hpsustain,
|
|
||||||
hprelease,
|
|
||||||
bpenv,
|
|
||||||
bandf,
|
|
||||||
bpattack,
|
|
||||||
bpdecay,
|
|
||||||
bpsustain,
|
|
||||||
bprelease,
|
|
||||||
phaserrate,
|
|
||||||
phasersweep,
|
|
||||||
phasercenter,
|
|
||||||
shape,
|
|
||||||
vowel,
|
|
||||||
room,
|
|
||||||
roomfade,
|
|
||||||
roomlp,
|
|
||||||
roomdim,
|
|
||||||
roomsize,
|
|
||||||
ir,
|
|
||||||
analyze,
|
|
||||||
fmh,
|
|
||||||
fmi
|
|
||||||
*/
|
|
||||||
value.freq = getFrequency(value);
|
value.freq = getFrequency(value);
|
||||||
Object.assign(this, value);
|
let $ = this;
|
||||||
// params with defaults:
|
Object.assign($, value);
|
||||||
this.s = this.s ?? getDefaultValue('s');
|
$.s = $.s ?? getDefaultValue('s');
|
||||||
this.gain = this.gain ?? getDefaultValue('gain');
|
$.gain = $.gain ?? getDefaultValue('gain');
|
||||||
this.velocity = this.velocity ?? getDefaultValue('velocity');
|
$.velocity = $.velocity ?? getDefaultValue('velocity');
|
||||||
this.postgain = this.postgain ?? getDefaultValue('postgain');
|
$.postgain = $.postgain ?? getDefaultValue('postgain');
|
||||||
this.density = this.density ?? getDefaultValue('density');
|
$.density = $.density ?? getDefaultValue('density');
|
||||||
this.fanchor = this.fanchor ?? getDefaultValue('fanchor');
|
$.fanchor = $.fanchor ?? getDefaultValue('fanchor');
|
||||||
this.drive = this.drive ?? 0.69;
|
$.drive = $.drive ?? 0.69;
|
||||||
this.phaserdepth = this.phaserdepth ?? getDefaultValue('phaserdepth');
|
$.phaserdepth = $.phaserdepth ?? getDefaultValue('phaserdepth');
|
||||||
this.shapevol = this.shapevol ?? getDefaultValue('shapevol');
|
$.shapevol = $.shapevol ?? getDefaultValue('shapevol');
|
||||||
this.distortvol = this.distortvol ?? getDefaultValue('distortvol');
|
$.distortvol = $.distortvol ?? getDefaultValue('distortvol');
|
||||||
this.i = this.i ?? getDefaultValue('i');
|
$.i = $.i ?? getDefaultValue('i');
|
||||||
this.fft = this.fft ?? getDefaultValue('fft');
|
$.fft = $.fft ?? getDefaultValue('fft');
|
||||||
this.pan = this.pan ?? getDefaultValue('pan');
|
$.pan = $.pan ?? getDefaultValue('pan');
|
||||||
this.orbit = this.orbit ?? getDefaultValue('orbit');
|
$.orbit = $.orbit ?? getDefaultValue('orbit');
|
||||||
this.fmenv = this.fmenv ?? getDefaultValue('fmenv');
|
$.fmenv = $.fmenv ?? getDefaultValue('fmenv');
|
||||||
|
$.resonance = $.resonance ?? getDefaultValue('resonance');
|
||||||
|
$.hresonance = $.hresonance ?? getDefaultValue('hresonance');
|
||||||
|
$.bandq = $.bandq ?? getDefaultValue('bandq');
|
||||||
|
|
||||||
[this.attack, this.decay, this.sustain, this.release] = getADSRValues([
|
[$.attack, $.decay, $.sustain, $.release] = getADSR([$.attack, $.decay, $.sustain, $.release]);
|
||||||
this.attack,
|
|
||||||
this.decay,
|
|
||||||
this.sustain,
|
|
||||||
this.release,
|
|
||||||
]);
|
|
||||||
|
|
||||||
this._holdEnd = this._begin + this._duration; // needed for gate
|
$._holdEnd = $._begin + $._duration; // needed for gate
|
||||||
this._end = this._holdEnd + this.release + 0.01; // needed for despawn
|
$._end = $._holdEnd + $.release + 0.01; // needed for despawn
|
||||||
|
|
||||||
this.s ??= 'triangle';
|
$.s ??= 'triangle';
|
||||||
if (this.s === 'saw' || this.s === 'sawtooth') {
|
if ($.s === 'saw' || $.s === 'sawtooth') {
|
||||||
this.s = 'zaw'; // polyblepped saw when fm is applied
|
$.s = 'zaw'; // polyblepped saw when fm is applied
|
||||||
}
|
}
|
||||||
if (shapes[this.s]) {
|
|
||||||
const SourceClass = shapes[this.s];
|
if (shapes[$.s]) {
|
||||||
this._sound = new SourceClass();
|
const SourceClass = shapes[$.s];
|
||||||
} else if (BufferPlayer.samples.has(this.s)) {
|
$._sound = new SourceClass();
|
||||||
this._sample = new BufferPlayer();
|
$._channels = 1;
|
||||||
const buffer = BufferPlayer.samples.get(this.s);
|
} else if (BufferPlayer.samples.has($.s)) {
|
||||||
this._sample.buffer = buffer;
|
$._sample = new BufferPlayer();
|
||||||
|
const buffer = BufferPlayer.samples.get($.s);
|
||||||
|
$._sample.buffer = buffer; // {channels,sampleRate}
|
||||||
|
$._channels = $._sample.buffer.channels.length;
|
||||||
} else {
|
} else {
|
||||||
console.warn('sound not found', this.s);
|
console.warn('sound not found', $.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.penv) {
|
if ($.penv) {
|
||||||
this._penv = new ADSR({ decayCurve: 4 });
|
$._penv = new ADSR({ decayCurve: 4 });
|
||||||
[this.pattack, this.pdecay, this.psustain, this.prelease] = getADSRValues([
|
[$.pattack, $.pdecay, $.psustain, $.prelease] = getADSR([$.pattack, $.pdecay, $.psustain, $.prelease]);
|
||||||
this.pattack,
|
|
||||||
this.pdecay,
|
|
||||||
this.psustain,
|
|
||||||
this.prelease,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.fmi) {
|
if ($.fmi) {
|
||||||
this._fm = new SineOsc();
|
$._fm = new SineOsc();
|
||||||
this.fmh = this.fmh ?? getDefaultValue('fmh');
|
$.fmh = $.fmh ?? getDefaultValue('fmh');
|
||||||
if (this.fmenv) {
|
if ($.fmenv) {
|
||||||
this._fmenv = new ADSR({ decayCurve: 2 });
|
$._fmenv = new ADSR({ decayCurve: 2 });
|
||||||
[this.fmattack, this.fmdecay, this.fmsustain, this.fmrelease] = getADSRValues([
|
[$.fmattack, $.fmdecay, $.fmsustain, $.fmrelease] = getADSR([$.fmattack, $.fmdecay, $.fmsustain, $.fmrelease]);
|
||||||
this.fmattack,
|
|
||||||
this.fmdecay,
|
|
||||||
this.fmsustain,
|
|
||||||
this.fmrelease,
|
|
||||||
]);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter setup
|
|
||||||
this._lpf = this.cutoff ? new TwoPoleFilter() : null;
|
|
||||||
this.resonance = this.resonance ?? getDefaultValue('resonance');
|
|
||||||
if (this.lpenv) {
|
|
||||||
|
|
||||||
this._lpenv = new ADSR({ decayCurve: 4 });
|
|
||||||
|
|
||||||
[this.lpattack, this.lpdecay, this.lpsustain, this.lprelease] = getADSRValues([
|
|
||||||
this.lpattack,
|
|
||||||
this.lpdecay,
|
|
||||||
this.lpsustain,
|
|
||||||
this.lprelease,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._hpf = this.hcutoff ? new TwoPoleFilter() : null;
|
|
||||||
this.hresonance = this.hresonance ?? getDefaultValue('hresonance');
|
|
||||||
if (this.hpenv) {
|
|
||||||
this._hpenv = new ADSR({ decayCurve: 4 });
|
|
||||||
|
|
||||||
[this.hpattack, this.hpdecay, this.hpsustain, this.hprelease] = getADSRValues([
|
|
||||||
this.hpattack,
|
|
||||||
this.hpdecay,
|
|
||||||
this.hpsustain,
|
|
||||||
this.hprelease,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
this._bpf = this.bandf ? new TwoPoleFilter() : null;
|
|
||||||
this.bandq = this.bandq ?? getDefaultValue('bandq');
|
|
||||||
if (this.bpenv) {
|
|
||||||
this._bpenv = new ADSR({ decayCurve: 4 });
|
|
||||||
[this.bpattack, this.bpdecay, this.bpsustain, this.bprelease] = getADSRValues([
|
|
||||||
this.bpattack,
|
|
||||||
this.bpdecay,
|
|
||||||
this.bpsustain,
|
|
||||||
this.bprelease,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// gain envelope
|
// gain envelope
|
||||||
this._adsr = new ADSR({ decayCurve: 2 });
|
$._adsr = new ADSR({ decayCurve: 2 });
|
||||||
|
|
||||||
// fx setup
|
|
||||||
this._coarse = this.coarse ? new Coarse() : null;
|
|
||||||
this._crush = this.crush ? new Crush() : null;
|
|
||||||
this._distort = this.distort ? new Distort() : null;
|
|
||||||
|
|
||||||
// delay
|
// delay
|
||||||
this.delay = this.delay ?? getDefaultValue('delay');
|
$.delay = $.delay ?? getDefaultValue('delay');
|
||||||
this.delayfeedback = this.delayfeedback ?? getDefaultValue('delayfeedback');
|
$.delayfeedback = $.delayfeedback ?? getDefaultValue('delayfeedback');
|
||||||
this.delaytime = this.delaytime ?? getDefaultValue('delaytime');
|
$.delaytime = $.delaytime ?? getDefaultValue('delaytime');
|
||||||
|
|
||||||
// precalculated values
|
// precalculated values
|
||||||
this.piOverSr = Math.PI / value.sampleRate;
|
$.piOverSr = Math.PI / value.sampleRate;
|
||||||
this.eighthOverLogHalf = 0.125 / Math.log(0.5);
|
$.eighthOverLogHalf = 0.125 / Math.log(0.5);
|
||||||
|
|
||||||
|
// filter setup
|
||||||
|
if ($.lpenv) {
|
||||||
|
$._lpenv = new ADSR({ decayCurve: 4 });
|
||||||
|
[$.lpattack, $.lpdecay, $.lpsustain, $.lprelease] = getADSR([$.lpattack, $.lpdecay, $.lpsustain, $.lprelease]);
|
||||||
|
}
|
||||||
|
if ($.hpenv) {
|
||||||
|
$._hpenv = new ADSR({ decayCurve: 4 });
|
||||||
|
[$.hpattack, $.hpdecay, $.hpsustain, $.hprelease] = getADSR([$.hpattack, $.hpdecay, $.hpsustain, $.hprelease]);
|
||||||
|
}
|
||||||
|
if ($.bpenv) {
|
||||||
|
$._bpenv = new ADSR({ decayCurve: 4 });
|
||||||
|
[$.bpattack, $.bpdecay, $.bpsustain, $.bprelease] = getADSR([$.bpattack, $.bpdecay, $.bpsustain, $.bprelease]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1 per channel::
|
||||||
|
$._lpf = $.cutoff ? new TwoPoleFilter() : null;
|
||||||
|
$._hpf = $.hcutoff ? new TwoPoleFilter() : null;
|
||||||
|
$._bpf = $.bandf ? new TwoPoleFilter() : null;
|
||||||
|
|
||||||
|
// fx setup
|
||||||
|
$._coarse = $.coarse ? new Coarse() : null;
|
||||||
|
$._crush = $.crush ? new Crush() : null;
|
||||||
|
$._distort = $.distort ? new Distort() : null;
|
||||||
}
|
}
|
||||||
// credits to pulu: https://github.com/felixroos/kabelsalat/issues/35
|
// credits to pulu: https://github.com/felixroos/kabelsalat/issues/35
|
||||||
freq2cutoff(freq) {
|
freq2cutoff(freq) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue