Using supercollider with python

2019-03-15 13:45发布

问题:

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.

回答1:

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, from a tutorial along these lines I wrote for a class at Art Center, 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 OSC
import time, random
client = OSC.OSCClient()
client.connect( ( '127.0.0.1', 57120 ) )
msg = OSC.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).



回答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



回答3:

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



回答4:

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.