Skip to content

Commit

Permalink
invisible rests
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois Schwarzentruber committed Feb 12, 2024
1 parent cf656ce commit 98c3303
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
4 changes: 3 additions & 1 deletion guessRhythm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def solve(dhat, arrayOfDurations, signature):
for j in range(len(arrayOfDurations[i])):
ct.SetCoefficient(booleanVars[i][j], 1)

## add the "virtual" variable di (duration of the i-th note) multiplied by coeff to the constraint ct
def addDi(ct, i, coeff):
for j in range(len(arrayOfDurations[i])):
ct.SetCoefficient(booleanVars[i][j], coeff*arrayOfDurations[i][j])
Expand All @@ -41,7 +42,7 @@ def addDi(ct, i, coeff):

print("Number of constraints =", solver.NumConstraints())

# Create the objective function, 3 * x + y.

objective = solver.Objective()
objective.SetMinimization()

Expand All @@ -52,6 +53,7 @@ def addDi(ct, i, coeff):
ct = solver.Constraint(0, inf, "di is generally greater than dj")
addDi(ct, i, 1)
addDi(ct, j, -1)
#ct is the constraint di - dj + err >= 0
ct.SetCoefficient(errorVar, 1)
objective.SetCoefficient(errorVar, 1)

Expand Down
2 changes: 1 addition & 1 deletion js/abcd2abc.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async function abcd2abc(abcd) {
}
else {
const p = accidentalize(ppure, currentTonalityTonicMaj);
console.log("looking for the tonality")
// console.log("looking for the tonality")
return p.accidental;
}
}
Expand Down
14 changes: 12 additions & 2 deletions js/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ function durationFractionToStr(d) {
return "/".repeat(i - 2);
}

for (let num of [1, 3, 7]) {
switch(d) {
case 1.5: return "6";
case 1: return "4";
}

for (let n of [1, 3, 7]) {
for (let i = 0; i < 6; i++) {
const possibleDuration = num / (2 ** i);


const possibleDuration = n / (2 ** i);
if (d == possibleDuration) {
const num = n;
const denom = 2 ** i;

return "" + (num != 1 ? num : "") + exp2sym(num != 1 ? (i) : i);
}
}
Expand Down
21 changes: 11 additions & 10 deletions js/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Element {

function eatLetterNote(s) {
const letterNote = s[0];
if (!(["a", "b", "c", "d", "e", "f", "g", "r"].indexOf(letterNote.toLowerCase()) >= 0))
if (!(["a", "b", "c", "d", "e", "f", "g", "r", "x"].indexOf(letterNote.toLowerCase()) >= 0))
return [s, undefined];
return [s.substr(1), letterNote];
}
Expand All @@ -70,22 +70,22 @@ class Element {
if (s == "")
throw "empty string";

let letterNote = undefined;
let letter = undefined;
let accidental = undefined;

[s, accidental] = eatAccidental(s);
[s, letterNote] = eatLetterNote(s);
if (!(["a", "b", "c", "d", "e", "f", "g", "r"].indexOf(letterNote.toLowerCase()) >= 0))
[s, letter] = eatLetterNote(s);
if (!(["a", "b", "c", "d", "e", "f", "g", "r", "x"].indexOf(letter.toLowerCase()) >= 0))
throw "not a note or a rest";

if (accidental == undefined)
[s, accidental] = eatAccidental(s);

this.isRest = (letterNote == "r");
this.isRest = (letter == "r") || (letter == "x");

let value = 0;
if (!this.isRest)
value = lyNoteLetterToiNote7(letterNote.toLowerCase());
value = lyNoteLetterToiNote7(letter.toLowerCase());

if (s == ":")
throw "not a note";
Expand All @@ -94,19 +94,20 @@ class Element {
[s, octave] = eatOctaves(s);

//if lowercase
if (letterNote == letterNote.toUpperCase())
if (letter == letter.toUpperCase())
octave--;

value += octave * 7;
this.letter = letter;
this.pitch = new Pitch(value, accidental);
this.duration = new Duration(s);
}

setDuration(d) { this.duration = new Duration(d); }

toStringLy() { return (this.isRest ? "r" : this.pitch.toStringLy()) + this.duration.toString(); }
toStringABC() { return (this.isRest ? "z" : this.pitch.toStringABC()) + this.duration.toString(); }
toStringABCD() { return (this.isRest ? "z" : this.pitch.toStringABCD()) + this.duration.toString(); }
toStringLy() { return (this.isRest ? this.letter : this.pitch.toStringLy()) + this.duration.toString(); }
toStringABC() { return (this.isRest ? (this.letter == "r" ? "z" : this.letter) : this.pitch.toStringABC()) + this.duration.toString(); }
toStringABCD() { return (this.isRest ? this.letter : this.pitch.toStringABCD()) + this.duration.toString(); }
}


Expand Down
50 changes: 38 additions & 12 deletions js/rhythmguess.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class RhythmGuess {
const signatureValue = eval(signature);
console.log(`inferRhythm(${abcdStr}, ${signature})`)
abcdStr = abcdStr.trimLeft();
if (abcdStr == "") return "";

if (abcdStr == "") return "";

function tokenToElements(tokens) {

Expand Down Expand Up @@ -99,6 +99,26 @@ class RhythmGuess {






/**
*
* @param {*} elements
* @returns elements unchanged if elements contain notes or rests
* elements + a rest
*/
function addFakeRestIfMeasureIsEmpty(elements) {
if (elements.some((el) => el instanceof Element || el instanceof Chord))
return elements;
else {
const extraRest = new Element("x")
elements.push(extraRest);
extraRest.dhat = 1;
return elements;
}
}

function computePossibleDurations(elements) {
let nupletValue = undefined;
let nupletCount = undefined;
Expand Down Expand Up @@ -178,13 +198,15 @@ class RhythmGuess {

}


//main
try {
const tokens = tokenize(abcdStr);
const elements = tokenToElements(tokens);
const elements = addFakeRestIfMeasureIsEmpty(tokenToElements(tokens));
const possibleDurations = computePossibleDurations(elements);
console.log(abcdStr)
console.log("elements", elements)
console.log("possibleDurations", possibleDurations)
// console.log("elements", elements)
// console.log("possibleDurations", possibleDurations)


const durationsSolution = await solve(elements.map((e) => e.dhat), possibleDurations, signatureValue);
Expand Down Expand Up @@ -214,6 +236,10 @@ function tokenize(abcdStr) {
let bracketType = "[";
let bracketToken = "";

abcdStr = abcdStr.replaceAll("(", " ( ");
abcdStr = abcdStr.replaceAll(")", " ) ");
abcdStr = abcdStr.replaceAll("-", " - ");

const L = abcdStr.split(" ");
console.log(L)

Expand Down Expand Up @@ -361,8 +387,8 @@ async function solve(dhats, possibleDurations, signatureValue) {



/*
async function solve(dhats, D, signature) {

async function solveQuickAndDirty(dhats, D, signature) {
console.log("dhats", dhats)
const solution = [];
function solveRec(D, i, subTotal) {
Expand Down Expand Up @@ -401,11 +427,11 @@ async function solve(dhats, D, signature) {
newD[i] = up(newD[i], [solution[j]]);
console.log(newD[i])
}
else if (dhats[i] > dhats[j])
newD[i] = up(newD[i], D[i].filter((d) => d >= solution[j]));
else if (dhats[i] < dhats[j])
newD[i] = up(newD[i], D[i].filter((d) => d <= solution[j]));
else if (dhats[i] > dhats[j])
newD[i] = up(newD[i], D[i].filter((d) => d >= solution[j]));
else if (dhats[i] < dhats[j])
newD[i] = up(newD[i], D[i].filter((d) => d <= solution[j]));


for (const v of newD[i]) {
solution[i] = v;
Expand All @@ -421,4 +447,4 @@ async function solve(dhats, D, signature) {
return solution;
else
throw "impossible to solve";
}*/
}

0 comments on commit 98c3303

Please sign in to comment.