\\filestore\c$\shares\websites\buzzformat\Uploads\Buzzscript\Scripts\(pattern) interpolate.pattern.buzzscript; interpolate v1.15
; for buzzscript 2.0.0.57 or higher
; written by ps

; todo - bugfix some bizare values when using the initial phase and frequency percentages from the sinus modulation
;      - do other interpolate methods (polynomial, adsr, ocx envelopes, whatever..)

;these compares of the interpolate variable exist so that you can call this script more then once
;from within another script, for that you just need to
;setstring interpolate 2 before you do the "include (pattern) interpolate.pattern.buzzscript"
;__only need, and should, do that once and ONLY once__ then for each time you want to do an interpolation,
;set the 7 vars to the values you want, the default interpolation method is ps_linear_interpolate,
;notice that you can force set it to something else by doing setting the buzz_interpolate var to the chosen method
;then "setstring interpolate 1" in order to bypass the gui popup and
;gosub usemelikeawhore 

compare interpolate 2
isequal goend2
label usemelikeawhore

;HIDEDEBUG
DEBUGOFF
;SHOWDEBUG
;EXECOFF

compare interpolate 1
isequal bypassgui

SETACTIVESCREEN PATTERN
ACTIVATE
GETPARAM
SETSTRING lastp PARAM

;get blockstart blockend and their values to use as defaults on the gui
UPDATEPATTERN
setstring var1 blockstart
setstring var2 blockend
setstring var3 0
setstring var4 0
setstring thisname param
gosub get_back_to_our_original_column
setstring tempdownvalue blockstart
SETSTRING temp1 0
SETSTRING temp2 0
SETSTRING temp3 16
SETSTRING temp4 0
gosub go_down_till_you_reach_tempdownvalue
getparam
compare paramvalue -1
isequal skip1
hex paramvalue paramvalue
setstring var3 paramvalue
label skip1
setstring tempdownvalue blockend
SETSTRING temp1 0
SETSTRING temp2 blockstart
SETSTRING temp3 16
SETSTRING temp4 0
gosub go_down_till_you_reach_tempdownvalue
getparam
compare paramvalue -1
isequal skip2
hex paramvalue paramvalue
setstring var4 paramvalue
label skip2

;init the rest of the default values for vars used/received on the gui
;var1 startrow, var2 endrow, var3 startvalue, var4 endvalue, var5 phase, var6 period, var7 amplitude
setstring var5 0
setstring var6 1
setstring var7 1
setstring varlinearbuzz buzz_interpolate
setstring varlinear ps_linear_interpolate
setstring varsomething ps_log_interpolate

;do the gui window thingie
WINDOW_CREATE somewin2 name=thisname,width=390,height=165,state=normal,icon=ps.ico
TEXTFIELD_CREATE somewin2 handle=mytext1,name=StartRow,x=15,y=15,width=45,height=20
TEXTFIELD_CREATE somewin2 handle=mytext2,name=EndRow,x=15,y=45,width=45,height=20
TEXTFIELD_CREATE somewin2 handle=mytext3,name=StartValue,x=15,y=75,width=45,height=20
TEXTFIELD_CREATE somewin2 handle=mytext4,name=EndValue,x=15,y=105,width=45,height=20
;the dropbox is here so you can tab your way through the gui
DROPBOX_CREATE somewin2 handle=somelist,jump=greyme,x=155,y=15,width=120,height=25
TEXTFIELD_CREATE somewin2 handle=mytext5,name=Phase,x=155,y=45,width=45,height=20
TEXTFIELD_CREATE somewin2 handle=mytext6,name=Period,x=155,y=75,width=45,height=20
TEXTFIELD_CREATE somewin2 handle=mytext7,name=Amplitude,x=155,y=105,width=45,height=20
TEXTFIELD_SETSTRING somewin2 handle=mytext1,text=var1
TEXTFIELD_SETSTRING somewin2 handle=mytext2,text=var2
TEXTFIELD_SETSTRING somewin2 handle=mytext3,text=var3
TEXTFIELD_SETSTRING somewin2 handle=mytext4,text=var4
TEXTFIELD_SETSTRING somewin2 handle=mytext5,text=var5
TEXTFIELD_SETSTRING somewin2 handle=mytext6,text=var6
TEXTFIELD_SETSTRING somewin2 handle=mytext7,text=var7
TEXTFIELD_GHOST somewin2 handle=mytext5,enable=enable
TEXTFIELD_GHOST somewin2 handle=mytext6,enable=enable
TEXTFIELD_GHOST somewin2 handle=mytext7,enable=enable
LABEL_CREATE somewin2 handle=thing1,name=StartRow,x=65,y=15,width=50,height=15
LABEL_CREATE somewin2 handle=thing2,name=EndRow,x=65,y=45,width=50,height=15
LABEL_CREATE somewin2 handle=thing3,name=StartValue,x=65,y=75,width=50,height=15
LABEL_CREATE somewin2 handle=thing4,name=EndValue,x=65,y=105,width=50,height=15
LABEL_CREATE somewin2 handle=thing5,name=Initial Phase,x=215,y=45,width=100,height=15
LABEL_CREATE somewin2 handle=thing51,name=(0=0 1=2Pi),x=215,y=56,width=100,height=15
LABEL_CREATE somewin2 handle=thing6,name=Frequency,x=215,y=75,width=100,height=15
LABEL_CREATE somewin2 handle=thing61,name=(1=end-start),x=215,y=86,width=100,height=15
LABEL_CREATE somewin2 handle=thing7,name=Amplitude,x=215,y=105,width=100,height=15
;DROPBOX_CREATE somewin2 handle=somelist,jump=greyme,x=155,y=15,width=120,height=25
;DROPBOX_CLEAR somewin2 handle=somelist
DROPBOX_ADDSTRING somewin2 handle=somelist,text=varlinearbuzz
DROPBOX_ADDSTRING somewin2 handle=somelist,text=varlinear
DROPBOX_SETSTRING somewin2 handle=somelist,text=varlinearbuzz
;DROPBOX_ADDSTRING somewin2 handle=somelist,text=varsomething
BUTTON_CREATE somewin2 name=do me,jump=init,x=305,y=15,width=55,height=110
IDLE WINDOWED

label greyme
DROPBOX_GETSTRING somewin2 handle=somelist,destvariable=buzz
compare buzz varlinear
isequal greythat
TEXTFIELD_GHOST somewin2 handle=mytext5,enable=enable
TEXTFIELD_GHOST somewin2 handle=mytext6,enable=enable
TEXTFIELD_GHOST somewin2 handle=mytext7,enable=enable
idle windowed
label greythat
TEXTFIELD_GHOST somewin2 handle=mytext5,enable=disable
TEXTFIELD_GHOST somewin2 handle=mytext6,enable=disable
TEXTFIELD_GHOST somewin2 handle=mytext7,enable=disable
idle windowed


LABEL init

;get values from the gui
TEXTFIELD_GETSTRING somewin2 handle=mytext1,destvariable=var1
TEXTFIELD_GETSTRING somewin2 handle=mytext2,destvariable=var2
TEXTFIELD_GETSTRING somewin2 handle=mytext3,destvariable=var3
TEXTFIELD_GETSTRING somewin2 handle=mytext4,destvariable=var4
TEXTFIELD_GETSTRING somewin2 handle=mytext5,destvariable=var5
TEXTFIELD_GETSTRING somewin2 handle=mytext6,destvariable=var6
TEXTFIELD_GETSTRING somewin2 handle=mytext7,destvariable=var7
DROPBOX_GETSTRING somewin2 handle=somelist,destvariable=buzz_interpolate

LABEL bypassgui

;make sure we are at pattern screen
SETACTIVESCREEN PATTERN
ACTIVATE

;compare interpolate 1
;isequal continue_bypass
GETPARAM
SETSTRING lastp PARAM
UPDATEPATTERN
gosub get_back_to_our_original_column
;label continue_bypass

;init the variables used
SETSTRING temp1 0
SETSTRING temp2 0
SETSTRING temp3 16
SETSTRING temp4 0
SETSTRING var3size 0
SETSTRING var4size 0

;swap var1 with var2 and var3 with var4 if need be (like the user putting endrow on startrow input)
SETSTRING temp1 var1
COMPARE var1 var2
ISGREATER swap
GOTO beforeagain
LABEL swap
setstring var1 var2
setstring var2 temp1
setstring temp1 var4
setstring var4 var3
setstring var3 temp1

;if pattern's maxrows is too little make it 512.
label beforeagain
compare rows var2
isgreater gohereinstead
RIGHTCLICK
VBKEY {DOWN}{DOWN}{DOWN}{DOWN}{ENTER}{TAB}{PGDN}{ENTER}
add 1 var2 thismax

LABEL gohereinstead

setstring temp2 0
setstring tempdownvalue var1
gosub go_down_till_you_reach_tempdownvalue

;if ( box = 0 || box = null ) do buzz interpolate
COMPARE buzz_interpolate varlinearbuzz
ISEQUAL somethingelse

;else if box = 1 do ps linear interpolate
COMPARE buzz_interpolate varlinear
ISEQUAL somethingelseelse

;else if box = 2 do ps log interpolate
;not implemented yet
COMPARE buzz_interpolate varsomething
ISEQUAL somethingelseelseelse

;do ps linear interpolation
LABEL somethingelseelse

setstring var33 var3
setstring m 0
;need to hex-dec it for some math shit
dec var3 var3
dec var4 var4
subtract var1 var2 t1
subtract var3 var4 t2
compare t2 0
isequal div0_patch1
divide t2 t1 m
label div0_patch1
multiply m var4 x1
subtract x1 var2 x2

setstring somecounter var1
add 1 somecounter
add 2 var2

compare var7 0
isequal bypass_sin
;init stuff for sinusmod
multiply 2 pi twopi
multiply var5 twopi phaser

;calculate first value of the iteration - adding to var3 the sinusmodulation
gosub sinusmod_math
label bypass_sin
setstring oldvar3 var3
integer var3 var3
hex var3 var3
;returns var3 back to hex couz thats what we are gonna put on screen instead of its decimal value.

;put var3 value on somecounter(var1) and update, loop till we are past var2.
LABEL loophere

enterparam var3
vbkey {down}
subtract x2 somecounter x3
compare m 0
isequal div0_patch3
divide m x3 var3
goto div0_patch3_skip
label div0_patch3
setstring var3 oldvar3
label div0_patch3_skip
;add to var3 the sinusmodulation
compare var7 0
isequal bypass_sin_again
gosub sinusmod_math
label bypass_sin_again
integer var3 var3
hex var3 var3
add 1 somecounter somecounter

compare somecounter var2
isless loophere

GOTO theend

;-- end of ps linear interpolate
;-- start of ps log interpolate -- not beeing used/working

LABEL somethingelseelseelse

setstring var33 var3
setstring m 0
dec var3 var3
dec var4 var4
subtract var1 var2 t1
subtract var3 var4 t2
compare t2 0
isequal div0_patch2
divide t2 t1 m
label div0_patch2
multiply m var4 x1
subtract x1 var2 x2

setstring somecounter var1
add 1 somecounter
add 2 var2
hex var3 var3

;put value and go to next value loop.
LABEL loopherer
length var3size var3
vbkey .{up}
compare var3size 1
isequal bb1fr
LABEL bbb1fr
add -1 var3size
VBKEY {LEFT}
compare 1 var3size
isless bbb1fr

length var3size var3
LABEL bb1fr
compare var3size 2
isless goon1fr
VBKEY var3
label this1fr
add -1 var3size
VBKEY {UP}.{UP}
compare var3size 1
isgreater this1fr

VBKEY {UP}{RIGHT}

length var3size var3
add -1 var3size
RIGHT var3 var3size
length var3size var3
goto bb1fr
LABEL goon1fr
VBKEY var3
;VBKEY {UP}

subtract x2 somecounter x3
;info x3
compare m 0
isequal div0_patch4
divide m x3 var3
label div0_patch4
;info var3
integer var3 var3
hex var3 var3
;info var3
;VBKEY {DOWN}
add 1 somecounter somecounter
;info somecounter
;info var3
compare somecounter var2
isless loopherer

;-- end of ps log interpolate -- not beeing used/working

LABEL reallygetout
GOTO theend


;does buzz interpolate method
LABEL somethingelse

;puts value on the start row
enterparam var3
BLOCKSTART

setstring tempdownvalue var2
gosub go_down_till_you_reach_tempdownvalue

;puts value from var4 on end row
enterparam var4
BLOCKEND

INTERPOLATE
;-- end of buzz interpolate
GOTO theend

; y = var3 + var7 * sin( var5*2PI + var6*(ourcounter/t1)*2PI )
label sinusmod_math
subtract var1 somecounter ourcounter
divide t1 ourcounter ourxvalue
multiply var6 ourxvalue ourxvalue
multiply ourxvalue twopi theothervalue
add phaser theothervalue sinusite
sin sinusite sinhue
multiply var7 sinhue sinusmod
add sinusmod var3 var3
return

label get_back_to_our_original_column
GOTO thisstep
LABEL nextstep
KEY {RIGHT}
LABEL thisstep
GETPARAM
SETSTRING pname PARAM
COMPARE pname lastp
ISEQUAL nextstep2
GOTO nextstep
LABEL nextstep2
return

label go_down_till_you_reach_tempdownvalue
LABEL again
SETSTRING temp1 temp2
SUBTRACT temp1 tempdownvalue temp4
COMPARE temp4 temp3
ISGREATER nexthop
COMPARE temp2 tempdownvalue
ISLESS next
GOTO continue
LABEL next
KEY {down}
ADD 1 temp2
GOTO again
LABEL nexthop
KEY {pgdn}
ADD 16 temp2
GOTO again
LABEL continue
return


LABEL theend

compare !steps \!steps
isequal goend
SETSTEP !steps
label goend
;info !steps

compare thismax \thismax
isequal goend3
RIGHTCLICK
VBKEY {DOWN}{DOWN}{DOWN}{DOWN}{ENTER}{TAB}
VBKEY thismax
VBKEY {ENTER}
label goend3


compare interpolate \interpolate
isequal goend2

setstring interpolate 0
return

label goend2