public static Composition Read(string filename, Fraction?prec = null) { var reader = new StreamReader(filename); MelodyPartList list = new MelodyPartList(MelodyPartList.Type.Voice); while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line == "") { continue; } var values = line.Split(','); if (values.Length != 2) { throw new Exception($"The line `{line}` must contain exactly two values."); } int note = int.Parse(values[0].Trim()); Fraction duration = Fraction.Parse(values[1].Trim()); NoteWithDuration nwd = new NoteWithDuration(60 + note, duration); list.Add(nwd); } Composition composition = new Composition(); composition.millisecondsPerNote = 500; // todo: arbitrary tempo composition.AddVoice(list); return(composition); }
public static Composition Read(string filename) { MidiFile midi = new MidiFile(filename, true); Composition composition = new Composition(); for (int track = 0; track < midi.Tracks; track++) { MelodyPartList list = new MelodyPartList(MelodyPartList.Type.Voice); foreach (MidiEvent midievent in midi.Events[track]) { if (midievent is NoteOnEvent noteonevent) { if (noteonevent.OffEvent != null) { if (noteonevent.DeltaTime > 0) { list.Add(new NoteWithDuration(new Fraction(noteonevent.DeltaTime, 120))); } list.Add( new NoteWithDuration( noteonevent.NoteNumber, Alteration.Natural, new Fraction(noteonevent.NoteLength, 120))); } } else { } } if (list.Count > 0) { composition.AddVoice(list); } } return(composition); }
private static void AddNote(MelodyPartList list, NoteWithDuration nwd, Fraction?prec = null, long absoluteTime = 0) { if (prec != null) { Fraction times = (nwd.Duration / prec.Value); if (!times.IsWhole()) { throw new Exception($"Duration of Note {nwd} not divisible by {prec.Value} at absoluteTime={absoluteTime}"); } } list.Add(nwd); }