{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, BangPatterns #-}

{-
    Core.hs - For functions judged to be 'core' to tidal functionality.
    Copyright (C) 2020, Alex McLean and contributors

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
-}

module Sound.Tidal.Core where

import           Prelude hiding ((<*), (*>))

import           Data.Fixed (mod')
import qualified Data.Map.Strict as Map
import           Data.Maybe (fromMaybe)
import           Sound.Tidal.Pattern

-- ** Elemental patterns

-- | An empty pattern
silence :: Pattern a
silence :: Pattern a
silence = Pattern a
forall a. Pattern a
empty

-- | Takes a function from time to values, and turns it into a 'Pattern'.
sig :: (Time -> a) -> Pattern a
sig :: (Time -> a) -> Pattern a
sig Time -> a
f = Query a -> Pattern a
forall a. Query a -> Pattern a
Pattern Query a
q
  where q :: Query a
q (State (Arc Time
s Time
e) StateMap
_)
          | Time
s Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
> Time
e = []
          | Bool
otherwise = [Context
-> Maybe (ArcF Time) -> ArcF Time -> a -> EventF (ArcF Time) a
forall a b. Context -> Maybe a -> a -> b -> EventF a b
Event ([((Int, Int), (Int, Int))] -> Context
Context []) Maybe (ArcF Time)
forall a. Maybe a
Nothing (Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc Time
s Time
e) (Time -> a
f (Time
sTime -> Time -> Time
forall a. Num a => a -> a -> a
+((Time
eTime -> Time -> Time
forall a. Num a => a -> a -> a
-Time
s)Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/Time
2)))]

-- | @sine@ - unipolar sinewave. A pattern of continuous values following a
-- sinewave with frequency of one cycle, and amplitude from 0 to 1.
sine :: Fractional a => Pattern a
sine :: Pattern a
sine = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ \Time
t -> (Double -> a
sin_rat ((Double
forall a. Floating a => a
pi :: Double) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Time -> Double
forall a. Fractional a => Time -> a
fromRational Time
t) a -> a -> a
forall a. Num a => a -> a -> a
+ a
1) a -> a -> a
forall a. Fractional a => a -> a -> a
/ a
2
  where sin_rat :: Double -> a
sin_rat = Time -> a
forall a. Fractional a => Time -> a
fromRational (Time -> a) -> (Double -> Time) -> Double -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Time
forall a. Real a => a -> Time
toRational (Double -> Time) -> (Double -> Double) -> Double -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double
forall a. Floating a => a -> a
sin

-- | @sine2@ - bipolar sinewave. A pattern of continuous values following a
-- sinewave with frequency of one cycle, and amplitude from -1 to 1.
sine2 :: Fractional a => Pattern a
sine2 :: Pattern a
sine2 = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ \Time
t -> Double -> a
sin_rat ((Double
forall a. Floating a => a
pi :: Double) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Time -> Double
forall a. Fractional a => Time -> a
fromRational Time
t)
  where sin_rat :: Double -> a
sin_rat = Time -> a
forall a. Fractional a => Time -> a
fromRational (Time -> a) -> (Double -> Time) -> Double -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Time
forall a. Real a => a -> Time
toRational (Double -> Time) -> (Double -> Double) -> Double -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double
forall a. Floating a => a -> a
sin

-- | @cosine@ - unipolar cosine wave. A pattern of continuous values
-- following a cosine with frequency of one cycle, and amplitude from
-- 0 to 1. Equivalent to `0.25 ~> sine`.
cosine :: Fractional a => Pattern a
cosine :: Pattern a
cosine = Time
0.25 Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
`rotR` Pattern a
forall a. Fractional a => Pattern a
sine

-- | @cosine2@ - bipolar cosine wave. A pattern of continuous values
-- 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 :: Pattern a
cosine2 = Time
0.25 Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
`rotR` Pattern a
forall a. Fractional a => Pattern a
sine

-- | @saw@ - unipolar ascending sawtooth wave. A pattern of continuous values
-- following a sawtooth with frequency of one cycle, and amplitude from
-- 0 to 1.
saw :: (Fractional a, Real a) => Pattern a
saw :: Pattern a
saw = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ \Time
t -> a -> a -> a
forall a. Real a => a -> a -> a
mod' (Time -> a
forall a. Fractional a => Time -> a
fromRational Time
t) a
1

-- | @saw2@ - bipolar ascending sawtooth wave. A pattern of continuous values
-- following a sawtooth with frequency of one cycle, and amplitude from
-- -1 to 1.
saw2 :: (Fractional a, Real a) => Pattern a
saw2 :: Pattern a
saw2 = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ \Time
t -> (a -> a -> a
forall a. Real a => a -> a -> a
mod' (Time -> a
forall a. Fractional a => Time -> a
fromRational Time
t) a
1) a -> a -> a
forall a. Num a => a -> a -> a
* a
2 a -> a -> a
forall a. Num a => a -> a -> a
- a
1

-- | @isaw@ like @saw@, but a descending (inverse) sawtooth.
isaw :: (Fractional a, Real a) => Pattern a
isaw :: Pattern a
isaw = (a
1a -> a -> a
forall a. Num a => a -> a -> a
-) (a -> a) -> Pattern a -> Pattern a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
forall a. (Fractional a, Real a) => Pattern a
saw

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

-- | @tri@ - unipolar triangle wave. A pattern of continuous values
-- following a triangle wave with frequency of one cycle, and amplitude from
-- 0 to 1.
tri :: (Fractional a, Real a) => Pattern a
tri :: Pattern a
tri = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
fastAppend Pattern a
forall a. (Fractional a, Real a) => Pattern a
saw Pattern a
forall a. (Fractional a, Real a) => Pattern a
isaw

-- | @tri2@ - bipolar triangle wave. A pattern of continuous values
-- following a triangle wave with frequency of one cycle, and amplitude from
-- -1 to 1.
tri2 :: (Fractional a, Real a) => Pattern a
tri2 :: Pattern a
tri2 = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
fastAppend Pattern a
forall a. (Fractional a, Real a) => Pattern a
saw Pattern a
forall a. (Fractional a, Real a) => Pattern a
isaw

-- | @square@ - unipolar square wave. A pattern of continuous values
-- following a square wave with frequency of one cycle, and amplitude from
-- 0 to 1.
-- | @square@ is like 'sine', for square waves.
square :: (Fractional a) => Pattern a
square :: Pattern a
square = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$
       \Time
t -> Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Double -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Integer) -> Double -> Integer
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Real a => a -> a -> a
mod' (Time -> Double
forall a. Fractional a => Time -> a
fromRational Time
t :: Double) Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2) :: Integer)

-- | @square2@ - bipolar square wave. A pattern of continuous values
-- following a square wave with frequency of one cycle, and amplitude from
-- -1 to 1.
square2 :: (Fractional a) => Pattern a
square2 :: Pattern a
square2 = (Time -> a) -> Pattern a
forall a. (Time -> a) -> Pattern a
sig ((Time -> a) -> Pattern a) -> (Time -> a) -> Pattern a
forall a b. (a -> b) -> a -> b
$
       \Time
t -> Integer -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Double -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Integer) -> Double -> Integer
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Real a => a -> a -> a
mod' (Time -> Double
forall a. Fractional a => Time -> a
fromRational Time
t :: Double) Double
1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
2) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1 :: Integer)

-- | @envL@ is a 'Pattern' of continuous 'Double' values, representing
-- a linear interpolation between 0 and 1 during the first cycle, then
-- staying constant at 1 for all following cycles. Possibly only
-- useful if you're using something like the retrig function defined
-- in tidal.el.
envL :: Pattern Double
envL :: Pattern Double
envL = (Time -> Double) -> Pattern Double
forall a. (Time -> a) -> Pattern a
sig ((Time -> Double) -> Pattern Double)
-> (Time -> Double) -> Pattern Double
forall a b. (a -> b) -> a -> b
$ \Time
t -> Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
0 (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Double
forall a. Ord a => a -> a -> a
min (Time -> Double
forall a. Fractional a => Time -> a
fromRational Time
t) Double
1

-- | like 'envL' but reversed.
envLR :: Pattern Double
envLR :: Pattern Double
envLR = (Double
1Double -> Double -> Double
forall a. Num a => a -> a -> a
-) (Double -> Double) -> Pattern Double -> Pattern Double
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern Double
envL

-- | 'Equal power' version of 'env', for gain-based transitions
envEq :: Pattern Double
envEq :: Pattern Double
envEq = (Time -> Double) -> Pattern Double
forall a. (Time -> a) -> Pattern a
sig ((Time -> Double) -> Pattern Double)
-> (Time -> Double) -> Pattern Double
forall a b. (a -> b) -> a -> b
$ \Time
t -> Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double
forall a. Floating a => a -> a
sin (Double
forall a. Floating a => a
piDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
0 (Double -> Double -> Double
forall a. Ord a => a -> a -> a
min (Time -> Double
forall a. Fractional a => Time -> a
fromRational (Time
1Time -> Time -> Time
forall a. Num a => a -> a -> a
-Time
t)) Double
1)))

-- | Equal power reversed
envEqR :: Pattern Double
envEqR :: Pattern Double
envEqR = (Time -> Double) -> Pattern Double
forall a. (Time -> a) -> Pattern a
sig ((Time -> Double) -> Pattern Double)
-> (Time -> Double) -> Pattern Double
forall a b. (a -> b) -> a -> b
$ \Time
t -> Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double
forall a. Floating a => a -> a
cos (Double
forall a. Floating a => a
piDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double -> Double
forall a. Ord a => a -> a -> a
max Double
0 (Double -> Double -> Double
forall a. Ord a => a -> a -> a
min (Time -> Double
forall a. Fractional a => Time -> a
fromRational (Time
1Time -> Time -> Time
forall a. Num a => a -> a -> a
-Time
t)) Double
1)))

-- ** Pattern algebra

-- class for types that support a left-biased union
class Unionable a where
  union :: a -> a -> a

-- default union is just to take the left hand side..
instance Unionable a where
  union :: a -> a -> a
union = a -> a -> a
forall a b. a -> b -> a
const

instance {-# OVERLAPPING #-} Unionable ControlMap where
  union :: ControlMap -> ControlMap -> ControlMap
union = ControlMap -> ControlMap -> ControlMap
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union

(|+|) :: (Applicative a, Num b) => a b -> a b -> a b
a b
a |+| :: a b -> a b -> a b
|+| a b
b = b -> b -> b
forall a. Num a => a -> a -> a
(+) (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|+ ) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |+ :: Pattern a -> Pattern a -> Pattern a
|+  Pattern a
b = a -> a -> a
forall a. Num a => a -> a -> a
(+) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( +|) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  +| :: Pattern a -> Pattern a -> Pattern a
+| Pattern a
b = a -> a -> a
forall a. Num a => a -> a -> a
(+) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|++|) :: Applicative a => a String -> a String -> a String
a String
a |++| :: a String -> a String -> a String
|++| a String
b = String -> String -> String
forall a. [a] -> [a] -> [a]
(++) (String -> String -> String) -> a String -> a (String -> String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a String
a a (String -> String) -> a String -> a String
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a String
b
(|++ ) :: Pattern String -> Pattern String -> Pattern String
Pattern String
a |++ :: Pattern String -> Pattern String -> Pattern String
|++  Pattern String
b = String -> String -> String
forall a. [a] -> [a] -> [a]
(++) (String -> String -> String)
-> Pattern String -> Pattern (String -> String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern String
a Pattern (String -> String) -> Pattern String -> Pattern String
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern String
b
( ++|) :: Pattern String -> Pattern String -> Pattern String
Pattern String
a  ++| :: Pattern String -> Pattern String -> Pattern String
++| Pattern String
b = String -> String -> String
forall a. [a] -> [a] -> [a]
(++) (String -> String -> String)
-> Pattern String -> Pattern (String -> String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern String
a Pattern (String -> String) -> Pattern String -> Pattern String
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern String
b

(|/|) :: (Applicative a, Fractional b) => a b -> a b -> a b
a b
a |/| :: a b -> a b -> a b
|/| a b
b = b -> b -> b
forall a. Fractional a => a -> a -> a
(/) (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|/ ) :: Fractional a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |/ :: Pattern a -> Pattern a -> Pattern a
|/  Pattern a
b = a -> a -> a
forall a. Fractional a => a -> a -> a
(/) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( /|) :: Fractional a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  /| :: Pattern a -> Pattern a -> Pattern a
/| Pattern a
b = a -> a -> a
forall a. Fractional a => a -> a -> a
(/) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|*|) :: (Applicative a, Num b) => a b -> a b -> a b
a b
a |*| :: a b -> a b -> a b
|*| a b
b = b -> b -> b
forall a. Num a => a -> a -> a
(*) (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|* ) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |* :: Pattern a -> Pattern a -> Pattern a
|*  Pattern a
b = a -> a -> a
forall a. Num a => a -> a -> a
(*) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( *|) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  *| :: Pattern a -> Pattern a -> Pattern a
*| Pattern a
b = a -> a -> a
forall a. Num a => a -> a -> a
(*) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|-|) :: (Applicative a, Num b) => a b -> a b -> a b
a b
a |-| :: a b -> a b -> a b
|-| a b
b = (-) (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|- ) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |- :: Pattern a -> Pattern a -> Pattern a
|-  Pattern a
b = (-) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( -|) :: Num a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  -| :: Pattern a -> Pattern a -> Pattern a
-| Pattern a
b = (-) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|%|) :: (Applicative a, Real b) => a b -> a b -> a b
a b
a |%| :: a b -> a b -> a b
|%| a b
b = b -> b -> b
forall a. Real a => a -> a -> a
mod' (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|% ) :: Real a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |% :: Pattern a -> Pattern a -> Pattern a
|%  Pattern a
b = a -> a -> a
forall a. Real a => a -> a -> a
mod' (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( %|) :: Real a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  %| :: Pattern a -> Pattern a -> Pattern a
%| Pattern a
b = a -> a -> a
forall a. Real a => a -> a -> a
mod' (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|**|) :: (Applicative a, Floating b) => a b -> a b -> a b
a b
a |**| :: a b -> a b -> a b
|**| a b
b = b -> b -> b
forall a. Floating a => a -> a -> a
(**) (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|** ) :: Floating a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |** :: Pattern a -> Pattern a -> Pattern a
|**  Pattern a
b = a -> a -> a
forall a. Floating a => a -> a -> a
(**) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( **|) :: Floating a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  **| :: Pattern a -> Pattern a -> Pattern a
**| Pattern a
b = a -> a -> a
forall a. Floating a => a -> a -> a
(**) (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|>|) :: (Applicative a, Unionable b) => a b -> a b -> a b
a b
a |>| :: a b -> a b -> a b
|>| a b
b = (b -> b -> b) -> b -> b -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> b -> b
forall a. Unionable a => a -> a -> a
union (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|> ) :: Unionable a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |> :: Pattern a -> Pattern a -> Pattern a
|>  Pattern a
b = (a -> a -> a) -> a -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> a -> a
forall a. Unionable a => a -> a -> a
union (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( >|) :: Unionable a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  >| :: Pattern a -> Pattern a -> Pattern a
>| Pattern a
b = (a -> a -> a) -> a -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> a -> a
forall a. Unionable a => a -> a -> a
union (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

(|<|) :: (Applicative a, Unionable b) => a b -> a b -> a b
a b
a |<| :: a b -> a b -> a b
|<| a b
b = b -> b -> b
forall a. Unionable a => a -> a -> a
union (b -> b -> b) -> a b -> a (b -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a b
a a (b -> b) -> a b -> a b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a b
b
(|< ) :: Unionable a => Pattern a -> Pattern a -> Pattern a
Pattern a
a |< :: Pattern a -> Pattern a -> Pattern a
|<  Pattern a
b = a -> a -> a
forall a. Unionable a => a -> a -> a
union (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
<* Pattern a
b
( <|) :: Unionable a => Pattern a -> Pattern a -> Pattern a
Pattern a
a  <| :: Pattern a -> Pattern a -> Pattern a
<| Pattern a
b = a -> a -> a
forall a. Unionable a => a -> a -> a
union (a -> a -> a) -> Pattern a -> Pattern (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern a
a Pattern (a -> a) -> Pattern a -> Pattern a
forall a b. Pattern (a -> b) -> Pattern a -> Pattern b
*> Pattern a
b

-- Backward compatibility - structure from left, values from right.
(#) :: Unionable b => Pattern b -> Pattern b -> Pattern b
# :: Pattern b -> Pattern b -> Pattern b
(#) = Pattern b -> Pattern b -> Pattern b
forall a. Unionable a => Pattern a -> Pattern a -> Pattern a
(|>)



-- ** Constructing patterns

-- | Turns a list of values into a pattern, playing one of them per cycle.
fromList :: [a] -> Pattern a
fromList :: [a] -> Pattern a
fromList = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat ([Pattern a] -> Pattern a)
-> ([a] -> [Pattern a]) -> [a] -> Pattern a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Pattern a) -> [a] -> [Pattern a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Pattern a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Turns a list of values into a pattern, playing all of them per cycle.
fastFromList :: [a] -> Pattern a
fastFromList :: [a] -> Pattern a
fastFromList = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
fastcat ([Pattern a] -> Pattern a)
-> ([a] -> [Pattern a]) -> [a] -> Pattern a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Pattern a) -> [a] -> [Pattern a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Pattern a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | A synonym for 'fastFromList'
listToPat :: [a] -> Pattern a
listToPat :: [a] -> Pattern a
listToPat = [a] -> Pattern a
forall a. [a] -> Pattern a
fastFromList

-- | 'fromMaybes; is similar to 'fromList', but allows values to
-- be optional using the 'Maybe' type, so that 'Nothing' results in
-- gaps in the pattern.
fromMaybes :: [Maybe a] -> Pattern a
fromMaybes :: [Maybe a] -> Pattern a
fromMaybes = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
fastcat ([Pattern a] -> Pattern a)
-> ([Maybe a] -> [Pattern a]) -> [Maybe a] -> Pattern a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe a -> Pattern a) -> [Maybe a] -> [Pattern a]
forall a b. (a -> b) -> [a] -> [b]
map Maybe a -> Pattern a
forall a. Maybe a -> Pattern a
f
  where f :: Maybe a -> Pattern a
f Maybe a
Nothing = Pattern a
forall a. Pattern a
silence
        f (Just a
x) = a -> Pattern a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

-- | A pattern of whole numbers from 0 to the given number, in a single cycle.
run :: (Enum a, Num a) => Pattern a -> Pattern a
run :: Pattern a -> Pattern a
run = (Pattern a -> (a -> Pattern a) -> Pattern a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Pattern a
forall a. (Enum a, Num a) => a -> Pattern a
_run)

_run :: (Enum a, Num a) => a -> Pattern a
_run :: a -> Pattern a
_run a
n = [a] -> Pattern a
forall a. [a] -> Pattern a
fastFromList [a
0 .. a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1]

-- | From @1@ for the first cycle, successively adds a number until it gets up to @n@
scan :: (Enum a, Num a) => Pattern a -> Pattern a
scan :: Pattern a -> Pattern a
scan = (Pattern a -> (a -> Pattern a) -> Pattern a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Pattern a
forall a. (Enum a, Num a) => a -> Pattern a
_scan)

_scan :: (Enum a, Num a) => a -> Pattern a
_scan :: a -> Pattern a
_scan a
n = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
slowcat ([Pattern a] -> Pattern a) -> [Pattern a] -> Pattern a
forall a b. (a -> b) -> a -> b
$ (a -> Pattern a) -> [a] -> [Pattern a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Pattern a
forall a. (Enum a, Num a) => a -> Pattern a
_run [a
1 .. a
n]

-- ** Combining patterns

-- | Alternate between cycles of the two given patterns
append :: Pattern a -> Pattern a -> Pattern a
append :: Pattern a -> Pattern a -> Pattern a
append Pattern a
a Pattern a
b = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat [Pattern a
a,Pattern a
b]

-- | Like 'append', but for a list of patterns. Interlaces them, playing the first cycle from each
-- in turn, then the second cycle from each, and so on.
cat :: [Pattern a] -> Pattern a
cat :: [Pattern a] -> Pattern a
cat [] = Pattern a
forall a. Pattern a
silence
cat [Pattern a]
ps = Query a -> Pattern a
forall a. Query a -> Pattern a
Pattern (Query a -> Pattern a) -> Query a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Query a
q
  where n :: Int
n = [Pattern a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Pattern a]
ps
        q :: Query a
q State
st = (ArcF Time -> [Event a]) -> [ArcF Time] -> [Event a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (State -> ArcF Time -> [Event a]
f State
st) ([ArcF Time] -> [Event a]) -> [ArcF Time] -> [Event a]
forall a b. (a -> b) -> a -> b
$ ArcF Time -> [ArcF Time]
arcCyclesZW (State -> ArcF Time
arc State
st)
        f :: State -> ArcF Time -> [Event a]
f State
st ArcF Time
a = Pattern a -> Query a
forall a. Pattern a -> Query a
query ((Time -> Time) -> Pattern a -> Pattern a
forall a. (Time -> Time) -> Pattern a -> Pattern a
withResultTime (Time -> Time -> Time
forall a. Num a => a -> a -> a
+Time
offset) Pattern a
p) Query a -> Query a
forall a b. (a -> b) -> a -> b
$ State
st {arc :: ArcF Time
arc = Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc (Time -> Time -> Time
forall a. Num a => a -> a -> a
subtract Time
offset (ArcF Time -> Time
forall a. ArcF a -> a
start ArcF Time
a)) (Time -> Time -> Time
forall a. Num a => a -> a -> a
subtract Time
offset (ArcF Time -> Time
forall a. ArcF a -> a
stop ArcF Time
a))}
          where p :: Pattern a
p = [Pattern a]
ps [Pattern a] -> Int -> Pattern a
forall a. [a] -> Int -> a
!! Int
i
                cyc :: Int
cyc = (Time -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Time -> Int) -> Time -> Int
forall a b. (a -> b) -> a -> b
$ ArcF Time -> Time
forall a. ArcF a -> a
start ArcF Time
a) :: Int
                i :: Int
i = Int
cyc Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
n
                offset :: Time
offset = (Int -> Time
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Time) -> Int -> Time
forall a b. (a -> b) -> a -> b
$ Int
cyc Int -> Int -> Int
forall a. Num a => a -> a -> a
- ((Int
cyc Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
n)) :: Time

-- | Alias for 'cat'
slowCat :: [Pattern a] -> Pattern a
slowCat :: [Pattern a] -> Pattern a
slowCat = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat
slowcat :: [Pattern a] -> Pattern a
slowcat :: [Pattern a] -> Pattern a
slowcat = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
slowCat

-- | Alias for 'append'
slowAppend :: Pattern a -> Pattern a -> Pattern a
slowAppend :: Pattern a -> Pattern a -> Pattern a
slowAppend = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
append
slowappend :: Pattern a -> Pattern a -> Pattern a
slowappend :: Pattern a -> Pattern a -> Pattern a
slowappend = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
append

-- | Like 'append', but twice as fast
fastAppend :: Pattern a -> Pattern a -> Pattern a
fastAppend :: Pattern a -> Pattern a -> Pattern a
fastAppend Pattern a
a Pattern a
b = Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast Time
2 (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
append Pattern a
a Pattern a
b
fastappend :: Pattern a -> Pattern a -> Pattern a
fastappend :: Pattern a -> Pattern a -> Pattern a
fastappend = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
fastAppend

-- | The same as 'cat', but speeds up the result by the number of
-- patterns there are, so the cycles from each are squashed to fit a
-- single cycle.
fastCat :: [Pattern a] -> Pattern a
fastCat :: [Pattern a] -> Pattern a
fastCat [Pattern a]
ps = Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast (Int -> Time
forall a. Real a => a -> Time
toTime (Int -> Time) -> Int -> Time
forall a b. (a -> b) -> a -> b
$ [Pattern a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Pattern a]
ps) (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat [Pattern a]
ps

fastcat :: [Pattern a] -> Pattern a
fastcat :: [Pattern a] -> Pattern a
fastcat = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
fastCat

-- | Similar to @fastCat@, but each pattern is given a relative duration
timeCat :: [(Time, Pattern a)] -> Pattern a
timeCat :: [(Time, Pattern a)] -> Pattern a
timeCat [(Time, Pattern a)]
tps = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
stack ([Pattern a] -> Pattern a) -> [Pattern a] -> Pattern a
forall a b. (a -> b) -> a -> b
$ ((Time, Time, Pattern a) -> Pattern a)
-> [(Time, Time, Pattern a)] -> [Pattern a]
forall a b. (a -> b) -> [a] -> [b]
map (\(Time
s,Time
e,Pattern a
p) -> ArcF Time -> Pattern a -> Pattern a
forall a. ArcF Time -> Pattern a -> Pattern a
compressArc (Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc (Time
sTime -> Time -> Time
forall a. Fractional a => a -> a -> a
/Time
total) (Time
eTime -> Time -> Time
forall a. Fractional a => a -> a -> a
/Time
total)) Pattern a
p) ([(Time, Time, Pattern a)] -> [Pattern a])
-> [(Time, Time, Pattern a)] -> [Pattern a]
forall a b. (a -> b) -> a -> b
$ Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
forall a. Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
arrange Time
0 [(Time, Pattern a)]
tps
    where total :: Time
total = [Time] -> Time
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Time] -> Time) -> [Time] -> Time
forall a b. (a -> b) -> a -> b
$ ((Time, Pattern a) -> Time) -> [(Time, Pattern a)] -> [Time]
forall a b. (a -> b) -> [a] -> [b]
map (Time, Pattern a) -> Time
forall a b. (a, b) -> a
fst [(Time, Pattern a)]
tps
          arrange :: Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
          arrange :: Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
arrange Time
_ [] = []
          arrange Time
t ((Time
t',Pattern a
p):[(Time, Pattern a)]
tps') = (Time
t,Time
tTime -> Time -> Time
forall a. Num a => a -> a -> a
+Time
t',Pattern a
p) (Time, Time, Pattern a)
-> [(Time, Time, Pattern a)] -> [(Time, Time, Pattern a)]
forall a. a -> [a] -> [a]
: Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
forall a. Time -> [(Time, Pattern a)] -> [(Time, Time, Pattern a)]
arrange (Time
tTime -> Time -> Time
forall a. Num a => a -> a -> a
+Time
t') [(Time, Pattern a)]
tps'

-- | 'overlay' combines two 'Pattern's into a new pattern, so that
-- their events are combined over time. 
overlay :: Pattern a -> Pattern a -> Pattern a
overlay :: Pattern a -> Pattern a -> Pattern a
overlay !Pattern a
p !Pattern a
p' = Query a -> Pattern a
forall a. Query a -> Pattern a
Pattern (Query a -> Pattern a) -> Query a -> Pattern a
forall a b. (a -> b) -> a -> b
$ \State
st -> Pattern a -> Query a
forall a. Pattern a -> Query a
query Pattern a
p State
st [Event a] -> [Event a] -> [Event a]
forall a. [a] -> [a] -> [a]
++ Pattern a -> Query a
forall a. Pattern a -> Query a
query Pattern a
p' State
st

-- | An infix alias of @overlay@
(<>) :: Pattern a -> Pattern a -> Pattern a
<> :: Pattern a -> Pattern a -> Pattern a
(<>) = Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
overlay

-- | 'stack' combines a list of 'Pattern's into a new pattern, so that
-- their events are combined over time.
stack :: [Pattern a] -> Pattern a
stack :: [Pattern a] -> Pattern a
stack = (Pattern a -> Pattern a -> Pattern a)
-> Pattern a -> [Pattern a] -> Pattern a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Pattern a -> Pattern a -> Pattern a
forall a. Pattern a -> Pattern a -> Pattern a
overlay Pattern a
forall a. Pattern a
silence



-- ** Manipulating time

-- | Shifts a pattern back in time by the given amount, expressed in cycles
(<~) :: Pattern Time -> Pattern a -> Pattern a
<~ :: Pattern Time -> Pattern a -> Pattern a
(<~) = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
rotL

-- | Shifts a pattern forward in time by the given amount, expressed in cycles
(~>) :: Pattern Time -> Pattern a -> Pattern a
~> :: Pattern Time -> Pattern a -> Pattern a
(~>) = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
rotR

-- | Speed up a pattern by the given time pattern
fast :: Pattern Time -> Pattern a -> Pattern a
fast :: Pattern Time -> Pattern a -> Pattern a
fast = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast

-- | Slow down a pattern by the factors in the given time pattern, 'squeezing'
-- the pattern to fit the slot given in the time pattern
fastSqueeze :: Pattern Time -> Pattern a -> Pattern a
fastSqueeze :: Pattern Time -> Pattern a -> Pattern a
fastSqueeze = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall a b c.
(a -> Pattern b -> Pattern c)
-> Pattern a -> Pattern b -> Pattern c
tParamSqueeze Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast

-- | An alias for @fast@
density :: Pattern Time -> Pattern a -> Pattern a
density :: Pattern Time -> Pattern a -> Pattern a
density = Pattern Time -> Pattern a -> Pattern a
forall a. Pattern Time -> Pattern a -> Pattern a
fast

_fast :: Time -> Pattern a -> Pattern a
_fast :: Time -> Pattern a -> Pattern a
_fast Time
r Pattern a
p | Time
r Time -> Time -> Bool
forall a. Eq a => a -> a -> Bool
== Time
0 = Pattern a
forall a. Pattern a
silence
          | Time
r Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
< Time
0 = Pattern a -> Pattern a
forall a. Pattern a -> Pattern a
rev (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast (Time -> Time
forall a. Num a => a -> a
negate Time
r) Pattern a
p
          | Bool
otherwise = (Time -> Time) -> Pattern a -> Pattern a
forall a. (Time -> Time) -> Pattern a -> Pattern a
withResultTime (Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/ Time
r) (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ (Time -> Time) -> Pattern a -> Pattern a
forall a. (Time -> Time) -> Pattern a -> Pattern a
withQueryTime (Time -> Time -> Time
forall a. Num a => a -> a -> a
* Time
r) Pattern a
p

-- | Slow down a pattern by the given time pattern
slow :: Pattern Time -> Pattern a -> Pattern a
slow :: Pattern Time -> Pattern a -> Pattern a
slow = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_slow
_slow :: Time -> Pattern a -> Pattern a
_slow :: Time -> Pattern a -> Pattern a
_slow Time
0 Pattern a
_ = Pattern a
forall a. Pattern a
silence
_slow Time
r Pattern a
p = Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fast (Time
1Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/Time
r) Pattern a
p

-- | Slow down a pattern by the factors in the given time pattern, 'squeezing'
-- the pattern to fit the slot given in the time pattern
slowSqueeze :: Pattern Time -> Pattern a -> Pattern a
slowSqueeze :: Pattern Time -> Pattern a -> Pattern a
slowSqueeze = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall a b c.
(a -> Pattern b -> Pattern c)
-> Pattern a -> Pattern b -> Pattern c
tParamSqueeze Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_slow

-- | An alias for @slow@
sparsity :: Pattern Time -> Pattern a -> Pattern a
sparsity :: Pattern Time -> Pattern a -> Pattern a
sparsity = Pattern Time -> Pattern a -> Pattern a
forall a. Pattern Time -> Pattern a -> Pattern a
slow

-- | @rev p@ returns @p@ with the event positions in each cycle
-- reversed (or mirrored).
rev :: Pattern a -> Pattern a
rev :: Pattern a -> Pattern a
rev Pattern a
p =
  Pattern a -> Pattern a
forall a. Pattern a -> Pattern a
splitQueries (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Pattern a
p {
    query :: Query a
query = \State
st -> (Event a -> Event a) -> [Event a] -> [Event a]
forall a b. (a -> b) -> [a] -> [b]
map Event a -> Event a
forall a. Event a -> Event a
makeWholeAbsolute ([Event a] -> [Event a]) -> [Event a] -> [Event a]
forall a b. (a -> b) -> a -> b
$
      (ArcF Time -> ArcF Time) -> [Event a] -> [Event a]
forall a. (ArcF Time -> ArcF Time) -> [Event a] -> [Event a]
mapParts (Time -> ArcF Time -> ArcF Time
mirrorArc (ArcF Time -> Time
midCycle (ArcF Time -> Time) -> ArcF Time -> Time
forall a b. (a -> b) -> a -> b
$ State -> ArcF Time
arc State
st)) ([Event a] -> [Event a]) -> [Event a] -> [Event a]
forall a b. (a -> b) -> a -> b
$
      (Event a -> Event a) -> [Event a] -> [Event a]
forall a b. (a -> b) -> [a] -> [b]
map Event a -> Event a
forall a. Event a -> Event a
makeWholeRelative
      (Pattern a -> Query a
forall a. Pattern a -> Query a
query Pattern a
p State
st
        {arc :: ArcF Time
arc = Time -> ArcF Time -> ArcF Time
mirrorArc (ArcF Time -> Time
midCycle (ArcF Time -> Time) -> ArcF Time -> Time
forall a b. (a -> b) -> a -> b
$ State -> ArcF Time
arc State
st) (State -> ArcF Time
arc State
st)
        })
    }
  where makeWholeRelative :: Event a -> Event a
        makeWholeRelative :: Event a -> Event a
makeWholeRelative (e :: Event a
e@(Event {whole :: forall a b. EventF a b -> Maybe a
whole = Maybe (ArcF Time)
Nothing})) = Event a
e
        makeWholeRelative (Event Context
c (Just (Arc Time
s Time
e)) p' :: ArcF Time
p'@(Arc Time
s' Time
e') a
v) =
          Context -> Maybe (ArcF Time) -> ArcF Time -> a -> Event a
forall a b. Context -> Maybe a -> a -> b -> EventF a b
Event Context
c (ArcF Time -> Maybe (ArcF Time)
forall a. a -> Maybe a
Just (ArcF Time -> Maybe (ArcF Time)) -> ArcF Time -> Maybe (ArcF Time)
forall a b. (a -> b) -> a -> b
$ Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc (Time
s'Time -> Time -> Time
forall a. Num a => a -> a -> a
-Time
s) (Time
eTime -> Time -> Time
forall a. Num a => a -> a -> a
-Time
e')) ArcF Time
p' a
v
        makeWholeAbsolute :: Event a -> Event a
        makeWholeAbsolute :: Event a -> Event a
makeWholeAbsolute (e :: Event a
e@(Event {whole :: forall a b. EventF a b -> Maybe a
whole = Maybe (ArcF Time)
Nothing})) = Event a
e
        makeWholeAbsolute (Event Context
c (Just (Arc Time
s Time
e)) p' :: ArcF Time
p'@(Arc Time
s' Time
e') a
v) =
          Context -> Maybe (ArcF Time) -> ArcF Time -> a -> Event a
forall a b. Context -> Maybe a -> a -> b -> EventF a b
Event Context
c (ArcF Time -> Maybe (ArcF Time)
forall a. a -> Maybe a
Just (ArcF Time -> Maybe (ArcF Time)) -> ArcF Time -> Maybe (ArcF Time)
forall a b. (a -> b) -> a -> b
$ Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc (Time
s'Time -> Time -> Time
forall a. Num a => a -> a -> a
-Time
e) (Time
e'Time -> Time -> Time
forall a. Num a => a -> a -> a
+Time
s)) ArcF Time
p' a
v
        midCycle :: Arc -> Time
        midCycle :: ArcF Time -> Time
midCycle (Arc Time
s Time
_) = Time -> Time
sam Time
s Time -> Time -> Time
forall a. Num a => a -> a -> a
+ Time
0.5
        mapParts :: (Arc -> Arc) -> [Event a] -> [Event a]
        mapParts :: (ArcF Time -> ArcF Time) -> [Event a] -> [Event a]
mapParts ArcF Time -> ArcF Time
f [Event a]
es = (\(Event Context
c Maybe (ArcF Time)
w ArcF Time
p' a
v) -> Context -> Maybe (ArcF Time) -> ArcF Time -> a -> Event a
forall a b. Context -> Maybe a -> a -> b -> EventF a b
Event Context
c Maybe (ArcF Time)
w (ArcF Time -> ArcF Time
f ArcF Time
p') a
v) (Event a -> Event a) -> [Event a] -> [Event a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Event a]
es
        -- | Returns the `mirror image' of a 'Arc' around the given point in time
        mirrorArc :: Time -> Arc -> Arc
        mirrorArc :: Time -> ArcF Time -> ArcF Time
mirrorArc Time
mid' (Arc Time
s Time
e) = Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc (Time
mid' Time -> Time -> Time
forall a. Num a => a -> a -> a
- (Time
eTime -> Time -> Time
forall a. Num a => a -> a -> a
-Time
mid')) (Time
mid'Time -> Time -> Time
forall a. Num a => a -> a -> a
+(Time
mid'Time -> Time -> Time
forall a. Num a => a -> a -> a
-Time
s))

{- | Plays a portion of a pattern, specified by a time arc (start and end time).
The new resulting pattern is played over the time period of the original pattern:

@
d1 $ zoom (0.25, 0.75) $ sound "bd*2 hh*3 [sn bd]*2 drum"
@

In the pattern above, `zoom` is used with an arc from 25% to 75%. It is equivalent to this pattern:

@
d1 $ sound "hh*3 [sn bd]*2"
@
-}
zoom :: (Time, Time) -> Pattern a -> Pattern a
zoom :: (Time, Time) -> Pattern a -> Pattern a
zoom (Time
s,Time
e) = ArcF Time -> Pattern a -> Pattern a
forall a. ArcF Time -> Pattern a -> Pattern a
zoomArc (Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc Time
s Time
e)

zoomArc :: Arc -> Pattern a -> Pattern a
zoomArc :: ArcF Time -> Pattern a -> Pattern a
zoomArc (Arc Time
s Time
e) Pattern a
p = Pattern a -> Pattern a
forall a. Pattern a -> Pattern a
splitQueries (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$
  (ArcF Time -> ArcF Time) -> Pattern a -> Pattern a
forall a. (ArcF Time -> ArcF Time) -> Pattern a -> Pattern a
withResultArc ((Time -> Time) -> ArcF Time -> ArcF Time
mapCycle ((Time -> Time -> Time
forall a. Fractional a => a -> a -> a
/Time
d) (Time -> Time) -> (Time -> Time) -> Time -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Time -> Time -> Time
forall a. Num a => a -> a -> a
subtract Time
s)) (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ (ArcF Time -> ArcF Time) -> Pattern a -> Pattern a
forall a. (ArcF Time -> ArcF Time) -> Pattern a -> Pattern a
withQueryArc ((Time -> Time) -> ArcF Time -> ArcF Time
mapCycle ((Time -> Time -> Time
forall a. Num a => a -> a -> a
+Time
s) (Time -> Time) -> (Time -> Time) -> Time -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Time -> Time -> Time
forall a. Num a => a -> a -> a
*Time
d))) Pattern a
p
     where d :: Time
d = Time
eTime -> Time -> Time
forall a. Num a => a -> a -> a
-Time
s

-- | @fastGap@ is similar to 'fast' but maintains its cyclic
-- alignment. For example, @fastGap 2 p@ would squash the events in
-- pattern @p@ into the first half of each cycle (and the second
-- halves would be empty). The factor should be at least 1
fastGap :: Pattern Time -> Pattern a -> Pattern a
fastGap :: Pattern Time -> Pattern a -> Pattern a
fastGap = (Time -> Pattern a -> Pattern a)
-> Pattern Time -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Time -> Pattern a -> Pattern a
forall a. Time -> Pattern a -> Pattern a
_fastGap

-- | An alias for @fastGap@
densityGap :: Pattern Time -> Pattern a -> Pattern a
densityGap :: Pattern Time -> Pattern a -> Pattern a
densityGap = Pattern Time -> Pattern a -> Pattern a
forall a. Pattern Time -> Pattern a -> Pattern a
fastGap

compress :: (Time,Time) -> Pattern a -> Pattern a
compress :: (Time, Time) -> Pattern a -> Pattern a
compress (Time
s,Time
e) = ArcF Time -> Pattern a -> Pattern a
forall a. ArcF Time -> Pattern a -> Pattern a
compressArc (Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc Time
s Time
e)

compressTo :: (Time,Time) -> Pattern a -> Pattern a
compressTo :: (Time, Time) -> Pattern a -> Pattern a
compressTo (Time
s,Time
e) = ArcF Time -> Pattern a -> Pattern a
forall a. ArcF Time -> Pattern a -> Pattern a
compressArcTo (Time -> Time -> ArcF Time
forall a. a -> a -> ArcF a
Arc Time
s Time
e)

repeatCycles :: Pattern Int -> Pattern a -> Pattern a
repeatCycles :: Pattern Int -> Pattern a -> Pattern a
repeatCycles = (Int -> Pattern a -> Pattern a)
-> Pattern Int -> Pattern a -> Pattern a
forall t1 t2 a.
(t1 -> t2 -> Pattern a) -> Pattern t1 -> t2 -> Pattern a
tParam Int -> Pattern a -> Pattern a
forall a. Int -> Pattern a -> Pattern a
_repeatCycles

_repeatCycles :: Int -> Pattern a -> Pattern a
_repeatCycles :: Int -> Pattern a -> Pattern a
_repeatCycles Int
n Pattern a
p = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat (Int -> Pattern a -> [Pattern a]
forall a. Int -> a -> [a]
replicate Int
n Pattern a
p)

fastRepeatCycles :: Int -> Pattern a -> Pattern a
fastRepeatCycles :: Int -> Pattern a -> Pattern a
fastRepeatCycles Int
n Pattern a
p = [Pattern a] -> Pattern a
forall a. [Pattern a] -> Pattern a
cat (Int -> Pattern a -> [Pattern a]
forall a. Int -> a -> [a]
replicate Int
n Pattern a
p)

-- | * Higher order functions

-- | Functions which work on other functions (higher order functions)

-- | @every n f p@ applies the function @f@ to @p@, but only affects
-- every @n@ cycles.
every :: Pattern Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
every :: Pattern Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
every Pattern Int
tp Pattern a -> Pattern a
f Pattern a
p = Pattern (Pattern a) -> Pattern a
forall a. Pattern (Pattern a) -> Pattern a
innerJoin (Pattern (Pattern a) -> Pattern a)
-> Pattern (Pattern a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ (\Int
t -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a. Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every Int
t Pattern a -> Pattern a
f Pattern a
p) (Int -> Pattern a) -> Pattern Int -> Pattern (Pattern a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern Int
tp

_every :: Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every :: Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every Int
0 Pattern a -> Pattern a
_ Pattern a
p = Pattern a
p
_every Int
n Pattern a -> Pattern a
f Pattern a
p = (Int -> Bool) -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a.
(Int -> Bool) -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
when ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0) (Int -> Bool) -> (Int -> Int) -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
n)) Pattern a -> Pattern a
f Pattern a
p

-- | @every n o f'@ is like @every n f@ with an offset of @o@ cycles
every' :: Pattern Int -> Pattern Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
every' :: Pattern Int
-> Pattern Int
-> (Pattern a -> Pattern a)
-> Pattern a
-> Pattern a
every' Pattern Int
np Pattern Int
op Pattern a -> Pattern a
f Pattern a
p = do { Int
n <- Pattern Int
np; Int
o <- Pattern Int
op; Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a.
Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every' Int
n Int
o Pattern a -> Pattern a
f Pattern a
p }

_every' :: Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every' :: Int -> Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
_every' Int
n Int
o = (Int -> Bool) -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a.
(Int -> Bool) -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
when ((Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
o) (Int -> Bool) -> (Int -> Int) -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
n))

-- | @foldEvery ns f p@ applies the function @f@ to @p@, and is applied for
-- each cycle in @ns@.
foldEvery :: [Int] -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
foldEvery :: [Int] -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
foldEvery [Int]
ns Pattern a -> Pattern a
f Pattern a
p = (Int -> Pattern a -> Pattern a) -> Pattern a -> [Int] -> Pattern a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a. Int -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
`_every` Pattern a -> Pattern a
f) Pattern a
p [Int]
ns

{-|
Only `when` the given test function returns `True` the given pattern
transformation is applied. The test function will be called with the
current cycle as a number.

@
d1 $ when ((elem '4').show)
  (striate 4)
  $ sound "hh hc"
@

The above will only apply `striate 4` to the pattern if the current
cycle number contains the number 4. So the fourth cycle will be
striated and the fourteenth and so on. Expect lots of striates after
cycle number 399.
-}
when :: (Int -> Bool) -> (Pattern a -> Pattern a) ->  Pattern a -> Pattern a
when :: (Int -> Bool) -> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
when Int -> Bool
test Pattern a -> Pattern a
f Pattern a
p = Pattern a -> Pattern a
forall a. Pattern a -> Pattern a
splitQueries (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Pattern a
p {query :: Query a
query = Query a
apply}
  where apply :: Query a
apply State
st | Int -> Bool
test (Time -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Time -> Int) -> Time -> Int
forall a b. (a -> b) -> a -> b
$ ArcF Time -> Time
forall a. ArcF a -> a
start (ArcF Time -> Time) -> ArcF Time -> Time
forall a b. (a -> b) -> a -> b
$ State -> ArcF Time
arc State
st) = Pattern a -> Query a
forall a. Pattern a -> Query a
query (Pattern a -> Pattern a
f Pattern a
p) State
st
                 | Bool
otherwise = Pattern a -> Query a
forall a. Pattern a -> Query a
query Pattern a
p State
st

-- | Like 'when', but works on continuous time values rather than cycle numbers.
whenT :: (Time -> Bool) -> (Pattern a -> Pattern a) ->  Pattern a -> Pattern a
whenT :: (Time -> Bool)
-> (Pattern a -> Pattern a) -> Pattern a -> Pattern a
whenT Time -> Bool
test Pattern a -> Pattern a
f Pattern a
p = Pattern a -> Pattern a
forall a. Pattern a -> Pattern a
splitQueries (Pattern a -> Pattern a) -> Pattern a -> Pattern a
forall a b. (a -> b) -> a -> b
$ Pattern a
p {query :: Query a
query = Query a
apply}
  where apply :: Query a
apply State
st | Time -> Bool
test (ArcF Time -> Time
forall a. ArcF a -> a
start (ArcF Time -> Time) -> ArcF Time -> Time
forall a b. (a -> b) -> a -> b
$ State -> ArcF Time
arc State
st) = Pattern a -> Query a
forall a. Pattern a -> Query a
query (Pattern a -> Pattern a
f Pattern a
p) State
st
                 | Bool
otherwise = Pattern a -> Query a
forall a. Pattern a -> Query a
query Pattern a
p State
st

_getP_ :: (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP_ :: (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP_ Value -> Maybe a
f Pattern Value
pat = Pattern (Maybe a) -> Pattern a
forall a. Pattern (Maybe a) -> Pattern a
filterJust (Pattern (Maybe a) -> Pattern a) -> Pattern (Maybe a) -> Pattern a
forall a b. (a -> b) -> a -> b
$ Value -> Maybe a
f (Value -> Maybe a) -> Pattern Value -> Pattern (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern Value
pat

_getP :: a -> (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP :: a -> (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP a
d Value -> Maybe a
f Pattern Value
pat = (a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
d (Maybe a -> a) -> (Value -> Maybe a) -> Value -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value -> Maybe a
f) (Value -> a) -> Pattern Value -> Pattern a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Pattern Value
pat

_cX :: a -> (Value -> Maybe a) -> String -> Pattern a
_cX :: a -> (Value -> Maybe a) -> String -> Pattern a
_cX a
d Value -> Maybe a
f String
s = Query a -> Pattern a
forall a. Query a -> Pattern a
Pattern (Query a -> Pattern a) -> Query a -> Pattern a
forall a b. (a -> b) -> a -> b
$ \(State ArcF Time
a StateMap
m) -> Pattern a -> ArcF Time -> [Event a]
forall a. Pattern a -> ArcF Time -> [Event a]
queryArc (Pattern a
-> (Pattern Value -> Pattern a)
-> Maybe (Pattern Value)
-> Pattern a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (a -> Pattern a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
d) (a -> (Value -> Maybe a) -> Pattern Value -> Pattern a
forall a. a -> (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP a
d Value -> Maybe a
f) (Maybe (Pattern Value) -> Pattern a)
-> Maybe (Pattern Value) -> Pattern a
forall a b. (a -> b) -> a -> b
$ String -> StateMap -> Maybe (Pattern Value)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup String
s StateMap
m) ArcF Time
a

_cX_ :: (Value -> Maybe a) -> String -> Pattern a
_cX_ :: (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe a
f String
s = Query a -> Pattern a
forall a. Query a -> Pattern a
Pattern (Query a -> Pattern a) -> Query a -> Pattern a
forall a b. (a -> b) -> a -> b
$ \(State ArcF Time
a StateMap
m) -> Pattern a -> ArcF Time -> [Event a]
forall a. Pattern a -> ArcF Time -> [Event a]
queryArc (Pattern a
-> (Pattern Value -> Pattern a)
-> Maybe (Pattern Value)
-> Pattern a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Pattern a
forall a. Pattern a
silence ((Value -> Maybe a) -> Pattern Value -> Pattern a
forall a. (Value -> Maybe a) -> Pattern Value -> Pattern a
_getP_ Value -> Maybe a
f) (Maybe (Pattern Value) -> Pattern a)
-> Maybe (Pattern Value) -> Pattern a
forall a b. (a -> b) -> a -> b
$ String -> StateMap -> Maybe (Pattern Value)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup String
s StateMap
m) ArcF Time
a

cF :: Double -> String -> Pattern Double
cF :: Double -> String -> Pattern Double
cF Double
d = Double -> (Value -> Maybe Double) -> String -> Pattern Double
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Double
d Value -> Maybe Double
getF
cF_ :: String -> Pattern Double
cF_ :: String -> Pattern Double
cF_ = (Value -> Maybe Double) -> String -> Pattern Double
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe Double
getF
cF0 :: String -> Pattern Double
cF0 :: String -> Pattern Double
cF0 = Double -> (Value -> Maybe Double) -> String -> Pattern Double
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Double
0 Value -> Maybe Double
getF

cN :: Note -> String -> Pattern Note
cN :: Note -> String -> Pattern Note
cN Note
d = Note -> (Value -> Maybe Note) -> String -> Pattern Note
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Note
d Value -> Maybe Note
getN
cN_ :: String -> Pattern Note
cN_ :: String -> Pattern Note
cN_ = (Value -> Maybe Note) -> String -> Pattern Note
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe Note
getN
cN0 :: String -> Pattern Note
cN0 :: String -> Pattern Note
cN0 = Note -> (Value -> Maybe Note) -> String -> Pattern Note
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX (Double -> Note
Note Double
0) Value -> Maybe Note
getN

cI :: Int -> String -> Pattern Int
cI :: Int -> String -> Pattern Int
cI Int
d = Int -> (Value -> Maybe Int) -> String -> Pattern Int
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Int
d Value -> Maybe Int
getI
cI_ :: String -> Pattern Int
cI_ :: String -> Pattern Int
cI_ = (Value -> Maybe Int) -> String -> Pattern Int
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe Int
getI
cI0 :: String -> Pattern Int
cI0 :: String -> Pattern Int
cI0 = Int -> (Value -> Maybe Int) -> String -> Pattern Int
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Int
0 Value -> Maybe Int
getI

cB :: Bool -> String -> Pattern Bool
cB :: Bool -> String -> Pattern Bool
cB Bool
d = Bool -> (Value -> Maybe Bool) -> String -> Pattern Bool
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Bool
d Value -> Maybe Bool
getB
cB_ :: String -> Pattern Bool
cB_ :: String -> Pattern Bool
cB_ = (Value -> Maybe Bool) -> String -> Pattern Bool
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe Bool
getB
cB0 :: String -> Pattern Bool
cB0 :: String -> Pattern Bool
cB0 = Bool -> (Value -> Maybe Bool) -> String -> Pattern Bool
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Bool
False Value -> Maybe Bool
getB

cR :: Rational -> String -> Pattern Rational
cR :: Time -> String -> Pattern Time
cR Time
d = Time -> (Value -> Maybe Time) -> String -> Pattern Time
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Time
d Value -> Maybe Time
getR
cR_ :: String -> Pattern Rational
cR_ :: String -> Pattern Time
cR_ = (Value -> Maybe Time) -> String -> Pattern Time
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe Time
getR
cR0 :: String -> Pattern Rational
cR0 :: String -> Pattern Time
cR0 = Time -> (Value -> Maybe Time) -> String -> Pattern Time
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX Time
0 Value -> Maybe Time
getR

cT :: Time -> String -> Pattern Time
cT :: Time -> String -> Pattern Time
cT = Time -> String -> Pattern Time
cR
cT0 :: String -> Pattern Time
cT0 :: String -> Pattern Time
cT0 = String -> Pattern Time
cR0
cT_ :: String -> Pattern Time
cT_ :: String -> Pattern Time
cT_ = String -> Pattern Time
cR_

cS :: String -> String -> Pattern String
cS :: String -> String -> Pattern String
cS String
d = String -> (Value -> Maybe String) -> String -> Pattern String
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX String
d Value -> Maybe String
getS
cS_ :: String -> Pattern String
cS_ :: String -> Pattern String
cS_ = (Value -> Maybe String) -> String -> Pattern String
forall a. (Value -> Maybe a) -> String -> Pattern a
_cX_ Value -> Maybe String
getS
cS0 :: String -> Pattern String
cS0 :: String -> Pattern String
cS0 = String -> (Value -> Maybe String) -> String -> Pattern String
forall a. a -> (Value -> Maybe a) -> String -> Pattern a
_cX String
"" Value -> Maybe String
getS

-- Default controller inputs (for MIDI)
in0 :: Pattern Double
in0 :: Pattern Double
in0 = Double -> String -> Pattern Double
cF Double
0 String
"0"
in1 :: Pattern Double
in1 :: Pattern Double
in1 = Double -> String -> Pattern Double
cF Double
0 String
"1"
in2 :: Pattern Double
in2 :: Pattern Double
in2 = Double -> String -> Pattern Double
cF Double
0 String
"2"
in3 :: Pattern Double
in3 :: Pattern Double
in3 = Double -> String -> Pattern Double
cF Double
0 String
"3"
in4 :: Pattern Double
in4 :: Pattern Double
in4 = Double -> String -> Pattern Double
cF Double
0 String
"4"
in5 :: Pattern Double
in5 :: Pattern Double
in5 = Double -> String -> Pattern Double
cF Double
0 String
"5"
in6 :: Pattern Double
in6 :: Pattern Double
in6 = Double -> String -> Pattern Double
cF Double
0 String
"6"
in7 :: Pattern Double
in7 :: Pattern Double
in7 = Double -> String -> Pattern Double
cF Double
0 String
"7"
in8 :: Pattern Double
in8 :: Pattern Double
in8 = Double -> String -> Pattern Double
cF Double
0 String
"8"
in9 :: Pattern Double
in9 :: Pattern Double
in9 = Double -> String -> Pattern Double
cF Double
0 String
"9"
in10 :: Pattern Double
in10 :: Pattern Double
in10 = Double -> String -> Pattern Double
cF Double
0 String
"10"
in11 :: Pattern Double
in11 :: Pattern Double
in11 = Double -> String -> Pattern Double
cF Double
0 String
"11"
in12 :: Pattern Double
in12 :: Pattern Double
in12 = Double -> String -> Pattern Double
cF Double
0 String
"12"
in13 :: Pattern Double
in13 :: Pattern Double
in13 = Double -> String -> Pattern Double
cF Double
0 String
"13"
in14 :: Pattern Double
in14 :: Pattern Double
in14 = Double -> String -> Pattern Double
cF Double
0 String
"14"
in15 :: Pattern Double
in15 :: Pattern Double
in15 = Double -> String -> Pattern Double
cF Double
0 String
"15"
in16 :: Pattern Double
in16 :: Pattern Double
in16 = Double -> String -> Pattern Double
cF Double
0 String
"16"
in17 :: Pattern Double
in17 :: Pattern Double
in17 = Double -> String -> Pattern Double
cF Double
0 String
"17"
in18 :: Pattern Double
in18 :: Pattern Double
in18 = Double -> String -> Pattern Double
cF Double
0 String
"18"
in19 :: Pattern Double
in19 :: Pattern Double
in19 = Double -> String -> Pattern Double
cF Double
0 String
"19"
in20 :: Pattern Double
in20 :: Pattern Double
in20 = Double -> String -> Pattern Double
cF Double
0 String
"20"
in21 :: Pattern Double
in21 :: Pattern Double
in21 = Double -> String -> Pattern Double
cF Double
0 String
"21"
in22 :: Pattern Double
in22 :: Pattern Double
in22 = Double -> String -> Pattern Double
cF Double
0 String
"22"
in23 :: Pattern Double
in23 :: Pattern Double
in23 = Double -> String -> Pattern Double
cF Double
0 String
"23"
in24 :: Pattern Double
in24 :: Pattern Double
in24 = Double -> String -> Pattern Double
cF Double
0 String
"24"
in25 :: Pattern Double
in25 :: Pattern Double
in25 = Double -> String -> Pattern Double
cF Double
0 String
"25"
in26 :: Pattern Double
in26 :: Pattern Double
in26 = Double -> String -> Pattern Double
cF Double
0 String
"26"
in27 :: Pattern Double
in27 :: Pattern Double
in27 = Double -> String -> Pattern Double
cF Double
0 String
"27"
in28 :: Pattern Double
in28 :: Pattern Double
in28 = Double -> String -> Pattern Double
cF Double
0 String
"28"
in29 :: Pattern Double
in29 :: Pattern Double
in29 = Double -> String -> Pattern Double
cF Double
0 String
"29"
in30 :: Pattern Double
in30 :: Pattern Double
in30 = Double -> String -> Pattern Double
cF Double
0 String
"30"
in31 :: Pattern Double
in31 :: Pattern Double
in31 = Double -> String -> Pattern Double
cF Double
0 String
"31"
in32 :: Pattern Double
in32 :: Pattern Double
in32 = Double -> String -> Pattern Double
cF Double
0 String
"32"
in33 :: Pattern Double
in33 :: Pattern Double
in33 = Double -> String -> Pattern Double
cF Double
0 String
"33"
in34 :: Pattern Double
in34 :: Pattern Double
in34 = Double -> String -> Pattern Double
cF Double
0 String
"34"
in35 :: Pattern Double
in35 :: Pattern Double
in35 = Double -> String -> Pattern Double
cF Double
0 String
"35"
in36 :: Pattern Double
in36 :: Pattern Double
in36 = Double -> String -> Pattern Double
cF Double
0 String
"36"
in37 :: Pattern Double
in37 :: Pattern Double
in37 = Double -> String -> Pattern Double
cF Double
0 String
"37"
in38 :: Pattern Double
in38 :: Pattern Double
in38 = Double -> String -> Pattern Double
cF Double
0 String
"38"
in39 :: Pattern Double
in39 :: Pattern Double
in39 = Double -> String -> Pattern Double
cF Double
0 String
"39"
in40 :: Pattern Double
in40 :: Pattern Double
in40 = Double -> String -> Pattern Double
cF Double
0 String
"40"
in41 :: Pattern Double
in41 :: Pattern Double
in41 = Double -> String -> Pattern Double
cF Double
0 String
"41"
in42 :: Pattern Double
in42 :: Pattern Double
in42 = Double -> String -> Pattern Double
cF Double
0 String
"42"
in43 :: Pattern Double
in43 :: Pattern Double
in43 = Double -> String -> Pattern Double
cF Double
0 String
"43"
in44 :: Pattern Double
in44 :: Pattern Double
in44 = Double -> String -> Pattern Double
cF Double
0 String
"44"
in45 :: Pattern Double
in45 :: Pattern Double
in45 = Double -> String -> Pattern Double
cF Double
0 String
"45"
in46 :: Pattern Double
in46 :: Pattern Double
in46 = Double -> String -> Pattern Double
cF Double
0 String
"46"
in47 :: Pattern Double
in47 :: Pattern Double
in47 = Double -> String -> Pattern Double
cF Double
0 String
"47"
in48 :: Pattern Double
in48 :: Pattern Double
in48 = Double -> String -> Pattern Double
cF Double
0 String
"48"
in49 :: Pattern Double
in49 :: Pattern Double
in49 = Double -> String -> Pattern Double
cF Double
0 String
"49"
in50 :: Pattern Double
in50 :: Pattern Double
in50 = Double -> String -> Pattern Double
cF Double
0 String
"50"
in51 :: Pattern Double
in51 :: Pattern Double
in51 = Double -> String -> Pattern Double
cF Double
0 String
"51"
in52 :: Pattern Double
in52 :: Pattern Double
in52 = Double -> String -> Pattern Double
cF Double
0 String
"52"
in53 :: Pattern Double
in53 :: Pattern Double
in53 = Double -> String -> Pattern Double
cF Double
0 String
"53"
in54 :: Pattern Double
in54 :: Pattern Double
in54 = Double -> String -> Pattern Double
cF Double
0 String
"54"
in55 :: Pattern Double
in55 :: Pattern Double
in55 = Double -> String -> Pattern Double
cF Double
0 String
"55"
in56 :: Pattern Double
in56 :: Pattern Double
in56 = Double -> String -> Pattern Double
cF Double
0 String
"56"
in57 :: Pattern Double
in57 :: Pattern Double
in57 = Double -> String -> Pattern Double
cF Double
0 String
"57"
in58 :: Pattern Double
in58 :: Pattern Double
in58 = Double -> String -> Pattern Double
cF Double
0 String
"58"
in59 :: Pattern Double
in59 :: Pattern Double
in59 = Double -> String -> Pattern Double
cF Double
0 String
"59"
in60 :: Pattern Double
in60 :: Pattern Double
in60 = Double -> String -> Pattern Double
cF Double
0 String
"60"
in61 :: Pattern Double
in61 :: Pattern Double
in61 = Double -> String -> Pattern Double
cF Double
0 String
"61"
in62 :: Pattern Double
in62 :: Pattern Double
in62 = Double -> String -> Pattern Double
cF Double
0 String
"62"
in63 :: Pattern Double
in63 :: Pattern Double
in63 = Double -> String -> Pattern Double
cF Double
0 String
"63"
in64 :: Pattern Double
in64 :: Pattern Double
in64 = Double -> String -> Pattern Double
cF Double
0 String
"64"
in65 :: Pattern Double
in65 :: Pattern Double
in65 = Double -> String -> Pattern Double
cF Double
0 String
"65"
in66 :: Pattern Double
in66 :: Pattern Double
in66 = Double -> String -> Pattern Double
cF Double
0 String
"66"
in67 :: Pattern Double
in67 :: Pattern Double
in67 = Double -> String -> Pattern Double
cF Double
0 String
"67"
in68 :: Pattern Double
in68 :: Pattern Double
in68 = Double -> String -> Pattern Double
cF Double
0 String
"68"
in69 :: Pattern Double
in69 :: Pattern Double
in69 = Double -> String -> Pattern Double
cF Double
0 String
"69"
in70 :: Pattern Double
in70 :: Pattern Double
in70 = Double -> String -> Pattern Double
cF Double
0 String
"70"
in71 :: Pattern Double
in71 :: Pattern Double
in71 = Double -> String -> Pattern Double
cF Double
0 String
"71"
in72 :: Pattern Double
in72 :: Pattern Double
in72 = Double -> String -> Pattern Double
cF Double
0 String
"72"
in73 :: Pattern Double
in73 :: Pattern Double
in73 = Double -> String -> Pattern Double
cF Double
0 String
"73"
in74 :: Pattern Double
in74 :: Pattern Double
in74 = Double -> String -> Pattern Double
cF Double
0 String
"74"
in75 :: Pattern Double
in75 :: Pattern Double
in75 = Double -> String -> Pattern Double
cF Double
0 String
"75"
in76 :: Pattern Double
in76 :: Pattern Double
in76 = Double -> String -> Pattern Double
cF Double
0 String
"76"
in77 :: Pattern Double
in77 :: Pattern Double
in77 = Double -> String -> Pattern Double
cF Double
0 String
"77"
in78 :: Pattern Double
in78 :: Pattern Double
in78 = Double -> String -> Pattern Double
cF Double
0 String
"78"
in79 :: Pattern Double
in79 :: Pattern Double
in79 = Double -> String -> Pattern Double
cF Double
0 String
"79"
in80 :: Pattern Double
in80 :: Pattern Double
in80 = Double -> String -> Pattern Double
cF Double
0 String
"80"
in81 :: Pattern Double
in81 :: Pattern Double
in81 = Double -> String -> Pattern Double
cF Double
0 String
"81"
in82 :: Pattern Double
in82 :: Pattern Double
in82 = Double -> String -> Pattern Double
cF Double
0 String
"82"
in83 :: Pattern Double
in83 :: Pattern Double
in83 = Double -> String -> Pattern Double
cF Double
0 String
"83"
in84 :: Pattern Double
in84 :: Pattern Double
in84 = Double -> String -> Pattern Double
cF Double
0 String
"84"
in85 :: Pattern Double
in85 :: Pattern Double
in85 = Double -> String -> Pattern Double
cF Double
0 String
"85"
in86 :: Pattern Double
in86 :: Pattern Double
in86 = Double -> String -> Pattern Double
cF Double
0 String
"86"
in87 :: Pattern Double
in87 :: Pattern Double
in87 = Double -> String -> Pattern Double
cF Double
0 String
"87"
in88 :: Pattern Double
in88 :: Pattern Double
in88 = Double -> String -> Pattern Double
cF Double
0 String
"88"
in89 :: Pattern Double
in89 :: Pattern Double
in89 = Double -> String -> Pattern Double
cF Double
0 String
"89"
in90 :: Pattern Double
in90 :: Pattern Double
in90 = Double -> String -> Pattern Double
cF Double
0 String
"90"
in91 :: Pattern Double
in91 :: Pattern Double
in91 = Double -> String -> Pattern Double
cF Double
0 String
"91"
in92 :: Pattern Double
in92 :: Pattern Double
in92 = Double -> String -> Pattern Double
cF Double
0 String
"92"
in93 :: Pattern Double
in93 :: Pattern Double
in93 = Double -> String -> Pattern Double
cF Double
0 String
"93"
in94 :: Pattern Double
in94 :: Pattern Double
in94 = Double -> String -> Pattern Double
cF Double
0 String
"94"
in95 :: Pattern Double
in95 :: Pattern Double
in95 = Double -> String -> Pattern Double
cF Double
0 String
"95"
in96 :: Pattern Double
in96 :: Pattern Double
in96 = Double -> String -> Pattern Double
cF Double
0 String
"96"
in97 :: Pattern Double
in97 :: Pattern Double
in97 = Double -> String -> Pattern Double
cF Double
0 String
"97"
in98 :: Pattern Double
in98 :: Pattern Double
in98 = Double -> String -> Pattern Double
cF Double
0 String
"98"
in99 :: Pattern Double
in99 :: Pattern Double
in99 = Double -> String -> Pattern Double
cF Double
0 String
"99"
in100 :: Pattern Double
in100 :: Pattern Double
in100 = Double -> String -> Pattern Double
cF Double
0 String
"100"
in101 :: Pattern Double
in101 :: Pattern Double
in101 = Double -> String -> Pattern Double
cF Double
0 String
"101"
in102 :: Pattern Double
in102 :: Pattern Double
in102 = Double -> String -> Pattern Double
cF Double
0 String
"102"
in103 :: Pattern Double
in103 :: Pattern Double
in103 = Double -> String -> Pattern Double
cF Double
0 String
"103"
in104 :: Pattern Double
in104 :: Pattern Double
in104 = Double -> String -> Pattern Double
cF Double
0 String
"104"
in105 :: Pattern Double
in105 :: Pattern Double
in105 = Double -> String -> Pattern Double
cF Double
0 String
"105"
in106 :: Pattern Double
in106 :: Pattern Double
in106 = Double -> String -> Pattern Double
cF Double
0 String
"106"
in107 :: Pattern Double
in107 :: Pattern Double
in107 = Double -> String -> Pattern Double
cF Double
0 String
"107"
in108 :: Pattern Double
in108 :: Pattern Double
in108 = Double -> String -> Pattern Double
cF Double
0 String
"108"
in109 :: Pattern Double
in109 :: Pattern Double
in109 = Double -> String -> Pattern Double
cF Double
0 String
"109"
in110 :: Pattern Double
in110 :: Pattern Double
in110 = Double -> String -> Pattern Double
cF Double
0 String
"110"
in111 :: Pattern Double
in111 :: Pattern Double
in111 = Double -> String -> Pattern Double
cF Double
0 String
"111"
in112 :: Pattern Double
in112 :: Pattern Double
in112 = Double -> String -> Pattern Double
cF Double
0 String
"112"
in113 :: Pattern Double
in113 :: Pattern Double
in113 = Double -> String -> Pattern Double
cF Double
0 String
"113"
in114 :: Pattern Double
in114 :: Pattern Double
in114 = Double -> String -> Pattern Double
cF Double
0 String
"114"
in115 :: Pattern Double
in115 :: Pattern Double
in115 = Double -> String -> Pattern Double
cF Double
0 String
"115"
in116 :: Pattern Double
in116 :: Pattern Double
in116 = Double -> String -> Pattern Double
cF Double
0 String
"116"
in117 :: Pattern Double
in117 :: Pattern Double
in117 = Double -> String -> Pattern Double
cF Double
0 String
"117"
in118 :: Pattern Double
in118 :: Pattern Double
in118 = Double -> String -> Pattern Double
cF Double
0 String
"118"
in119 :: Pattern Double
in119 :: Pattern Double
in119 = Double -> String -> Pattern Double
cF Double
0 String
"119"
in120 :: Pattern Double
in120 :: Pattern Double
in120 = Double -> String -> Pattern Double
cF Double
0 String
"120"
in121 :: Pattern Double
in121 :: Pattern Double
in121 = Double -> String -> Pattern Double
cF Double
0 String
"121"
in122 :: Pattern Double
in122 :: Pattern Double
in122 = Double -> String -> Pattern Double
cF Double
0 String
"122"
in123 :: Pattern Double
in123 :: Pattern Double
in123 = Double -> String -> Pattern Double
cF Double
0 String
"123"
in124 :: Pattern Double
in124 :: Pattern Double
in124 = Double -> String -> Pattern Double
cF Double
0 String
"124"
in125 :: Pattern Double
in125 :: Pattern Double
in125 = Double -> String -> Pattern Double
cF Double
0 String
"125"
in126 :: Pattern Double
in126 :: Pattern Double
in126 = Double -> String -> Pattern Double
cF Double
0 String
"126"
in127 :: Pattern Double
in127 :: Pattern Double
in127 = Double -> String -> Pattern Double
cF Double
0 String
"127"