summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2015-06-21 20:19:49 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2015-06-21 20:21:48 +0200
commit5c320eb3668f153c229fba7b118b20ccffb8734b (patch)
tree27df87d797f15562ade74c7506b002950618bb63 /src
parent0c8ccf5e367b08fc63fff31d364f77f9ffe9762e (diff)
downloadtrivmix-5c320eb3668f153c229fba7b118b20ccffb8734b.tar
trivmix-5c320eb3668f153c229fba7b118b20ccffb8734b.tar.gz
trivmix-5c320eb3668f153c229fba7b118b20ccffb8734b.tar.bz2
trivmix-5c320eb3668f153c229fba7b118b20ccffb8734b.tar.xz
trivmix-5c320eb3668f153c229fba7b118b20ccffb8734b.zip
Refined Adjustable Level instance
Diffstat (limited to 'src')
-rw-r--r--src/Trivmix/Types.hs22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/Trivmix/Types.hs b/src/Trivmix/Types.hs
index 66accdf..01418bf 100644
--- a/src/Trivmix/Types.hs
+++ b/src/Trivmix/Types.hs
@@ -43,18 +43,24 @@ withType f = f undefined
43withResolution :: (HasResolution a) => (Integer -> f a) -> f a 43withResolution :: (HasResolution a) => (Integer -> f a) -> f a
44withResolution f = withType (f . resolution) 44withResolution f = withType (f . resolution)
45 45
46linToDb :: Float -> Float
47linToDb x = 10 ** (0.05 * x)
48
49dBToLin :: Float -> Float
50dBToLin x = 20 * (logBase 10 x)
51
46instance Show Level where 52instance Show Level where
47 show (Lin x) = show x 53 show (Lin x) = show x
48 show (DB x) = (show $ (withPrec x' :: Milli)) ++ "dB" 54 show (DB x) = (show $ (withPrec x' :: Milli)) ++ "dB"
49 where 55 where
50 x' = 20 * (logBase 10 x) 56 x' = dBToLin x
51 57
52instance Read Level where 58instance Read Level where
53 readsPrec i = map toL . readsPrec i 59 readsPrec i = map toL . readsPrec i
54 where 60 where
55 toL :: (Float, String) -> (Level, String) 61 toL :: (Float, String) -> (Level, String)
56 toL (f, str) 62 toL (f, str)
57 | ((==) `on` CI.mk) prec unit = (DB $ 10 ** (0.05 * f), rest) 63 | ((==) `on` CI.mk) prec unit = (DB $ linToDb f, rest)
58 | otherwise = (Lin f, str) 64 | otherwise = (Lin f, str)
59 where 65 where
60 prec = take lU str 66 prec = take lU str
@@ -80,9 +86,15 @@ class Adjustable a where
80 add :: a -> a -> a 86 add :: a -> a -> a
81 sub :: a -> a -> a 87 sub :: a -> a -> a
82 88
83instance Num a => Adjustable a where 89-- instance Num a => Adjustable a where
84 add = (+) 90-- add = (+)
85 sub = (-) 91-- sub = (-)
92
93instance Adjustable Level where
94 add (DB x) (DB y) = DB $ ((+) `on` linToDb) x y
95 add x y = x + y
96 sub (DB x) (DB y) = DB $ ((-) `on` linToDb) x y
97 sub x y = x - y
86 98
87doAdjustment :: Adjustable a => a -> Adjustment a -> a 99doAdjustment :: Adjustable a => a -> Adjustment a -> a
88doAdjustment _ (Set y) = y 100doAdjustment _ (Set y) = y