示例#1
0
        public MidiFile(byte[] data)
        {
            var position = 0;

            if (Reader.ReadString(data, ref position, 4) != "MThd")
            {
                throw new FormatException("Invalid file header (expected MThd)");
            }

            if (Reader.Read32(data, ref position) != 6)
            {
                throw new FormatException("Invalid header length (expected 6)");
            }

            Format           = (MidiFormat)Reader.Read16(data, ref position);
            TracksCount      = Reader.Read16(data, ref position);
            PulsesPerQuarter = Reader.Read16(data, ref position);

            if ((PulsesPerQuarter & 0x8000) != 0)
            {
                throw new FormatException("Invalid timing mode (SMPTE timecode not supported)");
            }

            Tracks = new MidiTrack[TracksCount];

            for (var i = 0; i < TracksCount; i++)
            {
                Tracks[i] = ParseTrack(i, data, ref position, ref PulsesPerQuarter, ref BeatsPerMinute);
            }
        }
示例#2
0
 public HeaderChunk(MidiFormat Format, uint Length, uint NumTracks, ushort Division)
 {
     this.Format    = Format;
     this.Type      = ChunkType.Header;
     this.Length    = Length;
     this.NumTracks = NumTracks;
     this.Division  = new DivisionInfo(Division);
 }
示例#3
0
 public MidiFile(MidiFormat format, List <MidiTrack> tracks, ushort ticksPerqn)
 {
     _tracks = tracks ?? throw new ArgumentException("Tracks must not be null", nameof(tracks));
     if (_tracks.Count == 0)
     {
         throw new ArgumentException("No tracks provided to Midi File", nameof(tracks));
     }
     _format     = format;
     _ticksPerQn = ticksPerqn;
     Duration    = ProcessTempoMap();
 }
示例#4
0
    // Set midi format
    static void GetMidiFormatType(ref byte[] file, ref MidiFormat format)
    {
        uint data = ((uint)file[8] << 8) + ((uint)file[9] << 0);

        if (0 == data)
        {
            format = MidiFormat.Single_MultiChannelTrack;
        }
        else if (1 == data)
        {
            format = MidiFormat.OneOrMore_simultaneousTrack;
        }
        else if (2 == data)
        {
            format = MidiFormat.OneOrMore_independentTrack;
        }
    }
示例#5
0
 public static string GetMidiFormatString(MidiFormat value)
 {
     return(Enum.GetName(typeof(MidiFormat), value));
 }