{
   "name" : "analyze a sample with SCMIR and sort sample segments by similarity",
   "author" : "grirgz",
   "ancestor_list" : [],
   "description" : "",
   "labels" : [
      "example",
      "sample",
      "analysis",
      "scmir",
      "similarity",
      "clustering",
      "feature",
      "extraction"
   ],
   "is_private" : null,
   "id" : "1-5fj",
   "code" : "~file = \"~/Musique/sc/samplekit/loop/lovin.flac\".standardizePath;\r\n\r\ne = SCMIRAudioFile(~file, [[MFCC, 13], [Chromagram, 12]]); // open file for SCMIR\r\nb = Buffer.read(Server.default, ~file); // buffer for the Pbind\r\n{e.extractFeatures()}.fork; // extract features before creating similarityMatrix\r\nm = e.similarityMatrix(unit:10, metric:2); // create similarityMatrix    \r\n\r\n(\r\n// simple buffer player\r\nSynthDef(\\playersec, { arg out=0, amp=0.1, gate=1, pan=0, freq=200, bufnum, speed=1, pos=0, doneAction=2, loop=0, trigger=1, velamp=1;\r\n\t// pos in seconds\r\n\tvar sig;\r\n\tvar sig1;\r\n\tsig = PlayBuf.ar(2, bufnum, BufRateScale.kr(bufnum) * speed, trigger, startPos: (pos*BufSampleRate.kr(bufnum)), doneAction:doneAction, loop: loop);\r\n\tsig = sig * EnvGen.ar(\\adsr.kr(Env.adsr(0.001,0,1,0.01)),gate,doneAction:doneAction);\r\n\t//sig = Pan2.ar(sig, pan, 1).sum;\r\n\tOut.ar(out, sig * \\gain.kr(1) * velamp);\r\n}).add;\r\n);\r\n\r\n(\r\nPdef(\\sortsample,\r\n\tPbind(\r\n\t\t\\instrument, \\playersec,\r\n\t\t\\bufnum, b,\r\n\t\t// take sample at column 105 and play other columns by order of similarity\r\n\t\t// pos is position in the sample in seconds\r\n\t\t// column 105 start at 105*m.reducedrows+0 and ends at 105*m.reducedrows+m.reducedcolumns\r\n\t\t// we take this slice of array, sort it and return the indexes (.order)\r\n\t\t// then scale column number to position in second\r\n\t\t\\pos, Pseq(m.matrix[105*m.reducedrows+(0..m.reducedcolumns-1)].order) * b.duration/m.reducedrows, \r\n\t\t\\sustain, b.duration/m.reducedrows,\r\n\t\t\\dur, 1,\r\n\t\t\\gain, 1,\r\n\t)\r\n).play;\r\n);"
}
