Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- {-# LANGUAGE DeriveGeneric #-}
- import System.Environment
- import GHC.Generics
- import Data.Aeson
- import Data.Text (Text)
- import Data.List (intersperse)
- import Data.Map (Map)
- import qualified Data.Map as Map
- import Data.Maybe
- import Control.Arrow ((>>>))
- import qualified Data.ByteString.Lazy as B
- data Chord = Chord {
- delta :: Int
- , pitch :: Int
- , instrument :: Text
- , start :: Int
- , duration :: Int
- , velocity :: Int
- } deriving (Generic, Show)
- instance ToJSON Chord
- -- toEncoding = genericToEncoding defaultOptions
- instance FromJSON Chord
- -- (this mimics clojure's partition function)
- partition :: Int -> Int -> [a] -> [[a]]
- partition _ _ [] = []
- partition cnt step xs =
- (take cnt xs) : (partition cnt step (drop step xs))
- --getStartPoint :: Chord -> Int
- --getStartPoint chord = start chord
- getDelta :: Num a => [a] -> a
- getDelta [x, y] = y - x
- getDelta _ = 0
- getDeltas :: [Chord] -> [Int]
- getDeltas =
- map start
- >>> partition 2 1
- >>> init
- >>> map getDelta
- >>> (:) 0
- updateDelta :: Chord -> Int -> Chord
- updateDelta chord value =
- chord { delta = value }
- -- Read a list of json objects ("chords") from a file.
- -- Calculate the deltas between each chord and "assoc" the deltas onto the chords.
- -- Run like this:
- -- ghc chords.hs -o chords && ./chords
- main :: IO ()
- main = do
- args <- getArgs
- let inputFileName = head args
- chords <- B.readFile inputFileName
- let decoded = decode chords
- case decoded of
- Nothing ->
- print "Could not decode!"
- Just decodedChords ->
- let
- deltas = getDeltas decodedChords
- in
- print $ map (\(chord, value) -> updateDelta chord value) $ zip decodedChords deltas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement