Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3.  
  4. import System.Environment
  5.  
  6. import GHC.Generics
  7.  
  8. import Data.Aeson
  9. import Data.Text (Text)
  10. import Data.List (intersperse)
  11. import Data.Map (Map)
  12. import qualified Data.Map as Map
  13. import Data.Maybe
  14. import Control.Arrow ((>>>))
  15.  
  16. import qualified Data.ByteString.Lazy as B
  17.  
  18. data Chord = Chord {
  19. delta :: Int
  20. , pitch :: Int
  21. , instrument :: Text
  22. , start :: Int
  23. , duration :: Int
  24. , velocity :: Int
  25. } deriving (Generic, Show)
  26.  
  27. instance ToJSON Chord
  28. -- toEncoding = genericToEncoding defaultOptions
  29. instance FromJSON Chord
  30.  
  31. -- (this mimics clojure's partition function)
  32. partition :: Int -> Int -> [a] -> [[a]]
  33. partition _ _ [] = []
  34. partition cnt step xs =
  35. (take cnt xs) : (partition cnt step (drop step xs))
  36.  
  37. --getStartPoint :: Chord -> Int
  38. --getStartPoint chord = start chord
  39.  
  40. getDelta :: Num a => [a] -> a
  41. getDelta [x, y] = y - x
  42. getDelta _ = 0
  43.  
  44. getDeltas :: [Chord] -> [Int]
  45. getDeltas =
  46. map start
  47. >>> partition 2 1
  48. >>> init
  49. >>> map getDelta
  50. >>> (:) 0
  51.  
  52. updateDelta :: Chord -> Int -> Chord
  53. updateDelta chord value =
  54. chord { delta = value }
  55.  
  56. -- Read a list of json objects ("chords") from a file.
  57. -- Calculate the deltas between each chord and "assoc" the deltas onto the chords.
  58. -- Run like this:
  59. -- ghc chords.hs -o chords && ./chords
  60. main :: IO ()
  61. main = do
  62. args <- getArgs
  63. let inputFileName = head args
  64. chords <- B.readFile inputFileName
  65. let decoded = decode chords
  66. case decoded of
  67. Nothing ->
  68. print "Could not decode!"
  69. Just decodedChords ->
  70. let
  71. deltas = getDeltas decodedChords
  72. in
  73. print $ map (\(chord, value) -> updateDelta chord value) $ zip decodedChords deltas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement