0

I have made myself a Midi file reader in pure Lua and I decided that I would make a visualizer in love2d and So I did first I made it display the currently playing notes by looping through them and deciding which ones were on.

That worked fine and I constantly got around 300fps but then I decided I wanted a preview kinda like the synthesia program but I'm having a hard time thinking of a good way to store them so that I don't have to loop through each note per pixel to decide which are on (this method makes it go at about 15-30fps) at the time in the future (or past).

I have tried making a table with indexes as the time in seconds and the values tables with the current notes that are on but I cant seem to make it work. If anyone has any suggestions as to how I would store and access these notes from variables quickly your help would be really appreciated. (also I know I can use C or C++ or a library but I want to make one in pure Lua)

The notes are currently stored in a table of notes that are only in chronological order by track so like track1note1, track1note2, track1note3, track2note2 etc...

The table is structured like this

    table = {
--{start time(in seconds),end time(in seconds),note,track,velocity}, note1
{0.00043,1.387289,44,1,127},--note2
--etc...
}
1
  • I can give the code or parts of it if you want.
    – columna1
    Feb 1, 2014 at 19:08

1 Answer 1

0

I'm having a hard time thinking of a good way to store them so that I don't have to loop through each note per pixel

How about looping through each pixel per note? Your first solution looks fine to me:

for i = start, #note_table do
    local t0, t1, note, track, vel = unpack(note_table[i])

    -- break the loop if the current note is fully off-screen

    -- draw the note number 'i' in here,
    -- highlight the corresponding piano key, etc.
end

Since you mentioned the table is sorted chronologically, by carefully selecting the start index you'll only draw the notes you need.

Addressing the original question, you can reduce the memory usage by grouping each note attribute in it's own table. This may gain net a slight performance gain. eg.

note_table = {
    t0   = { ... },
    t1   = { ... },
    note = { ... },
    -- etc...
}

Nevertheless, if you are looking for something more involved, you should take a look at interval trees.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.