Skip to content

Commit

Permalink
fix musicology-plotting (breaking AESON changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
chfin committed May 16, 2024
1 parent 65d8693 commit 21d3be9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions musicology-core/src/Musicology/Core.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE StandaloneDeriving #-}
Expand Down
1 change: 1 addition & 0 deletions musicology-pitch/src/Musicology/Pitch/Class.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down
17 changes: 11 additions & 6 deletions musicology-plotting/src/Musicology/Plotting/Plotting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Musicology.Core
import Graphics.Vega.VegaLite
import Data.Aeson as J
import Data.Aeson.Encode.Pretty ( encodePretty )
import qualified Data.Aeson.KeyMap as KM
import qualified Data.ByteString.Lazy.Char8 as BS8
import qualified Data.HashMap.Strict as HM
import qualified Data.Vector as V
import qualified Data.Functor.Identity as ID
import qualified Data.Text as T
import qualified Data.Vector as V
import Data.Monoid ( (<>) )

import System.IO.Temp ( emptySystemTempFile )
Expand All @@ -34,12 +35,16 @@ viewPlot plot = do
fixRatios :: Value -> Value
fixRatios (J.Object o) = J.Object $ fixRatio "onset" $ fixRatio "offset" o
where
fixRatio name o = HM.adjust fix name o
fixRatio name o = adjust fix name o
fix (J.Object rat) =
let (J.Number num) = HM.lookupDefault (J.Number 0) "numerator" rat
(J.Number den) = HM.lookupDefault (J.Number 1) "denominator" rat
let (J.Number num) = maybe (J.Number 0) id $ KM.lookup "numerator" rat
(J.Number den) = maybe (J.Number 1) id $ KM.lookup "denominator" rat
in toJSON $ num / den
fix v = v
adjust f k map = ID.runIdentity $ KM.alterF apply k map
where
apply Nothing = ID.Identity Nothing
apply (Just v) = ID.Identity (Just $ f v)
fixRatios v = v

pianorollView
Expand Down Expand Up @@ -90,7 +95,7 @@ plotpolysView notes polys = [layer [background, foreground]]
polyJson name ns = fmap (extend . fixRatios) jNotes
where
(Array jNotes) = toJSON ns
extend (J.Object note) = J.Object $ HM.insert "name" (String name) note
extend (J.Object note) = J.Object $ KM.insert "name" (String name) note
notedat = polyJson "notes" notes
polydat = V.concat $ zipWith (polyJson . mkn "poly ") [1 ..] polys
mkn pfx = (pfx <>) . T.pack . show
Expand Down

0 comments on commit 21d3be9

Please sign in to comment.