diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2018-05-15 23:08:51 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2018-05-15 23:08:51 +0200 |
commit | 19b440fbabf5bc95e97a7a53119ec6218c3639d7 (patch) | |
tree | b33a0c3c5ddcbc41853af3bec260dee0dd5f8cfa /src/Trivmix | |
parent | 7bd8b73c107590bc2e578395fe940b95752654c0 (diff) | |
download | trivmix-master.tar trivmix-master.tar.gz trivmix-master.tar.bz2 trivmix-master.tar.xz trivmix-master.zip |
Diffstat (limited to 'src/Trivmix')
-rw-r--r-- | src/Trivmix/Types.hs | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/Trivmix/Types.hs b/src/Trivmix/Types.hs index 5e4660d..347be8e 100644 --- a/src/Trivmix/Types.hs +++ b/src/Trivmix/Types.hs | |||
@@ -26,12 +26,16 @@ import Data.Default | |||
26 | import Data.Function (on) | 26 | import Data.Function (on) |
27 | 27 | ||
28 | import Refined | 28 | import Refined |
29 | import Data.AEq | ||
29 | 30 | ||
30 | import Data.Scientific | 31 | import Data.Scientific |
31 | import Data.Scientific.Lift | 32 | import Data.Scientific.Lift |
32 | 33 | ||
34 | import Refined.AEq | ||
35 | |||
33 | 36 | ||
34 | type Level' = Refined NonNegative Scientific | 37 | type Level' = Refined NonNegative Scientific |
38 | |||
35 | data Level = Lin { toLin :: Level' } | DB { toLin :: Level' } | 39 | data Level = Lin { toLin :: Level' } | DB { toLin :: Level' } |
36 | 40 | ||
37 | instance Num Level where | 41 | instance Num Level where |
@@ -43,7 +47,7 @@ instance Num Level where | |||
43 | fromInteger = Lin . either error id . refine . fromInteger | 47 | fromInteger = Lin . either error id . refine . fromInteger |
44 | 48 | ||
45 | asScientific :: (Scientific -> Scientific -> Scientific) -> Level -> Level -> Either String Level | 49 | asScientific :: (Scientific -> Scientific -> Scientific) -> Level -> Level -> Either String Level |
46 | asScientific ((`on` toScientific) -> f) x y = toLvl <$> refine (f x y) | 50 | asScientific ((`on` toScientific) -> f) x y = toLvl <$> refineSticky (f x y) |
47 | where | 51 | where |
48 | toLvl | 52 | toLvl |
49 | | DB _ <- x = DB | 53 | | DB _ <- x = DB |
@@ -66,7 +70,13 @@ linToDb :: Level' -> Scientific | |||
66 | linToDb (unrefine -> x) = realToFrac (20 * (logBase 10 $ toRealFloat x) :: Double) | 70 | linToDb (unrefine -> x) = realToFrac (20 * (logBase 10 $ toRealFloat x) :: Double) |
67 | 71 | ||
68 | dBToLin :: Scientific -> Level' | 72 | dBToLin :: Scientific -> Level' |
69 | dBToLin x = either error id . refine . realToFrac $ (10 ** (0.05 * toRealFloat x) :: Double) | 73 | dBToLin x = either error id . refineSticky . realToFrac $ (10 ** (0.05 * toRealFloat x) :: Double) |
74 | |||
75 | refineSticky :: Scientific -> Either String Level' | ||
76 | refineSticky sc@(toRealFloat -> f) | ||
77 | | f ~== (1 :: Float) = Right ($$(refineTH 1) :: Level') | ||
78 | | f ~== (0 :: Float) = Right ($$(refineTH 0) :: Level') | ||
79 | | otherwise = refine sc | ||
70 | 80 | ||
71 | instance Show Level where | 81 | instance Show Level where |
72 | show (Lin (unrefine -> x)) = show x | 82 | show (Lin (unrefine -> x)) = show x |
@@ -84,7 +94,7 @@ instance Read Level where | |||
84 | return . DB $ dBToLin db | 94 | return . DB $ dBToLin db |
85 | parseLin = do | 95 | parseLin = do |
86 | lin <- readS_to_Prec readsPrec | 96 | lin <- readS_to_Prec readsPrec |
87 | either (const mzero) (return . Lin) $ refine lin | 97 | either (const mzero) (return . Lin) $ refineSticky lin |
88 | 98 | ||
89 | instance Eq Level where | 99 | instance Eq Level where |
90 | (==) = (==) `on` toLin | 100 | (==) = (==) `on` toLin |