; interpolate v1.1
; written by ps
;
; http://tpolm.org/~ps/
; http://tpolm.org/~ps/buzzscriptshit/
;
; todo - allow being able to enter values all the way to 512 instead of 510/508 limit
;      - do other interpolate methods (polynomial, adsr, ocx envelopes, whatever..)
;
; NOTE: if you leave the dropbox unselected the default will be interpolation using buzz's method,
;       which does not require any of the sinusmodulation values



;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 fast.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, dont forget to also set buzz_interpolate to ps_linear_interpolate
;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

;make sure we are at pattern
SETACTIVESCREEN PATTERN
ACTIVATE
;get param so we can get back to this column later on
GETPARAM
SETSTRING lastp PARAM
SETSTRING maxrows ROWS
;get values into blockstart, blockend and maxrows
UPDATEPATTERN

compare interpolate 1
isequal bypassgui

;init vars used/received on the gui
;var1 startrow, var2 endrow, var3 startvalue, var4 endvalue, var5 phase, var6 period, var7 amplitude
setstring var1 blockstart
setstring var2 blockend
setstring var3 0
setstring var4 FF
setstring var5 0
setstring var6 1
setstring var7 0
setstring varlinearbuzz buzz_interpolate
setstring varlinear ps_linear_interpolate
setstring varsomething ps_log_interpolate
;setstring thisname,Interpolate
;addspace thisname 1
;combine thisname param
setstring thisname param

;do the gui window thingie
WINDOW_CREATE somewin2 name=thisname,width=435,height=165,state=normal,icon=someicon.ico
TEXTFIELD_CREATE somewin2 handle=mytext1,name=StartRow,x=15,y=15,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext2,name=EndRow,x=15,y=45,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext3,name=StartValue,x=15,y=75,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext4,name=EndValue,x=15,y=105,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext5,name=Phase,x=195,y=15,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext6,name=Period,x=195,y=45,width=65,height=20
TEXTFIELD_CREATE somewin2 handle=mytext7,name=Amplitude,x=195,y=75,width=65,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
LABEL_CREATE somewin2 handle=thing1,name=StartRow,x=85,y=15,width=100,height=15
LABEL_CREATE somewin2 handle=thing2,name=EndRow,x=85,y=45,width=100,height=15
LABEL_CREATE somewin2 handle=thing3,name=StartValue,x=85,y=75,width=100,height=15
LABEL_CREATE somewin2 handle=thing4,name=EndValue,x=85,y=105,width=100,height=15
LABEL_CREATE somewin2 handle=thing5,name=Initial Phase,x=265,y=15,width=100,height=15
LABEL_CREATE somewin2 handle=thing51,name=(0=0 1=2Pi),x=265,y=26,width=100,height=15
LABEL_CREATE somewin2 handle=thing6,name=Period,x=265,y=45,width=100,height=15
LABEL_CREATE somewin2 handle=thing61,name=(1=end-start),x=265,y=56,width=100,height=15
LABEL_CREATE somewin2 handle=thing7,name=Amplitude,x=265,y=75,width=100,height=15
DROPBOX_CREATE somewin2 handle=somelist,jump=init,x=195,y=105,width=100,height=25
DROPBOX_ADDSTRING somewin2 handle=somelist,text=varlinearbuzz
DROPBOX_ADDSTRING somewin2 handle=somelist,text=varlinear
;DROPBOX_ADDSTRING somewin2 handle=somelist,text=varsomething
BUTTON_CREATE somewin2 name=do me,jump=init,x=355,y=15,width=55,height=110
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

;get back to our original column
GOTO thisstep
LABEL nextstep
VBKEY {RIGHT}
LABEL thisstep
GETPARAM
SETSTRING pname PARAM
COMPARE pname lastp
ISEQUAL nextstep2
GOTO nextstep
LABEL nextstep2

;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.
;the -4 is there because of the max possible 4 cell drop down when entering values.
label beforeagain
add -4 maxrows
compare maxrows var2
isgreater again
RIGHTCLICK
VBKEY {DOWN}{DOWN}{DOWN}{DOWN}{ENTER}{TAB}{PGDN}{ENTER}

;loop go down till you reach the start row
LABEL again
SETSTRING temp1 temp2
SUBTRACT temp1 var1 temp4
COMPARE temp4 temp3
ISGREATER nexthop
COMPARE temp2 var1
ISLESS next
GOTO continue
LABEL next
VBKEY {down}
ADD 1 temp2
GOTO again
LABEL nexthop
VBKEY {pgdn}
ADD 16 temp2
GOTO again
LABEL continue

;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
;need to hex-dec it for some math shit
dec var3 var3
dec var4 var4
subtract var1 var2 t1
subtract var3 var4 t2
divide t2 t1 m
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
setstring pi 3.1415926
multiply 2 pi twopi
multiply t2 var6 thebase
multiply var5 twopi phaser
setstring sinusmod 1

;calculate first value of the iteration - adding to var3 the sinusmodulation
gosub sinusmod_math
label bypass_sin
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
divide m x3 var3
;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

;not beeing used atm..
;do ps log interpolation
LABEL somethingelseelseelse

setstring var33 var3
dec var3 var3
dec var4 var4
subtract var1 var2 t1
subtract var3 var4 t2
divide t2 t1 m
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
divide m x3 varnoint
;info varnoint
integer varnoint 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

LABEL reallygetout
GOTO theend


;does buzz interpolate method
LABEL somethingelse

;puts value on the start row
enterparam var3
BLOCKSTART

LABEL againagain
SETSTRING temp1 temp2
SETSTRING temp4 var2
SUBTRACT temp1 temp4
COMPARE temp4 temp3
ISGREATER nexthop2
COMPARE temp2 var2
ISLESS nextnext
GOTO continue2
LABEL nextnext
VBKEY {down}
ADD 1 temp2
GOTO againagain
LABEL nexthop2
VBKEY {pgdn}
ADD 16 temp2
GOTO againagain
LABEL continue2

;puts value from var4 on end row
enterparam var4
BLOCKEND

INTERPOLATE
;-- end of buzz interpolate
GOTO theend

label sinusmod_math
subtract var1 somecounter ourcounter
;info ourcounter
divide thebase ourcounter ourxvalue
multiply ourxvalue twopi theothervalue
add phaser theothervalue sinusite
sin sinusite sinhue
multiply var7 sinhue sinusmod
add sinusmod var3 var3
return

LABEL theend

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

compare interpolate \interpolate
isequal goend2

setstring interpolate 0
return

label goend2
