Skip to content

feat(alphaTex): allow specifying tempo as a float in a string #1356

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

Merged
merged 2 commits into from
Feb 25, 2024
Merged
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
12 changes: 9 additions & 3 deletions src/importer/AlphaTexImporter.ts
Original file line number Diff line number Diff line change
@@ -136,6 +136,7 @@ export class AlphaTexImporter extends ScoreImporter {
private _sy: AlphaTexSymbols = AlphaTexSymbols.No;
private _syData: unknown = "";
private _allowNegatives: boolean = false;
private _allowFloat: boolean = false;
private _allowTuning: boolean = false;
private _currentDuration: Duration = Duration.QuadrupleWhole;
private _currentDynamics: DynamicValue = DynamicValue.PPP;
@@ -548,7 +549,7 @@ export class AlphaTexImporter extends ScoreImporter {
} else if (this._ch === 0x2d /* - */) {
// negative number
// is number?
if (this._allowNegatives && this.isDigit(this._ch)) {
if (this._allowNegatives) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(we know that a dash is a digit if negatives are allowed)

this._sy = AlphaTexSymbols.Number;
this._syData = this.readNumber();
} else {
@@ -649,7 +650,8 @@ export class AlphaTexImporter extends ScoreImporter {
private isDigit(ch: number): boolean {
return (
(ch >= 0x30 && ch <= 0x39) /* 0-9 */ ||
(this._allowNegatives && ch === 0x2d /* - */) // allow minus sign if negatives
(this._allowNegatives && ch === 0x2d /* - */) || // allow minus sign if negatives
(this._allowFloat && ch === 0x2e /* . */) // allow dot if float
);
}

@@ -676,7 +678,7 @@ export class AlphaTexImporter extends ScoreImporter {
str += String.fromCharCode(this._ch);
this._ch = this.nextChar();
} while (this.isDigit(this._ch));
return parseInt(str);
return this._allowFloat ? parseFloat(str) : parseInt(str);
}

private metaData(): boolean {
@@ -726,7 +728,9 @@ export class AlphaTexImporter extends ScoreImporter {
anyMeta = true;
break;
case 'tempo':
this._allowFloat = true;
this._sy = this.newSy();
this._allowFloat = false;
if (this._sy === AlphaTexSymbols.Number) {
this._score.tempo = this._syData as number;
} else {
@@ -1878,7 +1882,9 @@ export class AlphaTexImporter extends ScoreImporter {
}
this._sy = this.newSy();
} else if (syData === 'tempo') {
this._allowFloat = true;
this._sy = this.newSy();
this._allowFloat = false;
if (this._sy !== AlphaTexSymbols.Number) {
this.error('tempo', AlphaTexSymbols.Number, true);
}
15 changes: 15 additions & 0 deletions test/importer/AlphaTexImporter.test.ts
Original file line number Diff line number Diff line change
@@ -1129,4 +1129,19 @@ describe('AlphaTexImporterTest', () => {
expect(i.message?.includes('ABC')).to.be.true;
}
});

it('tempo-as-float', () => {
const score = parseTex('\\tempo 112.5 .');
expect(score.tempo).to.equal(112.5);
});

it('tempo-as-float-in-bar', () => {
const score = parseTex('\\tempo 112 . 3.3.1 | \\tempo 333.3 3.3');
expect(score.tempo).to.equal(112);
expect(score.tracks[0].staves[0].bars[1].masterBar.tempoAutomation?.value).to.equal(333.3);
});

it('tempo-invalid-float', () => {
expect(() => parseTex('\\tempo 112.Q .')).to.throw(UnsupportedFormatError);
});
});