protected byte[] GetMidiMessage(MIDITrack track) { byte command = Common.GetByte(ref ByteBuff, ref currentOffset); byte[] data; if (!((command & 0x80) == 0x80)) // is not command? { if (track.LastCommand == 0) { throw new System.Exception("Bad midi file"); } command = track.LastCommand; currentOffset--; // Command byte - its 1 first data dyte } else { track.LastCommand = command; } data = GetMessageDataForCommand(track); byte[] result = new byte[data.Length + 1]; result[0] = command; Array.Copy(data, 0, result, 1, data.Length); return(result); }
protected MIDIEvent LoadMidiEvent(MIDITrack track) { MIDIEvent result = new MIDIEvent(); result.absTime = Common.GetVariableNumber(ref ByteBuff, ref currentOffset); result.MidiMessage = GetMidiMessage(track); return(result); }
protected void ProcessMIDITracks() { for (int i = 0; i < MidiHeaderInfo.TrackCount; i++) { MIDITrack track = new MIDITrack(); ProcessMIDITrack(track); Tracks.Add(track); } }
protected void ProcessMidiEvents(MIDITrack track) { track.MidiEvents = new List <MIDIEvent>(); int _old; while (track.Info.Length > 0) { _old = currentOffset; MIDIEvent midiEvent = LoadMidiEvent(track); track.AbsTime += midiEvent.absTime; midiEvent.absTime = track.AbsTime; track.MidiEvents.Add(midiEvent); track.Info.Length -= (currentOffset - _old); } }
protected void ProcessMIDITrack(MIDITrack track) { ProcessMidiTrackInfo(ref track.Info); track.AbsTime = 0; track.LastCommand = 0; track.CurrentEventIndex = 0; MIDIMarker marker = new MIDIMarker(); marker.StartIndex = track.MidiEvents.Count(); marker.Name = "Init"; marker.Section = StyleSections.Init; track.CurrentMarker = marker; track.MidiMarkers.Add(marker); ProcessMidiEvents(track); if (track.CurrentMarker != null) { track.CurrentMarker.StopIndex = track.MidiEvents.Count() - 1; } }
protected byte[] GetMessageDataForCommand(MIDITrack track) { //Meta if (track.LastCommand == 0xFF) { byte type = Common.GetByte(ref ByteBuff, ref currentOffset); byte[] b = GetVarData(); byte[] result = new byte[b.Length + 1]; result[0] = type; Array.Copy(b, 0, result, 1, b.Length); if (type == 0x06) { if (track.CurrentMarker != null) { track.CurrentMarker.StopIndex = track.MidiEvents.Count() - 1; } string name = Encoding.Default.GetString(b); if (name != "SFF1" && name != "SInt") { MIDIMarker marker = new MIDIMarker(); marker.StartIndex = track.MidiEvents.Count(); marker.Name = name; track.CurrentMarker = marker; track.MidiMarkers.Add(marker); } } if (type == 0x03) { StyleName = Encoding.Default.GetString(b); } return(result); } else //SysEx if (track.LastCommand == 0xF0) { return(GetVarData()); } else if (track.LastCommand == 0xF7) { throw new NotImplementedException(); } else // Channel message if (Common.IsMasked(track.LastCommand, 0x8) || Common.IsMasked(track.LastCommand, 0x9) || Common.IsMasked(track.LastCommand, 0xA) || Common.IsMasked(track.LastCommand, 0xB) || Common.IsMasked(track.LastCommand, 0xE)) { return(new byte[2] { Common.GetByte(ref ByteBuff, ref currentOffset), Common.GetByte(ref ByteBuff, ref currentOffset) }); } else if (Common.IsMasked(track.LastCommand, 0xC) || Common.IsMasked(track.LastCommand, 0xD)) { return(new byte[1] { Common.GetByte(ref ByteBuff, ref currentOffset) }); } return(new byte[0]); }