Exemplo n.º 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;

}
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;
}
Exemplo n.º 4
0
void outputWaveformPNG(string inFileName, string outFileName)
{
#ifdef MARSYAS_PNG
	int length;
	int height = 150;
	int middle_right;
	int middle_left;

	double min = 99999999999.9;
	double max = -99999999999.9;

	length = getFileLengthForWaveform(inFileName,windowSize_,min,max);
	double pngLength = length;
	double pngHeight = height;
	
	

	pngwriter png(length,height,0,outFileName.c_str());

	MarSystemManager mng;

	// A series to contain everything
	MarSystem* net = mng.create("Series", "net");

	// The sound file
	net->addMarSystem(mng.create("SoundFileSource", "src"));
	net->updControl("SoundFileSource/src/mrs_string/filename", inFileName);
	net->updControl("SoundFileSource/src/mrs_natural/pos", position_);
	net->setctrl("mrs_natural/inSamples", windowSize_);
	net->addMarSystem(mng.create("MaxMin","maxmin"));

	mrs_natural channels = net->getctrl("mrs_natural/onObservations")->to<mrs_natural>();

	if (verboseopt_) {
		cout << "channels=" << channels << endl;
	}

	if (channels == 2) {
		middle_right = (height/4);
		middle_left = (height/2)+(height/4);
	} else {
		middle_left = (height/2);
	}

	realvec processedData;

	// Give it a white background
	png.invert();

	// A line across the middle of the plot
	png.line(0,middle_left,length,middle_left,0,0,0);
	if (channels == 2) {
		png.line(0,middle_right,length,middle_right,0,0,0);
	}
  
	double x = 0;

	double y_max_right = 0;
	double y_min_right = 0;
	double y_max_right_prev = 0;
	double y_min_right_prev = 0;

	double y_max_left = 0;
	double y_min_left = 0;
	double y_max_left_prev = 0;
	double y_min_left_prev = 0;

	double draw_color;

	// If we are just displaying individual samples, make the line dark blue.
	if (windowSize_ == 1) {
		draw_color = 0.0;
	} else {
		draw_color = 0.2;
	}
  
	while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()
		   && (ticks_ == -1 || x < ticks_))  {
		net->tick();
		processedData = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();

		if (channels == 2) {
			y_max_right = processedData(1,0) / 2.0 * height;
			y_min_right = processedData(1,1) / 2.0 * height;
		}

		y_max_left = processedData(0,0) / 2.0 * height;
		y_min_left = processedData(0,1) / 2.0 * height;

		//
		// Draw the right waveform
		//
		if (channels == 2) {
			// Draw a line from the maximum to the minimum value
			png.line(x,middle_right+y_min_right,x,middle_right+y_max_right,0.0,0.0,1.0);
	
			// Shade the middle part of the line lighter blue
			double right_height = (y_max_right - y_min_right) / 4.0;
			png.line(x,(middle_right+y_min_right)+right_height,x,(middle_right+y_max_right)-right_height,0.5,0.5,1.0);
	
			// Fill in any missing segments with light blue
			if (y_min_right_prev > y_max_right) {
				png.line(x,middle_right+y_min_right_prev,x,middle_right+y_max_right,draw_color,draw_color,1.0);
			} else if (y_max_right_prev < y_min_right) {
				png.line(x,middle_right+y_max_right_prev,x,middle_right+y_min_right,draw_color,draw_color,1.0);
			}
		}

		//
		// Draw the left waveform
		//
		// Draw a line from the maximum to the minimum value
		png.line(x,middle_left+y_min_left,x,middle_left+y_max_left,0.0,0.0,1.0);

		// Shade the middle part of the line lighter blue
		double left_height = (y_max_left - y_min_left) / 4.0;
		png.line(x,(middle_left+y_min_left)+left_height,x,(middle_left+y_max_left)-left_height,0.5,0.5,1.0);
	
		// Fill in any missing segments with light blue
		if (y_min_left_prev > y_max_left) {
			png.line(x,middle_left+y_min_left_prev,x,middle_left+y_max_left,draw_color,draw_color,1.0);
		} else if (y_max_left_prev < y_min_left) {
			png.line(x,middle_left+y_max_left_prev,x,middle_left+y_min_left,draw_color,draw_color,1.0);
		}
	

		y_max_right_prev = y_max_right;
		y_min_right_prev = y_min_right;
		y_max_left_prev = y_max_left;
		y_min_left_prev = y_min_left;

		x++;

	}

	if (width_ !=-1) 
		pngLength = width_;
	if (height_ != -1) 
		pngHeight = height_;
	
	if ((width_ !=-1)||(height_ != -1))
	{
		png.scale_wh(pngLength, pngHeight);
	}
	



	png.close();


	delete net;
#endif
}
Exemplo n.º 5
0
void correlogramPNGs(string inFileName, string outFilePrefix)
{
#ifdef MARSYAS_PNG 
	
	MarSystemManager mng;
	MarSystem* net = mng.create("Series", "net");

	net->addMarSystem(mng.create("SoundFileSource", "src"));
	net->addMarSystem(mng.create("Stereo2Mono", "stereo2mono"));
	net->addMarSystem(mng.create("AudioSink", "dest"));
	net->addMarSystem(mng.create("Windowing", "ham"));
	net->addMarSystem(mng.create("Spectrum", "spk"));
	net->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	net->addMarSystem(mng.create("Memory", "mem"));

	MarSystem* parallel = mng.create("Parallel", "parallel");
	net->addMarSystem(parallel);

	int powerSpectrumSize = (windowSize_/2)+1;
	for (int i = 0; i < powerSpectrumSize; ++i) {
		std::stringstream ss;
		ss << "auto" << i;
		parallel->addMarSystem(mng.create("AutoCorrelation", ss.str()));
	}

	net->updControl("SoundFileSource/src/mrs_string/filename", inFileName);
	net->setctrl("mrs_natural/inSamples", windowSize_);
	net->updControl("Memory/mem/mrs_natural/memSize", memorySize_);

	net->updControl("mrs_real/israte", 44100.0);
	net->updControl("AudioSink/dest/mrs_bool/initAudio", true);

	realvec data;
  
	int counter = 0;
	realvec max_data(powerSpectrumSize);

	while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()) {
		cout << "Processing tick " << counter << endl;
		net->tick();
		data = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
		// cout << "data" << data << endl;

		// Create the png we are going to write into
		std::stringstream outFileName;
		outFileName << outFilePrefix << std::setfill('0') << std::setw(5) << counter << ".png";
		pngwriter png(int(windowSize_),int(powerSpectrumSize),0,outFileName.str().c_str());

		// Find the maximum value of the data
		max_data.setval(-999.9);
		for (int x = 0; x < memorySize_; x++) {
			for (int y = 0; y < powerSpectrumSize; y++) {
				if (data(y,x) > max_data(y)) {
					max_data(y) = data(y,x);
				}
			}
		}

		// for (int x = 0; x < memorySize; x++) {
		//   cout << "max_data(" << x << ")" << max_data(x) << endl;
		// }

		// Plot all the data points
		for (int x = 0; x < memorySize_; x++) {
			for (int y = 0; y < powerSpectrumSize; y++) {
				double color = 1.0 - (double(data(y,x)) * (1.0 / max_data(y)));
				// cout << "x=" << x << " y=" << y << " data(y,x)=" << data(y,x) << " color=" << color << endl;
				png.plot(int(x),int(y),color,color,color);
			}
		}

		png.close();
		counter++;
	}
#endif
}
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;
}