module Sound.SC3.UGen.Optimise where
import System.Random
import Sound.SC3.UGen.Math
import Sound.SC3.UGen.Rate
import Sound.SC3.UGen.Type
import Sound.SC3.UGen.UGen
mul_add_optimise :: UGen -> UGen
mul_add_optimise u =
case u of
Primitive_U (Primitive _ "BinaryOpUGen" _ [_] (Special 0) NoId) -> mul_add_optimise_direct u
_ -> u
c_rand :: Random a => Int -> a -> a -> a
c_rand z l r = fst (randomR (l,r) (mkStdGen z))
c_irand :: (Num b, RealFrac a, Random a) => Int -> a -> a -> b
c_irand z l r = fromInteger (round (c_rand z l r))
ugen_optimise_ir_rand :: UGen -> UGen
ugen_optimise_ir_rand =
let f u =
case u of
Primitive_U p ->
case p of
Primitive IR "Rand" [Constant_U (Constant l),Constant_U (Constant r)] [IR] _ (UId z) ->
Constant_U (Constant (c_rand z l r))
Primitive IR "IRand" [Constant_U (Constant l),Constant_U (Constant r)] [IR] _ (UId z) ->
Constant_U (Constant (c_irand z l r))
_ -> u
_ -> u
in ugenTraverse f
ugen_optimise_const_operator :: UGen -> UGen
ugen_optimise_const_operator =
let f u =
case u of
Primitive_U p ->
case p of
Primitive _ "BinaryOpUGen" [Constant_U (Constant l)
,Constant_U (Constant r)] [_] (Special z) _ ->
case binop_special_hs z of
Just fn -> Constant_U (Constant (fn l r))
_ -> u
Primitive _ "UnaryOpUGen" [Constant_U (Constant i)] [_] (Special z) _ ->
case uop_special_hs z of
Just fn -> Constant_U (Constant (fn i))
_ -> u
_ -> u
_ -> u
in ugenTraverse f
constant_opt :: UGen -> Maybe Sample
constant_opt = u_constant . ugen_optimise_ir_rand