Exemplo n.º 1
0
// Play soundfile given by sfName, msys contains the playback
// network of MarSystem objects
void sfplayFile(MarSystem& msys, mrs_natural offset, mrs_natural duration,
                mrs_real start, mrs_real length, mrs_real gain, mrs_real repetitions, string sfName )
{
  msys.updctrl("SoundFileSource/src/mrs_string/filename", sfName);
  mrs_natural nChannels = msys.getctrl("SoundFileSource/src/mrs_natural/nChannels")->to<mrs_natural>();
  mrs_real srate = msys.getctrl("SoundFileSource/src/mrs_real/israte")->to<mrs_real>();

  // playback offset & duration
  offset = (mrs_natural) (start * srate * nChannels);
  duration = (mrs_natural) (length * srate * nChannels);

  // udpate controls
  msys.updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);
  msys.updctrl("Gain/gt/mrs_real/gain", gain);
  msys.updctrl("SoundFileSource/src/mrs_natural/pos", offset);

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

  while (msys.getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
  {
    msys.tick();
    wc ++;
    samplesPlayed += onSamples;
  }
  cerr << "Played " << wc << " slices of " << onSamples << " samples" << endl;
}
void newMidiInfo()
{
    /*
	  This function describes using the MidiInput object and MarControlPointers 
	  to read midi controls in a loop. The disadvantage of this approach is that
	  MidiINput will only return the last midi value between buffers. Change the 
	  buffer size you would like more precision and throughput for midi messages. 
    */
    MarSystemManager mng;
    MarSystem* series = mng.create("Series", "series");
    series->addMarSystem(mng.create("AudioSource","src"));
    series->addMarSystem(mng.create("MidiInput","midiin"));

    series->updctrl("mrs_real/israte", 44100.0);
    series->updctrl("mrs_real/osrate", 44100.0);
    // to change how often marsyas grabs messages change the buffersize
    series->updctrl("AudioSource/src/mrs_natural/bufferSize", 64);
    series->updctrl("AudioSource/src/mrs_bool/initAudio",true);
    series->updctrl("MidiInput/midiin/mrs_bool/initmidi",true);

    MarControlPtr b1 = series->getctrl("MidiInput/midiin/mrs_natural/byte1");
    MarControlPtr b2 = series->getctrl("MidiInput/midiin/mrs_natural/byte2");
    MarControlPtr b3 = series->getctrl("MidiInput/midiin/mrs_natural/byte3");

    while(1) 
    {  
        const  mrs_natural& byte1 = b1->to<mrs_natural>(); 
        const  mrs_natural& byte2 = b2->to<mrs_natural>();   
        const  mrs_natural& byte3 = b3->to<mrs_natural>(); 

        std::cout <<  "Byte 1: " << byte1 << "    Byte 2: " << byte2 << "    Byte 3: " << byte3 << endl;

        series->tick();
    }
}
// thread code
void* run(void * arg)
{
  
  thread_data* data = (thread_data*) arg;

  MarSystemManager mng;
  NetworkTCPSource* src = new NetworkTCPSource("src");
  
  MarSystem* featureNetwork = mng.create("Series", "featureNetwork");
  featureNetwork->addMarSystem(src);
  featureNetwork->addMarSystem(mng.create("AudioSink", "sink"));
  // featureNetwork->addMarSystem(mng.create("PlotSink", "psink"));
  
  featureNetwork->updctrl("NetworkTCPSource/src/mrs_natural/dataPort", data->dataPort);
  featureNetwork->updctrl("NetworkTCPSource/src/mrs_natural/controlsPort", data->controlsPort);

  featureNetwork->linkctrl("mrs_bool/hasData", "NetworkTCPSource/src/mrs_bool/hasData");
  
  src->refresh();
  
  mrs_natural wc = 0;
  
  mrs_real* controls = 0;
  
  mrs_natural onSamples = featureNetwork->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
  
  
  // start the network 
  while ( featureNetwork->getctrl("mrs_bool/hasData")->to<mrs_bool>() ) {

	try {
		
		controls = featureNetwork->recvControls();	
      		if ( controls != 0 ) {
			
			// get some reference controls, so if they have changed we update them
			mrs_natural inSamples = featureNetwork->getctrl("mrs_natural/inSamples")->to<mrs_natural>();
			mrs_natural inObservations = featureNetwork->getctrl("mrs_natural/inObservations")->to<mrs_natural>();
			mrs_real israte = featureNetwork->getctrl("mrs_real/israte")->to<mrs_real>();
			
			if ( (mrs_natural)controls[1] != inSamples || (mrs_natural)controls[2] != inObservations 
					|| controls[3] != israte ) {
			
				featureNetwork->updctrl("mrs_natural/inSamples", (mrs_natural)controls[1]);
				featureNetwork->updctrl("mrs_natural/inObservations", (mrs_natural)controls[2]);
				featureNetwork->updctrl("mrs_real/israte", controls[3]);
			}
      		}
		
		featureNetwork->tick(); // everything happens here 
	} catch ( SocketException e ) {
  		cerr << "Played " << wc << " slices of " << onSamples << " samples" << endl;
		pthread_exit(NULL);
	}
 	wc++;
   }
  cout << "played - " << wc << " slices of " << onSamples << " samples" << endl;
 
  pthread_exit( NULL );
}
Exemplo n.º 4
0
bool MslCommandRun::execute()
{
  MarSystem* msys;

  if ( workingSet.find(name) != workingSet.end() ) {
    msys = (MarSystem *)workingSet[name];
  } else {
    cout << "Cannot find MarSystem: " << name << endl;
    return false;
  }

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

  mrs_real* controls = 0;

  while (true) {

    try {

      controls = msys->recvControls();

      if ( controls != 0 ) {

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

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

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

      msys->tick();
    }
    catch( SocketException e ) {
      cout << "Played " << wc << " slices of " << onSamples << " samples" << endl;
      exit(1);
    }

    wc ++;

    if ( !msys->getctrl("mrs_bool/hasData")->isTrue() ) {
      break;
    }
    samplesPlayed += onSamples;

  } // while

  cout << "Played " << wc << " slices of " << onSamples << " samples" << endl;
  return true;
}
Exemplo n.º 5
0
MarsyasBExtractRolloff::MarsyasBExtractRolloff(float inputSampleRate) :
  Plugin(inputSampleRate),
  m_stepSize(0),
  m_previousSample(0.f),
  m_network(0)
{
  MarSystemManager mng;
  // Overall extraction and classification network
  m_network = mng.create("Series", "mainNetwork");

  // Build the overall feature calculation network
  MarSystem *featureNetwork = mng.create("Series", "featureNetwork");

  // Add a realvec as the source
  featureNetwork->addMarSystem(mng.create("RealvecSource", "src"));

  // Convert the data to mono
  featureNetwork->addMarSystem(mng.create("Stereo2Mono", "m2s"));

  // Setup the feature extractor
  MarSystem* featExtractor = mng.create("TimbreFeatures", "featExtractor");
  featExtractor->updctrl("mrs_string/enableSPChild", "Rolloff/rlf");
  featureNetwork->addMarSystem(featExtractor);

  // Add the featureNetwork to the main network
  m_network->addMarSystem(featureNetwork);
}
void
basic_delay(string infile, string outfile)
{
	MarSystem* pnet = mng.create("Series", "pnet");
	addSource( pnet, infile );
	pnet->addMarSystem(mng.create("Delay", "delay"));
	pnet->updctrl("Delay/delay/mrs_natural/delaySamples", 16);
	pnet->updctrl("Delay/delay/mrs_real/feedback", (mrs_real) 0.5);
	addDest( pnet, outfile);

	while (pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
	{
		pnet->tick();
	}
	delete pnet;
}
// take advantage of MarSystemManager 
void 
tempotest_sfplay(string sfName)
{
    cout << "Playing " << sfName << endl; 

    MarSystemManager mng;

    // Create a series Composite 
    MarSystem* series = mng.create("Series", "series");
    series->addMarSystem(mng.create("SoundFileSource", "src"));
    series->addMarSystem(mng.create("AudioSink", "dest"));

    // only update controls from Composite level 
    series->updctrl("mrs_natural/inSamples", 128);
    series->updctrl("SoundFileSource/src/mrs_string/filename", sfName);

    while (series->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
        series->tick();

    delete series;
}
// TODO: move
void
basic_vibrato(string infile, string outfile)
{
	MarSystem* pnet = mng.create("Series", "pnet");
	addSource( pnet, infile );
	pnet->addMarSystem(mng.create("Vibrato", "vib"));
	addDest( pnet, outfile);

	pnet->updctrl("Vibrato/vib/mrs_real/mod_freq", 10.0);
	while (pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
	{
		pnet->tick();
	}
	delete pnet;
}
void
basic_windowing(string infile, string outfile)
{
	MarSystem* pnet = mng.create("Series", "pnet");
	addSource( pnet, infile );
	pnet->addMarSystem(mng.create("Windowing", "win"));
	pnet->updctrl("Windowing/win/mrs_string/type", "Hanning");
	addDest( pnet, outfile);

	while (pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
	{
		pnet->tick();
	}
	delete pnet;
}
void
basic_shifter(string infile, string outfile)
{
	MarSystem* pnet = mng.create("Series", "pnet");
	addSource( pnet, infile );
	pnet->addMarSystem(mng.create("Shifter", "shift"));
	pnet->updctrl("Shifter/shift/mrs_natural/shift", 16);
	addDest( pnet, outfile);

	while (pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>())
	{
		pnet->tick();
	}
	delete pnet;
}
Exemplo n.º 11
0
	void centroidToTxt(string sfName1) {
		cout << "Toy with centroid " << sfName1 << endl;
		MarSystemManager mng;

		MarSystem* net = mng.create("Series/net");
		net->addMarSystem(mng.create("SoundFileSource/src"));
		net->addMarSystem(mng.create("Windowing/ham"));
		net->addMarSystem(mng.create("Spectrum/spk"));
		net->addMarSystem(mng.create("PowerSpectrum/pspk"));
		net->addMarSystem(mng.create("Centroid/cntrd"));
		net->addMarSystem(mng.create("Memory/mem"));
		net->addMarSystem(mng.create("Mean/mean"));
		net->linkctrl("mrs_string/filename", "SoundFileSource/src/mrs_string/filename");
		net->updctrl("mrs_string/filename", sfName1);

		mrs_real val = 0.0;

		ofstream ofs;
		ofs.open("centroid.mpl");
		ofs << *net << endl;
		ofs.close();

		ofstream text;
		text.open("centroid.txt");


		while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
		{
			net->tick();
			const mrs_realvec& src_data =
			net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
			val = src_data(0,0);
			text << val << endl;
			//cout << val << endl;
		}
		text.close();
	}
Exemplo n.º 12
0
int main(int argc, const char** argv)
{
	if (argc < 3) {
		cout << "Usage: " << argv[0] << " soundfile outputfile" << endl;
		return 1;
	}
	
	MarSystemManager mng;
	
//------------------ Feature Network ------------------------------

	// Decode the file, downmix to mono, and downsample
	MarSystem *fnet = mng.create("Series", "fnet");
	fnet->addMarSystem(mng.create("SoundFileSource", "src"));
	fnet->addMarSystem(mng.create("DownSampler", "ds"));
	fnet->addMarSystem(mng.create("Stereo2Mono", "s2m"));
	
	// Create the feature extractor
	fnet->addMarSystem(mng.create("TimbreFeatures", "tf"));
	fnet->updctrl("TimbreFeatures/tf/mrs_string/enableTDChild", "ZeroCrossings/zcrs");
	fnet->updctrl("TimbreFeatures/tf/mrs_string/enableSPChild", "MFCC/mfcc");
	fnet->updctrl("TimbreFeatures/tf/mrs_string/enableSPChild", "Centroid/cntrd");
	fnet->updctrl("TimbreFeatures/tf/mrs_string/enableSPChild", "Flux/flux");
	fnet->updctrl("TimbreFeatures/tf/mrs_string/enableSPChild", "Rolloff/rlf");
	
	// Add the texture statistics
	fnet->addMarSystem(mng.create("TextureStats", "tStats"));

//------------------- Set Parameters ------------------------------
	
	// Set the texture memory size to a 1-second window (22 analysis frames)
	fnet->updctrl("TextureStats/tStats/mrs_natural/memSize", 22);

	// Set the file name
	fnet->updctrl("SoundFileSource/src/mrs_string/filename", argv[1]);
	
	// Set the sample rate to 11250 Hz
	mrs_natural factor = round(fnet->getctrl("SoundFileSource/src/mrs_real/osrate")
		->to<mrs_real>()/11250.0);
	fnet->updctrl("DownSampler/ds/mrs_natural/factor", factor);
	
	// Set the window to 1024 samples at 11250 Hz
	// Should be able to set with simply TimbreFeatures/tf/mrs_natural/winSize,
	// but that doesn't seem to work
	fnet->updctrl("TimbreFeatures/tf/Series/timeDomain/ShiftInput/si/mrs_natural/winSize", 1024);
	fnet->updctrl("TimbreFeatures/tf/Series/spectralShape/ShiftInput/si/mrs_natural/winSize", 1024);
	fnet->updctrl("TimbreFeatures/tf/Series/lpcFeatures/ShiftInput/si/mrs_natural/winSize", 1024);
	
	// Find the length of the song
	mrs_natural slength = fnet->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
	
	// Find the number of samples resulting in a whole number of analysis windows by truncating
	mrs_natural numsamps = (mrs_natural)(((30*11250.0*factor)/512)*512);

	// Shift the start over so that the duration is in the middle
	mrs_natural start = (slength - numsamps)/2;

	fnet->updctrl("SoundFileSource/src/mrs_natural/start", start);
	fnet->updctrl("SoundFileSource/src/mrs_natural/duration", numsamps);

// ----------------- Accumulator ---------------------------------

	// Accumulate over the entire song
	MarSystem *acc = mng.create("Accumulator", "acc");
		
	// nTimes is measured in number of analysis windows
	acc->updctrl("mrs_natural/nTimes", (mrs_natural)((30*11250.0)/512));

//------------------ Song Statistics -----------------------------
	// Fanout and calculate mean and standard deviation
	MarSystem *sstats = mng.create("Fanout", "sstats");
	sstats->addMarSystem(mng.create("Mean", "smn"));
	sstats->addMarSystem(mng.create("StandardDeviation", "sstd"));

// ----------------- Top Level Network Wrapper -------------------
	
	// (src->downmix->downsample->features->texture stats)
	// --->accumulator->song stats->output
	MarSystem *tnet = mng.create("Series", "tnet");
	acc->addMarSystem(fnet);
	tnet->addMarSystem(acc);
	tnet->addMarSystem(sstats);
	
	// set the hop size to 512 (needs to be set for the top-level network)
	tnet->updctrl("mrs_natural/inSamples", factor*512);

	try {
		// Should only need to tick once
		tnet->tick();
	} catch (int i)
	{
		cout << "Error analyzing file" << endl;
		return 1;
	}

	realvec result = tnet->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();

	try {
		result.normMaxMin();
		result.write(argv[2]);
	} catch (int i)
	{
		cout << "Error writing vector file" << endl;
		return 1;
	}
	
	// Success
	return 0;
}
// Play soundfile given by sfName, playbacknet contains the playback 
// network of MarSystem objects 
void play(mrs_real gain, string outName)
{

  NetworkUDPSource* netSrc = new NetworkUDPSource("netSrc");
  
  // update controls if they are passed on cmd line...
  if ( port != 0 ) {
  	netSrc->updctrl("mrs_natural/port", port);
  }
  
  
  // Output destination is either audio or soundfile 
  MarSystem *dest;
  if (outName == EMPTYSTRING)
    dest = new AudioSink("dest");
  else 			
    {
      dest = new AuFileSink("dest");
      dest->updctrl("mrs_string/filename", outName);      
    }

  cout << "Creating playback network..." << endl;
  
  MarSystemManager mn;
  Series playbacknet("playbacknet");
  playbacknet.addMarSystem(netSrc);
  playbacknet.addMarSystem(mn.create("Gain", "gt"));
  playbacknet.addMarSystem(dest);
  
  // output network description to cout  
  cout << playbacknet << endl;      
  
  // udpate controls
  playbacknet.updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);
  playbacknet.updctrl("Gain/gt/mrs_real/gain", gain);
  
  // may want to update this as control data from networksource...
  if (outName == EMPTYSTRING) 
    playbacknet.updctrl("AudioSink/dest/mrs_natural/nChannels", 1);
  else 
    playbacknet.updctrl("AuFileSink/dest/mrs_natural/nChannels", 1);
	
  mrs_natural wc=0;
  mrs_natural samplesPlayed = 0;
  mrs_natural onSamples = playbacknet.getctrl("mrs_natural/onSamples")->to<mrs_natural>();
  // mrs_natural repeatId = 1;
  
  netSrc->refresh();
  
  while (true) 
  {
  	try {
  	  playbacknet.tick();
  	  if ( !showClient ) {
  	  	cout << "Receiving from: " << netSrc->getClientAddr() << endl;
  	  	showClient = true;
  	  }
  	  
  	}
  	catch( SocketException e ) {
  	  cerr << "Played " << wc << " slices of " << onSamples << " samples" << endl;	
  	  wc = onSamples = 0;
  	  exit(1);
  	} 
  	wc ++;
    samplesPlayed += onSamples;
  }
  
}
Exemplo n.º 14
0
	void extractBpm(string filename) {
		cout << "BPM extractor" << endl;
		MarSystemManager mng;
		MarSystem* blackbox = mng.create("Series", "blackbox");
		blackbox->addMarSystem(mng.create("SoundFileSource", "src"));

		/**
		 * Linkaggio dei controlli: SoundSource
		 */
		blackbox->linkctrl("mrs_string/filename", "SoundFileSource/src/mrs_string/filename");
		blackbox->linkctrl("mrs_string/labelNames", "SoundFileSource/src/mrs_string/labelNames");
		blackbox->updctrl("mrs_string/filename", filename);

		/*
		 * Creazione di un file di testo con alcune informazioni sul file:
		 * fname = name of the file courrently analyzed
		 * israte = input sample rate
		 * osrate = output sample rate
		 * duration = duration of the file in seconds
		 * size = number of audio samples contained into the file
		 * label = label for classification use
		 */

		ofstream finfo;
		finfo.open("durata.txt");

		mrs_string fname = blackbox->getctrl("mrs_string/filename")->to<mrs_string>();
		finfo << "Input file name: " << fname << endl;
		mrs_real israte = blackbox->getctrl("mrs_real/israte")->to<mrs_real>();
		finfo << "Input samplerate: " << israte << " Hz" << endl;
		mrs_real osrate = blackbox->getctrl("mrs_real/osrate")->to<mrs_real>();
		finfo << "Output samplerate: " << osrate << " Hz" << endl;
		mrs_real duration = blackbox->getctrl("SoundFileSource/src/mrs_real/duration")->to<mrs_real>();
		finfo << "Duration: " << duration <<  " seconds" << endl;
		mrs_natural size = blackbox->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
		finfo << "Number of samples of courrent file: " << size << endl;
		mrs_string label = blackbox->getctrl("mrs_string/labelNames")->to<mrs_string>();
		finfo << "Label name: " << label << endl;
		finfo.close();

		ofstream wavelet;
		wavelet.open("waveletbands.txt");
		wavelet << "WAVELET BANDS VALUES" << endl;
		/*
		 * Preparazione per l'estrazione dell'inviluppo del segnale audio
		 */

		//blackbox->addMarSystem(mng.create("WaveletBands", "wavelet"));
		//blackbox->addMarSystem(mng.create("FullWaveRectifier","rectifier"));
		blackbox->addMarSystem(mng.create("AutoCorrelation", "autocorrelation"));
		blackbox->updctrl("AutoCorrelation/autocorrelation/mrs_bool/aliasedOutput", false);
		blackbox->addMarSystem(mng.create("BeatHistogram", "histogram"));

		mrs_real val = 0.0;
		while (blackbox->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()) {
			blackbox->tick();
			const mrs_realvec& src_data =
			blackbox->getctrl("mrs_realvec/processedData")->to<mrs_realvec>();
			val = src_data(0,0);
			wavelet << val << endl;
		}
		wavelet.close();

		delete blackbox;
		cout << "done" << endl;
	}
void midiBoomChickAnnotate(mrs_natural bufferSize, 
						   mrs_natural inputSize, 
						   string backgroundopt)
{

    /* RtMidi* rtmidi = NULL;


       try {
       rtmidi = new RtMidi();
       }
       catch (RtError3 &error) {
       exit(1);
       }
	*/ 

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

    MarSystem* pnet = mng.create("Series","pnet");
    MarSystem* oscbank = mng.create("Fanout", "oscbank");
    oscbank->addMarSystem(mng.create("SoundFileSource", "src3"));
    oscbank->addMarSystem(mng.create("SoundFileSource", "src4"));

    pnet->addMarSystem(oscbank);
    pnet->addMarSystem(mng.create("Sum", "sum"));
    pnet->addMarSystem(mng.create("SoundFileSink", "dest"));  


    MarSystem* bbank = mng.create("Fanout", "bbank");
    bbank->addMarSystem(pnet);
    if (backgroundopt != EMPTYSTRING) 
        bbank->addMarSystem(mng.create("SoundFileSource", "src5"));


    total->addMarSystem(bbank);
    total->addMarSystem(mng.create("Sum", "tsum"));
    total->addMarSystem(mng.create("AudioSink", "dest"));

    // output file 
    pnet->updctrl("SoundFileSink/dest/mrs_string/filename", "drum.wav");


    int byte2, byte3;
    int channel;
    int type;
    int prev_pitch;

    // bass drum and snare drum sounds 
    pnet->updctrl("Fanout/oscbank/SoundFileSource/src3/mrs_string/filename", "../rawwaves/sd22k.wav");
    pnet->updctrl("Fanout/oscbank/SoundFileSource/src4/mrs_string/filename", "../rawwaves/bd22k.wav");
    if (backgroundopt != EMPTYSTRING) 
        bbank->updctrl("SoundFileSource/src5/mrs_string/filename", backgroundopt);


    total->updctrl("AudioSink/dest/mrs_natural/bufferSize", bufferSize);
    total->updctrl("mrs_natural/inSamples", inputSize);
    pnet->linkctrl("mrs_natural/pos3", "Fanout/oscbank/SoundFileSource/src3/mrs_natural/pos");    
    pnet->linkctrl("mrs_natural/pos4", "Fanout/oscbank/SoundFileSource/src4/mrs_natural/pos");    



    /* while(1) 
       {  
       if (rtmidi->nextMessage() > 0) 
       {
       byte3 = rtmidi->getByteThree();
       byte2 = rtmidi->getByteTwo();
       channel = rtmidi->getChannel();
       type = rtmidi->getType();

	   // STC1000 NoteOn's
	   if ((type == 144) && (byte3 != 0)) 
	   {
	   // rewind the files 
	   if (byte2 == 44) 
	   pnet->updctrl("mrs_natural/pos3", 0);

	   if (byte2 == 53) 
	   pnet->updctrl("mrs_natural/pos4", 0);		

	   }

	   // Radio Drum stick 1
	   if ((type == 160) && (byte2 == 15)) 	  
	   {

	   if ((byte3 >= 40) && 
	   (byte3 <= 100))
	   {
	   pnet->updctrl("mrs_natural/pos3", 0);	
	   }
	   else 
	   {
	   pnet->updctrl("mrs_natural/pos4", 0);	              
	   }
	   }

	   // Radio Drum stick 2 
	   if ((type == 160) && (byte2 == 17)) 	  
	   {
	   if ((byte3 >= 40) && 
	   (byte3 <= 100))
	   {
	   pnet->updctrl("mrs_natural/pos3", 0);	
	   }
	   else 
	   {
	   pnet->updctrl("mrs_natural/pos4", 0);	              
	   }
	   } 
	   }

	   total->tick();	



	   }
	*/ 

}
//Pluck Karplus Strong Model Plucked.cpp outputs to DAC
void PluckLive(string deviceopt, mrs_real pos, mrs_real fre, mrs_real loz, mrs_real stret)
{



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

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

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

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


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

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


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

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


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



    mrs_natural t=0;

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


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




    while(1)
    {

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

        nBytes = message.size();      

        if (nBytes > 0) 
        {


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

            if (deviceopt == "Keyboard")
            {

                if (type == 144) 
                {

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

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

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

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

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

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

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

                        }
                    }

                }

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

                            ostringstream oss, oss1;

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


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

            }


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

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

                }		
                p0byte3 = byte3;
            }



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

                p1byte3 = byte3;

            }


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

                p2byte3 = byte3;

            }
        }


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


#endif


}
double* recognize(string sfName)
{
	MarSystemManager mng;
	MarSystem* pnet = mng.create("Series", "pnet");
	MarSystem* Fanout1 = mng.create("Fanout","Fanout1");
	MarSystem* Fanout2 = mng.create("Fanout","Fanout2");
	MarSystem* Spec = mng.create("Series", "Spec");
	MarSystem* SerCentroid = mng.create("Series", "SerCentroid");
	MarSystem* SerRolloff = mng.create("Series", "SerRolloff");
	MarSystem* SerFlux = mng.create("Series", "SerFlux");
	MarSystem* SerSpecMean = mng.create("Series", "SerSpecMean");
	MarSystem* SerSpecMed = mng.create("Series", "SerSpecMed");
	//MarSystem* SerMean = mng.create("Series", "SerMean");		//comming same as spectral Mean
	MarSystem* SerStdDev = mng.create("Series", "SerStdDev");
	MarSystem* SerSkew = mng.create("Series", "SerSkew");
	MarSystem* SerKurtosis = mng.create("Series", "SerKurtosis");
	MarSystem* SerPower = mng.create("Series", "SerPower");
	MarSystem* SerAutoCor = mng.create("Series", "SerAutoCor");
	MarSystem* SerZeroCross = mng.create("Series", "SerZeroCross");
	MarSystem* SerRms = mng.create("Series", "SerRms");
	MarSystem* SerAmdf = mng.create("Series", "SerAmdf");
	MarSystem* SerMFCC = mng.create("Series", "SerMFCC");
	MarSystem* SerLPC = mng.create("Series", "SerLPC");
	MarSystem* SerMaxMin = mng.create("Series", "SerMaxMin"); //Distorting sound
	MarSystem* SerPeaker = mng.create("Series", "SerPeaker");
	MarSystem* SerChroma = mng.create("Series", "SerChroma");
	MarSystem* SerEnergy = mng.create("Series", "SerEnergy");
	//MarSystem* SerSCF = mng.create("Series", "SerSCF"); //Enough feature are up there no space for more.
														  //You can replace above ones by below ones to make them work.
	//MarSystem* SerSFM = mng.create("Series", "SerSFM"); //many values coming out as 'nan'
	//MarSystem* SerSnr = mng.create("Series", "SerSnr");
	//MarSystem* SerSpecSnr = mng.create("Series", "SerSepecSnr"); //printing extra stuff!!
	//MarSystem* SerF0analysis = mng.create("Series", "F0analysis");
	
	// standard network
	pnet->addMarSystem(mng.create("SoundFileSource", "src"));
	pnet->updctrl("SoundFileSource/src/mrs_string/filename", sfName);
	
	Fanout1->addMarSystem(mng.create("AudioSink", "dest"));
	Fanout1->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
	
	Spec->addMarSystem(mng.create("Spectrum","spk"));
	
	SerCentroid->addMarSystem(mng.create("Centroid", "centeroid"));
	SerCentroid->addMarSystem(mng.create("Gain", "g2"));
	
	SerRolloff->addMarSystem(mng.create("Rolloff", "rolloff"));
	SerRolloff->addMarSystem(mng.create("Gain", "g3"));
	
	SerFlux->addMarSystem(mng.create("Flux", "flux"));
	//SerFlux->updctrl("Flux/flux/mrs_bool/reset",true);
	SerFlux->addMarSystem(mng.create("Gain", "g4"));
	
	SerSpecMean->addMarSystem(mng.create("Mean", "Specmean"));
	SerSpecMean->addMarSystem(mng.create("Gain", "g5"));
	
	SerSpecMed->addMarSystem(mng.create("Median", "Specmed"));
	SerSpecMed->addMarSystem(mng.create("Gain", "g15"));
	
	SerSkew->addMarSystem(mng.create("Skewness", "skewness"));
	SerSkew->addMarSystem(mng.create("Gain", "g7"));
	
	SerKurtosis->addMarSystem(mng.create("Kurtosis", "kurtosis"));
	SerKurtosis->addMarSystem(mng.create("Gain", "g8"));
	
	SerPower->addMarSystem(mng.create("Power", "power"));
	SerPower->addMarSystem(mng.create("Gain", "g8"));
	
	SerAutoCor->addMarSystem(mng.create("AutoCorrelation", "autocor"));
	SerAutoCor->addMarSystem(mng.create("Gain", "g9"));
	
	SerRms->addMarSystem(mng.create("Rms", "rms"));
	SerRms->addMarSystem(mng.create("Gain", "g11"));
	
	SerMFCC->addMarSystem(mng.create("PowerSpectrum", "powspec"));
	SerMFCC->addMarSystem(mng.create("MFCC", "mfcc"));
	SerMFCC->addMarSystem(mng.create("Gain", "g13"));
	
	SerLPC->addMarSystem(mng.create("LPC", "lpc"));
	//SerLPC->updctrl("LPC/lpc/mrs_natural/order",5);
	SerLPC->addMarSystem(mng.create("LPCC", "lpcc"));
	//SerLPC->addMarSystem(mng.create("LSP", "lsp"));
	SerLPC->addMarSystem(mng.create("Gain", "g14"));
	
	SerMaxMin->addMarSystem(mng.create("MaxMin", "maxmin"));
	SerMaxMin->addMarSystem(mng.create("Gain", "g16"));
	
	SerChroma->addMarSystem(mng.create("Chroma", "chroma"));
	SerChroma->addMarSystem(mng.create("Gain", "g18"));
	
	////SerSCF->addMarSystem(mng.create("SCF", "scf"));
	////SerSCF->addMarSystem(mng.create("Gain", "g20"));
	
	////SerSFM->addMarSystem(mng.create("SFM", "sfm"));
	////SerSFM->addMarSystem(mng.create("Gain", "g21"));
	
	////SerSpecSnr->addMarSystem(mng.create("SpectralSNR", "specsnr"));
	////SerSpecSnr->addMarSystem(mng.create("Gain", "g23"));
	
	Fanout2->addMarSystem(SerCentroid);
	Fanout2->addMarSystem(SerRolloff);
	Fanout2->addMarSystem(SerFlux);
	Fanout2->addMarSystem(SerSpecMean);
	Fanout2->addMarSystem(SerSpecMed);
	Fanout2->addMarSystem(SerSkew);
	Fanout2->addMarSystem(SerKurtosis);
	Fanout2->addMarSystem(SerPower);
	////Fanout2->addMarSystem(SerAutoCor);
	Fanout2->addMarSystem(SerRms);
	Fanout2->addMarSystem(SerMFCC);
	Fanout2->addMarSystem(SerLPC);
	////Fanout2->addMarSystem(SerMaxMin);
	Fanout2->addMarSystem(SerChroma);
	////Fanout2->addMarSystem(SerSFM);
	////Fanout2->addMarSystem(SerSCF);
	////Fanout2->addMarSystem(SerSpecSnr);
	
		
	Spec->addMarSystem(Fanout2);
	
	Fanout1->addMarSystem(Spec);
	
//	SerMean->addMarSystem(mng.create("Mean", "mean"));
//	SerMean->addMarSystem(mng.create("Gain", "g6"));
//	Fanout1->addMarSystem(SerMean);
	
	SerStdDev->addMarSystem(mng.create("StandardDeviation", "stddev"));
	SerStdDev->addMarSystem(mng.create("Gain", "g6"));
	
	SerZeroCross->addMarSystem(mng.create("ZeroCrossings", "zerocross"));
	SerZeroCross->addMarSystem(mng.create("Gain", "g10"));
	
	SerAmdf->addMarSystem(mng.create("AMDF", "amdf"));
	SerAmdf->addMarSystem(mng.create("Gain", "g12"));
	
	SerPeaker->addMarSystem(mng.create("Peaker", "peaker"));
	SerPeaker->addMarSystem(mng.create("Gain", "g17"));
	
	SerEnergy->addMarSystem(mng.create("Energy", "energy"));
	SerEnergy->addMarSystem(mng.create("Gain", "g19"));
	
//	SerF0analysis->addMarSystem(mng.create("F0Analysis", "f0analysis"));
//	SerF0analysis->addMarSystem(mng.create("Gain", "g17"));
	
	////SerSnr->addMarSystem(mng.create("SNR", "snr"));
	////SerSnr->addMarSystem(mng.create("Gain", "g22"));
	
	Fanout1->addMarSystem(SerStdDev);
	Fanout1->addMarSystem(SerZeroCross);
	Fanout1->addMarSystem(SerAmdf);
	Fanout1->addMarSystem(SerPeaker);
	Fanout1->addMarSystem(SerEnergy);
	////Fanout1->addMarSystem(SerSnr);
//	Fanout1->addMarSystem(SerF0analysis);
	
	pnet->addMarSystem(Fanout1);
	double* FVec = (double*) malloc(1851*sizeof(double));
	//512+10+512+257+13+12+10+12+1+512
	
	while ( pnet->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() )
	{
		pnet->tick();
		
		mrs_natural i=0,j=0;
		// gets data from the Spectrum for read only!
		const realvec& spectrum =
		    Spec->getctrl("Spectrum/spk/mrs_realvec/processedData")->to<mrs_realvec>(); //512 X 1 vector
		for(i=0;i<512;i++)
			FVec[i] = spectrum(i);
		
		const realvec& center = SerCentroid->getctrl("Centroid/centeroid/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = center(0,0);
		
		const realvec& rolloff = SerRolloff->getctrl("Rolloff/rolloff/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = rolloff(0,0);
		
		////const realvec& flux = SerFlux->getctrl("Flux/flux/mrs_realvec/processedData")->to<mrs_realvec>();
		////FVec[i++] = flux(0,0); //ERROR: output is nan or 0??
		
		const realvec& Specmean = SerSpecMean->getctrl("Mean/Specmean/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = Specmean(0,0);
		
		const realvec& Specmed = SerSpecMed->getctrl("Median/Specmed/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = Specmed(0,0);
		
		//const realvec& mean = SerMean->getctrl("Mean/mean/mrs_realvec/processedData")->to<mrs_realvec>();
		//FVec[i++] = mean(0,0);
		
		const realvec& StdDev = SerStdDev->getctrl("StandardDeviation/stddev/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = StdDev(0,0);
		
		const realvec& skew = SerSkew->getctrl("Skewness/skewness/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = skew(0,0);
		
		const realvec& kurtosis = SerKurtosis->getctrl("Kurtosis/kurtosis/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = kurtosis(0,0);
		
		const realvec& power = SerPower->getctrl("Power/power/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = power(0,0);
		
		////const realvec& autocor = SerAutoCor->getctrl("AutoCorrelation/autocor/mrs_realvec/processedData")->to<mrs_realvec>();
		////FVec[i++] = autocor(0,0);
		
		const realvec& zerocross = SerZeroCross->getctrl("ZeroCrossings/zerocross/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = zerocross(0,0);
		
		const realvec& rms = SerRms->getctrl("Rms/rms/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = rms(0,0);
		
		const realvec& amdf = SerAmdf->getctrl("AMDF/amdf/mrs_realvec/processedData")->to<mrs_realvec>(); //1 X 512 vector
		for(j=0;j<512;j++,i++)
			FVec[i] = amdf(j);
		
		const realvec& powspec = SerMFCC->getctrl("PowerSpectrum/powspec/mrs_realvec/processedData")->to<mrs_realvec>(); //257 X 1 vector
		for(j=0;j<257;j++,i++)
			FVec[i] = powspec(j);
		
		const realvec& mfcc = SerMFCC->getctrl("MFCC/mfcc/mrs_realvec/processedData")->to<mrs_realvec>(); //13 X 1 vector
		for(j=0;j<13;j++,i++)
			FVec[i] = mfcc(j);
		
		const realvec& lpc = SerLPC->getctrl("LPC/lpc/mrs_realvec/processedData")->to<mrs_realvec>(); //12 X 1 vector
		for(j=0;j<12;j++,i++)
			FVec[i] = lpc(j);
		
		const realvec& lpcc = SerLPC->getctrl("LPCC/lpcc/mrs_realvec/processedData")->to<mrs_realvec>(); //10 X 1 vector
		for(j=0;j<10;j++,i++)
			FVec[i] = lpcc(j);
		
		//const realvec& lsp = SerLPC->getctrl("LSP/lsp/mrs_realvec/processedData")->to<mrs_realvec>(); //12 X 1 vector
		//for(j=0;j<12;j++,i++)
		//	FVec[i] = lsp(j);
		
		////const realvec& maxmin = SerMaxMin->getctrl("MaxMin/maxmin/mrs_realvec/processedData")->to<mrs_realvec>();
		////Fvec[i++] = maxmin(0); Fvec[i++] = maxmin(1); 
		
		//const realvec& f0 = SerF0analysis->getctrl("F0Analysis/f0analysis/mrs_realvec/processedData")->to<mrs_realvec>();
		//Error F0Analysis was not recognized
		
		const realvec& chroma = SerChroma->getctrl("Chroma/chroma/mrs_realvec/processedData")->to<mrs_realvec>(); // 12 X 1 vector
		for(j=0;j<12;j++,i++)
			FVec[i] = chroma(j);
		
		const realvec& energy = SerEnergy->getctrl("Energy/energy/mrs_realvec/processedData")->to<mrs_realvec>();
		FVec[i++] = energy(0,0);
		
		////const realvec& scf = SerSCF->getctrl("SCF/scf/mrs_realvec/processedData")->to<mrs_realvec>(); //24 X 1 vector
		////for(j=0;j<24;j++,i++)
		////	FVec[i] = scf(j);
		
		////const realvec& sfm = SerSFM->getctrl("SFM/sfm/mrs_realvec/processedData")->to<mrs_realvec>();
		////for(j=0;j<CHECK-IT;j++,i++) //Check the no. of rows amd col by: cout << sfm
		////	FVec[i] = sfm(j);
		
		////const realvec& snr = SerSnr->getctrl("SNR/snr/mrs_realvec/processedData")->to<mrs_realvec>(); //2 X 1 vector
		////for(j=0;j<2;j++,i++)
		////	FVec[i] = snr(j);
		
		////const realvec& specsnr = SerSpecSnr->getctrl("SpectralSNR/specsnr/mrs_realvec/processedData")->to<mrs_realvec>();
		////FVec[i++] = specsnr(0,0);
		
		const realvec& peaks = SerPeaker->getctrl("Peaker/peaker/mrs_realvec/processedData")->to<mrs_realvec>(); //1 X 512 vector with some peaks other zeros
		for(j=0;j<512;j++)
			if(peaks(j)>0)
				FVec[i++] = peaks(j);

		cout<<FVec[10]<<"\n";

		/*
		cout << "\nOriginal Spectrum = " << spectrum(1) << endl;
		cout << "Centroid = " << center(0,0) << endl;
		cout << "Roll Off = " << rolloff(0,0) << endl;
		////cout << "Flux = " << flux(0,0) << endl;
		cout << "Spectral Mean = " << Specmean(0,0) << endl;
		cout << "Spectral Median = " << Specmed(0,0) << endl;
		//cout << "Mean = " << mean(0,0) << endl;
		cout << "Standard Deviation = " << StdDev(0,0) << endl;
		cout << "Spectral Skewness = " << skew(0,0) << endl;
		cout << "Spectral Kurtosis = " << kurtosis(0,0) << endl;
		cout << "Spectral Power = " << power(0,0) << endl;
		////cout << "Spectral AutoCorrelation = " << autocor(0,0) << endl;
		cout << "Zero Crossings rate = " << zerocross(0,0) << endl;
		cout << "RMS Energy = " << rms(0,0) << endl;
		cout << "AMDF = " << amdf(500) <<"\t" << FVec[1023] << endl; //1 X 512 vector
		cout << "Power Spectrum = " << powspec(200) << endl; //257 X 1 vector
		cout << "MFCC = " << mfcc(12) << endl; //13 X 1 vector
		cout << "LPC = " << lpc(5) << endl; //12 X 1 vector
		cout << "LPCC = " << lpcc(5) << endl; //10 X 1 vector
		//cout << "LSP = " << lsp(5) << endl; //12 X 1 vector
		////cout << "Max Value = " << maxmin(0) << endl << "Min Value = " << maxmin(1) << endl;
		//cout << "F0 = " << f0 << endl;
		cout << "Chroma = " << chroma(5) << endl; // 12 X 1 vector
		cout << "Energy = " << energy(0,0) << endl;
		////cout << "SCF = " << scf(2) << endl; //24 X 1 vector
		////cout << "SFM = " << sfm << endl;
		////cout << "SNR = " << snr(0) << endl; //2 X 1 vector
		////cout << "Spectral SNR = " << specsnr(0,0) << endl; //printing extra stuff, get rid of it!!
		cout << "Peaks = " << FVec[i-3] << endl; //1 X 512 vector with some peaks other zeros
		*/
		
	}
	return FVec;
}
double* recognize(string sfName)
{
	MarSystemManager mng;
	MarSystem* pnet = mng.create("Series", "pnet");
	MarSystem* Fanout1 = mng.create("Fanout","Fanout1");
	MarSystem* Fanout2 = mng.create("Fanout","Fanout2");
	MarSystem* Spec = mng.create("Series", "Spec");
	MarSystem* SerCentroid = mng.create("Series", "SerCentroid");
	MarSystem* SerRolloff = mng.create("Series", "SerRolloff");
	MarSystem* SerFlux = mng.create("Series", "SerFlux");
	MarSystem* SerSpecMean = mng.create("Series", "SerSpecMean");
	MarSystem* SerSpecMed = mng.create("Series", "SerSpecMed");
	//MarSystem* SerMean = mng.create("Series", "SerMean");		//comming same as spectral Mean
	MarSystem* SerStdDev = mng.create("Series", "SerStdDev");
	MarSystem* SerSkew = mng.create("Series", "SerSkew");
	MarSystem* SerKurtosis = mng.create("Series", "SerKurtosis");
	MarSystem* SerPower = mng.create("Series", "SerPower");
	MarSystem* SerAutoCor = mng.create("Series", "SerAutoCor");
	MarSystem* SerZeroCross = mng.create("Series", "SerZeroCross");
	MarSystem* SerRms = mng.create("Series", "SerRms");
	MarSystem* SerAmdf = mng.create("Series", "SerAmdf");
	MarSystem* SerMFCC = mng.create("Series", "SerMFCC");
	MarSystem* SerLPC = mng.create("Series", "SerLPC");
	MarSystem* SerMaxMin = mng.create("Series", "SerMaxMin"); //Distorting sound
	MarSystem* SerPeaker = mng.create("Series", "SerPeaker");
	MarSystem* SerChroma = mng.create("Series", "SerChroma");
	MarSystem* SerEnergy = mng.create("Series", "SerEnergy");
	//MarSystem* SerSCF = mng.create("Series", "SerSCF"); //Enough feature are up there no space for more.
														  //You can replace above ones by below ones to make them work.
	//MarSystem* SerSFM = mng.create("Series", "SerSFM"); //many values coming out as 'nan'
	//MarSystem* SerSnr = mng.create("Series", "SerSnr");
	//MarSystem* SerSpecSnr = mng.create("Series", "SerSepecSnr"); //printing extra stuff!!
	//MarSystem* SerF0analysis = mng.create("Series", "F0analysis");
	
	// standard network
	pnet->addMarSystem(mng.create("SoundFileSource", "src"));
	pnet->updctrl("SoundFileSource/src/mrs_string/filename", sfName);
	
	Fanout1->addMarSystem(mng.create("AudioSink", "dest"));
	Fanout1->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
	
	Spec->addMarSystem(mng.create("Spectrum","spk"));
	
	SerCentroid->addMarSystem(mng.create("Centroid", "centeroid"));
	SerCentroid->addMarSystem(mng.create("Gain", "g2"));
	
	SerRolloff->addMarSystem(mng.create("Rolloff", "rolloff"));
	SerRolloff->addMarSystem(mng.create("Gain", "g3"));
	
	SerFlux->addMarSystem(mng.create("Flux", "flux"));
	//SerFlux->updctrl("Flux/flux/mrs_bool/reset",true);
	SerFlux->addMarSystem(mng.create("Gain", "g4"));
	
	SerSpecMean->addMarSystem(mng.create("Mean", "Specmean"));
	SerSpecMean->addMarSystem(mng.create("Gain", "g5"));
	
	SerSpecMed->addMarSystem(mng.create("Median", "Specmed"));
	SerSpecMed->addMarSystem(mng.create("Gain", "g15"));
	
	SerSkew->addMarSystem(mng.create("Skewness", "skewness"));
	SerSkew->addMarSystem(mng.create("Gain", "g7"));
	
	SerKurtosis->addMarSystem(mng.create("Kurtosis", "kurtosis"));
	SerKurtosis->addMarSystem(mng.create("Gain", "g8"));
	
	SerPower->addMarSystem(mng.create("Power", "power"));
	SerPower->addMarSystem(mng.create("Gain", "g8"));
	
	SerAutoCor->addMarSystem(mng.create("AutoCorrelation", "autocor"));
	SerAutoCor->addMarSystem(mng.create("Gain", "g9"));
	
	SerRms->addMarSystem(mng.create("Rms", "rms"));
	SerRms->addMarSystem(mng.create("Gain", "g11"));
	
	SerMFCC->addMarSystem(mng.create("PowerSpectrum", "powspec"));
	SerMFCC->addMarSystem(mng.create("MFCC", "mfcc"));
	SerMFCC->addMarSystem(mng.create("Gain", "g13"));
	
	SerLPC->addMarSystem(mng.create("LPC", "lpc"));
	//SerLPC->updctrl("LPC/lpc/mrs_natural/order",5);
	SerLPC->addMarSystem(mng.create("LPCC", "lpcc"));
	//SerLPC->addMarSystem(mng.create("LSP", "lsp"));
	SerLPC->addMarSystem(mng.create("Gain", "g14"));
	
	SerMaxMin->addMarSystem(mng.create("MaxMin", "maxmin"));
	SerMaxMin->addMarSystem(mng.create("Gain", "g16"));
	
	SerChroma->addMarSystem(mng.create("Chroma", "chroma"));
	SerChroma->addMarSystem(mng.create("Gain", "g18"));
	
	////SerSCF->addMarSystem(mng.create("SCF", "scf"));
	////SerSCF->addMarSystem(mng.create("Gain", "g20"));
	
	////SerSFM->addMarSystem(mng.create("SFM", "sfm"));
	////SerSFM->addMarSystem(mng.create("Gain", "g21"));
	
	////SerSpecSnr->addMarSystem(mng.create("SpectralSNR", "specsnr"));
	////SerSpecSnr->addMarSystem(mng.create("Gain", "g23"));
	
	Fanout2->addMarSystem(SerCentroid);
	Fanout2->addMarSystem(SerRolloff);
	Fanout2->addMarSystem(SerFlux);
	Fanout2->addMarSystem(SerSpecMean);
	Fanout2->addMarSystem(SerSpecMed);
	Fanout2->addMarSystem(SerSkew);
	Fanout2->addMarSystem(SerKurtosis);
	Fanout2->addMarSystem(SerPower);
	////Fanout2->addMarSystem(SerAutoCor);
	Fanout2->addMarSystem(SerRms);
	Fanout2->addMarSystem(SerMFCC);
	Fanout2->addMarSystem(SerLPC);
	////Fanout2->addMarSystem(SerMaxMin);
	Fanout2->addMarSystem(SerChroma);
	////Fanout2->addMarSystem(SerSFM);
	////Fanout2->addMarSystem(SerSCF);
	////Fanout2->addMarSystem(SerSpecSnr);
	
		
	Spec->addMarSystem(Fanout2);
	
	Fanout1->addMarSystem(Spec);
	
//	SerMean->addMarSystem(mng.create("Mean", "mean"));
//	SerMean->addMarSystem(mng.create("Gain", "g6"));
//	Fanout1->addMarSystem(SerMean);
	
	SerStdDev->addMarSystem(mng.create("StandardDeviation", "stddev"));
	SerStdDev->addMarSystem(mng.create("Gain", "g6"));
	
	SerZeroCross->addMarSystem(mng.create("ZeroCrossings", "zerocross"));
	SerZeroCross->addMarSystem(mng.create("Gain", "g10"));
	
	SerAmdf->addMarSystem(mng.create("AMDF", "amdf"));
	SerAmdf->addMarSystem(mng.create("Gain", "g12"));
	
	SerPeaker->addMarSystem(mng.create("Peaker", "peaker"));
	SerPeaker->addMarSystem(mng.create("Gain", "g17"));
	
	SerEnergy->addMarSystem(mng.create("Energy", "energy"));
	SerEnergy->addMarSystem(mng.create("Gain", "g19"));
	
//	SerF0analysis->addMarSystem(mng.create("F0Analysis", "f0analysis"));
//	SerF0analysis->addMarSystem(mng.create("Gain", "g17"));
	
	////SerSnr->addMarSystem(mng.create("SNR", "snr"));
	////SerSnr->addMarSystem(mng.create("Gain", "g22"));
	
	Fanout1->addMarSystem(SerStdDev);
	Fanout1->addMarSystem(SerZeroCross);
	Fanout1->addMarSystem(SerAmdf);
	Fanout1->addMarSystem(SerPeaker);
	Fanout1->addMarSystem(SerEnergy);
	////Fanout1->addMarSystem(SerSnr);
//	Fanout1->addMarSystem(SerF0analysis);
	
	pnet->addMarSystem(Fanout1);
	double* FVec;// = (double*) malloc(1851*sizeof(double));
	//512+10+512+257+13+12+10+12+1+512
	
	while ( pnet->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() )
		FVec = MyTick(pnet);
	return FVec;
}
void 
sfmixer(string sfName1)
{
 
  // Initialize Messager
  // Messager* messager =0;
  // messager = new Messager(2,2001);  

  // Set up mixer1 Series
  MarSystemManager mng;
 
  // Set up Channel 1 - 1
  MarSystem* Channel_1_1 = mng.create("Series", "Channel_1_1");
  Channel_1_1->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_1_1->addMarSystem(mng.create("Gain","gain"));
  Channel_1_1->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_1_1->updctrl("Gain/gain/mrs_real/gain", 0.0);
     
  
  // Set up Channel 2 - 1
  MarSystem* Channel_2_1 = mng.create("Series", "Channel_2_1");
  Channel_2_1->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_2_1->addMarSystem(mng.create("Gain","gain"));
  Channel_2_1->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_2_1->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Set up Channel 3 - 1
  MarSystem* Channel_3_1 = mng.create("Series", "Channel_3_1");
  Channel_3_1->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_3_1->addMarSystem(mng.create("Gain","gain"));
  Channel_3_1->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_3_1->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Set up Channel 4 - 1
  MarSystem* Channel_4_1 = mng.create("Series", "Channel_4_1");
  Channel_4_1->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_4_1->addMarSystem(mng.create("Gain","gain"));
  Channel_4_1->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_4_1->updctrl("Gain/gain/mrs_real/gain", 0.0);


  // Funnel all Channels together with a Fanin for Mix 1
  MarSystem* mix1 = mng.create("Fanin", "mix1");
  mix1->addMarSystem(Channel_1_1);
  mix1->addMarSystem(Channel_2_1);
  mix1->addMarSystem(Channel_3_1);
  mix1->addMarSystem(Channel_4_1);

  // Mixer1 for main Output 1 
  MarSystem* mixer1 = mng.create("Series", "mixer1");
  mixer1->addMarSystem(mix1);
  mixer1->addMarSystem(mng.create("Gain","gain"));
  mixer1->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Mixer 1 for main Output 1 (CROSS FADE CHAIN)
  MarSystem* Cmixer1 = mng.create("Series", "Cmixer1");
  Cmixer1->addMarSystem(mixer1);
  Cmixer1->addMarSystem(mng.create("Gain","gain"));
  Cmixer1->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Set up Channel 1 - 2
  MarSystem* Channel_1_2 = mng.create("Series", "Channel_1_2");
  Channel_1_2->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_1_2->addMarSystem(mng.create("Gain","gain"));
  Channel_1_2->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_1_2->updctrl("Gain/gain/mrs_real/gain", 0.0);
  
  // Set up Channel 2 - 2
  MarSystem* Channel_2_2 = mng.create("Series", "Channel_2_2");
  Channel_2_2->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_2_2->addMarSystem(mng.create("Gain","gain"));
  Channel_2_2->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_2_2->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Set up Channel 3 - 2
  MarSystem* Channel_3_2 = mng.create("Series", "Channel_3_2");
  Channel_3_2->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_3_2->addMarSystem(mng.create("Gain","gain"));
  Channel_3_2->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_3_2->updctrl("Gain/gain/mrs_real/gain", 0.0);

  // Set up Channel 4 - 2
  MarSystem* Channel_4_2 = mng.create("Series", "Channel_4_2");
  Channel_4_2->addMarSystem(mng.create("SoundFileSource","src"));
  Channel_4_2->addMarSystem(mng.create("Gain","gain"));
  Channel_4_2->updctrl("SoundFileSource/src/mrs_string/filename",sfName1);
  Channel_4_2->updctrl("Gain/gain/mrs_real/gain", 1.0);


  // Funnel all Channels together with a Fanin for Mixer 2
  MarSystem* mix2 = mng.create("Fanin", "mix2");
  mix2->addMarSystem(Channel_1_2);
  mix2->addMarSystem(Channel_2_2);
  mix2->addMarSystem(Channel_3_2);
  mix2->addMarSystem(Channel_4_2);

  // Mixer 2 for main Output 2
  MarSystem* mixer2 = mng.create("Series", "mixer2");
  mixer2->addMarSystem(mix2);
  mixer2->addMarSystem(mng.create("Gain","gain"));
  mixer2->updctrl("Gain/gain/mrs_real/gain", 1.0);

  // Mixer 2 for main Output 2 (CROSS FADE CHAIN)
  MarSystem* Cmixer2 = mng.create("Series", "Cmixer2");
  Cmixer2->addMarSystem(mixer2);
  Cmixer2->addMarSystem(mng.create("Gain","gain"));
  Cmixer2->updctrl("Gain/gain/mrs_real/gain", 1.0);

  // Master Mixer using Fan Out 
  MarSystem *mixMaster = mng.create("Series", "mixMaster"); 
  MarSystem *mixM = mng.create("Fanout", "mixM");
  mixM->addMarSystem(Cmixer2);
  mixM->addMarSystem(Cmixer1);
  mixMaster->addMarSystem(mixM);
  mixMaster->addMarSystem(mng.create("Sum", "sum"));
  mixMaster->addMarSystem(mng.create("Gain","gain"));
  mixMaster->addMarSystem(mng.create("AudioSink","dest"));
  mixMaster->updctrl("Gain/gain/mrs_real/gain", 1.0);
  
  // initialize controls for Series
  mixMaster->updctrl("mrs_natural/inSamples", 256);
 

  // create variables for messager
  string message;
  bool done = false;
  int type;
  string cname;
  mrs_real dur;

  // create variables for timing
  double start = -1.0;
  double bass[20];
  double snare[20];
  int ibass = 0;
  int isnare = 0;
  int i; 

  // intialize timing variables
  for (i = 0; i < 20; i++)
    {
      bass[i] = -1.0;
      snare[i] = -1.0;
    }


  while (1)
    {
      // **********LOOP files********************
      //  mixer1->tick();
      mixMaster->tick();
      // mixMaster->tick();
      if (Channel_1_1->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_1_1->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer1->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_1_1->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	} 
      
      if (Channel_2_1->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_2_1->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer1->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_2_1->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	}
      
      if (Channel_3_1->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_3_1->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer1->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_3_1->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	} 
      
      if (Channel_4_1->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_4_1->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer1->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_4_1->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	}

      if (Channel_1_2->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_1_2->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer2->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_1_2->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	} 
      
      if (Channel_2_2->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_2_2->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer2->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_2_2->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	}
      
      if (Channel_3_2->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_3_2->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer2->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_3_2->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	} 
      
      if (Channel_4_2->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>() < Channel_4_2->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>() + mixer2->getctrl("mrs_natural/inSamples")->to<mrs_natural>())
      	{
      	  // reset to 0 for looping
      	  Channel_4_2->updctrl("SoundFileSource/src/mrs_natural/pos", 0);
      	}


      // ********* MESSAGER CONTROL ***************//
      
      // type = messager->nextMessage();
      if (type < 0) 
	done = true;
      else 
	{
	//   message = messager->getMessage();
	  stringstream inss(message);
	  inss >> cname;  
	  //cout << cname << endl;

	  // MIXER SLIDER MESSAGES

	  if (cname == "Gain/gt/mrs_real/gain1") 
	    {
	      float x;
	      inss >> dur >> x;
	      Channel_1_1->updctrl("Gain/gain/mrs_real/gain", x);
	    }
	  else if (cname == "Gain/gt/mrs_real/gain2") 
	    {
	      float x;
	      inss >> dur >> x;
	      Channel_2_1->updctrl("Gain/gain/mrs_real/gain", x);
	    }	  	 
void sfplaygui(Collection l, mrs_natural offset, mrs_natural duration, mrs_real start, mrs_real length, mrs_real gain, string outName)
{
    MarSystemManager mng;
    string sfName = l.entry(0);
    MarSystem* src = mng.create("SoundFileSource", "src");
    src->updctrl("mrs_string/filename", sfName);

    Messager* messager =0;
    messager = new Messager(2,2001);


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

    if (src == NULL)
    {
        string errmsg = "Skipping file: " + sfName + " (problem with reading)";
        MRSWARN(errmsg);
    }
    else
    {
        if (outName == EMPTYSTRING)		// audio output
            dest = mng.create("AudioSink", "dest");
        else 				// filename output
        {
            dest = mng.create("SoundFileSink", "dest");
            dest->updctrl("mrs_string/filename", outName);
        }
        // create playback network
        playbacknet->addMarSystem(src);
        playbacknet->addMarSystem(mng.create("Gain", "gt"));
        playbacknet->addMarSystem(dest);



        int type;
        mrs_natural i;

        mrs_natural nChannels = playbacknet->getctrl("SoundFileSource/src/mrs_natural/nChannels")->to<mrs_natural>();
        mrs_real srate = playbacknet->getctrl("SoundFileSource/src/mrs_real/israte")->to<mrs_real>();


        // playback offset & duration
        offset = (mrs_natural) (start * srate * nChannels);
        duration = (mrs_natural) (length * srate * nChannels);


        for (i=0; i < l.size(); i++)
        {
            sfName = l.entry(i);
            cerr << "[" << start << ":" << (start + length) << "] - [" << offset << ":" << (offset + duration) << "] - " <<  sfName << "-" << endl;

            playbacknet->updctrl("SoundFileSource/src/mrs_string/filename", sfName);

            mrs_natural nChannels = src->getctrl("mrs_natural/nChannels")->to<mrs_natural>();
            mrs_real srate = src->getctrl("mrs_real/israte")->to<mrs_real>();

            // playback offset & duration
            offset = (mrs_natural) (start * srate * nChannels);
            duration = (mrs_natural) (length * srate * nChannels);

            // udpate controls
            // playbacknet.updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES/64);
            playbacknet->updctrl("mrs_natural/inSamples", 256);
            playbacknet->updctrl("Gain/gt/mrs_real/gain", gain);

            playbacknet->updctrl("SoundFileSource/src/mrs_natural/pos", offset);
            playbacknet->updctrl(dest->getType() + "/dest/mrs_natural/nChannels",
                                 src->getctrl("mrs_natural/nChannels")->to<mrs_natural>());

            mrs_natural wc=0;
            mrs_natural samplesPlayed = 0;
            mrs_natural onSamples = playbacknet->getctrl("mrs_natural/onSamples")->to<mrs_natural>();
            string message;
            bool done = false;

            while ((playbacknet->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>()) && (duration > samplesPlayed) && !done)
            {


                type = messager->nextMessage();
                if (type < 0)
                    done = true;
                else
                {
                    message = messager->getMessage();
                    stringstream inss(message);
                    string cname = "";
                    mrs_real dur;
                    mrs_real val;
                    inss >> cname >> dur >> val;
                    val = val / 100.0;
                    if (cname != "")
                        playbacknet->updctrl(cname,val);
                }
                playbacknet->tick();
                wc ++;
                samplesPlayed += onSamples;
            }
            cerr << "Played " << wc << " slices of " << onSamples << " samples"
                 << endl;


        }

    }


    // delete messager;
}
void recognize(string sfName)
{
	MarSystemManager mng;
	MarSystem* pnet = mng.create("Series", "pnet");
	MarSystem* Fanout1 = mng.create("Fanout","Fanout1");
	MarSystem* Fanout2 = mng.create("Fanout","Fanout2");
	MarSystem* Spec = mng.create("Series", "Spec");
	MarSystem* SerCentroid = mng.create("Series", "SerCentroid");
	MarSystem* SerRolloff = mng.create("Series", "SerRolloff");
	MarSystem* SerFlux = mng.create("Series", "SerFlux");
	MarSystem* SerSpecMean = mng.create("Series", "SerSpecMean");
	MarSystem* SerSpecMed = mng.create("Series", "SerSpecMed");
	//MarSystem* SerMean = mng.create("Series", "SerMean");		//comming same as spectral Mean
	MarSystem* SerStdDev = mng.create("Series", "SerStdDev");
	MarSystem* SerSkew = mng.create("Series", "SerSkew");
	MarSystem* SerKurtosis = mng.create("Series", "SerKurtosis");
	MarSystem* SerPower = mng.create("Series", "SerPower");
	MarSystem* SerAutoCor = mng.create("Series", "SerAutoCor");
	MarSystem* SerZeroCross = mng.create("Series", "SerZeroCross");
	MarSystem* SerRms = mng.create("Series", "SerRms");
	MarSystem* SerAmdf = mng.create("Series", "SerAmdf");
	MarSystem* SerMFCC = mng.create("Series", "SerMFCC");
	MarSystem* SerLPC = mng.create("Series", "SerLPC");
	MarSystem* SerMaxMin = mng.create("Series", "SerMaxMin"); //Distorting sound
	MarSystem* SerPeaker = mng.create("Series", "SerPeaker");
	MarSystem* SerChroma = mng.create("Series", "SerChroma");
	MarSystem* SerEnergy = mng.create("Series", "SerEnergy");
	//MarSystem* SerF0analysis = mng.create("Series", "F0analysis");
	
	// standard network
	pnet->addMarSystem(mng.create("SoundFileSource", "src"));
	pnet->updctrl("SoundFileSource/src/mrs_string/filename", sfName);
	
	Fanout1->addMarSystem(mng.create("AudioSink", "dest"));
	Fanout1->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
	
	Spec->addMarSystem(mng.create("Spectrum","spk"));
	
	SerCentroid->addMarSystem(mng.create("Centroid", "centeroid"));
	SerCentroid->addMarSystem(mng.create("Gain", "g2"));
	
	SerRolloff->addMarSystem(mng.create("Rolloff", "rolloff"));
	SerRolloff->addMarSystem(mng.create("Gain", "g3"));
	
	SerFlux->addMarSystem(mng.create("Flux", "flux"));
	//SerFlux->updctrl("Flux/flux/mrs_bool/reset",true);
	SerFlux->addMarSystem(mng.create("Gain", "g4"));
	
	SerSpecMean->addMarSystem(mng.create("Mean", "Specmean"));
	SerSpecMean->addMarSystem(mng.create("Gain", "g5"));
	
	SerSpecMed->addMarSystem(mng.create("Median", "Specmed"));
	SerSpecMed->addMarSystem(mng.create("Gain", "g15"));
	
	SerSkew->addMarSystem(mng.create("Skewness", "skewness"));
	SerSkew->addMarSystem(mng.create("Gain", "g7"));
	
	SerKurtosis->addMarSystem(mng.create("Kurtosis", "kurtosis"));
	SerKurtosis->addMarSystem(mng.create("Gain", "g8"));
	
	SerPower->addMarSystem(mng.create("Power", "power"));
	SerPower->addMarSystem(mng.create("Gain", "g8"));
	
	SerAutoCor->addMarSystem(mng.create("AutoCorrelation", "autocor"));
	SerAutoCor->addMarSystem(mng.create("Gain", "g9"));
	
	SerRms->addMarSystem(mng.create("Rms", "rms"));
	SerRms->addMarSystem(mng.create("Gain", "g11"));
	
	SerMFCC->addMarSystem(mng.create("PowerSpectrum", "powspec"));
	SerMFCC->addMarSystem(mng.create("MFCC", "mfcc"));
	SerMFCC->addMarSystem(mng.create("Gain", "g13"));
	
	SerLPC->addMarSystem(mng.create("LPC", "lpc"));
	//SerLPC->updctrl("LPC/lpc/mrs_natural/order",5);
	SerLPC->addMarSystem(mng.create("LPCC", "lpcc"));
	//SerLPC->addMarSystem(mng.create("LSP", "lsp"));
	SerLPC->addMarSystem(mng.create("Gain", "g14"));
	
	SerMaxMin->addMarSystem(mng.create("MaxMin", "maxmin"));
	SerMaxMin->addMarSystem(mng.create("Gain", "g16"));
	
	SerChroma->addMarSystem(mng.create("Chroma", "chroma"));
	SerChroma->addMarSystem(mng.create("Gain", "g18"));
	
	Fanout2->addMarSystem(SerCentroid);
	Fanout2->addMarSystem(SerRolloff);
	Fanout2->addMarSystem(SerFlux);
	Fanout2->addMarSystem(SerSpecMean);
	Fanout2->addMarSystem(SerSpecMed);
	Fanout2->addMarSystem(SerSkew);
	Fanout2->addMarSystem(SerKurtosis);
	Fanout2->addMarSystem(SerPower);
	////Fanout2->addMarSystem(SerAutoCor);
	Fanout2->addMarSystem(SerRms);
	Fanout2->addMarSystem(SerMFCC);
	Fanout2->addMarSystem(SerLPC);
	////Fanout2->addMarSystem(SerMaxMin);
	Fanout2->addMarSystem(SerChroma);
		
	Spec->addMarSystem(Fanout2);
	
	Fanout1->addMarSystem(Spec);
	
//	SerMean->addMarSystem(mng.create("Mean", "mean"));
//	SerMean->addMarSystem(mng.create("Gain", "g6"));
//	Fanout1->addMarSystem(SerMean);
	
	SerStdDev->addMarSystem(mng.create("StandardDeviation", "stddev"));
	SerStdDev->addMarSystem(mng.create("Gain", "g6"));
	
	SerZeroCross->addMarSystem(mng.create("ZeroCrossings", "zerocross"));
	SerZeroCross->addMarSystem(mng.create("Gain", "g10"));
	
	SerAmdf->addMarSystem(mng.create("AMDF", "amdf"));
	SerAmdf->addMarSystem(mng.create("Gain", "g12"));
	
	SerPeaker->addMarSystem(mng.create("Peaker", "peaker"));
	SerPeaker->addMarSystem(mng.create("Gain", "g17"));
	
	SerEnergy->addMarSystem(mng.create("Energy", "energy"));
	SerEnergy->addMarSystem(mng.create("Gain", "g19"));
	
//	SerF0analysis->addMarSystem(mng.create("F0Analysis", "f0analysis"));
//	SerF0analysis->addMarSystem(mng.create("Gain", "g17"));
	
	Fanout1->addMarSystem(SerStdDev);
	Fanout1->addMarSystem(SerZeroCross);
	Fanout1->addMarSystem(SerAmdf);
	Fanout1->addMarSystem(SerPeaker);
	Fanout1->addMarSystem(SerEnergy);
//	Fanout1->addMarSystem(SerF0analysis);
	
	pnet->addMarSystem(Fanout1);

	while ( pnet->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() )
	{
		pnet->tick();
		// gets data from the Spectrum for read only!
		const realvec& processedData =
		    Spec->getctrl("Spectrum/spk/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "\nOriginal Spectrum = " << processedData(1) << endl;
		
		const realvec& center = SerCentroid->getctrl("Centroid/centeroid/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Centroid = " << center(0,0) << endl;
		
		const realvec& rolloff = SerRolloff->getctrl("Rolloff/rolloff/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Roll Off = " << rolloff(0,0) << endl;
		
		const realvec& flux = SerFlux->getctrl("Flux/flux/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Flux = " << flux(0,0) << endl;
		
		const realvec& Specmean = SerSpecMean->getctrl("Mean/Specmean/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Mean = " << Specmean(0,0) << endl;
		
		const realvec& Specmed = SerSpecMed->getctrl("Median/Specmed/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Median = " << Specmed(0,0) << endl;
		
		//const realvec& mean = SerMean->getctrl("Mean/mean/mrs_realvec/processedData")->to<mrs_realvec>();
		//cout << "Mean = " << mean(0,0) << endl;
		
		const realvec& StdDev = SerStdDev->getctrl("StandardDeviation/stddev/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Standard Deviation = " << StdDev(0,0) << endl;
		
		const realvec& skew = SerSkew->getctrl("Skewness/skewness/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Skewness = " << skew(0,0) << endl;
		
		const realvec& kurtosis = SerKurtosis->getctrl("Kurtosis/kurtosis/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Kurtosis = " << kurtosis(0,0) << endl;
		
		const realvec& power = SerPower->getctrl("Power/power/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Power = " << power(0,0) << endl;
		
		////const realvec& autocor = SerAutoCor->getctrl("AutoCorrelation/autocor/mrs_realvec/processedData")->to<mrs_realvec>();
		////cout << "Spectral AutoCorrelation = " << autocor(0,0) << endl;
		
		const realvec& zerocross = SerZeroCross->getctrl("ZeroCrossings/zerocross/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Zero Crossings rate = " << zerocross(0,0) << endl;
		
		const realvec& rms = SerRms->getctrl("Rms/rms/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "RMS Energy = " << rms(0,0) << endl;
		
		const realvec& amdf = SerAmdf->getctrl("AMDF/amdf/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "AMDF = " << amdf(500) << endl; //1 X 512 vector
		
		const realvec& powspec = SerMFCC->getctrl("PowerSpectrum/powspec/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Power Spectrum = " << powspec(200) << endl; //257 X 1 vector
		
		const realvec& mfcc = SerMFCC->getctrl("MFCC/mfcc/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "MFCC = " << mfcc(12) << endl; //13 X 1 vector
		
		const realvec& lpc = SerLPC->getctrl("LPC/lpc/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "LPC = " << lpc(5) << endl; //12 X 1 vector
		
		const realvec& lpcc = SerLPC->getctrl("LPCC/lpcc/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "LPCC = " << lpcc(5) << endl; //10 X 1 vector
		
		//const realvec& lsp = SerLPC->getctrl("LSP/lsp/mrs_realvec/processedData")->to<mrs_realvec>();
		//cout << "LSP = " << lpc(5) << endl; //12 X 1 vector
		
		////const realvec& maxmin = SerMaxMin->getctrl("MaxMin/maxmin/mrs_realvec/processedData")->to<mrs_realvec>();
		////cout << "Max Value = " << maxmin(0) << endl << "Min Value = " << maxmin(1) << endl;
		
		const realvec& peaks = SerPeaker->getctrl("Peaker/peaker/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Peaks = " << peaks(14) << endl; //1 X 512 vector with some peaks other zeros
		//for(mrs_natural i=0;i<512;i++)
		//	if(peaks(i)>0)
		//		cout<<i<<" "<<peaks(i)<<"\n";
		
		//const realvec& f0 = SerF0analysis->getctrl("F0Analysis/f0analysis/mrs_realvec/processedData")->to<mrs_realvec>();
		//cout << "F0 = " << f0 << endl;
		
		const realvec& chroma = SerChroma->getctrl("Chroma/chroma/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Chroma = " << chroma(5) << endl; // 12 X 1 vector*/
		
		const realvec& energy = SerEnergy->getctrl("Energy/energy/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Energy = " << energy(0,0) << endl;
		
	}
	delete pnet;
}
Exemplo n.º 22
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;
}
void recognize(string sfName)
{
	MarSystemManager mng;
	MarSystem* pnet = mng.create("Series", "pnet");
	MarSystem* Fanout1 = mng.create("Fanout","Fanout1");
	MarSystem* Fanout2 = mng.create("Fanout","Fanout2");
	MarSystem* Spec = mng.create("Series", "Spec");
	MarSystem* SerCentroid = mng.create("Series", "SerCentroid");
	MarSystem* SerRolloff = mng.create("Series", "SerRolloff");
	MarSystem* SerFlux = mng.create("Series", "SerFlux");
	MarSystem* SerSpecMean = mng.create("Series", "SerSpecMean");
	//MarSystem* SerMean = mng.create("Series", "SerMean");		//comming same as spectral Mean
	MarSystem* SerStdDev = mng.create("Series", "SerStdDev");
	MarSystem* SerSkew = mng.create("Series", "SerSkew");
	MarSystem* SerKurtosis = mng.create("Series", "SerKurtosis");
	MarSystem* SerPower = mng.create("Series", "SerPower");
	MarSystem* SerAutoCor = mng.create("Series", "SerAutoCor");
	MarSystem* SerZeroCross = mng.create("Series", "SerZeroCross");
	MarSystem* SerRms = mng.create("Series", "SerRms");
	MarSystem* SerAmdf = mng.create("Series", "SerAmdf");
	MarSystem* SerMFCC = mng.create("Series", "SerMFCC");
	
	// standard network
	pnet->addMarSystem(mng.create("SoundFileSource", "src"));
	pnet->updctrl("SoundFileSource/src/mrs_string/filename", sfName);
	
	Fanout1->addMarSystem(mng.create("AudioSink", "dest"));
	Fanout1->updctrl("AudioSink/dest/mrs_bool/initAudio", true);
	
	Spec->addMarSystem(mng.create("Spectrum","spk"));
	
	SerCentroid->addMarSystem(mng.create("Centroid", "centeroid"));
	SerCentroid->addMarSystem(mng.create("Gain", "g2"));
	
	SerRolloff->addMarSystem(mng.create("Rolloff", "rolloff"));
	SerRolloff->addMarSystem(mng.create("Gain", "g3"));
	
	SerFlux->addMarSystem(mng.create("Flux", "flux"));
	//SerFlux->updctrl("Flux/flux/mrs_bool/reset",true);
	SerFlux->addMarSystem(mng.create("Gain", "g4"));
	
	SerSpecMean->addMarSystem(mng.create("Mean", "Specmean"));
	SerSpecMean->addMarSystem(mng.create("Gain", "g5"));
	
	SerSkew->addMarSystem(mng.create("Skewness", "skewness"));
	SerSkew->addMarSystem(mng.create("Gain", "g7"));
	
	SerKurtosis->addMarSystem(mng.create("Kurtosis", "kurtosis"));
	SerKurtosis->addMarSystem(mng.create("Gain", "g8"));
	
	SerPower->addMarSystem(mng.create("Power", "power"));
	SerPower->addMarSystem(mng.create("Gain", "g8"));
	
	SerAutoCor->addMarSystem(mng.create("AutoCorrelation", "autocor"));
	SerAutoCor->addMarSystem(mng.create("Gain", "g9"));
	
	SerRms->addMarSystem(mng.create("Rms", "rms"));
	SerRms->addMarSystem(mng.create("Gain", "g11"));
	
	SerMFCC->addMarSystem(mng.create("PowerSpectrum", "powspec"));
	SerMFCC->addMarSystem(mng.create("MFCC", "mfcc"));
	SerMFCC->addMarSystem(mng.create("Gain", "g13"));
	
	Fanout2->addMarSystem(SerCentroid);
	Fanout2->addMarSystem(SerRolloff);
	Fanout2->addMarSystem(SerFlux);
	Fanout2->addMarSystem(SerSpecMean);
	Fanout2->addMarSystem(SerSkew);
	Fanout2->addMarSystem(SerKurtosis);
	Fanout2->addMarSystem(SerPower);
	Fanout2->addMarSystem(SerAutoCor);
	Fanout2->addMarSystem(SerRms);
	Fanout2->addMarSystem(SerMFCC);
		
	Spec->addMarSystem(Fanout2);
	
	Fanout1->addMarSystem(Spec);
	
//	SerMean->addMarSystem(mng.create("Mean", "mean"));
//	SerMean->addMarSystem(mng.create("Gain", "g6"));
//	Fanout1->addMarSystem(SerMean);
	
	SerStdDev->addMarSystem(mng.create("StandardDeviation", "stddev"));
	SerStdDev->addMarSystem(mng.create("Gain", "g6"));
	
	SerZeroCross->addMarSystem(mng.create("ZeroCrossings", "zerocross"));
	SerZeroCross->addMarSystem(mng.create("Gain", "g10"));
	
	SerAmdf->addMarSystem(mng.create("AMDF", "amdf"));
	SerAmdf->addMarSystem(mng.create("Gain", "g12"));
	
	Fanout1->addMarSystem(SerStdDev);
	Fanout1->addMarSystem(SerZeroCross);
	Fanout1->addMarSystem(SerAmdf);
	
	pnet->addMarSystem(Fanout1);

	while ( pnet->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() )
	{
		pnet->tick();
		// gets data from the Spectrum for read only!
		const realvec& processedData =
		    Spec->getctrl("Spectrum/spk/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "\nOriginal Spectrum = " << processedData(1) << endl;
		
		const realvec& center = SerCentroid->getctrl("Centroid/centeroid/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Centroid = " << center(0,0) << endl;
		
		const realvec& rolloff = SerRolloff->getctrl("Rolloff/rolloff/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Roll Off = " << rolloff(0,0) << endl;
		
		const realvec& flux = SerFlux->getctrl("Flux/flux/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Flux = " << flux(0,0) << endl;
		
		const realvec& Specmean = SerSpecMean->getctrl("Mean/Specmean/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Mean = " << Specmean(0,0) << endl;
		
		//const realvec& mean = SerMean->getctrl("Mean/mean/mrs_realvec/processedData")->to<mrs_realvec>();
		//cout << "Mean = " << mean(0,0) << endl;
		
		const realvec& StdDev = SerStdDev->getctrl("StandardDeviation/stddev/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Standard Deviation = " << StdDev(0,0) << endl;
		
		const realvec& skew = SerSkew->getctrl("Skewness/skewness/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Skewness = " << skew(0,0) << endl;
		
		const realvec& kurtosis = SerKurtosis->getctrl("Kurtosis/kurtosis/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Kurtosis = " << kurtosis(0,0) << endl;
		
		const realvec& power = SerPower->getctrl("Power/power/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral Power = " << power(0,0) << endl;
		
		const realvec& autocor = SerAutoCor->getctrl("AutoCorrelation/autocor/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Spectral AutoCorrelation = " << autocor(0,0) << endl;
		
		const realvec& zerocross = SerZeroCross->getctrl("ZeroCrossings/zerocross/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Zero Crossings rate = " << zerocross(0,0) << endl;
		
		const realvec& rms = SerRms->getctrl("Rms/rms/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "RMS Energy = " << rms(0,0) << endl;
		
		const realvec& amdf = SerAmdf->getctrl("AMDF/amdf/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "AMDF = " << amdf(500) << endl; //1 X 512 vector
		
		const realvec& powspec = SerMFCC->getctrl("PowerSpectrum/powspec/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "Power Spectrum = " << powspec(200) << endl; //257 X 1 vector
		
		const realvec& mfcc = SerMFCC->getctrl("MFCC/mfcc/mrs_realvec/processedData")->to<mrs_realvec>();
		cout << "MFCC = " << mfcc(12) << endl; //13 X 1 vector
		
	}
	delete pnet;
}
Exemplo n.º 24
0
// Play soundfile given by sfName, playbacknet contains the playback
// network of MarSystem objects
void sftransform(mrs_real gain, string outName)
{


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

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

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

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

  playbacknet->update();

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


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

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

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


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


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

  mrs_real* controls = 0;

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

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

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

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

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

}
int process(string inName, string outName)
{
	MarSystemManager mng;
	// setting up the network
	MarSystem* input = mng.create("Series", "input");

	input->addMarSystem(mng.create("SoundFileSource","src"));
	input->addMarSystem(mng.create("ShiftInput", "si"));

	MarSystem* flowthru = mng.create("FlowThru", "flowthru");
	flowthru->addMarSystem(mng.create("Hamming", "hamAna")); 
	flowthru->addMarSystem(mng.create("LPC", "lpc"));
	input->addMarSystem(flowthru);
	input->addMarSystem(mng.create("Filter", "analysis"));


	MarSystem* audioSink = mng.create("SoundFileSink", "audioSink");


	input->addMarSystem(mng.create("NoiseSource", "ns"));

	//    input->addMarSystem(mng.create("Gain", "nsg1"));
	//  	input->addMarSystem(mng.create("PlotSink", "plot2"));
	input->addMarSystem(mng.create("Filter", "synthesis"));
	input->addMarSystem(mng.create("Gain", "nsg"));
	input->addMarSystem(mng.create("Windowing", "winSyn")); 
	input->addMarSystem(mng.create("OverlapAdd", "ova"));
	input->addMarSystem(audioSink);

	input->updctrl("SoundFileSource/src/mrs_string/filename", inName);
	input->updctrl("SoundFileSource/src/mrs_natural/inSamples", hopSize);

	input->updctrl("ShiftInput/si/mrs_natural/winSize", windowSize);
	input->updctrl("Windowing/winSyn/mrs_string/type", "Hanning");
	//input->updctrl("ShiftOutput/so/mrs_natural/Interpolation", hopSize);

	input->updctrl("FlowThru/flowthru/LPC/lpc/mrs_natural/order",lpcOrder);
	input->updctrl("FlowThru/flowthru/LPC/lpc/mrs_real/lambda",0.0);
	input->updctrl("FlowThru/flowthru/LPC/lpc/mrs_real/gamma",1.0);
	// input->updctrl("LPC/lpc/mrs_natural/featureMode", 0);

	input->linkctrl("Filter/analysis/mrs_realvec/ncoeffs",
					"FlowThru/flowthru/LPC/lpc/mrs_realvec/coeffs");
	input->linkctrl("Filter/synthesis/mrs_realvec/dcoeffs",
					"FlowThru/flowthru/LPC/lpc/mrs_realvec/coeffs");
	// link the power of the error with a gain
	input->linkctrl("Gain/nsg/mrs_real/gain",
					"FlowThru/flowthru/LPC/lpc/mrs_real/power");

	input->updctrl("SoundFileSink/audioSink/mrs_string/filename", outName);
	//input->updctrl("Gain/nsg1/mrs_real/gain", .1);
	input->updctrl("NoiseSource/ns/mrs_string/mode", "truc");

	int i = 0;
	while(input->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
	{
		input->tick();
		i++;
		//	cout << input->getctrl("Filter/analysis/mrs_realvec/ncoeffs")->to<mrs_realvec>() << endl;
		//	cout << input->getctrl("LPC/lpc/mrs_realvec/coeffs")->to<mrs_realvec>() << endl;
	}

	cout << endl << "LPC processing finished!";
	delete input;
	return 0;
}
void textract_trainAccumulator(string sfName, mrs_natural offset, mrs_natural duration, mrs_real start, mrs_real length, mrs_real gain, mrs_natural label, string pluginName, string wekafname, mrs_natural memSize, string extractorStr, TimeLine& tline)
{
	MarSystemManager mng;

	MRSDIAG("sfplay.cpp - sfplay");

	// default 
	if (extractorStr == EMPTYSTRING) 
		extractorStr = "STFT";

	// Find proper soundfile format and create SignalSource 
	MarSystem *src = mng.create("SoundFileSource", "src");

	src->updctrl("mrs_string/filename", sfName);
	src->updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);

	if (tlineName == EMPTYSTRING)
	{
		mrs_natural hops = src->getctrl("mrs_natural/size")->to<mrs_natural>() * src->getctrl("mrs_natural/nChannels")->to<mrs_natural>() / 2048 + 1;
		tline.regular(100, hops);
	}

	MarSystem *dest_;
	dest_ = mng.create("AudioSink", "dest");

	MarSystem *series = mng.create("Series", "playbacknet");
	series->addMarSystem(src);
	series->addMarSystem(dest_);

	series->updctrl("AudioSink/dest/mrs_natural/nChannels", 
		series->getctrl("SoundFileSource/src/mrs_natural/nChannels")->to<mrs_natural>());  

	// Calculate duration, offset parameters if necessary 
	if (start > 0.0f) 
		offset = (mrs_natural) (start 
		* src->getctrl("mrs_real/israte")->to<mrs_real>() 
		* src->getctrl("mrs_natural/nChannels")->to<mrs_natural>());
	if (length != 30.0f) 
		duration = (mrs_natural) (length 
		* src->getctrl("mrs_real/israte")->to<mrs_real>() 
		* src->getctrl("mrs_natural/nChannels")->to<mrs_natural>());

	// accumulate feature vectors over 30 seconds 
	MarSystem* acc = mng.create("Accumulator", "acc");

	acc->updctrl("mrs_natural/nTimes", 100);

	// Calculate windowed power spectrum and then 
	// calculate specific feature sets 
	MarSystem* spectralShape = mng.create("Series", "spectralShape");
	spectralShape->addMarSystem(mng.create("Windowing", "hamming"));
	spectralShape->addMarSystem(mng.create("Spectrum","spk"));
	spectralShape->addMarSystem(mng.create("PowerSpectrum", "pspk"));
	spectralShape->updctrl("PowerSpectrum/pspk/mrs_string/spectrumType","power");  

	// Spectrum Shape descriptors
	MarSystem* spectrumFeatures = mng.create("Fanout", "spectrumFeatures");

	if (extractorStr == "STFT") 
	{
		spectrumFeatures->addMarSystem(mng.create("Centroid", "cntrd"));
		spectrumFeatures->addMarSystem(mng.create("Rolloff", "rlf"));      
		spectrumFeatures->addMarSystem(mng.create("Flux", "flux"));
	}
	else if (extractorStr == "STFTMFCC")
	{
		spectrumFeatures->addMarSystem(mng.create("Centroid", "cntrd"));
		spectrumFeatures->addMarSystem(mng.create("Rolloff", "rlf"));      
		spectrumFeatures->addMarSystem(mng.create("Flux", "flux"));
		spectrumFeatures->addMarSystem(mng.create("MFCC", "mfcc"));
	}
	else if (extractorStr == "MFCC")
		spectrumFeatures->addMarSystem(mng.create("MFCC", "mfcc"));
	else if (extractorStr == "SCF")
		spectrumFeatures->addMarSystem(mng.create("SCF", "scf"));
	else if (extractorStr == "SFM") 
		spectrumFeatures->addMarSystem(mng.create("SFM", "sfm"));

	mng.registerPrototype("SpectrumFeatures", spectrumFeatures->clone());
	spectralShape->addMarSystem(mng.create("SpectrumFeatures", "spectrumFeatures"));
	mng.registerPrototype("SpectralShape", spectralShape->clone());

	//  add time-domain zerocrossings
	MarSystem* features = mng.create("Fanout", "features");
	features->addMarSystem(mng.create("SpectralShape", "SpectralShape"));
	if (extractorStr == "STFT")
		features->addMarSystem(mng.create("ZeroCrossings", "zcrs"));      
	mng.registerPrototype("Features", features->clone());

	// Means and standard deviation (statistics) for texture analysis 
	MarSystem* statistics = mng.create("Fanout","statistics");
	statistics->addMarSystem(mng.create("Mean", "mn"));
	statistics->addMarSystem(mng.create("StandardDeviation", "std"));
	mng.registerPrototype("Statistics", statistics->clone());

	// weka output 
	MarSystem *wsink = mng.create("WekaSink","wsink");

	// Build the overall feature calculation network 
	MarSystem* featureNetwork = mng.create("Series", "featureNetwork");

	featureNetwork->addMarSystem(src->clone());
	featureNetwork->addMarSystem(mng.create("Features", "features"));
	featureNetwork->addMarSystem(mng.create("Memory", "memory"));
	featureNetwork->updctrl("Memory/memory/mrs_natural/memSize", memSize);
	featureNetwork->addMarSystem(mng.create("Statistics", "statistics"));  

	// add network to accumulator
	acc->addMarSystem(featureNetwork->clone());

	// Final network compute 30-second statistics 
	MarSystem* total = mng.create("Series", "total");
	total->addMarSystem(acc->clone());
	total->addMarSystem(mng.create("Statistics", "statistics2"));
	// total->addMarSystem(mng.create("Mean", "mn2"));
	total->addMarSystem(wsink->clone());

	// update controls 
	total->updctrl("mrs_natural/inSamples", MRS_DEFAULT_SLICE_NSAMPLES);
	total->updctrl("Accumulator/acc/Series/featureNetwork/" + src->getType() + "/src/mrs_natural/pos", offset);      

	mrs_natural wc = 0;
	mrs_natural samplesPlayed =0;

	// main loop for extracting the features 
	string className = "";

	total->updctrl("Accumulator/acc/Series/featureNetwork/SoundFileSource/src/mrs_string/filename", sfName);
	wc = 0;  	  
	samplesPlayed = 0;

	total->updctrl("WekaSink/wsink/mrs_natural/nLabels", (mrs_natural)3);
	if (wekafname == EMPTYSTRING) 
		total->updctrl("WekaSink/wsink/mrs_string/filename", "weka2.arff");
	else 
		total->updctrl("WekaSink/wsink/mrs_string/filename", wekafname);  

	// total->tick();

	for (int r = 0; r < tline.numRegions(); r++)
	{
		cout << "start = " << tline.regionStart(r) << endl;
		cout << "end = " << tline.regionEnd(r) << endl;

		total->updctrl("Accumulator/acc/Series/featureNetwork/SoundFileSource/src/mrs_natural/pos", (mrs_natural)tline.regionStart(r) * tline.lineSize());
		total->updctrl("mrs_natural/inSamples", tline.lineSize());
		if (tlineName == EMPTYSTRING)
		{
			if ((tline.regionClass(r) > 0) && (tline.regionClass(r) != 4))//[?]
			{
				total->updctrl("WekaSink/wsink/mrs_natural/label", tline.regionClass(r)-1);
			}
		}
		else
			total->tick();
	}

	if (pluginName == EMPTYSTRING) // output to stdout 
		cout << (*total) << endl;      
	else 
	{
		ofstream oss(pluginName.c_str());
		oss << (*total) << endl;
	}
}
GLWidget::GLWidget(string inAudioFileName, QWidget *parent)
  : QGLWidget(parent)
{
  // // Initialize member variables
  xRot = 30;
  yRot = 0;
  zRot = 0;

  max_data.create(POWERSPECTRUM_BUFFER_SIZE);

  for (int i = 0; i < POWERSPECTRUM_BUFFER_SIZE; i++) {
  	max_data(i) = -999.9;
  }

  // Defaults
  y_scale = 350;

  //
  // Create the MarSystem to play and analyze the data
  // 
  MarSystemManager mng;

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

  MarSystem *accum = mng.create("Accumulator", "accum");
  net->addMarSystem(accum);

  MarSystem *accum_series = mng.create("Series", "accum_series");
  accum->addMarSystem(accum_series);

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

  net->addMarSystem(mng.create("ShiftInput", "si"));
  net->addMarSystem(mng.create("AutoCorrelation", "auto"));

  net->updctrl("Accumulator/accum/mrs_natural/nTimes", 10);

  net->updctrl("Accumulator/accum/Series/accum_series/SoundFileSource/src/mrs_string/filename", inAudioFileName);
  net->updctrl("ShiftInput/si/mrs_natural/winSize", MEMORY_SIZE);

  net->updctrl("mrs_real/israte", 44100.0);
  net->updctrl("Accumulator/accum/Series/accum_series/AudioSink/dest/mrs_bool/initAudio", true);
  // net->updctrl("AudioSink/dest/mrs_bool/initAudio", true);

  // // A Fanout to let us read audio from either a SoundFileSource or an
  // // AudioSource
  // MarSystem* inputfanout = mng.create("Fanout", "inputfanout");
  // net->addMarSystem(inputfanout);

  // inputfanout->addMarSystem(mng.create("SoundFileSource", "src"));
  // inputfanout->addMarSystem(mng.create("AudioSource", "src"));

  // net->addMarSystem(mng.create("Selector", "inputselector"));

  // if (inAudioFileName == "") {
  // 	net->updctrl("Selector/inputselector/mrs_natural/enable", 0);
  // 	net->updctrl("Selector/inputselector/mrs_natural/enable", 1);
  // 	cout << "input from AudioSource" << endl;
  // } else {
  // 	net->updctrl("Selector/inputselector/mrs_natural/enable", 1);
  // 	net->updctrl("Selector/inputselector/mrs_natural/enable", 0);
  // 	cout << "input from SoundFileSource" << endl;
  // }

  // Create and start playing the MarSystemQtWrapper that wraps
  // Marsyas in a Qt thread
  mwr_ = new MarSystemQtWrapper(net);
  mwr_->start();

  if (inAudioFileName == "") {
	mwr_->play();
	play_state = true;
  } else {
	play_state = false;
  }
		    
  // Create some handy pointers to access the MarSystem
  posPtr_ = mwr_->getctrl("Fanout/inputfanout/SoundFileSource/src/mrs_natural/pos");
  initPtr_ = mwr_->getctrl("AudioSink/dest/mrs_bool/initAudio");
  fnamePtr_ = mwr_->getctrl("Fanout/inputfanout/SoundFileSource/src/mrs_string/filename");

  //
  // Create the animation timer that periodically redraws the screen
  //
  QTimer *timer = new QTimer( this ); 
  connect( timer, SIGNAL(timeout()), this, SLOT(animate()) ); 
  timer->start(10); // Redraw the screen every 10ms
  
}
void Grid::setupNetworks() {


    MarSystem* extractNet = mng.create("Series", "extractNet");
    extractNet->addMarSystem(mng.create("SoundFileSource", "src"));
    extractNet->addMarSystem(mng.create("Stereo2Mono", "s2m"));
    MarSystem* spectralNet = mng.create("Series", "spectralNet");
    spectralNet->addMarSystem(mng.create("Windowing", "ham"));
    spectralNet->addMarSystem(mng.create("Spectrum", "spk"));
    spectralNet->addMarSystem(mng.create("PowerSpectrum", "pspk"));

    MarSystem* featureFanout = mng.create("Fanout", "featureFanout");
    featureFanout->addMarSystem(mng.create("Centroid", "centroid"));
    featureFanout->addMarSystem(mng.create("Rolloff", "rolloff"));
    featureFanout->addMarSystem(mng.create("Flux", "flux"));
    featureFanout->addMarSystem(mng.create("MFCC", "mfcc"));

    spectralNet->addMarSystem(featureFanout);
    extractNet->addMarSystem(spectralNet);
    extractNet->addMarSystem(mng.create("Memory", "mem"));

    MarSystem* stats = mng.create("Fanout", "stats");
    stats->addMarSystem(mng.create("Mean", "mn1"));
    stats->addMarSystem(mng.create("StandardDeviation", "std1"));
    extractNet->addMarSystem(stats);

    MarSystem* acc = mng.create("Accumulator", "acc");
    acc->updctrl("mrs_natural/nTimes", 1200);
    acc->addMarSystem(extractNet);

    total_ = mng.create("Series", "total");
    total_->addMarSystem(acc);
    MarSystem* stats2 = mng.create("Fanout", "stats2");
    stats2->addMarSystem(mng.create("Mean", "mn2"));
    stats2->addMarSystem(mng.create("StandardDeviation", "std2"));

    total_->addMarSystem(stats2);
    total_->addMarSystem(mng.create("Annotator", "ann"));
    total_->addMarSystem(mng.create("WekaSink", "wsink"));





    total_->linkctrl("mrs_string/filename",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_string/filename");


    total_->linkctrl("mrs_string/currentlyPlaying",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_string/currentlyPlaying");


    total_->linkctrl("mrs_bool/shuffle",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_bool/shuffle");

    total_->linkctrl("mrs_natural/pos",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/pos");

    total_->linkctrl("mrs_real/repetitions",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_real/repetitions");


    total_->linkctrl("mrs_natural/cindex",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/cindex");

    total_->linkctrl("mrs_natural/numFiles",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/numFiles");

    total_->linkctrl("mrs_string/allfilenames",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_string/allfilenames");

    total_->linkctrl("mrs_natural/numFiles",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/numFiles");


    total_->linkctrl("mrs_bool/hasData",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_bool/hasData");
    total_->linkctrl("mrs_natural/advance",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/advance");

    total_->linkctrl("mrs_bool/memReset",
                     "Accumulator/acc/Series/extractNet/Memory/mem/mrs_bool/reset");

    total_->linkctrl("mrs_natural/label",
                     "Annotator/ann/mrs_natural/label");

    total_->linkctrl("WekaSink/wsink/mrs_natural/nLabels",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_natural/numFiles");
    total_->updctrl("WekaSink/wsink/mrs_natural/downsample", 1);


    total_->linkctrl("WekaSink/wsink/mrs_string/labelNames",
                     "Accumulator/acc/Series/extractNet/SoundFileSource/src/mrs_string/allfilenames");

    total_->updctrl("mrs_natural/inSamples", 512);
    total_->updctrl("mrs_real/repetitions", 1.0);

    // Playback network
    pnet_ = mng.create("Series", "pnet_");
    pnet_->addMarSystem(mng.create("SoundFileSource", "src"));
    // pnet_->addMarSystem(mng.create("Stereo2Mono", "s2m"));
    pnet_->addMarSystem(mng.create("Gain", "gain"));
    pnet_->addMarSystem(mng.create("AudioSink", "dest"));
    pnet_->linkctrl("mrs_bool/hasData","SoundFileSource/src/mrs_bool/hasData");


    mwr_ = new MarSystemQtWrapper(pnet_);
    filePtr_ = mwr_->getctrl("SoundFileSource/src/mrs_string/filename");

    mwr_->start();

}
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;
}