Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
momentollogy committed Apr 14, 2023
1 parent f2321c0 commit ed941e8
Showing 1 changed file with 27 additions and 65 deletions.
92 changes: 27 additions & 65 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>


let playingNotes = new Set();
const sixteenthNoteDuration = 0.25; // Adjust this value according to your needs


// Map DURATION TO NOTE LENGTH
function mapDurationToNoteLength(duration, isRest) {
Expand Down Expand Up @@ -454,27 +452,10 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
});



//PLAY EVENT LISTENER
let playback;

document.getElementById('play').addEventListener('click', async () => {
document.getElementById('play').addEventListener('click', () => {
if (!playing) {
playing = true;
document.getElementById('play').innerText = "Stop";
playback = playRecordedNotes(recordedNotes);
await playback;
if (playing) {
playing = false;
document.getElementById('play').innerText = "Play";
}
} else {
playing = false;
document.getElementById('play').innerText = "Play";
stopPlayback = true;
if (playback) {
await playback;
}
playRecordedNotes(recordedNotes);
}
});

Expand All @@ -484,7 +465,6 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>




function getKeyCodeForFrequency(frequency) {
for (const [keyCode, freq] of Object.entries(frequencies)) {
if (freq === frequency) {
Expand All @@ -494,56 +474,45 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>
return null;
}

async function playRecordedNotes(notes) {
const timeBetweenNotesMs = 50;

for (const note of notes) {
const noteLengthsMs = {
"NOTE_THIRTY_SECOND": 108,
"NOTE_SIXTEENTH": 216,
"NOTE_EIGHTH": 324,
"NOTE_QUARTER": 648,
"NOTE_HALF": 1296,
"NOTE_WHOLE": 2592,
};

function noteTypeToDurationMilliseconds(noteType, tempo) {
const quarterNoteDuration = (60 / tempo) * 1000;

switch (noteType) {
case NOTE_THIRTY_SECOND:
return quarterNoteDuration / 8;
case NOTE_SIXTEENTH:
return quarterNoteDuration / 4;
case NOTE_EIGHTH:
return quarterNoteDuration / 2;
case NOTE_QUARTER:
return quarterNoteDuration;
case NOTE_HALF:
return quarterNoteDuration * 2;
case NOTE_WHOLE:
return quarterNoteDuration * 4;
default:
return 0;
}
}
console.log('Playing note:', note); // Debug: log the current note

async function playRecordedNotes(notes, tempo) {
for (const notePair of notes) {
if (stopPlayback) {
stopPlayback = false;
break;
}
const frequency = notePair[0];
const noteType = notePair[1];
const durationMilliseconds = noteTypeToDurationMilliseconds(noteType, tempo);

if (frequency === 0) {
await new Promise(resolve => setTimeout(resolve, durationMilliseconds));
if (note.rest) {
console.log(`Waiting (rest) for ${note.duration}ms`); // Debug: log the rest duration
await new Promise(resolve => setTimeout(resolve, note.duration + timeBetweenNotesMs));
} else {
const keyElement = document.querySelector(`.key[data-note="${getKeyCodeForFrequency(frequency)}"]`);
const keyElement = document.querySelector(`.key[data-note="${getKeyCodeForFrequency(note.frequency)}"]`);

if (!keyElement) {
console.error('Could not find key element for note:', frequency);
console.error('Could not find key element for note:', note);
continue;
}

const oscillator = playNote(frequency, keyElement);
const oscillator = playNote(note.frequency, keyElement);

// Stop the oscillator after the note duration and wait for it
await new Promise(resolve => setTimeout(() => {
console.log('Stopping note:', note); // Debug: log the note being stopped
oscillator.stop();
keyElement.classList.remove("pressed");
resolve();
}, durationMilliseconds));
}, note.duration));

// Wait for the time delay before proceeding to the next note
console.log(`Waiting (between notes) for ${timeBetweenNotesMs}ms`); // Debug: log the time between notes
await new Promise(resolve => setTimeout(resolve, timeBetweenNotesMs));
}
}
}
Expand All @@ -552,13 +521,6 @@ <h1>Arduino Piano Buzzer Song Creator V1 <br> <small>by momentollogy</small><br>










function quantize(value, step, precision) {
let decimalPlaces = Math.max(0, precision - Math.floor(Math.log10(step)));
let multiplier = Math.pow(10, decimalPlaces);
Expand Down

0 comments on commit ed941e8

Please sign in to comment.