{
   "description" : "template code for sending lots of data (1024 fft values 61 times per second) from sc to processing.  sound function from an old sc-tweet.  updated to work with sc3.9 and processing 3.3.6",
   "ancestor_list" : [],
   "author" : "Fredrik Olofsson",
   "name" : "example fft sc->processing",
   "code" : "/*\r\n//--processing code:\r\nimport oscP5.*;\r\nimport netP5.*;\r\nOscP5 oscP5;\r\n\r\nfinal int BUFFERSIZE= 2048;  //should correspond with fft size in supercollider\r\nfinal int BUFFERSIZE2= BUFFERSIZE/2; \r\n\r\nfloat[] fftArray;\r\n\r\nvoid setup() {\r\n  size(1280, 768);\r\n  frameRate(60);\r\n  background(0);\r\n  noSmooth();\r\n\r\n  fftArray= new float[BUFFERSIZE2];\r\n  for (int i= 0; i<BUFFERSIZE2; i++) {\r\n    fftArray[i]= 0.0;\r\n  }\r\n\r\n  //--network\r\n  OscProperties properties= new OscProperties();\r\n  //properties.setRemoteAddress(\"127.0.0.1\", 57120);  //osc send port (to sc)\r\n  properties.setListeningPort(47120);               //osc receive port (from sc)\r\n  //properties.setSRSP(OscProperties.ON);  //unused\r\n  //properties.setDatagramSize(min(BUFFERSIZE*4, 8192));\r\n  properties.setDatagramSize(5136);  //5136 is the minimum \r\n  oscP5= new OscP5(this, properties);\r\n}\r\n\r\nvoid oscEvent(OscMessage msg) {\r\n  if (msg.checkAddrPattern(\"/fftArray\")) {\r\n    for (int i= 0; i<BUFFERSIZE2; i++) {\r\n      fftArray[i]= msg.get(i).floatValue();\r\n    }\r\n  }\r\n}\r\n\r\nvoid draw() {\r\n  background(0);\r\n  noFill();\r\n  for (int x= 0; x<BUFFERSIZE2; x++) {\r\n    stroke(fftArray[x]*255.0, 0, 0);\r\n    line(x, height*0.1, x, height*0.9);\r\n  }\r\n}\r\n*/\r\n\r\n//--supercollider code:\r\n(\r\nvar buffersize= 2048;\r\nvar buffersize2= buffersize.div(2);\r\nn= NetAddr(\"127.0.0.1\", 47120);\r\ns.options.memSize= 8192*2;\r\ns.waitForBoot{\r\n\tc= Bus.control(s, buffersize2);\r\n\tSynthDef(\\avTrk, {|in= 0, t_trig= 0, amp= 1, bus|\r\n\t\tvar z= Mix(In.ar(in, 2)*amp);\r\n\t\tvar chain= FFT(LocalBuf(buffersize), z);\r\n\t\tArray.fill(buffersize2, {|i|\r\n\t\t\tvar a= Unpack1FFT(chain, buffersize, i);\r\n\t\t\tvar d= Demand.kr(chain>=0, 0, a);\r\n\t\t\tOut.kr(bus+i, d.min(1));\r\n\t\t});\r\n\t}).load;\r\n\ts.sync;\r\n\t~trk= Synth(\\avTrk, [\\in, 0, \\amp, 0.3, \\bus, c]);\r\n\ta= play{var a=SinOsc;Splay.ar(a.ar(PulseCount.ar(f=InFeedback.ar(0,2).sum)%999+(60,63.0005..99)*a.ar(2**f)*2+[3,4],f>0*f*9)).tanh*MouseX.kr(0, 1)};\r\n\t\r\n\tRoutine.run({\r\n\t\tinf.do{\r\n\t\t\tvar fftArray= c.getnSynchronous(buffersize2);\r\n\t\t\tn.sendMsg(\\fftArray, *fftArray);  //sending 1024 values\r\n\t\t\t(1/61).wait;  //a tiny bit faster than framerate\r\n\t\t};\r\n\t});\r\n\tCmdPeriod.doOnce({c.free});\r\n};\r\n)",
   "id" : "1-4Ty",
   "is_private" : null,
   "labels" : [
      "processing"
   ]
}
