Skip to content

Instantly share code, notes, and snippets.

@Enkerli
Last active September 15, 2023 02:27
Show Gist options
  • Save Enkerli/ff6a15401abbb0fc5f985686b58e3391 to your computer and use it in GitHub Desktop.
Save Enkerli/ff6a15401abbb0fc5f985686b58e3391 to your computer and use it in GitHub Desktop.
A Sonic Pi script (spi-ctl-bpf.rb) and a Processing one (spi_ctl_bpf.pde) to control a band pass filter using the mouse. Been having performance issues with the sound lagging behind. Setting `set_sched_ahead_time!` in Sonic Pi sounds like it helps, but not enough. Original code from Robin Newman: https://gist.github.com/rbnpi/ca5e80258c0c1296b1c…
set_sched_ahead_time! 0 # Setting that one to a negative value makes Sonic Pi complain, it sounds like.
with_fx :bpf do |s| # Setting up the fx to be controlled internally
synth :square, note: 32,release: 400 # Long release as the control will affect a single note
live_loop :ctl do # The loop is inside the fx as this is where the action will be.
ctl=sync "/ctl" # The OSC message which Processing sends, based on mouse coordinates.
rz=ctl[:args][0] # Assigning the first argument to resonance.
ct=ctl[:args][1] # Assigning the second argument to centre.
control s, res: rz, centre: ct # The actual control of the fx.
set_sched_ahead_time! -2 # Sounds like setting this again every time actually helps.
end
end
/**
* Based on oscP5broadcastClient by andreas schlegel
* an osc broadcast client.
* oscP5 website at http://www.sojamo.de/oscP5
*/
import oscP5.*;
import netP5.*;
OscP5 oscP5;
/* a NetAddress contains the ip address and port number of a remote location in the network. */
NetAddress myBroadcastLocation;
void setup() {
size(400,400);
frameRate(25); // Played with this a bit but it doesn’t sound like it really helps.
/* create a new instance of oscP5.
* 12000 is the port number Processing listening for incoming osc messages. We’re not using that.
*/
oscP5 = new OscP5(this,12000);
/* create a new NetAddress. a NetAddress is used when sending osc messages
* with the oscP5.send method.
*/
/* the address of the Sonic Pi broadcast server, on the same machine */
myBroadcastLocation = new NetAddress("127.0.0.1",4559);
}
void draw() {
background(0);
float mousx=mouseX/400.0; // Since resonance is 0 to 1 and the sketch is 400px wide… And we need a float, which explains the .0…
float mousy=mouseY/4.0; // Centre expects a value in MIDI notes…
OscMessage myOscMessage = new OscMessage("/ctl"); // Preparing the OSC message to be sent to the right address, the one Sonic Pi expects.
myOscMessage.add(mousx); // Adding those floats to the OSC message…
myOscMessage.add(mousy);
// println(mousx, mousy); // To check on those values…
oscP5.send(myOscMessage, myBroadcastLocation); // Sending the message each frame…
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment