Scripting sclang via external process

Hi there!

I’m trying to make a toy web app where the web server will interact with sclang. I’m doing this in Elixir, which doesn’t have an SC library already; that’s fine, I’m comfortable using sclang. However, when I open sclang from Elixir, I’m not sure how I can get sclang to execute commands correctly. For example, if I have the following simple code, how can I actually get the parenthesis groupings to work, and how can I sclang to block on running the next command while the previous one runs?

s.boot;
(
SynthDef(\grains, { |freq=0, arg=0|
  Out.ar(GrainSin.ar(2, Impluse.kr(10), 0.1, freq, pan, -1));
}).add;
)
x = Synth(\grains);

Just quick, maybe others can elaborate:

http://doc.sccode.org/Classes/Server.html#-waitForBoot

Example usage:
http://sccode.org/1-5cl

You can put code like this in your startup file, or call sclang with a source file as argument

Gotta run, have fun!
eddi

Also use fork{do something; s.sync; do something} to make the thread wait for the server to complete pending requests

You might want to check out how supercollider.js does it:

If I remember correctly, you need to get your parenthesized, multi-line code, append the escape char that means “execute”, and send it to sclang:

I have never used Elixir, so I can’t write any code example, sorry

I have been wondering how to do this forever! escape! who knew… thanks

…i’ve been controlling my computer via voice (talonvoice) and with this tip I can send commands directly to scsynth rather than through nvim. awesome

this should be in the docs somewhere (is it?) as I can imagine a lot of folks will have use for it

Thanks guys. I’m moving forward again! Out of curiosity, is there any way to break up code into multiple parts in SC? IE, an import/export system? As far as I can tell, the only thing you could (theoretically, but inadvisedly) do would be to read a file and eval it, or (laboriously) create an extension to load.

There has been this thread recently:

Why is that inadvisable?

hjh

If that’s the best move then that’s the best move. Obviously in other languages that would be verboten :slight_smile:

A few of my extensions use prototype-based programming to allow objects to be defined at runtime. Those definitions are loaded at runtime by executing file contents, but behaviorally those files are just creating containers for data and functions that are being stored but not necessarily used at that time. I’ve been doing this for years and it’s stable. (Of course it’s possible to abuse the mechanism, but it’s also possible to abuse a chainsaw to do damage to someone else’s property and we don’t ban chainsaws just for that reason.)

Now, it sounds like you’re asking about a Java or Python style import facility. We don’t have that as such. The class library is compiled once at the start of a session and we don’t load class definitions dynamically after that. (You can use LanguageConfig to include or exclude directories, but it doesn’t take effect until recompiling the class library.) There have been discussions about dynamic loading but it’s a big change to interpreter internals (i.e. difficult and risky).

My use of prototypes is a workaround for this limitation. Sure, prototypes are not to everyone’s taste, but they do work and I’ve built some quite elaborate OOPy designs with them.

hjh

Plus, if that was ever a concern for you, loading files is performed by the language, not by the server, which means that slow or blocking file I/O won’t be performed on the audio thread :slight_smile: