--====================================================================== 
--[[ 
* ReaScript Name: kawa_ENV_ClearPointsInTimeSelection. 
* Version: 2017/01/09 
* Author: kawa_ 
* Author URI: http://forum.cockos.com/member.php?u=105939 
* Repository: BitBucket - kawaCat - ReaScript-M2B 
* Repository URI: https://bitbucket.org/kawaCat/reascript-m2bpack/ 
--]] 
--====================================================================== 
bit32={band=function(a,b)return a&b;end,bor=function(a,b)return a|b;end,bxor=function(a,b)return a~b;end,bnot=function(a)return ~a;end,rshift=function(a,n)return a>>n;end,lshift=function(a,n)return a<<n;end,};   function checkReaperAppVersion()local e=false for n,t in pairs(reaper)do if(tostring(n)=="Envelope_GetParentTake")then e=true break;end end if(e==false)then reaper.ShowMessageBox("This script Need Reaper Version 5.24 ~ ","Can Not Execute",0)end return e;end function check_SWS_Function(n)local e=false e=reaper.APIExists("BR_EnvAlloc")if(e==false and n~=true)then reaper.ShowMessageBox("This Scripts Need SWS/S&M Extenstion. \n".."\n".."Please Check  www.sws-extension.org .","stop",0)end return e end function getDirSep()return(package.config:sub(1,1));end function getExecutedScriptName(o)local n,e,n,n,n,n,n=reaper.get_action_context();local n="kawa "..string.gsub(e,(#e)-20,(#e));local t=getDirSep();local t="(.*"..t..")"local n=string.match(e,t)or n;local e=string.gsub(e,n,"");e=e:gsub("_"," ")if(o==true)then e=e:match("(.*)%.lua")end return e;end function sqrt2(t)local n=t;local e;if(t==0)then return 0;end while(n<e)do e=n;n=bit32.rshift((e+t/e),1);n=(e+t/e)/2;end return e;end function sqrt3(e)local e=e;local n=(e+e/e)/2;local e=(n+e/n)/2;return x4;end function sqrt4(e)local e=e;local n=(e+e/e)/2;local n=(n+e/n)/2;local e=(n+e/n)/2;return e;end function sqrt10(e)local e=e;local n=(e+e/e)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local e=(n+e/n)/2;return e;end function sqrt8(e)if(e==0)then return 0;end local e=e;local n=(e+e/e)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local e=(n+e/n)/2;return e;end function sqrt9(e)if(e==0)then return 0;end local e=e;local n=(e+e/e)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local n=(n+e/n)/2;local e=(n+e/n)/2;return e;end local t={}local n=-1;local function e(e)n=e;local n=math.pi/2/e;for e=1,e do table.insert(t,math.cos(e*n))end end local function l(e)local o=1;if(e<0)then e=-e;end e=e/math.pi;e=(e-(math.floor)(e/2)*2);if(e>1)then e=2-e;end if(e>.5)then e=1-e;o=-1;end local e=math.floor(e*2*(n))+1;if(e>n)then e=e-1 end return(o*t[e]);end local function e(e)return l(e-(math.pi/2))end local function n(e)if e==0 then return 1 else return e*n(e-1)end end local function l(o)local e=0;local t=0;for t=1,5 do e=e+(o^(4*t-3))/n(4*t-3);end for e=1,5 do t=t+(o^(4*e-1))/n(4*e-1);end local e=e-t;return e;end local function e(e)return l(e+(math.pi/2))end local t=4/math.pi;local o=-4/(math.pi*math.pi);local function e(e)e=e-math.pi local n=e if(e<0)then n=-e end return-(t*e+o*e*n);end function deepcopy(n)local t=type(n)local e if t=='table'then e={}for t,n in next,n,nil do e[deepcopy(t)]=deepcopy(n)end setmetatable(e,deepcopy(getmetatable(n)))else e=n end return e end function metaTableCopy(...)local e={};local n={...}local function t(n,e)for t=1,#e do local e=e[t][n];if e then return e;end end end setmetatable(e,{__index=function(o,e)local n=t(e,n)o[e]=n;return n;end});e.__index=e;return e;end function mergeClass(...)return metaTableCopy(...);end function table.toString(t)local function d(e)return string.format("%q",e)end local s="";local function e(e)s=s..e end;local c,n="   ","\n"local r,t={t},{[t]=1}e("return {"..n)for o,a in ipairs(r)do e("-- Table: {"..o.."}"..n)e("{"..n)local i={}for l,o in ipairs(a)do i[l]=true local l=type(o)if l=="table"then if not t[o]then table.insert(r,o)t[o]=#r end e(c.."{"..t[o].."},"..n)elseif l=="string"then e(c..d(o)..","..n)elseif l=="number"then e(c..tostring(o)..","..n)end end for l,o in pairs(a)do if(not i[l])then local a=""local i=type(l)if i=="table"then if not t[l]then table.insert(r,l)t[l]=#r end a=c.."[{"..t[l].."}]="elseif i=="string"then a=c.."["..d(l).."]="elseif i=="number"then a=c.."["..tostring(l).."]="end if(a~="")then i=type(o)end if i=="table"then if not t[o]then table.insert(r,o)t[o]=#r end e(a.."{"..t[o].."},"..n)elseif i=="string"then e(a..d(o)..","..n)elseif i=="number"then e(a..tostring(o)..","..n)elseif i=="boolean"then e(a..tostring(o)..","..n)end end end e("},"..n)end e("}")return s;end function table.fromString(e)local e=load(e);local e=e()if(type(e)~="table")then local e={}return e end for n=1,#e do local o={}for t,l in pairs(e[n])do if type(l)=="table"then e[n][t]=e[l[1]]end if type(t)=="table"and e[t[1]]then table.insert(o,{t,e[t[1]]})end end for o,t in ipairs(o)do e[n][t[2]],e[n][t[1]]=e[n][t[1]],nil end end return e[1]end function table.doubleCheck(l,o,e)local n=false local t=e or true local e=ipairs;if(t==false)then e=pairs end for n,e in e(l)do if(e==o)then return true;end end return n;end function table.metaTableCopy(e)return metaTableCopy(e)end function table.deepcopy(e)return deepcopy(e)end function table.mergeTable(o,...)local n={}local t={...}local e=ipairs;if(o)then e=pairs;end;for o,t in e(t)do for t,e in e(t)do table.insert(n,e);end end return n;end function _toIntColor(t,e,n)local t=string.format("%x",t)local e=string.format("%x",e)local n=string.format("%x",n)if(#t<2)then t="0"..t end if(#e<2)then e="0"..e end if(#n<2)then n="0"..n end local e="01"..n..e..t;return tonumber(e,16)end function _toIntColor2(e,n,t)return e+math.floor(n*256)+math.floor((t*65536))end function _colorToNative(n,e,t)return reaper.ColorToNative(n,e,t);end function _nativeToColor(e)local t,n,e=reaper.ColorFromNative(e)return t,n,e;end function HSVToRGB(r,a,f)local o,l,i,s,d,e,n,c,t local r=r local a=a if(r>360)then r=r-360;elseif(r<0)then r=r+360;end if(a>1)then a=1;elseif(a<0)then a=0;end e=math.floor(255*f)if(e>255)then e=255;elseif(e<0)then e=0;end if(a==0)then o=e;l=e;i=e;else s=math.floor(r/60)d=r/60-s n=math.floor(e*(1-a))if(n<0)then n=0;elseif(n>255)then n=255;end c=math.floor(e*(1-d*a))if(c<0)then c=0;elseif(c>255)then c=255;end t=math.floor(e*(1-(1-d)*a))if(t<0)then t=0;elseif(t>255)then t=255;end if(s==0)then o=e;l=t;i=n;elseif(s==1)then o=c;l=e;i=n;elseif(s==2)then o=n;l=e;i=t;elseif(s==3)then o=n;l=c;i=e;elseif(s==4)then o=t;l=n;i=e;elseif(s==5)then o=e;l=n;i=c;else o=e;l=t;i=n;end end return o,l,i end function RGBToHSV(l,n,t)l,n,t=l/255,n/255,t/255;local o,i=math.max(l,n,t),math.min(l,n,t)local e,c,r=0,0,0 r=(o+i)/2 if(o==i)then e,c=0,0 else local a=o-i local c=0 if(r>.5)then c=a/(2-o-i);else c=a/(o+i);end if(o==l)then e=(n-t)/a if(n<t)then e=e+6;end elseif(o==n)then e=(t-l)/a+2 elseif(o==t)then e=(l-n)/a+4 end e=e/6 end return e*360,c,r end function color_Mul(e,n)return{e[1]*n,e[2]*n,e[3]*n,e[4]}end function color_Darker(n,e)local e=e or .8;return color_Mul(n,e)end function color_Brighter(n,e)local e=e or 1.2;return color_Mul(n,e)end function color_getInvert(e,t)local o=1-e[1]local l=1-e[2]local i=1-e[3]local n=e[4]if(t)then n=1-e[4];end return{o,l,i,n}end function rotPoint(n,t,o,l,e)local o=o-n local l=l-t local i=o*math.cos(e)-l*math.sin(e)local o=o*math.sin(e)+l*math.cos(e)local e=n+i local n=t+o return e,n end function scalePoint(t,n,o,l,e)local t=((o-t)*e)+t;local e=((l-n)*e)+n;return t,e end function string:split(e)local n,e=e or":",{}local n=string.format("([^%s]+)",n)self:gsub(n,function(n)e[#e+1]=n end)return e end function stringIter(n)local e=0;local t=string.len(n);return function()e=e+1;if(e>t)then return nil else return e,string.sub(n,e,e)end end end function stringIterMB(n)local e=0;local t=string.len(n);return function()e=e+1;if(e>t)then return nil else return e,mbStrSub(n,e,e)end end end function toByteString(n)local e={}for t,n in stringIter(n)do table.insert(e,string.byte(n));end return table.concat(e,",")end function restorByteString(n)local e={}for t,n in ipairs(n)do table.insert(e,string.char(n));end return table.concat(e,"")end function getMbLengh(l)local e=l local i=""local e=nil local n=0 local t=1 local o=0 while(true)do i=string.sub(l,t,t)e=string.byte(i)if(e==nil)then break;end if e<128 then n=1 elseif e<224 then n=2 elseif e<240 then n=3 elseif e<248 then n=4 elseif e<252 then n=5 elseif e<254 then n=6 end t=t+n o=o+1 end return o end function isContainMultiByteStr(o)local i=false;local e=o local l=""local e=nil local n=0 local t=1 while(true)do l=string.sub(o,t,t)e=string.byte(l)if(e==nil)then break;end if e<128 then n=1 elseif e<224 then n=2 elseif e<240 then n=3 elseif e<248 then n=4 elseif e<252 then n=5 elseif e<254 then n=6 end t=t+n if(n>1)then i=true;break;end end return i end function mbStrSub(i,s,c)if type(i)~="string"then return"-"end local l=""local r=""local e=nil local o=0 local n=0 local t=1 local a=1 local a={}while(true)do r=string.sub(i,t,t)e=string.byte(r)if(e==nil)then break;end if e<128 then n=1 elseif e<224 then n=2 elseif e<240 then n=3 elseif e<248 then n=4 elseif e<252 then n=5 elseif e<254 then n=6 end local e=string.sub(i,t,t+(n-1))t=t+n if(e~=nil)then table.insert(a,e)end o=o+1 end local e=s or 1 local n=c or o if(e<1)then e=1 end if(n<e)then n=e end if(e>n)then e=n-1 end if(n>o)then n=o end for e=e,n do l=l..a[e]end return l end DEFINE_MIN_DB=-96 DEFINE_MIN_DB_VALUE=(10^(DEFINE_MIN_DB*.05))function math.valueToDecivel(e)local e=math.log(e,10)*20 if(e<DEFINE_MIN_DB)then e=DEFINE_MIN_DB end return e end function math.decivelToValue(e)local e=(10^(e*.05))if(e<DEFINE_MIN_DB_VALUE)then e=DEFINE_MIN_DB_VALUE end return e end function math.getNearEven(e)local e=e if((e%2)~=0)then e=e-1;end return math.floor(e)end function math.getNearOdd(e)local e=e if((e%2)==0)then e=e-1;end return math.floor(e)end function getManhattanDist(o,e,n,t)return math.abs(o-n)+math.abs(e-t);end function getEuclideanDist(e,n,t,o)local t=math.abs(e-t);local e=math.abs(n-o);local e=math.sqrt((t^2)+(e^2));return e;end function getChebyshevDist(o,e,n,t)local n=math.abs(o-n);local e=math.abs(e-t);if(n>e)then return n;else return e;end end local a=123/128 local r=51/128 function approx_distance(n,i,e,l)local t;local o;local n=math.abs(n-e);local e=math.abs(i-l);t=math.min(n,e)o=math.max(n,e)return(a*o)+(r*t)end function checkValueLength_circle(e,n,o)local e=e;local t=math.max(n,o);local n=math.min(n,o);if(e>t)then e=n;end if(e<n)then e=t;end return e;end local t='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'function base_64_enc(l)return((l:gsub('.',function(e)local e,n='',e:byte()for t=8,1,-1 do e=e..(n%2^t-n%2^(t-1)>0 and'1'or'0')end return e;end)..'0000'):gsub('%d%d%d?%d?%d?%d?',function(o)if(#o<6)then return''end local e=0 for n=1,6 do e=e+(o:sub(n,n)=='1'and 2^(6-n)or 0)end return t:sub(e+1,e+1)end)..({'','==','='})[#l%3+1])end function base_64_dec(e)e=string.gsub(e,'[^'..t..'=]','')return(e:gsub('.',function(e)if(e=='=')then return''end local e,t='',(t:find(e)-1)for n=6,1,-1 do e=e..(t%2^n-t%2^(n-1)>0 and'1'or'0')end return e;end):gsub('%d%d%d?%d?%d?%d?%d?%d?',function(t)if(#t~=8)then return''end local e=0 for n=1,8 do e=e+(t:sub(n,n)=='1'and 2^(7-n)or 0)end return string.char(e)end))end local e={}e.char={"A","B","C","D","E","F","1","2","3","4","5","6","7","8","9"}e.isHyphen={[9]=1,[14]=1,[19]=1,[24]=1}e.used={}function generateGUID()local n=0 math.randomseed(reaper.time_precise()*os.time()/1e3)e.currentGuid=nil while(true)do n=n+1 if(n>20)then return false end e.pass={}for n=1,36 do if e.isHyphen[n]then e.x='-'else e.a=math.random(1,#e.char)e.x=e.char[e.a]end table.insert(e.pass,e.x)end z=nil e.currentGuid=tostring(table.concat(e.pass))if e.used[e.currentGuid]==nil then e.used[e.currentGuid]=e.currentGuid return(e.currentGuid)else end end end function fileExists(e)local e=io.open(e,"rb")if(e~=nil)then e:close()end return(e~=nil)end function readFileWithSplitedLine(e)if(fileExists(e)==false)then return{}end local n={}for e in io.lines(e)do table.insert(n,e);end return n end function readAll(e)local e=io.open(e,"rb")local n=e:read("*all")e:close()return n end function getBezierPointQB(e,t,o,i,l,a,r)local n=1-e;local t=e*e*a+2*e*n*i+n*n*t;local e=e*e*r+2*e*n*l+n*n*o;return t,e;end function getBezierAngleQB(e,l,i,n,t,a,r)local o=1-e;local n=2*(e*(a-n)+o*(n-l));local e=2*(e*(r-t)+o*(t-i));return math.atan2(e,n);end local n=-1 math.randomseed(reaper.time_precise()*os.time()/1e3)local S=40331 local r=40330 local e=40646 local v=40647 local e=40757 DEFINE_MAX_ENVELOPE_PROCESS_NUM=500 DEFINE_ENV_SHOW_ERROR_MESSAGEBOX=false DEFINE_AUTO_TIMESLECTION_MODE=false DEFINE_SAFTYTIME_SEC=3 DEFINE_ENV_POINT_ID_USE_GUID=false DEFINE_SAFTY_ROUND_TIME=5e-4 DEFINE_DELETE_ONE_POINTAROUND_TIME=.001 local t="kawa_EnvelopeFunc2"local P="lastAutoSelection"local f="lastEdtingPoints"local E="lastGneerateCurveType"local g="lastGneerateCurveRATE"local _="lastTimeSelectionStartTime"local h="lastTimeSelectionEndTime"local p="lastUndoSTring"local m="lastProcessTime"local i="envelope_AutoTimeSelection_DetectionMODE"local o={"4,0","4,1","2,0","2,1","1,0","1,1","0.5,0","0.5,1","0.25,0","0.25,1"}local u={4,3,2,1,5,6,7,8}local a=30 if(package.config:sub(1,1)=="\\")then else end local e={x=0,y=.5,tension=0,shape=0,selection=false}function createEnvelopePointClass(i,n,t,o,l)local e={}e.x=i or 0 e.y=n or .5 e.tension=o or 0 e.shape=t or 0 e.selection=l or true return e end function createEnvelopeFunc(c,T,l)local e={}e.trackEnvelope=nil e.BR_envelopeInfo=nil e.loopRangeInfo={}e.timeSelectionRangeInfo={}e.editingPoints={}e.originalPoints={}e.actionContextInfo={}e._isExistSelectedPoints=nil e.m_existMaxNoteIdx=0 e.m_needToClearInsertArea=false;e.m_needTimeSelectionLengthFitEditingPoint=true e.m_maxProcessCount=DEFINE_MAX_ENVELOPE_PROCESS_NUM e.m_undStr=c e.m_isNeedTimeSelectionCheck=(l==nil or l==true)e.m_onAfterCopyFunctionTale={}e.m_takeOffsetTime_Checked=nil e.m_isTakeEnvelope_Checked=nil e.m_isEnbaleTimeSelection_Checked=nil e.m_isEnbaleLoopSelection_Checked=nil e.m_isTempoEnvTrack_Checked=nil e.m_isPitchEnvTrack_Checked=nil e.m_isMuteEnvTrack_Checked=nil e.m_isDefaultShape_Checked=nil e.m_getGridPrjQN_Checked=nil e.m_isDiffFromLastTimeSelection_Checked=nil e.m_isDiffFromLastUndo_Checked=nil e.m_isStorActionContextInfo_Checked=nil function e:_check_SWS_Function(n)local e=false e=reaper.APIExists("BR_EnvAlloc")if(e==false and n~=true)then reaper.ShowMessageBox("This Scripts Need SWS/S&M Extenstion. \n".."\n".."Please Check  www.sws-extension.org .","stop",0)end return e end function e:getTrackEnvelope()return self.trackEnvelope;end function e:setTrackEnvelope(e)self.trackEnvelope=e self:_storeBR_EnvelopeInfo();self:_prepareTrackEnvelope(T)end function e:setMaxProcessPointsCount(e)self.m_maxProcessCount=e end function e:canExecuteProcess()if(self:_check_SWS_Function()==false)then return false;end local function e(e)if(DEFINE_ENV_SHOW_ERROR_MESSAGEBOX==true)then reaper.ShowMessageBox(e,"Stop Process",0);end end if(self:_isEnableTimeSelection()==false)then e("is Note Enable Time Selection")return false;end local n=self:_isExistSelectedTrackEnvelope()if(self:_isExistSelectedTrackEnvelope()==false)then e("is Note Selected Envelope")return false;end if(self.m_maxProcessCount<0)then return true end local e=#self.editingPoints local n=self:_getAllPointsCountsFromEnvTrack()local e=(e<self.m_maxProcessCount)if(e~=true)then reaper.ShowMessageBox("Target Points counts are higher than "..tostring(self.m_maxProcessCount).." counts.\n".."stop Process. \n".."----------------------------------------------------\n".."All Points in Track Envelope"..tostring(n).."Points","Stop Process",0)end local e=(e==true)return e end function e:getTrackEnvelope()return self.trackEnvelope end function e:isExistSelectedPoints()if(self._isExistSelectedPoints==nil)then for n,e in ipairs(self.editingPoints)do if(e.selection==true)then self._isExistSelectedPoints=true break end end if(self._isExistSelectedPoints==nil)then self._isExistSelectedPoints=false end end return self._isExistSelectedPoints end function e:isTempoEnvelopeTrack()if(self.m_isTempoEnvTrack_Checked==nil)then local n,e=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)local n=e:split("\n")local e=false if(n[1]:find("TEMPOENV")~=nil)then e=true end self.m_isTempoEnvTrack_Checked=e end return self.m_isTempoEnvTrack_Checked end function e:isPitchEnvelopeTrack()if(self.m_isPitchEnvTrack_Checked==nil)then local n,e=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)local n=e:split("\n")local e=false if(n[1]:find("PITCHENV")~=nil)then e=true end self.m_isPitchEnvTrack_Checked=e end return self.m_isPitchEnvTrack_Checked end function e:isMuteEnvelopeTrack()if(self.m_isMuteEnvTrack_Checked==nil)then local n,e=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)local n=e:split("\n")local e=false if(n[1]:find("MUTEENV")~=nil)then e=true end self.m_isMuteEnvTrack_Checked=e end return self.m_isMuteEnvTrack_Checked;end function e:isTakeEnvelope()return self:_isTakeEnvelope()end function e:isFaderScalingValueMode()return self.BR_envelopeInfo.faderScalingOut;end function e:getEnvelopeMaxMinCenterVal()local t=self.BR_envelopeInfo.minValueOut local n=self.BR_envelopeInfo.maxValueOut local o=self.BR_envelopeInfo.centerValueOut local l=n-t if(self:isPitchEnvelopeTrack()==true)then local i,e=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)local e=e:split("\n")local e=tonumber(e[6]:split(" ")[3])if(e==-1)then e=reaper.SNM_GetIntConfigVar("pitchenvrange",0)end t=-e n=e o=0 l=n-t end return t,n,o,l end function e:getDefaultShape()if(self.m_isDefaultShape_Checked==nil)then local n,e=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)local e=e:split("\n")local e=e[6]:split(" ")[2]or 0 self.m_isDefaultShape_Checked=e;end return self.m_isDefaultShape_Checked end function e:getAutoTimeSelectionDetectionMODE()local e=reaper.GetExtState(t,i)local n=(e=="true")if(e=="")then self:setAutoTimeSelectionDetectionMODE(false)end return n end function e:toggleAutoTimeSelectionDetectionMODE()local e=self:getAutoTimeSelectionDetectionMODE()local e=(e==false)self:setAutoTimeSelectionDetectionMODE(e)end function e:setAutoTimeSelectionDetectionMODE(e)reaper.SetExtState(t,i,tostring(e),true)end function e:selectAllPoints()local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.selection=true end end function e:unselectAllPoints()local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.selection=false end end function e:selectPointsInSelectedRange()self:unselectAllPoints()self:flush()self:resetAndReInit()local e=self:_detectTargetEnvelopePoints()if(self:_isEnableTimeSelection()==false)then e=self.editingPoints end for n,e in ipairs(e)do e.selection=true end self:_needForceReGetEditingPoints()end function e:clearDetectionRangePoint()local n=self:_detectTargetEnvelopePoints()local e={}for t,n in ipairs(n)do table.insert(e,n)end self:deletePoints(e)end function e:clearTimeSelectionRangePoint()if(self:_isExistSelectedTrackEnvelope()==false)then return end self:_updateRangeInfo();if(self:_isEnableTimeSelection()==true)then local o=self.timeSelectionRangeInfo.startTime;local t=self.timeSelectionRangeInfo.endTime;local e=self:_getTakeEnvelopeClipOffsetTime()local n=self:_getTakeEnvelopePlayRateOffset()reaper.DeleteEnvelopePointRange(self.trackEnvelope,(o-e)*n,(t-e)*n)end end function e:fixValue()local t=self:_detectTargetEnvelopePoints()local e,n=reaper.GetUserInputs("fix Points Value",7,"value( % ) (0  ~ 100)","50,--,--,--,--,--,--")if(e~=true)then return end local e=tonumber(n:split(",")[1])or 60 e=e/100 e=math.max(math.min(e,100),0)for t,n in ipairs(t)do n.value=e end end function e:changeValue_rel(n)local e=self:_detectTargetEnvelopePoints()for t,e in ipairs(e)do e.value=e.value*n end end function e:changeValue_add(e)self:shiftValue(e)end function e:selectAllAndResetAndReInit()if(self:_isEnableTimeSelection()==false)then return;end local e=self.timeSelectionRangeInfo.startTime local t=self.timeSelectionRangeInfo.endTime self:_changeTimeSelectionRange(e-.001,t,false)reaper.Main_OnCommandEx(r,0,n)self:_changeTimeSelectionRange(e,t,false)self:resetAndReInit()end function e:strechPostion(n)local e=self:_detectTargetEnvelopePoints()if(#e<1)then return end local t=n or .9 local n=e[1].positionTime for o,e in ipairs(e)do local o=e.positionTime-n local n=n+(o*t)e.positionTime=n end end function e:strechPostion_Type2(n)self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()if(#e<2)then return end local l=n or .9 local o,n,n=self:_getPointsLength(e)local t={}for e,n in ipairs(e)do local e=deepcopy(n)local n=n.positionTime-o local n=o+(n*l)e.positionTime=n e.selection=true table.insert(t,e)end self:deletePoints(e)self:insertPoints(t)self:_setNeedToClearInsertPointsArea(true);end function e:changePointTension(n)if(self:isPitchEnvelopeTrack()==true or self:isTempoEnvelopeTrack()==true)then return end local e=self:_detectTargetEnvelopePoints()for t,e in ipairs(e)do e.shape=5 local t=e.tension+1 local n=t*n n=n-1 e.tension=math.max(math.min(n,1),-1)end end function e:reduceExpandValue(n)local e=self:_detectTargetEnvelopePoints()if(#e<1)then return end;local o=n or .9 local n,t=self:_getMinMaxValue(e)local t=t-n local n=n+(t/2)for t,e in ipairs(e)do local t=e.value-n e.value=n+(t*o)end end function e:shiftValue(n)local e=self:_detectTargetEnvelopePoints()for t,e in ipairs(e)do e.value=e.value+n if(self:isFaderScalingValueMode()==true)then e.value=math.max(math.min(e.value,2),0)else e.value=math.max(math.min(e.value,1),0)end end self:_needForceReGetEditingPoints();end function e:shiftPosition(e)local t=self:_detectTargetEnvelopePoints()local n=reaper.TimeMap2_QNToTime(n,e)for t,e in ipairs(t)do e.positionTime=e.positionTime+n e.positionTime=math.max(e.positionTime,0)end self:_needForceReGetEditingPoints();self:_setNeedToFitTimeSelectionRangeEditingPoints(false)local t=self.timeSelectionRangeInfo.startTime local e=self.timeSelectionRangeInfo.endTime self:_changeTimeSelectionRange(t+n,e+n,true)end function e:shiftPosition_Type2(e)self:_toHighPriorityTimeSelection()local o=self:_detectTargetEnvelopePoints()local n=reaper.TimeMap2_QNToTime(n,e)local i=self:_getTakeEnvelopeClipOffsetTime()local e=self:_getTakeEnvelopePlayRateOffset()local t={}local l={}for e,o in ipairs(o)do local e=deepcopy(o)e.positionTime=o.positionTime+n e.positionTime=math.max(e.positionTime,0)e.selection=true table.insert(t,e)table.insert(l,o)end self:_needForceReGetEditingPoints();local e,e,e=self:_getPointsLength(t)self:_setNeedToClearInsertPointsArea(true);self:_setNeedToFitTimeSelectionRangeEditingPoints(false)local e=self.timeSelectionRangeInfo.startTime local o=self.timeSelectionRangeInfo.endTime self:deletePointsRange(e+i+n,o+i+n)self:deletePoints(l)self:insertPoints(t)self:_changeTimeSelectionRange(e+n,o+n,true)end function e:invertValueAndPosition(n,t)local e=self:_detectTargetEnvelopePoints()local a=n local r=t if(#e<2)then return end local n=self:_getTakeEnvelopeClipOffsetTime()local o,t=self:_getPointsLength(e)if(self:isExistSelectedPoints()==false)then o=self.timeSelectionRangeInfo.startTime-n t=self.timeSelectionRangeInfo.endTime-n end local i={}local l={}for n,e in ipairs(e)do local n=deepcopy(e)if(a==true)then n.value=1-e.value end if(r==true)then local e=self:_getRelativePosition2(e.positionTime,o,t)local e=1-e local e=self:_relativePositionToTime2(e,o,t)n.positionTime=e end n.selection=false;table.insert(l,e)table.insert(i,n)end self:deletePoints(l)self:insertPoints(i)end function e:splitRange(e)local o=self.timeSelectionRangeInfo.startTime local t=self.timeSelectionRangeInfo.endTime local n=t-o local i=e or 4 local n=self:_getTakeEnvelopeClipOffsetTime()local a=self:_getTakeEnvelopePlayRateOffset()local l=self:getDefaultShape()function _getPt(e,n)return{positionTime=e,value=self:_getValueAtPos(e*a),shape=l,tension=0,selection=true}end local e={}local l=o-n local a=t-n local a=a-l local a=a/i for n=1,i-1 do local n=l+(a*n)table.insert(e,_getPt(n))end table.insert(e,_getPt(o-n))table.insert(e,_getPt(t-n))self:insertPoints(e)self:_setNeedToFitTimeSelectionRangeEditingPoints(false)self:_needForceReGetEditingPoints();end function e:splitRangeType2(e)local n=e or 4 local t=self:getDefaultShape()local o=self:_getTakeEnvelopePlayRateOffset()local e=self:_getTakeEnvelopeClipOffsetTime()function _getPt(e)return{positionTime=e,value=self:_getValueAtPos(e*o),shape=t,tension=0,selection=true}end local e=self:_detectTargetEnvelopePoints()local l={}local t=1 for t=1,#e do local o=t+1 if(o>#e)then break end e[t].selection=true e[o].selection=true local t=e[t].positionTime local e=e[o].positionTime local e=e-t local o=e/n for e=1,n-1 do local e=t+(o*e)table.insert(l,_getPt(e))end end if(self:isExistSelectedPoints()==false)then self:splitRange(n)end self:_needForceReGetEditingPoints();self:insertPoints(l)self:_setNeedToFitTimeSelectionRangeEditingPoints(false)end function e:splitRange_FromGrid(e)if(self:_isEnableTimeSelection()==false)then return end self:_toHighPriorityTimeSelection()local e=self:_getTakeEnvelopeClipOffsetTime()local t=self:_getTakeEnvelopePlayRateOffset()local o=self:getDefaultShape()local function i(e)return{positionTime=e,value=self:_getValueAtPos(e*t),shape=o,tension=0,selection=true}end local t,e=self:_detectTargetRange()local o=e-t if(e-t>a)then reaper.MB(" Stop . timeSelection range  longer than "..tostring(a).." sec.","Process Stop",0)return end;local a=self:getProjectGridProjQN()local o=reaper.TimeMap2_timeToQN(n,t)local r=reaper.TimeMap2_timeToQN(n,e)local o=o-(o%a)+a local o=o local l={}while o<r do local e=reaper.TimeMap2_QNToTime(n,o)table.insert(l,i(e))o=o+a end if(self:isExistSelectedPoints()==false)then table.insert(l,i(t))table.insert(l,i(e))end self:insertPoints(l)self:_setNeedToFitTimeSelectionRangeEditingPoints(false)self:_needForceReGetEditingPoints();end function e:quantizeEnvelopePoint()local e=self:_detectTargetEnvelopePoints()local r=self:getProjectGridProjQN()local i=self:_getTakeEnvelopeClipOffsetTime();local t=self:_getTakeEnvelopePlayRateOffset()local a={}local l={}for e,t in ipairs(e)do local e=deepcopy(t)local o=reaper.TimeMap2_timeToQN(n,t.positionTime+i)local r=(o%r)local o=o-r e.positionTime=reaper.TimeMap2_QNToTime(n,o)e.positionTime=e.positionTime-i table.insert(a,e)table.insert(l,t)end self:deletePoints(l)self:insertPoints(a)self:_needForceReGetEditingPoints();end function e:quantizeEnvelopePointType2()local e=self:_detectTargetEnvelopePoints()local o=self:getProjectGridProjQN()local r=self:_getTakeEnvelopeClipOffsetTime();local s={}local c={}for e,l in ipairs(e)do local e=deepcopy(l)local t=reaper.TimeMap2_timeToQN(n,l.positionTime+r)local a=(t%o)local i=t local d=1 if(a<=o/2)then local e=a*d i=t-e else local e=(o-a)*d i=t+e end e.positionTime=reaper.TimeMap2_QNToTime(n,i)e.positionTime=e.positionTime-r table.insert(s,e)table.insert(c,l)end self:deletePoints(c)self:insertPoints(s)end function e:interpolationPoints(t,e,n)local l=self:_detectTargetEnvelopePoints()if(#l<2)then return end local o=t or .3 local s=e or false local c=n or 0 local d,f,e=self:_getPointsLength(l)local r,a=self:_getFirstEndPointValue(l)local function i(o,t)local n if(t==0)then n=o elseif(t==1)then local function t(e)e=e*2 if e<1 then return .5*e*e*e else e=e-2 return .5*(e*e*e+2)end end local e=t(o)n=e elseif(t==2)then n=math.cos(math.rad(360*o))elseif(t==3)then end return n end for n,e in ipairs(l)do local n=self:_getRelativePosition2(e.positionTime,d,f)if(s==true)then local t=e.value-a local n=i(1-n,c)local n=a+t*n e.value=(e.value)*(1-o)+(n)*o else local t=e.value-r local n=i(n,c)local n=r+t*n e.value=(e.value)*(1-o)+(n)*o end e.value=math.max(math.min(e.value,1),0)if(self:isFaderScalingValueMode()==true)then e.value=math.max(math.min(e.value,2),0)else e.value=math.max(math.min(e.value,1),0)end e.selection=true;end self:_needForceReGetEditingPoints();end function e:ligthWeightPointCount(e)local n=e or 4 n=math.max(2,n)local t=self:_detectTargetEnvelopePoints()local e=0 local o={}for e,l in ipairs(t)do if(e~=1 and e~=#t)then if(e%n==0)then table.insert(o,l)end end end self:deletePoints(o)self:_needForceReGetEditingPoints();end function e:lightWeightPointCountType2(l)self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()local n=0 local t={}local n={}local o=#e local o=math.floor(o/l+.5)local i,a,l=self:_getPointsLength(e)local a=l/o local r=self:_getTakeEnvelopePlayRateOffset()local l=nil for e=1,o+1 do local n=i+(a*(e-1))local e=self:_getValueAtPos(n*r)local n={selection=false,positionTime=n,value=e,shape=0,tension=0}if(l~=e)then table.insert(t,n)l=e end end for t,e in ipairs(e)do table.insert(n,e)end self:deletePoints(n)self:insertPoints(t)self:_needForceReGetEditingPoints();end function e:lightWeightPointCountType3(r)self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()if(#e<1)then return;end local n=0 local t={}local l={}local i={}local n,n,n=self:_getPointsLength(e)local o=e[1]for l,n in ipairs(e)do local i=o.positionTime local a=o.value local r=n.positionTime local c=n.value local i=getEuclideanDist(i,a,r,c)n.distanceFromLastPt=i o=n if((l~=1)and(l~=#e))then table.insert(t,n)end end table.sort(t,function(n,e)return(n.distanceFromLastPt<e.distanceFromLastPt)end)local n=(#t*r)for t,e in ipairs(t)do if(t<n)then table.insert(i,e)else table.insert(l,e)end end self:deletePoints(i)self:insertPoints(l)self:_needForceReGetEditingPoints();end function e:randomPointValue()math.randomseed(reaper.time_precise()*os.time()/1e3)local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.value=math.random()end end function e:randomPointPosition()math.randomseed(reaper.time_precise()*os.time()/1e3)local e=self:_detectTargetEnvelopePoints()if(self:_isEnableTimeSelection()==false)then return end local n={}local o={}local i=self.timeSelectionRangeInfo.startTime local a=self.timeSelectionRangeInfo.endTime local l=self:_getTakeEnvelopeClipOffsetTime()for e,t in ipairs(e)do local e=deepcopy(t)e.positionTime=self:_relativePositionToTime2(math.random(),i,a)e.positionTime=e.positionTime-l e.selection=false table.insert(n,t)table.insert(o,e)end self:deletePoints(n)self:insertPoints(o)end function e:randomPointShape()math.randomseed(reaper.time_precise()*os.time()/1e3)local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.shape=math.floor(math.random(0,5))end end function e:showEnvelopeStateChunk()local e=self:_detectTargetEnvelopePoints()local e=""local n=""for t=1,20 do n=n.."+++"end e=n.."\n"local o,t=reaper.GetEnvelopeStateChunk(self.trackEnvelope,"",false)e=e..t e=e..n.."\n"e=e.."this scripts for debug ".."\n"e=e..n.."\n"local n=self.BR_envelopeInfo.totalPointsCount e=e.."total Points count : "..tostring(n).."  \n"reaper.ShowConsoleMsg(e)end function e:shownPointsInfo_More()local e=self:_detectTargetEnvelopePoints()if(self:isExistSelectedPoints()==false and self:_isEnableTimeSelection()==false)then e=self.editingPoints end local t=""local o=""for e=1,20 do o=o.."+++"end t=o.."\n"local n,n,n,n=self:getEnvelopeMaxMinCenterVal()local n=self:_getTakeEnvelopePlayRateOffset()for e,n in ipairs(e)do n.selection=true local o=self:_normarizeValueToScaledValue(n.value)local e="id:"..tostring(n.idx)e=e..",position:"..tostring(n.positionTime)e=e..",value:"..tostring(o)e=e..",normizeValue:"..tostring(n.value)e=e..",formatedValue:"..tostring(reaper.Envelope_FormatValue(self.trackEnvelope,o))e=e..",shape:"..tostring(n.shape)e=e..",tension:"..tostring(n.tension)e=e..",selection:"..tostring(n.selection)e=e.."\n"t=t..e end t=t..o.."\n"reaper.ShowConsoleMsg(t)end function e:shownPointsInfo()local n=self:_detectTargetEnvelopePoints()if(self:isExistSelectedPoints()==false and self:_isEnableTimeSelection()==false)then n=self.editingPoints end local e=""local t=""for n=1,20 do t=t.."+++"end e=t.."\n"local o=-1e6 local l=1e6 for n,e in ipairs(n)do e.selection=true o=math.max(o,e.value)l=math.min(l,e.value)end local a,s,c=self:_getPointsLength(n)local i=self:_getTakeEnvelopePlayRateOffset()local i=self:_getTakeEnvelopeClipOffsetTime()local r=self:time2FormatStr(a+i)local a=self:time2FormatStr(s+i)local i=self:time2FormatStr(c+i)e=e.."Points Num : "..tostring(#n).."\n"e=e.."Max Value : "..tostring(o).."\n"e=e.."Min Value : "..tostring(l).."\n"e=e.."Start Time : "..tostring(r).."  \n"e=e.."End Time : "..tostring(a).."  \n"e=e.."Length Time : "..tostring(i).."  \n"e=e..t.."\n"reaper.ShowConsoleMsg(e)end function e:selecetEvenOddPoint(n)if(self:_isEnableTimeSelection()==false)then return end self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()if(#e<2)then return end local n=n or false local function t(e)return((e%2)==0)end for o,e in ipairs(e)do if(t(o)==n)then e.selection=true else e.selection=false end end end function e:selectPointThanFirstPpoint(n)self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()if(#e<2)then return end local o=n or false local n,t=self:_getFirstEndPointValue(e)for t,e in ipairs(e)do local t=(e.value>=n)if(t==o)then e.selection=true else e.selection=false end if(e.value==n)then e.selection=true end end end function e:autoSelection(l)self:_toHighPriorityTimeSelection()local i=self:_detectTargetEnvelopePoints()if(#i<2)then return end local e=reaper.GetExtState(t,P)local a=l or 1 if(e=="")then e=o[1]end local l=nil for t,n in ipairs(o)do if(e==n)then local e=t+a if(e>#o)then e=1 end if(e<1)then e=#o end l=o[e]break end end local a=tonumber(l:split(",")[1])local o=tonumber(l:split(",")[2])for t,e in ipairs(i)do local n=reaper.TimeMap2_timeToQN(n,e.positionTime)n=math.floor(n*100+.6)/100 local t=false if(math.floor((n/a)%2)==o)then e.selection=true else e.selection=false end end reaper.SetExtState(t,P,l,false)table.insert(self.m_onAfterCopyFunctionTale,function(e)e.editingPoints=e.originalPoints end)end function e:_stepPointShape(o,t)local function l(n)local e=0 if(self:isTempoEnvelopeTrack()==true)then if(n==0)then e=1 elseif(n==1)then e=0 else e=0 end elseif(self:isMuteEnvelopeTrack()==true)then e=1 else if(n==0)then e=1 elseif(n==1)then e=2 elseif(n==2)then e=3 elseif(n==3)then e=4 elseif(n==4)then e=5 elseif(n==5)then e=0 else e=0 end end return e end local function i(n)local e=0 if(self:isTempoEnvelopeTrack()==true)then if(n==0)then e=1 elseif(n==1)then e=0 else e=0 end elseif(self:isMuteEnvelopeTrack()==true)then e=1 else if(n==0)then e=5 elseif(n==1)then e=0 elseif(n==2)then e=1 elseif(n==3)then e=2 elseif(n==4)then e=3 elseif(n==5)then e=4 else e=0 end end return e end local e=0 if(o==true)then e=l(t)else e=i(t)end return e end function e:nextPointShape()local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.shape=self:_stepPointShape(true,e.shape)end end function e:prevPointShape()local e=self:_detectTargetEnvelopePoints()for n,e in ipairs(e)do e.shape=self:_stepPointShape(false,e.shape)end end function e:nextPointShapeAll()local e=self:_detectTargetEnvelopePoints()local n=nil for t,e in ipairs(e)do if(t==1)then n=self:_stepPointShape(true,e.shape)end e.shape=n end end function e:prevPointShapeAll()local n=self:_detectTargetEnvelopePoints()local e=nil for t,n in ipairs(n)do if(t==1)then e=self:_stepPointShape(false,n.shape)end n.shape=e end end function e:duplicateSimple(o,t)local e=self:_detectTargetEnvelopePoints()if(#e<2)then self:_toHighPriorityTimeSelection()e=self:_detectTargetEnvelopePoints()end if(#e<2)then return end local l={}local c=o or false;local r=t or false;local i,o,t=self:_getPointsLength(e)local i=reaper.TimeMap2_timeToQN(n,i)local a=reaper.TimeMap2_timeToQN(n,o)local o=a-i local s=self:_getTakeEnvelopeClipOffsetTime()t=t+.001 if(r==true)then t=t*-1 o=o*-1 end for e,n in ipairs(e)do local e=deepcopy(n)if(c==true)then e.positionTime=e.positionTime+t+.001 else local n=self:_getReleativePQN2(e.positionTime+.001,i,a)e.positionTime=self:_relativePQNToTime2(n,i+o,a+o)e.positionTime=e.positionTime end e.selection=true table.insert(l,e)n.selection=false end local e=self:_getTakeEnvelopeClipOffsetTime()local t,o,i=self:_getPointsLength(l)self:deletePointsRange(t,o)self:insertPoints(l)self:_setNeedToClearInsertPointsArea(true);self:_setNeedToFitTimeSelectionRangeEditingPoints(false)reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(t+e,o+e,true)local o=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,t+e,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,o,0,0)self:_needForceReGetEditingPoints()end function e:duplicateMusical(e,t)self:_toHighPriorityTimeSelection()local u=self:_detectTargetEnvelopePoints()if(#u<2)then self:_toHighPriorityTimeSelection()end if(#u<2)then return end local m=e or false;local f=t or false local t=self:_getTakeEnvelopeClipOffsetTime()local e=self:_getTakeEnvelopePlayRateOffset()local i=self.timeSelectionRangeInfo.startTime local c=self.timeSelectionRangeInfo.lengthTime local s=self.timeSelectionRangeInfo.endTime local r=reaper.TimeMap2_timeToQN(n,i)local d=reaper.TimeMap2_timeToQN(n,s)local o=d-r local function a(e)local n=0 local o,t,l=reaper.TimeMap_GetTimeSigAtTime(0,i+.001)local l=0;local t=o*(4/t)if(e<=.25)then n=.25-e elseif(e<=.5)then n=.5-e elseif(e<=1)then n=1-e elseif(e<=2)then n=2-e elseif(e<=t)then n=t-e else local o=math.floor(e/t);local t=(o+1)*t;n=t-e;end return n end local l=o-1e-6 local e=a(l)l=l+e local l=reaper.TimeMap2_QNToTime(n,r+e)local a=l-i if(f==true)then a=l-l c=c*-1 o=o*-1 e=e*-1 end local l={}local function f(l)local n=0 if(m==true)then local e=l+c+.001+a n=e else local i=l+t local l=self:_getReleativePQN2(l+t,r,d)n=self:_relativePQNToTime2(l,r+o+e,d+o+e)n=n-t end return n end local function p()local n=self:getDefaultShape()local function e(e)local e={positionTime=e,value=self:_getValueAtPos(e),shape=n,tension=0,selection=true}return e end if(isTimeBase==true)then table.insert(l,e(i-t+a))table.insert(l,e(s-t+a))else local t=f(i)local n=f(s)table.insert(l,e(t))table.insert(l,e(n))end end for e,n in ipairs(u)do local e=metaTableCopy(n)e.positionTime=f(e.positionTime)e.selection=true table.insert(l,e)n.selection=false end if(m==true)then local e=i+c+.001+a local o=s+c+.001+a reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(e,o,true)local i=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,e,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,i,false,false)self:deletePointsRange(e,o)self:_deletePointsAreaFromReaper(e-t,o-t)self:insertPoints(l)else local t=reaper.TimeMap2_QNToTime(n,r+o+e)local e=reaper.TimeMap2_QNToTime(n,d+o+e)reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(t,e,true)local o=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,t,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,o,0,0)self:deletePointsRange(t,e)self:insertPoints(l)end self:_setNeedToClearInsertPointsArea(true);self:_setNeedToFitTimeSelectionRangeEditingPoints(false)self:_needForceReGetEditingPoints()end function e:timeSelectionPointsFitToLoopArea()if(self:getAutoTimeSelectionDetectionMODE()==true)then self:_toHighPriorityTimeSelection()end if(self:_isEnableTimeSelection()==false or self:_isEnableLooplection()==false)then return end local t=self:_getContainRangePoints(self.editingPoints)local a=self.timeSelectionRangeInfo.startTime local o=self.timeSelectionRangeInfo.endTime local e,e=self:_getPointsLength(t)local i=self.loopRangeInfo.startTime local l=self.loopRangeInfo.endTime local n={}local e={}local e=self:_getTakeEnvelopeClipOffsetTime()for r,t in ipairs(t)do local o=self:_getRelativePosition2(t.positionTime,a-e,o-e)local o=self:_relativePositionToTime2(o,i-e,l-e)t.selection=self:getAutoTimeSelectionDetectionMODE()local e=deepcopy(t)e.positionTime=o e.selection=(self:getAutoTimeSelectionDetectionMODE()==false)table.insert(n,e)end local function a(t,n,o)local e={positionTime=t-e,value=n,shape=self:getDefaultShape(),tension=0,selection=true}return e end local o,t,r=self:_getPointsLength(n)table.insert(n,a(o-.001,self:_getValueAtPos(o-e)))table.insert(n,a(t,self:_getValueAtPos(t-e)))self:_deletePointsAreaFromReaper(i-e,l-e)self:deletePointsRange(o,t)self:insertPoints(n)self:_setNeedToFitTimeSelectionRangeEditingPoints(false)self:_needForceReGetEditingPoints()end function e:getAndCheck_Curve_RateOrder(e)local n=reaper.GetExtState(t,EXT_STATE_KEY_LAST_EDITCURSOL);local e=e end function e:generateSinPoints(r)if(self:_isEnableTimeSelection()==false)then return end local i=self.timeSelectionRangeInfo.startTime local a=self.timeSelectionRangeInfo.endTime if(i==a)then return;end;local function s(o,n,e,l,i)local t=.5 if(e==0 or e==5)then local e=math.sin(math.rad(360*n*o))t=(e*.5)+.5 elseif(e==1)then local e=1/(2*n)local e=1*((math.floor(o/e)%2)*-1)t=(e)+1 elseif(e==2 or e==6)then local o=1 local e=math.ceil(i/2/n)local e=(o/e)*(e-math.abs((l-1)%(2*e)-e))t=e elseif(e==3 or e==7)then local e=1/(n)local e=1-(o%e)*n-.05 t=e elseif(e==4 or e==8)then local e=math.abs(math.sin(math.rad(360*n*o)))t=e end return t end local e=reaper.GetExtState(t,E);local o=reaper.GetExtState(t,g);e=tonumber(e)or 0;o=tonumber(o)or 1;if(e~=r)then o=1;else o=o+1;if(o>#u)then o=1 end if(o<1)then o=#u end end local l=u[o]or 1 local e=r or 0 local n=10*l reaper.SetExtState(t,g,tostring(o),false);reaper.SetExtState(t,E,tostring(e),false);if(e==0)then n=(16*l)elseif(e==1)then n=(10*l)elseif(e==2)then n=(12*l)elseif(e==3)then n=(10*l)elseif(e==4)then n=(16*l)elseif(e==5)then n=(14*l)elseif(e==6)then n=(6*l)elseif(e==7)then n=(6*l)elseif(e==8)then n=(16*l)end local c=self:_getTakeEnvelopeClipOffsetTime()local t=self:getDefaultShape()function _getPt(e,n,o)local e={positionTime=e-c,value=n,shape=t,tension=0,selection=true}return e end local o={}local t=1/n for r=1,n+1 do local t=t*(r-1)local l=s(t,l,e,r,n)local t=self:_relativePositionToTime2(t,i,a)if(r==1)then t=t+1e-5 elseif(r==n)then t=t-1e-5 end local n=_getPt(t,l)if(e==5 or e==6 or e==7 or e==8)then n.shape=1;end table.insert(o,n)end table.insert(o,_getPt(i,self:_getValueAtPos(i-c)))table.insert(o,_getPt(a,self:_getValueAtPos(a-c)))local e,n,t=self:_getPointsLength(o)self:_setNeedToClearInsertPointsArea(true);self:deletePointsRange(e,n)self:insertPoints(o)end function e:tempolatePointFitToTimeSelection(r,a)local e=1e-5 local n=self:_getTakeEnvelopeClipOffsetTime()local t=self.timeSelectionRangeInfo.startTime+e local e=self.timeSelectionRangeInfo.endTime-e local l=e-t local i=t-n local o=e-n local e=a or 1 local l=(l/e)local t={}local n=i while(n<o)do for i,e in ipairs(r)do local n=self:_relativePositionToTime2(e[1],n,n+l)if(n>o)then break;end local e=self:createPoint(n,e[2],e[3],e[4],e[5])table.insert(t,e)end n=n+l end local e=i-1e-4 local n=o+1e-4 local o=self:_getValueAtPos(e)local l=self:_getValueAtPos(n)table.insert(t,self:createPoint(e,o))table.insert(t,self:createPoint(n,l))self:deletePointsRange(e-1e-4,n+1e-4)self:_deletePointsAreaFromReaper(e,n)self:insertPoints(t)end function e:envelopePresetFitToTimeSelection(e,o)local t={};for n,e in ipairs(e.points)do local n=0 if(e.x==0)then e.x=e.x+n;end if(e.x==1)then e.x=e.x-n;end local n={}n[1]=e.x n[2]=e.y or e.value n[3]=e.shape n[4]=e.tension n[5]=e.selection table.insert(t,n)end self:tempolatePointFitToTimeSelection(t,o)end function e:_getTargetPointsPresetStr()local function l(n)local e="createEnvelopePointClass("local t=self:_getTakeEnvelopePlayRateOffset()local t=self:_getTakeEnvelopeClipOffsetTime()local t=self:_getRelativePosition2(n.positionTime+t,self.timeSelectionRangeInfo.startTime,self.timeSelectionRangeInfo.endTime)e=e..tostring(t)..","e=e..tostring(n.value)..","e=e..tostring(n.shape)..","e=e..tostring(n.tension)..","e=e..tostring(n.selection)..")".."\n"return e;end local n=self:_detectTargetEnvelopePoints()local e="--====================================================================\n"local t=generateGUID();local o="DEFINE_PRESET_ENV_TABLE"e=e.."table.insert( "..o..", \n"e=e.."    {".."\n"e=e..'       name = "'..t..'"\n'e=e.."     , userPreset = true \n"e=e..'     , category = "none" \n'e=e..'     , author = "---" \n'e=e.."     , points =".."\n"e=e.."      {".."\n"for t,n in ipairs(n)do if(t~=1)then e=e.."          ,"else e=e.."           "end e=e..l(n)end e=e.."      }".."\n"e=e.."    }".."\n"e=e..")".."\n"return e end function e:timeSelectionToEnvelopPresetPoints()local e=self:_getTargetPointsPresetStr()reaper.ShowConsoleMsg(e)end function e:SaveToTimeSelectionToEnvelopPresetPoints()local o=self:_getTargetPointsPresetStr()local t=reaper.GetProjectPathEx(n,"")local n="preset.txt"local e=package.config:sub(1,1)local e=t..e..n local e=io.open(e,"w")e:write(o)e:close()end function e:getPointAsEnvelopePreset(t)local n=self:_detectTargetEnvelopePoints()local e={}e.name=t or generateGUID()e.name=string.sub(e.name,1,7)e.userPreset=true e.points={}table.sort(n,function(e,n)return(e.positionTime<=n.positionTime and e.idx<n.idx)end)local o=self:_getTakeEnvelopeClipOffsetTime()local t=self:_getTakeEnvelopePlayRateOffset()for l,n in ipairs(n)do local t=self:_getRelativePosition2((n.positionTime*t)+o,self.timeSelectionRangeInfo.startTime,self.timeSelectionRangeInfo.endTime)t=math.min(math.max(t,0),1)local o=nil if(self:isFaderScalingValueMode()==true)then o=math.max(math.min(n.value,2),0)else o=math.max(math.min(n.value,1),0)end table.insert(e.points,createEnvelopePointClass(t,o,n.shape,n.tension,n.selection))end return e end function e:flush()if(self:_isExistSelectedTrackEnvelope()==false)then return end self:_saftyPointSort_()self:_deleteAllOriginalPoints()if(self.m_needToClearInsertArea==true)then self:_deleteEditingPointsArea();end self:_editingPointsToTrack()if(self.m_needTimeSelectionLengthFitEditingPoint==true)then self:_checkEditingPointRangeAndTimeSeletionRange()end for n,e in ipairs(self.m_onAfterCopyFunctionTale)do e(self);end reaper.Envelope_SortPoints(self.trackEnvelope)self:_saveUndoStringToProjectState();self:_saveEditingPointsToProjectState();self:_seveTimeSelectionRangeInfoToProjectState()self:_saveLastProcessTime()end function e:resetEditingPoints()self._isExistSelectedPoints=nil self.editingPoints={}self.editingPoints=deepcopy(self.originalPoints)end function e:resetAndReInit()self._isExistSelectedPoints=nil self:_init()end function e:createPoint(e,n,t,l,o)return{positionTime=e,value=n or .5,shape=t or self:getDefaultShape(),tension=l or .5,selection=o or false}end function e:insertPoint(e)self.m_existMaxNoteIdx=self.m_existMaxNoteIdx+1 e.idx=self:_getNewPointIDX()e.positionProjQN=reaper.TimeMap2_timeToQN(n,e.positionTime)local n=false for t,n in ipairs(self.editingPoints)do if(n.positionTime>e.positionTime)then break end if((n.positionTime==e.positionTime or math.abs(n.positionTime-e.positionTime)<3e-4)and(n.value==e.value or math.abs(n.value-e.value)<3e-7))then return end end table.insert(self.editingPoints,e)end function e:insertPoints(e)for n,e in ipairs(e)do self:insertPoint(e)end end function e:deletePoint(t)for n,e in ipairs(self.editingPoints)do if(e.idx==t.idx)then table.remove(self.editingPoints,n)break end end end function e:deletePoints(e)if(e==self.editingPoints)then self.editingPoints={}return end for n,e in ipairs(e)do self:deletePoint(e)end end function e:deletePointsRange(o,t)self:_editiongPointsTable_SortbyTimePos()local n={}for l,e in ipairs(self.editingPoints)do if(e.positionTime>t)then break end if(e.positionTime>=o and e.positionTime<=t)then table.insert(n,e)end end for e,t in ipairs(n)do for n,e in ipairs(self.editingPoints)do if(t.idx==e.idx)then table.remove(self.editingPoints,n)break end end end end function e:getProjectGridProjQN(n)if(self.m_getGridPrjQN_Checked~=nil)then return self.m_getGridPrjQN_Checked end local l=1 local e=0 local i=n or 1 local n=false local o=0 local t=0 local a,r=reaper.GetSet_ArrangeView2(e,n,o,t)local c=reaper.GetCursorPosition()reaper.SetEditCurPos2(e,0,0,0)reaper.Main_OnCommand(v,0)local i=reaper.GetCursorPosition()*i local i=reaper.TimeMap2_timeToQN(e,i)l=i reaper.SetEditCurPos2(e,c,0,0)n=true reaper.GetSet_ArrangeView2(e,n,o,t,a,r)self.m_getGridPrjQN_Checked=l;return self.m_getGridPrjQN_Checked end function e:_scaleValueToNormValue2(e,n,o,t)if(self:isFaderScalingValueMode()==true)then return e else return(e-n)/t end end function e:_scaleValueToNormValue(n)local e,o,l,t=self:getEnvelopeMaxMinCenterVal()return self:_scaleValueToNormValue2(n,e,o,t)end function e:_normarizeValueToScaledValue2(n,e,t,o)if(self:isFaderScalingValueMode()==true)then local e=reaper.ScaleToEnvelopeMode(1,n)return e else local n=(n*o)+e return math.max(math.min(n,t),e)end end function e:_normarizeValueToScaledValue(e)local e=e if(self:isFaderScalingValueMode()==true)then e=math.max(math.min(e,2),0)else e=math.max(math.min(e,1),0)end local t,o,l,n=self:getEnvelopeMaxMinCenterVal()return self:_normarizeValueToScaledValue2(e,t,o,n)end function e:_getValueAtPos(n)if(self:_isExistSelectedTrackEnvelope()==false)then return{}end local e=reaper.BR_EnvAlloc(self.trackEnvelope,false)local n=reaper.BR_EnvValueAtPos(e,n)reaper.BR_EnvFree(e,true)return self:_scaleValueToNormValue(n)end function e:time2FormatStr(e)local o=math.floor(e/3600)local n=math.floor((e%3600)/60)local t=math.floor(e%60)local e=e-math.floor(e)e=math.floor(e*100)return string.format("%02i :%02i :%02i.%02i",o,n,t,e)end function e:_init(e)reaper.PreventUIRefresh(1e6)self._isExistSelectedPoints=nil self:_updateRangeInfo()if(self:_check_SWS_Function()==false)then return end if(self:_storeSelectedTrakeEnvelope()==nil)then return end self:_storeActionContextInfo();local n=self:_isEnableTimeSelection()if(self:getAutoTimeSelectionDetectionMODE()==true)then self:_detection()end if(n==false)then return end self:_prepareTrackEnvelope(e)reaper.atexit(self:_atExit())end function e:_atExit()end function e:_prepareTrackEnvelope(e)local e=e or false if(e==false)then self.originalPoints=self:_getEnvelopePoint()self.editingPoints=deepcopy(self.originalPoints)end self:_storeActionContextInfo()return true;end function e:_deleteAllOriginalPoints()local n=self:_getTakeEnvelopePlayRateOffset()local e=1e-8 for o,t in ipairs(self.originalPoints)do local n=t.positionTime*n reaper.DeleteEnvelopePointRange(self.trackEnvelope,n-e,n+e)end end function e:_deleteEditingPointsArea()local n,t=self:_getPointsLength(self.editingPoints)local e=self:_getTakeEnvelopeClipOffsetTime()local e=self:_getTakeEnvelopePlayRateOffset()self:_deletePointsAreaFromReaper(n*e,t*e);end function e:_deletePointsAreaFromReaper(n,t)local e=DEFINE_DELETE_ONE_POINTAROUND_TIME reaper.DeleteEnvelopePointRange(self.trackEnvelope,n-e,t+e)end function e:_setNeedToClearInsertPointsArea(e)self.m_needToClearInsertArea=e end function e:_setNeedToFitTimeSelectionRangeEditingPoints(e)self.m_needTimeSelectionLengthFitEditingPoint=e end function e:_getNewPointIDX()if(DEFINE_ENV_POINT_ID_USE_GUID==true)then return generateGUID();else return self.m_existMaxNoteIdx end end function e:_editingPointsToTrack()table.sort(self.editingPoints,function(e,n)return(e.positionTime<=n.positionTime and e.idx<n.idx)end)local l,o,e,t=self:getEnvelopeMaxMinCenterVal()for n,e in ipairs(self.editingPoints)do local n=e.positionTime*self:_getTakeEnvelopePlayRateOffset()n=math.max(n,0)local e=reaper.InsertEnvelopePoint(self.trackEnvelope,n,self:_normarizeValueToScaledValue2(e.value,l,o,t),e.shape,e.tension,e.selection,false)end end function e:_clearEditingPointsToProjectState()local e={}local e=table.toString(e);reaper.SetExtState(t,f,e,false)end function e:_saveEditingPointsToProjectState()local e=table.toString(self.editingPoints);reaper.SetExtState(t,f,e,false)end function e:_getEditinPointsFromProjectState()local e=reaper.GetExtState(t,f)return table.fromString(e)or{};end function e:_seveTimeSelectionRangeInfoToProjectState()local e=self.timeSelectionRangeInfo.startTime local n=self.timeSelectionRangeInfo.endTime reaper.SetExtState(t,_,tostring(e),false)reaper.SetExtState(t,h,tostring(n),false)end function e:_getTimeSelectionRangeInfoFromProjectState()local n=reaper.GetExtState(t,_)local e=reaper.GetExtState(t,h)local n=tonumber(n)or 0;local e=tonumber(e)or 0;return n,e end function e:_isDiffFromLastTimeSelection()if(self.m_isDiffFromLastTimeSelection_Checked~=nil)then return self.m_isDiffFromLastTimeSelection_Checked end local l,n=self:_getTimeSelectionRangeInfoFromProjectState();local e=self.timeSelectionRangeInfo.startTime local t=self.timeSelectionRangeInfo.endTime local o=false;if(math.floor(l*1e3)/1e3~=math.floor(e*1e3)/1e3)then return true end if(math.floor(n*1e3)/1e3~=math.floor(t*1e3)/1e3)then return true end self.m_isDiffFromLastTimeSelection_Checked=o return self.m_isDiffFromLastTimeSelection_Checked;end function e:_saveLastProcessTime()reaper.SetExtState(t,m,tostring(os.time()),false)end function e:_getLastProcessTime()local e=reaper.GetExtState(t,m)return tonumber(e)or 0;end function e:_isStepedTimeDistance()local e=reaper.time_precise()local n=self:_getLastProcessTime()return((e-n)>DEFINE_SAFTYTIME_SEC)end function e:_clearUndoString()self.m_undStr=generateGUID()end function e:_needForceReGetEditingPoints()self:_clearUndoString();end function e:_saveUndoStringToProjectState()reaper.SetExtState(t,p,tostring(self.m_undStr),false)end function e:_getUndoStringFromProjectState()local e=reaper.GetExtState(t,p)return e end function e:_isDiffFromLastUndoString()if(self.m_isDiffFromLastUndo_Checked~=nil)then return self.m_isDiffFromLastUndo_Checked end local e=self:_getUndoStringFromProjectState()self.m_isDiffFromLastUndo_Checked=(e~=tostring(self.m_undStr))return self.m_isDiffFromLastUndo_Checked end function e:_saftyPointSort_()local e={}self:_sortByIDX(self.editingPoints)local n=nil for t,e in ipairs(self.editingPoints)do if(n~=nil and(math.abs(n.positionTime-e.positionTime)<.003)and t~=1 and t~=#self.editingPoints)then e.positionTime=e.positionTime+1e-5 end n=e end end function e:_updateRangeInfo()self.loopRangeInfo=self:_storeSelectionRangeInfo_(true)self.timeSelectionRangeInfo=self:_storeSelectionRangeInfo_(false)end function e:_storeSelectionRangeInfo_(e)local e=e or false local t,e=reaper.GetSet_LoopTimeRange2(0,false,e,0,0,false)if(t==0)then t=-1 end if(e==0)then e=-1 end local o=reaper.TimeMap2_timeToQN(n,t)local i=reaper.TimeMap2_timeToQN(n,e)local l=e-t local n=i-o if(l<=0)then n=0 end local e={startTime=t,endTime=e,lengthTime=l,startProjQN=o,endProjQN=i,lengthProjQN=n}return e end function e:_changeTimeSelectionRange(t,n,e)local e,e=reaper.GetSet_LoopTimeRange2(0,true,false,t,n,e)end function e:_getAllPointsCountsFromEnvTrack()local e=0;if(self:_isExistSelectedTrackEnvelope()==true)then e=self.BR_envelopeInfo.totalPointsCount end return e end function e:_getEnvelopePoint()local function n()local e=self:_getEnvelopePoint_(true)if(#e<1)then e=self:_getEnvelopePoint_(false)end self:_clearEditingPointsToProjectState()return e;end local e={}if(self:_isDiffFromLastTimeSelection()==true or self:_isDiffFromLastUndoString()==true or self:_isStepedTimeDistance()==true)then e=n()else e=self:_getEditinPointsFromProjectState()if(e==nil)then e=n();end end return e end function e:_getEnvelopePoint_(t)local e=self.trackEnvelope reaper.Envelope_SortPoints(self.trackEnvelope)local o=self.timeSelectionRangeInfo.startTime local f=self.timeSelectionRangeInfo.endTime local n=self.timeSelectionRangeInfo.lengthTime local u=self:_isEnableTimeSelection()self._isExistSelectedPoints=false local n=reaper.BR_EnvAlloc(e,true)local r={}local e=0 e=reaper.BR_EnvFindNext(n,o-DEFINE_SAFTY_ROUND_TIME);local l,i,s,c,o,d=reaper.BR_EnvGetPoint(n,e)local a=0 local h=self:_getTakeEnvelopeClipOffsetTime()local _,m,T,p=self:getEnvelopeMaxMinCenterVal()while(l==true)do if(a>self.m_maxProcessCount)then break end a=a+1 local a={selection=o,positionTime=(i)-h,value=self:_scaleValueToNormValue2(s,_,m,p),shape=c,tension=d,idx=e}if(u==true and(f+DEFINE_SAFTY_ROUND_TIME)<i)then l=false break;end if((t==true and o==t)or(t==nil or t==false))then table.insert(r,a)end e=e+1 l,i,s,c,o,d=reaper.BR_EnvGetPoint(n,e)if(self._isExistSelectedPoints==false and o==true)then self._isExistSelectedPoints=true end end reaper.BR_EnvFree(n,false)self.m_existMaxNoteIdx=e return r end function e:_isExistSelectedTrackEnvelope()return((self.trackEnvelope==nil or self.trackEnvelope==0)==false)end function e:_storeSelectedTrakeEnvelope()self.trackEnvelope=reaper.GetSelectedEnvelope(n)self:_storeBR_EnvelopeInfo()return self.trackEnvelope end function e:_storeBR_EnvelopeInfo()if(self:_isExistSelectedTrackEnvelope()==false)then return{}end local e=reaper.BR_EnvAlloc(self.trackEnvelope,false)local s,d,f,r,c,a,i,o,l,t,n=reaper.BR_EnvGetProperties(e)local n={activeOut=s,visibleOut=d,armedOut=f,inLaneOut=r,laneHeightOut=c,defaultShapeOut=a,minValueOut=i,maxValueOut=o,centerValueOut=l,typeOut=t,faderScalingOut=n,totalPointsCount=reaper.BR_EnvCountPoints(e)}reaper.BR_EnvFree(e,true)self.BR_envelopeInfo=n return n end function e:_toHighPriorityTimeSelection()if(self:_isEnableTimeSelection()==true)then reaper.Main_OnCommandEx(S,0,n)self:resetAndReInit()end end function e:_getSelectionPoints(e)local n=e or self.editingPoints local e={}for t,n in ipairs(n)do if(n.selection==true)then table.insert(e,n)end end return e end function e:_getContainRangePoints(e,n)local t=e or self.editingPoints local e=self.timeSelectionRangeInfo if(isUseLoopRange==true)then e=self.loopRangeInfo end local n={}local l=self:_getTakeEnvelopeClipOffsetTime()for t,o in ipairs(t)do local t=o.positionTime+l if(e.startTime-1e-5<=t and t<=e.endTime+1e-5)then table.insert(n,o)end end return n end function e:_getPointsLength(e)local t=e or self.editingPoints local n=1e5*1e4 local e=0 for o,t in ipairs(t)do n=math.min(t.positionTime,n)e=math.max(t.positionTime,e)end local t=e-n return n,e,t end function e:_getPointsLengthAsPQN(e)local e=e or self.editingPoints local o,t,e=self:_getPointsLength(e)local e=reaper.TimeMap2_timeToQN(n,e)local n=reaper.TimeMap2_timeToQN(n,t)local t=n-e return e,n,t end function e:_getFirstEndPoint(e)local e=e or self.editingPoints local n=e[1]local e=e[#e]return n,e end function e:_getFirstEndPointValue(e)local e=e or self.editingPoints local n,e=self:_getFirstEndPoint(e)return n.value,e.value end function e:_getMinMaxValue(e)local e=e or self.editingPoints local n=nil local t=nil for o,e in ipairs(e)do n=math.max(n or e.value,e.value)t=math.min(t or e.value,e.value)end return t,n end function e:_getReleativePQN2(o,e,t)local t=t-e local n=reaper.TimeMap2_timeToQN(n,o)local e=n-e local e=e/t return e end function e:_relativePQNToTime2(t,e,o)local o=o-e local e=reaper.TimeMap2_QNToTime(n,e+o*t)return e end function e:_getRelativePosition2(t,e,n)local e=e local n=n local n=n-e local e=(t-e)/n return e end function e:_getRelativePosition(t,e)local e=e or self.editingPoints if(#e<2)then return 0 end local n=e[1].positionTime local e=e[#e].positionTime local e=self:_getRelativePosition2(t,n,e)return e end function e:_relativePositionToTime2(n,e,t)local t=t-e local e=e+t*n return e end function e:_relativePositionToTime(n,e)local e=e or self.editingPoints if(#e<2)then return 0 end local t=e[1].positionTime local e=e[#e].positionTime local e=self:_relativePositionToTime2(n,t,e)return e end function e:_checkEditingPointRangeAndTimeSeletionRange()if(self:_isEnableTimeSelection()==false)then return end local o,l,e=self:_getPointsLength(self.editingPoints)local t=self.timeSelectionRangeInfo.startTime local i=self.timeSelectionRangeInfo.endTime local e=self:_getTakeEnvelopeClipOffsetTime()local a=math.min(o+e,t)local l=math.max(l+e,i)local i=true local o=false local t=false Envelop_Option_CMD_ID=41576 local e=reaper.GetToggleCommandStateEx(self.actionContextInfo.sectionID,Envelop_Option_CMD_ID)if(e==1)then reaper.Main_OnCommand(Envelop_Option_CMD_ID,0)end local n,n=reaper.GetSet_LoopTimeRange2(n,i,o,a,l,t)if(e==1)then reaper.Main_OnCommand(Envelop_Option_CMD_ID,e)end end function e:_isTakeEnvelope()if(self.m_isTakeEnvelope_Checked==nil)then if(self:_isExistSelectedTrackEnvelope()==false)then self.m_isTakeEnvelope_Checked=false;return self.m_isTakeEnvelope_Checked end local e,n,n=reaper.Envelope_GetParentTake(self.trackEnvelope)self.m_isTakeEnvelope_Checked=(e~=nil)end return self.m_isTakeEnvelope_Checked;end function e:_getTakeEnvelopePlayRateOffset()if(self.m_takePlayRateOffset_Checked==nil)then if(self:_isTakeEnvelope()==false)then self.m_takePlayRateOffset_Checked=1;return self.m_takePlayRateOffset_Checked end local e=reaper.Envelope_GetParentTake(self.trackEnvelope)local e=reaper.GetMediaItemTakeInfo_Value(e,"D_PLAYRATE")local n=1/e self.m_takePlayRateOffset_Checked=e end return self.m_takePlayRateOffset_Checked end function e:_getTakeEnvelopeClipOffsetTime()if(self.m_takeOffsetTime_Checked==nil)then if(self:_isTakeEnvelope()==false)then self.m_takeOffsetTime_Checked=0;return self.m_takeOffsetTime_Checked end local e=reaper.Envelope_GetParentTake(self.trackEnvelope)local e=reaper.GetMediaItemTake_Item(e)local e=reaper.GetMediaItemInfo_Value(e,"D_POSITION")self.m_takeOffsetTime_Checked=e end return self.m_takeOffsetTime_Checked end function e:_getPlayRatedFromAbsoluteTime(n)local t=self:_getTakeEnvelopePlayRateOffset()local e=self:_getTakeEnvelopeClipOffsetTime()return((n-e)/t)+e end function e:_storeActionContextInfo()if(m_isStorActionCOntextInfo_Checked==nil)then local a,i,e,t,n,l,o=reaper.get_action_context()self.actionContextInfo={is_new_value=a,filename=i,sectionID=e,cmdID=t,mode=n,resolution=l,val=o}end return self.actionContextInfo end function e:_fitTimeSelectionToSelectedPoints()ChangeTimeSelectionRangeFitToSelectedPointsRange()self:_updateRangeInfo()self.m_isEnbaleTimeSelection_Checked=nil return self:_isEnableTimeSelection();end function e:_checkExistSelectedPoints_()local c=reaper.BR_EnvAlloc(self.trackEnvelope,false)local l=0;local s,n,p,u,f,m=reaper.BR_EnvGetPoint(c,l)local d=reaper.Envelope_GetParentTake(self.trackEnvelope)local r=0 local e=nil local t=nil local o=false local a=false local i=0 local h=self.timeSelectionRangeInfo.startTime local _=self.timeSelectionRangeInfo.endTime while(s==true)do if(f==true)then if(o==false)then o=true end if(e==nil)then e=n else e=math.min(e,n)end if(t==nil)then t=n else t=math.max(t,n)end if(a==false and n>=h and n<=_)then a=true end i=i+1 end l=l+1;s,n,p,u,f,m=reaper.BR_EnvGetPoint(c,l)end reaper.BR_EnvFree(c,true)local l=0 local n=0 if(d~=nil)then local e=reaper.GetMediaItemTake_Item(d)r=reaper.GetMediaItemInfo_Value(e,"D_POSITION")end if(e==nil or t==nil or e==t)then return o,e or 0,e or 0,i end l=e+r n=t+r return o,l,n,a,i end function e:_detection()local e=false;local t=self:_isEnableTimeSelection()if(t==true)then local i,l,o,t,a=self:_checkExistSelectedPoints_()if(i==false)then return true end if(t==true)then e=true else local n,n=reaper.GetSet_LoopTimeRange2(n,true,false,l,o,true)self:_updateRangeInfo()self:_needForceReGetEditingPoints()self.m_isEnbaleTimeSelection_Checked=nil e=true end else ChangeTimeSelectionRangeFitToSelectedPointsRange()self:_needForceReGetEditingPoints()self:_updateRangeInfo()self.m_isEnbaleTimeSelection_Checked=nil e=self:_isEnableTimeSelection();end return e end function e:_isEnableTimeSelection()if(self.m_isEnbaleTimeSelection_Checked==nil)then if(self.m_isNeedTimeSelectionCheck~=true)then self.m_isEnbaleTimeSelection_Checked=true return self.m_isEnbaleTimeSelection_Checked end local function n()local e=false self:_updateRangeInfo()if(math.floor(self.timeSelectionRangeInfo.startTime*1e3)/1e3~=math.floor(self.timeSelectionRangeInfo.endTime*1e3)/1e3)then e=true end return e end local e=n()if(self:getAutoTimeSelectionDetectionMODE()==false and e==false)then ChangeTimeSelectionRangeFitToSelectedPointsRange()self:_updateRangeInfo()e=n()end self.m_isEnbaleTimeSelection_Checked=e end return self.m_isEnbaleTimeSelection_Checked end function e:_isEnableLooplection()if(self.m_isEnbaleLoopSelection_Checked==nil)then local e=false if(math.floor(self.loopRangeInfo.startTime*1e3)/1e3~=math.floor(self.loopRangeInfo.endTime*1e3)/1e3)then e=true end self.m_isEnbaleLoopSelection_Checked=e end return self.m_isEnbaleLoopSelection_Checked end function e:_sortByTimePos(e)local e=e or self.editingPoints table.sort(self.editingPoints,function(n,e)return(n.positionTime<e.positionTime)end)end function e:_sortByIDX(e)local e=e or self.editingPoints table.sort(self.editingPoints,function(e,n)return(e.idx<n.idx)end)end function e:_editiongPointsTable_SortbyTimePos()table.sort(self.editingPoints,function(n,e)return(n.positionTime<e.positionTime)end)end function e:_detectTargetEnvelopePoints()local e=nil if(self:isExistSelectedPoints()==true)then e=self:_getSelectionPoints(self.editingPoints)else e=self:_getContainRangePoints(self.editingPoints,false)end self:_editiongPointsTable_SortbyTimePos()return e end function e:_detectTargetRange()local t=0 local n=0 if(self:isExistSelectedPoints()==true)then local e={}e=self:_getSelectionPoints(self.editingPoints)t,n=self:_getPointsLength(e)else local e=self:_getTakeEnvelopePlayRateOffset()local e=self:_getTakeEnvelopeClipOffsetTime()t=(self.timeSelectionRangeInfo.startTime-e)n=(self.timeSelectionRangeInfo.endTime-e)end return t,n end e:_init(T)return e end function ChangeTimeSelectionRangeFitToSelectedPointsRange()if(check_SWS_Function()==false)then return end local a=reaper.GetSelectedEnvelope(n)if(a==nil)then return end local i=reaper.BR_EnvAlloc(a,false)local l=0;local c,o,f,s,r,d=reaper.BR_EnvGetPoint(i,l)local e=nil local t=nil while(c==true)do if(r==true)then if(e==nil)then e=o else e=math.min(e,o)end if(t==nil)then t=o else t=math.max(t,o)end end l=l+1;c,o,f,s,r,d=reaper.BR_EnvGetPoint(i,l)end reaper.BR_EnvFree(i,true)if(e==nil or t==nil or e==t)then local e=40635 reaper.Main_OnCommandEx(e,0,n)return end local l=reaper.Envelope_GetParentTake(a)local o=0 if(l~=nil)then local e=reaper.GetMediaItemTake_Item(l)o=reaper.GetMediaItemInfo_Value(e,"D_POSITION")end local e,e=reaper.GetSet_LoopTimeRange2(n,true,false,e+o,t+o,false)end if(package.config:sub(1,1)=="\\")then else end local e="kawa Clear EnvPoints In TimeSelectioin"local n=createEnvelopeFunc(e,true)reaper.Undo_BeginBlock();n:clearTimeSelectionRangePoint();reaper.TrackList_UpdateAllExternalSurfaces()reaper.UpdateArrange();reaper.UpdateTimeline();reaper.Undo_EndBlock(e,-1);