Skip to content

Commit

Permalink
util: play: change a bit the envelope of the notes
Browse files Browse the repository at this point in the history
The percussion instruments, as piano, should fade down more naturally.

Issue #84.
  • Loading branch information
moinejf committed Mar 25, 2018
1 parent 2f2b573 commit ca0ed08
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions util/toaudio5.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function Audio5(i_conf) {
hold: Math.pow(2, (gen.holdVolEnv ?
gen.holdVolEnv.amount : -12000) / 1200),
decay: Math.pow(2, (gen.decayVolEnv ?
gen.decayVolEnv.amount : -12000) / 1200) / 10,
gen.decayVolEnv.amount : -12000) / 1200) / 3,
sustain: gen.sustainVolEnv ?
(gen.sustainVolEnv.amount / 1000) : 0,
// release: Math.pow(2, (gen.releaseVolEnv ?
Expand All @@ -191,6 +191,12 @@ function Audio5(i_conf) {
parm.hold += parm.attack;
parm.decay += parm.hold;

// sustain > 40dB is not audible
if (parm.sustain >= .4)
parm.sustain = 0.01 // must not be null
else
parm.sustain = 1 - parm.sustain / .4

sample_cp(parm.buffer, sample)

if (gen.sampleModes && (gen.sampleModes.amount & 1)) {
Expand Down Expand Up @@ -272,17 +278,21 @@ function Audio5(i_conf) {
// o.playbackRate.setValueAtTime(parm.rate, ac.currentTime);
o.playbackRate.value = rates[instr][key];

var vol = .5;
g = ac.createGain();
g.gain.setValueAtTime(0, t);
g.gain.linearRampToValueAtTime(vol, t + parm.attack);
g.gain.setTargetAtTime((1 - parm.sustain) * vol,
t + parm.hold, parm.decay);
if (parm.hold < 0.002) {
g.gain.setValueAtTime(1, t)
} else {
if (parm.attack < 0.002) {
g.gain.setValueAtTime(1, t)
} else {
g.gain.setValueAtTime(0, t);
g.gain.linearRampToValueAtTime(1, t + parm.attack)
}
g.gain.setValueAtTime(1, t + parm.hold)
}

//fixme: does not work
// g.gain.setValueAtTime((1 - parm.sustain) * vol, t + d);
// g.gain.linearRampToValueAtTime(0,
// t + d);
g.gain.exponentialRampToValueAtTime(parm.sustain,
t + parm.decay);

o.connect(g);
g.connect(gain);
Expand Down Expand Up @@ -462,6 +472,6 @@ function Audio5(i_conf) {
else
gain_val = v;
set_cookie("volume", v.toFixed(2))
}, // set_vol()
} // set_vol()
}
} // end Audio5

0 comments on commit ca0ed08

Please sign in to comment.