Skip to content

Commit

Permalink
Merge pull request #818 from mindofmatthew/signal-tests
Browse files Browse the repository at this point in the history
Test and bugfix basic patterns (sine, etc)
  • Loading branch information
yaxu committed Apr 13, 2021
2 parents c1e5d82 + dcd60b3 commit 669fde1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Sound/Tidal/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cosine = 0.25 `rotR` sine
-- following a cosine with frequency of one cycle, and amplitude from
-- -1 to 1. Equivalent to `0.25 ~> sine2`.
cosine2 :: Fractional a => Pattern a
cosine2 = 0.25 `rotR` sine
cosine2 = 0.25 `rotR` sine2

-- | @saw@ - unipolar ascending sawtooth wave. A pattern of continuous values
-- following a sawtooth with frequency of one cycle, and amplitude from
Expand All @@ -82,7 +82,7 @@ isaw = (1-) <$> saw

-- | @isaw2@ like @saw2@, but a descending (inverse) sawtooth.
isaw2 :: (Fractional a, Real a) => Pattern a
isaw2 = (1-) <$> saw
isaw2 = (*(-1)) <$> saw2

-- | @tri@ - unipolar triangle wave. A pattern of continuous values
-- following a triangle wave with frequency of one cycle, and amplitude from
Expand All @@ -94,7 +94,7 @@ tri = fastAppend saw isaw
-- following a triangle wave with frequency of one cycle, and amplitude from
-- -1 to 1.
tri2 :: (Fractional a, Real a) => Pattern a
tri2 = fastAppend saw isaw
tri2 = fastAppend saw2 isaw2

-- | @square@ - unipolar square wave. A pattern of continuous values
-- following a square wave with frequency of one cycle, and amplitude from
Expand Down
26 changes: 26 additions & 0 deletions test/Sound/Tidal/CoreTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Sound.Tidal.CoreTest where

import Data.List (sort)
import Data.Ratio
import qualified Data.Map as Map
import Sound.Tidal.Context
import Test.Microspec
import TestUtils
Expand All @@ -12,6 +13,29 @@ import Prelude hiding ((*>), (<*))
run :: Microspec ()
run =
describe "Sound.Tidal.Core" $ do
describe "Elemental patterns" $ do
let sampleOf :: Pattern Double -> Rational -> Double
sampleOf pat t = (value . head) $ query pat (State (Arc t t) Map.empty)
describe "are in range [0, 1]" $ do
let inNormalRange pat t = (y >= 0) && (y <= 1)
where y = sampleOf pat t
it "sine" $ inNormalRange sine
it "cosine" $ inNormalRange cosine
it "saw" $ inNormalRange saw
it "isaw" $ inNormalRange isaw
it "tri" $ inNormalRange tri
it "square" $ inNormalRange square
describe "have correctly-scaled bipolar variants" $ do
let areCorrectlyScaled pat pat2 t = (y * 2 - 1) ~== y2
where y = sampleOf pat t
y2 = sampleOf pat2 t
it "sine" $ areCorrectlyScaled sine sine2
it "cosine" $ areCorrectlyScaled cosine cosine2
it "saw" $ areCorrectlyScaled saw saw2
it "isaw" $ areCorrectlyScaled isaw isaw2
it "tri" $ areCorrectlyScaled tri tri2
it "square" $ areCorrectlyScaled square square2

describe "append" $
it "can switch between the cycles from two pures" $ do
queryArc (append (pure "a") (pure "b")) (Arc 0 5)
Expand All @@ -23,6 +47,7 @@ run =
(((3, 4), (3, 4)), "b"),
(((4, 5), (4, 5)), "a")
]

describe "cat" $ do
it "can switch between the cycles from three pures" $ do
queryArc (cat [pure "a", pure "b", pure "c"]) (Arc 0 5)
Expand All @@ -42,6 +67,7 @@ run =
(Arc 0 10)
(rev $ cat [a, b, c])
(cat [rev a, rev b, rev c])

describe "fastCat" $ do
it "can switch between the cycles from three pures inside one cycle" $ do
it "1" $
Expand Down
3 changes: 3 additions & 0 deletions test/TestUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import qualified Data.Map.Strict as Map
class TolerantEq a where
(~==) :: a -> a -> Bool

instance TolerantEq Double where
a ~== b = abs (a - b) < 0.000001

instance TolerantEq Value where
(VS a) ~== (VS b) = a == b
(VI a) ~== (VI b) = a == b
Expand Down

0 comments on commit 669fde1

Please sign in to comment.