Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NonlinearFruit committed Mar 30, 2024
1 parent 634938f commit 08a3c06
Showing 1 changed file with 23 additions and 43 deletions.
66 changes: 23 additions & 43 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,11 @@ <h1>Dry Fire</h1>
</div>
</sl-card>

<style>
.card-header {
max-width: 300px;
}

.card-header [slot='header'] {
display: flex;
align-items: center;
justify-content: space-between;
}

.card-header h3 {
margin: 0;
}

.card-header sl-icon-button {
font-size: var(--sl-font-size-medium);
}
</style>

<script>
var audioCtx = new (window.AudioContext || window.webkitAudioContext || window.audioContext);
var isRunning = false;
var isDebug = true;

// https://stackoverflow.com/a/29641185
// All arguments are optional:
// duration of the tone in milliseconds. Default is 500
// frequency of the tone in hertz. default is 440
// volume of the tone. Default is 1, off is 0.
// type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
// callback to use on end of tone
function beep(duration, frequency, volume, type, callback) {
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();

oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);

if (volume){gainNode.gain.value = volume;}
if (frequency){oscillator.frequency.value = frequency;}
if (type){oscillator.type = type;}
if (callback){oscillator.onended = callback;}

oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
};

function goSignal() {
log("go signal about to beep");
beep(115, 500, 0.5);
Expand Down Expand Up @@ -136,6 +93,29 @@ <h1>Dry Fire</h1>
console.log(msg);
}
}

// https://stackoverflow.com/a/29641185
// All arguments are optional:
// duration of the tone in milliseconds. Default is 500
// frequency of the tone in hertz. default is 440
// volume of the tone. Default is 1, off is 0.
// type of tone. Possible values are sine, square, sawtooth, triangle, and custom. Default is sine.
// callback to use on end of tone
function beep(duration, frequency, volume, type, callback) {
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();

oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);

if (volume){gainNode.gain.value = volume;}
if (frequency){oscillator.frequency.value = frequency;}
if (type){oscillator.type = type;}
if (callback){oscillator.onended = callback;}

oscillator.start(audioCtx.currentTime);
oscillator.stop(audioCtx.currentTime + ((duration || 500) / 1000));
};
</script>
</body>
</html>

0 comments on commit 08a3c06

Please sign in to comment.