Skip to content

Instantly share code, notes, and snippets.

@Enkerli
Created March 6, 2016 18:58
Show Gist options
  • Save Enkerli/0f82a6a77d466c20facc to your computer and use it in GitHub Desktop.
Save Enkerli/0f82a6a77d466c20facc to your computer and use it in GitHub Desktop.
A quick ChucK script to accept accelerometer coordinates from the iOS app TouchOSC and use those coordinates to control a sound. TouchOSC needs to be set up properly.
/*
A quick ChucK script to accept accelerometer coordinates from the iOS app TouchOSC and use those coordinates to control a sound.
TouchOSC needs to be set up properly, in its preferences (from any layout, those can be accessed through a button at the top-right corner).
The app needs to be running on the same (WiFi) network as this ChucK script and it needs to be associated with the right host: "OSC" is enabled, "Host" is the IP address or network name of the device where this script is running, the "Port (outgoing)" is set to the proper port.
In the case of a RaspberryPi running an unmodified version of Raspbian, the Host would be "raspberrypi.local". The port used in this script is 6449, an arbitrary number used in the ChucK-based book "Programming for Musicians and Digital Artists" (PMDA) which served as the basis for this script.
TouchOSC needs to be sending accelerometer values, which is in the "OSC" section of "Options": "Accelerometer (/xyz)" should be on (green).
Finally, TouchOSC needs to be in sending mode. In other words, the "Done" button should be tapped from the main screen.
Tested on an iPhone 6s Plus and a Raspberry Pi 2 model B. In some positions of the iPhone, some distortion is introduced. After some attempts to reduce this noise, it became one of the instrument's features. In contemporary music, any bug can be disguised as a feature.
*/
// Soundchain
TriOsc tri=>ResonZ rez=>dac; // Create a triangle oscillator and chuck it to a resonant filter
100=>rez.Q; // Set the quality of the resonant filter
10=>rez.gain; // Set the gain of the resonant filter
// OSC setup
OscIn oscin; // Create an input for OSC
6449 =>oscin.port; // Set the OSC port to 6449 (arbitrary number, used in PMDA book). TouchOSC needs to be set on the same port and send to the right host (in this case, "raspberrypi.local").
"/accxyz"=>oscin.addAddress; // Set OSC address to what TouchOSC sends from accelerometer messages
OscMsg mesg; // Set the variable which will receive OSC messages
while(true) // Infinite loop.
{
oscin=>now; // Wait for an OSC message.
while(oscin.recv(mesg)) // As long as the OSC message is being received
{
// Take the absolute values of the "xyz" coordinates sent by TouchOSC from the accelerometer
// and chuck each of them to a similarly named float.
Std.fabs(mesg.getFloat(0))=>float x;
Std.fabs(mesg.getFloat(1))=>float y;
Std.fabs(mesg.getFloat(2))=>float z;
<<<x,y,z>>>; // Print the value of those floats.
(x*1000)+100=>tri.freq; // Chuck the x coordinate to the oscillator's frequency, from 100Hz to 1.1kHz
y=>tri.gain; // Chuck the y coordinate to the oscillator's volume, from 0.0 to 1.0.
(z*1000)+100=>rez.freq; // Chuck the z coordinate to the resonant filter's frequency, from 100Hz to 1.1kHz
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment