Skip to content

Commit

Permalink
resolves #21 - detect beat base value from starttempo
Browse files Browse the repository at this point in the history
  • Loading branch information
jeetee committed Apr 15, 2020
1 parent ddecb80 commit e180abe
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions TempoChanges.qml
Expand Up @@ -66,6 +66,16 @@ MuseScore {
segment = segment.prev;
}
if (foundTempo !== undefined) {
console.log('Found start tempo text = ' + foundTempo.text);
// Try to extract base beat
var targetBeatBaseIndex = findBeatBaseFromMarking(foundTempo);
if (targetBeatBaseIndex != -1) {
// Apply it
previousBeatIndex = targetBeatBaseIndex;
beatBase.currentIndex = targetBeatBaseIndex;
beatBaseItem = beatBase.model.get(targetBeatBaseIndex);
}
// Update input field according to the (detected) beat
startBPMvalue.placeholderText = Math.round(foundTempo.tempo * 60 / beatBaseItem.mult * 10) / 10;
}
// End Tempo
Expand All @@ -76,10 +86,56 @@ MuseScore {
segment = segment.next;
}
if (foundTempo !== undefined) {
console.log('Found end tempo text = ' + foundTempo.text);
endBPMvalue.placeholderText = Math.round(foundTempo.tempo * 60 / beatBaseItem.mult * 10) / 10;
}
}

/// Analyses tempo marking text to attempt to discover the base beat being used
/// If a beat is detected, returns the index in the beatBaseList matching the marking
/// @returns -1 if beat is not detected or not present in our beatBaseList
function findBeatBaseFromMarking(tempoMarking)
{
var metronomeMarkIndex = -1;
// First look for metronome marking symbols
var foundTempoText = tempoMarking.text.replace('<sym>space</sym>', '');
var foundMetronomeSymbols = foundTempoText.match(/(<sym>met.*<\/sym>)+/g);
if (foundMetronomeSymbols !== null) {
// Locate the index in our dropdown matching the found beatString
for (metronomeMarkIndex = beatBase.model.count; --metronomeMarkIndex >= 0; ) {
if (beatBase.model.get(metronomeMarkIndex).sym == foundMetronomeSymbols[0]) {
break; // Found this marking in the dropdown at metronomeMarkIndex
}
}
}
else {
// Metronome marking symbols are substituted with their character entity if the text was edited
// UTF-16 range [\uE1D0 - \uE1D6] (double whole - 1024th)
for (var beatString, charidx = 0; charidx < foundTempoText.length; charidx++) {
beatString = foundTempoText[charidx];
if ((beatString >= "\uE1D2") && (beatString <= "\uE1D9")) {
// Found base tempo - continue looking for augmentation dots
while (++charidx < foundTempoText.length) {
if (foundTempoText[charidx] == "\uE1E7") {
beatString += " \uE1E7";
}
else if (foundTempoText[charidx] != ' ') {
break; // No longer augmentation dots or spaces
}
}
// Locate the index in our dropdown matching the found beatString
for (metronomeMarkIndex = beatBase.model.count; --metronomeMarkIndex >= 0; ) {
if (beatBase.model.get(metronomeMarkIndex).text == beatString) {
break; // Found this marking in the dropdown at metronomeMarkIndex
}
}
break; // Done processing base tempo
}
}
}
return metronomeMarkIndex;
}

function applyTempoChanges()
{
var sel = getSelection();
Expand Down

0 comments on commit e180abe

Please sign in to comment.