Skip to content

Commit

Permalink
musicxml
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricelacharme committed Jan 5, 2025
1 parent 797d2d6 commit aab8330
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 23 deletions.
8 changes: 4 additions & 4 deletions KMusicTxt/MusicTxtWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ private void AddSeqInfos()
@I Information ex Date(of Karaoke file, not song)
@V (single) Version ex 0100 ?
*/
if (sequence.KTag.Count == 0)
if (sequence.KTag == null || sequence.KTag.Count == 0)
stream.WriteLine("0, 0, Text_t, \"@KMIDI KARAOKE FILE\"");
if (sequence.VTag.Count == 0)
if (sequence.VTag == null || sequence.VTag.Count == 0)
stream.WriteLine("0, 0, Text_t, \"@V0100\"");
if (sequence.TTag.Count == 0)
if (sequence.TTag == null || sequence.TTag.Count == 0)
stream.WriteLine(string.Format("0, 0, Text_t, \"@T{0}\"", song));

if (sequence.ITag.Count == 0)
if (sequence.ITag == null || sequence.ITag.Count == 0)
stream.WriteLine("0, 0, Text_t, \"@IMidi file dump made with Karaboss\"");

stream.WriteLine("0, 0, Copyright_t, \"No copyright\"");
Expand Down
4 changes: 3 additions & 1 deletion KMusicXml/MusicXml/Domain/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public enum TieTypes
internal Note()
{
Type = string.Empty;
Duration = -1;
Duration = 0;
TieDuration = 0;
Voice = 1;
Staff = -1;
IsChordTone = false;
Expand Down Expand Up @@ -46,6 +47,7 @@ internal Note()
public int Voice { get; internal set; }

public int Duration { get; internal set; }
public int TieDuration { get; internal set; }

public int Velocity { get; internal set; }

Expand Down
34 changes: 28 additions & 6 deletions KMusicXml/MusicXml/Domain/Part.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,12 @@ public static Part Create(XDocument doc, XElement partlistElement)
if (quarterlength != null)
{
_part.Division = (int)attributes.Descendants("divisions").FirstOrDefault();
_part._measurelength = _part.Division * _part.Numerator; // FAB pour avoir la longueur d'une mesure

int m = (int)(_part.Division * _part.Numerator * (4f/ _part.Denominator));
//_part._measurelength = _part.Division * _part.Numerator; // FAB pour avoir la longueur d'une mesure
_part._measurelength = m;


_part.coeffmult = 480 / _part.Division;
_part.Division = 480;
}
Expand Down Expand Up @@ -862,12 +867,19 @@ private static Note GetNote(XElement node, int mult, int chromatictranspose, int
if (stp.Length > 2)
{
stp = stp.Substring(stp.Length - 2, 2);
note.DrumInstrument = Convert.ToInt32(stp) - 1;
if (isNumeric(stp))
note.DrumInstrument = Convert.ToInt32(stp) - 1;
else
{
stp = stp.Substring(stp.Length - 1, 1);
if (isNumeric(stp))
note.DrumInstrument = Convert.ToInt32(stp) - 1;
}
}
}
else
{
note.DrumInstrument = note.Pitch.Step;
//note.DrumInstrument = note.Pitch.Step;
}
}

Expand Down Expand Up @@ -921,6 +933,9 @@ private static Note GetNote(XElement node, int mult, int chromatictranspose, int
// Ajust calculation with notes having tie
if (note.TieType == Note.TieTypes.Start)
{

note.TieDuration = note.Duration;

bool bStart = false;
// Start of a linked note: add duration of Tie Stop note
foreach (XElement e in c)
Expand All @@ -936,7 +951,8 @@ private static Note GetNote(XElement node, int mult, int chromatictranspose, int
var ttie = e.Descendants("tie").FirstOrDefault();
if (ttie != null)
{
if (bStart && ttie.Attribute("type").Value == "stop")
var steep = e.Descendants("step").FirstOrDefault();
if (bStart && steep.Value == step.Value && ttie.Attribute("type").Value == "stop")
{
var ddur = e.Descendants("duration").FirstOrDefault();
if (ddur != null)
Expand Down Expand Up @@ -975,7 +991,12 @@ private static Note GetNote(XElement node, int mult, int chromatictranspose, int

return note;
}


private static bool isNumeric(string stp)
{
return int.TryParse(stp, out int test);
}


/// <summary>
/// Extract the list of lyrics for a single note
Expand Down Expand Up @@ -1048,7 +1069,8 @@ private static List<Lyric> GetLyrics(XElement node)
return lstLyrics;
}





}
}
53 changes: 41 additions & 12 deletions KMusicXml/ReaderXml/MusicXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,19 @@ public Sequence Load(MusicXml.Domain.Score SC)
int d = chord.RemainDuration;
note.Duration = (int)(d * multcoeff);


// Change patch & channel in order to play piano for the chords
int chordchannel = Channel;
if (track1.ProgramChange != 0)
chordchannel = 15;


List<int> lnotes = chord.GetNotes(notenumber);

// Create chord
for (int idx = 0; idx < lnotes.Count; idx++)
{
CreateMidiNote1(note, lnotes[idx], starttime);
CreateMidiNote1(note, chordchannel, lnotes[idx], starttime);
}


Expand All @@ -486,7 +493,10 @@ public Sequence Load(MusicXml.Domain.Score SC)
notenumber += alter;
}

CreateMidiNote1(note, notenumber, starttime);
CreateMidiNote1(note, chordchannel, notenumber, starttime);

// Restore patch
//track1.insertPatch(Channel, pc, starttime + 10);
break;


Expand All @@ -509,6 +519,7 @@ public Sequence Load(MusicXml.Domain.Score SC)
// Note duration
//int t = part.Division;
note.Duration = (int)(note.Duration * multcoeff);
note.TieDuration = (int)(note.TieDuration * multcoeff);

// REVOIR les mult & multcoeff

Expand All @@ -524,18 +535,35 @@ public Sequence Load(MusicXml.Domain.Score SC)
// Take into account previous note
if (note.IsChordTone)
offset = 0;

timeline += offset;


// JE_NE_PEUX_PLUS_DIRE_JE_TAIME_-_Jacques_HIGELIN.xml
//pb avec les notes liées / accords à approfondir
// Quand toutes les notes de l'accord sonbt liées c'st bon
// Quand une seule note liée avec l'accord qui suit, c'est la merde
// Voir mesure 9 en midi, je ne peux plus dire je t'aime
// ou mesure 5 dans musescore.

// For the next note (if not chord)
offset = note.Duration;
if (note.TieType == Note.TieTypes.Start)
{
//offset = note.TieDuration;
offset = note.Duration;
}
else
{
offset = note.Duration;
}

starttime = timeline;


octave = note.Pitch.Octave;
letter = note.Pitch.Step.ToString();

if (note.IsDrums)
if (note.IsDrums && note.DrumInstrument != 0)
{
notenumber = note.DrumInstrument;
}
Expand All @@ -556,9 +584,9 @@ public Sequence Load(MusicXml.Domain.Score SC)
if (part.ScoreType == Part.ScoreTypes.Notes || part.ScoreType == Part.ScoreTypes.Chords && note.Stem != null && note.Stem != "none")
{
if (note.Staff <= 1)
CreateMidiNote1(note, notenumber, starttime);
CreateMidiNote1(note, Channel, notenumber, starttime);
else
CreateMidiNote2(note, notenumber, starttime);
CreateMidiNote2(note, Channel, notenumber, starttime);


if (note.Lyrics.Count > 0 && note.Lyrics[0].Text != null)
Expand Down Expand Up @@ -927,10 +955,10 @@ private void CreateTrack1()

track1.insertTrackname(TrackName);

if (Volume >= 0)
if (Volume >= 0 && Volume <= 15)
track1.insertVolume(Channel, Volume);

if (Pan >= 0)
if (Pan >= 0 && Pan <= 127)
track1.insertPan(Channel, Pan);

newTracks.Add(track1);
Expand Down Expand Up @@ -962,9 +990,9 @@ private void CreateTrack2()

track2.insertTrackname(TrackName);

if (Volume >= 0)
if (Volume >= 0 && Volume <= 127)
track2.insertVolume(Channel, Volume);
if (Pan >= 0)
if (Pan >= 0 && Pan <= 127)
track2.insertPan(Channel, Pan);

newTracks.Add(track2);
Expand All @@ -983,13 +1011,14 @@ private void CreateTempoEvent(float tmp, int ticks)

#region notes


/// <summary>
/// Create a MIDI note in the current rack
/// </summary>
/// <param name="n"></param>
/// <param name="v"></param>
/// <param name="st"></param>
private void CreateMidiNote1(Note n, int v, int st)
private void CreateMidiNote1(Note n, int Channel, int v, int st)
{

if (v < 21)
Expand All @@ -1009,7 +1038,7 @@ private void CreateMidiNote1(Note n, int v, int st)
}
}

private void CreateMidiNote2(Note n, int v, int st)
private void CreateMidiNote2(Note n, int Channel, int v, int st)
{
if (v < 21)
return;
Expand Down
5 changes: 5 additions & 0 deletions Sanford.Multimedia.Midi/Sequencing/Track Classes/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,11 @@ public void insertPatch(int channel, int programchange) {
Insert(0, message);
}

public void insertPatch(int channel, int programchange, int position)
{
ChannelMessage message = new ChannelMessage(ChannelCommand.ProgramChange, channel, programchange, 0);
Insert(position, message);
}
#endregion

#region pitchbend
Expand Down

0 comments on commit aab8330

Please sign in to comment.