Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
porime42 committed Aug 6, 2024
1 parent 27de2c5 commit 43f23ff
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions OpenUtau.Core/Format/MusicXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,21 @@ static public UProject LoadProject(string file)

int divisions = (int)part.Measure[0].Attributes[0].Divisions;
int currPosTick = 0;
List<string> lyrics = new List<string>();

foreach (var measure in part.Measure)
{
// BPM
foreach (var direction in measure.Direction)
{
foreach (var directionType in direction.DirectionType)
{
if (directionType.Metronome != null)
{
int bpm = Int32.Parse(directionType.Metronome.PerMinute.Value);
uproject.tempos.Add(new UTempo {position = currPosTick,
bpm = bpm});
Log.Information($"Measure {measure.Number} BPM: {bpm.ToString()}");
}
}
double? bpm;
if ((bpm = MeasureBPM(measure)).HasValue) {
uproject.tempos.Add(new UTempo(currPosTick, bpm.Value));
Log.Information($"Measure {measure.Number} BPM: {bpm.ToString()}");
}

// Time Signature
foreach (var attributes in measure.Attributes)
{
foreach (var time in attributes.Time)
{
if (time.Beats.Count > 0 && time.BeatType.Count > 0)
{
uproject.timeSignatures.Add(new UTimeSignature
{
foreach (var attributes in measure.Attributes) {
foreach (var time in attributes.Time) {
if (time.Beats.Count > 0 && time.BeatType.Count > 0) {
uproject.timeSignatures.Add(new UTimeSignature {
barPosition = currPosTick,
beatPerBar = Int32.Parse(time.Beats[0]),
beatUnit = Int32.Parse(time.BeatType[0])
Expand All @@ -74,16 +62,13 @@ static public UProject LoadProject(string file)
}

// Note
foreach(var note in measure.Note)
{
foreach(var note in measure.Note) {
int durTick = (int)note.Duration * uproject.resolution / divisions;

if (note.Rest != null)
{
if (note.Rest != null) {
// pass
}
else
{
else {
var pitch = note.Pitch.Step.ToString() + note.Pitch.Octave.ToString();
int tone = MusicMath.NameToTone(pitch) + (int)note.Pitch.Alter;
UNote unote = uproject.CreateNote(tone, currPosTick, durTick);
Expand Down Expand Up @@ -115,6 +100,14 @@ static public Encoding DetectXMLEncoding(string file)
return xmlEncoding;
}

static public double? MeasureBPM(MusicXMLSchema.ScorePartwisePartMeasure measure)
{
foreach (var direction in measure.Direction) {
if (direction.Sound != null) { return (double)direction.Sound.Tempo; }
}
return null;
}

static public MusicXMLSchema.ScorePartwise ReadXMLScore(string xmlFile)
{
Log.Information($"MusicXML Character Encoding: {DetectXMLEncoding(xmlFile)}");
Expand Down

0 comments on commit 43f23ff

Please sign in to comment.