play

maelzel.core.playback.play(*sources, whenfinished=None, **eventparams)[source]

Play a sequence of objects / events

When playing multiple objects via their respective .play method, initialization (loading soundfiles, soundfonts, etc.) might result in events getting out of sync with each other.

This function first collects all events; any initialization is done beforehand as to ensure that events keep in sync. After initialization all events are scheduled and their synths are gathered in a SynthGroup

This function can also be used as context manager if not given any sources (see example below)

Note

To customize playback use the .events method, which works exactly like .play but returns the data so that it can be played later.

Parameters:
  • sources (Union[MObj, Sequence[SynthEvent], Event]) – a possibly nested sequence of MObjs or events as returned from MObj.events(). Empty when used as a context manager.

  • whenfinished (Optional[Callable]) – a callback taking no arguments and returning None. It will be called when the last event is finished

  • eventparams – any keyword arguments will be passed to MObj.events() if events need to be generated

Return type:

SynthGroup | SynchronizedContext

Returns:

A SynthGroup holding all scheduled synths

Example

>>> from maelzel.core import *
>>> from csoundengine.session import SessionEvent
>>> import csoundengine as ce
>>> session = playSession()
>>> session.defInstr('reverb', r'''
>>> |kfeedback=0.85|
... a1, a2 monitor
... aL, aR  reverbsc a1, a2, kfeedback, 12000, sr, 0.5, 1
... outch 1, aL - a1, 2, aR - a2
... ''')
>>> session.defInstr('sin', r'''
... |imidi=60, iamp=0.01|
... a1 oscili iamp, mtof(imidi)
... a1 *= linsegr(0, 0.5, 1, 2, 0)
... outch 1, a1
... ''')
>>> play(
>>>     Chord("4C 4E", 7, start=1).events(position=0.5),
>>>     Note("4C#", 6, offset=1.5),  # No customization,
>>>     SessionEvent('reverb', dur=10, args={'kfeedback': 0.8}, priority=2),
>>>     SessionEvent('sin', delay=0.1, dur=3, args={'imidi': 61.33, 'iamp':0.02})
>>> )

As context manager

>>> note = Note(...)
>>> clip = Clip(...)
>>> with play() as p:
...     note.play(...)
...     clip.play(...)

See also

Synched, render(), MObj.play(), MObj.events()