int cLibsvmLiveSink::loadClasses( const char *file )
{
  if (file != NULL) {
    if (strlen(file)<1) return 0;

    FILE *f = fopen(file,"r");
    if (f== NULL) {
      SMILE_IERR(2,"error opening class map file '%s' for reading! NOT using a class map!",file);
      return 0;
    }

    char line[MAX_LINE_LENGTH+1];
    nCls=0;
    
    while(fgets(line,MAX_LINE_LENGTH,f) != NULL) {
        if (strlen( line ) > 1) { 
          line[strlen( line ) - 1] = 0;
          const char *cn = strchr(line,':');
          if (cn!=NULL) {
            nCls++;
            // TODO: use class number instead of cont. index
          }
        }
    }
    fclose(f);

    f = fopen(file,"r");
    if (f== NULL) {
      SMILE_IERR(2,"error opening class map file '%s' for reading (2nd pass)! NOT using a class map!",file);
      return 0;
    }

    int i=0;
    classNames=(char**)calloc(1,sizeof(char*)*nCls);
    while(fgets(line,MAX_LINE_LENGTH,f) != NULL) {
        if (strlen( line ) > 1) { 
          line[strlen( line ) - 1] = 0;
          const char *cn = strchr(line,':');
          if (cn!=NULL) {
            classNames[i++] = strdup(cn+1);
            // TODO: use class number instead of cont. index
          }
        }
    }
    fclose(f);

    return 1;
  }
  return 0;
}
Beispiel #2
0
int cActiveMqSink::myTick(long long t)
{
  if (featureSender == NULL) {
    if (warned==0) {
      SMILE_IERR(1,"the semaine featureSender has not been set (it is NULL!), please check the log output of the semaine components, and check the semaineCfg section in the config file! (this warning will be shown only once, the system will not work!)");
      warned = 1;
    }
    return 0;
  }

  SMILE_DBG(4,"tick # %i, reading value vector:",t);
  cVector *vec= reader->getNextFrame();  //new cVector(nValues+1);
  if (vec == NULL) return 0;
  //else reader->nextFrame();

  long vi = vec->tmeta->vIdx;
  double tm = vec->tmeta->time;

  int n,i;
  n = vec->N;
  if (!featureSender->areFeatureNamesSet()) {
    std::vector<std::string> tumFeatureNames(n);
    for (i=0; i<n; i++) {
      std::string name(vec->fmeta->getName(i)); 
      tumFeatureNames[i] = name;
    }
    //    tumFeatureNames[i]="speaking";
    //      tumFeatureNames[i+1]="seqNr";
    featureSender->setFeatureNames(tumFeatureNames);
  }

  std::vector<float> features(n);
  for (i=0; i<n; i++) {
    features[i] = (float)(vec->dataF[i]);
  }
  //    features[i] = speakingStatus;
  //    features[i+1] = seqNr;
  //    seqNr++;

//printf("about to send\n");
// NOTE: (FIXME) sometimes the activeMq sender hangs here:
//               restarting activeMq helps to solve this problem
//               nevertheless it should be investigated...
  if (meta != NULL) {
    featureSender->sendFeatureVector(features, meta->getTime(), false);
  } else {
    featureSender->sendFeatureVector(features, 0, false);
  }
//printf("after sending\n");

  // tick success
  return 1;
}
/*
parse a line of the format:
  word <tab> logprob_weight
add the logprob to the lmWeights array at the correct position
*/
int cTumkwsjSink::parseLmWeightsLine(char *line, long lineNr, const char
*lmfile)
{
  int ret = 0;
  if (line != NULL) {

    char *l=line;
    long le = (long)strlen(line);
    while((le>0)&&((l[le-1]=='\n')||(l[le-1]=='\r'))) { l[le-1] = 0; le--; }
    char *cf = strchr(l,'\t');
    if (cf == NULL) cf = strchr(l,' ');
    if (cf != NULL) {
      *cf=0; cf++;
      le=(long)strlen(cf);
      while ((le>0)&&(cf[0]=='\t')||(cf[0]==' ')) {cf++; le--;}
      while ((le>0)&&(cf[le-1]=='\t')||(cf[le-1]==' ')) {cf[le-1]=0; le--;}
      double lmW=0.0; char *ep=NULL;
      if (le > 0) lmW = strtod(cf,&ep);


      le=(long)strlen(l);
      while ((le>0)&&(l[0]=='\t')||(l[0]==' ')) {l++; le--;}
      while ((le>0)&&(l[le-1]=='\t')||(l[le-1]==' ')) {l[le-1]=0; le--;}

      if (ep == cf) {
        SMILE_IERR(1,"parse error on line %i of lmweights file '%s', expected a number after the <tab> cf='%s'",lineNr,lmfile,cf);
        lmW = 0.0;
      }

      if (le>0) {
        int i;
        for (i=0; i<numWords; i++) {
        //printf("XXX %s -",lmWinfo->woutput[i]);
          if (strcasecmp(lmWinfo->woutput[i],l) == 0) {
            SMILE_IMSG(3,"adding lmweight %f for word '%s'",lmW,l);
            lmWeights[i] = (LOGPROB)lmW;
            ret = 1;
            break;
          }
        }
      }

    }

  }
  return ret;
}
int cLibsvmLiveSink::loadSelection( const char *selFile )
{
  if (selFile != NULL) {
    if (strlen(selFile)<1) return 0;

    FILE *f = fopen(selFile,"r");
    if (f== NULL) {
      SMILE_IERR(2,"error opening feature selection file '%s' for reading! NOT using a feature selection!",selFile);
      return 0;
    }
    
    // read first line to determine filetype:
    char line[MAX_LINE_LENGTH+1];
    long nStr=0;
    fgets( line, 5, f);
    line[3] = 0;
    if (!strcmp(line,"str")) { // string list
      fselType = 2;
      SMILE_IDBG(5,"reading string list of features");
      fscanf( f, "%u\n", &nStr);
      if (nStr < 1) { COMP_ERR("Error reading feature selection file, nFeatures < 1!"); }
      outputSelStr.n = nStr;
      Nsel=nStr;
      outputSelStr.names = (char **)calloc(1,sizeof(char *)*nStr);
      int i=0; line[0] = 0;
      while(fgets(line,MAX_LINE_LENGTH,f) != NULL) {
        if (strlen( line ) > 1) { 
          line[strlen( line ) - 1] = 0;
          outputSelStr.names[i++] = strdup(line);
        }
      }
      SMILE_IDBG(5,"enabled %i features",i);
      fclose(f);
      return 1;
    } else if (!strcmp(line,"idx")) { // index list
      fselType = 1;
      SMILE_IDBG(5,"reading index list of features");
      long idx=0; int i;
      // pass1: parse for max index
      while(fscanf(f,"%u\n",&idx) == 1)
        outputSelIdx.nFull = MAX(outputSelIdx.nFull, idx);
      SMILE_IDBG(5,"max index in selection file was found to be %i",outputSelIdx.nFull);
      outputSelIdx.nFull++;
      outputSelIdx.enabled = (long *)calloc(1,sizeof(long)*outputSelIdx.nFull);
      rewind( f );
      fgets(line, 5, f); // skip header line;
      // pass2: parse for max index
      i=0;
      while(fscanf(f,"%u\n",&idx) == 1) {
        outputSelIdx.enabled[idx] = 1; // enable feature "idx"
        i++;
      }
      outputSelIdx.nSel = i;
      Nsel = i;
      SMILE_IDBG(5,"enabled %i features",i);
      fclose(f);
      return 1;
    } else { // bogus file...
      COMP_ERR("error parsing fselection file '%s'. bogus header! expected 'str' or 'idx' at beginning. found '%s'.",selFile,line);
      fclose( f );
    }
  }
  return 0;
}
int cTumkwsjSink::setupJulius()
{
  try {

    int argc=3;
    char* argv[3] = {"julius","-C",NULL};
    if (configfile != NULL)
      argv[2]=strdup(configfile);
    else
      argv[2]=strdup("kws.cfg");

    /* add application option dummies */
    /*
    j_add_option("-gprob", 1, 1, "garbage probability", opt_gprob);
    j_add_option("-kprob", 1, 1, "keyword probability", opt_kprob);
    j_add_option("-knum", 1, 1, "number of keywords", opt_knum);
    */  

    /* create a configuration variables container */
    jconf = j_jconf_new();
    //    j_config_load_file(jconf, strdup(configfile));
    if (j_config_load_args(jconf, argc, argv) == -1) {
      COMP_ERR("error parsing julius decoder options, this might be a bug. see tumkwsjSink.cpp!");
      j_jconf_free(jconf);
      free(argv[2]);
      return 0;
    }
    free(argv[2]);

    /* output system log to a file */
    if (getInt("debug") != 1) {
      jlog_set_output(NULL);
    }

    /* here you can set/modify any parameter in the jconf before setup */
    jconf->input.type = INPUT_VECTOR;
    jconf->input.speech_input = SP_EXTERN;
    jconf->decodeopt.realtime_flag = TRUE; // ??? 
    jconf->ext.period = (float)(reader->getLevelT());
    jconf->ext.veclen = reader->getLevelN();
    jconf->ext.userptr = (void *)this;
    jconf->ext.fv_read = external_fv_read_loader;

    /* Fixate jconf parameters: it checks whether the jconf parameters
    are suitable for recognition or not, and set some internal
    parameters according to the values for recognition.  Modifying
    a value in jconf after this function may be errorous.
    */
    if (j_jconf_finalize(jconf) == FALSE) {
      SMILE_IERR(1,"error finalising julius jconf in setupJulius()!");
      j_jconf_free(jconf);
      return 0;
    }

    /* create a recognition instance */
    recog = j_recog_new();
    /* use user-definable data hook to set a pointer to our class instance */
    recog->hook = (void *)this;
    /* assign configuration to the instance */
    recog->jconf = jconf;
    /* load all files according to the configurations */
    if (j_load_all(recog, jconf) == FALSE) {
      SMILE_IERR(1, "Error in loading model for Julius decoder");
      j_recog_free(recog);
      return 0;
    }

    SMILE_IMSG(2,"garbage prob.: %f",glogprob);
    SMILE_IMSG(2,"keyword prob.: %f",klogprob);
    SMILE_IMSG(2,"number of phonemes: %i",numphon);

    // register user LM, get vocab size, and init lmWeights with zero or load from file
    PROCESS_LM *lm; 
    for(lm=recog->lmlist;lm;lm=lm->next) {
      if (lm->lmtype == LM_PROB) {
        lm->winfo->userptr = (void*)this;  // PATCH: sent/vocabulary.h (WORD_INFO struct): ++   void * userptr;   // Pointer to userdata....
        j_regist_user_lm_func(lm, userlm_uni_loader, userlm_bi_loader, userlm_lm_loader);
        if ((numWords==0)&&((long)(lm->winfo->num)>0)) {
          lmWinfo = lm->winfo;
          numWords = (long)(lm->winfo->num);
        }
      }
    }

    // load lmWeights data:
    //printf("XXX HEREA");
    if (dynamicLm) {
      //printf("XXX HERE0");
      if (lmWinfo != NULL) {
        //printf("XXX HERE");
        lmWeights = (LOGPROB*)malloc(sizeof(LOGPROB)*numWords);
        if (lmWeights == NULL) { OUT_OF_MEMORY; }
        int i;
        for (i=0; i<numWords; i++) { lmWeights[i] = (LOGPROB)lmpenalty; }
        const char *lmfile = getStr("lmfile");
        if (lmfile != NULL) {
          FILE *lf=fopen(lmfile,"r");
          if (lf == NULL) { SMILE_IERR(1,"Error opening word weights file (lmfile) '%s'",lmfile); }
          else  {
            SMILE_IMSG(1,"lmfile: '%s'",lmfile);
            long lineNr = 0;
            char *line=NULL; size_t len=0;
            size_t r=-1;
            do {      
              r = getline(&line, &len, lf);
              //printf("XXX LINE '%s'",line);
              if ((r != (size_t)-1)&&(line!=NULL)) {
                lineNr++;
                parseLmWeightsLine(line,lineNr,lmfile);
              } 
            } while (r != (size_t)-1);
          }
        }

      } else { 
        SMILE_IERR(1,"no language model word info (vocabulary) found, check julius config!"); 
      }
    }
    //----

    /* checkout for recognition: build lexicon tree, allocate cache */
    if (j_final_fusion(recog) == FALSE) {
      fprintf(stderr, "ERROR: Error while setup work area for recognition\n");
      j_recog_free(recog);
      if (logfile) fclose(fp);
      return 0;
    }

    setupCallbacks(recog, NULL);

    /* output system information to log */
    j_recog_info(recog);

    terminated = FALSE;

  }
  catch (int) { }

  juliusIsSetup=1;

  return 1;
}
Beispiel #6
0
int cArffSink::myTick(long long t)
{
  if (filehandle == NULL) return 0;

  SMILE_DBG(4,"tick # %i, reading value vector (lag=%i):",t,lag);
  cVector *vec= reader_->getFrameRel(lag);  //new cVector(nValues+1);
  if (vec == NULL) return 0;
  //else reader->nextFrame();

  long vi = vec->tmeta->vIdx;
  double tm = vec->tmeta->time;
  double len = vec->tmeta->lengthSec;

  if (vec->tmeta->metadata.iData[1] == 1234) {
    instanceName = vec->tmeta->metadata.text;
    if (prname==1) {
      fprintf(filehandle,"%s,",instanceName);
    }
  } else {
    if (prname==1) {
      fprintf(filehandle,"'%s',",instanceName);
    } else if (prname==2) {
      fprintf(filehandle,"'%s_%i',",instanceBase,vi);
    }
  }

  if (number) fprintf(filehandle,"%i,",vi);
  if (timestamp) fprintf(filehandle,"%f,",tm+frameTimeAdd);
  if (frameLength) fprintf(filehandle,"%f,",len);
  
  // now print the vector:
  int i;
  fprintf(filehandle,"%e",vec->dataF[0]);
  for (i=1; i<vec->N; i++) {
    fprintf(filehandle,",%e",vec->dataF[i]);
    //printf("  (a=%i vi=%i, tm=%fs) %s.%s = %f\n",reader->getCurR(),vi,tm,reader->getLevelName(),vec->name(i),vec->dataF[i]);
  }

  // classes: 
  if ((vec->tmeta->metadata.iData[1] == 1234)&&(vec->tmeta->metadata.custom != NULL)&&(vec->tmeta->metadata.customLength > 0)&&(useTargetsFromMetadata)) {
    // TODO: check the order of the fields....
    fprintf(filehandle, ",%s", (const char *)(vec->tmeta->metadata.custom));
  } else {
    if (nClasses > 0) {
      if (nInst>0) {
        if (inr >= nInst) {
          SMILE_WRN(3,"more instances writte to ARFF file, then there are targets available for (%i)!",nInst);
          if (targetall != NULL) {
            for (i=0; i<nClasses; i++) {
              if (targetall[i] != NULL)
                fprintf(filehandle,",%s",targetall[i]);
              else
                fprintf(filehandle,",NULL");
            }
          } else {
            for (i=0; i<nClasses; i++) {
              fprintf(filehandle,",NULL");
            }
          }
          //inr++;
        } else {
          for (i=0; i<nClasses; i++) {
            fprintf(filehandle,",%s",targetinst[i][inr]);
          }
          inr++;
        }
      } else {
        if (targetall != NULL) {
          for (i=0; i<nClasses; i++) {
            if (targetall[i] != NULL)
              fprintf(filehandle,",%s",targetall[i]);
            else
              fprintf(filehandle,",NULL");
          }
        } else {
          for (i=0; i<nClasses; i++) {
            fprintf(filehandle,",NULL");
          }
        }
      }
    } else {
      // dummy class attribute, always 0
      if (printDefaultClassDummyAttribute) {
        fprintf(filehandle,",0");
      }
    }
  }

  fprintf(filehandle,"%s",NEWLINE);

  int err = fflush(filehandle);
  if (err == EOF) {
    SMILE_IERR(1,"error writing to file '%s' (code: %i)",filename,errno);
    COMP_ERR("aborting");
    return 0;
  }

  nWritten_++;

  // tick success
  return 1;
}
long cFunctionalModulation::process(FLOAT_DMEM *in, FLOAT_DMEM *inSorted, FLOAT_DMEM *out, long Nin, long Nout)
{
  if (mappedSpec_ == NULL) {
    // we need to init
    FLOAT_DMEM T = (FLOAT_DMEM)getInputPeriod();
    if (T == 0.0) {
      SMILE_IERR(1, "Cannot compute modulation spectrum when input period is unknown (asynchronous input level!). T = 0.0");
      return 0;
    }
    mappedSpec_ = new cSmileUtilMappedMagnitudeSpectrum(Nin, modSpecNumBins_,
        winFuncId_, modSpecMinFreq_, modSpecMaxFreq_, T);
    if (stftWinSizeFrames_ == 0 && T > 0) {
      stftWinSizeFrames_ = stftWinSizeSec_ / T;
      stftWinStepFrames_ = stftWinStepSec_ / T;
    }
  }
  if (avgModSpec_ == NULL) {
    avgModSpec_ = (FLOAT_DMEM*)malloc(sizeof(FLOAT_DMEM) * modSpecNumBins_);
  }
  if (removeNonZeroMean_) {
    if (inNormN_ != Nin) {
      if (inNorm_ != NULL) {
        free(inNorm_);
      }
      inNorm_ = NULL;
    }
    if (inNorm_ == NULL) {
      inNormN_ = Nin;
      inNorm_ = (FLOAT_DMEM*)malloc(sizeof(FLOAT_DMEM) * Nin);
    }
    FLOAT_DMEM mean = 0.0;
    long nMean = 0;
    for (long i = 0; i < Nin; i++) {
      if (in[i] != 0.0) {
        mean += in[i];
        nMean++;
      }
    }
    if (nMean > 0) {
      mean /= (FLOAT_DMEM)nMean;
    }
    for (long i = 0; i < Nin; i++) {
      if (in[i] != 0.0) {
        inNorm_[i] = in[i] - mean;
      } else {
        inNorm_[i] = 0.0;
      }
    }
    in = inNorm_;
  }
  if (stftWinSizeFrames_ == 0) {
    mappedSpec_->compute(in, Nin, false);
    const FLOAT_DMEM * ms = mappedSpec_->getModSpec();
    memcpy(avgModSpec_, ms, sizeof(FLOAT_DMEM) * modSpecNumBins_);
  } else {
    computeModSpecSTFTavg(in, Nin, (FLOAT_DMEM *)&avgModSpec_);
  }
  // do some transformations, like power, log

  // here would be also the place to apply ACF/CEPSTRUM/DCT etc.

  // copy the resulting modspec to the output vector
  for (int i = 0; i < modSpecNumBins_; i++) {
    out[i] = avgModSpec_[i];
  }
  return Nout;
}