52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
// let freq = 440
|
|
|
|
const tons = [264, 275, 297, 316.8, 330, 352, 371.25, 396, 412.5, 440, 475.2, 495]
|
|
// const tons = [396, 417, 528. 639, 741, 852, 963]
|
|
|
|
/*
|
|
* Play one note
|
|
*/
|
|
const note = (context, freq, counter) => {
|
|
// console.log("Note n°", counter, freq)
|
|
|
|
const oscil = context.createOscillator()
|
|
oscil.type = 'sine'
|
|
oscil.frequency.value = freq
|
|
|
|
const gainNode = context.createGain()
|
|
oscil.connect(gainNode)
|
|
gainNode.connect(context.destination)
|
|
|
|
const notesPerSecond = 5 // In Hertz
|
|
// All are in seconds
|
|
const start = counter / notesPerSecond
|
|
const duration = .5
|
|
|
|
// Increase volume
|
|
gainNode.gain.setValueAtTime(1, start)
|
|
oscil.start(start)
|
|
|
|
// Decrease volume
|
|
gainNode.gain.exponentialRampToValueAtTime(0.001, start + duration);
|
|
oscil.stop(start + duration)
|
|
}
|
|
|
|
/*
|
|
* Launch notes based on the current URL
|
|
*/
|
|
const launchNotes = (event, counter = 0, uuid = getUuidFromUrl()) => {
|
|
if (counter >= uuid.length || counter >= 5) return
|
|
|
|
const context = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
const freq = tons[parseInt(uuid[counter], 16) % tons.length]
|
|
// freq *= Math.max(1, parseInt(uuid[counter], 16)) / Math.max(1, parseInt(uuid[counter + 1], 16))
|
|
// freq = Math.min(2000, Math.round(freq))
|
|
|
|
note(context, freq, counter)
|
|
// note(context, freq * 2, counter)
|
|
|
|
launchNotes(event, counter + 1, uuid)
|
|
}
|
|
|
|
// document.onclick = launchNotes
|