// Information on the quantity of data available for each client
// Outputs a list with the selected files for a defined quantity of data
int ExtractTargetDataInfo(Config& config)
{
	String inputClientListFileName = config.getParam("targetIdList");
	bool fixedLabelSelectedFrame;
	String labelSelectedFrames;
	if (config.existsParam("useIdForSelectedFrame"))      // the ID of each speaker is used as labelSelectedFrame
		fixedLabelSelectedFrame=false;
	else{                                                // the label is decided by the command line and is unique for the run
		labelSelectedFrames=config.getParam("labelSelectedFrames");
		if (verbose) cout << "Computing on" << labelSelectedFrames << " label" << endl;
		fixedLabelSelectedFrame=true;
	}
	unsigned long maxFrame=config.getParam("maxFrame").toLong();
	String outputFilename=config.getParam("outputFilename");
	
	
	ofstream outputFile(outputFilename.c_str(),ios::out| ios::trunc);
	try{
		XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
		XLine * linep;
		if (verbose) cout << "InfoTarget" << endl;
		// *********** Target loop *****************
		while ((linep=inputClientList.getLine()) != NULL){             // linep gives the XLine with the Id of a given client and the list of files
			String *id=linep->getElement();                              // Get the Client ID (id)
			outputFile<<*id;
			String currentFile="";
			XLine featureFileListp=linep->getElements();	           // Get the list of feature file for the client (end of the line)
			if (verbose) cout << "Info model ["<<*id<<"]"<<endl;
			if (!fixedLabelSelectedFrame){                                // the ID is used as label for selecting the frame
				labelSelectedFrames=*id;
				if (debug) cout <<*id<<" is used for label selected frames"<<endl;
			}
			// label files reading - It creates, for each file and each label, a cluster of segments - will be integrated witth the featre s - asap
			SegServer segmentsServer;                                    // Reading the segmentation files for each feature input file
			LabelServer labelServer;
			initializeClusters(featureFileListp,segmentsServer,labelServer,config);           // Reading the segmentation files for each feature input file
			unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);            // Get the index of the cluster with in interest audio segments
			SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments
			Seg *seg;                                                                  // Will give the current segment
			unsigned long frameCount=0;
			selectedSegments.rewind();                                                 // at the begin of the selected segments list
			while(((seg=selectedSegments.getSeg())!=NULL) && (frameCount<maxFrame)){   // For each of the selected segments until the amount of data is get
				frameCount+=seg->length();
				cout << seg->sourceName()<<" "<<seg->begin()<<" "<<seg->length()<<" Total time="<<frameCount<<endl;
				if (seg->sourceName()!=currentFile){
					outputFile<<" "<<seg->sourceName();
					currentFile=seg->sourceName();
				}
			}                                                                          // end of the initial Train Iteration loop
			outputFile<<endl;
			if (verbose) cout << "Save info client ["<<*id<<"]" << endl;
		}                                                                            // end of the the target loop
	} // fin try
	
	catch (Exception& e)
	{
		cout << e.toString().c_str() << endl;
	}
	return 0;
}
Пример #2
0
void FactorAnalysisStat::computeAndAccumulateGeneralFAStats(FeatureServer &fs,Config & config){
	SegServer segmentsServer;
	LabelServer labelServer;
	initializeClusters(fileList,segmentsServer,labelServer,config);
	verifyClusterFile(segmentsServer,fs,config);
	unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(config.getParam("labelSelectedFrames"));
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); 
	this->computeAndAccumulateGeneralFAStats(selectedSegments,fs,config);
};
// Can use this function to get likelihood with a topgauss
double TopGauss::get(MixtureGD & UBM,FeatureServer &fs,String & featureFilename,Config & config){
	StatServer ss(config);
	String labelSelectedFrames =config.getParam("labelSelectedFrames");
	unsigned long begin=fs.getFirstFeatureIndexOfASource(featureFilename);
	fs.seekFeature(begin);
	SegServer segmentsServer;
	LabelServer labelServer;
	initializeClusters(featureFilename,segmentsServer,labelServer,config);
	//	__android_log_print(ANDROID_LOG_DEBUG, "TopGauss::get", " Feature file  %s  \n", featureFilename.c_str());

	verifyClusterFile(segmentsServer,fs,config);
	unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);	
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);  
	MixtureGDStat &acc=ss.createAndStoreMixtureStat(UBM);
	
	Seg *seg;          // current selected segment
	selectedSegments.rewind();		
	unsigned long t=0; //cnt frames
	acc.resetLLK();
	unsigned long idxBegin=0;
	while((seg=selectedSegments.getSeg())!=NULL){  
		unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); 
		fs.seekFeature(begin);
		Feature f;
		idxBegin=this->frameToIdx(t);
		for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
			fs.readFeature(f); 
			//unsigned long idx=this->frameToIdx(t);
			unsigned long nbg=_nbg[t];	
			ULongVector index;
			double sumNonSelectedWeights=_snsw[t];
			double sumNonSelectedLLK=_snsl[t];
			for (unsigned long i=0;i<nbg;i++) {
				index.addValue(_idx[idxBegin+i]);
			}		
			char c[100];
			sprintf(c,"%d",(int)index.size());
			config.setParam("topDistribsCount",c); // this should be high enough	
			if (t==0) {acc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);acc.resetLLK();} // to remove in ALIZE, this is to init the LKvector
			ss.setTopDistribIndexVector(index, sumNonSelectedWeights, sumNonSelectedLLK);
			acc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
			idxBegin+=nbg;
			t++;
		}	
	}	
	//ss.deleteMixtureStat(acc);
	if (t!=_nt || idxBegin !=_nbgcnt) cout << "W: t("<<t<<") != _nt(" <<_nt<<")"<<"W: idxBegin("<<idxBegin<<") != _nbgcnt(" <<_nbgcnt<<")"<<endl;
return acc.getMeanLLK();
}
Пример #4
0
void featureStream(Config &config,String filename,FeatureServer *&fs,SegServer *&segServ,SegCluster *&segCluster,String labelSelectedFrames){
  fs=new FeatureServer(config,filename);
  try{   
  	// TODO Test if the stream is not empty
  	
  	segServ=new SegServer;                                                                      // Create the segment server for managing the segments/clusters
  	LabelServer labelServer;                                                                    // Create the label server, for indexing the segments/clusters
 	initializeClusters(filename,*segServ,labelServer,config);                                   // Reading the segmentation files for each feature input file
 	verifyClusterFile(*segServ,*fs,config);                                                     // Verify if the segments ending before the end of the feature files...
	unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);     // Get the index of the cluster with in interest audio segments
 	segCluster=&(segServ->getCluster(codeSelectedFrame));
  }
  catch (Exception& e){
    cout << e.toString() << endl;
  }
}
void cms(String & featureFileName,FeatureServer &fs,Config &config) {
        unsigned long begin=fs.getFirstFeatureIndexOfASource(featureFileName);
	fs.seekFeature(begin);
	SegServer segmentsServer;
	LabelServer labelServer;
	initializeClusters(featureFileName,segmentsServer,labelServer,config);
	verifyClusterFile(segmentsServer,fs,config);
	unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(config.getParam("labelSelectedFrames"));
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);  
	selectedSegments.rewind();  
	RealVector <double> mean,cov;
	FrameAccGD frameAccu;
	frameAccu.reset();   
	accumulateStatFrame(frameAccu,fs, selectedSegments, config);
	mean = frameAccu.getMeanVect();     // Get the mean vector
	cov  = frameAccu.getStdVect();      // Get the std vector
	computeZeroOne(mean,cov,fs, selectedSegments, config);	
}
Пример #6
0
int saveApost(Config &config)
{

 bool writeAllFeature=true; // Output a vector for all input vectors (selected and not selected vectors) - DEFAULT=on
 if (config.existsParam("writeAllFeatures")) writeAllFeature=config.getParam("writeAllFeatures").toBool();    // Define if all the feature     (selected or not) should be written

	String modelname = config.getParam("inputModelFilename");
	  String inputFeatureFileName =config.getParam("inputFeatureFilename");          // input feature - could be a simple feature file or a list of filenames
        XLine inputFeatureFileNameList;                                                // The (feature) input filename list
        if (inputFeatureFileName.endsWith(".lst")){                                   // If the file parameter is the name of a XList file
	   XList inputFileNameXList(inputFeatureFileName,config);                   // Read the filename list file
           inputFeatureFileNameList=inputFileNameXList.getAllElements();            // And put the filename in a list if the file is a list of feature filenames
			      }
      else {                                                                         // It was a simple feature file and not a filename list
	          inputFeatureFileNameList.addElement(inputFeatureFileName);                   // add the filename in the list
		    }

	try{

        // read UBM 
        MixtureServer _ms(config);
	StatServer _ss(config);
        _ms.loadMixtureGD(config.getParam("inputWorldFilename"));
        MixtureGD & UBM=_ms.getMixtureGD((unsigned long) 0);
        MixtureGDStat &acc=_ss.createAndStoreMixtureStat(UBM);

	unsigned long _vsize=UBM.getVectSize();
	unsigned long _mixsize=UBM.getDistribCount();
        // Loop over the list of feature files
	String *file;
	String labelSelectedFrames;
        unsigned long codeSelectedFrame;
	while ((file=inputFeatureFileNameList.getElement())!= NULL){         
	String & featureFilename=(*file);

	FeatureServer fs(config,featureFilename);
	FeatureServer fs_out(config,featureFilename);
        SegServer segmentsServer;
        LabelServer labelServer;
        initializeClusters(featureFilename,segmentsServer,labelServer,config);
        verifyClusterFile(segmentsServer,fs,config);
	labelSelectedFrames=config.getParam("labelSelectedFrames");
        codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
        SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); 

	// Compute Occupations and Statistics
        acc.resetOcc();
        Seg *seg;
        selectedSegments.rewind();
        String currentSource="";
        while((seg=selectedSegments.getSeg())!=NULL){
                unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());    // Idx of the first frame of the current file in the feature server
                if (currentSource!=seg->sourceName()) {
                currentSource=seg->sourceName();
                if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl;
                }

                fs.seekFeature(begin);
                Feature f;

	for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
                                fs.readFeature(f);
                                acc.computeAndAccumulateOcc(f);
                                RealVector <double> aPost=acc.getOccVect();

				Feature tmpF;
				for(unsigned long k=0;k<_mixsize;k++) {
				tmpF[k]=aPost[k];
				}

                                fs_out.addFeature(f);
}

}

// Writing apost probabilities to file 

	cout << "Writing to: " << featureFilename << endl;
		        FeatureFileWriter w(featureFilename, config);   // build a featurefile writer to output the features (real features)
			SegServer fakeSegServer;
		        if (writeAllFeature) {                  // Output all the features- feature count id the same SegServer fakeSegServer;                                          // Create a new fake segment server
		            fakeSegServer.createCluster(0);       // Create a new cluster
		            SegCluster& fakeSeg=fakeSegServer.getCluster(0);    // Get the cluster               
		            fakeSeg.add(fakeSegServer.createSeg(0,fs_out.getFeatureCount(),codeSelectedFrame, labelSelectedFrames,featureFilename));            // Add a segment with all the features
		            outputFeatureFile(config,fs_out,fakeSeg,w);   // output all the features - giving the same file length
		        }
		        else
		            outputFeatureFile(config,fs_out,selectedSegments, w);    // Output only the selected features - giving a shorter output 


}


	}	
	catch (Exception& e){cout << e.toString().c_str() << endl;}
return 0;
}
//---------------------------------------------------------------------------------------------------------------
// Energy detector
// TAKE CARE !!!! INPUT DATA IS A PARAMETER FILE WITH ONLY THE ENERGY AS THE COEFF 0
// Use the featureMask ALIZE option for selecting the ernegy
SegCluster&  energyDetector(Config& config,SegServer &segServer,String &featureFileName)
{
  int nbTrainIt = config.getParam("nbTrainIt").toLong();	         // number of train it
  String thresholdMode;                                                  // Select the mode for the threshold (default=weight)
  if (config.existsParam("thresholdMode"))                               // weight -> select wh(+alpha*wm) % of the frames 
    thresholdMode= config.getParam("thresholdMode");                     //     (wh is the weight of the highest comp, wm, the weight of the midle component
  else thresholdMode="meanStd";                                          // meanStd -> the threshold is mean of the highest comp - alpha*Std of the same comp
  double alpha = config.getParam("alpha").toDouble();                    // alpha is the percentage of central gaussian framess taken into account
  double flooring = config.getParam("varianceFlooring").toDouble();      // Variance flooring and ceiling 
  double ceiling  = config.getParam("varianceCeiling").toDouble();  
  String labelSelectedFrames = config.getParam("labelSelectedFrames");   // Label of the selected frames/segments in the input file
  String labelOutput = config.getParam("labelOutputFrames");             // Label of the selected frames in the output file
  
  if (verbose) cout << "Proceeding Energy based silence detection for ["<<featureFileName<<"]"<<endl;
  FeatureServer fs(config,featureFileName);                            // Reading the feature file and create a feature server to deal with the data
  LabelServer labelServer;                                                              // Create the lable server, for indexing the segments/clusters
  initializeClusters(featureFileName,segServer,labelServer,config);                     // Reading the segmentation files for each feature input file
  unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);            // Get the index of the cluster with in interest audio segments    
  SegCluster& selectedSegments=segServer.getCluster(codeSelectedFrame);   // Gives the cluster of the selected/used segments   
  //FrameAcc energyAccu(fs.getVectSize());
  //computeZeroOneOnACluster(energyAccu, fs, selectedSegments, config);
  unsigned long codeOutput=segServer.getClusterCount();
  segServer.createCluster(codeOutput);                                    // Create a new cluster for the output segments
  SegCluster& outputSeg=segServer.getCluster(codeOutput);                 // Get the cluster  
  MixtureServer ms(config);                                          // Create a mixture server in order to build an energy model
  StatServer ss(config,ms);                                          // Create a statistic server for model learning
  
  FrameAccGD globalFrameAcc;                                         // Create a frame accumulator for mean/cov computation
  globalMeanCov (fs,selectedSegments,globalFrameAcc,config);         // Compute the global mean and cov on the input segments
  
  DoubleVector globalMean=globalFrameAcc.getMeanVect();              // Get the Mean
  DoubleVector globalCov=globalFrameAcc.getCovVect();                // Get the Cov
  if (verboseLevel>1){
	cout <<"global mean and cov"<<endl;
	for (unsigned i=0; i < fs.getVectSize(); i++)cout << "mean[" << i << "=" << globalMean[i] << "]\tcov[" << globalCov[i] << "]" << endl;
  }
  MixtureGD & energyModel=ms.createMixtureGD();	               // Creating the energy model
  //  energyMixtureInit(ms,ss,fs,energyModel,selectedSegments,globalCov,config); // Init the energy model with randomly picked data 
  energyMixtureInit(ms, ss, fs,energyModel,selectedSegments,globalCov,config);  // Fixed init
  if (verboseLevel>1)plotEnergyDistrib(energyModel);
  MixtureStat &emAcc=ss.createAndStoreMixtureStat(energyModel);
  for (int trainIt=0; trainIt<nbTrainIt; trainIt++){                 // Training It loop
    emAcc.resetEM();                                                 // EM stuff
    double llkPreviousIt=accumulateStatEM(ss,fs,emAcc,selectedSegments,config); // Accumulate EM statistics 
    energyModel = emAcc.getEM();                                     // Get the EM estimate
    varianceControl(energyModel,flooring,ceiling,globalCov);         // Apply the variance normalisation		  	  
    if (verbose) cout << "Partial Train it["<<trainIt-1 <<"] (-1 means initial partial it) LLK="<<llkPreviousIt<<" Nb Frames="
		      <<emAcc.getEMFeatureCount()<<endl;
    if (verboseLevel>1)plotEnergyDistrib(energyModel);
  } 
  unsigned long nbInitialFrames= (unsigned long) emAcc.getEMFeatureCount(); // TODO, REPLACE THIS BY A COUNT OF selectedSegments duration - 
  double threshold=0;                                                  // The Energy threshold 
  unsigned long higher=findMaxEnergyDistrib(energyModel);	    
  if (thresholdMode=="weight"){                                      // The selection is based on the weight of the highest energy component
    double selectedWeight=energyModel.weight(higher);
    if  (energyModel.getDistribCount()== 3){
      unsigned long lower=findMinEnergyDistrib(energyModel);
      unsigned long middle=3-(higher+lower);
      double lossH=likelihoodLoss(energyModel.getDistrib(middle),energyModel.weight(middle),
				  energyModel.getDistrib(higher),energyModel.weight(higher));
      double lossL=likelihoodLoss(energyModel.getDistrib(middle),energyModel.weight(middle),
				  energyModel.getDistrib(lower),energyModel.weight(lower));
      if (verbose) cout  << "lossL["<<lossL<<"] lossH["<<lossH<<"]"<<endl;
      if (lossH<lossL)
	selectedWeight+=alpha*energyModel.weight(middle);		  
    }
    threshold=computeEnergyThreshold(fs,selectedWeight);		
  }
  else if (thresholdMode=="meanStd"){                                     // the Energy threshold is the mean of the highest energy component minus alpha*std 
    threshold=energyModel.getDistrib(higher).getMean(0) - (alpha*sqrt(energyModel.getDistrib(higher).getCov(0)));
  }
  else cout << thresholdMode<< "unknown"<<endl;
  unsigned long nbCurrentFrames=selectFrames(fs,segServer,threshold,selectedSegments,outputSeg,labelOutput,featureFileName); // Build the segments with the energized frames
  if (verbose){
    double selected=(float) nbCurrentFrames/ (float)nbInitialFrames;
    cout <<"File ["<<featureFileName<<"]	Number of initial frames ["<<nbInitialFrames<<"]	Number of selected frames ["
	 <<nbCurrentFrames << "]	percentage selected [" << (int) (selected*100) << "]" << endl;
  }
  return outputSeg;
}
//-------------------------------------------------------------------------
int labelNGram(Config& config)
{
  if (config.existsParam("debug"))debug=true; else debug=false;  
  if (config.existsParam("verbose"))verbose=true; else verbose=false;
  String extOutputLabel=".sym.lbl";                                               // the extension of the output files    
  if (config.existsParam("saveLabelFileExtension")) extOutputLabel=config.getParam("saveLabelFileExtension");   
  String pathOutput="./";                                                // the path of the output files    
  if (config.existsParam("labelOutputPath")) pathOutput=config.getParam("labelOutputPath");    
  String extSymbol=".sym";                                               // the extension of the symbol files   
  if (config.existsParam("symbolFileExtension")) extSymbol=config.getParam("symbolFileExtension");   
  String pathSymbol="./";   
  if (config.existsParam("symbolPath")) pathSymbol=config.getParam("symbolPath");   
  String formatSymbol="ascii";   
  if (config.existsParam("symbolFormat")) pathSymbol=config.getParam("symbolFormat");  
 
  String NGramFilename=config.getParam("NGramFilename");                                        
  unsigned long NGramOrder=3;
  if (config.existsParam("NGramOrder")) NGramOrder=config.getParam("NGramOrder").toLong();  
  unsigned long NGramSelected=16;
  if (config.existsParam("NGramSelected")) NGramSelected=config.getParam("NGramSelected").toLong();  
  NGram NGramTable(NGramOrder,NGramSelected);
  NGramTable.load(NGramFilename,config);                       // Load the NGRAM table, selecting the NGramSelected first
 
  String inputFilename=config.getParam("inputFilename");
  String labelSelectedFrames=config.getParam("labelSelectedFrames");
  XLine inputFileList;
  try{
    if (inputFilename.endsWith(".lst")){ // input is file containing a list of filenames
      XList tmp(inputFilename,config);
      inputFileList=tmp.getAllElements();
    }
    else inputFileList.addElement(inputFilename); // a single filename
    String *p;
    while ((p=inputFileList.getElement())){
      String& filename=*p;
      if (verbose)
	cout <<"labelNGram file["<<filename<<"] Table["<<NGramFilename<<"] Order["<<NGramOrder<<"] Selected["<<NGramSelected<<"]"<<endl;
      SegServer segServer;                
      LabelServer labelServer;
      loadClusterFile(filename,segServer,labelServer,config);
      long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);       // Get the index of the selected cluster
      if (codeSelectedFrame==-1){                                                             // No data for this model !!!!!!!!!!!!!!
      cout << " WARNING - NO DATA with the label["<<labelSelectedFrames<<"] in file ["<<filename<<"]"<<endl;
      exit(0);
      }
      SegCluster& cluster=segServer.getCluster(codeSelectedFrame);                                   // Gives the cluster of the selected/used segments
      ULongVector tabS;
      unsigned long nbSym=loadSymbol(pathSymbol+filename+extSymbol,formatSymbol,tabS,config);        // Read the stream of symbols
      SegServer segServerOutput;
      SegCluster& clusterOut=segServerOutput.createCluster(0,labelSelectedFrames,cluster.sourceName());  
      //  
      computeLabelNGram(NGramTable,cluster,clusterOut,tabS,nbSym);
      //
    
      if (verbose){
	cout <<"File["<<filename<<"]" <<endl;
	cout << "Output the new label file in ["<<pathOutput+filename+extOutputLabel <<"]"<<endl;
      }
      outputLabelFile(clusterOut,pathOutput+filename+extOutputLabel,config);
    } // end file loop
  } // fin try
  
  
  catch (Exception& e)
    { 
      cout << e.toString().c_str() << endl;
    }
  return 0;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
int TrainTargetLFA(Config& config)
{
	String inputClientListFileName = config.getParam("targetIdList");
	String inputWorldFilename = config.getParam("inputWorldFilename");
	String outputSERVERFilename = "";
	if (config.existsParam("mixtureServer")) outputSERVERFilename =config.getParam("mixtureServer");
	bool initByClient=false;                                              // In this case, the init model is read from the file
	if (config.existsParam("initByClient")) initByClient=config.getParam("initByClient").toBool();
	bool saveEmptyModel=false;
	if (config.existsParam("saveEmptyModel")) saveEmptyModel=config.getParam("saveEmptyModel").toBool();
	// label for selected frames - Only the frames associated with this label, in the label files, will be used
	bool fixedLabelSelectedFrame=true;
	String labelSelectedFrames;
	if (config.existsParam("useIdForSelectedFrame"))    // the ID of each speaker is used as labelSelectedFrame ?
		fixedLabelSelectedFrame=(config.getParam("useIdForSelectedFrame").toBool()==false);  
	if (fixedLabelSelectedFrame)                        // the label is decided by the command line and is unique for the run
		labelSelectedFrames=config.getParam("labelSelectedFrames");
	bool modelData=false;
	if (config.existsParam("useModelData")) modelData=config.getParam("useModelData").toBool();
	String initModelS=inputWorldFilename;
	if (modelData) if (config.existsParam("initModel")) initModelS=config.getParam("initModel"); // Use a specific model for Em init
	bool outputAdaptParam=false;
	if (config.existsParam("superVectors")) outputAdaptParam=true;
 
try{
	XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
	XLine * linep;
	inputClientList.getLine(0);
	MixtureServer ms(config);
	StatServer ss(config, ms);
	if (verbose) cout << "(TrainTarget) Latent Factor Analysis - Load world model [" << inputWorldFilename<<"]"<<endl;
	MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);      
	if (verbose) cout <<"(TrainTarget) Use["<<initModelS<<"] for initializing EM"<<endl;
	
	//LOAD JFA MAtrices
	unsigned long svsize=world.getDistribCount()*world.getVectSize();
	Matrix<double> U, V; 
	DoubleVector D(svsize,svsize);
	
	//Initialise EC matrix
	if(config.existsParam("eigenChannelMatrix")){
		String uName = config.getParam("matrixFilesPath") + config.getParam("eigenChannelMatrix") + config.getParam("loadMatrixFilesExtension");
 		U.load (uName, config);
		if (verboseLevel >=1) cout << "(TrainTargetLFA) Init EC matrix from "<< config.getParam("eigenChannelMatrix") <<"  from EigenChannel Matrix: "<<", rank: ["<<U.rows() << "] sv size: [" << U.cols() <<"]"<<endl;
	}
	else{
		U.setDimensions(1,svsize);
		U.setAllValues(0.0);
		if (verboseLevel >1) cout << "(TrainTargetLFA) Init EC matrix to 0"<<endl;
	}
	
	V.setDimensions(1,svsize);
	V.setAllValues(0.0);
	if (verboseLevel >=1) cout << "(TrainTargetLFA) Init EV matrix to 0"<<endl;

	//Initialise the D matrix for MAP adaptation
	for(unsigned long i=0; i<world.getDistribCount(); i++){
		for(unsigned long j = 0; j<world.getVectSize(); j++){
			D[i*world.getVectSize()+j] = sqrt(1.0/(world.getDistrib(i).getCovInv(j)*config.getParam("regulationFactor").toDouble()));
		}
	}

	// *********** Target loop ***************** 
	while ((linep=inputClientList.getLine()) != NULL){             	// linep gives the XLine with the Id of a given client and the list of files

		String *id=linep->getElement();                              		// Get the Client ID (id)
		XLine featureFileListp=linep->getElements();	           	// Get the list of feature file for the client (end of the line)
		if (verbose) cout << "(TrainTargetLFA) Train model ["<<*id<<"]"<<endl;
	
		XList ndx; ndx.addLine() = featureFileListp;
		JFAAcc jfaAcc(ndx,config,"TrainTarget");
		
		//Charger les matrices V, U et D a partir des objets matrice existant.
		jfaAcc.loadEV(V, config); jfaAcc.loadEC(U, config); jfaAcc.loadD(D);  

		//Initialise VU matrix
		jfaAcc.initVU();

		FeatureServer fs(config,featureFileListp);											// Reading the features (from several files)
		SegServer segmentsServer;															// Create the segment server for managing the segments/clusters
		LabelServer labelServer;															// Create the lable server, for indexing the segments/clusters
		initializeClusters(featureFileListp,segmentsServer,labelServer,config);				// Reading the segmentation files for each feature input file
		verifyClusterFile(segmentsServer,fs,config);                                     	// Verify if the segments ending before the end of the feature files...

		MixtureGD & adaptedMixture = ms.duplicateMixture(world,DUPL_DISTRIB);               // Creating final as a copy of the world model
		MixtureGD & clientMixture= ms.duplicateMixture(world,DUPL_DISTRIB);
		long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);   	// Get the index of the cluster with in interest audio segments
		if (codeSelectedFrame==-1){                                                         	// No data for this model !!!!!!!!!!!!!!
			cout << " WARNING - NO DATA FOR TRAINING ["<<*id<<"]";
			if (saveEmptyModel){
				cout <<" World model is returned"<<endl;                                    // In this case, the client model is the world model
				if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
				adaptedMixture.save(*id, config);                                           // Save the client model
			}
		}			

		else{
			SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments                                   

			//Compute the JFA statistics
			jfaAcc.computeAndAccumulateJFAStat(selectedSegments,fs,config);

			//Estimate X and Y in one time for each speaker
			jfaAcc.storeAccs();
			jfaAcc.estimateVUEVUT(config);
			jfaAcc.estimateAndInverseL_VU(config);
			jfaAcc.substractMplusDZ(config);
			jfaAcc.estimateYX();
			//Reinitialise the accumulators
			jfaAcc.resetTmpAcc();
			jfaAcc.restoreAccs();
			
			//Split X and Y estimates
			jfaAcc.splitYX();

			//Substract speaker and channel statistics M + VUYX
			jfaAcc.substractMplusVUYX();		
			//Estimate Z for each speaker
			double tau = config.getParam("regulationFactor").toLong();
			jfaAcc.estimateZMAP(tau);
			//Reinitialise the accumulators
			jfaAcc.resetTmpAcc();
			jfaAcc.restoreAccs();

			bool varAdapt = false;
			if((config.existsParam("varAdapt")) && ( config.getParam("varAdapt").toBool() )){
				varAdapt = true;
			}

			DoubleVector clientModel(jfaAcc.getSvSize(), jfaAcc.getSvSize());
			clientModel.setSize(jfaAcc.getSvSize());

			jfaAcc.getMplusVYplusDZ(clientModel, 0);
			
			//Create the ClientMixture
			svToModel(clientModel, clientMixture);
			clientMixture.save(*id, config);

			long tid=ms.getMixtureIndex(*id);
			ms.deleteMixtures(tid,tid);
			ms.deleteUnusedDistribs();
		}
	}
} // fin try
catch (Exception& e) {cout << e.toString().c_str() << endl;}
return 0;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
int TrainTargetJFA(Config& config)
{
	String inputClientListFileName = config.getParam("targetIdList");
	String inputWorldFilename = config.getParam("inputWorldFilename");
	String outputSERVERFilename = "";
	if (config.existsParam("mixtureServer")) outputSERVERFilename =config.getParam("mixtureServer");
	bool initByClient=false;                                              // In this case, the init model is read from the file
	if (config.existsParam("initByClient")) initByClient=config.getParam("initByClient").toBool();
	bool saveEmptyModel=false;
	if (config.existsParam("saveEmptyModel")) saveEmptyModel=config.getParam("saveEmptyModel").toBool();
	// label for selected frames - Only the frames associated with this label, in the label files, will be used
	bool fixedLabelSelectedFrame=true;
	String labelSelectedFrames;
	if (config.existsParam("useIdForSelectedFrame"))    // the ID of each speaker is used as labelSelectedFrame ?
		fixedLabelSelectedFrame=(config.getParam("useIdForSelectedFrame").toBool()==false);  
	if (fixedLabelSelectedFrame)                        // the label is decided by the command line and is unique for the run
		labelSelectedFrames=config.getParam("labelSelectedFrames");
	bool modelData=false;
	if (config.existsParam("useModelData")) modelData=config.getParam("useModelData").toBool();
	String initModelS=inputWorldFilename;
	if (modelData) if (config.existsParam("initModel")) initModelS=config.getParam("initModel"); // Use a specific model for Em init
	bool outputAdaptParam=false;
	if (config.existsParam("superVectors")) outputAdaptParam=true;
 
try{
	XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
	XLine * linep;
	inputClientList.getLine(0);
	MixtureServer ms(config);
	StatServer ss(config, ms);
	if (verbose) cout << "(TrainTarget) Joint Factor Analysis - Load world model [" << inputWorldFilename<<"]"<<endl;
	MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);      
	if (verbose) cout <<"(TrainTarget) Use["<<initModelS<<"] for initializing EM"<<endl;
	
	//LOAD JFA MAtrices
	Matrix<double> U, V; 
	DoubleVector D;
	
	//Initialise EC matrix
	if(config.existsParam("eigenChannelMatrix")){
		String uName = config.getParam("matrixFilesPath") + config.getParam("eigenChannelMatrix") + config.getParam("loadMatrixFilesExtension");
 		U.load (uName, config);
		if (verboseLevel >=1) cout << "(TrainTargetJFA) Init EC matrix from "<< config.getParam("eigenChannelMatrix") <<"  from EigenChannel Matrix: "<<", rank: ["<<U.rows() << "] sv size: [" << U.cols() <<"]"<<endl;
	}
	else{
		unsigned long sS = world.getVectSize() * world.getDistribCount();
		U.setDimensions(1,sS);
		U.setAllValues(0.0);
		if (verboseLevel >=1) cout << "(TrainTargetJFA) Init EC matrix to 0"<<endl;
	}
	
	//Initialise EV matrix
	if(config.existsParam("eigenVoiceMatrix")){
		String vName = config.getParam("matrixFilesPath") + config.getParam("eigenVoiceMatrix") + config.getParam("loadMatrixFilesExtension");
		V.load (vName, config);
		if (verboseLevel >=1) cout << "(TrainTargetJFA) Init EV matrix from "<< config.getParam("eigenVoiceMatrix") <<"  from EigenVoice Matrix: "<<", rank: ["<<V.rows() << "] sv size: [" << V.cols() <<"]"<<endl;
	}
	else{
		unsigned long sS = world.getVectSize() * world.getDistribCount();
		V.setDimensions(1,sS);
		V.setAllValues(0.0);
		if (verboseLevel >=1) cout << "(TrainTargetJFA) Init EV matrix to 0"<<endl;
	}
	
	//Initialise D matrix
	if(config.existsParam("DMatrix")){
		String dName = config.getParam("matrixFilesPath") + config.getParam("DMatrix") + config.getParam("loadMatrixFilesExtension");
		Matrix<double> tmpD(dName, config);
		
		if( (tmpD.rows() != 1) || ( tmpD.cols() != world.getVectSize()*world.getDistribCount() ) ){
			throw Exception("Incorrect dimension of D Matrix",__FILE__,__LINE__);
		}
		else{
			D.setSize(world.getVectSize()*world.getDistribCount());
			D.setAllValues(0.0);
			for(unsigned long i=0; i<world.getVectSize()*world.getDistribCount(); i++){
				D[i] = tmpD(0,i);
			}
			if (verboseLevel >=1) cout << "(TrainTargetJFA) Init D matrix from "<<config.getParam("DMatrix")<<endl;
		}
	}
	else{
		unsigned long sS = world.getVectSize() * world.getDistribCount();
		D.setSize(sS);
		D.setAllValues(0.0);
		if (verboseLevel >1) cout << "(TrainTargetJFA) Init D matrix to 0"<<endl;
	}
	
	// *********** Target loop ***************** 
	while ((linep=inputClientList.getLine()) != NULL){             	// linep gives the XLine with the Id of a given client and the list of files

		String *id=linep->getElement();                              		// Get the Client ID (id)
		XLine featureFileListp=linep->getElements();	           	// Get the list of feature file for the client (end of the line)
		if (verbose) cout << "(TrainTarget) Train model ["<<*id<<"]"<<endl;
	
		XList ndx; ndx.addLine() = featureFileListp;
		JFAAcc jfaAcc(ndx,config,"TrainTarget");

		//Load V, U and D from existing matrices.
		jfaAcc.loadEV(V, config); jfaAcc.loadEC(U, config); jfaAcc.loadD(D);  

		//Initialize VU matrix
		jfaAcc.initVU();

		FeatureServer fs(config,featureFileListp);                                            			// Reading the features (from several files)
		SegServer segmentsServer;                                                             				// Create the segment server for managing the segments/clusters
		LabelServer labelServer;                                                              				// Create the lable server, for indexing the segments/clusters
		initializeClusters(featureFileListp,segmentsServer,labelServer,config);         		// Reading the segmentation files for each feature input file
		verifyClusterFile(segmentsServer,fs,config);                                     				// Verify if the segments ending before the end of the feature files...

		MixtureGD & adaptedMixture = ms.duplicateMixture(world,DUPL_DISTRIB);                 	// Creating final as a copy of the world model
		MixtureGD & clientMixture= ms.duplicateMixture(world,DUPL_DISTRIB);
		long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);   	// Get the index of the cluster with in interest audio segments
		if (codeSelectedFrame==-1){                                                           					// No data for this model !!!!!!!!!!!!!!
			cout << " WARNING - NO DATA FOR TRAINING ["<<*id<<"]";
			if (saveEmptyModel){
				cout <<" World model is returned"<<endl;                                    				// In this case, the client model is the world model
				if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
				adaptedMixture.save(*id, config);                                           					// Save the client model
			}
		}			

		else{
			SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments                                   

			//Compute the JFA statistics
			jfaAcc.computeAndAccumulateJFAStat(selectedSegments,fs,config);

			//Estimate X and Y in one time for each speaker
			jfaAcc.storeAccs();
			jfaAcc.estimateVUEVUT(config);

			jfaAcc.estimateAndInverseL_VU(config);

			jfaAcc.substractMplusDZ(config);

			jfaAcc.estimateYX();
			//Reinitialise the accumulators
			jfaAcc.resetTmpAcc();
			jfaAcc.restoreAccs();
			
			//Split X and Y estimates
			jfaAcc.splitYX();

			//Substract speaker and channel statistics M + VUYX
			jfaAcc.substractMplusVUYX();		
			//Estimate Z for each speaker
			jfaAcc.estimateZ();
			//Reinitialise the accumulators
			jfaAcc.resetTmpAcc();
			jfaAcc.restoreAccs();

			bool varAdapt = false;
			if((config.existsParam("varAdapt")) && ( config.getParam("varAdapt").toBool() )){
				varAdapt = true;
			}

			DoubleVector clientSV(jfaAcc.getSvSize(), jfaAcc.getSvSize());
			clientSV.setSize(jfaAcc.getSvSize());
			DoubleVector clientModel(jfaAcc.getSvSize(), jfaAcc.getSvSize());
			clientModel.setSize(jfaAcc.getSvSize());

			bool saveMixture = true;
			if((config.existsParam("saveMixture")) && !( config.getParam("saveMixture").toBool() ))	saveMixture = false;
			bool saveSuperVector = true;
			if((config.existsParam("saveSuperVector")) && !( config.getParam("saveSuperVector").toBool() ))	saveSuperVector = false;
			bool saveX = false;
			bool saveY = false;
			bool saveZ = false;


			if(config.existsParam("saveX"))			saveX = config.getParam("saveX").toBool();
			if(config.existsParam("saveY"))			saveY = config.getParam("saveY").toBool();
			if(config.existsParam("saveZ"))			saveZ = config.getParam("saveZ").toBool();
			String xExtension = ".x"; String yExtension = ".y"; String zExtension = ".z";
			if(config.existsParam("xExtension"))	xExtension = config.getParam("xExtension");
			if(config.existsParam("yExtension"))	yExtension = config.getParam("yExtension");
			if(config.existsParam("zExtension"))	zExtension = config.getParam("zExtension");

			jfaAcc.getVYplusDZ(clientSV, 0);
			jfaAcc.getMplusVYplusDZ(clientModel, 0);
			
			//WARNING !!!!! only the SuperVector model is divided by the UBM Co-Variance.
			for(unsigned long i=0; i<jfaAcc.getSvSize(); i++){
				clientSV[i] *= jfaAcc.getUbmInvVar()[i];
			}
			
			//Create the ClientMixture to save if required
			if(saveMixture){
				svToModel(clientModel, clientMixture);
				clientMixture.save(*id, config);
			}

			if(saveSuperVector){
				String svPath=config.getParam("saveVectorFilesPath");
				String svExt=config.getParam("vectorFilesExtension"); 
				String svFile=svPath+*id+svExt; 
				((Matrix<double>)clientSV).save(svFile,config);   
			}

			String svPath=config.getParam("saveVectorFilesPath");

			if(saveX){
				String xFile=svPath+*id+xExtension;
				jfaAcc.saveX(xFile,config);
			}
			if(saveY){
				String yFile=svPath+*id+yExtension;
				jfaAcc.saveY(yFile,config);
			}
			if(saveZ){
				String zFile=svPath+*id+zExtension;
				jfaAcc.saveZ(zFile,config);
			}

			long tid=ms.getMixtureIndex(*id);
			ms.deleteMixtures(tid,tid);
			ms.deleteUnusedDistribs();
		}
	}
} // fin try
catch (Exception& e) {cout << e.toString().c_str() << endl;}
return 0;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
int TrainTargetFA(Config& config)
{
  String inputClientListFileName = config.getParam("targetIdList");
  String inputWorldFilename = config.getParam("inputWorldFilename");
  String outputSERVERFilename = "";
  if (config.existsParam("mixtureServer")) outputSERVERFilename =config.getParam("mixtureServer");
  bool initByClient=false;                                              // In this case, the init model is read from the file
  if (config.existsParam("initByClient")) initByClient=config.getParam("initByClient").toBool();
  bool saveEmptyModel=false;
  if (config.existsParam("saveEmptyModel")) saveEmptyModel=config.getParam("saveEmptyModel").toBool();
  // label for selected frames - Only the frames associated with this label, in the label files, will be used
  bool fixedLabelSelectedFrame=true;
  String labelSelectedFrames;
  if (config.existsParam("useIdForSelectedFrame"))    // the ID of each speaker is used as labelSelectedFrame ?
    fixedLabelSelectedFrame=(config.getParam("useIdForSelectedFrame").toBool()==false);  
  if (fixedLabelSelectedFrame)                        // the label is decided by the command line and is unique for the run
    labelSelectedFrames=config.getParam("labelSelectedFrames");
  bool modelData=false;
  if (config.existsParam("useModelData")) modelData=config.getParam("useModelData").toBool();
  String initModelS=inputWorldFilename;
  if (modelData) if (config.existsParam("initModel")) initModelS=config.getParam("initModel"); // Use a specific model for Em init
  bool outputAdaptParam=false;
  if (config.existsParam("superVectors")) outputAdaptParam=true;
  Matrix <double> ChannelMatrix;
  if (verbose) cout<< "EigenMAP and Eigenchannel with [" << config.getParam("initChannelMatrix") << "] of size: ["; 
  ChannelMatrix.load(config.getParam("initChannelMatrix"),config); //get Channel Matrix from args and load in a Matrix object
  if (verbose) cout << ChannelMatrix.rows() << "," <<ChannelMatrix.cols() << "]" << endl;
  bool varAdapt=false;
  if (config.existsParam("FAVarAdapt")) varAdapt=true;
  bool saveCompleteServer=false;
 
  try{
    XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
    XLine * linep;
    inputClientList.getLine(0);
    MixtureServer ms(config);
    StatServer ss(config, ms);
    if (verbose) cout << "(TrainTarget) Factor Analysis - Load world model [" << inputWorldFilename<<"]"<<endl;
    MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);      
    if (verbose) cout <<"(TrainTarget) Use["<<initModelS<<"] for initializing EM"<<endl;
    
    // *********** Target loop ***************** 
    while ((linep=inputClientList.getLine()) != NULL){             // linep gives the XLine with the Id of a given client and the list of files

      String *id=linep->getElement();                              // Get the Client ID (id)
      XLine featureFileListp=linep->getElements();	           // Get the list of feature file for the client (end of the line)
      if (verbose) cout << "(TrainTarget) Train model ["<<*id<<"]"<<endl;   
      FeatureServer fs(config,featureFileListp);                                            // Reading the features (from several files)
      SegServer segmentsServer;                                                             // Create the segment server for managing the segments/clusters
      LabelServer labelServer;                                                              // Create the lable server, for indexing the segments/clusters
      initializeClusters(featureFileListp,segmentsServer,labelServer,config);               // Reading the segmentation files for each feature input file
      verifyClusterFile(segmentsServer,fs,config);                                          // Verify if the segments ending before the end of the feature files...
      MixtureGD & adaptedMixture = ms.duplicateMixture(world,DUPL_DISTRIB);                 // Creating final as a copy of the world model
      MixtureGD & clientMixture= ms.duplicateMixture(world,DUPL_DISTRIB);
      long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);        // Get the index of the cluster with in interest audio segments
      if (codeSelectedFrame==-1){                                                           // No data for this model !!!!!!!!!!!!!!
	cout << " WARNING - NO DATA FOR TRAINING ["<<*id<<"]";
	if (saveEmptyModel){
	  cout <<" World model is returned"<<endl;                                    // In this case, the client model is the world model
	  if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
	  adaptedMixture.save(*id, config);                                           // Save the client model
	}
      }
      else{
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments                                   
        /// **** Factor Analysis Stuff
        XList faNdx;
        faNdx.addLine()=featureFileListp; 
        FactorAnalysisStat FA(faNdx,fs,config); // give all features to FA stats
        
        //FA.computeAndAccumulateGeneralFAStats(selectedSegments,fs,config);    
        for(int i=0;i<config.getParam("nbTrainIt").toLong();i++){
          if (verbose) cout << "------ Iteration ["<<i<<"] ------"<<endl;
          FA.computeAndAccumulateGeneralFAStats(selectedSegments,fs,config);                
          /*if (!varAdapt) FA.getTrueSpeakerModel(clientMixture,linep->getElement(1));
          else FA.getFactorAnalysisModel(clientMixture,linep->getElement(1));
          if (verbose) cout << "LLK for model["<<*id<<"] at it["<<i-1<<"]="<<FA.getLLK(selectedSegments,clientMixture,fs,config) << endl; */
          FA.estimateAndInverseL(config);
          FA.substractSpeakerStats();
          FA.getXEstimate();
          FA.substractChannelStats(); 
          FA.getYEstimate();    
      }      
      MixtureGD & sessionMixture= ms.duplicateMixture(world,DUPL_DISTRIB);
      bool saveSessionModel=false;
      if (config.existsParam("saveSessionModel")) saveSessionModel=true;
      if (saveSessionModel) FA.getSessionModel(sessionMixture,linep->getElement(1));
      if (!varAdapt) FA.getTrueSpeakerModel(clientMixture,linep->getElement(1)); // basically compute M_s_h=M+Dy_s and get a model
      else FA.getFactorAnalysisModel(clientMixture,linep->getElement(1)); // get FA variance adapted model
      if (verbose) cout << "Final LLK for model["<<*id<<"]="<<FA.getLLK(selectedSegments,clientMixture,fs,config) << endl;    

      /// **** End of FA
        if (!outputAdaptParam) {
            if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
            clientMixture.save(*id, config);                                           // Save the client model
            if (saveSessionModel) {
              String sessionfile=*id+".session";
              if (verbose) cout << "Save session model ["<<sessionfile<<"]" << endl;              
              sessionMixture.save(sessionfile,config);   
            }              
        }
	if (!saveCompleteServer){
	  long tid=ms.getMixtureIndex(*id);      // TO BE SUPPRESSED BY
	  ms.deleteMixtures(tid,tid);            // ADDING a delete on a mixture pointor
	  ms.deleteUnusedDistribs();
	  }
      }
    }    
  } // fin try
catch (Exception& e) {cout << e.toString().c_str() << endl;}
  return 0;
}
// Training of client Speakers
// The same than TrainTarget but train simultaneoulsy 1 model for each cluster (set of segments with the same label)
// found in the input files labels.
// One option in order to save the n models as a modification of the world model - save disk space
 int TrainTargetByLabel(Config& config)
{
  String inputClientListFileName = config.getParam("targetIdList");
  String inputWorldFilename = config.getParam("inputWorldFilename");
  String outputSERVERFilename = config.getParam("mixtureServer");
  // label for selected frames - Only the frames associated with this label, in the label files, will be used
  //bool fixedLabelSelectedFrame;
  bool initByClient=false;
  bool aprioriWorld=true;
  if (config.existsParam("initByClient")) initByClient=true;
  if (config.existsParam("aprioriClient")){
    aprioriWorld=false;
    initByClient=true;
  }
  bool saveCompleteServer=false;
  bool outputAdaptParam=false;
  if (config.existsParam("outputAdaptParam")) outputAdaptParam=config.getParam("outputAdaptParam").toBool();
  
  try{
    XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
    XLine *linep;
    inputClientList.getLine(0);
    MixtureServer ms(config);
    StatServer ss(config, ms);
    if (verbose) cout << "TrainTarget - by label opption - Load world model [" << inputWorldFilename<<"]"<<endl;
    MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
    // *********** Target loop ***************** 
    while ((linep=inputClientList.getLine()) != NULL){             // linep gives the XLine with the Id of a given client and the list of files
      String clientId=(*linep->getElement());                      // Get the Client ID (clientId)
      XLine featureFileListp=linep->getElements();	           // Get the list of feature file for the client (end of the line)
      FeatureServer fs(config,featureFileListp);                   // Reading the features (from several files)
      if (verbose) cout << "Train label models for client ["<<clientId<<"]"<<endl;   
      MixtureGD &clientGModel=ms.createMixtureGD();
      if (initByClient) {
          if (verbose) cout << "Load client model [" << clientId <<"]"<<endl;
          clientGModel = ms.loadMixtureGD(clientId); //not necessary to load client model
      }
      SegServer segmentsServer;                                                             // Create the segment server for managing the segments/clusters
      LabelServer labelServer;                                                              // Create the lable server, for indexing the segments/clusters
      initializeClusters(featureFileListp,segmentsServer,labelServer,config);               // Reading the segmentation files for each feature input file
      verifyClusterFile(segmentsServer,fs,config);                                          // Verify if the segments ending before the end of the feature files...
      for (unsigned long codeSelectedFrame=0;codeSelectedFrame<segmentsServer.getClusterCount();codeSelectedFrame++){ // For each cluster
	String clientIdByLabel=clientId+"_"+labelServer.getLabel(codeSelectedFrame).getString(); // Build the model name for the client and the label
	if (verbose) cout << "Train labeldependent model ["<<clientIdByLabel<<"]"<<endl;   
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);          // Gives the cluster of the selected/used segments
	MixtureGD & clientMixture = ms.duplicateMixture(world,DUPL_DISTRIB);       // Creating clientMixture as a copy of the world model
	ms.setMixtureId(clientMixture,clientIdByLabel);                                     // Set the client model Id
	if (initByClient)                                                                   // During trainig data statistic estimation by EM,
	  clientMixture=clientGModel;                                                       // the global client model is used for initalization
	if (aprioriWorld)                                                                   // EM algo with MAP criterion
	  adaptModel(config,ss,ms,fs,selectedSegments,world,clientMixture);          // A priori info is the world model  
	else adaptModel(config,ss,ms,fs,selectedSegments,clientGModel,clientMixture);// A priori info is the client model-by default initByClient is also set
        if (!outputAdaptParam) {
            if (verbose) cout << "Save client model ["<<clientIdByLabel<<"]" << endl;
            clientMixture.save(clientIdByLabel, config);                                           // Save the client model
        }
	if (!saveCompleteServer){
	  long tid=ms.getMixtureIndex(clientIdByLabel);      // TO BE SUPPRESSED BY
	  ms.deleteMixtures(tid,tid);            // ADDING a delete on a mixture pointor
	  ms.deleteUnusedDistribs();
	  }
      }      
      if (!saveCompleteServer){
	long tid=ms.getMixtureIndex(clientId);      // TO BE SUPPRESSED BY
	ms.deleteMixtures(tid,tid);                 // ADDING a delete on a mixture pointor
	ms.deleteUnusedDistribs();
      }                                                                   // end of the the label loop fr a speaker
    } // end of the the target loop 
    
    // Save the complete mixture server
    // TODO
  } // fin try
  

  
  catch (Exception& e)
    { 
      cout << e.toString().c_str() << endl;
    }
  return 0;
}
// Training of client Speakers
// Input: Xlist	Format: ID_Client Seg1 Seg2 ..
// Output: ALIZE_MixtureServer (binaire) + GMM / Client (binary)
 int TrainTarget(Config& config)
{
  String inputClientListFileName = config.getParam("targetIdList");
  String inputWorldFilename = config.getParam("inputWorldFilename");
  String outputSERVERFilename = "";
  if (config.existsParam("mixtureServer")) outputSERVERFilename =config.getParam("mixtureServer");
  bool initByClient=false;                                              // In this case, the init model is read from the file
  if (config.existsParam("initByClient")) initByClient=config.getParam("initByClient").toBool();
  bool saveEmptyModel=false;
  if (config.existsParam("saveEmptyModel")) saveEmptyModel=config.getParam("saveEmptyModel").toBool();
  // label for selected frames - Only the frames associated with this label, in the label files, will be used
  bool fixedLabelSelectedFrame=true;
  String labelSelectedFrames;
  if (config.existsParam("useIdForSelectedFrame"))    // the ID of each speaker is used as labelSelectedFrame ?
    fixedLabelSelectedFrame=(config.getParam("useIdForSelectedFrame").toBool()==false);  
  if (fixedLabelSelectedFrame)                        // the label is decided by the command line and is unique for the run
    labelSelectedFrames=config.getParam("labelSelectedFrames");
  bool modelData=false;
  if (config.existsParam("useModelData")) modelData=config.getParam("useModelData").toBool();
  String initModelS=inputWorldFilename;
  if (modelData) if (config.existsParam("initModel")) initModelS=config.getParam("initModel"); // Use a specific model for Em init
  bool outputAdaptParam=false;
   if (config.existsParam("superVector")) outputAdaptParam=true;
  bool NAP=false;
  Matrix <double> ChannelMatrix;
  if (config.existsParam("NAP")) {
     if (verbose) cout<< "Removing channel effect with NAP from " << config.getParam("NAP") << " of size: ["; 
    NAP=true; // enable NAP
    ChannelMatrix.load(config.getParam("NAP"),config); //get Channel Matrix from args and load in a Matrix object
     if (verbose) cout << ChannelMatrix.rows() << "," <<ChannelMatrix.cols() << "]" << endl;
    }
    

  bool saveCompleteServer=false;
 
  try{
    XList inputClientList(inputClientListFileName,config);          // read the Id + filenames for each client
    XLine * linep;
    inputClientList.getLine(0);
    MixtureServer ms(config);
    StatServer ss(config, ms);
    if (verbose) cout << "TrainTarget - Load world model [" << inputWorldFilename<<"]"<<endl;
    MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
    MixtureGD& initModel =ms.loadMixtureGD(initModelS);
   
    if (verbose) cout <<"Use["<<initModelS<<"] for initializing EM"<<endl;
    
    // *********** Target loop ***************** 
    while ((linep=inputClientList.getLine()) != NULL){             // linep gives the XLine with the Id of a given client and the list of files
      String *id=linep->getElement();                              // Get the Client ID (id)
      XLine featureFileListp=linep->getElements();	           // Get the list of feature file for the client (end of the line)
      if (verbose) cout << "Train model ["<<*id<<"]"<<endl;   
      if (!fixedLabelSelectedFrame){                                // the ID is used as label for selecting the frame
	labelSelectedFrames=*id;
	if (verbose) cout <<*id<<" is used for label selected frames"<<endl;
      }
      FeatureServer fs(config,featureFileListp);                                            // Reading the features (from several files)
      SegServer segmentsServer;                                                             // Create the segment server for managing the segments/clusters
      LabelServer labelServer;                                                              // Create the lable server, for indexing the segments/clusters
      initializeClusters(featureFileListp,segmentsServer,labelServer,config);               // Reading the segmentation files for each feature input file
      verifyClusterFile(segmentsServer,fs,config);                                          // Verify if the segments ending before the end of the feature files...
      MixtureGD & adaptedMixture = ms.duplicateMixture(world,DUPL_DISTRIB);                 // Creating final as a copy of the world model
      MixtureGD & clientMixture= ms.duplicateMixture(world,DUPL_DISTRIB);
      if (initByClient){                                                                   // During trainig data statistic estimation by EM,
	clientMixture= ms.loadMixtureGD(*id);                                               // the client model is used for initalization
	adaptedMixture=clientMixture;
      }
      long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);        // Get the index of the cluster with in interest audio segments
      if (codeSelectedFrame==-1){                                                           // No data for this model !!!!!!!!!!!!!!
	cout << " WARNING - NO DATA FOR TRAINING ["<<*id<<"]";
	if (saveEmptyModel){
	  cout <<" World model is returned"<<endl;                                    // In this case, the client model is the world model
	  if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
	  adaptedMixture.save(*id, config);                                           // Save the client model
	}
      }
      else{
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments                                   
	if (!initByClient) ms.setMixtureId(clientMixture,*id);                                        // Set the client model Id
	if (modelData) modelBasedadaptModel(config,ss,ms,fs,selectedSegments,world,clientMixture,initModel);          // EM algo with MAP criterion
	else adaptModel(config,ss,ms,fs,selectedSegments,world,clientMixture);          // EM algo with MAP criterion
        if (NAP) {
          if (verbose) cout << "NAP on SVs" << endl;
          computeNap(clientMixture,ChannelMatrix);
        }
        if (outputAdaptParam) {
            RealVector<double> v;
            getSuperVector(v,world,clientMixture,config);   
           String out=config.getParam("saveVectorFilesPath")+*id+config.getParam("vectorFilesExtension");        
            Matrix <double> vv=(Matrix<double>)v;
            vv.save(out,config);     
          }
        if (!outputAdaptParam) {
            if (verbose) cout << "Save client model ["<<*id<<"]" << endl;
            clientMixture.save(*id, config);                                           // Save the client model
        }
	if (!saveCompleteServer){
	  long tid=ms.getMixtureIndex(*id);      // TO BE SUPPRESSED BY
	  ms.deleteMixtures(tid,tid);            // ADDING a delete on a mixture pointor
	  ms.deleteUnusedDistribs();
	  }
      }
    }                                                                              // end of the the target loop 
    
    // Save the complete mixture server
    // TODO
  } // fin try
   


  catch (Exception& e)
    { 
      cout << e.toString().c_str() << endl;
    }
  return 0;
}
// Main init function
double TopGauss::compute(MixtureGD & UBM,FeatureServer &fs,String & featureFilename,Config & config){
	StatServer ss(config);
	MixtureGDStat &acc=ss.createAndStoreMixtureStat(UBM);	
	unsigned long _mixsize=UBM.getDistribCount();
	String labelSelectedFrames =config.getParam("labelSelectedFrames");
	unsigned long begin=fs.getFirstFeatureIndexOfASource(featureFilename);
	fs.seekFeature(begin);
	SegServer segmentsServer;
	LabelServer labelServer;
	initializeClusters(featureFilename,segmentsServer,labelServer,config);
	//	__android_log_print(ANDROID_LOG_DEBUG, "TopGauss::compute", " Feature file  %s  \n", featureFilename.c_str());

	verifyClusterFile(segmentsServer,fs,config);
	unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);	
	SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);  
	acc.resetLLK();
	double topD=config.getParam("topGauss").toDouble();
	if (verbose) {if(topD<1.0) cout << "LLK %="<< topD << "% ";else cout << "Top-"<<topD<<" ";}
	
	// Class values
	_nt=totalFrame(selectedSegments);	
	_nbg.setSize(_nt); _idx.setSize(0);_snsw.setSize(0); _snsl.setSize(0);
	_nbg.setAllValues(0); _idx.setAllValues(0);_snsw.setAllValues(0.0);_snsl.setAllValues(0.0);
	_nbgcnt=0;
	Seg *seg;          // current selected segment
	selectedSegments.rewind();		
	unsigned long t=0; //cnt frames
	while((seg=selectedSegments.getSeg())!=NULL){                       	
		unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); 
		fs.seekFeature(begin);
		Feature f;
		for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
			fs.readFeature(f); 
			double llk=acc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
			const LKVector &topV=ss.getTopDistribIndexVector();
			double lk_tot=exp(llk);
			
			double val=0.0;
			if (topD<1.0) {
				for(unsigned long j=0;j<_mixsize;j++){
					if (val > topD*lk_tot) break;
					val+=(topV[j].lk);
					_nbg[t]++;
				}
			} else _nbg[t]=(unsigned long)topD;
			_nbgcnt+=_nbg[t];
			 
			double snsw=1.0;
			double snsl=lk_tot;					
			for(unsigned long j=0;j<_nbg[t];j++) {
				_idx.addValue(topV[j].idx);    		
				snsw -=UBM.weight(topV[j].idx);
				snsl -=topV[j].lk;
			}

			_snsw.addValue(snsw);
			if (snsl < EPS_LK)
				_snsl.addValue(EPS_LK);
			else _snsl.addValue(snsl);
			t++;
		}		
	}
	if (t!=_nt) cout << "W: t("<<t<<") != _nt(" <<_nt<<")"<<endl;
return acc.getMeanLLK();
}
void TurnDetection(Config& config, SegCluster& cluster,SegServer& segOutputServer,
		  StatServer& ss,FeatureServer &fs,MixtureServer&
		  ms,LabelServer& labelServer){

SegServer segTemp;	

segOutputServer.removeAllClusters();
segOutputServer.removeAllSegs();

SegServer actualSeg;	
String et_temp="speech";
Label l(et_temp);
SegCluster& clusterSeg=actualSeg.createCluster(labelServer.addLabel(l),et_temp," "); //Create the cluster L


String crit="DGLR";
if(config.existsParam("clusteringCrit")) 
	crit=config.getParam("clusteringCrit");

double threshold=0.0;
if(config.existsParam("clusteringCritThresh"))
	threshold=config.getParam("clusteringCritThresh").toDouble();

unsigned long winSize=50;
if(config.existsParam("winSize")) winSize=config.getParam("winSize").toLong();
unsigned long winStep=5;
if(config.existsParam("winStep")) winStep=config.getParam("winStep").toLong();
double alpha=0.7;
if(config.existsParam("alpha")) alpha=config.getParam("alpha").toDouble();

unsigned long start1=0, end1=0;
unsigned long start2=0, end2=0;
unsigned long accu=0;


for(unsigned long iseg=0; iseg<cluster.getCount(); iseg++){
	
	
	Seg& segment=(Seg&)cluster.get(iseg);
	if(verbose)
		cout << "Segment" << iseg << ": " << segment.begin() << " " << endSeg(&segment) << endl; 
	if(segment.length() <= 2*winSize){
		clusterSeg.add(actualSeg.createSeg(segment.begin(),endSeg(&segment)-segment.begin()+1,0,segment.string(),segment.sourceName()));
		if(debug) cout << "add: " << segment.begin() << " " << endSeg(&segment) << endl;		
	}
	else{
		ObjectRefVector res;
		start1=segment.begin();
		end1=start1+winSize-1;
		start2=end1+1;
		end2=start2+winSize-1;
		accu = start1;
	
		while(end2 < endSeg(&segment)){
			if(verbose){
				cout << "Computation between: " << start1 << " " << end1; 
				cout << " and " << start2 << " " << end2 << endl; 
			}
			SegCluster& c1=segTemp.createCluster();
			c1.add(segTemp.createSeg(start1,winSize,0,"null",segment.sourceName()));
			SegCluster& c2=segTemp.createCluster();
			c2.add(segTemp.createSeg(start2,winSize,0,"null",segment.sourceName()));
			CritInfo *resCrit=new CritInfo(clusteringCriterionWithoutWorldInitOneGaus(config, c1, c2, ss, fs,crit),false,end1);
			
			res.addObject((Object&)*resCrit);	
			start1+=winStep;
			end1+=winStep;
			start2+=winStep;
			end2+=winStep;	
			
		}	
		
	
		/* smoothing */
	/*	for(unsigned long i=1; i<res.size()-1; i++){
			CritInfo &resCrit=(CritInfo&)(res.getObject(i));
			CritInfo &resCritP=(CritInfo&)(res.getObject(i-1));
			CritInfo &resCritN=(CritInfo&)(res.getObject(i+1));
		
			resCrit.setValue(0.25*resCritP.getValue()+0.25*resCritN.getValue()+0.5*resCrit.getValue());
		}	
	*/

           	DoubleVector score_buffer;
           	score_buffer.setSize(2);
           	score_buffer[0 % 2]=((CritInfo&)(res.getObject(0))).getValue();

           	for(unsigned long i=1; i<res.size()-1; i++)
           	{
               		CritInfo &resCrit=(CritInfo&)(res.getObject(i));
               		CritInfo &resCritN=(CritInfo&)(res.getObject(i+1));//right window

               		score_buffer[i % 2]=resCrit.getValue();
               		resCrit.setValue(0.25*score_buffer[(i-1) % 2]+0.25*resCritN.getValue()+0.5*resCrit.getValue());

           	}

		/* to look for maxima in the criterion value curve */
		/* if difference on left and right of a point with neighboor points is over alpha*standard deviation => maxima is found ! */
	
		double sum=0.0;
		double sum2=0.0;
		for(unsigned long i=0; i<res.size(); i++){
			CritInfo &resCrit=(CritInfo&)res.getObject(i);
			sum += resCrit.getValue();
			sum2+=resCrit.getValue()*resCrit.getValue();
		}
		double mean=sum/(double)res.size();
		double std=sqrt((sum2/(double)(res.size())-(mean*mean)));
	
		if(verbose){
			cout << "Mean and std: " << mean << " " << std << endl;
		}
	
		CritInfo &resCrit=(CritInfo&)res.getObject(0);
		resCrit.setDec(false);
		for(unsigned long i=1, j=0; i<res.size()-1; i++){
			/* for each value */
			/* search left min */
			j=i-1;
			double minL=((CritInfo&)res.getObject(i)).getValue();
			bool ok=true;
			while(ok && (j > 0)){
				if(((CritInfo&)res.getObject(j)).getValue() < minL){
					minL =((CritInfo&)res.getObject(j)).getValue();
					j--;
				}else{	
					ok = false;
				}
			}	
		
			if(myabs(((CritInfo&)res.getObject(i)).getValue()-minL) > alpha*std){
			// search right min 
		
				j=i+1;
				double minR=((CritInfo&)res.getObject(i)).getValue();
				ok=true;
				while(ok && (j < res.size())){
					if(((CritInfo&)res.getObject(j)).getValue() < minR){
						minR = ((CritInfo&)res.getObject(j)).getValue();
						j++;
					}else{
						ok = false;
					}
				}
		
				if(myabs(((CritInfo&)res.getObject(i)).getValue()-minR) > alpha*std){
					((CritInfo&)res.getObject(i)).setDec(true);				
				}else{
					((CritInfo&)res.getObject(i)).setDec(false);				
				}
			}else{
				((CritInfo&)res.getObject(i)).setDec(false);					
			}
			/*double max=((CritInfo&)res.getObject(i)).getValue();
			double minL=((CritInfo&)res.getObject(i-1)).getValue();
			double minR=((CritInfo&)res.getObject(i+1)).getValue();
			if((minL < max) && (minR < max) && (max > mean+std))
				((CritInfo&)res.getObject(i)).setDec(true);
			else
				((CritInfo&)res.getObject(i)).setDec(false);
			*/
		}
	
		start1 = segment.begin();
		for(unsigned long i=0; i<res.size(); i++){
			cout << ((CritInfo&)res.getObject(i)).getFrame() << " " << ((CritInfo&)res.getObject(i)).getValue() << " => " << ((CritInfo&)res.getObject(i)).getDec() << endl;
			if(((CritInfo&)res.getObject(i)).getDec()){
				clusterSeg.add(actualSeg.createSeg(start1,((CritInfo&)res.getObject(i)).getFrame()-start1+1,0,segment.string(),segment.sourceName()));
				if(verbose) cout << "add: " << start1 << " " << ((CritInfo&)res.getObject(i)).getFrame() << endl;
				start1=((CritInfo&)res.getObject(i)).getFrame()+1;
			}
		}
		// last point
		cout << "last point: " << start1 << " fin segment: " <<  endSeg(&segment) << endl;
		if(start1 < endSeg(&segment)){
			clusterSeg.add(actualSeg.createSeg(start1,endSeg(&segment)-start1+1,0,segment.string(),segment.sourceName()));
			if(verbose) cout << "add: " << start1 << " " << endSeg(&segment) << endl;	
		}

	}
}


displayAllClusters(config, actualSeg);
segOutputServer=actualSeg;
}