Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Created April 11, 2024 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyfast/e4e588962716e0a2e2c75bc892d10d74 to your computer and use it in GitHub Desktop.
Save tonyfast/e4e588962716e0a2e2c75bc892d10d74 to your computer and use it in GitHub Desktop.
my first sonification
// 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