16

I want to do some real time sound processing and I heard about supercollider

and it looks great, but I want to stick to python as far as 'normal' programming is the issue.

Is there any way to load a python script as a module to supercollider or the oposite?

meaning importing a library to my python code and using the supercollider features?

I did not find much info about it in the web so any help will be great.

Philip Kirkbride
  • 17,347
  • 30
  • 101
  • 195
Itzik984
  • 13,250
  • 24
  • 66
  • 100
  • 1
    Do you already know [SC 0.3.1](https://pypi.python.org/pypi/SC/0.3.1)? – halex Apr 18 '13 at 15:15
  • 1
    Also, googling for `python supercollider` yields some other interesting results. – Aya Apr 18 '13 at 15:16
  • Will look both up. im having a bit of trouble importing the sc 0.3.1 package, im using mac os 10.8. will update the question if a solution will come across – Itzik984 Apr 18 '13 at 15:21

7 Answers7

15

I am not aware of a python implementation of SuperCollider, however it is very easy to communicate between SC and Python with OpenSoundControl. Here is some sample code that shows how to send control information from Python to SC (used here as the audio engine). First the SC part:

s.boot;

(
SynthDef( \sin, { | amp = 0.01, freq = 333, trig = 1 |
    var env, sig;
    env = EnvGen.kr( Env.asr( 0.001, 0.9, 0.001 ), trig, doneAction: 0 );
    sig = LFTri.ar( [ freq, freq * 0.999 ], 0.0, amp ) * env;
    Out.ar( [ 0 ], sig * 0.6 );
}).add;

h = Synth( \sin, [ \amp, 0.4 ] );

x = OSCFunc( { | msg, time, addr, port |
    var pyFreq;

    pyFreq = msg[1].asFloat;
    ( "freq is " + pyFreq ).postln;
    h.set( \freq, pyFreq );
}, "/print" );
)


Now the Python part:

import pyOSC3
import time, random
client = pyOSC3.OSCClient()
client.connect( ( '127.0.0.1', 57120 ) )
msg = pyOSC3.OSCMessage()
msg.setAddress("/print")
msg.append(500)
client.send(msg)


So, you would still need to write some code in SC (to generate the type of audio, as well as to establish the connection between Python and SC), but you could do everything else in Python. See the link to the tutorial page for a significantly more in depth explanation (as well as a basic explanation of working with SC).

caseyanderson
  • 450
  • 1
  • 5
  • 10
  • 1
    Thanks so much for this! and the tutorial! I found it, but your link needs updating. =) – Iain Duncan Nov 17 '15 at 20:11
  • Glad it was helpful, thanks for pointing out that the link was broken! – caseyanderson Nov 18 '15 at 02:56
  • @caseyanderson Great guide, thank you - however I am running into an issue: SuperCollider seems to receive the message, but I am getting an error: `FAILURE IN SERVER: /print Command not found` in SC. Any clue what I may be doing wrong? – Toivo Säwén Apr 18 '18 at 17:46
  • 1
    @ToivoSäwén pardon the delay, only just seeing this now but glad you got it figured out. Thanks for your kind words about the tutorial! – caseyanderson Apr 19 '18 at 18:06
4

FoxDot (http://foxdot.org/) may provide what you are looking for

BGanesh
  • 51
  • 2
2

You can also use Python-osc. ( i really like that one!) @caseyanderson is right about there not being a python implementation. you can grab it with pip: pip install python-osc and import with import pythonosc or grab from here: https://pypi.python.org/pypi/python-osc

Rod O'Connor
  • 33
  • 1
  • 7
2

Haven't tried it but supriya looks promising...

CpILL
  • 4,495
  • 3
  • 32
  • 31
1

I update the answer of @caseyanderson because the OSC python module seems out to date. The code is still coming from this tutorial, that shows how to send control information from Python to SC (used here as the audio engine). First the SC part (unchanged):

s.boot;

(
SynthDef( \sin, { | amp = 0.01, freq = 333, trig = 1 |
    var env, sig;
    env = EnvGen.kr( Env.asr( 0.001, 0.9, 0.001 ), trig, doneAction: 0 );
    sig = LFTri.ar( [ freq, freq * 0.999 ], 0.0, amp ) * env;
    Out.ar( [ 0 ], sig * 0.6 );
}).add;

h = Synth( \sin, [ \amp, 0.4 ] );

x = OSCFunc( { | msg, time, addr, port |
    var pyFreq;
    pyFreq = msg[1].asFloat;
    ( "freq is " + pyFreq ).postln; h.set( \freq, pyFreq );
}, '/print' );
)

Then the Python part (updated, based on python-osc):

from pythonosc import udp_client
client = udp_client.SimpleUDPClient("127.0.0.1", 57120) #default ip and port for SC
client.send_message("/print", 440) # set the frequency at 440

You must first excecute the SC part. You should hear a sine wave at 330 Hz. The python part change the frequency of the sine to 440 Hz.

PatriceG
  • 3,270
  • 5
  • 23
  • 43
  • hi, you can also use pyOSC3, which is what I am using these days for this. I updated the answer up there, its a similar change to the changes for SimpleUDPClient. – caseyanderson Feb 26 '20 at 19:58
1

I also couldn't find anything appropriate, so I've created a lightweight module, python-supercollider, which lets you use the SuperCollider server for synthesis and Python 3 for your control and sequencing logic.

It's a wrapper around the SuperCollider OSC command set, following the same patterns as in the SC client language:

from supercollider import Server, Synth

server = Server()
synth = Synth(server, "sine", { "freq" : 440.0, "gain" : -12.0 })
synth.set("freq", 880.0)
Daniel Jones
  • 405
  • 3
  • 10
-1

My Professor with some fellow students developed this library sc3nb which can be used to address, code and manipulate SuperCollider from Python. It works with OSC but is easier syntax.

Library Code

Tutorial

Example (more detail in the Tutorial):

import sc3nb as scn
sc = scn.startup()  # startup sc3 sclang, boot server, load

#easy communication with Server Command Refs
sc.cmd(r"""
             #your SuperCollider-code here
       """)
sc.msg(...)
sc.bundle(...)
LBoss
  • 152
  • 2
  • 9