Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow connection to remote SuperCollider #243

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions FoxDot/lib/ServerManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
('sample_rate', 'actual_sample_rate', 'num_synths', 'num_groups',
'num_audio_bus_channels', 'num_control_bus_channels',
'num_input_bus_channels', 'num_output_bus_channels', 'num_buffers',
'max_nodes', 'max_synth_defs'))
'max_nodes', 'max_synth_defs', 'foxdot_root', 'foxdot_snd'))


class OSCClientWrapper(OSCClient):
Expand Down Expand Up @@ -178,11 +178,15 @@ class SCLangServerManager(ServerManager):
fxlist = None
synthdefs = None

def __init__(self, addr, osc_port, sclang_port):
def __init__(self, addr, osc_port, sclang_port, foxdot_root, foxdot_snd):

self.addr = addr
self.port = osc_port
self.SCLang_port = sclang_port
self.foxdot_root = foxdot_root
self.foxdot_snd = foxdot_snd
self.foxdot_root_remote = ""
self.foxdot_snd_remote = ""

self.midi_nudge = 0

Expand Down Expand Up @@ -213,7 +217,12 @@ def reset(self):

# OSC Connection for custom OSCFunc in SuperCollider
if GET_SC_INFO:
self.sclang = BidirectionalOSCServer()
# Prevent SocketError when connecting to a remote FoxDot instance
server_address=('0.0.0.0', 0)
if (self.addr == 'localhost' or self.addr == '127.0.0.1'):
server_address=('localhost', 0)

self.sclang = BidirectionalOSCServer(server_address=server_address)
self.sclang.connect( (self.addr, self.SCLang_port) )
self.loadSynthDef(FOXDOT_INFO_FILE)
try:
Expand All @@ -227,6 +236,8 @@ def reset(self):
self.num_output_busses = info.num_output_bus_channels
self.max_busses = info.num_audio_bus_channels
self.bus = self.num_input_busses + self.num_output_busses
self.foxdot_root_remote = info.foxdot_root
self.foxdot_snd_remote = info.foxdot_snd
else:
self.sclang = OSCClientWrapper()
self.sclang.connect( (self.addr, self.SCLang_port))
Expand Down Expand Up @@ -608,6 +619,11 @@ def free_node(self, node):

def bufferRead(self, path, bufnum):
""" Sends a message to SuperCollider to read an audio file into a buffer """

# Update path for proper file load in the remote Supercollider
if (not(self.addr == 'localhost' or self.addr == '127.0.0.1')):
path = path.replace(self.foxdot_snd, self.foxdot_snd_remote)

message = OSCMessage("/b_allocRead")
message.append([bufnum, path])
self.client.send( message )
Expand All @@ -627,6 +643,11 @@ def sendMidi(self, msg, cmd=OSC_MIDI_ADDRESS):

def loadSynthDef(self, fn, cmd='/foxdot'):
""" Sends a message to the FoxDot class in SuperCollider to load a SynthDef from file """

# Update path for proper file load in the remote Supercollider
if (not(self.addr == 'localhost' or self.addr == '127.0.0.1')):
fn = fn.replace(self.foxdot_root, self.foxdot_root_remote)

msg = OSCMessage()
msg.setAddress(cmd)
msg.append(fn)
Expand Down Expand Up @@ -1097,10 +1118,10 @@ def kill(self):

if __name__ != "__main__":

from .Settings import ADDRESS, PORT, PORT2, FORWARD_PORT, FORWARD_ADDRESS
from .Settings import ADDRESS, PORT, PORT2, FORWARD_PORT, FORWARD_ADDRESS, FOXDOT_ROOT, FOXDOT_SND

# DefaultServer = SCLangServerManager(ADDRESS, PORT, PORT2)
Server = SCLangServerManager(ADDRESS, PORT, PORT2)
Server = SCLangServerManager(ADDRESS, PORT, PORT2, FOXDOT_ROOT, FOXDOT_SND)

if FORWARD_PORT and FORWARD_ADDRESS:
Server.add_forward(FORWARD_ADDRESS, FORWARD_PORT)
15 changes: 15 additions & 0 deletions FoxDot/lib/Settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ def GET_TUTORIAL_FILES():

FOXDOT_SND = os.path.realpath(conf.SAMPLES_DIR)

# Recreate info file from template
try:
with open(FOXDOT_INFO_FILE+".template", "r") as fread:
# Delete info file only if template file exists
if os.path.isfile(FOXDOT_INFO_FILE):
os.remove(FOXDOT_INFO_FILE)

content = fread.read()
content = content.replace("<FOXDOT_ROOT>", f"'{FOXDOT_ROOT}'")
content = content.replace("<FOXDOT_SND>", f"'{FOXDOT_SND}'")
with open(FOXDOT_INFO_FILE, "w") as f:
f.write(content)
except FileNotFoundError:
pass

def get_timestamp():
import time
return time.strftime("%Y%m%d-%H%M%S")
Expand Down
2 changes: 2 additions & 0 deletions FoxDot/osc/Info.scd → FoxDot/osc/Info.scd.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ OSCFunc(
Server.default.options.numBuffers,
Server.default.options.maxNodes,
Server.default.options.maxSynthDefs,
<FOXDOT_ROOT>,
<FOXDOT_SND>,
);
}, '/foxdot/info'
);