Created
April 30, 2016 13:07
-
-
Save danyx23/31d2cf79590bd473b7a35f0aa11dfdf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment