Skip to content

Commit

Permalink
start rand at 0.5, and fix tests accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
yaxu committed Feb 22, 2025
1 parent ab21570 commit db1ecbc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion 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
8 changes: 4 additions & 4 deletions tidal-core/test/Sound/Tidal/UITest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,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 @@ -116,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) (0 :: 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.26202741871417984 :: 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 db1ecbc

Please sign in to comment.