Esempio n. 1
0
static MarSystem *load_network( const string system_filename )
{

  auto string_ends_with =
      []( const string & str, const string & ending ) -> bool
  {
    if (str.length() < ending.length())
      return false;
    return str.compare( str.length() - ending.length(), ending.length(), ending ) == 0;
  };

  MarSystem* system = 0;

  if (string_ends_with(system_filename, ".mrs"))
  {
#ifdef MARSYAS_HAS_SCRIPT
    system = system_from_script_file(system_filename);
#endif
  }
  else if (string_ends_with(system_filename, ".mpl"))
  {
    ifstream system_istream(system_filename);
    MarSystemManager mng;
    system = mng.getMarSystem(system_istream);
  }
  else if (string_ends_with(system_filename, ".json"))
  {
#ifdef MARSYAS_HAS_JSON
    system = system_from_json_file(system_filename);
#endif
  }

  return system;
}
Esempio n. 2
0
int run( const string system_filename, const CommandLineOptions & opt )
{
    int ticks = 0;
    if (opt.has("count"))
    {
      ticks = opt.value<int>("count");
      if (ticks < 1)
      {
        cerr << "Invalid value for option 'count' (must be > 0)." << endl;
        return 1;
      }
    }

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

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

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

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

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

    return 0;
}
Esempio n. 3
0
void
predict(mrs_string mode)
{

	MarSystemManager mng;

	cout << "Predicting using " << trainedclassifier_ << endl;

	ifstream pluginStream(trainedclassifier_.c_str());
    MRS_WARNINGS_OFF;
	MarSystem* net = mng.getMarSystem(pluginStream);
	MRS_WARNINGS_ON;

  if (!twekafname_Set()) return;

   vector<string> classNames;
   string s = net->getctrl("WekaSource/wsrc/mrs_string/classNames")->to<mrs_string>();
   char *str = (char *)s.c_str();
   char * pch;
   pch = strtok (str,",");
   classNames.push_back(pch);
   while (pch != NULL) {
 	pch = strtok (NULL, ",");
 	if (pch != NULL)
 	  classNames.push_back(pch);
   }





  ////////////////////////////////////////////////////////////
  //
  // Predict the classes of the test data
  //
  net->updControl("WekaSource/wsrc/mrs_string/filename", twekafname_);
  net->updControl("Classifier/cl/mrs_string/mode", "predict");
  ////////////////////////////////////////////////////////////
  //
  // Tick over the test WekaSource until all lines in the
  // test file have been read.
  //


  ofstream prout;
  prout.open(predictcollectionfname_.c_str());

  ofstream prtout;
  prtout.open(predicttimeline_.c_str());



  realvec data;
  int end=0;
  int start=0;

  mrs_string prev_name = "";
  mrs_string name;

  mrs_real srate;




  while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>()) {
   	net->tick();
   	data = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
	srate = net->getctrl("WekaSource/wsrc/mrs_real/currentSrate")->to<mrs_real>();


	if (mode == "default")
	{
		cout << net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>() << "\t";
		cout << classNames[(int)data(0,0)] << endl;
		prout << net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>() << "\t";
		prout << classNames[(int)data(0,0)] << endl;
	}
	else if (mode == "timeline")
	{

	  name = classNames[(int)data(0,0)];

	  if (name != prev_name)
	  {
		if ((end * (1.0/srate)-start*(1.0 / srate) > minspan_))
		{
		  if (predicttimeline_ == EMPTYSTRING)
		  {
			cout << start*(1.0 / srate) << "\t" << end*(1.0 / srate) << "\t";
			cout << prev_name << endl;
		  }
		  else
		  {
			prtout << start*(1.0 / srate) << "\t" << end*(1.0 / srate) << "\t";
			prtout << prev_name << endl;
		  }
		  
		}
		start = end;
		
	  }
	  else
	  {
		
	  }
	  
	  prev_name = name;
	  

	}
	else
		cout << "Unsupported mode" << endl;

	//	cout << data(0,0) << endl;
	end++;
  }


  prout.close();

  // cout << "DONE" << endl;

  // sness - hmm, I really should be able to delete net, but I get a
  // coredump when I do.  Maybe I need to destroy something else first?
  //  delete net;


}
Esempio n. 4
0
void
MarGrid::predict()
{
  MarSystemManager mng;

  // read trained som network from file som.mpl and normalization network norm.mpl
  ifstream iss;
  iss.open("som.mpl");
  som_ = mng.getMarSystem(iss);

  ifstream niss;
  niss.open("norm.mpl");
  norm_ = mng.getMarSystem(niss);

  resetPredict();
  cout << "Starting prediction" << endl;
  som_->updControl("mrs_string/mode", "predict");

  Collection l1;
  l1.read(predictFname.toStdString());
  cout << "Read collection" << endl;

  total_->updControl("mrs_natural/pos", 0);
  total_->updControl("mrs_natural/advance", 0);
  total_->updControl("mrs_string/filename", predictFname.toStdString());

  som_->updControl("mrs_natural/inSamples", 1);

  realvec predict_res(som_->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
                      som_->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
  norm_->updControl("mrs_natural/inSamples", 1);


  realvec som_in;
  realvec som_res;
  realvec som_res1;

  realvec norm_som_res;

  mrs_natural inObs = total_->getctrl("mrs_natural/inObservations")->to<mrs_natural>();
  mrs_natural inSms = total_->getctrl("mrs_natural/inSamples")->to<mrs_natural>();

  mrs_natural onObs = total_->getctrl("mrs_natural/onObservations")->to<mrs_natural>();
  mrs_natural onSms = total_->getctrl("mrs_natural/onSamples")->to<mrs_natural>();

  som_in.create(inObs, inSms);
  som_res.create(onObs, onSms);

  som_res1.create(onObs+2, onSms);

  norm_som_res.create(onObs+2, onSms);

  for (unsigned int index = 0; index < l1.size(); index++)
  {
    total_->updControl("mrs_natural/label", index);
    total_->updControl("mrs_bool/memReset", true);
    total_->updControl("mrs_natural/cindex", index);


    total_->process(som_in, som_res);
    string current = total_->getctrl("mrs_string/currentlyPlaying")->to<mrs_string>();


    cout << "CURRENT = " << current << endl;

    for (int o=0; o < onObs; o++)
      som_res1(o, 0) = som_res(o, 0);

    norm_->process(som_res1, norm_som_res);
    som_->process(norm_som_res, predict_res);



    grid_x = predict_res(0);
    grid_y = predict_res(1);
    addFile(grid_x,grid_y, current);
    repaint();
    total_->updControl("mrs_natural/advance", 1);
  }



  cout << "end_prediction" << endl;


}
Esempio n. 5
0
void classifyFile(vector<string> soundfiles, string pluginName)
{

  MRSDIAG("classifyFile.cpp - classifyFile");
  // read the plugin from the file
  ifstream pluginStream(pluginName.c_str());



  MarSystemManager mngr;
  MarSystem* msys = mngr.getMarSystem(pluginStream);



  if (msys == 0)
  {
    cout << "Manager does not support system " << endl;
    exit(1);
  }
  mrs_real srate;

  // run audio through the plugin for each file in the collection
  // assume there is a SoundFileSource in the plugin
  // and an audio sink
  string sfName;

  // output the plugin

  // cout << (*msys) << endl;



  vector<string>::iterator sfi;
  mrs_natural count = 0;





  for (sfi = soundfiles.begin(); sfi != soundfiles.end(); ++sfi)
  {
    count++;


    // udpate source filename
    sfName = *sfi;
    msys->updctrl("mrs_string/filename", sfName);



    mrs_natural size = msys->getctrl("SilenceRemove/srm/SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
    mrs_natural inSamples = msys->getctrl("SilenceRemove/srm/SoundFileSource/src/mrs_natural/inSamples")->to<mrs_natural>();

    mrs_natural memSize = (size / inSamples);
    memSize /= 2;

    // hardwire to approximately 15 seconds
    memSize = 600;


    msys->updctrl("Confidence/confidence/mrs_natural/memSize", memSize);

    if (verboseopt)
      cout << (*msys) << endl;



    srate = msys->getctrl("mrs_real/israte")->to<mrs_real>();


    mrs_natural samplesPlayed = 0;
    mrs_natural onSamples = msys->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
    mrs_natural wc = 0;
    int i;




    if (onetick)
    {
      msys->tick();
    }
    else
    {
      // if (verboseopt)
      cout << sfName << " ";
      //

      // while (msys->getctrl("mrs_bool/hasData")->to<mrs_bool>())
      for (i=0; i < memSize-1; i++)
      {
        if (msys->getctrl("mrs_bool/hasData")->to<mrs_bool>() == false)
        {
          break;
        }
        msys->tick();
        wc++;
        samplesPlayed += onSamples;
      }

      if (i >= 10)
      {
        msys->updctrl("Confidence/confidence/mrs_bool/print", true);
        msys->updctrl("Confidence/confidence/mrs_bool/forcePrint", true);
        msys->tick();
      }
      else
        cout << "silence 100.0" << endl;


    }

    msys->updctrl("Confidence/confidence/mrs_bool/print",false);
    msys->updctrl("Confidence/confidence/mrs_bool/forcePrint", false);
    msys->updctrl("Memory/memory/mrs_bool/reset", true);
    msys->updctrl("SilenceRemove/srm/SoundFileSource/src/mrs_string/filename", "");

  }



  delete msys;
}