Skip to content

Commit

Permalink
Add volume control to playNote function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackiBoiz authored Sep 19, 2024
1 parent 0311b7e commit 576b655
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions letters.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,29 @@ for (let i = 1; i < 10; i++) {
}

async function playNote(frequency, duration, ctx, ops = {}) {
var o = ctx.createOscillator()
var g = ctx.createGain()
var o = ctx.createOscillator();
var g = ctx.createGain();

o.frequency.value = frequency;
o.type = ops.type || 'sine';
o.connect(g)
g.connect(ctx.destination)
o.connect(g);
g.connect(ctx.destination);

const volume = (ops.volume !== undefined ? ops.volume : 100) / 100;
o.start();

const endTime = ctx.currentTime + duration*2/1000;
o.stop(endTime)
const endTime = ctx.currentTime + duration * 2 / 1000;
o.stop(endTime);

g.gain.setValueAtTime(0, ctx.currentTime);
g.gain.linearRampToValueAtTime(.2, ctx.currentTime + duration/5/1000);
g.gain.exponentialRampToValueAtTime(0.00001, ctx.currentTime + duration/1000)
g.gain.linearRampToValueAtTime(0, ctx.currentTime + duration*2/1000) // does this ramp from the last ramp
g.gain.linearRampToValueAtTime(volume * 0.2, ctx.currentTime + duration / 5 / 1000);
g.gain.exponentialRampToValueAtTime(0.00001, ctx.currentTime + duration / 1000);
g.gain.linearRampToValueAtTime(0, ctx.currentTime + duration * 2 / 1000);
o.onended = () => {
g.disconnect();
};
}


function getLetters(ops) {
const type = ops.type ?? "sine";
const volume = ops.volume ?? 100;
Expand All @@ -75,4 +78,3 @@ function getLetters(ops) {
}

export { getLetters };

0 comments on commit 576b655

Please sign in to comment.