Skip to content

Env

Envelope generator module.

This module provides an envelope generator class that can create complex envelopes with multiple segments, curves, and looping capabilities.

Structs

struct EnvParams

Parameters for the Env class.

This struct defines the parameters of an envelope. An instance of this struct is used internally in the Env struct. It is also used in other places in the library when appropriate.

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable


EnvParams Functions

struct EnvParams . fn init

Initialize EnvParams.

fn init Signature

def __init__(out self, values: List[Float64] = List(Float64("0"), Float64("1"), Float64("0"), __list_literal__=NoneType(None)), times: List[Float64] = List(Float64("1"), Float64("1"), __list_literal__=NoneType(None)), curves: List[Float64] = List(Float64("1"), __list_literal__=NoneType(None)), loop: Bool = False, time_warp: Float64 = 1)

fn init Arguments

Name Type Default Description
values List[Float64] List(Float64("0"), Float64("1"), Float64("0"), __list_literal__=NoneType(None)) List of envelope values at each breakpoint.
times List[Float64] List(Float64("1"), Float64("1"), __list_literal__=NoneType(None)) List of durations (in seconds) for each segment between adjacent breakpoints. This List should be one element shorter than the values List.
curves List[Float64] List(Float64("1"), __list_literal__=NoneType(None)) List of curve shapes for each segment. Positive values for convex "exponential" curves, negative for concave "logarithmic" curves. (if the output of the envelope is negative, the curve will be inverted).
loop Bool False Bool to indicate if the envelope should loop.
time_warp Float64 1 Time warp factor to speed up or slow down the envelope. Default is 1.0 meaning no warp. A value of 2.0 will make the envelop take twice as long to complete. A value of 0.5 will make the envelope take half as long to complete.

fn init Returns : Self

Static Method

This is a static method.

struct Env

Envelope generator with an arbitrary number of segments.

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable


Env Functions

struct Env . fn init

Initialize the Env struct.

fn init Signature

def __init__(out self, world: UnsafePointer[MMMWorld, MutUntrackedOrigin])

fn init Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.

fn init Returns : Self

Static Method

This is a static method.

struct Env . fn next

Generate the next envelope value. Uses the internal params struct for envelope parameters. See EnvParams for more details on the parameters. Uses an internal phasor to track the progression of the envelope, which is reset on each trigger.

fn next Signature

def next[win_type: WindowType = WindowType.none, interp: Interp = Interp.none](mut self, trig: Bool = True) -> Float64

fn next Parameters

Name Type Default Description
win_type WindowType WindowType.none Used to apply a window type to the envelope curves. Default is WindowType.none, which means linear ramps. See WindowType struct for available window types.
interp Interp Interp.none Interpolation type to apply to the window. Default is Interp.none, which means no interpolation. See Interp struct for available interpolation types.

fn next Arguments

Name Type Default Description
trig Bool True Trigger to start the envelope.

fn next Returns : Float64 The next envelope value.

struct Env . fn next

Generate the next envelope value with a provided phase rather than using the internal phasor. Works well with a Line UGen. Uses the internal params struct for envelope parameters. See EnvParams for more details on the parameters.

fn next Signature

def next[win_type: WindowType = WindowType.none, interp: Interp = Interp.linear](mut self, trig: Bool, phase: Float64) -> Float64

fn next Parameters

Name Type Default Description
win_type WindowType WindowType.none Used to apply a window type to the envelope curves. Default is WindowType.none, which means linear ramps. See WindowType struct for available window types.
interp Interp Interp.linear Interpolation type to apply to the window. Default is Interp.linear. See Interp struct for available interpolation types.

fn next Arguments

Name Type Default Description
trig Bool Trigger to start the envelope.
phase Float64 The current phase of the envelope, between 0 and 1. This allows the user to control the progression of the envelope externally rather than using the internal phasor.

fn next Returns : Float64 The envelope value at the specified phase.

struct Env . fn get_phase

Get the current phase of the envelope.

fn get_phase Signature

def get_phase(self) -> Float64

fn get_phase Returns : Float64 The current envelope phase, clamped to the range 0 to 1.

struct Env . fn get_env_buffer

Get a SIMDBuffer of envelope values.

fn get_env_buffer Signature

def get_env_buffer[num_chans: Int = 1, win_type: WindowType = WindowType.none, interp: Interp = Interp.linear](world: UnsafePointer[MMMWorld, MutUntrackedOrigin], size: Int, *env_defs: EnvParams) -> SIMDBuffer[num_chans]

fn get_env_buffer Parameters

Name Type Default Description
num_chans Int 1 Number of SIMD channels in the output buffer.
win_type WindowType WindowType.none A WindowType to apply to the envelope. See WindowType struct for available window types.
interp Interp Interp.linear Interpolation mode used when reading the window. See Interp struct for available interpolation types.

fn get_env_buffer Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.
size Int Size of the output table.
*env_defs EnvParams One or more EnvParams structs defining the envelope parameters to generate the table from.

fn get_env_buffer Returns : SIMDBuffer[num_chans] A SIMDBuffer containing num_chans of envelope values.

Static Method

This is a static method.

struct ASREnv

Simple ASR envelope generator.

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable


ASREnv Functions

struct ASREnv . fn init

Initialize the ASREnv struct.

fn init Signature

def __init__(out self, world: UnsafePointer[MMMWorld, MutUntrackedOrigin])

fn init Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.

fn init Returns : Self

Static Method

This is a static method.

struct ASREnv . fn next

Simple ASR envelope generator.

fn next Signature

def next[win_type: WindowType = WindowType.none, interp: Interp = Interp.none](mut self, attack: Float64, sustain: Float64, release: Float64, gate: Bool, curve: SIMD[DType.float64, 2] = SIMD[DType.float64, 2]("1")) -> Float64

fn next Parameters

Name Type Default Description
win_type WindowType WindowType.none Used to apply a window type to the envelope curves. Default is WindowType.none, which means linear ramps. See WindowType struct for available window types.
interp Interp Interp.none Interpolation type to apply to the window. Default is Interp.none. See Interp struct for available interpolation types.

fn next Arguments

Name Type Default Description
attack Float64 Attack time in seconds.
sustain Float64 Sustain level (0 to 1).
release Float64 Release time in seconds.
gate Bool Gate signal (True or False).
curve SIMD[DType.float64, 2] SIMD[DType.float64, 2]("1") Can pass a Float64 for equivalent curve on rise and fall or MFloat[2] for different rise and fall curve. Positive values for convex "exponential" curves, negative for concave "logarithmic" curves.

fn next Returns : Float64 The current ASR envelope value.

struct Compressor

Compressor from Nathan Ho's Negative Compression web post.

Params: num_chans: Number of channels of input/output. ov_samp: TimesOversampling factor for oversampling the input signal before compression.

Traits: AnyType, Copyable, ImplicitlyDeletable, Movable

Compressor Parameters

Name Type Default Description
num_chans Int
ov_samp TimesOversampling TimesOversampling.none

Compressor Functions

struct Compressor . fn init

Initialize the Compressor struct.

fn init Signature

def __init__(out self, world: UnsafePointer[MMMWorld, MutUntrackedOrigin])

fn init Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.

fn init Returns : Self

Static Method

This is a static method.

struct Compressor . fn next

Returns the compressed signal, which is the original signal multiplied by the negative compression gain.

fn next Signature

def next(mut self, input: SIMD[DType.float64, num_chans], threshold: Float64 = -20, ratio: Float64 = 4, attack: Float64 = 0.01, release: Float64 = 0.10000000000000001, knee_width: Float64 = 0) -> SIMD[DType.float64, num_chans]

fn next Arguments

Name Type Default Description
input SIMD[DType.float64, num_chans] MFloat[Self.num_chans] audio signal to be compressed.
threshold Float64 -20 MFloat[1] threshold in dBFS above which compression occurs.
ratio Float64 4 MFloat[1] compression ratio. For example, a ratio of 4.0 means that for every 4 dB the input signal exceeds the threshold, the output will only exceed the threshold by 1 dB.
attack Float64 0.01 MFloat[1] attack time in seconds for the compressor's envelope follower.
release Float64 0.10000000000000001 MFloat[1] release time in seconds for the compressor's envelope follower.
knee_width Float64 0 MFloat[1] width of the knee in dB. A value of 0 means a hard knee, while higher values create a softer knee N/2 decibels around the threshold.

fn next Returns : SIMD[DType.float64, num_chans] MFloat[Self.num_chans] compressed audio signal, which is the original signal multiplied by the negative compression gain.

struct Compressor . fn next_neg_comp

Returns the negative compression gain (in amplitude units, not dB) for the input signal, which can be multiplied with the original signal or used as a sidechain.

fn next_neg_comp Signature

def next_neg_comp(mut self, input: SIMD[DType.float64, num_chans], threshold: Float64 = -20, ratio: Float64 = 4, attack: Float64 = 0.01, release: Float64 = 0.10000000000000001, knee_width: Float64 = 0) -> Float64

fn next_neg_comp Arguments

Name Type Default Description
input SIMD[DType.float64, num_chans] MFloat[Self.num_chans] audio signal to be compressed.
threshold Float64 -20 MFloat[1] threshold in dBFS above which compression occurs.
ratio Float64 4 MFloat[1] compression ratio. For example, a ratio of 4.0 means that for every 4 dB the input signal exceeds the threshold, the output will only exceed the threshold by 1 dB.
attack Float64 0.01 MFloat[1] attack time in seconds for the compressor's envelope follower.
release Float64 0.10000000000000001 MFloat[1] release time in seconds for the compressor's envelope follower.
knee_width Float64 0 MFloat[1] width of the knee in dB. A value of 0 means a hard knee, while higher values create a softer knee N/2 decibels around the threshold.

fn next_neg_comp Returns : Float64 MFloat[1] negative compression gain in amplitude units, which can be multiplied with the original signal or used as a sidechain.

Functions

(Functions that are not associated with a Struct)

fn env

Creates an envelope by linearly mapping an input value x based on a series of input-output points. This is just a function, so it should be paired with a Line or Phasor UGen to create an envelope generator.

The function takes a variable number of (input, output) pairs and linearly maps the input x to the corresponding output value based on which segment of the input range x falls into. If x is outside the range of the provided points, it will be clamped to the nearest segment.

There are optional parameters and arguments for applying a curve to the segments. See below for some possibilities:

env_points = InlineArray[Tuple[Float64, Float64], 4][(0.0, 0.0), (0.01, 1.0), (0.9, 1.0), (1.0, 0.0)]
env(world, x, env_points) # standard linear envelope
env(world, x, env_points, curve=2.0) # exponential curve
env(world, x, env_points, curve=0.5) # logarithmic curve
env[win_type=WindowType.sine, interp=Interp.linear](world, x, env_points) # standard envelope with sine shaped edges

Signature

def env[win_type: WindowType = WindowType.none, interp: Interp = Interp.none](world: UnsafePointer[MMMWorld, MutUntrackedOrigin], x: Float64, points: Span[Tuple[Float64, Float64]], curve: Float64 = 1) -> Float64

Parameters

Name Type Description
win_type WindowType An optional WindowType that specifies if the output should be processed through a window function. Default is WindowType.none (no windowing). If a window type is specified, the output values in the points should be in the range [0, 1] as they will be treated as positions within the window.
interp Interp An optional Interp that specifies the type of interpolation to use if win_type is not none. Default is Interp.none (no interpolation).

Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] The World object, required if win_type is not none for window processing.
x Float64 The input value to be mapped.
points Span[Tuple[Float64, Float64]] A variable number of (input, output) pairs that define the envelope. The first value is the input breakpoint, and the second value is the corresponding output value. The input breakpoints must be in ascending order.
curve Float64 1 A Float64 value that applies a curve to the interpolation between points. A value of 1.0 means no curve (linear), values greater than 1.0 will create a more exponential curve, and values between 0 and 1 will create a logarithmic curve.

Returns

Type: Float64 The mapped output value corresponding to the input x.

fn win_read

Reads a window function from MMMWorld's default Window by indexing into it with the provided phase.

Signature

def win_read[window_type: WindowType = WindowType.sine, interp: Interp = Interp.none](world: UnsafePointer[MMMWorld, MutUntrackedOrigin], phase: Float64) -> Float64

Parameters

Name Type Description
window_type WindowType The type of window to look up. See WindowType struct for available window types.
interp Interp Interpolation type to apply to the window. Default is Interp.none, which means no interpolation. See Interp struct for available interpolation types.

Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.
phase Float64 The current phase of the window, between 0 and 1.

Returns

Type: Float64 The window value at the specified phase.

fn buf_read

Reads a buffer by indexing into it with the provided phase.

Signature

def buf_read[num_chans: Int, interp: Interp = Interp.linear, bWrap: Bool = True](world: UnsafePointer[MMMWorld, MutUntrackedOrigin], env_buffer: SIMDBuffer[num_chans], phase: Float64, prev_phase: Float64 = 0) -> SIMD[DType.float64, num_chans]

Parameters

Name Type Description
num_chans Int Number of SIMD channels stored in the buffer.
interp Interp Interpolation mode used for buffer lookup. See Interp struct for available interpolation types.
bWrap Bool Whether the read should wrap around the buffer edges.

Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.
env_buffer SIMDBuffer[num_chans] The buffer containing the envelope values.
phase Float64 The phase at which to read the buffer, between 0 and 1.
prev_phase Float64 0 The previous phase, used for sinc interpolation. Otherwise, this argument can be ignored.

Returns

Type: SIMD[DType.float64, num_chans] The envelope value at the specified phase.

fn min_env

Simple envelope.

Envelope that creates a simple trapezoidal envelope with linear ramps. The attack and release ramps are determined by the ramp_amount parameter.

Signature

def min_env[win_type: WindowType = WindowType.none, interp: Interp = Interp.none](world: UnsafePointer[MMMWorld, MutUntrackedOrigin], phase: Float64 = 0, ramp_amount: Float64 = 0.01) -> Float64

Parameters

Name Type Description
win_type WindowType Used to apply a window type to the envelope curves. Default is WindowType.none, which means linear ramps. See WindowType struct for available window types.
interp Interp Interpolation type to apply to the window. Default is Interp.none, which means no interpolation. See Interp struct for available interpolation types.

Arguments

Name Type Default Description
world UnsafePointer[MMMWorld, MutUntrackedOrigin] Pointer to the MMMWorld.
phase Float64 0 Current env position between 0 (beginning) and 1 (end).
ramp_amount Float64 0.01 Duration of the attack and release ramps as a proportion of the total envelope duration. Values between 0 and 0.5.

Returns

Type: Float64 Envelope value at the current ramp position.


Documentation generated with mojo doc from Mojo version 1.0.0b2