Data Sonification example
This example combines Supercollider and Python: Python reads data from a text file and prepares it for controlling Supercollider, which creates the sounds.
In Supercollider we create an OSC (Open Sound Control) listener on port 7777
From Python we send OSC commands to port 7777
It is possible to do all of this (reading and preparing the data and generating the sound) in Supercollider but I (Marc) feel more comfortable in Python and it makes a nice example of controlling Supercollider from another domain.
Supercollider
s.boot
NetAddr.langPort;
NetAddr.localAddr;
thisProcess.openPorts; // list available ports
thisProcess.openUDPPort(7777);
thisProcess.openPorts; // list available ports again
// Create a synth definition
// This is a combination of two sine oscillators where the slower
// one modulates the amplitude of the faster one. Their frequencies
// are coupled to the same parameter freq.
SynthDef("mySine",
{arg freq=400,mul=0.1; Out.ar([0,1],
SinOsc.ar(freq,0,SinOsc.kr(0.03*freq,0,0.5,1)))}).add;
// Start a synth based on the synth definition above
a=Synth("mySine",[\freq, 440]);
p = OSCFunc({ arg msg,time,addr,recvPort;
a.set(\freq,msg[1]); a.set(\mul,msg[2]) }, '/freq');
Python basic OSC message
Execute the entire file by python, or use ipython to experiment with the code in an interactive way.
import time
from pythonosc.udp_client import SimpleUDPClient
ip = "127.0.0.1"
port = 7777
client = SimpleUDPClient(ip,port)
client.send_message("/freq",[300,0.6])
Python data sonification
Use python to read data from a text file, line by line, and convert each number into a frequency and amplitude value for sending to Supercollider.
import time
from pythonosc.udp_client import SimpleUDPClient
ip = "127.0.0.1"
port = 7777
client = SimpleUDPClient(ip,port)
f=open("my_data_file.txt",'r')
for line in f:
level = float(line)
print(level)
freq = some_formula with level
volume = some_other_formula_with level
client.send_message("/freq",[freq,volume])
time.sleep(0.2)