pink.util

Audio utility code for working with buffers (double[])

!*!

(!*! func & args)
Returns a reference (instance of IDeref) that when deref'd, will call the 
given function and args. Note that apply!*! is used to call the function
with the arguments, so arguments that are instances of IDeref (i.e. atoms)
will first be derefenced before the function is applied.

!r!

(!r! r)
Returns an IDeref that when derefed, returns the r-argument.  Used for
passing atoms and other references as arguments for events, so that the event
function will receive the atom/ref itself and not its value.  (This is due to
events using apply!*!, which derefs any args before applying its function.)

add2

(add2 & a)

Allocator

protocol

members

acquire-alloc!

(acquire-alloc! x)
Returns true if alloc is available. Note: may update current alloc num when called.

num-allocs

(num-allocs x)
Returns number of current allocs.

reset-allocs!

(reset-allocs! x)
Reset allocs to 0.

with-allocator

(with-allocator x afn)
Decorates afn with dealloc-on-done afn, which decrements alloc count when afn is done.

apply!*!

(apply!*! func)(apply!*! func args)(apply!*! func arg & args)

apply-stereo

(apply-stereo func afn & args)
Applies function func individually to each channel of a stereo audio function. 
Merges result back into a stereo audio function signal.

arg

(arg a)
Utility function to pass through if it is a function, or
wrap within a const if it is a number

box-val

macro

(box-val v)

buffer-channel-count

(buffer-channel-count buffer)
Get the channel count for a buffer

clear-buffer

(clear-buffer b)
Resets double-array to 0.0. Works with double[] and double[][].

concat-drain!

macro

(concat-drain! v r)

const

(const a)
Initializes a *buffer-size*-sized buffer with the given value,
returns a function that will return that buffer on each call

create-buffer

(create-buffer)(create-buffer i)
Creates a single-channel audio buffer with optional default value

create-buffers

(create-buffers nchnls)
Creates a single-channel or multi-channel buffer

create-max-allocator

(create-max-allocator max-allocs)
Keeps track of current allocations for limiting max-number. Warning: Depends on audio functions
exiting by returning nil to keep track of deallocations. .

dealloc-on-done

(dealloc-on-done atm afn)

div

(div & a)

div2

(div2 & a)

drain-atom!

macro

(drain-atom! a)

duration-processor

(duration-processor dur done-arr afn)

EMPTY-BUFFER

fill

macro

(fill out start f)
Fills double[] buf with values. Initial value is set to value from double[1] start, 
then f called like iterate with the value.  Last value is stored back into the start.
Returns buf at end.

gen-recur

macro

(gen-recur & args)

generator

macro

(generator bindings afn-bindings body yield-form)
Creates an audio-function. 
* Bindings are for values that will be automatically saved and loaded between calls
* afn-bindings are for setting var name to use when indexing a sample from the buffer generated
by the audio-function
* body should do processing and recur with newly calculated values
* yield-form should be (yield value-to-return)

get-channel

(get-channel afn index)
Returns a single channel from a multi-channel signal, generated by afn. 
Checks if afn returns nil. Note: afn should be wrapped with pink.util/shared 
macro if being used by muliple get-channels.

getd

macro

(getd a)

getl

macro

(getl a)

hold-until

(hold-until delay-time start-value end-value)
Audio function that emits a given start value for duration, then
emits end-value. duration is given in seconds. end-value may be a
double or audio function; if the latter, the audio function will be
called to process once the hold time is complete.

is-done?

(is-done? done)

let-s

macro

(let-s bindings & body)
Macro that decorates bindings with (shared) to simplify instrument building.

limit

(limit afn min-val max-val)
Clamp values asig betwen min-val and max-val (block-based audio function)

limit1

(limit1 v min-val max-val)
Clamps value v betwen min-val and max-val

map-d

(map-d out f x)(map-d out f x y)(map-d out f x y z)(map-d out f x y z a)(map-d out f x y z a b)(map-d out f x y z a b c)
Maps function f across double[] buffers and writes output to out buffer

map-d-impl

macro

(map-d-impl out f & buffers)

merge-signals

(merge-signals afn1 afn2)
Merge output of two audio functions into a single stereo signal.

mix-buffers

(mix-buffers src dest)
Mix src audio buffer into dest audio buffer, taking into account 
differences in channel counts

mul

(mul & a)

mul2

(mul2 & a)

MULTI-CHANNEL-TYPE

multi-channel?

(multi-channel? buffer)
Returns if buffer is multi-channel

native-operator

macro

(native-operator f a)

not==

macro

(not== a b)
Use for fast comparison of double != double, or long != long. 
Based on Zach Tellman's primitive-math not==

operator

macro

(operator f a)
takes in func and list of generators funcs, map operator across the result buffers

pole2tau

(pole2tau pole)

reader

(reader atm)
Returns function that reads from atom and returns a buffer. Useful for mutable data derived from external source such as MIDI or OSC

setd!

macro

(setd! a v)

setl!

macro

(setl! a v)

shared

(shared afn)
Wraps an audio function so that it only generates values once per buffer-size block; uses 
*curent-buffer-num* dynamic variable to track if update is required

sub

(sub & a)

sub2

(sub2 & a)

sum

(sum & a)

swapd!

macro

(swapd! d f)

swapl!

macro

(swapl! l f)

tag-double

(tag-double a)

tag-doubles

(tag-doubles a)

tag-long

(tag-long a)

tag-longs

(tag-longs a)

tagit

(tagit a t)

tau2pole

(tau2pole tau)

try-func

(try-func f)
Trys to call a function, returns the func's return value or
nil if an exception was caught.

with-buffer-size

macro

(with-buffer-size buffer-size & bindings)
Run code with given buffer-size. Uses binding to bind *buffer-size* during 
initialization-time as well as performance-time. Returns an audio function
that will appropriately fill a buffer of *buffer-size* size with repeated calls 
to the code of buffer-size size.

with-duration

macro

(with-duration dur body)

with-signals

macro

(with-signals [sigsvec afn] & body)
Destructures signals from multi-channel audio func. Returns an audio function.
Does wrap afn with shared macro so that afn generates only once per buffer time.

Example usage:

(with-signals [[hp lp _ _] (statevar afn 5000 0.8) ]
(some-calc hp lp))