diff --git a/index.html b/index.html index 6db4432..0eda4b7 100644 --- a/index.html +++ b/index.html @@ -369,31 +369,35 @@

Arduino Piano Buzzer Song Creator V1
by momentollogy
// FUNCTION DISPLAYRECORDING -function displayRecording() { - let output = ""; - let songArray = ""; - - const songTitle = document.getElementById("song-title").value; - const notesCount = recordedNotes.length; +async function playRecordedNotes(recordedNotes) { + const timeBetweenNotesMs = 50; - output += `${songTitle}[${notesCount}][2] =\n{\n`; + for (const note of recordedNotes) { + console.log('Playing note:', note); // Debug: log the current note - recordedNotes.forEach((note, index) => { if (note.rest) { - output += ` {0, ${mapDurationToNoteLength(note.duration, true)}},\n`; + await new Promise(resolve => setTimeout(resolve, note.duration + timeBetweenNotesMs)); } else { - output += ` {${noteNames[note.frequency]}, ${mapDurationToNoteLength(note.duration)}},\n`; - } - }); + const keyElement = document.querySelector(`.key[data-note="${getKeyCodeForFrequency(note.frequency)}"]`); - output += "};"; - songArray = output; + if (!keyElement) { + console.error('Could not find key element for note:', note); + continue; + } - const recordingElement = document.getElementById("recording"); - recordingElement.innerHTML = `
${output}
`; - - const copySongButton = document.getElementById("copy-song"); - copySongButton.addEventListener("click", () => copySongToClipboard(output)); + const oscillator = playNote(note.frequency, keyElement); + + // Stop the oscillator after the note duration + setTimeout(() => { + console.log('Stopping note:', note); // Debug: log the note being stopped + oscillator.stop(); + keyElement.classList.remove("pressed"); + }, note.duration); + + // Wait for the note duration plus the time delay before proceeding to the next note + await new Promise(resolve => setTimeout(resolve, note.duration + timeBetweenNotesMs)); + } + } }