Skip to content

Commit

Permalink
optimized sound
Browse files Browse the repository at this point in the history
  • Loading branch information
Elixonus committed Jul 21, 2024
1 parent 97eacaf commit 1fab58e
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions doppler/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,25 @@ for (let t = 0; t < wnum; t++) {
amph.obs[t] = null;
}

let ctxSnd = null;
let oscl = null;
let gain = null;

window.setInterval(doLgc, 30);
window.setInterval(doStep, 30);

function doLgc() {
function doStep() {
if (run === true) {
setFmod();
doTime();
}
doPhys();

if (snd === true) {
setSnd();
if (snd === true) {
setSnd();
}
}
}

function doTime() {
function doPhys() {
src.vel.x += src.acc.x;
src.vel.y += src.acc.y;
src.pos.x += src.vel.x;
src.pos.y += src.vel.y;

obs.vel.x += obs.acc.x;
obs.vel.y += obs.acc.y;
obs.pos.x += obs.vel.x;
Expand Down Expand Up @@ -461,6 +457,10 @@ function doPlot() {
plot++;
}

let ctxSnd = null;
let oscl = null;
let gain = null;

const btnTimeStrt = document.getElementById("button-time-start");
const btnTimeStop = document.getElementById("button-time-stop");
const btnBufrSave = document.getElementById("button-buffer-save");
Expand Down Expand Up @@ -523,6 +523,7 @@ function setTimeStrt() {
function setTimeStop() {
run = false;
fixTime();
setSnd();
}

function doBufrSave() {
Expand Down Expand Up @@ -726,17 +727,15 @@ function doBufrRstr() {
}

function setSndOn() {
let temp = snd;
snd = true;
fixSnd();

if (temp === false) {
if (ctxSnd === null && oscl === null && gain === null) {
ctxSnd = new window.AudioContext();
oscl = ctxSnd.createOscillator();
oscl.type = "square";
oscl.frequency.value = 50;
gain = ctxSnd.createGain();
gain.gain.value = 0;
setSnd();
oscl.connect(gain);
gain.connect(ctxSnd.destination);
Expand All @@ -745,26 +744,27 @@ function setSndOn() {
}

function setSndOff() {
let temp = snd;
snd = false;
fixSnd();

if (temp === true) {
if (ctxSnd !== null && oscl !== null && gain !== null) {
oscl.stop();
oscl.disconnect();
oscl = null;
gain.disconnect();
gain = null;
ctxSnd.close();
ctxSnd = null;
}
}

function setSnd() {
if (snd === true) {
if (run === true && obs.frq !== null && obs.amp !== null) {
oscl.frequency.linearRampToValueAtTime(Math.min(Math.max(1000 * Math.abs(obs.frq), 50), 3000), ctxSnd.currentTime + 0.03);
gain.gain.linearRampToValueAtTime(0.2 * obs.amp, ctxSnd.currentTime + 0.03);
oscl.frequency.value = Math.min(Math.max(1000 * Math.abs(obs.frq), 50), 3000);
gain.gain.value = Math.min(Math.max(0.2 * obs.amp, 0), 3);
} else {
oscl.frequency.linearRampToValueAtTime(50, ctxSnd.currentTime + 0.03);
gain.gain.linearRampToValueAtTime(0, ctxSnd.currentTime + 0.03);
gain.gain.value = 0;
}
}
}
Expand Down

0 comments on commit 1fab58e

Please sign in to comment.