module Sound.Sc3.Common.Math.Warp where
import Numeric
import qualified Sound.Sc3.Common.Math as Math
type Warp_f t = t -> t -> t -> t
warp_lin :: Fractional t => Warp_f t
warp_lin :: forall t. Fractional t => Warp_f t
warp_lin t
l t
r t
n = let z :: t
z = t
r forall a. Num a => a -> a -> a
- t
l in t
n forall a. Num a => a -> a -> a
* t
z forall a. Num a => a -> a -> a
+ t
l
warp_lin_inv :: Fractional t => Warp_f t
warp_lin_inv :: forall t. Fractional t => Warp_f t
warp_lin_inv t
l t
r t
n = let z :: t
z = t
r forall a. Num a => a -> a -> a
- t
l in (t
n forall a. Num a => a -> a -> a
- t
l) forall a. Fractional a => a -> a -> a
/ t
z
warp_exp :: Floating a => Warp_f a
warp_exp :: forall a. Floating a => Warp_f a
warp_exp a
l a
r a
n = let z :: a
z = a
r forall a. Fractional a => a -> a -> a
/ a
l in (a
z forall a. Floating a => a -> a -> a
** a
n) forall a. Num a => a -> a -> a
* a
l
warp_exp_inv :: Floating a => Warp_f a
warp_exp_inv :: forall a. Floating a => Warp_f a
warp_exp_inv a
l a
r a
n = let z :: a
z = a
r forall a. Fractional a => a -> a -> a
/ a
l in forall a. Floating a => a -> a -> a
logBase a
z (a
n forall a. Fractional a => a -> a -> a
/ a
l)
warp_cos :: Floating t => Warp_f t
warp_cos :: forall a. Floating a => Warp_f a
warp_cos t
l t
r t
n = forall t. Fractional t => Warp_f t
warp_lin t
0 (t
r forall a. Num a => a -> a -> a
- t
l) (t
0.5 forall a. Num a => a -> a -> a
- (forall a. Floating a => a -> a
cos (forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* t
n) forall a. Fractional a => a -> a -> a
/ t
2))
warp_cos_inv :: Floating a => Warp_f a
warp_cos_inv :: forall a. Floating a => Warp_f a
warp_cos_inv a
l a
r a
n = forall a. Floating a => a -> a
acos (a
1.0 forall a. Num a => a -> a -> a
- (forall t. Fractional t => Warp_f t
warp_lin_inv a
0 (a
r forall a. Num a => a -> a -> a
- a
l) a
n forall a. Num a => a -> a -> a
* a
2)) forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a
pi
warp_sin :: Floating t => Warp_f t
warp_sin :: forall a. Floating a => Warp_f a
warp_sin t
l t
r t
n = forall t. Fractional t => Warp_f t
warp_lin t
0 (t
r forall a. Num a => a -> a -> a
- t
l) (forall a. Floating a => a -> a
sin (forall a. Floating a => a
pi forall a. Num a => a -> a -> a
* t
0.5 forall a. Num a => a -> a -> a
* t
n))
warp_sin_inv :: Floating t => Warp_f t
warp_sin_inv :: forall a. Floating a => Warp_f a
warp_sin_inv t
l t
r t
n = forall a. Floating a => a -> a
asin (forall t. Fractional t => Warp_f t
warp_lin_inv t
0 (t
r forall a. Num a => a -> a -> a
- t
l) t
n) forall a. Fractional a => a -> a -> a
/ (forall a. Floating a => a
pi forall a. Fractional a => a -> a -> a
/ t
2)
warp_amp :: Num a => Warp_f a
warp_amp :: forall a. Num a => Warp_f a
warp_amp a
l a
r a
n = (a
n forall a. Num a => a -> a -> a
* a
n) forall a. Num a => a -> a -> a
* (a
r forall a. Num a => a -> a -> a
- a
l) forall a. Num a => a -> a -> a
+ a
l
warp_amp_inv :: Floating a => Warp_f a
warp_amp_inv :: forall a. Floating a => Warp_f a
warp_amp_inv a
l a
r a
n = forall a. Floating a => a -> a
sqrt ((a
n forall a. Num a => a -> a -> a
- a
l) forall a. Fractional a => a -> a -> a
/ (a
r forall a. Num a => a -> a -> a
- a
l))
warp_db :: (Eq a, Floating a) => Warp_f a
warp_db :: forall a. (Eq a, Floating a) => Warp_f a
warp_db a
l a
r a
n =
let n' :: a
n' = if a
n forall a. Eq a => a -> a -> Bool
== a
0 then -a
180 else forall a. Floating a => a -> a
Math.amp_to_db (a
n forall a. Num a => a -> a -> a
* a
n)
in forall a. Fractional a => a -> a -> a -> a -> a -> a
Math.sc3_linlin a
n' (-a
180) a
0 a
l a
r
warp_db_inv :: Floating a => Warp_f a
warp_db_inv :: forall a. Floating a => Warp_f a
warp_db_inv a
l a
r a
n = forall a. Floating a => a -> a
sqrt (forall a. Floating a => a -> a
Math.db_to_amp (forall a. Fractional a => a -> a -> a -> a -> a -> a
Math.sc3_linlin a
n a
l a
r (-a
180) a
0))
warp_curve :: (Ord a, Floating a) => a -> Warp_f a
warp_curve :: forall a. (Ord a, Floating a) => a -> Warp_f a
warp_curve a
k a
l a
r a
n =
if forall a. Num a => a -> a
abs a
k forall a. Ord a => a -> a -> Bool
< a
0.001
then forall t. Fractional t => Warp_f t
warp_lin a
l a
r a
n
else let e :: a
e = forall a. Floating a => a -> a
exp a
k
a :: a
a = (a
r forall a. Num a => a -> a -> a
- a
l) forall a. Fractional a => a -> a -> a
/ (a
1 forall a. Num a => a -> a -> a
- a
e)
b :: a
b = a
l forall a. Num a => a -> a -> a
+ a
a
in a
b forall a. Num a => a -> a -> a
- ((a
e forall a. Floating a => a -> a -> a
** a
n) forall a. Num a => a -> a -> a
* a
a)
warp_curve_inv :: (Ord a, Floating a) => a -> Warp_f a
warp_curve_inv :: forall a. (Ord a, Floating a) => a -> Warp_f a
warp_curve_inv a
k a
l a
r a
n =
if forall a. Num a => a -> a
abs a
k forall a. Ord a => a -> a -> Bool
< a
0.001
then forall t. Fractional t => Warp_f t
warp_lin a
l a
r a
n
else let e :: a
e = forall a. Floating a => a -> a
exp a
k
a :: a
a = (a
r forall a. Num a => a -> a -> a
- a
l) forall a. Fractional a => a -> a -> a
/ (a
1 forall a. Num a => a -> a -> a
- a
e)
b :: a
b = a
l forall a. Num a => a -> a -> a
+ a
a
in forall a. Floating a => a -> a
log ((a
b forall a. Num a => a -> a -> a
- a
n) forall a. Fractional a => a -> a -> a
/ a
a) forall a. Fractional a => a -> a -> a
/ a
k
warp_named :: (Floating t, RealFrac t) => String -> Maybe (Warp_f t, Warp_f t)
warp_named :: forall t.
(Floating t, RealFrac t) =>
String -> Maybe (Warp_f t, Warp_f t)
warp_named String
nm =
case String
nm of
String
"lin" -> forall a. a -> Maybe a
Just (forall t. Fractional t => Warp_f t
warp_lin,forall t. Fractional t => Warp_f t
warp_lin_inv)
String
"exp" -> forall a. a -> Maybe a
Just (forall a. Floating a => Warp_f a
warp_exp,forall a. Floating a => Warp_f a
warp_exp_inv)
String
"sin" -> forall a. a -> Maybe a
Just (forall a. Floating a => Warp_f a
warp_sin,forall a. Floating a => Warp_f a
warp_sin_inv)
String
"cos" -> forall a. a -> Maybe a
Just (forall a. Floating a => Warp_f a
warp_cos,forall a. Floating a => Warp_f a
warp_cos_inv)
String
"amp" -> forall a. a -> Maybe a
Just (forall a. Num a => Warp_f a
warp_amp,forall a. Floating a => Warp_f a
warp_amp_inv)
String
"db" -> forall a. a -> Maybe a
Just (forall a. (Eq a, Floating a) => Warp_f a
warp_db,forall a. Floating a => Warp_f a
warp_db_inv)
String
_ -> case forall a. Real a => ReadS a -> ReadS a
readSigned forall a. RealFrac a => ReadS a
readFloat String
nm of
[(t
c,String
"")] -> forall a. a -> Maybe a
Just (forall a. (Ord a, Floating a) => a -> Warp_f a
warp_curve t
c,forall a. (Ord a, Floating a) => a -> Warp_f a
warp_curve_inv t
c)
[(t, String)]
_ -> forall a. Maybe a
Nothing