Exemple #1
0
void
toy_with_arff_in_out(mrs_string in_name, mrs_string out_name)
{
    MarSystemManager mng;

    MarSystem *net = mng.create("Series", "net");
    net->addMarSystem(mng.create("WekaSource", "src"));
    net->addMarSystem(mng.create("WekaSink", "dest"));

    net->updControl("WekaSource/src/mrs_string/filename", in_name);

    net->updControl("WekaSink/dest/mrs_natural/nLabels",
                    net->getControl("WekaSource/src/mrs_natural/nClasses"));
    net->updControl("WekaSink/dest/mrs_string/labelNames",
                    net->getControl("WekaSource/src/mrs_string/classNames"));
    net->updControl("WekaSink/dest/mrs_bool/regression",
                    net->getControl("WekaSource/src/mrs_bool/regression"));

    // must happen after setting the above controls
    net->updControl("WekaSink/dest/mrs_string/filename", out_name);

    while ( !net->getctrl("WekaSource/src/mrs_bool/done")->to<mrs_bool>() )
    {
        net->tick();
    }
    delete net;
}
Exemple #2
0
// Variation that outputs RMS and Flux
void 
output_rmsflux(string inFileName)
{

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

	net->addMarSystem(mng.create("SoundFileSource", "src"));
	net->addMarSystem(mng.create("Stereo2Mono", "s2m"));

	// A fanout that will do both RMS and Flux calculations
	MarSystem* fanout = mng.create("Fanout","fanout");
	net->addMarSystem(fanout);

	// The branch to do the RMS
	MarSystem* rms_series = mng.create("Series","rms_series");
	rms_series->addMarSystem(mng.create("Rms", "rms"));
	fanout->addMarSystem(rms_series);

	// The branch to do the Flux
	MarSystem* flux_series = mng.create("Series","flux_series");
	flux_series->addMarSystem(mng.create("ShiftInput", "si"));
	flux_series->addMarSystem(mng.create("Windowing", "win"));
	flux_series->addMarSystem(mng.create("Spectrum","spk"));
	flux_series->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	flux_series->addMarSystem(mng.create("Flux", "flux")); 
	fanout->addMarSystem(flux_series);

	// Update the controls with required values
	net->updControl("SoundFileSource/src/mrs_string/filename", inFileName);

	realvec processedData;
	float time = 0;
	mrs_natural samples_per_tick = net->getControl("SoundFileSource/src/mrs_natural/onSamples")->to<mrs_natural>();
	mrs_real rate = net->getControl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
	mrs_real sec_per_tick = samples_per_tick / rate;
	while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()) {
		net->tick();
		processedData = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
		cout << time << "," << processedData(0,0) << "," << processedData(1,0) << endl;
		time += sec_per_tick;
	}

	delete net;

}
Exemple #3
0
int getFileLengthForWaveform(string inFileName, int windowSize_, double& min, double& max) {

	MarSystemManager mng;

	// A series to contain everything
	MarSystem* net = mng.create("Series", "net");
	
	// The sound file
	net->addMarSystem(mng.create("SoundFileSource", "src"));
	net->addMarSystem(mng.create("Stereo2Mono", "s2m"));
	net->addMarSystem(mng.create("ShiftInput", "si"));
	net->updControl("SoundFileSource/src/mrs_string/filename", inFileName);


	mrs_real srate = net->getControl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
	if ((position_ == 0) && (start_ != 0.0))
		position_ = (mrs_natural) (srate * start_);
	
	if ((ticks_ == -1) && (length_ != -1.0))
		ticks_ = (mrs_natural) ((length_ * srate) / windowSize_);
	
	net->updControl("SoundFileSource/src/mrs_natural/pos", position_);
	net->updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize_);
	net->updControl("ShiftInput/si/mrs_natural/winSize", windowSize_);
	
	// Compute the AbsMax of this window
	net->addMarSystem(mng.create("AbsMax","absmax"));

	realvec processedData;

	int length = 0;

	while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() 
		   && (ticks_ == -1 || length < ticks_))  {
		net->tick();
		length++;

		processedData = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
		if (processedData(0) < min)
			min = processedData(0);
		if (processedData(0) > max)
			max = processedData(0);
	}


	delete net;

	if (verboseopt_) {
		cout << "length=" << length << endl;
		cout << "max=" << max << endl;
		cout << "min=" << min << endl;
	}

	return length;
}
int getFileLengthForSummaryITD(string inFileName, double& min, double& max, double& average) {

  realvec processedData;
  double dataLength = 0;
  double dataTotal = 0.0;

  MarSystemManager mng;
  MarSystem* net = mng.create("Series", "net");
  net->addMarSystem(mng.create("SoundFileSource", "src"));
  net->addMarSystem(mng.create("Stereo2Mono", "s2m"));
  net->addMarSystem(mng.create("ShiftInput", "si"));
  net->addMarSystem(mng.create("Spectrum","spk"));
  net->addMarSystem(mng.create("PowerSpectrum","pspk"));
  net->updControl("PowerSpectrum/pspk/mrs_string/spectrumType", "decibels");
  net->updControl("SoundFileSource/src/mrs_string/filename", inFileName);

  mrs_real srate = net->getControl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
  if ((position_ == 0) && (start_ != 0.0))
    position_ = (mrs_natural) (srate * start_);

  if ((ticks_ == -1) && (length_ != -1.0))
    ticks_ = (mrs_natural) ((length_ * srate) / windowSize_);

  net->updControl("SoundFileSource/src/mrs_natural/pos", position_);
  net->updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize_);
  net->updControl("ShiftInput/si/mrs_natural/winSize", windowSize_);
  net->updControl("mrs_natural/inSamples", int(hopSize_));


  mrs_real frequency = net->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
  double fftBins = windowSize_ / 2.0 + 1;  // N/2 + 1
  mrs_natural nChannels = net->getctrl("SoundFileSource/src/mrs_natural/onObservations")->to<mrs_natural>();

  double maxBin = fftBins * (highFreq_ / (frequency / nChannels));
  double minBin = fftBins * (lowFreq_ / (frequency / nChannels));
  // cout << "maxBin = " << maxBin << endl;

  int length = 0;
  while ( net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()
          && (ticks_ == -1 || length < ticks_)) {
    net->tick();
    length++;

    processedData = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
    for (int i = minBin; i < maxBin; ++i) {
      for (int j = 0; j < processedData.getCols(); j++) {
        if (processedData(i,j) < min)
          min = processedData(i,j);
        if (processedData(i,j) > max)
          max = processedData(i,j);
        dataLength += 1;
        dataTotal += processedData(i,j);
      }
    }
  }

  delete net;

  average = dataTotal / dataLength;

  if (verboseopt_) {
    cout << "length=" << length << endl;
    cout << "max=" << max << endl;
    cout << "min=" << min << endl;
    cout << "average=" << average << endl;
  }

  return length;
}
Exemple #5
0
int main(int argc, char *argv[])
{
  if (argc < 3)
  {
    cerr << "Usage: <input file> <output file>" << endl;
    return 1;
  }

  char *input_filename = argv[1];
  char *output_filename = argv[2];

  detector::registerScripts();

  ScriptTranslator translator;
  MarSystem *system = translator.translateRegistered("detector.mrs");
  if (!system)
  {
    cerr << "Failure loading script!" << endl;
    return 1;
  }

  MarControlPtr input_control = system->control("input");
  MarControlPtr output_control = system->control("output");
  MarControlPtr done_control = system->control("done");

  if ( input_control.isInvalid() ||
       output_control.isInvalid() ||
       done_control.isInvalid() )
  {
    cerr << "Failure: Invalid script!" << endl;
    delete system;
    return 1;
  }

  input_control->setValue(string(input_filename));
  //output_control->setValue(string("features.out"));

  mrs_real sample_rate = system->remoteControl("sndfile/osrate")->to<mrs_real>();
  mrs_natural block_size = system->remoteControl("sndfile/onSamples")->to<mrs_natural>();
  mrs_real block_duration = block_size / sample_rate;

  MarControlPtr output = system->getControl("mrs_realvec/processedData");
  MarControlPtr confidence_ctl = system->remoteControl("onsets/confidence");
  assert(!output.isInvalid());
  assert(!confidence_ctl.isInvalid());

  MarSystem *rms_sys = system->remoteSystem("rms");
  assert(rms_sys);
  MarControlPtr rms_out = rms_sys->getControl("mrs_real/value");
  assert(!rms_out.isInvalid());

  std::vector<onset> onsets;
  int block = 0;
  const int block_offset = 5;

  while(!done_control->to<bool>())
  {
    system->tick();

    const realvec & data = output->to<realvec>();
    assert(data.getSize() == 2);

    mrs_real confidence = confidence_ctl->to<mrs_real>();

    if (!(data(0) > 0.0) || confidence < 10.0 / 100.0)
    {
      ++block;
      continue;
    }

    mrs_real centroid = data(1);

    double rms = rms_out->to<mrs_real>();

    onset o;

    o.time = (block - block_offset + 0.5) * block_duration;

    if (centroid < 0.04)
      o.type = 0;
    else if (centroid < 0.3)
      o.type = 1;
    else
      o.type = 2;

    o.strength = rms;

    onsets.push_back(o);

    ++block;
  }

  string separator(",");

  ofstream out_file(output_filename);
  if (!out_file.is_open())
  {
    cerr << "Failed to open output file for writing: " << output_filename << endl;
    return 1;
  }

  for (int i = 0; i < onsets.size(); ++i)
  {
    out_file << onsets[i].time << separator
             << onsets[i].type << separator
             << onsets[i].strength
             << endl;
  }

  cout << "Done." << endl;

  return 0;
}
void
pitchextract_caricature(mrs_string sfName, mrs_natural winSize, mrs_natural hopSize,
                        mrs_real lowPitch, mrs_real highPitch, mrs_real threshold,
                        mrs_bool playPitches, mrs_string ofName)
{
  (void) winSize;
  (void) threshold;
  MRSDIAG("pitchextract.cpp - pitchextract");

  MarSystemManager mng;
  // Build pitch contour extraction network
  MarSystem* pitchContour     = mng.create("Series", "pitchContour");

  MarSystem* pitchExtractor = mng.create("Series", "pitchExtractor");
  pitchExtractor->addMarSystem(mng.create("SoundFileSource", "src"));
  pitchExtractor->addMarSystem(mng.create("Stereo2Mono", "s2m"));
  if (mode == "praat") {
    pitchExtractor->addMarSystem(mng.create("PitchPraat", "pitchPraat"));
  } else {
    pitchExtractor->addMarSystem(mng.create("PitchSACF", "pitchSACF"));
  }

  pitchExtractor->updControl("SoundFileSource/src/mrs_string/filename", sfName);

  mrs_natural fileSize;
  fileSize= pitchExtractor->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
  mrs_natural contourSize = fileSize / hopSize;

  // Accumulate the extracted pitches and confidences in a single vector
  // of size contourSize
  MarSystem* pitchAccumulator = mng.create("Accumulator", "pitchAccumulator");
  pitchAccumulator->addMarSystem(pitchExtractor);
  pitchAccumulator->updControl("mrs_natural/nTimes", contourSize);
  pitchContour->addMarSystem(pitchAccumulator);

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

  mrs_real srate = pitchExtractor->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();

  ofstream ofs1;
  ofs1.open("p.mpl");
  ofs1 << *pitchExtractor << endl;
  ofs1.close();



  // Using explicit loop
  mrs_natural len = contourSize;
  mrs_realvec pitches(len);
  mrs_realvec confidences(len);
  mrs_realvec chords(len);
  mrs_realvec booms(len);
  mrs_realvec chicks(len);


  vector<mrs_string> chord_names;


  mrs_realvec pitchres;
  mrs_realvec peak_in;

  ofstream ofs;
  ofs.open(ofName.c_str());


  for (int i=0; i < contourSize; ++i)
  {
    pitchExtractor->tick();
    pitchres = pitchExtractor->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
    confidences(i) = pitchres(0);
    pitches(i) = samples2hertz(pitchres(1), srate);
    // cout << "Pitch = " << pitches(i) << "- (conf) - " << confidences(i) << endl;

    float scaled_pitch = pitches(i);
    if (frsopt == "bark") {
      scaled_pitch = hertz2bark(pitches(i));
    }
    if (frsopt == "mel") {
      scaled_pitch = hertz2mel(pitches(i),1);
    }
    if (frsopt == "midi") {
      scaled_pitch = hertz2pitch(pitches(i));
    }


    if (pitches(i) <= pitch2hertz(lowPitch))
    {
      // confidences(i) = 0.0;
      pitches(i) += 12;
    }
    if (pitches(i) >= pitch2hertz(highPitch))
    {
      pitches(i) -= 12;
      // confidences(i) = 0.0;
    }



    ofs << scaled_pitch << endl;


    /*
    peak_in = pitchExtractor->getctrl("PitchPraat/pitchPraat/AutoCorrelation/acr/mrs_realvec/processedData")->to<mrs_realvec>();
    mrs_natural pos = pitchExtractor->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>();
    MATLAB_PUT(peak_in, "peak_in");
    MATLAB_PUT(pos, "pos");
    MATLAB_EVAL("plot(peak_in); title(num2str(pos));");
    getchar();
    */
  }

  // Normalize confidence to 0-1 range
  confidences.normMaxMin();

  ofs.close();


  // Optionally plot the pitches
#ifdef MARSYAS_MATLAB
  mrs_realvec foo(len);
  MATLAB_PUT(confidences, "confidences");
  MATLAB_PUT(pitches, "pitches");
  MATLAB_EVAL("plot(confidences)");
  cerr << "Enter any character to continue" << endl;
  getchar();
  MATLAB_EVAL("a = pitches .* pitches;");
  MATLAB_GET("a", foo);
  MATLAB_EVAL("plot(a)");
  getchar();
  MATLAB_EVAL("plot(pitches)");
  cerr << "Enter any character to continue" << endl;
  getchar();
  MATLAB_CLOSE();
#endif


  // extract chords

  MarSystem* chordExtract = mng.create("Series/chordExtract");
  chordExtract->addMarSystem(mng.create("SoundFileSource/src"));
  chordExtract->addMarSystem(mng.create("Windowing/win"));
  chordExtract->addMarSystem(mng.create("Spectrum/spk"));
  chordExtract->addMarSystem(mng.create("PowerSpectrum/pspk"));
  chordExtract->addMarSystem(mng.create("Spectrum2Chroma/s2c"));
  chordExtract->addMarSystem(mng.create("Memory/mem"));
  chordExtract->addMarSystem(mng.create("Mean/mean"));
  chordExtract->addMarSystem(mng.create("Krumhansl_key_finder/kkf"));

  chordExtract->updControl("mrs_natural/inSamples", hopSize);
  chordExtract->updControl("Memory/mem/mrs_natural/memSize", 200);

  chordExtract->updControl("SoundFileSource/src/mrs_string/filename", sfName);

  for (int i=0; i < contourSize; ++i)
  {
    chordExtract->tick();
    chords(i) = chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_natural/key")->to<mrs_natural>();
    cout << "chords(i) = " << chords(i) << endl;

    chord_names.push_back(chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_string/key_name")->to<mrs_string>());

  }


  // extra boom-chick pattern


  // high and low bandpass filters
  MarSystem *filters = mng.create("Fanout", "filters");
  realvec al(5),bl(5);

  al(0) = 1.0;
  al(1) = -3.9680;
  al(2) = 5.9062;
  al(3) = -3.9084;
  al(4) = 0.9702;

  bl(0) = 0.0001125;
  bl(1) = 0.0;
  bl(2) = -0.0002250;
  bl(3) = 0.0;
  bl(4) = 0.0001125;

  MarSystem *lfilter = mng.create("Series", "lfilter");
  lfilter->addMarSystem(mng.create("Filter", "llfilter"));
  lfilter->updControl("Filter/llfilter/mrs_realvec/ncoeffs", bl);
  lfilter->updControl("Filter/llfilter/mrs_realvec/dcoeffs", al);

  MarSystem *lowpkr = mng.create("PeakerAdaptive","lowpkr");
  lowpkr->updControl("mrs_real/peakSpacing", 0.3);
  lowpkr->updControl("mrs_real/peakStrength", 0.95);
  lowpkr->updControl("mrs_natural/peakStart", 0);
  lowpkr->updControl("mrs_natural/peakEnd", hopSize);

  lowpkr->updControl("mrs_real/peakGain", 1.0);
  lowpkr->updControl("mrs_natural/peakStrengthReset", 4);
  lowpkr->updControl("mrs_real/peakDecay", 0.99);
  lfilter->addMarSystem(lowpkr);



  realvec ah(5),bh(5);
  ah(0) = 1.0;
  ah(1) = -3.5797;
  ah(2) = 4.9370;
  ah(3) = -3.1066;
  ah(4) = 0.7542;

  bh(0) = 0.0087;
  bh(1) = 0.0;
  bh(2) = -0.0174;
  bh(3) = 0;
  bh(4) = 0.0087;

  MarSystem *hfilter = mng.create("Series", "hfilter");
  hfilter->addMarSystem(mng.create("Filter", "hhfilter"));
  hfilter->addMarSystem(mng.create("Gain", "gain"));
  hfilter->updControl("Filter/hhfilter/mrs_realvec/ncoeffs", bh);
  hfilter->updControl("Filter/hhfilter/mrs_realvec/dcoeffs", ah);

  MarSystem* hipkr = mng.create("PeakerAdaptive", "hipkr");
  hipkr->updControl("mrs_real/peakSpacing", 0.5);
  hipkr->updControl("mrs_real/peakStrength", 0.90);
  hipkr->updControl("mrs_natural/peakStart", 0);
  hipkr->updControl("mrs_natural/peakEnd", hopSize);
  hipkr->updControl("mrs_real/peakGain", 1.0);
  hipkr->updControl("mrs_natural/peakStrengthReset", 4);
  hipkr->updControl("mrs_real/peakDecay", 0.99);
  hfilter->addMarSystem(hipkr);


  filters->addMarSystem(lfilter);
  filters->addMarSystem(hfilter);

  MarSystem* boomchickExtract = mng.create("Series/boomchickExtract");
  boomchickExtract->addMarSystem(mng.create("SoundFileSource/src"));
  boomchickExtract->addMarSystem(filters);
  boomchickExtract->addMarSystem(mng.create("SoundFileSink/dest"));

  boomchickExtract->updControl("mrs_natural/inSamples", hopSize);
  boomchickExtract->updControl("SoundFileSource/src/mrs_string/filename", sfName);
  boomchickExtract->updControl("SoundFileSink/dest/mrs_string/filename", "boom_chick.wav");

  for (int i=0; i < contourSize; ++i)
  {
    boomchickExtract->tick();
    booms(i) = boomchickExtract->getControl("Fanout/filters/Series/lfilter/PeakerAdaptive/lowpkr/mrs_bool/peakFound")->to<mrs_bool>();
    chicks(i) = boomchickExtract->getControl("Fanout/filters/Series/hfilter/PeakerAdaptive/hipkr/mrs_bool/peakFound")->to<mrs_bool>();

  }



  // Playback the pitches
  if (playPitches)
  {
    MarSystem* playback = mng.create("Series/playback");
    MarSystem* mix = mng.create("Fanout/mix");

    MarSystem* ch0 = mng.create("Series/ch0");
    ch0->addMarSystem(mng.create("SineSource/ss"));
    ch0->addMarSystem(mng.create("Gain/sinegain"));

    MarSystem* ch1 = mng.create("Series/ch1");
    ch1->addMarSystem(mng.create("SoundFileSource/src"));
    ch1->addMarSystem(mng.create("Gain/soundgain"));

    MarSystem* ch2 = mng.create("Series/ch2");
    ch2->addMarSystem(mng.create("SineSource/ss"));
    ch2->addMarSystem(mng.create("Gain/sinegain"));

    MarSystem* ch3 = mng.create("Series/ch3");
    ch3->addMarSystem(mng.create("SoundFileSource/bdsrc"));
    ch3->addMarSystem(mng.create("Gain/bdsrcgain"));


    MarSystem* ch4 = mng.create("Series/ch4");
    ch4->addMarSystem(mng.create("SoundFileSource/sdsrc"));
    ch4->addMarSystem(mng.create("Gain/sdsrcgain"));

    mix->addMarSystem(ch0);
    mix->addMarSystem(ch1);
    mix->addMarSystem(ch2);
    // mix->addMarSystem(ch3);
    // mix->addMarSystem(ch4);


    playback->addMarSystem(mix);
    playback->addMarSystem(mng.create("Sum/sum"));
    playback->addMarSystem(mng.create("Gain", "g"));
    playback->addMarSystem(mng.create("AudioSink", "dest"));
    playback->addMarSystem(mng.create("SoundFileSink/dest"));

    playback->updControl("mrs_natural/inSamples", hopSize);
    playback->updControl("mrs_real/israte", srate);
    playback->updControl("AudioSink/dest/mrs_bool/initAudio", true);
    playback->updControl("mrs_real/israte", pitchContour->getctrl("mrs_real/osrate"));

    playback->updControl("SoundFileSink/dest/mrs_string/filename", "caricature.wav");

    playback->updControl("Fanout/mix/Series/ch1/SoundFileSource/src/mrs_string/filename",
                         sfName);

    playback->updControl("Fanout/mix/Series/ch3/SoundFileSource/bdsrc/mrs_string/filename",
                         "bd22k.wav");

    playback->updControl("Fanout/mix/Series/ch4/SoundFileSource/sdsrc/mrs_string/filename",
                         "sd22k.wav");


    for (int i=0; i < len; ++i)
    {
      playback->updControl("Fanout/mix/Series/ch0/SineSource/ss/mrs_real/frequency",
                           pitches(i));
      cout << "pitches(i) = " << pitches(i) << endl;
      cout << "chords(i) = " << chords(i) << endl;
      if (chords(i) > 12)
        chords(i) -= 12;


      cout << "chords(i) = " << chords(i) << endl;
      playback->updControl("Fanout/mix/Series/ch2/SineSource/ss/mrs_real/frequency",
                           pitch2hertz(57 + chords(i)));
      cout << "hertz = = " << pitch2hertz(57 + chords(i)) << endl;

      playback->updControl("Fanout/mix/Series/ch0/Gain/sinegain/mrs_real/gain", 0.5 );
      playback->updControl("Fanout/mix/Series/ch1/Gain/soundgain/mrs_real/gain", 0.15);
      playback->updControl("Fanout/mix/Series/ch2/Gain/sinegain/mrs_real/gain", 0.5);

      playback->tick();

#ifdef MARSYAS_OSC
#define ADDRESS "127.0.0.1"
#define PORT 7000
#define OUTPUT_BUFFER_SIZE 1024
      UdpTransmitSocket transmitSocket( IpEndpointName( ADDRESS, PORT ) );
      char buffer[OUTPUT_BUFFER_SIZE];
#endif

      if (booms(i))
      {
        playback->updControl("Fanout/mix/Series/ch3/SoundFileSource/bdsrc/mrs_natural/pos",0);

#ifdef MARSYAS_OSC
        osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE );

        p << osc::BeginBundleImmediate
          << osc::BeginMessage( "/notomoton" )
          << 1 << 127 << osc::EndMessage;
        transmitSocket.Send(p.Data(), p.Size());

#endif
      }

      if (chicks(i))
        playback->updControl("Fanout/mix/Series/ch4/SoundFileSource/sdsrc/mrs_natural/pos",0);



      // cout << chord_names[i] << endl;


    }
    delete playback;
  }


  delete pitchExtractor;

}
int
pitchextract_key(mrs_string sfName, mrs_natural winSize, mrs_natural hopSize,
                 mrs_real lowPitch, mrs_real highPitch, mrs_real threshold,
                 mrs_bool playPitches, mrs_string ofName)
{
  (void) lowPitch;
  (void) highPitch;
  (void) threshold;
  (void) playPitches;
  (void) ofName;
  MRSDIAG("pitchextract.cpp - pitchextract");

  MarSystemManager mng;

  // Build pitch contour extraction network
  //MarSystem* pitchContour     = mng.create("Series", "pitchContour");

  MarSystem* pitchExtractor = mng.create("Series", "pitchExtractor");
  pitchExtractor->addMarSystem(mng.create("SoundFileSource", "src"));
  pitchExtractor->addMarSystem(mng.create("Stereo2Mono", "s2m"));
  if (mode == "praat") {
    pitchExtractor->addMarSystem(mng.create("PitchPraat", "pitchPraat"));
  } else {
    pitchExtractor->addMarSystem(mng.create("PitchSACF", "pitchSACF"));
  }

  pitchExtractor->updControl("SoundFileSource/src/mrs_string/filename", sfName);

  mrs_natural fileSize;
  fileSize= pitchExtractor->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
  mrs_natural contourSize = fileSize / hopSize;

  cout << "\ncontourSize = " << contourSize << endl;

  mrs_natural len = contourSize;

  vector<mrs_string> chord_names;
  mrs_realvec chords(len);

  // extract chords

  MarSystem* chordExtract = mng.create("Series/chordExtract");
  chordExtract->addMarSystem(mng.create("SoundFileSource/src"));
  chordExtract->addMarSystem(mng.create("ShiftInput/si"));
  chordExtract->addMarSystem(mng.create("Windowing/win"));
  chordExtract->addMarSystem(mng.create("Spectrum/spk"));
  chordExtract->addMarSystem(mng.create("PowerSpectrum/pspk"));
  chordExtract->addMarSystem(mng.create("Spectrum2Chroma/s2c"));
  chordExtract->addMarSystem(mng.create("Memory/mem"));
  chordExtract->addMarSystem(mng.create("Mean/mean"));
  chordExtract->addMarSystem(mng.create("Krumhansl_key_finder/kkf"));

  chordExtract->updControl("mrs_natural/inSamples", hopSize);
  chordExtract->updControl("ShiftInput/si/mrs_natural/winSize", winSize);
  chordExtract->updControl("Memory/mem/mrs_natural/memSize", 120);
  chordExtract->updControl("SoundFileSource/src/mrs_string/filename", sfName);

  for (int i=0; i < contourSize; ++i)
  {
    chordExtract->tick();
    chords(i) = chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_natural/key")->to<mrs_natural>();
    // cout << "chords(i) = " << chords(i) << endl;

    chord_names.push_back(chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_string/key_name")->to<mrs_string>());

  }


  delete pitchExtractor;
  delete chordExtract;
  // return chords.median();

  return chords(contourSize-1-40);
}