/* We just learnt about a node, not necessarily a new one. Confirm is 1 if the node sent a message, 2 if it sent us a reply. */ static struct node* new_node(const unsigned char *id, const struct sockaddr *sa, int salen, int confirm){ struct bucket *b = find_bucket(id, sa->sa_family); struct node *n; int mybucket, split; if(b == NULL){ return NULL; } if(id_cmp(id, myid) == 0){ return NULL; } if(is_martian(sa) || node_blacklisted(sa, salen)){ return NULL; } mybucket = in_bucket(myid, b); if(confirm == 2){ b->time = now.tv_sec; } n = b->nodes; while(n){ if(id_cmp(n->id, id) == 0){ if(confirm || n->time < now.tv_sec - 15 * 60){ /* Known node. Update stuff. */ memcpy((struct sockaddr*)&n->ss, sa, salen); if(confirm){ n->time = now.tv_sec } if(confirm >= 2){ n->reply_time = now.tv_sec; n->pinged = 0; n->pinged_time = 0; } } return 0; } n = n->next; }
int dht_periodic(const void *buf, size_t buflen, const struct sockaddr *from, int fromlen, time_t *tosleep, dht_callback *callback, void *closure) { (...) // variáveis temporárias int message; unsigned char tid[16], id[20], info_hash[20], target[20]; unsigned char nodes[256], nodes6[1024], token[128]; int tid_len = 16, token_len = 128; int nodes_len = 256, nodes6_len = 1024; unsigned short port; unsigned char values[2048], values6[2048]; int values_len = 2048, values6_len = 2048; int want; (...) // Processa a mensagem recebida e identifica seu tipo. message = parse_message(buf, buflen, tid, &tid_len, id, info_hash, target, &port, token, &token_len, nodes, &nodes_len, nodes6, &nodes6_len, values, &values_len, values6, &values6_len, &want); (...) // Realiza os procedimentos conforme o tipo de mensagem recebida. switch (message) { (...) case FIND_NODE: // find_node: só envia os nós mais próximos ao 'target'. debugf("Find node!\n"); new_node(id, from, fromlen, 1); debugf("Sending closest nodes (%d).\n", want); send_closest_nodes(from, fromlen, tid, tid_len, target, want, 0, NULL, NULL, 0); break; case GET_PEERS: debugf("Get_peers!\n"); new_node(id, from, fromlen, 1); if (id_cmp(info_hash, zeroes) == 0) { (...) } else {
/* Process utterances in the control file (-ctlfn argument) */ static void process_ctlfile ( void ) { FILE *ctlfp, *sentfp, *mllrctlfp; char *ctlfile, *cepdir, *cepext, *sentfile, *outsentfile, *mllrctlfile; char line[1024], cepfile[1024], ctlspec[1024]; /* CHANGE BY BHIKSHA: ADDED veclen AS A VARIABLE, 6 JAN 98 */ int32 ctloffset, ctlcount, veclen, sf, ef, nfr; /* END OF CHANGES BY BHIKSHA */ char mllrfile[4096], prevmllr[4096], sent[16384]; char uttid[1024]; int32 i, k; float32 **mfc; ctlfile = (char *) cmd_ln_access("-ctlfn"); if ((ctlfp = fopen (ctlfile, "r")) == NULL) E_FATAL("fopen(%s,r) failed\n", ctlfile); if ((mllrctlfile = (char *) cmd_ln_access("-mllrctlfn")) != NULL) { if ((mllrctlfp = fopen (mllrctlfile, "r")) == NULL) E_FATAL("fopen(%s,r) failed\n", mllrctlfile); } else mllrctlfp = NULL; prevmllr[0] = '\0'; sentfile = (char *) cmd_ln_access("-insentfn"); if ((sentfp = fopen (sentfile, "r")) == NULL) E_FATAL("fopen(%s,r) failed\n", sentfile); if ((outsentfile = (char *) cmd_ln_access("-outsentfn")) != NULL) { if ((outsentfp = fopen (outsentfile, "w")) == NULL) E_FATAL("fopen(%s,r) failed\n", outsentfile); } E_INFO("Processing ctl file %s\n", ctlfile); cepdir = (char *) cmd_ln_access("-cepdir"); cepext = (char *) cmd_ln_access("-cepext"); assert ((cepdir != NULL) && (cepext != NULL)); /* BHIKSHA: ADDING VECLEN TO ALLOW VECTORS OF DIFFERENT SIZES */ veclen = *((int32 *) cmd_ln_access("-ceplen")); /* END CHANGES, 6 JAN 1998, BHIKSHA */ ctloffset = *((int32 *) cmd_ln_access("-ctloffset")); if (! cmd_ln_access("-ctlcount")) ctlcount = 0x7fffffff; /* All entries processed if no count specified */ else ctlcount = *((int32 *) cmd_ln_access("-ctlcount")); if (ctlcount == 0) { E_INFO("-ctlcount argument = 0!!\n"); fclose (ctlfp); fclose (sentfp); if (outsentfp) fclose (outsentfp); return; } /* Skipping initial offset */ if (ctloffset > 0) E_INFO("Skipping %d utterances in the beginning of control file\n", ctloffset); while ((ctloffset > 0) && (fgets(line, sizeof(line), ctlfp) != NULL)) { if (sscanf (line, "%s", ctlspec) > 0) { if (fgets (sent, sizeof(sent), sentfp) == NULL) { E_ERROR("EOF(%s)\n", sentfile); ctlcount = 0; break; } if (mllrctlfp) { if (fscanf (mllrctlfp, "%s", mllrfile) != 1) E_FATAL ("Unexpected EOF(%s)\n", mllrctlfile); } --ctloffset; } } /* Process the specified number of utterance or until end of control file */ while ((ctlcount > 0) && (fgets(line, sizeof(line), ctlfp) != NULL)) { printf ("\n"); E_INFO("Utterance: %s", line); sf = 0; ef = (int32)0x7ffffff0; if ((k = sscanf (line, "%s %d %d %s", ctlspec, &sf, &ef, uttid)) <= 0) continue; /* Empty line */ if ((k == 2) || ( (k >= 3) && ((sf >= ef) || (sf < 0))) ) E_FATAL("Bad ctlfile line: %s\n", line); if (k < 4) { /* Create utt-id from mfc-filename (and sf/ef if specified) */ for (i = strlen(ctlspec)-1; (i >= 0) && (ctlspec[i] != '/'); --i); if (k == 3) sprintf (uttid, "%s_%d_%d", ctlspec+i+1, sf, ef); else strcpy (uttid, ctlspec+i+1); } if (mllrctlfp) { if (fscanf (mllrctlfp, "%s", mllrfile) != 1) E_FATAL ("Unexpected EOF(%s)\n", mllrctlfile); if (strcmp (prevmllr, mllrfile) != 0) { float32 ***A, **B; int32 gid, sid; uint8 *mgau_xform; gauden_mean_reload (g, (char *) cmd_ln_access("-meanfn")); if (mllr_read_regmat (mllrfile, &A, &B, featlen, n_feat) < 0) E_FATAL("mllr_read_regmat failed\n"); mgau_xform = (uint8 *) ckd_calloc (g->n_mgau, sizeof(uint8)); /* Transform each non-CI mixture Gaussian */ for (sid = 0; sid < sen->n_sen; sid++) { if (mdef->cd2cisen[sid] != sid) { /* Otherwise it's a CI senone */ gid = sen->mgau[sid]; if (! mgau_xform[gid]) { mllr_norm_mgau (g->mean[gid], g->n_density, A, B, featlen, n_feat); mgau_xform[gid] = 1; } } } ckd_free (mgau_xform); mllr_free_regmat (A, B, featlen, n_feat); strcpy (prevmllr, mllrfile); } } if (ctlspec[0] != '/') sprintf (cepfile, "%s/%s.%s", cepdir, ctlspec, cepext); else sprintf (cepfile, "%s.%s", ctlspec, cepext); /* Read utterance transcript */ if (fgets (sent, sizeof(sent), sentfp) == NULL) { E_ERROR("EOF(%s)\n", sentfile); break; } /* Strip utterance id from the end of the transcript */ for (k = strlen(sent) - 1; (k > 0) && ((sent[k] == '\n') || (sent[k] == '\t') || (sent[k] == ' ')); --k); if ((k > 0) && (sent[k] == ')')) { for (--k; (k >= 0) && (sent[k] != '('); --k); if ((k >= 0) && (sent[k] == '(')) { sent[k] = '\0'; /* Check that uttid in transcript and control file match */ for (i = ++k; sent[i] && (sent[i] != ')') && (sent[i] != '\n') && (sent[i] != '\t') && (sent[i] != ' '); i++); sent[i] = '\0'; if (id_cmp (sent+k, uttid) != 0) E_ERROR("Uttid mismatch: ctlfile = \"%s\"; transcript = \"%s\"\n", uttid, sent+k); } } /* Read and process mfc file */ /* CHANGE BY BHIKSHA; PASSING VECLEN TO s2mfc_read(), 6 JAN 98 */ /* Read mfc file */ /* HACK HACKA HACK BHIKSHA */ { int32 asf, aef; asf = sf; sf = asf - 4; aef = ef; ef = aef + 4; if (sf < 0 ) { E_ERROR("Utterance %s begin %d < 4; ignored\n", uttid, asf); return; } if ((nfr = s2mfc_read (cepfile, sf, ef, &mfc, veclen)) <= 0) E_ERROR("Utt %s: MFC file read (%s) failed\n", uttid, cepfile); /* END CHANGES BY BHIKSHA */ else { E_INFO ("%d mfc frames\n", nfr-8); /* -8 HACK HACKA HACK */ /* Align utterance */ align_utt (sent, mfc+4, nfr-8, ctlspec, uttid); /* +4 HACKA HACK */ } } /* END HACK HACKA HACK */ --ctlcount; } printf ("\n"); while (fgets(line, sizeof(line), ctlfp) != NULL) { if (sscanf (line, "%s", ctlspec) > 0) { E_INFO("Skipping rest of control file beginning with:\n\t%s", line); break; } } fclose (ctlfp); fclose (sentfp); if (outsentfp) fclose (outsentfp); if (mllrctlfp) fclose (mllrctlfp); }
static void utt_align(void *data, utt_res_t * ur, int32 sf, int32 ef, char *uttid) { int32 nfr; int k, i; const char *cepdir; const char *cepext; char sent[16384]; cmd_ln_t *config = (cmd_ln_t*) data; cepdir = cmd_ln_str_r(kbc->config, "-cepdir"); cepext = cmd_ln_str_r(kbc->config, "-cepext"); /* UGLY! */ /* Read utterance transcript and match it with the control file. */ if (fgets(sent, sizeof(sent), sentfp) == NULL) { E_FATAL("EOF(%s) of the transcription\n", sentfile); } /* E_INFO("SENT %s\n",sent); */ /* Strip utterance id from the end of the transcript */ for (k = strlen(sent) - 1; (k > 0) && ((sent[k] == '\n') || (sent[k] == '\t') || (sent[k] == ' ')); --k); if ((k > 0) && (sent[k] == ')')) { for (--k; (k >= 0) && (sent[k] != '('); --k); if ((k >= 0) && (sent[k] == '(')) { sent[k] = '\0'; /* Check that uttid in transcript and control file match */ for (i = ++k; sent[i] && (sent[i] != ')') && (sent[i] != '\n') && (sent[i] != '\t') && (sent[i] != ' '); i++); sent[i] = '\0'; if (id_cmp(sent + k, uttid) != 0) E_ERROR ("Uttid mismatch: ctlfile = \"%s\"; transcript = \"%s\"\n", uttid, sent + k); } } /* Convert input file to cepstra if waveform input is selected */ if (cmd_ln_boolean_r(config, "-adcin")) { int16 *adcdata; int32 nsamps = 0; mfcc_t **mfcc; if ((adcdata = bio_read_wavfile(cmd_ln_str_r(config, "-cepdir"), ur->uttfile, cmd_ln_str_r(config, "-cepext"), cmd_ln_int32_r(config, "-adchdr"), strcmp(cmd_ln_str_r(config, "-input_endian"), "big"), &nsamps)) == NULL) { E_FATAL("Cannot read file %s\n", ur->uttfile); } fe_start_utt(fe); if (fe_process_utt(fe, adcdata, nsamps, &mfcc, &nfr) < 0) { E_FATAL("MFCC calculation failed\n", ur->uttfile); } ckd_free(adcdata); if (nfr > S3_MAX_FRAMES) { E_FATAL("Maximum number of frames (%d) exceeded\n", S3_MAX_FRAMES); } if ((nfr = feat_s2mfc2feat_live(kbcore_fcb(kbc), mfcc, &nfr, TRUE, TRUE, feat)) < 0) { E_FATAL("Feature computation failed\n"); } if (mfcc) ckd_free_2d((void **)mfcc); } else { nfr = feat_s2mfc2feat(kbcore_fcb(kbc), ur->uttfile, cepdir, cepext, sf, ef, feat, S3_MAX_FRAMES); } if (ur->regmatname) { if (kbc->mgau) adapt_set_mllr(adapt_am, kbc->mgau, ur->regmatname, ur->cb2mllrname, kbc->mdef, kbc->config); else if (kbc->ms_mgau) model_set_mllr(kbc->ms_mgau, ur->regmatname, ur->cb2mllrname, kbcore_fcb(kbc), kbc->mdef, kbc->config); else E_WARN("Can't use MLLR matrices with .s2semi. yet\n"); } if (nfr <= 0) { if (cepdir != NULL) { E_ERROR ("Utt %s: Input file read (%s) with dir (%s) and extension (%s) failed \n", uttid, ur->uttfile, cepdir, cepext); } else { E_ERROR ("Utt %s: Input file read (%s) with extension (%s) failed \n", uttid, ur->uttfile, cepext); } } else { E_INFO("%s: %d input frames\n", uttid, nfr); align_utt(sent, nfr, ur->uttfile, uttid); } }