Skip to content

📼 sampler

sampler

Info

The sampler module can be used on loops running on tracks with instrument type set to sampler.

The sampler module provides a way to load samples and play them inside loops in an intuitive way. Once instantiated, a Sampler can be used to play samples from the selected pack given their name. If no exact match of the sample name is found, the first matching sample is selected. The cost of creating and using a sampler is cheap so it is fine to have a lot of instances at once.

Cookbook

Play samples

s = sampler.new('808')

@loop
def kick(beats=4):
  for i in range(4):
    s.play('kick')
    sleep(1)

List available packs

packs = sampler.packs()

List samples in a pack

samples = sampler.samples('808')

Reference

Sample(name, pack, path, duration) dataclass

A sample is a sound file that can be played by a sampler. It has a name, a pack, a path, and a duration. The name is the identifier used to play the sample, the pack is the name of the pack the sample belongs to, the path is the location of the sample file, and the duration is the length of the sample in seconds.

Attributes:

Name Type Description
name str

The name of the sample.

pack str

The name of the pack.

path str

The location of the sample file.

duration float

The length of the sample in seconds.

Sampler(pack_name)

Creates a new sampler with samples from the designated pack.

Note

You can create a Sampler via the shortcut n.sampler.new().

Parameters:

Name Type Description Default
pack_name str

The name of the sample pack to use.

required

play(name, start=0.0, end=1.0, pan=0.0, attack=0.0, decay=0.0, sustain=None, level=1.0, release=0.0, rate=1.0, amp=1.0)

Plays a sample by its given name. If there is no exact match, attempts to find one that contains the name (for example, 'kick' will match 'hard-kick'). If the selected sample is already being played, enqueues a new one, allowing to play simultaneously multiple times the same sample.

Parameters:

Name Type Description Default
name str

The name of the sample.

required
start float

When in the sample to start playing in the [0.0, 1.0] range.

0.0
end float

When in the sample to end playing in the [0.0, 1.0] range.

1.0
pan float

The panning of the sample in the [-1.0, 1.0] range.

0.0
attack float

The attack time in seconds.

0.0
decay float

The decay time in seconds.

0.0
sustain float | None

The sustain time in seconds, infered from the sample duration if None.

None
release float

The release time in seconds.

0.0
level float

The sustain level in the [0.0, 1.0] range.

1.0
rate float

The playback rate of the sample.

1.0
amp float

The amplitude of the sample.

1.0

stop(name)

Stops playing the sample. If there is no exact match, attempts to find one that contains the name (for example, 'kick' will match 'hard-kick'). If the same sample is currently played multiple times, the latest one is selected to stop (LIFO).

Parameters:

Name Type Description Default
name str

The name of the sample.

required

new(pack_name)

Creates a new sampler with samples from the designated pack.

Parameters:

Name Type Description Default
pack_name str

The name of the sample pack to use.

required

packs()

Returns the list of available sample packs.

Returns:

Type Description
list[str]

The list of loaded sample packs.

samples(pack_name)

Returns the list of samples available in the given pack.

Parameters:

Name Type Description Default
pack_name str

The name of the sample pack.

required

Returns:

Type Description
list[Sample]

The list of samples from the sample pack.