/// <summary> /// Loads a Harmony Sequence though /// </summary> /// <param name="f"></param> /// <returns></returns> public static Composition LoadMidiType1(MidiFile f) { Composition comp = new Composition(); int c = 1; foreach(var trackEvents in f.Events) { Track t = new Track(); t.Channel = (byte)c++; TempoEvent tempo = new TempoEvent((int)(Note.ToRealDuration((int)Durations.qn, 60) * 1000000), 0); HarmonySequence seq = new HarmonySequence(); long lastAbsTime = -1; List<int> chordNotes = new List<int>(); int chordDuration = 0; PatchNames instr = (PatchNames)9999; bool add = true; foreach(var e in trackEvents) { if (e as TempoEvent != null) { tempo = (TempoEvent)e; } if (e as PatchChangeEvent != null) { var p = e as PatchChangeEvent; t.Instrument = (PatchNames)p.Patch; if(t.Instrument == instr) { break; } instr = t.Instrument; /*if(t.Duration < 1) { foreach(var t_ in comp.Tracks) if(t_.Instrument == (PatchNames)p.Patch) { t = t_; seq = t_.GetMainSequence() as HarmonySequence; add = false; break; } }*/ } NoteOnEvent on = e as NoteOnEvent; if (on != null && on.OffEvent != null) { int newDuration = Note.ToNoteLength(on.NoteLength, f.DeltaTicksPerQuarterNote, tempo.Tempo); if (on.AbsoluteTime == lastAbsTime && chordDuration==newDuration)//skip chords { chordNotes.Add(on.NoteNumber); } else { Chord lastChord = new Chord(chordNotes.ToArray(), chordDuration); chordNotes.Clear(); chordDuration = 0; seq.AddChord(lastChord); chordNotes.Add(on.NoteNumber); } chordDuration = newDuration; lastAbsTime = on.AbsoluteTime; } } if(chordNotes.Count > 0) { Chord lastChord = new Chord(chordNotes.ToArray(), chordDuration); chordNotes.Clear(); chordDuration = 0; seq.AddChord(lastChord); } t.AddSequence(seq); if (!comp.Tracks.Contains(t) && t.Duration > 0 && add) comp.Add(t); } return comp; }
static void ChordTest() { MusicPlayer player = new MusicPlayer(); Chord dminor = new Chord(new NoteNames[] { NoteNames.D, NoteNames.F, NoteNames.A }, 4, Durations.wn); Chord dmajor = new Chord(new NoteNames[] { NoteNames.G, NoteNames.B, NoteNames.B }, 4, Durations.wn); Chord cmajor = new Chord(new NoteNames[] { NoteNames.C, NoteNames.E, NoteNames.G }, 4, Durations.bn); HarmonySequence seq = new HarmonySequence(); seq.AddChord(dminor); seq.AddChord(dmajor); seq.AddChord(cmajor); seq.AddChord(dminor); seq.AddChord(cmajor); seq.AddChord(dminor); seq.AddChord(dmajor); seq.AddChord(cmajor); player.Play(seq); player.Close(); }
public HarmonySequence GetHarmonySequence(MelodySequence seq) { HarmonySequence sequence = new HarmonySequence(); foreach(Note n in seq.ToArray()) { if (n.Pitch > 0 && n.Velocity > 0 && n.Duration > (int)Durations.en) sequence.AddChord(GetChord(n)); else seq.AddPause(n.Duration); } return sequence; }