A Python API for SuperCollider
Project description
Supriya
Supriya is a Python API for SuperCollider.
Supriya lets you:
-
Boot and communicate with SuperCollider's
scsynth
synthesis engine: servers in realtime -
Compile SuperCollider SynthDefs natively in Python code
-
Explore nonrealtime composition with object-oriented sessions
-
Build time-agnostic asyncio-aware applications with providers
-
Schedule patterns and callbacks with tempo- and meter-aware clocks
Installation
Get SuperCollider from http://supercollider.github.io/.
Get Supriya from PyPI:
pip install supriya
... or from source:
git clone https://github.com/josiah-wolf-oberholtzer/supriya.git
cd supriya/
pip install -e .
Example: Hello World!
Let's make some noise. One synthesis context, one synth, one parameter change.
>>> import supriya
Realtime
Grab a reference to a realtime server and boot it:
>>> server = supriya.Server().boot()
Add a synth, using the default SynthDef
:
>>> synth = server.add_synth()
Set the synth's frequency parameter like a dictionary:
>>> synth["frequency"] = 123.45
Release the synth:
>>> synth.release()
Quit the server:
>>> server.quit()
Non-realtime
Non-realtime work looks similar to realtime, with a couple key differences.
Create a Session
instead of a Server
:
>>> session = supriya.Session()
Use Session.at(...)
to select the desired point in time to make a mutation,
and add a synth using the default SynthDef
, and an explicit duration for the
synth which the Session will use to terminate the synth automatically at the
appropriate timestep:
>>> with session.at(0):
... synth = session.add_synth(duration=2)
...
Select another point in time and modify the synth's frequency, just like in realtime work:
>>> with session.at(1):
... synth["frequency"] = 123.45
...
Finally, render the session to disk:
>>> session.render(duration=3)
(0, PosixPath('/Users/josiah/Library/Caches/supriya/session-981245bde945c7550fa5548c04fb47f7.aiff'))
Example: Defining SynthDefs
Let's build a simple SynthDef
for playing a sine tone with an ADSR envelope.
First, some imports:
>>> from supriya.ugens import EnvGen, Out, SinOsc
>>> from supriya.synthdefs import Envelope, synthdef
We'll define a function and decorate it with the synthdef
decorator:
>>> @synthdef()
... def simple_sine(frequency=440, amplitude=0.1, gate=1):
... sine = SinOsc.ar(frequency=frequency) * amplitude
... envelope = EnvGen.kr(envelope=Envelope.adsr(), gate=gate, done_action=2)
... Out.ar(bus=0, source=[sine * envelope] * 2)
...
This results not in a function definition, but in the creation of a SynthDef
object:
>>> simple_sine
<SynthDef: simple_sine>
... which we can print to dump out its structure:
>>> print(simple_sine)
synthdef:
name: simple_sine
ugens:
- Control.kr: null
- SinOsc.ar:
frequency: Control.kr[1:frequency]
phase: 0.0
- BinaryOpUGen(MULTIPLICATION).ar/0:
left: SinOsc.ar[0]
right: Control.kr[0:amplitude]
- EnvGen.kr:
gate: Control.kr[2:gate]
level_scale: 1.0
level_bias: 0.0
time_scale: 1.0
done_action: 2.0
envelope[0]: 0.0
envelope[1]: 3.0
envelope[2]: 2.0
envelope[3]: -99.0
envelope[4]: 1.0
envelope[5]: 0.01
envelope[6]: 5.0
envelope[7]: -4.0
envelope[8]: 0.5
envelope[9]: 0.3
envelope[10]: 5.0
envelope[11]: -4.0
envelope[12]: 0.0
envelope[13]: 1.0
envelope[14]: 5.0
envelope[15]: -4.0
- BinaryOpUGen(MULTIPLICATION).ar/1:
left: BinaryOpUGen(MULTIPLICATION).ar/0[0]
right: EnvGen.kr[0]
- Out.ar:
bus: 0.0
source[0]: BinaryOpUGen(MULTIPLICATION).ar/1[0]
source[1]: BinaryOpUGen(MULTIPLICATION).ar/1[0]
Now let's boot the server:
>>> server = supriya.Server().boot()
... add our SynthDef
to it explicitly:
>>> server.add_synthdef(simple_sine)
... make a synth using our new SynthDef:
>>> synth = server.add_synth(simple_sine)
...release it:
>>> synth.release()
...and quit:
>>> server.quit()
Example: SynthDef Builders
Let's build a simple SynthDef
for playing an audio buffer as a one-shot, with
panning and speed controls.
This time we'll use Supriya's SynthDefBuilder
context manager. It's more
verbose than decorating a function, but it also gives more flexibility. For
example, the context manager can be passed around from function to function to
add progressively more complexity. The synthdef
decorator uses
SynthDefBuilder
under the hood.
First, some imports, just to save horizontal space:
>>> from supriya.ugens import Out, Pan2, PlayBuf
Second, define a builder with the control parameters we want for our
SynthDef
:
>>> builder = supriya.SynthDefBuilder(
... amplitude=1, buffer_id=0, out=0, panning=0.0, rate=1.0
... )
Third, use the builder as a context manager. Unit generators defined inside the context will be added automatically to the builder:
>>> with builder:
... player = PlayBuf.ar(
... buffer_id=builder["buffer_id"],
... done_action=supriya.DoneAction.FREE_SYNTH,
... rate=builder["rate"],
... )
... panner = Pan2.ar(
... source=player,
... position=builder["panning"],
... level=builder["amplitude"],
... )
... _ = Out.ar(bus=builder["out"], source=panner)
...
Finally, build the SynthDef
:
>>> buffer_player = builder.build()
Let's print its structure. Note that Supriya has given the SynthDef
a name
automatically by hashing its structure:
>>> print(buffer_player)
synthdef:
name: a056603c05d80c575333c2544abf0a05
ugens:
- Control.kr: null
- PlayBuf.ar:
buffer_id: Control.kr[1:buffer_id]
done_action: 2.0
loop: 0.0
rate: Control.kr[4:rate]
start_position: 0.0
trigger: 1.0
- Pan2.ar:
level: Control.kr[0:amplitude]
position: Control.kr[3:panning]
source: PlayBuf.ar[0]
- Out.ar:
bus: Control.kr[2:out]
source[0]: Pan2.ar[0]
source[1]: Pan2.ar[1]
"Anonymous" SynthDef
s are great! Supriya keeps track of what SynthDef
s have
been allocated by name, so naming them after the hash of their structure
guarantees no accidental overwrites and no accidental re-allocations.
Example: Playing Samples
Boot the server and allocate a sample:
>>> server = supriya.Server().boot()
>>> buffer_ = server.add_buffer(file_path="supriya/assets/audio/birds/birds-01.wav")
Allocate a synth using the SynthDef
we defined before:
>>> server.add_synth(synthdef=buffer_player, buffer_id=buffer_)
<+ Synth: 1000 a056603c05d80c575333c2544abf0a05>
The synth will play to completion and terminate itself.
Example: Performing Patterns
Supriya implements a pattern library inspired by SuperCollider's, using nested generators.
Let's import some pattern classes:
>>> from supriya.patterns import EventPattern, ParallelPattern, SequencePattern
... then define a pattern comprised of two event patterns played in parallel:
>>> pattern = ParallelPattern([
... EventPattern(
... frequency=SequencePattern([440, 550]),
... ),
... EventPattern(
... frequency=SequencePattern([1500, 1600, 1700]),
... delta=0.75,
... ),
... ])
Patterns can be manually iterated over:
>>> for event in pattern:
... event
...
CompositeEvent([
NoteEvent(UUID('ec648473-4e7b-4a9a-9708-6893c054ac0b'), delta=0.0, frequency=440),
NoteEvent(UUID('32b84a7d-ba7b-4508-81f3-b9f560bc34a7'), delta=0.0, frequency=1500),
], delta=0.75)
NoteEvent(UUID('412f3bf3-df75-4eb5-bc9d-3e074bfd2f46'), delta=0.25, frequency=1600)
NoteEvent(UUID('69a01ba8-4c00-4b55-905a-99f3933a6963'), delta=0.5, frequency=550)
NoteEvent(UUID('2c6a6d95-f418-4613-b213-811a442ea4c8'), delta=0.5, frequency=1700)
Patterns can be played (and stopped) in real-time contexts:
>>> server = supriya.Server().boot()
>>> player = pattern.play(provider=server)
>>> player.stop()
... or in non-realtime contexts:
>>> session = supriya.Session()
>>> _ = pattern.play(provider=session, at=0.5)
>>> print(session.to_strings(include_controls=True))
0.0:
NODE TREE 0 group
0.5:
NODE TREE 0 group
1001 default
amplitude: 0.1, frequency: 1500.0, gate: 1.0, out: 0.0, pan: 0.5
1000 default
amplitude: 0.1, frequency: 440.0, gate: 1.0, out: 0.0, pan: 0.5
2.0:
NODE TREE 0 group
1002 default
amplitude: 0.1, frequency: 1600.0, gate: 1.0, out: 0.0, pan: 0.5
1001 default
amplitude: 0.1, frequency: 1500.0, gate: 1.0, out: 0.0, pan: 0.5
1000 default
amplitude: 0.1, frequency: 440.0, gate: 1.0, out: 0.0, pan: 0.5
2.5:
NODE TREE 0 group
1003 default
amplitude: 0.1, frequency: 550.0, gate: 1.0, out: 0.0, pan: 0.5
1002 default
amplitude: 0.1, frequency: 1600.0, gate: 1.0, out: 0.0, pan: 0.5
3.5:
NODE TREE 0 group
1006 default
amplitude: 0.1, frequency: 1700.0, gate: 1.0, out: 0.0, pan: 0.5
1003 default
amplitude: 0.1, frequency: 550.0, gate: 1.0, out: 0.0, pan: 0.5
1002 default
amplitude: 0.1, frequency: 1600.0, gate: 1.0, out: 0.0, pan: 0.5
4.0:
NODE TREE 0 group
1006 default
amplitude: 0.1, frequency: 1700.0, gate: 1.0, out: 0.0, pan: 0.5
1003 default
amplitude: 0.1, frequency: 550.0, gate: 1.0, out: 0.0, pan: 0.5
4.5:
NODE TREE 0 group
1006 default
amplitude: 0.1, frequency: 1700.0, gate: 1.0, out: 0.0, pan: 0.5
5.5:
NODE TREE 0 group
Example: Asyncio
Supriya also supports asyncio, with async servers, providers and clocks.
Async servers expose a minimal interface (effectively just .boot()
, .send()
and .quit()
), and don't support the rich stateful entities their non-async
siblings do (e.g. Group
, Synth
, Bus
, Buffer
). To split the difference,
we'll wrap the async server with a Provider
that exposes an API of common
actions and returns lightweight stateless proxies we can use as references.
The proxies know their IDs and provide convenience functions, but otherwise
don't keep track of changes reported by the server.
Let's grab a couple imports:
import asyncio, random
... and get a little silly.
First we'll define an async clock callback. It takes a Provider
and a list of
buffer proxies created elsewhere by that provider, picks a random buffer and
uses the buffer_player
SynthDef
we defined earlier to play it (with a lot
of randomized parameters). Finally, it returns a random delta between 0 beats
and 2/4:
async def callback(clock_context, provider, buffer_proxies):
print("playing a bird...")
buffer_proxy = random.choice(buffer_proxies)
async with provider.at():
provider.add_synth(
synthdef=buffer_player,
buffer_id=buffer_proxy,
amplitude=random.random(),
rate=random.random() * 2,
panning=(random.random() * 2) - 1,
)
return random.random() * 0.5
Next we'll define our top-level async function. This one boots the server,
creates the Provider
we'll use to interact with it, loads in all of Supriya's
built-in bird samples, schedules our clock callback with an async clock, waits
10 seconds and shuts down:
async def serve():
print("preparing the birds...")
# Boot an async server
server = await supriya.AsyncServer().boot(
port=supriya.osc.utils.find_free_port(),
)
# Create a provider for higher-level interaction
provider = supriya.Provider.from_context(server)
# Use the provider as a context manager and load some buffers
async with provider.at():
buffer_proxies = [
provider.add_buffer(file_path=bird_sample)
for bird_sample in supriya.Assets["audio/birds/*"]
]
# Create an async clock
clock = supriya.AsyncClock()
# Schedule our bird-playing clock callback to run immediately
clock.schedule(callback, args=[provider, buffer_proxies])
# Start the clock
await clock.start()
# Wait 10 seconds
await asyncio.sleep(10)
# Stop the clock
await clock.stop()
# And quit the server
await server.quit()
print("... done!")
Let's make some noise:
>>> asyncio.run(serve())
preparing the birds...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
playing a bird...
... done!
Working async means we can hook into other interesting projects like python-prompt-toolkit, aiohttp and pymonome.
License
This library is made available under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for supriya-22.11b0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8dab37ed2efd0641c9bc33a1e73d2070ed597606cd1edcee104107e2275b1c5 |
|
MD5 | f74e230e48a5459e3a8d3cbf19b63a0b |
|
BLAKE2b-256 | bc298972292009fbbd7f9e1154a37476d2af411b09ae19a73b77aee4050e9409 |
Hashes for supriya-22.11b0-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62617e6e26c2ae9a845efa048b71876e270d5e69df49a50c669a8fb4a9189357 |
|
MD5 | d87823256fe291dcfe9379606a2ecc9c |
|
BLAKE2b-256 | 2f0e3888005289cd590fa8eea01f7966d0421b9f4323a303e1fa55048e335bd6 |
Hashes for supriya-22.11b0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fc8466ded031ae3fe74d60666bfc0f7298954996957d30394024dd0ed94501a |
|
MD5 | ad0dbdaeda734754508aa7ef5f27902b |
|
BLAKE2b-256 | ab4076782dac727e51725a5d007b419cd577b6f8fe962c3b3167786db44df48d |
Hashes for supriya-22.11b0-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a05633e29afdd8619277a45081471f3230e35176eab3b6dad0d714425c1e5457 |
|
MD5 | 53ef9e230319b1da42aa754b56f8ce23 |
|
BLAKE2b-256 | c0fa45e1738d1025046c8b61da81b80d6d3c5e8e2ce7ef8f52e58c4da16e19e2 |
Hashes for supriya-22.11b0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c50844a13eb90e20c3bffa42f6425121407133f2c838f654178e51e8cf5f6342 |
|
MD5 | bb2059183cb0077b034bc222c2341cc9 |
|
BLAKE2b-256 | f70e1e90a62976fdca2ca0f6f082c9d699459678a438d5bb40dea608a2a50472 |
Hashes for supriya-22.11b0-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 339b0081bccbcaa65126b0162b8e842828e111d8e589f2398e88ff87608fed63 |
|
MD5 | 7b01fa380cc2b3cf5e8c5127e31a6fb5 |
|
BLAKE2b-256 | a94936308646e92bfd800c92c7dcb622e4bab2f904f9467068416c4f50e07f32 |
Hashes for supriya-22.11b0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5a4c1bdd10bbc6139ca4e1c4f064d1383fdcf5fc8dbe8c3eb13c6601abeb134 |
|
MD5 | 8bc2dd497d6e9eac8c9fe7a07ba02563 |
|
BLAKE2b-256 | 5a3b3a96a502aac10d84e86b489f46e949a021e1033101182b4e8d1e2fa3857e |
Hashes for supriya-22.11b0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0aca712f35b096a42c0efc003ee7aaf91f4262d1f8513af5a36122fe0e60623 |
|
MD5 | 642d77977906e05f747bd7254f96d3fd |
|
BLAKE2b-256 | 7da7a34829f93c828b395c9eccb132ae53124930a89202a6c7a3f5cd1bfbe7a6 |
Hashes for supriya-22.11b0-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e9c6f134a8defa40b27f4aeff58e01741f3875667e1dc3a03ef87076d3b773c |
|
MD5 | 616694e57fc205bf60fd5f99d2606cd4 |
|
BLAKE2b-256 | d842203848d0b4182e0873230fa527e5b8954f8260911281c85f20aaf9dc33c1 |
Hashes for supriya-22.11b0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 928de3bd22c3386b06d0548cd96a915763d23ddb8619c6d2f1eabd52bd8b5d65 |
|
MD5 | 6fa20882f5fc54e889082aae1c3b0cb6 |
|
BLAKE2b-256 | b8247d5164dc2ff0f380605fc3fe0f3821a12044526a9c3a267096e2e2d0cf55 |
Hashes for supriya-22.11b0-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c942608d1ba39b40ed99db28eda1133c2435c8c2bfdf2f7bbcc86f7e553b5b87 |
|
MD5 | 6e398b8851ccdb3b8c7e75d0f1831c3b |
|
BLAKE2b-256 | 5f314a84196b2a081c0517d67f3cd36f099ddfcadcfbec0d0e59c1919614457f |
Hashes for supriya-22.11b0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b078a25a37e09768cc8352026aa823ef9d7d2b6d8ed68234644e1ff2ab84e1d |
|
MD5 | 5c339899eb77e5b3bfd9fd9525f71d78 |
|
BLAKE2b-256 | 0bcea3a0efd87db782f813a4fefa6f981c0951f347efefd09f4131c9eb96493a |
Hashes for supriya-22.11b0-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e2bf7d4cafa40234c23610b7591bd5c24836369c5b97fd9864033fb8ef17613 |
|
MD5 | 0e9775e75e932597536399e07704b98f |
|
BLAKE2b-256 | 0841227d325a03172b2ad51b802e3252de8efa725e49c3f99b150f0d60653720 |
Hashes for supriya-22.11b0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aeb92099507d0a93a0e771bbb283f05007fa4c6657272217e1d1a5ba72e2187d |
|
MD5 | 23f4728c48725e9241d17e803319dba5 |
|
BLAKE2b-256 | 598314586fc5c7b23e81377a48b560ca5780328772019f33029b69157b1a2bd3 |
Hashes for supriya-22.11b0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fc2d19ad9ce52443d11b80ebd3066d772780310d4099c7735322a757e70135e |
|
MD5 | 9c67fb32ef4bcc719195c8b75ab08828 |
|
BLAKE2b-256 | bf45b1a513eab8bef405000dd538123a98f56a0fc62a9cbe32cdbbda1f51dbfa |
Hashes for supriya-22.11b0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9afbc56b9aad169753c28c1f0c0601e9787205dab41516c866dc3aa916823c35 |
|
MD5 | 352ccb1113a6cba41546cd96d287ab36 |
|
BLAKE2b-256 | 585138e20f48391cec3a157378ac2975005879842610fdfc3ad510a52230e6f6 |
Hashes for supriya-22.11b0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db0700453c5086af3ac19cc0b1ef672d5d7dfc8f1cf04d5af9ffb6d25a361378 |
|
MD5 | 789ae8d6ffa371adec97d9041de8f945 |
|
BLAKE2b-256 | eb6c21b8dfce14192601c14fe0c42ddfe7e61da4a16df29db7e9516f218ce4af |
Hashes for supriya-22.11b0-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58c2ea863980d2d06983f1b8a20dcc1d326cbb48ac0d5bed6386568edecfe246 |
|
MD5 | 2646fb66ab11f6a69516d3abb2c5b12b |
|
BLAKE2b-256 | 7b926b62bdd0ae60b28fe8c27519e3c9e4b1194df0a780fdb15cd8420853bcd2 |
Hashes for supriya-22.11b0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 161a134b2b028a82fda8526dd8bf369d2a26e2451ecd325c19453009f8aa34bf |
|
MD5 | 8fa2c61d06ca56d4cf48b277bfa8e0e0 |
|
BLAKE2b-256 | 57ef33acc355a9244ac3b489d45a2cd6e392317e0ab994a8e84aea1612621ff7 |
Hashes for supriya-22.11b0-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ea26f916834f990946bf67f444887705c5304c927a5f52151e442926f71170a |
|
MD5 | e5c7e7d1a905358e8138ce1dfae0ce54 |
|
BLAKE2b-256 | 92b8246623dfa2fc29c2a07ad434471498a09cf53499e3b2c61f4c23361fb947 |
Hashes for supriya-22.11b0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 228a270375c8b695a31992344b791803481be38aa814d4f522ec1eef25bd2f3e |
|
MD5 | a7e9f0e03edd5e20c31b4808affd7ef0 |
|
BLAKE2b-256 | 47507263d7d0933f91749a0b0f34cae5dfe817560e92a9996d5fdcb36387f1a8 |