Js
Js
const notesEmptyStaff="B4/4/r,B4/4/r,B4/4/r,B4/4/r";
let renderNotes;
const VF=Vex.Flow;
let totalCurrentDuration=0;
document.addEventListener ('DOMContentLoaded',()=>{
createStaff(notesEmptyStaff);
document.getElementById('duration').
addEventListener ('change',function(obj){
let inputPitch=document.getElementById('pitch');
let inputAccidental=document.getElementById('accidental');
let inputOctave=document.getElementById('octave');
let inputDot=document.getElementById('dotted');
inputPitch.disabled=(this.value.includes('r'));
inputAccidental.disabled=(this.value.includes('r'));
inputOctave.disabled=(this.value.includes('r'));
inputDot.disabled=(this.value.includes('16')||this.value.includes('1'));
});
document.querySelector('#input-form').onsubmit=(e)=>{
e.preventDefault();
let inputDuration=document.getElementById('duration').value;
let inputPitch=document.getElementById('pitch').value;
let inputAccidental=document.getElementById('accidental').value;
let inputOctave=document.getElementById('octave').value;
let inputNote=inputPitch+inputAccidental+inputOctave
let isDotted=false;
if (document.getElementById('dotted').checked==true){
isDotted=true;
inputDuration+='.';
}
let note={'duration':inputDuration,'pitch':inputNote,'isDotted':isDotted };
let noteDurationInSixteenth=calculateNoteDurationInSixteenth(note);
if (noteDurationInSixteenth+totalCurrentDuration>16)
{
return false;
}
notes=[...notes,note];
let restsString=restsToComplete(notes);
let [notesString,currentDuration]=parseNotesToVex(notes);
console.log(restsString,notesString);
totalCurrentDuration=currentDuration;
renderNotes=notesString+restsString;
createStaff(renderNotes);
if (totalCurrentDuration===16){
document.getElementById('input').disabled=true;
}
console.log(notes)
}
})
function createStaff(renderNotes){
const staff=document.getElementById('staff');
while (staff.hasChildNodes())
{
staff.removeChild(staff.lastChild);
}
var vf=new VF.Factory({renderer:{elementId:'staff'}});
var score=vf.EasyScore();
var system=vf.System();
system.addStave({
voices:[score.voice(score.notes(renderNotes))]
}).addClef('treble').addTimeSignature('4/4');
vf.draw();
}
function parseNotesToVex(notes){
let notesString='';
let currentDuration=0;
for (let note of notes){
let noteString='';
let noteDuration;
if (note.duration.includes('r'))
{
noteDuration= note.duration.replace('r','');
if (note.isDotted){
noteString=noteDuration.replace ('.','');
noteString='B4/'+noteString+'/r.,';
}
else{
noteString='B4/'+noteDuration+'/r,';
}
}
else
{
noteString=note.pitch+'/'+note.duration+',';
noteDuration=note.duration;
}
notesString+=noteString;
let noteDurationInSixteenth=calculateNoteDurationInSixteenth(note);
currentDuration+=noteDurationInSixteenth;
}
return [notesString,currentDuration];
}
function calculateNoteDurationInSixteenth(note){
let noteDuration=note.duration.replace('r','');
let noteDurationInSixteenth=(16/parseInt(noteDuration));
if (note.isDotted){
noteDurationInSixteenth+=noteDurationInSixteenth/2;
}
return noteDurationInSixteenth;
function restsToComplete(notes){
let restsDuration=16;
let notesDuration=0;
for (let note of notes){
let noteDuration=calculateNoteDurationInSixteenth(note);
notesDuration+=noteDuration;
}
restsDuration-=notesDuration;
let quarterRests=Math.floor(restsDuration/4);
let sixteenthRests=restsDuration%4;
let quarterRestsString='B4/4/r,'.repeat(quarterRests);
let sixteenthRestsString='B4/16/r,'.repeat(sixteenthRests);
return quarterRestsString+sixteenthRestsString;