<commit_before>/*
 * This material contains unpublished, proprietary software of 
 * Entropic Research Laboratory, Inc. Any reproduction, distribution, 
 * or publication of this work must be authorized in writing by Entropic 
 * Research Laboratory, Inc., and must bear the notice: 
 *
 *    "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. 
 *                   All rights reserved"
 *
 * The copyright notice above does not evidence any actual or intended 
 * publication of this source code.     
 *
 * Written by:  <NAME>
 * Checked by:
 * Revised by:  <NAME>
 *
 * Brief description:  Estimates F0 using normalized cross correlation and
 *   dynamic programming.
 *
 */

static char *sccs_id = "@(#)get_f0.c	1.14	10/21/96	ERL";

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <esps/esps.h>
#include <esps/fea.h>
#include <esps/feasd.h>

#include "f0.h"

#define SYNTAX USAGE("get_f0 [-P param_file][-{pr} range][-s range][-S frame_step_samples]\n     [-i frame_step_seconds][-x debug_level] infile outfile")

char	    *ProgName = "get_f0";
static char *Version = "1.14";
static char *Date = "10/21/96";

int	    debug_level = 0;
extern void fea_skiprec();


static int check_f0_params(F0_params *par, double sample_freq);

int main_sw_tmp(ac, av)
    int     ac;
    char    **av;
{
  extern char *optarg;
  extern int optind, getopt();
  char *get_cmd_line();
  float *fdata;
  char c, *ifname, *ofname, *range = NULL;
  FILE *ifile, *ofile;
  struct header *ihd, *ohd;
  struct feasd *sd_rec;
  struct fea_data *fea_rec;
  int done;
  long buff_size, actsize;
  double sf, output_starts, frame_rate;
  F0_params *par, *read_f0_params();
  char *param_file = NULL;
  float *f0p, *vuvp, *rms_speech, *acpkp;
  double *rec_F0, *rec_pv, *rec_rms, *rec_acp;
  int i, vecsize;
  int init_dp_f0(), dp_f0(), check_f0_params();
  long sdstep = 0;

  par = (F0_params *) malloc(sizeof(F0_params));
  par->cand_thresh = 0.3;
  par->lag_weight = 0.3;
  par->freq_weight = 0.02;
  par->trans_cost = 0.005;
  par->trans_amp = 0.5;
  par->trans_spec = 0.5;
  par->voice_bias = 0.0;
  par->double_cost = 0.35;
  par->min_f0 = 50;
  par->max_f0 = 550;
  par->frame_step = 0.01;
  par->wind_dur = 0.0075;
  par->n_cands = 20;
  par->mean_f0 = 200;     /* unused */
  par->mean_f0_weight = 0.0;  /* unused */
  par->conditioning = 0;    /*unused */

  while((c = getopt(ac,av,"x:P:p:r:s:S:i:")) != EOF){
    switch(c){
    case 'P':
      param_file = optarg;
      break;
    case 'x':
      debug_level = atoi(optarg);
      break;
    default:
      SYNTAX;
      exit(1);
    }
  }
  
  if((ac - optind) != 2){
    SYNTAX;
    exit(1);
  }
  
  (void) read_params(param_file, SC_NOCOMMON, (char *)NULL);
  
  if(symtype("frame_step") != ST_UNDEF)
    par->frame_step = getsym_d("frame_step");

  if( symtype("cand_thresh") != ST_UNDEF)
    par->cand_thresh = getsym_d("cand_thresh");
  if( symtype("lag_weight") != ST_UNDEF)
    par->lag_weight = getsym_d("lag_weight");
  if( symtype("freq_weight") != ST_UNDEF)
    par->freq_weight = getsym_d("freq_weight");
  if( symtype("trans_cost") != ST_UNDEF)
    par->trans_cost = getsym_d("trans_cost");
  if( symtype("trans_amp") != ST_UNDEF)
    par->trans_amp = getsym_d("trans_amp");
  if( symtype("trans_spec") != ST_UNDEF)
    par->trans_spec = getsym_d("trans_spec");
  if( symtype("voice_bias") != ST_UNDEF)
    par->voice_bias = getsym_d("voice_bias");
  if( symtype("double_cost") != ST_UNDEF)
    par->double_cost = getsym_d("double_cost");
  if( symtype("min_f0") != ST_UNDEF)
    par->min_f0 = getsym_d("min_f0");
  if( symtype("max_f0") != ST_UNDEF)
    par->max_f0 = getsym_d("max_f0");
  if( symtype("wind_dur") != ST_UNDEF)
    par->wind_dur = getsym_d("wind_dur");
  if( symtype("n_cands") != ST_UNDEF)
    par->n_cands = getsym_i("n_cands");

  ifname = eopen(ProgName, av[optind], "r", FT_FEA, FEA_SD, &ihd, &ifile);
  ofname = eopen(ProgName, av[optind+1], "w", NONE, NONE, &ohd, &ofile);
  sf = get_genhd_val("record_freq", ihd, 0.0);
  if (sf == 0.0) {
    Fprintf(stderr, "%s: no sampling frequency---exiting.\n", ProgName);
    exit(1);
  }
  if(check_f0_params(par, sf)){
    Fprintf(stderr, "%s: invalid/inconsistent parameters -- exiting.\n",
	    ProgName);
    exit(1);
  }

  /*SW: Removed range restricter, but this may be interesting: 
    if (total_samps < ((par->frame_step * 2.0) + par->wind_dur) * sf), then
      input range too small*/

  ohd = new_header(FT_FEA);
  if (ohd == NULL) {
    Fprintf(stderr, "%s: failed to create output header---exiting.\n",
	    ProgName);
    exit(1);
  }
  (void) strcpy (ohd->common.prog, ProgName);
  (void) strcpy (ohd->common.vers, Version);
  (void) strcpy (ohd->common.progdate, Date);
  ohd->common.tag = NO;
  add_source_file(ohd,ifname,ihd);
  add_comment(ohd,get_cmd_line(ac,av));

  add_fea_fld("F0", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("prob_voice", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("rms", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("ac_peak", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  fea_rec = allo_fea_rec(ohd);
  rec_F0 = (double *) get_fea_ptr(fea_rec,"F0", ohd);
  rec_pv = (double *) get_fea_ptr(fea_rec,"prob_voice", ohd);
  rec_rms = (double *) get_fea_ptr(fea_rec,"rms", ohd);
  rec_acp = (double *) get_fea_ptr(fea_rec,"ac_peak", ohd);

  output_starts = par->wind_dur/2.0; 
  /* Average delay due to loc. of ref. window center. */
  frame_rate = 1.0 / par->frame_step;

  write_header(ohd, ofile);

  /* Initialize variables in get_f0.c; allocate data structures;
   * determine length and overlap of input frames to read.
   */
  if (init_dp_f0(sf, par, &buff_size, &sdstep)
      || buff_size > INT_MAX || sdstep > INT_MAX)
  {
    Fprintf(stderr, "%s: problem in init_dp_f0().\n", ProgName);
    exit(1);
  }

  if (debug_level)
    Fprintf(stderr, "%s: init_dp_f0 returned buff_size %ld, sdstep %ld.\n",
	    ProgName, buff_size, sdstep);

  sd_rec = allo_feasd_recs(ihd, FLOAT, buff_size, NULL, NO);
  fdata = (float *) sd_rec->data;
  
  fea_skiprec(ifile, 0, ihd);

  actsize = get_feasd_recs(sd_rec, 0L, buff_size, ihd, ifile);

  while (TRUE) {

    done = (actsize < buff_size);

    if (dp_f0(fdata, (int) actsize, (int) sdstep, sf, par,
	      &f0p, &vuvp, &rms_speech, &acpkp, &vecsize, done)) {
      Fprintf(stderr, "%s: problem in dp_f0().\n", ProgName);
      exit(1);
    }

    for (i = vecsize - 1; i >= 0; i--) {
      *rec_F0 = f0p[i];
      *rec_pv = vuvp[i];
      *rec_rms = rms_speech[i];
      *rec_acp = acpkp[i];
      put_fea_rec(fea_rec, ohd, ofile);
    }

    if (done)
      break;
    
    actsize = get_feasd_orecs( sd_rec, 0L, buff_size, sdstep, ihd, ifile);

  }

  exit(0);
}


/*
 * Some consistency checks on parameter values.
 * Return a positive integer if any errors detected, 0 if none.
 */

static int
check_f0_params(par, sample_freq)
    F0_params   *par;<commit_msg>remove param_file, debug_level, trailing whitespace<commit_after>/*
 * This material contains unpublished, proprietary software of 
 * Entropic Research Laboratory, Inc. Any reproduction, distribution, 
 * or publication of this work must be authorized in writing by Entropic 
 * Research Laboratory, Inc., and must bear the notice: 
 *
 *    "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. 
 *                   All rights reserved"
 *
 * The copyright notice above does not evidence any actual or intended 
 * publication of this source code.     
 *
 * Written by:  <NAME>
 * Checked by:
 * Revised by:  <NAME>
 *
 * Brief description:  Estimates F0 using normalized cross correlation and
 *   dynamic programming.
 *
 */

static char *sccs_id = "@(#)get_f0.c	1.14	10/21/96	ERL";

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <esps/esps.h>
#include <esps/fea.h>
#include <esps/feasd.h>

#include "f0.h"

#define SYNTAX USAGE("get_f0 [-P param_file][-{pr} range][-s range][-S frame_step_samples]\n     [-i frame_step_seconds][-x debug_level] infile outfile")

char	    *ProgName = "get_f0";
static char *Version = "1.14";
static char *Date = "10/21/96";

int	    debug_level = 0;
extern void fea_skiprec();


static int check_f0_params(F0_params *par, double sample_freq);

int main_sw_tmp(ac, av)
    int     ac;
    char    **av;
{
  extern char *optarg;
  extern int optind, getopt();
  char *get_cmd_line();
  float *fdata;
  char c, *ifname, *ofname, *range = NULL;
  FILE *ifile, *ofile;
  struct header *ihd, *ohd;
  struct feasd *sd_rec;
  struct fea_data *fea_rec;
  int done;
  long buff_size, actsize;
  double sf, output_starts, frame_rate;
  F0_params *par, *read_f0_params();
  float *f0p, *vuvp, *rms_speech, *acpkp;
  double *rec_F0, *rec_pv, *rec_rms, *rec_acp;
  int i, vecsize;
  int init_dp_f0(), dp_f0(), check_f0_params();
  long sdstep = 0;

  par = (F0_params *) malloc(sizeof(F0_params));
  par->cand_thresh = 0.3;
  par->lag_weight = 0.3;
  par->freq_weight = 0.02;
  par->trans_cost = 0.005;
  par->trans_amp = 0.5;
  par->trans_spec = 0.5;
  par->voice_bias = 0.0;
  par->double_cost = 0.35;
  par->min_f0 = 50;
  par->max_f0 = 550;
  par->frame_step = 0.01;
  par->wind_dur = 0.0075;
  par->n_cands = 20;
  par->mean_f0 = 200;     /* unused */
  par->mean_f0_weight = 0.0;  /* unused */
  par->conditioning = 0;    /*unused */

  while((c = getopt(ac,av,"x:P:p:r:s:S:i:")) != EOF){
    switch(c){
    default:
      SYNTAX;
      exit(1);
    }
  }

  if((ac - optind) != 2){
    SYNTAX;
    exit(1);
  }

#define SW_CUSTOMIZABLE(x) //TODO(sw)
  SW_CUSTOMIZABLE(debug_level);

  SW_CUSTOMIZABLE(par->frame_step);
  SW_CUSTOMIZABLE(par->cand_thresh);
  SW_CUSTOMIZABLE(par->lag_weight);
  SW_CUSTOMIZABLE(par->freq_weight);
  SW_CUSTOMIZABLE(par->trans_cost);
  SW_CUSTOMIZABLE(par->trans_amp);
  SW_CUSTOMIZABLE(par->trans_spec);
  SW_CUSTOMIZABLE(par->voice_bias);
  SW_CUSTOMIZABLE(par->double_cost);
  SW_CUSTOMIZABLE(par->min_f0);
  SW_CUSTOMIZABLE(par->max_f0);
  SW_CUSTOMIZABLE(par->wind_dur);
  SW_CUSTOMIZABLE(par->n_cands);
#undef SW_CUSTOMIZABLE

#define SW_FILE_PARAMS(x, y) //TODO (sw)
  SW_FILE_PARAMS(sf, "sampling frequency");
#undef SW_FILE_PARAMS

  ifname = eopen(ProgName, av[optind], "r", FT_FEA, FEA_SD, &ihd, &ifile);
  ofname = eopen(ProgName, av[optind+1], "w", NONE, NONE, &ohd, &ofile);

  if(check_f0_params(par, sf)){
    Fprintf(stderr, "%s: invalid/inconsistent parameters -- exiting.\n",
	    ProgName);
    exit(1);
  }

  /*SW: Removed range restricter, but this may be interesting:
    if (total_samps < ((par->frame_step * 2.0) + par->wind_dur) * sf), then
      input range too small*/

  ohd = new_header(FT_FEA);
  if (ohd == NULL) {
    Fprintf(stderr, "%s: failed to create output header---exiting.\n",
	    ProgName);
    exit(1);
  }
  (void) strcpy (ohd->common.prog, ProgName);
  (void) strcpy (ohd->common.vers, Version);
  (void) strcpy (ohd->common.progdate, Date);
  ohd->common.tag = NO;
  add_source_file(ohd,ifname,ihd);
  add_comment(ohd,get_cmd_line(ac,av));

  add_fea_fld("F0", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("prob_voice", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("rms", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  add_fea_fld("ac_peak", 1L, 0, (long *) NULL, DOUBLE, (char **) NULL, ohd);
  fea_rec = allo_fea_rec(ohd);
  rec_F0 = (double *) get_fea_ptr(fea_rec,"F0", ohd);
  rec_pv = (double *) get_fea_ptr(fea_rec,"prob_voice", ohd);
  rec_rms = (double *) get_fea_ptr(fea_rec,"rms", ohd);
  rec_acp = (double *) get_fea_ptr(fea_rec,"ac_peak", ohd);

  output_starts = par->wind_dur/2.0;
  /* Average delay due to loc. of ref. window center. */
  frame_rate = 1.0 / par->frame_step;

  write_header(ohd, ofile);

  /* Initialize variables in get_f0.c; allocate data structures;
   * determine length and overlap of input frames to read.
   */
  if (init_dp_f0(sf, par, &buff_size, &sdstep)
      || buff_size > INT_MAX || sdstep > INT_MAX)
  {
    Fprintf(stderr, "%s: problem in init_dp_f0().\n", ProgName);
    exit(1);
  }

  /*SW: pass sdstep to caller so it knows how much we have to buffer. */

  if (debug_level)
    Fprintf(stderr, "%s: init_dp_f0 returned buff_size %ld, sdstep %ld.\n",
	    ProgName, buff_size, sdstep);

  sd_rec = allo_feasd_recs(ihd, FLOAT, buff_size, NULL, NO);
  fdata = (float *) sd_rec->data;

  fea_skiprec(ifile, 0, ihd);

  actsize = get_feasd_recs(sd_rec, 0L, buff_size, ihd, ifile);

  while (TRUE) {

    done = (actsize < buff_size);

    if (dp_f0(fdata, (int) actsize, (int) sdstep, sf, par,
	      &f0p, &vuvp, &rms_speech, &acpkp, &vecsize, done)) {
      Fprintf(stderr, "%s: problem in dp_f0().\n", ProgName);
      exit(1);
    }

    for (i = vecsize - 1; i >= 0; i--) {
      *rec_F0 = f0p[i];
      *rec_pv = vuvp[i];
      *rec_rms = rms_speech[i];
      *rec_acp = acpkp[i];
      put_fea_rec(fea_rec, ohd, ofile);
    }

    if (done)
      break;

    actsize = get_feasd_orecs( sd_rec, 0L, buff_size, sdstep, ihd, ifile);

  }

  exit(0);
}


/*
 * Some consistency checks on parameter values.
 * Return a positive integer if any errors detected, 0 if none.
 */

static int
check_f0_params(par, sample_freq)
    F0_params   *par;
{'fraction_non_alphanumeric': 0.15889607459768387, 'fraction_numerical': 0.020980598586253573, 'mean_word_length': 3.557573680603153, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3118479', 'n_tokens_mistral': 5688, 'n_tokens_neox': 5423, 'n_words': 1232}