Skip to content

Commit

Permalink
Updated and added example
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcTheSpark committed May 6, 2016
1 parent 5b8fe7e commit 9d85f19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 29 deletions.
1 change: 1 addition & 0 deletions midi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'mpevans'
8 changes: 8 additions & 0 deletions supercollider/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This folder contains my implementation of OSC communication between python and Supercollider.

The "executable" folder contains an old version of supercollider (the reason being that the old version did not contain the large QT library). A better approach would be to compile a newer version of supercollider without qt, but somehow that kept failing on my computer. Anyway, the class library has one custom class called "PyCom" used in the supercollider code for setting up responders to OSC messages coming from python (and sending messages if desired).

"supercollider.py" contains the python implementation. A short example at the end of that file opens supercollider, runs "Test.scd", and does some basic communication with the synth.

Hopefully it's pretty self-explanatory.

15 changes: 15 additions & 0 deletions supercollider/Test.scd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fork {
SynthDef("pinkWobble", { |wobbleFreq=0.5|
Out.ar(0, Pan2.ar(PinkNoise.ar(0.2), SinOsc.kr(wobbleFreq)));
}).add;

s.sync;

~wobble = Synth.new("pinkWobble");
};

PyCom.receive("setWobbleFrequency", { |time, node, msg|
// msg[0] is just the name of the message
~wobble.set("wobbleFreq", msg[1]);
PyCom.send("/chatter", "I can talk back too!");
});
27 changes: 0 additions & 27 deletions supercollider/executable/Test.scd

This file was deleted.

40 changes: 38 additions & 2 deletions supercollider/supercollider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import thread
import os
from marcpy.utilities import get_relative_file_path
from threading import Event


_supercollider_process = None
Expand Down Expand Up @@ -57,7 +58,7 @@ def kill_supercollider():
atexit.register(kill_supercollider)


def start_supercollider(callback_when_ready=None):
def start_supercollider(callback_when_ready=None, hold_until_booted=False):
if not is_running():
global _supercollider_process, _supercollider_listener, \
executable_path, sc_directory, sc_file_runner_path
Expand All @@ -66,18 +67,24 @@ def start_supercollider(callback_when_ready=None):
_supercollider_listener = OSC.OSCServer(('127.0.0.1', port))
_supercollider_listener.addDefaultHandlers()

boot_done_event = Event()

def sc_port_received(addr, tags, stuff, source):
global _py_to_supercollider_client
print "SuperCollider started and listening on port", stuff[0]
_py_to_supercollider_client = OSC.OSCClient()
_py_to_supercollider_client.connect(( '127.0.0.1', stuff[0] ))
if callback_when_ready is not None:
callback_when_ready()
boot_done_event.set()

_supercollider_listener.addMsgHandler("/sendPort", sc_port_received)
thread.start_new_thread(_supercollider_listener.serve_forever, ())

_supercollider_process = subprocess.Popen([executable_path, "-d", sc_directory, sc_file_runner_path, str(port)])

if hold_until_booted:
boot_done_event.wait()
else:
print "Tried to start supercollider, but it was already running"

Expand Down Expand Up @@ -106,4 +113,33 @@ def send_sc_message(address, data):
the_msg.append(datum)
_py_to_supercollider_client.send(the_msg)

# start_supercollider(callback_when_ready=lambda: run_file(get_relative_file_path("executable/Test.scd")))

def add_sc_listener(tag_to_respond_to, response_function):
if is_running():
_supercollider_listener.addMsgHandler(tag_to_respond_to, response_function)
else:
raise Exception("Supercollider not started yet, so can't listen!")


# # ---------------------------------------- A SIMPLE EXAMPLE ---------------------------------------------
# # start supercollider and wait until it's booted (since hold_until_booted is True)
# # then the callback function runs the file "Test.scd"
#
# start_supercollider(callback_when_ready=lambda: run_file(os.path.realpath("Test.scd")), hold_until_booted=True)
#
#
# # set up a responder to handle messages coming from supercollider; this is likely unnecessary a lot of the time
# def talk_back_responder(addr, tags, stuff, source):
# print "Supercollider says \"{}\"".format(stuff[0])
#
# add_sc_listener("/chatter", talk_back_responder)
#
# # wait five seconds while we listen to the initial state of the synth
# time.sleep(5)
#
# # set the frequency of the left-right wobble in the synth to 20Hz
# # also, the supercollider file is set up to send back a message in response
# send_sc_message("setWobbleFrequency", [20])
#
# # listen for 5 seconds
# time.sleep(5)

0 comments on commit 9d85f19

Please sign in to comment.