Prev Previous Post   Next Post Next
Old 11-03-2019, 09:32 AM   #11
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

@timbralzoom

This is not exactly what you requested, but can still be useful when working on hundreds or thousands of MIDI items:

Transpose selected MIDI items (all takes) to target octave
  • It transposes selected MIDI takes by octaves, so that the lowest note lands on that certain (user defined) octave
  • You can edit the last line to set the target octave



Code:
function get_notes_and_range(take)
  local MIDI_notes = {}
  local note_min, note_max = 127, 0
  local retval, note_cnt = reaper.MIDI_CountEvts(take)
  for n=1, note_cnt do
    local ret, selected, muted, startppqpos, endppqpos, chan, pitch, vel = reaper.MIDI_GetNote(take, n-1)
    MIDI_notes[#MIDI_notes+1] = {selected, muted, startppqpos, endppqpos, chan, pitch, vel}
    if pitch < note_min then note_min = pitch end
    if pitch > note_max then note_max = pitch end
  end
  return MIDI_notes, note_min, note_max
end


function transpose_to_octave(octave) -- octave 4 is middle C
  octave = octave + 1
  local item_cnt = reaper.CountSelectedMediaItems(0)
  for i=1, item_cnt do
    local item = reaper.GetSelectedMediaItem(0, i-1)
    if item then
      local take_count = reaper.GetMediaItemNumTakes(item)
      for t=1, take_count do
        local take = reaper.GetMediaItemTake(item, t-1)
        if take and reaper.TakeIsMIDI(take) then
          local note_tbl, note_min, note_max = get_notes_and_range(take)
          local note_min_transposed = note_min % 12 + octave*12
          local pitch_diff = note_min_transposed - note_min     
          for noteidx=1, #note_tbl do
            local n = note_tbl[noteidx]
            --boolean reaper.MIDI_SetNote(MediaItem_Take take, integer noteidx, optional boolean selectedIn, optional boolean mutedIn, optional number startppqposIn, optional number endppqposIn, optional number chanIn, optional number pitchIn, optional number velIn, optional boolean noSortIn)
            reaper.MIDI_SetNote(take, noteidx-1, n[1], n[2], n[3], n[4], n[5], n[6]+pitch_diff, n[7], false)
          end
        end
      end
    end
  end
  reaper.Undo_OnStateChange("Transpose")
end

transpose_to_octave(4) -- 4 is middle C (5 = middle C + 1 octave etc.)
spk77 is offline   Reply With Quote
 

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 11:06 PM.


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