Skip to content

Commit

Permalink
Merge pull request #1176 from tidalcycles/rand-sample-onset
Browse files Browse the repository at this point in the history
Sample rand signal on onset, start rand pattern at 0.5
  • Loading branch information
yaxu authored Feb 22, 2025
2 parents c0c0200 + db1ecbc commit 84d4a9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
10 changes: 6 additions & 4 deletions tidal-core/src/Sound/Tidal/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ intSeedToRand :: (Fractional a) => Int -> a
intSeedToRand = (/ 536870912) . realToFrac . (`mod` 536870912)

timeToRand :: (RealFrac a, Fractional b) => a -> b
timeToRand = intSeedToRand . timeToIntSeed
-- Otherwise the value would be 0 at time 0.. Let's start in the middle.
timeToRand 0 = 0.5
timeToRand t = intSeedToRand $ timeToIntSeed t

timeToRands :: (RealFrac a, Fractional b) => a -> Int -> [b]
timeToRands 0 n = timeToRands' (timeToIntSeed (9999999 :: Double)) n
Expand Down Expand Up @@ -137,7 +139,7 @@ timeToRands' seed n
--
-- > jux (# ((1024 <~) $ gain rand)) $ sound "sn sn ~ sn" # gain rand
rand :: (Fractional a) => Pattern a
rand = pattern (\(State a@(Arc s e) _) -> [Event (Context []) Nothing a (realToFrac (timeToRand ((e + s) / 2) :: Double))])
rand = pattern (\(State a@(Arc s _) _) -> [Event (Context []) Nothing a (realToFrac (timeToRand s :: Double))])

-- | Boolean rand - a continuous stream of true\/false values, with a 50\/50 chance.
brand :: Pattern Bool
Expand Down Expand Up @@ -363,7 +365,7 @@ _degradeBy = _degradeByUsing rand

-- Useful for manipulating random stream, e.g. to change 'seed'
_degradeByUsing :: Pattern Double -> Double -> Pattern a -> Pattern a
_degradeByUsing prand x p = fmap fst $ filterValues ((> x) . snd) $ (,) <$> p <* prand
_degradeByUsing prand x p = fmap fst $ filterValues ((>= x) . snd) $ (,) <$> p <* prand

-- |
-- As 'degradeBy', but the pattern of probabilities represents the chances to retain rather
Expand All @@ -372,7 +374,7 @@ unDegradeBy :: Pattern Double -> Pattern a -> Pattern a
unDegradeBy = patternify' _unDegradeBy

_unDegradeBy :: Double -> Pattern a -> Pattern a
_unDegradeBy x p = fmap fst $ filterValues ((<= x) . snd) $ (,) <$> p <* rand
_unDegradeBy x p = fmap fst $ filterValues ((< x) . snd) $ (,) <$> p <* rand

degradeOverBy :: Int -> Pattern Double -> Pattern a -> Pattern a
degradeOverBy i tx p = unwrap $ (\x -> fmap fst $ filterValues ((> x) . snd) $ (,) <$> p <* fastRepeatCycles i rand) <$> slow (fromIntegral i) tx
Expand Down
21 changes: 11 additions & 10 deletions tidal-core/test/Sound/Tidal/UITest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ module Sound.Tidal.UITest where

import qualified Data.Map.Strict as Map
-- import Sound.Tidal.Pattern
import Sound.Tidal.Control
import Sound.Tidal.Control ( _chop )
import Sound.Tidal.Core
import Sound.Tidal.Params
import Sound.Tidal.ParseBP
( sig, sine, saw, (|+), (>|), (#), run, cat, fastcat, (<~) )
import Sound.Tidal.Params ( sound, begin, crush, end, n, speed, s )
import Sound.Tidal.ParseBP ( parseBP_E )
import Sound.Tidal.Pattern
import Sound.Tidal.UI
import Test.Hspec
import TestUtils
import Test.Hspec ( describe, it, shouldBe, Spec )
import TestUtils ( compareP, comparePD, compareTol, ps )
import Prelude hiding ((*>), (<*))

run :: Spec
Expand All @@ -27,7 +28,7 @@ run =
compareP
(Arc 0 1)
(slow 2 $ _chop 2 $ s (pure "a"))
(begin (pure 0) # end (pure 0.5) # (s (pure "a")))
(begin (pure 0) # end (pure 0.5) # s (pure "a"))
it "can chop a chop" $
compareTol (Arc 0 1) (_chop 6 $ s $ pure "a") (_chop 2 $ _chop 3 $ s $ pure "a")

Expand Down Expand Up @@ -104,7 +105,7 @@ run =

describe "rand" $ do
it "it generates a (pseudo-)random number between 0 and 1 at the start of a cycle" $
(queryArc rand (Arc 0 0)) `shouldBe` [Event (Context []) Nothing (Arc 0 0) (0 :: Float)]
(queryArc rand (Arc 0 0)) `shouldBe` [Event (Context []) Nothing (Arc 0 0) (0.5 :: Float)]
it "it generates a (pseudo-)random number between 0 and 1 at 1/4 of a cycle" $
(queryArc rand (Arc 0.25 0.25))
`shouldBe` [Event (Context []) Nothing (Arc 0.25 0.25) (0.6295689214020967 :: Float)]
Expand All @@ -115,19 +116,19 @@ run =
describe "irand" $ do
-- it "generates a (pseudo-random) integer between zero & i" $ do
it "at the start of a cycle" $
(queryArc (irand 10) (Arc 0 0)) `shouldBe` [Event (Context []) Nothing (Arc 0 0) (0 :: Int)]
(queryArc (irand 10) (Arc 0 0)) `shouldBe` [Event (Context []) Nothing (Arc 0 0) (5 :: Int)]
it "at 1/4 of a cycle" $
(queryArc (irand 10) (Arc 0.25 0.25)) `shouldBe` [Event (Context []) Nothing (Arc 0.25 0.25) (6 :: Int)]
it "is patternable" $
(queryArc (irand "10 2") (Arc 0 1))
`shouldBe` [ Event (Context [((1, 1), (3, 1))]) Nothing (Arc 0 0.5) (6 :: Int),
`shouldBe` [ Event (Context [((1, 1), (3, 1))]) Nothing (Arc 0 0.5) (5 :: Int),
Event (Context [((4, 1), (5, 1))]) Nothing (Arc 0.5 1) (0 :: Int)
]

describe "normal" $ do
it "produces values within [0,1] in a bell curve at different parts of a cycle" $ do
queryArc normal (Arc 0 0.1)
`shouldBe` [Event (Context []) Nothing (Arc 0 0.1) (0.4614205864457064 :: Double)]
`shouldBe` [Event (Context []) Nothing (Arc 0 0.1) (0.42461738824387246 :: Double)]
queryArc normal (Arc 0.25 0.25)
`shouldBe` [Event (Context []) Nothing (Arc 0.25 0.25) (0.5 :: Double)]
queryArc normal (Arc 0.75 0.75)
Expand Down

0 comments on commit 84d4a9f

Please sign in to comment.