/**
   * Clustering.
   *
   * @param threshold the threshold
   * @param clusterSet the cluster set
   * @param featureSet the feature set
   * @param parameter the parameter
   * @return the cluster set
   * @throws Exception the exception
   */
  public ClusterSet clustering(
      double threshold, ClusterSet clusterSet, AudioFeatureSet featureSet, Parameter parameter)
      throws Exception {
    String mask = parameter.getParameterSegmentationOutputFile().getMask();
    String oldMethod = parameter.getParameterClustering().getMethodAsString();
    double oldThreshold = parameter.getParameterClustering().getThreshold();
    String oldModelKind = parameter.getParameterModel().getModelKindAsString();
    int oldNumberOfComponent = parameter.getParameterModel().getNumberOfComponents();

    // --- begin NEW v 1.14 / 4.16 / 4.18 / 4.20---
    parameter.getParameterClustering().setMethod("h");
    // parameter.getParameterClustering().setMethod("sr");
    // --- end NEW v 1.14 ---
    parameter.getParameterClustering().setThreshold(threshold);
    logger.finer(
        "method:"
            + parameter.getParameterClustering().getMethod()
            + " thr:"
            + parameter.getParameterClustering().getThreshold());
    parameter.getParameterModel().setModelKind("FULL");
    parameter.getParameterModel().setNumberOfComponents(1);
    ClusterSet clustersHClust = MClust.make(featureSet, clusterSet, parameter, null);
    if (parameter.getParameterDiarization().isSaveAllStep()) {
      parameter.getParameterSegmentationOutputFile().setMask(mask + ".h.seg");
      MainTools.writeClusterSet(parameter, clustersHClust, false);
    }

    parameter.getParameterSegmentation().setMethod(oldMethod);
    parameter.getParameterModel().setNumberOfComponents(oldNumberOfComponent);
    parameter.getParameterModel().setModelKind(oldModelKind);
    parameter.getParameterClustering().setThreshold(oldThreshold);
    parameter.getParameterSegmentationOutputFile().setMask(mask);

    return clustersHClust;
  }
  /**
   * Clustering linear.
   *
   * @param threshold the threshold
   * @param clusterSet the cluster set
   * @param featureSet the feature set
   * @param parameter the parameter
   * @return the cluster set
   * @throws Exception the exception
   */
  public ClusterSet clusteringLinear(
      double threshold, ClusterSet clusterSet, AudioFeatureSet featureSet, Parameter parameter)
      throws Exception {
    String mask = parameter.getParameterSegmentationOutputFile().getMask();
    String oldMethod = parameter.getParameterClustering().getMethodAsString();
    double oldThreshold = parameter.getParameterClustering().getThreshold();
    String oldModelKind = parameter.getParameterModel().getModelKindAsString();
    int oldNumberOfComponent = parameter.getParameterModel().getNumberOfComponents();

    parameter.getParameterModel().setModelKind("FULL");
    parameter.getParameterModel().setNumberOfComponents(1);
    parameter.getParameterClustering().setMethod("l");
    parameter.getParameterClustering().setThreshold(threshold);

    ClusterSet clustersLClust = MClust.make(featureSet, clusterSet, parameter, null);
    if (parameter.getParameterDiarization().isSaveAllStep()) {
      parameter.getParameterSegmentationOutputFile().setMask(mask + ".l.seg");
      MainTools.writeClusterSet(parameter, clustersLClust, false);
    }
    parameter.getParameterSegmentation().setMethod(oldMethod);
    parameter.getParameterModel().setNumberOfComponents(oldNumberOfComponent);
    parameter.getParameterModel().setModelKind(oldModelKind);
    parameter.getParameterClustering().setThreshold(oldThreshold);
    parameter.getParameterSegmentationOutputFile().setMask(mask);

    return clustersLClust;
  }
  /**
   * Speaker clustering.
   *
   * @param threshold the threshold
   * @param method the method
   * @param clusterSetBase the cluster set base
   * @param clustersSet the clusters set
   * @param featureSet the feature set
   * @param parameter the parameter
   * @return the cluster set
   * @throws Exception the exception
   */
  public ClusterSet speakerClustering(
      double threshold,
      String method,
      ClusterSet clusterSetBase,
      ClusterSet clustersSet,
      AudioFeatureSet featureSet,
      Parameter parameter)
      throws Exception {
    String mask = parameter.getParameterSegmentationOutputFile().getMask();
    String oldMethod = parameter.getParameterClustering().getMethodAsString();
    double oldThreshold = parameter.getParameterClustering().getThreshold();
    String oldEMControl = parameter.getParameterEM().getEMControl();
    int oldNTop = parameter.getParameterTopGaussian().getScoreNTop();
    String oldSpeechDetectorMethod = parameter.getParameterInputFeature().getSpeechMethodAsString();
    double oldSpeechDetectorThreshold = parameter.getParameterInputFeature().getSpeechThreshold();

    // ** bottom up hierarchical classification using GMMs
    // ** one for each cluster, trained by MAP adaptation of a UBM composed of the fusion of
    // 4x128GMM
    // ** the feature normalization use feature mapping technique, after the cluster frames are
    // centered and reduced
    String dir = "ester2";
    InputStream ubmInputStream = getClass().getResourceAsStream(dir + "/ubm.gmm");
    GMMArrayList ubmVect =
        MainTools.readGMMContainer(ubmInputStream, parameter.getParameterModel());
    GMM ubm = ubmVect.get(0);
    // int nbCep = ubm.getDimension() + 1;
    String FeatureFormat = "featureSetTransformation";

    parameter.getParameterInputFeature().setSpeechMethod("E");
    parameter.getParameterInputFeature().setSpeechThreshold(0.1);

    AudioFeatureSet featureSet2 =
        loadFeature(
            featureSet, parameter, clustersSet, FeatureFormat + ",1:3:2:0:0:0,13,1:1:300:4");
    parameter.getParameterClustering().setMethod(method);
    parameter.getParameterClustering().setThreshold(threshold);
    parameter.getParameterEM().setEMControl("1,5,0.01");
    parameter.getParameterTopGaussian().setScoreNTop(5);
    // ---- Begin NEW v 1.13 ---

    // if (parameter.parameterSpeechDetector.useSpeechDetection() == true) {
    // MSpeechDetector.EnergyThresholdMethod(clustersSet, featureSet, parameter);
    // }
    // ---- End NEW v 1.13 ---

    boolean saveAll = parameter.getParameterDiarization().isSaveAllStep();
    parameter.getParameterDiarization().setSaveAllStep(false);
    ClusterSet clustersCLR = MClust.make(featureSet2, clustersSet, parameter, ubm);
    parameter.getParameterDiarization().setSaveAllStep(saveAll);

    parameter.getParameterSegmentationOutputFile().setMask(mask);
    if (parameter.getParameterDiarization().isSaveAllStep()) {
      parameter.getParameterSegmentationOutputFile().setMask(mask + ".c.seg");
      MainTools.writeClusterSet(parameter, clustersCLR, false);
    }
    parameter.getParameterSegmentationOutputFile().setMask(mask);
    parameter.getParameterClustering().setMethod(oldMethod);
    parameter.getParameterClustering().setThreshold(oldThreshold);
    parameter.getParameterEM().setEMControl(oldEMControl);
    parameter.getParameterTopGaussian().setScoreNTop(oldNTop);
    parameter.getParameterInputFeature().setSpeechMethod(oldSpeechDetectorMethod);
    parameter.getParameterInputFeature().setSpeechThreshold(oldSpeechDetectorThreshold);

    return clustersCLR;
  }