View Single Post
Old 06-02-2017, 03:34 AM   #1187
reddiesel41264
Human being with feelings
 
reddiesel41264's Avatar
 
Join Date: Jan 2012
Location: North East UK
Posts: 492
Default

Here's my version - thanks to Julian for help with notation events. My script only works on the selected notes in a selected take, it doesn't work across multiple ones.

There are some variables at the top that you can edit. The numbers in DYNAMICS are for each velocity range, so as I have set it up, anything below 15 is ppp, anything below 30 but above 15 is pp etc. You can adjust these thresholds to suit you but I think they are a pretty good starting point and I've taken accented notes into account which will have a higher velocity than the surrounding notes of the same dynamic.

If you want the range of dynamics to go from pppp to ffff then you need to comment out the first DYNAMICS line and uncomment the second one, same for the MARKINGS.

Code:
--[[ 
* ReaScript Name: MIDI Add Velocity Based Dynamics To Selected Notes
* Version: 01/06/2017
* Author: David Healey.
--]] 

--USER SETTINGS
DYNAMICS = {15, 30, 45, 60, 75, 90, 105, 127} --ppp to fff
--DYNAMICS = {8, 20, 31, 42, 53, 64, 80, 96, 112, 127} --pppp to ffff
MARKINGS = {"ppp", "pp", "p", "mp", "mf", "f", "ff", "fff"} --ppp to fff
--MARKINGS = {"pppp", "ppp", "pp", "p", "mp", "mf", "f", "ff", "fff", "ffff"} --pppp to ffff
---------------------------

function main()

	e = reaper.MIDIEditor_GetActive() --Get active editor
	take = reaper.MIDIEditor_GetTake(e) --Get active take

	if take ~= nil then

		reaper.Undo_BeginBlock();

		retval, notes, ccs, sysex = reaper.MIDI_CountEvts(take) --Count note and cc events

		for n = 0, notes, 1 do 
			retval, selected, muted, ppq, endppq, chan, pitch, vel = reaper.MIDI_GetNote(take, n) --Get note data

			--Find which dynamic range the velocity value fits to determine which dynamic marking to use
			if selected == true then
				reaper.MIDIEditor_OnCommand(e, 42173) --Remove any existing note text

				for i in pairs(DYNAMICS) do
					if vel <= DYNAMICS[i] then
						reaper.MIDI_InsertTextSysexEvt(take, true, false, ppq, 0xF, "TRAC dynamic " .. MARKINGS[i] .. " ypos 2.000")
						break;
					end
				end
			end
		end

		reaper.MIDI_Sort(take)
		reaper.Undo_EndBlock("MIDI Add Velocity Based Dynamics To Selected Notes",-1);
		reaper.UpdateArrange();
	end
end

main()
__________________
http://librewave.com - Freedom respecting instruments and effects
http://xtant-audio.com/ - Purveyor of fine sample libraries (and Kontakt scripting tutorials)
reddiesel41264 is offline   Reply With Quote