Skip to content

Instantly share code, notes, and snippets.

@ryosuzuki
Created November 2, 2018 00:01
Show Gist options
  • Save ryosuzuki/60404fb60a4605184bce1f311d5c2825 to your computer and use it in GitHub Desktop.
Save ryosuzuki/60404fb60a4605184bce1f311d5c2825 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>High frequency sound generator</title>
<script>
var WAcontext, oscillator, isPlay;
initSound();
function initSound() {
WAcontext = new AudioContext || new webkitAudioContext;
oscillator = WAcontext.createOscillator();
oscillator.connect(WAcontext.destination);
isPlay = false;
}
function startSound(hertz) {
if (hertz === undefined) {
var hertz = parseInt(document.getElementById('hertz').value);
if (!hertz) {
return false;
}
}
if (isPlay) {
stopSound();
}
oscillator.type = 0;
oscillator.frequency.value = hertz;
oscillator.start(0);
isPlay = true;
}
function stopSound() {
oscillator.stop(0);
initSound();
}
</script>
</head>
<body>
<h1>High frequency sound maker</h1>
<div class='soundFormArea'>
<input type="text" id='hertz' value='442'> Hz
<input type='button' onclick='startSound()' value='sound on'>
<input type='button' onclick='stopSound()' value='sound off'>
</div><!-- /soundFormArea -->
<div class='exampleArea'>
<span>Example)</span>
<input type='button' onclick='startSound(440)' value='440 Hz'>
<input type='button' onclick='startSound(2000)' value='2,000 Hz'>
<input type='button' onclick='startSound(12000)' value='12,000 Hz'>
</div><!-- /exampleArea -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment