public static (IMessage[], MetaState) Region2Messages(Region region) { LinkedList <IMessage> messages = new LinkedList <IMessage>(); Dictionary <long, TimedNote> startTimes = new Dictionary <long, TimedNote>(); Dictionary <long, TimedNote> endTimes = new Dictionary <long, TimedNote>(); // Find start times and end times foreach (TimedNote note in region) { if (note.StartTime.HasValue) { startTimes.Add(note.StartTime.Value, note); } if (note.EndTime.HasValue) { startTimes.Add(note.EndTime.Value, note); } } long end = startTimes.Keys.Union(endTimes.Keys).Max(); long lastEvent = 0; for (long i = 0; i < end; i++) { if (startTimes.Keys.Contains(i)) { TimedNote note = startTimes.GetValueOrDefault(i); messages.AddLast(new NoteOnMessage( note, i - lastEvent )); lastEvent = i; } if (endTimes.Keys.Contains(i)) { TimedNote note = endTimes.GetValueOrDefault(i); messages.AddLast(new NoteOffMessage( note, i - lastEvent )); lastEvent = i; } } return(messages.ToArray(), region.State); }
public static Region Messages2Region(IMessage[] buffer, MetaState state) { List <TimedNote> notes = new List <TimedNote>(buffer.Length); Dictionary <byte, TimedNote> notesOn = new Dictionary <byte, TimedNote>(); long currentTime = 0; foreach (IMessage message in buffer) { currentTime += message.TimeDelta; if (message is NoteOnMessage noteOnMessage) { TimedNote newNote = new TimedNote( noteOnMessage.Note.Channel, noteOnMessage.Note.Pitch, noteOnMessage.Note.Velocity, currentTime, null ); notes.Add(newNote); notesOn.Add(newNote.Pitch, newNote); } else if (message is NoteOffMessage noteOffMessage) { if (notesOn.ContainsKey(noteOffMessage.Note.Pitch)) { notesOn.GetValueOrDefault(noteOffMessage.Note.Pitch) .EndTime = currentTime; } else { TimedNote newNote = new TimedNote( noteOffMessage.Note.Channel, noteOffMessage.Note.Pitch, noteOffMessage.Note.Velocity, null, currentTime ); notes.Add(newNote); } } } return(new Region(notes, state)); }
public void Add(TimedNote note) { int index = 0; if (note.StartTime.HasValue) { long startTime = note.StartTime.Value; while (startTime < _notes[index].StartTime && index < _notes.Count) { index++; } _notes.Insert(index, note); } else { _notes.Insert(0, note); } }
public bool Remove(TimedNote item) { return(_notes.Remove(item)); }
public bool Contains(TimedNote note) { return(_notes.Contains(note)); }