Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER General Discussion Forum

Reply
 
Thread Tools Display Modes
Old 12-30-2019, 12:13 PM   #1
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default proportional transition times for automatically created envelope points?

Hi -- I have a mouse modifier set for left drag on an envelope segment. When a time selection is present, it automatically makes four points so the region within the segment is dragged.

The setting Prefs->Editing Behavior->Envelope Display->"Transition time for automatically created envelope edge points" adds a transition, which is great.

I'm wondering if there is any way (via a plugin, SWS, or maybe this is a FR) to make this transitional time proportional to the size of the time selection. E.g. set the transition to X% of the time selection length.

The motivation is that for e.g. a 5 minute time selection, I'd like a 15 second transition, for 30 seconds a 1.5 second transition, for 3 seconds, a 150ms transition, for .5 seconds a 2.5ms transition, etc, so I can just set it to 5% and be done.

The existing "one size fits all" is sufficient for avoiding clicks, but not for auto-creating natural fades that are appropriate for the size of the time selection made.

Any thoughts? Thanks!
clepsydrae is offline   Reply With Quote
Old 12-30-2019, 04:41 PM   #2
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

Here's a Lua script you could try. It's continously running in background, could be assigned to a toolbar button for toggling on/off. The transition time percentage can be set in script's USER CONFIG AREA (currently set to 5 as per your example). Needs SWS installed.

Code:
-- USER CONFIG AREA -----------------------------------------------------------
ENVELOPE_EDGE_POINTS_TRANSITION_TIME = 5 -- percentage of time selection length in sec.

SHOW_CURRENT_TRANSITION_TIME = false -- true/false (for debugging)
------------------------------------------------------- END OF USER CONFIG AREA


local _, _, sectionID, cmdID, _, _, _ = reaper.get_action_context()
reaper.SetToggleCommandState(sectionID, cmdID, 1);
reaper.RefreshToolbar2(sectionID, cmdID);

local origTransitionTime = reaper.SNM_GetDoubleConfigVar("envtranstime", -666)
local transitionTimePercentage = ENVELOPE_EDGE_POINTS_TRANSITION_TIME/100
local lastProjectStateChangeCount = -1
local lastTimeSelLength = -1
function Main()
  local projectStateChangeCount =  reaper.GetProjectStateChangeCount(0)
  if lastProjectStateChangeCount ~= projectStateChangeCount then
    local startTime, endTime = reaper.GetSet_LoopTimeRange(false, false, 0, 0, false)
    timeSelLength = endTime - startTime
    if (lastTimeSelLength ~= timeSelLength) and (endTime - startTime > 0) then
      reaper.SNM_SetDoubleConfigVar("envtranstime", timeSelLength * transitionTimePercentage)
      if SHOW_CURRENT_TRANSITION_TIME == true then
        envtranstime = reaper.SNM_GetDoubleConfigVar("envtranstime", -666)
        reaper.ShowConsoleMsg(envtranstime .. "\n")
      end
    end
  end
  lastProjectStateChangeCount = projectStateChangeCount
  lastTimeSelLength = timeSelLength
  reaper.defer(Main)
end

Main()

function DoAtExit()
  reaper.SNM_SetDoubleConfigVar("envtranstime", origTransitionTime)
  -- set toggle state to off
  reaper.SetToggleCommandState(sectionID, cmdID, 0);
  reaper.RefreshToolbar2(sectionID, cmdID);
end

reaper.atexit(DoAtExit)

Last edited by nofish; 12-30-2019 at 05:10 PM.
nofish is offline   Reply With Quote
Old 12-30-2019, 07:04 PM   #3
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default

Brilliant, thanks a lot! Works great. The fact that it restores the original transition time on close is a nice touch, too! :-)

I notice that if I exit reaper with the script still toggled on, it gives a warning:

Code:
.../Scripts/autofade_envelope_segment.lua:37: 'reaper.SNM_SetDoubleConfigVar' is unknown
...there's probably a simple way to check for that object still being around when exiting?

I also realized that I probably also want a variant where the fade time is proportional to the horizontal zoom level and not the time selection, so using your excellent example I whipped up this version:

Code:
-- USER CONFIG AREA -----------------------------------------------------------
ENVELOPE_EDGE_POINTS_NUM_PIXELS = 20 -- width of transition time in pixels

SHOW_CURRENT_TRANSITION_TIME = false -- true/false (for debugging)
------------------------------------------------------- END OF USER CONFIG AREA


local _, _, sectionID, cmdID, _, _, _ = reaper.get_action_context()
reaper.SetToggleCommandState(sectionID, cmdID, 1);
reaper.RefreshToolbar2(sectionID, cmdID);

local origTransitionTime = reaper.SNM_GetDoubleConfigVar("envtranstime", -666)
local lastProjectStateChangeCount = -1
local lastpps = -1
function Main()
  local projectStateChangeCount =  reaper.GetProjectStateChangeCount(0)
  if lastProjectStateChangeCount ~= projectStateChangeCount then
    local pps = reaper.GetHZoomLevel()
    if (lastpps ~= pps) then
      reaper.SNM_SetDoubleConfigVar("envtranstime", ENVELOPE_EDGE_POINTS_NUM_PIXELS/pps)
      if SHOW_CURRENT_TRANSITION_TIME == true then
        envtranstime = reaper.SNM_GetDoubleConfigVar("envtranstime", -666)
        reaper.ShowConsoleMsg(envtranstime .. "\n")
      end
    end
  end
  lastProjectStateChangeCount = projectStateChangeCount
  lastpps = pps
  reaper.defer(Main)
end

Main()

function DoAtExit()
  reaper.SNM_SetDoubleConfigVar("envtranstime", origTransitionTime)
  -- set toggle state to off
  reaper.SetToggleCommandState(sectionID, cmdID, 0);
  reaper.RefreshToolbar2(sectionID, cmdID);
end

reaper.atexit(DoAtExit)
clepsydrae is offline   Reply With Quote
Old 12-30-2019, 09:31 PM   #4
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

You're welcome.
It's best when the requester(s) do scripting themselves and can further refine the scripts to their needs.

I found it an interesting idea btw. (maybe I'll use it myself too, not sure yet), that's what motivated me to try it.

Quote:
I notice that if I exit reaper with the script still toggled on, it gives a warning:
Hm..maybe Reaper unloads SWS before the atexit() function is run?

Last edited by nofish; 12-30-2019 at 09:46 PM.
nofish is offline   Reply With Quote
Old 12-30-2019, 09:57 PM   #5
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default

Quote:
Originally Posted by nofish View Post
Hm..maybe Reaper unloads SWS before the atexit() function is run?
Is SNM_* an SWS call? So, that's the only way to set config vars?

It seems that the config var is not saved on exit anyway (i.e. it's reset back to it's non-script value on next reaper load) so is the thing to do here just to remove the SNM call on atExit?
clepsydrae is offline   Reply With Quote
Old 12-30-2019, 10:49 PM   #6
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

Quote:
Originally Posted by clepsydrae View Post
Is SNM_* an SWS call? So, that's the only way to set config vars?
Yes and yes (afaik).

Quote:
It seems that the config var is not saved on exit anyway (i.e. it's reset back to it's non-script value on next reaper load) so is the thing to do here just to remove the SNM call on atExit?
Ah right, it doesn't get saved on exit because the value isn't written to REAPER.ini, where it's read from at next Reaper start.
Better than removing would maybe doing (not tested if it works though)

Code:
if reaper.SNM_SetDoubleConfigVar ~= nil then
  reaper.SNM_SetDoubleConfigVar("envtranstime", origTransitionTime)
end
so it'd be restored when stopping the script while Reaper is running.
nofish is offline   Reply With Quote
Old 12-31-2019, 03:32 AM   #7
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 3,008
Default

Running scripts still remember unloaded functions (reaper.SNM_SetDoubleConfigVar is not automatically set to nil once SWS has been unloaded). And because of this issue, the resulting "'xyz' is unknown" error message cannot be caught using Lua's protected call mechanism.

Also NamedCommandLookup still recognizes SWS actions after they have been unloaded...
cfillion is offline   Reply With Quote
Old 12-31-2019, 10:13 AM   #8
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

Thanks.
Do you by chance have a suggestion how to handle this then? I mean handling 'better' than just removing the SNM_SetDoubleConfigVar() call in atexit().
(The purpose of this was meant to restore the value it had before the script was started when the script is stopped by the user.)
nofish is offline   Reply With Quote
Old 12-31-2019, 10:33 AM   #9
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 3,008
Default

reaper.EnumProjects(0) returns nil when REAPER is exiting:

Code:
reaper.atexit(function()
  if reaper.EnumProjects(0) then -- check whether SWS functions are still available (REAPER is not exiting)
    reaper.SNM_SetDoubleConfigVar("envtranstime", origTransitionTime)
  end
end)

Last edited by cfillion; 12-31-2019 at 10:38 AM.
cfillion is offline   Reply With Quote
Old 12-31-2019, 11:52 AM   #10
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default

Thanks all -- I will amend my scripts.
clepsydrae is offline   Reply With Quote
Old 01-19-2020, 06:25 PM   #11
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default

Hey all -- I just discovered that this script doesn't work on my Windows install of reaper, and apparently the reason is that GetProjectStateChangeCount doesn't change on Windows when the time selection is changed, but it does on Linux.

At least, that's what I'm seeing. Is there a config that might affect this? My two reapers are pretty much identical in configuration, but maybe I'm overlooking something.

nofish -- did you develop this on mac or linux? (or windows??)
clepsydrae is offline   Reply With Quote
Old 01-19-2020, 07:21 PM   #12
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,373
Default

Quote:
Originally Posted by clepsydrae View Post
nofish -- did you develop this on mac or linux? (or windows??)
I did it on Windows.
I think it may be because of 'Preferences -> General -> Undo settings, Include selection: time' which I have ticked here, could this be the problem?
(It just occured to me now though.)
nofish is offline   Reply With Quote
Old 01-25-2020, 08:23 PM   #13
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 2,527
Default

Quote:
Originally Posted by nofish View Post
I did it on Windows.
I think it may be because of 'Preferences -> General -> Undo settings, Include selection: time' which I have ticked here, could this be the problem?
(It just occured to me now though.)
Yep, that was it. (Well, i didn't get a chance to actually test, but that option was not clicked in windows.) Thanks!
clepsydrae 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 06:27 AM.


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