Skip to content
Merged
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
83 changes: 12 additions & 71 deletions packages/alphatab/src/exporter/GpifWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GeneralMidi } from '@coderline/alphatab/midi/GeneralMidi';
import { MidiFileGenerator } from '@coderline/alphatab/midi/MidiFileGenerator';
import { MidiUtils } from '@coderline/alphatab/midi/MidiUtils';
import { AccentuationType } from '@coderline/alphatab/model/AccentuationType';
import { AccidentalType } from '@coderline/alphatab/model/AccidentalType';
import { type Automation, AutomationType } from '@coderline/alphatab/model/Automation';
import { type Bar, SustainPedalMarkerType } from '@coderline/alphatab/model/Bar';
import { BarreShape } from '@coderline/alphatab/model/BarreShape';
Expand All @@ -32,7 +31,7 @@ import type { MasterBar } from '@coderline/alphatab/model/MasterBar';
import { ModelUtils } from '@coderline/alphatab/model/ModelUtils';
import { MusicFontSymbol } from '@coderline/alphatab/model/MusicFontSymbol';
import type { Note } from '@coderline/alphatab/model/Note';
import { NoteAccidentalMode } from '@coderline/alphatab/model/NoteAccidentalMode';
import type { NoteAccidentalMode } from '@coderline/alphatab/model/NoteAccidentalMode';
import { NoteOrnament } from '@coderline/alphatab/model/NoteOrnament';
import { Ottavia } from '@coderline/alphatab/model/Ottavia';
import { PercussionMapper } from '@coderline/alphatab/model/PercussionMapper';
Expand Down Expand Up @@ -451,7 +450,8 @@ export class GpifWriter {
}
}

private static readonly _defaultSteps: string[] = ['C', 'C', 'D', 'D', 'E', 'F', 'F', 'G', 'G', 'A', 'A', 'B'];
private static readonly _steps: string[] = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
private static readonly _accidentals: string[] = ['bb', 'b', '', '#', 'x'];

private _writePitchForValue(
properties: XmlNode,
Expand All @@ -460,74 +460,15 @@ export class GpifWriter {
accidentalMode: NoteAccidentalMode,
keySignature: KeySignature
) {
let index = 0;
let octave = 0;

let step = '';
let accidental = '';

const updateParts: () => void = () => {
index = value % 12;
octave = (value / 12) | 0;

step = GpifWriter._defaultSteps[index];
switch (ModelUtils.computeAccidental(keySignature, NoteAccidentalMode.Default, value, false)) {
case AccidentalType.None:
case AccidentalType.Natural:
accidental = '';
break;
case AccidentalType.Sharp:
accidental = '#';
break;
case AccidentalType.Flat:
accidental = 'b';
break;
case AccidentalType.DoubleSharp:
accidental = 'x';
break;
case AccidentalType.DoubleFlat:
accidental = 'bb';
break;
}
};
updateParts();

switch (accidentalMode) {
case NoteAccidentalMode.Default:
break;
case NoteAccidentalMode.ForceNone:
accidental = '';
break;
case NoteAccidentalMode.ForceNatural:
accidental = '';
break;
case NoteAccidentalMode.ForceSharp:
accidental = '#';
break;
case NoteAccidentalMode.ForceDoubleSharp:
if (accidental === '#') {
value -= 2;
updateParts();
}
accidental = 'x';
break;
case NoteAccidentalMode.ForceFlat:
if (accidental === '#') {
value += 1;
updateParts();
}
accidental = 'b';
break;
case NoteAccidentalMode.ForceDoubleFlat:
if (accidental === '#') {
value += 2;
updateParts();
}
accidental = 'bb';
break;
}

this._writePitch(properties, propertyName, step, octave.toString(), accidental);
// Guitar Pro stores the spelled pitch, the octave belongs to the step (B#4 = C5, Cb5 = B4)
const spelling = ModelUtils.resolveSpelling(keySignature, value, accidentalMode);
this._writePitch(
properties,
propertyName,
GpifWriter._steps[spelling.degree],
(spelling.octave + 1).toString(),
GpifWriter._accidentals[spelling.accidentalOffset + 2]
);
}

private _writePitch(properties: XmlNode, propertyName: string, step: string, octave: string, accidental: string) {
Expand Down
54 changes: 46 additions & 8 deletions packages/alphatab/src/importer/MusicXmlImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4085,25 +4085,28 @@ export class MusicXmlImporter extends ScoreImporter {
}
}

/**
* The spelling of the note is defined by its `<pitch>`, the `<accidental>` only describes the printed sign
* which is computed during rendering. We only report signs which contradict the pitch.
* https://www.w3.org/2021/06/musicxml40/musicxml-reference/elements/accidental/
*/
private _parseAccidental(element: XmlNode, note: Note) {
// NOTE: this can currently lead to wrong notes shown,
// TODO: check partwise-complex-measures.xml where accidentals and notes get wrong
// in combination with key signatures
let accidentalMode = NoteAccidentalMode.Default;
switch (element.innerText) {
case 'sharp':
note.accidentalMode = NoteAccidentalMode.ForceSharp;
accidentalMode = NoteAccidentalMode.ForceSharp;
break;
case 'natural':
note.accidentalMode = NoteAccidentalMode.ForceNatural;
accidentalMode = NoteAccidentalMode.ForceNatural;
break;
case 'flat':
note.accidentalMode = NoteAccidentalMode.ForceFlat;
accidentalMode = NoteAccidentalMode.ForceFlat;
break;
case 'double-sharp':
note.accidentalMode = NoteAccidentalMode.ForceDoubleSharp;
accidentalMode = NoteAccidentalMode.ForceDoubleSharp;
break;
case 'flat-flat':
note.accidentalMode = NoteAccidentalMode.ForceDoubleFlat;
accidentalMode = NoteAccidentalMode.ForceDoubleFlat;
break;
// case 'sharp-sharp': Not supported
// case 'natural-sharp': Not supported
Expand Down Expand Up @@ -4147,6 +4150,17 @@ export class MusicXmlImporter extends ScoreImporter {
// Logger.warning('MusicXML', `Unsupported accidental ${element.innerText}`);
// break;
}

if (
accidentalMode !== NoteAccidentalMode.Default &&
note.accidentalMode !== NoteAccidentalMode.Default &&
accidentalMode !== note.accidentalMode
) {
Logger.warning(
'MusicXML',
`Accidental '${element.innerText}' does not match the pitch of the note, the pitch is used`
);
}
}

private _calculatePitchedNoteValue(note: Note) {
Expand Down Expand Up @@ -4214,10 +4228,34 @@ export class MusicXmlImporter extends ScoreImporter {

note.octave = (value / 12) | 0;
note.tone = value - note.octave * 12;
note.accidentalMode = MusicXmlImporter._accidentalModeForAlter(semitones);

return note;
}

/**
* The `<step>` and `<alter>` define the spelling of the note (e.g. F# vs Gb), also if no `<accidental>` is printed.
* https://www.w3.org/2021/06/musicxml40/musicxml-reference/elements/pitch/
*/
private static _accidentalModeForAlter(alter: number): NoteAccidentalMode {
if (alter === 0) {
return NoteAccidentalMode.ForceNatural;
}
if (alter === 1) {
return NoteAccidentalMode.ForceSharp;
}
if (alter === 2) {
return NoteAccidentalMode.ForceDoubleSharp;
}
if (alter === -1) {
return NoteAccidentalMode.ForceFlat;
}
if (alter === -2) {
return NoteAccidentalMode.ForceDoubleFlat;
}
return NoteAccidentalMode.Default;
}

private _applyNoteHead(
note: Note,
beatDuration: Duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import { Fingers } from '@coderline/alphatab/model/Fingers';
import { GolpeType } from '@coderline/alphatab/model/GolpeType';
import { GraceType } from '@coderline/alphatab/model/GraceType';
import { HarmonicType } from '@coderline/alphatab/model/HarmonicType';
import { KeySignature } from '@coderline/alphatab/model/KeySignature';
import { KeySignatureType } from '@coderline/alphatab/model/KeySignatureType';
import { Lyrics } from '@coderline/alphatab/model/Lyrics';
import { BeamingRules, type MasterBar } from '@coderline/alphatab/model/MasterBar';
Expand Down Expand Up @@ -3437,18 +3436,14 @@ export class AlphaTex1LanguageHandler implements IAlphaTexLanguageImportHandler
Atnf.prop(properties, 'slur', Atnf.identValue(slurId));
}

// NOTE: it would be better to check via accidentalhelper what accidentals we really need to force
const skipAccidental =
note.accidentalMode === NoteAccidentalMode.Default ||
(note.beat.voice.bar.keySignature === KeySignature.C &&
note.accidentalMode === NoteAccidentalMode.ForceNatural);

if (!skipAccidental) {
Atnf.prop(
properties,
'acc',
Atnf.identValue(ModelUtils.reverseAccidentalModeMapping.get(note.accidentalMode)!)
);
// only the spelling hints which change the rendering are needed
const accidentalMode = ModelUtils.simplifyAccidentalMode(
note.beat.voice.bar.keySignature,
note.displayValue,
note.accidentalMode
);
if (accidentalMode !== NoteAccidentalMode.Default) {
Atnf.prop(properties, 'acc', Atnf.identValue(ModelUtils.reverseAccidentalModeMapping.get(accidentalMode)!));
}

switch (note.ornament) {
Expand Down
27 changes: 27 additions & 0 deletions packages/alphatab/src/model/ModelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,33 @@ export class ModelUtils {
};
}

/**
* Returns the simplest accidental mode which results in the same rendering as the given one.
* The forced modes are only spelling hints: when the hint resolves to the spelling which
* {@link NoteAccidentalMode.Default} would choose anyhow, the hint is not needed.
* The rendering only uses the accidental mode via {@link resolveSpelling} and checks for
* {@link NoteAccidentalMode.ForceNone}. If this changes, this method has to be adapted.
* @param keySignature The key signature of the bar holding the note.
* @param noteValue The display value of the note.
* @param accidentalMode The accidental mode of the note.
*/
public static simplifyAccidentalMode(
keySignature: KeySignature,
noteValue: number,
accidentalMode: NoteAccidentalMode
): NoteAccidentalMode {
if (accidentalMode === NoteAccidentalMode.Default || accidentalMode === NoteAccidentalMode.ForceNone) {
return accidentalMode;
}

const forced = ModelUtils.resolveSpelling(keySignature, noteValue, accidentalMode);
const preferred = ModelUtils.resolveSpelling(keySignature, noteValue, NoteAccidentalMode.Default);
if (forced.degree === preferred.degree && forced.accidentalOffset === preferred.accidentalOffset) {
return NoteAccidentalMode.Default;
}
return accidentalMode;
}

public static computeAccidental(
keySignature: KeySignature,
accidentalMode: NoteAccidentalMode,
Expand Down
Loading
Loading