sc3nb.sc_objects.buffer

Module for using SuperCollider Buffers in Python

Module Contents

Class List

BufferReply

Buffer Command Replies

BufferCommand

Buffer OSC Commands for Buffers

BufferAllocationMode

Buffer Allocation Modes

BufferInfo

Information about the Buffer

Buffer

A Buffer object represents a SuperCollider3 Buffer on scsynth

Content

class sc3nb.sc_objects.buffer.BufferReply[source]

Bases: str, enum.Enum

Buffer Command Replies

Initialize self. See help(type(self)) for accurate signature.

INFO = '/b_info'[source]
class sc3nb.sc_objects.buffer.BufferCommand[source]

Bases: str, enum.Enum

Buffer OSC Commands for Buffers

Initialize self. See help(type(self)) for accurate signature.

ALLOC = '/b_alloc'[source]
ALLOC_READ = '/b_allocRead'[source]
ALLOC_READ_CHANNEL = '/b_allocReadChannel'[source]
READ = '/b_read'[source]
READ_CHANNEL = '/b_readChannel'[source]
WRITE = '/b_write'[source]
FREE = '/b_free'[source]
ZERO = '/b_zero'[source]
SET = '/b_set'[source]
SETN = '/b_setn'[source]
FILL = '/b_fill'[source]
GEN = '/b_gen'[source]
CLOSE = '/b_close'[source]
QUERY = '/b_query'[source]
GET = '/b_get'[source]
GETN = '/b_getn'[source]
class sc3nb.sc_objects.buffer.BufferAllocationMode[source]

Bases: str, enum.Enum

Buffer Allocation Modes

Initialize self. See help(type(self)) for accurate signature.

FILE = 'file'[source]
ALLOC = 'alloc'[source]
DATA = 'data'[source]
EXISTING = 'existing'[source]
COPY = 'copy'[source]
NONE = 'none'[source]
class sc3nb.sc_objects.buffer.BufferInfo[source]

Bases: NamedTuple

Information about the Buffer

bufnum: int[source]
num_frames: int[source]
num_channels: int[source]
sample_rate: float[source]
class sc3nb.sc_objects.buffer.Buffer(bufnum: int | None = None, server: sc3nb.sc_objects.server.SCServer | None = None)[source]

A Buffer object represents a SuperCollider3 Buffer on scsynth and provides access to low-level buffer commands of scsynth via methods of the Buffer objects.

The constructor merely initializes a buffer:

  • it selects a buffer number using the server’s buffer allocator

  • it initializes attribute variables

Parameters:
bufnumint, optional

buffer number to be used on scsynth. Defaults to None, can be set to enforce a given bufnum

serverSCServer, optional

The server instance to establish the Buffer, by default use the SC default server

Notes

For more information on Buffer commands, refer to the Server Command Reference in SC3. https://doc.sccode.org/Reference/Server-Command-Reference.html#Buffer%20Commands

Examples

(see examples/buffer-examples.ipynb)

>>> b = Buffer().load_file(...)
>>> b = Buffer().load_data(...)
>>> b = Buffer().alloc(...)
>>> b = Buffer().load_asig(...)
>>> b = Buffer().use_existing(...)
>>> b = Buffer().copy(Buffer)
Attributes:
serverthe SCServer object

to communicate with scsynth

_bufnumint

buffer number = bufnum id on scsynth

_srint

the sampling rate of the buffer

_channelsint

number of channels of the buffer

_samplesint

buffer length = number of sample frames

_alloc_modestr

[‘file’, ‘alloc’, ‘data’, ‘existing’, ‘copy’] according to previously used generator, defaults to None

_allocatedboolean

True if Buffer has been allocated by any of the initialization methods

_pathstr

path to the audio file used in load_file()

Overview:

read

Allocate buffer memory and read a sound file.

alloc

Allocate buffer memory.

load_data

Allocate buffer memory and read input data.

load_collection

Wrapper method of Buffer.load_data()

load_asig

Create buffer from asig

use_existing

Creates a buffer object from already existing Buffer bufnum.

copy_existing

Duplicate an existing buffer

fill

Fill range of samples with value(s).

gen

Call a command to fill a buffer.

zero

Set buffer data to zero.

gen_sine1

Fill the buffer with sine waves & given amplitude

gen_sine2

Fill the buffer with sine waves

gen_sine3

Fill the buffer with sine waves & given a list of

gen_cheby

Fills a buffer with a series of chebyshev polynomials, which can be

gen_copy

Copy samples from the source buffer to the destination buffer

play

Play the Buffer using a Synth

write

Write buffer data to a sound file

close

Close soundfile after using a Buffer with DiskOut

to_array

Return the buffer data as an array representation.

query

Get buffer info.

__repr__

Return repr(self).

free

Free buffer data.

_gen_flags

Generate Wave Fill Commands flags from booleans

read(path: str, starting_frame: int = 0, num_frames: int = -1, channels: int | Sequence[int] | None = None) Buffer[source]

Allocate buffer memory and read a sound file.

If the number of frames argument num_frames is negative or zero, the entire file is read.

Parameters:
pathstring

path name of a sound file.

starting_frameint

starting frame in file

num_framesint

number of frames to read

channelslist | int

channels and order of channels to be read from file. if only a int is provided it is loaded as only channel

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

alloc(size: int, sr: int = 44100, channels: int = 1) Buffer[source]

Allocate buffer memory.

Parameters:
sizeint

number of frames

srint

sampling rate in Hz (optional. default = 44100)

channelsint

number of channels (optional. default = 1 channel)

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

load_data(data: numpy.ndarray, sr: int = 44100, mode: str = 'file', sync: bool = True) Buffer[source]

Allocate buffer memory and read input data.

Parameters:
datanumpy array

Data which should inserted

srint, default: 44100

sample rate

mode‘file’ or ‘osc’

Insert data via filemode (‘file’) or n_set OSC commands (‘osc’) Bundling is only supported for ‘osc’ mode and if sync is False.

sync: bool, default: True

Use SCServer.sync after sending messages when mode = ‘osc’

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

load_collection(data: numpy.ndarray, mode: str = 'file', sr: int = 44100) Buffer[source]

Wrapper method of Buffer.load_data()

load_asig(asig: pya.Asig, mode: str = 'file') Buffer[source]

Create buffer from asig

Parameters:
asigpya.Asig

asig to be loaded in buffer

modestr, optional

Insert data via filemode (‘file’) or n_set OSC commands (‘osc’), by default ‘file’

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

use_existing(bufnum: int, sr: int = 44100) Buffer[source]

Creates a buffer object from already existing Buffer bufnum.

Parameters:
bufnumint

buffer node id

srint

Sample rate

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

copy_existing(buffer: Buffer) Buffer[source]

Duplicate an existing buffer

Parameters:
bufferBuffer object

Buffer which should be duplicated

Returns:
selfBuffer

the newly created Buffer object

Raises:
RuntimeError

If the Buffer is already allocated.

fill(start: int = 0, count: int = 0, value: float = 0) Buffer[source]

Fill range of samples with value(s).

Parameters:
startint or list

int : sample starting index list : n*[start, count, value] list

countint

number of samples to fill

valuefloat

value

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen(command: str, args: List[Any]) Buffer[source]

Call a command to fill a buffer. If you know, what you do -> you can use this method.

Parameters:
commandstr

What fill command to use.

argsList[Any]

Arguments for command

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

zero() Buffer[source]

Set buffer data to zero.

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen_sine1(amplitudes: List[float], normalize: bool = False, wavetable: bool = False, clear: bool = False) Buffer[source]

Fill the buffer with sine waves & given amplitude

Parameters:
amplitudeslist

The first float value specifies the amplitude of the first partial, the second float value specifies the amplitude of the second partial, and so on.

normalizebool

Normalize peak amplitude of wave to 1.0.

wavetablebool

If set, then the buffer is written in wavetable format so that it can be read by interpolating oscillators.

clearbool

If set then the buffer is cleared before new partials are written into it. Otherwise the new partials are summed with the existing contents of the buffer

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen_sine2(freq_amps: List[float], normalize: bool = False, wavetable: bool = False, clear: bool = False) Buffer[source]

Fill the buffer with sine waves given list of [frequency, amplitude] lists

Parameters:
freq_ampslist

Similar to sine1 except that each partial frequency is specified explicitly instead of being an integer multiple of the fundamental. Non-integer partial frequencies are possible.

normalizebool

If set, normalize peak amplitude of wave to 1.0.

wavetablebool

If set, the buffer is written in wavetable format so that it can be read by interpolating oscillators.

clearbool

If set, the buffer is cleared before new partials are written into it. Otherwise the new partials are summed with the existing contents of the buffer.

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen_sine3(freqs_amps_phases: List[float], normalize: bool = False, wavetable: bool = False, clear: bool = False) Buffer[source]

Fill the buffer with sine waves & given a list of [frequency, amplitude, phase] entries.

Parameters:
freqs_amps_phaseslist

Similar to sine2 except that each partial may have a nonzero starting phase.

normalizebool

if set, normalize peak amplitude of wave to 1.0.

wavetablebool

If set, the buffer is written in wavetable format so that it can be read by interpolating oscillators.

clearbool

If set, the buffer is cleared before new partials are written into it. Otherwise the new partials are summed with the existing contents of the buffer.

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen_cheby(amplitudes: List[float], normalize: bool = False, wavetable: bool = False, clear: bool = False) Buffer[source]

Fills a buffer with a series of chebyshev polynomials, which can be defined as cheby(n) = amplitude * cos(n * acos(x))

Parameters:
amplitudeslist

The first float value specifies the amplitude for n = 1, the second float value specifies the amplitude for n = 2, and so on

normalizebool

If set, normalize the peak amplitude of the Buffer to 1.0.

wavetablebool

If set, the buffer is written in wavetable format so that it can be read by interpolating oscillators.

clearbool

If set the buffer is cleared before new partials are written into it. Otherwise the new partials are summed with the existing contents of the buffer.

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

gen_copy(source: Buffer, source_pos: int, dest_pos: int, copy_amount: int) Buffer[source]

Copy samples from the source buffer to the destination buffer specified in the b_gen command.

Parameters:
sourceBuffer

Source buffer object

source_posint

sample position in source

dest_posint

sample position in destination

copy_amountint

number of samples to copy. If the number of samples to copy is negative, the maximum number of samples possible is copied.

Returns:
selfBuffer

the created Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

play(rate: float = 1, loop: bool = False, pan: float = 0, amp: float = 0.3) sc3nb.sc_objects.node.Synth[source]

Play the Buffer using a Synth

Parameters:
ratefloat, optional

plackback rate, by default 1

loopbool, optional

if True loop the playback, by default False

panint, optional

pan position, -1 is left, +1 is right, by default 0

ampfloat, optional

amplitude, by default 0.3

Returns:
Synth

Synth to control playback.

Raises:
RuntimeError

If the Buffer is not allocated yet.

write(path: str, header: str = 'wav', sample: str = 'float', num_frames: int = -1, starting_frame: int = 0, leave_open: bool = False) Buffer[source]

Write buffer data to a sound file

Parameters:
pathstring

path name of a sound file.

headerstring

header format. Header format is one of: “aiff”, “next”, “wav”, “ircam””, “raw”

samplestring

sample format. Sample format is one of: “int8”, “int16”, “int24”, “int32”, “float”, “double”, “mulaw”, “alaw”

num_framesint

number of frames to write. -1 means all frames.

starting_frameint

starting frame in buffer

leave_openboolean

Whether you want the buffer file left open. For use with DiskOut you will want this to be true. The file is created, but no frames are written until the DiskOut UGen does so. The default is false which is the correct value for all other cases.

Returns:
selfBuffer

the Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

close() Buffer[source]

Close soundfile after using a Buffer with DiskOut

Returns:
selfBuffer

the Buffer object

Raises:
RuntimeError

If the Buffer is not allocated yet.

to_array() numpy.ndarray[source]

Return the buffer data as an array representation.

Returns:
np.ndarray:

Values of the buffer

Raises:
RuntimeError

If the Buffer is not allocated yet.

query() BufferInfo[source]

Get buffer info.

Returns:
Tuple:

(buffer number, number of frames, number of channels, sampling rate)

Raises:
RuntimeError

If the Buffer is not allocated yet.

__repr__() str[source]

Return repr(self).

free() None[source]

Free buffer data.

Raises:
RuntimeError

If the Buffer is not allocated yet.

_gen_flags(a_normalize=False, a_wavetable=False, a_clear=False) int[source]

Generate Wave Fill Commands flags from booleans according to the SuperCollider Server Command Reference.

Parameters:
a_normalizebool, optional

Normalize peak amplitude of wave to 1.0, by default False

a_wavetablebool, optional

If set, then the buffer is written in wavetable format so that it can be read by interpolating oscillators, by default False

a_clearbool, optional

If set then the buffer is cleared before new partials are written into it. Otherwise the new partials are summed with the existing contents of the buffer, by default False

Returns:
int

Wave Fill Commands flags