Exemple #1
0
void
distance_matrix()
{
  if (!wekafname_Set()) return;

  cout << "Distance matrix calculation using " << wekafname_ << endl;

  wekafname_  = inputdir_ + wekafname_;

  MarSystemManager mng;

  MarSystem* net = mng.create("Series", "net");

  MarSystem* wsrc = mng.create("WekaSource", "wsrc");
  net->addMarSystem(wsrc);
	//!!!: mode control
	net->updControl("WekaSource/wsrc/mrs_string/validationMode", "OutputInstancePair");
	net->updControl("WekaSource/wsrc/mrs_bool/normMaxMin", true);
  net->updControl("WekaSource/wsrc/mrs_string/filename", wekafname_);


	MarSystem* dmatrix = mng.create("SelfSimilarityMatrix", "dmatrix");
  dmatrix->addMarSystem(mng.create("Metric", "dmetric"));
  dmatrix->updControl("Metric/dmetric/mrs_string/metric", "euclideanDistance");
	//!!!: lmartins: normalization can only be applied when we have all feature vectors in memory...
	//... which is what we are trying to avoid here (having big realvecs in memory)...
  //dmatrix->updControl("mrs_string/normalize", "MinMax");
  net->addMarSystem(dmatrix);
	//!!!: mode control
	net->updControl("SelfSimilarityMatrix/dmatrix/mrs_natural/mode", 1); //FIXME: replace use of enum for strings?

	//link controls between WekaSource and SelfSimilarityMatrix
	net->linkControl("SelfSimilarityMatrix/dmatrix/mrs_natural/nInstances",
									 "WekaSource/wsrc/mrs_natural/nInstances");
	net->linkControl("WekaSource/wsrc/mrs_realvec/instanceIndexes",
									 "SelfSimilarityMatrix/dmatrix/mrs_realvec/instanceIndexes");

	ofstream oss;
	oss.open(distancematrix_.c_str());
	oss << "Marsyas-kea distance matrix" << endl;

	while(!net->getctrl("SelfSimilarityMatrix/dmatrix/mrs_bool/done")->to<bool>())
	{
		const mrs_realvec& idxs = net->getctrl("SelfSimilarityMatrix/dmatrix/mrs_realvec/instanceIndexes")->to<mrs_realvec>();
		oss << "(" << mrs_natural(idxs(0)) << "," << mrs_natural(idxs(1)) << ") = ";

		net->tick();

		const mrs_realvec& value = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
		oss << value(0) << endl;
	}

  oss << endl;
}
Exemple #2
0
int
isClose(string infile1, string infile2)
{
  MarSystemManager mng;
  MarSystem* pnet = mng.create("Series", "pnet");

  MarSystem* invnet = mng.create("Series", "invnet");
  invnet->addMarSystem(mng.create("SoundFileSource", "src2"));
  invnet->updControl("SoundFileSource/src2/mrs_string/filename", infile2);
  invnet->addMarSystem(mng.create("Negative", "neg"));

  MarSystem* fanout = mng.create("Fanout", "fanout");
  fanout->addMarSystem(mng.create("SoundFileSource", "src1"));
  fanout->updControl("SoundFileSource/src1/mrs_string/filename", infile1);
  fanout->addMarSystem(invnet);

  pnet->addMarSystem(fanout);
  pnet->addMarSystem(mng.create("Sum", "sum"));
  pnet->linkControl("mrs_bool/hasData",
                    "Fanout/fanout/SoundFileSource/src1/mrs_bool/hasData");

  mrs_natural i;
  mrs_natural samples =
    pnet->getctrl("mrs_natural/inSamples")->to<mrs_natural>();
  while ( pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>() )
  {
    pnet->tick();
    const realvec& processedData =
      pnet->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
    for (i=0; i<samples; ++i)
    {
      //  useful for tweaking CLOSE_ENOUGH
      //cout<<processedData(i)<<" ";
      if ( abs(processedData(i)) > CLOSE_ENOUGH )
      {
        delete pnet;
        return(1);
      }
    }
  }
  delete pnet;
  return 0;
}
void
peakClusteringEval(realvec &peakSet, string sfName, string outsfname, string noiseName, string mixName, string intervalFrequency, string panningInfo, mrs_real noiseDelay, string T, mrs_natural N, mrs_natural Nw, 
				   mrs_natural D, mrs_natural S, mrs_natural C,
				   mrs_natural accSize, mrs_natural synthetize, mrs_real *snr0)
{
	//FIXME: D is the same as hopSize_ -> fix to avoid confusion!

	MATLAB_EVAL("clear");

	MarSystemManager mng;

	cout << "Extracting Peaks and Computing Clusters..." << endl;
	
	//**************************************************
	// create the peakClusteringEval network
	//**************************************************
	MarSystem* mainNet = mng.create("Series", "mainNet");

	//**************************************************************************
	//create accumulator for the texture window and add it to the main network
	//**************************************************************************
	MarSystem* textWinNet = mng.create("Accumulator", "textWinNet");
	mainNet->addMarSystem(textWinNet);

	//************************************************************************
	//create Analysis Network and add it to the texture window accumulator
	//************************************************************************
	MarSystem* analysisNet = mng.create("Series", "analysisNet");
	textWinNet->addMarSystem(analysisNet);

	//************************************************************************
	//create FanInOut for mixing with a noise source and add to Analysis Net
	//************************************************************************
	MarSystem* mixer = mng.create("FanOutIn", "mixer");
	//---- create original series and add it to mixer
	MarSystem* oriNet = mng.create("Series", "oriNet");
	if (microphone_) 
		oriNet->addMarSystem(mng.create("AudioSource", "src"));
	else
	{
		//oriNet->addMarSystem(mng.create("SoundFileSource", "src"));
		oriNet->addMarSystem(mng.create("MidiFileSynthSource", "src")); //[!]
	}
	oriNet->addMarSystem(mng.create("Gain", "oriGain"));
	mixer->addMarSystem(oriNet);
	//---- create a series for the noiseSource
	if(noiseName != EMPTYSTRING)
	{
		MarSystem* mixseries = mng.create("Series", "mixseries");
		if(noiseName == "white")
			mixseries->addMarSystem(mng.create("NoiseSource", "noise"));
		else
			mixseries->addMarSystem(mng.create("SoundFileSource", "noise"));

		mixseries->addMarSystem(mng.create("Delay", "noiseDelay"));
		MarSystem* noiseGain = mng.create("Gain", "noiseGain");
		mixseries->addMarSystem(noiseGain);
		// add this series in the fanout
		mixer->addMarSystem(mixseries);
	}
	//add Mixer to analysis network
	analysisNet->addMarSystem(mixer);

	//********************************************************
	// create SoundFileSink and add it to the analysis net
	//********************************************************
	if(noiseName != EMPTYSTRING)
		analysisNet->addMarSystem(mng.create("SoundFileSink", "mixSink"));

	
	//***********************************************************
	// create peakExtract network and add it to the analysis net
	//***********************************************************
	MarSystem* peakExtract = mng.create("Series","peakExtract");
	peakExtract->addMarSystem(mng.create("ShiftInput", "si")); //[?]

	MarSystem* stereoFo = mng.create("Fanout","stereoFo");
	// create Spectrum Network and add it to the analysis net
	MarSystem* spectrumNet = mng.create("Series", "spectrumNet");
	spectrumNet->addMarSystem(mng.create("Stereo2Mono","s2m"));

	//onset detector
	MarSystem* onsetdetector = mng.create("FlowThru", "onsetdetector");
	//onsetdetector->addMarSystem(mng.create("ShiftInput", "si"));
	onsetdetector->addMarSystem(mng.create("Windowing", "win")); 
	onsetdetector->addMarSystem(mng.create("Spectrum","spk"));
	onsetdetector->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	onsetdetector->addMarSystem(mng.create("Flux", "flux")); 
	onsetdetector->addMarSystem(mng.create("ShiftInput","sif"));
	onsetdetector->addMarSystem(mng.create("Filter","filt1"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev1"));
	onsetdetector->addMarSystem(mng.create("Filter","filt2"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev2"));
	onsetdetector->addMarSystem(mng.create("PeakerOnset","peaker")); 
	spectrumNet->addMarSystem(onsetdetector);
		
	spectrumNet->addMarSystem(mng.create("Shifter", "sh"));
	spectrumNet->addMarSystem(mng.create("Windowing", "wi"));
	MarSystem* parallel = mng.create("Parallel", "par");
	parallel->addMarSystem(mng.create("Spectrum", "spk1"));
	parallel->addMarSystem(mng.create("Spectrum", "spk2"));
	spectrumNet->addMarSystem(parallel);
	// add spectrumNet to stereo fanout
	stereoFo->addMarSystem(spectrumNet);
	//
	//create stereo spectrum net
	MarSystem* stereoSpkNet = mng.create("Series","stereoSpkNet");
	MarSystem* LRnet = mng.create("Parallel","LRnet");
	//
	MarSystem* spkL = mng.create("Series","spkL");
	spkL->addMarSystem(mng.create("Windowing","win"));
	spkL->addMarSystem(mng.create("Spectrum","spk"));
	LRnet->addMarSystem(spkL);
	//
	MarSystem* spkR = mng.create("Series","spkR");
	spkR->addMarSystem(mng.create("Windowing","win"));
	spkR->addMarSystem(mng.create("Spectrum","spk"));
	LRnet->addMarSystem(spkR);
	//
	//add it to the stereo spectrum net series
	stereoSpkNet->addMarSystem(LRnet);
	//
	//add stereo spectrum object to stereo spectrum net
	//stereoSpkNet->addMarSystem(mng.create("StereoSpectrum","stereoSpk")); //AVENDANO
	stereoSpkNet->addMarSystem(mng.create("EnhADRess","ADRess"));//enhADRess_1
	stereoSpkNet->addMarSystem(mng.create("EnhADRessStereoSpectrum","stereoSpk")); //enhADRess_2
	//
	// add the stereo Spectrum net to the Fanout
	stereoFo->addMarSystem(stereoSpkNet);
	//
	// add the fanout to the peakExtract net
	peakExtract->addMarSystem(stereoFo);
	//
	//add peakExtract net to analysis net 
	analysisNet->addMarSystem(peakExtract);

	//***************************************************************
	//add PeakConvert to main SERIES for processing texture windows
	//***************************************************************
	mainNet->addMarSystem(mng.create("PeakConvert", "conv"));
	mainNet->linkControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_natural/winSize",
						 "Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/ShiftInput/si/mrs_natural/winSize");

	//***************************************************************
	//create a FlowThru for the Clustering Network and add to main net
	//***************************************************************
	MarSystem* clustNet = mng.create("FlowThru", "clustNet");
	mainNet->addMarSystem(clustNet);

	//***************************************************************
	// create Similarities Network and add it to ClustNet
	//***************************************************************
	MarSystem* simNet = mng.create("FanOutIn", "simNet");
	simNet->updControl("mrs_string/combinator", "*");
	//
	//create Frequency similarity net and add it to simNet
	//
	MarSystem* freqSim = mng.create("Series","freqSim");
	//--------
	freqSim->addMarSystem(mng.create("PeakFeatureSelect","FREQfeatSelect"));
	freqSim->updControl("PeakFeatureSelect/FREQfeatSelect/mrs_natural/selectedFeatures",
					 PeakFeatureSelect::pkFrequency | PeakFeatureSelect::barkPkFreq);
	//--------
	MarSystem* fsimMat = mng.create("SelfSimilarityMatrix","FREQsimMat");
	fsimMat->addMarSystem(mng.create("Metric","FreqL2Norm"));
	fsimMat->updControl("Metric/FreqL2Norm/mrs_string/metric","euclideanDistance");
	fsimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//fsimMat->updControl("mrs_string/normalize", "MinMax");
	//fsimMat->linkControl("mrs_realvec/covMatrix", "Metric/FreqL2Norm/mrs_realvec/covMatrix");
	freqSim->addMarSystem(fsimMat);	
	//--------
	freqSim->addMarSystem(mng.create("RBF","FREQrbf"));
	freqSim->updControl("RBF/FREQrbf/mrs_string/RBFtype","Gaussian");
	freqSim->updControl("RBF/FREQrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(freqSim);
	//
	//create Amplitude similarity net and add it to simNet
	//
	MarSystem* ampSim = mng.create("Series","ampSim");
	//--------
	ampSim->addMarSystem(mng.create("PeakFeatureSelect","AMPfeatSelect"));
	ampSim->updControl("PeakFeatureSelect/AMPfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkAmplitude | PeakFeatureSelect::dBPkAmp);
	//--------
	MarSystem* asimMat = mng.create("SelfSimilarityMatrix","AMPsimMat");
	asimMat->addMarSystem(mng.create("Metric","AmpL2Norm"));
	asimMat->updControl("Metric/AmpL2Norm/mrs_string/metric","euclideanDistance");
	asimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//asimMat->updControl("mrs_string/normalize", "MinMax");
	//asimMat->linkControl("mrs_realvec/covMatrix", "Metric/AmpL2Norm/mrs_realvec/covMatrix");
	ampSim->addMarSystem(asimMat);	
	//--------
	ampSim->addMarSystem(mng.create("RBF","AMPrbf"));
	ampSim->updControl("RBF/AMPrbf/mrs_string/RBFtype","Gaussian");
	ampSim->updControl("RBF/AMPrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(ampSim);
	//
	//create HWPS similarity net and add it to simNet
	//
	MarSystem* HWPSim = mng.create("Series","HWPSim");
	//--------
	HWPSim->addMarSystem(mng.create("PeakFeatureSelect","HWPSfeatSelect"));
	HWPSim->updControl("PeakFeatureSelect/HWPSfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkFrequency | PeakFeatureSelect::pkSetFrequencies | PeakFeatureSelect::pkSetAmplitudes);
	//--------
	MarSystem* HWPSsimMat = mng.create("SelfSimilarityMatrix","HWPSsimMat");
	HWPSsimMat->addMarSystem(mng.create("HWPS","hwps"));
	HWPSsimMat->updControl("HWPS/hwps/mrs_bool/calcDistance", true);
	HWPSim->addMarSystem(HWPSsimMat);	
	//--------
	HWPSim->addMarSystem(mng.create("RBF","HWPSrbf"));
	HWPSim->updControl("RBF/HWPSrbf/mrs_string/RBFtype","Gaussian");
	HWPSim->updControl("RBF/HWPSrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(HWPSim);
	//
	//create Panning similarity net and add it to simNet
	//
	MarSystem* panSim = mng.create("Series","panSim");
	//--------
	panSim->addMarSystem(mng.create("PeakFeatureSelect","PANfeatSelect"));
	panSim->updControl("PeakFeatureSelect/PANfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkPan);
	//--------
	MarSystem* psimMat = mng.create("SelfSimilarityMatrix","PANsimMat");
	psimMat->addMarSystem(mng.create("Metric","PanL2Norm"));
	psimMat->updControl("Metric/PanL2Norm/mrs_string/metric","euclideanDistance");
	psimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//psimMat->updControl("mrs_string/normalize", "MinMax");
	//psimMat->linkControl("mrs_realvec/covMatrix", "Metric/PanL2Norm/mrs_realvec/covMatrix");
	panSim->addMarSystem(psimMat);	
	//--------
	panSim->addMarSystem(mng.create("RBF","PANrbf"));
	panSim->updControl("RBF/PANrbf/mrs_string/RBFtype","Gaussian");
	panSim->updControl("RBF/PANrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(panSim);
	//
	// LINK controls of PeakFeatureSelects in each similarity branch
	//
	simNet->linkControl("Series/ampSim/PeakFeatureSelect/AMPfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	simNet->linkControl("Series/HWPSim/PeakFeatureSelect/HWPSfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	simNet->linkControl("Series/panSim/PeakFeatureSelect/PANfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	//------
	simNet->linkControl("Series/ampSim/PeakFeatureSelect/AMPfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	simNet->linkControl("Series/HWPSim/PeakFeatureSelect/HWPSfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	simNet->linkControl("Series/panSim/PeakFeatureSelect/PANfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//add simNet to clustNet
	clustNet->addMarSystem(simNet);
	//
	// LINK controls related to variable number of peak from PeakConvert to simNet
	//
	mainNet->linkControl("FlowThru/clustNet/FanOutIn/simNet/Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks",
					  "PeakConvert/conv/mrs_natural/totalNumPeaks");
	mainNet->linkControl("FlowThru/clustNet/FanOutIn/simNet/Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks",
					  "PeakConvert/conv/mrs_natural/frameMaxNumPeaks");

	//***************************************************************
	// create NCutNet MarSystem and add it to clustNet
	//***************************************************************
// 	MarSystem* NCutNet = mng.create("Series","NCutNet");
// 	clustNet->addMarSystem(NCutNet);
// 	//---add NCutNet components
// 	// add a stack to stack the 
// 	MarSystem* stack = mng.create("Fanout","stack");
// 	NCutNet->addMarSystem(stack);
// 	stack->addMarSystem(mng.create("NormCut","NCut"));
// 	stack->addMarSystem(mng.create("Gain", "ID"));
// 	// add the cluster selection module
// 	NCutNet->addMarSystem(mng.create("PeakClusterSelect","clusterSelect"));
	
	// Do not select most prominent clusters anymore
	clustNet->addMarSystem(mng.create("NormCut","NCut")); //[!]

	//***************************************************************
	// create PeakLabeler MarSystem and add it to mainNet
	//***************************************************************
	MarSystem* labeler = mng.create("PeakLabeler","labeler");
	mainNet->addMarSystem(labeler);
	//---- link labeler label control to the NCut output control
	mainNet->linkControl("PeakLabeler/labeler/mrs_realvec/peakLabels", "FlowThru/clustNet/mrs_realvec/innerOut");

	//***************************************************************
	// create PeakViewSink MarSystem and add it to mainNet
	//***************************************************************
	if(peakStore_)
	{
		mainNet->addMarSystem(mng.create("PeakViewSink", "peSink"));
		mainNet->updControl("PeakViewSink/peSink/mrs_string/filename", filePeakName);
	}

	//****************************************************************
	// Create Synthesis Network
	//****************************************************************
	MarSystem* postNet = mng.create("Series", "postNet");

	// Create Peak Synth ////////////////////////////////////
	MarSystem* peakSynth = mng.create("Series", "peakSynth");
	peakSynth->addMarSystem(mng.create("PeakSynthOsc", "pso"));
	peakSynth->addMarSystem(mng.create("Windowing", "wiSyn"));
	peakSynth->addMarSystem(mng.create("OverlapAdd", "ov"));
	peakSynth->addMarSystem(mng.create("Gain", "outGain"));
// 		MarSystem *dest;
// 		dest = new SoundFileSink("dest");
// 		//dest->updControl("mrs_string/filename", outsfname);
// 		peakSynth->addMarSystem(dest);
	mng.registerPrototype("PeakSynth", peakSynth);

	// Create Bank of Synths ////////////////////////////////////////////////
	MarSystem* synthBank = mng.create("Fanout","synthBank");
	mrs_natural numSynths = 10; // HARDCODED FOR NOW [!]
	synthBank->addMarSystem(mng.create("PeakSynth", "synthCluster_0"));
	synthBank->updControl("PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_natural/peakGroup2Synth", 0);
	for(mrs_natural s=1; s < numSynths; ++s)
	{
		ostringstream oss;
		oss << "synthCluster_" << s;
		synthBank->addMarSystem(mng.create("PeakSynth", oss.str()));
		//link controls between branches
		synthBank->linkControl("PeakSynth/"+oss.str()+"/PeakSynthOsc/pso/mrs_real/samplingFreq",
							   "PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_real/samplingFreq");
		synthBank->linkControl("PeakSynth/"+oss.str()+"/PeakSynthOsc/pso/mrs_natural/delay",
							   "PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_natural/delay");
		synthBank->linkControl("PeakSynth/"+oss.str()+"/PeakSynthOsc/pso/mrs_natural/synSize",
							   "PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_natural/synSize");
		synthBank->linkControl("PeakSynth/"+oss.str()+"/Windowing/wiSyn/mrs_string/type",
							   "PeakSynth/synthCluster_0/Windowing/wiSyn/mrs_string/type");

		synthBank->updControl("PeakSynth/"+oss.str()+"/PeakSynthOsc/pso/mrs_natural/peakGroup2Synth", s);
	}

	postNet->addMarSystem(synthBank);
	
	// SHREDDER /////////////////////////////////////////////////
	MarSystem* synthNet = mng.create("Shredder", "synthNet");
	synthNet->addMarSystem(postNet);
	synthNet->updControl("mrs_bool/accumulate", true); //accumulate output
	mainNet->addMarSystem(synthNet);
	//link Shredder nTimes to Accumulator nTimes
	mainNet->linkControl("Shredder/synthNet/mrs_natural/nTimes",
					  "Accumulator/textWinNet/mrs_natural/nTimes");

	// add a MATLAB sink at the very end of the network!
	mainNet->addMarSystem(mng.create("PlotSink", "send2MATLAB"));
	mainNet->updControl("PlotSink/send2MATLAB/mrs_bool/sequence", false);
	mainNet->updControl("PlotSink/send2MATLAB/mrs_bool/messages", false);
	mainNet->updControl("PlotSink/send2MATLAB/mrs_bool/matlab", true);
	mainNet->updControl("PlotSink/send2MATLAB/mrs_string/matlabCommand",
					 "evaluateTextWin;");
	
	//****************************************************************
	
	////////////////////////////////////////////////////////////////
	// update the controls
	////////////////////////////////////////////////////////////////
	//mainNet->updControl("Accumulator/textWinNet/mrs_natural/nTimes", accSize);

	if (microphone_) 
	{
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("mrs_natural/inObservations", 1);
	}
	else
	{
		cout << ">> Loading MIDI file and synthetizing audio data..." << endl;
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("mrs_real/israte", samplingFrequency_);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_real/start", 10.0);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_real/end", 20.0);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_string/filename", sfName);
		//mainNet->updControl("mrs_natural/inObservations", 1);
		//samplingFrequency_ = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_real/osrate")->to<mrs_real>();
	}

	if(noiseName != EMPTYSTRING)
	{
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/SoundFileSource/noise/mrs_string/filename", noiseName);
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/NoiseSource/noise/mrs_string/mode", "rand");
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Seriesmixseries/Delay/noiseDelay/mrs_real/delaySeconds",  noiseDelay);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/Gain/noiseGain/mrs_real/gain", noiseGain_);
	}

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/ShiftInput/si/mrs_natural/winSize", Nw+1);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Shifter/sh/mrs_natural/shift", 1);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_bool/zeroPhasing", true);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_bool/zeroPhasing", true);
	//
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_bool/zeroPhasing", true);

	if(unprecise_)
		mainNet->updControl("PeakConvert/conv/mrs_bool/improvedPrecision", false);      
	else
		mainNet->updControl("PeakConvert/conv/mrs_bool/improvedPrecision", true);  
	
	if(noPeakPicking_)
		mainNet->updControl("PeakConvert/conv/mrs_bool/picking", false);      
	
	mainNet->updControl("PeakConvert/conv/mrs_natural/frameMaxNumPeaks", S); 
	mainNet->updControl("PeakConvert/conv/mrs_string/frequencyInterval", intervalFrequency);  
	mainNet->updControl("PeakConvert/conv/mrs_natural/nbFramesSkipped", 0);//(N/D));  

	//mainNet->updControl("FlowThru/clustNet/Series/NCutNet/Fanout/stack/NormCut/NCut/mrs_natural/numClusters", C); [!]
	//mainNet->updControl("FlowThru/clustNet/Series/NCutNet/PeakClusterSelect/clusterSelect/mrs_natural/numClustersToKeep", nbSelectedClusters_);//[!]
	//
	if(C > 0) //use fixed and manually set number of clusters
	{
		cout << ">> Using fixed number of clusters: " << C << " clusters." << endl;
		mainNet->updControl("FlowThru/clustNet/NormCut/NCut/mrs_natural/numClusters", C); //[!]
	}
	else if(C==0) //use GT number of clusters
	{
		cout << "** Using GT number of clusters." << endl;
		mainNet->linkControl("FlowThru/clustNet/NormCut/NCut/mrs_natural/numClusters",
							 "Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_natural/numActiveNotes");
	}
	else if(C==-1) //automatically estimate number of clusters
	{
		cout << "Automatic Estimation of number of clusters: NOT YET IMPLEMENTED! Exiting...";
		exit(0);
	}

	// 	//[TODO]
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/selectedClusters", nbSelectedClusters_); 
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/hopSize", D); 
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/storePeaks", (mrs_natural) peakStore_); 
	// 	mainNet->updControl("PeClust/peClust/mrs_string/similarityType", T); 
	//
	// 	similarityWeight_.stretch(3);
	// 	similarityWeight_(0) = 1;
	// 	similarityWeight_(1) = 10;  //[WTF]
	// 	similarityWeight_(2) = 1;
	// 	mainNet->updControl("PeClust/peClust/mrs_realvec/similarityWeight", similarityWeight_); 

	if(noiseName != EMPTYSTRING)
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/SoundFileSink/mixSink/mrs_string/filename", mixName);

	mainNet->update();

	//check if input is a stereo signal
	if(mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/mrs_natural/onObservations")->to<mrs_natural>() == 1)
	{
		//if a not a stereo signal, we must set the Stereo2Mono weight to 1.0 (i.e. do no mixing)!
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Stereo2Mono/s2m/mrs_real/weight", 1.0);
	}

	//------------------------------------------------------------------------
	//check which similarity computations should be disabled (if any)
	//------------------------------------------------------------------------
	// Frequency Similarity
	if(ignoreFrequency)
	{
		cout << "** Frequency Similarity Computation disabled!" << endl;
		mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
						 "Series/freqSim");
	}
	else
		cout << "** Frequency Similarity Computation enabled!" << endl;
	// amplitude similarity
	if(ignoreAmplitude)
	{
		cout << "** Amplitude Similarity Computation disabled!" << endl;
		mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
						 "Series/ampSim");
	}
	else
		cout << "** Amplitude Similarity Computation enabled!" << endl;
	// HWPS similarity
	if(ignoreHWPS)
	{
		cout << "** HWPS (harmonicity) Similarity Computation disabled!" << endl;
		mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
						 "Series/HWPSim");
	}
	else
		cout << "** HWPS (harmonicity) Similarity Computation enabled!" << endl;
	//
	//Panning Similarity
	if(mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/mrs_natural/onObservations")->to<mrs_natural>() == 2 &&
	   !ignorePan)
	{
		cout << "** Panning Similarity Computation enabled!" << endl;
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/mrs_string/enableChild",
						 "Series/stereoSpkNet");
		mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/enableChild",
						 "Series/panSim");
	}
	else //if not stereo or if stereo to be ignored, disable some branches 
	{
		cout << "** Panning Similarity Computation disabled!" << endl;
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/mrs_string/disableChild",
						 "Series/stereoSpkNet");
		mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
						 "Series/panSim");
	}
	//
	mainNet->update(); //probably not necessary... [!]
	//------------------------------------------------------------------------

	if(noiseDuration_) //[WTF]
	{
		ostringstream ossi;
		ossi << ((noiseDelay_+noiseDuration_)) << "s";
		cout << ossi.str() << endl;
		// touch the gain directly
		//	noiseGain->updControl("0.1s", Repeat("0.1s", 1), new EvValUpd(noiseGain,"mrs_real/gain", 0.0));
	}

	//ONSET DETECTION CONFIGURATION (if enabled)
	if(useOnsets)
	{
		cout << "** Onset detector enabled -> using dynamically adjusted texture windows!" << endl;
		cout << "WinSize = " << winSize_ << endl;
		cout << "N = " << N << endl;
		cout << "Nw = " << Nw << endl;
		cout << "hopSize = " << hopSize_ << endl;
		cout << "D = " << D << endl;
		cout << "fs = " << samplingFrequency_ << endl;

		//link controls for onset detector
		onsetdetector->linkControl("Filter/filt2/mrs_realvec/ncoeffs",
								"Filter/filt1/mrs_realvec/ncoeffs");
		onsetdetector->linkControl("Filter/filt2/mrs_realvec/dcoeffs",
								"Filter/filt1/mrs_realvec/dcoeffs");
		//link onset detector to accumulator and if onsets enabled, set "explicitFlush" mode
		textWinNet->linkControl("mrs_bool/flush",
								"Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_bool/onsetDetected");

		//update onset detector controls
		onsetdetector->updControl("PowerSpectrum/pspk/mrs_string/spectrumType", "wrongdBonsets");
		onsetdetector->updControl("Flux/flux/mrs_string/mode", "DixonDAFX06");
		realvec bcoeffs(1,3);//configure zero-phase Butterworth filter of Flux time series -> butter(2, 0.28)
		bcoeffs(0) = 0.1174;
		bcoeffs(1) = 0.2347;
		bcoeffs(2) = 0.1174;
		realvec acoeffs(1,3);
		acoeffs(0) = 1.0;
		acoeffs(1) = -0.8252;
		acoeffs(2) = 0.2946;
		onsetdetector->updControl("Filter/filt1/mrs_realvec/ncoeffs", bcoeffs);
		onsetdetector->updControl("Filter/filt1/mrs_realvec/dcoeffs", acoeffs);
		mrs_natural lookAheadSamples = 6;
		onsetdetector->updControl("PeakerOnset/peaker/mrs_natural/lookAheadSamples", lookAheadSamples); //!!
		onsetdetector->updControl("PeakerOnset/peaker/mrs_real/threshold", 1.5); //!!!
		onsetdetector->updControl("ShiftInput/sif/mrs_natural/winSize", 4*lookAheadSamples+1);

		//set Accumulator controls for explicit flush mode
		mrs_natural winds = 1+lookAheadSamples+mrs_natural(ceil(mrs_real(winSize_)/hopSize_/2.0));
		cout << "Accumulator/textWinNet timesToKeep = " << winds << endl;
		mrs_real textureWinMinLen = 0.050; //secs
		mrs_real textureWinMaxLen = 1.0; //secs
		mrs_natural minTimes = (mrs_natural)(textureWinMinLen*samplingFrequency_/hopSize_); 
		mrs_natural maxTimes = (mrs_natural)(textureWinMaxLen*samplingFrequency_/hopSize_); 
		cout << "Accumulator/textWinNet MinTimes = " << minTimes << " (i.e. " << textureWinMinLen << " secs)" << endl;
		cout << "Accumulator/textWinNet MaxTimes = " << maxTimes << " (i.e. " << textureWinMaxLen << " secs)" <<endl;
		textWinNet->updControl("mrs_string/mode", "explicitFlush");
		textWinNet->updControl("mrs_natural/timesToKeep", winds);
		//textWinNet->updControl("mrs_string/mode","explicitFlush");
		textWinNet->updControl("mrs_natural/maxTimes", maxTimes); 
		textWinNet->updControl("mrs_natural/minTimes", minTimes);

		//set MidiFileSynthSource to receive a signal for each texture window
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/sigNewTextWin", false);
		mainNet->linkControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/newTextWin",
							 "Accumulator/textWinNet/mrs_bool/flush");
	}
	else
	{
		cout << "** Onset detector disabled ->";
		if(accSize_>0)//manually set texture window length
		{
			cout << " using fixed length texture windows" << endl;
			cout << "Accumulator/textWinNet nTimes = " << accSize << " (i.e. " << accSize*hopSize_/samplingFrequency_ << " secs)" << endl;
			mainNet->updControl("Accumulator/textWinNet/mrs_natural/nTimes", accSize);
			
			//set MidiFileSynthSource to receive a signal for each texture window
			mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/sigNewTextWin", false);
			mainNet->linkControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/newTextWin",
								 "Accumulator/textWinNet/mrs_bool/flush");

		}
		else //use GT for texture window length
		{
			cout << " using GT length texture windows" << endl;

			//set texture window
			mainNet->updControl("Accumulator/textWinNet/mrs_string/mode", "explicitFlush");
			mrs_real textureWinMinLen = 0.0; //secs - let GT take care of this...
			mrs_real textureWinMaxLen = 15.0; //secs - just a majorant...
			mrs_natural minTimes = (mrs_natural)(textureWinMinLen*samplingFrequency_/hopSize_); 
			mrs_natural maxTimes = (mrs_natural)(textureWinMaxLen*samplingFrequency_/hopSize_); 
			cout << "Accumulator/textWinNet MinTimes = " << minTimes << " (i.e. " << textureWinMinLen << " secs)" << endl;
			cout << "Accumulator/textWinNet MaxTimes = " << maxTimes << " (i.e. " << textureWinMaxLen << " secs)" <<endl;
			mainNet->updControl("Accumulator/textWinNet/mrs_natural/maxTimes", maxTimes); 
			mainNet->updControl("Accumulator/textWinNet/mrs_natural/minTimes", minTimes);

			//set MidiFileSynthSource to send a signal for each texture window
			mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/sigNewTextWin", true);
			mainNet->linkControl("Accumulator/textWinNet/mrs_bool/flush",
								 "Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/newTextWin");
		}
	}

	mrs_natural delay = -(winSize_/2 - hopSize_); 
	mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/synthBank/PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_real/samplingFreq", samplingFrequency_);
	mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/synthBank/PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_natural/delay", delay); // Nw/2+1 
	mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/synthBank/PeakSynth/synthCluster_0/PeakSynthOsc/pso/mrs_natural/synSize", hopSize_*2);
	mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/synthBank/PeakSynth/synthCluster_0/Windowing/wiSyn/mrs_string/type", "Hanning");
		
	//[!]
	//if (outsfname == "MARSYAS_EMPTY") 
	//	mainNet->updControl("Shredder/synthNet/Series/postNet/AudioSink/dest/mrs_natural/bufferSize", bopt_);
	// else
	//	mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSink/dest/mrs_string/filename", outsfname);//[!]
	

	//***************************************************************************************************************
	//									MAIN TICKING LOOP
	//***************************************************************************************************************
	cout <<">> Start processing..." << endl;
	//ofstream cfile("density.txt", ios::app); //[WTF] [TODO]
	mrs_real globalSnr = 0;
	mrs_natural frameCount = 0;
	//	mrs_real time=0;

	mrs_natural numTextWinds = 0;
	while(analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/hasData")->to<mrs_bool>())//(1)
	{	
		mainNet->tick();
		numTextWinds++;

		//if(numTextWinds == 63)
		//{
		//	cout << "!!!!" << endl;
		//}


		if (!microphone_)
		{
			//bool temp = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/hasData")->to<mrs_bool>();
			//bool temp1 = textWinNet->getctrl("Series/analysisNet/FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/hasData")->to<mrs_bool>();
			//bool temp2 = analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_bool/hasData")->to<mrs_bool>();

			mrs_real timeRead =  analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_natural/pos")->to<mrs_natural>()/samplingFrequency_;
			mrs_real timeLeft;
			//if(!stopAnalyse_)
			timeLeft =  analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/MidiFileSynthSource/src/mrs_natural/size")->to<mrs_natural>()/samplingFrequency_;
			//else
			//	timeLeft = stopAnalyse_;
			
			// string fname = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_string/filename")->to<mrs_string>();

			printf("Processed texture window %d : %.2f / %.2f \r", (int)numTextWinds, timeRead, timeLeft);
			fflush(stdout);

			//cout << fixed << setprecision(2) << timeRead << "/" <<  setprecision(2) << timeLeft;
			///*bool*/ temp = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>();

			//[TODO]
			// 			mrs_real density = mainNet->getctrl("PeClust/peClust/mrs_real/clusterDensity")->to<mrs_real>();
			// 			cfile << density << " " << oriGain << endl;
			// 			//cout << oriGain << endl;

			//if (temp2 == false || (stopAnalyse_ !=0 && stopAnalyse_<timeRead))
			//	break;
		}
	}
	if(synthetize_ > -1 && residual_ && frameCount != 0)
	{
		cout << "Global SNR : " << globalSnr/frameCount << endl;
		*snr0 = globalSnr/frameCount;
	}

	if(peakStore_)
	{
// 		mainNet->updControl("PeakViewSink/peSink/mrs_real/fs", samplingFrequency_);
// 		mainNet->updControl("PeakViewSink/peSink/mrs_natural/frameSize", D);
// 		mainNet->updControl("PeakViewSink/peSink/mrs_string/filename", filePeakName); 
// 		mainNet->updControl("PeakViewSink/peSink/mrs_bool/done", true);
	}

	//cout << ">> Saving .mat file with results so they can be opened in MATLAB in: " << fileMatName << endl;
	//MATLAB_EVAL("save " + fileMatName);

	if(numTextWinds > 0)
	{
		cout << ">> Saving SDR results to SDR.mat" << endl;
		MATLAB_EVAL("saveSDRresults");
	}

	cout << endl << ">> End of processing. Bye!" << endl;

	//cfile.close(); [TODO]
}
Exemple #4
0
void tags() {
	if (!wekafname_Set()) return;
	if (!twekafname_Set()) return;

	// The file paths we will be reading/writing to.
	string testing_arff = inputdir_ + twekafname_;
	string training_arff = inputdir_ + wekafname_;
	string testing_predictions = outputdir_ + predictcollectionfname_;
	string testing_predictions_arff = outputdir_ + twekafname_ + ".affinities.arff";
	string training_predictions_arff = outputdir_ + wekafname_ + ".affinities.arff";

	// Initialize the network, classifier, and weka source through which
	// we will read our .arff files
	MarSystemManager mng;
	MarSystem* net = mng.create("Series", "series");
	net->addMarSystem(mng.create("WekaSource", "wsrc"));
	MarSystem* classifier = mng.create("Classifier", "cl");
	net->addMarSystem(classifier);
	net->addMarSystem(mng.create("Gain/gain"));

	// Instantiate the correct classifier:
	cout << "Selected classifier type = " << classifier_ << endl;
	if (classifier_ == "GS") {
		net->updControl("Classifier/cl/mrs_string/enableChild", "GaussianClassifier/gaussiancl");
	} else if (classifier_ == "ZEROR") {
		net->updControl("Classifier/cl/mrs_string/enableChild", "ZeroRClassifier/zerorcl");
	} else if (classifier_ == "SVM") {
		net->updControl("Classifier/cl/mrs_string/enableChild", "SVMClassifier/svmcl");
	} else {
		// TODO: ERROR CONDITION; ADD ERROR HANDLING HERE
	}


	/**
	 * TRAINING
	 *
	 *     Read in the training arff data, and train the classifier.
	 **/

	// Set up the weka source to read the training .arff
	// and hook together some controls.
	cout << "Training Filename = " << training_arff << endl;
	net->updControl("WekaSource/wsrc/mrs_string/filename", training_arff);
	net->updControl("mrs_natural/inSamples", 1);
	net->updControl("Classifier/cl/mrs_natural/nClasses", net->getctrl("WekaSource/wsrc/mrs_natural/nClasses"));
	net->linkControl("Classifier/cl/mrs_string/mode", "mrs_string/train");

	// Tick over the training WekaSource until all lines in the training file have been read.
	// FIXME: Remove the mode updates, unless someone can justify their existence.
	//        The mode is not switched to 'predict' until further down.
	cout << "Reading features" << endl;
	while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>())
	{
		string mode = net->getctrl("WekaSource/wsrc/mrs_string/mode")->to<mrs_string>();
		net->tick();
		net->updControl("Classifier/cl/mrs_string/mode", mode);
	}

	// Switch the Classifier's mode to predict:
	// This causes the classifier to train itself on all input data so far.
	cout << "Training" << endl;
	net->updControl("Classifier/cl/mrs_string/mode", "predict");

	// Collect information about the labels (classes) in this dataset
	mrs_natural nLabels = net->getctrl("WekaSource/wsrc/mrs_natural/nClasses")->to<mrs_natural>();
	mrs_string labelNames = net->getctrl("WekaSource/wsrc/mrs_string/classNames")->to<mrs_string>();
	vector<string> classNames;
	// TODO: you could probably replace "s = ..." with "s = labelNames"
	string s = net->getctrl("WekaSource/wsrc/mrs_string/classNames")->to<mrs_string>();
	for (int i=0; i < nLabels; ++i)
	{
		string className;
		string temp;
		className = s.substr(0, s.find(","));
		temp = s.substr(s.find(",") + 1, s.length());
		s = temp;
		classNames.push_back(className);
	}

	/**
	 * PREDICT STEP 1
	 *
	 *     Predictions for the testing arff data.
	 **/

	// Initialize the weka sink that we will use to write an .arff file
	// for the testing dataset, where the features are the predicted
	// probabilities from our classifier
	MarSystem* testpSink = mng.create("WekaSink/testpSink");
	testpSink->updControl("mrs_natural/inSamples", 1);
	testpSink->updControl("mrs_natural/inObservations", nLabels+1);
	testpSink->updControl("mrs_natural/nLabels", nLabels);
	testpSink->updControl("mrs_string/labelNames", labelNames);
	testpSink->updControl("mrs_string/inObsNames", labelNames);
	testpSink->updControl("mrs_string/filename", testing_predictions_arff);

	// Set up the weka source to read the testing data
	cout << "Testing Filename = " << testing_arff << endl;
	net->updControl("WekaSource/wsrc/mrs_string/filename", testing_arff);
	cout << "Starting Prediction for Testing Collection" << endl;
	cout << "Writing .mf style predictions to " << testing_predictions << endl;
	cout << "The following output file can serve as a stacked testing .arff" << endl;
	cout << "Writing .arff style predictions to " << testing_predictions_arff << endl;

	mrs_realvec probs;
	mrs_realvec testpSinkOut;
	mrs_string currentlyPlaying;
	realvec data;
	realvec wsourcedata;
	vector<string> previouslySeenFilenames;

	// Open the non-stacked predictions output file to write to.
	ofstream prout;
	prout.open(testing_predictions.c_str());

	// Tick over the test WekaSource, saving our predictions for each line,
	// until all lines in the test file have been read.
	testpSinkOut.create(nLabels+1,1);
	while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>())
	{
		net->tick();

		wsourcedata = net->getctrl("WekaSource/wsrc/mrs_realvec/processedData")->to<mrs_realvec>();
		data = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
		currentlyPlaying = net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>();

		if (!alreadySeen(currentlyPlaying, previouslySeenFilenames))
		{
			probs = net->getctrl("Classifier/cl/mrs_realvec/processedData")->to<mrs_realvec>();

			for (mrs_natural i=0; i < probs.getSize()-2; i++)
			{
				testpSinkOut(i,0) = probs(2+i);
				prout << currentlyPlaying << "\t" << classNames[i] << "\t" << probs(2+i) << endl;
			}

			testpSinkOut(probs.getSize()-2,0) = probs(0);
			testpSink->updControl("mrs_string/currentlyPlaying", currentlyPlaying);
			testpSink->process(testpSinkOut, testpSinkOut);

			// Mark this filename as seen!
			previouslySeenFilenames.push_back(currentlyPlaying);
		}
	}

	// Close the non-stacked predictions; they are written!
	prout.close();

	/**
	 * PREDICT STEP 2
	 *
	 *     Predictions for the training arff data
	 **/

	// Initialize the weka sink that we will use to write an .arff file
	// for the training dataset, where the features are the predicted
	// probabilities from our classifier
	MarSystem* trainpSink = mng.create("WekaSink/trainpSink");
	trainpSink->updControl("mrs_natural/inSamples", 1);
	trainpSink->updControl("mrs_natural/inObservations", nLabels+1);
	trainpSink->updControl("mrs_natural/nLabels", nLabels);
	trainpSink->updControl("mrs_string/labelNames", labelNames);
	trainpSink->updControl("mrs_string/inObsNames", labelNames);
	trainpSink->updControl("mrs_string/filename", training_predictions_arff);

	cout << "Starting prediction for training collection (for stacked generalization)" << endl;
	cout << "The following output file can serve as a stacked training .arff" << endl;
	cout << "Writing .arff style predictions to " << training_predictions_arff << endl;

	// Empty our list of previously seen filenames; we will reuse it.
	previouslySeenFilenames.clear();
	mrs_realvec trainpSinkOut;
	trainpSinkOut.create(nLabels+1,1);
	mrs_natural label;

	net->updControl("WekaSource/wsrc/mrs_string/filename", training_arff);
	// Tick over the test WekaSource, saving our predictions for each line in
	// the training file until all lines in the training file have been read.
	while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>())
	{
		net->tick();
		currentlyPlaying = net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>();
		wsourcedata = net->getctrl("WekaSource/wsrc/mrs_realvec/processedData")->to<mrs_realvec>();
		label = wsourcedata(wsourcedata.getSize()-1,0);
		probs = net->getctrl("Classifier/cl/mrs_realvec/processedData")->to<mrs_realvec>();

		if (!alreadySeen(currentlyPlaying, previouslySeenFilenames))
		{
			// Store the predicted probabilities for this file and mark the file as seen!
			for (mrs_natural i=0; i < probs.getSize()-2; i++)
			{
				trainpSinkOut(i,0) = probs(2+i);
			}
			previouslySeenFilenames.push_back(currentlyPlaying);
		}

		// Write out a line in the arff file.
		trainpSinkOut(probs.getSize()-2,0) = label;
		trainpSink->updControl("mrs_string/currentlyPlaying", currentlyPlaying);
		trainpSink->process(trainpSinkOut, trainpSinkOut);
	}

	cout << "DONE" << endl;
}
Exemple #5
0
void
train_evaluate()
{
  if (!wekafname_Set()) return;

  wekafname_  = inputdir_ + wekafname_;

  cout << "Training classifier using .arff file: " << wekafname_ << endl;
  cout << "Classifier type : " << classifier_ << endl;


  MarSystemManager mng;

  MarSystem* net;
  net = mng.create("Series", "net");
  net->addMarSystem(mng.create("WekaSource", "wsrc"));
  net->addMarSystem(mng.create("Classifier", "cl"));
  net->addMarSystem(mng.create("ClassificationReport", "summary"));

  if (classifier_ == "GS")
    net->updControl("Classifier/cl/mrs_string/enableChild", "GaussianClassifier/gaussiancl");
  if (classifier_ == "ZEROR")
    net->updControl("Classifier/cl/mrs_string/enableChild", "ZeroRClassifier/zerorcl");
  if (classifier_ == "SVM")
    net->updControl("Classifier/cl/mrs_string/enableChild", "SVMClassifier/svmcl");
  // net->updControl("WekaSource/wsrc/mrs_string/attributesToInclude", "1,2,3");

  // net->updControl("WekaSource/wsrc/mrs_string/validationMode", "PercentageSplit,50%");
  net->updControl("WekaSource/wsrc/mrs_string/validationMode", "kFold,NS,10");
  // net->updControl("WekaSource/wsrc/mrs_string/validationMode", "UseTestSet,lg.arff");
  net->updControl("WekaSource/wsrc/mrs_string/filename", wekafname_);
  net->updControl("mrs_natural/inSamples", 1);

  if (classifier_ == "SVM") {
    if (svm_svm_ != EMPTYSTRING) {
      net->updControl("Classifier/cl/SVMClassifier/svmcl/mrs_string/svm",
        svm_svm_);
    }
    if (svm_kernel_ != EMPTYSTRING) {
      net->updControl("Classifier/cl/SVMClassifier/svmcl/mrs_string/kernel",
        svm_kernel_);
    }
  }

  if (net->getctrl("WekaSource/wsrc/mrs_bool/regression")->isTrue()) {
    // TODO: enable regression for ZeroRClassifier and GaussianClassifier,
    // and don't assume we're only dealing with svm
    net->updControl("Classifier/cl/SVMClassifier/svmcl/mrs_bool/output_classPerms", false);
    net->updControl("Classifier/cl/mrs_natural/nClasses", 1);

    net->updControl("ClassificationReport/summary/mrs_natural/nClasses", 1);
    net->updControl("ClassificationReport/summary/mrs_bool/regression", true);
  } else {
    net->updControl("ClassificationReport/summary/mrs_natural/nClasses", net->getctrl("WekaSource/wsrc/mrs_natural/nClasses"));
    net->updControl("ClassificationReport/summary/mrs_string/classNames",
	       net->getctrl("WekaSource/wsrc/mrs_string/classNames"));

    net->updControl("Classifier/cl/mrs_natural/nClasses", net->getctrl("WekaSource/wsrc/mrs_natural/nClasses"));
  }

  net->linkControl("Classifier/cl/mrs_string/mode", "ClassificationReport/summary/mrs_string/mode");

  int i = 0;
  while(net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>() == false)
    {
      net->tick();

	  //  cout << net->getControl("WekaSource/wsrc/mrs_realvec/processedData")->to<mrs_realvec>() << endl;
	  
      string mode = net->getctrl("WekaSource/wsrc/mrs_string/mode")->to<mrs_string>();
      net->updControl("Classifier/cl/mrs_string/mode", mode);
      ++i;
    }

  net->updControl("Classifier/cl/mrs_string/mode", "predict");
  net->updControl("ClassificationReport/summary/mrs_bool/done", true);
  net->tick();
  delete net;
}
void
peakClustering(realvec &peakSet, string sfName, string outsfname, string noiseName, string mixName, string intervalFrequency, string panningInfo, mrs_real noiseDelay, string T, mrs_natural N, mrs_natural Nw, 
			   mrs_natural D, mrs_natural S, mrs_natural C,
			   mrs_natural accSize, mrs_natural synthetize, mrs_real *snr0)
{
	MarSystemManager mng;

	cout << "Extracting Peaks and Computing Clusters..." << endl;
	cout << "Nw = " << Nw << endl;
	cout << "N = " << N << endl;
	cout << "winSize_ = " << winSize_ << endl;
	cout << "D = " << D << endl;
	cout << "hopSize_ = " << hopSize_ << endl;
	
	//**************************************************
	// create the peakClustering network
	//**************************************************
	MarSystem* mainNet = mng.create("Series", "mainNet");

	//**************************************************************************
	//create accumulator for the texture window and add it to the main network
	//**************************************************************************
	MarSystem* textWinNet = mng.create("Accumulator", "textWinNet");
	mainNet->addMarSystem(textWinNet);

	//************************************************************************
	//create Analysis Network and add it to the texture window accumulator
	//************************************************************************
	MarSystem* analysisNet = mng.create("Series", "analysisNet");
	textWinNet->addMarSystem(analysisNet);

	//************************************************************************
	//create FanInOut for mixing with a noise source and add to Analysis Net
	//************************************************************************
	MarSystem* mixer = mng.create("FanOutIn", "mixer");
	//---- create original series and add it to mixer
	MarSystem* oriNet = mng.create("Series", "oriNet");
	if (microphone_) 
		oriNet->addMarSystem(mng.create("AudioSource", "src"));
	else 
		oriNet->addMarSystem(mng.create("SoundFileSource", "src"));
	oriNet->addMarSystem(mng.create("Gain", "oriGain"));
	mixer->addMarSystem(oriNet);
	//---- create a series for the noiseSource
	if(noiseName != EMPTYSTRING)
	{
		MarSystem* mixseries = mng.create("Series", "mixseries");
		if(noiseName == "white")
			mixseries->addMarSystem(mng.create("NoiseSource", "noise"));
		else
			mixseries->addMarSystem(mng.create("SoundFileSource", "noise"));

		mixseries->addMarSystem(mng.create("Delay", "noiseDelay"));
		MarSystem* noiseGain = mng.create("Gain", "noiseGain");
		mixseries->addMarSystem(noiseGain);
		// add this series in the fanout
		mixer->addMarSystem(mixseries);
	}
	//add Mixer to analysis network
	analysisNet->addMarSystem(mixer);

	//********************************************************
	// create SoundFileSink and add it to the analysis net
	//********************************************************
	if(noiseName != EMPTYSTRING)
		analysisNet->addMarSystem(mng.create("SoundFileSink", "mixSink"));

	
	//***********************************************************
	// create peakExtract network and add it to the analysis net
	//***********************************************************
	MarSystem* peakExtract = mng.create("Series","peakExtract");
	peakExtract->addMarSystem(mng.create("ShiftInput", "si"));

	MarSystem* stereoFo = mng.create("Fanout","stereoFo");
	// create Spectrum Network and add it to the analysis net
	MarSystem* spectrumNet = mng.create("Series", "spectrumNet");
	spectrumNet->addMarSystem(mng.create("Stereo2Mono","s2m"));

	//onset detector
	MarSystem* onsetdetector = mng.create("FlowThru", "onsetdetector");
	//onsetdetector->addMarSystem(mng.create("ShiftInput", "si"));
	onsetdetector->addMarSystem(mng.create("Windowing", "win")); 
	onsetdetector->addMarSystem(mng.create("Spectrum","spk"));
	onsetdetector->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	onsetdetector->addMarSystem(mng.create("Flux", "flux")); 
	onsetdetector->addMarSystem(mng.create("ShiftInput","sif"));
	onsetdetector->addMarSystem(mng.create("Filter","filt1"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev1"));
	onsetdetector->addMarSystem(mng.create("Filter","filt2"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev2"));
	onsetdetector->addMarSystem(mng.create("PeakerOnset","peaker")); 
	spectrumNet->addMarSystem(onsetdetector);
		
	spectrumNet->addMarSystem(mng.create("Shifter", "sh"));
	spectrumNet->addMarSystem(mng.create("Windowing", "wi"));
	MarSystem* parallel = mng.create("Parallel", "par");
	parallel->addMarSystem(mng.create("Spectrum", "spk1"));
	parallel->addMarSystem(mng.create("Spectrum", "spk2"));
	spectrumNet->addMarSystem(parallel);
	// add spectrumNet to stereo fanout
	stereoFo->addMarSystem(spectrumNet);
	//
	//create stereo spectrum net
	MarSystem* stereoSpkNet = mng.create("Series","stereoSpkNet");
	MarSystem* LRnet = mng.create("Parallel","LRnet");
	//
	MarSystem* spkL = mng.create("Series","spkL");
	spkL->addMarSystem(mng.create("Windowing","win"));
	spkL->addMarSystem(mng.create("Spectrum","spk"));
	LRnet->addMarSystem(spkL);
	//
	MarSystem* spkR = mng.create("Series","spkR");
	spkR->addMarSystem(mng.create("Windowing","win"));
	spkR->addMarSystem(mng.create("Spectrum","spk"));
	LRnet->addMarSystem(spkR);
	//
	//add it to the stereo spectrum net series
	stereoSpkNet->addMarSystem(LRnet);
	//
	//add stereo spectrum object to stereo spectrum net
	//stereoSpkNet->addMarSystem(mng.create("StereoSpectrum","stereoSpk")); //AVENDANO
	stereoSpkNet->addMarSystem(mng.create("EnhADRess","ADRess"));//enhADRess_1
	stereoSpkNet->addMarSystem(mng.create("EnhADRessStereoSpectrum","stereoSpk")); //enhADRess_2
	//
	// add the stereo Spectrum net to the Fanout
	stereoFo->addMarSystem(stereoSpkNet);
	//
	// add the fanout to the peakExtract net
	peakExtract->addMarSystem(stereoFo);
	//
	//add peakExtract net to analysis net 
	analysisNet->addMarSystem(peakExtract);

	//***************************************************************
	//add PeakConvert to main SERIES for processing texture windows
	//***************************************************************
	mainNet->addMarSystem(mng.create("PeakConvert", "conv"));

	//***************************************************************
	//create a FlowThru for the Clustering Network and add to main net
	//***************************************************************
	MarSystem* clustNet = mng.create("FlowThru", "clustNet");
	
	if (!disableClustering)
		mainNet->addMarSystem(clustNet);

	//***************************************************************
	// create Similarities Network and add it to ClustNet
	//***************************************************************
	MarSystem* simNet = mng.create("FanOutIn", "simNet");
	simNet->updControl("mrs_string/combinator", "*");
	//
	//create Frequency similarity net and add it to simNet
	//
	MarSystem* freqSim = mng.create("Series","freqSim");
	//--------
	freqSim->addMarSystem(mng.create("PeakFeatureSelect","FREQfeatSelect"));
	freqSim->updControl("PeakFeatureSelect/FREQfeatSelect/mrs_natural/selectedFeatures",
					 PeakFeatureSelect::pkFrequency | PeakFeatureSelect::barkPkFreq);
	//--------
	MarSystem* fsimMat = mng.create("SelfSimilarityMatrix","FREQsimMat");
	fsimMat->addMarSystem(mng.create("Metric","FreqL2Norm"));
	fsimMat->updControl("Metric/FreqL2Norm/mrs_string/metric","euclideanDistance");
	fsimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//fsimMat->updControl("mrs_string/normalize", "MinMax");
	//fsimMat->linkControl("mrs_realvec/covMatrix", "Metric/FreqL2Norm/mrs_realvec/covMatrix");
	freqSim->addMarSystem(fsimMat);	
	//--------
	freqSim->addMarSystem(mng.create("RBF","FREQrbf"));
	freqSim->updControl("RBF/FREQrbf/mrs_string/RBFtype","Gaussian");
	freqSim->updControl("RBF/FREQrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(freqSim);
	//
	//create Amplitude similarity net and add it to simNet
	//
	MarSystem* ampSim = mng.create("Series","ampSim");
	//--------
	ampSim->addMarSystem(mng.create("PeakFeatureSelect","AMPfeatSelect"));
	ampSim->updControl("PeakFeatureSelect/AMPfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkAmplitude | PeakFeatureSelect::dBPkAmp);
	//--------
	MarSystem* asimMat = mng.create("SelfSimilarityMatrix","AMPsimMat");
	asimMat->addMarSystem(mng.create("Metric","AmpL2Norm"));
	asimMat->updControl("Metric/AmpL2Norm/mrs_string/metric","euclideanDistance");
	asimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//asimMat->updControl("mrs_string/normalize", "MinMax");
	//asimMat->linkControl("mrs_realvec/covMatrix", "Metric/AmpL2Norm/mrs_realvec/covMatrix");
	ampSim->addMarSystem(asimMat);	
	//--------
	ampSim->addMarSystem(mng.create("RBF","AMPrbf"));
	ampSim->updControl("RBF/AMPrbf/mrs_string/RBFtype","Gaussian");
	ampSim->updControl("RBF/AMPrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(ampSim);
	//
	//create HWPS similarity net and add it to simNet
	//
	MarSystem* HWPSim = mng.create("Series","HWPSim");
	//--------
	HWPSim->addMarSystem(mng.create("PeakFeatureSelect","HWPSfeatSelect"));
	HWPSim->updControl("PeakFeatureSelect/HWPSfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkFrequency | PeakFeatureSelect::pkSetFrequencies | PeakFeatureSelect::pkSetAmplitudes);
	//--------
	MarSystem* HWPSsimMat = mng.create("SelfSimilarityMatrix","HWPSsimMat");
	HWPSsimMat->addMarSystem(mng.create("HWPS","hwps"));
	HWPSsimMat->updControl("HWPS/hwps/mrs_bool/calcDistance", true);
	HWPSim->addMarSystem(HWPSsimMat);	
	//--------
	HWPSim->addMarSystem(mng.create("RBF","HWPSrbf"));
	HWPSim->updControl("RBF/HWPSrbf/mrs_string/RBFtype","Gaussian");
	HWPSim->updControl("RBF/HWPSrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(HWPSim);
	//
	//create Panning similarity net and add it to simNet
	//
	MarSystem* panSim = mng.create("Series","panSim");
	//--------
	panSim->addMarSystem(mng.create("PeakFeatureSelect","PANfeatSelect"));
	panSim->updControl("PeakFeatureSelect/PANfeatSelect/mrs_natural/selectedFeatures",
					PeakFeatureSelect::pkPan);
	//--------
	MarSystem* psimMat = mng.create("SelfSimilarityMatrix","PANsimMat");
	psimMat->addMarSystem(mng.create("Metric","PanL2Norm"));
	psimMat->updControl("Metric/PanL2Norm/mrs_string/metric","euclideanDistance");
	psimMat->updControl("mrs_natural/calcCovMatrix", SelfSimilarityMatrix::diagCovMatrix);
	//psimMat->updControl("mrs_string/normalize", "MinMax");
	//psimMat->linkControl("mrs_realvec/covMatrix", "Metric/PanL2Norm/mrs_realvec/covMatrix");
	panSim->addMarSystem(psimMat);	
	//--------
	panSim->addMarSystem(mng.create("RBF","PANrbf"));
	panSim->updControl("RBF/PANrbf/mrs_string/RBFtype","Gaussian");
	panSim->updControl("RBF/PANrbf/mrs_bool/symmetricIn",true);
	//--------
	simNet->addMarSystem(panSim);
	//
	// LINK controls of PeakFeatureSelects in each similarity branch
	//
	simNet->linkControl("Series/ampSim/PeakFeatureSelect/AMPfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	simNet->linkControl("Series/HWPSim/PeakFeatureSelect/HWPSfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	simNet->linkControl("Series/panSim/PeakFeatureSelect/PANfeatSelect/mrs_natural/totalNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks");
	//------
	simNet->linkControl("Series/ampSim/PeakFeatureSelect/AMPfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	simNet->linkControl("Series/HWPSim/PeakFeatureSelect/HWPSfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	simNet->linkControl("Series/panSim/PeakFeatureSelect/PANfeatSelect/mrs_natural/frameMaxNumPeaks",
					 "Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks");
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//add simNet to clustNet
	clustNet->addMarSystem(simNet);
	//
	// LINK controls related to variable number of peak from PeakConvert to simNet
	//
	if (!disableClustering)
	{
		mainNet->linkControl("FlowThru/clustNet/FanOutIn/simNet/Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/totalNumPeaks",
					  "PeakConvert/conv/mrs_natural/totalNumPeaks");
		mainNet->linkControl("FlowThru/clustNet/FanOutIn/simNet/Series/freqSim/PeakFeatureSelect/FREQfeatSelect/mrs_natural/frameMaxNumPeaks",
					  "PeakConvert/conv/mrs_natural/frameMaxNumPeaks");
	}

	//***************************************************************
	// create NCutNet MarSystem and add it to clustNet
	//***************************************************************
	MarSystem* NCutNet = mng.create("Series","NCutNet");
	clustNet->addMarSystem(NCutNet);
	//---add NCutNet components
	// add a stack to stack the 
	MarSystem* stack = mng.create("Fanout","stack");
	NCutNet->addMarSystem(stack);
	stack->addMarSystem(mng.create("NormCut","NCut"));
	stack->addMarSystem(mng.create("Gain", "ID"));
	// add the cluster selection module
	NCutNet->addMarSystem(mng.create("PeakClusterSelect","clusterSelect"));

	//***************************************************************
	// create PeakLabeler MarSystem and add it to mainNet
	//***************************************************************
	MarSystem* labeler = mng.create("PeakLabeler","labeler");
	mainNet->addMarSystem(labeler);
	if (!disableClustering)
	{
		//---- link labeler label control to the NCut output control
		mainNet->linkControl("PeakLabeler/labeler/mrs_realvec/peakLabels", "FlowThru/clustNet/mrs_realvec/innerOut");
	}	

	//***************************************************************
	// create PeakViewSink MarSystem and add it to mainNet
	//***************************************************************
	if(peakStore_)
	{
		mainNet->addMarSystem(mng.create("PeakViewSink", "peSink"));
		mainNet->updControl("PeakViewSink/peSink/mrs_string/filename", filePeakName);
	}

	//****************************************************************
	// Create Synthesis Network
	//****************************************************************
	if(synthetize >-1) 
	{
		//create Shredder series
		MarSystem* postNet = mng.create("Series", "postNet");
		//	postNet->addMarSystem(mng->create("PeOverlapadd", "ob"));
		if (synthetize < 3)
		{
			if(synthetize == 0)
			{
				postNet->addMarSystem(mng.create("PeakSynthOsc", "pso"));
				postNet->addMarSystem(mng.create("Windowing", "wiSyn"));
			}
			else
			{
				// put a fake object for probing the series
				postNet->addMarSystem(mng.create("Gain", "fakeGain"));
				postNet->addMarSystem(mng.create("FlowCutSource", "fcs"));
				// put the original source
				if (microphone_) 
					postNet->addMarSystem(mng.create("AudioSource", "srcSyn"));
				else 
					postNet->addMarSystem(mng.create("SoundFileSource", "srcSyn"));
				// set the correct buffer size
				postNet->addMarSystem(mng.create("ShiftInput", "siSyn"));
				// perform an FFT
				postNet->addMarSystem(mng.create("Spectrum", "specSyn"));
				// convert to polar
				postNet->addMarSystem(mng.create("Cartesian2Polar", "c2p"));
				// perform amplitude and panning change
				postNet->addMarSystem(mng.create("PeakSynthFFT", "psf"));
				// convert back to cartesian
				postNet->addMarSystem(mng.create("Polar2Cartesian", "p2c"));	
				// perform an IFFT
				//	 postNet->addMarSystem(mng.create("PlotSink", "plot"));
				postNet->addMarSystem(mng.create("InvSpectrum", "invSpecSyn"));
				// postNet->addMarSystem(mng.create("PlotSink", "plot2"));
				postNet->addMarSystem(mng.create("Windowing", "wiSyn"));
			}
			postNet->addMarSystem(mng.create("OverlapAdd", "ov"));
		}
		else
		{
			postNet->addMarSystem(mng.create("PeakSynthOscBank", "pso"));
			// postNet->addMarSystem(mng.create("ShiftOutput", "so"));
		}

		postNet->addMarSystem(mng.create("Gain", "outGain"));

		MarSystem *dest;
		if (outsfname == "MARSYAS_EMPTY") 
			dest = mng.create("AudioSink/dest");
		else
		{
			dest = mng.create("SoundFileSink/dest");
			//dest->updControl("mrs_string/filename", outsfname);
		}

		if(residual_)
		{
			MarSystem* fanout = mng.create("Fanout", "fano");
			fanout->addMarSystem(dest);
			MarSystem* fanSeries = mng.create("Series", "fanSeries");

			if (microphone_) 
				fanSeries->addMarSystem(mng.create("AudioSource", "src2"));
			else 
				fanSeries->addMarSystem(mng.create("SoundFileSource", "src2"));

			fanSeries->addMarSystem(mng.create("Delay", "delay"));
			fanout->addMarSystem(fanSeries);

			postNet->addMarSystem(fanout);
			postNet->addMarSystem(mng.create("PeakResidual", "res"));

			MarSystem *destRes;
			if (outsfname == "MARSYAS_EMPTY") 
				destRes = mng.create("AudioSink/destRes");
			else
			{
				destRes = mng.create("SoundFileSink/destRes");
				//dest->updControl("mrs_string/filename", outsfname);
			}
			postNet->addMarSystem(destRes);
		}
		else
			postNet->addMarSystem(dest);

		MarSystem* synthNet = mng.create("Shredder", "synthNet");
		synthNet->addMarSystem(postNet);
		
		mainNet->addMarSystem(synthNet);

		//link Shredder nTimes to Accumulator nTimes
		mainNet->linkControl("Shredder/synthNet/mrs_natural/nTimes",
						  "Accumulator/textWinNet/mrs_natural/nTimes");
	}

	
	//****************************************************************

	
	////////////////////////////////////////////////////////////////
	// update the controls
	////////////////////////////////////////////////////////////////
	//mainNet->updControl("Accumulator/textWinNet/mrs_natural/nTimes", accSize);

	if (microphone_) 
	{
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("mrs_natural/inObservations", 1);
	}
	else
	{
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_string/filename", sfName);
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("mrs_natural/inObservations", 1);
		samplingFrequency_ = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
	}

	if(noiseName != EMPTYSTRING)
	{
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/SoundFileSource/noise/mrs_string/filename", noiseName);
		mainNet->updControl("mrs_natural/inSamples", D);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/NoiseSource/noise/mrs_string/mode", "rand");
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Seriesmixseries/Delay/noiseDelay/mrs_real/delaySeconds",  noiseDelay);
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/mixseries/Gain/noiseGain/mrs_real/gain", noiseGain_);
	}

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/ShiftInput/si/mrs_natural/winSize", Nw+1);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Shifter/sh/mrs_natural/shift", 1);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Windowing/wi/mrs_bool/zeroPhasing", true);

	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkL/Windowing/win/mrs_bool/zeroPhasing", true);
	//
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_natural/size", N);
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_string/type", "Hanning");
	mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/stereoSpkNet/Parallel/LRnet/Series/spkR/Windowing/win/mrs_bool/zeroPhasing", true);

	if(unprecise_)
		mainNet->updControl("PeakConvert/conv/mrs_bool/improvedPrecision", false);      
	else
		mainNet->updControl("PeakConvert/conv/mrs_bool/improvedPrecision", true);  
	
	if(noPeakPicking_)
		mainNet->updControl("PeakConvert/conv/mrs_bool/picking", false);      
	
	mainNet->updControl("PeakConvert/conv/mrs_natural/frameMaxNumPeaks", S); 
	mainNet->updControl("PeakConvert/conv/mrs_string/frequencyInterval", intervalFrequency);  
	mainNet->updControl("PeakConvert/conv/mrs_natural/nbFramesSkipped", 0);//(N/D));  

	if (!disableClustering)
	{
		mainNet->updControl("FlowThru/clustNet/Series/NCutNet/Fanout/stack/NormCut/NCut/mrs_natural/numClusters", C); 
		mainNet->updControl("FlowThru/clustNet/Series/NCutNet/PeakClusterSelect/clusterSelect/mrs_natural/numClustersToKeep", nbSelectedClusters_);
	}

	// 	//[TODO]
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/selectedClusters", nbSelectedClusters_); 
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/hopSize", D); 
	// 	mainNet->setctrl("PeClust/peClust/mrs_natural/storePeaks", (mrs_natural) peakStore_); 
	// 	mainNet->updControl("PeClust/peClust/mrs_string/similarityType", T); 
	//
	// 	similarityWeight_.stretch(3);
	// 	similarityWeight_(0) = 1;
	// 	similarityWeight_(1) = 10;  //[WTF]
	// 	similarityWeight_(2) = 1;
	// 	mainNet->updControl("PeClust/peClust/mrs_realvec/similarityWeight", similarityWeight_); 

	if(noiseName != EMPTYSTRING)
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/SoundFileSink/mixSink/mrs_string/filename", mixName);

	mainNet->update();

	//check if input is a stereo signal
	if(mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/mrs_natural/onObservations")->to<mrs_natural>() == 1)
	{
		//if a not a stereo signal, we must set the Stereo2Mono weight to 1.0 (i.e. do no mixing)!
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/Stereo2Mono/s2m/mrs_real/weight", 1.0);
	}

	//------------------------------------------------------------------------
	//check which similarity computations should be disabled (if any)
	//------------------------------------------------------------------------
	if (!disableClustering)
	{
		// Frequency Similarity
		if(ignoreFrequency)
		{
			cout << "** Frequency Similarity Computation disabled!" << endl;
			mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
							 "Series/freqSim");
		}
		else
			cout << "** Frequency Similarity Computation enabled!" << endl;
		// amplitude similarity
		if(ignoreAmplitude)
		{
			cout << "** Amplitude Similarity Computation disabled!" << endl;
			mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
							 "Series/ampSim");
		}
		else
			cout << "** Amplitude Similarity Computation enabled!" << endl;
		// HWPS similarity
		if(ignoreHWPS)
		{
			cout << "** HWPS (harmonicity) Similarity Computation disabled!" << endl;
			mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
							 "Series/HWPSim");
		}
		else
			cout << "** HWPS (harmonicity) Similarity Computation enabled!" << endl;
		//
		//Panning Similarity
		if(mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/mrs_natural/onObservations")->to<mrs_natural>() == 2 &&
		   !ignorePan)
		{
			cout << "** Panning Similarity Computation enabled!" << endl;
			mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/mrs_string/enableChild",
							 "Series/stereoSpkNet");
			mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/enableChild",
							 "Series/panSim");
		}
		else //if not stereo or if stereo to be ignored, disable some branches 
		{
			cout << "** Panning Similarity Computation disabled!" << endl;
			mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/mrs_string/disableChild",
							 "Series/stereoSpkNet");
			mainNet->updControl("FlowThru/clustNet/FanOutIn/simNet/mrs_string/disableChild",
							 "Series/panSim");
		}
	}
	else
	{
		mainNet->updControl("Accumulator/textWinNet/Series/analysisNet/Series/peakExtract/Fanout/stereoFo/mrs_string/disableChild",
			"Series/stereoSpkNet");
	}
	//
	mainNet->update(); //probably not necessary... [!]
	//------------------------------------------------------------------------

	if(noiseDuration_) //[WTF]
	{
		ostringstream ossi;
		ossi << ((noiseDelay_+noiseDuration_)) << "s";
		cout << ossi.str() << endl;
		// touch the gain directly
		//	noiseGain->updControl("0.1s", Repeat("0.1s", 1), new EvValUpd(noiseGain,"mrs_real/gain", 0.0));
	}

	//ONSET DETECTION CONFIGURATION (if enabled)
	if(useOnsets)
	{
		cout << "** Onset detector enabled -> using dynamically adjusted texture windows!" << endl;
		cout << "WinSize = " << winSize_ << endl;
		cout << "N = " << N << endl;
		cout << "Nw = " << Nw << endl;
		cout << "hopSize = " << hopSize_ << endl;
		cout << "D = " << D << endl;
		cout << "fs = " << samplingFrequency_ << endl;

		//link controls for onset detector
		onsetdetector->linkControl("Filter/filt2/mrs_realvec/ncoeffs",
								"Filter/filt1/mrs_realvec/ncoeffs");
		onsetdetector->linkControl("Filter/filt2/mrs_realvec/dcoeffs",
								"Filter/filt1/mrs_realvec/dcoeffs");
		//link onset detector to accumulator and if onsets enabled, set "explicitFlush" mode
		textWinNet->linkControl("mrs_bool/flush",
								"Series/analysisNet/Series/peakExtract/Fanout/stereoFo/Series/spectrumNet/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_bool/onsetDetected");

		//update onset detector controls
		onsetdetector->updControl("PowerSpectrum/pspk/mrs_string/spectrumType", "wrongdBonsets");
		onsetdetector->updControl("Flux/flux/mrs_string/mode", "DixonDAFX06");
		realvec bcoeffs(1,3);//configure zero-phase Butterworth filter of Flux time series -> butter(2, 0.28)
		bcoeffs(0) = 0.1174;
		bcoeffs(1) = 0.2347;
		bcoeffs(2) = 0.1174;
		realvec acoeffs(1,3);
		acoeffs(0) = 1.0;
		acoeffs(1) = -0.8252;
		acoeffs(2) = 0.2946;
		onsetdetector->updControl("Filter/filt1/mrs_realvec/ncoeffs", bcoeffs);
		onsetdetector->updControl("Filter/filt1/mrs_realvec/dcoeffs", acoeffs);
		mrs_natural lookAheadSamples = 6;
		onsetdetector->updControl("PeakerOnset/peaker/mrs_natural/lookAheadSamples", lookAheadSamples); //!!
		onsetdetector->updControl("PeakerOnset/peaker/mrs_real/threshold", 1.5); //!!!
		onsetdetector->updControl("ShiftInput/sif/mrs_natural/winSize", 4*lookAheadSamples+1);

		//set Accumulator controls for explicit flush mode
		mrs_natural winds = 1+lookAheadSamples+mrs_natural(ceil(mrs_real(winSize_)/hopSize_/2.0));
		cout << "Accumulator/textWinNet timesToKeep = " << winds << endl;
		mrs_real textureWinMinLen = 0.050; //secs
		mrs_real textureWinMaxLen = 1.0; //secs
		mrs_natural minTimes = (mrs_natural)(textureWinMinLen*samplingFrequency_/hopSize_); 
		mrs_natural maxTimes = (mrs_natural)(textureWinMaxLen*samplingFrequency_/hopSize_); 
		cout << "Accumulator/textWinNet MinTimes = " << minTimes << " (i.e. " << textureWinMinLen << " secs)" << endl;
		cout << "Accumulator/textWinNet MaxTimes = " << maxTimes << " (i.e. " << textureWinMaxLen << " secs)" <<endl;
		textWinNet->updControl("mrs_string/mode", "explicitFlush");
		textWinNet->updControl("mrs_natural/timesToKeep", winds);
		textWinNet->updControl("mrs_string/mode","explicitFlush");
		textWinNet->updControl("mrs_natural/maxTimes", maxTimes); 
		textWinNet->updControl("mrs_natural/minTimes", minTimes);
	}
	else
	{
		cout << "** Onset detector disabled -> using fixed length texture windows" << endl;
		cout << "Accumulator/textWinNet nTimes = " << accSize << " (i.e. " << accSize*hopSize_/samplingFrequency_ << " secs)" << endl;
		mainNet->updControl("Accumulator/textWinNet/mrs_natural/nTimes", accSize);
	}

	if(synthetize>-1)
	{
		mrs_natural delay = -(winSize_/2 - hopSize_); 
		cout << "Delay = " << delay << endl;
		if (synthetize < 3)
		{
			if(synthetize==0)
			{
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthOsc/pso/mrs_real/samplingFreq", samplingFrequency_);
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthOsc/pso/mrs_natural/delay", delay); // Nw/2+1 
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthOsc/pso/mrs_natural/synSize", hopSize_*2);//D*2);
				mainNet->updControl("Shredder/synthNet/Series/postNet/Windowing/wiSyn/mrs_string/type", "Hanning");

				// changed the cluster labeling from -1 (don't use) and 0 (use) to negative (don't use) and positive (use) indices
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthOsc/pso/mrs_natural/peakGroup2Synth", -1);

			}
			else 
			{
				// linking between the first slice and the psf
				mainNet->linkControl("Shredder/synthNet/Series/postNet/mrs_realvec/input0", "PeSynthetize/synthNet/Series/postNet/PeakSynthFFT/psf/mrs_realvec/peaks");
				//
				mainNet->updControl("Shredder/synthNet/Series/postNet/Windowing/wiSyn/mrs_string/type", "Hanning");
				mainNet->updControl("Shredder/synthNet/Series/postNet/FlowCutSource/fcs/mrs_natural/setSamples", D);	
				mainNet->updControl("Shredder/synthNet/Series/postNet/FlowCutSource/fcs/mrs_natural/setObservations", 1);	
				// setting the panning mode mono/stereo
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthFFT/psf/mrs_natural/nbChannels", synthetize_);
				mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthFFT/psf/mrs_string/panning", panningInfo);
				// setting the FFT size
				mainNet->updControl("Shredder/synthNet/Series/postNet/ShiftInput/siSyn/mrs_natural/winSize", D*2);
				// setting the name of the original file
				if (microphone_) 
				{
					mainNet->updControl("Shredder/synthNet/Series/postNet/AudioSource/srcSyn/mrs_natural/inSamples", D);
					mainNet->updControl("Shredder/synthNet/Series/postNet/AudioSource/srcSyn/mrs_natural/inObservations", 1);
				}
				else
				{
					mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSource/srcSyn/mrs_string/filename", sfName);
					// pvseries->updControl("Shredder/synthNet/Series/postNet/SoundFileSource/srcSyn/mrs_natural/pos", 0);
					mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSource/srcSyn/mrs_natural/onSamples", D);
					mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSource/srcSyn/mrs_natural/onObservations", 1);
				}
				// setting the synthesis starting time (default 0)
			}
		}
		//else
		//	mainNet->updControl("Shredder/synthNet/Series/postNet/PeakSynthOscBank/pso/mrs_natural/Interpolation", D); //this control exists?!? [WTF]

		//mainNet->updControl("Shredder/synthNet/Series/postNet/ShiftOutput/so/mrs_natural/Interpolation", D); //[WTF]

		if (outsfname == "MARSYAS_EMPTY") 
			mainNet->updControl("Shredder/synthNet/Series/postNet/AudioSink/dest/mrs_natural/bufferSize", bopt_);

		if(residual_)
		{
			mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/Delay/delay/mrs_natural/delay", delay); // Nw+1-D

			if (microphone_) 
			{
				mainNet->updControl("PeSynthetize/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/AudioSource/src2/mrs_natural/inSamples", D);
				mainNet->updControl("PeSynthetize/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/AudioSource/src2/mrs_natural/inObservations", 1);
			}
			else
			{
				mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/SoundFileSource/src2/mrs_string/filename", sfName);
				mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/SoundFileSource/src2/mrs_natural/pos", 0);
				mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/SoundFileSource/src2/mrs_natural/inSamples", D);
				mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/Series/fanSeries/SoundFileSource/src2/mrs_natural/inObservations", 1);
			}

			mainNet->updControl("Shredder/synthNet/Series/postNet/Fanout/fano/SoundFileSink/dest/mrs_string/filename", outsfname);//[!]
			mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSink/destRes/mrs_string/filename", fileResName);//[!]
		}
		else
			mainNet->updControl("Shredder/synthNet/Series/postNet/SoundFileSink/dest/mrs_string/filename", outsfname);//[!]
	}

	//***************************************************************************************************************
	//									MAIN TICKING LOOP
	//***************************************************************************************************************
	//ofstream cfile("density.txt", ios::app); //[WTF] [TODO]
	mrs_real globalSnr = 0;
	mrs_natural frameCount = 0;
	//	mrs_real time=0;

	while(1)
	{	
		mainNet->tick();

		if(synthetize > -1 && residual_)
		{
			mrs_real snr = mainNet->getctrl("PeSynthetize/synthNet/Series/postNet/PeakResidual/res/mrs_real/SNR")->to<mrs_real>();
			globalSnr += snr;
			frameCount++;
			// cout << "Frame " << frameCount << " SNR : "<< snr << endl;
		}

		if (!microphone_)
		{
			bool temp2 = analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>();

			mrs_real timeRead =  analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>()/samplingFrequency_;
			mrs_real timeLeft;
			if(!stopAnalyse_)
				timeLeft =  analysisNet->getctrl("FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_natural/size")->to<mrs_natural>()/samplingFrequency_;
			else
				timeLeft = stopAnalyse_;
			// string fname = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/FanOutIn/mixer/Series/oriNet/SoundFileSource/src/mrs_string/filename")->to<mrs_string>();

			printf("  %.2f / %.2f \r", timeRead, timeLeft);
			fflush(stdout);

			//cout << fixed << setprecision(2) << timeRead << "/" <<  setprecision(2) << timeLeft;
			///*bool*/ temp = mainNet->getctrl("Accumulator/textWinNet/Series/analysisNet/SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>();

			//[TODO]
			// 			mrs_real density = mainNet->getctrl("PeClust/peClust/mrs_real/clusterDensity")->to<mrs_real>();
			// 			cfile << density << " " << oriGain << endl;
			// 			//cout << oriGain << endl;

			if (temp2 == false || (stopAnalyse_ !=0 && stopAnalyse_<timeRead))
				break;
		}
	}
	if(synthetize_ > -1 && residual_)
	{
		cout << "Global SNR : " << globalSnr/frameCount << endl;
		*snr0 = globalSnr/frameCount;
	}

	if(peakStore_)
	{
		mainNet->updControl("PeakViewSink/peSink/mrs_real/fs", samplingFrequency_);
		mainNet->updControl("PeakViewSink/peSink/mrs_natural/frameSize", D);
		mainNet->updControl("PeakViewSink/peSink/mrs_string/filename", filePeakName); 
		mainNet->updControl("PeakViewSink/peSink/mrs_bool/done", true);
	}

	//cfile.close(); [TODO]
	delete mainNet;
}
// Play a collection l of soundfiles
void speakerSeg(vector<string> soundfiles)
{
	MRSDIAG("speakerSeg.cpp - speakerSeg");

	MarSystemManager mng;
	string sfName;

	//create main processing network
	MarSystem* pnet = mng.create("Series", "pnet");

// 	// Output destination is either audio or soundfile 
// 	MarSystem* dest;
// 	if (fileName == EMPTYSTRING)	// audio output
// 		dest = mng.create("AudioSink", "dest");
// 	else 				// filename output
// 	{
// 		dest = mng.create("SoundFileSink", "dest");
// 	}
	MarSystem* lspSeries = mng.create("Series","lspSeries");
	MarSystem* mfccSeries = mng.create("Series","mfccSeries");
	MarSystem* lspFeatSeries = mng.create("Series","lspFeatSeries");
	MarSystem* mfccFeatSeries = mng.create("Series","mfccFeatSeries");
	MarSystem* pitchSeries = mng.create("Series","pitchSeries");
	MarSystem* features = mng.create("Fanout","features");
	MarSystem* parallel = mng.create("Parallel","parallel");


	mfccFeatSeries->addMarSystem(mng.create("Windowing", "hammfccFeatSeries"));
	mfccFeatSeries->addMarSystem(mng.create("Spectrum", "spkmfccFeatSeries"));
	mfccFeatSeries->addMarSystem(mng.create("PowerSpectrum", "mfccFeatSeriespspk"));
	mfccFeatSeries->addMarSystem(mng.create("MFCC", "mfcca"));
	mfccFeatSeries->updControl("MFCC/mfcca/mrs_natural/coefficients",
				   16);
	//				   MarControlPtr((mrs_natural)16));

	// based on the nr of features (in this case, the LSP order = 10),
	// calculate the minimum number of frames each speech segment should have
	// in order to avoid ill calculation of the corresponding covariance matrices.
	// To have a good estimation of the covariance matrices
	// the number of data points (i.e. feature vectors) should be at least
	// equal or bigger than d(d+1)/2, where d = cov matrix dimension.
	mrs_real d = (mrs_real)mfccFeatSeries->getctrl("MFCC/mfcca/mrs_natural/coefficients")->to<mrs_natural>();
	mrs_natural minSegFrames = (mrs_natural)ceil(0.5*d*(d+1.0)); //0.5*d*(d+1.0) or 0.5*d*(d-1.0) [?] // make sure it's even

	//	cout << "d = " << d << endl;
	//exit(0);


	lspFeatSeries->addMarSystem(mng.create("LPCnet", "lpc"));
	lspFeatSeries->addMarSystem(mng.create("LSP", "lsp"));
	lspFeatSeries->updControl("LPCnet/lpc/mrs_natural/order", 10);	//hardcoded [!]


 	features->addMarSystem(mfccFeatSeries);
 	features->addMarSystem(lspFeatSeries);



	//speakerSeg processes data at each tick as depicted bellow, 
	//which includes 4 speech sub-segments (C1, C2, C3, C4) that will be used for detecting 
	//speaker changes:
	//
	// data processed at each tick
	// |------------------------|
	//      C1        C2
	// |----+----|----+----|					
	//      |----+----|----+----|
	//           C3        C4
	// |--->|
	//   hop
	//
	// So, the speech segments C1, C2, C3 and C4 should have a minimum of minSegFrames in order
	// to avoid cov ill-calculation. Consequently, at each tick, at least 5/2*minSegFrames should
	// be processed, with a hop set to 1/2*minSegFrames (which could be changed, but for now is fixed).

	//create an accumulator for creating hopsize new feature vectors at each tick
	MarSystem* accum = mng.create("Accumulator", "accum");
	//	accum->addMarSystem(features);


// 	MarSystem* accum2 = mng.create("Accumulator", "accum2");
// 	accum2->addMarSystem(mfccFeatSeries);
// 	accum2->updControl("mrs_natural/nTimes", minSegFrames/2);
	//accum->updControl("mrs_natural/nTimes", ceil(minSegFrames/2.0));

	//add accumuated feature extraction to main processing network
	//	lspSeries->addMarSystem(accum);
	//mfccSeries->addMarSystem(accum2);

	//create a circular buffer for storing most recent LSP10 speech data
	//lspSeries->addMarSystem(mng.create("Memory", "mem"));
	// mfccSeries->addMarSystem(mng.create("Memory", "mem2"));
	mfccSeries->updControl("mrs_natural/inObservations",16);
	lspSeries->updControl("mrs_natural/inObservations",10);
	// lspSeries->updControl("Memory/mem/mrs_natural/memSize", 5); //see above for an explanation why we use memSize = 5
	// mfccSeries->updControl("Memory/mem2/mrs_natural/memSize", 5); //see above for an explanation why we use memSize = 5

	//add a BIC change detector
	lspSeries->addMarSystem(mng.create("BICchangeDetector", "BICdet"));
	mfccSeries->addMarSystem(mng.create("BICchangeDetector", "BICdet2"));
	

	
	
	// link top-level controls 



	// parallel->addMarSystem(lspSeries);
	// parallel->addMarSystem(mfccSeries);

	//create feature extraction network for calculating LSP-10
	MarSystem* featExtractor = mng.create("Series", "featExtractor");


	featExtractor->addMarSystem(mng.create("SoundFileSource", "src"));
	featExtractor->addMarSystem(features);
	accum->addMarSystem(featExtractor);
	accum->updControl("mrs_natural/nTimes", minSegFrames/2);
	pnet->addMarSystem(accum);
	// pnet->addMarSystem(parallel);

	pnet->addMarSystem(mng.create("ShiftInput", "si"));
	pnet->updControl("ShiftInput/si/mrs_natural/winSize", 135);

	pnet->linkControl("mrs_string/filename","Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_string/filename");
	pnet->linkControl("Accumulator/accum/mrs_natural/inSamples","mrs_natural/inSamples");

	pnet->linkControl("Accumulator/accum/Series/featExtractor/mrs_natural/inSamples","mrs_natural/inSamples");
	pnet->linkControl("Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_natural/inSamples","mrs_natural/inSamples");
	pnet->linkControl("Accumulator/accum/Series/featExtractor/Fanout/features/Series/lspFeatSeries/LPCnet/lpc/mrs_natural/inSamples","mrs_natural/inSamples");
	pnet->linkControl("mrs_real/israte", "Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_real/israte");
	pnet->linkControl("mrs_natural/pos", "Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_natural/pos");
	//	pnet->linkControl("mrs_bool/hasData", "Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_bool/hasData");
	pnet->linkControl("Accumulator/accum/Series/featExtractor/SoundFileSource/src/mrs_bool/hasData","mrs_bool/hasData");
	pnet->linkControl("mrs_bool/MFCCMemreset", "Parallel/parallel/Series/mfccSeries/Memory/mem2/mrs_bool/reset");
	pnet->linkControl("mrs_bool/LSPMemreset", "Parallel/parallel/Series/lspSeries/Memory/mem/mrs_bool/reset");
	pnet->linkControl("mrs_bool/MFCCBICreset", "Parallel/parallel/Series/mfccSeries/BICchangeDetector/BICdet2/mrs_bool/reset");
	pnet->linkControl("mrs_bool/LSPBICreset", "Parallel/parallel/Series/lspSeries/BICchangeDetector/BICdet/mrs_bool/reset");


	//featExtractor->updControl("mrs_natural/inSamples", 125); //hardcoded for fs=8khz [!]



	// play each collection or soundfile 
	vector<string>::iterator sfi;  
	for (sfi = soundfiles.begin(); sfi != soundfiles.end(); ++sfi) 
	{
		string fname = *sfi;
		//clear any memory data and any stored models
		pnet->updControl("mrs_bool/LSPMemreset", true);
		pnet->updControl("mrs_bool/LSPBICreset", true);
		pnet->updControl("mrs_bool/MFCCMemreset", true);
		pnet->updControl("mrs_bool/MFCCBICreset", true);
		//set new file name
		pnet->updControl("mrs_string/filename", fname);
		// need to override the control to be 25 ms every time the file is read in.
		pnet->updControl("mrs_natural/inSamples",200);
// 		if (fileName != EMPTYSTRING) // soundfile output instead of audio output
// 			pnet->updControl("SoundFileSink/dest/mrs_string/filename", fileName);
// 
// 		if (fileName == EMPTYSTRING)	// audio output
// 		{
// 			pnet->updControl("AudioSink/dest/mrs_natural/bufferSize", 256); 
// 			pnet->updControl("AudioSink/dest/mrs_bool/initAudio", true);
// 		}

	// output network description to cout  
	if ((pluginName == EMPTYSTRING) && (verboseopt)) // output to stdout 
	{
		cout << (*pnet) << endl;      
	}
	else if (pluginName != EMPTYSTRING)             // output to plugin
	{
		ofstream oss(pluginName.c_str());
		oss << (*pnet) << endl;
	}



		MarControlPtr hasDataPtr_ = 
			pnet->getctrl("mrs_bool/hasData");

		while (hasDataPtr_->isTrue())	
		{
			pnet->tick();
		}
	}

	delete pnet;
}
// Play a collection l of soundfiles
void sfplay(vector<string> soundfiles)
{
    MRSDIAG("sfplay.cpp - sfplay");

    MarSystemManager mng;
    string sfName;

    // Output destination is either audio or soundfile
    MarSystem* dest;
    if (fileName == EMPTYSTRING)	// audio output
        dest = mng.create("AudioSink", "dest");
    else 				// filename output
    {
        dest = mng.create("SoundFileSink", "dest");
    }

    // create playback network with source-gain-dest
    MarSystem* playbacknet = mng.create("Series", "playbacknet");

    playbacknet->addMarSystem(mng.create("SoundFileSource", "src"));
    playbacknet->addMarSystem(mng.create("Gain", "gt"));
    playbacknet->addMarSystem(dest);

    // playback offset


    // update controls
    playbacknet->updControl("mrs_natural/inSamples", windowsize);
    playbacknet->updControl("Gain/gt/mrs_real/gain", gain);

    // link top-level controls
    playbacknet->linkControl("mrs_string/filename","SoundFileSource/src/mrs_string/filename");
    playbacknet->linkControl("mrs_real/israte", "SoundFileSource/src/mrs_real/israte");
    playbacknet->linkControl("mrs_natural/pos", "SoundFileSource/src/mrs_natural/pos");
    playbacknet->linkControl("mrs_natural/loopPos", "SoundFileSource/src/mrs_natural/loopPos");
    playbacknet->linkControl("mrs_bool/hasData", "SoundFileSource/src/mrs_bool/hasData");


    if (fileName == EMPTYSTRING)	// audio output
        playbacknet->linkControl("mrs_bool/initAudio", "AudioSink/dest/mrs_bool/initAudio");


    // play each collection or soundfile
    vector<string>::iterator sfi;
    for (sfi = soundfiles.begin(); sfi != soundfiles.end(); ++sfi)
    {
        string fname = *sfi;
        playbacknet->updControl("mrs_string/filename", fname);

        mrs_natural nChannels = playbacknet->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
        mrs_real srate = playbacknet->getctrl("mrs_real/israte")->to<mrs_real>();

        ;
        offset = (mrs_natural) (start * srate * nChannels);

        playbacknet->updControl("mrs_natural/loopPos", offset);
        playbacknet->updControl("mrs_natural/pos", offset);
        playbacknet->updControl("SoundFileSource/src/mrs_real/repetitions", repetitions);
        playbacknet->updControl("SoundFileSource/src/mrs_real/duration", length);



        if (fileName != EMPTYSTRING) // soundfile output instead of audio output
            playbacknet->updControl("SoundFileSink/dest/mrs_string/filename", fileName);

        if (fileName == EMPTYSTRING)	// audio output
        {
            playbacknet->updControl("AudioSink/dest/mrs_natural/bufferSize", 256);
            playbacknet->updControl("AudioSink/dest/mrs_bool/initAudio", true);
        }
        MarControlPtr hasDataPtr_ =
            playbacknet->getctrl("mrs_bool/hasData");

        while (hasDataPtr_->isTrue())
        {
            playbacknet->tick();
        }
        //cout << *playbacknet << endl;
    }

    // output network description to cout
    if ((pluginName == EMPTYSTRING) && (verboseopt)) // output to stdout
    {
        cout << (*playbacknet) << endl;
    }
    else if (pluginName != EMPTYSTRING)             // output to plugin
    {
        ofstream oss(pluginName.c_str());
        oss << (*playbacknet) << endl;
    }
    delete playbacknet;
}
Exemple #9
0
void 
detect_onsets(string sfName) 
{
	// cout << "toying with onsets" << endl;
	MarSystemManager mng;

	// assemble the processing network 
	MarSystem* onsetnet = mng.create("Series", "onsetnet");
	MarSystem* onsetaccum = mng.create("Accumulator", "onsetaccum");
	MarSystem* onsetseries= mng.create("Series","onsetseries");
	onsetseries->addMarSystem(mng.create("SoundFileSource", "src"));
	onsetseries->addMarSystem(mng.create("Stereo2Mono", "src")); //replace by a "Monofier" MarSystem (to be created) [!]
	//onsetseries->addMarSystem(mng.create("ShiftInput", "si"));
	//onsetseries->addMarSystem(mng.create("Windowing", "win"));
	MarSystem* onsetdetector = mng.create("FlowThru", "onsetdetector");
	onsetdetector->addMarSystem(mng.create("ShiftInput", "si")); //<---
	onsetdetector->addMarSystem(mng.create("Windowing", "win")); //<---
	onsetdetector->addMarSystem(mng.create("Spectrum","spk"));
	onsetdetector->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	onsetdetector->addMarSystem(mng.create("Flux", "flux")); 
	//onsetdetector->addMarSystem(mng.create("Memory","mem"));
	onsetdetector->addMarSystem(mng.create("ShiftInput","sif"));
	onsetdetector->addMarSystem(mng.create("Filter","filt1"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev1"));
	onsetdetector->addMarSystem(mng.create("Filter","filt2"));
	onsetdetector->addMarSystem(mng.create("Reverse","rev2"));
	onsetdetector->addMarSystem(mng.create("PeakerOnset","peaker")); 
	onsetseries->addMarSystem(onsetdetector);
	onsetaccum->addMarSystem(onsetseries);
	onsetnet->addMarSystem(onsetaccum);
	//onsetnet->addMarSystem(mng.create("ShiftOutput","so"));
	
	if(audiosynthopt)
	{
		//Audio synthesis
		MarSystem* onsetmix = mng.create("Fanout","onsetmix");
		onsetmix->addMarSystem(mng.create("Gain","gainaudio"));
		MarSystem* onsetsynth = mng.create("Series","onsetsynth");
		onsetsynth->addMarSystem(mng.create("NoiseSource","noisesrc"));
		onsetsynth->addMarSystem(mng.create("ADSR","env"));
		onsetsynth->addMarSystem(mng.create("Gain", "gainonsets"));
		onsetmix->addMarSystem(onsetsynth);
		onsetnet->addMarSystem(onsetmix);
		
		//onsetnet->addMarSystem(mng.create("AudioSink", "dest"));
		onsetnet->addMarSystem(mng.create("SoundFileSink", "fdest"));
	}


	///////////////////////////////////////////////////////////////////////////////////////
	//link controls
	///////////////////////////////////////////////////////////////////////////////////////
	onsetnet->linkControl("mrs_bool/hasData", 
					   "Accumulator/onsetaccum/Series/onsetseries/SoundFileSource/src/mrs_bool/hasData");
	//onsetnet->linkControl("ShiftOutput/so/mrs_natural/Interpolation","mrs_natural/inSamples");
	onsetnet->linkControl("Accumulator/onsetaccum/mrs_bool/flush",
					   "Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_bool/onsetDetected"); 
    if (confidenceopt) {
	    onsetnet->linkControl("Fanout/onsetmix/Series/onsetsynth/Gain/gainonsets/mrs_real/gain",
		"Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_real/confidence");
    }
	
	//onsetnet->linkControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Memory/mem/mrs_bool/reset",
	//	"Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_bool/onsetDetected");

	//link FILTERS coeffs
	onsetnet->linkControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt2/mrs_realvec/ncoeffs",
					   "Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt1/mrs_realvec/ncoeffs");
	onsetnet->linkControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt2/mrs_realvec/dcoeffs",
					   "Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt1/mrs_realvec/dcoeffs");

	///////////////////////////////////////////////////////////////////////////////////////
	// update controls
	///////////////////////////////////////////////////////////////////////////////////////
	FileName outputFile(sfName);
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/SoundFileSource/src/mrs_string/filename", sfName);
	
	if(audiosynthopt)
		onsetnet->updControl("SoundFileSink/fdest/mrs_string/filename", outputFile.nameNoExt() + "_onsets.wav");
	
	mrs_real fs = onsetnet->getctrl("mrs_real/osrate")->to<mrs_real>();

	mrs_natural winSize = 2048;//2048;
	mrs_natural hopSize = 512;//411;


	// mrs_natural winSize = 4096;//2048;
	// mrs_natural hopSize = 1024;//411;

	mrs_natural lookAheadSamples = 6;
	mrs_real thres = thresholdopt;

	mrs_real textureWinMinLen = 0.050; //secs
	mrs_natural minTimes = (mrs_natural) (textureWinMinLen*fs/hopSize); //12;//onsetWinSize+1;//15;
	// cout << "MinTimes = " << minTimes << " (i.e. " << textureWinMinLen << " secs)" << endl;
	mrs_real textureWinMaxLen = 60.000; //secs
	mrs_natural maxTimes = (mrs_natural) (textureWinMaxLen*fs/hopSize);//1000; //whatever... just a big number for now...
	// cout << "MaxTimes = " << maxTimes << " (i.e. " << textureWinMaxLen << " secs)" << endl;

	// best result till now are using dB power Spectrum!
	// FIXME: should fix PowerSpectrum (remove that ugly wrongdBonsets control) and use a Gain with factor of two instead. 
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PowerSpectrum/pspk/mrs_string/spectrumType",
					  "wrongdBonsets");

	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Flux/flux/mrs_string/mode",
					  "DixonDAFX06");
	
	//configure zero-phase Butterworth filter of Flux time series (from J.P.Bello TASLP paper)
	// Coefficients taken from MATLAB butter(2, 0.28)
	realvec bcoeffs(1,3);
	bcoeffs(0) = 0.1174;
	bcoeffs(1) = 0.2347;
	bcoeffs(2) = 0.1174;
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt1/mrs_realvec/ncoeffs",
					  bcoeffs);
	realvec acoeffs(1,3);
	acoeffs(0) = 1.0;
	acoeffs(1) = -0.8252;
	acoeffs(2) = 0.2946;
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/Filter/filt1/mrs_realvec/dcoeffs",
					  acoeffs);

	onsetnet->updControl("mrs_natural/inSamples", hopSize);
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/ShiftInput/si/mrs_natural/winSize", winSize);

	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_natural/lookAheadSamples", lookAheadSamples);
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_real/threshold", thres); //!!!
	
	onsetnet->updControl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/ShiftInput/sif/mrs_natural/winSize", 4*lookAheadSamples+1);
	
	mrs_natural winds = 1+lookAheadSamples+mrs_natural(ceil(mrs_real(winSize)/hopSize/2.0));
	// cout << "timesToKeep = " << winds << endl;
	onsetnet->updControl("Accumulator/onsetaccum/mrs_natural/timesToKeep", winds);
	onsetnet->updControl("Accumulator/onsetaccum/mrs_string/mode","explicitFlush");
	onsetnet->updControl("Accumulator/onsetaccum/mrs_natural/maxTimes", maxTimes); 
	onsetnet->updControl("Accumulator/onsetaccum/mrs_natural/minTimes", minTimes);

	if(audiosynthopt)
	{
		//set audio/onset resynth balance and ADSR params for onset sound
		onsetnet->updControl("Fanout/onsetmix/Gain/gainaudio/mrs_real/gain", 1.0);
		onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/Gain/gainonsets/mrs_real/gain", 0.8);
		onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/aTarget", 1.0);
		onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/aTime", winSize/80/fs); //!!!
		onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/susLevel", 0.0);
		onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/dTime", winSize/4/fs); //!!!
		
		//onsetnet->updControl("AudioSink/dest/mrs_bool/initAudio", true);
	}

	
	//MATLAB Engine inits
	//used for toy_with_onsets.m
	MATLAB_EVAL("clear;");
	MATLAB_PUT(winSize, "winSize");
	MATLAB_PUT(hopSize, "hopSize");
	MATLAB_PUT(lookAheadSamples, "lookAheadSamples");
	MATLAB_EVAL("srcAudio = [];");
	MATLAB_EVAL("onsetAudio = [];");
	MATLAB_EVAL("FluxTS = [];");
	MATLAB_EVAL("segmentData = [];");
	MATLAB_EVAL("onsetTS = [];");

	///////////////////////////////////////////////////////////////////////////////////////
	//process input file (till EOF)
	///////////////////////////////////////////////////////////////////////////////////////
	mrs_natural timestamps_samples = 0;
	cout << "Sampling rate = " << fs << endl;
	
	if(audiosynthopt)
	{
		while(onsetnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
		{
			onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/nton", 1.0); //note on
			onsetnet->tick();
			timestamps_samples += onsetnet->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
			cout << timestamps_samples / fs << endl; //in seconds
			//cout << timestamps_samples << endl; //in samples
			onsetnet->updControl("Fanout/onsetmix/Series/onsetsynth/ADSR/env/mrs_real/ntoff", 0.0); //note off
		}
	}
	else {
		ofstream outFile;
		string outFileName = outputFile.nameNoExt() + ".output";
		outFile.open(outFileName.c_str(), ios::out);
		
		while(onsetnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
		{
			onsetnet->tick();
			timestamps_samples += onsetnet->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
		    if (confidenceopt) {
	            mrs_real confidence = onsetnet->getctrl("Accumulator/onsetaccum/Series/onsetseries/FlowThru/onsetdetector/PeakerOnset/peaker/mrs_real/confidence")->to<mrs_real>();
                if (confidence > thresholdopt) {
			        cout << timestamps_samples / fs; //in seconds
                    cout << "\t";
			        cout << timestamps_samples / fs;
                    cout << "\t" << confidence << endl;
                }
            } else {
			    cout << timestamps_samples / fs << endl; //in seconds
            }
			outFile << timestamps_samples / fs << "\t";
			outFile << (timestamps_samples / fs)+1 << "\t";
			outFile << "w" << endl;
	
		}
		
		cout << "Done writing " << outFileName << endl;
		outFile.close();
	}	
    delete onsetnet;
}
mrs_real extractBpm (string sfName) {
	cout << "Onsets function calculation: " << sfName << endl;
	MarSystemManager mng;

	/*
	 * ONSET FUNCTION: Estrazione del flusso dell'energia spettrale del segnale audio
	 * La catena è composta da una sorgente sonora, una funzione di finestratura,
	 * due blocchi per computare la potenza spettrale ed in fine il blocco per calcolare
	 * il flusso.
	 */
	MarSystem* beatextractor = mng.create("Series", "beatextractor");

	MarSystem* onset_strength = mng.create ("Series", "ostrenght");

	MarSystem* acc = mng.create("Accumulator", "acc");
	MarSystem* onsets = mng.create("Series", "onsets");
	MarSystem* src = mng.create("SoundFileSource", "src");
	MarSystem* s2m = mng.create("Stereo2Mono", "s2m");
	MarSystem* si1 = mng.create("ShiftInput", "si");
	MarSystem* windowing = mng.create("Windowing", "windows");
	MarSystem* spectrum = mng.create("Spectrum", "spectrum");
	MarSystem* powspec = mng.create("PowerSpectrum", "power");
	MarSystem* flux = mng.create("Flux", "flux");

	onsets->addMarSystem(src);
	onsets->addMarSystem(s2m);
	onsets->addMarSystem(si1);
	onsets->addMarSystem(windowing);
	onsets->addMarSystem(spectrum);
	onsets->addMarSystem(powspec);
	onsets->addMarSystem(flux);

	//aggiunta della rete per il calcolo del flusso all'accumulatore
	acc->addMarSystem(onsets);

	//aggiunta del sistema flusso estrattore al blocco superiore
	onset_strength->addMarSystem(acc);
	onset_strength->addMarSystem(mng.create("ShiftInput", "si2"));

	//aggiunta al componente principale
	beatextractor->addMarSystem(onset_strength);

	/*
	 * SECONDO SOTTOSISTEMA: TEMPO INDUCTION
	 * Questa parte del sistema di occupa della tempo induction
	 */

	MarSystem* timeind = mng.create("FlowThru", "timeind" );

	timeind->addMarSystem(mng.create("Filter", "filt1"));
	timeind->addMarSystem(mng.create("Reverse", "reverse"));
	timeind->addMarSystem(mng.create("Filter", "filt2"));
	timeind->addMarSystem(mng.create("Reverse", "reverse"));
	timeind->addMarSystem(mng.create("AutoCorrelation", "acr"));
	timeind->addMarSystem(mng.create("BeatHistogram", "histo"));

	MarSystem* hfanout = mng.create("Fanout", "hfanout");
	hfanout->addMarSystem(mng.create("Gain", "id1"));
	hfanout->addMarSystem(mng.create("TimeStretch", "tsc1"));

	timeind->addMarSystem(hfanout);
	timeind->addMarSystem(mng.create("Sum", "hsum"));
	timeind->addMarSystem(mng.create("Peaker", "peaker1"));
	timeind->addMarSystem(mng.create("MaxArgMax", "mxr1"));

	//aggiunta al beatextractor
	beatextractor->addMarSystem(timeind);
	beatextractor->addMarSystem(mng.create("BeatPhase","beatphase"));
	beatextractor->addMarSystem(mng.create("Gain", "id"));

	/*
	 * DEFINIZIONE ED ASSEGNAMENTO DEI PARAMETRI
	 */

	//Sorgente sonora
	onsets->updControl("SoundFileSource/src/mrs_string/filename", sfName);
	onsets->linkControl("mrs_bool/hasData", "SoundFileSource/src/mrs_bool/hasData");

	//parametri per le finestrature e per l'accumulatore
	mrs_natural winSize = 256;
	mrs_natural hopSize = 128;
	mrs_natural bwinSize = 2048;
	mrs_natural bhopSize = 128;

	onset_strength->updControl("Accumulator/acc/mrs_natural/nTimes", bhopSize);
	onset_strength->updControl("ShiftInput/si2/mrs_natural/winSize",bwinSize);



	//Coefficienti per il filtro

	mrs_realvec bcoeffs (1,3);
	bcoeffs(0) = 0.0564;
	bcoeffs(1) = 0.1129;
	bcoeffs(2) = 0.0564;
	mrs_realvec acoeffs (1,3);
	acoeffs(0) = 1.0000;
	acoeffs(1) = -1.2247;
	acoeffs(2) = 0.4504;

	timeind->updControl("Filter/filt1/mrs_realvec/ncoeffs", bcoeffs);
	timeind->updControl("Filter/filt2/mrs_realvec/ncoeffs", bcoeffs);
	timeind->updControl("Filter/filt1/mrs_realvec/dcoeffs", acoeffs);
	timeind->updControl("Filter/filt2/mrs_realvec/dcoeffs", acoeffs);

	/*
	 * CONFIGURAZIONE DEL PEAKER DEI BEAT:
	 * peakNeighbors = numero di picchi in prossimità
	 * peakSpacing = spaziatura dei picchi tra di loro in percentuale
	 * 				 rispetto alla lunghezza del vettore
	 * peakStart = punto di partenza dei picchi (in samples)
	 * peakEnd = punto finale dei picchi (in samples)
	 */

	timeind->updControl("Peaker/peaker1/mrs_natural/peakNeighbors", 40);
	timeind->updControl("Peaker/peaker1/mrs_real/peakSpacing", 0.1);
	timeind->updControl("Peaker/peaker1/mrs_natural/peakStart", 200);
	timeind->updControl("Peaker/peaker1/mrs_natural/peakEnd", 640);

	/*
	 * RICERCA DEL MASSIMO TRAMITE INTERPOLAZIONE
	 * nMaximums = 2 cerca due massimi
	 */

	timeind->updControl("MaxArgMax/mxr1/mrs_natural/interpolation", 1);
	timeind->updControl("Peaker/peaker1/mrs_natural/interpolation", 1);
	timeind->updControl("MaxArgMax/mxr1/mrs_natural/nMaximums", 2);

	/*
	 * COMPUTAZIONE SPETTRO DI AMPIEZZA (MAGNITUDE)
	 * il tipo di spettro specificato è "magnitude" ovvero ampiezza
	 * il flusso viene calcolare con modalità DixonDAFX06
	 */
	onset_strength->updControl("Accumulator/acc/Series/onsets/PowerSpectrum/power/mrs_string/spectrumType", "magnitude");
	onset_strength->updControl("Accumulator/acc/Series/onsets/Flux/flux/mrs_string/mode", "DixonDAFX06");

	/*
	 * CONFIGURAZIONE DEL BEAT HISTOGRAM CALCULATOR:
	 * startBin = "finestra" di partenza
	 * endBin = "finestra" finale
	 * factor = fattore di finestratura
	 */
	timeind->updControl("BeatHistogram/histo/mrs_natural/startBin", 0);
	timeind->updControl("BeatHistogram/histo/mrs_natural/endBin", 800);
	timeind->updControl("BeatHistogram/histo/mrs_real/factor", 16.0);

	/*
	 * Aggiunta di Timestretching e compressione
	 */

	timeind->updControl("Fanout/hfanout/TimeStretch/tsc1/mrs_real/factor", 0.5);
	timeind->updControl("Fanout/hfanout/Gain/id1/mrs_real/gain", 2.0);
	timeind->updControl("AutoCorrelation/acr/mrs_real/magcompress", 0.65);

	/*
	 * INPUT SHIFTER
	 */

	onset_strength->updControl("Accumulator/acc/Series/onsets/ShiftInput/si/mrs_natural/winSize", winSize);
	onset_strength->updControl("Accumulator/acc/Series/onsets/SoundFileSource/src/mrs_string/filename", sfName);

	/*
	 * Configurazione finale beat extractor:
	 * Il numero di campioni in ingresso viene settato alla dimensione della finestra di valutazione
	 */

	beatextractor->updControl("mrs_natural/inSamples", hopSize);

	/*
	 * Qui inizia la parte di processing vera a propria:
	 * Vengono creati due vettori, uno per i battiti primari, uno per i secondari
	 * bin indica il numero del bin attualmente in processing (tra 0 e 800)
	 */

	vector<mrs_real> bpms;
	vector<mrs_real> secondary_bpms;
	mrs_real bin;

	/*
	 * SETTAGGIO DELLE FINESTRE E DEI SALTI PER IL BEAT EXTRACTOR
	 */

	beatextractor->updControl("BeatPhase/beatphase/mrs_natural/bhopSize", bhopSize);
	beatextractor->updControl("BeatPhase/beatphase/mrs_natural/bwinSize", bwinSize);

	/*
	 * SETTAGGIO DEL NUMERO DI EXTRATICK PER LA RETE = dimensione della finestra/salto
	 */

	int extra_ticks = bwinSize/bhopSize;
	mrs_realvec tempos(2);
	mrs_realvec tempo_scores(2);
	tempo_scores.setval(0.0);

	/*
	 * PROCESSING
	 */



	while (1)
	{
		beatextractor->tick();
		mrs_realvec estimate = beatextractor->getctrl("FlowThru/timeind/MaxArgMax/mxr1/mrs_realvec/processedData")->to<mrs_realvec>();

		bin = estimate(1) * 0.25;

		tempos(0) = bin;
		tempos(1) = estimate(3) * 0.25;

		beatextractor->updControl("BeatPhase/beatphase/mrs_realvec/tempos", tempos);
		beatextractor->updControl("BeatPhase/beatphase/mrs_realvec/tempo_scores", tempo_scores);
		bpms.push_back(beatextractor->getctrl("BeatPhase/beatphase/mrs_real/phase_tempo")->to<mrs_real>());

		secondary_bpms.push_back(estimate(3) * 0.25);

		if (!beatextractor->getctrl("Series/onset_strength/Accumulator/acc/Series/onset/SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()) {
			extra_ticks --;
		}

		if (extra_ticks == 0)
			break;
	}
	mrs_real bpm_estimate = bpms[bpms.size()-1-extra_ticks];
	mrs_real secondary_bpm_estimate;
	secondary_bpm_estimate = secondary_bpms[bpms.size()-1-extra_ticks];

	mrs_real error = secondary_bpm_estimate - bpm_estimate;
	cout << "bpm_estimate = " << bpm_estimate << " BPM, with an error of: "
			<< abs(error) << endl;
	return bpm_estimate;
	delete onsets;
}