Old 04-14-2017, 03:14 PM   #1
Matt Mayfield
Human being with feelings
 
Join Date: Aug 2016
Posts: 62
Default Advice on a custom script to make MIDI note items?

Hi all,

I'm looking to write a ReaScript that will do this (pseudocode example):

Code:
Dialog - enter a note number ("N")
Check: Is there a MIDI item selected?
    If yes {
           Store the start and end points of the selected item
           Delete the selected item
           Create a 1-beat MIDI item at the start point of the previously-selected item on the selected track
           Put note N in it, at 127 velocity, 1/8th note length
           Extend the item (with loop) up through the stored endpoint of the previously-selected item
          }
	If no {
           Check: Is there a time selection?
           If yes {
               Create a 1-beat MIDI item at the start of time selection on the selected track
               Put note N in it, at 127 velocity, 1/8th note length
               Extend the item (with loop) until the end of the time selection
               }
           If no {
               Create a 1-beat MIDI item at the edit cursor on the selected track
               Put note N in it, at 127 velocity, 1/8th note length
               }
          }
Make the just-created item a custom color based on note N (e.g. 0=black, 1=white, 2=red, 3=cyan, 4=purple, 5=green, 6=blue, 7=yellow, etc.)
End script
Can anyone suggest a couple code snippets that would show how to do those things, or some existing scripts to pull ideas from?

Thanks,

Matt
Matt Mayfield is offline   Reply With Quote
Old 04-16-2017, 05:56 PM   #2
Matt Mayfield
Human being with feelings
 
Join Date: Aug 2016
Posts: 62
Default

Here's what I ended up with, in case anyone wants to do the same thing:

Code:
track = reaper.GetSelectedTrack(0, 0)
okay, note = reaper.GetUserInputs("Note Number", 1, "Note Number", "1")

if okay == false then goto ending
end

colors = {0x01000000, 0x01ffffff, 0x01ff0000, 0x0100ffff, 0x01ff00ff, 0x0100ff00,
          0x010000ff, 0x01ffff00, 0x01ff7f00, 0x017f3f00, 0x01ff7f7f, 0x013f3f3f,
          0x017f7f7f, 0x017fff7f, 0x017f7fff, 0x01bfbfbf}

starttime, endtime = reaper.GetSet_LoopTimeRange2(0, false, false, 0, 0, false)
qnotetime = 60 / reaper.Master_GetTempo()

if starttime == endtime then -- no time selection exists

   -- check if a mediaItem is selected, and if so take its start & end times and delete it
   if reaper.CountSelectedMediaItems() == 0 then
      starttime = reaper.GetCursorPosition()
      endtime = starttime + qnotetime
   else
      selectedItem = reaper.GetSelectedMediaItem(0, 0)
      starttime = reaper.GetMediaItemInfo_Value(selectedItem, "D_POSITION")
      endtime = starttime + reaper.GetMediaItemInfo_Value(selectedItem, "D_LENGTH")
      reaper.DeleteTrackMediaItem(track, selectedItem)
   end      
end

length = (endtime - starttime)

midiItem = reaper.CreateNewMIDIItemInProj(track, starttime, starttime + qnotetime)
midiTake = reaper.GetActiveTake(midiItem)
reaper.MIDI_InsertNote(midiTake, false, false, 0, 480, 1, note, 127)

reaper.SetMediaItemInfo_Value(midiItem, "B_LOOPSRC", 1)
reaper.SetMediaItemLength(midiItem, length, false)
reaper.GetSetMediaItemTakeInfo_String(midiTake, 'P_NAME', note, true)
itemColor = math.fmod(note,16)+1
reaper.SetMediaItemTakeInfo_Value(midiTake, 'I_CUSTOMCOLOR', colors[itemColor])

::ending::
Matt Mayfield 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 04:51 AM.


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