SAPF, some mystery functions (to me)

‘tempo’, ‘beats’, ‘pack’, ‘packed’

What is ‘tempo’ used for?

Is ‘beats’ merely used a convenience function for the output of ‘tempo’?

Is the output of ‘tempo’ what’s called for in the argument list of, let’s say adsr:

sapf> `adsr help
@akkz ([attack decay sustain release] amp dur tempo --> envelope) an envelope generator.
sapf>

What is a ‘packed list’?

             Thank You For Your Attention

Tempo

sapf> `tempo help
@az ([bps dur bps dur ...] rate --> out) returns a signal of tempo vs time given a list of interleaved tempos (in beats per second) and durations (in beats).

The tempo function produces a signal of a series of ramps of tempo in beats per second over the duration that those ramps would take.
So [2 16 2] 1 tempo will return a signal which has the value of 2 (in beats per second (bps)) for 16 beats, which is 768000 samples. The duration in samples of a number of beats at a constant tempo is samples = beats / tempo * sampleRate or 76800 = 16 / 2 * 96000 (96000 is the default sample rate, it will differ if you run at a different sample rate).

And [2 16 1] 1 tempo return a signal which ramps from 2 bps down to 1 bps. Since the tempo is slowing, 16 beats takes more samples than in the first example, so the length is 1064674 samples. The formula for the sample duration of a tempo ramp is given by this formula: beats * ln(endTempo/startTempo)/(endTempo - startTempo) * sampleRate

You can have any number of ramps by interleaving bps and beats. A step in tempo can be expressed by having an endpoint be zero beats long and starting a new segment. For example: [2 16 2 0 1 16 1] 1 tempo returns a signal which has value 2 bps for 16 beats (768000 samples) and value 1 bps for 1536000 samples for a total of 2304000 samples.

Beats

sapf> `beats help
@z (tempo --> beats) integrates a tempo signal to produce a signal of the time in beats.

The beats function integrates tempo.
[2 16 2] 1 tempo beats
The above integrates the tempo signal and returns the beat position of each sample.

Adsr

The tempo parameter to adsr can be a constant value or a signal in beats per second. Envelope segments are timed in beats - which equals seconds if tempo equals 1 beat per second.

Pack

A packed list is a list that has been made into an array. Sapf uses chunky linked lists as its primary data structure. By default, value lists have a chunk size of one and signals have a chunk size of 512.

const int kDefaultVBlockSize = 1;
const int kDefaultZBlockSize = 512;

Some algorithms require arrays instead of lists, and to convert a list into an array, the entire list is packed into one chunk. pack performs this operation, and packed reports whether a list is packed or not.
Literal lists are packed:
[1 2 3] packed returns 1
Computed lists may not be packed.
ord 3 N returns [1 2 3]
but
ord 3 N packed returns 0
but you can pack it
ord 3 N pack packed returns 1

3 Likes