Example #1
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;

}
Example #2
0
void tags() {
	if (!wekafname_Set()) return;
	if (!twekafname_Set()) return;

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	cout << "DONE" << endl;
}
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;
}
void
old_pitchextract(string sfName, mrs_natural winSize, mrs_natural hopSize,
                 mrs_natural lowPitch, mrs_natural highPitch, mrs_real threshold,
                 mrs_real playPitches)
{
  (void) winSize;
  (void) hopSize;
  (void) threshold;
  (void) playPitches;
  MRSDIAG("pitchextract.cpp - pitchextract");

  MarSystemManager mng;

  // Build the pitch extractor network
  MarSystem* pitchExtractor = mng.create("Series", "pitchExtractor");
  // pitchExtractor->addMarSystem(mng.create("AudioSource", "src"));

  pitchExtractor->addMarSystem(mng.create("SoundFileSource", "src"));
  pitchExtractor->updControl("SoundFileSource/src/mrs_string/filename", sfName);


  pitchExtractor->addMarSystem(mng.create("AutoCorrelation", "acr"));
  pitchExtractor->updControl("AutoCorrelation/acr/mrs_real/magcompress", 0.67);
  pitchExtractor->addMarSystem(mng.create("HalfWaveRectifier", "hwr"));

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

  pitchExtractor->addMarSystem(fanout);

  MarSystem* fanin = mng.create("Fanin", "fanin");
  fanin->addMarSystem(mng.create("Gain", "id2"));
  fanin->addMarSystem(mng.create("Negative", "nid"));

  pitchExtractor->addMarSystem(fanin);
  pitchExtractor->addMarSystem(mng.create("HalfWaveRectifier", "hwr"));

  pitchExtractor->addMarSystem(mng.create("PlotSink", "psink0"));
  pitchExtractor->addMarSystem(mng.create("Peaker", "pkr"));
  // pitchExtractor->addMarSystem(mng.create("PlotSink", "psink1"));
  pitchExtractor->addMarSystem(mng.create("MaxArgMax", "mxr"));

  // update controls
  pitchExtractor->updControl("mrs_natural/inSamples", 1024);
  pitchExtractor->updControl("Fanout/fanout/TimeStretch/tsc/mrs_real/factor", 0.5);
  // Convert pitch bounds to samples
  cout << "lowPitch = " << lowPitch << endl;
  cout << "highPitch = " << highPitch << endl;

  mrs_real lowFreq = pitch2hertz(lowPitch);
  mrs_real highFreq = pitch2hertz(highPitch);

  mrs_natural lowSamples =
    // hertz2samples(highFreq, pitchExtractor->getctrl("AudioSource/src/mrs_real/osrate")->to<mrs_real>());
    hertz2samples(highFreq, pitchExtractor->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>());
  mrs_natural highSamples =
    //     hertz2samples(lowFreq, pitchExtractor->getctrl("AudioSource/src/mrs_real/osrate")->to<mrs_real>());
    hertz2samples(lowFreq, pitchExtractor->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>());


  cout << "lowSamples" << lowSamples << endl;
  cout << "highSamples" << highSamples << endl;


  pitchExtractor->updControl("Peaker/pkr/mrs_real/peakSpacing", 0.00);
  pitchExtractor->updControl("Peaker/pkr/mrs_real/peakStrength", 0.4);
  pitchExtractor->updControl("Peaker/pkr/mrs_natural/peakStart", lowSamples);
  pitchExtractor->updControl("Peaker/pkr/mrs_natural/peakEnd", highSamples);
  pitchExtractor->updControl("MaxArgMax/mxr/mrs_natural/nMaximums", 1);

  cout << (*pitchExtractor) << endl;

  realvec pitchres(pitchExtractor->getctrl("mrs_natural/onObservations")->to<mrs_natural>(), pitchExtractor->getctrl("mrs_natural/onSamples")->to<mrs_natural>());


  realvec win(pitchExtractor->getctrl("mrs_natural/inObservations")->to<mrs_natural>(),
              pitchExtractor->getctrl("mrs_natural/inSamples")->to<mrs_natural>());


  /// playback network
  MarSystem* playback = mng.create("Series", "playback");
  playback->addMarSystem(mng.create("SineSource", "ss"));
  playback->addMarSystem(mng.create("AudioSink", "dest"));
  playback->updControl("mrs_natural/inSamples", 512);
  //playback->updControl("mrs_bool/initAudio", true);


  mrs_real pitch;
  while (pitchExtractor->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
  {
    if (plopt)
      playback->tick();

    pitchExtractor->process(win, pitchres);

    // pitch = samples2hertz((mrs_natural)pitchres(1), pitchExtractor->getctrl("AudioSource/src/mrs_real/osrate")->to<mrs_real>());
    pitch = samples2hertz((mrs_natural)pitchres(1), pitchExtractor->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>());

    cout << "conf" << "---" << pitchres(0) << endl;
    cout << "midi" << "---" << hertz2pitch(pitch) << endl ;
    cout << "hz " << "---" << pitch << endl;

    if (pitchres(0) > 0.05)
      playback->updControl("SineSource/ss/mrs_real/frequency", pitch);
  }
}
Example #6
0
void recognize(string sfName, string tpName)
{
  mrs_natural  i, j, k, l;
  mrs_natural nsamples, sfrq, obs, outsize;
  mrs_natural wsize = 0;

  mrs_natural maxsize, totalCount, inputsize;
  mrs_real msecondsPerFrame;
  MarSystemManager mng;
  MarSystem* netInp = mng.create("Series", "netInp");
  MarSystem* accInp = mng.create("Accumulator", "accInp");
  MarSystem* wavInp = mng.create("Series", "wavInp");
  MarSystem* wavaccInp = mng.create("Accumulator","wavaccInp");
  MarSystem* wavnetInp = mng.create("Series","wavnetInp");
  MarSystem* wavplOut = mng.create("Parallel","wavplOut");
  MarSystem* wavsrdOut = mng.create("Shredder","wavsrdOut");
  MarSystem* plTpl = mng.create("Parallel","plTpl");
  MarSystem* Inp = mng.create("Series","Inp");
  MarSystem* Tpl = mng.create("Series","Tpl");
  MarSystem* sim = mng.create("SimilarityMatrix", "sim");
  MarSystem* met = mng.create("Metric2", "met");
  MarSystem* dtw = mng.create("DTW", "dtw");
  MarSystem* ap = mng.create("AveragingPattern", "ap");
  Collection templates;
  ostringstream oss, oss2;
  string tmpStr;
  realvec sizes, b, countvector, segments;
  realvec beginPos, endPos, order;
  realvec dataInp, dataTpl, inpspec, allspec, tplspec;
  realvec featuresInp, featuresTpl;
  realvec simInput, simOutput, algOutput, tplOutput;
  mrs_bool b_begin;
  MarSystem* netTpl[MAX_TEMPLATES];
  MarSystem* accTpl[MAX_TEMPLATES];
  MarSystem* srsTpl[MAX_TEMPLATES];
  MarSystem* wavnetOut[MAX_TEMPLATES];

  templates.read(tpName);
  sizes.create(templates.size()+1);
  outsize = 0;

  /*** caluculate templates spectrogram ***/

  if(templates.size() > MAX_TEMPLATES) {
    cerr << "Error: invalied templates size!" << endl;
    exit(-1);
  }
  for(i=0; i< (mrs_natural)templates.size(); ++i) {
    oss.str(""); oss << "net" << i;
    tmpStr = oss.str();
    netTpl[i] = mng.create("Series", tmpStr);
    oss.str(""); oss << "srs" << i;
    srsTpl[i] = mng.create("Series", oss.str());
    oss.str(""); oss << "tplsrc" << i;
    netTpl[i]->addMarSystem(mng.create("SoundFileSource",oss.str()));
    oss2.str(""); oss2 << "SoundFileSource/" << oss.str() << "/mrs_string/filename";
    netTpl[i]->updControl(oss2.str(),templates.entry(i));
    oss2.str(""); oss2 << "SoundFileSource/" << oss.str() << "/mrs_natural/size";
    nsamples = netTpl[i]->getctrl(oss2.str())->to<mrs_natural>();
    oss.str(""); oss << "han" << i;
    netTpl[i]->addMarSystem(mng.create("Windowing",oss.str()));
    oss2.str(""); oss2 << "Windowing/" << oss.str() << "/mrs_natural/onSamples";
    wsize = netTpl[i]->getctrl(oss2.str())->to<mrs_natural>();
    sizes(i+1) = nsamples/wsize;
    oss.str(""); oss << "spc" << i;
    netTpl[i]->addMarSystem(mng.create("Spectrum",oss.str()));
    oss.str(""); oss << "psc" << i;
    netTpl[i]->addMarSystem(mng.create("PowerSpectrum",oss.str()));
    //oss.str(""); oss << "mfcc" << i;
    //netTpl[i]->addMarSystem(mng.create("MFCC",oss.str()));
    oss.str(""); oss << "acc" << i;
    accTpl[i] = mng.create("Accumulator",oss.str());
    accTpl[i]->addMarSystem(netTpl[i]);
    accTpl[i]->updControl("mrs_natural/nTimes",ACC_TEMPLATE);
    srsTpl[i]->addMarSystem(accTpl[i]);
    plTpl->addMarSystem(srsTpl[i]);
    outsize += sizes(i+1);
  }
  Tpl->addMarSystem(plTpl);
  Tpl->tick();
  dataTpl = Tpl->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
  maxsize = sizes.maxval();





  /*** calculate the feature vector of template ***/

  featuresTpl.create(BIN*(templates.size()+1),dataTpl.getCols());
  sfrq = netTpl[0]->getctrl("SoundFileSource/tplsrc0/mrs_real/osrate")->to<mrs_real>();
  obs = netTpl[0]->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
  b.create(BIN+2);
  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(l=0; l<(mrs_natural)templates.size(); l++) {
      for(k=0; k<obs; k++) {
        if(b(j) < k && k < b(j+1)) {
          for(i=0; i<dataTpl.getCols(); ++i) {
            featuresTpl(j+l*BIN,i) += dataTpl(k+l*obs,i)*(k-b(j))/(b(j+1)-b(j));
          }
        } else if(b(j+1) <= k && k <= b(j+2)) {
          for(i=0; i<dataTpl.getCols(); ++i) {
            featuresTpl(j+l*BIN,i) += dataTpl(k+l*obs,i)*(b(j+2)-k)/(b(j+2)-b(j+1));
          }
        }
      }
    }
    for(l=0; l<(mrs_natural)templates.size(); l++) {
      for(i=0; i<featuresTpl.getCols(); ++i) {
        featuresTpl(j+l*BIN,i) /= (b(j+2)-b(j))/2;
        featuresTpl(j+l*BIN,i) = log(100000*featuresTpl(j+l*BIN,i)+1);
      }
    }
  }



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

  netInp->addMarSystem(mng.create("SoundFileSource","inpsrc"));
  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);

  /*** set controls to rhythm map ***/

  sim->updControl("mrs_natural/calcCovMatrix",2);
  sim->updControl("mrs_string/normalize", "MeanStd");
  sim->addMarSystem(met);
  met->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");

  /*** calculate first templates ***/

  tplOutput.create(featuresTpl.getRows(),maxsize);
  for(i=0; i<maxsize; ++i) {
    for(j=0; j<featuresTpl.getRows(); j++) {
      tplOutput(j,i) = featuresTpl(j,i);
    }
  }



  /*** calculate input spectrogram ***/

  netInp->updControl("SoundFileSource/inpsrc/mrs_string/filename",sfName);
  inputsize = netInp->getctrl("SoundFileSource/inpsrc/mrs_natural/size")->to<mrs_natural>();
  inputsize /= wsize;
  accInp->updControl("mrs_natural/nTimes",inputsize);
  Inp->tick();
  dataInp = Inp->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();



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

  featuresInp.create(BIN,dataInp.getCols());
  sfrq = netInp->getctrl("SoundFileSource/inpsrc/mrs_real/osrate")->to<mrs_real>();
  obs = netInp->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
  b.create(BIN+2);
  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);
    }
  }


  /*** calculate input of SimilarityMatrix ***/

  simInput.create(featuresInp.getRows()+featuresTpl.getRows(),dataInp.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.create(outsize,sizes(0));
  dtw->updControl("mrs_realvec/sizes",sizes);
  dtw->updControl("mrs_natural/inSamples",simOutput.getCols());
  dtw->updControl("mrs_natural/inObservations",simOutput.getRows());
//<<<<<<< .mine
//=======
//
//>>>>>>> .r3676
  algOutput.create(3*sizes(0),2);
  ap->updControl("mrs_realvec/sizes",sizes);
  ap->updControl("mrs_natural/inSamples",simInput.getCols());
  ap->updControl("mrs_natural/inObservations",simInput.getRows());



  /*** iterative learning ***/

  for(l=0; l<NITERATION; l++) {
    sim->process(simInput,simOutput);
    dtw->process(simOutput,algOutput);
    ap->updControl("mrs_realvec/alignment",algOutput);
    ap->process(simInput,simInput);
    MarControlAccessor acs(dtw->getctrl("mrs_real/totalDistance"));
    cout << "ITR#" << l << " " << acs.to<mrs_real>() << endl;
  }

  /*** calculate the segment information ***/

  beginPos.create(sizes.getSize()-1);
  endPos.create(sizes.getSize()-1);
  beginPos(0) = 0;
  for(i=1; i<sizes.getSize()-1; ++i) {
    beginPos(i) = sizes(i) + beginPos(i-1);
  }
  for(i=0; i<sizes.getSize()-1; ++i) {
    endPos(i) = beginPos(i) + sizes(i+1);
  }
  order.create(sizes.getSize()-1);
  l=0; i=0;
  while(l==0) {
    if(algOutput(i,0) >= 0 && algOutput(i,1) >= 0) {
      l = i;
    }
    ++i ;
  }
  k = 1; totalCount = 1;
  for(i=0; i<beginPos.getSize(); ++i) {
    if(beginPos(i) <= algOutput(l,1) && algOutput(l,1) < endPos(i)) {
      order(i) = k;
      k ++;
      break;
    }
  }
  b_begin = true;
  for(i=l; i<algOutput.getRows(); ++i) {
    for(j=0; j<beginPos.getSize(); j++) {
      if(algOutput(i,1) == beginPos(j)) {
        if(!b_begin) {
          if(order(j) == 0) {
            order(j) = k;
            k++;
          }
          b_begin = true;
          totalCount++;
        }
        break;
      } else if(algOutput(i,1) == endPos(j)-1) {
        b_begin = false;
        break;
      }
    }
  }
  segments.create(totalCount,3);
  msecondsPerFrame = (mrs_real)wsize/(mrs_real)sfrq*1000.0;
  segments(0,0) = 0.0; segments(0,2) = 1;
  b_begin = true;
  k = 0;
  for(i=l; i<algOutput.getRows(); ++i) {
    for(j=0; j<beginPos.getSize(); j++) {
      if(algOutput(i,1) == beginPos(j)) {
        if(!b_begin) {
          b_begin = true;
          segments(k,1) = (mrs_real)algOutput(i,0)*msecondsPerFrame;
          k++;
          segments(k,0) = (mrs_real)algOutput(i,0)*msecondsPerFrame;
          segments(k,2) = order(j);
        }
        break;
      } else if(algOutput(i,1) == endPos(j)-1) {
        b_begin = false;
        break;
      }
    }
  }
  segments(k,1) = (mrs_real)algOutput(algOutput.getRows()-1,0)*msecondsPerFrame;
  oss.str(""); oss << sfName << "_segments.txt";
  segments.write(oss.str());
  cout << "Now writing a segmentation file: " << oss.str() << "." << endl;

  /*** create WAV file of template patterns ***/

  wavInp->addMarSystem(wavaccInp);
  wavaccInp->addMarSystem(wavnetInp);
  wavnetInp->addMarSystem(mng.create("SoundFileSource","wavsrc"));
  wavnetInp->updControl("SoundFileSource/wavsrc/mrs_string/filename",sfName);
  wavnetInp->addMarSystem(mng.create("Spectrum","wavspc"));
  wavaccInp->updControl("mrs_natural/nTimes",inputsize);
  wavInp->tick();
  inpspec = wavInp->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
  allspec.create(inpspec.getRows()*sizes.getSize(),inpspec.getCols());
  for(i=0; i<inpspec.getCols(); ++i) {
    for(j=0; j<inpspec.getRows(); j++) {
      allspec(j,i) = inpspec(j,i);
    }
  }
  sizes(0) = inpspec.getCols();
  ap->updControl("mrs_natural/inSamples",allspec.getCols());
  ap->updControl("mrs_natural/inObservations",allspec.getRows());
  ap->updControl("mrs_realvec/sizes",sizes);
  ap->updControl("mrs_realvec/alignmnet",algOutput);
  ap->process(allspec,allspec);
  tplspec.create(inpspec.getRows()*order.maxval(),maxsize);
  for(k=1; k<=order.maxval(); k++) {
    for(l=0; l<order.getSize(); l++) {
      if(order(l) == k) {
        for(i=0; i<maxsize; ++i) {
          for(j=0; j<inpspec.getRows(); j++) {
            tplspec(j+(k-1)*inpspec.getRows(),i) = allspec(j+(l+1)*inpspec.getRows(),i);
          }
        }
        break;
      }
    }
  }
  for(i=0; i<order.maxval(); ++i) {
    oss.str(""); oss << "wavnetOut" << i;
    wavnetOut[i] = mng.create("Series",oss.str());
    wavplOut->addMarSystem(wavnetOut[i]);
    oss.str(""); oss << "ispOut" << i;
    wavnetOut[i]->addMarSystem(mng.create("InvSpectrum",oss.str()));
    oss.str(""); oss << "sfsOut" << i;
    wavnetOut[i]->addMarSystem(mng.create("SoundFileSink",oss.str()));
    oss.str(""); oss << "SoundFileSink/sfsOut" << i << "/mrs_natural/inObservations";
    wavnetOut[i]->updControl(oss.str(),1);
    oss.str(""); oss << "SoundFileSink/sfsOut" << i << "/mrs_natural/inSamples";
    wavnetOut[i]->updControl(oss.str(),inpspec.getRows());
    oss.str(""); oss << "SoundFileSink/sfsOut" << i << "/mrs_real/israte";
    wavnetOut[i]->updControl(oss.str(),sfrq);
    oss2.str(""); oss2 << "SoundFileSink/sfsOut" << i << "/mrs_string/filename";
    oss.str(""); oss << sfName << "_template" << i+1 << ".wav";
    wavnetOut[i]->updControl(oss2.str(),oss.str());
    wavnetOut[i]->updControl("mrs_natural/inObservations",inpspec.getRows());
    wavnetOut[i]->updControl("mrs_natural/inSamples",1);
    cout << "Now writing WAV file: " << oss.str() << "." << endl;
  }
  wavsrdOut->addMarSystem(wavplOut);
  wavsrdOut->updControl("mrs_natural/nTimes",maxsize);
  wavsrdOut->updControl("mrs_natural/inSamples",tplspec.getCols());
  wavsrdOut->updControl("mrs_natural/inObservations",tplspec.getRows());
  wavplOut->updControl("mrs_natural/inSamples",1);
  wavplOut->updControl("mrs_natural/inObservations",tplspec.getRows());
  realvec tmpRealvec(order.maxval(),wsize);
  wavsrdOut->process(tplspec,tmpRealvec);

  /*** delete memory ***/

  delete sim;
  delete dtw;
  delete ap;
  delete Inp;
  delete Tpl;
  delete wavInp;
  delete wavsrdOut;

}
int 
main(int argc, const char **argv)
{
	string name = argv[1];
	mrs_natural channel(atoi(argv[2]));
	mrs_natural window(atoi(argv[3]));
	mrs_real gain(atof(argv[4]));


	MarSystemManager mng;
	MarSystem* src = mng.create("SoundFileSource", "src");
	MarSystem* erb = mng.create("ERB","ERBfilterBank");
	MarSystem* dest = mng.create("AudioSink", "dest");
  



  
	src->updctrl("mrs_natural/inSamples", window);
	src->updctrl("mrs_string/filename", name);


	// This core dumps. Need to check it out. 
	erb->setctrl("mrs_natural/inObservations", src->getctrl("mrs_natural/onObservations"));
  
	cout << *src << endl;
  
	cout << src->getctrl("mrs_natural/onObservations") << endl;
	cout << src->getctrl("mrs_natural/onSamples") << endl;  
	erb->updctrl("mrs_natural/inObservations", 1);
  

	erb->updctrl("mrs_natural/inSamples", src->getctrl("mrs_natural/onSamples"));
	erb->updctrl("mrs_real/israte",src->getctrl("mrs_real/osrate"));
	erb->updctrl("mrs_natural/numChannels",64);
	erb->updctrl("mrs_real/lowFreq",100.0f);
  
	dest->updctrl("mrs_natural/inObservations", src->getctrl("mrs_natural/onObservations"));
	dest->updctrl("mrs_natural/inSamples", src->getctrl("mrs_natural/onSamples"));
	dest->updctrl("mrs_real/israte", src->getctrl("mrs_real/osrate"));
	dest->updctrl("mrs_natural/nChannels", 1);
	dest->updctrl("mrs_bool/initAudio", true);


  
	realvec src_in, dest_in;
	realvec src_out, erb_out, dest_out;
  
	src_in.create(src->getctrl("mrs_natural/inObservations")->to<mrs_natural>(), src->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
	src_out.create(src->getctrl("mrs_natural/onObservations")->to<mrs_natural>(), src->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
  
	erb_out.create(erb->getctrl("mrs_natural/onObservations")->to<mrs_natural>(), erb->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
  
	dest_in.create(dest->getctrl("mrs_natural/inObservations")->to<mrs_natural>(), dest->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
	dest_out.create(dest->getctrl("mrs_natural/onObservations")->to<mrs_natural>(), dest->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
  
	while (src->getctrl("mrs_bool/hasData")->to<mrs_bool>()){
		src->process(src_in, src_out);
		erb->process(src_out, erb_out);
    
		for (mrs_natural i = 0; i < erb->getctrl("mrs_natural/onSamples")->to<mrs_natural>(); i++){
			dest_in(i) = gain*erb_out(channel,i);
		}
		dest->process(dest_in, dest_out);
	}
  
	return 0;
}