Pull up FractionalBeatTimeTimingModel.

This commit is contained in:
2022-12-12 23:48:15 +08:00
parent a8658856ca
commit 4b4356aaab
4 changed files with 79 additions and 24 deletions

View File

@@ -9,7 +9,7 @@ using System.Text.RegularExpressions;
namespace Cryville.Crtr { namespace Cryville.Crtr {
[JsonConverter(typeof(BeatTimeConverter))] [JsonConverter(typeof(BeatTimeConverter))]
public struct BeatTime { public struct BeatTime : IComparable<BeatTime>, IEquatable<BeatTime> {
[JsonConstructor()] [JsonConstructor()]
public BeatTime(int _b, int _n, int _d) { public BeatTime(int _b, int _n, int _d) {
b = _b; b = _b;
@@ -25,6 +25,36 @@ namespace Cryville.Crtr {
[JsonIgnore] [JsonIgnore]
public int d; public int d;
[JsonIgnore]
public double Decimal { get { return b + (double)n / d; } }
public int CompareTo(BeatTime other) {
var c = b.CompareTo(other.b);
if (c != 0) return c;
return ((double)n / d).CompareTo((double)other.n / other.d);
}
public override bool Equals(object obj) {
if (!(obj is BeatTime)) return false;
return Equals((BeatTime)obj);
}
public bool Equals(BeatTime other) {
return b.Equals(other.b) && ((double)n / d).Equals((double)other.n / other.d);
}
public override int GetHashCode() {
return Decimal.GetHashCode();
}
public static bool operator ==(BeatTime left, BeatTime right) {
return left.Equals(right);
}
public static bool operator !=(BeatTime left, BeatTime right) {
return !left.Equals(right);
}
} }
public class BeatTimeConverter : JsonConverter { public class BeatTimeConverter : JsonConverter {
@@ -56,7 +86,7 @@ namespace Cryville.Crtr {
[JsonIgnore] [JsonIgnore]
public float BeatPosition { public float BeatPosition {
get { get {
return time.Value.b + time.Value.n / (float)time.Value.d + BeatOffset; return (float)time.Value.Decimal + BeatOffset;
} }
} }
@@ -66,7 +96,7 @@ namespace Cryville.Crtr {
public float EndBeatPosition { public float EndBeatPosition {
get { get {
if (endtime == null) return BeatPosition; if (endtime == null) return BeatPosition;
return endtime.Value.b + endtime.Value.n / (float)endtime.Value.d + BeatOffset; return (float)endtime.Value.Decimal + BeatOffset;
} }
} }

View File

@@ -73,30 +73,28 @@ namespace Cryville.Crtr.Extensions.Malody {
Dictionary<MalodyChart.IEvent, StartEventState> longEvents Dictionary<MalodyChart.IEvent, StartEventState> longEvents
= new Dictionary<MalodyChart.IEvent, StartEventState>(); = new Dictionary<MalodyChart.IEvent, StartEventState>();
float? baseBpm = null, cbpm = null; float? baseBpm = null;
float pbeat = 0f, ctime = 0f; var tm = new FractionalBeatTimeTimingModel();
int[] endbeat = new int[] { 0, 0, 1 };
foreach (var ev in events) { foreach (var ev in events) {
float cbeat = ConvertBeat(ev.beat); var beat = new BeatTime(ev.beat[0], ev.beat[1], ev.beat[2]);
ctime += cbpm == null ? 0 : (cbeat - pbeat) / cbpm.Value * 60f; tm.ForwardTo(beat);
pbeat = cbeat;
if (ev is MalodyChart.Time) { if (ev is MalodyChart.Time) {
var tev = (MalodyChart.Time)ev; var tev = (MalodyChart.Time)ev;
if (baseBpm == null) baseBpm = tev.bpm; if (baseBpm == null) baseBpm = tev.bpm;
cbpm = tev.bpm; tm.BPM = tev.bpm;
chart.sigs.Add(new Chart.Signature { chart.sigs.Add(new Chart.Signature {
time = new BeatTime(ev.beat[0], ev.beat[1], ev.beat[2]), time = beat,
tempo = tev.bpm, tempo = tev.bpm,
}); });
chart.motions.Add(new Chart.Motion { chart.motions.Add(new Chart.Motion {
time = new BeatTime(ev.beat[0], ev.beat[1], ev.beat[2]), time = beat,
motion = "svm:" + (tev.bpm / baseBpm.Value).ToString(CultureInfo.InvariantCulture) motion = "svm:" + (tev.bpm / baseBpm.Value).ToString(CultureInfo.InvariantCulture)
}); });
} }
else if (ev is MalodyChart.Effect) { else if (ev is MalodyChart.Effect) {
var tev = (MalodyChart.Effect)ev; var tev = (MalodyChart.Effect)ev;
if (tev.scroll != null) group.motions.Add(new Chart.Motion { if (tev.scroll != null) group.motions.Add(new Chart.Motion {
time = new BeatTime(ev.beat[0], ev.beat[1], ev.beat[2]), time = beat,
motion = "svm:" + tev.scroll.Value.ToString(CultureInfo.InvariantCulture) motion = "svm:" + tev.scroll.Value.ToString(CultureInfo.InvariantCulture)
}); });
} }
@@ -115,19 +113,17 @@ namespace Cryville.Crtr.Extensions.Malody {
else throw new NotImplementedException(); else throw new NotImplementedException();
} }
else { else {
if (ConvertBeat(tev.beat) > ConvertBeat(endbeat)) endbeat = tev.beat;
var rn = new Chart.Note() { var rn = new Chart.Note() {
time = new BeatTime(tev.beat[0], tev.beat[1], tev.beat[2]), time = beat,
motions = new List<Chart.Motion> { motions = new List<Chart.Motion> {
new Chart.Motion() { motion = "track:" + tev.column.ToString(CultureInfo.InvariantCulture) } new Chart.Motion() { motion = "track:" + tev.column.ToString(CultureInfo.InvariantCulture) }
}, },
}; };
if (tev.endbeat != null) { if (tev.endbeat != null) {
if (ConvertBeat(tev.endbeat) > ConvertBeat(endbeat)) endbeat = tev.endbeat;
rn.endtime = new BeatTime(tev.endbeat[0], tev.endbeat[1], tev.endbeat[2]); rn.endtime = new BeatTime(tev.endbeat[0], tev.endbeat[1], tev.endbeat[2]);
longEvents.Add(ev, new StartEventState { longEvents.Add(ev, new StartEventState {
Destination = rn, Destination = rn,
Time = ctime, Time = tm.Time,
}); });
} }
group.notes.Add(rn); group.notes.Add(rn);
@@ -143,8 +139,10 @@ namespace Cryville.Crtr.Extensions.Malody {
} }
else throw new NotSupportedException(); else throw new NotSupportedException();
} }
chart.endtime = new BeatTime(endbeat[0] + 4, endbeat[1], endbeat[2]); var endbeat = tm.FractionalBeatTime;
meta.length = ctime; endbeat.b += 4;
chart.endtime = endbeat;
meta.length = (float)tm.Time;
meta.note_count = group.notes.Count; meta.note_count = group.notes.Count;
string chartName = string.Format("{0} - {1}", meta.song.name, meta.name); string chartName = string.Format("{0} - {1}", meta.song.name, meta.name);
if (src.meta.background != null) { if (src.meta.background != null) {
@@ -155,14 +153,10 @@ namespace Cryville.Crtr.Extensions.Malody {
} }
struct StartEventState { struct StartEventState {
public float Time { get; set; } public double Time { get; set; }
public ChartEvent Destination { get; set; } public ChartEvent Destination { get; set; }
} }
float ConvertBeat(int[] beat) {
return beat[0] + (float)beat[1] / beat[2];
}
#pragma warning disable IDE1006 #pragma warning disable IDE1006
struct MalodyChart { struct MalodyChart {
public interface IEvent { public interface IEvent {

View File

@@ -0,0 +1,20 @@
namespace Cryville.Crtr.Extensions {
public abstract class TimingModel {
public double Time { get; protected set; }
public double BeatTime { get; protected set; }
public BeatTime FractionalBeatTime { get; protected set; }
public double BPM { get; set; }
public TimingModel() {
FractionalBeatTime = new BeatTime(0, 0, 1);
}
}
public class FractionalBeatTimeTimingModel : TimingModel {
public void ForwardTo(BeatTime t) {
if (t == FractionalBeatTime) return;
FractionalBeatTime = t;
var nt = t.Decimal;
Time += (nt - BeatTime) / BPM * 60;
BeatTime = nt;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c4a1fab8f53dd742ba6501d682eb7f6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: