usage: require("midi")
the midi library allows you to play midi files on synthesizers. you can load a midi file from a disk or binary string
if you want to somehow transfer the sound from the library to another source, provide it with the virtual synthesizer API (you will have to do it yourself)
attention. this library may not understand some midi formats and may not perceive instruments correctly. It is better to use a library for reproducing NBS files instead
methods:
- midi.create():midiPlayer - creates a new midi player
midiPlayer methods:
- midiPlayer:load(disk, path:string) - load a midi file to the player
- midiPlayer:loadStr(content:string) - loads a midi file from a string
- midiPlayer:setSynthesizers(synthesizers:table) - sets the synthesizers table
- midiPlayer:tick() - call each tick in the callback_loop. automatically makes a correction from getSkippedTicks()
- midiPlayer:isPlaying():boolean - returns the current state of the player, that is, whether it is playing now
- midiPlayer:start() - starts playback of the track
- midiPlayer:stop() - stops the playback of the track
- midiPlayer:setVolume(volume:float) - sets the volume from 0.1 to 1. By default, the volume 0.1 (the standard volume of robot heads) volume 1 increases it by 10 times. It is better not to set the volume to more than 0.3
- midiPlayer:setSpeed(speed:float) - sets the playback speed, by default 1. if you set 2, it will be twice as fast, if you set 0.5, it will be twice as slow. this is a fractional number
- midiPlayer:setDefaultInstrument(id:number) - sets the ID of the tool to be used if it was not possible to select a tool from the "instrumentTable" (default: 4)
- midiPlayer:setNoteShift(shift:number) - sets the offset of the notes relative to the notes of the robot's head (default: -50)
- midiPlayer:setNoteAlignment(alignment:number) - if set to 0, the notes that have gone beyond the playback limit will not be played. if 1 is set, the notes that have gone beyond the limit will still be reproduced but with distortion (default: 1)
midiPlayer fields:
- midiPlayer.instrumentTable - the current matching table for the instrument selection. if you want to play the entire track with default instrument, then assign this table to an empty one.
default: {
square = {8, 1},
guitar = {9, 1},
piano = 4,
synth = 3,
bass = 5,
drum = 2
}
- midiPlayer.synthesizers - current synthesizer table (default: {})
- midiPlayer.instrument - the current default instrument (default: 4)
- midiPlayer.volume (default: 0.1)
- midiPlayer.noteshift (default: -50)
- midiPlayer.notealigment (default: 1)
- midiPlayer.speed (default: 1)
--plays a midi file from a disk
--for this example to work, import the "midis" example to disk
local midi = require("midi")
local synthesizers = getComponents("synthesizer")
local disk = getComponent("disk")
local player = midi.create()
player:load(disk, "2.mid")
player:setSynthesizers(synthesizers)
player:setSpeed(1)
player:setNoteShift(-50)
player:setNoteAlignment(1)
player:setVolume(0.1)
player:setDefaultInstrument(4)
player:start()
function callback_loop()
if _endtick then
player:stop()
return
end
if not player:isPlaying() then
player:start()
end
player:tick()
end