Machine "Listening" in Supercollider

Posted on 2011-03-22 11:42:34

The code below calculates the MFCCs, spectral centroid, and spectral flatness for an input signal and sends the results over OSC. There’s one problem with it as written, which is that the MFCCs are not sent (or calculated) the first time you run the code, but if you stop it and start it again, they are calculated and sent properly. Still trying to figure that one out.

 s = Server.local.boot;
 (
 // Setup address and buffer
 n = NetAddr("127.0.0.1", 57130);
 b = Buffer.alloc(s, 1024, 1);
 )
 (
 // Setup control buses
 ~mfccBus = Bus.alloc(\control, s, 13);
 ~specCentroidBus = Bus.alloc(\control, s, 1);
 ~specFlatnessBus = Bus.alloc(\control, s, 1);
 SynthDef("listenSynth", {|name, buffer = 0, inputChannel = 0, specCentroidBusName = nil, specFlatnessBusName = nil, mfccBusName = nil|
     var in, fft, specCentroid, specFlatness, mfcc;
     in = SoundIn.ar(inputChannel);
     fft = FFT(buffer, in);
     specCentroid = SpecCentroid.kr(fft);
     specFlatness = SpecFlatness.kr(fft);
     mfcc = MFCC.kr(fft);
     // Output computed values to buses
     Out.kr(mfccBusName, mfcc);
     Out.kr(specCentroidBusName, specCentroid);
     Out.kr(specFlatnessBusName, specFlatness);
 }).send(s);
 // Routine that sends the values over OSC
 // Note the "*" in the code for the MFCCs; this is needed to "flatten" the array into a series of numbers that Supercollider likes.
 r = Routine.new({
     inf.do({
         ~specCentroidBus.get({|val|
             n.sendMsg("/specCentroid", val);
         });
         ~specFlatnessBus.get({|val|
             n.sendMsg("/specFlatness", val);
         });
         ~mfccBus.getn(13, {|val| {n.sendMsg("/mfcc", *val);}.defer});
          0.5.wait;
     });
 }).play;
 // Create the synth
 ~listenSynth = Synth.new(\listenSynth, [\name, "creature", \buffer, b, \specCentroidBusName, ~specCentroidBus, \specFlatnessBusName, ~specFlatnessBus, \mfccBusName, ~mfccBus]);
 )
 (
 // Clear everything out
 r.stop;
 b.free;
 ~specCentroidBus.free;
 ~specFlatnessBus.free;
 ~mfccBus.free;
 ~listenSynth.free;
 )
 

Comments

Submit comment