Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Waldmann committed Jan 28, 2024
1 parent 36d94a8 commit 867496f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Sound/Tidal/UI.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, OverloadedStrings, BangPatterns #-}

{-
UI.hs - Tidal's main 'user interface' functions, for transforming
Expand Down Expand Up @@ -45,6 +45,10 @@ import Data.Maybe (isJust, fromJust, fromMaybe, mapMaybe)
import qualified Data.Text as T
import qualified Data.Map.Strict as Map
import Data.Bool (bool)
import qualified Data.List as L
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U


import Sound.Tidal.Bjorklund (bjorklund)
import Sound.Tidal.Core
Expand Down Expand Up @@ -1226,12 +1230,28 @@ runMarkov 8 [[2,3], [1,3]] 0 0
will produce a two-state chain 8 steps long, from initial state @0@, where the
transition probability from state 0->0 is 2/5, 0->1 is 3/5, 1->0 is 1/4, and
1->1 is 3/4. -}

runMarkov :: Int -> [[Double]] -> Int -> Time -> [Int]
runMarkov n tp xi seed = reverse $ (iterate (markovStep $ renorm) [xi])!! (n-1) where
markovStep tp' xs = (fromJust $ findIndex (r <=) $ scanl1 (+) (tp'!!(head xs))) : xs where
r = timeToRand $ seed + (fromIntegral . length) xs / fromIntegral n
renorm = [ map (/ sum x) x | x <- tp ]

runMarkov' :: Int -> [[Double]] -> Int -> Time -> [Int]
runMarkov' n tp xi seed = take n $ map fst $ L.iterate' (markovStep $ renorm) (xi, seed + delta) where
markovStep tp' (x,seed) = (let (s,v) = tp' V.! x in binarySearch 0 (r * s) v , seed + delta) where

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.4.4

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.8.1.0 - ghc 9.4.1

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal latest - ghc latest

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.10.1

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.8.3

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 9.0.1

This binding for ‘seed’ shadows the existing binding

Check warning on line 1242 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.6.5

This binding for ‘seed’ shadows the existing binding
r = timeToRand seed
renorm :: V.Vector (Double, U.Vector Double)
renorm = V.fromList [ fmap U.fromList $ L.mapAccumL (\ a y -> let s = a+y in s `seq` (s,s)) 0 x | x <- tp ]
binarySearch :: Int -> Double -> U.Vector Double -> Int
binarySearch !off x v =

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.4.4

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.8.1.0 - ghc 9.4.1

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal latest - ghc latest

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.10.1

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.8.3

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 9.0.1

This binding for ‘off’ shadows the existing binding

Check warning on line 1247 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.6.5

This binding for ‘off’ shadows the existing binding
if U.length v == 0 then off
else if U.length v == 1 then off + 1
else let i = div (U.length v) 2
in if x < v U.! i then binarySearch off x $ U.slice 0 i v
else binarySearch (off + i) x (U.slice i (U.length v - i) v)
delta = 1 / fromIntegral n

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.4.4

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / build (9.0.1, 3.4.0.0)

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.8.1.0 - ghc 9.4.1

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal latest - ghc latest

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.10.1

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.8.3

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 9.0.1

This binding for ‘delta’ shadows the existing binding

Check warning on line 1253 in src/Sound/Tidal/UI.hs

View workflow job for this annotation

GitHub Actions / cabal 3.4.0.0 - ghc 8.6.5

This binding for ‘delta’ shadows the existing binding

{- @markovPat n xi tp@ generates a one-cycle pattern of @n@ steps in a Markov
chain starting from state @xi@ with transition matrix @tp@. Each row of the
transition matrix is automatically normalized. For example:
Expand Down
1 change: 1 addition & 0 deletions tidal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ library
, random < 1.3
, exceptions < 0.11
, mtl >= 2.2
, vector
, tidal-link == 1.0.2

test-suite tests
Expand Down

0 comments on commit 867496f

Please sign in to comment.