updated fontloader
This commit is contained in:
parent
96e0cce2d2
commit
93a4efcf6d
2 changed files with 43 additions and 12 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { noteToMidi, freqToMidi } from '@strudel.cycles/core';
|
import { noteToMidi, freqToMidi } from '@strudel.cycles/core';
|
||||||
import { getAudioContext, registerSound, getEnvelope } from '@strudel.cycles/webaudio';
|
import { getAudioContext, registerSound, getEnvelope, getADSRValues } from '@strudel.cycles/webaudio';
|
||||||
import gm from './gm.mjs';
|
import gm from './gm.mjs';
|
||||||
|
|
||||||
let loadCache = {};
|
let loadCache = {};
|
||||||
|
|
@ -131,7 +131,15 @@ export function registerSoundfonts() {
|
||||||
name,
|
name,
|
||||||
async (time, value, onended) => {
|
async (time, value, onended) => {
|
||||||
const { n = 0 } = value;
|
const { n = 0 } = value;
|
||||||
const { attack = 0.001, decay = 0.001, sustain = 1, release = 0.001 } = value;
|
const [attack, decay, sustain, release] = getADSRValues([
|
||||||
|
value.attack,
|
||||||
|
value.decay,
|
||||||
|
value.sustain,
|
||||||
|
value.release,
|
||||||
|
]);
|
||||||
|
|
||||||
|
console.log(attack, decay, sustain, release);
|
||||||
|
|
||||||
const font = fonts[n % fonts.length];
|
const font = fonts[n % fonts.length];
|
||||||
const ctx = getAudioContext();
|
const ctx = getAudioContext();
|
||||||
const bufferSource = await getFontBufferSource(font, value, ctx);
|
const bufferSource = await getFontBufferSource(font, value, ctx);
|
||||||
|
|
|
||||||
|
|
@ -70,18 +70,41 @@ export const getADSR = (attack, decay, sustain, release, velocity, begin, end) =
|
||||||
return gainNode;
|
return gainNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getParamADSR = (param, attack, decay, sustain, release, min, max, begin, end) => {
|
export const getParamADSR = (
|
||||||
// let phase = begin;
|
context,
|
||||||
|
param,
|
||||||
|
attack,
|
||||||
|
decay,
|
||||||
|
sustain,
|
||||||
|
release,
|
||||||
|
min,
|
||||||
|
max,
|
||||||
|
begin,
|
||||||
|
end,
|
||||||
|
//exponential works better for frequency modulations (such as filter cutoff) due to human ear perception
|
||||||
|
curve = 'exponential',
|
||||||
|
) => {
|
||||||
|
const ramp = curve === 'exponential' ? 'exponentialRampToValueAtTime' : 'linearRampToValueAtTime';
|
||||||
|
let phase = begin;
|
||||||
const range = max - min;
|
const range = max - min;
|
||||||
const peak = min + range;
|
const peak = min + range;
|
||||||
const sustainLevel = min + sustain * range;
|
|
||||||
param.setValueAtTime(min, begin);
|
param.setValueAtTime(min, begin);
|
||||||
param.exponentialRampToValueAtTime(peak, begin + attack);
|
phase += attack;
|
||||||
param.exponentialRampToValueAtTime(sustainLevel, begin + attack + decay);
|
//attack
|
||||||
//stop current envelope ramp at event end, and begin release ramp
|
param[ramp](peak, phase);
|
||||||
console.log(param);
|
phase += decay;
|
||||||
param.cancelAndHoldAtTime(end);
|
const sustainLevel = min + sustain * range;
|
||||||
param.exponentialRampToValueAtTime(min, end + Math.max(release, 0.1));
|
//decay
|
||||||
|
param[ramp](sustainLevel, phase);
|
||||||
|
//this timeout can be replaced with cancelAndHoldAtTime once it is implemented in Firefox
|
||||||
|
setTimeout(() => {
|
||||||
|
//sustain at current value
|
||||||
|
param.cancelScheduledValues(0);
|
||||||
|
phase += Math.max(release, 0.1);
|
||||||
|
//release
|
||||||
|
param[ramp](min, phase);
|
||||||
|
}, (end - context.currentTime) * 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getCompressor(ac, threshold, ratio, knee, attack, release) {
|
export function getCompressor(ac, threshold, ratio, knee, attack, release) {
|
||||||
|
|
@ -123,7 +146,7 @@ export function createFilter(context, type, frequency, Q, att, dec, sus, rel, fe
|
||||||
const min = clamp(2 ** -offset * frequency, 0, 20000);
|
const min = clamp(2 ** -offset * frequency, 0, 20000);
|
||||||
const max = clamp(2 ** (fenv - offset) * frequency, 0, 20000);
|
const max = clamp(2 ** (fenv - offset) * frequency, 0, 20000);
|
||||||
|
|
||||||
getParamADSR(filter.frequency, attack, decay, sustain, release, min, max, start, end);
|
getParamADSR(context, filter.frequency, attack, decay, sustain, release, min, max, start, end);
|
||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue