From 4b20993ef22a4872f802e291421e202831642ba8 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 18 May 2021 12:06:24 +0100 Subject: [PATCH] simplify definition of cat, based on compressArc --- src/Sound/Tidal/Core.hs | 16 +++++++--------- src/Sound/Tidal/UI.hs | 4 ++-- src/Sound/Tidal/Utils.hs | 5 ++++- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Sound/Tidal/Core.hs b/src/Sound/Tidal/Core.hs index d9f1e0f80..20fb36ed2 100644 --- a/src/Sound/Tidal/Core.hs +++ b/src/Sound/Tidal/Core.hs @@ -26,6 +26,8 @@ import Data.Fixed (mod') import qualified Data.Map.Strict as Map import Data.Maybe (fromMaybe) import Sound.Tidal.Pattern +import Data.Ratio ((%)) +import Sound.Tidal.Utils (enumerate) -- ** Elemental patterns @@ -259,14 +261,7 @@ append a b = cat [a,b] -- in turn, then the second cycle from each, and so on. cat :: [Pattern a] -> Pattern a cat [] = silence -cat ps = Pattern q - where n = length ps - q st = concatMap (f st) $ arcCyclesZW (arc st) - f st a = query (withResultTime (+offset) p) $ st {arc = Arc (subtract offset (start a)) (subtract offset (stop a))} - where p = ps !! i - cyc = (floor $ start a) :: Int - i = cyc `mod` n - offset = (fromIntegral $ cyc - ((cyc - i) `div` n)) :: Time +cat ps = _slow (toTime $ length ps) $ fastcat ps -- | Alias for 'cat' slowCat :: [Pattern a] -> Pattern a @@ -290,7 +285,10 @@ fastappend = fastAppend -- patterns there are, so the cycles from each are squashed to fit a -- single cycle. fastCat :: [Pattern a] -> Pattern a -fastCat ps = _fast (toTime $ length ps) $ cat ps +fastCat [] = silence +fastCat xs = stack $ map (\(i, x) -> compressArc (a i) x) $ enumerate xs + where a i = Arc (i % len) ((i+1) % len) + len = fromIntegral $ length xs fastcat :: [Pattern a] -> Pattern a fastcat = fastCat diff --git a/src/Sound/Tidal/UI.hs b/src/Sound/Tidal/UI.hs index 84a7ff242..844eaee0e 100644 --- a/src/Sound/Tidal/UI.hs +++ b/src/Sound/Tidal/UI.hs @@ -1056,7 +1056,7 @@ randStruct n = splitQueries $ Pattern {query = f} where as = map (\(i, Arc s' e') -> (Arc (s' + sam s) (e' + sam s), subArc (Arc s e) (Arc (s' + sam s) (e' + sam s)), i)) $ - enumerate $ value $ head $ + enumerateI $ value $ head $ queryArc (randArcs n) (Arc (sam s) (nextSam s)) (Arc s e) = arc st @@ -1398,7 +1398,7 @@ arpg = arpeggiate arpWith :: ([EventF (ArcF Time) a] -> [EventF (ArcF Time) b]) -> Pattern a -> Pattern b arpWith f p = withEvents munge p where munge es = concatMap (spreadOut . f) (groupBy (\a b -> whole a == whole b) $ sortOn whole es) - spreadOut xs = mapMaybe (\(n, x) -> shiftIt n (length xs) x) $ enumerate xs + spreadOut xs = mapMaybe (\(n, x) -> shiftIt n (length xs) x) $ enumerateI xs shiftIt n d (Event c (Just (Arc s e)) a' v) = do a'' <- subArc (Arc newS newE) a' diff --git a/src/Sound/Tidal/Utils.hs b/src/Sound/Tidal/Utils.hs index b77307f10..f52c1b739 100644 --- a/src/Sound/Tidal/Utils.hs +++ b/src/Sound/Tidal/Utils.hs @@ -79,9 +79,12 @@ accumulate (x:xs) = scanl (+) x xs >>> enumerate ["foo","bar","baz"] [(1,"foo"), (2,"bar"), (3,"baz")] -} -enumerate :: [a] -> [(Int, a)] +enumerate :: (Num a, Enum a) => [b] -> [(a, b)] enumerate = zip [0..] +enumerateI :: [b] -> [(Int, b)] +enumerateI = enumerate + {- | split given list of @a@ by given single a, e.g. >>> wordsBy (== ':') "bd:3"