diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-21 20:19:49 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-21 20:21:48 +0200 |
commit | 5c320eb3668f153c229fba7b118b20ccffb8734b (patch) | |
tree | 27df87d797f15562ade74c7506b002950618bb63 /src/Trivmix | |
parent | 0c8ccf5e367b08fc63fff31d364f77f9ffe9762e (diff) | |
download | trivmix-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/Trivmix')
-rw-r--r-- | src/Trivmix/Types.hs | 22 |
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 | |||
43 | withResolution :: (HasResolution a) => (Integer -> f a) -> f a | 43 | withResolution :: (HasResolution a) => (Integer -> f a) -> f a |
44 | withResolution f = withType (f . resolution) | 44 | withResolution f = withType (f . resolution) |
45 | 45 | ||
46 | linToDb :: Float -> Float | ||
47 | linToDb x = 10 ** (0.05 * x) | ||
48 | |||
49 | dBToLin :: Float -> Float | ||
50 | dBToLin x = 20 * (logBase 10 x) | ||
51 | |||
46 | instance Show Level where | 52 | instance 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 | ||
52 | instance Read Level where | 58 | instance 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 | ||
83 | instance Num a => Adjustable a where | 89 | -- instance Num a => Adjustable a where |
84 | add = (+) | 90 | -- add = (+) |
85 | sub = (-) | 91 | -- sub = (-) |
92 | |||
93 | instance 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 | ||
87 | doAdjustment :: Adjustable a => a -> Adjustment a -> a | 99 | doAdjustment :: Adjustable a => a -> Adjustment a -> a |
88 | doAdjustment _ (Set y) = y | 100 | doAdjustment _ (Set y) = y |