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
1 change: 1 addition & 0 deletions packages/alphatab/src/generated/model/BeatCloner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class BeatCloner {
clone.overrideDisplayDuration = original.overrideDisplayDuration;
clone.golpe = original.golpe;
clone.dynamics = original.dynamics;
clone.showDynamics = original.showDynamics;
clone.invertBeamDirection = original.invertBeamDirection;
clone.preferredBeamDirection = original.preferredBeamDirection;
clone.isEffectSlurOrigin = original.isEffectSlurOrigin;
Expand Down
4 changes: 4 additions & 0 deletions packages/alphatab/src/generated/model/BeatSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class BeatSerializer {
o.set("overridedisplayduration", obj.overrideDisplayDuration);
o.set("golpe", obj.golpe as number);
o.set("dynamics", obj.dynamics as number);
o.set("showdynamics", obj.showDynamics);
o.set("invertbeamdirection", obj.invertBeamDirection);
o.set("preferredbeamdirection", obj.preferredBeamDirection as number | null);
o.set("beamingmode", obj.beamingMode as number);
Expand Down Expand Up @@ -256,6 +257,9 @@ export class BeatSerializer {
case "dynamics":
obj.dynamics = JsonHelper.parseEnum<DynamicValue>(v, DynamicValue)!;
return true;
case "showdynamics":
obj.showDynamics = v as boolean | undefined;
return true;
case "invertbeamdirection":
obj.invertBeamDirection = v! as boolean;
return true;
Expand Down
106 changes: 98 additions & 8 deletions packages/alphatab/src/importer/MusicXmlImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ interface MusicXmlHarmonicGroup {
* @internal
*/
class StaffContext {
public currentDynamics = DynamicValue.F;
/**
* The dynamics of a direction, shown on the next note of the staff (and of the voice, if given).
*/
public pendingDynamics: DynamicValue | null = null;
public pendingDynamicsVoice: string = '';
public lastDynamics: DynamicValue | null = null;
public transpose: number = 0;
public isExplicitlyBeamed = false;
}
Expand Down Expand Up @@ -283,7 +288,6 @@ export class MusicXmlImporter extends ScoreImporter {
* Whether the exporter writes the stop of octave shifts before the last shifted note instead of after it.
*/
private _octaveShiftEndsBeforeLastNote: boolean = false;
private _currentDynamics = DynamicValue.F;

public get name(): string {
return 'MusicXML';
Expand All @@ -303,6 +307,7 @@ export class MusicXmlImporter extends ScoreImporter {

this._parseDom(dom);
this._applySpans();
this._applyDynamics();
ModelUtils.consolidate(this._score);
this._score.finish(this.settings);
this._score.rebuildRepeatGroups();
Expand Down Expand Up @@ -2263,7 +2268,9 @@ export class MusicXmlImporter extends ScoreImporter {
case 'dynamics':
const newDynamics = this._parseDynamics(direction);
if (newDynamics !== null) {
this._currentDynamics = newDynamics;
const context = this._getStaffContext(staff);
context.pendingDynamics = newDynamics;
context.pendingDynamicsVoice = voice;
this._score.stylesheet.hideDynamics = false;
}
break;
Expand Down Expand Up @@ -2653,6 +2660,15 @@ export class MusicXmlImporter extends ScoreImporter {
beat.addNote(note!);
} else {
beat = this._createBeat(staff, masterBar, voiceRaw, note);
const context = this._getStaffContext(staff);
if (
note !== null &&
context.pendingDynamics !== null &&
(context.pendingDynamicsVoice.length === 0 || context.pendingDynamicsVoice === voiceRaw)
) {
this._markDynamics(context, beat, context.pendingDynamics!, false);
context.pendingDynamics = null;
}
}

if (note !== null) {
Expand Down Expand Up @@ -2995,7 +3011,6 @@ export class MusicXmlImporter extends ScoreImporter {
// need an empty placeholder beat for the gap
if (gap > 0) {
const placeholder = new Beat();
placeholder.dynamics = this._currentDynamics;
placeholder.isEmpty = true;
placeholder.duration = Duration.TwoHundredFiftySixth; // smallest we have
placeholder.overrideDisplayDuration = gap;
Expand All @@ -3013,7 +3028,6 @@ export class MusicXmlImporter extends ScoreImporter {
this._beats.push(newBeat);
this._harmonicGroups.clear();
newBeat.isEmpty = false;
newBeat.dynamics = this._currentDynamics;

const automations = this._nextBeatAutomations;
this._nextBeatAutomations = null;
Expand Down Expand Up @@ -3516,7 +3530,6 @@ export class MusicXmlImporter extends ScoreImporter {
}

const placeholder = new Beat();
placeholder.dynamics = this._currentDynamics;
placeholder.isEmpty = false;
placeholder.duration = preferredDuration;
placeholder.overrideDisplayDuration = preferredDurationTicks;
Expand Down Expand Up @@ -3710,8 +3723,8 @@ export class MusicXmlImporter extends ScoreImporter {
case 'dynamics':
const dynamics = this._parseDynamics(c);
if (dynamics !== null) {
beat.dynamics = dynamics;
this._currentDynamics = dynamics;
this._markDynamics(this._getStaffContext(beat.voice.bar.staff), beat, dynamics, true);
this._score.stylesheet.hideDynamics = false;
}
break;
case 'fermata':
Expand Down Expand Up @@ -3747,6 +3760,83 @@ export class MusicXmlImporter extends ScoreImporter {
}
}

/**
* A note-attached marking repeating the previous one of the staff is not shown, as some exporters (e.g. Guitar Pro)
* write the dynamics on every note.
*/
private _markDynamics(context: StaffContext, beat: Beat, value: DynamicValue, isNoteAttached: boolean) {
beat.dynamics = value;
beat.showDynamics = !isNoteAttached || context.lastDynamics !== value;
context.lastDynamics = value;
}

/**
* Carries the dynamics markings forward in time to all beats of their part, e.g. a single marking for both hands
* of a piano. Of markings at the same position, the one on the own staff wins.
*/
private _applyDynamics() {
if (this._score.stylesheet.hideDynamics) {
return;
}
for (const track of this._score.tracks) {
const markings: Beat[] = [];
for (const staff of track.staves) {
for (const bar of staff.bars) {
for (const voice of bar.voices) {
for (const beat of voice.beats) {
if (beat.showDynamics !== undefined) {
markings.push(beat);
}
}
}
}
}

for (const staff of track.staves) {
const ordered = markings.slice();
ordered.sort((a, b) => {
const position = MusicXmlImporter._comparePosition(a, b);
if (position !== 0) {
return position;
}
return (a.voice.bar.staff === staff ? 1 : 0) - (b.voice.bar.staff === staff ? 1 : 0);
});

// per voice: index of the next marking and the dynamics in effect
const nextMarking: number[] = [];
const current: DynamicValue[] = [];
for (const bar of staff.bars) {
for (const voice of bar.voices) {
while (nextMarking.length <= voice.index) {
nextMarking.push(0);
current.push(DynamicValue.F);
}
for (const beat of voice.beats) {
let next = nextMarking[voice.index];
while (
next < ordered.length &&
MusicXmlImporter._comparePosition(ordered[next], beat) <= 0
) {
current[voice.index] = ordered[next].dynamics;
next++;
}
nextMarking[voice.index] = next;
if (beat.showDynamics === undefined) {
beat.dynamics = current[voice.index];
beat.showDynamics = false;
}
}
}
}
}
}
}

private static _comparePosition(a: Beat, b: Beat): number {
const bar = a.voice.bar.index - b.voice.bar.index;
return bar !== 0 ? bar : a.displayStart - b.displayStart;
}

private _getStaffContext(staff: Staff) {
if (!this._staffToContext.has(staff)) {
const context = new StaffContext();
Expand Down
13 changes: 13 additions & 0 deletions packages/alphatab/src/model/Beat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,16 @@ export class Beat {
*/
public dynamics: DynamicValue = DynamicValue.F;

/**
* Whether a dynamics marking with the {@link dynamics} of this beat is shown.
* @remarks
* If not set, a marking is shown where the dynamics change.
* Formats encoding the written markings (e.g. MusicXML) set it, as the dynamics in effect can differ
* from the shown markings: e.g. a single marking on a piano applies to both staves.
* @since 1.9.0
*/
public showDynamics?: boolean;

/**
* Gets or sets a value indicating whether the beam direction should be inverted.
*/
Expand Down Expand Up @@ -1185,6 +1195,9 @@ export class Beat {
const cloneBeat: Beat = BeatCloner.clone(this);
cloneBeat.id = Beat._globalBeatId++;
cloneBeat.pickStroke = PickStroke.None;
if (cloneBeat.showDynamics === true) {
cloneBeat.showDynamics = false;
}
for (let i: number = 0, j: number = cloneBeat.notes.length; i < j; i++) {
const cloneNote: Note = cloneBeat.notes[i];
const note: Note = this.notes[i];
Expand Down
4 changes: 4 additions & 0 deletions packages/alphatab/src/rendering/effects/DynamicsEffectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function shouldCreateDynamicsGlyph(beat: Beat): boolean {
return false;
}

if (beat.showDynamics !== undefined) {
return beat.showDynamics!;
}

const previousBeat = getPreviousDynamicsBeat(beat);

let show: boolean = (beat.voice.index === 0 && !previousBeat) || beat.dynamics !== previousBeat?.dynamics;
Expand Down
Binary file modified packages/alphatab/test-data/musicxml-samples/BeetAnGeSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/BrahWiMeSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/DebuMandSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/Dichterliebe01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/Echigo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/FaurReveSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/MahlFaGe4Sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/MozaChloSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/MozaVeilSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/MozartTrio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-samples/SchbAvMaSample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-testsuite/31a-Directions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-testsuite/34b-Colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml-testsuite/34c-Font-Size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/alphatab/test-data/musicxml3/chord-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading