--====================================================================== 
--[[ 
* ReaScript Name: kawa_ENV_GenerateAbsSin. 
* 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(n)local t=n;local e;if(n==0)then return 0;end while(t<e)do e=t;t=bit32.rshift((e+n/e),1);t=(e+n/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 t=0;local e=0;for e=1,5 do t=t+(o^(4*e-3))/n(4*e-3);end for t=1,5 do e=e+(o^(4*t-1))/n(4*t-1);end local e=t-e;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 l={...}local function o(t,e)for n=1,#e do local e=e[n][t];if e then return e;end end end setmetatable(e,{__index=function(t,e)local n=o(e,l)t[e]=n;return n;end});e.__index=e;return e;end function mergeClass(...)return metaTableCopy(...);end function table.toString(t)local function s(e)return string.format("%q",e)end local d="";local function e(e)d=d..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..s(o)..","..n)elseif l=="number"then e(c..tostring(o)..","..n)end end for a,l in pairs(a)do if(not i[a])then local i=""local o=type(a)if o=="table"then if not t[a]then table.insert(r,a)t[a]=#r end i=c.."[{"..t[a].."}]="elseif o=="string"then i=c.."["..s(a).."]="elseif o=="number"then i=c.."["..tostring(a).."]="end if(i~="")then o=type(l)end if o=="table"then if not t[l]then table.insert(r,l)t[l]=#r end e(i.."{"..t[l].."},"..n)elseif o=="string"then e(i..s(l)..","..n)elseif o=="number"then e(i..tostring(l)..","..n)elseif o=="boolean"then e(i..tostring(l)..","..n)end end end e("},"..n)end e("}")return d;end function table.fromString(e)local e=load(e);local e=e()if(type(e)~="table")then local e={}return e end for t=1,#e do local o={}for n,l in pairs(e[t])do if type(l)=="table"then e[t][n]=e[l[1]]end if type(n)=="table"and e[n[1]]then table.insert(o,{n,e[n[1]]})end end for o,n in ipairs(o)do e[t][n[2]],e[t][n[1]]=e[t][n[1]],nil end end return e[1]end function table.doubleCheck(o,t,e)local n=false local l=e or true local e=ipairs;if(l==false)then e=pairs end for n,e in e(o)do if(e==t)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(e,n,o)local t=string.format("%x",e)local n=string.format("%x",n)local e=string.format("%x",o)if(#t<2)then t="0"..t end if(#n<2)then n="0"..n end if(#e<2)then e="0"..e end local e="01"..e..n..t;return tonumber(e,16)end function _toIntColor2(t,n,e)return t+math.floor(n*256)+math.floor((e*65536))end function _colorToNative(e,n,t)return reaper.ColorToNative(e,n,t);end function _nativeToColor(e)local e,n,t=reaper.ColorFromNative(e)return e,n,t;end function HSVToRGB(a,c,f)local i,l,t,s,d,e,n,r,o local a=a local c=c if(a>360)then a=a-360;elseif(a<0)then a=a+360;end if(c>1)then c=1;elseif(c<0)then c=0;end e=math.floor(255*f)if(e>255)then e=255;elseif(e<0)then e=0;end if(c==0)then i=e;l=e;t=e;else s=math.floor(a/60)d=a/60-s n=math.floor(e*(1-c))if(n<0)then n=0;elseif(n>255)then n=255;end r=math.floor(e*(1-d*c))if(r<0)then r=0;elseif(r>255)then r=255;end o=math.floor(e*(1-(1-d)*c))if(o<0)then o=0;elseif(o>255)then o=255;end if(s==0)then i=e;l=o;t=n;elseif(s==1)then i=r;l=e;t=n;elseif(s==2)then i=n;l=e;t=o;elseif(s==3)then i=n;l=r;t=e;elseif(s==4)then i=o;l=n;t=e;elseif(s==5)then i=e;l=n;t=r;else i=e;l=o;t=n;end end return i,l,t end function RGBToHSV(l,o,t)l,o,t=l/255,o/255,t/255;local n,i=math.max(l,o,t),math.min(l,o,t)local e,c,r=0,0,0 r=(n+i)/2 if(n==i)then e,c=0,0 else local a=n-i local c=0 if(r>.5)then c=a/(2-n-i);else c=a/(n+i);end if(n==l)then e=(o-t)/a if(o<t)then e=e+6;end elseif(n==o)then e=(t-l)/a+2 elseif(n==t)then e=(l-o)/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(e,n)local n=n or .8;return color_Mul(e,n)end function color_Brighter(n,e)local e=e or 1.2;return color_Mul(n,e)end function color_getInvert(e,i)local o=1-e[1]local t=1-e[2]local l=1-e[3]local n=e[4]if(i)then n=1-e[4];end return{o,t,l,n}end function rotPoint(l,o,n,t,e)local n=n-l local t=t-o local i=n*math.cos(e)-t*math.sin(e)local e=n*math.sin(e)+t*math.cos(e)local n=l+i local e=o+e return n,e end function scalePoint(n,e,l,o,t)local n=((l-n)*t)+n;local e=((o-e)*t)+e;return n,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(i)local e=i local l=""local e=nil local n=0 local t=1 local o=0 while(true)do l=string.sub(i,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 o=o+1 end return o end function isContainMultiByteStr(o)local l=false;local e=o local i=""local e=nil local n=0 local t=1 while(true)do i=string.sub(o,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 if(n>1)then l=true;break;end end return l end function mbStrSub(i,s,c)if type(i)~="string"then return"-"end local l=""local r=""local n=nil local o=0 local e=0 local t=1 local a=1 local a={}while(true)do r=string.sub(i,t,t)n=string.byte(r)if(n==nil)then break;end if n<128 then e=1 elseif n<224 then e=2 elseif n<240 then e=3 elseif n<248 then e=4 elseif n<252 then e=5 elseif n<254 then e=6 end local n=string.sub(i,t,t+(e-1))t=t+e if(n~=nil)then table.insert(a,n)end o=o+1 end local n=s or 1 local e=c or o if(n<1)then n=1 end if(e<n)then e=n end if(n>e)then n=e-1 end if(e>o)then e=o end for e=n,e 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(t,n,e,o)return math.abs(t-e)+math.abs(n-o);end function getEuclideanDist(o,t,n,e)local n=math.abs(o-n);local e=math.abs(t-e);local e=math.sqrt((n^2)+(e^2));return e;end function getChebyshevDist(e,n,o,t)local e=math.abs(e-o);local n=math.abs(n-t);if(e>n)then return e;else return n;end end local r=123/128 local a=51/128 function approx_distance(l,o,e,i)local n;local t;local e=math.abs(l-e);local o=math.abs(o-i);n=math.min(e,o)t=math.max(e,o)return(r*t)+(a*n)end function checkValueLength_circle(e,o,n)local e=e;local t=math.max(o,n);local n=math.min(o,n);if(e>t)then e=n;end if(e<n)then e=t;end return e;end local n='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'function base_64_enc(t)return((t: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(t)if(#t<6)then return''end local e=0 for n=1,6 do e=e+(t:sub(n,n)=='1'and 2^(6-n)or 0)end return n:sub(e+1,e+1)end)..({'','==','='})[#t%3+1])end function base_64_dec(e)e=string.gsub(e,'[^'..n..'=]','')return(e:gsub('.',function(e)if(e=='=')then return''end local e,n='',(n:find(e)-1)for t=6,1,-1 do e=e..(n%2^t-n%2^(t-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 n=0 for e=1,8 do n=n+(t:sub(e,e)=='1'and 2^(7-e)or 0)end return string.char(n)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(n)if(fileExists(n)==false)then return{}end local e={}for n in io.lines(n)do table.insert(e,n);end return e end function readAll(e)local e=io.open(e,"rb")local n=e:read("*all")e:close()return n end function getBezierPointQB(e,i,a,r,l,o,t)local n=1-e;local o=e*e*o+2*e*n*r+n*n*i;local e=e*e*t+2*e*n*l+n*n*a;return o,e;end function getBezierAngleQB(e,l,i,o,n,a,r)local t=1-e;local o=2*(e*(a-o)+t*(o-l));local e=2*(e*(r-n)+t*(n-i));return math.atan2(e,o);end local n=-1 math.randomseed(reaper.time_precise()*os.time()/1e3)local S=40331 local a=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 u="lastAutoSelection"local m="lastEdtingPoints"local P="lastGneerateCurveType"local T="lastGneerateCurveRATE"local _="lastTimeSelectionStartTime"local h="lastTimeSelectionEndTime"local E="lastUndoSTring"local g="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 f={4,3,2,1,5,6,7,8}local r=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,l,n,t,o)local e={}e.x=i or 0 e.y=l or .5 e.tension=t or 0 e.shape=n or 0 e.selection=o or true return e end function createEnvelopeFunc(c,p,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(p)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 n=self.BR_envelopeInfo.minValueOut local t=self.BR_envelopeInfo.maxValueOut local o=self.BR_envelopeInfo.centerValueOut local l=t-n 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 n=-e t=e o=0 l=t-n end return n,t,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 n=self:_detectTargetEnvelopePoints()local t,e=reaper.GetUserInputs("fix Points Value",7,"value( % ) (0  ~ 100)","50,--,--,--,--,--,--")if(t~=true)then return end local e=tonumber(e:split(",")[1])or 60 e=e/100 e=math.max(math.min(e,100),0)for t,n in ipairs(n)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(a,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 o=n or .9 local n=e[1].positionTime for t,e in ipairs(e)do local t=e.positionTime-n local n=n+(t*o)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 t,n,n=self:_getPointsLength(e)local n={}for e,o in ipairs(e)do local e=deepcopy(o)local o=o.positionTime-t local t=t+(o*l)e.positionTime=t e.selection=true table.insert(n,e)end self:deletePoints(e)self:insertPoints(n)self:_setNeedToClearInsertPointsArea(true);end function e:changePointTension(t)if(self:isPitchEnvelopeTrack()==true or self:isTempoEnvelopeTrack()==true)then return end local e=self:_detectTargetEnvelopePoints()for e,n in ipairs(e)do n.shape=5 local e=n.tension+1 local e=e*t e=e-1 n.tension=math.max(math.min(e,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(t)local e=self:_detectTargetEnvelopePoints()local n=reaper.TimeMap2_QNToTime(n,t)for t,e in ipairs(e)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 t=self:_detectTargetEnvelopePoints()local e=reaper.TimeMap2_QNToTime(n,e)local i=self:_getTakeEnvelopeClipOffsetTime()local n=self:_getTakeEnvelopePlayRateOffset()local o={}local l={}for n,t in ipairs(t)do local n=deepcopy(t)n.positionTime=t.positionTime+e n.positionTime=math.max(n.positionTime,0)n.selection=true table.insert(o,n)table.insert(l,t)end self:_needForceReGetEditingPoints();local n,n,n=self:_getPointsLength(o)self:_setNeedToClearInsertPointsArea(true);self:_setNeedToFitTimeSelectionRangeEditingPoints(false)local t=self.timeSelectionRangeInfo.startTime local n=self.timeSelectionRangeInfo.endTime self:deletePointsRange(t+i+e,n+i+e)self:deletePoints(l)self:insertPoints(o)self:_changeTimeSelectionRange(t+e,n+e,true)end function e:invertValueAndPosition(t,n)local e=self:_detectTargetEnvelopePoints()local a=t local r=n if(#e<2)then return end local n=self:_getTakeEnvelopeClipOffsetTime()local t,o=self:_getPointsLength(e)if(self:isExistSelectedPoints()==false)then t=self.timeSelectionRangeInfo.startTime-n o=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,t,o)local e=1-e local e=self:_relativePositionToTime2(e,t,o)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 l=e or 4 local e=self:_getTakeEnvelopeClipOffsetTime()local n=self:_getTakeEnvelopePlayRateOffset()local i=self:getDefaultShape()function _getPt(e,t)return{positionTime=e,value=self:_getValueAtPos(e*n),shape=i,tension=0,selection=true}end local n={}local i=o-e local a=t-e local a=a-i local a=a/l for e=1,l-1 do local e=i+(a*e)table.insert(n,_getPt(e))end table.insert(n,_getPt(o-e))table.insert(n,_getPt(t-e))self:insertPoints(n)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 e=e/n for n=1,n-1 do local e=t+(e*n)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 o=self:_getTakeEnvelopePlayRateOffset()local t=self:getDefaultShape()local function a(e)return{positionTime=e,value=self:_getValueAtPos(e*o),shape=t,tension=0,selection=true}end local e,l=self:_detectTargetRange()local t=l-e if(l-e>r)then reaper.MB(" Stop . timeSelection range  longer than "..tostring(r).." sec.","Process Stop",0)return end;local i=self:getProjectGridProjQN()local t=reaper.TimeMap2_timeToQN(n,e)local r=reaper.TimeMap2_timeToQN(n,l)local t=t-(t%i)+i local o=t local t={}while o<r do local e=reaper.TimeMap2_QNToTime(n,o)table.insert(t,a(e))o=o+i end if(self:isExistSelectedPoints()==false)then table.insert(t,a(e))table.insert(t,a(l))end self:insertPoints(t)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 l={}local o={}for e,t in ipairs(e)do local e=deepcopy(t)local a=reaper.TimeMap2_timeToQN(n,t.positionTime+i)local r=(a%r)local a=a-r e.positionTime=reaper.TimeMap2_QNToTime(n,a)e.positionTime=e.positionTime-i table.insert(l,e)table.insert(o,t)end self:deletePoints(o)self:insertPoints(l)self:_needForceReGetEditingPoints();end function e:quantizeEnvelopePointType2()local e=self:_detectTargetEnvelopePoints()local o=self:getProjectGridProjQN()local r=self:_getTakeEnvelopeClipOffsetTime();local c={}local d={}for e,l in ipairs(e)do local e=deepcopy(l)local t=reaper.TimeMap2_timeToQN(n,l.positionTime+r)local i=(t%o)local a=t local s=1 if(i<=o/2)then local e=i*s a=t-e else local e=(o-i)*s a=t+e end e.positionTime=reaper.TimeMap2_QNToTime(n,a)e.positionTime=e.positionTime-r table.insert(c,e)table.insert(d,l)end self:deletePoints(d)self:insertPoints(c)end function e:interpolationPoints(t,e,o)local n=self:_detectTargetEnvelopePoints()if(#n<2)then return end local t=t or .3 local d=e or false local r=o or 0 local s,c,e=self:_getPointsLength(n)local a,i=self:_getFirstEndPointValue(n)local function l(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(n)do local n=self:_getRelativePosition2(e.positionTime,s,c)if(d==true)then local o=e.value-i local n=l(1-n,r)local n=i+o*n e.value=(e.value)*(1-t)+(n)*t else local o=e.value-a local n=l(n,r)local n=a+o*n e.value=(e.value)*(1-t)+(n)*t 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 o=self:_detectTargetEnvelopePoints()local e=0 local t={}for e,l in ipairs(o)do if(e~=1 and e~=#o)then if(e%n==0)then table.insert(t,l)end end end self:deletePoints(t)self:_needForceReGetEditingPoints();end function e:lightWeightPointCountType2(e)self:_toHighPriorityTimeSelection()local n=self:_detectTargetEnvelopePoints()local t=0 local o={}local t={}local l=#n local e=math.floor(l/e+.5)local r,i,l=self:_getPointsLength(n)local a=l/e local i=self:_getTakeEnvelopePlayRateOffset()local l=nil for e=1,e+1 do local n=r+(a*(e-1))local e=self:_getValueAtPos(n*i)local n={selection=false,positionTime=n,value=e,shape=0,tension=0}if(l~=e)then table.insert(o,n)l=e end end for n,e in ipairs(n)do table.insert(t,e)end self:deletePoints(t)self:insertPoints(o)self:_needForceReGetEditingPoints();end function e:lightWeightPointCountType3(s)self:_toHighPriorityTimeSelection()local e=self:_detectTargetEnvelopePoints()if(#e<1)then return;end local n=0 local t={}local a={}local i={}local n,n,n=self:_getPointsLength(e)local o=e[1]for l,n in ipairs(e)do local c=o.positionTime local i=o.value local a=n.positionTime local r=n.value local i=getEuclideanDist(c,i,a,r)n.distanceFromLastPt=i o=n if((l~=1)and(l~=#e))then table.insert(t,n)end end table.sort(t,function(e,n)return(e.distanceFromLastPt<n.distanceFromLastPt)end)local n=(#t*s)for t,e in ipairs(t)do if(t<n)then table.insert(i,e)else table.insert(a,e)end end self:deletePoints(i)self:insertPoints(a)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 t={}local l=self.timeSelectionRangeInfo.startTime local i=self.timeSelectionRangeInfo.endTime local a=self:_getTakeEnvelopeClipOffsetTime()for e,o in ipairs(e)do local e=deepcopy(o)e.positionTime=self:_relativePositionToTime2(math.random(),l,i)e.positionTime=e.positionTime-a e.selection=false table.insert(n,o)table.insert(t,e)end self:deletePoints(n)self:insertPoints(t)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 l=-1e6 local o=1e6 for n,e in ipairs(n)do e.selection=true l=math.max(l,e.value)o=math.min(o,e.value)end local a,r,c=self:_getPointsLength(n)local i=self:_getTakeEnvelopePlayRateOffset()local i=self:_getTakeEnvelopeClipOffsetTime()local a=self:time2FormatStr(a+i)local r=self:time2FormatStr(r+i)local i=self:time2FormatStr(c+i)e=e.."Points Num : "..tostring(#n).."\n"e=e.."Max Value : "..tostring(l).."\n"e=e.."Min Value : "..tostring(o).."\n"e=e.."Start Time : "..tostring(a).."  \n"e=e.."End Time : "..tostring(r).."  \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 t=n or false local n,o=self:_getFirstEndPointValue(e)for o,e in ipairs(e)do local o=(e.value>=n)if(o==t)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,u)local a=l or 1 if(e=="")then e=o[1]end local l=nil for n,t in ipairs(o)do if(e==t)then local e=n+a if(e>#o)then e=1 end if(e<1)then e=#o end l=o[e]break end end local o=tonumber(l:split(",")[1])local a=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/o)%2)==a)then e.selection=true else e.selection=false end end reaper.SetExtState(t,u,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 i={}local r=o or false;local c=t or false;local o,l,t=self:_getPointsLength(e)local a=reaper.TimeMap2_timeToQN(n,o)local l=reaper.TimeMap2_timeToQN(n,l)local o=l-a local s=self:_getTakeEnvelopeClipOffsetTime()t=t+.001 if(c==true)then t=t*-1 o=o*-1 end for e,n in ipairs(e)do local e=deepcopy(n)if(r==true)then e.positionTime=e.positionTime+t+.001 else local n=self:_getReleativePQN2(e.positionTime+.001,a,l)e.positionTime=self:_relativePQNToTime2(n,a+o,l+o)e.positionTime=e.positionTime end e.selection=true table.insert(i,e)n.selection=false end local t=self:_getTakeEnvelopeClipOffsetTime()local e,o,l=self:_getPointsLength(i)self:deletePointsRange(e,o)self:insertPoints(i)self:_setNeedToClearInsertPointsArea(true);self:_setNeedToFitTimeSelectionRangeEditingPoints(false)reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(e+t,o+t,true)local o=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,e+t,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,o,0,0)self:_needForceReGetEditingPoints()end function e:duplicateMusical(t,e)self:_toHighPriorityTimeSelection()local f=self:_detectTargetEnvelopePoints()if(#f<2)then self:_toHighPriorityTimeSelection()end if(#f<2)then return end local m=t or false;local u=e or false local t=self:_getTakeEnvelopeClipOffsetTime()local e=self:_getTakeEnvelopePlayRateOffset()local a=self.timeSelectionRangeInfo.startTime local r=self.timeSelectionRangeInfo.lengthTime local s=self.timeSelectionRangeInfo.endTime local c=reaper.TimeMap2_timeToQN(n,a)local d=reaper.TimeMap2_timeToQN(n,s)local l=d-c local function i(e)local n=0 local o,t,l=reaper.TimeMap_GetTimeSigAtTime(0,a+.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 o=l-1e-6 local e=i(o)o=o+e local o=reaper.TimeMap2_QNToTime(n,c+e)local i=o-a if(u==true)then i=o-o r=r*-1 l=l*-1 e=e*-1 end local o={}local function u(o)local n=0 if(m==true)then local e=o+r+.001+i n=e else local i=o+t local o=self:_getReleativePQN2(o+t,c,d)n=self:_relativePQNToTime2(o,c+l+e,d+l+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(o,e(a-t+i))table.insert(o,e(s-t+i))else local n=u(a)local t=u(s)table.insert(o,e(n))table.insert(o,e(t))end end for e,n in ipairs(f)do local e=metaTableCopy(n)e.positionTime=u(e.positionTime)e.selection=true table.insert(o,e)n.selection=false end if(m==true)then local e=a+r+.001+i local l=s+r+.001+i reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(e,l,true)local i=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,e,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,i,false,false)self:deletePointsRange(e,l)self:_deletePointsAreaFromReaper(e-t,l-t)self:insertPoints(o)else local t=reaper.TimeMap2_QNToTime(n,c+l+e)local e=reaper.TimeMap2_QNToTime(n,d+l+e)reaper.PreventUIRefresh(-1)self:_changeTimeSelectionRange(t,e,true)local l=reaper.GetCursorPosition()reaper.SetEditCurPos2(n,t,true,true)reaper.PreventUIRefresh(1e6)reaper.SetEditCurPos2(n,l,0,0)self:deletePointsRange(t,e)self:insertPoints(o)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 o=self.timeSelectionRangeInfo.startTime local a=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,o-e,a-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,t,e,i,l)local n=.5 if(e==0 or e==5)then local e=math.sin(math.rad(360*t*o))n=(e*.5)+.5 elseif(e==1)then local e=1/(2*t)local e=1*((math.floor(o/e)%2)*-1)n=(e)+1 elseif(e==2 or e==6)then local o=1 local e=math.ceil(l/2/t)local e=(o/e)*(e-math.abs((i-1)%(2*e)-e))n=e elseif(e==3 or e==7)then local e=1/(t)local e=1-(o%e)*t-.05 n=e elseif(e==4 or e==8)then local e=math.abs(math.sin(math.rad(360*t*o)))n=e end return n end local e=reaper.GetExtState(t,P);local o=reaper.GetExtState(t,T);e=tonumber(e)or 0;o=tonumber(o)or 1;if(e~=r)then o=1;else o=o+1;if(o>#f)then o=1 end if(o<1)then o=#f end end local l=f[o]or 1 local e=r or 0 local n=10*l reaper.SetExtState(t,T,tostring(o),false);reaper.SetExtState(t,P,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(a,l)local e=1e-5 local n=self:_getTakeEnvelopeClipOffsetTime()local t=self.timeSelectionRangeInfo.startTime+e local e=self.timeSelectionRangeInfo.endTime-e local r=e-t local i=t-n local o=e-n local e=l or 1 local l=(r/e)local t={}local e=i while(e<o)do for i,n in ipairs(a)do local e=self:_relativePositionToTime2(n[1],e,e+l)if(e>o)then break;end local e=self:createPoint(e,n[2],n[3],n[4],n[5])table.insert(t,e)end e=e+l end local e=i-1e-4 local n=o+1e-4 local l=self:_getValueAtPos(e)local o=self:_getValueAtPos(n)table.insert(t,self:createPoint(e,l))table.insert(t,self:createPoint(n,o))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 n,t in ipairs(n)do if(n~=1)then e=e.."          ,"else e=e.."           "end e=e..l(t)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 t=self:_getTargetPointsPresetStr()local o=reaper.GetProjectPathEx(n,"")local e="preset.txt"local n=package.config:sub(1,1)local e=o..n..e local e=io.open(e,"w")e:write(t)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(o,l,t,n,e)return{positionTime=o,value=l or .5,shape=t or self:getDefaultShape(),tension=n or .5,selection=e 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(n)for e,t in ipairs(self.editingPoints)do if(t.idx==n.idx)then table.remove(self.editingPoints,e)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 n,e in ipairs(n)do for n,t in ipairs(self.editingPoints)do if(e.idx==t.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 o=1 local e=0 local a=n or 1 local n=false local t=0 local l=0 local c,r=reaper.GetSet_ArrangeView2(e,n,t,l)local i=reaper.GetCursorPosition()reaper.SetEditCurPos2(e,0,0,0)reaper.Main_OnCommand(v,0)local a=reaper.GetCursorPosition()*a local a=reaper.TimeMap2_timeToQN(e,a)o=a reaper.SetEditCurPos2(e,i,0,0)n=true reaper.GetSet_ArrangeView2(e,n,t,l,c,r)self.m_getGridPrjQN_Checked=o;return self.m_getGridPrjQN_Checked end function e:_scaleValueToNormValue2(e,t,o,n)if(self:isFaderScalingValueMode()==true)then return e else return(e-t)/n end end function e:_scaleValueToNormValue(t)local o,n,l,e=self:getEnvelopeMaxMinCenterVal()return self:_scaleValueToNormValue2(t,o,n,e)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 n=math.floor(e/3600)local o=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",n,o,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 e=self:_getTakeEnvelopePlayRateOffset()local n=1e-8 for o,t in ipairs(self.originalPoints)do local e=t.positionTime*e reaper.DeleteEnvelopePointRange(self.trackEnvelope,e-n,e+n)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(t,n)local e=DEFINE_DELETE_ONE_POINTAROUND_TIME reaper.DeleteEnvelopePointRange(self.trackEnvelope,t-e,n+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 o,l,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,o,l,t),e.shape,e.tension,e.selection,false)end end function e:_clearEditingPointsToProjectState()local e={}local e=table.toString(e);reaper.SetExtState(t,m,e,false)end function e:_saveEditingPointsToProjectState()local e=table.toString(self.editingPoints);reaper.SetExtState(t,m,e,false)end function e:_getEditinPointsFromProjectState()local e=reaper.GetExtState(t,m)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 e=reaper.GetExtState(t,_)local n=reaper.GetExtState(t,h)local e=tonumber(e)or 0;local n=tonumber(n)or 0;return e,n end function e:_isDiffFromLastTimeSelection()if(self.m_isDiffFromLastTimeSelection_Checked~=nil)then return self.m_isDiffFromLastTimeSelection_Checked end local l,t=self:_getTimeSelectionRangeInfoFromProjectState();local o=self.timeSelectionRangeInfo.startTime local n=self.timeSelectionRangeInfo.endTime local e=false;if(math.floor(l*1e3)/1e3~=math.floor(o*1e3)/1e3)then return true end if(math.floor(t*1e3)/1e3~=math.floor(n*1e3)/1e3)then return true end self.m_isDiffFromLastTimeSelection_Checked=e return self.m_isDiffFromLastTimeSelection_Checked;end function e:_saveLastProcessTime()reaper.SetExtState(t,g,tostring(os.time()),false)end function e:_getLastProcessTime()local e=reaper.GetExtState(t,g)return tonumber(e)or 0;end function e:_isStepedTimeDistance()local n=reaper.time_precise()local e=self:_getLastProcessTime()return((n-e)>DEFINE_SAFTYTIME_SEC)end function e:_clearUndoString()self.m_undStr=generateGUID()end function e:_needForceReGetEditingPoints()self:_clearUndoString();end function e:_saveUndoStringToProjectState()reaper.SetExtState(t,E,tostring(self.m_undStr),false)end function e:_getUndoStringFromProjectState()local e=reaper.GetExtState(t,E)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 l=reaper.TimeMap2_timeToQN(n,t)local i=reaper.TimeMap2_timeToQN(n,e)local n=e-t local o=i-l if(n<=0)then o=0 end local e={startTime=t,endTime=e,lengthTime=n,startProjQN=l,endProjQN=i,lengthProjQN=o}return e end function e:_changeTimeSelectionRange(n,t,e)local e,e=reaper.GetSet_LoopTimeRange2(0,true,false,n,t,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_(o)local e=self.trackEnvelope reaper.Envelope_SortPoints(self.trackEnvelope)local n=self.timeSelectionRangeInfo.startTime local f=self.timeSelectionRangeInfo.endTime local t=self.timeSelectionRangeInfo.lengthTime local u=self:_isEnableTimeSelection()self._isExistSelectedPoints=false local t=reaper.BR_EnvAlloc(e,true)local d={}local e=0 e=reaper.BR_EnvFindNext(t,n-DEFINE_SAFTY_ROUND_TIME);local i,a,c,s,n,r=reaper.BR_EnvGetPoint(t,e)local l=0 local p=self:_getTakeEnvelopeClipOffsetTime()local m,_,T,h=self:getEnvelopeMaxMinCenterVal()while(i==true)do if(l>self.m_maxProcessCount)then break end l=l+1 local l={selection=n,positionTime=(a)-p,value=self:_scaleValueToNormValue2(c,m,_,h),shape=s,tension=r,idx=e}if(u==true and(f+DEFINE_SAFTY_ROUND_TIME)<a)then i=false break;end if((o==true and n==o)or(o==nil or o==false))then table.insert(d,l)end e=e+1 i,a,c,s,n,r=reaper.BR_EnvGetPoint(t,e)if(self._isExistSelectedPoints==false and n==true)then self._isExistSelectedPoints=true end end reaper.BR_EnvFree(t,false)self.m_existMaxNoteIdx=e return d 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 l,n,t,o,i,a,c,r,d,f,s=reaper.BR_EnvGetProperties(e)local n={activeOut=l,visibleOut=n,armedOut=t,inLaneOut=o,laneHeightOut=i,defaultShapeOut=a,minValueOut=c,maxValueOut=r,centerValueOut=d,typeOut=f,faderScalingOut=s,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 e=e or self.editingPoints local n={}for t,e in ipairs(e)do if(e.selection==true)then table.insert(n,e)end end return n end function e:_getContainRangePoints(e,n)local n=e or self.editingPoints local e=self.timeSelectionRangeInfo if(isUseLoopRange==true)then e=self.loopRangeInfo end local o={}local t=self:_getTakeEnvelopeClipOffsetTime()for l,n in ipairs(n)do local t=n.positionTime+t if(e.startTime-1e-5<=t and t<=e.endTime+1e-5)then table.insert(o,n)end end return o 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 t,o,e=self:_getPointsLength(e)local t=reaper.TimeMap2_timeToQN(n,e)local e=reaper.TimeMap2_timeToQN(n,o)local n=e-t return t,e,n 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 e,n=self:_getFirstEndPoint(e)return e.value,n.value end function e:_getMinMaxValue(e)local e=e or self.editingPoints local t=nil local n=nil for o,e in ipairs(e)do t=math.max(t or e.value,e.value)n=math.min(n or e.value,e.value)end return n,t 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(o,e,t)local t=t-e local e=reaper.TimeMap2_QNToTime(n,e+t*o)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(t,e,n)local n=n-e local e=e+n*t return e end function e:_relativePositionToTime(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:_relativePositionToTime2(t,n,e)return e end function e:_checkEditingPointRangeAndTimeSeletionRange()if(self:_isEnableTimeSelection()==false)then return end local t,l,e=self:_getPointsLength(self.editingPoints)local o=self.timeSelectionRangeInfo.startTime local i=self.timeSelectionRangeInfo.endTime local e=self:_getTakeEnvelopeClipOffsetTime()local o=math.min(t+e,o)local t=math.max(l+e,i)local i=true local a=false local l=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,a,o,t,l)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,o,t,n,e,l=reaper.get_action_context()self.actionContextInfo={is_new_value=a,filename=i,sectionID=o,cmdID=t,mode=n,resolution=e,val=l}end return self.actionContextInfo end function e:_fitTimeSelectionToSelectedPoints()ChangeTimeSelectionRangeFitToSelectedPointsRange()self:_updateRangeInfo()self.m_isEnbaleTimeSelection_Checked=nil return self:_isEnableTimeSelection();end function e:_checkExistSelectedPoints_()local r=reaper.BR_EnvAlloc(self.trackEnvelope,false)local l=0;local f,n,u,m,s,p=reaper.BR_EnvGetPoint(r,l)local d=reaper.Envelope_GetParentTake(self.trackEnvelope)local a=0 local e=nil local t=nil local o=false local c=false local i=0 local _=self.timeSelectionRangeInfo.startTime local h=self.timeSelectionRangeInfo.endTime while(f==true)do if(s==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(c==false and n>=_ and n<=h)then c=true end i=i+1 end l=l+1;f,n,u,m,s,p=reaper.BR_EnvGetPoint(r,l)end reaper.BR_EnvFree(r,true)local n=0 local l=0 if(d~=nil)then local e=reaper.GetMediaItemTake_Item(d)a=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 n=e+a l=t+a return o,n,l,c,i end function e:_detection()local e=false;local t=self:_isEnableTimeSelection()if(t==true)then local o,t,l,i,a=self:_checkExistSelectedPoints_()if(o==false)then return true end if(i==true)then e=true else local n,n=reaper.GetSet_LoopTimeRange2(n,true,false,t,l,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(n,e)return(n.idx<e.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(p)return e end function ChangeTimeSelectionRangeFitToSelectedPointsRange()if(check_SWS_Function()==false)then return end local i=reaper.GetSelectedEnvelope(n)if(i==nil)then return end local a=reaper.BR_EnvAlloc(i,false)local l=0;local r,o,f,d,c,s=reaper.BR_EnvGetPoint(a,l)local t=nil local e=nil while(r==true)do if(c==true)then if(t==nil)then t=o else t=math.min(t,o)end if(e==nil)then e=o else e=math.max(e,o)end end l=l+1;r,o,f,d,c,s=reaper.BR_EnvGetPoint(a,l)end reaper.BR_EnvFree(a,true)if(t==nil or e==nil or t==e)then local e=40635 reaper.Main_OnCommandEx(e,0,n)return end local l=reaper.Envelope_GetParentTake(i)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,t+o,e+o,false)end if(package.config:sub(1,1)=="\\")then else end local n="kawa Generate Abusolute Sin"local e=createEnvelopeFunc(n,true)if(e:canExecuteProcess()==true)then reaper.Undo_BeginBlock();e:generateSinPoints(4);e:flush();reaper.TrackList_UpdateAllExternalSurfaces()reaper.UpdateArrange();reaper.UpdateTimeline();reaper.Undo_EndBlock(n,-1);end
