API Reference

Clip

class tenseur.core.clip.Clip(tensor: ndarray[tuple[Any, ...], dtype[float64]], _track: int = 1, name: str = '', channel: int = 0, bar_offset: int = 0, bars: float | None = None)[source]

A musical clip wrapping a (V, B, S, 4) tensor.

tensor

Note data of shape (V, B, S, 4).

Type:

numpy.ndarray[tuple[Any, …], numpy.dtype[numpy.float64]]

track[source]

Track number for rendering (1-based).

name

Human-readable label.

Type:

str

channel

MIDI channel (0-15).

Type:

int

bar_offset

Bar offset for positioning in a timeline.

Type:

int

bars

Number of bars for rendering. Computed automatically.

Type:

float | None

linear_pitch(scale: float = 1.0, offset: float = 0) → Clip[source]

Affine transform on pitch: pitch = pitch * scale + offset.

Returns self for chaining.

scale_pitch(factor: float) → Clip[source]

Multiply all pitches by a factor.

Returns self for chaining.

transpose(semitones: float) → Clip[source]

Shift all pitches by a number of semitones.

Returns self for chaining.

project_pitch(scale: PitchSet) → Clip[source]

Quantize active pitches to the nearest note in a scale.

Parameters:

scale – Allowed MIDI pitch values.

Returns:

self (for chaining).

humanize(velocity: float = 0.04, timing: float = 0.05) → Clip[source]

Add humanization noise to velocity and timing.

Parameters:
  • velocity – Std-dev of noise added to velocity.

  • timing – Std-dev of noise added to start times.

Returns:

self (for chaining).

quantize(grid: float = 1.0) → Clip[source]

Snap start times to the nearest grid point.

Parameters:

grid – Grid resolution in steps (1.0 = integer steps, 0.5 = half steps).

Returns:

self (for chaining).

rgate(probability: float = 0.5) → Clip[source]

Randomly silence active notes.

Each active note has a probability chance of passing through. Uses the global RNG (controlled via seed()).

Parameters:

probability – Chance of keeping each note (0 = silence all, 1 = keep all).

Returns:

self (for chaining).

sparsify(priority: list[int] | ndarray | None = None) → Clip[source]

Keep only the highest-priority voice at each step.

When multiple voices are active at the same (bar, step) position, only the one with the highest priority (earliest in the list) is kept.

Parameters:

priority – Voice indices in priority order (first = highest). Defaults to [0, 1, 2, …].

Returns:

self (for chaining).

speed(factor: float) → Clip[source]

Speed up start times and durations by factor. Returns self.

Parameters:

factor – Speed multiplier (2 = twice as fast, 0.5 = twice as slow).

Returns:

self (for chaining).

show(view: float | str = 25, azim: float = -60) → Clip[source]

Visualize the tensor as 3D scatter and save to PNG.

Parameters:
  • view – Preset name (“step”, “bar”, “top”) or elevation angle.

  • azim – Azimuth angle in degrees (ignored when view is a string).

Saves to track_{track}_{name}.png (or track_{track}.png if unnamed). Returns self for chaining.

render(live: tenseur.backends.ableton.Live) → Clip[source]

Push this clip to Ableton Live via a Live connection.

Parameters:

live – An open Live instance.

Returns:

self (for chaining).

Tensor Creation

tenseur.helpers.create.simple_tensor(size: int | tuple[int, int] | tuple[int, int, int], pitch: float = 0.0, velocity: float = 0.0, duration: float = 1.0, steps_per_bar: int | None = None) → ndarray[tuple[Any, ...], dtype[float64]][source]

Create a tensor filled with uniform note values.

Parameters:
  • size – Shape specification: - int S -> (1, 1, S) - (V, S) -> (V, 1, S) - (V, B, S) -> (V, B, S)

  • pitch – MIDI pitch value (0-127).

  • velocity – Normalized velocity (0-1). 0 means inactive.

  • duration – Note duration in steps.

  • steps_per_bar – Steps per bar for start-time computation. Defaults to S (the last dimension).

Returns:

Tensor of shape (V, B, S, 4) with dtype float64.

tenseur.helpers.create.drum_tensor(size: tuple[int, int, int] | list[int] = (16, 4, 16), pitch: float = 4.0, velocity: float = 0.0, duration: float = 0.5, kit: float = 0.0, steps_per_bar: int | None = None) → ndarray[tuple[Any, ...], dtype[float64]][source]

Create a drum tensor with one voice per pitch.

Each voice gets a sequential pitch starting from pitch: voice 0 = pitch, voice 1 = pitch+1, etc.

Parameters:
  • size – (voices, bars, steps).

  • pitch – Base MIDI pitch for voice 0 (default 36 = C2/kick).

  • velocity – Normalized velocity (0-1). 0 means inactive.

  • duration – Note duration in steps.

  • steps_per_bar – Steps per bar for start-time computation. Defaults to S (the last dimension).

Returns:

Tensor of shape (V, B, S, 4).

tenseur.helpers.create.euclidean(pulses: int, steps: int | tuple[int, ...], rotation: int = 0) → ndarray[source]

Generate a Euclidean rhythm pattern.

Distributes pulses hits as evenly as possible across steps slots by spacing ideal positions linearly and projecting onto the grid.

Parameters:
  • pulses – Number of active hits.

  • steps – Number of steps (int), or a shape tuple. - int S -> (S,) - (S,) -> (S,) - (B, S) -> (B, S) pattern broadcast across rows - (V, B, S) -> (V, B, S) pattern broadcast across leading dims

  • rotation – Rotate the pattern to the right by this many steps.

Returns:

Boolean numpy array of the given shape.

tenseur.helpers.create.scatter(pulses: int, steps: int | tuple[int, ...]) → ndarray[source]

Place pulses hits randomly across steps slots.

Uses the global RNG (controlled via seed()).

Parameters:
  • pulses – Number of active hits per row.

  • steps – Number of steps (int), or a shape tuple. - int S -> (S,) - (S,) -> (S,) - (B, S) -> (B, S) with independent draws per row - (V, B, S) -> (V, B, S) with independent draws per row

Returns:

Boolean numpy array of the given shape.

Scales

tenseur.helpers.scales.generate_scale(divisions: int | float = 12, root: int = 60, offset: float = 0.0) → list[int][source]

Generate a pitch set from equal division of the octave (EDO).

Divides the octave into divisions equal parts and returns all resulting MIDI pitches in the valid range [0, 127].

Parameters:
  • divisions – Number of equal divisions per octave. 12 = chromatic (12-TET), 7 = roughly diatonic, 5 = roughly pentatonic, 19 = microtonal, etc.

  • root – Root pitch as MIDI number (default 60 = C4).

  • offset – Pitch offset in semitones (for detuning/shifting).

Returns:

Sorted list of MIDI pitch values (int), usable with quantize_pitch.

Examples

>>> generate_scale(12)          # chromatic
>>> generate_scale(7, root=60)  # ~diatonic from C4
>>> generate_scale(5, offset=-0.15)  # detuned pentatonic

Transforms

tenseur.helpers.transforms.sine(t: ndarray, freq: float = 1.0, amp: float = 1.0, offset: float = 0.0) → ndarray[source]

Sine wave over time values.

Parameters:
  • t – Time array (typically start times from a tensor slice).

  • freq – Frequency in cycles per step.

  • amp – Amplitude.

  • offset – DC offset.

Returns:

offset + amp * sin(2π * freq * t)

tenseur.helpers.transforms.swing(tensor: ndarray[tuple[Any, ...], dtype[float64]], amount: float = 0.5) → ndarray[tuple[Any, ...], dtype[float64]][source]

Apply swing by shifting odd-indexed steps’ start times.

Parameters:
  • tensor – Input tensor of shape (V, B, S, 4).

  • amount – How much to shift odd steps (in fractional steps).

Returns:

New tensor with swing applied.

tenseur.helpers.transforms.upsample(tensor: ndarray[tuple[Any, ...], dtype[float64]], factor: int = 2) → ndarray[tuple[Any, ...], dtype[float64]][source]

Upsample by zero-stuffing along the step axis.

Inserts (factor - 1) silent steps after each original step. A factor of 2 doubles S, a factor of 3 triples it, etc.

Parameters:
  • tensor – Input tensor of shape (V, B, S, 4).

  • factor – Upsampling factor (must be >= 1).

Returns:

New tensor of shape (V, B, S * factor, 4) with zeros between original steps.

tenseur.helpers.transforms.quantize(values: ndarray, allowed: list[int] | list[float] | ndarray) → ndarray[source]

Snap every value to the nearest in an allowed set.

Works on any array shape — use it on a slice of a tensor or standalone.

Parameters:
  • values – Array of any shape.

  • allowed – Sorted list/array of allowed values.

Returns:

New array with each value snapped to the nearest allowed value.

tenseur.helpers.transforms.quantize_pitch(tensor: ndarray[tuple[Any, ...], dtype[float64]], pitch_set: list[int]) → ndarray[tuple[Any, ...], dtype[float64]][source]

Snap active pitches to the nearest value in a pitch set.

Convenience wrapper around quantize() for full (V, B, S, 4) tensors. Only affects active notes (vel > 0).

Parameters:
  • tensor – Input tensor of shape (V, B, S, 4).

  • pitch_set – Sorted list of allowed MIDI pitch values.

Returns:

New tensor with active pitches snapped to the nearest allowed value.

Random

tenseur.utils.random.seed(s: int | None = None) → None[source]

Reset the global RNG with a new seed.

tenseur.utils.random.normal(size: tuple[int, ...] = (16, 4, 16), scale: float = 1.0) → ndarray[source]

Draw normal noise with given shape and scale. Uses the global RNG.

tenseur.utils.random.bernoulli(size: tuple[int, ...] = (16, 4, 16), prob: float = 0.5) → ndarray[source]

Draw 0/1 values with given probability. Uses the global RNG.

tenseur.utils.random.shuffle(arr: ndarray) → ndarray[source]

Return a shuffled copy of the array (uses the global RNG).

Live Backend

class tenseur.backends.ableton.Live(index: int = 1, bpm: float | None = None, beats_per_bar: int = 4, steps_per_bar: int | None = None, create: bool = True, dry: bool = False, **kwargs: Any)[source]

Persistent connection to Ableton Live via OSC.

Opens the connection once at init; use push(clip) to send clips, and stop() when done.

Parameters:
  • index – Clip-slot number (1-based, default 1).

  • bpm – If set, change the song tempo on connect.

  • beats_per_bar – Beats per bar in Ableton (default 4).

  • steps_per_bar – Steps per bar (auto-detected from first clip if None).

  • create – Whether to create new clips in Ableton (default True).

  • **kwargs – hostname, port, client_port forwarded to AbletonOSCClient.

push(clip: Any) → None[source]

Send a single clip to Ableton Live. No-op in dry mode.

stop() → None[source]

Close the OSC connection. No-op in dry mode.