//central class for music information retrieval operations in SC //class variables and methods only, singleton class SS_SCMIR { //for auxilliary operations classvar lamelocation; classvar <>nrtoutputfilename; classvar <>nrtanalysisfilename; *initClass { soundfile = SoundFile.new; samplingrate = 44100; //framesize = 1024; framehop = 1024; framerate = samplingrate/framehop; hoptime = framerate.reciprocal; // if (( Main.scVersionMajor == 3) && (Main.scVersionMinor<5)) { // executabledirectory = SS_SCMIR.filenameSymbol.asString.dirname.escapeChar($ )++"/scmirexec/"; // // } //used to be packaged with scmirexec in classes directory, no longer executabledirectory = SS_SCMIR.filenameSymbol.asString.dirname.escapeChar($ )++"/../scmirexec/"; //tempdir = SS_SCMIR.filenameSymbol.asString.dirname++"/scmirtemp/"; tempdir = Platform.defaultTempDir; //user should always have write permission here? SS_SCMIR.initGlobalFeatureNorms; lamelocation = "/usr/local/bin/lame"; nrtoutputfilename = "/dev/null"; nrtanalysisfilename = tempdir++"NRTanalysis"; //Windows has no /dev/null for the NRT render to write its (unused) audio //to — use a real file in the temp dir instead. (The old check compared //platform.name.CLASS — the class Symbol — against \windows, so it never //fired: every Windows NRT analysis died unable to open its output file, //and no sound file could be loaded at all. The old fallback "NRToutput" //was also relative to the CWD, which is not writable under Program Files.) if(thisProcess.platform.name == \windows) { nrtoutputfilename = tempdir ++ "NRToutput.wav"; }; } //warning: this method can invalidate all previous SCMIR method calls, and conflict with saved information //for individual files, can check using numframes and duration versus current SCMIR.framerate*duration *setFrameHop {|hopsize| if ((hopsize!= 1024) && (hopsize!=512)) { "SS_SCMIR:setFrameHop: only hopsize of 1024 or 512 supported".postln; ^nil}; framehop = hopsize; framerate = samplingrate/framehop; hoptime = framerate.reciprocal; } //frame hop length in seconds // hopTime { // // ^hoptime; //framerate.reciprocal; // // } //COMPENSATION REQUIRED since triggering on chromafft, will be storing values based on initial delay of 4096 samples //should associate value with 2048 samples in halfway through window *frameTime{|whichframe| ^(framehop*whichframe+2048)/samplingrate} *setTempDir {|tmpdirectory| //can do further tests here tempdir = tmpdirectory; //if nil, will construct as go from paths passed in if(tempdir.notNil,{ if(tempdir.last!=$/,{ tempdir = tempdir++"/"; }); }); } *getTempDir { //return empty string as prefix otherwise ^ tempdir?""; } *setExecutableDir {|execdir| //can do further tests here executabledirectory = execdir; //if nil, will construct as go from paths passed in if(executabledirectory.notNil,{ if(executabledirectory.last!=$/,{ executabledirectory = executabledirectory++"/"; }); }); } //try to force garbage collection, close all pipes, wait for a moment *clearResources { var temp; //"clear resources".postln; Pipe.closeAll; //safety over long haul? //UnixFILE.openFiles.postln; UnixFILE.closeAll; temp = 0!100; temp = nil; temp = 0!100; //hopefully force garbage collection round if(thisThread.class==Routine) { //really pause 1.0.wait; }; } //return true if in Routine, else in Main thread and .wait not valid *waitStatus { ^(thisThread.class==Routine); //Thread } *waitIfRoutine {|time| if(thisThread.class==Routine) { time.wait; } } *external {|command, scorerender=false, limit=2000| //command.postln; if(thisThread.class==Routine) { SS_SCMIR.waitOnUnixCmd(command, limit); if(scorerender) { //safety first, file not being written out quickly enough 0.1.wait; }; //unworkable since blocks SC even if within separate thread // if(scorerender) { // systemCmd(command); // } { // SCMIR.waitOnUnixCmd(command, limit); // // }; } { if(scorerender) { systemCmd(command); } { SS_SCMIR.pipe(command); }; } } //wait on process, assumed within Routine *processWait {|processname, limit=2000| var a, count; var checkstring = "ps -xc | grep '"++processname++"'"; count=0; 0.01.wait; //make sure process running first (can be zero but better safe than sorry) while({ a=checkstring.systemCmd; //256 if not running, 0 if running if(count%10==0,{(processname+"running for"+(count.div(10))+"seconds").postln}); (a==0) and: {(count = count+1) ` polling: Windows has //no `ps`, so the wait fell through after ~0.1s and the analysis file was //read back while scsynth NRT was still writing it (or hadn't started). *waitOnUnixCmd {|command, limit=2000| var done = false; var count = 1; command.unixCmd({done = true}); while({ if(count%10==0,{("SS_SCMIR: calculation running for"+(count.div(10))+"seconds").postln}); done.not and: {(count = count+1) 0,{sep= sep-1;}); if ((val>threshold) and: (sep==0)) {list.add(i); sep= minseparation; } }; ^[list, peakcurve] } *initGlobalFeatureNorms { globalfeaturenorms = nil; } //expert use implied *setGlobalFeatureNorms {|norms| globalfeaturenorms = norms; } //could make a dictionary over all observations ever, just a bit trickier? //*lookupGlobalFeatureNorm {|featuretype,normalizationtype| // // } //assumes you know what you're doing, that format of featureinfo used to extract globals is same as that now being used //and that values do exist *lookupGlobalFeatureNorms {|which| ^[globalfeaturenorms[0][which],globalfeaturenorms[1][which]]; } *saveGlobalFeatureNorms {|filename| var archive; filename = filename ?? { (SS_SCMIR.getTempDir)++"globalfeaturenorms.scmirZ" }; archive = SS_SCMIRZArchive.write(filename); archive.writeItem(globalfeaturenorms); archive.writeClose; } *loadGlobalFeatureNorms {|filename| var archive; filename = filename ?? {(SS_SCMIR.getTempDir)++"globalfeaturenorms.scmirZ"}; archive = SS_SCMIRZArchive.read(filename); globalfeaturenorms = archive.readItem; archive.close; } //run normalization procedures for all standard features over all filenames in list, obtaining global max and min, and mean and stddev *findGlobalFeatureNorms {|filenamelist, featureinfo, normalizationtype=0,filestart=0,filedur=0,numquantiles=10,whichchannel| var e; var norms; var framesum = 0, framesumr; var temp1, temp2; var durations; norms= nil!(filenamelist.size); durations = 0.0!(filenamelist.size); filenamelist.do {|filename,j| [j,filename].postln; e = SS_SCMIRAudioFile(filename,featureinfo, normalizationtype,filestart,filedur); //if nil passed in, will pass on nil and thence go with standard behaviour e.extractFeatures(false,whichchannel:whichchannel); norms[j] = [e.normalize(e.featuredata, true), e.numframes]; norms[j].postln; durations[j] = e.duration; framesum = framesum + e.numframes; }; switch(normalizationtype,0,{ //normalize temp1 = norms[0][0][0]; temp2 = norms[0][0][1]; norms.do{|val| temp1 = min(val[0][0],temp1); temp2 = max(val[0][1],temp2); }; //combine globalfeaturenorms = [temp1,temp2]; },1,{ //standardize framesumr = 1.0/framesum; temp1 = 0.0; temp2 = 0.0; norms.do{|val| temp1 = temp1 + (val[0][0]*val[1]*framesumr); temp2 = temp2 + (val[0][1]*val[1]*framesumr); }; globalfeaturenorms = [temp1,temp2.sqrt]; //to stddev from variance at this point },{ //quantilisation var lastworking = 0; globalfeaturenorms = {[]}!(norms[0][0][0].size); //accumulate all data over files norms.do{|val| var rawdata = val[0][0]; //var numframes = val[1]; rawdata.do{|datapoints,i| if(datapoints.notNil) { globalfeaturenorms[i] = globalfeaturenorms[i] ++ datapoints; }; }; }; //find quantiles, could be slow for larger array and sorting globalfeaturenorms = globalfeaturenorms.collect{|datapoints,i| ("finding quantile "++ i).postln; if(datapoints.notNil,{ datapoints.percentile((1.0/numquantiles)*(0,1..numquantiles)); },nil); }; //copies for connected features in groups globalfeaturenorms.do{|info,i| if(info.isNil) { globalfeaturenorms[i] = globalfeaturenorms[lastworking]; } { lastworking = i; } }; //required for compatibility with the way norm and standardisation achieved globalfeaturenorms = [globalfeaturenorms,[]]; }); ^durations } //for adding multiple sound file feature instances to *createARFF {|filename,numfeatures,classes| var file; filename = filename ?? {(SS_SCMIR.getTempDir)++"features.arff"}; numfeatures = numfeatures ? 1; classes = classes ? ["class1","class2"]; file = File(filename,"w"); file.write("@RELATION SCMIR\n"); numfeatures.do{|i| file.write("@ATTRIBUTE"+i+"NUMERIC\n"); }; file.write("@ATTRIBUTE class {"); classes.do{|class,i| file.write(class.asString); if((i+1)