Exemplo n.º 1
0
void print_word_token(srec* rec, wtokenID wtoken_index, char* msg)
{
  bigcostdata cost, cost_for_word;
  char *p = "NULL";
  word_token* wtoken = &rec->word_token_array[wtoken_index];

  PLogMessage ( msg );
  if (wtoken->word < rec->context->olabels->num_words)
    p = rec->context->olabels->words[wtoken->word];
  ASSERT(rec->accumulated_cost_offset[ wtoken->end_time] != 0);
  cost = wtoken->cost + rec->accumulated_cost_offset[wtoken->end_time];
  if (wtoken->backtrace != MAXwtokenID)
  {
    word_token* next_wtoken = &rec->word_token_array[wtoken->backtrace];
    cost_for_word = cost - next_wtoken->cost - rec->accumulated_cost_offset[next_wtoken->end_time];
  }
  else
  {
    cost_for_word = cost;
  }
  printf("wtoken %d W%i %s cost=%d/%d/%d time=%d/%d node=%d", wtoken_index,
         wtoken->word, p, wtoken->cost, cost, cost_for_word, wtoken->end_time, WORD_TOKEN_GET_WD_ETIME(wtoken), wtoken->end_node);
  pfflush(PSTDOUT);
  print_word_token_backtrace(rec, wtoken->backtrace, "\n");
}
ESR_ReturnCode SR_RecognizerResult_GetWaveform(const SR_RecognizerResult* self, 
																							 const asr_int16_t** waveform, size_t* size)
{
  SR_RecognizerResultImpl* impl = (SR_RecognizerResultImpl*) self;
  
  if (waveform == NULL)
  {
    PLogError(L("ESR_INVALID_ARGUMENT"));
    return ESR_INVALID_ARGUMENT;
  }
  
  // just point to the circular buffer read start point
  if (impl->recogImpl->waveformBuffer->overflow_count == 0)
  {
    *waveform = (asr_int16_t*)(((unsigned char *) impl->recogImpl->waveformBuffer->cbuffer) + 
      sizeof(CircularBuffer) + impl->recogImpl->waveformBuffer->cbuffer->readIdx);
    
    *size = impl->recogImpl->waveformBuffer->read_size;
    return ESR_SUCCESS;
  }
  else
  {
    PLogMessage(L("Warning: Voice Enrollment audio buffer overflow (spoke too much, over by %d bytes)\n"),
                impl->recogImpl->waveformBuffer->overflow_count);
                
    *waveform = (asr_int16_t*)(((unsigned char *) impl->recogImpl->waveformBuffer->cbuffer) + sizeof(CircularBuffer) + impl->recogImpl->waveformBuffer->cbuffer->readIdx);
    *size = impl->recogImpl->waveformBuffer->read_size;
    return ESR_SUCCESS;
  }
}
Exemplo n.º 3
0
CA_Recog *CA_AllocateRecognition()
{
  CA_Recog *hRecog = NULL;

#ifdef SREC_ENGINE_VERBOSE_LOGGING
  PLogMessage("in CA_AllocateRecognition\n");
#endif

  TRY_CA_EXCEPT


  /* CREC_SET_SIGNATURE must be 'tied' to the
   * actual creation of the recog_info structure.
   * Any methods which take 'recog_info' as an argument,
   * even 'destroy_recognition()' will test the signature!
   */
  hRecog = (CA_Recog *) CALLOC_CLR(1, sizeof(CA_Recog), "ca.hRecog");

  hRecog->setup_count = 0;
  hRecog->is_running = False;
  hRecog->is_configured = False;
  hRecog->is_resultBlocked = False;
  hRecog->ca_rtti = CA_RECOGNIZER_SIGNATURE;

  hRecog->recm = (multi_srec*)CALLOC_CLR(1, sizeof(multi_srec), "ca.hRecog.srec");
  return (hRecog);

  BEG_CATCH_CA_EXCEPT
  END_CATCH_CA_EXCEPT(hRecog)
}
Exemplo n.º 4
0
void create_lookup_logadd(logadd_table_info *logtab, float mul_scale)
{
  int ii;

  ASSERT(logtab);
  ASSERT(logtab->scale != 0);

  logtab->logscale = mul_scale;
  logtab->add_log_limit = (prdata)(ADD_LOG_LIMIT * mul_scale * logtab->scale);
  if( DO_USE_LOGTAB__TABLE) {
	  logtab->table = logtab__table;
  } else {
	prdata* table = (prdata *) CALLOC(logtab->add_log_limit + 2, sizeof(prdata), "clib.logadd");
	logtab->table = table;
	PRINT_SOME_CODE( "/* values for default case %d */\n", 0);
	PRINT_SOME_CODE( "#define DO_USE_LOGTAB__TABLE (logtab->scale == %d  \\\n", logtab->scale);
    PRINT_SOME_CODE( "    && ( (int)(logtab->logscale*10000) == (int)(%ff*10000) \\\n", logtab->logscale);
	PRINT_SOME_CODE( "    && ADD_LOG_LIMIT == %d) \n", ADD_LOG_LIMIT);
	PRINT_SOME_CODE( "static prdata logtab__table[%d] = {", logtab->add_log_limit + 2);
	for (ii = 0; ii <= logtab->add_log_limit; ii++) {
		table[ii] = (prdata)(logtab->scale * mul_scale * log(1
									+ exp(-(float)ii / ((float)logtab->scale * mul_scale)))
									+ 0.5) ;
		PRINT_SOME_CODE(" %d,", table[ii]);
	}
	PRINT_SOME_CODE( ", 0 }; /*%i*/\n", 0); /*not sure why the +2 above, just append 0 for now*/
  }
#ifdef SREC_ENGINE_VERBOSE_LOGGING
  PLogMessage("L: log table scale is %f\n", (float)logtab->scale);
  PLogMessage("L: log table has %d entries\n", (int)logtab->add_log_limit);
#endif
#if DEBUG
  for (ii = 0; ii <= logtab->add_log_limit; ii++)
    log_report("T: %d %f\n", ii, (float)logtab->table[ii]);
#endif
#if REPORT_USAGE
  report_malloc_usage();
#endif
  return;
}
Exemplo n.º 5
0
static void allocate_recognition1(srec *rec,
                                  int viterbi_prune_thresh,  /*score-based pruning threshold - only keep paths within this delta of best cost*/
                                  int max_hmm_tokens,
                                  int max_fsmnode_tokens,
                                  int max_word_tokens,
                                  int max_altword_tokens,
                                  int num_wordends_per_frame,
                                  int max_frames,
                                  int max_model_states)
{
#ifdef SREC_ENGINE_VERBOSE_LOGGING
    PLogMessage("allocating recognition arrays2 prune %d max_hmm_tokens %d max_fsmnode_tokens %d max_word_tokens %d max_altword_tokens %d max_wordends_per_frame %d\n",
                viterbi_prune_thresh,
                max_hmm_tokens,
                max_fsmnode_tokens,
                max_word_tokens,
                max_altword_tokens,
                num_wordends_per_frame);
#endif
    rec->current_model_scores = (costdata*) CALLOC_CLR(max_model_states, sizeof(costdata), "search.srec.current_model_scores"); /*FIX - either get NUM_MODELS from acoustic models, or check this someplace to make sure we have enough room*/
    rec->num_model_slots_allocated = (modelID)max_model_states;

    rec->fsmarc_token_array_size = (stokenID)max_hmm_tokens;

    rec->fsmarc_token_array = (fsmarc_token*) CALLOC_CLR(rec->fsmarc_token_array_size , sizeof(fsmarc_token), "search.srec.fsmarc_token_array");
    rec->max_new_states = (stokenID)max_hmm_tokens;

    rec->word_token_array = (word_token*) CALLOC_CLR(max_word_tokens, sizeof(word_token), "search.srec.word_token_array");
    rec->word_token_array_size = (wtokenID)max_word_tokens;
    /* todo: change this to a bit array later */
    rec->word_token_array_flags = (asr_int16_t*) CALLOC_CLR(max_word_tokens, sizeof(asr_int16_t), "search.srec.word_token_array_flags");

    rec->fsmnode_token_array = (fsmnode_token*) CALLOC_CLR(max_fsmnode_tokens, sizeof(fsmnode_token), "search.srec.fsmnode_token_array");
    rec->fsmnode_token_array_size = (ftokenID)max_fsmnode_tokens;

    rec->altword_token_array = (altword_token*) CALLOC_CLR(max_altword_tokens, sizeof(altword_token), "search.srec.altword_token_array");
    rec->altword_token_array_size = (wtokenID)max_altword_tokens;

    rec->prune_delta = (costdata)viterbi_prune_thresh;

    rec->max_frames   = (frameID)max_frames;
    rec->best_model_cost_for_frame = (costdata*)CALLOC_CLR(max_frames, sizeof(costdata), "search.srec.best_model_cost_for_frame");
    rec->word_lattice = allocate_word_lattice((frameID)max_frames);

    rec->word_priority_q = allocate_priority_q(num_wordends_per_frame);
    rec->best_fsmarc_token = MAXstokenID;

#define ASTAR_NBEST_LEN 10
    rec->astar_stack = astar_stack_make(rec, ASTAR_NBEST_LEN);
    rec->context = NULL;
}
Exemplo n.º 6
0
void print_word_token_backtrace(srec* rec, wtokenID wtoken_index, char* tail)
{
  char* null = "NULL", *p;
  char iwttime[8] = { 0, 0, 0, 0, 0, 0, 0, 0};
  bigcostdata cost;
  bigcostdata cost_for_word;
  word_token *wtoken, *last_wtoken;

  last_wtoken = NULL;
  while (wtoken_index != MAXwtokenID)
  {
    wtoken = &rec->word_token_array[wtoken_index];
    if (wtoken->word < rec->context->olabels->num_words)
      p = rec->context->olabels->words[wtoken->word];
    else
      p = null;
    ASSERT(!last_wtoken || last_wtoken->end_time > wtoken->end_time);
    ASSERT(rec->accumulated_cost_offset[ wtoken->end_time] != 0);
    cost = wtoken->cost + rec->accumulated_cost_offset[ wtoken->end_time];

    if (wtoken->backtrace != MAXwtokenID)
    {
      word_token* next_wtoken = &rec->word_token_array[wtoken->backtrace];
      cost_for_word = cost - next_wtoken->cost - rec->accumulated_cost_offset[ next_wtoken->end_time];
    }
    else
    {
      cost_for_word = cost;
    }
    sprintf(iwttime, "/%d", WORD_TOKEN_GET_WD_ETIME(wtoken) );
    PLogMessage (" (%d W%d %s cost=%d/%d/%d time=%d%s node=%d)", wtoken_index, wtoken->word, p, wtoken->cost, cost, cost_for_word, wtoken->end_time, iwttime, wtoken->end_node);
    fflush(stdout);
    ASSERT(wtoken->backtrace != wtoken_index);
    wtoken_index = wtoken->backtrace;
    last_wtoken = wtoken;
  }
  PLogMessage (tail);
}
Exemplo n.º 7
0
void print_word_token_list(srec* rec, wtokenID wtoken_index, char* msg)
{
#ifndef NDEBUG
  int sanity_counter = 0;
#endif
  PLogMessage ( msg );
  while (wtoken_index != MAXwtokenID)
  {
    word_token* wtoken = &rec->word_token_array[wtoken_index];
    print_word_token(rec, wtoken_index, "");
    ASSERT(sanity_counter++ < 200);
    ASSERT(wtoken_index != wtoken->next_token_index);
    wtoken_index = wtoken->next_token_index;
  }
}
Exemplo n.º 8
0
const SWIModel* load_swimodel(const char *filename)
{
  int i;
  SWIModel *swimodel = NULL;
  const void* file = NULL;

#ifdef SREC_ENGINE_VERBOSE_LOGGING
  PLogMessage("load_swimodel: loaded %s", filename);
#endif
  swimodel = (SWIModel*) CALLOC(1, sizeof(SWIModel), "clib.models.base");

  if (mmap_zip(filename, &swimodel->mmap_zip_data, &swimodel->mmap_zip_size)) {
      PLogError("load_swimodel: mmap_zip failed for %s\n", filename);
      goto CLEANUP;
  }
  file = swimodel->mmap_zip_data;

  swimodel->num_hmmstates = *(const short*)file;
  file += sizeof(short);
  swimodel->num_dims = *(const short*)file;
  file += sizeof(short);
  swimodel->num_pdfs = *(const short*)file;
  file += sizeof(short);

  SWIhmmState* hmmstates = (SWIhmmState*) CALLOC(swimodel->num_hmmstates, sizeof(SWIhmmState), "clib.models.states");
  swimodel->hmmstates = hmmstates;

  const short* num_pdfs_in_model = (const short*)file;
  file += sizeof(short) * swimodel->num_hmmstates;

  swimodel->allmeans = (const featdata*)file;
  file += sizeof(featdata) * swimodel->num_pdfs * swimodel->num_dims;

  swimodel->allweights = (const wtdata*)file;
  file += sizeof(wtdata) * swimodel->num_pdfs;

  swimodel->avg_state_durations = (const featdata*)file;
  file += sizeof(featdata) * swimodel->num_hmmstates;

  if (file > swimodel->mmap_zip_data + swimodel->mmap_zip_size) {
      PLogError("load_swimodel: not enough data in %s", filename);
      goto CLEANUP;
  }

#ifdef SREC_ENGINE_VERBOSE_LOGGING
  PLogMessage("loaded models %s num_hmmstates %d num_dims %d num_pdfs %d weights[0] %d\n",
              filename, swimodel->num_hmmstates, swimodel->num_dims, swimodel->num_pdfs,
              *swimodel->allweights);
#endif

  const featdata* mean_ptr = swimodel->allmeans;
  const wtdata* weight_ptr = swimodel->allweights;

  for (i = 0;i < swimodel->num_hmmstates;i++)
  {
    hmmstates[i].num_pdfs = num_pdfs_in_model[i];
    hmmstates[i].means = mean_ptr;
    hmmstates[i].weights = weight_ptr;
    mean_ptr += swimodel->num_dims * num_pdfs_in_model[i];
    weight_ptr += num_pdfs_in_model[i];
  }

  return swimodel;

CLEANUP:
  free_swimodel(swimodel);
  return NULL;
}
Exemplo n.º 9
0
int init_newton_transform(preprocessed *prep, float reqscale,
                          char *filename, int dimen)
/*
*/
{
  int  ii, jj;
  unsigned short matdim;
  double scale, onerow[MAX_DIMEN];
  PFile* dfpt;
  long foffset;
  double xfp;
  /* Open file
  */
  ASSERT(prep);
  ASSERT(filename);
  dfpt = file_must_open(NULL, filename, ("rb"), ESR_TRUE);
  prep->post_proc |= LIN_TRAN;
  prep->use_dim = dimen;
  pfread(&matdim, sizeof(short), 1, dfpt);
  if (matdim > MAX_DIMEN)
    SERVICE_ERROR(BAD_IMELDA);

  create_linear_transform(prep, matdim, 1);
  pfread(&scale, sizeof(double), 1, dfpt);

  if (reqscale != 0) scale = reqscale;
#if DEBUG
  PLogMessage("L: LDA Suggested scale is %.1f\n", scale);
#endif
  if (!prep->dim) prep->dim = matdim;
  else if (prep->dim != matdim)
  {
    log_report("Data (%d) and LDA (%d) dimensions don't match\n",
               prep->dim, matdim);
    SERVICE_ERROR(BAD_IMELDA);
  }

  /*  Eigenvalues, ignored
  */
  pfread(onerow, sizeof(double), matdim, dfpt);

  /*  Translation Vector
  */
  pfread(onerow, sizeof(double), matdim, dfpt);
  for (ii = 0; ii < matdim; ii++)
  {
    xfp = scale * (onerow[ii] - UTB_MEAN) + UTB_MEAN;
    if (xfp > 0.0)
      xfp += 0.5;
    else if (xfp < 0.0)
      xfp -= 0.5;

    prep->offset[ii] = (imeldata) xfp;
  }

  /*  The imelda matrix
  */
  for (ii = 0; ii < matdim; ii++)
  {
    pfread(onerow, sizeof(double), matdim, dfpt);
    for (jj = 0; jj < matdim; jj++)
      prep->imelda[ii][jj] = (covdata)(scale * onerow[jj]);
  }

  prep->imel_shift = scale_matrix_for_fixedpoint(prep->matrix,
                     prep->imelda, matdim);

  /* The inverse imelda matrix
   */
  foffset = pftell(dfpt);
  pfread(onerow, sizeof(double), matdim, dfpt);

  if (pfeof(dfpt) != 0)
  {
#ifdef SREC_ENGINE_VERBOSE_LOGGING
    PLogMessage("W: Inverting imelda matrix");
#endif
    invert_matrix(prep->imelda, prep->inverse, prep->dim);
  }
  else
  {
    pfseek(dfpt, foffset, SEEK_SET);

    for (ii = 0; ii < matdim; ii++)
    {
      pfread(onerow, sizeof(double), matdim, dfpt);
      for (jj = 0; jj < matdim; jj++)
        prep->inverse[ii][jj] = (covdata)(onerow[jj] / scale);
    }
  }

  prep->inv_shift = scale_matrix_for_fixedpoint(prep->invmat,
                    prep->inverse, matdim);

  pfclose(dfpt);
  return (0);
}
Exemplo n.º 10
0
int CA_ComputeConfidenceValues(CA_ConfidenceScorer* hConfidenceScorer, CA_Recog* recog,
                               CA_NBestList *nbestlist)
{
    float features[12];
    double value=1.0, final_value, confidence_value;
    double confidence_feature, confidence_feature_weighted;
    int i, num_features,current_choice;
    int rc, error_check;
    int num_choices,num_choices_left;

    confidence_value = 1.0;
    num_choices_left = num_choices = srec_nbest_get_num_choices(nbestlist);

    for(current_choice=0; current_choice<num_choices; current_choice++)
    {
        confidence_value = 1.0;
        rc = CA_ConfScorerGetFeatures(recog, nbestlist, features, &num_features, current_choice, num_choices_left);
        if (rc)
        {
            PLogError("confscor failed\n");
            error_check = srec_nbest_put_confidence_value(nbestlist, current_choice, 0);
            if(error_check)
                return 1;
            num_choices_left--;
            continue;
        }

        if (num_choices_left == 1)
        {
            for (i=0; i<NUM_CONF_FEATURES; i++) {
                if(i==CONF_FEATURE_SCORE_DIFF || i==CONF_FEATURE_SCORE_DIFF13) {
                    confidence_feature_weighted = 1.0;
                }
                else {
                    confidence_feature = 1.0/(1.0 + exp((hConfidenceScorer->one_nbest.scale[i] *
                                                         features[i]) + hConfidenceScorer->one_nbest.offset[i]));
                    confidence_feature_weighted = pow(confidence_feature,hConfidenceScorer->one_nbest.weight[i]);
                }
                confidence_value = confidence_value * confidence_feature_weighted;
            }
        }
        else
        {
            for (i=0; i<NUM_CONF_FEATURES; i++) {
                confidence_feature = 1.0/(1.0 + exp((hConfidenceScorer->many_nbest.scale[i] *
                                                     features[i]) + hConfidenceScorer->many_nbest.offset[i]));
                confidence_value = confidence_value * pow(confidence_feature, hConfidenceScorer->many_nbest.weight[i]);
            }
        }

        value *= confidence_value;
        final_value = 1000.0 * value;
        error_check = srec_nbest_put_confidence_value(nbestlist, current_choice, (int)final_value);
        if(error_check)
            return 1;
        num_choices_left--;
    }
    num_choices_left = srec_nbest_fix_homonym_confidence_values( nbestlist);
#ifdef SREC_ENGINE_VERBOSE_LOGGING
    PLogMessage("confidence %d features ", (int)final_value);
    for (i = 0; i < num_features; i++)
        PLogMessage(" %s %f", conf_feature_names[i], features[i]);
#endif

    return 0;
}