play

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

Play a sequence of objects / events in sync. Can be used as a context manager

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

To customize playback, use this function as a context manager or call .synthEvents method on each object instead of .play. .synthEvents has the same signature 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

  • display – if called as a context manager, the result of playback is displayed

  • 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 = getSession()
>>> 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).synthEvents(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("4C#", 6, offset=1.5)
>>> chord = Chord("4C 4E", 7, start=1)
>>> clip = Clip(...)
>>> with play() as s:  # returns the audio Session used
...     note.play(instr='.piano')
...     chord.play(position=0.5)
...     clip.play(speed=0.5, delay=1)
...     s.sched('reverb, priority=2')
...     s.sched('sin', ...)

See also

render(), MObj.play(), MObj.synthEvents()