mescaline-0.0.1: Mescaline is a data-driven sequencer and synthesizer.

Mescaline.Synth.Pattern.ASTLib

Contents

Synopsis

Structure

data Pattern t Source

A Pattern is a finite or infinite stream of values of a certain type.

There are four different types in the pattern language, real numbers (Scalar), boolean values (Boolean), two dimensional coordinates (Coord) and synthesis events (Event).

The pattern functions in this module can be used to combine patterns in various ways in order to build more complex expressions.

For the Scalar pattern type, many unary functions (UnaryFunc) and binary functions (BinaryFunc) are mapped to standard Haskell type classes. This allows to use more descriptive function names and binary operators, in particular the ones from the Num, Fractional and Floating type classes.

patch :: Pattern Event -> Tree EventSource

Construct an abstract syntax tree from an event pattern.

This is the main entry point, a patch will always look like this:

 it = patch (<expressions>)

Scalar patterns

value :: Double -> Pattern ScalarSource

Construct an infinite constant value scalar pattern.

Unary and binary function application

data UnaryFunc Source

Constructors

F_abs

Absolute value

F_signum

Sign

F_negate

Negation

F_recip

Reciprocal

F_truncate

Truncation towards zero.

F_round

nearest integer to argument.

F_ceiling

Least integer not less than argument.

F_floor

Truncation towards minus infinity.

F_exp

Exponential function.

F_sqrt

Square root.

F_log

Natural logarithm

F_sin

Sine.

F_tan

Tangent.

F_cos

Cosine.

F_asin

Arc sine.

F_atan

Arc tangent.

F_acos

Arc cosine.

F_sinh

Hyperbolic sine.

F_tanh

Hyperbolic tangent.

F_cosh

Hyperbolic cosine.

F_asinh

Arc hyperbolic sine.

F_atanh

Arc hyperbolic tangent.

F_acosh

Arc hyperbolic cosine.

data BinaryFunc Source

Constructors

F_min

Minimum of the arguments.

F_max

Maximum of the arguments.

F_add

Sum of the arguments.

F_subtract

Difference of the arguments.

F_multiply

Product of the arguments.

F_divide

Division of the arguments.

F_power

First argument raised to the power of second argument.

F_logBase

Logarithm of the second argument to the base of the first argument.

map :: UnaryFunc -> Pattern Scalar -> Pattern ScalarSource

Map a unary function to a scalar.

zip :: BinaryFunc -> Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Combine two scalars with a binary function.

min :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Return the smaller of the arguments.

min a b

max :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Return the bigger of the arguments.

max a b

Numeric functions

truncateP :: Pattern Scalar -> Pattern ScalarSource

Truncate scalar towards -Infinity.

roundP :: Pattern Scalar -> Pattern ScalarSource

Round scalar to the closest integer.

ceilingP :: Pattern Scalar -> Pattern ScalarSource

Return the next integer bigger than a scalar.

floorP :: Pattern Scalar -> Pattern ScalarSource

Truncate scalar towards zero.

clip :: Pattern Scalar -> Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Constrain a scalar to the interval [min,max].

clip min max x

wrap :: Pattern Scalar -> Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Wrap a scalar into the interval [min,max].

wrap min max x

fold :: Pattern Scalar -> Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Fold a scalar into the interval [min,max].

fold min max x

Scalar conversions

Random functions

rand :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Generate white noise in a given range.

rand min max
  • min Minimum value.
  • max Maximum value.

randi :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Generate integer random value in [min,max[.

randi min max
  • min Minimum value (inclusive).
  • max Maximum value (exclusive).

exprand :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Generate exponential noise in a given range.

exprand min max
  • min Minimum value.
  • max Maximum value.

gaussian :: Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Generate gaussian noise with the given mean and variance.

gaussian mean var
  • mean Mean value.
  • var Variance, or standard deviation squared.

brown :: Limit -> Pattern Scalar -> Pattern Scalar -> Pattern Scalar -> Pattern Scalar -> Pattern ScalarSource

Generate brown noise in a given range with a given step size.

brown limmit stepMin stepMax min max
  • limit Interval boundary behavior.
  • stepMin Minimum step size.
  • stepMax Maximum step size.
  • min Minimum value.
  • max Maximum value.

Boolean patterns

Comparisons of scalars

(|==|) :: Pattern Scalar -> Pattern Scalar -> Pattern BooleanSource

Return True if a equals b.

a |==| b

(|>|) :: Pattern Scalar -> Pattern Scalar -> Pattern BooleanSource

Return True if a is greater than b.

a |>| b

(|>=|) :: Pattern Scalar -> Pattern Scalar -> Pattern BooleanSource

Return True if a is greater than or equal to b.

a |>=| b

(|<|) :: Pattern Scalar -> Pattern Scalar -> Pattern BooleanSource

Return True if a is smaller than b.

a |<| b

(|<=|) :: Pattern Scalar -> Pattern Scalar -> Pattern BooleanSource

Return True if a is smaller than or equal to b.

a |<=| b

Coordinates

coord :: Pattern Scalar -> Pattern Scalar -> Pattern CoordSource

Construct a coordinate pattern from a pair of scalar patterns, one for each axis.

coord 0.5 (rand 0.4 0.6)

polar :: Pattern Coord -> Pattern Scalar -> Pattern Scalar -> Pattern CoordSource

Construct a coordinate pattern from a center coordinate, a radius and an angle in radians.

polar (coord 0.5 0.5) 0.5 (pi/2)

x :: Pattern Coord -> Pattern ScalarSource

Retrieve the x value from a coordinate.

y :: Pattern Coord -> Pattern ScalarSource

Retrieve the y value from a coordinate.

Regions

center :: Int -> Pattern CoordSource

Return the center of the specified feature space region.

radius :: Int -> Pattern ScalarSource

Return the radius of the specified feature space region.

Event patterns

data Event Source

Event expression.

Events can be thought of as notes controlling the synthesizer. They are either silent, or rests, or carry parameter that affect the synthesis and the sequencer.

They carry the time to the next event in the stream in the Delta field.

Event generators

rest :: Double -> Pattern EventSource

Return a rest of the specified duration.

closest :: Int -> Pattern Scalar -> Pattern Scalar -> Pattern Coord -> Pattern EventSource

Select the unit closest to a given coordinate.

closest cursor defaultDelta radius coord
  • cursor Cursor id for the generated event.
  • defaultDelta Default delta time in case no unit is found.
  • radius Radius to restrict the search; if the found unit is outside the given radius around the coordinate, a rest is generated.
  • coord Coordinate for nearest neighbor search.

region :: Int -> Pattern Scalar -> Pattern Scalar -> Pattern EventSource

Select a unit from a region according to the iterator.

region cursor defaultDelta iterator
  • cursor Cursor and region id for this event.
  • defaultDelta Default delta time in case the region is empty.
  • iterator Scalar pattern in the range [0;1[ that selects a unit from the region.

Event accessors

data Field Source

Represents the various fields in a synthesis event.

Note that some fields can be queried but not modified (Cursor, CursorValue and Feature).

Constructors

Delta

Delta time to next event in seconds.

Cursor

Cursor id.

CursorValue

Value at this cursor.

Offset

Playback offset in seconds.

Duration

Duration of the event being played.

Level

Level between 0 and 1.

Pan

Panning coefficient in [-1;1].

Rate

Playback rate (> 0).

AttackTime

Envelope attack time in seconds.

ReleaseTime

Envelope release time in seconds.

SendLevel1

First send effect level.

SendLevel2

Second send effect level.

FXParam1

Parameter for first send effect.

FXParam2

Parameter for second send effect.

Feature Int Int

Feature value.

Instances

get :: Field -> Pattern Event -> Pattern ScalarSource

Return the value of an event field.

get Duration e

set :: Field -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Set the value of an event field

set Duration 0.1 e

Feature field accessors

fSpec :: Int -> FieldSource

Spectral feature at index 0 or 1.

fPower :: FieldSource

Power feature in dB.

fFreq :: FieldSource

Fundamental frequency feature in Hz.

Event modifiers

mapf :: UnaryFunc -> Field -> Pattern Event -> Pattern EventSource

Map a unary function to a field.

zipf :: BinaryFunc -> Pattern Scalar -> Field -> Pattern Event -> Pattern EventSource

Combine a scalar with a field value.

fzip :: BinaryFunc -> Field -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Combine a field value with a scalar.

add :: Field -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Add a scalar to a field value.

multiply :: Field -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Multiply a field value by a scalar.

Event filters

filter :: Pattern Boolean -> Pattern Event -> Pattern EventSource

Filter an event stream with the given boolean pattern.

Events for which the predicate returns True are replaced by rests.

bind (<event expression>) (\e -> filter (get Duration e |>| 0.5) e)

player :: Pattern Scalar -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Filter out events that don't have the cursor set.

stepPlayer rowInc colInc pattern
  • rowInc Cursor row increment.
  • colInc Cursor column increment.
  • pattern The pattern to filter.

sequencer :: Pattern Scalar -> Pattern Event -> Pattern EventSource

Given a tick increment in seconds filter the event pattern argument so that only cursor values greater than zero produce events.

sequencer tick pattern
  • tick Tick increment in seconds.
  • pattern The pattern to filter.

envelope :: Pattern Scalar -> Pattern Scalar -> Pattern Event -> Pattern EventSource

Set envelope attack and release time.

envelope attackTime releaseTime e

Stream functions

cycle :: Stream Pattern a => Pattern a -> Pattern aSource

Repeat a pattern indefinitely.

replicate :: Stream Pattern a => Int -> Pattern a -> Pattern aSource

Constant signal. constant, c :: Double -> Pattern Scalar constant = cycle . value c = constant

Repeat a pattern n times.

repeat n p

times :: Stream Pattern a => Pattern a -> Int -> Pattern aSource

Repeat a pattern n times.

This is the same as replicate with the arguments flipped in order to allow infix application:

p `times` 4

take :: Stream Pattern a => Int -> Pattern a -> Pattern aSource

Take the first n values of a pattern.

If the pattern is shorter, a smaller number of values is returned.

only :: Stream Pattern a => Pattern a -> Int -> Pattern aSource

Take the n initial values of a pattern repeated infinitely.

This is the same as take with the arguments flipped in order to allow infix application:

p `only` 4

restrict :: Stream Pattern a => Int -> Pattern a -> Pattern aSource

Take the n initial values of a pattern repeated infinitely.

restrict n p = cycle (take n p)

gimme :: Stream Pattern a => Pattern a -> Int -> Pattern aSource

Take the n initial values of a pattern repeated infinitely.

This is the same as restrict with the arguments flipped in order to allow infix application:

p `gimme` 4

once :: Stream Pattern a => Pattern a -> Pattern aSource

Take the first value of a pattern.

o :: Stream Pattern a => Pattern a -> Pattern aSource

Take the first value of a pattern.

Short for once.

Event streams

takeDur :: Double -> Pattern Event -> Pattern EventSource

Limit an event pattern to the first n seconds.

takeDur 2.125 e

par :: [Pattern Event] -> Pattern EventSource

Combine a list of event patterns by merging their event streams.

List patterns

seq :: List Pattern a => Pattern Scalar -> [Pattern a] -> Pattern aSource

Enumerate a list of patterns sequentially, n times.

seq n ps
  • n Number of repeats.
  • ps List of patterns.

seq1 :: (List Pattern a, Stream Pattern a) => Pattern Scalar -> [Pattern a] -> Pattern aSource

Enumerate a list of patterns sequentially, n times.

Similar to seq but applies once to each of the patterns, so that sequences of scalars can be written more conveniently:

seq1 (once 4) [0.5, 0.6, 0.8, 1.3]

ser :: List Pattern a => Pattern Scalar -> [Pattern a] -> Pattern aSource

Enumerate a list of patterns sequentially, returning n items.

ser n ps
  • n Number of items to return.
  • ps List of patterns.

ser1 :: (List Pattern a, Stream Pattern a) => Pattern Scalar -> [Pattern a] -> Pattern aSource

Enumerate a list of patterns sequentially, returning n items.

Similar to ser but applies once to each of the patterns, so that sequences of scalars can be written more conveniently:

ser1 (once 7) [0.5, 0.6, 0.8, 1.3]

choose :: List Pattern a => Pattern Scalar -> [Pattern a] -> Pattern aSource

Returns one item from the list at random for each repeat.

choose n ps
  • n Number of repeats.
  • ps List of patterns.

choose1 :: (List Pattern a, Stream Pattern a) => Pattern Scalar -> [Pattern a] -> Pattern aSource

Returns one item from the list at random for each repeat.

Similar to choose but applies once to each of the patterns, so that sequences of scalars can be written more conveniently:

choose1 (once 7) [seq1 (once 1) [0 1], 0.6, 0.8, 1.3]

chooseNew :: List Pattern a => Pattern Scalar -> [Pattern a] -> Pattern aSource

Returns one item from the list at random for each repeat.

Similar to choose but doesn't return the same item twice in a row.

chooseNew1 :: (List Pattern a, Stream Pattern a) => Pattern Scalar -> [Pattern a] -> Pattern aSource

Returns one item from the list at random for each repeat.

Similar to chooseNew but but applies once to each of the patterns.

Bindings

bind :: Bind Pattern a b => Pattern a -> (Pattern a -> Pattern b) -> Pattern bSource

Bind the current value of a pattern to a function argument.

This function is needed to express value sharing (as opposed to expression sharing) in the pattern language.

bind (<event pattern expression>) (\e -> set Delta (get Duration e) e)

Constants

data Limit Source

Interval boundary behavior.

Constructors

Clip

Clip to interval.

Wrap

Wrap to other end of interval.

Fold

Fold back into interval.

Instances

Debugging

trace :: Trace Pattern a => Pattern a -> Pattern aSource

Print the current value of the argument pattern to the console.