mesonic.backend package#

Submodules#

mesonic.backend.backend_sc3nb module#

Implementation of the backend using sc3nb.

class mesonic.backend.backend_sc3nb.BackendSC3NB(**kwargs)[source]#

Bases: Backend

Backend for sc3nb

create_buffer_manager(context: Context) BufferManagerSC3NB[source]#

Create a BufferManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

BufferManager instance.

Return type:

BufferManager

create_record_manager(context: Context) RecordManagerSC3NB[source]#

Create a RecordManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

RecordManager instance.

Return type:

RecordManager

create_synth_manager(context: Context) SynthManagerSC3NB[source]#

Create a SynthManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

SynthManger instance.

Return type:

SynthManager

quit() None[source]#

Quit this Backend.

register_context(context: Context) None[source]#

Register a context to this Backend.

Parameters:

context (Context) – Context that should use this backend.

render_nrt(context: Context, output_path, options: 'ServerOptions' | None = None, **backend_kwargs) None[source]#

Render the provided Context in non-realtime.

Parameters:
  • context (Context) – Context to be rendered.

  • output_path (path like) – Path for the output file.

property sampling_rate#

Get the native sampling rate of the Backend

stop(context: Context) None[source]#

Stop all sounds of the provided Context.

Parameters:

context (Context) – The Context which creates the sounds to be stopped.

unregister_context(context: Context) None[source]#

Remove the context from the registered contexts.

Parameters:

context (Context) – Context to be removed.

class mesonic.backend.backend_sc3nb.BufferManagerSC3NB(backend: B, context: Context)[source]#

Bases: BufferManager

BufferManager for sc3nb

from_data(data, sr, **kwargs) Buffer[source]#

Create a Buffer from the provided data and sampling rate.

Parameters:
  • data (Any) – Samples for the Buffer. Typically numpy arrays or other collections.

  • sr (numeric) – Sampling rate of the Buffer. Type depends on backend.

Returns:

The created Buffer.

Return type:

Buffer

from_file(path, starting_frame: int = 0, num_frames: int = -1, channels: int | Sequence[int] | None = None, **kwargs) Buffer[source]#

Create a Buffer from a file.

Parameters:
  • path (path like) – Path of the file.

  • starting_frame (int, optional) – Index of the first frame, by default 0

  • num_frames (int, optional) – Number of frames to be loaded, by default -1

  • channels (Optional[Union[int, Sequence[int]]], optional) – Specification of the channels to load, by default None - None means all channels. - If a int is provided only that channel will be loaded. - If a Sequence of ints is provided the provided channels will be loaded.

Returns:

The Buffer loaded from the file.

Return type:

Buffer

class mesonic.backend.backend_sc3nb.EventHandlerSC3NB[source]#

Bases: EventHandler

A abstract EventHandler as basis sc3nb EventHandlers.

Breaks down the handling of mutliple Events to just single events by using the sc3nb.Bundler.

backend: BackendSC3NB#
handle(time, events: Iterable[Event], reversed: bool, **kwargs) None[source]#

Handle events with the provided time and state.

Parameters:
  • time (float) – Timestamp provided for the events.

  • events (Iterable[E]) – Events to be handled.

  • reversed (bool) – Whether the events must be reversed or not.

class mesonic.backend.backend_sc3nb.RecordEventHandlerSC3NB(backend: BackendSC3NB, records: WeakKeyDictionary[Record, Recorder])[source]#

Bases: EventHandlerSC3NB, RecordEventHandler

class mesonic.backend.backend_sc3nb.RecordManagerSC3NB(backend: B, context: Context)[source]#

Bases: RecordManager

RecordManager for sc3nb

create(path, track=0, nr_channels: int = 2, rec_header: str = 'wav', rec_format: str = 'int16', bufsize: int = 65536, **kwargs) Record[source]#

Create a Record instance.

Parameters:
  • path (path like) – Path where the record should be stored.

  • track (int, optional) – On which track the RecordEvents should be created, by default 0

Returns:

created Record

Return type:

Record

class mesonic.backend.backend_sc3nb.SynthEventHandlerSC3NB(backend: BackendSC3NB, synths: WeakKeyDictionary[Synth, Synth])[source]#

Bases: EventHandlerSC3NB, SynthEventHandler

SynthEventHandler for sc3nb

class mesonic.backend.backend_sc3nb.SynthManagerSC3NB(backend: B, context: Context)[source]#

Bases: SynthManager

add_buffer_synth_def(name, code=None, **kwargs)[source]#

Create a Synth Definition for SynthManager.from_buffer() to the backend.

Parameters:

name (str) – SynthDef name.

add_synth_def(name, code=None, **kwargs)[source]#

Create a Synth Definition to the backend.

Parameters:

name (str) – SynthDef name.

buffer_synthdefs: Dict[str, str] = {'playbuf': '\n{ |out=0, bufnum={{BUFNUM}}, rate=1, loop=0, pan=0, amp=0.3 |\n    var sig = PlayBuf.ar({{NUM_CHANNELS}}, bufnum,\n        rate*BufRateScale.kr(bufnum),\n        loop: loop,\n        doneAction: Done.freeSelf);\n    Out.ar(out, Pan2.ar(sig, pan, amp))\n}'}#
buffer_synthdefs_slots = {'BUFNUM': <function SynthManagerSC3NB.<lambda>>, 'NUM_CHANNELS': <function SynthManagerSC3NB.<lambda>>}#
create(name: str, track: int = 0, *, mutable: bool = True, **kwargs) Synth[source]#

Create a Synth.

Parameters:
  • name (str) – Name of Synth

  • track (int, optional) – On which track the SynthEvents should be created, by default 0

  • mutable (bool, optional) – Wether this Synth is mutable, by default True

Returns:

The created Synth.

Return type:

Synth

from_buffer(buffer: Buffer, synth_name: str = 'playbuf', **synth_kwargs) Synth[source]#

Create a Synth to playback a buffer.

This will provide the Backend with the information needed to create a Synth that can playback the provided Buffer.

Parameters:
  • buffer (Buffer) – Buffer instance that should be connected to the Synth.

  • synth_name (str) – Name of Synth that should playback the Buffer.

Returns:

The Synth created for the Buffer.

Return type:

Synth

sent_synthdefs = {}#

mesonic.backend.bases module#

Defines backend base classes

This module contains the relevant classes that are needed to implement a backend for mesonic.

class mesonic.backend.bases.Backend[source]#

Bases: ABC

Base class of the mesonic Backend.

This describes the Backend interface. The Backend should start or connect to the actual backend program. It is also creating the single Managers for the Contexts and stores variables.

abstract create_buffer_manager(context: Context) BufferManager[source]#

Create a BufferManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

BufferManager instance.

Return type:

BufferManager

abstract create_record_manager(context: Context) RecordManager[source]#

Create a RecordManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

RecordManager instance.

Return type:

RecordManager

abstract create_synth_manager(context: Context) SynthManager[source]#

Create a SynthManager instance for the provided Context

Parameters:

context (Context) – Context instance.

Returns:

SynthManger instance.

Return type:

SynthManager

get_context_vars(context: Context) Dict[source]#

Get the stored variables of the provided Context.

Parameters:

context (Context) – A registered Context of which the variables should be returned.

Returns:

Stored variables of a context.

Return type:

Dict

abstract quit()[source]#

Quit this Backend.

abstract register_context(context: Context) None[source]#

Register a context to this Backend.

Parameters:

context (Context) – Context that should use this backend.

abstract render_nrt(context: Context, output_path, **backend_kwargs)[source]#

Render the provided Context in non-realtime.

Parameters:
  • context (Context) – Context to be rendered.

  • output_path (path like) – Path for the output file.

abstract property sampling_rate#

Get the native sampling rate of the Backend

abstract stop(context: Context) None[source]#

Stop all sounds of the provided Context.

Parameters:

context (Context) – The Context which creates the sounds to be stopped.

abstract unregister_context(context: Context) None[source]#

Remove the context from the registered contexts.

Parameters:

context (Context) – Context to be removed.

class mesonic.backend.bases.BufferManager(backend: B, context: Context)[source]#

Bases: Manager[B, None]

Manager for Buffers.

event_handler: None#
from_asig(asig: Asig, **kwargs) Buffer[source]#

Create a Buffer from an pya.Asig

Parameters:

asig (Asig) – The Asig that should be created as Buffer.

Returns:

The Buffer created from the Asig.

Return type:

Buffer

abstract from_data(data, sr: numeric, **kwargs) Buffer[source]#

Create a Buffer from the provided data and sampling rate.

Parameters:
  • data (Any) – Samples for the Buffer. Typically numpy arrays or other collections.

  • sr (numeric) – Sampling rate of the Buffer. Type depends on backend.

Returns:

The created Buffer.

Return type:

Buffer

abstract from_file(path, starting_frame: int = 0, num_frames: int = -1, channels: int | Sequence[int] | None = None, **kwargs) Buffer[source]#

Create a Buffer from a file.

Parameters:
  • path (path like) – Path of the file.

  • starting_frame (int, optional) – Index of the first frame, by default 0

  • num_frames (int, optional) – Number of frames to be loaded, by default -1

  • channels (Optional[Union[int, Sequence[int]]], optional) – Specification of the channels to load, by default None - None means all channels. - If a int is provided only that channel will be loaded. - If a Sequence of ints is provided the provided channels will be loaded.

Returns:

The Buffer loaded from the file.

Return type:

Buffer

class mesonic.backend.bases.EventHandler[source]#

Bases: Generic[E]

A generic handler for abitrary Events.

This describes the EventHandler interface. It is used for processing Events at the backend.

The handling of a Event should be quickly done or run in the background using an explicit Thread for example to not stall the processing of multiple events.

abstract get_etype() type[source]#

Get the type of Event this handler processes.

Returns:

type of Event this handler processes.

Return type:

type

abstract handle(time: float, events: Iterable[E], reversed: bool, **kwargs) None[source]#

Handle events with the provided time and state.

Parameters:
  • time (float) – Timestamp provided for the events.

  • events (Iterable[E]) – Events to be handled.

  • reversed (bool) – Whether the events must be reversed or not.

class mesonic.backend.bases.Manager(backend: B, context: Context)[source]#

Bases: Generic[B, H]

A generic Manager.

This describes the Manager interface. It is used for preparing the Event generating instances and also prepares the EventHandler. It connects the Context with the Backend

Parameters:
  • backend (B) – backend instance that is used.

  • context (Context) – context instance that is used.

event_handler: H#
get_event_handler() H[source]#

Get the responsible EventHandler.

This should return None if no Events need to be handled.

Returns:

EventHandler for the instances managed by this Manager.

Return type:

H

class mesonic.backend.bases.RecordEventHandler[source]#

Bases: EventHandler[RecordEvent]

A EventHandler for RecordEvent.

get_etype() type[source]#

Get the type of Event this handler processes.

Returns:

type of Event this handler processes.

Return type:

type

abstract handle(time, events: Iterable[RecordEvent], reversed: bool, **kwargs) None[source]#

Handle events with the provided time and state.

Parameters:
  • time (float) – Timestamp provided for the events.

  • events (Iterable[E]) – Events to be handled.

  • reversed (bool) – Whether the events must be reversed or not.

class mesonic.backend.bases.RecordManager(backend: B, context: Context)[source]#

Bases: Manager[B, RecordEventHandler]

Manager for Records.

abstract create(path, track: int = 0, **kwargs) Record[source]#

Create a Record instance.

Parameters:
  • path (path like) – Path where the record should be stored.

  • track (int, optional) – On which track the RecordEvents should be created, by default 0

Returns:

created Record

Return type:

Record

property instances: Set[Record]#

Get the current Records.

Returns:

Set of current Records.

Return type:

Set[Record]

class mesonic.backend.bases.SynthEventHandler[source]#

Bases: EventHandler[SynthEvent]

A EventHandler for SynthEvent.

get_etype() type[source]#

Get the type of Event this handler processes.

Returns:

type of Event this handler processes.

Return type:

type

abstract handle(time, events: Iterable[SynthEvent], reversed: bool, **kwargs) None[source]#

Handle events with the provided time and state.

Parameters:
  • time (float) – Timestamp provided for the events.

  • events (Iterable[E]) – Events to be handled.

  • reversed (bool) – Whether the events must be reversed or not.

class mesonic.backend.bases.SynthManager(backend: B, context: Context)[source]#

Bases: Manager[B, SynthEventHandler]

Manager for Synths.

abstract add_buffer_synth_def(name, **kwargs)[source]#

Create a Synth Definition for SynthManager.from_buffer() to the backend.

Parameters:

name (str) – SynthDef name.

abstract add_synth_def(name, **kwargs)[source]#

Create a Synth Definition to the backend.

Parameters:

name (str) – SynthDef name.

abstract create(name: str, track: int = 0, *, mutable: bool = True, **kwargs) Synth[source]#

Create a Synth.

Parameters:
  • name (str) – Name of Synth

  • track (int, optional) – On which track the SynthEvents should be created, by default 0

  • mutable (bool, optional) – Wether this Synth is mutable, by default True

Returns:

The created Synth.

Return type:

Synth

abstract from_buffer(buffer: Buffer, synth_name: str, **synth_kwargs) Synth[source]#

Create a Synth to playback a buffer.

This will provide the Backend with the information needed to create a Synth that can playback the provided Buffer.

Parameters:
  • buffer (Buffer) – Buffer instance that should be connected to the Synth.

  • synth_name (str) – Name of Synth that should playback the Buffer.

Returns:

The Synth created for the Buffer.

Return type:

Synth

property names: Set[str]#

Get the names of the currently used Synths.

Returns:

Set containing the names of the currently used Synths.

Return type:

Set[str]

Module contents#

Package that contains all the Backend related modules.

The bases module contains the abstract classes that needs to be implemented by the concrete Backend implementations.

The other modules contains the implementations of the abstract classes from the bases module.

mesonic.backend.start_backend(backend: str | B | Type[B] = 'sc3nb', **backend_kwargs) Backend[source]#

Start a Backend.

Parameters:

backend (Union[str, Backend], optional) – Backend to be started, by default “sc3nb”. Typically this takes the name of the backend as string. However it can also allows the user to provide a class or instance of the Backend type.

Returns:

The started Backend.

Return type:

Backend

Raises:
  • ValueError – If the provided backend is not a subclass of the Backend class or if it is the abstract Backend class.

  • NotImplementedError – If an unknown backend is provided.