static cst_tokenstream *new_tokenstream(const cst_string *whitespace, const cst_string *singlechars, const cst_string *prepunct, const cst_string *postpunct) { /* Constructor function */ cst_tokenstream *ts = cst_alloc(cst_tokenstream,1); ts->fd = NULL; ts->file_pos = 0; ts->line_number = 0; ts->string_buffer = NULL; ts->token_pos = 0; ts->whitespace = cst_alloc(cst_string,TS_BUFFER_SIZE); ts->ws_max = TS_BUFFER_SIZE; if (prepunct && prepunct[0]) { ts->prepunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE); ts->prep_max = TS_BUFFER_SIZE; } ts->token = cst_alloc(cst_string,TS_BUFFER_SIZE); ts->token_max = TS_BUFFER_SIZE; if (postpunct && postpunct[0]) { ts->postpunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE); ts->postp_max = TS_BUFFER_SIZE; } set_charclasses(ts,whitespace,singlechars,prepunct,postpunct); ts->current_char = 0; return ts; }
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; }
cst_audiodev *audio_open_pulseaudio(unsigned int sps, int channels, cst_audiofmt fmt) { cst_audiodev *ad; int err=0; /* Pulseaudio specific stuff */ pa_sample_spec *ss; pa_simple *s; ss = cst_alloc(pa_sample_spec,1); ss->rate = sps; ss->channels = channels; switch (fmt) { case CST_AUDIO_LINEAR16: if (CST_LITTLE_ENDIAN) ss->format = PA_SAMPLE_S16LE; else ss->format = PA_SAMPLE_S16BE; break; case CST_AUDIO_LINEAR8: ss->format = PA_SAMPLE_U8; break; case CST_AUDIO_MULAW: ss->format = PA_SAMPLE_ULAW; break; default: return NULL; break; } s = pa_simple_new( NULL, /* use default server */ "flite", PA_STREAM_PLAYBACK, NULL, /* use default device */ "Speech", ss, NULL, /* default channel map */ NULL, /* default buffering attributes */ &err); if (err < 0) return NULL; /* Write hardware parameters to flite audio device data structure */ ad = cst_alloc(cst_audiodev, 1); ad->real_sps = ad->sps = sps; ad->real_channels = ad->channels = channels; ad->real_fmt = ad->fmt = fmt; ad->platform_data = (void *) s; return ad; }
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_filemap *cst_read_whole_file(const char *path) { cst_filemap *fmap; cst_file fh; if ((fh = cst_fopen(path, CST_OPEN_READ)) < 0) { cst_errmsg("cst_read_whole_file: Failed to open file\n"); return NULL; } fmap = cst_alloc(cst_filemap, 1); fmap->fh = fh; fmap->mapsize = cst_filesize(fmap->fh); fmap->mem = cst_alloc(char, fmap->mapsize); if (cst_fread(fmap->fh, fmap->mem, 1, fmap->mapsize) < fmap->mapsize) { cst_errmsg("cst_read_whole_file: read() failed\n"); cst_fclose(fmap->fh); cst_free(fmap->mem); cst_free(fmap); return NULL; } return fmap; }
cst_filemap *cst_mmap_file(const char *path) { HANDLE ffm; cst_filemap *fmap = NULL; /* By default, CreateFile uses wide-char strings; this doesn't do the expected thing when passed non-wide strings We're explicitly using the non-wide version of CreateFile to sidestep this issue. If you're having problems with unicode pathnames, you'll need to change this to call CreateFileW (and then ensure you're always passing a wide-char string). */ ffm = CreateFileA(path,GENERIC_READ,FILE_SHARE_READ,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (ffm == INVALID_HANDLE_VALUE) { return NULL; } else { fmap = cst_alloc(cst_filemap,1); fmap->h = CreateFileMapping(ffm,NULL,PAGE_READONLY,0,0,NULL); fmap->mapsize = GetFileSize(fmap->h, NULL); fmap->mem = MapViewOfFile(fmap->h,FILE_MAP_READ,0,0,0); if (fmap->h == NULL || fmap->mem == NULL) { CloseHandle(ffm); cst_free(fmap); return NULL; } } return fmap; }
bard_token *bard_token_new(const char *name) { bard_token *token = cst_alloc(bard_token,1); token->token = cst_strdup(name); return token; }
cst_features *new_features(void) { cst_features *f; f = cst_alloc(cst_features,1); f->head = NULL; f->ctx = NULL; return f; }
cst_audiodev * audio_open_none(unsigned int sps, int channels) { cst_audiodev *ad; ad = cst_alloc(cst_audiodev,1); ad->sps = sps; ad->channels = channels; return ad; }
cst_audiodev * audio_open_none(int sps, int channels, int fmt) { cst_audiodev *ad; ad = cst_alloc(cst_audiodev,1); ad->sps = ad->real_sps = sps; ad->channels = ad->real_channels = channels; ad->fmt = ad->real_fmt = fmt; return ad; }
cst_lts_rules *new_lts_rules() { cst_lts_rules *lt = cst_alloc(cst_lts_rules,1); lt->name = 0; lt->letter_index = 0; lt->models = 0; lt->phone_table = 0; lt->context_window_size = 0; lt->context_extra_feats = 0; return lt; }
static void extend_buffer(cst_string **buffer,int *buffer_max) { int new_max; cst_string *new_buffer; new_max = (*buffer_max)+(*buffer_max)/5; new_buffer = cst_alloc(cst_string,new_max); memmove(new_buffer,*buffer,*buffer_max); cst_free(*buffer); *buffer = new_buffer; *buffer_max = new_max; }
/* HTS_SStreamSet_create: parse label and determine state duration */ HTS_Boolean HTS_SStreamSet_create(HTS_SStreamSet * sss, HTS_ModelSet * ms, char** label, size_t num_labels, double speed) { size_t i, j, k; double temp; int shift; size_t state; HTS_SStream *sst; double *duration_mean, *duration_vari; double frame_length; /* initialize state sequence */ sss->nstate = HTS_ModelSet_get_nstate(ms); sss->nstream = HTS_ModelSet_get_nstream(ms); sss->total_frame = 0; sss->total_state = num_labels * sss->nstate; sss->duration = cst_alloc(size_t,sss->total_state); sss->sstream = cst_alloc(HTS_SStream,sss->nstream); for (i = 0; i < sss->nstream; i++) { sst = &sss->sstream[i]; sst->vector_length = HTS_ModelSet_get_vector_length(ms, i); sst->mean = cst_alloc(double *,sss->total_state); sst->vari = cst_alloc(double *,sss->total_state); if (HTS_ModelSet_is_msd(ms, i)) sst->msd = cst_alloc(double,sss->total_state); else sst->msd = NULL; for (j = 0; j < sss->total_state; j++) { sst->mean[j] = cst_alloc(double,(sst->vector_length * HTS_ModelSet_get_window_size(ms, i))); sst->vari[j] = cst_alloc(double,(sst->vector_length * HTS_ModelSet_get_window_size(ms, i))); } if (HTS_ModelSet_use_gv(ms, i)) { sst->gv_switch = cst_alloc(HTS_Boolean,sss->total_state); for (j = 0; j < sss->total_state; j++) sst->gv_switch[j] = TRUE; } else { sst->gv_switch = NULL; } }
cst_lexicon *cmu_LANGNAME_lex_init(void) { /* Should it be global const or dynamic */ /* Can make lts_rules just a cart tree like others */ cst_lexicon *l; l = cst_alloc(cst_lexicon,1); l->name = "cmu_LANGNAME_lex"; l->lts_function = cmu_LANGNAME_lex_lts_function; return l; }
cst_filemap *cst_read_part_file(const char *path) { cst_filemap *fmap; cst_file fh; if ((fh = cst_fopen(path, CST_OPEN_READ)) == NULL) { cst_errmsg("cst_read_part_file: Failed to open file\n"); return NULL; } fmap = cst_alloc(cst_filemap, 1); fmap->fh = fh; return fmap; }
cst_sts *find_sts(cst_wave *sig, cst_track *lpc) { cst_sts *sts; int i,j; double *resd; int size,start,end; short *sigplus; sts = cst_alloc(cst_sts,lpc->num_frames); sigplus = cst_alloc(short,sig->num_samples+lpc->num_channels); memset(sigplus,0,sizeof(short)*lpc->num_channels); memmove(&sigplus[lpc->num_channels], sig->samples, sizeof(short)*sig->num_samples); /* EST LPC Windows are centered around the point */ /* so offset things by a have period */ start = (int)((float)sig->sample_rate * lpc->times[0]/2); for (i=0; i<lpc->num_frames; i++) { if (i+1 == lpc->num_frames) end = (int)((float)sig->sample_rate * lpc->times[i]); else end = (int)((float)sig->sample_rate * (lpc->times[i]+lpc->times[i+1]))/2; size = end - start; if (size == 0) printf("frame size at %f is 0\n",lpc->times[i]); resd = cst_alloc(double,size); inv_lpc_filterd(&sigplus[start+lpc->num_channels], lpc->frames[i],lpc->num_channels, resd, size); sts[i].size = size; sts[i].frame = cst_alloc(unsigned short,lpc->num_channels-1); for (j=1; j < lpc->num_channels; j++) sts[i].frame[j-1] = (unsigned short) (((lpc->frames[i][j]-lpc_min)/lpc_range)*65535); sts[i].residual = cst_alloc(unsigned char,size); for (j=0; j < size; j++) sts[i].residual[j] = cst_short_to_ulaw((short)resd[j]); start = end; } cst_free(sigplus); return sts; }
static cst_tokenstream *new_tokenstream(const char *whitespacesymbols, const char *singlecharsymbols, const char *prepunctsymbols, const char *postpunctsymbols) { /* Constructor function */ cst_tokenstream *ts = cst_alloc(cst_tokenstream,1); ts->fd = NULL; ts->file_pos = 0; ts->line_number = 0; ts->string_buffer = NULL; ts->token_pos = 0; ts->whitespace = cst_alloc(char,TS_BUFFER_SIZE); ts->ws_max = TS_BUFFER_SIZE; if (prepunctsymbols && prepunctsymbols[0]) { ts->prepunctuation = cst_alloc(char,TS_BUFFER_SIZE); ts->prep_max = TS_BUFFER_SIZE; }
cst_filemap *cst_read_whole_file(const char *path) { cst_filemap *fmap; cst_file fh; if ((fh = cst_fopen(path, CST_OPEN_READ)) == NULL) { cst_errmsg("cst_read_whole_file: Failed to open file\n"); return NULL; } fmap = cst_alloc(cst_filemap, 1); fmap->fh = fh; cst_fseek(fmap->fh, 0, CST_SEEK_ENDREL); fmap->mapsize = cst_ftell(fmap->fh); fmap->mem = VirtualAlloc(NULL, fmap->mapsize, MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE); cst_fseek(fmap->fh, 0, CST_SEEK_ABSOLUTE); cst_fread(fmap->fh, fmap->mem, 1, fmap->mapsize); return fmap; }
cst_filemap *cst_mmap_file(const char *path) { HANDLE ffm; cst_filemap *fmap = NULL; ffm = CreateFile(path,GENERIC_READ,FILE_SHARE_READ,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (ffm == INVALID_HANDLE_VALUE) { return NULL; } else { fmap = cst_alloc(cst_filemap,1); fmap->h = CreateFileMapping(ffm,NULL,PAGE_READONLY,0,0,NULL); fmap->mapsize = GetFileSize(fmap->h, NULL); fmap->mem = MapViewOfFile(fmap->h,FILE_MAP_READ,0,0,0); if (fmap->h == NULL || fmap->mem == NULL) { CloseHandle(ffm); cst_free(fmap); return NULL; } } return fmap; }
cst_string *cst_implode(const cst_val *sl) { const cst_val *v; int l=0; char *s; for (v=sl; v; v=val_cdr(v)) { if (val_stringp(val_car(v))) l += cst_strlen(val_string(val_car(v))); } s = cst_alloc(cst_string,l+1); for (v=sl; v; v=val_cdr(v)) { if (val_stringp(val_car(v))) cst_sprintf(s,"%s%s",s,val_string(val_car(v))); } return s; }
/* - regcomp - compile a regular expression into internal code * * We can't allocate space until we know how big the compiled form will be, * but we can't compile it (and thus know how big it is) until we've got a * place to put the code. So we cheat: we compile it twice, once with code * generation turned off and size counting turned on, and once "for real". * This also means that we don't allocate space until we are sure that the * thing really will compile successfully, and we never have to move the * code and thus invalidate pointers into it. (Note that it has to be in * one piece because free() must be able to free it all.) * * Beware that the optimization-preparation code in here knows about some * of the structure of the compiled regexp. */ cst_regex * hs_regcomp(const char *exp) { cst_regex *r; char *scan; char *longest; unsigned int len; int flags; if (exp == NULL) FAIL("NULL argument"); /* First pass: determine size, legality. */ #ifdef notdef if (exp[0] == '.' && exp[1] == '*') exp += 2; /* aid grep */ #endif regparse = exp; regnpar = 1; regsize = 0L; regcode = ®dummy; regc(CST_REGMAGIC); if (reg(0, &flags) == NULL) return(NULL); /* Small enough for pointer-storage convention? */ if (regsize >= 32767L) /* Probably could be 65535L. */ FAIL("regexp too big"); /* Allocate space. */ r = cst_alloc(cst_regex,1); r->regsize = regsize; r->program = cst_alloc(char,regsize); if (r == NULL) FAIL("out of space"); /* Second pass: emit code. */ regparse = exp; regnpar = 1; regcode = r->program; regc(CST_REGMAGIC); if (reg(0, &flags) == NULL) return(NULL); /* Dig out information for optimizations. */ r->regstart = '\0'; /* Worst-case defaults. */ r->reganch = 0; r->regmust = NULL; r->regmlen = 0; scan = r->program+1; /* First BRANCH. */ if (OP(regnext(scan)) == END) { /* Only one top-level choice. */ scan = OPERAND(scan); /* Starting-point info. */ if (OP(scan) == EXACTLY) r->regstart = *OPERAND(scan); else if (OP(scan) == BOL) r->reganch++; /* * If there's something expensive in the r.e., find the * longest literal string that must appear and make it the * regmust. Resolve ties in favor of later strings, since * the regstart check works with the beginning of the r.e. * and avoiding duplication strengthens checking. Not a * strong reason, but sufficient in the absence of others. */ if (flags&SPSTART) { longest = NULL; len = 0; for (; scan != NULL; scan = regnext(scan)) if ((OP(scan) == EXACTLY) && (strlen(OPERAND(scan)) >= len)) { longest = OPERAND(scan); len = strlen(OPERAND(scan)); } r->regmust = longest; r->regmlen = len; } } return(r); }
cst_ss *new_ss() { cst_ss *ss = cst_alloc(cst_ss,1); return ss; }
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; }
cst_audiodev *audio_open_alsa(unsigned int sps, int channels, cst_audiofmt fmt) { cst_audiodev *ad; unsigned int real_rate; int err; /* alsa specific stuff */ snd_pcm_t *pcm_handle; snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; snd_pcm_hw_params_t *hwparams; snd_pcm_format_t format; snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED; /* Allocate the snd_pcm_hw_params_t structure on the stack. */ snd_pcm_hw_params_alloca(&hwparams); /* Open pcm device */ err = snd_pcm_open(&pcm_handle, pcm_dev_name, stream, 0); if (err < 0) { cst_errmsg("audio_open_alsa: failed to open audio device %s. %s\n", pcm_dev_name, snd_strerror(err)); return NULL; } /* Init hwparams with full configuration space */ err = snd_pcm_hw_params_any(pcm_handle, hwparams); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to get hardware parameters from audio device. %s\n", snd_strerror(err)); return NULL; } /* Set access mode */ err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, access); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set access mode. %s.\n", snd_strerror(err)); return NULL; } /* Determine matching alsa sample format */ /* This could be implemented in a more */ /* flexible way (byte order conversion). */ switch (fmt) { case CST_AUDIO_LINEAR16: if (CST_LITTLE_ENDIAN) format = SND_PCM_FORMAT_S16_LE; else format = SND_PCM_FORMAT_S16_BE; break; case CST_AUDIO_LINEAR8: format = SND_PCM_FORMAT_U8; break; case CST_AUDIO_MULAW: format = SND_PCM_FORMAT_MU_LAW; break; default: snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to find suitable format.\n"); return NULL; break; } /* Set samble format */ err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format); if (err <0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set format. %s.\n", snd_strerror(err)); return NULL; } /* Set sample rate near the disired rate */ real_rate = sps; err = snd_pcm_hw_params_set_rate(pcm_handle, hwparams, real_rate, 0); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set sample rate near %d. %s.\n", sps, snd_strerror(err)); return NULL; } /*FIXME: This is probably too strict */ assert(sps == real_rate); /* Set number of channels */ assert(channels >0); err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set number of channels to %d. %s.\n", channels, snd_strerror(err)); return NULL; } /* Commit hardware parameters */ err = snd_pcm_hw_params(pcm_handle, hwparams); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set hw parameters. %s.\n", snd_strerror(err)); return NULL; } /* Make sure the device is ready to accept data */ assert(snd_pcm_state(pcm_handle) == SND_PCM_STATE_PREPARED); /* Write hardware parameters to flite audio device data structure */ ad = cst_alloc(cst_audiodev, 1); assert(ad != NULL); ad->real_sps = ad->sps = sps; ad->real_channels = ad->channels = channels; ad->real_fmt = ad->fmt = fmt; ad->platform_data = (void *) pcm_handle; return ad; }
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; }
cst_audiodev *audio_open_oss(int sps, int channels, cst_audiofmt fmt) { cst_audiodev *ad; int afmt; int sfmts; int afd; int frag; afd = open(oss_audio_device, O_WRONLY); if (afd == -1) { cst_errmsg("oss_audio: failed to open audio device %s\n", oss_audio_device); return NULL; } ad = cst_alloc(cst_audiodev, 1); ad->sps = sps; ad->channels = channels; ad->fmt = fmt; ad->platform_data = (void *) afd; if (ad->channels == 0) ad->channels = 1; ioctl(afd, SNDCTL_DSP_RESET, 0); ad->real_sps = ad->sps; ad->real_channels = ad->channels; ioctl(afd, SNDCTL_DSP_CHANNELS, &ad->real_channels); if (ad->real_channels != ad->channels) { /* Its a stereo only device so it'll play things twice as fast */ ad->real_sps /= 2; } ioctl(afd, SNDCTL_DSP_SPEED, &ad->real_sps); frag = (4 << 16) | 10; ioctl(afd, SNDCTL_DSP_SETFRAGMENT, &frag); ioctl(afd, SNDCTL_DSP_GETFMTS, &sfmts); if (fmt == CST_AUDIO_LINEAR8 && (sfmts & AFMT_U8)) { ad->real_fmt = CST_AUDIO_LINEAR8; afmt = AFMT_U8; } else if (fmt == CST_AUDIO_MULAW && (sfmts & AFMT_MU_LAW)) { ad->real_fmt = CST_AUDIO_MULAW; afmt = AFMT_MU_LAW; } else if (CST_LITTLE_ENDIAN) { if (sfmts & AFMT_S16_LE) { ad->real_fmt = CST_AUDIO_LINEAR16; afmt = AFMT_S16_LE; } else if (sfmts & AFMT_S16_BE) /* not likely */ { ad->byteswap = 1; ad->real_fmt = CST_AUDIO_LINEAR16; afmt = AFMT_S16_BE; } else if (sfmts & AFMT_U8) { afmt = AFMT_U8; ad->real_fmt = CST_AUDIO_LINEAR8; } else { cst_free(ad); close(afd); return NULL; } } else { if (sfmts & AFMT_S16_BE) { ad->real_fmt = CST_AUDIO_LINEAR16; afmt = AFMT_S16_BE; } else if (sfmts & AFMT_S16_LE) /* likely */ { ad->real_fmt = CST_AUDIO_LINEAR16; ad->byteswap = 1; afmt = AFMT_S16_LE; } else if (sfmts & AFMT_U8) { ad->real_fmt = CST_AUDIO_LINEAR8; afmt = AFMT_U8; } else { cst_free(ad); close(afd); return NULL; } } ioctl(afd, SNDCTL_DSP_SETFMT, &afmt); return ad; }
cst_lexicon *new_lexicon() { cst_lexicon *l = cst_alloc(cst_lexicon,1); l->syl_boundary = no_syl_boundaries; return l; }
/* - regexec - match a regexp against a string */ cst_regstate * hs_regexec(const cst_regex *prog, const char *string) { cst_regstate *state; char *s; /* Be paranoid... */ if (prog == NULL || string == NULL) { FAIL("NULL parameter"); return(0); } /* Check validity of program. */ if (UCHARAT(prog->program) != CST_REGMAGIC) { FAIL("corrupted program"); return(0); } /* If there is a "must appear" string, look for it. */ if (prog->regmust != NULL) { s = (char *)string; while ((s = strchr(s, prog->regmust[0])) != NULL) { if (strncmp(s, prog->regmust, prog->regmlen) == 0) break; /* Found it. */ s++; } if (s == NULL) /* Not present. */ return(0); } state = cst_alloc(cst_regstate, 1); /* Mark beginning of line for ^ . */ state->bol = string; /* Simplest case: anchored match need be tried only once. */ if (prog->reganch) { if (regtry(state, string, prog->program+1)) return state; else { cst_free(state); return NULL; } } /* Messy cases: unanchored match. */ s = (char *)string; if (prog->regstart != '\0') /* We know what char it must start with. */ while ((s = strchr(s, prog->regstart)) != NULL) { if (regtry(state, s, prog->program+1)) return state; s++; } else /* We don't -- general case. */ do { if (regtry(state, s, prog->program+1)) return state; } while (*s++ != '\0'); cst_free(state); return NULL; }
cst_audiodev *audio_open_alsa(unsigned int sps, int channels) { cst_audiodev *ad; int err; /* alsa specific stuff */ snd_pcm_t *pcm_handle; snd_pcm_hw_params_t *hwparams; snd_pcm_format_t format; /* Allocate the snd_pcm_hw_params_t structure on the stack. */ snd_pcm_hw_params_alloca(&hwparams); /* Open pcm device */ err = snd_pcm_open(&pcm_handle, pcm_dev_name, SND_PCM_STREAM_PLAYBACK, 0); if (err < 0) { cst_errmsg("audio_open_alsa: failed to open audio device %s. %s\n", pcm_dev_name, snd_strerror(err)); return NULL; } /* Init hwparams with full configuration space */ err = snd_pcm_hw_params_any(pcm_handle, hwparams); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to get hardware parameters from audio device. %s\n", snd_strerror(err)); return NULL; } /* Set access mode */ err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set access mode. %s.\n", snd_strerror(err)); return NULL; } #ifdef WORDS_BIGENDIAN format = SND_PCM_FORMAT_S16_BE; #else format = SND_PCM_FORMAT_S16_LE; #endif /* Set sample format */ err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format); if (err <0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set format. %s.\n", snd_strerror(err)); return NULL; } /* Set sample rate */ err = snd_pcm_hw_params_set_rate(pcm_handle, hwparams, sps, 0); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set sample rate near %d. %s.\n", sps, snd_strerror(err)); return NULL; } /* Set number of channels */ assert(channels >0); err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set number of channels to %d. %s.\n", channels, snd_strerror(err)); return NULL; } /* Commit hardware parameters */ err = snd_pcm_hw_params(pcm_handle, hwparams); if (err < 0) { snd_pcm_close(pcm_handle); snd_config_update_free_global(); cst_errmsg("audio_open_alsa: failed to set hw parameters. %s.\n", snd_strerror(err)); return NULL; } /* Make sure the device is ready to accept data */ assert(snd_pcm_state(pcm_handle) == SND_PCM_STATE_PREPARED); /* Write hardware parameters to audio device data structure */ ad = cst_alloc(cst_audiodev, 1); assert(ad != NULL); ad->sps = sps; ad->channels = channels; ad->platform_data = (void *) pcm_handle; return ad; }
cst_val *lts_apply(const char *word,const char *feats,const cst_lts_rules *r) { int pos; cst_val *phones=0; cst_lts_letter *fval_buff; cst_lts_letter *full_buff; cst_lts_phone phone; char *left, *right, *p; /* For feature vals for each letter */ fval_buff = cst_alloc(cst_lts_letter, (r->context_window_size*2)+ r->context_extra_feats); /* Buffer with added contexts */ full_buff = cst_alloc(cst_lts_letter, (r->context_window_size*2)+ strlen(word)+1); /* TBD assumes single POS feat */ /* Assumes l_letter is a char and context < 8 */ sprintf(full_buff,"%.*s#%s#%.*s", r->context_window_size-1, "00000000", word, r->context_window_size-1, "00000000"); /* Do the prediction backwards so we don't need to reverse the answer */ for (pos = r->context_window_size + strlen(word) - 1; full_buff[pos] != '#'; pos--) { /* Fill the features buffer for the predictor */ sprintf(fval_buff,"%.*s%.*s%s", r->context_window_size, full_buff+pos-r->context_window_size, r->context_window_size, full_buff+pos+1, feats); if ((full_buff[pos] < 'a') || (full_buff[pos] > 'z')) { /* English specific */ #ifdef EXCESSIVELY_CHATTY cst_errmsg("lts:skipping unknown char \"%c\"\n", full_buff[pos]); #endif continue; } phone = apply_model(fval_buff, r->letter_index[((full_buff[pos])-'a')%26], r->models); /* delete epsilons and split dual-phones */ if (cst_streq("epsilon",r->phone_table[phone])) continue; else if ((p=strchr(r->phone_table[phone],'-')) != NULL) { left = cst_substr(r->phone_table[phone],0, strlen(r->phone_table[phone])-strlen(p)); right = cst_substr(r->phone_table[phone], (strlen(r->phone_table[phone])-strlen(p))+1, (strlen(p)-1)); phones = cons_val(string_val(left), cons_val(string_val(right),phones)); cst_free(left); cst_free(right); } else phones = cons_val(string_val(r->phone_table[phone]),phones); } cst_free(full_buff); cst_free(fval_buff); return phones; }