Skip to content
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

feat: Add Sustain pedal reading and rendering #1741

Merged
merged 1 commit into from
Nov 10, 2024
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
4 changes: 3 additions & 1 deletion src/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { NumberedBarRendererFactory } from './rendering/NumberedBarRendererFacto
import { FreeTimeEffectInfo } from './rendering/effects/FreeTimeEffectInfo';
import { ScoreBarRenderer } from './rendering/ScoreBarRenderer';
import { TabBarRenderer } from './rendering/TabBarRenderer';
import { SustainPedalEffectInfo } from './rendering/effects/SustainPedalEffectInfo';

export class LayoutEngineFactory {
public readonly vertical: boolean;
Expand Down Expand Up @@ -525,7 +526,8 @@ export class Environment {
new EffectBarRendererFactory(Environment.StaffIdBeforeNumberedAlways, [
new CrescendoEffectInfo(),
new OttaviaEffectInfo(false),
new DynamicsEffectInfo()
new DynamicsEffectInfo(),
new SustainPedalEffectInfo()
]),
// no before-numbered-hideable
new NumberedBarRendererFactory(),
Expand Down
7 changes: 6 additions & 1 deletion src/NotationSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,12 @@ export enum NotationElement {
/**
* The "Free time" text shown above the staff.
*/
EffectFreeTime
EffectFreeTime,

/**
* The Sustain pedal effect shown above the staff "Ped.____*"
*/
EffectSustainPedal
}

/**
Expand Down
39 changes: 30 additions & 9 deletions src/exporter/GpifWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GeneralMidi } from '@src/midi/GeneralMidi';
import { MidiUtils } from '@src/midi/MidiUtils';
import { AccentuationType } from '@src/model/AccentuationType';
import { AutomationType } from '@src/model/Automation';
import { Bar } from '@src/model/Bar';
import { Bar, SustainPedalMarkerType } from '@src/model/Bar';
import { Beat } from '@src/model/Beat';
import { BendPoint } from '@src/model/BendPoint';
import { BrushType } from '@src/model/BrushType';
Expand Down Expand Up @@ -390,7 +390,7 @@ export class GpifWriter {
this.writeSimplePropertyNode(properties, 'String', 'String', (note.string - 1).toString());
this.writeSimplePropertyNode(properties, 'Fret', 'Fret', note.fret.toString());
this.writeSimplePropertyNode(properties, 'Midi', 'Number', note.realValue.toString());
if(note.showStringNumber) {
if (note.showStringNumber) {
this.writeSimplePropertyNode(properties, 'ShowStringNumber', 'Enable', null);
}
}
Expand Down Expand Up @@ -737,7 +737,7 @@ export class GpifWriter {
}

beatNode.addElement('ConcertPitchStemOrientation').innerText = 'Undefined';
if(beat.slashed) {
if (beat.slashed) {
beatNode.addElement('Slashed');
}
if (!beat.isRest) {
Expand Down Expand Up @@ -953,7 +953,7 @@ export class GpifWriter {
masterTrackNode.addElement('Anacrusis');
}

if(score.masterBars[0].tempoAutomations.length === 0){
if (score.masterBars[0].tempoAutomations.length === 0) {
const initialTempoAutomation = automations.addElement('Automation');
initialTempoAutomation.addElement('Type').innerText = 'Tempo';
initialTempoAutomation.addElement('Linear').innerText = 'false';
Expand All @@ -965,7 +965,7 @@ export class GpifWriter {
initialTempoAutomation.addElement('Text').innerText = score.tempoLabel;
}
}

for (const mb of score.masterBars) {
for (const automation of mb.tempoAutomations) {
const tempoAutomation = automations.addElement('Automation');
Expand Down Expand Up @@ -1135,6 +1135,29 @@ export class GpifWriter {
}
}
}

for (const s of track.staves) {
for (const b of s.bars) {
for (const sustainPedal of b.sustainPedals) {
if (sustainPedal.pedalType !== SustainPedalMarkerType.Hold) {
const automation = automationsNode.addElement('Automation');
automation.addElement('Type').innerText = 'SustainPedal';
automation.addElement('Linear').innerText = 'false';
automation.addElement('Bar').innerText = b.index.toString();
automation.addElement('Position').innerText = sustainPedal.ratioPosition.toString();
automation.addElement('Visible').innerText = 'true';
switch (sustainPedal.pedalType) {
case SustainPedalMarkerType.Down:
automation.addElement('Value').innerText = `0 1`;
break;
case SustainPedalMarkerType.Up:
automation.addElement('Value').innerText = `0 3`;
break;
}
}
}
}
}
}

private writeMidiConnectionNode(trackNode: XmlNode, track: Track) {
Expand Down Expand Up @@ -1557,10 +1580,8 @@ export class GpifWriter {
'Time'
).innerText = `${masterBar.timeSignatureNumerator}/${masterBar.timeSignatureDenominator}`;

if(masterBar.isFreeTime) {
masterBarNode.addElement(
'FreeTime'
);
if (masterBar.isFreeTime) {
masterBarNode.addElement('FreeTime');
}

let bars: string[] = [];
Expand Down
11 changes: 11 additions & 0 deletions src/generated/model/BarSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import { Bar } from "@src/model/Bar";
import { JsonHelper } from "@src/io/JsonHelper";
import { VoiceSerializer } from "@src/generated/model/VoiceSerializer";
import { SustainPedalMarkerSerializer } from "@src/generated/model/SustainPedalMarkerSerializer";
import { Clef } from "@src/model/Clef";
import { Ottavia } from "@src/model/Ottavia";
import { Voice } from "@src/model/Voice";
import { SimileMark } from "@src/model/SimileMark";
import { SustainPedalMarker } from "@src/model/Bar";
export class BarSerializer {
public static fromJson(obj: Bar, m: unknown): void {
if (!m) {
Expand All @@ -29,6 +31,7 @@ export class BarSerializer {
o.set("similemark", obj.simileMark as number);
o.set("displayscale", obj.displayScale);
o.set("displaywidth", obj.displayWidth);
o.set("sustainpedals", obj.sustainPedals.map(i => SustainPedalMarkerSerializer.toJson(i)));
return o;
}
public static setProperty(obj: Bar, property: string, v: unknown): boolean {
Expand Down Expand Up @@ -59,6 +62,14 @@ export class BarSerializer {
case "displaywidth":
obj.displayWidth = v! as number;
return true;
case "sustainpedals":
obj.sustainPedals = [];
for (const o of (v as (Map<string, unknown> | null)[])) {
const i = new SustainPedalMarker();
SustainPedalMarkerSerializer.fromJson(i, o);
obj.sustainPedals.push(i);
}
return true;
}
return false;
}
Expand Down
36 changes: 36 additions & 0 deletions src/generated/model/SustainPedalMarkerSerializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated>
// This code was auto-generated.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
import { SustainPedalMarker } from "@src/model/Bar";
import { JsonHelper } from "@src/io/JsonHelper";
import { SustainPedalMarkerType } from "@src/model/Bar";
export class SustainPedalMarkerSerializer {
public static fromJson(obj: SustainPedalMarker, m: unknown): void {
if (!m) {
return;
}
JsonHelper.forEach(m, (v, k) => this.setProperty(obj, k, v));
}
public static toJson(obj: SustainPedalMarker | null): Map<string, unknown> | null {
if (!obj) {
return null;
}
const o = new Map<string, unknown>();
o.set("ratioposition", obj.ratioPosition);
o.set("pedaltype", obj.pedalType as number);
return o;
}
public static setProperty(obj: SustainPedalMarker, property: string, v: unknown): boolean {
switch (property) {
case "ratioposition":
obj.ratioPosition = v! as number;
return true;
case "pedaltype":
obj.pedalType = JsonHelper.parseEnum<SustainPedalMarkerType>(v, SustainPedalMarkerType)!;
return true;
}
return false;
}
}
35 changes: 34 additions & 1 deletion src/importer/AlphaTexImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ScoreImporter } from '@src/importer/ScoreImporter';
import { UnsupportedFormatError } from '@src/importer/UnsupportedFormatError';
import { AccentuationType } from '@src/model/AccentuationType';
import { Automation, AutomationType } from '@src/model/Automation';
import { Bar } from '@src/model/Bar';
import { Bar, SustainPedalMarker, SustainPedalMarkerType } from '@src/model/Bar';
import { Beat } from '@src/model/Beat';
import { BendPoint } from '@src/model/BendPoint';
import { BrushType } from '@src/model/BrushType';
Expand Down Expand Up @@ -153,6 +153,7 @@ export class AlphaTexImporter extends ScoreImporter {
private _staffHasExplicitTuning: boolean = false;
private _staffTuningApplied: boolean = false;
private _percussionArticulationNames = new Map<string, number>();
private _sustainPedalToBeat = new Map<SustainPedalMarker, Beat>();

private _accidentalMode: AlphaTexAccidentalMode = AlphaTexAccidentalMode.Explicit;

Expand All @@ -179,6 +180,8 @@ export class AlphaTexImporter extends ScoreImporter {
}
this._allowTuning = true;
this._lyrics = new Map<number, Lyrics[]>();
this._sustainPedalToBeat = new Map<SustainPedalMarker, Beat>();

this.createDefaultScore();
this._curChPos = 0;
this._line = 1;
Expand Down Expand Up @@ -206,6 +209,12 @@ export class AlphaTexImporter extends ScoreImporter {
for (const [track, lyrics] of this._lyrics) {
this._score.tracks[track].applyLyrics(lyrics);
}
for (const [sustainPedal, beat] of this._sustainPedalToBeat) {
if(sustainPedal.ratioPosition === 0) {
const duration = beat.voice.bar.masterBar.calculateDuration();
sustainPedal.ratioPosition = beat.playbackStart / duration;
}
}
return this._score;
} catch (e) {
if (e instanceof AlphaTexError) {
Expand Down Expand Up @@ -1576,6 +1585,30 @@ export class AlphaTexImporter extends ScoreImporter {
this._sy = this.newSy();
}
return true;
} else if (syData === 'spd') {
const sustainPedal = new SustainPedalMarker();
sustainPedal.pedalType = SustainPedalMarkerType.Down;
// exact ratio position will be applied after .finish() when times are known
this._sustainPedalToBeat.set(sustainPedal, beat);
beat.voice.bar.sustainPedals.push(sustainPedal);
this._sy = this.newSy();
return true;
} else if (syData === 'spu') {
const sustainPedal = new SustainPedalMarker();
sustainPedal.pedalType = SustainPedalMarkerType.Up;
// exact ratio position will be applied after .finish() when times are known
this._sustainPedalToBeat.set(sustainPedal, beat);
beat.voice.bar.sustainPedals.push(sustainPedal);
this._sy = this.newSy();
return true;
} else if (syData === 'spe') {
const sustainPedal = new SustainPedalMarker();
sustainPedal.pedalType = SustainPedalMarkerType.Up;
sustainPedal.ratioPosition = 1;
this._sustainPedalToBeat.set(sustainPedal, beat);
beat.voice.bar.sustainPedals.push(sustainPedal);
this._sy = this.newSy();
return true;
} else if (syData === 'slashed') {
beat.slashed = true;
this._sy = this.newSy();
Expand Down
Loading