Old 05-24-2019, 11:13 AM   #1
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 91
Default How to return a midi note?

When I use the FNG functions to return midi notes into a table, I later get an error (on the 3rd last line) saying that the argument got light userdata instead of a number. I've tried using other functions such as 'reaper.MIDI_GetNote' but in Lua it doesn't seem to return a midi note. Is there a function available that returns a midi note or am I using the FNG functions wrong?

Code:
item = reaper.GetMediaItem(0, 0) -- NEED TO CHANGE TO GET SELECTED ITEM!
take = reaper.GetMediaItemTake(item, 0)
midi_take = reaper.FNG_AllocMidiTake(take)
midi_note_count = reaper.FNG_CountMidiNotes(midi_take)

function get_notes_table(all_notes)
  
  for i = 0, midi_note_count - 1 do
    
    local midi_note = reaper.FNG_GetMidiNote(midi_take, i)
    table.insert(all_notes, midi_note)
    
  end
  
  return all_notes

end

reaper.ShowConsoleMsg(tostring(all_notes))

all_notes = {}

notes_table = get_notes_table(all_notes)

for i = 0, midi_note_count - 1 do

  local midi_note, selected, muted, startppqpos, endppqpos = reaper.MIDI_GetNote(take, i)
  
  if i + 1 > midi_note_count - 1 then
      
    break
        
  else
      
    local midi_note_next, selected, muted, startppqpos_next, endppqpos_next = reaper.MIDI_GetNote(take, i + 1)
    reaper.MIDI_SetNote(take, notes_table[i + 2], nil, nil, endppqpos, endppqpos_next - (startppqpos_next - endppqpos))
      
  end  

end

Last edited by Sonorityscape; 05-24-2019 at 11:18 AM.
Sonorityscape is offline   Reply With Quote
Old 05-24-2019, 03:07 PM   #2
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 91
Default

I seemed to have gotten the script to work except for the end where the MIDI note position values are being set (4th last written line). From my debugging, the note positions are being updated each iteration through the loop to reflect exactly what I want, but they're not changing position in the MIDI editor. Does anyone know how to fix this?

Code:
item = reaper.GetMediaItem(0, 0) 
take = reaper.GetMediaItemTake(item, 0)
take_fng = reaper.FNG_AllocMidiTake(take)
note_count = reaper.FNG_CountMidiNotes(take_fng)

function get_notes_table(all_notes)
  
  for i = 0, note_count - 1 do
    
    local note_fng = reaper.FNG_GetMidiNote(take_fng, i)
    table.insert(all_notes, note_fng)
  
  end
  
  return all_notes

end

all_notes = {}

notes_table = get_notes_table(all_notes)


for i = 0, note_count - 1 do

  local note_start = reaper.FNG_GetMidiNoteIntProperty(notes_table[i+1], "POSITION")
  local note_end = (reaper.FNG_GetMidiNoteIntProperty(notes_table[i+1], "LENGTH")) + (note_start)
  --DEBUG
  reaper.ShowConsoleMsg("start note " .. i .. ": " .. tostring(note_start) .. "\n")
  reaper.ShowConsoleMsg("end note " .. i .. ": " .. tostring(note_end) .. "\n")

  
  if i + 1 > note_count - 1 then
      
    break
        
  else
    
    reaper.FNG_SetMidiNoteIntProperty(notes_table[i + 2], "POSITION", note_end)

  end  

end

reaper.UpdateArrange()
Sonorityscape is offline   Reply With Quote
Old 05-24-2019, 06:39 PM   #3
Sonorityscape
Human being with feelings
 
Sonorityscape's Avatar
 
Join Date: Aug 2016
Posts: 91
Default

It works after deallocating the memory.
Sonorityscape is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 01:56 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.