private void ReadHeader(IReadable input) { if (input.ReadInt32BE() != 6) { throw new Exception("Midi header is invalid."); } TrackFormat = (MidiTrackFormat)input.ReadInt16BE(); Tracks = new MidiTrack[input.ReadInt16BE()]; var div = input.ReadInt16BE(); Division = div & 0x7FFF; TimingStandard = ((div & 0x8000) > 0) ? MidiTimeFormat.FramesPerSecond : MidiTimeFormat.TicksPerBeat; }
private static MidiEvent ReadSystemCommonMessage(IReadable input, int delta, byte status) { switch ((SystemCommonTypeEnum)status) { case SystemCommonTypeEnum.SystemExclusive2: case SystemCommonTypeEnum.SystemExclusive: { var maker = input.ReadInt16BE(); if (maker == 0x0) { maker = input.ReadInt16BE(); } else if (maker == 0xF7) { return(null); } var data = new FastList <byte>(); var b = input.ReadByte(); while (b != 0xF7) { data.Add((byte)b); b = input.ReadByte(); } return(new SystemExclusiveEvent(delta, status, maker, data.ToArray())); } case SystemCommonTypeEnum.MtcQuarterFrame: return(new SystemCommonEvent(delta, status, (byte)input.ReadByte(), 0)); case SystemCommonTypeEnum.SongPosition: return(new SystemCommonEvent(delta, status, (byte)input.ReadByte(), (byte)input.ReadByte())); case SystemCommonTypeEnum.SongSelect: return(new SystemCommonEvent(delta, status, (byte)input.ReadByte(), 0)); case SystemCommonTypeEnum.TuneRequest: return(new SystemCommonEvent(delta, status, 0, 0)); default: throw new Exception("The system common message was invalid or unsupported : " + status); } }
private static MidiEvent ReadMetaMessage(IReadable input, int delta, byte status) { var metaStatus = input.ReadByte(); switch ((MetaEventTypeEnum)metaStatus) { case MetaEventTypeEnum.SequenceNumber: { var count = input.ReadByte(); if (count == 0) { return(new MetaNumberEvent(delta, status, (byte)metaStatus, -1)); } else if (count == 2) { return(new MetaNumberEvent(delta, status, (byte)metaStatus, input.ReadInt16BE())); } else { throw new Exception("Invalid sequence number event."); } } case MetaEventTypeEnum.TextEvent: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.CopyrightNotice: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.SequenceOrTrackName: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.InstrumentName: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.LyricText: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.MarkerText: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.CuePoint: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.PatchName: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.PortName: return(new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input))); case MetaEventTypeEnum.MidiChannel: if (input.ReadByte() != 1) { throw new Exception("Invalid midi channel event. Expected size of 1."); } return(new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte())); case MetaEventTypeEnum.MidiPort: if (input.ReadByte() != 1) { throw new Exception("Invalid midi port event. Expected size of 1."); } return(new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte())); case MetaEventTypeEnum.EndOfTrack: return(new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte())); case MetaEventTypeEnum.Tempo: if (input.ReadByte() != 3) { throw new Exception("Invalid tempo event. Expected size of 3."); } return(new MetaNumberEvent(delta, status, (byte)metaStatus, (input.ReadByte() << 16) | (input.ReadByte() << 8) | input.ReadByte())); case MetaEventTypeEnum.SmpteOffset: if (input.ReadByte() != 5) { throw new Exception("Invalid smpte event. Expected size of 5."); } return(new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte())); case MetaEventTypeEnum.TimeSignature: if (input.ReadByte() != 4) { throw new Exception("Invalid time signature event. Expected size of 4."); } return(new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte())); case MetaEventTypeEnum.KeySignature: if (input.ReadByte() != 2) { throw new Exception("Invalid key signature event. Expected size of 2."); } return(new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte())); case MetaEventTypeEnum.SequencerSpecific: var length = ReadVariableLength(input); var data = input.ReadByteArray(length); return(new MetaDataEvent(delta, status, (byte)metaStatus, data)); } throw new Exception("Not a valid meta message Status: " + status + " Meta: " + metaStatus); }
private static MidiEvent ReadSystemCommonMessage(IReadable input, int delta, byte status) { switch ((SystemCommonTypeEnum)status) { case SystemCommonTypeEnum.SystemExclusive2: case SystemCommonTypeEnum.SystemExclusive: { var maker = input.ReadInt16BE(); if (maker == 0x0) { maker = input.ReadInt16BE(); } else if (maker == 0xF7) return null; var data = new FastList<byte>(); var b = input.ReadByte(); while (b != 0xF7) { data.Add((byte)b); b = input.ReadByte(); } return new SystemExclusiveEvent(delta, status, maker, data.ToArray()); } case SystemCommonTypeEnum.MtcQuarterFrame: return new SystemCommonEvent(delta, status, (byte)input.ReadByte(), 0); case SystemCommonTypeEnum.SongPosition: return new SystemCommonEvent(delta, status, (byte)input.ReadByte(), (byte)input.ReadByte()); case SystemCommonTypeEnum.SongSelect: return new SystemCommonEvent(delta, status, (byte)input.ReadByte(), 0); case SystemCommonTypeEnum.TuneRequest: return new SystemCommonEvent(delta, status, 0, 0); default: throw new Exception("The system common message was invalid or unsupported : " + status); } }
private static MidiEvent ReadMetaMessage(IReadable input, int delta, byte status) { var metaStatus = input.ReadByte(); switch ((MetaEventTypeEnum)metaStatus) { case MetaEventTypeEnum.SequenceNumber: { var count = input.ReadByte(); if (count == 0) return new MetaNumberEvent(delta, status, (byte)metaStatus, -1); else if (count == 2) { return new MetaNumberEvent(delta, status, (byte)metaStatus, input.ReadInt16BE()); } else throw new Exception("Invalid sequence number event."); } case MetaEventTypeEnum.TextEvent: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.CopyrightNotice: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.SequenceOrTrackName: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.InstrumentName: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.LyricText: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.MarkerText: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.CuePoint: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.PatchName: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.PortName: return new MetaTextEvent(delta, status, (byte)metaStatus, ReadString(input)); case MetaEventTypeEnum.MidiChannel: if (input.ReadByte() != 1) throw new Exception("Invalid midi channel event. Expected size of 1."); return new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte()); case MetaEventTypeEnum.MidiPort: if (input.ReadByte() != 1) throw new Exception("Invalid midi port event. Expected size of 1."); return new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte()); case MetaEventTypeEnum.EndOfTrack: return new MetaEvent(delta, status, (byte)metaStatus, (byte)input.ReadByte()); case MetaEventTypeEnum.Tempo: if (input.ReadByte() != 3) throw new Exception("Invalid tempo event. Expected size of 3."); return new MetaNumberEvent(delta, status, (byte)metaStatus, (input.ReadByte() << 16) | (input.ReadByte() << 8) | input.ReadByte()); case MetaEventTypeEnum.SmpteOffset: if (input.ReadByte() != 5) throw new Exception("Invalid smpte event. Expected size of 5."); return new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte()); case MetaEventTypeEnum.TimeSignature: if (input.ReadByte() != 4) throw new Exception("Invalid time signature event. Expected size of 4."); return new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte() + ":" + input.ReadByte()); case MetaEventTypeEnum.KeySignature: if (input.ReadByte() != 2) throw new Exception("Invalid key signature event. Expected size of 2."); return new MetaTextEvent(delta, status, (byte)metaStatus, input.ReadByte() + ":" + input.ReadByte()); case MetaEventTypeEnum.SequencerSpecific: var length = ReadVariableLength(input); var data = input.ReadByteArray(length); return new MetaDataEvent(delta, status, (byte)metaStatus, data); } throw new Exception("Not a valid meta message Status: " + status + " Meta: " + metaStatus); }
private void ReadHeader(IReadable input) { if (input.ReadInt32BE() != 6) throw new Exception("Midi header is invalid."); TrackFormat = (MidiTrackFormat)input.ReadInt16BE(); Tracks = new MidiTrack[input.ReadInt16BE()]; var div = input.ReadInt16BE(); Division = div & 0x7FFF; TimingStandard = ((div & 0x8000) > 0) ? MidiTimeFormat.FramesPerSecond : MidiTimeFormat.TicksPerBeat; }