Skip to content

Commit

Permalink
feat: added timing change deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
xhayper committed Nov 12, 2023
1 parent ef7da18 commit 0841f56
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/internal/syntacticAnalysis/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ export class Deserializer {
*/
public readonly enumerator: Enumerator<Token>;

/**
* INTERNAL USAGE ONLY! DO NOT USE THIS PROPERTY DIRECTLY!
*/
public readonly timingChanges: TimingChange[] = [new TimingChange(0, 4)];
private _maxFinishTime: number = 0;
private _currentTime: number = 0;
/**
* INTERNAL USAGE ONLY! DO NOT USE THIS PROPERTY DIRECTLY!
*/
public currentTime: number = 0;
/**
* INTERNAL USAGE ONLY! DO NOT USE THIS PROPERTY DIRECTLY!
*/
Expand Down Expand Up @@ -61,7 +68,7 @@ export class Deserializer {
break;
}
case TokenType.Location: {
this.currentNoteCollection ??= new NoteCollection(this._currentTime);
this.currentNoteCollection ??= new NoteCollection(this.currentTime);

if (token.lexeme[0] === "0") {
if (this.currentNoteCollection.eachStyle !== EachStyle.ForceBroken)
Expand All @@ -86,7 +93,7 @@ export class Deserializer {
this.currentNoteCollection = undefined;
}

this._currentTime += this.currentTiming.secondsPerBeat;
this.currentTime += this.timingChanges[this.timingChanges.length - 1].secondsPerBeat;
}
break;
case TokenType.EachDivider:
Expand All @@ -111,7 +118,7 @@ export class Deserializer {
case TokenType.SlideJoiner:
throw new ScopeMismatchException(token.line, token.character, ScopeType.Slide);
case TokenType.EndOfFile:
this._chart.finishTiming = this._currentTime;
this._chart.finishTiming = this.currentTime;
break;
case TokenType.None:
break;
Expand Down
3 changes: 3 additions & 0 deletions src/internal/syntacticAnalysis/serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Serializer {

}
4 changes: 2 additions & 2 deletions src/internal/syntacticAnalysis/states/noteReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class NoteReader {
}

case TokenType.Duration: {
NoteReader.readDuration(token, parent.currentTiming, currentNote);
NoteReader.readDuration(parent.timingChanges[parent.timingChanges.length-1], token, currentNote);
break;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ export class NoteReader {
}
}

private static readDuration(token: Token, timing: TimingChange, note: Note) {
private static readDuration(timing: TimingChange, token: Token, note: Note) {
if (note.type !== NoteType.Break) note.type = NoteType.Hold;

if (token.lexeme[0] === "#") {
Expand Down
18 changes: 16 additions & 2 deletions src/internal/syntacticAnalysis/states/subdivisionReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ export class SubdivisionReader {
if (isNaN(explicitTempo))
throw new UnexpectedCharacterException(token.line, token.character + 1, '0~9, or "."');

parent.currentTiming.explicitOverride(explicitTempo);
// TODO: This might not copy the object and just be a reference.
const newTimingChange = parent.timingChanges[parent.timingChanges.length - 1];
newTimingChange.explicitOverride(explicitTempo);

if (Math.abs(parent.timingChanges[parent.timingChanges.length - 1].time - parent.currentTime) <= 0.0001)
parent.timingChanges.pop();

parent.timingChanges.push(newTimingChange);
return;
}

const subdivision = parseFloat(token.lexeme);

if (isNaN(subdivision)) throw new UnexpectedCharacterException(token.line, token.character, '0~9, or "."');

parent.currentTiming.subdivisions = subdivision;
const newTimingChange = parent.timingChanges[parent.timingChanges.length - 1];
newTimingChange.subdivisions = subdivision;

// TODO: This might not copy the object and just be a reference.
if (Math.abs(parent.timingChanges[parent.timingChanges.length - 1].time - parent.currentTime) <= 0.0001)
parent.timingChanges.pop();

parent.timingChanges.push(newTimingChange);
}
}
9 changes: 8 additions & 1 deletion src/internal/syntacticAnalysis/states/tempoReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export class TempoReader {

if (isNaN(tempo)) throw new UnexpectedCharacterException(token.line, token.character, '0~9, or "."');

parent.currentTiming.tempo = tempo;
const newTimingChange = parent.timingChanges[parent.timingChanges.length - 1];
newTimingChange.tempo = tempo;
newTimingChange.time = parent.currentTime;

if (Math.abs(parent.timingChanges[parent.timingChanges.length - 1].time - parent.currentTime) <= 0.0001)
parent.timingChanges.pop();

parent.timingChanges.push(newTimingChange);
}
}
1 change: 1 addition & 0 deletions src/internal/syntacticAnalysis/timingChange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class TimingChange {
public time: number = 0;
public tempo: number;
public subdivisions: number;

Expand Down
2 changes: 2 additions & 0 deletions src/structures/maiChart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TimingChange } from "../internal/syntacticAnalysis/timingChange";
import { NoteCollection } from "./noteCollection";

export class MaiChart {
public finishTiming: number | undefined;
public noteCollections: NoteCollection[] = [];
public timingChanges: TimingChange[] = [];
}

0 comments on commit 0841f56

Please sign in to comment.