-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2-read-single-track.lua
More file actions
49 lines (41 loc) · 1.39 KB
/
2-read-single-track.lua
File metadata and controls
49 lines (41 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local midi = require "midi"
-- It is possible to read only a single track.
-- This will efficiently skip tracks using stream:seek().
local file = assert(io.open("resources/short-tune.mid", "rb"))
-- First we need to find out how many tracks there are in total.
-- The callback is always optional and defaults to an empty function.
local tracks = midi.processHeader(file)
print("Found " .. tracks .. " midi tracks!")
-- Now that we know the track count, let's load just the last one.
-- The file already has been read partially, so we need to seek back or reopen the file altogether.
assert(file:seek("set"))
midi.processTrack(file, print, tracks)
file:close()
--[[ Output:
Found 2 midi tracks!
header 1 2 480
track 2
sequencerOrTrackName Piano
keySignature 0 C major
noteOn 1 48 0.62992125984252
noteOn 1 52 0.62992125984252
deltatime 911
noteOn 1 48 0.0
noteOn 1 52 0.0
deltatime 49
noteOn 1 50 0.62992125984252
noteOn 1 57 0.62992125984252
deltatime 683
noteOn 1 50 0.0
noteOn 1 57 0.0
deltatime 37
noteOn 1 52 0.62992125984252
noteOn 1 55 0.62992125984252
noteOn 1 59 0.62992125984252
deltatime 227
noteOn 1 52 0.0
noteOn 1 55 0.0
noteOn 1 59 0.0
deltatime 1
endOfTrack
]]