diff --git a/src.compiler/AstPrinterBase.ts b/src.compiler/AstPrinterBase.ts index 0ae50fedb..71ca98236 100644 --- a/src.compiler/AstPrinterBase.ts +++ b/src.compiler/AstPrinterBase.ts @@ -35,7 +35,6 @@ export default abstract class AstPrinterBase { } protected writeLine(txt?: string) { - this.writeIndent(); if (txt) { this.write(txt); } diff --git a/src/importer/AlphaTexImporter.ts b/src/importer/AlphaTexImporter.ts index fc381028e..5625682a0 100644 --- a/src/importer/AlphaTexImporter.ts +++ b/src/importer/AlphaTexImporter.ts @@ -570,7 +570,7 @@ export class AlphaTexImporter extends ScoreImporter { * @returns true if the given character is a digit, otherwise false. */ private isDigit(code: number): boolean { - return (code >= 0x30 && code <= 0x39) /*0-9*/ || (code === 0x2d /* - */ && this._allowNegatives); // allow - if negatives + return (code >= 0x30 && code <= 0x39) /* 0-9 */ || (code === 0x2d /* - */ && this._allowNegatives); // allow - if negatives } /** @@ -582,7 +582,7 @@ export class AlphaTexImporter extends ScoreImporter { do { str += String.fromCharCode(this._ch); this._ch = this.nextChar(); - } while (AlphaTexImporter.isLetter(this._ch) || this.isDigit(this._ch) || this._ch === 0x23); + } while (AlphaTexImporter.isLetter(this._ch) || this.isDigit(this._ch) || this._ch === 0x23 /* # */); return str; } @@ -614,74 +614,44 @@ export class AlphaTexImporter extends ScoreImporter { let anyMeta: boolean = false; let continueReading: boolean = true; while (this._sy === AlphaTexSymbols.MetaCommand && continueReading) { - let syData: string = (this._syData as string).toLowerCase(); - switch (syData) { + let metadataTag: string = (this._syData as string).toLowerCase(); + switch (metadataTag) { case 'title': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.title = (this._syData as string); - } else { - this.error('title', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'subtitle': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.subTitle = (this._syData as string); - } else { - this.error('subtitle', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'artist': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.artist = (this._syData as string); - } else { - this.error('artist', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'album': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.album = (this._syData as string); - } else { - this.error('album', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'words': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.words = (this._syData as string); - } else { - this.error('words', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'music': - this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.music = (this._syData as string); - } else { - this.error('music', AlphaTexSymbols.String, true); - } - this._sy = this.newSy(); - anyMeta = true; - break; case 'copyright': this._sy = this.newSy(); - if (this._sy === AlphaTexSymbols.String) { - this._score.copyright = (this._syData as string); - } else { - this.error('copyright', AlphaTexSymbols.String, true); + if (this._sy !== AlphaTexSymbols.String) { + // Known issue: Strings that happen to be parsed as valid Tunings or positive Numbers will not pass this. + // Need to use quotes in that case, or rewrite parsing logic. + this.error(metadataTag, AlphaTexSymbols.String, true); + } + let metadataValue: string = (this._syData as string); + switch (metadataTag) { + case 'title': + this._score.title = metadataValue; + break; + case 'subtitle': + this._score.subTitle = metadataValue; + break; + case 'artist': + this._score.artist = metadataValue; + break; + case 'album': + this._score.album = metadataValue; + break; + case 'words': + this._score.words = metadataValue; + break; + case 'music': + this._score.music = metadataValue; + break; + case 'copyright': + this._score.copyright = metadataValue; + break; } this._sy = this.newSy(); anyMeta = true; @@ -722,8 +692,7 @@ export class AlphaTexImporter extends ScoreImporter { } private handleStaffMeta(): boolean { - let syData: string = (this._syData as string).toLowerCase(); - switch (syData) { + switch ((this._syData as string).toLowerCase()) { case 'capo': this._sy = this.newSy(); if (this._sy === AlphaTexSymbols.Number) { @@ -772,7 +741,7 @@ export class AlphaTexImporter extends ScoreImporter { this._staffTuningApplied = false; if (this._sy === AlphaTexSymbols.Number) { let instrument: number = this._syData as number; - if (instrument >= 0 && instrument <= 128) { + if (instrument >= 0 && instrument <= 127) { this._currentTrack.playbackInfo.program = this._syData as number; } else { this.error('instrument', AlphaTexSymbols.Number, false); @@ -1223,66 +1192,35 @@ export class AlphaTexImporter extends ScoreImporter { let syData: string = (this._syData as string).toLowerCase(); if (syData === 'f') { beat.fadeIn = true; - this._sy = this.newSy(); - return true; - } - if (syData === 'v') { + } else if (syData === 'v') { beat.vibrato = VibratoType.Slight; - this._sy = this.newSy(); - return true; - } - if (syData === 's') { + } else if (syData === 's') { beat.slap = true; - this._sy = this.newSy(); - return true; - } - if (syData === 'p') { + } else if (syData === 'p') { beat.pop = true; - this._sy = this.newSy(); - return true; - } - if (syData === 'tt') { + } else if (syData === 'tt') { beat.tap = true; - this._sy = this.newSy(); - return true; - } - if (syData === 'dd') { + } else if (syData === 'dd') { beat.dots = 2; - this._sy = this.newSy(); - return true; - } - if (syData === 'd') { + } else if (syData === 'd') { beat.dots = 1; - this._sy = this.newSy(); - return true; - } - if (syData === 'su') { + } else if (syData === 'su') { beat.pickStroke = PickStroke.Up; - this._sy = this.newSy(); - return true; - } - if (syData === 'sd') { + } else if (syData === 'sd') { beat.pickStroke = PickStroke.Down; - this._sy = this.newSy(); - return true; - } - if (syData === 'tu') { + } else if (syData === 'tu') { this._sy = this.newSy(); if (this._sy !== AlphaTexSymbols.Number) { this.error('tuplet', AlphaTexSymbols.Number, true); return false; } this.applyTuplet(beat, this._syData as number); - this._sy = this.newSy(); - return true; - } - if (syData === 'tb' || syData === 'tbe') { + } else if (syData === 'tb' || syData === 'tbe') { let exact: boolean = syData === 'tbe'; // read points this._sy = this.newSy(); if (this._sy !== AlphaTexSymbols.LParensis) { this.error('tremolobar-effect', AlphaTexSymbols.LParensis, true); - return false; } this._allowNegatives = true; this._sy = this.newSy(); @@ -1292,19 +1230,16 @@ export class AlphaTexImporter extends ScoreImporter { if (exact) { if (this._sy !== AlphaTexSymbols.Number) { this.error('tremolobar-effect', AlphaTexSymbols.Number, true); - return false; } offset = this._syData as number; this._sy = this.newSy(); if (this._sy !== AlphaTexSymbols.Number) { this.error('tremolobar-effect', AlphaTexSymbols.Number, true); - return false; } value = this._syData as number; } else { if (this._sy !== AlphaTexSymbols.Number) { this.error('tremolobar-effect', AlphaTexSymbols.Number, true); - return false; } offset = 0; value = this._syData as number; @@ -1326,20 +1261,14 @@ export class AlphaTexImporter extends ScoreImporter { i++; } } else { - beat.whammyBarPoints.sort((a, b) => { - return a.offset - b.offset; - }); + beat.whammyBarPoints.sort((a, b) => a.offset - b.offset); } } this._allowNegatives = false; if (this._sy !== AlphaTexSymbols.RParensis) { this.error('tremolobar-effect', AlphaTexSymbols.RParensis, true); - return false; } - this._sy = this.newSy(); - return true; - } - if (syData === 'bu' || syData === 'bd' || syData === 'au' || syData === 'ad') { + } else if (syData === 'bu' || syData === 'bd' || syData === 'au' || syData === 'ad') { switch (syData) { case 'bu': beat.brushType = BrushType.BrushUp; @@ -1369,8 +1298,7 @@ export class AlphaTexImporter extends ScoreImporter { beat.brushDuration = beat.playbackDuration / beat.notes.length; } return true; - } - if (syData === 'ch') { + } else if (syData === 'ch') { this._sy = this.newSy(); let chordName: string = (this._syData as string); let chordId: string = this.getChordId(this._currentStaff, chordName); @@ -1381,10 +1309,7 @@ export class AlphaTexImporter extends ScoreImporter { this._currentStaff.addChord(chordId, chord); } beat.chordId = chordId; - this._sy = this.newSy(); - return true; - } - if (syData === 'gr') { + } else if (syData === 'gr') { this._sy = this.newSy(); if ((this._syData as string).toLowerCase() === 'ob') { beat.graceType = GraceType.OnBeat; @@ -1396,9 +1321,8 @@ export class AlphaTexImporter extends ScoreImporter { beat.graceType = GraceType.BeforeBeat; } return true; - } - if (syData === 'dy') { - this.newSy(); + } else if (syData === 'dy') { + this._sy = this.newSy(); switch ((this._syData as string).toLowerCase()) { case 'ppp': beat.dynamics = DynamicValue.PPP; @@ -1426,43 +1350,39 @@ export class AlphaTexImporter extends ScoreImporter { break; } this._currentDynamics = beat.dynamics; - this.newSy(); - return true; - } - if (syData === 'cre') { + } else if (syData === 'cre') { beat.crescendo = CrescendoType.Crescendo; - this.newSy(); - return true; - } - if (syData === 'dec') { + } else if (syData === 'dec') { beat.crescendo = CrescendoType.Decrescendo; - this.newSy(); - return true; - } - if (syData === 'tp') { + } else if (syData === 'tp') { this._sy = this.newSy(); - let duration: Duration = Duration.Eighth; + beat.tremoloSpeed = Duration.Eighth; if (this._sy === AlphaTexSymbols.Number) { switch (this._syData as number) { case 8: - duration = Duration.Eighth; + beat.tremoloSpeed = Duration.Eighth; break; case 16: - duration = Duration.Sixteenth; + beat.tremoloSpeed = Duration.Sixteenth; break; case 32: - duration = Duration.ThirtySecond; + beat.tremoloSpeed = Duration.ThirtySecond; break; default: - duration = Duration.Eighth; + beat.tremoloSpeed = Duration.Eighth; break; } this._sy = this.newSy(); } - beat.tremoloSpeed = duration; return true; + } else { + // string didn't match any beat effect syntax + return false; } - return false; + // default behaviour when a beat effect above + // does not handle new symbol + return on its own + this._sy = this.newSy(); + return true; } private getChordId(currentStaff: Staff, chordName: string): string { diff --git a/src/midi/GeneralMidi.ts b/src/midi/GeneralMidi.ts index 27eee7a29..82556fd0f 100644 --- a/src/midi/GeneralMidi.ts +++ b/src/midi/GeneralMidi.ts @@ -41,7 +41,7 @@ export class GeneralMidi { if (!GeneralMidi._values) { GeneralMidi._values = new Map(); } - name = name.toLowerCase().split(' ').join(''); + name = name.toLowerCase().replaceAll(' ', ''); return GeneralMidi._values.has(name) ? GeneralMidi._values.get(name)! : 0; } diff --git a/src/model/ModelUtils.ts b/src/model/ModelUtils.ts index 6f97a1db9..c8b52e497 100644 --- a/src/model/ModelUtils.ts +++ b/src/model/ModelUtils.ts @@ -121,7 +121,6 @@ export class ModelUtils { /** * Checks if the given string is a tuning inticator. * @param name - * @returns */ public static isTuning(name: string): boolean { return !!ModelUtils.parseTuning(name); @@ -132,13 +131,13 @@ export class ModelUtils { let octave: string = ''; for (let i: number = 0; i < name.length; i++) { let c: number = name.charCodeAt(i); - if (c >= 0x30 && c <= 0x39) { + if (c >= 0x30 && c <= 0x39 /* 0-9 */) { // number without note? if (!note) { return null; } octave += String.fromCharCode(c); - } else if ((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x23) { + } else if ((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x23) /* A-Za-Z# */ { note += String.fromCharCode(c); } else { return null; @@ -163,53 +162,39 @@ export class ModelUtils { } public static getToneForText(note: string): number { - let b: number = 0; switch (note.toLowerCase()) { case 'c': - b = 0; - break; + return 0; case 'c#': case 'db': - b = 1; - break; + return 1; case 'd': - b = 2; - break; + return 2; case 'd#': case 'eb': - b = 3; - break; + return 3; case 'e': - b = 4; - break; + return 4; case 'f': - b = 5; - break; + return 5; case 'f#': case 'gb': - b = 6; - break; + return 6; case 'g': - b = 7; - break; + return 7; case 'g#': case 'ab': - b = 8; - break; + return 8; case 'a': - b = 9; - break; + return 9; case 'a#': case 'bb': - b = 10; - break; + return 10; case 'b': - b = 11; - break; + return 11; default: return 0; } - return b; } public static newGuid(): string {