void textract_trainAccumulator(string sfName, mrs_natural offset, mrs_natural duration, mrs_real start, mrs_real length, mrs_real gain, mrs_natural label, string pluginName, string wekafname, mrs_natural memSize, string extractorStr, TimeLine& tline)
{
	MarSystemManager mng;

	MRSDIAG("sfplay.cpp - sfplay");

	// default 
	if (extractorStr == EMPTYSTRING) 
		extractorStr = "STFT";

	// Find proper soundfile format and create SignalSource 
	MarSystem *src = mng.create("SoundFileSource", "src");

	src->updctrl("mrs_string/filename", sfName);
	src->updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);

	if (tlineName == EMPTYSTRING)
	{
		mrs_natural hops = src->getctrl("mrs_natural/size")->to<mrs_natural>() * src->getctrl("mrs_natural/nChannels")->to<mrs_natural>() / 2048 + 1;
		tline.regular(100, hops);
	}

	MarSystem *dest_;
	dest_ = mng.create("AudioSink", "dest");

	MarSystem *series = mng.create("Series", "playbacknet");
	series->addMarSystem(src);
	series->addMarSystem(dest_);

	series->updctrl("AudioSink/dest/mrs_natural/nChannels", 
		series->getctrl("SoundFileSource/src/mrs_natural/nChannels")->to<mrs_natural>());  

	// Calculate duration, offset parameters if necessary 
	if (start > 0.0f) 
		offset = (mrs_natural) (start 
		* src->getctrl("mrs_real/israte")->to<mrs_real>() 
		* src->getctrl("mrs_natural/nChannels")->to<mrs_natural>());
	if (length != 30.0f) 
		duration = (mrs_natural) (length 
		* src->getctrl("mrs_real/israte")->to<mrs_real>() 
		* src->getctrl("mrs_natural/nChannels")->to<mrs_natural>());

	// accumulate feature vectors over 30 seconds 
	MarSystem* acc = mng.create("Accumulator", "acc");

	acc->updctrl("mrs_natural/nTimes", 100);

	// Calculate windowed power spectrum and then 
	// calculate specific feature sets 
	MarSystem* spectralShape = mng.create("Series", "spectralShape");
	spectralShape->addMarSystem(mng.create("Windowing", "hamming"));
	spectralShape->addMarSystem(mng.create("Spectrum","spk"));
	spectralShape->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	spectralShape->updctrl("PowerSpectrum/pspk/mrs_string/spectrumType","power");  

	// Spectrum Shape descriptors
	MarSystem* spectrumFeatures = mng.create("Fanout", "spectrumFeatures");

	if (extractorStr == "STFT") 
	{
		spectrumFeatures->addMarSystem(mng.create("Centroid", "cntrd"));
		spectrumFeatures->addMarSystem(mng.create("Rolloff", "rlf"));      
		spectrumFeatures->addMarSystem(mng.create("Flux", "flux"));
	}
	else if (extractorStr == "STFTMFCC")
	{
		spectrumFeatures->addMarSystem(mng.create("Centroid", "cntrd"));
		spectrumFeatures->addMarSystem(mng.create("Rolloff", "rlf"));      
		spectrumFeatures->addMarSystem(mng.create("Flux", "flux"));
		spectrumFeatures->addMarSystem(mng.create("MFCC", "mfcc"));
	}
	else if (extractorStr == "MFCC")
		spectrumFeatures->addMarSystem(mng.create("MFCC", "mfcc"));
	else if (extractorStr == "SCF")
		spectrumFeatures->addMarSystem(mng.create("SCF", "scf"));
	else if (extractorStr == "SFM") 
		spectrumFeatures->addMarSystem(mng.create("SFM", "sfm"));

	mng.registerPrototype("SpectrumFeatures", spectrumFeatures->clone());
	spectralShape->addMarSystem(mng.create("SpectrumFeatures", "spectrumFeatures"));
	mng.registerPrototype("SpectralShape", spectralShape->clone());

	//  add time-domain zerocrossings
	MarSystem* features = mng.create("Fanout", "features");
	features->addMarSystem(mng.create("SpectralShape", "SpectralShape"));
	if (extractorStr == "STFT")
		features->addMarSystem(mng.create("ZeroCrossings", "zcrs"));      
	mng.registerPrototype("Features", features->clone());

	// Means and standard deviation (statistics) for texture analysis 
	MarSystem* statistics = mng.create("Fanout","statistics");
	statistics->addMarSystem(mng.create("Mean", "mn"));
	statistics->addMarSystem(mng.create("StandardDeviation", "std"));
	mng.registerPrototype("Statistics", statistics->clone());

	// weka output 
	MarSystem *wsink = mng.create("WekaSink","wsink");

	// Build the overall feature calculation network 
	MarSystem* featureNetwork = mng.create("Series", "featureNetwork");

	featureNetwork->addMarSystem(src->clone());
	featureNetwork->addMarSystem(mng.create("Features", "features"));
	featureNetwork->addMarSystem(mng.create("Memory", "memory"));
	featureNetwork->updctrl("Memory/memory/mrs_natural/memSize", memSize);
	featureNetwork->addMarSystem(mng.create("Statistics", "statistics"));  

	// add network to accumulator
	acc->addMarSystem(featureNetwork->clone());

	// Final network compute 30-second statistics 
	MarSystem* total = mng.create("Series", "total");
	total->addMarSystem(acc->clone());
	total->addMarSystem(mng.create("Statistics", "statistics2"));
	// total->addMarSystem(mng.create("Mean", "mn2"));
	total->addMarSystem(wsink->clone());

	// update controls 
	total->updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);
	total->updctrl("Accumulator/acc/Series/featureNetwork/" + src->getType() + "/src/mrs_natural/pos", offset);      

	mrs_natural wc = 0;
	mrs_natural samplesPlayed =0;

	// main loop for extracting the features 
	string className = "";

	total->updctrl("Accumulator/acc/Series/featureNetwork/SoundFileSource/src/mrs_string/filename", sfName);
	wc = 0;  	  
	samplesPlayed = 0;

	total->updctrl("WekaSink/wsink/mrs_natural/nLabels", (mrs_natural)3);
	if (wekafname == EMPTYSTRING) 
		total->updctrl("WekaSink/wsink/mrs_string/filename", "weka2.arff");
	else 
		total->updctrl("WekaSink/wsink/mrs_string/filename", wekafname);  

	// total->tick();

	for (int r = 0; r < tline.numRegions(); r++)
	{
		cout << "start = " << tline.regionStart(r) << endl;
		cout << "end = " << tline.regionEnd(r) << endl;

		total->updctrl("Accumulator/acc/Series/featureNetwork/SoundFileSource/src/mrs_natural/pos", (mrs_natural)tline.regionStart(r) * tline.lineSize());
		total->updctrl("mrs_natural/inSamples", tline.lineSize());
		if (tlineName == EMPTYSTRING)
		{
			if ((tline.regionClass(r) > 0) && (tline.regionClass(r) != 4))//[?]
			{
				total->updctrl("WekaSink/wsink/mrs_natural/label", tline.regionClass(r)-1);
			}
		}
		else
			total->tick();
	}

	if (pluginName == EMPTYSTRING) // output to stdout 
		cout << (*total) << endl;      
	else 
	{
		ofstream oss(pluginName.c_str());
		oss << (*total) << endl;
	}
}
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]
}
Spectrum2ACMChroma::Spectrum2ACMChroma(mrs_string inName)
	:MarSystem("Spectrum2ACMChroma",inName)
{
	addControls();

	MarSystem* theNewSystem;
	MarSystemManager theManager;

	// Add new MarSystems to manager
	MarSystem* theDummy;
	theDummy = new MedianFilter("Anything");
	theManager.registerPrototype("MedianFilter",theDummy);
	theDummy = new PeakInObservation("Anything");
	theManager.registerPrototype("PeakInObservation",theDummy);
	theDummy = new Negative("Anything");
	theManager.registerPrototype("Negative",theDummy);
	theDummy = new Signum("Anything");
	theManager.registerPrototype("Signum",theDummy);
	theDummy = new F0Analysis("Anything");
	theManager.registerPrototype("F0Analysis",theDummy);
	theDummy = new Pitch2Chroma("Anything");
	theManager.registerPrototype("Pitch2Chroma",theDummy);

	// ------------------------ DEFINE CHROMA EXTRACTOR ------------------
	Spectrum2ACMChromaNet_ = theManager.create("Series","SER1");

	// --- 8.1 Compute peaks minus background in spectrum
	MarSystem* theFan2 = theManager.create("FanOutIn","FAN2");

	// ------ 8.1.1 Compute background spectrum
	MarSystem* theSeries3 = theManager.create("Series","SER3");

	// --------- 8.1.1a Median filter
	theNewSystem = theManager.create("MedianFilter","MedianFilter");
	theSeries3->addMarSystem(theNewSystem);

	// --------- 8.1.1b Amplify background spectrum
	theNewSystem = theManager.create("Gain","Gain");
	theSeries3->addMarSystem(theNewSystem);

	// --------- 8.1.1c Reverse sign of background spectrum (for subtraction)
	theNewSystem = theManager.create("Negative","Negative");
	theSeries3->addMarSystem(theNewSystem);

	// ------ 8.1.2 Compute peaks in spectrum
	theNewSystem = theManager.create("PeakInObservation","FindPeaks");

	// --- Determine the positions of the salient peaks
	//     Therefore, subtract peaks and background via sum in fanOutIn
	//     (Default combinator: +)
	theFan2->addMarSystem(theNewSystem);
	theFan2->addMarSystem(theSeries3);

	// --- 8.2 Derive booleans from salient peaks
	MarSystem* theSeries2 = theManager.create("Series","SER2");
	theSeries2->addMarSystem(theFan2);

	theNewSystem = theManager.create("Signum","Sign");
	theSeries2->addMarSystem(theNewSystem);

	// --- 8.3 Compute salient peaks
	//         Therefore, multiply booleans with spectrum via multiply in fanOutIn
	MarSystem* theFan1 = theManager.create("FanOutIn","FAN1");
	theFan1->addMarSystem(theSeries2);

	theNewSystem = theManager.create("Gain","Gain");	// No function
	theFan1->addMarSystem(theNewSystem);

	Spectrum2ACMChromaNet_->addMarSystem(theFan1);

	// 9. Perform pitch analysis
	theNewSystem = theManager.create("F0Analysis","F0Analysis");
	Spectrum2ACMChromaNet_->addMarSystem(theNewSystem);

	// 10. Map pitches to chroma
	theNewSystem = theManager.create("Pitch2Chroma","Pitch2Chroma");
	Spectrum2ACMChromaNet_->addMarSystem(theNewSystem);

	// ------------------------ DEFINE FIXED PARAMETERS ------------------
	mrs_string theControlString = "FanOutIn/FAN1/Series/SER2/FanOutIn/FAN2/"
		"Series/SER3/Gain/Gain/mrs_real/gain";
	Spectrum2ACMChromaNet_->updControl(theControlString,2.);

	theControlString = "FanOutIn/FAN1/Series/SER2/FanOutIn/FAN2/"
		"PeakInObservation/FindPeaks/mrs_real/HystFactor";
	Spectrum2ACMChromaNet_->updControl(theControlString,sqrt(2.));

	theControlString = "FanOutIn/FAN1/Gain/Gain/mrs_real/gain";
	Spectrum2ACMChromaNet_->updControl(theControlString,1.);

	theControlString = "FanOutIn/FAN1/mrs_string/combinator";
	Spectrum2ACMChromaNet_->updControl(theControlString,"*");
}