Created
November 11, 2021 15:15
-
-
Save danturu/3ba4bf78dece71603a7e70f4de46b53c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sampleRate = 44100; | |
const sineWaveArray = new Float32Array(sampleRate); | |
const hz = 440; | |
for (i = 0; i < sineWaveArray.length; i++) { | |
sineWaveArray[i] = Math.sin(i * Math.PI * 8 / hz); | |
} | |
function playSound({ array, sampleRate }) { | |
const audioContext = new AudioContext({sampleRate}); | |
const audioBuffer = audioContext.createBuffer(1, array.length, sampleRate); | |
audioBuffer.copyToChannel(array, 0); | |
const source = audioContext.createBufferSource(); | |
source.connect(audioContext.destination); | |
source.buffer = audioBuffer; | |
source.start(); | |
} | |
function playSineWave440hz() { | |
playSound({ array: sineWaveArray, sampleRate }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment