-
Notifications
You must be signed in to change notification settings - Fork 22
Use SuperCollider in your Android Activity
SuperCollider can be the audio engine for your Android activity (which you create in Java).
For a real-life example see the ScanVox application, and in particular the java class SoundManager which handles most of the communication between the Java layer and SuperCollider.
There are different ways you could go about things — for example, you could run SuperCollider as a Service in the background, and control it remotely from your app — but here we’re going to use the source directly by importing it as a library project.
This is basically the same as any Android library project in Eclipse . Get the source code (using git clone) and open it in Eclipse as a project. Then right-click on the project and choose Properties, then go to the Android tab and enable the option marked is a Library.
Then create yourself a new Eclipse project for your new Android project, as described in the Android docs . In this project’s Properties → Android, under the “Uses libraries” setting choose to add the SuperCollider-Android project.
In your Java code you’ll need to import the relevant classes:
import net.sf.supercollider.android.SCAudio; import net.sf.supercollider.android.ScService;
and then to start the engine you can do something like:
// Where to find the plugin DLLs public static final String dllDirStr = "/data/data/net.sf.supercollider.android/lib"; // Starting SuperCollider: superCollider = new SCAudio(dllDirStr); superCollider.start();
then you should be ready to send messages to SuperCollider and make it do things (see below). To stop SuperCollider correctly, use
superCollider.sendQuit();
If you want SC to listen on a UDP port (for OSC messages sent from your laptop, for example) then you can do this at the time you start SC:
superCollider.openUDP(57110);
… and this before you stop SC:
superCollider.closeUDP();
There’s a class called OscMessage which encapsulates OSC messages sent to/from scsynth:
import net.sf.supercollider.android.OscMessage;
Basically you create one of these with an array of strings/floats/integers used to create an OSC message as described in the SuperCollider Commend Reference – for example, to send the [/notify, 1] message (which tells the synth we’d like to be notified when things happen):
superCollider.sendMessage(new OscMessage( new Object[] {"notify", 1}));
or to send the [/s_new, “default”, 999, 0, 1] message which creates a default synth with the node ID 999:
superCollider.sendMessage(new OscMessage( new Object[] {"s_new", "default", 999, 0, 1}));
For convenience we made a specific call for the create-a-synth message since we use it often:
superCollider.sendMessage(OscMessage.createSynthMessage("default", 999, 0, 1));
The same basic approach is used for sending any command to the synth engine – buffer reading, synth starting/pausing/stopping.
(Note: OscMessage items can even be sent from a separate Android Activity — we created an AIDL interface which enables this. But here we’re just thinking about sending messages from within the same Activity.)
Receiving messages back from SuperCollider is sometimes important. For this you can use a call like
if(SCAudio.hasMessages()){
OscMessage receivedMessage = SCAudio.getMessage();
...
}
and then you can parse it to see what’s in it. For example
if(receivedMessage.get(0).toString().equals("/tr")) {
// We've received a /tr message! Now what shall we do with it?
}
For a more complete approach to receiving messages back, take a look at the SCMessageManager class which shows one way to create a message dispatch depot which different parts of your program could use to register for different types of notification.
What next?
Learn how to use your own synthdefs