Page tree
Skip to end of metadata
Go to start of metadata
readMidiFile(path)

Description

Function to read a MIDI file (.mid). The function creates a MIDI sequence table that contains the data of the MIDI file. See MIDI Sequence Table for details on the fields of the MIDI sequence table.

Available in: Controller.

Arguments

pathThe path and file name of the MIDI file.string

Return Values

Returns a MIDI sequence table. See MIDI Sequence Table for details.

Example

To explore the following script:

  1. Download C Major Scale.mid.
  2. Create an empty program and add a script module.
  3. Paste the script into the text editor of the script module and adjust the file path of readMidiFile to the location of the file on your system.
  4. Execute the script and play some notes.

 

-- read note-on events from a MIDI file and play them

midiSequence = readMidiFile("c:/temp/C Major Scale.mid") --[[ please set the file path to the location
                                                              of the file on your system before you run
                                                              the script ]]
numberOfEvents = #midiSequence.tracks[1].events
i = 1

print(midiSequence.songname..".mid loaded!")

function onNote(event)
    -- start from the beginning if the last event has been reached
    if i >= numberOfEvents then
        i = 1
    end
    -- find the next note-on event
    repeat
        event = midiSequence.tracks[1].events[i]
        i = i + 1
        if i > numberOfEvents then
            return
        end
    until event.type == EventType.noteOn
    -- print and play the note-on events
    print(event)
    local id = playNote(event.note, event.velocity, 0)
    wait(getBeatDuration()*0.5)
    releaseVoice(id)
end

1 Comment