Created
April 11, 2024 20:19
-
-
Save tonyfast/e4e588962716e0a2e2c75bc892d10d74 to your computer and use it in GitHub Desktop.
my first sonification
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
// https://hf.co/chat/r/8qoS6Gb | |
// Set up the Web Audio API context and create an oscillator node | |
audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | |
oscillator = audioCtx.createOscillator(); | |
yourArray = [1,2,3,3,2,1] | |
// Calculate the range of pitches to use | |
minVal = Math.min(...yourArray); | |
maxVal = Math.max(...yourArray); | |
pitchRange = 12*20; // range of pitches to use | |
calcPitchForWord = (val) => { | |
return 60 + ((val - minVal) / (maxVal - minVal)) * pitchRange; | |
}; | |
// Sonify word array | |
oscillator.start(audioCtx.currentTime); | |
for (let i=0; i<yourArray.length; i++) { | |
currPitch = calcPitchForWord(yourArray[i]); | |
oscillator.frequency.setValueAtTime(currPitch, audioCtx.currentTime); | |
setTimeout(() => { | |
oscillator.stop(audioCtx.currentTime); | |
}, 500); | |
} | |
// Finally connect the oscilator to speakers or headphones | |
oscillator.connect(audioCtx.destination); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment