Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #2645 Better Faux Pickup Support #4068

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/blocks/MeterBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ function setupMeterBlocks(activity) {
*/
flow(args, logo, turtle, blk) {
const arg0 = args[0];
if (args.length !== 1 || typeof args[0] !== "number") {
if (args.length !== 1 || typeof args[0] !== "number" || arg0 < 0) {
activity.errorMsg(NOINPUTERRORMSG, blk);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion js/js-export/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ class MusicBlocks {

set PICKUP(value) {
const args = JSInterface.validateArgs("PICKUP", [value]);
Singer.MeterActions.setPickup(args[0], this.turIndex);
const validatedValue = Math.max(0, args[0]);
Singer.MeterActions.setPickup(validatedValue, this.turIndex);
}

get WHOLENOTESPLAYED() {
Expand Down
7 changes: 0 additions & 7 deletions js/notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,6 @@ class Notation {
this._notationStaging[turtle].push("pickup", beat);
this._pickupPOW2[turtle] = true;
} else {
if (this.activity.logo.runningLilypond) {
obj = rationalToFraction(factor);
this.activity.errorMsg(
_("Lilypond cannot process pickup of ") + obj[0] + "/" + obj[1]
);
}

obj = rationalToFraction(1 - factor);
for (let i = 0; i < obj[0]; i++) {
this.activity.logo.updateNotation(["R"], obj[1], turtle, false, "");
Expand Down
15 changes: 13 additions & 2 deletions js/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,9 @@ readable-fractions/681534#681534
"3/5"

*/
if (d === 0 || isNaN(d) || !isFinite(d)) {
return [0, 1];
}

let invert;
if (d > 1) {
Expand All @@ -1154,17 +1157,25 @@ readable-fractions/681534#681534

let df = 1.0;
let top = 1;
let iterations = 0
const maxIterations = 10000;
let bot = 1;

while (Math.abs(df - d) > 0.00000001) {
while (Math.abs(df - d) > 0.00000001 && iterations < maxIterations) {
if (df < d) {
top += 1;
} else {
bot += 1;
top = Math.floor(d * bot);
top = Math.round(d * bot);
}

df = top / bot;
iterations++;
}

if (iterations === maxIterations) {
//console.warn("rationalToFraction: Reached iteration limit");
return [top, bot];
}

if (bot === 0 || top === 0) {
Expand Down