Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricelacharme committed Jan 7, 2025
1 parent ff9b9d5 commit 3e0ded9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 18 deletions.
4 changes: 4 additions & 0 deletions KMusicXml/MusicXml/Domain/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal Note()
PitchDrums = new Pitch();

TieType = TieTypes.None;

//MeasureNumber = 0;
}


Expand Down Expand Up @@ -73,5 +75,7 @@ internal Note()
public bool IsRest { get; internal set; }

public string Accidental { get; internal set; }

//public int MeasureNumber { get; internal set; }
}
}
79 changes: 67 additions & 12 deletions KMusicXml/MusicXml/Domain/Part.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static Part Create(XDocument doc, XElement partlistElement)
{
string creator = partIdent.Descendants("creator").FirstOrDefault()?.Value;
if (creator != null)
MidiTags.ITag.Add(string.Format("Composer: {0}", creator) );
MidiTags.ITag.Add(string.Format("Arranger: {0}", creator) );

var encod = partIdent.Descendants("encoding").FirstOrDefault();
if (encod != null)
Expand Down Expand Up @@ -375,7 +375,7 @@ public static Part Create(XDocument doc, XElement partlistElement)
*/
try
{
curMeasure.Number = int.Parse(measureNode.Attribute("number").Value);
curMeasure.Number = int.Parse(measureNode.Attribute("number").Value);
}
catch (Exception ex)
{
Expand All @@ -385,6 +385,48 @@ public static Part Create(XDocument doc, XElement partlistElement)
#endregion measure number


// if number = 0, this measure is different than other ones (shorter)
// ?? add additional rests ?
// <measure implicit="yes" number="0" width="167">
if (curMeasure.Number == 0)
{
var vLocalNotes = measureNode.Descendants("note");

int d = 0;
int curstaff = -1;
int x = -1;
foreach (XElement e in vLocalNotes)
{
var ddur = e.Descendants("duration").FirstOrDefault();
var chord = e.Descendants("chord").FirstOrDefault();
var staf = e.Descendants("staff").FirstOrDefault();
if (staf != null)
{
x = ConvertStringValue(staf.Value, -1);
if (x != curstaff)
{
curstaff = x;
d = 0;
}
}

if (ddur != null && chord == null && (x == curstaff))
{
d += ConvertStringValue(ddur.Value, 0);
}
}
if (d < _part._measurelength)
{
// Add a rest note to complete the measure
Note note = new Note();
note.IsRest = true;
note.Duration = (int)((_part._measurelength - d) * _part.coeffmult);
MeasureElement trucmeasureElement = new MeasureElement { Type = MeasureElementType.Note, Element = note };
curMeasure.MeasureElements.Add(trucmeasureElement);
}
}


foreach (XElement childnode in measureNode.Descendants())
{
// Speed
Expand Down Expand Up @@ -432,7 +474,8 @@ public static Part Create(XDocument doc, XElement partlistElement)
//if (int.Parse(t) == 11 && _part.Id == "P2")
// Console.Write("");

Note note = GetNote(childnode, _part.coeffmult, _part._chromatictranspose, _part._octavechange, _part.SoundDynamics, vNotes, _part._measurelength);
Note note = GetNote(childnode, _part.coeffmult, _part._chromatictranspose, _part._octavechange, _part.SoundDynamics, vNotes, _part._measurelength);
//note.MeasureNumber = curMeasure.Number;

#region note lyrics
if (note.Lyrics != null && note.Lyrics.Count > 0)
Expand Down Expand Up @@ -542,9 +585,7 @@ public static Part Create(XDocument doc, XElement partlistElement)
// Create new element
MeasureElement trucmeasureElement = new MeasureElement { Type = MeasureElementType.Note, Element = note };
curMeasure.MeasureElements.Add(trucmeasureElement);

}

}
else if (childnode.Name == "harmony")
{
// Chords
Expand All @@ -561,8 +602,7 @@ public static Part Create(XDocument doc, XElement partlistElement)
curMeasure.MeasureElements.Add(trucmeasureElement);
}

}

}
else if (childnode.Name == "backup")
{
// voir https://www.w3.org/2021/06/musicxml40/tutorial/midi-compatible-part/
Expand Down Expand Up @@ -996,10 +1036,17 @@ private static Note GetNote(XElement node, float mult, int chromatictranspose, i
if (note.TieType == Note.TieTypes.Start)
{
bool bStart = false;

int curstaff = -1;
int x = -1;
if (staff != null)
{
curstaff = ConvertStringValue(staff.Value, -1);
}

// Start of a linked note: add duration of Tie Stop note
foreach (XElement e in c)
{
x = -1;
if (e.Name == "note")
{
if (e == node)
Expand All @@ -1016,7 +1063,11 @@ private static Note GetNote(XElement node, float mult, int chromatictranspose, i
if (!note.IsDrums)
{
var stepnext = e.Descendants("step").FirstOrDefault();
if (bStart && stepnext.Value == step.Value)
var staffnext = e.Descendants("staff").FirstOrDefault();
if (staffnext != null)
x = ConvertStringValue(staffnext.Value, -1);

if (bStart && stepnext.Value == step.Value && x == curstaff)
{
var ddur = e.Descendants("duration").FirstOrDefault();
if (ddur != null)
Expand All @@ -1034,9 +1085,13 @@ private static Note GetNote(XElement node, float mult, int chromatictranspose, i
{
// Drums
var displaystepnext = e.Descendants("display-step").FirstOrDefault();
if (bStart && displaystepnext.Value == displaystep.Value)
var staffnext = e.Descendants("staff").FirstOrDefault();
if (staffnext != null)
x = ConvertStringValue(staffnext.Value, -1);

if (bStart && displaystepnext.Value == displaystep.Value && x == curstaff)
{
var ddur = e.Descendants("duration").FirstOrDefault();
var ddur = e.Descendants("duration").FirstOrDefault();
if (ddur != null)
{
int ddd = (int)(int.Parse(ddur.Value) * mult);
Expand Down
41 changes: 35 additions & 6 deletions KMusicXml/ReaderXml/MusicXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ public Sequence Load(MusicXml.Domain.Score SC)
int starttime = 0;

int versenumber = 0;

// limits of a measure
int measureId = 0;
int measureStart;
int measureEnd;


// =========================================
Expand Down Expand Up @@ -394,6 +399,14 @@ public Sequence Load(MusicXml.Domain.Score SC)
// if (part.Id == "P2")
// Console.WriteLine("");


// Measure limits to check notes

measureId++;
measureStart = (measureId - 1) * MeasureLength;
measureEnd = measureId * MeasureLength;


// BEGIN RECUP
decimal W = measure.Width;
int notenumber = 0;
Expand Down Expand Up @@ -531,22 +544,25 @@ public Sequence Load(MusicXml.Domain.Score SC)

// Note duration
note.Duration = (int)(note.Duration * multcoeff); // Full duration (can be several measures)
note.TieDuration = (int)(note.TieDuration * multcoeff); // duration inside the current measure in case of linked notes
note.TieDuration = (int)(note.TieDuration * multcoeff); // original duration of the note in case of linked notes

/*
if (measure.Number == 5)
if (measure.Number == 26)
if (part.Id == "P2")
Console.WriteLine("");
*/

// TODO: REVOIR les mult & multcoeff

// Note is a linked note
// Note is the end of a linked note
// => add duration inside the current measure and exit
if (note.Duration == 0)
{
if (!note.IsChordTone)
{
offset = note.TieDuration;
timeline += note.TieDuration;
}

break;
}
Expand Down Expand Up @@ -586,11 +602,23 @@ public Sequence Load(MusicXml.Domain.Score SC)
notenumber += alter;
}
}


// Start time of note = tiumeline

// Start time of note = timeline
starttime = timeline;


if (starttime < measureStart)
{
//timeline = measureStart;
//starttime = timeline;
Console.WriteLine(measure.Number);
}
if (starttime > measureEnd)
{
Console.WriteLine(measure.Number);
}


// Create note
// in case of harmony (chord), eliminate notes having no stem (except note having whole duration)
// because these notes are only here for the duration of the chord and must not be played
Expand All @@ -609,6 +637,7 @@ public Sequence Load(MusicXml.Domain.Score SC)
}



// offset for the timeline for the next note (if not chord)
// if a chord, the offset will be substract
if (note.TieDuration > 0)
Expand Down

0 comments on commit 3e0ded9

Please sign in to comment.