using System; using System.IO; namespace NAudio.Midi { /// /// Represents a MIDI key signature event event /// public class KeySignatureEvent : MetaEvent { private readonly byte sharpsFlats; private readonly byte majorMinor; /// /// Reads a new track sequence number event from a MIDI stream /// /// The MIDI stream /// the data length public KeySignatureEvent(BinaryReader br, int length) { if (length != 2) { throw new FormatException("Invalid key signature length"); } sharpsFlats = br.ReadByte(); // sf=sharps/flats (-7=7 flats, 0=key of C,7=7 sharps) majorMinor = br.ReadByte(); // mi=major/minor (0=major, 1=minor) } /// /// Creates a new Key signature event with the specified data /// public KeySignatureEvent(int sharpsFlats, int majorMinor, long absoluteTime) : base(MetaEventType.KeySignature, 2, absoluteTime) { this.sharpsFlats = (byte) sharpsFlats; this.majorMinor = (byte) majorMinor; } /// /// Creates a deep clone of this MIDI event. /// public override MidiEvent Clone() => (KeySignatureEvent)MemberwiseClone(); /// /// Number of sharps or flats /// public int SharpsFlats => (sbyte)sharpsFlats; /// /// Major or Minor key /// public int MajorMinor => majorMinor; /// /// Describes this event /// /// String describing the event public override string ToString() { return String.Format("{0} {1} {2}", base.ToString(), SharpsFlats, majorMinor); } /// /// Calls base class export first, then exports the data /// specific to this event /// MidiEvent.Export /// public override void Export(ref long absoluteTime, BinaryWriter writer) { base.Export(ref absoluteTime, writer); writer.Write(sharpsFlats); writer.Write(majorMinor); } } }