Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yaxu committed Jan 18, 2025
1 parent 1cad7bc commit 2a26000
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions src/Sound/Tidal/Stepwise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,57 @@

module Sound.Tidal.Stepwise where

import Data.List (sort, transpose)
import Data.Maybe (catMaybes, fromMaybe, isJust)
import Data.List (sort, transpose, sortOn)
import Data.Maybe (catMaybes, fromMaybe, isJust, fromJust)

import Sound.Tidal.Core
import Sound.Tidal.Pattern
import Sound.Tidal.UI (while)
import Sound.Tidal.Utils (applyWhen, nubOrd, pairs)
import Sound.Tidal.Utils (applyWhen, nubOrd, pairs, enumerate)

_lcmtactus :: [Pattern a] -> Maybe Time
_lcmtactus pats = foldl1 lcmr <$> (sequence $ map tactus pats)
-- _lcmtactus :: [Pattern a] -> Maybe Time
-- _lcmtactus pats = foldl1 lcmr <$> (sequence $ map tactus pats)



s_patternify :: (a -> Pattern b -> Pattern c) -> (Pattern a -> Pattern b -> Pattern c)
s_patternify f (Pattern _ _ (Just a)) b = f a b
s_patternify f pa p = stepJoin $ (`f` p) <$> pa

s_patternify2 :: (a -> b -> c -> Pattern d) -> Pattern a -> Pattern b -> c -> Pattern d
s_patternify2 f a b p = stepJoin $ (\x y -> f x y p) <$> a <*> b

stepJoin :: Pattern (Pattern a) -> Pattern a
stepJoin pp = Pattern q first_t Nothing

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.10.7

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.8.4

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.1.0 - ghc 9.0.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.6.5

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.8.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 43 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer
where q st@(State a c) = query (timecat $ retime $ slices $ query (rotL (sam $ start a) pp) (st {arc = Arc 0 1})) st
first_t :: Maybe Rational
first_t = tactus $ timecat $ retime $ slices $ queryArc pp (Arc 0 1)

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.10.7

• Couldn't match type ‘Pattern Rational’

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.8.4

• Couldn't match type ‘Pattern Rational’

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.1.0 - ghc 9.0.2

• Couldn't match type: Pattern Rational

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.6.5

• Couldn't match type ‘Pattern Rational’

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: Pattern Rational

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.8.2

• Couldn't match type: Pattern Rational

Check failure on line 46 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: Pattern Rational
retime :: [(Time, Pattern a)] -> [(Time, Pattern a)]
retime xs = map (\(dur, pat) -> adjust dur pat) xs

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.10.7

• Couldn't match type ‘Pattern Rational’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.10.7

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.8.4

• Couldn't match type ‘Pattern Rational’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.8.4

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.1.0 - ghc 9.0.2

• Couldn't match type: Pattern Rational

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.1.0 - ghc 9.0.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.6.5

• Couldn't match type ‘Pattern Rational’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.6.5

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: Pattern Rational

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.8.2

• Couldn't match type: Pattern Rational

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.8.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: Pattern Rational

Check failure on line 48 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer
where occupied_perc = sum $ map fst $ filter (isJust . tactus . snd) xs
occupied_tactus = sum $ catMaybes $ map (tactus . snd) xs
total_tactus = occupied_tactus / occupied_perc

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.10.7

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.8.4

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.1.0 - ghc 9.0.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.6.2.0-p1 - ghc 8.6.5

• Couldn't match type ‘GHC.Real.Ratio Integer’

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.8.2

• Couldn't match type: GHC.Real.Ratio Integer

Check failure on line 51 in src/Sound/Tidal/Stepwise.hs

View workflow job for this annotation

GitHub Actions / cabal 3.12.1.0 - ghc 9.4.8

• Couldn't match type: GHC.Real.Ratio Integer
adjust dur pat@(Pattern {tactus = Just t}) = (t, pat)
adjust dur pat = (dur*total_tactus, pat)
-- break up events at all start/end points, into groups, including empty ones.
slices :: [Event (Pattern a)] -> [(Time, Pattern a)]
slices evs = map (\s -> ((snd s - fst s), stack $ map (\x -> withContext (\c -> combineContexts [c, context x]) $ value x) $ fit s evs)) $ pairs $ sort $ nubOrd $ 0:1:concatMap (\ev -> start (part ev):stop (part ev):[]) evs
-- list of slices of events within the given range
fit :: (Rational, Rational) -> [Event (Pattern a)] -> [Event (Pattern a)]
fit (b,e) evs = catMaybes $ map (match (b,e)) evs
-- slice of event within the given range
match :: (Rational, Rational) -> Event (Pattern a) -> Maybe (Event (Pattern a))
match (b,e) ev = do a <- subArc (Arc b e) $ part ev
return ev {part = a}

s_cat :: [Pattern a] -> Pattern a
s_cat pats = timecat $ map (\pat -> (fromMaybe 1 $ tactus pat, pat)) pats
s_cat pats = innerJoin $ (timecat . map snd . sortOn fst) <$> (tpat $ epats pats)
where epats :: [Pattern a] -> [(Int, Pattern a)]
epats pats = enumerate $ filter (isJust . tactus) pats
tpat :: [(Int, Pattern a)] -> Pattern [(Int, (Time, Pattern a))]
tpat pats = sequence $ map (\(i, pat) -> (\t -> (i, (t, pat))) <$> (fromJust $ tactus pat) ) pats

{-
_s_add :: Rational -> Pattern a -> Pattern a
-- raise error?
_s_add _ pat@(Pattern _ Nothing _) = pat
Expand Down Expand Up @@ -136,32 +173,4 @@ s_expand = s_patternify _s_expand
s_contract :: Pattern Rational -> Pattern a -> Pattern a
s_contract = s_patternify _s_contract
s_patternify :: (a -> Pattern b -> Pattern c) -> (Pattern a -> Pattern b -> Pattern c)
s_patternify f (Pattern _ _ (Just a)) b = f a b
s_patternify f pa p = stepJoin $ (`f` p) <$> pa

s_patternify2 :: (a -> b -> c -> Pattern d) -> Pattern a -> Pattern b -> c -> Pattern d
s_patternify2 f a b p = stepJoin $ (\x y -> f x y p) <$> a <*> b

stepJoin :: Pattern (Pattern a) -> Pattern a
stepJoin pp = Pattern q first_t Nothing
where q st@(State a c) = query (timecat $ retime $ slices $ query (rotL (sam $ start a) pp) (st {arc = Arc 0 1})) st
first_t :: Maybe Rational
first_t = tactus $ timecat $ retime $ slices $ queryArc pp (Arc 0 1)
retime :: [(Time, Pattern a)] -> [(Time, Pattern a)]
retime xs = map (\(dur, pat) -> adjust dur pat) xs
where occupied_perc = sum $ map fst $ filter (isJust . tactus . snd) xs
occupied_tactus = sum $ catMaybes $ map (tactus . snd) xs
total_tactus = occupied_tactus / occupied_perc
adjust dur pat@(Pattern {tactus = Just t}) = (t, pat)
adjust dur pat = (dur*total_tactus, pat)
-- break up events at all start/end points, into groups, including empty ones.
slices :: [Event (Pattern a)] -> [(Time, Pattern a)]
slices evs = map (\s -> ((snd s - fst s), stack $ map (\x -> withContext (\c -> combineContexts [c, context x]) $ value x) $ fit s evs)) $ pairs $ sort $ nubOrd $ 0:1:concatMap (\ev -> start (part ev):stop (part ev):[]) evs
-- list of slices of events within the given range
fit :: (Rational, Rational) -> [Event (Pattern a)] -> [Event (Pattern a)]
fit (b,e) evs = catMaybes $ map (match (b,e)) evs
-- slice of event within the given range
match :: (Rational, Rational) -> Event (Pattern a) -> Maybe (Event (Pattern a))
match (b,e) ev = do a <- subArc (Arc b e) $ part ev
return ev {part = a}
-}

0 comments on commit 2a26000

Please sign in to comment.