Advertisement
guitarplayer616

play_concurrent

May 14th, 2022
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. local midi = require "midi"
  2.  
  3. local short_tune = "short-tune.mid"
  4. local batman = "batman-Brass.mid"
  5. local file = assert(io.open(batman, "rb"))
  6.  
  7. local notes = {}
  8. local i = 1
  9. function add_to_notes(cmd, channel, key, vel)
  10.   if cmd == "track" then
  11.     i = 1
  12.   elseif cmd == "noteOn" and vel > 0 then
  13.     key = key - 30
  14.     vel = vel * 3
  15.     if not notes[i] then
  16.       notes[i] = {}
  17.     end
  18.     if #notes[i] < 8 then
  19.       table.insert(notes[i], {key, vel})
  20.     end
  21.   elseif cmd == "deltatime" then
  22.     i = i + channel
  23.   end
  24. end
  25.  
  26. local tracks = midi.process(file, add_to_notes)
  27.  
  28. print("Loaded " .. tracks .. " midi tracks!")
  29.  
  30. file:close()
  31.  
  32. function sleep(k)
  33.   print("sleep "..tonumber(k))
  34. end
  35.  
  36. local max = 0
  37. for k,v in pairs(notes) do
  38.   if k > max then
  39.     max = k
  40.   end
  41. end
  42.  
  43. local prev = 1
  44. for i = 1,max do
  45.   if notes[i] then
  46.     sleep((i-prev)/1000)
  47.     prev = i
  48.     for _, note in pairs(notes[i]) do
  49.       print(note[1], note[2])
  50.     end
  51.   end
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement