示例#1
0
	void 
	MarControl::callMarSystemUpdate()
	{
		if (state_ && msys_)
		{
			MarSystem* msys = msys_;
			msys->update(this);
			return;
		}
	}
示例#2
0
int run( const string system_filename, const CommandLineOptions & opt )
{
    int ticks = 0;
    if (opt.has("count"))
    {
      ticks = opt.value<int>("count");
      if (ticks < 1)
      {
        cerr << "Invalid value for option 'count' (must be > 0)." << endl;
        return 1;
      }
    }

    ifstream system_istream(system_filename);
    MarSystemManager mng;
    MarSystem* system = mng.getMarSystem(system_istream);
    if (!system) {
        cerr << "Could not load filesystem file:" << system;
        return 1;
    }

    bool realtime = opt.value<bool>("realtime");
    mrs_real sr = opt.value<mrs_real>("samplerate");
    mrs_natural block = opt.value<mrs_natural>("block");

    if (sr > 0)
      system->setControl("mrs_real/israte", sr);
    if (block > 0)
      system->setControl("mrs_natural/inSamples", block);
    system->update();

    RealTime::Runner runner(system);
    runner.setRtPriorityEnabled(realtime);

    runner.start((unsigned int)ticks);
    runner.wait();

    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]
}
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;
}
示例#5
0
// Play soundfile given by sfName, playbacknet contains the playback
// network of MarSystem objects
void sftransform(mrs_real gain, string outName)
{


  MarSystemManager mng;
  MarSystem* dest = mng.create("SoundFileSink", "dest");
  dest->updctrl("mrs_string/filename", "sftransformOutput.au");

  NetworkTCPSource* netSrc = new NetworkTCPSource("netSrc");

  MarSystem* playbacknet = mng.create("Series", "playbacknet");
  playbacknet->addMarSystem(netSrc);
  playbacknet->addMarSystem(mng.create("InvSpectrum", "ispk"));
  playbacknet->addMarSystem(mng.create("Gain", "gt"));
  playbacknet->addMarSystem(dest);

  // update controls if they are passed on cmd line...
  if ( port != 0 ) {
    netSrc->updctrl("mrs_natural/port", port);
  }

  playbacknet->update();

  playbacknet->linkctrl("mrs_natural/nChannels", "NetworkTCPSource/netSrc/mrs_natural/nChannels");
  playbacknet->linkctrl("mrs_natural/pos", "NetworkTCPSource/netSrc/mrs_natural/pos");
  playbacknet->linkctrl("mrs_natural/nChannels", "SoundFileSink/dest/mrs_natural/nChannels");
  playbacknet->linkctrl("mrs_bool/hasData", "NetworkTCPSource/netSrc/mrs_bool/hasData");
  playbacknet->linkctrl("mrs_bool/mute", "Gain/gt/mrs_bool/mute");


  // output network description to cout
  cout << *playbacknet << endl;

  // setup TCP Server and wait for connection...
  netSrc->refresh();
  cout << "Connection Established with: " << netSrc->getClientAddr() << endl;

  // udpate controls
  //playbacknet.updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);


  playbacknet->updctrl("Gain/gt/mrs_real/gain", gain);


  mrs_natural wc=0;
  mrs_natural samplesPlayed = 0;
  mrs_natural onSamples = playbacknet->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
  // mrs_natural repeatId = 1;

  mrs_real* controls = 0;

  while (true)
  {
    try {
      controls = playbacknet->recvControls();
      if ( controls != 0 ) {

        // get some reference controls, so if they have changed we update them
        mrs_natural inSamples = playbacknet->getctrl("mrs_natural/inSamples")->to<mrs_natural>();
        mrs_natural inObservations = playbacknet->getctrl("mrs_natural/inObservations")->to<mrs_natural>();
        mrs_real israte = playbacknet->getctrl("mrs_real/israte")->to<mrs_real>();

        if ( (mrs_natural)controls[1] != inSamples || (mrs_natural)controls[2] != inObservations
             || controls[3] != israte ) {

          playbacknet->updctrl("mrs_natural/inSamples", (mrs_natural)controls[1]);
          playbacknet->updctrl("mrs_natural/inObservations", (mrs_natural)controls[2]);
          playbacknet->updctrl("mrs_real/israte", controls[3]);
        }
      }
      playbacknet->tick();

    }
    catch( SocketException e ) {
      cerr << "Played " << wc << " slices of " << onSamples << " samples" << endl;
      exit(1);
    }
    wc ++;
    samplesPlayed += onSamples;
  }

}
示例#6
0
void recognize(string sfName, string outName)
{
  MarSystemManager mng;

  mrs_realvec out, tmpvec;
  mrs_natural inputsize, wsize;
  mrs_real samplingFreq;
  Collection inputs;

  MarSystem* all = mng.create("Series", "all");
  MarSystem* acc = mng.create("Accumulator", "acc");
  MarSystem* net = mng.create("Series", "net");
  MarSystem* total = mng.create("Series", "total");
  MarSystem* src = mng.create("SoundFileSource", "src");
  MarSystem* spc = mng.create("Spectrum", "spc");
  MarSystem* pws = mng.create("PowerSpectrum", "pws");
  MarSystem* crm = mng.create("Chroma","crm");
  MarSystem* csl = mng.create("ChromaScale","csl");
  MarSystem* ff = mng.create("Fanout", "ff");
  MarSystem* ss = mng.create("Fanout", "ss");
  MarSystem* mn = mng.create("Mean", "mn");
  MarSystem* std = mng.create("StandardDeviation", "std");

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

  MarSystem* ant = mng.create("Annotator", "ant");
  MarSystem* wks = mng.create("WekaSink", "wks");
  MarSystem* wksnet = mng.create("Series", "wksnet");

  inputs.read(sfName);

  mfccnet->addMarSystem(mng.create("Spectrum","spc2"));
  mfccnet->addMarSystem(mng.create("PowerSpectrum","psc2"));
  mfccnet->addMarSystem(mng.create("MFCC", "mfcc"));

  total->addMarSystem(src);
  total->addMarSystem(ff);
  net->addMarSystem(spc);
  net->addMarSystem(pws);
  net->addMarSystem(crm);
  net->addMarSystem(csl);
  ff->addMarSystem(mfccnet);
  ff->addMarSystem(net);
  acc->addMarSystem(total);
  all->addMarSystem(acc);
  ss->addMarSystem(mn);
  ss->addMarSystem(std);
  all->addMarSystem(ss);

  wksnet->addMarSystem(ant);
  wksnet->addMarSystem(wks);

  wksnet->updControl("mrs_natural/inSamples", 1);
  wksnet->updControl("mrs_natural/inObservations", all->getctrl("mrs_natural/onObservations")->to<mrs_natural>());
  wksnet->updControl("WekaSink/wks/mrs_string/labelNames", inputs.getLabelNames()); // change to just sfName
  wksnet->updControl("WekaSink/wks/mrs_natural/nLabels", (mrs_natural)inputs.getNumLabels());  // change to just 1
  wksnet->updControl("WekaSink/wks/mrs_natural/downsample", 1);
  wksnet->updControl("WekaSink/wks/mrs_string/filename", outName);
  tmpvec.create(all->getctrl("mrs_natural/onObservations")->to<mrs_natural>()+1,1);

  for(mrs_natural i=0; i<inputs.size(); ++i) {
    cout << "Now processing: " << inputs.entry(i) << endl;

    src->updControl("mrs_string/filename", inputs.entry(i));
    //spc->updControl("mrs_natural/inSamples", 1024);
    inputsize = src->getctrl("mrs_natural/size")->to<mrs_natural>();
    wsize = src->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
    inputsize /= wsize;
    acc->updControl("mrs_natural/nTimes", inputsize);
    samplingFreq = src->getctrl("mrs_real/israte")->to<mrs_real>();
    all->update();
    crm->setctrl("mrs_real/samplingFreq", samplingFreq);
    crm->setctrl("mrs_natural/lowOctNum", 2);
    crm->setctrl("mrs_natural/highOctNum", 5);
    crm->setctrl("mrs_natural/inObservations", wsize/2);
    crm->update();
    csl->setctrl("mrs_natural/inObservations", 12);
    csl->update();

    all->tick();

    out = all->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();

    wksnet->updControl("Annotator/ant/mrs_natural/label", (mrs_natural)inputs.labelNum(inputs.labelEntry(i)));  // change to just 0
    wksnet->updControl("WekaSink/wks/mrs_string/currentlyPlaying", inputs.entry(i));
    wksnet->process(out, tmpvec);

  }

  delete all;
  delete wksnet;

}
void recognize(string sfName, string hName, string tpName, string cnName, string szName, string outName)
{
  mrs_natural i, j, k, l, m;
  mrs_natural wsize, obs, totalCount;
  mrs_natural inputsize, maxind, outsize/*, prevNum*/;
  mrs_real msecondsPerFrame, sfrq;
  //mrs_natural startX, startY, endX, endY;
  MarSystemManager mng;
  MarSystem* netInp = mng.create("Series","netInp");
  MarSystem* netInp2 = mng.create("Series","netInp2");
  MarSystem* accInp = mng.create("Accumulator","accInp");
  MarSystem* accInp2 = mng.create("Accumulator", "accInp2");
  MarSystem* Inp = mng.create("Series","Inp");
  MarSystem* Inp2 = mng.create("Series", "Inp2");
  MarSystem* sim = mng.create("SimilarityMatrix", "sim");
  MarSystem* sim2 = mng.create("SimilarityMatrix", "sim2");
  MarSystem* met = mng.create("Metrix2", "met");
  MarSystem* dtw = mng.create("DTWWD", "dtw");
  MarSystem* met2 = mng.create("Metrix2", "met2");
  MarSystem* dtw2 = mng.create("DTW", "dtw");
  MarSystem* ap = mng.create("AveragingPattern","ap");
  //MarSystem* total = mng.create("Series","total");
  MarSystem* total2 = mng.create("Series", "total2");
  //MarSystem* total3 = mng.create("Series", "total3");
  //MarSystem* total4 = mng.create("Series", "total4");
  //MarSystem* ann = mng.create("Annotator","ann");
  MarSystem* ann2 = mng.create("Annotator", "ann2");
  //MarSystem* ann3 = mng.create("Annotator", "ann3");
  //MarSystem* ann4 = mng.create("Annotator", "ann4");
  //MarSystem* wks = mng.create("WekaSink","wks");
  MarSystem* wks2 = mng.create("WekaSink", "wks2");
  //MarSystem* wks3 = mng.create("WekaSink", "wks3");
  //MarSystem* wks4 = mng.create("WekaSink", "wks4");
  MarSystem* crm = mng.create("Chroma", "crm");
  MarSystem* dlt = mng.create("Delta", "dlt");
  Collection templates, countsclc, inputs, hfiles, sizesclc;
  ostringstream oss;
  realvec sizes, b, tmpcounts, counts, genres, tmpsizes, formin, delta, map;
  realvec dataInp, dataTpl, beginPos, endPos, tmpFeatures, tmpbegin, tmpend;
  realvec featuresInp, featuresTpl, /*outFeatures,*/ outFeatures2, /*outFeatures3, outFeatures4, tmpvec,*/ tmpvec2/*, tmpvec3, tmpvec4*/;
  realvec simInput, simOutput, algOutput, segments, tmpsimin, tmpsimout, tmpalgout;
  mrs_bool b_begin;
  vector<string> strVec;

  inputs.read(sfName);
  templates.read(tpName);
  hfiles.read(hName);
  countsclc.read(cnName);
  sizesclc.read(szName);
  outsize = 0;
  strVec.resize(templates.size());


  /*** load template patterns ***/

  oss.str("");
  l=0;
  for(i=0; i< (mrs_natural)countsclc.size(); ++i){
    tmpcounts.read(countsclc.entry(i));
    for(j=1; j<(mrs_natural)tmpcounts.getSize(); j++){
      if(tmpcounts(j) > 0){
	l++;
      }
    }
  }
  sizes.create(l+1);
  genres.create(l);
  //outFeatures.create(l+1,1);
  outFeatures2.create(templates.getNumLabels(),1);
  //outFeatures3.create(l*l,1);
  //outFeatures4.create(l,1);
  //tmpvec.create(outFeatures.getRows()+1,1);
  tmpvec2.create(outFeatures2.getRows()+1,1);
  //tmpvec3.create(outFeatures3.getRows()+1,1);
  //tmpvec4.create(outFeatures4.getRows()+1,1);
  tmpFeatures.create(1);
  l=0;
  for(i=0; i<(mrs_natural)countsclc.size(); ++i){
    tmpcounts.read(countsclc.entry(i));
    tmpsizes.read(sizesclc.entry(i));
    for(j=1; j<tmpcounts.getSize(); j++){
      if(tmpcounts(j) > 0){
	l++;
	sizes(l) = tmpsizes(j);
      }
    }
  }
  featuresTpl.create(sizes.getSize()*BIN,sizes.maxval());
  for(i=0; i<(mrs_natural)templates.size(); ++i){
    if(templates.labelName(i) != countsclc.labelName(i)){
      cerr << "Error: templates and counts don't match!" << endl;
      exit(-1);
    }
  }
  l=0;
  for(i=0; i<(mrs_natural)templates.size(); ++i){
    strVec[i] = templates.labelName(i);
    tmpcounts.read(countsclc.entry(i));
    dataTpl.read(templates.entry(i));
    tmpsizes.read(sizesclc.entry(i));
    for(j=1; j<tmpcounts.getSize(); j++){
      if(tmpcounts(j) > 0){
	genres(l) = i;
	//sizes(l+1) = tmpsizes(j);
	outsize += tmpsizes(j);
	oss << templates.labelName(i) << j << ",";
	for(k=0; k<BIN; k++){
	  for(m=0; m<sizes(l+1); m++){
	    featuresTpl(k+BIN*l,m) = dataTpl(k+BIN*(j-1),m);
	  }
	}
	l++;
      }
    }
  }

  /*** set controls to input series ***/

  netInp->addMarSystem(mng.create("SoundFileSource","inpsrc"));
  netInp->addMarSystem(mng.create("Windowing","ham"));
  netInp->addMarSystem(mng.create("Spectrum","spc"));
  netInp->addMarSystem(mng.create("PowerSpectrum","psc"));
  //netInp->addMarSystem(mng.create("MFCC","mfcc"));
  accInp->addMarSystem(netInp);
  accInp->updControl("mrs_natural/nTimes",ACC_INPUT);
  Inp->addMarSystem(accInp);

  netInp2->addMarSystem(mng.create("SoundFileSource","inpsrc2"));
  netInp2->addMarSystem(mng.create("Windowing","ham2"));
  netInp2->addMarSystem(mng.create("Spectrum","spc2"));
  netInp2->addMarSystem(mng.create("PowerSpectrum","psc"));
  netInp2->addMarSystem(crm);
  netInp2->addMarSystem(dlt);
  accInp2->addMarSystem(netInp2);
  accInp2->updControl("mrs_natural/nTimes",ACC_INPUT);
  Inp2->addMarSystem(accInp2);
  
  /*** set conttols to rhythm map ***/

  sim->updControl("mrs_natural/calcCovMatrix",2);
  sim->updControl("mrs_string/normalize", "MeanStd");
  sim->addMarSystem(met);
  sim2->updControl("mrs_natural/calcCovMatrix",2);
  sim2->updControl("mrs_string/normalize", "MeanStd");
  sim2->addMarSystem(met2);
  met->updControl("mrs_string/metric","euclideanDistance");
  met2->updControl("mrs_string/metric", "euclideanDistance");
  dtw->updControl("mrs_string/lastPos","lowest");
  dtw->updControl("mrs_string/startPos","lowest");
  dtw->updControl("mrs_string/localPath","diagonal");
  dtw->updControl("mrs_bool/weight",false);
  dtw->updControl("mrs_string/mode","OnePass");
  dtw2->updControl("mrs_string/lastPos", "end");
  dtw2->updControl("mrs_string/startPos", "zero");
  dtw2->updControl("mrs_string/localPath", "diagonal");
  dtw2->updControl("mrs_bool/weight", false);
  dtw2->updControl("mrs_string/mode", "normal");

  /*** create wekasink series ***/

  //total->addMarSystem(ann);
  //total->addMarSystem(wks);
  total2->addMarSystem(ann2);
  total2->addMarSystem(wks2);
  //total3->addMarSystem(ann3);
  //total3->addMarSystem(wks3);
  //total4->addMarSystem(ann4);
  //total4->addMarSystem(wks4);

  /*** update control of wekasink series ***/
  /*
  total->updControl("mrs_natural/inSamples",1);
  total->updControl("mrs_natural/inObservations",outFeatures.getRows());
  total->updControl("WekaSink/wks/mrs_string/labelNames",inputs.getLabelNames());
  total->updControl("WekaSink/wks/mrs_natural/nLabels",inputs.getNumLabels());
  total->updControl("WekaSink/wks/mrs_natural/downsample",1);
  total->updControl("WekaSink/wks/mrs_string/inObsNames",oss.str());
  total->updControl("WekaSink/wks/mrs_string/filename",outName);
  */
  total2->updControl("mrs_natural/inSamples",1);
  total2->updControl("mrs_natural/inObservations",outFeatures2.getRows());
  total2->updControl("WekaSink/wks2/mrs_string/labelNames",sfName);
  total2->updControl("WekaSink/wks2/mrs_natural/nLabels",1);
  total2->updControl("WekaSink/wks2/mrs_natural/downsample",1);
  total2->updControl("WekaSink/wks2/mrs_string/inObsNames",oss.str());
  //oss.str("");
  //oss << outName << "_add.arff";
  //total2->updControl("WekaSink/wks2/mrs_string/filename",oss.str());
  total2->updControl("WekaSink/wks2/mrs_string/filename",outName);
  /*
  total3->updControl("mrs_natural/inSamples",1);
  total3->updControl("mrs_natural/inObservations",outFeatures3.getRows());
  total3->updControl("WekaSink/wks3/mrs_string/labelNames",inputs.getLabelNames());
  total3->updControl("WekaSink/wks3/mrs_natural/nLabels",inputs.getNumLabels());
  total3->updControl("WekaSink/wks3/mrs_natural/downsample",1);
  total3->updControl("WekaSink/wks3/mrs_string/inObsNames",oss.str());
  oss.str("");
  oss << outName << "_LSA.arff";
  total3->updControl("WekaSink/wks3/mrs_string/filename",oss.str());

  total4->updControl("mrs_natural/inSamples",1);
  total4->updControl("mrs_natural/inObservations",outFeatures4.getRows());
  total4->updControl("WekaSink/wks4/mrs_string/labelNames",inputs.getLabelNames());
  total4->updControl("WekaSink/wks4/mrs_natural/nLabels",inputs.getNumLabels());
  total4->updControl("WekaSink/wks4/mrs_natural/downsample",1);
  total4->updControl("WekaSink/wks4/mrs_string/inObsNames",oss.str());
  oss.str("");
  oss << outName << "_distance.arff";
  total4->updControl("WekaSink/wks4/mrs_string/filename",oss.str());
  */


  /*** process ***/

  featuresInp.create(BIN,ACC_INPUT);
  b.create(BIN+2);
  simInput.create(BIN+featuresTpl.getRows(),ACC_INPUT);
  simOutput.create(outsize,ACC_INPUT);
  tmpsimin.create(BIN*2,ACC_INPUT);
  tmpsimout.create(ACC_INPUT,ACC_INPUT);
  algOutput.create(3*ACC_INPUT,2);
  tmpalgout.create(3*ACC_INPUT,2);
  delta.create(1,ACC_INPUT);
  tmpsizes.create(2);
  segments.create(1,1);
  tmpbegin.create(1,1);
  tmpend.create(1,1);
  for(l=0; l<(mrs_natural)inputs.size(); l++){
    cout << "Now processing: " << inputs.entry(l) << endl;

    /*** calculate input spectrogram ***/

    netInp->updControl("SoundFileSource/inpsrc/mrs_string/filename",inputs.entry(l));
    netInp2->updControl("SoundFileSource/inpsrc2/mrs_string/filename",hfiles.entry(l));
    inputsize = netInp->getctrl("SoundFileSource/inpsrc/mrs_natural/size")->to<mrs_natural>();
    wsize = netInp->getctrl("Windowing/ham/mrs_natural/size")->to<mrs_natural>();
    inputsize /= wsize;
    accInp->updControl("mrs_natural/nTimes",inputsize);
    accInp2->updControl("mrs_natural/nTimes",inputsize);
    Inp->tick();
    dataInp = Inp->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();

    /*** calculate input feature vector of input ***/

    featuresInp.stretch(BIN,dataInp.getCols());
    for(i=0; i<featuresInp.getRows(); ++i){
      for(j=0; j<featuresInp.getCols(); j++){
	featuresInp(i,j) = 0;
      }
    }
    sfrq = netInp->getctrl("SoundFileSource/inpsrc/mrs_real/osrate")->to<mrs_real>();
    obs = netInp->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
    for(i=0; i<BIN+2; ++i){
      b(i) = wsize*700/sfrq*(pow(10, (log10(1.0+sfrq/1400))*i/(BIN+1))-1);
    }
    for(j=0; j<BIN; j++){
      for(k=0; k<obs; k++){
	if(b(j) < k && k < b(j+1)){
	  for(i=0; i<dataInp.getCols(); ++i){
	    featuresInp(j,i) += dataInp(k,i)*(k-b(j))/(b(j+1)-b(j));
	  }
	} else if(b(j+1) <= k && k <= b(j+2)){
	  for(i=0; i<dataInp.getCols(); ++i){
	    featuresInp(j,i) += dataInp(k,i)*(b(j+2)-k)/(b(j+2)-b(j+1));
	  }
	}
      }
      for(i=0; i<featuresInp.getCols(); ++i){
	featuresInp(j,i) /= (b(j+2)-b(j))/2;
	featuresInp(j,i) = log(100000*featuresInp(j,i)+1);
      }
    }
    dataInp.stretch(0,0);

    // calculate input chroma delta
    crm->setctrl("mrs_real/samplingFreq", sfrq);
    crm->setctrl("mrs_natural/lowOctNum", 0);
    crm->setctrl("mrs_natural/highOctNum", 8);
    crm->setctrl("mrs_natural/inObservations", wsize/2);
    crm->update();

    dlt->setctrl("mrs_bool/sum", true);
    dlt->setctrl("mrs_bool/absolute", true);
    dlt->setctrl("mrs_bool/normalize", true);
    dlt->setctrl("mrs_natural/normSize", 40);
    dlt->update();
    Inp2->tick();
    delta = Inp2->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();

    /*** calculate input of SimilarityMatrix ***/
    simInput.stretch(featuresInp.getRows()+featuresTpl.getRows(),featuresInp.getCols());
    for(i=0; i<featuresInp.getCols(); ++i){
      for(j=0; j<featuresInp.getRows(); j++){
	simInput(j,i) = featuresInp(j,i);
      }
    }
    for(i=0; i<featuresTpl.getCols(); ++i){
      for(j=0; j<featuresTpl.getRows(); j++){
	simInput(j+featuresInp.getRows(),i) = featuresTpl(j,i);
      }
    }

    /*** update control of rhythm map ***/
    sizes(0) = featuresInp.getCols();
    sim->updControl("mrs_realvec/sizes",sizes);
    sim->updControl("mrs_natural/inSamples",simInput.getCols());
    sim->updControl("mrs_natural/inObservations",simInput.getRows());
    simOutput.stretch(outsize,sizes(0));
    dtw->updControl("mrs_realvec/sizes",sizes);
    dtw->setctrl("mrs_realvec/delta", delta);
    dtw->setctrl("mrs_real/deltaWeight", 10.0);
    dtw->updControl("mrs_natural/inSamples",simOutput.getCols());
    dtw->updControl("mrs_natural/inObservations",simOutput.getRows());
    algOutput.stretch(3*sizes(0),2);
    ap->updControl("mrs_realvec/sizes",sizes);
    ap->updControl("mrs_natural/inSamples",simInput.getCols());
    ap->updControl("mrs_natural/inObservations",simInput.getRows());

    /*** calculate the alignment ***/
    sim->process(simInput,simOutput);
    dtw->process(simOutput,algOutput);
    ap->updControl("mrs_realvec/alignment",algOutput);
    ap->process(simInput,simInput);

    /*** out the counts features vector ***/
    tmpFeatures.stretch(ap->getctrl("mrs_realvec/counts")->to<mrs_realvec>()(0)-1);
    counts = ap->getctrl("mrs_realvec/counts")->to<mrs_realvec>();
    //oss.str(""); oss << inputs.entry(l) << "_counts.dat";
    //counts.write(oss.str());
    for(i=1; i<counts.getSize(); ++i){
      counts(i) = counts(i)/counts(0);
    }
    counts(0) = 0;
    counts.maxval(&maxind);
    //cout << strVec[genres(maxind)] << endl;
    /*
    for(i=0; i<counts.getSize(); ++i){
      outFeatures(i,0) = counts(i);
    }
    */
    for(i=0; i<outFeatures2.getRows(); ++i){
      outFeatures2(i,0) = 0;
    }

    for(i=0; i<counts.getSize(); ++i){
      outFeatures2(genres(i),0) += counts(i);
    }
    /*for(i=0; i<outFeatures3.getRows(); ++i){
      for(j=0; j<outFeatures3.getCols(); j++){
	outFeatures3(i,j) = 0.0;
      }
    }
    
    b_begin = false;
    startX = algOutput(m,0);
    startY = algOutput(m,1);
    k = 0;
    prevNum = 1000;
    for(i=m; i<algOutput.getRows(); ++i){
      for(j=0; j<beginPos.getSize(); j++){
	if(algOutput(i,1) == beginPos(j)){
	  if(!b_begin){
	    b_begin = true;
	    m = i;
	    startX = algOutput(i,0);
	    startY = algOutput(i,1);
	  }
	  break;
	} else if(algOutput(i,1) == endPos(j)-1){
	  if(b_begin){
	    endX = algOutput(i,0);
	    endY = algOutput(i,1);
	    if(prevNum != 1000){
	      //tmpFeatures(k-1) = templatesDistance(map(j),prevNum);
	      outFeatures3(prevNum*(sizes.getSize()-1)+map(j),0)++;
	    }
	    prevNum = map(j);
	    k++;
	    b_begin = false;
	    break;
	  }
	}
      }
    }
    for(i=0; i<outFeatures3.getRows(); ++i){
      outFeatures3(i,0) /= tmpFeatures.getSize();
    }
	 */
	  
    // update control
    //total->updControl("Annotator/ann/mrs_natural/label",inputs.labelNum(inputs.labelEntry(l)));
    total2->updControl("Annotator/ann2/mrs_natural/label", 0);
    total2->process(outFeatures2,tmpvec2);

    // out segment data 
	
    tmpbegin.stretch(sizes.getSize()-1);
    tmpend.stretch(sizes.getSize()-1);
    tmpbegin(0) = 0;
    for(i=1; i<sizes.getSize()-1; ++i){
      tmpbegin(i) = sizes(i) + tmpbegin(i-1);
    }      
    for(i=0; i<sizes.getSize()-1; ++i){
      tmpend(i) = tmpbegin(i) + sizes(i+1);
    }
    m=0; i=0;
    while(m==0){
      if(algOutput(i,0) >= 0 && algOutput(i,1) >= 0){
	m = i;
      }
      ++i ;
    }

    totalCount = 1;
    for(i=0; i<tmpbegin.getSize(); ++i){
      if(tmpbegin(i) <= algOutput(m,1) && algOutput(m,1) < tmpend(i)){
	k = i;
	break;
      }
    }
    b_begin = true;
    for(i=m; i<algOutput.getRows(); ++i){
      for(j=0; j<tmpbegin.getSize(); j++){
	if(algOutput(i,1) == tmpbegin(j)){
	  if(!b_begin){
	    b_begin = true;
	    totalCount++;
	  }
	  break;
	} else if(algOutput(i,1) == tmpend(j)-1){
	  b_begin = false;
	  break;
	}
      }
    }

    segments.stretch(totalCount,3);
    msecondsPerFrame = (mrs_real)wsize/(mrs_real)sfrq*1000.0;
    segments(0,0) = 0.0; segments(0,2) = k+1;
    b_begin = true;
    k = 0;
    for(i=m; i<algOutput.getRows(); ++i){
      for(j=0; j<tmpbegin.getSize(); j++){
	if(algOutput(i,1) == tmpbegin(j)){
	  if(!b_begin){
	    b_begin = true;
	    segments(k,1) = (mrs_real)algOutput(i,0)*msecondsPerFrame;
	    //segments(k,1) = algOutput(i,0);
	    k++;
	    segments(k,0) = (mrs_real)algOutput(i,0)*msecondsPerFrame;
	    //segments(k,0) = algOutput(i,0);
	    segments(k,2) = genres(j)+1;
	  }
	  break;
	} else if(algOutput(i,1) == tmpend(j)-1){
	  b_begin = false;
	  break;
	}
      }
    }
    segments(k,1) = (mrs_real)algOutput(algOutput.getRows()-1,0)*msecondsPerFrame;
    //segments(k,1) = algOutput(algOutput.getRows()-1,0);
    //oss.str(""); oss << sfName << "_genres.txt";
    oss.str(""); oss << inputs.entry(l) << "_segments.txt";
    segments.write(oss.str());
    /*
    for(j=0; j<outFeatures4.getRows(); j++){
      outFeatures4(j,0) = 0.0;
    }
    for(i=0; i<segments.getRows(); ++i){
      for(k=0; k<genres.getSize(); k++){
	tmpsizes(0) = segments(i,1) - segments(i,0);
	tmpsizes(1) = sizes(k+1);
	tmpsimin.stretch(BIN*2,tmpsizes.maxval());
	sim2->updControl("mrs_realvec/sizes",tmpsizes);
	sim2->update();
	sim2->updControl("mrs_natural/inSamples",tmpsimin.getCols());
	sim2->updControl("mrs_natural/inObservations",tmpsimin.getRows());
	tmpsimout.stretch(tmpsizes(1),tmpsizes(0));
	dtw2->updControl("mrs_realvec/sizes",tmpsizes);
	dtw2->update();
	dtw2->updControl("mrs_natural/inSamples",tmpsimout.getCols());
	dtw2->updControl("mrs_natural/inObservations",tmpsimout.getRows());
	tmpalgout.stretch(tmpsizes(0)+tmpsizes(1),2);
	for(j=0; j<tmpsizes(0); j++){
	  for(m=0; m<BIN; m++){
	    tmpsimin(m,j) = featuresInp(m,j+segments(i,0));
	  }
	}
	for(j=tmpsizes(0); j<tmpsizes.maxval(); j++){
	  for(m=0; m<BIN; m++){
	    tmpsimin(m,j) = 0.0;
	  }
	}
	for(j=0; j<tmpsizes(1); j++){
	  for(m=0; m<BIN; m++){
	    tmpsimin(m+BIN,j) = featuresTpl(m+k*BIN,j);
	  }
	}
	for(j=tmpsizes(1); j<tmpsizes.maxval(); j++){
	  for(m=0; m<BIN; m++){
	    tmpsimin(m+BIN,j) = 0.0;
	  }
	}
	sim2->process(tmpsimin,tmpsimout);
	dtw2->process(tmpsimout,tmpalgout);
	if(tmpsizes(0) > 0 && dtw2->getctrl("mrs_real/totalDistance")->to<mrs_real>()> 0){
	  outFeatures4(k,0) += dtw2->getctrl("mrs_real/totalDistance")->to<mrs_real>()/tmpsizes(0);
	}
      }
    }
    if(segments.getRows() > 0){
      for(j=0; j<outFeatures4.getRows(); j++){
	outFeatures4(j,0) /= segments.getRows();
      }
    }
    total4->updControl("Annotator/ann4/mrs_natural/label", inputs.labelNum(inputs.labelEntry(l)));
    total4->process(outFeatures4,tmpvec4);*/
  }

  /*** delete memory ***/
  
  delete sim;
  delete sim2;
  delete dtw;
  delete dtw2;
  delete ap;
  delete Inp;
  delete Inp2;
  //delete total;
  delete total2;
  //delete total3;
  //delete total4;

}
void record_orcas(mrs_real length, mrs_natural year, 
        string id1, string id2, string id3, string id4) 
{ 


    copt = 8;
    sropt = 44100.0;
    int bufferSize = 6144;


    MarSystemManager mng;

    MarSystem* asrc = mng.create("AudioSource", "asrc");
    MarSystem* dest1 = mng.create("SoundFileSink", "dest1");
    MarSystem* dest2 = mng.create("SoundFileSink", "dest2");
    MarSystem* dest3 = mng.create("SoundFileSink", "dest3");
    MarSystem* dest4 = mng.create("SoundFileSink", "dest4");


    ostringstream oss1;
    oss1 << "/Users/orcalab/orcaArchive/" << year << "/" << id1 << ".wav";
    ostringstream oss2;
    oss2 << "/Users/orcalab/orcaArchive/" << year << "/" << id2 << ".wav";
    ostringstream oss3;
    oss3 << "/Users/orcalab/orcaArchive/" << year << "/" << id3 << ".wav";
    ostringstream oss4;
    oss4 << "/Users/orcalab/orcaArchive/" << year << "/" << id4 << ".wav";

    string fname1 = oss1.str();
    string fname2 = oss2.str();
    string fname3 = oss3.str();
    string fname4 = oss4.str();


    dest1->updControl("mrs_natural/inObservations", 2);
    dest1->updControl("mrs_natural/inSamples", bufferSize);
    dest1->updControl("mrs_real/israte", sropt);
    dest1->updControl("mrs_string/filename", fname1);


    dest2->updControl("mrs_natural/inObservations", 2);
    dest2->updControl("mrs_natural/inSamples", bufferSize);
    dest2->updControl("mrs_real/israte", sropt);
    dest2->updControl("mrs_string/filename", fname2);

    dest3->updControl("mrs_natural/inObservations", 2);
    dest3->updControl("mrs_natural/inSamples", bufferSize);
    dest3->updControl("mrs_real/israte", sropt);
    dest3->updControl("mrs_string/filename", fname3);


    dest4->updControl("mrs_natural/inObservations", 2);
    dest4->updControl("mrs_natural/inSamples", bufferSize);
    dest4->updControl("mrs_real/israte", sropt);
    dest4->updControl("mrs_string/filename", fname4);

    asrc->setctrl("mrs_natural/nChannels", copt);
    asrc->setctrl("mrs_natural/inSamples", bufferSize);
    asrc->setctrl("mrs_real/israte", sropt);
    asrc->update();
    // asrc->updControl("mrs_real/gain", gain);



    mrs_real srate = asrc->getctrl("mrs_real/israte")->to<mrs_real>();
    mrs_natural inSamples = asrc->getctrl("mrs_natural/inSamples")->to<mrs_natural>();
    mrs_natural iterations = (mrs_natural)((srate * length * 60.0) / inSamples);

    realvec rin;
    realvec rout;
    realvec orca1;
    realvec orca2;
    realvec orca3;
    realvec orca4;


    rin.create(copt, bufferSize);
    rout.create(copt, bufferSize);

    orca1.create(2, bufferSize);
    orca2.create(2, bufferSize);
    orca3.create(2, bufferSize);
    orca4.create(2, bufferSize);

    mrs_natural t;

    cout << "Recording " << length << " minutes to files: " << endl;
    cout << fname1 << endl;
    cout << fname2 << endl;
    cout << fname3 << endl;
    cout << fname4 << endl;

    mrs_natural minutes =0;


    for (mrs_natural i = 0; i < iterations; ++i) 
    {
        if (((i % 430)==0)&&(i != 0))
        {
            minutes ++;
            cout << minutes << ":" << lengthopt << endl;
        }
        asrc->process(rin,rout);
        for (t=0; t < bufferSize; t++)
        { 
            orca1(0,t) = rout(0,t);
            orca1(1,t) = rout(1,t);
            orca2(0,t) = rout(2,t);
            orca2(1,t) = rout(3,t);
            orca3(0,t) = rout(4,t);
            orca3(1,t) = rout(5,t);
            orca4(0,t) = rout(6,t);
            orca4(1,t) = rout(7,t);
        }
        dest1->process(orca1,orca1);
        dest2->process(orca2, orca2);
        dest3->process(orca3, orca3);
        dest4->process(orca4, orca4);

    }

    cout << "Recording complete" << endl;
    cout << "Recorded to files: " << endl;
    cout << fname1 << endl;
    cout << fname2 << endl;
    cout << fname3 << endl;
    cout << fname4 << endl;
    delete dest1;
    delete dest2;
    delete dest3;
    delete dest4;
    delete asrc;
}
//Pluck Karplus Strong Model Plucked.cpp outputs to DAC
void PluckLive(string deviceopt, mrs_real pos, mrs_real fre, mrs_real loz, mrs_real stret)
{



#ifdef MARSYAS_MIDIIO  
    RtMidiIn *midiin = 0;
    std::vector<unsigned char> message;
    double stamp;
    int nBytes;
    int i;

    // initialize RtMidi
    try {
        midiin = new RtMidiIn();
    }
    catch (RtError3 &error) {
        error.printMessage();
        exit(1);
    }

    // find input midi port 
    try {
        midiin->openPort(portopt);
    }
    catch (RtError3 &error) {
        error.printMessage();
        exit(1);

    }
    MarSystemManager mng;
    MarSystem* series = mng.create("Series", "series");


    // create 16 plucked Karplus-Strong strings
    MarSystem* mix  = mng.create("Fanout", "mix");
    for (mrs_natural i = 0; i < 16; i++) 
    {
        ostringstream oss;
        oss << "src" << i;
        mix->addMarSystem(mng.create("Plucked", oss.str()));  
    }

    series->addMarSystem(mix);
    series->addMarSystem(mng.create("Sum", "sum"));
    series->addMarSystem(mng.create("Gain", "gain"));
    series->addMarSystem(mng.create("AudioSink", "dest"));
    series->update();


    series->updctrl("Gain/gain/mrs_real/gain", 0.10);
    series->updctrl("AudioSink/dest/mrs_real/israte", 
					series->getctrl("Fanout/mix/Plucked/src0/mrs_real/osrate"));

    series->updctrl("AudioSink/dest/mrs_natural/bufferSize", 128); 
    series->updctrl("Fanout/mix/Plucked/src0/mrs_real/frequency",fre);
    series->updctrl("Fanout/mix/Plucked/src0/mrs_real/pluckpos",pos);
    series->updctrl("Fanout/mix/Plucked/src0/mrs_real/loss",loz);
    series->updctrl("mrs_natural/inSamples", 64);
    series->update();


    // initially only play one string 
    for (int i=1; i < 16; i++)
    {
        ostringstream oss1;
        oss1 << "Fanout/mix/Plucked/src" 
			 << i << "/mrs_real/nton";
        series->updctrl(oss1.str(), 0.0);      
    }



    mrs_natural t=0;

    int channel, type, byte2, byte3;
    int mes_count = 0;
    mrs_real freq;
    int p0byte3, p1byte3, p2byte3;


    // used to keep track of polyphony
    vector<int> voices;
    for (int i=0; i < 16; i++) 
    {
        voices.push_back(0);
    }




    while(1)
    {

        // for (int i=0; i < 4; i++)
        // {
        stamp = midiin->getMessage( &message );
        // }

        nBytes = message.size();      

        if (nBytes > 0) 
        {


            byte3 = message[2];
            byte2 = message[1];
            type = message[0];

            if (deviceopt == "Keyboard")
            {

                if (type == 144) 
                {

                    // allocate voice 
                    for (int i=0; i < 16; i++)
                    {
                        if (voices[i] == 0)
                        {
                            voices[i] = byte2;
                            break;
                        }
                    }
                    // free voice if velocity is 0 (implicit noteoff)
                    for (int i=0; i < 16; i++)
                    {
                        if ((byte3 == 0)&&(voices[i] == byte2))
                        {
                            voices[i] = 0;
                            break;
                        }
                    }	      

                    for (int i=0; i < 16; i++)
                    {

                        ostringstream oss, oss1;
                        oss << "Fanout/mix/Plucked/src" 
                            << i << "/mrs_real/frequency";

                        oss1 << "Fanout/mix/Plucked/src" 
							 << i << "/mrs_real/nton";

                        if (voices[i] != 0) 
                        {
                            freq =  220.0 * pow( 2.0,(voices[i] - 57.0) / 12.0 );

                            series->updctrl(oss1.str(), 1.0);      

                            series->updctrl(oss.str(),freq);      	       		      
                            // series->update();

                        }
                    }

                }

                if (type == 128) 
                {
                    // free voice if noteoff
                    for (int i=0; i < 16; i++)
                    {
                        if (voices[i] == byte2)
                        {

                            ostringstream oss, oss1;

                            oss1 << "Fanout/mix/Plucked/src" 
								 << i << "/mrs_real/nton";


                            series->updctrl(oss1.str(), 0.0);      
                            voices[i] = 0;
                            break;
                        }
                    }	      
                }

            }


            if (deviceopt == "KiomB") 
            {
                if (byte2 == 0)
                {
                    freq =  220.0 * pow( 2.0, (byte3 - 57.0) / 12.0 );

                    if ((byte3 > 25)&&(abs(byte3 - p0byte3) > 2))
                    {
                        series->updctrl("Fanout/mix/Plucked/src0/mrs_real/frequency",freq);      		    
                    }

                }		
                p0byte3 = byte3;
            }



            if (byte2 == 1)
            {
                freq = 220.0 * pow( 2.0, (byte3 - 60.0) / 12.0 );
                if ((byte3 > 25)&&(abs(byte3 - p1byte3) > 2))
                    series->updctrl("Fanout/mix/Plucked/src1/mrs_real/frequency",freq);      

                p1byte3 = byte3;

            }


            if (byte2 == 2)
            {
                freq = 220.0 * pow( 2.0, (byte3 - 62.0) / 12.0 );
                if ((byte3 > 25)&&(abs(byte3 - p2byte3) > 2))
                    series->updctrl("Fanout/mix/Plucked/src2/mrs_real/frequency",freq);      

                p2byte3 = byte3;

            }
        }


        series->tick();
        t++;
    }


#endif


}