int audio_close_wince(cst_audiodev *ad) { au_wince_pdata *pd = ad->platform_data; MMRESULT err; if (ad) { /* Okay, I actually think this isn't a race, because bcnt is only ever decremented asynchronously, and the asynchronous callback can't be interrupted. So the only issue is whether it hits zero between the time we test it and the time we start waiting, and in this case, the event will get set anyway. */ if (pd->bcnt > 0) WaitForSingleObject(pd->bevt, INFINITE); pd->in_reset = 1; err = waveOutReset(pd->wo); if (err != MMSYSERR_NOERROR) { cst_errmsg("Failed to reset output device: %x\n", err); cst_error(); } pd->in_reset = 0; free_queue_empty(ad); err = waveOutClose(pd->wo); if (err != MMSYSERR_NOERROR) { cst_errmsg("Failed to close output device: %x\n", err); cst_error(); } cst_free(pd); cst_free(ad); } return 0; }
int audio_write_wince(cst_audiodev *ad, void *samples, int num_bytes) { au_wince_pdata *pd = ad->platform_data; WAVEHDR *hdr; MMRESULT err; if (num_bytes == 0) return 0; hdr = cst_alloc(WAVEHDR,1); hdr->lpData = cst_alloc(char,num_bytes); memcpy(hdr->lpData,samples,num_bytes); hdr->dwBufferLength = num_bytes; err = waveOutPrepareHeader(pd->wo, hdr, sizeof(*hdr)); if (err != MMSYSERR_NOERROR) { cst_errmsg("Failed to prepare header %p: %x\n", hdr, err); cst_error(); } if (InterlockedIncrement(&pd->bcnt) == 8) WaitForSingleObject(pd->wevt, INFINITE); err = waveOutWrite(pd->wo, hdr, sizeof(*hdr)); if (err != MMSYSERR_NOERROR) { cst_errmsg("Failed to write header %p: %x\n", hdr, err); cst_error(); } return num_bytes; }
cst_audiodev *audio_open_wince(int sps, int channels, int fmt) { cst_audiodev *ad; au_wince_pdata *pd; HWAVEOUT wo; WAVEFORMATEX wfx; MMRESULT err; ad = cst_alloc(cst_audiodev,1); ad->sps = ad->real_sps = sps; ad->channels = ad->real_channels = channels; ad->fmt = ad->real_fmt = fmt; memset(&wfx,0,sizeof(wfx)); wfx.nChannels = channels; wfx.nSamplesPerSec = sps; switch (fmt) { case CST_AUDIO_LINEAR16: wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wBitsPerSample = 16; break; case CST_AUDIO_LINEAR8: wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wBitsPerSample = 8; break; default: cst_errmsg("audio_open_wince: unsupported format %d\n", fmt); cst_free(ad); cst_error(); } wfx.nBlockAlign = wfx.nChannels*wfx.wBitsPerSample/8; wfx.nAvgBytesPerSec = wfx.nSamplesPerSec*wfx.nBlockAlign; err = waveOutOpen( &wo, WAVE_MAPPER, &wfx, (DWORD_PTR)sndbuf_done, (DWORD_PTR)ad, CALLBACK_FUNCTION ); if (err != MMSYSERR_NOERROR) { cst_errmsg("Failed to open output device: %x\n", err); cst_free(ad); cst_error(); } pd = cst_alloc(au_wince_pdata,1); pd->wo = wo; pd->bevt = CreateEvent(NULL,FALSE,FALSE,NULL); pd->wevt = CreateEvent(NULL,FALSE,FALSE,NULL); pd->bcnt = 0; ad->platform_data = pd; return ad; }
size_t cst_regsub(const cst_regstate *state, const char *in, char *out, size_t max) { const char *src; char *dst; int c, no, len; size_t count; if (state == NULL || in == NULL) { cst_errmsg("NULL parm to regsub\n"); cst_error(); } src = in; dst = out; count = 0; while ((c = *src++) != '\0') { if (out && dst + 1 > out + max - 1) break; if (c == '&') no = 0; else if (c == '\\' && '0' <= *src && *src <= '9') no = *src++ - '0'; else no = -1; if (no < 0) { /* Ordinary character. */ if (c == '\\' && (*src == '\\' || *src == '&')) c = *src++; if (out) *dst++ = c; count++; } else if (state->startp[no] != NULL && state->endp[no] != NULL) { len = state->endp[no] - state->startp[no]; if (out) { if (dst + len > out + max - 1) len = (out + max - 1) - dst; strncpy(dst, state->startp[no], len); dst += len; /* strncpy hit NUL. */ if (len != 0 && *(dst-1) == '\0') { cst_errmsg("damaged match string"); cst_error(); } } count += len; } } if (out && dst - out + 1 < max) *dst++ = '\0'; return count; }
static double get_gauss_dia(long clsidx, DVECTOR vec, // [dim] DVECTOR detvec, // [clsnum] DMATRIX weightmat, // [clsnum][1] DMATRIX meanmat, // [clsnum][dim] DMATRIX invcovmat) // [clsnum][dim] { double gauss, sb; long k; if (detvec->data[clsidx] <= 0.0) { cst_errmsg("#error: det <= 0.0\n"); cst_error(); } for (k = 0, gauss = 0.0; k < vec->length; k++) { sb = vec->data[k] - meanmat->data[clsidx][k]; gauss += sb * invcovmat->data[clsidx][k] * sb; } gauss = weightmat->data[clsidx][0] / sqrt(pow(2.0 * PI, (double)vec->length) * detvec->data[clsidx]) * exp(-gauss / 2.0); return gauss; }
static double cal_xmcxmc(long clsidx, DVECTOR x, DMATRIX mm, // [num class][dim] DMATRIX cm) // [num class * dim][dim] { long clsnum, k, l, b, dim; double *vec = NULL; double td, d; dim = x->length; clsnum = mm->row; b = clsidx * dim; if (mm->col != dim || cm->col != dim || clsnum * dim != cm->row) { cst_errmsg("Error cal_xmcxmc: different dimension\n"); cst_error(); } // memory allocation vec = mlpg_alloc((int)dim, double); for (k = 0; k < dim; k++) vec[k] = x->data[k] - mm->data[clsidx][k]; for (k = 0, d = 0.0; k < dim; k++) { for (l = 0, td = 0.0; l < dim; l++) td += vec[l] * cm->data[l + b][k]; d += td * vec[k]; } // memory free mlpg_free(vec); vec = NULL; return d; }
int cst_munmap_file(cst_filemap *fmap) { cst_dbgmsg("cst_munmap_file: unsupported on this platform"); cst_error(); return -1; }
cst_utterance *join_units_simple(cst_utterance *utt) { cst_wave *w = 0; cst_lpcres *lpcres; const char *resynth_type; const cst_val *streaming_info_val; resynth_type = get_param_string(utt->features, "resynth_type", "fixed"); asis_to_pm(utt); concat_units(utt); lpcres = val_lpcres(utt_feat_val(utt, "target_lpcres")); streaming_info_val = get_param_val(utt->features, "streaming_info", NULL); if (streaming_info_val) { lpcres->asi = val_audio_streaming_info(streaming_info_val); lpcres->asi->utt = utt; } if (cst_streq(resynth_type, "fixed")) w = lpc_resynth_fixedpoint(lpcres); else { cst_errmsg("unknown resynthesis type %s\n", resynth_type); cst_error(); /* Should not happen */ } utt_set_wave(utt, w); return utt; }
static void mlgparaChol(DMATRIX pdf, PStreamChol *pst, DMATRIX mlgp) { int t, d; // error check if (pst->vSize * 2 != pdf->col || pst->order + 1 != mlgp->col) { cst_errmsg("Error mlgparaChol: Different dimension\n"); cst_error(); } // mseq: U^{-1}*M, ifvseq: U^{-1} for (t = 0; t < pst->T; t++) { for (d = 0; d < pst->vSize; d++) { pst->mseq[t][d] = pdf->data[t][d]; pst->ivseq[t][d] = pdf->data[t][pst->vSize + d]; } } // ML parameter generation mlpgChol(pst); // extracting parameters for (t = 0; t < pst->T; t++) for (d = 0; d <= pst->order; d++) mlgp->data[t][d] = pst->c[t][d]; return; }
cst_filemap *cst_mmap_file(const char *path) { cst_dbgmsg("cst_mmap_file: unsupported on this platform"); cst_error(); return NULL; }
static void finish_header(HWAVEOUT drvr, WAVEHDR *hdr) { if (waveOutUnprepareHeader(drvr,hdr,sizeof(*hdr)) != MMSYSERR_NOERROR) { cst_errmsg("Failed to unprepare header %p\n", hdr); cst_error(); } cst_free(hdr->lpData); cst_free(hdr); }
const cst_val *val_cdr(const cst_val *v) { if (v && cst_val_consp(v)) return CST_VAL_CDR(v); else { cst_errmsg("VAL: tried to access cdr in %d typed val\n", (v ? CST_VAL_TYPE(v) : -1)); cst_error(); } return 0; }
const char *val_string(const cst_val *v) { if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING)) return CST_VAL_STRING(v); else { cst_errmsg("VAL: tried to access string in %d typed val\n", (v ? CST_VAL_TYPE(v) : -1)); cst_error(); } return 0; }
void *val_generic(const cst_val *v, int type, const char *stype) { /* a generic access function that checks the expected type */ if (v && CST_VAL_TYPE(v) == type) return CST_VAL_VOID(v); else { cst_errmsg("VAL: tried to access %s in %d type val\n", stype, (v ? CST_VAL_TYPE(v) : -1)); cst_error(); } return NULL; }
void add_to_free_queue(cst_audiodev *ad, void *datum) { au_wince_pdata *pd = ad->platform_data; if (pd->fqlen == pd->fqmaxlen && !(pd->fqmaxlen % 32)) { pd->fqmaxlen += 32; pd->fq = (void **)realloc(pd->fq, pd->fqmaxlen * sizeof(void *)); if (!pd->fq) { cst_errmsg("Out of memory\n"); cst_error(); } } pd->fq[pd->fqlen++] = datum; }
void *val_void(const cst_val *v) { /* The scary, do anything function, this shouldn't be called by mortals */ if ((v == NULL) || (CST_VAL_TYPE(v) == CST_VAL_TYPE_CONS) || (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT) || (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT)) { cst_errmsg("VAL: tried to access void in %d typed val\n", (v ? CST_VAL_TYPE(v) : -1)); cst_error(); return NULL; } else return CST_VAL_VOID(v); }
float val_float(const cst_val *v) { if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_INT)) return (float)CST_VAL_INT(v); else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_FLOAT)) return CST_VAL_FLOAT(v); else if (v && (CST_VAL_TYPE(v) == CST_VAL_TYPE_STRING)) return cst_atof(CST_VAL_STRING(v)); else { cst_errmsg("VAL: tried to access float in %d typed val\n", (v ? CST_VAL_TYPE(v) : -1)); cst_error(); } return 0; }
const cst_val *set_car(cst_val *v1, const cst_val *v2) { /* destructive set car, be careful you have a pointer to current cdr */ if (!cst_val_consp(v1)) { cst_errmsg("VAL: tried to set car of non-consp cell\n"); cst_error(); return NULL; } else { val_dec_refcount(CST_VAL_CAR(v1)); val_inc_refcount(v1); CST_VAL_CAR(v1) = (cst_val *)v2; } return v1; }
cst_utterance *join_units_modified_lpc(cst_utterance *utt) { cst_wave *w = 0; cst_lpcres *lpcres; const char *resynth_type; const cst_val *streaming_info_val; resynth_type = get_param_string(utt->features, "resynth_type", "float"); f0_targets_to_pm(utt); concat_units(utt); lpcres = val_lpcres(utt_feat_val(utt, "target_lpcres")); streaming_info_val = get_param_val(utt->features, "streaming_info", NULL); if (streaming_info_val) { lpcres->asi = val_audio_streaming_info(streaming_info_val); lpcres->asi->utt = utt; } if (cst_streq(resynth_type, "float")) w = lpc_resynth(lpcres); else if (cst_streq(resynth_type, "fixed")) { w = lpc_resynth_fixedpoint(lpcres); } else { cst_errmsg("unknown resynthesis type %s\n", resynth_type); cst_error(); /* Should not happen */ } if (w == NULL) { /* Synthesis Failed, probably because it was interrupted */ utt_set_feat_int(utt, "Interrupted", 1); w = new_wave(); } utt_set_wave(utt, w); return utt; }
static double get_gauss_full(long clsidx, DVECTOR vec, // [dim] DVECTOR detvec, // [clsnum] DMATRIX weightmat, // [clsnum][1] DMATRIX meanvec, // [clsnum][dim] DMATRIX invcovmat) // [clsnum * dim][dim] { double gauss; if (detvec->data[clsidx] <= 0.0) { cst_errmsg("#error: det <= 0.0\n"); cst_error(); } gauss = weightmat->data[clsidx][0] / sqrt(pow(2.0 * PI, (double)vec->length) * detvec->data[clsidx]) * exp(-1.0 * cal_xmcxmc(clsidx, vec, meanvec, invcovmat) / 2.0); return gauss; }
// Diagonal Covariance Version static void mlgparaGrad(DMATRIX pdf, PStreamChol *pst, DMATRIX mlgp, const int max, double th, double e, double alpha, DVECTOR vm, DVECTOR vv, XBOOL nrmflag, XBOOL extvflag) { int t, d; // error check if (pst->vSize * 2 != pdf->col || pst->order + 1 != mlgp->col) { cst_errmsg("Error mlgparaChol: Different dimension\n"); cst_error(); } // mseq: U^{-1}*M, ifvseq: U^{-1} for (t = 0; t < pst->T; t++) { for (d = 0; d < pst->vSize; d++) { pst->mseq[t][d] = pdf->data[t][d]; pst->ivseq[t][d] = pdf->data[t][pst->vSize + d]; } } // ML parameter generation mlpgChol(pst); // extend variance if (extvflag == XTRUE) for (d = 0; d <= pst->order; d++) varconv(pst->c, d, pst->T, vm->data[d]); // estimating parameters mlpgGrad(pst, max, th, e, alpha, vm, vv, nrmflag); // extracting parameters for (t = 0; t < pst->T; t++) for (d = 0; d <= pst->order; d++) mlgp->data[t][d] = pst->c[t][d]; return; }
// diagonal covariance static double get_gauss_dia5(double det, double weight, DVECTOR vec, // dim DVECTOR meanvec, // dim DVECTOR invcovvec) // dim { double gauss, sb; long k; if (det <= 0.0) { cst_errmsg("#error: det <= 0.0\n"); cst_error(); } for (k = 0, gauss = 0.0; k < vec->length; k++) { sb = vec->data[k] - meanvec->data[k]; gauss += sb * invcovvec->data[k] * sb; } gauss = weight / sqrt(pow(2.0 * PI, (double)vec->length) * det) * exp(-gauss / 2.0); return gauss; }
cst_audiodev *audio_open_alsa(int sps, int channels, cst_audiofmt fmt) { snd_pcm_channel_info_t pinfo; snd_pcm_channel_params_t params; snd_pcm_channel_setup_t setup; snd_pcm_t *pcm; cst_audiodev *ad; int err; #ifdef __QNXNTO__ if (snd_pcm_open_preferred(&pcm,&alsa_card,&alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) { cst_errmsg("alsa_audio: failed to open audio device\n"); cst_error(); } if (snd_pcm_plugin_set_disable(pcm,PLUGIN_DISABLE_MMAP) < 0) { cst_errmsg("alsa_audio: failed to disable mmap\n"); snd_pcm_close(pcm); cst_error(); } #else if (snd_pcm_open(&pcm,alsa_card,alsa_device,SND_PCM_OPEN_PLAYBACK) < 0) { cst_errmsg("alsa_audio: failed to open audio device\n"); cst_error(); } #endif memset(&pinfo, 0, sizeof(pinfo)); memset(¶ms, 0, sizeof(params)); memset(&setup, 0, sizeof(setup)); pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; snd_pcm_plugin_info(pcm,&pinfo); params.mode = SND_PCM_MODE_BLOCK; params.channel = SND_PCM_CHANNEL_PLAYBACK; params.start_mode = SND_PCM_START_DATA; params.stop_mode = SND_PCM_STOP_STOP; params.buf.block.frag_size = pinfo.max_fragment_size; params.buf.block.frags_max = 1; params.buf.block.frags_min = 1; params.format.interleave = 1; params.format.rate = sps; params.format.voices = channels; switch (fmt) { case CST_AUDIO_LINEAR16: if (CST_LITTLE_ENDIAN) params.format.format = SND_PCM_SFMT_S16_LE; else params.format.format = SND_PCM_SFMT_S16_BE; break; case CST_AUDIO_LINEAR8: params.format.format = SND_PCM_SFMT_U8; break; case CST_AUDIO_MULAW: params.format.format = SND_PCM_SFMT_MU_LAW; break; } if((err = snd_pcm_plugin_params(pcm,¶ms)) < 0) { cst_errmsg("alsa_audio params setting failed: %s\n",snd_strerror(err)); snd_pcm_close(pcm); cst_error(); } if((err = snd_pcm_plugin_setup(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); snd_pcm_close(pcm); cst_error(); } if((err = snd_pcm_plugin_prepare(pcm,SND_PCM_CHANNEL_PLAYBACK)) > 0) { cst_errmsg("alsa_audio sound prepare setting failed: %s\n",snd_strerror(err)); snd_pcm_close(pcm); cst_error(); } pinfo.channel = SND_PCM_CHANNEL_PLAYBACK; snd_pcm_plugin_info(pcm,&pinfo); ad = cst_alloc(cst_audiodev, 1); ad->platform_data = pcm; ad->sps = ad->real_sps = sps; ad->channels = ad->real_channels = channels; ad->fmt = ad->real_fmt = fmt; return ad; }
const cst_val *cart_interpret(cst_item *item, const cst_cart *tree) { /* Tree interpretation */ const cst_val *v=0; const cst_val *tree_val; const char *tree_feat = ""; cst_features *fcache; int r=0; int node=0; fcache = new_features_local(item_utt(item)->ctx); while (cst_cart_node_op(node,tree) != CST_CART_OP_LEAF) { #if CART_DEBUG cart_print_node(node,tree); #endif tree_feat = cst_cart_node_feat(node,tree); v = get_param_val(fcache,tree_feat,0); if (v == 0) { v = ffeature(item,tree_feat); feat_set(fcache,tree_feat,v); } #if CART_DEBUG val_print(stdout,v); printf("\n"); #endif tree_val = cst_cart_node_val(node,tree); if (cst_cart_node_op(node,tree) == CST_CART_OP_IS) r = val_equal(v,tree_val); else if (cst_cart_node_op(node,tree) == CST_CART_OP_LESS) r = val_less(v,tree_val); else if (cst_cart_node_op(node,tree) == CST_CART_OP_GREATER) r = val_greater(v,tree_val); else if (cst_cart_node_op(node,tree) == CST_CART_OP_IN) r = val_member(v,tree_val); else if (cst_cart_node_op(node,tree) == CST_CART_OP_MATCHES) r = cst_regex_match(cst_regex_table[val_int(tree_val)], val_string(v)); else { cst_errmsg("cart_interpret_question: unknown op type %d\n", cst_cart_node_op(node,tree)); cst_error(); } if (r) { /* Oh yes it is */ #if CART_DEBUG printf(" YES\n"); #endif node = cst_cart_node_yes(node,tree); } else { /* Oh no it isn't */ #if CART_DEBUG printf(" NO\n"); #endif node = cst_cart_node_no(node,tree); } } delete_features(fcache); return cst_cart_node_val(node,tree); }
cst_audiodev *audio_open_sun(int sps, int channels, cst_audiofmt fmt) { audio_info_t ainfo; int fd; cst_audiodev *ad; char *audio_device; if ((fd = open(sun_audio_device, O_WRONLY)) < 0) { /* the device might be a SunRay, so get the AUDIODEV env var */ audio_device = getenv("AUDIODEV"); if (audio_device != NULL) { if ((fd = open(audio_device, O_WRONLY)) < 0) { cst_errmsg("sun_audio: failed to open audio device %s: %s\n", audio_device, strerror(errno)); } } else { cst_errmsg("sun_audio: failed to open audio device %s: %s\n", sun_audio_device, strerror(errno)); cst_error(); } } ioctl(fd, AUDIO_GETINFO, &ainfo); switch (fmt) { case CST_AUDIO_LINEAR16: ainfo.play.encoding = AUDIO_ENCODING_LINEAR; ainfo.play.precision = 16; break; case CST_AUDIO_LINEAR8: ainfo.play.encoding = AUDIO_ENCODING_LINEAR; ainfo.play.precision = 8; break; case CST_AUDIO_MULAW: ainfo.play.encoding = AUDIO_ENCODING_ULAW; ainfo.play.precision = 8; break; } ainfo.play.channels = 1; ainfo.play.sample_rate = sps; if (ioctl(fd, AUDIO_SETINFO, &ainfo) == -1) { cst_errmsg("sun_audio: failed to set audio params: %s\n", strerror(errno)); close(fd); cst_error(); } ad = cst_alloc(cst_audiodev, 1); ad->sps = sps; ad->real_sps = ainfo.play.sample_rate; ad->channels = channels; ad->real_channels = ainfo.play.channels; ad->fmt = fmt; if (ainfo.play.encoding == AUDIO_ENCODING_LINEAR) { if (ainfo.play.precision == 16) ad->real_fmt = CST_AUDIO_LINEAR16; else if (ainfo.play.precision == 8) ad->real_fmt = CST_AUDIO_LINEAR8; else { cst_errmsg("sun_audio: linear %d bit audio unsupported\n", ainfo.play.precision); close(fd); cst_free(ad); cst_error(); } } else if (ainfo.play.encoding == AUDIO_ENCODING_ULAW) ad->real_fmt = CST_AUDIO_MULAW; else { cst_errmsg ("sun_audio: unsupported audio format (%d bit/encoding #%d)\n", ainfo.play.precision, ainfo.play.encoding); close(fd); cst_free(ad); cst_error(); } ad->platform_data = (void *) fd; return ad; }
static cst_utterance *tokentosegs(cst_utterance *u) { cst_item *t; cst_relation *seg, *syl, *sylstructure, *word; cst_item *sylitem, *sylstructureitem, *worditem, *sssyl; cst_phoneset *ps; ps = val_phoneset(utt_feat_val(u, "phoneset")); /* Just copy tokens into the Segment relation */ seg = utt_relation_create(u, "Segment"); syl = utt_relation_create(u, "Syllable"); word = utt_relation_create(u, "Word"); sylstructure = utt_relation_create(u, "SylStructure"); sssyl = sylitem = worditem = sylstructureitem = 0; for (t = relation_head(utt_relation(u, "Token")); t; t = item_next(t)) { cst_item *segitem = relation_append(seg, NULL); char const *pname = item_feat_string(t, "name"); char *name = cst_strdup(pname); if (worditem == 0) { worditem = relation_append(word,NULL); item_set_string(worditem, "name", "phonestring"); sylstructureitem = relation_append(sylstructure,worditem); } if (sylitem == 0) { sylitem = relation_append(syl,NULL); sssyl = item_add_daughter(sylstructureitem,sylitem); } if (name[cst_strlen(name)-1] == '1') { item_set_string(sssyl,"stress","1"); name[cst_strlen(name)-1] = '\0'; } else if (name[cst_strlen(name)-1] == '0') { item_set_string(sssyl,"stress","0"); name[cst_strlen(name)-1] = '\0'; } if (cst_streq(name,"-")) { sylitem = 0; /* syllable break */ } else if (phone_id(ps, name) == -1) { cst_errmsg("Phone `%s' not in phoneset\n", pname); cst_error(); } else { item_add_daughter(sssyl,segitem); item_set_string(segitem, "name", name); } cst_free(name); } return u; }