Git Product home page Git Product logo

lua-midi's Introduction

lua-midi

A pure Lua implementation to read midi files using a callback function.

Usage

The library allows not only reading all tracks in a midi file at once, but also reading only the header (e.g. to a find out the track count) and then reading a single, specific midi track.

Reading all tracks in a midi file:

local midi = require "midi"

local file = assert(io.open("short-tune.mid"))
midi.process(file, print)

file:close()

Reading only the last track in a midi file:

local midi = require "midi"

local file = assert(io.open("short-tune.mid"))
local tracks = midi.processHeader(file) -- find number of tracks

file:seek("set") -- seek back to the beginning of the file
midi.processTrack(file, print, tracks)

file:close()

Library

The library consists of a single Lua file, namely midi.lua.

Reading full midi files

The following functions require a stream of a real midi file and use a callback to report individual midi events:

function midi.process(stream, callback, onlyHeader, onlyTrack)
function midi.processHeader(stream, callback)
function midi.processTrack(stream, callback, track)

All functions return the total number of tracks in the midi file.

Parameter Description
stream A stream (e.g. file*) that points to the start of a midi file. required
callback A callback function which is invoked for all midi events. optional
onlyHeader When set to true, only the header chunk will be processed. optional
onlyTrack When set to any integer, only the header chunk and track with this one-based index will be processed. optional
track Same as onlyTrack but required. required

Reading single midi events

The following function simply reads a single midi event (excluding the usually preceeding delta-time) from the given stream:

function midi.processEvent(stream, callback, runningStatus)

It returns how many bytes it had to read from the stream, followed by the updated runningStatus.

Parameter Description
stream A stream (e.g. file*) that points to a midi event. required
callback A callback function which is invoked for the midi event. required
runningStatus The running status of a previous midi event. optional

Examples

Prints all midi events in the given midi file.

Prints only the midi events of a single track in the midi file.

Handles only specific midi events using a dispatch table.

Lists the signatures, on how the callback is invoked, for each midi event.

Shows how to read single events from a stream.

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.