acoeur/themes/acoeur/assets/js/notes.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-11-21 22:30:21 +01:00
// 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
*/
2023-05-07 17:52:17 +02:00
const note = (context, freq, counter) => {
// console.log("Note n°", counter, freq)
2022-11-21 22:30:21 +01:00
const oscil = context.createOscillator()
2022-12-25 16:58:52 +01:00
oscil.type = 'sine'
2022-11-21 22:30:21 +01:00
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
2022-12-27 23:38:12 +01:00
const duration = .5
2022-11-21 22:30:21 +01:00
// Increase volume
2022-12-27 23:38:12 +01:00
gainNode.gain.setValueAtTime(1, start)
2022-11-21 22:30:21 +01:00
oscil.start(start)
2022-12-25 16:58:52 +01:00
// Decrease volume
2022-12-27 23:38:12 +01:00
gainNode.gain.exponentialRampToValueAtTime(0.001, start + duration);
2022-11-21 22:30:21 +01:00
oscil.stop(start + duration)
}
/*
* Launch notes based on the current URL
*/
2023-05-07 17:52:17 +02:00
const launchNotes = (event, counter = 0, uuid = getUuidFromUrl()) => {
2022-11-21 22:30:21 +01:00
if (counter >= uuid.length || counter >= 5) return
2022-12-25 16:58:52 +01:00
const context = new (window.AudioContext || window.webkitAudioContext)();
2022-11-21 22:30:21 +01:00
2022-12-25 16:58:52 +01:00
const freq = tons[parseInt(uuid[counter], 16) % tons.length]
2022-11-21 22:30:21 +01:00
// 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)
2022-12-27 23:38:12 +01:00
// note(context, freq * 2, counter)
2022-11-21 22:30:21 +01:00
launchNotes(event, counter + 1, uuid)
}
2022-12-25 16:58:52 +01:00
2023-05-07 17:52:17 +02:00
// document.onclick = launchNotes