Skip to content

Instantly share code, notes, and snippets.

@Enkerli
Created March 7, 2016 00:09
Show Gist options
  • Save Enkerli/47805f6e3f7e0281cdd7 to your computer and use it in GitHub Desktop.
Save Enkerli/47805f6e3f7e0281cdd7 to your computer and use it in GitHub Desktop.
Second ChucK script to control an oscillator through an accelerometer via TouchOSC.
/*
A quick ChucK script to accept accelerometer coordinates from the iOS app TouchOSC and use those coordinates to control a sound.
Originally setup the z coordinate to control a filter, before noticing that most ChucK oscillators accept “width” as a parameter. Sending the z coordinate to this parameter instead, simplifying the code from the previous version.
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 hostname of the device where this script is running
* The "Port (outgoing)" is set to the proper port.
In the case of a MacBook Pro running an ad hoc WiFi network with a manual/static IP, that IP address is used in TouchOSC.
Used these instructions: <http://www.musicalgeometry.com/?p=361>, which specifically describes use with TouchOSC.
So, TouchOSC needs to use 192.168.2.1 as the IP address for the host.
(An ad hoc network makes a lot of sense for OSC because it doesn?t require a router and is less likely to be disturbed by other connections.)
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 in the "on" position (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 13 inch Retina MacBook Pro Late 2013, on a bus ride. Works without any obvious issue. No apparent issue with latency or distortion. Seat neighbour didn’t even react.
*/
// Soundchain
TriOsc tri=>dac; // Create a triangle oscillator and chuck it to the audio out. Can be switched to SawOsc or other oscillators.
// 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, "192.168.2.1").
"/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=>tri.width; // Chuck the z coordinate to the oscillator’s “width”.
}
}
@Enkerli
Copy link
Author

Enkerli commented Mar 7, 2016

Second version, simplifying a bit from the previous version (which used a resonant filter instead of the oscillator’s “width”)

This is quick ChucK script to accept accelerometer coordinates from the iOS/Android app TouchOSC and use those coordinates to control a sound.

Originally setup the z coordinate to control a filter, before noticing that most ChucK oscillators accept “width” as a parameter. Sending the z coordinate to this parameter instead, simplifying the code from the previous version.

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 hostname of the device where this script is running
  • The "Port (outgoing)" is set to the proper port.

In the case of a MacBook Pro running an ad hoc WiFi network with a manual/static IP, that IP address is used in TouchOSC.
Used these instructions, which specifically describes use with TouchOSC.
So, TouchOSC needs to use 192.168.2.1 as the IP address for the host.
(An ad hoc network makes a lot of sense for OSC because it doesn?t require a router and is less likely to be disturbed by other connections.)

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 in the "on" position (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 13 inch Retina MacBook Pro Late 2013, on a bus ride. Works without any obvious issue. No apparent issue with latency or distortion. Seat neighbour didn’t even react.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment