[update 3] trying to do something like this
Code: Select all
' faster than int() http://www.freebasic.net/forum/viewtopic.php?p=118633
#define flo(x) (((x)*2.0-0.5)shr 1)
type func_vars extends object
decl constructor ( sng = .5, sng = .5, sng = 0, sng = 1 )
decl virtual function wave( sng ) sng
decl sub function_path( as string )
protected:
sng clip_hi
sng clip_lo
sng off_y
sng amp
End Type
function func_vars.wave( i sng ) sng: return -1/0
End Function
'' polymorphism emulation (?) courtesy of fxm
type func_sw extends func_vars
decl virtual function wave( sng ) sng
End Type
function func_sw.wave( i sng ) sng
'' sawtooth: [range -.5 to .5]
i = (i - flo(i) - .5) * amp + off_y
return i + _
( i - clip_hi ) * ( i > clip_hi ) + _
( i - clip_lo ) * ( i < clip_lo )
End function
type func_tr extends func_vars
decl virtual function wave( sng ) sng
End Type
function func_tr.wave( i sng ) sng
'' triangle: [range -.25 to .25]
i = ( abs(i - flo(i) - .5) - .25 ) * amp + off_y '' modified Stonemonkey formula
return i + _
( i - clip_hi ) * ( i > clip_hi ) + _
( i - clip_lo ) * ( i < clip_lo )
End function
'' back to regularly scheduled programming
constructor func_vars( hi sng, lo sng, off sng, _amp sng )', wave_name as string )
clip_hi = hi
clip_lo = lo
off_y = off
amp = _amp
End constructor
sub func_vars.function_path( wave_name as string )
#define polymorph_hack(fname) _
Cptr(any ptr ptr, @this)[0] = Cptr(any ptr ptr, @fname())[0]
select case lcase( left(wave_name,3) )
case "saw"
polymorph_hack(func_sw)
case else
polymorph_hack(func_tr)
End Select
End Sub
old functions which fxm (on next page) identified as problematic
Code: Select all
constructor func_vars( lo sng, hi sng, off sng, amp sng, wave_name as string )
values lo,hi,off,amp, tr
if lcase( left(wave_name,3) ) <> "tri" then values lo,hi,off,amp*2, sw
End constructor
sub func_vars.values( lo sng, hi sng, off sng, amp sng, pf as function ( sng ) sng )
this = type(lo,hi,off,amp)
wave = @pf
End Sub
thanks paul doe & fxm. i did see 'This' as a parameter. Hopefully there's a way around that