Skip to content

Commit

Permalink
FIX #2645 Better Faux Pickup Support (#4068)
Browse files Browse the repository at this point in the history
* FIX #2645 Better Faux Pickup Support

Changes Made-

Added logic to handle non-negative pickup values and support "faux pickups" like 1/3.
Ensured notationPickup is called after setting the pickup value.

Validated the pickup value before passing it to MeterActions.setPickup to prevent errors.

Added input validation for pickup values in flow, ensuring only valid values are processed.

* Updating rationalToFraction function to handle all the edge cases for better error handling

* Forgot to commit the Syntax error fix

* removed warning messgae

* removed unnecesarry warning
  • Loading branch information
arth-1 authored Nov 22, 2024
1 parent eadd178 commit ec80db5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
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

0 comments on commit ec80db5

Please sign in to comment.