Example #1
0
static void cb_import_select_row(GtkWidget *file_clist,
                                 gint row,
                                 gint column,
                                 GdkEventButton *bevent,
                                 gpointer data)
{
   const char *sel;
   struct stat statb;
   int guessed_type;
   int i;

   jp_logf(JP_LOG_DEBUG, "cb_import_select_row\n");
   sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew));

   /* Check to see if its a regular file */
   if (stat(sel, &statb)) {
      jp_logf(JP_LOG_DEBUG, "File selected was not stat-able\n");
      return;
   }
   if (!S_ISREG(statb.st_mode)) {
      jp_logf(JP_LOG_DEBUG, "File selected was not a regular file\n");
      return;
   }

   guessed_type=guess_file_type(sel);
   for (i=0; i<MAX_IMPORT_TYPES; i++) {
      if (radio_types[i]==NULL) break;
      if (guessed_type==radio_file_types[i]) {
         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio_types[i]), TRUE);
         break;
      }
   }

   return;
}
static int
extract_pitch(const char *in, const char *out)
{
    FILE *infh = NULL, *outfh = NULL;
    size_t flen, fshift, nsamps;
    int16 *buf = NULL;
    yin_t *yin = NULL;
    uint16 period, bestdiff;
    int32 sps;

    if (out) {
        if ((outfh = fopen(out, "w")) == NULL) {
            E_ERROR_SYSTEM("Failed to open %s for writing", out);
            goto error_out;
        }
    }
    else {
        outfh = stdout;
    }
    if ((infh = fopen(in, "rb")) == NULL) {
        E_ERROR_SYSTEM("Failed to open %s for reading", in);
        goto error_out;
    }

    /* If we weren't told what the file type is, weakly try to
     * determine it (actually it's pretty obvious) */
    if (!(cmd_ln_boolean("-raw")
          || cmd_ln_boolean("-mswav")
          || cmd_ln_boolean("-nist"))) {
        if (guess_file_type(in, infh) < 0)
            goto error_out;
    }
    
    /* Grab the sampling rate and byte order from the header and also
     * make sure this is 16-bit linear PCM. */
    if (cmd_ln_boolean("-mswav")) {
        if (read_riff_header(infh) < 0)
            goto error_out;
    }
    else if (cmd_ln_boolean("-nist")) {
        if (read_nist_header(infh) < 0)
            goto error_out;
    }
    else if (cmd_ln_boolean("-raw")) {
        /* Just use some defaults for sampling rate and endian. */
        if (cmd_ln_str("-input_endian") == NULL) {
            if (WORDS_BIGENDIAN)
                cmd_ln_set_str("-input_endian", "big");
            else
                cmd_ln_set_str("-input_endian", "little");
        }
        if (cmd_ln_int32("-samprate") == 0)
            cmd_ln_set_int32("-samprate", 16000);
    }

    /* Now read frames and write pitch estimates. */
    sps = cmd_ln_int32("-samprate");
    flen = (size_t)(0.5 + sps * cmd_ln_float32("-flen"));
    fshift = (size_t)(0.5 + sps * cmd_ln_float32("-fshift"));
    yin = yin_init(flen, cmd_ln_float32("-voice_thresh"),
                   cmd_ln_float32("-search_range"),
                   cmd_ln_int32("-smooth_window"));
    if (yin == NULL) {
        E_ERROR("Failed to initialize YIN\n");
        goto error_out;
    }
    buf = ckd_calloc(flen, sizeof(*buf));
    /* Read the first full frame of data. */
    if (fread(buf, sizeof(*buf), flen, infh) != flen) {
        /* Fail silently, which is probably okay. */
    }
    yin_start(yin);
    nsamps = 0;
    while (!feof(infh)) {
        /* Process a frame of data. */
        yin_write(yin, buf);
        if (yin_read(yin, &period, &bestdiff)) {
            fprintf(outfh, "%.3f %.2f %.2f\n",
                    /* Time point. */
                    (double)nsamps/sps,
                    /* "Probability" of voicing. */
                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
                    /* Pitch (possibly bogus) */
                    period == 0 ? sps : (double)sps / period);
            nsamps += fshift;
        }
        /* Shift it back and get the next frame's overlap. */
        memmove(buf, buf + fshift, (flen - fshift) * sizeof(*buf));
        if (fread(buf + flen - fshift, sizeof(*buf), fshift, infh) != fshift) {
            /* Fail silently (FIXME: really?) */
        }
    }
    yin_end(yin);
    /* Process trailing frames of data. */
    while (yin_read(yin, &period, &bestdiff)) {
            fprintf(outfh, "%.3f %.2f %.2f\n",
                    /* Time point. */
                    (double)nsamps/sps,
                    /* "Probability" of voicing. */
                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
                    /* Pitch (possibly bogus) */
                    period == 0 ? sps : (double)sps / period);
    }

    if (yin)
        yin_free(yin);
    ckd_free(buf);
    fclose(infh);
    if (outfh != stdout)
        fclose(outfh);
    return 0;

error_out:
    yin_free(yin);
    ckd_free(buf);
    if (infh) fclose(infh);
    if (outfh && outfh != stdout) fclose(outfh);
    return -1;
}
Example #3
0
/*-------------------------------------------------------------*/
int main(int argc, char *argv[]) 
{
  int   nargs;
  int   source_is_label      = 0;
  int   source_is_path       = 0;
  int   err                  = 0;
  FILE* fp                   = NULL;

  nargs = handle_version_option 
    (argc, argv, 
     "$Id: mri_path2label.c,v 1.23 2011/03/02 00:04:23 nicks Exp $", 
     "$Name:  $");
  if(nargs && argc - nargs == 1) exit (0);
  argc -= nargs;

  Progname = argv[0] ;
  argc --;
  argv++;
  ErrorInit(NULL, NULL, NULL) ;
  DiagInit(NULL, NULL, NULL) ;
  if (argc == 0) usage_exit();
  parse_commandline(argc, argv);
  check_options();
  if (checkoptsonly) return(0);

  if(!label_to_path && !path_to_label) {
    err = guess_file_type (source_file, &source_is_path, &source_is_label);
    if (err) {
      printf ("ERROR: Couldn't determine source file type.\n");
      exit (1);
    }

    if(source_is_path)  path_to_label = 1;
    if(source_is_label) label_to_path = 1;
  }

  if (dest_file) {
    fp = fopen (dest_file, "w");
    if (NULL == fp) {
      printf ("ERROR: Couldn't open dest file for writing.\n");
      exit (1);
    }
    fclose (fp);
  }

  if(connect){
    // Still under construction
    printf("Connecting vertices in path\n");
    int stat=connect_path(source_file, dest_file, subject, hemi) ;
    exit(stat);
  }
  if(fill){
    // Still under construction
    printf("Filling vertices in path\n");
    int stat=fill_path(source_file, dest_file, subject, hemi, fillseed) ;
    exit(stat);
  }
  if(con_and_fill){
    // Still under construction
    printf("Connecting and Filling vertices in path\n");
    int stat=
      con_and_fill_path(source_file, dest_file, subject, hemi, fillseed) ;
    exit(stat);
  }
  if(con_and_fillx){
    int stat=1;
    if (con_and_fillx_fname) {
      // open file containing the list of fillseeds and path/label files
      FILE* fp2 = fopen (con_and_fillx_fname, "r");
      if (NULL == fp2) {
        printf ("ERROR: Couldn't open file %s for reading.\n",
                con_and_fillx_fname);
        exit (1);
      }
      printf("Reading %s\n",surfacefname);
      MRIS* mris = MRISread(surfacefname);
      if(mris == NULL) exit(1);
      size_t n=2000;
      char tmpstr[2000];
      char* tmps = &tmpstr[0];
      while (0 < getline(&tmps,&n,fp2)) {
        char srcfn[2000];
        char dstfn[2000];
        //printf("%s\n",tmpstr);
        if (sscanf(tmpstr,"%d %s %s",&fillseed,srcfn,dstfn) > 0) {
          printf("Connecting and filling vertices in path at fillseed %d\n", 
                 fillseed);
          MRIS* mrisclone = MRISclone(mris);
          stat = con_and_fill_pathy (srcfn, dstfn, mrisclone, fillseed) ;
          if (stat) break;
        }
      }
    }
    else {
      printf("Connecting and filling vertices in path at fillseed %d\n", 
             fillseed);
      stat=con_and_fill_pathx
        (source_file, dest_file, surfacefname, fillseed) ;
    }
    exit(stat);
  }

  printf ("INFO: Converting %s\n", source_file);
  printf ("INFO:         to %s\n", dest_file);
  if(path_to_label) printf ("INFO: Path to label\n");
  else              printf ("INFO: Label to path\n");
  printf ("\n");

  if (single_path) printf ("INFO: Converting a single path\n");

  if (single_path) {
    if(path_to_label) convert_single_path_to_label(source_file, dest_file);
    else if (label_to_path) convert_single_label_to_path 
                              (source_file, dest_file);
  } else {
    if (path_to_label)      convert_path_to_label (source_file, dest_file);
    else if (label_to_path) convert_label_to_path (source_file, dest_file);
  }

  return 0;
}