Git Product home page Git Product logo

midi-parser's Introduction

Midi Parser in C#

A small midi parser written in C#. Just drop MidiFile.cs into your project and use it like:

var midiFile = new MidiFile("song.mid");

// 0 = single-track, 1 = multi-track, 2 = multi-pattern
var midiFileformat = midiFile.Format;

// also known as pulses per quarter note
var ticksPerQuarterNote = midiFile.TicksPerQuarterNote;

foreach(var track in midiFile.Tracks)
{
    foreach(var midiEvent in track.MidiEvents)
    {
        if(midiEvent.MidiEventType == MidiEventType.NoteOn)
        {
            var channel = midiEvent.Channel;
            var note = midiEvent.Note;
            var velocity = midiEvent.Velocity;
        }
    }

    foreach(var textEvent in track.TextEvents)
    {
        if(textEvent.TextEventType == TextEventType.Lyric)
        {
            var time = textEvent.Time;
            var text = textEvent.Value;
        }
    }    
}

Notes

  • Extracts channel number from the event type for easier use.
  • Provides easy access to events relevant to music performance like tempo and time signature.
  • The code is easy to extend to support other event types.

Some implementation details:

  • Events are stored without polymorphism using a small shared MidiEvent struct.
  • Text events are stored separately because of their reference to strings.
  • The parsing is done using byte arrays instead of streams since midi files are so small.
  • Only works in one direction so it can't write midi files.

Some event types are preprocessed during the parsing:

  • Note on events with zero velocity are normalized to note off events.
  • Tempo events are simplified to beats per minute.
  • Time signature events are simplified to standard notation.

Midi File

class MidiFile
{
    int Format;
    int TicksPerQuarterNote;
    MidiTrack[] Tracks;
}
class MidiTrack
{
    int Index;
    List<MidiEvent> MidiEvents;
    List<TextEvent> TextEvents;
}

Midi Events

struct MidiEvent
{
    int Time;
    byte Type;
    byte Arg1;
    byte Arg2;
    byte Arg3;
}
Type Arg1 Arg2 Arg3
NoteOff Channel Note Velocity
NoteOn Channel Note Velocity
PitchBendChange Channel Value Value
KeyAfterTouch Channel Note Amount
ChannelAfterTouch Channel Amount -
ProgramChange Channel Program -
ControlChange Channel BankSelect Value
Channel Modulation Value
Channel Volume Value
Channel Balance Value
Channel Pan Value
Channel Sustain Value
MetaEvent Tempo BeatsMinute -
TimeSignature Numerator Denominator
KeySignature SharpsFlats MajorMinor

Text Events

struct TextEvent
{
    int Time;
    byte Type;
    string Value;
}
Type
Text
TrackName
Lyric

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.