diff --git a/MANIFEST.in b/MANIFEST.in index 64c2655f..c1c0c2ce 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,5 +1,6 @@ recursive-include aeneas/cdtw * recursive-include aeneas/cew * +recursive-include aeneas/cfw * recursive-include aeneas/cint * recursive-include aeneas/cmfcc * recursive-include aeneas/cwave * diff --git a/README.md b/README.md index 2a436909..7daf9680 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ **aeneas** is a Python/C library and a set of tools to automagically synchronize audio and text (aka forced alignment). * Version: 1.6.0.0 -* Date: 2016-09-?? +* Date: 2016-09-26 * Developed by: [ReadBeyond](http://www.readbeyond.it/) * Lead Developer: [Alberto Pettarin](http://www.albertopettarin.it/) * License: the GNU Affero General Public License Version 3 (AGPL v3) @@ -62,7 +62,7 @@ or raw AUD/CSV/SSV/TSV/TXT/XML for further processing. 3. [FFmpeg](https://www.ffmpeg.org/) 4. [eSpeak](http://espeak.sourceforge.net/) 5. Python packages `BeautifulSoup4`, `lxml`, and `numpy` -6. Python C headers to compile the Python C extensions (optional but strongly recommended) +6. Python headers to compile the Python C/C++ extensions (optional but strongly recommended) 7. A shell supporting UTF-8 (optional but strongly recommended) ### Supported Platforms diff --git a/README.rst b/README.rst index a50a8aab..a7a3a4e9 100644 --- a/README.rst +++ b/README.rst @@ -5,7 +5,7 @@ aeneas synchronize audio and text (aka forced alignment). - Version: 1.6.0.0 -- Date: 2016-09-?? +- Date: 2016-09-26 - Developed by: `ReadBeyond `__ - Lead Developer: `Alberto Pettarin `__ - License: the GNU Affero General Public License Version 3 (AGPL v3) @@ -75,7 +75,7 @@ System Requirements 3. `FFmpeg `__ 4. `eSpeak `__ 5. Python packages ``BeautifulSoup4``, ``lxml``, and ``numpy`` -6. Python C headers to compile the Python C extensions (optional but +6. Python headers to compile the Python C/C++ extensions (optional but strongly recommended) 7. A shell supporting UTF-8 (optional but strongly recommended) diff --git a/aeneas/cew/100_run_driver.sh b/aeneas/cew/100_run_driver.sh index f3c62f5d..6908c2ea 100644 --- a/aeneas/cew/100_run_driver.sh +++ b/aeneas/cew/100_run_driver.sh @@ -38,6 +38,10 @@ echo "Run 3" echo "" echo "Run 4" +./cew_driver en "Hello|World|My|Dear|Friend" /tmp/out.wav 2.0 0 +echo "" + +echo "Run 5" ./cew_driver en "Hello|World|My|Dear|Friend" /tmp/out.wav 2.0 1 echo "" diff --git a/aeneas/cew/cew_driver.c b/aeneas/cew/cew_driver.c index 8aa49199..4e6d2195 100644 --- a/aeneas/cew/cew_driver.c +++ b/aeneas/cew/cew_driver.c @@ -40,6 +40,7 @@ void _usage(const char *prog) { printf("\n"); printf("Example: %s en \"Hello|World|My|Dear|Friend\" /tmp/out.wav 0.0 0\n", prog); printf(" %s en \"Hello|World|My|Dear|Friend\" /tmp/out.wav 0.0 1\n", prog); + printf(" %s en \"Hello|World|My|Dear|Friend\" /tmp/out.wav 2.0 0\n", prog); printf(" %s en \"Hello|World|My|Dear|Friend\" /tmp/out.wav 2.0 1\n", prog); printf("\n"); } diff --git a/aeneas/cfw/.gitignore b/aeneas/cfw/.gitignore new file mode 100644 index 00000000..e4141ce5 --- /dev/null +++ b/aeneas/cfw/.gitignore @@ -0,0 +1,5 @@ +build +__pycache__ + +*.so +cfw_driver diff --git a/aeneas/cfw/000_compile_driver.sh b/aeneas/cfw/000_compile_driver.sh new file mode 100644 index 00000000..307f7e1e --- /dev/null +++ b/aeneas/cfw/000_compile_driver.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +g++ cfw_driver.cc cfw_func.cc -Ifestival -lFestival -Ispeech_tools -lestools -lestbase -leststring -o cfw_driver -Wall -pedantic + diff --git a/aeneas/cfw/100_run_driver.sh b/aeneas/cfw/100_run_driver.sh new file mode 100644 index 00000000..46b70807 --- /dev/null +++ b/aeneas/cfw/100_run_driver.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +if [ ! -e cfw_driver ] +then + bash 000_compile_driver.sh +fi + +echo "Run 1" +./cfw_driver +echo "" + +echo "Run 2" +./cfw_driver "(language_english)" "Hello|World|My|Dear|Friend" /tmp/out.wav 0.0 0 +echo "" + +echo "Run 3" +./cfw_driver "(language_english)" "Hello|World|My|Dear|Friend" /tmp/out.wav 0.0 1 +echo "" + +echo "Run 4" +./cfw_driver "(language_english)" "Hello|World|My|Dear|Friend" /tmp/out.wav 2.0 0 +echo "" + +echo "Run 5" +./cfw_driver "(language_english)" "Hello|World|My|Dear|Friend" /tmp/out.wav 2.0 1 +echo "" + diff --git a/aeneas/cfw/800_compile_py.sh b/aeneas/cfw/800_compile_py.sh new file mode 100644 index 00000000..eb3bd9af --- /dev/null +++ b/aeneas/cfw/800_compile_py.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +rm -rf build *.so +python cfw_setup.py build_ext --inplace + diff --git a/aeneas/cfw/900_clean.sh b/aeneas/cfw/900_clean.sh new file mode 100644 index 00000000..03e72d6b --- /dev/null +++ b/aeneas/cfw/900_clean.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +rm -rf build __pycache__ *.so cfw_driver + diff --git a/aeneas/cfw/README.md b/aeneas/cfw/README.md new file mode 100644 index 00000000..40910f00 --- /dev/null +++ b/aeneas/cfw/README.md @@ -0,0 +1,22 @@ +# aeneas.cfw + +**aeneas.cfw** is a Python C extension to synthesize text with Festival. + +## API + +See the [__init__.py](__init__.py) file. + +## Compiling the Python C extension locally + +```bash +$ python cfw_setup.py build_ext --inplace +``` + +## Compiling the pure C driver program + +```bash +$ bash 000_compile_driver.sh +``` + + + diff --git a/aeneas/cfw/__init__.py b/aeneas/cfw/__init__.py new file mode 100644 index 00000000..b199bcd7 --- /dev/null +++ b/aeneas/cfw/__init__.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# coding=utf-8 + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" +aeneas.cfw is a Python C++ extension to synthesize text with Festival. + +The only function provided by this module is: + +.. function:: cfw.synthesize_multiple(output_file_path, quit_after, backwards, text) + + Synthesize several text fragments into a single WAVE file. + + The returned tuple ``(sr, synt, anchors)`` contains + the sample rate of the output WAVE file, + the number of fragments actually synthesized, + and a list of time values, each representing + the begin time in the output WAVE file + of the corresponding text fragment. + + Note that if ``quit_after`` is specified, + the number ``synt`` of fragments actually synthesized + might be less than the number of fragments in ``text``. + + :param string output_file_path: the path of the WAVE file to be created, UTF-8 encoded + :param float quit_after: stop synthesizing after reaching the given duration (in seconds) + :param int backwards: if nonzero, synthesize backwards, that is, + starting from the last fragment. + In any case, the fragments in the output WAVE file + will be in natural order. + This option is meaningful only if ``quit_after > 0``. + :param list text: a list of ``(voice_code, fragment_text)`` tuples + with the text to be synthesized. + The ``voice_code`` is the the Festival voice code + (e.g., ``(language_english)``, ``(language_scots_gaelic)``, + ``(language_italian)``, etc.). + The ``fragment_text`` must be UTF-8 encoded. + :rtype: tuple + +""" diff --git a/aeneas/cfw/cfw_driver.cc b/aeneas/cfw/cfw_driver.cc new file mode 100644 index 00000000..a9e3d329 --- /dev/null +++ b/aeneas/cfw/cfw_driver.cc @@ -0,0 +1,147 @@ +/* + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +Python C Extension for synthesizing text with Festival + +*/ + +#include +#include +#include + +#include "cfw_func.h" +#include "EST_walloc.h" + +#define DRIVER_SUCCESS 0 +#define DRIVER_FAILURE 1 + +// print usage +void _usage(const char *prog) { + printf("\n"); + printf("Usage: %s VOICE_CODE TEXT AUDIO_FILE.wav QUIT_AFTER BACKWARDS\n", prog); + printf("\n"); + printf("Example: %s \"(language_english)\" \"Hello|World|My|Dear|Friend\" /tmp/out.wav 0.0 0\n", prog); + printf(" %s \"(language_english)\" \"Hello|World|My|Dear|Friend\" /tmp/out.wav 0.0 1\n", prog); + printf(" %s \"(language_english)\" \"Hello|World|My|Dear|Friend\" /tmp/out.wav 2.0 0\n", prog); + printf(" %s \"(language_english)\" \"Hello|World|My|Dear|Friend\" /tmp/out.wav 2.0 1\n", prog); + printf("\n"); +} + +// split a given string using a delimiter character +// adapted from +// http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c +char **_str_split(char* a_str, const char a_delim, int *count) { + char** result = 0; + char* tmp = a_str; + char* last_delim = 0; + char delim[2]; + delim[0] = a_delim; + delim[1] = 0; + + // count how many elements will be extracted + while (*tmp) { + if (a_delim == *tmp) { + ++(*count); + last_delim = tmp; + } + ++tmp; + } + + // add space for trailing token + (*count) += last_delim < (a_str + strlen(a_str) - 1); + + // tokenize + result = (char**)safe_wcalloc((*count) * sizeof(char*)); + if (result) { + size_t idx = 0; + char* token = strtok(a_str, delim); + while (token) { + *(result + idx++) = wstrdup(token); + token = strtok(0, delim); + } + } + return result; +} + +int main(int argc, char **argv) { + + const char *voice_code, *text, *output_file_name; + int sample_rate_ret, backwards; + struct FRAGMENT_INFO fragment; + float quit_after; + struct FRAGMENT_INFO *fragments; + char **texts; + int i, n; + size_t synthesized_ret; + + if (argc < 6) { + _usage(argv[0]); + return DRIVER_FAILURE; + } + voice_code = argv[1]; + text = argv[2]; + output_file_name = argv[3]; + quit_after = (float)atof(argv[4]); + backwards = atoi(argv[5]); + + // split text into fragments + n = 0; + texts = _str_split((char *)text, '|', &n); + + // create fragments + fragments = (struct FRAGMENT_INFO *)safe_wcalloc(sizeof(fragment) * n); + for (i = 0; i < n; ++i) { + fragments[i].voice_code = voice_code; + fragments[i].text = texts[i]; + } + + // synthesize + if(_synthesize_multiple( + output_file_name, + &fragments, + n, + quit_after, + backwards, + &sample_rate_ret, + &synthesized_ret + ) != CFW_SUCCESS) { + printf("Error while calling _synthesize_multiple()\n"); + return DRIVER_FAILURE; + } + printf("Sample rate: %d\n", sample_rate_ret); + printf("Synthesized: %lu\n", synthesized_ret); + for (i = 0; i < synthesized_ret; ++i) { + printf("%d %.3f %.3f\n", i, fragments[i].begin, fragments[i].end); + } + + // deallocate + for (i = 0; i < n; ++i) { + wfree((void *)fragments[i].text); + } + wfree((void *)fragments); + wfree((void *)texts); + fragments = NULL; + texts = NULL; + + return DRIVER_SUCCESS; +} + diff --git a/aeneas/cfw/cfw_func.cc b/aeneas/cfw/cfw_func.cc new file mode 100644 index 00000000..e1f882d2 --- /dev/null +++ b/aeneas/cfw/cfw_func.cc @@ -0,0 +1,287 @@ +/* + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +Python C Extension for synthesizing text with Festival + +*/ + +#include +#include +#include +#include + +#include "festival/festival.h" +#include "cfw_func.h" + +#ifndef CFW_DIRECT_WRITE +#define CFW_DIRECT_WRITE CFW_TRUE +#endif + +static int initialized = CFW_FALSE; + +#if CFW_DIRECT_WRITE +#include "EST_walloc.h" + +static FILE *wave_file = NULL; + +/* +00000000 52 49 46 46 XX XX XX XX 57 41 56 45 66 6d 74 20 |RIFF....WAVEfmt | +00000010 10 00 00 00 01 00 01 00 22 56 00 00 44 ac 00 00 |........"V..D...| +00000020 02 00 10 00 64 61 74 61 XX XX XX XX |....data.... | +*/ +static const unsigned char wave_hdr[44] = { + 'R' , 'I', 'F' , 'F', 0x2c , 0 , 0 , 0 , 'W' , 'A' , 'V' , 'E' , 'f' , 'm' , 't', ' ', + 0x10, 0 , 0 , 0 , 1 , 0 , 1 , 0 , 9 , 0x3d, 0 , 0 , 0x12, 0x7a, 0 , 0 , + 2 , 0 , 0x10 , 0 , 'd' , 'a' , 't' , 'a' , 0 , 0 , 0 , 0 +}; + +// write an uint32_t as a little endian int to file +// that is, least significant byte first +void _write_uint32_t(FILE *f, int value) { + int ix; + for (ix = 0; ix < 4; ix++) { + fputc(value & 0xff, f); + value = value >> 8; + } +} + +// open wave file and write its header +// NOTE: the uint32_t representing the file size and data size +// will be set by _close_wave_file() +// once all audio samples are generated +int _open_wave_file(char const *path, int rate) { + if (path == NULL) { + return CFW_FAILURE; + } + + while (isspace(*path)) { + path++; + } + + wave_file = NULL; + if (path[0] != 0) { + wave_file = fopen(path, "wb"); + } + + if (wave_file == NULL) { + return CFW_FAILURE; + } + + fwrite(wave_hdr, 1, 24, wave_file); + _write_uint32_t(wave_file, rate); + _write_uint32_t(wave_file, rate * 2); + fwrite(&wave_hdr[32], 1, 12, wave_file); + return CFW_SUCCESS; +} + +// close wave file +int _close_wave_file(void) { + long pos; + + if (wave_file == NULL) { + return CFW_FAILURE; + } + + // flush and get the current position, + // which is the file length + fflush(wave_file); + pos = ftell(wave_file); + + // set file size at byte #4 + fseek(wave_file, 4, SEEK_SET); + _write_uint32_t(wave_file, pos - 8); + + // set data size at byte #40 + fseek(wave_file, 40, SEEK_SET); + _write_uint32_t(wave_file, pos - 44); + + // close file + fclose(wave_file); + wave_file = NULL; + + return CFW_SUCCESS; +} +#endif + +// synthesize multiple fragments +int _synthesize_multiple( + const char *output_file_path, + struct FRAGMENT_INFO **fragments_ret, + const size_t number_of_fragments, + const float quit_after, + const int backwards, + int *sample_rate_ret, + size_t *synthesized_ret + ) { + +#if CFW_DIRECT_WRITE + // nothing +#else + EST_Wave wave; + //printf("\n\nUsing EST_Wave\n\n\n"); +#endif + + float current_time; + size_t i, synthesized, start; + int sample_rate; + + start = 0; + + // suppress warning + sample_rate = 16000; + + // festival initialize can be called only once + // and it returns void (no status code is returned) + if (initialized == CFW_FALSE) { + festival_initialize(CFW_LOAD_INIT_FILES, CFW_HEAP_SIZE); + initialized = CFW_TRUE; + } + + if ((backwards != 0) && (quit_after > 0)) { + // synthesize a first time to determine how many fragments + // from the back we need to reach quit_after seconds of audio + + // temporary wave + EST_Wave wave_tmp; + current_time = 0.0; + + // synthesize from the back + for (i = number_of_fragments - 1; ; --i) { + + // set voice + if (festival_eval_command((*fragments_ret)[i].voice_code) != CFW_FESTIVAL_SUCCESS) { + return CFW_FAILURE; + } + + // synthesize + if (festival_text_to_wave((*fragments_ret)[i].text, wave_tmp) != CFW_FESTIVAL_SUCCESS) { + return CFW_FAILURE; + } + // update current time + current_time += ((float)wave_tmp.num_samples()) / wave_tmp.sample_rate(); + start = i; + + // check if we generated >= quit_after seconds of audio + if (current_time >= quit_after) { + break; + } + // end of the loop, checked here because i is size_t i.e. unsigned! + if (i == 0) { + break; + } + } + } + + // number of synthesized fragments + synthesized = 0; + + // reset time + current_time = 0.0; + +#if CFW_DIRECT_WRITE + // open wave file + if (wave_file == NULL) { + // temporary wave + EST_Wave wave_tmp; + // synthesize dummy fragment to get the sample rate + if (festival_text_to_wave("dummy text", wave_tmp) != CFW_FESTIVAL_SUCCESS) { + return CFW_FAILURE; + } + sample_rate = wave_tmp.sample_rate(); + // open wave file + if (_open_wave_file(output_file_path, sample_rate) != CFW_SUCCESS) { + return CFW_FAILURE; + } + } +#endif + + // loop over all input fragments + for (i = start; i < number_of_fragments; ++i) { + + // temporary wave + EST_Wave wave_tmp; + + // set voice + if (festival_eval_command((*fragments_ret)[i].voice_code) != CFW_FESTIVAL_SUCCESS) { + return CFW_FAILURE; + } + + // NOTE: if backwards, we move the anchor times to the first fragments, + // so that looping from 0 to synthesized will give the correct anchors + // despite the fact that they will not be saved with the "correct" text + // this trick avoids copying data around + // if backwards, the user is not expected to use the time anchors anyway + + // set begin time + (*fragments_ret)[i-start].begin = current_time; + // synthesize + if (festival_text_to_wave((*fragments_ret)[i].text, wave_tmp) != CFW_FESTIVAL_SUCCESS) { + return CFW_FAILURE; + } + // update current time + current_time += ((float)wave_tmp.num_samples()) / wave_tmp.sample_rate(); + (*fragments_ret)[i-start].end = current_time; + + // append audio data +#if CFW_DIRECT_WRITE + int numsamples = wave_tmp.num_samples(); + short *buffer = (short *)safe_wcalloc(numsamples * sizeof(short)); + wave_tmp.copy_channel(0, buffer); + fwrite(buffer, numsamples * 2, 1, wave_file); + wfree((void *)buffer); +#else + // NOTE: EST_Wave has a concat operator that allows: + // wave_accumulator += wave_tmp; + // but unfortunately it does a realloc, + // so it is very slow when used on many (> 100) fragments. + wave += wave_tmp; +#endif + + // increase number of synthesized fragments + synthesized += 1; + + // check if we generated >= quit_after seconds of audio + if ((quit_after > 0) && (current_time >= quit_after)) { + break; + } + } + +#if CFW_DIRECT_WRITE + // close wave file + if (_close_wave_file() != CFW_SUCCESS) { + return CFW_FAILURE; + } +#else + // output wave file + wave.save(output_file_path, "riff"); + sample_rate = wave.sample_rate(); +#endif + + // save values to be returned + *sample_rate_ret = sample_rate; + *synthesized_ret = synthesized; + + return CFW_SUCCESS; +} + + + diff --git a/aeneas/cfw/cfw_func.h b/aeneas/cfw/cfw_func.h new file mode 100644 index 00000000..78721790 --- /dev/null +++ b/aeneas/cfw/cfw_func.h @@ -0,0 +1,76 @@ +/* + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +Python C Extension for synthesizing text with Festival + +*/ + +#define CFW_SUCCESS 0 +#define CFW_FAILURE 1 +#define CFW_FALSE 0 +#define CFW_TRUE 1 + +// Festival-specific +// Festival defines SUCCESS = TRUE = 1 and FAILURE = False = 0 +#define CFW_FESTIVAL_SUCCESS 1 +#define CFW_FESTIVAL_FAILURE 0 +#define CFW_HEAP_SIZE 1000000 +#define CFW_LOAD_INIT_FILES 1 + +struct FRAGMENT_INFO { + float begin; + float end; + const char *voice_code; + const char *text; +}; + +/* + Synthesize multiple text fragments, + described by the FRAGMENT_INFO fragments_ret array, + creating a WAVE file at output_file_path. + + If quit_after > 0, then the synthesis is terminated + as soon as the total duration reaches >= quit_after seconds. + + If backwards is != 0, then the synthesis is done + backwards, from the end of the fragments array. + This option is meaningful only if quit_after is > 0, + otherwise it has no effect. + + The sample rate of the output WAVE file is stored + in sample_rate_ret, the number of synthesized fragments + in synthesized_ret, and the begin and end times + are stored in the begin and end attributes of + the elements of fragments_ret. +*/ +int _synthesize_multiple( + const char *output_file_path, + struct FRAGMENT_INFO **fragments_ret, + const size_t number_of_fragments, + const float quit_after, + const int backwards, + int *sample_rate_ret, // int because the Festival lib returns it as such + size_t *synthesized_ret +); + + + diff --git a/aeneas/cfw/cfw_py.cc b/aeneas/cfw/cfw_py.cc new file mode 100644 index 00000000..c41eacb2 --- /dev/null +++ b/aeneas/cfw/cfw_py.cc @@ -0,0 +1,190 @@ +/* + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +Python C Extension for synthesizing text with Festival + +*/ + +#include +#include +#include +#include +#include + +#include +#include "cfw_func.h" + +static PyObject *synthesize_multiple(PyObject *self, PyObject *args) { + PyObject *tuple; + PyObject *anchors; + PyObject *fragments; + + char const *output_file_path; + float const quit_after = 0.0; + int const backwards = 0; + struct FRAGMENT_INFO *fragments_synt; + + int sample_rate; // int because Festival lib returns it as such + size_t number_of_fragments, i, synthesized; + + // s = string + // f = float + // i = integer (used as a boolean, 0=false, 1=true) + // O = object + if (!PyArg_ParseTuple(args, "sfiO", &output_file_path, &quit_after, &backwards, &fragments)) { + PyErr_SetString(PyExc_ValueError, "Error while parsing the arguments"); + Py_XDECREF(fragments); + return NULL; + } + Py_INCREF(fragments); + + // get number of fragments + number_of_fragments = PyList_Size(fragments); + + // allocate an array of FRAGMENT_INFO to be passed to _synthesize_multiple() + fragments_synt = (struct FRAGMENT_INFO *)calloc(number_of_fragments, sizeof(struct FRAGMENT_INFO)); + + // loop over all input fragments + for (i = 0; i < number_of_fragments; ++i) { + PyObject *fragment; + + // get fragment, which is a tuple (fragment_language, fragment_text) + fragment = PyList_GetItem(fragments, i); + Py_INCREF(fragment); + if (!PyArg_ParseTuple(fragment, "ss", &(fragments_synt[i].voice_code), &(fragments_synt[i].text))) { + PyErr_SetString(PyExc_ValueError, "Error while parsing the arguments"); + free((void*)fragments_synt); + fragments_synt = NULL; + Py_XDECREF(fragments); + Py_XDECREF(fragment); + return NULL; + } + Py_DECREF(fragment); + //printf("Added: %s %s\n", fragments_synt[i].voice_code, fragments_synt[i].text); + } + Py_DECREF(fragments); + + // synthesize multiple + if (_synthesize_multiple( + output_file_path, + &fragments_synt, + number_of_fragments, + quit_after, + backwards, + &sample_rate, + &synthesized + ) != 0) { + PyErr_SetString(PyExc_ValueError, "Error while synthesizing multiple fragments"); + free((void*)fragments_synt); + fragments_synt = NULL; + return NULL; + } + + // allocates the list of anchors + //anchors = PyList_New(n); + anchors = PyList_New(0); + + // loop over all input fragments + for (i = 0; i < synthesized; i++) { + PyObject *anchor; + + anchor = PyTuple_New(2); + // PyTuple_SetItem steals a reference, so no PyDECREF is needed + PyTuple_SetItem(anchor, 0, Py_BuildValue("f", fragments_synt[i].begin)); + PyTuple_SetItem(anchor, 1, Py_BuildValue("f", fragments_synt[i].end)); + PyList_Append(anchors, anchor); + Py_DECREF(anchor); + } + + // deallocate + free((void*)fragments_synt); + fragments_synt = NULL; + + // build the tuple to be returned + // NOTE: returning sample_rate as an int, as the Festival lib does + tuple = PyTuple_New(3); + PyTuple_SetItem(tuple, 0, Py_BuildValue("i", sample_rate)); + PyTuple_SetItem(tuple, 1, Py_BuildValue("I", synthesized)); + PyTuple_SetItem(tuple, 2, anchors); + + return tuple; +} + +static PyMethodDef cfw_methods[] = { + { + "synthesize_multiple", + synthesize_multiple, + METH_VARARGS, + "Synthesize multiple text fragments with Festival\n" + ":param string output_file_path: the path of the WAVE file to be created\n" + ":param float quit_after: if > 0, stop synthesizing when reaching quit_after seconds\n" + ":param int backwards: if 1, synthesize backwards, from the last fragment to the first\n" + ":param list fragments: list of (voice_code, text) tuples of text fragments to be synthesized\n" + ":rtype: tuple (sample_rate, synthesized, list) where list is a list of (begin, end) time values" + }, + { + NULL, + NULL, + 0, + NULL + } +}; + +// Python 2 and 3 +#if PY_MAJOR_VERSION >= 3 +static struct PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "cfw", /* m_name */ + "cfw", /* m_doc */ + -1, /* m_size */ + cfw_methods, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ +}; +#endif + +static PyObject *moduleinit(void) { + PyObject *m; + +#if PY_MAJOR_VERSION >= 3 + m = PyModule_Create(&moduledef); +#else + m = Py_InitModule("cfw", cfw_methods); +#endif + + return m; +} + +#if PY_MAJOR_VERSION >= 3 +PyMODINIT_FUNC PyInit_cfw(void) { + return moduleinit(); +} +#else +PyMODINIT_FUNC initcfw(void) { + moduleinit(); +} +#endif + + + diff --git a/aeneas/cfw/cfw_setup.py b/aeneas/cfw/cfw_setup.py new file mode 100644 index 00000000..f4f95f5b --- /dev/null +++ b/aeneas/cfw/cfw_setup.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# coding=utf-8 + +# aeneas is a Python/C library and a set of tools +# to automagically synchronize audio and text (aka forced alignment) +# +# Copyright (C) 2012-2013, Alberto Pettarin (www.albertopettarin.it) +# Copyright (C) 2013-2015, ReadBeyond Srl (www.readbeyond.it) +# Copyright (C) 2015-2016, Alberto Pettarin (www.albertopettarin.it) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" +Compile the Python C Extension for synthesizing text with Festival. + +.. versionadded:: 1.6.1 +""" + +from __future__ import absolute_import +from __future__ import print_function +from setuptools import Extension +from setuptools import setup +import sys + + +CMODULE = Extension( + name="cfw", + sources=[ + "cfw_py.cc", + "cfw_func.cc" + ], + include_dirs=[ + "festival", + "speech_tools" + ], + libraries=[ + "Festival", + "estools", + "estbase", + "eststring", + ] +) + +setup( + name="cfw", + version="1.6.1", + description="Python C Extension for synthesizing text with Festival.", + ext_modules=[CMODULE] +) + +print("\n[INFO] Module cfw successfully compiled\n") +sys.exit(0) diff --git a/aeneas/cfw/festival/ModuleDescription.h b/aeneas/cfw/festival/ModuleDescription.h new file mode 100644 index 00000000..3c1f8477 --- /dev/null +++ b/aeneas/cfw/festival/ModuleDescription.h @@ -0,0 +1,150 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + +#ifndef __MODULEDESCRIPTION_H__ +#define __MODULEDESCRIPTION_H__ + +#include +#include + +using namespace std; + +#include "EST_String.h" + +/** Machine readable descriptions of modules. Useful for help messages + * and for verifying that a set of modules should work together. + * + * This is a struct rather than a class so that it can be initialised + * in the source of the module. + * @author Richard Caley + * @version $Id: ModuleDescription.h,v 1.4 2014/11/25 14:32:04 robert Exp $ + */ + +struct ModuleDescription { + + /**@name limits */ +//@{ +/// Number of lines of descriptive text. +#define MD_MAX_DESCRIPTION_LINES (10) +/// Space for input streams. +#define MD_MAX_INPUT_STREAMS (5) +/// Space for optional streams. +#define MD_MAX_OPTIONAL_STREAMS (5) +/// Space for output streams. +#define MD_MAX_OUTPUT_STREAMS (5) +/// Space for parameters. +#define MD_MAX_PARAMETERS (10) +//@} + +/**@name Parameter types + * Use these for types to avoid typoes and to allow for a cleverer system + * at a later date. + */ +//@{ +/// t or nil +#define mpt_bool "BOOL" +/// Positive integer +#define mpt_natnum "NATNUM" +/// Integer +#define mpt_int "INT" +/// Floating point number +#define mpt_float "FLOAT" +/// Any string +#define mpt_string "STRING" +/// A UnitDatabase +#define mpt_unitdatabase "UNITDATABASE" +/// Anything +#define mpt_other "OTHER" +//@} + + /// name of module + const char * name; + /// version number of module + float version; + /// where it comes from + const char * organisation; + /// person(s) responsible + const char * author; + + /// general description + const char * description[MD_MAX_DESCRIPTION_LINES]; + + /// streams affected. + struct stream_parameter { + /// default stream name + const char * name; + /// what itis used for + const char * description; + }; + + /// Streams which must have values when the module is called. + struct stream_parameter input_streams[MD_MAX_INPUT_STREAMS]; + /// Streams which may or not be defined. + struct stream_parameter optional_streams[MD_MAX_OPTIONAL_STREAMS]; + /// Streams which will be defined after the module has run. + struct stream_parameter output_streams[MD_MAX_OUTPUT_STREAMS]; + + /// Record for a parameter. + struct parameter { + /// Name of parameter + const char * name; + /// Type of value. + const char * type; + /// Default value assumed. + const char * default_val; + /// Human readable description of effect. + const char * description; + }; + /// Parameters which effect the module. + struct parameter parameters[MD_MAX_PARAMETERS]; + + /// Create human readable string from description. + static EST_String to_string(const ModuleDescription &desc); + /// Create a module description, initialising it properly. + static struct ModuleDescription *create(); + /// Print the description to the strream. + static ostream &print(ostream &s, const ModuleDescription &desc); + + static int print(FILE *s, const ModuleDescription &desc); + +}; + +/// Output operator for descriptions. +ostream &operator << (ostream &stream, const ModuleDescription &desc); + +//VAL_REGISTER_CLASS_DCLS(moddesc,ModuleDescription) // clang/llvm complains about this (Rob) +//SIOD_REGISTER_CLASS_DCLS(moddesc,ModuleDescription) +VAL_REGISTER_TYPE_DCLS(moddesc,ModuleDescription) +SIOD_REGISTER_TYPE_DCLS(moddesc,ModuleDescription) + +#endif diff --git a/aeneas/cfw/festival/Phone.h b/aeneas/cfw/festival/Phone.h new file mode 100644 index 00000000..1cb29fe8 --- /dev/null +++ b/aeneas/cfw/festival/Phone.h @@ -0,0 +1,147 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* Phone and PhoneSet class header file */ +/* */ +/*=======================================================================*/ +#ifndef __PHONE_H__ +#define __PHONE_H__ + +class Phone{ + private: + EST_String name; + EST_StrStr_KVL features; +public: + Phone() {name = "";} + EST_String &phone_name() {return name;} + void set_phone_name(const EST_String &p) {name = p;} + void add_feat(const EST_String &f, const EST_String &v) + { features.add_item(f,v); } + const EST_String &val(const EST_String &key) const + { return features.val_def(key,"");} + const EST_String &val(const EST_String &key,const EST_String &def) + { return features.val_def(key,def); } + int match_features(Phone *foreign); + + inline friend ostream& operator<<(ostream& s, Phone &p); + + Phone & operator =(const Phone &a); +}; + +inline ostream& operator<<(ostream& s, Phone &p) +{ + s << "[PHONE " << p.phone_name() << "]"; +// s << p.features << endl; + return s; +} + +inline Phone &Phone::operator = (const Phone &a) +{ + name = a.name; + features = a.features; + return *this; +} + + +class PhoneSet{ + private: + EST_String psetname; + LISP silences; + LISP map; + LISP feature_defs; // List of features and values + LISP phones; +public: + PhoneSet() {psetname = ""; phones=feature_defs=map=silences=NIL; + gc_protect(&silences); gc_protect(&map); + gc_protect(&feature_defs); gc_protect(&phones);} + ~PhoneSet(); + const EST_String &phone_set_name() const {return psetname;} + void set_phone_set_name(const EST_String &p) {psetname = p;} + int present(const EST_String &phone) const + {return (siod_assoc_str(phone,phones) != NIL);} + int is_silence(const EST_String &ph) const; + void set_silences(LISP sils); + void set_map(LISP m); + LISP get_silences(void) {return silences;} + LISP get_phones(void) {return phones;} + LISP get_feature_defs(void) {return reverse(feature_defs);} + int num_phones(void) const {return siod_llength(phones);} + Phone *member(const EST_String &phone) const; + int phnum(const char *phone) const; + const char *phnum(const int n) const; + int add_phone(Phone *phone); + int feat_val(const EST_String &feat, const EST_String &val) + { return (siod_member_str(val, + car(cdr(siod_assoc_str(feat,feature_defs)))) + != NIL); } + void set_feature(const EST_String &name, LISP vals); + + inline friend ostream& operator<<(ostream& s, PhoneSet &p); + + Phone *find_matched_phone(Phone *phone); + PhoneSet & operator =(const PhoneSet &a); +}; + +inline ostream& operator<<(ostream& s, PhoneSet &p) +{ + s << p.phone_set_name(); return s; +} + +const EST_String &map_phone(const EST_String &fromphonename, + const EST_String &fromsetname, + const EST_String &tosetname); +const EST_String &ph_feat(const EST_String &ph,const EST_String &feat); +int ph_is_silence(const EST_String &ph); +int ph_is_vowel(const EST_String &ph); +int ph_is_consonant(const EST_String &ph); +int ph_is_liquid(const EST_String &ph); +int ph_is_approximant(const EST_String &ph); +int ph_is_stop(const EST_String &ph); +int ph_is_nasal(const EST_String &ph); +int ph_is_fricative(const EST_String &ph); +int ph_is_sonorant(const EST_String &ph); +int ph_is_obstruent(const EST_String &ph); +int ph_is_voiced(const EST_String &ph); +int ph_is_sonorant(const EST_String &ph); +int ph_is_syllabic(const EST_String &ph); +int ph_sonority(const EST_String &ph); +EST_String ph_silence(void); + +PhoneSet *phoneset_name_to_set(const EST_String &name); + +#endif + + + diff --git a/aeneas/cfw/festival/festival.h b/aeneas/cfw/festival/festival.h new file mode 100644 index 00000000..c11d5b8b --- /dev/null +++ b/aeneas/cfw/festival/festival.h @@ -0,0 +1,168 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Top level .h file: main public functions */ +/*=======================================================================*/ +#ifndef __FESTIVAL_H__ +#define __FESTIVAL_H__ + +#include +#include + +using namespace std; + +#include "EST.h" +#include "EST_cutils.h" +#include "siod.h" + +#include "Phone.h" + +#ifndef streq +#define streq(X,Y) (strcmp(X,Y)==0) +#endif + +struct ModuleDescription; + +/* An iostream for outputing debug messages, switchable */ +/* to /dev/null or cerr */ +extern ostream *cdebug; +#define cwarn cout +extern "C" FILE* stddebug; +extern int ft_server_socket; +extern const char *festival_version; + +/* For server/client */ +#define FESTIVAL_DEFAULT_PORT 1314 +int festival_socket_client(const char *host,int port); +int festival_start_server(int port); + +void festival_initialize(int load_init_files,int heap_size); +void festival_init_lang(const EST_String &language); +int festival_eval_command(const EST_String &expr); +int festival_load_file(const EST_String &fname); +int festival_say_file(const EST_String &fname); +int festival_say_text(const EST_String &text); +int festival_text_to_wave(const EST_String &text,EST_Wave &wave); +void festival_repl(int interactive); +void festival_server_mode(void); +void festival_wait_for_spooler(void); +void festival_tidy_up(); + +/* Never used and conflicts with some external system */ +/* typedef void (*FT_Module)(EST_Utterance &utt); */ + +/* Feature functions */ +void festival_def_nff(const EST_String &name,const EST_String &sname, + EST_Item_featfunc func,const char *doc); +typedef EST_Val (*FT_ff_pref_func)(EST_Item *s,const EST_String &name); +void festival_def_ff_pref(const EST_String &pref,const EST_String &sname, + FT_ff_pref_func func, const char *doc); +EST_Val ffeature(EST_Item *s, const EST_String &name); + +/* proclaim a new module + option Copyright to add to startup banner + description is a computer readable description of the + module + */ +void proclaim_module(const EST_String &name, + const EST_String &banner_copyright, + const ModuleDescription *description = NULL); + +void proclaim_module(const EST_String &name, + const ModuleDescription *description = NULL); + +void init_module_subr(const char *name, LISP (*fcn)(LISP), const ModuleDescription *description); + +/* Some basic functions for accessing structures created by */ +/* various modelling techniques */ +EST_Val wagon_predict(EST_Item *s, LISP tree); +LISP wagon_pd(EST_Item *s, LISP tree); +EST_Val lr_predict(EST_Item *s, LISP lr_model); + +/* Grammar access functions */ +EST_Ngrammar *get_ngram(const EST_String &name, + const EST_String &filename = EST_String::Empty); +EST_WFST *get_wfst(const EST_String &name, + const EST_String &filename = EST_String::Empty); +LISP lisp_wfst_transduce(LISP wfstname, LISP input); + +EST_String map_pos(LISP posmap, const EST_String &pos); +LISP map_pos(LISP posmap, LISP pos); + +/* On error do a longjmp to appropriate place */ +/* This is done as a macro so the compiler can tell its non-returnable */ +#define festival_error() (errjmp_ok ? longjmp(*est_errjmp,1) : festival_tidy_up(),exit(-1)) + +/* Add new (utterance) module */ +void festival_def_utt_module(const char *name, + LISP (*fcn)(LISP), + const char *docstring); + +void utt_cleanup(EST_Utterance &u); // delete all relations +const EST_String utt_iform_string(EST_Utterance &utt); +LISP utt_iform(EST_Utterance &utt); +const EST_String utt_type(EST_Utterance &utt); +void add_item_features(EST_Item *s,LISP features); + +extern const char *festival_libdir; +extern const char *festival_datadir; + +// Module specific LISP/etc definitions +void festival_init_modules(void); + +// Some general functions +LISP ft_get_param(const EST_String &pname); + +// SIOD user defined types used by festival + +#define tc_festival_dummyobject tc_application_1 +#define tc_festival_unit tc_application_2 +#define tc_festival_unitdatabase tc_application_3 +#define tc_festival_unitindex tc_application_4 +#define tc_festival_join tc_application_5 +#define tc_festival_schememoduledescription tc_application_6 +#define tc_festival_unitcatalogue tc_application_7 + +// used to recognise our types +#define tc_festival_first_type tc_festival_dummyobject +#define tc_festival_last_type tc_festival_schememoduledescription +#define is_festival_type(X) ((X) >= tc_festival_first_type && (X) <= tc_festival_last_type) + +class UnitDatabase *get_c_unitdatabase(LISP x); + +#define FESTIVAL_HEAP_SIZE 10000000 + +#endif diff --git a/aeneas/cfw/festival/fngram.h b/aeneas/cfw/festival/fngram.h new file mode 100644 index 00000000..749635b1 --- /dev/null +++ b/aeneas/cfw/festival/fngram.h @@ -0,0 +1,46 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Authors: Alan W Black */ +/* Date : October 1996 */ +/*-----------------------------------------------------------------------*/ +/* LISP level interface to ngrams */ +/* */ +/*=======================================================================*/ + +#ifndef __FNGRAM_H__ +#define __FNGRAM_H__ + +const EST_Ngrammar &get_ngram(const EST_String &name); +const EST_WFST &get_wfst(const EST_String &name); + +#endif // __FNGRAM_H__ diff --git a/aeneas/cfw/festival/intonation.h b/aeneas/cfw/festival/intonation.h new file mode 100644 index 00000000..06fa7ea1 --- /dev/null +++ b/aeneas/cfw/festival/intonation.h @@ -0,0 +1,51 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Shared intonation utilies */ +/* */ +/*=======================================================================*/ +#ifndef __INTONATION_H__ +#define __INTONATION_H__ + +EST_Item *add_target(EST_Utterance *u, EST_Item *seg, + float pos,float val); +EST_Item *add_IntEvent(EST_Utterance *u, EST_Item *syl, + const EST_String &label); + +#endif /* __INTONATION_H__ */ + + + diff --git a/aeneas/cfw/festival/lexicon.h b/aeneas/cfw/festival/lexicon.h new file mode 100644 index 00000000..1bd41b61 --- /dev/null +++ b/aeneas/cfw/festival/lexicon.h @@ -0,0 +1,130 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Shared lexicon utilities */ +/* Top level form (simply an s-expression of the input form */ +/* */ +/*=======================================================================*/ +#ifndef __LEXICON_H__ +#define __LEXICON_H__ + +#include "EST_Pathname.h" + +enum lex_type_t {lex_external, lex_internal}; +class Lexicon{ + private: + lex_type_t type; + EST_String name; + EST_String ps_name; + LISP addenda; // for personal local changes + LISP posmap; + int comp_num_entries; + EST_Pathname bl_filename; + FILE *binlexfp; + EST_String lts_method; + EST_String lts_ruleset; + int blstart; + LISP index_cache; + void binlex_init(void); + LISP lookup_addenda(const EST_String &word, LISP features); + LISP lookup_complex(const EST_String &word, LISP features); + LISP lookup_lts(const EST_String &word, LISP features); + LISP bl_bsearch(const EST_String &word,LISP features, + int start,int end,int depth); + LISP bl_find_next_entry(int pos); + LISP bl_find_actual_entry(int pos,const EST_String &word,LISP features); + int lex_entry_match; + LISP matched_lexical_entries; +public: + LISP pre_hooks; + LISP post_hooks; + + Lexicon(); + ~Lexicon(); + const EST_String &lex_name() const {return name;} + const EST_String &phoneset_name() const {return ps_name;} + void set_lex_name(const EST_String &p) {name = p;} + const EST_String &get_lex_name(void) const { return name; } + void set_phoneset_name(const EST_String &p) {ps_name = p;} + void set_lts_method(const EST_String &p) {lts_method = p;} + void set_lts_ruleset(const EST_String &p) {lts_ruleset = p;} + void set_pos_map(LISP p) {posmap = p;} + const EST_String &get_lts_ruleset(void) const { return lts_ruleset; } + void set_bl_filename(const EST_String &p) + {bl_filename = p; + if (binlexfp != NULL) fclose(binlexfp); + binlexfp=NULL;} + void add_addenda(LISP entry) {addenda = cons(entry,addenda);} + LISP lookup(const EST_String &word,const LISP features); + LISP lookup_all(const EST_String &word); + EST_String str_lookup(const EST_String &word,const LISP features); + int in_lexicon(const EST_String &word,LISP features); + int num_matches() { return lex_entry_match; } + void bl_lookup_cache(LISP cache, const EST_String &word, + int &start, int &end, int &depth); + void add_to_cache(LISP index_cache, + const EST_String &word, + int start,int mid, int end); + + inline friend ostream& operator<<(ostream& s, Lexicon &p); + + Lexicon & operator =(const Lexicon &a); +}; + +inline ostream& operator<<(ostream& s, Lexicon &p) +{ + s << "[LEXICON " << p.lex_name() << "]" ; + return s; +} + +inline Lexicon &Lexicon::operator = (const Lexicon &a) +{ + name = a.name; + addenda = a.addenda; + bl_filename = a.bl_filename; + binlexfp = NULL; + lts_method = a.lts_method; + return *this; +} + +LISP lex_lookup_word(const EST_String &word,LISP features); +EST_String lex_current_phoneset(void); +LISP lex_select_lex(LISP lexname); +LISP lex_syllabify(LISP phones); +LISP lex_syllabify_phstress(LISP phones); +int in_current_lexicon(const EST_String &word,LISP features); + +#endif /* __LEXICON_H__ */ diff --git a/aeneas/cfw/festival/module_support.h b/aeneas/cfw/festival/module_support.h new file mode 100644 index 00000000..990c9954 --- /dev/null +++ b/aeneas/cfw/festival/module_support.h @@ -0,0 +1,108 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Jul 29 1997 */ + /* -------------------------------------------------------------------- */ + /* Some things which are useful in modules. */ + /* */ + /*************************************************************************/ + + +#ifndef __MODULE_SUPPORT_H__ +#define __MODULE_SUPPORT_H__ + +#include "EST.h" +#include "festival.h" +#include "ModuleDescription.h" + +// To extract arguments passed as a list + +void unpack_multiple_args(LISP args, LISP &v1, LISP &v2, LISP &v3, LISP &v4); +void unpack_multiple_args(LISP args, LISP &v1, LISP &v2, LISP &v3, LISP &v4, LISP &v5); + +// To extract arguments for a module, modules are called +// (module-function Utterance StreamName1 StreamName2 StreamName3 ...) + +// To tell the unpacking functions what is expected of the stream. +enum RelArgType { + sat_existing, // must exist + sat_new, // must be new + sat_replace, // erase if there, then create + sat_as_is // take what we find +}; + + +void unpack_relation_arg(EST_Utterance *utt, + LISP lrelation_name, + EST_String &relation_name, EST_Relation *&relation, RelArgType type); + +void unpack_module_args(LISP args, + EST_Utterance *&utt); +void unpack_module_args(LISP args, + EST_Utterance *&utt, + EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1); +void unpack_module_args(LISP args, + EST_Utterance *&utt, + EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1, + EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2 + ); +void unpack_module_args(LISP args, + EST_Utterance *&utt, + EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1, + EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2, + EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3 + ); +void unpack_module_args(LISP args, + EST_Utterance *&utt, + EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1, + EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2, + EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3, + EST_String &relation4_name, EST_Relation *&relation4, RelArgType type4 + ); +void unpack_module_args(LISP args, + EST_Utterance *&utt, + EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1, + EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2, + EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3, + EST_String &relation4_name, EST_Relation *&relation4, RelArgType type4, + EST_String &relation5_name, EST_Relation *&relation5, RelArgType type5 + ); + +LISP lisp_parameter_get(const EST_String parameter); +int int_parameter_get(const EST_String parameter, int def=0); +float float_parameter_get(const EST_String parameter, float def=0.0); +bool bool_parameter_get(const EST_String parameter); +EST_String string_parameter_get(const EST_String parameter, EST_String def=""); + +#endif diff --git a/aeneas/cfw/festival/modules.h b/aeneas/cfw/festival/modules.h new file mode 100644 index 00000000..a06dcd4c --- /dev/null +++ b/aeneas/cfw/festival/modules.h @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : August 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* General functions shared between various simple modules */ +/* e.e. POS, Word, Phrasify etc */ +/* */ +/*=======================================================================*/ +#ifndef __MODULES_H__ +#define __MODULES_H__ + +EST_Item *add_word(EST_Utterance *u, const EST_String &name); +EST_Item *add_word(EST_Utterance *u, LISP word); +EST_Item *add_segment(EST_Utterance *u, const EST_String &s); +EST_Item *add_phrase(EST_Utterance *u); + +void create_phraseinput(EST_Utterance *u); + +#endif /* __MODULES_H__ */ + + + + + + diff --git a/aeneas/cfw/festival/text.h b/aeneas/cfw/festival/text.h new file mode 100644 index 00000000..a6824471 --- /dev/null +++ b/aeneas/cfw/festival/text.h @@ -0,0 +1,68 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Shared text utilities */ +/* */ +/*=======================================================================*/ +#ifndef __TEXT_H__ +#define __TEXT_H__ + +EST_Item *add_token(EST_Utterance *u,EST_Token &t); +void festival_token_init(void); +LISP extract_tokens(LISP file, LISP tokens,LISP ofile); +LISP new_token_utt(void); +void tts_file_xxml(LISP filename); +void tts_file_raw(LISP filename); + +LISP xxml_call_element_function(const EST_String &element, + LISP atts, LISP elements, LISP utt); +LISP xxml_get_tokens(const EST_String &line,LISP feats,LISP utt); + +typedef void (*TTS_app_tok)(EST_Item *token); +typedef void (*TTS_app_utt)(LISP utt); + +LISP tts_chunk_stream(EST_TokenStream &ts, + TTS_app_tok app_tok, + TTS_app_utt app_utt, + LISP eou_tree, + LISP utt); + +void tts_file_user_mode(LISP filename, LISP params); + +#endif /* __TEXT_H__ */ + + + diff --git a/aeneas/cfw/speech_tools/EST.h b/aeneas/cfw/speech_tools/EST.h new file mode 100644 index 00000000..3ae2bbb1 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST.h @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Authors: Paul Taylor, Simon King, Alan Black, Richard Caley */ +/* Date : June 1994-March 1997 */ +/*-----------------------------------------------------------------------*/ +/* Edinburgh Speech Tools General Header File */ +/* */ +/*=======================================================================*/ +#ifndef __EST_H__ +#define __EST_H__ + +// Standard include files. +#include "EST_system.h" +#include + +using namespace std; + +#include "EST_String.h" +#include "EST_string_aux.h" +#include "EST_types.h" + +// Utilities +#include "EST_util_class.h" + +#include "EST_cutils.h" +#include "EST_io_aux.h" + +// Audio I/O +#include "EST_audio.h" + +// Speech Classes +#include "EST_speech_class.h" + +// Linguistic Classes +#include "EST_ling_class.h" + +// Signal Processing +#include "EST_sigpr.h" + +// Grammar +#include "EST_grammar.h" + +// Stats +#include "EST_stats.h" + +#endif /* __EST_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_ChannelType.h b/aeneas/cfw/speech_tools/EST_ChannelType.h new file mode 100644 index 00000000..0a637b4e --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_ChannelType.h @@ -0,0 +1,289 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +#ifndef __EST_CHANNELTYPE_H__ +#define __EST_CHANNELTYPE_H__ + +/**@name Channel Types + */ +//@{ + +/** Symbolic names for coefficient types. + * + * Used to record what kinds of information are in a track and + * anywhere else we need to refer to coefficient types. + * + * @see EST_ChannelType + */ + +enum EST_CoefficientType +{ + /// Linear prediction filter + cot_lpc=0, + /// guaranteed to be the first known type + cot_first=cot_lpc, + /// reflection coefficients. + cot_reflection, + /// Cepstral coefficients + cot_cepstrum, + /// Mel Scale Cepstrum + cot_melcepstrum, + /// Mel Scale filter bank + cot_fbank, + /// Line spectral pairs. + cot_lsf, + /// Tube areas for filter. + cot_tubearea, + /// Unknown filter type. + cot_filter, + /// Free for experimentation + cot_user1, + /// Free for experimentation + cot_user2, + /// Guaranteed to be one more than last legal coefficient type + cot_free +}; + +/**@name Channel Type Numbering Scheme + * + * Channel types are given numbers containing the following information: + * \begin{itemize} + * \item A numeric index. + * \item A Number of differentiations 0-2 + * \item 0 for start, 1 for end + * \end{itemize} + * Things which do not require all these features are packed in according + * to the following rules: + * \begin{itemize} + * \item Single values which can be differentiated are paired as + * if they were start and end positions of an unknown type + * of coefficient. + * \item Single values which can't be differentiated are put in the + * positions where the 3rd derivatives would logically be + * found. + * \end{itemize} + */ +//@{ + +/// extract the coefficient type +#define EST_ChannelTypeCT(T) ( (T) >> 3 ) +/// extract the number of differentiations +#define EST_ChannelTypeD(T) ( (T) >> 1 & 3 ) +/// extract the start/end flag. +#define EST_ChannelTypeSE(T) ( (T) & 1 ) + +/// get start from end +#define EST_ChannelTypeStart(T) EST_CoefChannelId(\ + EST_ChannelTypeCT(T), \ + EST_ChannelTypeD(T), \ + 0) +/// get end from start +#define EST_ChannelTypeEnd(T) EST_CoefChannelId(\ + EST_ChannelTypeCT(T), \ + EST_ChannelTypeD(T), \ + 1) +/// differentiate once +#define EST_ChannelTypeIncD(T) EST_CoefChannelId(\ + EST_ChannelTypeCT(T), \ + EST_ChannelTypeD(T)+1, \ + EST_ChannelTypeSE(T)) +/// differentiate N times +#define EST_ChannelTypeDelta(T, N) EST_CoefChannelId(\ + EST_ChannelTypeCT(T), \ + EST_ChannelTypeD(T)+(N), \ + EST_ChannelTypeSE(T)) +/// integrate once +#define EST_ChannelTypeDecD(T) EST_CoefChannelId(\ + EST_ChannelTypeCT(T), \ + EST_ChannelTypeD(T)-1, \ + EST_ChannelTypeSE(T)) + + +/** Build a number representing a channel type for a coefficient type. + * + * CT = coefficient type + * D = Number of levels of differentiation. + * SE = Start=0 end=1 + */ +#define EST_CoefChannelId(CT,D,SE) ( (CT)<<3 | ((D)<<1 & 6) | ((SE)&1) ) + +/** Build a number representing a channel type for a single value which can + * N = count starting from 0 + * D = Number of levels of differentiation. + * be differentiated. + */ + +#define EST_DiffChannelId(N,D) ( EST_CoefChannelId(((N)>>1)+(int)cot_free, D, (N)&1) ) + +/** Build a number representing a channel type for a simple value + * such as length or voicing probability. + */ + +#define EST_ChannelId(N) EST_CoefChannelId((N)>>1, 3, (N)&1) +//@} + + +/** Symbolic names for track channels. + * Used in track maps to label channels so they can be accessed without + * knowing exactly where in the track they are. + * + * @see EST_CoefficientType + * @see EST_TrackMap + * @see EST_Track + * @see EST_TrackMap:example + * @author Richard Caley + * @version $Id: EST_ChannelType.h,v 1.4 2009/07/03 17:13:56 awb Exp $ + */ + +enum EST_ChannelType { + /// Value to return for errors, never occurs in TrackMaps + channel_unknown = EST_ChannelId(0), + /// order of analysis. + channel_order = EST_ChannelId(1), + /// So we know how many there are + first_channel_type=channel_order, + /// Peak amplitude. + channel_peak = EST_ChannelId(2), + /// Duration of section of signal. + channel_duration = EST_ChannelId(3), + /// Length of section in samples. + channel_length = EST_ChannelId(4), + /// Offset from frame center to center of window + channel_offset = EST_ChannelId(5), + /// Voicing decision. + channel_voiced = EST_ChannelId(6), + /// Number of related frame in another track. + channel_frame = EST_ChannelId(7), + /// Time in seconds this frame refers to. + channel_time = EST_ChannelId(8), + + /// RMS power of section of signal. + channel_power = EST_DiffChannelId(0,0), + channel_power_d = EST_DiffChannelId(0,1), + channel_power_a = EST_DiffChannelId(0,2), + /// RMS energy of section of signal. + channel_energy = EST_DiffChannelId(1,0), + channel_energy_d = EST_DiffChannelId(1,1), + channel_energy_a = EST_DiffChannelId(1,2), + /// F0 in Hz. + channel_f0 = EST_DiffChannelId(2,0), + channel_f0_d = EST_DiffChannelId(2,1), + channel_f0_a = EST_DiffChannelId(2,2), + + channel_lpc_0 = EST_CoefChannelId(cot_lpc,0,0), + channel_lpc_N = EST_CoefChannelId(cot_lpc,0,1), + channel_lpc_d_0 = EST_CoefChannelId(cot_lpc,1,0), + channel_lpc_d_N = EST_CoefChannelId(cot_lpc,1,1), + channel_lpc_a_0 = EST_CoefChannelId(cot_lpc,2,0), + channel_lpc_a_N = EST_CoefChannelId(cot_lpc,2,1), + + channel_reflection_0 = EST_CoefChannelId(cot_reflection,0,0), + channel_reflection_N = EST_CoefChannelId(cot_reflection,0,1), + channel_reflection_d_0 = EST_CoefChannelId(cot_reflection,1,0), + channel_reflection_d_N = EST_CoefChannelId(cot_reflection,1,1), + channel_reflection_a_0 = EST_CoefChannelId(cot_reflection,2,0), + channel_reflection_a_N = EST_CoefChannelId(cot_reflection,2,1), + + + channel_cepstrum_0 = EST_CoefChannelId(cot_cepstrum,0,0), + channel_cepstrum_N = EST_CoefChannelId(cot_cepstrum,0,1), + channel_cepstrum_d_0 = EST_CoefChannelId(cot_cepstrum,1,0), + channel_cepstrum_d_N = EST_CoefChannelId(cot_cepstrum,1,1), + channel_cepstrum_a_0 = EST_CoefChannelId(cot_cepstrum,2,0), + channel_cepstrum_a_N = EST_CoefChannelId(cot_cepstrum,2,1), + + + channel_melcepstrum_0 = EST_CoefChannelId(cot_melcepstrum,0,0), + channel_melcepstrum_N = EST_CoefChannelId(cot_melcepstrum,0,1), + channel_melcepstrum_d_0 = EST_CoefChannelId(cot_melcepstrum,1,0), + channel_melcepstrum_d_N = EST_CoefChannelId(cot_melcepstrum,1,1), + channel_melcepstrum_a_0 = EST_CoefChannelId(cot_melcepstrum,2,0), + channel_melcepstrum_a_N = EST_CoefChannelId(cot_melcepstrum,2,1), + + channel_fbank_0 = EST_CoefChannelId(cot_fbank,0,0), + channel_fbank_N = EST_CoefChannelId(cot_fbank,0,1), + channel_fbank_d_0 = EST_CoefChannelId(cot_fbank,1,0), + channel_fbank_d_N = EST_CoefChannelId(cot_fbank,1,1), + channel_fbank_a_0 = EST_CoefChannelId(cot_fbank,2,0), + channel_fbank_a_N = EST_CoefChannelId(cot_fbank,2,1), + + + channel_lsf_0 = EST_CoefChannelId(cot_lsf,0,0), + channel_lsf_N = EST_CoefChannelId(cot_lsf,0,1), + channel_lsf_d_0 = EST_CoefChannelId(cot_lsf,1,0), + channel_lsf_d_N = EST_CoefChannelId(cot_lsf,1,1), + channel_lsf_a_0 = EST_CoefChannelId(cot_lsf,2,0), + channel_lsf_a_N = EST_CoefChannelId(cot_lsf,2,1), + + + channel_tubearea_0 = EST_CoefChannelId(cot_tubearea,0,0), + channel_tubearea_N = EST_CoefChannelId(cot_tubearea,0,1), + channel_tubearea_d_0 = EST_CoefChannelId(cot_tubearea,1,0), + channel_tubearea_d_N = EST_CoefChannelId(cot_tubearea,1,1), + channel_tubearea_a_0 = EST_CoefChannelId(cot_tubearea,2,0), + channel_tubearea_a_N = EST_CoefChannelId(cot_tubearea,2,1), + + + channel_filter_0 = EST_CoefChannelId(cot_filter,0,0), + channel_filter_N = EST_CoefChannelId(cot_filter,0,1), + channel_filter_d_0 = EST_CoefChannelId(cot_filter,1,0), + channel_filter_d_N = EST_CoefChannelId(cot_filter,1,1), + channel_filter_a_0 = EST_CoefChannelId(cot_filter,2,0), + channel_filter_a_N = EST_CoefChannelId(cot_filter,2,1), + + + channel_user1_0 = EST_CoefChannelId(cot_user1,0,0), + channel_user1_N = EST_CoefChannelId(cot_user1,0,1), + channel_user1_d_0 = EST_CoefChannelId(cot_user1,1,0), + channel_user1_d_N = EST_CoefChannelId(cot_user1,1,1), + channel_user1_a_0 = EST_CoefChannelId(cot_user1,2,0), + channel_user1_a_N = EST_CoefChannelId(cot_user1,2,1), + + + channel_user2_0 = EST_CoefChannelId(cot_user2,0,0), + channel_user2_N = EST_CoefChannelId(cot_user2,0,1), + channel_user2_d_0 = EST_CoefChannelId(cot_user2,1,0), + channel_user2_d_N = EST_CoefChannelId(cot_user2,1,1), + channel_user2_a_0 = EST_CoefChannelId(cot_user2,2,0), + channel_user2_a_N = EST_CoefChannelId(cot_user2,2,1), + + + + last_channel_type = channel_f0_a, + /// Can be used to size arrays etc. + num_channel_types +}; +//@} +typedef enum EST_ChannelType EST_ChannelType; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Chunk.h b/aeneas/cfw/speech_tools/EST_Chunk.h new file mode 100644 index 00000000..67bba3a1 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Chunk.h @@ -0,0 +1,251 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: February 1997 */ + /* -------------------------------------------------------------------- */ + /* */ + /* Use counted memory chunks and smart pointers to them. */ + /* */ + /************************************************************************/ + +#if ! defined(__EST_CHUNK_H__) +#define __EST_CHUNK_H__ + +#define HAVE_WALLOC_H (1) + +#include +using namespace std; +#include +#include + +// Warn when getting a writable version of a shared chunk -- +// useful for minimising copies. + +/* #define __INCLUDE_CHUNK_WARNINGS__ (1) */ + +#if defined(__INCULDE_CHUNK_WARNINGS__) +# define CHUNK_WARN(WHAT) do { cerr << "chunk: " <size:0; }; + int shareing(void) const { return ptr?(ptr->count >1):0; }; + int count(void) const { return ptr?(ptr->count):-1; }; + + EST_ChunkPtr &operator = (EST_ChunkPtr cp) CII({ + // doing it in this order means self assignment is safe. + if (cp.ptr) + ++ *(cp.ptr); + if (ptr) + -- *ptr; + ptr=cp.ptr; + return *this; + }); + + // If they manage to get hold of one... + // Actually usually used to assign NULL and so (possibly) deallocate + // the chunk currently pointed to. + EST_ChunkPtr &operator = (EST_Chunk *chp) CII({ + // doing it in this order means self assignment is safe. + if (chp) + ++ *chp; + if (ptr) + -- *ptr; + ptr=chp; + return *this; + }); + + // Casting to a non-const pointer causes a + // warning to stderr if the chunk is shared. + operator char*() CII({ + if (ptr && ptr->count > 1) + { + CHUNK_WARN("getting writable version of shared chunk\n"); + cp_make_updatable(*this); + } + return ptr?&(ptr->memory[0]):(char *)NULL; + }); + operator const char*() const CII({ + return ptr?&(ptr->memory[0]):(const char *)NULL; + }); + operator const char*() CII({ + return ptr?&(ptr->memory[0]):(const char *)NULL; + }); + + + const char operator [] (int i) const { return ptr->memory[i]; }; + char &operator () (int i) CII({ + if (ptr->count>1) + { + CHUNK_WARN("getting writable version of shared chunk\n"); + cp_make_updatable(*this); + } + return ptr->memory[i]; + }); + + // Creating a new one + friend EST_ChunkPtr chunk_allocate(int size); + friend EST_ChunkPtr chunk_allocate(int bytes, const char *initial, int initial_len); + friend EST_ChunkPtr chunk_allocate(int bytes, const EST_ChunkPtr &initial, int initial_start, int initial_len); + + // Make sure the memory isn`t shared. + friend void cp_make_updatable(EST_ChunkPtr &shared, EST_Chunk::EST_chunk_size inuse); + friend void cp_make_updatable(EST_ChunkPtr &shared); + + // Make sure there is enough room (also makes updatable) + friend void grow_chunk(EST_ChunkPtr &shared, EST_Chunk::EST_chunk_size inuse, EST_Chunk::EST_chunk_size newsize); + friend void grow_chunk(EST_ChunkPtr &shared, EST_Chunk::EST_chunk_size newsize); + + // we print it by just printing the chunk + friend ostream &operator << (ostream &s, const EST_ChunkPtr &cp) { return (s<< *cp.ptr); }; + + friend void tester(void); +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Complex.h b/aeneas/cfw/speech_tools/EST_Complex.h new file mode 100644 index 00000000..b78cd206 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Complex.h @@ -0,0 +1,111 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor (pault@cstr.ed.ac.uk) */ +/* Date : December 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* */ +/*************************************************************************/ + +#ifndef __EST_COMPLEX_H__ +#define __EST_COMPLEX_H__ + +#include +#include +using namespace std; + + +#ifndef PI +#define PI 3.14159265358979323846 +#endif + + +/** A class for complex numbers. The class stores the values as +cartesian real and imaginary parts, but these can be read as polar +coordinates using the {\tt mag()} and {\tt ang()} functions. Addition, +subtraction, multiplication and division are supported. */ + +class EST_Complex { + private: + double r; + double i; +public: + /**@name Constructor functions */ + //@{ + /// default constructor, initialises values to 0.0 + EST_Complex() {r = 0.0; i = 0.0;} + /// Constructor initialising real and imaginary parts + EST_Complex(double real, double imag) + { r = real; i = imag;} + //@} + + /// Polar magnitude, read only + double mag() const + { return(sqrt(r*r+i*i)); } + + /// Polar angle, read only + double ang(int degrees=0) const { + double a; + if ( r == 0. && i == 0. ) a = 0.0; + else if ( r >= 0. ) a = atan(i/r); + else if ( i >= 0. ) a = atan(i/r) + PI; + else a = atan(i/r) - PI; + return (degrees == 1) ? (a * 180 / PI) : a; + } + + /// The real part - can be used for reading or writing + double &real() {return r;} + /// The imaginary part - can be used for reading or writing + double &imag() {return i;} + +friend EST_Complex operator + (const EST_Complex& z1, const EST_Complex &z2); +friend EST_Complex operator + (const EST_Complex &z, float x); +friend EST_Complex operator + (float x, const EST_Complex &z); +friend EST_Complex operator - (const EST_Complex &z1, const EST_Complex &z2); +friend EST_Complex operator - (const EST_Complex &z, float x); +friend EST_Complex operator - (float x, const EST_Complex &z); +friend EST_Complex operator * (const EST_Complex &z1, const EST_Complex &z2); +friend EST_Complex operator * (const EST_Complex &z, float x); +friend EST_Complex operator * (float x, const EST_Complex &z); +friend EST_Complex operator / (const EST_Complex &z1, const EST_Complex &z2); +friend EST_Complex operator / (const EST_Complex &z, float x); +friend EST_Complex operator / (float x, const EST_Complex &z); + + +friend ostream& operator<< (ostream& s, const EST_Complex& a) +{ s << a.r << " " << a.i; return s;} +}; + + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Contents.h b/aeneas/cfw/speech_tools/EST_Contents.h new file mode 100644 index 00000000..7df61a8d --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Contents.h @@ -0,0 +1,98 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : May 1996 */ +/*-----------------------------------------------------------------------*/ +/* A class for representing ints floats and strings */ +/* */ +/*=======================================================================*/ +#ifndef __EST_CONTENTS_H__ +#define __EST_CONTENTS_H__ + +/** A class for containing some other (arbitrary) class + Not general enough to call itself a run-time type system + Is designed to solve the problem of holding user + specified information. + Keeps reference count to know when to delete contents + + This is done on two levels EST_Contents and Contents_Data +*/ +class EST_Content_Data{ + private: + int refs; + void *data; + void (*free_func)(void *data); + public: + EST_Content_Data(void *d,void (*f)(void *d)) {free_func=f; data=d; refs=1;} + ~EST_Content_Data() { free_func(data); } + /// + int unref() { return --refs; } + /// + int ref() { return ++refs; } + /// + int the_refs() { return refs; } + void *contents() { return data; } + EST_Content_Data &operator=(const EST_Content_Data &c) + {refs = c.refs; data = c.data; free_func = c.free_func; return *this;} +}; + +/** More contents */ + +class EST_Contents{ +private: + EST_Content_Data *content_data; + void unref_contents(void) + { if ((content_data != 0) && + (content_data->unref() == 0)) + delete content_data;} +public: + EST_Contents() { content_data = 0; } + EST_Contents(void *p,void (*free_func)(void *p)) + { content_data = new EST_Content_Data(p,free_func); } + ~EST_Contents() { unref_contents(); } + void set_contents(void *p,void (*free_func)(void *p)) + { unref_contents(); content_data = new EST_Content_Data(p,free_func);} + void *get_contents() const + {return (content_data ? content_data->contents() : 0);} + /// + int refs() const { return ((content_data == 0) ? 0 : + content_data->the_refs());} + EST_Contents &operator=(const EST_Contents &c) + { unref_contents(); + content_data = c.content_data; + if (content_data != 0) content_data->ref(); + return *this; } +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_DMatrix.h b/aeneas/cfw/speech_tools/EST_DMatrix.h new file mode 100644 index 00000000..f07909c3 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_DMatrix.h @@ -0,0 +1,237 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* */ +/* Author : Simon King */ +/* Date : February 1999 */ +/* --------------------------------------------------------------------- */ +/* Double matrix class - copied from FMatrix ! */ +/* */ +/*************************************************************************/ + +#ifndef __DMatrix_H__ +#define __DMatrix_H__ + +#include "EST_TSimpleMatrix.h" +#include "EST_TSimpleVector.h" +#include "EST_FMatrix.h" + + +class EST_DVector; + +/** A matrix class for double precision floating point numbers. +EST_DMatrix x should be used instead of double **x wherever +possible.*/ +class EST_DMatrix : public EST_TSimpleMatrix { +private: +public: + /// size constructor + EST_DMatrix(int m, int n):EST_TSimpleMatrix(m, n) {} + /// copy constructor + EST_DMatrix(const EST_DMatrix &a):EST_TSimpleMatrix(a) {} + + static EST_String default_file_type; + /// CHECK - what does this do??? + EST_DMatrix(const EST_DMatrix &a, int b); + /// default constructor + EST_DMatrix():EST_TSimpleMatrix() {} + + /// Save in file (ascii or binary) + EST_write_status save(const EST_String &filename, + const EST_String &type = + EST_DMatrix::default_file_type); + /// Load from file (ascii or binary as defined in file) + EST_read_status load(const EST_String &filename); + /// Save in file in est format + EST_write_status est_save(const EST_String &filename, + const EST_String &type); + /// Load from file in est format (binary/ascii defined in file itself) + EST_read_status est_load(const EST_String &filename); + + /// Copy 2-d array {\tt x} of size {\tt rows x cols} into matrix. + void copyin(double **x, int rows, int cols); + + /// Add elements of 2 same sized matrices. + EST_DMatrix &operator+=(const EST_DMatrix &a); + + /// Subtract elements of 2 same sized matrices. + EST_DMatrix &operator-=(const EST_DMatrix &a); + + /// elementwise multiply by scalar + EST_DMatrix &operator*=(const double f); + + /// elementwise divide by scalar + EST_DMatrix &operator/=(const double f); + + /// Multiply all elements of matrix by {\tt x}. + friend EST_DMatrix operator*(const EST_DMatrix &a, const double x); + + /// Multiply matrix by vector. + friend EST_DVector operator*(const EST_DMatrix &a, const EST_DVector &v); + + /// Multiply vector by matrix + friend EST_DVector operator*(const EST_DVector &v,const EST_DMatrix &a); + + /// Multiply matrix by matrix. + friend EST_DMatrix operator*(const EST_DMatrix &a, const EST_DMatrix &b); +}; + + +/** A vector class for double precision floating point + numbers. {\tt EST_DVector x} should be used instead of + {\tt float *x} wherever possible. +*/ +class EST_DVector: public EST_TSimpleVector { +public: + /// Size constructor. + EST_DVector(int n): EST_TSimpleVector(n) {} + /// Copy constructor. + EST_DVector(const EST_DVector &a): EST_TSimpleVector(a) {} + /// Default constructor. + EST_DVector(): EST_TSimpleVector() {} + + /// elementwise multiply + EST_DVector &operator*=(const EST_DVector &s); + + /// elementwise add + EST_DVector &operator+=(const EST_DVector &s); + + /// elementwise multiply by scalar + EST_DVector &operator*=(const double d); + + /// elementwise divide by scalar + EST_DVector &operator/=(const double d); + + EST_write_status est_save(const EST_String &filename, + const EST_String &type); + + /// save vector to file filename. + EST_write_status save(const EST_String &filename, + const EST_String &type); + + /// load vector from file filename. + EST_read_status load(const EST_String &filename); + /// Load from file in est format (binary/ascii defined in file itself) + EST_read_status est_load(const EST_String &filename); +}; + +int square(const EST_DMatrix &a); +/// inverse +int inverse(const EST_DMatrix &a, EST_DMatrix &inv); +int inverse(const EST_DMatrix &a, EST_DMatrix &inv, int &singularity); +/// pseudo inverse (for non-square matrices) +int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv); +int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv,int &singularity); + +/// some useful matrix creators +/// make an identity matrix of dimension n +void eye(EST_DMatrix &a, const int n); +/// make already square matrix into I without resizing +void eye(EST_DMatrix &a); + +/// the user should use est_seed to seed the random number generator +void est_seed(); +void est_seed48(); +/// all elements are randomised +void make_random_vector(EST_DVector &M, const double scale); +/// all elements are randomised +void make_random_matrix(EST_DMatrix &M, const double scale); +/// used for variance +void make_random_diagonal_matrix(EST_DMatrix &M, const double scale); +/// used for covariance +void make_random_symmetric_matrix(EST_DMatrix &M, const double scale); + +void make_poly_basis_function(EST_DMatrix &T, EST_DVector t); + +/// elementwise add +EST_DVector add(const EST_DVector &a,const EST_DVector &b); +/// elementwise subtract +EST_DVector subtract(const EST_DVector &a,const EST_DVector &b); + +/// enforce symmetry +void symmetrize(EST_DMatrix &a); +/// stack columns on top of each other to make a vector +void stack_matrix(const EST_DMatrix &M, EST_DVector &v); +/// inplace diagonalise +void inplace_diagonalise(EST_DMatrix &a); + + +double determinant(const EST_DMatrix &a); +/// not implemented ?? +int singular(EST_DMatrix &a); +/// exchange rows and columns +void transpose(const EST_DMatrix &a,EST_DMatrix &b); +EST_DMatrix triangulate(const EST_DMatrix &a); + +/// extract leading diagonal as a matrix +EST_DMatrix diagonalise(const EST_DMatrix &a); +/// extract leading diagonal as a vector +EST_DVector diagonal(const EST_DMatrix &a); +/// sum of elements +double sum(const EST_DMatrix &a); +void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &c); +int floor_matrix(EST_DMatrix &M, const double floor); + +/// matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) +EST_DMatrix cov_prod(const EST_DVector &v1,const EST_DVector &v2); + +EST_DMatrix operator*(const EST_DMatrix &a, const EST_DMatrix &b); +EST_DMatrix operator-(const EST_DMatrix &a, const EST_DMatrix &b); +EST_DMatrix operator+(const EST_DMatrix &a, const EST_DMatrix &b); + +EST_DVector operator-(const EST_DVector &a, const EST_DVector &b); +EST_DVector operator+(const EST_DVector &a, const EST_DVector &b); + +EST_DMatrix sub(const EST_DMatrix &a, int row, int col); +EST_DMatrix DMatrix_abs(const EST_DMatrix &a); + +EST_DMatrix row(const EST_DMatrix &a, int row); +EST_DMatrix column(const EST_DMatrix &a, int col); + + +/// least squares fit +bool +polynomial_fit(EST_DVector &x, EST_DVector &y, EST_DVector &co_effs, int order); + +/// weighted least squares fit +bool +polynomial_fit(EST_DVector &x, EST_DVector &y, EST_DVector &co_effs, + EST_DVector &weights, int order); + +double +polynomial_value(const EST_DVector &coeffs, const double x); + +/// vector dot product +double operator*(const EST_DVector &v1, const EST_DVector &v2); + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Event.h b/aeneas/cfw/speech_tools/EST_Event.h new file mode 100644 index 00000000..625dae6f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Event.h @@ -0,0 +1,189 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : July 1995 */ +/*-----------------------------------------------------------------------*/ +/* Event class header file */ +/* */ +/*=======================================================================*/ + +// Warning: These event classes can be used as items in the EST_Stream +// int he normal way. However, the EventSI class has internal pointers to +// adjacent events which it uses to work out various parameters. Thus, +// when making a EST_Stream of events, it is important to link the events +// pointers as well. + +#ifndef __Event_H__ +#define __Event_H__ + +#include + +#ifndef FALSE +# define FALSE (0) +#endif +#ifndef TRUE +# define TRUE (1) +#endif + + +//class RFCelement{ +// float amp; +// float dur; +// float start_amp; +// float start_pos; +// EST_String type; +//}; + +class EventBase{ + private: + public: + EST_String type; + int save(EST_String filename, EST_String type = ""); + +}; + +class EventRFC: public EventBase{ +public: + void init() {rise_amp = 0.0; rise_dur = 0.0; + start_amp =0.0; fall_amp = 0.0; fall_dur = 0.0; + start_pos = 0.0; peak_pos = 0.0; type = ""; } + + float rise_amp; + float rise_dur; + float fall_amp; + float fall_dur; + float peak_pos; + float start_amp; + float start_pos; + friend ostream& operator << (ostream& s, EventRFC &e) + { + s << e.type << " " << e.rise_amp << " " << e.rise_dur + << " " << e.fall_amp << " " << e.fall_dur + << " " << e.start_amp << " " << e.start_pos + << endl; + return s; + } +}; + +class EventSI: public EventBase { +private: + float s_f0; + float s_pos; + float p_f0; + float p_pos; + float e_pos; + float e_f0; + +public: + void init(); + + float amp(); + float dur(); + + float rise_amp(); + float rise_dur(); + float fall_amp(); + float fall_dur(); + + float start_f0(); + float start_pos(); + float peak_f0(); + float peak_pos(); + float end_f0(); + float end_pos(); + + void set_start_f0(float a); + void set_start_pos(float a); + void set_peak_f0(float a); + void set_peak_pos(float a); + void set_end_f0(float a); + void set_end_pos(float a); + + EventSI *sn; + EventSI *sp; + + friend ostream& operator << (ostream& s, EventSI &e) + { + s << e.type << " ra:" << e.rise_amp() << " rd:" << e.rise_dur() + << "fa: " << e.fall_amp() << " fd:" << e.fall_dur() + << " sf0:" << e.start_f0() << " spos:" << e.start_pos() + << " pf0:" << e.peak_f0() << " ppos:" << e.peak_pos() + << " ef0:" << e.end_f0() << " epos:" << e.end_pos() + << endl; + return s; + } +}; + +class EventTilt: public EventBase{ +private: + float samp; + float sdur; + float stilt; + float spos; + float s_f0; + float s_pos; + +public: + void init(); + + float amp(); + float dur(); + float tilt(); + float pos(); + + void set_amp(float a); + void set_dur(float a); + void set_tilt(float a); + void set_pos(float a); + + float start_f0(); + float start_pos(); + void set_start_f0(float a); + void set_start_pos(float a); + + friend ostream& operator << (ostream& s, EventTilt &e) + { + s << e.type << " " << e.amp() << " " << e.dur() + << " " << e.tilt() << " " << e.pos() + << " sf0 " << e.start_f0() << " " << e.start_pos() + << endl; + return s; + } + +}; + +void gc_eventsi(void *w); +void gc_eventtilt(void *w); +void gc_eventrfc(void *w); + +#endif // __Event_H__ diff --git a/aeneas/cfw/speech_tools/EST_FMatrix.h b/aeneas/cfw/speech_tools/EST_FMatrix.h new file mode 100644 index 00000000..6848b2f7 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_FMatrix.h @@ -0,0 +1,246 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1996 */ + /* --------------------------------------------------------------------- */ + /* Matrix class */ + /* */ + /*************************************************************************/ + +#ifndef __FMatrix_H__ +#define __FMatrix_H__ + +#include "EST_TSimpleMatrix.h" +#include "EST_TSimpleVector.h" + +#include "EST_Val.h" +#include "EST_Val_defs.h" + +class EST_FVector; + +/** A matrix class for floating point numbers. EST_FMatrix x should be + used instead of float **x wherever possible. +*/ + +class EST_FMatrix : public EST_TSimpleMatrix { +private: +public: + /// size constructor + EST_FMatrix(int m, int n):EST_TSimpleMatrix(m, n) {} + /// copy constructor + EST_FMatrix(const EST_FMatrix &a):EST_TSimpleMatrix(a) {} + + static EST_String default_file_type; + /// CHECK - what does this do??? + EST_FMatrix(const EST_FMatrix &a, int b); + /// default constructor + EST_FMatrix():EST_TSimpleMatrix() {} + + /// Save in file (ascii or binary) + EST_write_status save(const EST_String &filename, + const EST_String &type = + EST_FMatrix::default_file_type ); + /// Load from file (ascii or binary as defined in file) + EST_read_status load(const EST_String &filename); + /// Save in file in est format + EST_write_status est_save(const EST_String &filename, + const EST_String &type); + /// Load from file in est format (binary/ascii defined in file itself) + EST_read_status est_load(const EST_String &filename); + + /// Copy 2-d array {\tt x} of size {\tt rows x cols} into matrix. + void copyin(float **x, int rows, int cols); + + /// Add elements of 2 same sized matrices. + EST_FMatrix &operator+=(const EST_FMatrix &a); + + /// Subtract elements of 2 same sized matrices. + EST_FMatrix &operator-=(const EST_FMatrix &a); + + /// elementwise multiply by scalar + EST_FMatrix &operator*=(const float f); + + /// elementwise divide by scalar + EST_FMatrix &operator/=(const float f); + + /// Multiply all elements of matrix by {\tt x}. + friend EST_FMatrix operator*(const EST_FMatrix &a, const float x); + + /// Multiply matrix by vector. + friend EST_FVector operator*(const EST_FMatrix &a, const EST_FVector &v); + + /// Multiply vector by matrix + friend EST_FVector operator*(const EST_FVector &v,const EST_FMatrix &a); + + /// Multiply matrix by matrix. + friend EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b); +}; + +/** A vector class for floating point numbers. + {\tt EST_FVector x} should be used instead of {\tt float *x} + wherever possible. +*/ +class EST_FVector: public EST_TSimpleVector { +public: + /// Size constructor. + EST_FVector(int n): EST_TSimpleVector(n) {} + /// Copy constructor. + EST_FVector(const EST_FVector &a): EST_TSimpleVector(a) {} + /// Default constructor. + EST_FVector(): EST_TSimpleVector() {} + + /// elementwise multiply + EST_FVector &operator*=(const EST_FVector &s); + + /// elementwise add + EST_FVector &operator+=(const EST_FVector &s); + + /// elementwise multiply by scalar + EST_FVector &operator*=(const float f); + + /// elementwise divide by scalar + EST_FVector &operator/=(const float f); + + EST_write_status est_save(const EST_String &filename, + const EST_String &type); + + /// save vector to file filename. + EST_write_status save(const EST_String &filename, + const EST_String &type); + + /// load vector from file filename. + EST_read_status load(const EST_String &filename); + /// Load from file in est format (binary/ascii defined in file itself) + EST_read_status est_load(const EST_String &filename); + +}; + +/// find largest element +float matrix_max(const EST_FMatrix &a); +/// find largest element +float vector_max(const EST_FVector &a); + +int square(const EST_FMatrix &a); +/// inverse +int inverse(const EST_FMatrix &a, EST_FMatrix &inv); +int inverse(const EST_FMatrix &a, EST_FMatrix &inv, int &singularity); +/// pseudo inverse (for non-square matrices) +int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv); +int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv,int &singularity); + +/// some useful matrix creators +/// make an identity matrix of dimension n +void eye(EST_FMatrix &a, const int n); +/// make already square matrix into I without resizing +void eye(EST_FMatrix &a); + +/// the user should use est_seed to seed the random number generator +void est_seed(); +void est_seed48(); +/// all elements are randomised +void make_random_vector(EST_FVector &M, const float scale); +/// all elements are randomised +void make_random_matrix(EST_FMatrix &M, const float scale); +/// used for variance +void make_random_diagonal_matrix(EST_FMatrix &M, const float scale); +/// used for covariance +void make_random_symmetric_matrix(EST_FMatrix &M, const float scale); + +void make_poly_basis_function(EST_FMatrix &T, EST_FVector t); + +/// elementwise add +EST_FVector add(const EST_FVector &a,const EST_FVector &b); +/// elementwise subtract +EST_FVector subtract(const EST_FVector &a,const EST_FVector &b); + +/// enforce symmetry +void symmetrize(EST_FMatrix &a); +/// stack columns on top of each other to make a vector +void stack_matrix(const EST_FMatrix &M, EST_FVector &v); +/// inplace diagonalise +void inplace_diagonalise(EST_FMatrix &a); + + +float determinant(const EST_FMatrix &a); +/// not implemented ?? +int singular(EST_FMatrix &a); +/// exchange rows and columns +void transpose(const EST_FMatrix &a,EST_FMatrix &b); +EST_FMatrix triangulate(const EST_FMatrix &a); + +/// extract leading diagonal as a matrix +EST_FMatrix diagonalise(const EST_FMatrix &a); +/// extract leading diagonal as a vector +EST_FVector diagonal(const EST_FMatrix &a); +/// sum of elements +float sum(const EST_FMatrix &a); +void multiply(const EST_FMatrix &a, const EST_FMatrix &b, EST_FMatrix &c); +int floor_matrix(EST_FMatrix &M, const float floor); + +/// matrix product of two vectors (#rows = length of first vector, #cols = length of second vector) +EST_FMatrix cov_prod(const EST_FVector &v1,const EST_FVector &v2); + +EST_FMatrix operator*(const EST_FMatrix &a, const EST_FMatrix &b); +EST_FMatrix operator-(const EST_FMatrix &a, const EST_FMatrix &b); +EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b); + +EST_FVector operator-(const EST_FVector &a, const EST_FVector &b); +EST_FVector operator+(const EST_FVector &a, const EST_FVector &b); + +EST_FMatrix sub(const EST_FMatrix &a, int row, int col); +EST_FMatrix fmatrix_abs(const EST_FMatrix &a); + +EST_FMatrix row(const EST_FMatrix &a, int row); +EST_FMatrix column(const EST_FMatrix &a, int col); + + +/// least squares fit +bool +polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, int order); + +/// weighted least squares fit +bool +polynomial_fit(EST_FVector &x, EST_FVector &y, EST_FVector &co_effs, + EST_FVector &weights, int order); + +float +polynomial_value(const EST_FVector &coeffs, const float x); + +/// vector dot product +float operator*(const EST_FVector &v1, const EST_FVector &v2); + +VAL_REGISTER_CLASS_DCLS(fmatrix,EST_FMatrix) +VAL_REGISTER_CLASS_DCLS(fvector,EST_FVector) + +#endif diff --git a/aeneas/cfw/speech_tools/EST_FeatureData.h b/aeneas/cfw/speech_tools/EST_FeatureData.h new file mode 100644 index 00000000..a7e9fe15 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_FeatureData.h @@ -0,0 +1,180 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* */ +/* Author : Paul Taylor */ +/* ------------------------------------------------------------------- */ +/* EST_Date Class header file */ +/* */ +/*************************************************************************/ + +#ifndef __EST_FeatureData_H__ +#define __EST_FeatureData_H__ + +#include "EST_Val.h" +#include "EST_TVector.h" +#include "EST_TList.h" +#include "EST_rw_status.h" +#include "EST_types.h" +#include "EST_Features.h" + + +typedef EST_TMatrix EST_ValMatrix; +typedef EST_TVector EST_ValVector; + +/** A class for containing arbitrary multi-dimensional feature data. + +A number of fields are defined in the EST_FeatureData class, each of +which represents a measurable quantity, such as height, age or +gender. Any number of fields can be defined and each field can take a +float, integer or string value. The class holds multiple instances of +field values, representing samples taken from a population. + +Several statistical training, testing and analysis programs use +EST_FeatureData as input and output. Member functions exist for +comparing fields, extracting given named or numbered fields, can +converting appropriate fields to a EST_FMatrix. + +*/ + + +class EST_FeatureSample : public EST_ValVector { +private: + bool p_sub_fd; + + void default_vals(); + void free_internals(); + void alloc_internals(); + + EST_Features *p_info; + EST_FeatureSample ©(const EST_FeatureSample &a); +public: + EST_FeatureSample(); + EST_FeatureSample(const EST_FeatureSample &a); + + /**@name Information functions */ + //@{ + + /** set number of samples to be held in object and allocate + space for storing them */ +/* + int num_fields() const {return info().num_fields();} + + //@} + + EST_Val &a(int field) + {return EST_ValVector::a(field);} + + const EST_Val &a(int field) const + {return EST_ValVector::a(field);} + + EST_Val &a(const EST_String &name) + {return EST_ValVector::a(info().field_index(name));} + + const EST_Val &a(const EST_String &name) const + {return EST_ValVector::a(info().field_index(name));} + + /// const element access operator +// const EST_Val &operator () (int sample, const EST_String &field); + /// non-const element access operator + EST_Val &operator () (const EST_String &field); + EST_Val &operator () (int field); + + EST_FeatureSample &EST_FeatureSample::operator= + (const EST_FeatureSample &f); + + friend ostream& operator << (ostream &st, const EST_FeatureSample &a); + //@} +*/ + + +}; + + +class EST_FeatureData{ +private: + bool p_sub_fd; + + void default_vals(); + void free_internals(); + void alloc_internals(); + + + EST_FeatureData ©(const EST_FeatureData &a); + + EST_Features info; + EST_ValMatrix fd; +public: + EST_FeatureData(); + EST_FeatureData(const EST_FeatureData &a); + ~EST_FeatureData(); + EST_Features &ginfo() {return info;} + + int num_samples() const; + int num_features() const; + + void resize(int num_samples, int num_columns, bool preserve = 1); + void resize(int num_samples, EST_Features &f, bool preserve = 1); + + void set_num_samples(int num_samples, bool preserve=1); + +/* void extract_features(EST_FeatureData &f, const EST_StrList &fields) const; + void extract_features(EST_FeatureData &f, EST_IList &fields) const; +*/ + + EST_String type(const EST_String &feature_name); + EST_StrList values(const EST_String &feature_name); + + void set_type(EST_String &feature_name, EST_String &type); + void set_values(EST_String &feature_name, EST_StrList &values); + + int update_values(const EST_String &feature_name, int max); + + int feature_position(const EST_String &feature_name); + + + EST_read_status load(const EST_String &name); + + EST_write_status save(const EST_String &name, + const EST_String &EST_filetype = "") const; + + EST_Val &a(int sample, int field); + EST_Val &a(int sample, const EST_String &name); + const EST_Val &a(int sample, int field) const; + const EST_Val &a(int sample, const EST_String &name) const; + + friend ostream& operator << (ostream &st,const EST_FeatureData &a); + +}; + + +#endif /* __EST_FeatureData_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_Featured.h b/aeneas/cfw/speech_tools/EST_Featured.h new file mode 100644 index 00000000..9acab41a --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Featured.h @@ -0,0 +1,162 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_FEATURED_H__ +#define __EST_FEATURED_H__ + + +#include "EST_Features.h" + +/** A class with the mechanisms needed to give an object features and + * access them nicely. Used as a parent class. + * + * @author Richard Caley + * @version $Id: EST_Featured.h,v 1.3 2004/05/04 00:00:16 awb Exp $ */ + +class EST_Featured { +private: + + EST_Features *p_features; + +protected: + + EST_Featured(void); + EST_Featured(const EST_Featured &f); + ~EST_Featured(void); + + void init_features(); + + void copy_features(const EST_Featured &f); + + void clear_features(); + + void ensure_features(void) + { if (p_features==NULL) p_features= new EST_Features; } + +public: + + int f_Int(const char *name, int def) const + { return p_features?p_features->I(name, def):def; } + int f_Int(const char *name) const + { return p_features?p_features->I(name):0; } + int f_I(const char *name, int def) const + {return f_Int(name, def);} + int f_I(const char *name) const + {return f_Int(name);} + void f_set(const EST_String name, int val) + { ensure_features(); p_features->set(name, val); } + void f_set_path(const EST_String name, int val) + { ensure_features(); p_features->set_path(name, val); } + + + float f_Float(const char *name, float def) const + { return p_features?p_features->F(name, def):def; } + float f_Float(const char *name) const + { return p_features?p_features->F(name):0.0; } + float f_F(const char *name, float def) const + {return f_Float(name, def);} + float f_F(const char *name) const + {return f_Float(name);} + void f_set(const EST_String name, float val) + { ensure_features(); p_features->set(name, val); } + void f_set_path(const EST_String name, float val) + { ensure_features(); p_features->set_path(name, val); } + + + EST_String f_String(const char *name, const EST_String &def) const + { return p_features?p_features->S(name, def):def; } + EST_String f_String(const char *name) const + { return p_features?p_features->S(name):EST_String::Empty; } + EST_String f_S(const char *name, const EST_String &def) const + {return f_String(name, def);} + EST_String f_S(const char *name) const + {return f_String(name);} + void f_set(const EST_String name, const char *val) + { ensure_features(); p_features->set(name, val); } + void f_set_path(const EST_String name, const char *val) + { ensure_features(); p_features->set_path(name, val); } + + + const EST_Val &f_Val(const char *name, const EST_Val &def) const; + const EST_Val &f_Val(const char *name) const; + + const EST_Val &f_V(const char *name, const EST_Val &def) const + {return f_Val(name, def);} + const EST_Val &f_V(const char *name) const + {return f_Val(name);} + void f_set_val(const EST_String name, EST_Val val) + { ensure_features(); p_features->set_val(name, val); } + void f_set_path(const EST_String name, EST_Val val) + { ensure_features(); p_features->set_path(name, val); } + + void f_set(const EST_Features &f) + { ensure_features(); *p_features = f; } + + int f_present(const EST_String name) const + {return p_features && p_features->present(name); } + + void f_remove(const EST_String name) + { if (p_features) p_features->remove(name); } + + // iteration + + protected: + struct IPointer_feat { EST_Features::RwEntries i; }; +// struct IPointer_feat { EST_TRwStructIterator< EST_Features, EST_Features::IPointer, EST_Features::Entry> i; }; + + void point_to_first(IPointer_feat &ip) const + { if (p_features) ip.i.begin(*p_features);} + void move_pointer_forwards(IPointer_feat &ip) const + { ++(ip.i); } + bool points_to_something(const IPointer_feat &ip) const + { return ip.i != 0; } + EST_TKVI &points_at(const IPointer_feat &ip) + { return *(ip.i); } + + friend class EST_TIterator< EST_Featured, IPointer_feat, EST_TKVI >; + friend class EST_TStructIterator< EST_Featured, IPointer_feat, EST_TKVI >; + friend class EST_TRwIterator< EST_Featured, IPointer_feat, EST_TKVI >; + friend class EST_TRwStructIterator< EST_Featured, IPointer_feat, EST_TKVI >; + +public: + typedef EST_TKVI FeatEntry; + typedef EST_TStructIterator< EST_Featured, IPointer_feat, FeatEntry> FeatEntries; + typedef EST_TRwStructIterator< EST_Featured, IPointer_feat, FeatEntry> RwFeatEntries; + + + +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_Features.h b/aeneas/cfw/speech_tools/EST_Features.h new file mode 100644 index 00000000..d720bfe6 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Features.h @@ -0,0 +1,330 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : March 1998 */ +/*-----------------------------------------------------------------------*/ +/* A class for feature value pairs */ +/*=======================================================================*/ +#ifndef __EST_FEATURES_H__ +#define __EST_FEATURES_H__ + +#include "EST_TKVL.h" +#include "EST_Val.h" +#include "EST_types.h" +#include "EST_TIterator.h" +#include "EST_error.h" + +class EST_TokenStream; +class EST_String; + +VAL_REGISTER_CLASS_DCLS(feats,EST_Features) + +// This shouldn't be here and only is for older code +typedef EST_Val (*EST_Item_featfunc)(class EST_Item *); +EST_Val est_val(const EST_Item_featfunc f); + + +/** A class for containing feature structures which can hold atomic +values (int, float, string) or other feature structures. +*/ + + +class EST_Features { + protected: + EST_TKVL *features; + + void save_fpair(ostream &outf, + const EST_String &fname, + const EST_Val &fvalue) const; + public: + static EST_Val feature_default_value; + EST_Features(); + EST_Features(const EST_Features &f); + ~EST_Features(); + + /**@name Access functions which return EST_Val. + Features can + either be simple features, in which their name is the name of + an plain attribute (e.g. "name"), or path features where their + name is a dot separated path of concatenated attributes + (e.g. "df.poa.alveolar"). + */ + //@{ + /** Look up directly without decomposing name as path (just simple feature) + */ + const EST_Val &val(const char *name) const; + + /** Look up directly without decomposing name as path (just simple feature), + returning def if not found + */ + const EST_Val &val(const char *name, const EST_Val &def) const; + + /** Look up feature name, which may be simple feature or path + */ + const EST_Val &val_path(const EST_String &path) const; + + /** Look up feature name, which may be simple feature or path, + returning def if not found + */ + const EST_Val &val_path(const EST_String &path, const EST_Val &def) const; + + /** Look up feature name, which may be simple feature or path. + */ + const EST_Val &operator() (const EST_String &path) const + {return val_path(path);} + + /** Look up feature name, which may be simple feature or path, + returning def if not found + */ + const EST_Val &operator() (const EST_String &path, const EST_Val &def) const + {return val_path(path, def);} + + /** Look up feature name, which may be simple feature or path. + */ + const EST_Val &f(const EST_String &path) const + { return val_path(path); } + + /** Look up feature name, which may be simple feature or path, + returning def if not found + */ + const EST_Val &f(const EST_String &path, const EST_Val &def) const + { return val_path(path,def); } + //@} + + /**@name Access functions which return types. + These functions cast + their EST_Val return value to a requested type, either float, + int, string or features (A). In all cases the name can be a + simple feature or a path, in which case their name is a dot + separated string of concatenated attributes + (e.g. "df.poa.alveolar"). */ + //@{ + + /** Look up feature name, which may be simple feature or path, and return + as a float */ + const float F(const EST_String &path) const + {return val_path(path).Float(); } + + /** Look up feature name, which may be simple feature or path, and + return as a float, returning def if not + found */ + const float F(const EST_String &path, float def) const + {return val_path(path, def).Float(); } + + /** Look up feature name, which may be simple feature or path, and return + as an int */ + const int I(const EST_String &path) const + {return val_path(path).Int(); } + + /** Look up feature name, which may be simple feature or path, and + return as an int, returning def if not + found */ + const int I(const EST_String &path, int def) const + {return val_path(path, def).Int(); } + + /** Look up feature name, which may be simple feature or path, and return + as a EST_String */ + const EST_String S(const EST_String &path) const + {return val_path(path).string(); } + + /** Look up feature name, which may be simple feature or path, and + return as a EST_String, returning def if not + found */ + + const EST_String S(const EST_String &path, const EST_String &def) const + {return val_path(path, def).string(); } + + /** Look up feature name, which may be simple feature or path, and return + as a EST_Features */ + EST_Features &A(const EST_String &path) const + {return *feats(val_path(path));} + + /** Look up feature name, which may be simple feature or path, and + return as a EST_Features, returning def if not + found */ + EST_Features &A(const EST_String &path, EST_Features &def) const; + + //@} + + /**@name Setting features + */ + //@{ + /** Add a new feature or set an existing feature name + to value ival + */ + void set(const EST_String &name, int ival) + { EST_Val pv(ival); set_path(name, pv);} + + /** Add a new feature or set an existing feature name + to value fval + */ + void set(const EST_String &name, float fval) + { EST_Val pv(fval); set_path(name, pv); } + + /** Add a new feature or set an existing feature name + to value dval + */ + void set(const EST_String &name, double dval) + { EST_Val pv((float)dval); set_path(name, pv); } + + /** Add a new feature or set an existing feature name + to value sval + */ + void set(const EST_String &name, const EST_String &sval) + { EST_Val pv(sval); set_path(name, pv); } + + /** Add a new feature or set an existing feature name + to value cval + */ + void set(const EST_String &name, const char *cval) + { EST_Val pv(cval); set_path(name, pv); } + + /** Add a new feature or set an existing feature name + to value val. Name must be + not be a path. + */ + void set_val(const EST_String &name, const EST_Val &sval) + { features->add_item(name,sval); } + + /** Add a new feature or set an existing feature name + to value val, where name + is a path. + */ + void set_path(const EST_String &name, const EST_Val &sval); + + /** Add a new feature feature or set an existing feature + name to value f, which + is the named of a registered feature function. + */ + void set_function(const EST_String &name, const EST_String &f); + + /** Add a new feature or set an existing feature + name to value f, + which itself is a EST_Features. The information in + f is copied into the features. */ + void set(const EST_String &name, EST_Features &f) + { EST_Features *ff = new EST_Features(f); + set_path(name, est_val(ff)); } + + //@} + + /**@name Utility functions + */ + + //@{ + /** remove the named feature */ + void remove(const EST_String &name) + { features->remove_item(name,1); } + + /** number of features in feature structure */ + int length() const { return features->length(); } + + /** return 1 if the feature is present */ + int present(const EST_String &name) const; + + /** Delete all features from object */ + void clear() { features->clear(); } + + /** Feature assignment */ + EST_Features& operator = (const EST_Features& a); + /** Print Features */ + friend ostream& operator << (ostream &s, const EST_Features &f) + { f.save(s); return s; } + //@} + + + // Iteration +#if 0 + EST_Litem *head() const { return features->list.head(); } + EST_String &fname(EST_Litem *p) const { return features->list(p).k; } + EST_Val &val(EST_Litem *p) const { return features->list(p).v; } + float F(EST_Litem *p) const { return features->list(p).v.Float(); } + EST_String S(EST_Litem *p) const { return features->list(p).v.string(); } + int I(EST_Litem *p) const { return features->list(p).v.Int(); } + EST_Features &A(EST_Litem *p) { return *feats(features->list(p).v); } +#endif + + + + protected: + struct IPointer { EST_TKVL::RwEntries i; }; + + void point_to_first(IPointer &ip) const + { ip.i.begin(*features);} + void move_pointer_forwards(IPointer &ip) const + { ++(ip.i); } + bool points_to_something(const IPointer &ip) const + { return ip.i != 0; } + EST_TKVI &points_at(const IPointer &ip) + { return *(ip.i); } + + friend class EST_TIterator< EST_Features, IPointer, EST_TKVI >; + friend class EST_TStructIterator< EST_Features, IPointer, EST_TKVI >; + friend class EST_TRwIterator< EST_Features, IPointer, EST_TKVI >; + friend class EST_TRwStructIterator< EST_Features, IPointer, EST_TKVI >; + +public: + + /**@name Iteration + */ + + //@{ + typedef EST_TKVI Entry; + typedef EST_TStructIterator< EST_Features, IPointer, Entry> Entries; + typedef EST_TRwStructIterator< EST_Features, IPointer, Entry> RwEntries; + //@} + + /**@name File I/O + */ + + //@{ + /// load features from already opened EST_TokenStream + EST_read_status load(EST_TokenStream &ts); + /// load features from sexpression, contained in already opened EST_TokenStream + EST_read_status load_sexpr(EST_TokenStream &ts); + /// save features in already opened ostream + EST_write_status save(ostream &outf) const; + /// save features as s-expression in already opened ostream + EST_write_status save_sexpr(ostream &outf) const; + + //@} +}; + +inline bool operator == (const EST_Features &a,const EST_Features &b) +{(void)a; (void)b; return false;} + +void merge_features(EST_Features &to,EST_Features &from); +EST_String error_name(const EST_Features &a); + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_FileType.h b/aeneas/cfw/speech_tools/EST_FileType.h new file mode 100644 index 00000000..7e1a9591 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_FileType.h @@ -0,0 +1,73 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : March 1998 */ +/*-----------------------------------------------------------------------*/ +/* File functions for EST type files */ +/* */ +/*=======================================================================*/ + +// This is the .h file for EST generic header type parsing. +// Only include this file in file i/o functions or else ott dependencies +// will crop in. + +#ifndef __EST_FILETYPE_H__ +#define __EST_FILETYPE_H__ + +#include "EST_Token.h" +#include "EST_Option.h" + +typedef enum EST_EstFileType { + est_file_none=0, + est_file_track, + est_file_wave, + est_file_label, + est_file_utterance, + est_file_fmatrix, + est_file_fvector, + est_file_dmatrix, + est_file_dvector, + est_file_feature_data, + est_file_fst, + est_file_ngram, + est_file_f_catalogue, + est_file_index, + est_file_unknown +} EST_EstFileType; + +EST_read_status read_est_header(EST_TokenStream &ts, EST_Option &hinfo, + bool &ascii, EST_EstFileType &t); + +#endif + + diff --git a/aeneas/cfw/speech_tools/EST_FringeServer.h b/aeneas/cfw/speech_tools/EST_FringeServer.h new file mode 100644 index 00000000..4dbc0b96 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_FringeServer.h @@ -0,0 +1,88 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_FRINGE_CLIENT_H__ +#define __EST_FRINGE_CLIENT_H__ + +#include "EST_String.h" +#include "EST_rw_status.h" +#include "EST_TKVL.h" +#include "EST_Server.h" + +/** A class providing mechanisms to talk to fringe. + * + * @author Richard Caley + * @version $Id: EST_FringeServer.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ +class EST_FringeServer : public EST_Server +{ +public: + + class ResultHandler : public EST_Server::ResultHandler + { + public: + ResultHandler(); + virtual void process(void); + virtual ~ResultHandler(); + }; + + /// Create a server connection by name, defaulting to "fringe", the default server name. + EST_FringeServer(EST_String name="fringe"); + + EST_FringeServer(EST_String name, ostream *trace); + + /// Create a server connection by explicitly saying where to connect to. + EST_FringeServer(EST_String hostname, int port); + EST_FringeServer(EST_String hostname, int port, ostream *trace); + + /// Destroy the connection. + ~EST_FringeServer(); + + virtual bool parse_command(const EST_String command, + EST_String &package, + EST_String &operation, + Args &arguments); + + virtual EST_String build_command(const EST_String package, + const EST_String operation, + const Args &arguments); + + virtual bool parse_result(const EST_String resultString, + Result &res); + + virtual EST_String build_result(const Result &res); +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_HMM.h b/aeneas/cfw/speech_tools/EST_HMM.h new file mode 100644 index 00000000..1a98d4fe --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_HMM.h @@ -0,0 +1,125 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : June 1996 */ +/*-----------------------------------------------------------------------*/ +/* HMM Class header file */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_HMM_H__ +#define __EST_HMM_H__ + +#include "EST_String.h" +#include "EST_FMatrix.h" +#include "EST_Token.h" + +class HMM_Mixture { +private: +public: + HMM_Mixture(); + HMM_Mixture(const HMM_Mixture &s); + HMM_Mixture(int n); + + void init(int vsize); + + EST_FVector mean; + EST_FVector var; + float gconst; + int vecsize; + HMM_Mixture& operator = (const HMM_Mixture& a); +}; + +ostream& operator<<(ostream& s, const HMM_Mixture &mix); + +class HMM_State { +private: +public: + HMM_State(); + HMM_State(const HMM_State &s); + HMM_State(int n); + + void init(int n_mixes, int vsize); + + EST_TVector mixture; + EST_FVector m_weight; + + HMM_State& operator = (const HMM_State& a); +}; + +ostream& operator<<(ostream& s, const HMM_State &st); + +class HMM { +private: +public: + HMM(); + HMM(int n, int v); + + void init(int n_states, int vsize, int n_streams); + + EST_String name; + + void clear(); + + EST_String covkind; + EST_String durkind; + EST_String sampkind; + + EST_TVector state; + EST_FMatrix trans; + + int num_streams; + int vecsize; + + EST_read_status load(EST_String file); + EST_read_status load_portion(EST_TokenStream &ts, int v_size, + int n_streams); + EST_write_status save(EST_String file); + HMM& operator = (const HMM& a); + + void balls(void); +}; + + + +ostream& operator<<(ostream& s, const HMM &model); + +int operator !=(const HMM_Mixture &a, const HMM_Mixture &b); +int operator !=(const HMM_State &a, const HMM_State &b); + + +typedef EST_TList EST_HMMList; + + + +#endif /* __EST_HMM_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_Handleable.h b/aeneas/cfw/speech_tools/EST_Handleable.h new file mode 100644 index 00000000..21707387 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Handleable.h @@ -0,0 +1,76 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_HANDLABLE_H__ +#define __EST_HANDLABLE_H__ + +/** Reference Counting Interface. + * + * This simple class does most of the things an object which is to be + * manipulated by EST_THandle style smart pointers needs to provide. + * + * @see EST_THandle + * @see EST_TBox + * @see EST_TrackMap + * @author Richard Caley + * @version $Id: EST_Handleable.h,v 1.4 2004/09/29 08:24:17 robert Exp $ + */ + +#include + +using namespace std; + +class EST_Handleable +{ +private: + int p_refcount; + +public: +# define NOT_REFCOUNTED (INT_MAX) + + EST_Handleable(void) { p_refcount=NOT_REFCOUNTED; } + + int refcount(void) const { return p_refcount;} + + void start_refcounting(int initial=0) {p_refcount=initial;} + void inc_refcount(void) {if (p_refcount!=NOT_REFCOUNTED) p_refcount++;} + void dec_refcount(void) {if (p_refcount!=NOT_REFCOUNTED) p_refcount--;} + + int is_unreferenced(void) const {return p_refcount == 0;} + int is_refcounted(void) const {return p_refcount!= NOT_REFCOUNTED;} + //@} +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_IMatrix.h b/aeneas/cfw/speech_tools/EST_IMatrix.h new file mode 100644 index 00000000..670c68e6 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_IMatrix.h @@ -0,0 +1,66 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1996 */ + /* --------------------------------------------------------------------- */ + /* Matrix class */ + /* */ + /*************************************************************************/ + +#ifndef __IMatrix_H__ +#define __IMatrix_H__ + +#include "EST_TSimpleMatrix.h" +#include "EST_TSimpleVector.h" + +/** A matrix class for integers. {\tt EST_IMatrix x} should be + used instead of {\tt int **x} wherever possible. +*/ +class EST_IMatrix: public EST_TSimpleMatrix { +private: +public: + /// size constructor + EST_IMatrix(int m, int n):EST_TSimpleMatrix(m, n) {} + /// copy constructor + EST_IMatrix(EST_IMatrix &a):EST_TSimpleMatrix(a) {} + /// CHECK - what does this do??? + EST_IMatrix(EST_IMatrix &a, int b); + /// default constructor + EST_IMatrix():EST_TSimpleMatrix() {} +}; + +/// find largest element +int matrix_max(const EST_IMatrix &a); + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Ngrammar.h b/aeneas/cfw/speech_tools/EST_Ngrammar.h new file mode 100644 index 00000000..7691e67f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Ngrammar.h @@ -0,0 +1,608 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King & Alan W Black */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A general class for ngrams (bi-gram, tri-gram etc) */ +/* */ +/*=======================================================================*/ +#ifndef __EST_NGRAMMAR_H__ +#define __EST_NGRAMMAR_H__ + +#include +#include + +using namespace std; + +#include "EST_String.h" +#include "EST_Val.h" +#include "EST_rw_status.h" +#include "EST_types.h" +#include "EST_FMatrix.h" +#include "EST_TList.h" +#include "EST_StringTrie.h" +#include "EST_simplestats.h" +#include "EST_PST.h" +#include "EST_string_aux.h" +#include "EST_math.h" + +// HTK style +#define SENTENCE_START_MARKER "!ENTER" +#define SENTENCE_END_MARKER "!EXIT" +#define OOV_MARKER "!OOV" + +#define EST_NGRAMBIN_MAGIC 1315402337 + +// for compressed save/load +#define GZIP_FILENAME_EXTENSION "gz" +#define COMPRESS_FILENAME_EXTENSION "Z" + +// Ultimate floor +#define TINY_FREQ 1.0e-10 + +// ngram state - represents the N-1 word history and contains +// the pdf of the next word + +class EST_NgrammarState { + +private: + +protected: + EST_DiscreteProbDistribution p_pdf; + int p_id; // a 'name' + +public: + EST_NgrammarState() : + + p_pdf() + + { + init(); + }; + EST_NgrammarState(int id,EST_Discrete *d){clear();init(id,d);}; + EST_NgrammarState(int id,const EST_DiscreteProbDistribution &pdf) + {clear();init(id,pdf);}; + EST_NgrammarState(const EST_NgrammarState &s); + EST_NgrammarState(const EST_NgrammarState *const s); + ~EST_NgrammarState(); + + EST_IVector path; // how we got here + + // initialise + void clear(); + void init(); + void init(int id, EST_Discrete *d); + void init(int id, const EST_DiscreteProbDistribution &pdf); + + // build + void cumulate(const int index, const double count=1) + {p_pdf.cumulate(index,count);}; + void cumulate(const EST_String &word, const double count=1) + {p_pdf.cumulate(word,count);}; + + // access + int id() const {return p_id; }; + const EST_DiscreteProbDistribution &pdf_const() const {return p_pdf; }; + EST_DiscreteProbDistribution &pdf() {return p_pdf; }; + double probability(const EST_String &w) const + {return p_pdf.probability(w);} + double probability(int w) const {return p_pdf.probability(w);} + double frequency(const EST_String &w) const + {return p_pdf.frequency(w);} + double frequency(int w) const {return p_pdf.frequency(w);} + const EST_String &most_probable(double *prob = NULL) const + {return p_pdf.most_probable(prob);} + +friend ostream& operator<<(ostream& s, const EST_NgrammarState &a); + +}; + +class EST_BackoffNgrammarState { + +private: + +protected: + int p_level; // = 0 for root node + double backoff_weight; + EST_DiscreteProbDistribution p_pdf; + EST_StringTrie children; + + EST_BackoffNgrammarState* add_child(const EST_Discrete *d, + const EST_StrVector &words); + EST_BackoffNgrammarState* add_child(const EST_Discrete *d, + const EST_IVector &words); +public: + EST_BackoffNgrammarState() + { init(); }; + EST_BackoffNgrammarState(const EST_Discrete *d,int level) + {clear();init(d,level);}; + EST_BackoffNgrammarState(const EST_DiscreteProbDistribution &pdf,int level) + {clear();init(pdf,level);}; + EST_BackoffNgrammarState(const EST_BackoffNgrammarState &s); + EST_BackoffNgrammarState(const EST_BackoffNgrammarState *const s); + ~EST_BackoffNgrammarState(); + + // initialise + void clear(); + void init(); + void init(const EST_Discrete *d, int level); + void init(const EST_DiscreteProbDistribution &pdf, int level); + + // build + bool accumulate(const EST_StrVector &words, + const double count=1); + bool accumulate(const EST_IVector &words, + const double count=1); + // access + const EST_DiscreteProbDistribution &pdf_const() const {return p_pdf; }; + EST_DiscreteProbDistribution &pdf() {return p_pdf; }; + double probability(const EST_String &w) const + {return p_pdf.probability(w);} + double frequency(const EST_String &w) const + {return p_pdf.frequency(w);} + const EST_String &most_probable(double *prob = NULL) const + {return p_pdf.most_probable(prob);} + + const int level() const {return p_level;} + + EST_BackoffNgrammarState* get_child(const EST_String &word) const + { + return (EST_BackoffNgrammarState*)children.lookup(word); + } + EST_BackoffNgrammarState* get_child(const int word) const + { + return (EST_BackoffNgrammarState*)children.lookup(p_pdf.get_discrete()->name(word)); + } + + void remove_child(EST_BackoffNgrammarState *child, + const EST_String &name); + + // recursive delete of contents and children + void zap(); + + const EST_BackoffNgrammarState *const get_state(const EST_StrVector &words) const; + + bool ngram_exists(const EST_StrVector &words, + const double threshold) const; + const double get_backoff_weight() const {return backoff_weight; } + const double get_backoff_weight(const EST_StrVector &words) const; + bool set_backoff_weight(const EST_StrVector &words, const double w); + void frequency_of_frequencies(EST_DVector &ff); + + void print_freqs(ostream &os,const int order,EST_String followers=""); + +friend ostream& operator<<(ostream& s, const EST_BackoffNgrammarState &a); + +}; + +class EST_Ngrammar { + +public: + + // 3 representations : sparse, dense and backed off. User specifies which. + enum representation_t {sparse, dense, backoff}; + + // now only keep frequencies (or log frequencies) + // probabilities (or log probabilities) can be done + // on the fly quickly enough + enum entry_t {frequencies, log_frequencies}; + +protected: + + // each instance of an EST_Ngrammar is a grammar of fixed order + // e.g. a bigram (order = 2) + int p_order; + int p_num_samples; + + double p_number_of_sentences; // which were used to build this grammar + + + EST_String p_sentence_start_marker; + EST_String p_sentence_end_marker; + + // only one representation in use at a time + representation_t p_representation; + entry_t p_entry_type; + + // sparse representation is a tree structure + // holding only those N-grams which were seen + EST_PredictionSuffixTree sparse_representation; + bool init_sparse_representation(); + + // dense representation is just an array of all states + bool init_dense_representation(); + + // backoff representation is also a tree structure + // but the root state pdf is the most recent word in the + // ngram and going down the tree is going back in time.... + // here is the root node : + EST_BackoffNgrammarState *backoff_representation; + + double backoff_threshold; + + // need a non-zero unigram floor to enable backing off + double backoff_unigram_floor_freq; + + // instead of simple discounting, we have a (possibly) different + // discount per order and per frequency + // e.g. backoff_discount[2](4) contains the discount to be + // applied to a trigram frequency of 4 + // backoff_discount[0] is unused (we don't discount unigrams) + EST_DVector *backoff_discount; + const double get_backoff_discount(const int order, const double freq) const; + + bool init_backoff_representation(); + void prune_backoff_representation(EST_BackoffNgrammarState *start_state=NULL); // remove any zero frequency branches + void backoff_restore_unigram_states(); + int p_num_states; // == p_vocab_size ^ (p_ord-1) if fully dense + EST_NgrammarState *p_states; // state id is index into this array + int find_dense_state_index(const EST_IVector &words, int index=0) const; + + // and the reverse + const EST_StrVector &make_ngram_from_index(const int i) const; + + // vocabulary + EST_Discrete *vocab; + EST_Discrete *pred_vocab; // may be different from state vocab + bool init_vocab(const EST_StrList &wordlist); + bool init_vocab(const EST_StrList &word_list, + const EST_StrList &pred_list); + + // make sure vocab matches a given wordlist + bool check_vocab(const EST_StrList &wordlist); + + EST_DiscreteProbDistribution vocab_pdf; // overall pdf + + const EST_String &lastword(const EST_StrVector &words) const + { return words(p_order-1); } + const int lastword(const EST_IVector &words) const + { return words(p_order-1); } + // are we allowing out-of-vocabulary words, or is the vocabulary closed? + bool allow_oov; + + bool sparse_to_dense(); + bool dense_to_sparse(); + + // these aren't sorted yet ... + void take_logs(); + void take_exps(); + void freqs_to_probs(); // just calls normalise + + bool build_sparse(const EST_String &filename, + const EST_String &prev, + const EST_String &prev_prev, + const EST_String &last); + // for dense and backoff + bool build_ngram(const EST_String &filename, + const EST_String &prev, + const EST_String &prev_prev, + const EST_String &last, + const EST_String &input_format); + + // go through all matching ngrams ( *(ngram[i])="" matches anything ) + void iterate(EST_StrVector &words, + void (*function)(EST_Ngrammar *n, + EST_StrVector &words, + void *params), + void *params); + + // same, but with a constant Ngrammar + void const_iterate(EST_StrVector &words, + void (*function)(const EST_Ngrammar *const n, + EST_StrVector &words, + void *params), + void *params) const; + + bool p_init(int o, representation_t r); + + // new filename returned of we had to copy stdin to a + // temporary file - must delete it later ! + bool oov_preprocess(const EST_String &filename, + EST_String &new_filename, + const EST_String &what); + + + const EST_NgrammarState &find_state_const(const EST_StrVector &words)const; + EST_NgrammarState &find_state(const EST_StrVector &words); + const EST_NgrammarState &find_state_const(const EST_IVector &words) const; + EST_NgrammarState &find_state(const EST_IVector &words); + + // special versions for backoff grammars + const EST_DiscreteProbDistribution &backoff_prob_dist(const EST_StrVector &words) const; + const double backoff_reverse_probability_sub(const EST_StrVector &words, + const EST_BackoffNgrammarState *root) const; + const double backoff_probability(const EST_StrVector &words, + const bool trace=false) const; + const double backoff_reverse_probability(const EST_StrVector &words) const; + const EST_String & backoff_most_probable(const EST_StrVector &words, + double *prob = NULL) const; + + // backoff representation isn't a nice array of states + // so use this to visit every node in the tree + // and apply the function to that node + void backoff_traverse(EST_BackoffNgrammarState *start_state, + void (*function)(EST_BackoffNgrammarState *s, + void *params), + void *params); + + // visit every node at a given level + void backoff_traverse(EST_BackoffNgrammarState *start_state, + void (*function)(EST_BackoffNgrammarState *s, + void *params), + void *params, const int level); +public: + + EST_Ngrammar() {default_values();} + + EST_Ngrammar(int o, representation_t r, + const EST_StrList &wordlist) + { + default_values(); init(o,r,wordlist); + } + + // When state trans vocab differs from prediction vocab + EST_Ngrammar(int o, representation_t r, + const EST_StrList &wordlist, + const EST_StrList &predlist) + { + default_values(); init(o,r,wordlist,predlist); + } + + EST_Ngrammar(int o, representation_t r, EST_Discrete &v) + { + default_values(); init(o,r,v); + } + ~EST_Ngrammar(); + + void default_values(); + void clear(); + bool init(int o, representation_t r, + const EST_StrList &wordlist); + bool init(int o, representation_t r, + const EST_StrList &wordlist, + const EST_StrList &predlist); + bool init(int o, representation_t r, EST_Discrete &v); + bool init(int o, representation_t r, + EST_Discrete &v,EST_Discrete &pv); + + // access + int num_states(void) const { return p_num_states;} + double samples(void) const { return p_num_samples;} + int order() const { return p_order; } + int get_vocab_length() const { return vocab?vocab->length():0; } + EST_String get_vocab_word(int i) const; + int get_vocab_word(const EST_String &s) const; + int get_pred_vocab_length() const { return pred_vocab->length(); } + EST_String get_pred_vocab_word(int i) const { return pred_vocab->name(i); } + int get_pred_vocab_word(const EST_String &s) const + { return pred_vocab->name(s); } + int closed_vocab() const {return !allow_oov; } + entry_t entry_type() const {return p_entry_type;} + representation_t representation() const + { return p_representation;} + + // build + bool build(const EST_StrList &filenames, + const EST_String &prev = SENTENCE_START_MARKER, + const EST_String &prev_prev = SENTENCE_END_MARKER, + const EST_String &last = SENTENCE_END_MARKER, + const EST_String &input_format = "", + const EST_String &oov_mode = "", + const int mincount=1, + const int maxcount=10); + + // Accumulate ngrams + void accumulate(const EST_StrVector &words, + const double count=1); + //const int index=0); + void accumulate(const EST_IVector &words, + const double count=1); + //const int index=0); + + // hack - fix enter/exit probs s.t. P(...,!ENTER)=P(!EXIT,...)=0, for all x + void make_htk_compatible(); + + // I/O functions + EST_read_status load(const EST_String &filename); + EST_read_status load(const EST_String &filename, const EST_StrList &wordlist); + EST_write_status save(const EST_String &filename, + const EST_String type="cstr_ascii", + const bool trace=false, + double floor=0.0); + + int wordlist_index(const EST_String &word, const bool report=true) const; + const EST_String &wordlist_index(int i) const; + int predlist_index(const EST_String &word) const; + const EST_String &predlist_index(int i) const; + + // set + bool set_entry_type(entry_t new_type); + bool set_representation(representation_t new_representation); + + // probability distributions + // ------------------------- + // flag 'force' forces computation of probs on-the-fly if necessary + double probability(const EST_StrVector &words, bool force=false, + const bool trace=false) const; + double frequency(const EST_StrVector &words, bool force=false, + const bool trace=false) const; + + const EST_String &predict(const EST_StrVector &words, + double *prob,int *state) const; + const EST_String &predict(const EST_StrVector &words) const + {double p; int state; return predict(words,&p,&state); } + const EST_String &predict(const EST_StrVector &words,double *prob) const + {int state; return predict(words,prob,&state); } + + const EST_String &predict(const EST_IVector &words,double *prob,int *state) const; + const EST_String &predict(const EST_IVector &words) const + {double p; int state; return predict(words,&p,&state); } + const EST_String &predict(const EST_IVector &words,double *prob) const + {int state; return predict(words,prob,&state); } + + int find_state_id(const EST_StrVector &words) const; + int find_state_id(const EST_IVector &words) const; + int find_next_state_id(int state, int word) const; + // fast versions for common N + //const double probability(const EST_String w1); + //const double probability(const EST_String w1,const EST_String w2); + //const double probability(const EST_String w1,const EST_String w2, + //const EST_String w2); + + // reverse - probability of words[0..order-2] given word[order-1] + double reverse_probability(const EST_StrVector &words, + bool force=false) const; + double reverse_probability(const EST_IVector &words, + bool force=false) const; + + // predict, where words has 'order' elements and the last one is "" or NULL + const EST_DiscreteProbDistribution &prob_dist(const EST_StrVector &words) const; + const EST_DiscreteProbDistribution &prob_dist(const EST_IVector &words) const; + const EST_DiscreteProbDistribution &prob_dist(int state) const; + +// bool stats(const EST_String filename, +// double &raw_entropy, double &count, +// double &entropy, double &perplexity, +// const EST_String &prev = SENTENCE_START_MARKER, +// const EST_String &prev_prev = SENTENCE_END_MARKER, +// const EST_String &last = SENTENCE_END_MARKER, +// const EST_String &input_format = "") const; + + void fill_window_start(EST_IVector &window, + const EST_String &prev, + const EST_String &prev_prev) const; + + void fill_window_start(EST_StrVector &window, + const EST_String &prev, + const EST_String &prev_prev) const; + + // why anybody would want to do this .... + //EST_Ngrammar &operator =(const EST_Ngrammar &a); + + bool ngram_exists(const EST_StrVector &words) const; + bool ngram_exists(const EST_StrVector &words, const double threshold) const; + const double get_backoff_weight(const EST_StrVector &words) const; + bool set_backoff_weight(const EST_StrVector &words, const double w); + + void print_freqs(ostream &os,double floor=0.0); + + // i/o functions + // ------------- + friend ostream& operator<<(ostream& s, EST_Ngrammar &n); + friend EST_read_status load_ngram_htk_ascii(const EST_String filename, + EST_Ngrammar &n); + friend EST_read_status load_ngram_htk_binary(const EST_String filename, + EST_Ngrammar &n); + friend EST_read_status load_ngram_arpa(const EST_String filename, + EST_Ngrammar &n, + const EST_StrList &vocab); + friend EST_read_status load_ngram_cstr_ascii(const EST_String filename, + EST_Ngrammar &n); + friend EST_read_status load_ngram_cstr_bin(const EST_String filename, + EST_Ngrammar &n); + + friend EST_write_status save_ngram_htk_ascii_sub(const EST_String &word, + ostream *ost, + EST_Ngrammar &n, + double floor); + friend EST_write_status save_ngram_htk_ascii(const EST_String filename, + EST_Ngrammar &n, + double floor); + + //friend EST_write_status save_ngram_htk_binary(const EST_String filename, + // EST_Ngrammar &n); + friend EST_write_status save_ngram_cstr_ascii(const EST_String filename, + EST_Ngrammar &n, + const bool trace, + double floor); + friend EST_write_status save_ngram_cstr_bin(const EST_String filename, + EST_Ngrammar &n, + const bool trace, + double floor); + friend EST_write_status save_ngram_arpa(const EST_String filename, + EST_Ngrammar &n); + friend EST_write_status save_ngram_arpa_sub(ostream *ost, + EST_Ngrammar &n, + const EST_StrVector &words); + friend EST_write_status save_ngram_wfst(const EST_String filename, + EST_Ngrammar &n); + + // Auxiliary functions + + // smoothing +friend void frequency_of_frequencies(EST_DVector &ff, EST_Ngrammar &n,int this_order); +friend void map_frequencies(EST_Ngrammar &n, const EST_DVector &map, const int this_order); +friend bool Good_Turing_smooth(EST_Ngrammar &n, int maxcount, int mincount); +friend void Good_Turing_discount(EST_Ngrammar &ngrammar, const int maxcount, + const double default_discount); + +friend void fs_build_backoff_ngrams(EST_Ngrammar *backoff_ngrams, + EST_Ngrammar &ngram); +friend int fs_backoff_smooth(EST_Ngrammar *backoff_ngrams, + EST_Ngrammar &ngram, int smooth_thresh); + + // frequencies below mincount get backed off + // frequencies above maxcount are not smoothed(discounted) + bool compute_backoff_weights(const int mincount=1, + const int maxcount=10); + + + bool merge(EST_Ngrammar &n,float weight); + +friend class EST_BackoffNgrammar; + +}; + +void Ngram_freqsmooth(EST_Ngrammar &ngram, + int smooth_thresh1, + int smooth_thresh2); + +// utils +void slide(EST_IVector &i, const int l); +void slide(EST_StrVector &i, const int l); + +bool test_stats(EST_Ngrammar &ngram, + const EST_String &filename, + double &raw_entropy, + double &count, + double &entropy, + double &perplexity, + const EST_String &input_format, + const EST_String &prev = SENTENCE_START_MARKER, + const EST_String &prev_prev = SENTENCE_END_MARKER, + const EST_String &last = SENTENCE_END_MARKER); + +VAL_REGISTER_CLASS_DCLS(ngrammar,EST_Ngrammar) + +#endif // __EST_NGRAMMAR_H__ diff --git a/aeneas/cfw/speech_tools/EST_Option.h b/aeneas/cfw/speech_tools/EST_Option.h new file mode 100644 index 00000000..a5e54020 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Option.h @@ -0,0 +1,105 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : September 1994 */ +/*-----------------------------------------------------------------------*/ +/* EST_Option Class header file */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_OPTION_H__ +#define __EST_OPTION_H__ + +#include "EST_String.h" +#include "EST_TKVL.h" +#include "EST_rw_status.h" + +/** Provide a high level interface for String String key value lists. +*/ + +class EST_Option: public EST_TKVL { +public: + /// add prefix to every key + void add_prefix(EST_String prefix); + /// remove prefix from every key + void remove_prefix(EST_String prefix); + + /** read keyval list from file. The file type is an ascii file + with each line representing one key value pair. The first entry in + the line defines the key, and the rest, which may contain + whitespaces, defins the value. Lines starting with the comment + character are ignored. + @return returns EST_read_status errors, @see */ + EST_read_status load(const EST_String &filename, const EST_String &comment = ";"); + + /// add to end of list or overwrite. If rval is empty, do nothing + int override_val(const EST_String rkey, const EST_String rval); + /// add to end of list or overwrite. If rval is empty, do nothing + int override_fval(const EST_String rkey, const float rval); + /// add to end of list or overwrite. If rval is empty, do nothing + int override_ival(const EST_String rkey, const int rval); + + /** return value of type int relating to key. By default, + an error occurs if the key is not present. Use m=0 if + to get a dummy value returned if key is not present */ + int ival(const EST_String &rkey, int m=1) const; + + /** return value of type float relating to key. By default, + an error occurs if the key is not present. Use m=0 if + to get a dummy value returned if key is not present */ + double dval(const EST_String &rkey, int m=1) const; + + /** return value of type float relating to key. By default, + an error occurs if the key is not present. Use m=0 if + to get a dummy value returned if key is not present */ + float fval(const EST_String &rkey, int m=1) const; + + /** return value of type String relating to key. By default, + an error occurs if the key is not present. Use m=0 if + to get a dummy value returned if key is not present */ + const EST_String &sval(const EST_String &rkey, int m=1) const; + + /** return value of type String relating to key. By default, + an error occurs if the key is not present. Use m=0 if + to get a dummy value returned if key is not present */ +// const EST_String &val(const EST_String &rkey, int m=1) const +// { return sval(rkey,m); } + + int add_iitem(const EST_String &rkey, const int &rval); + int add_fitem(const EST_String &rkey, const float &rval); + + /// print options + friend ostream& operator << (ostream& s, const EST_Option &kv); +}; + +#endif // __EST_OPTION_H__ diff --git a/aeneas/cfw/speech_tools/EST_PST.h b/aeneas/cfw/speech_tools/EST_PST.h new file mode 100644 index 00000000..67ac2c0f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_PST.h @@ -0,0 +1,153 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A general class for PredictionSuffixTrees */ +/* */ +/*=======================================================================*/ + +#ifndef __PredictionSuffixTree_H__ +#define __PredictionSuffixTree_H__ + +#include "EST_simplestats.h" +#include "EST_types.h" +#include "EST_Features.h" + +class EST_PredictionSuffixTree_tree_node { +private: + +protected: + + int p_level; + int state; + EST_DiscreteProbDistribution pd; + EST_String path; /* context */ + void delete_node(void *n) { if (n != 0) delete (EST_PredictionSuffixTree_tree_node *)n;} + +public: + +// EST_StringTrie nodes; +// EST_TKVL nodes; + EST_Features nodes; + EST_PredictionSuffixTree_tree_node() {p_level=0;} + ~EST_PredictionSuffixTree_tree_node(); + void clear(void); + const EST_String &get_path(void) const {return path;} + void set_path(const EST_String &s) {path=s;} + void set_level(int l) {p_level=l;} + void set_state(int s) {state=s;} + int get_state(void) const {return state;} + int get_level(void) const {return p_level;} + void cumulate(const EST_String &s,double count=1) {pd.cumulate(s,count);} + void cumulate(const int i,double count=1) {pd.cumulate(i,count);} + const EST_String &most_probable(double *p) const; + const EST_DiscreteProbDistribution &prob_dist() const {return pd;} + void print_freqs(ostream &os); + void print_probs(ostream &os); +}; + +VAL_REGISTER_CLASS_DCLS(pstnode,EST_PredictionSuffixTree_tree_node) + +class EST_PredictionSuffixTree { + +private: + + enum EST_filetype {PredictionSuffixTree_ascii, PredictionSuffixTree_binary}; + +protected: + + int p_order; + int num_states; + EST_PredictionSuffixTree_tree_node *nodes; + EST_DiscreteProbDistribution *pd; // distribution of predictees + const EST_String &ppredict(EST_PredictionSuffixTree_tree_node *node, + const EST_StrVector &words, + double *prob, int *state, + const int index=0) const; + + void p_accumulate(EST_PredictionSuffixTree_tree_node *node, + const EST_StrVector &words, + double count, + const int index=0); + + const EST_DiscreteProbDistribution &p_prob_dist( + EST_PredictionSuffixTree_tree_node *node, + const EST_StrVector &words, + const int index=0) const; +public: + EST_PredictionSuffixTree(); + EST_PredictionSuffixTree(const int order) {init(order);} + EST_PredictionSuffixTree(const EST_String filename); + EST_PredictionSuffixTree(const EST_TList &vocab,int order=2); + ~EST_PredictionSuffixTree(); + void clear(void); + void init(const int order); + double samples() const { return pd->samples(); } + int states() const { return num_states; } + int order(void) const {return p_order;} + void accumulate(const EST_StrVector &words,const double count=1,const int index=0); + + int load(const EST_String filename); + int save(const EST_String filename,const EST_PredictionSuffixTree::EST_filetype type=PredictionSuffixTree_ascii); + + // build EST_PredictionSuffixTree from train data + void build(const EST_String filename, + const EST_String prev, + const EST_String prev_prev, + const EST_String last); + + void build(const EST_StrList &input); // to go + + void test(const EST_String filename); // test EST_PredictionSuffixTree against test data + void print_freqs(ostream &os); + void print_probs(ostream &os); + + const EST_String &predict(const EST_StrVector &words) const; + const EST_String &predict(const EST_StrVector &words,double *prob) const; + const EST_String &predict(const EST_StrVector &words,double *prob,int *state) const; + const EST_DiscreteProbDistribution &prob_dist(const EST_StrVector &words) + const + {return p_prob_dist(nodes,words);} + /* Reverse probability, given X what is prob of EST_PredictionSuffixTree Y */ + double rev_prob(const EST_StrVector &words) const; + double rev_prob(const EST_StrVector &words, + const EST_DiscreteProbDistribution &pd) const; + /* print frequency or probability models */ + /* build model from file */ + /* predict and measure success */ + +}; + +#endif // __PredictionSuffixTree_H__ diff --git a/aeneas/cfw/speech_tools/EST_Pathname.h b/aeneas/cfw/speech_tools/EST_Pathname.h new file mode 100644 index 00000000..b8211bac --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Pathname.h @@ -0,0 +1,108 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +#ifndef __EST_PATHNAME_H__ +#define __EST_PATHNAME_H__ + +#include "EST_String.h" +#include "EST_TList.h" + +/** Class representing pathnames. Makes common filename manipulations + * available as methods. Different implementations are provided for + * different systems. + */ +class EST_Pathname : public EST_String { + +private: + void setup(void); + +public: + + EST_Pathname(void) : EST_String("") { }; + EST_Pathname(EST_String s) : EST_String(s) { this->setup(); }; + EST_Pathname(const char *s) : EST_String(s) { this->setup(); }; + + static EST_Pathname construct(EST_Pathname dir, EST_String basename, EST_String extension); + static EST_Pathname construct(EST_Pathname dir, EST_String filename); + + // component parts of a filename + EST_Pathname directory(void) const; + EST_Pathname filename(void) const; + EST_String basename(int remove_all=0) const; + EST_String extension(void) const; + + EST_Pathname as_file(void) const; + EST_Pathname as_directory(void) const; + + int is_absolute(void) const; + inline int is_relative(void) const {return !is_absolute();}; + int is_dirname(void) const; + inline int is_filename(void) const {return !is_dirname(); }; + + EST_TList entries(int check_for_directories = 1) const; + + static EST_Pathname append(EST_Pathname directory, EST_Pathname addition); + + static void divide(EST_Pathname path, int at, EST_Pathname &start, EST_Pathname &end); + + friend EST_Pathname operator + (const EST_Pathname p, const EST_Pathname addition); + friend EST_Pathname operator + (const char *p, const EST_Pathname addition); + + // solve an ambiguity + EST_Pathname &operator += (const char * addition) + { return (*this) = append(*this, addition); } + EST_Pathname &operator += (const EST_String addition) + { return (*this) = append(*this, addition); } + EST_Pathname &operator += (const EST_Pathname addition) + { return (*this) = append(*this, addition); } + + EST_Pathname operator + (const EST_String addition) + { return append(*this, EST_Pathname(addition)); } + EST_Pathname operator + (const char *addition) + { return append(*this, EST_Pathname(addition)); } + + int operator == (const EST_String thing) + { return EST_String(*this) == thing; } + int operator == (const char * thing) + { return EST_String(*this) == EST_String(thing); } + int operator != (const EST_String thing) + { return EST_String(*this) != thing; } + int operator != (const char * thing) + { return EST_String(*this) != EST_String(thing); } + +}; + +EST_Pathname operator + (const EST_Pathname p, const EST_Pathname addition); +EST_Pathname operator + (const char *p, const EST_Pathname addition); + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Regex.h b/aeneas/cfw/speech_tools/EST_Regex.h new file mode 100644 index 00000000..f649af1d --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Regex.h @@ -0,0 +1,151 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1997 */ + /* All Rights Reserved. */ + /* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +#ifndef __EST_REGEX_H__ +#define __EST_REGEX_H__ + +class EST_Regex; + +#include "EST_String.h" + +/** A Regular expression class to go with the CSTR EST_String class. + * + * The regular expression syntax is the FSF syntax used in emacs and + * in the FSF String library. This is translated into the syntax supported + * by Henry Spensor's regular expression library, this translation is a place + * to look if you find regular expressions not matching where expected. + * + * @see EST_String + * @see string_example + * @author Richard Caley + * @author (regular expression library by Henry Spencer, University of Toronto) + * @version $Id: EST_Regex.h,v 1.3 2004/05/04 00:00:16 awb Exp $ + */ + +class EST_Regex : protected EST_String { + +private: + /// The compiled form. + void *compiled; + /// Compiled form for whole string match. + void *compiled_match; + +protected: + /// Compile expression. + void compile(); + /// Compile expression in a form which only matches whole string. + void compile_match(); + /// Translate the expression into the internally used syntax. + char *regularize(int match) const; + +public: + /// Empty constructor, just for form. + EST_Regex(void); + + /// Construct from EST_String. + EST_Regex(EST_String s); + + /// Construct from C string. + EST_Regex(const char *ex); + + /// Copy constructor. + EST_Regex(const EST_Regex &ex); + + /// Destructor. + ~EST_Regex(); + + /// Size of the expression. + int size() const { return EST_String::size; }; + + /// Run to find a matching substring + int run(const char *on, int from, int &start, int &end, int *starts=NULL, int *ends=NULL); + /// Run to see if it matches the entire string. + int run_match(const char *on, int from=0, int *starts=NULL, int *ends=NULL); + + /// Get the expression as a string. + EST_String tostring(void) const {return (*this);}; + + /// Cast operator, disambiguates it for some compilers + operator const char *() const { return (const char *)tostring(); } + + int operator == (const EST_Regex ex) const + { return (const EST_String)*this == (const EST_String)ex; } + + int operator != (const EST_Regex ex) const + { return (const EST_String)*this != (const EST_String)ex; } + + /**@name Assignment */ + //@{ + /// + EST_Regex &operator = (const EST_Regex ex); + /// + EST_Regex &operator = (const EST_String s); + /// + EST_Regex &operator = (const char *s); + //@} + + /// Stream output of regular expression. + friend ostream &operator << (ostream &s, const EST_Regex &str); +}; + +ostream &operator << (ostream &s, const EST_Regex &str); + +/**@name Predefined_regular_expressions + * Some regular expressions matching common things are predefined + */ +//@{ +/// White space +extern EST_Regex RXwhite; // "[ \n\t\r]+" +/// Sequence of alphabetic characters. +extern EST_Regex RXalpha; // "[A-Za-z]+" +/// Sequence of lower case alphabetic characters. +extern EST_Regex RXlowercase; // "[a-z]+" +/// Sequence of upper case alphabetic characters. +extern EST_Regex RXuppercase; // "[A-Z]+" +/// Sequence of letters and/or digits. +extern EST_Regex RXalphanum; // "[0-9A-Za-z]+" +/// Initial letter or underscore followed by letters underscores or digits. +extern EST_Regex RXidentifier; // "[A-Za-z_][0-9A-Za-z_]+" +/// Integer. +extern EST_Regex RXint; // "-?[0-9]+" +/// Floating point number. +extern EST_Regex RXdouble; // "-?\\(\\([0-9]+\\.[0-9]*\\)\\|\\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\)\\([eE][---+]?[0-9]+\\)?" +//@} + +// GCC lets us use the static constant to declare arrays, Sun CC +// doesn't, so for a quiet, if ugly, life we declare it here with a suitable +// value and check in EST_Regex.cc to make sure it`s OK + +#define EST_Regex_max_subexpressions 10 + +#endif diff --git a/aeneas/cfw/speech_tools/EST_SCFG.h b/aeneas/cfw/speech_tools/EST_SCFG.h new file mode 100644 index 00000000..76547721 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_SCFG.h @@ -0,0 +1,352 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : October 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Stochastic context free grammars */ +/* */ +/*=======================================================================*/ +#ifndef __EST_SCFG_H__ +#define __EST_SCFG_H__ + +#include "EST_simplestats.h" +#include "EST_rw_status.h" +#include "EST_TList.h" +#include "siod.h" + +/** This class represents a bracketed string used in training of SCFGs. + + An object in this class builds an index of valid bracketing of + the string, thus offering both a tree like access and direct + access to the leafs of the tree. The definition of ``valid + bracketing'' is any substring \[ W_{i,j} \] that doesn't cross any + brackets. +*/ +class EST_bracketed_string { + private: + int p_length; + LISP *symbols; + LISP bs; + int **valid_spans; // triangular matrix + int find_num_nodes(LISP string); + int set_leaf_indices(LISP string,int i,LISP *symbols); + int num_leafs(LISP l) const; + void find_valid(int i,LISP t) const; + void init(); + public: + /// + EST_bracketed_string(); + /// + EST_bracketed_string(LISP string); + /// + ~EST_bracketed_string(); + + /// + void set_bracketed_string(LISP string); + /// + int length() const {return p_length;} + /// + LISP string() const { return bs; } + /// The nth symbol in the string. + const EST_String symbol_at(int i) const + { return EST_String(get_c_string(car(symbols[i]))); } + /// If a bracketing from i to k is valid in string + int valid(int i,int k) const { return valid_spans[i][k]; } + + /// + int operator !=(const EST_bracketed_string &a) const + { return (!(this == &a)); } + int operator ==(const EST_bracketed_string &a) const + { return ((this == &a)); } + /// + friend ostream& operator << (ostream &s, const EST_bracketed_string &a) + { (void)a; s << "[a bracketed string]" << endl; return s; } + +}; + +typedef EST_TVector EST_Bcorpus; + +// Only support Chomsky Normal Form at present +enum est_scfg_rtype {est_scfg_unset, est_scfg_binary_rule, + est_scfg_unary_rule}; + +/** A stochastic context free grammar rule. + + At present only two types of rule are supported: + {\tt est\_scfg\_binary\_rule} and {\tt est\_scfg\_unary\_rule}. + This is sufficient for the representation of grammars in + Chomsky Normal Form. Each rule also has a probability associated + with it. Terminals and noterminals are represented as ints using + the \Ref{EST_Discrete}s in \Ref{EST_SCFG} to reference the actual + alphabets. + + Although this class includes a ``probability'' nothing in the rule + itself enforces it to be a true probability. It is responsibility + of the classes that use this rule to enforce that condition if + desired. + + @author Alan W Black (awb@cstr.ed.ac.uk): October 1997 +*/ +class EST_SCFG_Rule { + private: + int p_mother; + int p_daughter1; + int p_daughter2; + est_scfg_rtype p_type; + double p_prob; + public: + /// + EST_SCFG_Rule() {p_type=est_scfg_unset; p_prob=0;} + /// + EST_SCFG_Rule(const EST_SCFG_Rule &r) + {p_mother = r.p_mother; p_daughter1 = r.p_daughter1; + p_daughter2 = r.p_daughter2; p_type=r.p_type; p_prob = r.p_prob;} + /// Create a unary rule. + EST_SCFG_Rule(double prob,int p,int m); + /// Create a binary rule. + EST_SCFG_Rule(double prob,int p, int q, int r); + /// The rule's probability + double prob() const {return p_prob;} + /// set the probability + void set_prob(double p) { p_prob=p;} + /// rule type + est_scfg_rtype type() const { return p_type; } + /// + int mother() const {return p_mother;} + /** In a unary rule this is a terminal, in a binary rule it + is a nonterminal + */ + int daughter1() const {return p_daughter1;} + /// + int daughter2() const {return p_daughter2;} + /// + void set_rule(double prob,int p, int m); + /// + void set_rule(double prob,int p, int q, int r); +}; + +typedef EST_TList SCFGRuleList; + +/** A class representing a stochastic context free grammar (SCFG). + + This class includes the representation of the grammar itself and + methods for training and testing it against some corpus. + + At presnet of grammars in Chomsky Normal Form are supported. That + is rules may be binary or unary. If binary the mother an two + daughters are nonterminals, if unary the mother must be nonterminal + and daughter a terminal symbol. + + The terminals and nonterminals symbol sets are derived automatically + from the LISP representation of the rules at initialization time + and are represented as \Ref{EST_Discrete}s. The distinguished + symbol is assumed to be the first mother of the first rule in + the given grammar. + +*/ +class EST_SCFG { + private: + EST_Discrete nonterminals; + EST_Discrete terminals; + int p_distinguished_symbol; + // Index of probabilities for binary rules in grammar + double ***p_prob_B; + // Index of probabilities for unary rules in grammar + double **p_prob_U; + // Build rule probability caches + void rule_prob_cache(); + // Delete rule probability caches + void delete_rule_prob_cache(); + public: + /**@name Constructor and initialisation functions */ + //@{ + EST_SCFG(); + /// Initialize from a set of rules + EST_SCFG(LISP rules); + ~EST_SCFG(); + //@} + + /**@name utility functions */ + //@{ + /// Set (or reset) rules from external source after construction + void set_rules(LISP rules); + /// Return rules as LISP list. + LISP get_rules(); + /// The rules themselves + SCFGRuleList rules; + int distinguished_symbol() const { return p_distinguished_symbol; } + /** Find the terminals and nonterminals in the given grammar, adding + them to the appropriate given string lists. + */ + void find_terms_nonterms(EST_StrList &nt, EST_StrList &t,LISP rules); + /// Convert nonterminal index to string form + EST_String nonterminal(int p) const { return nonterminals.name(p); } + /// Convert terminal index to string form + EST_String terminal(int m) const { return terminals.name(m); } + /// Convert nonterminal string to index + int nonterminal(const EST_String &p) const { return nonterminals.name(p); } + /// Convert terminal string to index + int terminal(const EST_String &m) const { return terminals.name(m); } + /// Number of nonterminals + int num_nonterminals() const { return nonterminals.length(); } + /// Number of terminals + int num_terminals() const { return terminals.length(); } + /// The rule probability of given binary rule + double prob_B(int p, int q, int r) const { return p_prob_B[p][q][r]; } + /// The rule probability of given unary rule + double prob_U(int p, int m) const { return p_prob_U[p][m]; } + /// (re-)set rule probability caches + void set_rule_prob_cache(); + //@} + + /**@name file i/o functions */ + //@{ + /// Load grammar from named file + EST_read_status load(const EST_String &filename); + /// Save current grammar to named file + EST_write_status save(const EST_String &filename); + //@} +}; + +/** A class used to train (and test) SCFGs is an extension of + \Ref{EST_SCFG}. + + This offers an implementation of Pereira and Schabes ``Inside-Outside + reestimation from partially bracket corpora.'' ACL 1992. + + A SCFG maybe trained from a corpus (optionally) containing brackets + over a series of passes reestimating the grammar probabilities + after each pass. This basically extends the \Ref{EST_SCFG} class + adding support for a bracket corpus and various indexes for efficient + use of the grammar. +*/ +class EST_SCFG_traintest : public EST_SCFG { + private: + /// Index for inside probabilities + double ***inside; + /// Index for outside probabilities + double ***outside; + EST_Bcorpus corpus; + /// Partial (numerator) for reestimation + EST_DVector n; + /// Partial (denominator) for reestimation + EST_DVector d; + + /// Calculate inside probability. + double f_I_cal(int c, int p, int i, int k); + /// Lookup or calculate inside probability. + double f_I(int c, int p, int i, int k) + { double r; + if ((r=inside[p][i][k]) != -1) return r; + else return f_I_cal(c,p,i,k); } + /// Calculate outside probability. + double f_O_cal(int c, int p, int i, int k); + /// Lookup or calculate outside probability. + double f_O(int c, int p, int i, int k) + { double r; + if ((r=outside[p][i][k]) != -1) return r; + else return f_O_cal(c,p,i,k); } + /// Find probability of parse of corpus sentence {\tt c} + double f_P(int c); + /** Find probability of parse of corpus sentence {\tt c} for + nonterminal {\tt p} + */ + double f_P(int c,int p); + /// Re-estimate probability of binary rule using inside-outside algorithm + void reestimate_rule_prob_B(int c, int ri, int p, int q, int r); + /// Re-estimate probability of unary rule using inside-outside algorithm + void reestimate_rule_prob_U(int c, int ri, int p, int m); + /// Do grammar re-estimation + void reestimate_grammar_probs(int passes, + int startpass, + int checkpoint, + int spread, + const EST_String &outfile); + /// + double cross_entropy(); + /// Initialize the cache for inside/outside values for sentence {\tt c} + void init_io_cache(int c,int nt); + /// Clear the cache for inside/outside values for sentence {\tt c} + void clear_io_cache(int c); + public: + EST_SCFG_traintest(); + ~EST_SCFG_traintest(); + + /** Test the current grammar against the current corpus print summary. + + Cross entropy measure only is given. + */ + void test_corpus(); + /** Test the current grammar against the current corpus. + + Summary includes percentage of cross bracketing accuracy + and percentage of fully correct parses. + */ + void test_crossbrackets(); + + /** Load a corpus from the given file. + + Each sentence in the corpus should be contained in parentheses. + Additional parenthesis may be used to denote phrasing within + a sentence. The corpus is read using the LISP reader so LISP + conventions shold apply, notable single quotes should appear + within double quotes. + */ + void load_corpus(const EST_String &filename); + + /** Train a grammar using the loaded corpus. + + @param passes the number of training passes desired. + @param startpass from which pass to start from + @param checkpoint save the grammar every n passes + @param spread Percentage of corpus to use on each pass, + this cycles through the corpus on each pass. + */ + void train_inout(int passes, + int startpass, + int checkpoint, + int spread, + const EST_String &outfile); +}; + +/** From a full parse, extract the string with bracketing only. +*/ +LISP scfg_bracketing_only(LISP parse); +/** Cummulate cross bracketing information between ref and test. + */ +void count_bracket_crossing(const EST_bracketed_string &ref, + const EST_bracketed_string &test, + EST_SuffStats &vs); + +#endif diff --git a/aeneas/cfw/speech_tools/EST_SCFG_Chart.h b/aeneas/cfw/speech_tools/EST_SCFG_Chart.h new file mode 100644 index 00000000..eed7c846 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_SCFG_Chart.h @@ -0,0 +1,184 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : June 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A SCFG chart parser, general functions */ +/* */ +/*=======================================================================*/ +#ifndef __EST_SCFG_CHART_H__ +#define __EST_SCFG_CHART_H__ + +#include "EST_String.h" +#include "EST_simplestats.h" +#include "EST_string_aux.h" +#include "EST_SCFG.h" +#include "ling_class/EST_Relation.h" + +class EST_SCFG_Chart_Edge; + +/** An internal class for \Ref{EST_SCFG_Chart} for representing edges + in the chart during parsing with SCFGs. + + A standard Earley type chart edge, with representations for two + daughters and a position or what has been recognised. A probability + is also included. +*/ +class EST_SCFG_Chart_Edge { + private: + int p_d1; + int p_d2; + int p_pos; + double p_prob; + public: + /**@name Constructor and initialisation functions */ + //@{ + EST_SCFG_Chart_Edge(); + EST_SCFG_Chart_Edge(double prob, int d1, int d2, int pos); + ~EST_SCFG_Chart_Edge(); + //@} + + /// Postion, 0 1 or 2, where 0 is empty, 1 is incomplete 2 is complete. + int pos(void) { return p_pos; } + /// Edge probability + double prob(void) { return p_prob; } + /// (Non)terminal of daughter 1 + int d1() { return p_d1; } + /// (Non)terminal of daughter 2 + int d2() { return p_d2; } + +}; + +/** A class for parsing with a probabilistic grammars. + + The chart (sort of closer to CKY table) consists of indexes of + edges indexed by vertex number of mother non-terminal. + + The initial values (well-formed substring table) are taken from + an \Ref{EST_Stream} with a given feature. The grammar may be + specified as LISP rules or as an already constructed \Ref{EST_SCFG}. + + This produces a single best parse. It treats the grammar as + strictly context free in that the probability of a nonterminal + over vertex n to m, is the sum of all the possible analyses + of that sub-tree. Only the best analysis is kept for the + resulting parse tree. + + @author Alan W Black (awb@cstr.ed.ac.uk): October 1997 +*/ +class EST_SCFG_Chart { + private: + /// pointer to grammar + EST_SCFG *grammar; + /// TRUE is grammar was created internally, FALSE is can't be freed + int grammar_local; + /// Number of vertices (number of words + 1) + int n_vertices; + /// Index of edges by vertex start x vertex end x nonterminal + EST_SCFG_Chart_Edge ****edges; + /// Index of basic symbols indexed by (start) vertex. + EST_SCFG_Chart_Edge **wfst; + /// An empty edge, denotes 0 probability edge. + EST_SCFG_Chart_Edge *emptyedge; + + // Find the best analysis of nonterminal {\tt p} from {\tt start} + // to {\tt end}. Used after parsing + double find_best_tree(int start,int end,int p) + { EST_SCFG_Chart_Edge *r; + if ((r=edges[start][end][p]) != 0) return r->prob(); + else return find_best_tree_cal(start,end,p); } + // Calculate best tree/probability + double find_best_tree_cal(int start,int end,int p); + void setup_edge_table(); + void delete_edge_table(); + LISP print_edge(int start, int end, int name, EST_SCFG_Chart_Edge *e); + // Extract edge from chart and add it to stream + void extract_edge(int start, int end, int p, + EST_SCFG_Chart_Edge *e, + EST_Item *s, + EST_Item **word); + // Build parse from distinguished symbol alone + void extract_forced_parse(int start, int end, EST_Item *s, EST_Item *w); + public: + /**@name Constructor and initialisation functions */ + //@{ + EST_SCFG_Chart(); + ~EST_SCFG_Chart(); + //@} + + /**@name Grammar and parse string initialisation functions */ + //@{ + /// Initialize from LISP rules set + void set_grammar_rules(LISP r); + /// Initialize from existing \Ref{EST_SCFG} grammar + void set_grammar_rules(EST_SCFG &grammar); + /** Initialize for parsing from relation using {\tt name} feature + setting up the "Well Formed Substring Table" */ + void setup_wfst(EST_Relation *s,const EST_String &name="name"); + /** Initialize for parsing from s to e using {\tt name} feature + setting up the "Well Formed Substring Table" */ + void setup_wfst(EST_Item *s, EST_Item *e,const EST_String &name="name"); + //@} + + /**@name parsing functions */ + //@{ + /// Parses the loaded WFST with the loaded grammar. + void parse(); + /// Return the parse in full LISP form. + LISP find_parse(); + /// Extract parse tree and add it to syn linking leafs to word + void extract_parse(EST_Relation *syn,EST_Relation *word,int force=0); + /// Extract parse tree and add it to syn linking leafs to items s to e + void extract_parse(EST_Relation *syn,EST_Item *s, + EST_Item *e,int force=0); + //@} +}; + +/** Build a relation from a LISP list of items. +*/ +void EST_SCFG_chart_load_relation(EST_Relation *s,LISP sent); + +/** Parse a given string using the given grammar. +*/ +LISP scfg_parse(LISP string,LISP grammar); +/** Parse the given string using the given \Ref{EST_SCFG}. +*/ +LISP scfg_parse(LISP string,EST_SCFG &grammar); +/** Parse named features in (list) relation Word into (tree) + ** relation Syntax + */ +void scfg_parse(class EST_Relation *Word, const EST_String &name, + class EST_Relation *Syntax, EST_SCFG &grammar); + +#endif diff --git a/aeneas/cfw/speech_tools/EST_SMatrix.h b/aeneas/cfw/speech_tools/EST_SMatrix.h new file mode 100644 index 00000000..1389e48c --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_SMatrix.h @@ -0,0 +1,62 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1996 */ + /* --------------------------------------------------------------------- */ + /* Matrix class */ + /* */ + /*************************************************************************/ + +#ifndef __SMatrix_H__ +#define __SMatrix_H__ + +#include "EST_TSimpleMatrix.h" +#include "EST_TSimpleVector.h" + +class EST_SMatrix: public EST_TSimpleMatrix { +private: +public: + /// size constructor + EST_SMatrix(int m, int n):EST_TSimpleMatrix(m, n) {} + /// copy constructor + EST_SMatrix(EST_SMatrix &a):EST_TSimpleMatrix(a) {} + /// CHECK - what does this do??? + EST_SMatrix(EST_SMatrix &a, int b); + /// default constructor + EST_SMatrix():EST_TSimpleMatrix() {} + + int rateconv(int old_sr, int new_sr); +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Server.h b/aeneas/cfw/speech_tools/EST_Server.h new file mode 100644 index 00000000..26a048c2 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Server.h @@ -0,0 +1,233 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_SERVER_H__ +#define __EST_SERVER_H__ + +#include "EST_Features.h" +#include "EST_ServiceTable.h" + +/** Client-server interface. + * + * An EST_Server object represents a server, it can be at either + * end of a network connection. That is, a server process has an + * EST_Server object representing it's wait-dispatch-answer loop, + * while a client process has an EST_Server object which represents + * the server process. + * + * @author Richard Caley + * @version $Id: EST_Server.h,v 1.4 2004/05/04 00:00:16 awb Exp $ + */ + +class EST_Server { + +public: + + /// What type of server is this. + enum Mode { + /// Bizarre state + sm_unknown = 0, + /// Client end of the connection. + sm_client = 1, + /// Answer one client at a time. + sm_sequential = 2, + /// Answer requests from several clients, as requests arrive. + sm_interleaved =3, + /// For off a process for each client. + sm_fork = 4, + /// Multi-threaded (not implemented) + sm_threded = 5 + }; + + typedef EST_Features Args; + typedef EST_Features Result; + + class RequestHandler + { + public: + EST_Server *server; + EST_String requestString; + EST_String package; + EST_String operation; + Args args; + Result res; + + RequestHandler(); + virtual ~RequestHandler(); + virtual EST_String process(void)=0; + }; + + class ResultHandler + { + public: + EST_Server *server; + EST_String resString; + Result res; + + ResultHandler(); + virtual ~ResultHandler(); + virtual void process(void)=0; + }; + + class BufferedSocket + { + public: + int s; + int bpos; + int blen; + char *buffer; + + BufferedSocket(int socket); + ~BufferedSocket(); + void ensure(int n); + int read_data(void); + }; + +private: + /// Then server we are connected to. + EST_ServiceTable::Entry p_entry; + void *p_serv_addr; + int p_socket; + BufferedSocket *p_buffered_socket; + ostream *p_trace; + Mode p_mode; + + void zero(void); + void init(ostream *trace); + + void initClient(const EST_ServiceTable::Entry &e, ostream *trace); + void initClient(EST_String name, EST_String type, ostream *trace); + void initClient(EST_String hostname, int port, ostream *trace); + + void initServer(Mode mode, EST_String name, EST_String type, ostream *trace); + +protected: + void write(BufferedSocket &s, const EST_String string, const EST_String term = ""); + + EST_String read_data(BufferedSocket &s, const EST_String end, int &eof); + + bool check_cookie(BufferedSocket &socket); + + bool process_command(BufferedSocket &socket, EST_String command, RequestHandler &handler); + + void handle_client(BufferedSocket &socket, RequestHandler &handler); + + void return_error(BufferedSocket &socket, EST_String err); + + void return_value(BufferedSocket &socket, Result &res, bool last); + + void run_sequential(RequestHandler &handler); + +public: + + /**@name Client end constructors. + */ + //@{ + /// Create a server connection by name, defaulting to "fringe", the default server name. + EST_Server(EST_String name, EST_String type); + + EST_Server(EST_String name, EST_String type, ostream *trace); + + /// Create a server connection by explicitly saying where to connect to. + EST_Server(EST_String hostname, int port); + EST_Server(EST_String hostname, int port, ostream *trace); + //@} + + /**@name Server end constructors + */ + //@{ + EST_Server(Mode mode, EST_String name, EST_String type); + EST_Server(Mode mode, EST_String name, EST_String type, ostream *trace); + //@} + + /// Destroy the connection. + virtual ~EST_Server(); + + /**@name information about the server. + */ + //@{ + /// Name of server. + const EST_String name(void) const; + /// Type of server. + const EST_String type(void) const; + /// Domain name of the server. + const EST_String hostname(void) const; + /// Dotted numeric IP address + const EST_String address(void) const; + /// Domain name or IP number + const EST_String servername(void) const; + /// Port number + int port(void) const; + //@} + + /**@name connection management + */ + //@{ + /// Connect to the server. + EST_connect_status connect(void); + /// Are we connected at the moment? + bool connected(void); + /// Disconnect. + EST_connect_status disconnect(void); + //@} + + virtual bool parse_command(const EST_String command, + EST_String &package, + EST_String &operation, + Args &arguments); + + virtual EST_String build_command(const EST_String package, + const EST_String operation, + const Args &arguments); + + virtual bool parse_result(const EST_String resultString, + Result &res); + + virtual EST_String build_result(const Result &res); + + + bool execute(const EST_String package, + const EST_String operation, + const Args &arguments, + ResultHandler &handler); + + bool execute(const EST_String command, + ResultHandler &handler); + + void run(RequestHandler &handler); + +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_ServiceTable.h b/aeneas/cfw/speech_tools/EST_ServiceTable.h new file mode 100644 index 00000000..8f5429dc --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_ServiceTable.h @@ -0,0 +1,144 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_SERVICETABLE_H__ +#define __EST_SERVICETABLE_H__ + +#include "EST_String.h" +#include "EST_rw_status.h" +#include "EST_TKVL.h" + +/** A global table of known services. Used for fringe and festival + * servers. + * + * @author Richard Caley + * @version $Id: EST_ServiceTable.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +class EST_ServiceTable { + + +public: + /** A service record. + * + * This is returned by service lookup operations, it contains + * enough information to contact the server and authenticate yourself. + */ + + class Entry + { + public: + /// Name of the server. + EST_String name; + /// Type of server (eg `fringe') + EST_String type; + /// Human readable hostname. + EST_String hostname; + /// Numeric IP address (###.###.###.###) + EST_String address; + /// A random string to send as authorisation. + EST_String cookie; + /// TCP port number. + int port; + + /// Create an empty entry. + Entry(); + + /// A suitable human readable name for the entry. + operator EST_String() const; + + /// All entries are taken to be different. + friend bool operator == (const Entry &a, const Entry &b); + + /// Print in human readable form. + friend ostream & operator << (ostream &s, const Entry &a); + }; + +private: + + class EntryTable + { + public: + EST_TKVL t; + typedef EST_TKVL::Entries Entries; + typedef EST_TKVL::KeyEntries KeyEntries; + }; + + /// Table of available Fringe servers. + static EntryTable entries; + + static bool random_init; + static void init_random(void); + +public: + + /**@name Finding What Services Are Available. + * + * Servers maintain a per-user file which lists the processes + * which are running in server mode by name. These functions read + * that table. */ + //@{ + + /// Read the users default table. ~/.estServices + static void read_table(void); + /// Read a specific table. + static void read_table(EST_String socketsFileName); + /// Write the users default table. ~/.estServices + static void write_table(void); + /// Write a specific table. + static void write_table(EST_String socketsFileName); + /// List the table to given stream + static void list(ostream &s, const EST_String type); + /// Return a list of server names. + static void names(EST_TList &names, const EST_String type=""); + + //@} + + /** Return the entry for the server with the given name and type. + * If no such entry is found a dummy entry with a port of 0 is returned. + */ + static const Entry &lookup(const EST_String name, + const EST_String type); + + /** Create an entry for a server of the given name and type which is + * listening on the given socket. + */ + static const Entry &create(const EST_String name, + const EST_String type, + int socket); + +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_SingleChannelWave.h b/aeneas/cfw/speech_tools/EST_SingleChannelWave.h new file mode 100644 index 00000000..c47e7801 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_SingleChannelWave.h @@ -0,0 +1,64 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_SINGECHANNELWAVE_H__ +#define __EST_SINGECHANNELWAVE_H__ + +/** An alias for wave which can be used to tell the compiler you know it + * is a simple one channel wave. This can speed up access. + * + * @author Richard Caley + * @version $Id: EST_SingleChannelWave.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#include "EST_Wave.h" + +class EST_SingleChannelWave : public EST_Wave { + +public: + /// default constructor + EST_SingleChannelWave() : EST_Wave() { }; + + EST_SingleChannelWave(const EST_SingleChannelWave &w) : EST_Wave(w) { }; + + EST_SingleChannelWave(int n, int sr) : EST_Wave(n, 1, sr) { }; + + INLINE short &a_no_check(int i, int channel = 0) + { return p_values.a_no_check_1(i,channel); } + INLINE short a_no_check(int i, int channel = 0) const + { return p_values.a_no_check_1(i,channel); } +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_String.h b/aeneas/cfw/speech_tools/EST_String.h new file mode 100644 index 00000000..da930fff --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_String.h @@ -0,0 +1,652 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + +#ifndef __EST_STRING_H__ +#define __EST_STRING_H__ + +class EST_String; +class EST_Regex; + +#define EST_Regex_max_subexpressions 10 + +#include +#include +#include +using namespace std; +#include "EST_Chunk.h" +#include "EST_strcasecmp.h" +#include "EST_bool.h" + +extern "C" void abort(void); + +/** A non-copyleft implementation of a string class to use with + * compilers that aren't GNU C++. + * + * Strings are reference-counted and reasonably efficient (eg you + * can pass them around, into and out of functions and so on + * without worrying too much about the cost). + * + * The associated class EST_Regex can be used to represent regular + * expressions. + * + * @see EST_Chunk + * @see EST_Regex + * @see string_example + * @author Alan W Black + * @author Richard Caley + * @version $Id: EST_String.h,v 1.10 2014/10/13 13:26:19 robert Exp $ + */ + +class EST_String { + + /** For better libg++ compatibility. + * + * Includes String from char constructor which + * tends to mask errors in use. Also reverses the () and [] operators. + */ +# define __FSF_COMPATIBILITY__ (0) + + /** Allow gsub() to be used in multi-threaded applications + * This will cause gsub to use a local table of substitution points + * walloced for each gsub. Otherwise one global one is used which + * should be faster, but non reentrant. + */ +# define __GSUB_REENTRANT__ (1) + +/// Gripe about weird arguments like Nulls +#define __STRING_ARG_GRIPE__ (1) + +/// When we find something to gripe about we die then and there. +#define __GRIPE_FATAL__ (1) + +#if __GRIPE_FATAL__ +# define gripe(WHAT) (cerr<< ("oops! " WHAT "\n"),abort()) +#else +# define gripe(WHAT) (cerr<< ("oops! " WHAT "\n")) +#endif + +#if __STRING_ARG_GRIPE__ +# define safe_strlen(S) ((S)?strlen(S):(gripe("null strlen"),0)) +# define CHECK_STRING_ARG(S) if (!(S)) gripe("null string arg") +#else +# define safe_strlen(S) ((S)?strlen(S):0) +# define CHECK_STRING_ARG(S) /* empty */ +#endif + +public: + /// Global version string. + static const char *version; + + /// Constant empty string + static const EST_String Empty; + + /// Type of string size field. + typedef int EST_string_size; + /// Maximum string size. +# define MAX_STRING_SIZE (INT_MAX) + +private: + /// Smart pointer to actual memory. + EST_ChunkPtr memory; + /// Size of string. + EST_string_size size; + + // Make sure this is exactly the same as an EST_String. This is being too + // clever by half. + + struct EST_dumb_string { + EST_ChunkPtr memory; + EST_string_size size; + } ; + + /// Flags indicating which bit of a string to extract. + enum EST_chop_direction { + Chop_Before = -1, + Chop_At = 0, + Chop_After = 1 + }; + + /// Simple utility which removes const-ness from memory + static inline EST_ChunkPtr &NON_CONST_CHUNKPTR(const EST_ChunkPtr &ecp) + { return *((EST_ChunkPtr *)&ecp);} + + /// private constructor which uses the buffer given. + EST_String(int len, EST_ChunkPtr cp) { + size=len; + memory = cp; + } + + /// Is more than one String represented by the same memory? + int shareing (void) { return memory.shareing();} + + /**@name Finding substrings */ + //@{ + /// Find substring + int locate(const char *it, int len, int from, int &start, int &end) const; + /// Find substring + int locate(const EST_String &s, int from, int &start, int &end) const + { return locate((const char *)s.memory, s.size, from, start, end); } + /// Find match for regexp. + int locate(EST_Regex &ex, int from, int &start, int &end, int *starts=NULL, int *ends=NULL) const; + //@} + + + /**@name Extract Substrings */ + //@{ + int extract(const char *it, int len, int from, int &start, int &end) const; + int extract(const EST_String &s, int from, int &start, int &end) const + { return extract((const char *)s.memory, s.size, from, start, end); } + int extract(EST_Regex &ex, int from, int &start, int &end) const; + //@} + + /**@name Chop out part of string */ + //@{ + /// Locate subsring and chop. + EST_String chop_internal(const char *s, int length, int pos, EST_chop_direction directionult) const; + /// Chop at given position. + EST_String chop_internal(int pos, int length, EST_chop_direction directionult) const; + + /// Locate match for expression and chop. + EST_String chop_internal(EST_Regex &ex, int pos, EST_chop_direction directionult) const; + //@} + + /**@name Global search and replace */ + //@{ + /// Substitute for string + int gsub_internal(const char *os, int olength, const char *s, int length); + /// Substitute for matches of regexp. + int gsub_internal(EST_Regex &ex, const char *s, int length); + //@} + + /// Split the string down into parts. + int split_internal(EST_String result[], int max, const char* s_seperator, int slen, EST_Regex *re_separator, char quote) const; + + int Int(bool *ok_p) const; + long Long(bool *ok_p) const; + float Float(bool *ok_p) const; + double Double(bool *ok_p) const; +public: + + /// Construct an empty string. + EST_String(void) :memory() {size=0;} + + /// Construct from char * + EST_String(const char *s); + + /// Construct from part of char * or fill with given character. + EST_String(const char *s, int start_or_fill, int len); + + /// Construct from C string. + EST_String(const char *s, int s_size, int start, int len); + + // Create from EST_String + EST_String(const EST_String &s, int start, int len); + + /** Copy constructor + * We have to declare our own copy constructor to lie to the + * compiler about the constness of the RHS. + */ + EST_String(const EST_String &s) { + memory = NON_CONST_CHUNKPTR(s.memory); + size = s.size; + } + +#if __FSF_COMPATIBILITY__ + /** Construct from single char. + * This constructor is not usually included as it can mask errors. + * @see __FSF_COMPATIBILITY__ + */ + EST_String(const char c); +#endif + + /// Destructor. + ~EST_String() { + size=0; + memory=NULL; + } + + /// Length of string ({\em not} length of underlying chunk) + int length(void) const { return size; } + /// Size of underlying chunk. + int space (void) const { return memory.size(); } + /// Get a const-pointer to the actual memory. + const char *str(void) const { return size==0?"":(const char *)memory; } + /// Get a writable pointer to the actual memory. + char *updatable_str(void) { return size==0?(char *)"":(char *)memory; } + void make_updatable(void) { cp_make_updatable(memory, size+1);} + + + /// Build string from a single character. + static EST_String FromChar(const char c) + { const char s[2] = { c, 0 }; return EST_String(s); } + + /// Build string from an integer. + static EST_String Number(int i, int base=10); + + /// Build string from a long integer. + static EST_String Number(long i, int base=10); + + /// Build string from a double. + static EST_String Number(double d); + + /// Build string from a float + static EST_String Number(float f); + + /// Convert to an integer + int Int(bool &ok) const { return Int(&ok); } + int Int(void) const { return Int((bool *)NULL); } + + /// Convert to a long + long Long(bool &ok) const { return Long(&ok); } + long Long(void) const { return Long((bool *)NULL); } + + /// Convert to a float + float Float(bool &ok) const { return Float(&ok); } + float Float(void) const { return Float((bool *)NULL); } + + /// Convert to a double + double Double(bool &ok) const { return Double(&ok); } + double Double(void) const { return Double((bool *)NULL); } + + /**@name Before */ + //@{ + /// Part before position + EST_String before(int pos, int len=0) const + { return chop_internal(pos, len, Chop_Before); } + /// Part before first matching substring after pos. + EST_String before(const char *s, int pos=0) const + { return chop_internal(s, safe_strlen(s), pos, Chop_Before); } + /// Part before first matching substring after pos. + EST_String before(const EST_String &s, int pos=0) const + { return chop_internal(s.str(), s.size, pos, Chop_Before); } + /// Part before first match of regexp after pos. + EST_String before(EST_Regex &e, int pos=0) const + { return chop_internal(e, pos, Chop_Before); } + //@} + + /**@name At */ + //@{ + /// Return part at position + EST_String at(int from, int len=0) const + { return EST_String(str(),size,from<0?(size+from):from,len); } + /// Return part where substring found (not useful, included for completeness) + EST_String at(const char *s, int pos=0) const + { return chop_internal(s, safe_strlen(s), pos, Chop_At); } + /// Return part where substring found (not useful, included for completeness) + EST_String at(const EST_String &s, int pos=0) const + { return chop_internal(s.str(), s.size, pos, Chop_At); } + /// Return part matching regexp. + EST_String at(EST_Regex &e, int pos=0) const + { return chop_internal(e, pos, Chop_At); } + //@} + + /**@name After */ + //@{ + /// Part after pos+len + EST_String after(int pos, int len=1) const + { return chop_internal(pos, len, Chop_After); } + /// Part after substring. + EST_String after(const char *s, int pos=0) const + { return chop_internal(s, safe_strlen(s), pos, Chop_After); } + /// Part after substring. + EST_String after(const EST_String &s, int pos=0) const + { return chop_internal(s.str(), s.size, pos, Chop_After); } + /// Part after match of regular expression. + EST_String after(EST_Regex &e, int pos=0) const + { return chop_internal(e, pos, Chop_After); } + //@} + + /**@name Search for something */ + //@{ + /// Find a substring. + int search(const char *s, int len, int &mlen, int pos=0) const + { int start, end; + if (locate(s, len, pos, start, end)) + { mlen=end-start; return start; } + return -1; + } + + /// Find a substring. + int search(const EST_String s, int &mlen, int pos=0) const + { int start, end; + if (locate(s, pos, start, end)) + { mlen=end-start; return start; } + return -1; + } + + /// Find a match of the regular expression. + int search(EST_Regex &re, int &mlen, int pos=0, int *starts=NULL, int *ends=NULL) const + { int start=0, end=0; + if (locate(re, pos, start, end, starts, ends)) + { mlen=end-start; return start; } + return -1; + } + //@} + + + /**@name Get position of something */ + //@{ + /// Position of substring (starting at pos) + int index(const char *s, int pos=0) const + { int start, end; return locate(s, safe_strlen(s), pos, start, end)?start:-1; } + /// Position of substring (starting at pos) + int index(const EST_String &s, int pos=0) const + { int start, end; return locate(s, pos, start, end)?start:-1; } + /// Position of match of regexp (starting at pos) + int index(EST_Regex &ex, int pos=0) const + { int start, end; return locate(ex, pos, start, end)?start:-1; } + //@} + + /**@name Does string contain something? */ + //@{ + /// Does it contain this substring? + int contains(const char *s, int pos=-1) const + { int start, end; return extract(s, safe_strlen(s), pos, start, end); } + /// Does it contain this substring? + int contains(const EST_String &s, int pos=-1) const + { int start, end; return extract(s, pos, start, end); } + /// Does it contain this character? + int contains(const char c, int pos=-1) const + { int start, end; char s[2] = {c,0}; return extract(s, 1, pos, start, end); } + /// Does it contain a match for this regular expression? + int contains(EST_Regex &ex, int pos=-1) const + { int start, end; return extract(ex, pos, start, end); } + //@} + + /**@name Does string exactly match? */ + //@{ + /// Exactly match this string? + int matches(const char *e, int pos=0) const; + /// Exactly match this string? + int matches(const EST_String &e, int pos=0) const; + /// Exactly matches this regular expression, can return ends of sub-expressions. + int matches(EST_Regex &e, int pos=0, int *starts=NULL, int *ends=NULL) const; + //@} + + /**@name Global replacement */ + //@{ + /// Substitute one string for another. + int gsub(const char *os, const EST_String &s) + { return gsub_internal(os, safe_strlen(os), s, s.size); } + /// Substitute one string for another. + int gsub(const char *os, const char *s) + { return gsub_internal(os, safe_strlen(os), s, safe_strlen(s)); } + /// Substitute one string for another. + int gsub(const EST_String &os, const EST_String &s) + { return gsub_internal(os, os.size, s, s.size); } + /// Substitute one string for another. + int gsub(const EST_String &os, const char *s) + { return gsub_internal(os, os.size, s, safe_strlen(s)); } + + /// Substitute string for matches of regular expression. + int gsub(EST_Regex &ex, const EST_String &s) + { return gsub_internal(ex, s, s.size); } + /// Substitute string for matches of regular expression. + int gsub(EST_Regex &ex, const char *s) + { return gsub_internal(ex, s, safe_strlen(s)); } + /// Substitute string for matches of regular expression. + int gsub(EST_Regex &ex, int bracket_num) + { return gsub_internal(ex, NULL, bracket_num); } + /// Substitute the result of a match into a string. + int subst(EST_String source, + int (&starts)[EST_Regex_max_subexpressions], + int (&ends)[EST_Regex_max_subexpressions]); + //@} + + /**@name Frequency counts */ + //@{ + /// Number of occurrences of substring + int freq(const char *s) const; + /// Number of occurrences of substring + int freq(const EST_String &s) const; + /// Number of matches of regular expression. + int freq(EST_Regex &s) const; + //@} + + /**@name Quoting */ + //@{ + /// Return the string in quotes with internal quotes protected. + EST_String quote(const char quotec) const; + /// Return in quotes if there is something to protect (e.g. spaces) + EST_String quote_if_needed(const char quotec) const; + /// Remove quotes and unprotect internal quotes. + EST_String unquote(const char quotec) const; + /// Remove quotes if any. + EST_String unquote_if_needed(const char quotec) const; + //@} + +#if __FSF_COMPATIBILITY__ + const char operator [] (int i) const { return memory[i]; } + char &operator () (int i) { return memory(i); } +#else + /**@name Operators */ + //@{ + /// Function style access to constant strings. + const char operator () (int i) const { return memory[i]; } + /// Array style access to writable strings. + char &operator [] (int i) { return memory(i); } +#endif + + /// Cast to const char * by simply giving access to pointer. + operator const char*() const {return str(); } + operator const char*() {return str(); } + /// Cast to char *, may involve copying. + operator char*() { return updatable_str(); } + + /**@name Add to end of string. */ + //@{ + /// Add C string to end of EST_String + EST_String &operator += (const char *b); + /// Add EST_String to end of EST_String + EST_String &operator += (const EST_String b); + //@} + + /**@name Assignment */ + //@{ + /// Assign C string to EST_String + EST_String &operator = (const char *str); + /// Assign single character to EST_String + EST_String &operator = (const char c); + /// Assign EST_String to EST_String. + EST_String &operator = (const EST_String &s); + //@} + + /**@name Concatenation */ + //@{ + /// Concatenate two EST_Strings + friend EST_String operator + (const EST_String &a, const EST_String &b); + /// Concatenate C String with EST_String + friend EST_String operator + (const char *a, const EST_String &b); + /// Concatenate EST_String with C String + friend EST_String operator + (const EST_String &a, const char *b); + //@} + + /// Repeat string N times + friend EST_String operator * (const EST_String &s, int n); + + /**@name relational operators */ + //@{ + /// + friend int operator == (const char *a, const EST_String &b); + /// + friend int operator == (const EST_String &a, const char *b) + { return b == a; } + /// + friend int operator == (const EST_String &a, const EST_String &b); + + /// + friend int operator != (const char *a, const EST_String &b) + { return !(a==b); } + /// + friend int operator != (const EST_String &a, const char *b) + { return !(a==b); } + /// + friend int operator != (const EST_String &a, const EST_String &b) + { return !(a==b); } + + /// + friend inline int operator < (const char *a, const EST_String &b) + { return compare(a,b) < 0; } + /// + friend inline int operator < (const EST_String &a, const char *b) + { return compare(a,b) < 0; } + /// + friend inline int operator < (const EST_String &a, const EST_String &b) + { return compare(a,b) < 0; } + /// + friend inline int operator > (const char *a, const EST_String &b) + { return compare(a,b) > 0; } + /// + friend inline int operator > (const EST_String &a, const char *b) + { return compare(a,b) > 0; } + /// + friend inline int operator > (const EST_String &a, const EST_String &b) + { return compare(a,b) > 0; } + /// + friend inline int operator <= (const char *a, const EST_String &b) + { return compare(a,b) <= 0; } + /// + friend inline int operator <= (const EST_String &a, const char *b) + { return compare(a,b) <= 0; } + /// + friend inline int operator <= (const EST_String &a, const EST_String &b) + { return compare(a,b) <= 0; } + /// + friend inline int operator >= (const char *a, const EST_String &b) + { return compare(a,b) >= 0; } + /// + friend inline int operator >= (const EST_String &a, const char *b) + { return compare(a,b) >= 0; } + /// + friend inline int operator >= (const EST_String &a, const EST_String &b) + { return compare(a,b) >= 0; } + //@} + + //@} + + /**@name String comparison. + * All these operators return -1, 0 or 1 to indicate the sort + * order of the strings. + */ + //@{ + /// + friend int compare(const EST_String &a, const EST_String &b); + /// + friend int compare(const EST_String &a, const char *b); + /// + friend inline int compare(const char *a, const EST_String &b) + { return -compare(b,a); } + /** Case folded comparison. + * + * The table argument can defined how upper and lower + * case characters correspond. The default works for + * ASCII. + */ + //@{ + friend int fcompare(const EST_String &a, const EST_String &b, + const unsigned char *table); + + friend inline int fcompare(const EST_String &a, const EST_String &b) + { return fcompare(a,b,NULL); } + + + friend int fcompare(const EST_String &a, const char *b, + const unsigned char *table); + /// + friend inline int fcompare(const EST_String &a, const EST_String &b, + const EST_String &table) + { return fcompare(a, b, (const unsigned char *)(const char *)table); } + //@} + //@} + //@} + + + /**@name Split a string into parts. + * + * These functions divide up a string producing an array of + * substrings. + */ + //@{ + /// Split at a given separator. + friend int split(const EST_String & s, EST_String result[], + int max, const EST_String& seperator, char quote=0) + { return s.split_internal(result, max, (const char *)seperator, seperator.length(), NULL, quote); } + /// Split at a given separator. + friend int split(const EST_String &s, EST_String result[], + int max, const char *seperator, char quote=0) + { return s.split_internal(result, max, seperator, strlen(seperator), NULL, quote); } + /// Split at each match of the regular expression. + friend int split(const EST_String & s, EST_String result[], int max, + EST_Regex& seperator, char quote=0) + { return s.split_internal(result, max, NULL, 0, &seperator, quote); } + //@} + + /// Convert to upper case. + friend EST_String upcase(const EST_String &s); + /// Convert to lower case. + friend EST_String downcase(const EST_String &s); + + /** Concatenate a number of strings. + * This is more efficient than multiple uses of + or += + */ + static EST_String cat(const EST_String s1, + const EST_String s2 = Empty, + const EST_String s3 = Empty, + const EST_String s4 = Empty, + const EST_String s5 = Empty, + const EST_String s6 = Empty, + const EST_String s7 = Empty, + const EST_String s8 = Empty, + const EST_String s9 = Empty + ); + + /* Hacky way to ignore volatile */ + EST_String & ignore_volatile(void) volatile { return *((EST_String *)(void *)this); } + + /// Stream output for EST_String. + friend ostream &operator << (ostream &s, const EST_String &str); + friend class EST_Regex; + +}; + +EST_ChunkPtr chunk_allocate(int bytes); +EST_ChunkPtr chunk_allocate(int bytes, const char *initial, int initial_len); +EST_ChunkPtr chunk_allocate(int bytes, const EST_ChunkPtr &initial, int initial_start, int initial_len); + +int operator == (const char *a, const EST_String &b); +int operator == (const EST_String &a, const EST_String &b); +ostream &operator << (ostream &s, const EST_String &str); + +#include "EST_Regex.h" + +#endif diff --git a/aeneas/cfw/speech_tools/EST_StringTrie.h b/aeneas/cfw/speech_tools/EST_StringTrie.h new file mode 100644 index 00000000..f8879598 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_StringTrie.h @@ -0,0 +1,110 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : June 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A class for building EST_String (char-based) tries for indexing */ +/* arbitrary objects by Strings */ +/* */ +/*=======================================================================*/ +#ifndef __EST_STRINGTRIE_H__ +#define __EST_STRINGTRIE_H__ + +#include "EST_String.h" + +class EST_StringTrie; + +/** An internal class for \Ref{EST_StringTrie} used to hold represent the + node in an string index tree. + + This basically represents a 128-branching node (on for each character) + plus a contents field for strings ending at this point. + + @author Alan W Black (awb@cstr.ed.ac.uk): June 1996 +*/ +class EST_TrieNode { + private: + int w; + EST_TrieNode **d; + void *contents; + // will use EST_TrieContents when I have a list of contents + public: + /// + EST_TrieNode() {w=0; d=0; contents=0;} + /// + EST_TrieNode(const int width); + /// + ~EST_TrieNode(); + /// Find the contents for given string, 0 if no current contents + void *lookup(const unsigned char *key) const; + /// add {\tt item} for {\tt key} overwriting previous contents + void add(const unsigned char *key,void *item); + /// copy all entries in trie node into trie + void copy_into(EST_StringTrie &trie, const EST_String &path) const; +}; + +/** A string tree index class for indexing arbitrary objects by + strings of characters. + + Note this only deals with 7 but characters, and can only hold + one item per index key. +*/ +class EST_StringTrie { + private: + EST_TrieNode *tree; + public: + /// + EST_StringTrie(); + /// + EST_StringTrie(const EST_StringTrie &trie) { copy(trie); } + /// + ~EST_StringTrie(); + /// + void copy(const EST_StringTrie &trie); + /// Find contents index by {\tt key}, 0 if there is not contents + void *lookup(const EST_String &key) const; + /// Add {\tt item} indexed by {\tt key}, overwriting previous contents + void add(const EST_String &key,void *item); + /// Delete the tree + void clear(void); + /// Delete the tree, apply {\tt deletenote} function to each {\tt contents} + void clear(void (*deletenode)(void *n)); + + /// + EST_StringTrie & operator = (const EST_StringTrie &a) + { copy(a); return *this; } + +}; + +#endif // __EST_STRINGTRIE_H__ diff --git a/aeneas/cfw/speech_tools/EST_TBox.h b/aeneas/cfw/speech_tools/EST_TBox.h new file mode 100644 index 00000000..2e648354 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TBox.h @@ -0,0 +1,69 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TBOX_H__ +#define __EST_TBOX_H__ + +/** Box for putting things in. The box and the thing can then be refcounted + * as a unit. + * + * @author Richard Caley + * @version $Id: EST_TBox.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#include "EST_Handleable.h" + +template +class EST_TBox : public EST_Handleable +{ +private: + T * const p_contents; + + /// This never happens. + EST_TBox(void) : p_contents(NULL) {}; + +public: + + EST_TBox(T *c) : p_contents(c) { start_refcounting(); } + ~EST_TBox(void) { delete p_contents; } + + T *object_ptr() { return p_contents; } + const T *object_ptr() const { return p_contents; } + + T *c() { return p_contents; } + const T *c() const { return p_contents; } +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_TBuffer.h b/aeneas/cfw/speech_tools/EST_TBuffer.h new file mode 100644 index 00000000..89d1d83f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TBuffer.h @@ -0,0 +1,159 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + +/**@name EST_TBuffer.h + * Extending buffers, ie arrays which grow as needed. + * I got fed up of writing equivalent code all over the place. + * + * @see EST_TBuffer + * @author Richard Caley + * @version $Id: EST_TBuffer.h,v 1.4 2004/09/29 08:24:17 robert Exp $ + */ +//@{ +#ifndef __EST_TBUFFER_H__ +#define __EST_TBUFFER_H__ + +#include "EST_bool.h" + +/// How many chunks of memory to keep around for re-use. +#define TBUFFER_N_OLD (10) + +/// Initial size for buffers created with no size specified. +#define TBUFFER_DEFAULT_SIZE 0 + +/// Amount to increment buffer size by. +#define TBUFFER_DEFAULT_STEP 100 + +/** Structure to remember old buffers for re-use. + * @see EST_TBuffer.h + */ +struct old_tbuffer { void *mem; unsigned int size; }; + +/// Memory of old buffers +extern struct old_tbuffer EST_old_buffers[TBUFFER_N_OLD]; + + +/** Extending buffer class. + *

+ * This class provides the convenience of arrays which change in size + * at run time rather more efficiently than the full EST_TVector class + * would. + *

+ * Buffers can only be expanded and when a buffer is no longer needed + * (i.e. when the variable goes out of scope) the memory is not deleted, + * rather it is saved for re-use as a new buffer. + *

+ * A typical use would be a buffer to hold a windowed section of a signal + * inside a signal processing loop where the size of the window changes from + * iteration to iteration. + * + * @see EST_TBuffer.h, Associated definitions. + * @see EST_TVector, class to use for more general uses. + * @see lpc_analyse, example of use. + */ + +template +class EST_TBuffer { + +private: + /// Pointer to memory. + TYPE *p_buffer; + /// Current size. + unsigned int p_size; + /// Amount to grow by (if negative it is a percentage). + int p_step; + +private: + /// Setup code. + void init(unsigned int size, int step); + /// Expand the buffer. + void expand_to(unsigned int req_size, bool cpy); + /// Expand and set some of it to a known value. + void expand_to(unsigned int req_size, const TYPE &set_to, int howmany); + +public: + + /** Create with size and increment. + * Increment can be negative for percentage growth. + *

+ * Tries to use a buffer from EST_old_buffers[] if there is one which + * is suitable + * @see EST_old_buffers + */ + EST_TBuffer(unsigned int size=TBUFFER_DEFAULT_SIZE, int step=TBUFFER_DEFAULT_STEP); + + /// Destructor. Places the memory in EST_old_buffers[] for re-use if there is room. + ~EST_TBuffer(void); + + /// Current available space. + unsigned int length(void) const {return p_size;} + + /// Set to the given value. By default sets all values. + void set(const TYPE &set_to, int howmany=-1); + + /**@name Methods to make sure there is enough space. */ + //@{ + + /// Extend if needed, copying existing data. + void ensure(unsigned int req_size) + {if (req_size > p_size) expand_to(req_size, (bool)TRUE);} + + /// Make sure there is enough space, copying if requested. + void ensure(unsigned int req_size, bool copy) + {if (req_size > p_size) expand_to(req_size, copy,-1);} + + /// Make sure there is enough space, setting to a known value. + void ensure(unsigned int req_size, const TYPE &set_to, int howmany=-1) + {if (req_size > p_size) expand_to(req_size, set_to, howmany);} + //@} + + /**@name Access to the memory itself. */ + //@{ + + /// Simple access as a pointer. + TYPE *b(void) {return p_buffer;} + /// Read-only access when the EST_TBuffer is a constant + const TYPE *b(void) const {return p_buffer;} + + /// operator () is simple access + const TYPE &operator() (unsigned int i) const { return p_buffer[i];} + + TYPE &operator[] (unsigned int i) { return p_buffer[i];} + const TYPE &operator[] (unsigned int i) const { return p_buffer[i];} + + //@} +}; + +#endif + +//@} diff --git a/aeneas/cfw/speech_tools/EST_TDeque.h b/aeneas/cfw/speech_tools/EST_TDeque.h new file mode 100644 index 00000000..7539aca9 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TDeque.h @@ -0,0 +1,120 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TDEQUE_H__ +#define __EST_TDEQUE_H__ + +#include "EST_TVector.h" +#include "instantiate/EST_TDequeI.h" + +/** Double ended queue. + * + * @author Richard Caley + * @version $Id: EST_TDeque.h,v 1.3 2006/07/19 21:52:12 awb Exp $ + */ + +template +class EST_TDeque { +private: + EST_TVector p_vector; + int p_increment; + int p_back; + int p_front; + + // Make the structure bigger. + void expand(void); + +public: + EST_TDeque(unsigned int capacity, unsigned int increment); + EST_TDeque(unsigned int capacity); + EST_TDeque(void); + + /// Used to fill empty spaces when possible. + static const T *Filler; + + /// Empty it out. + void clear(void); + + /// Is there anything to get? + bool is_empty(void) const; + + /// print picture of state. Mostly useful for debugging. + ostream &print(ostream &s) const; + + /**@name stack + * + * An interface looking like a stack. + */ + //@{ + void push(T &item); + T &pop(void); + T &nth(int i); + //@} + + /**@name inverse stack + * + * The other end as a stack. + */ + //@{ + void back_push(T& item); + T &back_pop(void); + //@} + + /**@name queue + * + * An interface looking like a queue. + */ + //@{ + void add(T& item) { push(item); } + T &next() { return back_pop(); } + T &last() { return pop(); } + //@} + + /**@name perl + * + * For people who think in perl + */ + //@{ + void unshift(T& item) { back_push(item); } + T &shift() { return back_pop(); } + //@} + + friend ostream& operator << (ostream &st, const EST_TDeque< T > &deq) + { + return deq.print(st); + } +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_THandle.h b/aeneas/cfw/speech_tools/EST_THandle.h new file mode 100644 index 00000000..1bd880cf --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_THandle.h @@ -0,0 +1,152 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + +#ifndef __EST_THANDLE_H__ +#define __EST_THANDLE_H__ + +#include + +using namespace std; + +#include "EST_bool.h" + +/** A `smart' pointer which does reference counting. + * + * Behaves almost like a pointer as far as naive code is concerned, but + * keeps count of how many handles are holding on to the contents + * and deletes it when there are none. + * + * You need to be careful there are no dumb C++ pointers to things which + * are being handled this way. + * + * Things to be handled should implement the same interface as EST_Handleable + * (either by taking that as a superclass or by reimplementing it) and in + * addition define {\tt object_ptr()}. See EST_TBox for an example. + * + * There are two parameter types. In most cases the thing which contains the + * reference count and the data it represents will be the same object, but + * in the case of boxed values it may not be, so you can specify the type + * of both independently. + * + * @see EST_Handleable + * @see EST_TBox + * @see EST_THandle:example + * + * @author Richard Caley + * @version $Id: EST_THandle.h,v 1.5 2006/07/19 21:52:12 awb Exp $ + */ + + +template +class EST_THandle { + +private: + BoxT *ptr; + +public: + + EST_THandle(void) { ptr = (BoxT *)NULL; } + + EST_THandle(BoxT *p) { if ((ptr=p)) p->inc_refcount(); } + + EST_THandle(const EST_THandle &cp) { + ptr=cp.ptr; + if (ptr) + ptr->inc_refcount(); + } + + ~EST_THandle(void) { + if (ptr) + ptr->dec_refcount(); + if (ptr && ptr->is_unreferenced()) + delete ptr; + } + + bool null() const { return ptr == NULL; } + + int shareing(void) const { return ptr?(ptr->refcount() > 1):0; } + + EST_THandle &operator = (EST_THandle h) { + // doing it in this order means self assignment is safe. + if (h.ptr) + (h.ptr)->inc_refcount(); + if (ptr) + ptr->dec_refcount(); + if (ptr && ptr->is_unreferenced()) + delete ptr; + ptr=h.ptr; + return *this; + } + + // If they manage to get hold of one... + // Actually usually used to assign NULL and so (possibly) deallocate + // the object currently pointed to. + EST_THandle &operator = (BoxT *t_ptr) { + // doing it in this order means self assignment is safe. + if (t_ptr) + t_ptr->inc_refcount(); + if (ptr) + ptr->dec_refcount(); + if (ptr && ptr->is_unreferenced()) + delete ptr; + ptr=t_ptr; + return *this; + } + + operator ObjectT *() { + return ptr?(ptr->object_ptr()):(ObjectT *)NULL; + } + + operator const ObjectT *() const { + return ptr?(ptr->object_ptr()):(const ObjectT *)NULL; + } + + + int operator == (const BoxT *p) const { return ptr == p; } + int operator != (const BoxT *p) const { return !(*this == p); } + + const ObjectT& operator *() const { return *(ptr->object_ptr()); } + ObjectT& operator *() { return *(ptr->object_ptr()); } + const ObjectT* operator ->() const { return (ptr->object_ptr()); } + ObjectT* operator ->() { return (ptr->object_ptr()); } + + friend int operator == (const EST_THandle< BoxT, ObjectT > &a, const EST_THandle< BoxT, ObjectT > & b) + {return a.ptr==b.ptr; } + friend int operator != (const EST_THandle< BoxT, ObjectT > &a, const EST_THandle< BoxT, ObjectT > & b) + { return !( a==b ); } + + friend ostream & operator << (ostream &s, const EST_THandle< BoxT, ObjectT > &a) + { return s << "HANDLE"; } +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_THash.h b/aeneas/cfw/speech_tools/EST_THash.h new file mode 100644 index 00000000..c093390f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_THash.h @@ -0,0 +1,333 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /************************************************************************/ + +#ifndef __EST_THASH_H__ +#define __EST_THASH_H__ + +#include + +using namespace std; + +#include "EST_String.h" +#include "EST_system.h" +#include "EST_bool.h" +#include "EST_TIterator.h" + +#include "instantiate/EST_THashI.h" +#include "instantiate/EST_TStringHashI.h" + +/**@name Hash Tables + * + * @author Richard Caley + * @version $Id: EST_THash.h,v 1.6 2004/09/29 08:24:17 robert Exp $ + */ +//@{ + +/** This is just a convenient place to put some useful hash functions. + */ +class EST_HashFunctions { +public: + /// A generally useful hash function. + static unsigned int DefaultHash(const void *data, size_t size, unsigned int n); + + /// A hash function for strings. + static unsigned int StringHash(const EST_String &key, unsigned int size); +}; + +template class EST_THash; + +/** This class is used in hash tables to hold a key value pair. + * Not much to say beyond that. + */ +template +class EST_Hash_Pair { +public: + /// The key + K k; + /// The value + V v; + +private: + /// Pointer used to chain entries into buckets. + EST_Hash_Pair *next; + + /// The hash table must be a friend so it can see the pointer. + friend class EST_THash; +}; + +/** An open hash table. The number of buckets should be set to allow + * enough space that there are relatively few entries per bucket on + * average. + */ + +template +class EST_THash : protected EST_HashFunctions { + +private: + /// Something to return when there is no entry in the table. + static V Dummy_Value; + static K Dummy_Key; + + /// Total number of entries. + unsigned int p_num_entries; + + /// Number of buckets. + unsigned int p_num_buckets; + + /// Pointer to an array of p_num_bucketsbuckets. + EST_Hash_Pair **p_buckets; + + /// The hash function to use on this table. + unsigned int (*p_hash_function)(const K &key, unsigned int size); + +public: + /** Create a table with the given number of buckets. Optionally setting + * a custom hash function. + */ + EST_THash(int size, + unsigned int (*hash_function)(const K &key, unsigned int size)= NULL); + + /// Create a copy + EST_THash(const EST_THash &from); + + /// Destroy the table. + ~EST_THash(void); + + /// Empty the table. + void clear(void); + + /// Return the total number of entries in the table. + unsigned int num_entries(void) const + { return p_num_entries; }; + + /// Does the key have an entry? + int present(const K &key) const; + + /** Return the value associated with the key. + * found is set to whether such an entry was found. + */ + V &val(const K &key, int &found) const; + + /// Return the value associated with the key. + V &val(const K &key) const {int x; return val(key, x); } + + const K &key(const V &val, int &found) const; + const K &key(const V &val) const {int x; return key(val, x); } + + /// Copy all entries + void copy(const EST_THash &from); + + /// Apply func to each entry in the table. + void map(void (*func)(K&, V&)); + + /// Add an entry to the table. + int add_item(const K &key, const V &value, int no_search = 0); + + /// Remove an entry from the table. + int remove_item(const K &rkey, int quiet = 0); + + /// Assignment is a copy operation + EST_THash &operator = (const EST_THash &from); + + /// Print the table to stream in a human readable format. + void dump(ostream &stream, int all=0); + + /**@name Pair Iteration + * + * This iterator steps through the table returning key-value pairs. + */ + //@{ +protected: + /** A position in the table is given by a bucket number and a + * pointer into the bucket. + */ + // struct IPointer{ unsigned int b; EST_Hash_Pair *p; }; + struct IPointer_s{ unsigned int b; EST_Hash_Pair *p; }; + + typedef struct IPointer_s IPointer; + + /// Shift to point at something. + void skip_blank(IPointer &ip) const + { + while (ip.p==NULL && ip.bnext; + skip_blank(ip); + } + + /// We are at the end if the pointer ever becomes NULL + bool points_to_something(const IPointer &ip) const { return ip.b &points_at(const IPointer &ip) { return *(ip.p); } + + /// The iterator must be a friend to access this private interface. + friend class EST_TStructIterator< EST_THash, IPointer, EST_Hash_Pair >; + friend class EST_TRwStructIterator< EST_THash, IPointer, EST_Hash_Pair >; + friend class EST_TIterator< EST_THash, IPointer, EST_Hash_Pair >; + friend class EST_TRwIterator< EST_THash, IPointer, EST_Hash_Pair >; + +public: + /// An entry returned by the iterator is a key value pair. + typedef EST_Hash_Pair Entry; + + /// Give the iterator a sensible name. + typedef EST_TStructIterator< EST_THash, IPointer, EST_Hash_Pair > Entries; + typedef EST_TRwStructIterator< EST_THash, IPointer, EST_Hash_Pair > RwEntries; + //@} + + /**@name Key Iteration + * + * This iterator steps through the table returning just keys. + */ + //@{ +protected: + /** A position in the table is given by a bucket number and a + * pointer into the bucket. + */ + struct IPointer_k_s { unsigned int b; EST_Hash_Pair *p; }; + + typedef struct IPointer_k_s IPointer_k; + + /// Shift to point at something. + void skip_blank(IPointer_k &ip) const + { + while (ip.p==NULL && ip.bnext; + skip_blank(ip); + } + + /// We are at the end if the pointer ever becomes NULL + bool points_to_something(const IPointer_k &ip) const { return ip.bk; } + + /// The iterator must be a friend to access this private interface. + friend class EST_TIterator< EST_THash, IPointer_k, K >; + friend class EST_TRwIterator< EST_THash, IPointer_k, K >; + +public: + /// An entry returned by this iterator is just a key. + typedef K KeyEntry; + + /// Give the iterator a sensible name. + typedef EST_TIterator< EST_THash, IPointer_k, K > KeyEntries; + typedef EST_TRwIterator< EST_THash, IPointer_k, K > KeyRwEntries; + //@} + +}; + +/** A specialised hash table for when the key is an EST_String. + * + * This is just a version of EST_THash which + * has a different default hash function. + */ + +template +class EST_TStringHash : public EST_THash { +public: + + /// Create a string hash table of size buckets. + EST_TStringHash(int size) : EST_THash(size, EST_HashFunctions::StringHash) {}; + + /// An entry returned by the iterator is a key value pair. + typedef EST_Hash_Pair Entry; + +/* struct IPointer_s{ unsigned int b; Entry *p; }; + typedef struct IPointer_s IPointer; */ + + // Convince GCC that the IPointer we're going to use is a typename + typedef typename EST_THash::IPointer TN_IPointer; + + /// Give the iterator a sensible name. + typedef EST_TStructIterator< EST_THash, typename EST_THash::IPointer, + EST_Hash_Pair > Entries; + + typedef EST_TRwStructIterator< EST_THash, typename EST_THash::IPointer, + EST_Hash_Pair > RwEntries; + //@} + + typedef EST_String KeyEntry; + +/* struct IPointer_k_s { unsigned int b; EST_Hash_Pair *p; }; + typedef struct IPointer_k_s IPointer_k; */ + + /// Give the iterator a sensible name. + + // Convince GCC that the IPointer_k we're going to use is a typename + typedef typename EST_THash::IPointer_k TN_IPointer_k; + + typedef EST_TIterator< EST_THash, typename EST_THash::IPointer_k, + EST_String > KeyEntries; + typedef EST_TRwIterator< EST_THash, typename EST_THash::IPointer_k, + EST_String > KeyRwEntries; +}; + + +/** The default hash function used by EST_THash + */ +inline static unsigned int DefaultHashFunction(const void *data, size_t size, unsigned int n) +{ + unsigned int x=0; + const char *p = (const char *)data; + for(; size>0 ; p++, size--) + x = ((x+*p)*33) % n; + return x; +} + +//@} +#endif diff --git a/aeneas/cfw/speech_tools/EST_TIterator.h b/aeneas/cfw/speech_tools/EST_TIterator.h new file mode 100644 index 00000000..832e8e37 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TIterator.h @@ -0,0 +1,298 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +#ifndef __EST_TITERATOR_H__ +#define __EST_TITERATOR_H__ + +/** Template class defining interface to an iterator, i.e an object + * which returns elements from a structure one at a time. + * + * This is template is usually hidden in the declaration of the + * container classes with a typedef for Entries providing a more + * convenient name for the iterator. However the interface is that + * defined here. + * + * We support two interfaces, a pointer like interface similar to + * specialised iteration code elsewhere in the speech tools library + * and to the iterators in the C++ standard template library and an + * interface similar to that of Enumerations in Java. + * + * + * MyContainer::Entries them; + * + * for(them.begin(container); them; them++) + * { + * MyContainer::Entry &it = *them; + * // Do Something With it + * } + * + * + * MyContainer::Entries them; + * + * them.begin(container); + * while (them.has_more_entries()) + * { + * MyContainer::Entry &it = them.next_entry(); + * // Do Something With it + * } + * + * @author Richard Caley + * @version $Id: EST_TIterator.h,v 1.7 2013/04/13 14:17:11 awb Exp $ + */ + +template + class EST_TStructIterator; +template + class EST_TRwIterator; +template + class EST_TRwStructIterator; + +template +class EST_TIterator +{ +protected: + /// The container we are looking at. + Container *cont; + + /// Position in the structure. May or may not be useful. + unsigned int pos; + + /** Structure defined by the container class which contains the + * current state of the iteration. + */ + IPointer pointer; + +public: + /// Name for an iterator like this + typedef EST_TIterator Iter; + + /// Create an iterator not associated with any specific container. + EST_TIterator() {cont=NULL;} + + /// Create an iterator ready to run over the given container. + EST_TIterator(const Container &over) + { begin(over); } + + /// Copy an iterator by assignment + Iter &operator = (const Iter &orig) + { cont=orig.cont; pos=orig.pos; pointer=orig.pointer; return *this;} + + /// Assigning a container to an iterator sets it ready to start. + Iter &operator = (const Container &over) + { begin(over); return *this;} + + /// Set the iterator ready to run over this container. + void begin(const Container &over) + {cont=((Container *)(void *)&over); beginning();} + + /// Reset to the start of the container. + void beginning() + {if (cont) cont->point_to_first(pointer); pos=0;} + + /**@name End Tests + */ + //@{ + /// True if there are more elements to look at. + bool has_more_elements() const + {return cont && cont->points_to_something(pointer);} + + /// True when there are no more. + bool at_end() const + {return !has_more_elements();} + + /** Viewing the iterator as an integer (for instance in a test) + * sees a non-zero value iff there are elements still to look at. + */ + operator int() const + {return has_more_elements();} + //@} + + /**@name Moving Forward + */ + //@{ + /// Next moves to the next entry. + void next() + {cont->move_pointer_forwards(pointer); pos++;} + + /// The increment operator does the same as next. + Iter &operator ++() + {next(); return *this;} + Iter operator ++(int dummy) + { + (void)dummy; + Iter old =*this; + next(); + return old; + } + //@} + + /**@name Access + */ + //@{ + /// Return the element currently pointed to. + const Entry& current() const + {return cont->points_at(pointer);} + + /// The * operator returns the current element. + const Entry &operator *() const + {return current();} + +#if 0 + // This only works for some Entry types. + const Entry *operator ->() const + {return ¤t();} +#endif + + /// Return the current element and move the pointer forwards. + const Entry& next_element() + { + const Entry &it = cont->points_at(pointer); + cont->move_pointer_forwards(pointer); + return it; + } + + /// Return the current position + + unsigned int n() const { return pos; } + //@} + + friend class EST_TStructIterator ; + friend class EST_TRwIterator ; + friend class EST_TRwStructIterator ; + +}; + +template +class EST_TStructIterator + : public EST_TIterator +{ +public: + + typedef EST_TIterator Iter; + + /// Create an iterator not associated with any specific container. + EST_TStructIterator() {this->cont=NULL;} + + /// Copy an iterator by assignment + Iter &operator = (const Iter &orig) + { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;} + + /// Create an iterator ready to run over the given container. + EST_TStructIterator(const Container &over) + { this->begin(over); } + + const Entry *operator ->() const + {return &this->current();} +}; + +template +class EST_TRwIterator + : public EST_TIterator +{ +private: + /// Can't access constant containers this way. + // EST_TRwIterator(const Container &over) { (void) over; } + + /// Can't access constant containers this way. + // void begin(const Container &over) { (void) over; } + +public: + + typedef EST_TRwIterator Iter; + + /// Create an iterator not associated with any specific container. + EST_TRwIterator() {this->cont=NULL;} + + /// Copy an iterator by assignment + Iter &operator = (const Iter &orig) + { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;} + + /// Create an iterator ready to run over the given container. + EST_TRwIterator(Container &over) + { begin(over); } + + /// Set the iterator ready to run over this container. + void begin(Container &over) + {this->cont=&over; this->beginning();} + + /**@name Access + */ + //@{ + /// Return the element currently pointed to. + Entry& current() const + {return this->cont->points_at(this->pointer);} + + /// The * operator returns the current element. + Entry &operator *() const + {return current();} + +#if 0 + Entry *operator ->() const + {return ¤t();} +#endif + + /// Return the current element and move the pointer forwards. + Entry& next_element() + { + Entry &it = this->cont->points_at(this->pointer); + this->cont->move_pointer_forwards(this->pointer); + return it; + } + + //@} +}; + +template +class EST_TRwStructIterator + : public EST_TRwIterator +{ +public: + + typedef EST_TRwStructIterator Iter; + + /// Create an iterator not associated with any specific container. + EST_TRwStructIterator() {this->cont=NULL;} + + /// Copy an iterator by assignment + Iter &operator = (const Iter &orig) + { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;} + + /// Create an iterator ready to run over the given container. + EST_TRwStructIterator(Container &over) + { this->begin(over); } + + Entry *operator ->() const + {return &this->current();} +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TKVL.h b/aeneas/cfw/speech_tools/EST_TKVL.h new file mode 100644 index 00000000..4b9a6d5d --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TKVL.h @@ -0,0 +1,204 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : January 1995 */ +/*-----------------------------------------------------------------------*/ +/* Key/Value list template class */ +/* */ +/*=======================================================================*/ +#ifndef __EST_TKVL_H__ +#define __EST_TKVL_H__ + +#include + +using namespace std; + +#include "EST_TList.h" +#include "instantiate/EST_TKVLI.h" +#include "EST_TIterator.h" + +class EST_String; + + +/** Templated Key-Value Item. Serves as the items in the list of the +EST_TKVL class. */ +template class EST_TKVI { + public: + K k; + V v; + + inline bool operator==(const EST_TKVI &i){ + return( (i.k == k) && (i.v == v) ); + } + + friend ostream& operator << (ostream& s, EST_TKVI const &i) + { return s << i.k << "\t" << i.v << "\n"; } +}; + + +/** Templated Key-Value list. Objects of type EST_TKVL contain lists which +are accessed by a key of type {\bf K}, which returns a value of type +{\bf V}. */ +template class EST_TKVL { + private: + EST_Litem *find_pair_key(const K &key) const; + EST_Litem *find_pair_val(const V &val) const; + public: + /**@name Constructor functions */ + //@{ + /// default constructor + EST_TKVL() {;} + /// copy constructor + EST_TKVL(const EST_TKVL &kv); + //@} + + /// default value, returned when there is no such entry. + static V *default_val; + + /// default value, returned when there is no such entry. + static K *default_key; + + /// Linked list of key-val pairs. Don't use + /// this as it will be made private in the future + EST_TList< EST_TKVI > list; + + /// number of key value pairs in list + const int length() const {return list.length();} + + /// Return First key value pair in list + EST_Litem * head() const {return list.head();}; + + /// Empty list. + void clear(); + + /**@name Access functions. + */ + //@{ + /// return value according to key (const) + const V &val(const K &rkey, bool m=0) const; + /// return value according to key (non-const) + V &val(const K &rkey, bool m=0); + /// return value according to ptr + const V &val(EST_Litem *ptr, bool m=0) const; + /// return value according to ptr + V &val(EST_Litem *ptr, bool m=0); + /// value or default + const V &val_def(const K &rkey, const V &def) const; + + /// find key, reference by ptr + const K &key(EST_Litem *ptr, int m=1) const; + /// find key, reference by ptr + K &key(EST_Litem *ptr, int m=1); + + /// return first matching key, referenced by val + const K &key(const V &v, int m=1) const; + + /** change key-val pair. If no corresponding entry is present, add + to end of list. + */ + int change_val(const K &rkey,const V &rval); + /** change key-val pair. If no corresponding entry is present, add + to end of list.*/ + int change_val(EST_Litem *ptr,const V &rval); // change key-val pair. + /// change name of key pair. + int change_key(EST_Litem *ptr,const K &rkey); + + /// add key-val pair to list + int add_item(const K &rkey,const V &rval, int no_search = 0); + + /// remove key and val pair from list + int remove_item(const K &rkey, int quiet = 0); + + //@} + + /// Returns true if key is present. + const int present(const K &rkey) const; + + /// apply function to each pair + void map(void (*func)(K&, V&)); + + friend ostream& operator << (ostream& s, EST_TKVL const &l) + {EST_Litem *p; + for (p = l.list.head(); p ; p = p->next()) + s << l.list(p).k << "\t" << l.list(p).v << endl; + return s; + } + + /// full copy of KV list. + EST_TKVL & operator = (const EST_TKVL &kv); + /// add kv after existing list. + EST_TKVL & operator += (const EST_TKVL &kv); + /// make new concatenated list + EST_TKVL operator + (const EST_TKVL &kv); + + // Iteration support + +protected: + struct IPointer { EST_Litem *p; }; + + void point_to_first(IPointer &ip) const { ip.p = list.head(); } + void move_pointer_forwards(IPointer &ip) const { ip.p = ip.p->next(); } + bool points_to_something(const IPointer &ip) const { return ip.p != NULL; } + EST_TKVI &points_at(const IPointer &ip) { return list(ip.p); } + + friend class EST_TIterator< EST_TKVL, IPointer, EST_TKVI >; + friend class EST_TStructIterator< EST_TKVL, IPointer, EST_TKVI >; + friend class EST_TRwIterator< EST_TKVL, IPointer, EST_TKVI >; + friend class EST_TRwStructIterator< EST_TKVL, IPointer, EST_TKVI >; + +public: + typedef EST_TKVI Entry; + typedef EST_TStructIterator< EST_TKVL, IPointer, Entry> Entries; + typedef EST_TRwStructIterator< EST_TKVL, IPointer, Entry> RwEntries; + + // Iteration support + +protected: + struct IPointer_k { EST_Litem *p; }; + + void point_to_first(IPointer_k &ip) const { ip.p = list.head(); } + void move_pointer_forwards(IPointer_k &ip) const { ip.p = ip.p->next(); } + bool points_to_something(const IPointer_k &ip) const { return ip.p != NULL; } + K &points_at(const IPointer_k &ip) { return list(ip.p).k; } + + friend class EST_TIterator< EST_TKVL, IPointer_k, K >; + friend class EST_TRwIterator< EST_TKVL, IPointer_k, K >; + +public: + typedef K KeyEntry; + typedef EST_TIterator< EST_TKVL, IPointer_k, KeyEntry> KeyEntries; + typedef EST_TRwIterator< EST_TKVL, IPointer_k, KeyEntry> KeyRwEntries; + +}; + +#endif // __KVL_H__ diff --git a/aeneas/cfw/speech_tools/EST_TList.h b/aeneas/cfw/speech_tools/EST_TList.h new file mode 100644 index 00000000..a5fcf6ef --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TList.h @@ -0,0 +1,328 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1995 */ + /* --------------------------------------------------------------------- */ + /* Double linked list class */ + /* */ + /* Modified by RJC, 21/7/97. Now much of the working code is in the */ + /* UList class, this template class provides a type safe front end to */ + /* the untyped list. */ + /* */ + /*************************************************************************/ + +#ifndef __Tlist_H__ +#define __Tlist_H__ + +#include + +using namespace std; + +#include "EST_common.h" +#include "EST_UList.h" +#include "EST_TSortable.h" +#include "EST_TIterator.h" + +#include "instantiate/EST_TListI.h" + +class EST_String; + +template class EST_TList; + +template class EST_TItem : public EST_UItem { +private: + static void *operator new(size_t not_used, void *place) + {(void)not_used; return place;} + static void *operator new(size_t size) + {void *p; + p = (void *)walloc(char,size); + return p;} + static void operator delete(void *p) + { wfree(p);} + + static EST_TItem *s_free; + static unsigned int s_nfree; + static unsigned int s_maxFree; + +protected: + static EST_TItem *make(const T &val); + static void release(EST_TItem *it); + + friend class EST_TList; + +public: + T val; + + EST_TItem(const T &v) : val(v) + { init(); }; + EST_TItem() + { init();}; +}; + +// pretty name + +typedef EST_UItem EST_Litem; + +/** + +A Template doubly linked list class. This class contains doubly linked +lists of a type denoted by {\tt T}. A pointer of type \Ref{EST_Litem} +is used to access items in the list. The class supports a variety of +ways of adding, removing and accessing items in the list. For examples +of how to operate lists, see \Ref{list_example}. + +Iteration through the list is performed using a pointer of type +\Ref{EST_Litem}. See \Ref{Iteration} for example code. + +*/ + +template class EST_TList : public EST_UList +{ + private: + void copy_items(const EST_TList &l); + public: + void init() { EST_UList::init(); }; + static void free_item(EST_UItem *item); + + /**@name Constructor functions */ + //@{ + /// default constructor + EST_TList() { }; + /// copy constructor + EST_TList(const EST_TList &l); + ~ EST_TList() { clear_and_free(free_item); } + //@} + + /**@name Access functions for reading and writing items. + See \Ref{EST_TList_Accessing} for examples.*/ + + //@{ + + /** return the value associated with the EST_Litem pointer. This + has the same functionality as the overloaded () operator. + */ + T &item(const EST_Litem *p) + { return ((EST_TItem *)p) -> val; }; + /** return a const value associated with the EST_Litem pointer.*/ + const T &item(const EST_Litem *p) const + { return ((EST_TItem *)p) -> val; }; + /// return the Nth value + T &nth(int n) + { return item(nth_pointer(n)); }; + /// return a const Nth value + const T &nth(int n) const + { return item(nth_pointer(n)); }; + + /// return const reference to first item in list + const T &first() const + { return item(head()); }; + /// return const reference to last item in list + const T &last() const + { return item(tail()); }; + /** return reference to first item in list + * @see last + */ + T &first() + { return item(head()); }; + /// return reference to last item in list + T &last() + { return item(tail()); }; + + /// return const reference to item in list pointed to by {\tt ptr} + const T &operator () (const EST_Litem *ptr) const + { return item(ptr); }; + /// return non-const reference to item in list pointed to by {\tt ptr} + T &operator () (const EST_Litem *ptr) + { return item(ptr); }; + + //@} + + /**@name Removing items in a list. + more. + */ + //@{ + /** remove item pointed to by {\tt ptr}, return pointer to previous item. + See \Ref{Removing} for example code.*/ + EST_Litem *remove(EST_Litem *ptr) + { return EST_UList::remove(ptr, free_item); }; + + /// remove nth item, return pointer to previous item + EST_Litem *remove_nth(int n) + { return EST_UList::remove(n, free_item); }; + + //@} + + + /**@name Adding items to a list. + In all cases, a complete copy of + the item is made and added to the list. See \Ref{Addition} for examples. + */ + //@{ + /// add item onto end of list + void append(const T &item) + { EST_UList::append(EST_TItem::make(item)); }; + /// add item onto start of list + void prepend(const T &item) + { EST_UList::prepend(EST_TItem::make(item)); }; + + /** add {\tt item} after position given by {\tt ptr}, return pointer + to added item. */ + + EST_Litem *insert_after(EST_Litem *ptr, const T &item) + { return EST_UList::insert_after(ptr, EST_TItem::make(item)); }; + + /** add {\tt item} before position given by {\tt ptr}, return + pointer to added item. */ + + EST_Litem *insert_before(EST_Litem *ptr, const T &item) + { return EST_UList::insert_before(ptr, EST_TItem::make(item)); }; + + //@} + + /**@name Exchange */ + //@{ + /// exchange 1 + void exchange(EST_Litem *a, EST_Litem *b) + { EST_UList::exchange(a, b); }; + /// exchange 2 + void exchange(int i, int j) + { EST_UList::exchange(i,j); }; + /// exchange 3 + static void exchange_contents(EST_Litem *a, EST_Litem *b); + //@} + + /**@name General functions */ + //@{ + /// make full copy of list + EST_TList &operator=(const EST_TList &a); + /// Add list onto end of existing list + EST_TList &operator +=(const EST_TList &a); + + /// print list + friend ostream& operator << (ostream &st, EST_TList const &list) { + EST_Litem *ptr; + for (ptr = list.head(); ptr != 0; ptr = ptr->next()) + st << list.item(ptr) << " "; + return st; + } + + /// remove all items in list + void clear(void) + { clear_and_free(free_item); }; + //@} + + // Iteration support + +protected: + struct IPointer { EST_Litem *p; }; + + void point_to_first(IPointer &ip) const { ip.p = head(); } + void move_pointer_forwards(IPointer &ip) const { ip.p = ip.p->next(); } + bool points_to_something(const IPointer &ip) const { return ip.p != NULL; } + T &points_at(const IPointer &ip) { return item(ip.p); } + + friend class EST_TIterator< EST_TList, IPointer, T >; + friend class EST_TRwIterator< EST_TList, IPointer, T >; + +public: + typedef T Entry; + typedef EST_TIterator< EST_TList, IPointer, T > Entries; + typedef EST_TRwIterator< EST_TList, IPointer, T > RwEntries; + +}; + + +template +bool operator==(const EST_TList &a, const EST_TList &b) +{ + return EST_UList::operator_eq(a, b, EST_TSortable::items_eq); +} + +template +bool operator!=(const EST_TList &a, const EST_TList &b) +{ + return !(a==b); +} + +template +int index(EST_TList &l, T& val, bool (*eq)(const EST_UItem *, const EST_UItem *) = NULL) +{ + EST_TItem item(val); + return EST_UList::index(l, item, eq?eq:EST_TSortable::items_eq); +} + +template +void sort(EST_TList &a, bool (*gt)(const EST_UItem *, const EST_UItem *) = NULL) +{ + EST_UList::sort(a, gt?gt:EST_TSortable::items_gt); +} + +template +void ptr_sort(EST_TList &a) +{ + EST_UList::sort(a, EST_TSortable::items_gt); +} + +template +void qsort(EST_TList &a, bool (*gt)(const EST_UItem *, const EST_UItem *) = NULL) +{ + EST_UList::qsort(a, gt?gt:EST_TSortable::items_gt, EST_TList::exchange_contents); +} + +template +void ptr_qsort(EST_TList &a) +{ + EST_UList::qsort(a, EST_TSortable::items_gt, EST_TList::exchange_contents); +} + +template +void sort_unique(EST_TList &l) +{ + EST_UList::sort_unique(l, + EST_TSortable::items_eq, + EST_TSortable::items_gt, + EST_TList::free_item); +} + +template +void merge_sort_unique(EST_TList &l, EST_TList &m) +{ + EST_UList::merge_sort_unique(l, m, + EST_TSortable::items_eq, + EST_TSortable::items_gt, + EST_TList::free_item); +} + +template +const char *error_name(EST_TList val) { (void)val; return "<>"; } + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TMatrix.h b/aeneas/cfw/speech_tools/EST_TMatrix.h new file mode 100644 index 00000000..238eaef1 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TMatrix.h @@ -0,0 +1,328 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Rewritten : Richard Caley */ + /* --------------------------------------------------------------------- */ + /* Matrix class */ + /* */ + /*************************************************************************/ + +#ifndef __TMatrix_H__ +#define __TMatrix_H__ + +#include + +using namespace std; + +#include "EST_rw_status.h" +#include "EST_TVector.h" +#include "instantiate/EST_TMatrixI.h" + +/* When set bounds checks (safe but slow) are done on matrix access */ +#ifndef TMATRIX_BOUNDS_CHECKING +# define TMATRIX_BOUNDS_CHECKING 0 +#endif + +#if TMATRIX_BOUNDS_CHECKING +#define A_CHECK a_check +#else +#define A_CHECK a_no_check +#endif + +#define INLINE inline + +/* This doesn't work as I thought so I have disabled it for now. + */ + +#if defined(__GNUC__) && 0 +# define mx_move_pointer(P, TY, STEP, N) \ + ((TY *)\ + ((void *) (((char (*) [sizeof(TY)*STEP])P) + N) ) \ + ) +# define fast_a_m_gcc(R,C) \ + ( * mx_move_pointer(mx_move_pointer(p_memory,T,p_column_step,C),T,p_row_step,R)) +# define fast_a_m_x(R,C) (fast_a_m_gcc(R,C)) +#else +# define fast_a_m_x(R,C) (fast_a_m(R,C)) +#endif + + + +/** Template Matrix class. + * + * This is an extension of the EST_TVector class to two dimensions. + * + * @see matrix_example + * @see EST_TVector + */ + +template +class EST_TMatrix : public EST_TVector +{ + +protected: + /// Visible shape + unsigned int p_num_rows; + + /// How to access the memory + unsigned int p_row_step; + + INLINE unsigned int mcell_pos(int r, int c, + int rs, int cs) const + { return (rs==1?r:(r*rs)) + (cs==1?c:(c*cs));} + + + INLINE unsigned int mcell_pos(int r, int c) const + { + + return mcell_pos(r, c, + this->p_row_step, this->p_column_step); + } + + INLINE unsigned int mcell_pos_1(int r, int c) const + { + + (void)r; + return c; + } + + /// quick method for returning {\tt x[m][n]} + INLINE const T &fast_a_m(int r, int c) const + { return this->p_memory[mcell_pos(r,c)]; } + INLINE T &fast_a_m(int r, int c) + { return this->p_memory[mcell_pos(r,c)]; } + + INLINE const T &fast_a_1(int r, int c) const + { return this->p_memory[mcell_pos_1(r,c)]; } + INLINE T &fast_a_1(int r, int c) + { return this->p_memory[mcell_pos_1(r,c)]; } + + + /// Get and set values from array + void set_values(const T *data, + int r_step, int c_step, + int start_r, int num_r, + int start_c, int num_c + ); + void get_values(T *data, + int r_step, int c_step, + int start_r, int num_r, + int start_c, int num_c + ) const; + + /// private resize and copy function. + void copy(const EST_TMatrix &a); + /// just copy data, no resizing, no size check. + void copy_data(const EST_TMatrix &a); + + /// resize the memory and reset the bounds, but don't set values. + void just_resize(int new_rows, int new_cols, T** old_vals); + + /// sets data and length to default values (0 in both cases). + void default_vals(); +public: + + ///default constructor + EST_TMatrix(); + + /// copy constructor + EST_TMatrix(const EST_TMatrix &m); + + /// "size" constructor + EST_TMatrix(int rows, int cols); + + /// construct from memory supplied by caller + EST_TMatrix(int rows, int cols, + T *memory, int offset=0, int free_when_destroyed=0); + + /// EST_TMatrix + + ~EST_TMatrix(); + + /**@name access + * Basic access methods for matrices. + */ + //@{ + + /// return number of rows + int num_rows() const {return this->p_num_rows;} + /// return number of columns + int num_columns() const {return this->p_num_columns;} + + /// const access with no bounds check, care recommend + INLINE const T &a_no_check(int row, int col) const + { return fast_a_m_x(row,col); } + /// access with no bounds check, care recommend + INLINE T &a_no_check(int row, int col) + { return fast_a_m_x(row,col); } + + INLINE const T &a_no_check_1(int row, int col) const { return fast_a_1(row,col); } + INLINE T &a_no_check_1(int row, int col) { return fast_a_1(row,col); } + + /// const element access function + const T &a_check(int row, int col) const; + /// non-const element access function + T &a_check(int row, int col); + + const T &a(int row, int col) const { return A_CHECK(row,col); } + T &a(int row, int col) { return A_CHECK(row,col); } + + /// const element access operator + const T &operator () (int row, int col) const { return a(row,col); } + /// non-const element access operator + T &operator () (int row, int col) { return a(row,col); } + + //@} + + bool have_rows_before(int n) const; + bool have_columns_before(int n) const; + + /** resize matrix. If {\tt set=1}, then the current values in + the matrix are preserved up to the new size {\tt n}. If the + new size exceeds the old size, the rest of the matrix is + filled with the {\tt def_val} + */ + void resize(int rows, int cols, int set=1); + + /// fill matrix with value v + void fill(const T &v); + void fill() { fill(*this->def_val); } + + /// assignment operator + EST_TMatrix &operator=(const EST_TMatrix &s); + + /// The two versions of what might have been operator += + EST_TMatrix &add_rows(const EST_TMatrix &s); + EST_TMatrix &add_columns(const EST_TMatrix &s); + + /**@name Sub-Matrix/Vector Extraction + * + * All of these return matrices and vectors which share + * memory with the original, so altering values them alters + * the original. + */ + //@{ + + /// Make the vector {\tt rv} a window onto row {\tt r} + void row(EST_TVector &rv, int r, int start_c=0, int len=-1); + /// Make the vector {\tt cv} a window onto column {\tt c} + void column(EST_TVector &cv, int c, int start_r=0, int len=-1); + /// Make the matrix {\tt sm} a window into this matrix. + void sub_matrix(EST_TMatrix &sm, + int r=0, int numr=EST_ALL, + int c=0, int numc=EST_ALL); + //@} + + /**@name Copy in and out + * Copy data between buffers and the matrix. + */ + //@{ + /** Copy row {\tt r} of matrix to {\tt buf}. {\tt buf} + should be pre-malloced to the correct size. + */ + void copy_row(int r, T *buf, int offset=0, int num=-1) const; + + /** Copy row r of matrix to + buf. buf should be + pre-malloced to the correct size. */ + + void copy_row(int r, EST_TVector &t, int offset=0, int num=-1) const; + + /** Copy column {\tt c} of matrix to {\tt buf}. {\tt buf} + should be pre-malloced to the correct size. + */ + void copy_column(int c, T *buf, int offset=0, int num=-1) const; + + /** Copy column c of matrix to + buf. buf should + be pre-malloced to the correct size. */ + + void copy_column(int c, EST_TVector &t, int offset=0, int num=-1)const; + + /** Copy buf into row {\tt n} of matrix. + */ + void set_row(int n, const T *buf, int offset=0, int num=-1); + + void set_row(int n, const EST_TVector &t, int offset=0, int num=-1) + { set_row(n, t.memory(), offset, num); } + + void set_row(int r, + const EST_TMatrix &from, int from_r, int from_offset=0, + int offset=0, int num=-1); // set nth row + + + /** Copy buf into column {\tt n} of matrix. + */ + void set_column(int n, const T *buf, int offset=0, int num=-1); + + void set_column(int n, const EST_TVector &t, int offset=0, int num=-1) + { set_column(n, t.memory(), offset, num); } + + void set_column(int c, + const EST_TMatrix &from, int from_c, int from_offset=0, + int offset=0, int num=-1); // set nth column + + /** For when you absolutely have to have access to the memory. + */ + void set_memory(T *buffer, int offset, int rows, int columns, + int free_when_destroyed=0); + + //@} + + /**@name io + * Matrix file io. + */ + //@{ + /// load Matrix from file - Not currently implemented. + EST_read_status load(const class EST_String &filename); + /// save Matrix to file {\tt filename} + EST_write_status save(const class EST_String &filename) const; + + /// print matrix. + friend ostream& operator << (ostream &st,const EST_TMatrix &a) + {int i, j; + for (i = 0; i < a.num_rows(); ++i) { + for (j = 0; j < a.num_columns(); ++j) + st << a.a_no_check(i, j) << " "; st << endl; + } + return st; + } + //@} + +}; + +#undef A_CHECK + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_TNamedEnum.h b/aeneas/cfw/speech_tools/EST_TNamedEnum.h new file mode 100644 index 00000000..f1e5aa16 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TNamedEnum.h @@ -0,0 +1,169 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Feb 28 1997 */ + /************************************************************************/ + /* */ + /* A template class which allows names (const char *s) to be */ + /* associated with enums, providing conversion. */ + /* */ + /* EST_TValuesEnum is the obvious generalisation to associating */ + /* things other than const char * with each value. */ + /* */ + /* EST_T{Named/Valued}EnumI can include extra information for each */ + /* enum element. */ + /* */ + /* This should be rewritten as something other than linear search. At */ + /* least sort them. */ + /* */ + /************************************************************************/ + +#ifndef __EST_TNAMEDENUM_H__ +#define __EST_TNAMEDENUM_H__ + +#include + +using namespace std; + +#include "EST_String.h" +#include "EST_rw_status.h" + +#define NAMED_ENUM_MAX_SYNONYMS (10) + +// Used in the type of tables with no info field. + +typedef char NO_INFO; + +// struct used to define the mapping. + +template +struct EST_TValuedEnumDefinition { +public: + ENUM token; + VAL values[NAMED_ENUM_MAX_SYNONYMS]; + INFO info; +} ; + +// This is the most general case, a mapping from enum to some other type +// with extra info. + +template class EST_TValuedEnumI { + +protected: + int ndefinitions; + ENUM p_unknown_enum; + VAL p_unknown_value; + EST_TValuedEnumDefinition *definitions; + + virtual int eq_vals(VAL v1, VAL v2) const {return v1 == v2; }; + // This is only a void * because INFO can`t manage to get the + // parameter declaration in the definition past gcc with the actual type. + void initialise(const void *defs); + void initialise(const void *defs, ENUM (*conv)(const char *)); + void initialise(void) {ndefinitions=0; definitions=NULL;}; + void initialise(ENUM unknown_e, VAL unknown_v) {initialise(); p_unknown_enum=unknown_e; p_unknown_value = unknown_v;}; + +protected: + EST_TValuedEnumI(void) {initialise();}; + +public: + EST_TValuedEnumI(EST_TValuedEnumDefinition defs[]) + {initialise((const void *)defs); }; + EST_TValuedEnumI(EST_TValuedEnumDefinition defs[], ENUM (*conv)(const char *)) + {initialise((const void *)defs, conv); }; + virtual ~EST_TValuedEnumI(void); + + int n(void) const; + + ENUM token(VAL value) const; + ENUM token(int n) const { return nth_token(n); } + ENUM nth_token(int n) const; + VAL value(ENUM token, int n=0) const; + INFO &info(ENUM token) const; + + ENUM unknown_enum(void) const {return p_unknown_enum;}; + VAL unknown_value(void) const {return p_unknown_value;}; + int valid(ENUM token) const { return !eq_vals(value(token),p_unknown_value); }; +}; + +// This is a special case for names. This saves typing and also +// takes care of the fact that strings need their own compare function. + +template class EST_TNamedEnumI : public EST_TValuedEnumI { + +protected: + EST_TNamedEnumI(void) : EST_TValuedEnumI() {}; + int eq_vals(const char *v1, const char *v2) const {return strcmp(v1,v2) ==0; }; +public: + + EST_TNamedEnumI(EST_TValuedEnumDefinition defs[]) + {this->initialise((const void *)defs); }; + EST_TNamedEnumI(EST_TValuedEnumDefinition defs[], ENUM (*conv)(const char *)) + {this->initialise((const void *)defs, conv); }; + const char *name(ENUM tok, int n=0) const {return this->value(tok,n); }; + +}; + +// Now the simple cases with no extra information + +template class EST_TValuedEnum : public EST_TValuedEnumI { +public: + EST_TValuedEnum(EST_TValuedEnumDefinition defs[]) + {this->initialise((const void *)defs);}; + EST_TValuedEnum(EST_TValuedEnumDefinition defs[], ENUM (*conv)(const char *)) + {this->initialise((const void *)defs, conv);}; +}; + + +template class EST_TNamedEnum : public EST_TNamedEnumI { +private: + EST_read_status priv_load(EST_String name, EST_TNamedEnum *definitive); + EST_write_status priv_save(EST_String name, EST_TNamedEnum *definitive, char quote) const; +public: + EST_TNamedEnum(ENUM undef_e, const char *undef_n = NULL) + {this->initialise(undef_e, undef_n);}; + EST_TNamedEnum(EST_TValuedEnumDefinition defs[]) + {this->initialise((const void *)defs);}; + EST_TNamedEnum(EST_TValuedEnumDefinition defs[], ENUM (*conv)(const char *)) + {this->initialise((const void *)defs, conv);}; + + EST_read_status load(EST_String name) { return priv_load(name, NULL); }; + EST_read_status load(EST_String name, EST_TNamedEnum &definitive) { return priv_load(name, &definitive); }; + EST_write_status save(EST_String name, char quote='"') const { return priv_save(name, NULL, quote); }; + EST_write_status save(EST_String name, EST_TNamedEnum &definitive, char quote='"') const { return priv_save(name, &definitive, quote); }; + +}; + +#include "instantiate/EST_TNamedEnumI.h" + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TSimpleMatrix.h b/aeneas/cfw/speech_tools/EST_TSimpleMatrix.h new file mode 100644 index 00000000..6485cf33 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TSimpleMatrix.h @@ -0,0 +1,74 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Oct 10 1997 */ + /* -------------------------------------------------------------------- */ + /* A subclass of TMatrix which copies using memcopy. This isn't */ + /* suitable for matrices of class objects which have to be copied */ + /* using a constructor or specialised assignment operator. */ + /* */ + /*************************************************************************/ + +#ifndef __EST_TSIMPLEMATRIX_H__ +#define __EST_TSIMPLEMATRIX_H__ + +#include "EST_TMatrix.h" +#include "instantiate/EST_TSimpleMatrixI.h" + +template +class EST_TSimpleMatrix : public EST_TMatrix { +protected: + + // just copy data, no resizing. + void copy_data(const EST_TSimpleMatrix &a); + +public: + /// default constructor + EST_TSimpleMatrix(void) : EST_TMatrix() {}; + /// size constructor + EST_TSimpleMatrix(int m, int n) : EST_TMatrix(m, n) {}; + /// copy constructor + EST_TSimpleMatrix(const EST_TSimpleMatrix &m); + + /// copy one matrix into another + void copy(const EST_TSimpleMatrix &a); + + /// resize matrix + void resize(int rows, int cols, int set=1); + + /// assignment operator + EST_TSimpleMatrix &operator=(const EST_TSimpleMatrix &s); +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TSimpleVector.h b/aeneas/cfw/speech_tools/EST_TSimpleVector.h new file mode 100644 index 00000000..d0e1e049 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TSimpleVector.h @@ -0,0 +1,83 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Oct 10 1997 */ + /* -------------------------------------------------------------------- */ + /* A subclass of TVector which copies using memcopy. This isn't */ + /* suitable for matrices of class objects which have to be copied */ + /* using a constructor or specialised assignment operator. */ + /* */ + /*************************************************************************/ + +#ifndef __EST_TSimpleVector_H__ +#define __EST_TSimpleVector_H__ + +#include "EST_TVector.h" +#include "instantiate/EST_TSimpleVectorI.h" + +/** A derived class from EST_TVector which is used for +containing simple types, such as float or int. +*/ +template class EST_TSimpleVector : public EST_TVector { +private: + /// private copy function + void copy(const EST_TSimpleVector &a); +public: + ///default constructor + EST_TSimpleVector() : EST_TVector() {}; + /// copy constructor + EST_TSimpleVector(const EST_TSimpleVector &v); + /// "size" constructor + EST_TSimpleVector(int n): EST_TVector(n) {}; + /// memory constructor + EST_TSimpleVector(int n, T* memory, int offset=0, + int free_when_destroyed=0): EST_TVector(n,memory) {}; + + /// resize vector + void resize(int n, int set=1); + + /// assignment operator + EST_TSimpleVector &operator=(const EST_TSimpleVector &s); + + void copy_section(T* dest, int offset=0, int num=-1) const; + void set_section(const T* src, int offset=0, int num=-1); + + /// Fill entire array with 0 bits. + void zero(void); + + // /// Fill vector with default value + // void empty(void) { if (*this->def_val == 0) zero(); else fill(*this->def_val); } +}; + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TSortable.h b/aeneas/cfw/speech_tools/EST_TSortable.h new file mode 100644 index 00000000..a7506eb5 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TSortable.h @@ -0,0 +1,58 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Thu Sep 4 1997 */ + /* -------------------------------------------------------------------- */ + /* A record which defines a sorting order. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TSORTABLE_H__ +#define __EST_TSORTABLE_H__ + +#include "EST_UList.h" +#include "instantiate/EST_TSortableI.h" + +template +class EST_TSortable +{ +public: + static bool items_eq(const EST_UItem *item1, const EST_UItem *item2); + static bool items_lt(const EST_UItem *item1, const EST_UItem *item2); + static bool items_gt(const EST_UItem *item1, const EST_UItem *item2); +}; + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_TTimeIndex.h b/aeneas/cfw/speech_tools/EST_TTimeIndex.h new file mode 100644 index 00000000..1746fb41 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TTimeIndex.h @@ -0,0 +1,90 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TTIMEINDEX_H__ +#define __EST_TTIMEINDEX_H__ + +#include + +/** A time index for a container. The container defines how to get an + * object and so on, this lets you find a point in the container not + * far before the entry you want. + * + * @author Richard Caley + * @version $Id: EST_TTimeIndex.h,v 1.3 2004/09/29 08:24:17 robert Exp $ + */ + +template +struct EST_TTI_Entry +{ + float t; + CONTAINER::Index i; +}; + +template +class EST_TTimeIndex +{ +public: + typedef CONTAINER::Index Index; + typedef EST_TTI_Entry Entry; + +protected: + CONTAINER *p_container; + EST_TVector p_entries; + float p_time_step; + + void initialise(); + +private: + void just_before(float time, void *in) const; + +public: + EST_TTimeIndex(); + EST_TTimeIndex(CONTAINER &c, int bunch=10); + + void index(CONTAINER &c, int bunch=10); + void just_before(float time, Index &in) const + { just_before(time, &in); } +}; + +template +extern int operator !=(const EST_TTI_Entry &e1, + const EST_TTI_Entry &e2); + +template +extern ostream& operator <<( + ostream &s, + const EST_TTI_Entry &e); +#endif + diff --git a/aeneas/cfw/speech_tools/EST_TVector.h b/aeneas/cfw/speech_tools/EST_TVector.h new file mode 100644 index 00000000..3658f098 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TVector.h @@ -0,0 +1,334 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* Vector class */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_TVector_H__ +#define __EST_TVector_H__ + +#include +using namespace std; +#include "EST_bool.h" +#include "EST_rw_status.h" + +#include "instantiate/EST_TVectorI.h" + +template class EST_TMatrix; +template class EST_TList; +class EST_String; + +/* A constants to make it clearer what is going on when we pass `-1' + * meaning `current size' or `all the rest' + */ + +extern const int EST_CURRENT; +extern const int EST_ALL; + +/* When set bounds checks (safe but slow) are done on vector access */ +#ifndef TVECTOR_BOUNDS_CHECKING +# define TVECTOR_BOUNDS_CHECKING 0 +#endif + +#if TVECTOR_BOUNDS_CHECKING +#define A_CHECK a_check +#else +#define A_CHECK a_no_check +#endif + +#define INLINE inline + +/* This doesn't work as I thought so I have disabled it for now. + */ + +#if defined(__GNUC__) && 0 +# define fast_a_v_gcc(C) \ + ( *((T *)\ + (((char (*) [sizeof(T)*p_column_step])p_memory) + (C))\ + )) +# define fast_a_v_x(C) (fast_a_v_gcc(C)) +#else +# define fast_a_v_x(C) (fast_a_v(C)) +#endif + + +/**@name Template vector + + This serves as a base class for a vector + of type T. This acts as a higher level + version of a normal C array as defined as float *x etc. + + The vector can be resized after declaration, access can be + with or without bounds checking. Round brackets denote read-only + access (for consts) while square brackets are for read-write access. + In both cases references are returned. + + The standard operators () and [] should be thought of as + having no bounds checking, though they may do so optionally + as a compile time option. The methods a_check and + a_nocheck provide explicit boundary checking/nonchecking, + both const and non-const versions are provided. + + Access through () and [] are guaranteed to be as fast as standard + C arrays (assuming a reasonable optimizing compiler). + + + EST_FVector x(10); + int i; + + for (i=0; i < x.length(); ++i) + x[i] = sqrt((float)i); + + x.resize(20); + + for (i=10; i < x.length(); ++i) + x[i] = sqrt((float)i); + + + + To instantiate a template for a a vector of type {FooBar} + + + #include "../base_class/EST_TVector.cc" + // If you want List to vector conversion (and defined a TList) + #include "../base_class/EST_Tvectlist.cc" + + template class EST_TVector; + template ostream& operator << + (ostream &st, const EST_TVector &v); + + + The EST library already has template vector instantiations for + int, float, double and + . Also types are defined for them + in as , , + and for ints, + floats, doubless and s respectively. + + * @see matrix_example */ +//@{ +template +class EST_TVector +{ + // protected: +public: + /** Pointer to the start of the vector. + * The start of allocated memory is p_memory-p_offset. + */ + T *p_memory; + + /// Visible shape + unsigned int p_num_columns; + + /// How to access the memory + unsigned int p_offset; + unsigned int p_column_step; + + bool p_sub_matrix; + + + /// The memory access rule, in one place for easy reference + INLINE unsigned int vcell_pos(unsigned int c, + unsigned int cs) const + {return cs==1?c:c*cs;} + + INLINE unsigned int vcell_pos(unsigned int c) const + { + return vcell_pos(c, + p_column_step); + } + + INLINE unsigned int vcell_pos_1(unsigned int c) const + { + return c; + } + + /// quick method for returning \(x[n]\) + INLINE const T &fast_a_v(int c) const { return p_memory[vcell_pos(c)]; } + + INLINE T &fast_a_v(int c) { return p_memory[vcell_pos(c)]; } + + INLINE const T &fast_a_1(int c) const { return p_memory[vcell_pos_1(c)]; } + INLINE T &fast_a_1(int c) { return p_memory[vcell_pos_1(c)]; } + + /// Get and set values from array + void set_values(const T *data, int step, int start_c, int num_c); + void get_values(T *data, int step, int start_c, int num_c) const; + + /// private copy function, called from all other copying functions. + void copy(const EST_TVector &a); + /// just copy data, no resizing, no size check. + void copy_data(const EST_TVector &a); + + /// resize the memory and reset the bounds, but don't set values. + void just_resize(int new_cols, T** old_vals); + + /// sets data and length to default values (0 in both cases). + void default_vals(); + +public: + ///default constructor + EST_TVector(); + + /// copy constructor + EST_TVector(const EST_TVector &v); + + /// "size" constructor - make vector of size n. + EST_TVector(int n); + + /// construct from memory supplied by caller + EST_TVector(int, + T *memory, int offset=0, int free_when_destroyed=0); + + /// destructor. + ~EST_TVector(); + + /// default value, used for filling matrix after resizing + static const T *def_val; + + /** A reference to this variable is returned if you try and access + * beyond the bounds of the matrix. The value is undefined, but you + * can check for the reference you get having the same address as + * this variable to test for an error. + */ + static T *error_return; + + /** resize vector. If set=1, then the current values in + the vector are preserved up to the new length n. If the + new length exceeds the old length, the rest of the vector is + filled with the def_val + */ + void resize(int n, int set=1); + + /** For when you absolutely have to have access to the memory. + */ + const T * memory() const { return p_memory; } + T * memory(){ return p_memory; } + + /**@name access + * Basic access methods for vectors. + */ + //@{ + + /// number of items in vector. + INLINE int num_columns() const {return p_num_columns;} + /// number of items in vector. + INLINE int length() const {return num_columns();} + /// number of items in vector. + INLINE int n() const {return num_columns();} + + /// read-only const access operator: without bounds checking + INLINE const T &a_no_check(int n) const { return fast_a_v_x(n); } + /// read/write non-const access operator: without bounds checking + INLINE T &a_no_check(int n) { return fast_a_v_x(n); } + /// read-only const access operator: without bounds checking + INLINE const T &a_no_check_1(int n) const { return fast_a_1(n); } + /// read/write non-const access operator: without bounds checking + INLINE T &a_no_check_1(int n) { return fast_a_1(n); } + + // #define pp_a_no_check(V,N) (pp_fast_a(V,N)) + + /// read-only const access operator: with bounds checking + const T &a_check(int n) const; + /// read/write non-const access operator: with bounds checking + T &a_check(int n); + + const T &a(int n) const { return A_CHECK(n); } + T &a(int n) { return A_CHECK(n); } + + /// read-only const access operator: return reference to nth member + const T &operator () (int n) const {return A_CHECK(n);} + + // PT + // /// non const access operator: return reference to nth member + // T &operator () (int n) const {return a(n);} + + /// read/write non const access operator: return reference to nth member + T &operator [] (int n) { return A_CHECK(n); } + + //@} + + void set_memory(T *buffer, int offset, int columns, + int free_when_destroyed=0); + + /// assignment operator + EST_TVector &operator=(const EST_TVector &s); + + /// Fill entire array will value v. + void fill(const T &v); + + /// Fill vector with default value + void empty() { fill(*def_val); } + + /// is true if vectors are equal size and all elements are equal. + int operator == (const EST_TVector &v) const; + /// is true if vectors are not equal size or a single elements isn't equal. + int operator != (const EST_TVector &v) const + { return ! ((*this) == v); } + + /// Copy data in and out. Subclassed by SimpleVector for speed. + + void copy_section(T* dest, int offset=0, int num=-1) const; + void set_section(const T* src, int offset=0, int num=-1); + + /// Create a sub vector. + void sub_vector(EST_TVector &sv, int start_c=0, int len=-1); + /// print out vector. + friend ostream& operator << (ostream &st, const EST_TVector &m) + { + int i; + for (i = 0; i < m.n(); ++i) + st << m(i) << " "; st << endl; + return st; + } + + /// Matrix must be friend to set up subvectors + friend class EST_TMatrix; + + void integrity() const; + +}; + +//@} +/// assignment operator: fill track with values in list s. +template +extern EST_TVector &set(EST_TVector &v, const EST_TList &s); + +#undef A_CHECK +#endif diff --git a/aeneas/cfw/speech_tools/EST_Token.h b/aeneas/cfw/speech_tools/EST_Token.h new file mode 100644 index 00000000..6b9512fd --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Token.h @@ -0,0 +1,393 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* Token/Tokenizer class */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_TOKEN_H__ +#define __EST_TOKEN_H__ + +#include + +using namespace std; + +#include "EST_String.h" +#include "EST_common.h" + +// I can never really remember this so we'll define it here +/// The default whitespace characters +extern const EST_String EST_Token_Default_WhiteSpaceChars; +/// +extern const EST_String EST_Token_Default_SingleCharSymbols; +/// +extern const EST_String EST_Token_Default_PunctuationSymbols; +/// +extern const EST_String EST_Token_Default_PrePunctuationSymbols; + +/** This class is similar to \Ref{EST_String} but also maintains + the original punctuation and whitespace found around the + token. + + \Ref{EST_Token}'s primary use is with \Ref{EST_TokenStream} class + which allows easy tokenizing of ascii files. + + A token consists of four parts, any of which may be empty: a + name, the actual token, preceding whitespace, preceding + punctuation, the name and succeeding punctuation. + + @author Alan W Black (awb@cstr.ed.ac.uk): April 1996 +*/ +class EST_Token { + private: + EST_String space; + EST_String prepunc; + EST_String pname; + EST_String punc; + int linenum; + int linepos; + int p_filepos; + int p_quoted; + + public: + /// + EST_Token() {init();} + /// + EST_Token(const EST_String p) {init(); pname = p; } + /// + void init() {p_quoted=linenum=linepos=p_filepos=0;} + + /**@name Basic access to fields */ + //@{ + /// set token from a string + void set_token(const EST_String &p) { pname = p; } + /// + void set_token(const char *p) { pname = p; } + /// set whitespace of token. + void set_whitespace(const EST_String &p) { space = p; } + /// + void set_whitespace(const char *p) { space = p; } + /// set (post) punctuation of token. + void set_punctuation(const EST_String &p) { punc = p; } + /// + void set_punctuation(const char *p) { punc = p; } + /// set prepunction + void set_prepunctuation(const EST_String &p) { prepunc = p; } + /// + void set_prepunctuation(const char *p) { prepunc = p; } + /// + const EST_String &whitespace() { return space; } + /// + const EST_String &punctuation() { return punc; } + /// + const EST_String &prepunctuation() { return prepunc; } + + /**@name Access token as a string */ + //@{ + const EST_String &string() const { return String(); } + /// Access token as a string + const EST_String &S() const { return S(); } + /// Access token as a string + const EST_String &String() const { return pname; } + /// For automatic coercion to \Ref{EST_String} + operator EST_String() const { return String(); } + //@} + + /**@name Access token as a int */ + //@{ + int Int(bool &valid) const { return String().Int(valid); } + int Int() const { return String().Int(); } + int I(bool &valid) const { return Int(valid); } + int I() const { return Int(); } + operator int() const { return Int(); } + //@} + + /**@name Access token as a long */ + //@{ + long Long(bool &valid) const { return String().Long(valid); } + long Long() const { return String().Long(); } + long L(bool &valid) const { return Long(valid); } + long L() const { return Long(); } + operator long() const { return Long(); } + //@} + + /**@name Access token as a float */ + //@{ + float Float(bool &valid) const { return String().Float(valid); } + float Float() const { return String().Float(); } + float F(bool &valid) const { return Float(valid); } + float F() const { return Float(); } + operator float() const { return Float(); } + //@} + + /**@name Access token as a double */ + //@{ + double Double(bool &valid) const { return String().Double(valid); } + double Double() const { return String().Double(); } + double D(bool &valid) const { return Double(valid); } + double D() const { return Double(); } + operator double() const { return Double(); } + //@} + + //@} + //@{ + /// Note that this token was quoted (or not) + void set_quoted(int q) { p_quoted = q; } + /// TRUE is token was quoted + int quoted() const { return p_quoted; } + //@} + /// + void set_row(int r) { linenum = r; } + /// + void set_col(int c) { linepos = c; } + /// Set file position in original \Ref{EST_TokenStream} + void set_filepos(int c) { p_filepos = c; } + /// Return lower case version of token name + EST_String lstring() { return downcase(pname); } + /// Return upper case version of token name + EST_String ustring() { return upcase(pname); } + /// Line number in original \Ref{EST_TokenStream}. + int row(void) const { return linenum; } + /// Line position in original \Ref{EST_TokenStream}. + int col(void) const { return linepos; } + /// file position in original \Ref{EST_TokenStream}. + int filepos(void) const { return p_filepos; } + + /// A string describing current position, suitable for error messages + const EST_String pos_description() const; + + /// + friend ostream& operator << (ostream& s, const EST_Token &p); + + /// + EST_Token & operator = (const EST_Token &a); + /// + EST_Token & operator = (const EST_String &a); + /// + int operator == (const EST_String &a) { return (pname == a); } + /// + int operator != (const EST_String &a) { return (pname != a); } + /// + int operator == (const char *a) { return (strcmp(pname,a)==0); } + /// + int operator != (const char *a) { return (strcmp(pname,a)!=0); } +}; + +enum EST_tokenstream_type {tst_none, tst_file, tst_pipe, tst_string, tst_istream}; + +/** A class that allows the reading of \Ref{EST_Token}s from a file + stream, pipe or string. It automatically tokenizes a file based on + user definable whitespace and punctuation. + + The definitions of whitespace and punctuation are user definable. + Also support for single character symbols is included. Single + character symbols {\em always} are treated as individual tokens + irrespective of their white space context. Also a quote + mode can be used to read uqoted tokens. + + The setting of whitespace, pre and post punctuation, single character + symbols and quote mode must be down (immediately) after opening + the stream. + + There is no unget but peek provides look ahead of one token. + + Note there is an interesting issue about what to do about + the last whitespace in the file. Should it be ignored or should + it be attached to a token with a name string of length zero. + In unquoted mode the eof() will return TRUE if the next token name + is empty (the mythical last token). In quoted mode the last must + be returned so eof will not be raised. + + @author Alan W Black (awb@cstr.ed.ac.uk): April 1996 +*/ +class EST_TokenStream{ + private: + EST_tokenstream_type type; + EST_String WhiteSpaceChars; + EST_String SingleCharSymbols; + EST_String PunctuationSymbols; + EST_String PrePunctuationSymbols; + EST_String Origin; + FILE *fp; + istream *is; + int fd; + char *buffer; + int buffer_length; + int pos; + int linepos; + int p_filepos; + int getch(void); + EST_TokenStream &getch(char &C); + int peeked_charp; + int peeked_char; // ungot character + int peekch(void); + int peeked_tokp; + int eof_flag; + int quotes; + char quote; + char escape; + EST_Token current_tok; + void default_values(void); + /* local buffers to save reallocating */ + int tok_wspacelen; + char *tok_wspace; + int tok_stufflen; + char *tok_stuff; + int tok_prepuncslen; + char *tok_prepuncs; + int close_at_end; + + /* character class map */ + char p_table[256]; + bool p_table_wrong; + + /** This function is deliberately private so that you'll get a compilation + error if you assign a token stream or pass it as an (non-reference) + argument. The problem with copying is that you need to copy the + filedescriptiors too (which can't be done for pipes). You probably + don't really want a copy anyway and meant to pass it as a reference. + If you really need this (some sort of clever look ahead) I am not + sure what he consequences really are (or how portable they are). + Pass the \Ref{EST_TokenStream} by reference instead. + */ + EST_TokenStream(EST_TokenStream &s); + + void build_table(); + + inline int getch_internal(); + inline int peekch_internal(); + inline int getpeeked_internal(); + public: + /// + EST_TokenStream(); + /// will close file if appropriate for type + ~EST_TokenStream(); + //@{ + /// open a \Ref{EST_TokenStream} for a file. + int open(const EST_String &filename); + /// open a \Ref{EST_TokenStream} for an already opened file + int open(FILE *ofp, int close_when_finished); + /// open a \Ref{EST_TokenStream} for an already open istream + int open(istream &newis); + /// open a \Ref{EST_TokenStream} for string rather than a file + int open_string(const EST_String &newbuffer); + /// Close stream. + void close(void); + //@} + /**@name stream access functions */ + //@{ + /// get next token in stream + EST_TokenStream &get(EST_Token &t); + /// get next token in stream + EST_Token &get(); + /**@name get the next token which must be the argument. */ + //@{ + EST_Token &must_get(EST_String expected, bool *ok); + EST_Token &must_get(EST_String expected, bool &ok) + { return must_get(expected, &ok); } + EST_Token &must_get(EST_String expected) + { return must_get(expected, (bool *)NULL); } + //@} + /// get up to {\tt s} in stream as a single token. + EST_Token get_upto(const EST_String &s); + /// get up to {\tt s} in end of line as a single token. + EST_Token get_upto_eoln(void); + /// peek at next token + EST_Token &peek(void) + { if (!peeked_tokp) get(); + peeked_tokp = TRUE; return current_tok; } + /// Reading binary data, (don't use peek() immediately beforehand) + int fread(void *buff,int size,int nitems) EST_WARN_UNUSED_RESULT; + //@} + /**@name stream initialization functions */ + //@{ + /// set which characters are to be treated as whitespace + void set_WhiteSpaceChars(const EST_String &ws) + { WhiteSpaceChars = ws; p_table_wrong=1;} + /// set which characters are to be treated as single character symbols + void set_SingleCharSymbols(const EST_String &sc) + { SingleCharSymbols = sc; p_table_wrong=1;} + /// set which characters are to be treated as (post) punctuation + void set_PunctuationSymbols(const EST_String &ps) + { PunctuationSymbols = ps; p_table_wrong=1;} + /// set which characters are to be treated as (post) punctuation + void set_PrePunctuationSymbols(const EST_String &ps) + { PrePunctuationSymbols = ps; p_table_wrong=1;} + /// set characters to be used as quotes and escape, and set quote mode + void set_quotes(char q, char e) { quotes = TRUE; quote = q; escape = e; p_table_wrong=1;} + /// query quote mode + int quoted_mode(void) { return quotes; } + //@} + /**@name miscellaneous */ + //@{ + /// returns line number of \Ref{EST_TokenStream} + int linenum(void) const {return linepos;} + /// end of file + int eof() + { return (eof_flag || ((!quotes) && (peek() == ""))); } + /// end of line + int eoln(); + /// current file position in \Ref{EST_TokenStream} + int filepos(void) const { return (type == tst_string) ? pos : p_filepos; } + /// tell, synonym for filepos + int tell(void) const { return filepos(); } + /// seek, reposition file pointer + int seek(int position); + int seek_end(); + /// Reset to start of file/string + int restart(void); + /// A string describing current position, suitable for error messages + const EST_String pos_description(); + /// The originating filename (if there is one) + const EST_String filename() const { return Origin; } + /// For the people who *need* the actual description (if possible) + FILE *filedescriptor() { return (type == tst_file) ? fp : 0; } + /// + EST_TokenStream & operator >>(EST_Token &p); + /// + EST_TokenStream & operator >>(EST_String &p); + /// + friend ostream& operator <<(ostream& s, EST_TokenStream &p); + //@} +}; + +/** Quote a string with given quotes and escape character +*/ +EST_String quote_string(const EST_String &s, + const EST_String "e = "\"", + const EST_String &escape = "\\", + int force=0); + +#endif // __EST_TOKEN_H__ diff --git a/aeneas/cfw/speech_tools/EST_Track.h b/aeneas/cfw/speech_tools/EST_Track.h new file mode 100644 index 00000000..1d45a351 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Track.h @@ -0,0 +1,773 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Rewritten : Richard Caley */ + /* ------------------------------------------------------------------- */ + /* EST_Track Class header file */ + /* */ + /*************************************************************************/ + +class EST_Track; + +#ifndef __Track_H__ +#define __Track_H__ + +#include "EST_FMatrix.h" +#include "EST_types.h" +#include "EST_TrackMap.h" +#include "EST_ChannelType.h" +#include "EST_Featured.h" + +typedef EST_TMatrix EST_ValMatrix; + +class EST_TokenStream; +class EST_String; + +typedef enum EST_TrackFileType { + tff_none=0, + tff_ascii, + tff_esps, + tff_htk, + tff_htk_fbank, + tff_htk_mfcc, + tff_htk_mfcc_e, + tff_htk_user, + tff_htk_discrete, + tff_xmg, + tff_xgraph, + tff_ema, + tff_ema_swapped, + tff_NIST, + tff_est_ascii, + tff_est_binary, + tff_snns, + tff_ssff +} EST_TrackFileType; + +typedef enum EST_InterpType { + it_nearest, // nearest time point + it_linear, // linerar interpolation + it_linear_nz // .. unless one end near zero +} EST_InterpType; + +/** A class for storing time aligned coefficients. + +some stuff. +*/ + +class EST_Track : public EST_Featured { + +protected: + EST_FMatrix p_values; // float x array + EST_FVector p_times; // float y array + EST_CVector p_is_val; // for breaks and non-breaks + + EST_ValMatrix p_aux; // Auxiliary channels + EST_StrVector p_aux_names; // Names of auxiliary channels + + float p_t_offset; // time shift. + + EST_TrackMap::P p_map; + EST_StrVector p_channel_names; // name of each track + + bool p_equal_space; // fixed or variable frame rate + bool p_single_break; // single break lots between data + + void default_vals(); + void default_channel_names(); + void clear_arrays(); + void pad_breaks(); // put in extra breaks + + int interp_value(float x, float f); + float interp_amp(float x, int c, float f); + float estimate_shift(float x); + void copy(const EST_Track& a); + +public: + static const float default_frame_shift; + static const int default_sample_rate; + + /**@name Constructor and Destructor functions + */ + + //@{ + + /// Default constructor + EST_Track(); + + /// Copy constructor + EST_Track(const EST_Track &a); + + /// resizing constructor + EST_Track(int num_frames, int num_channels); + + /// resizing constructor + EST_Track(int num_frames, EST_StrList &map); + + /// default destructor + ~EST_Track(); + //@} + + /** @name Configuring Tracks + */ + //@{ + + /** resize the track to have {\tt num_frames} and {\tt num_channels}. + if {\tt preserve} is set to 1, any existing values in the track + are kept, up to the limits imposed by the new number of frames + and channels. If the new track size is bigger, new positions + are filled with 0 */ + void resize(int num_frames, int num_channels, bool preserve = 1); + + /** resize the track to have {\tt num_frames} and {\tt num_channels}. + if {\tt preserve} is set to 1, any existing values in the track + are kept, up to the limits imposed by the new number of frames + and channels. If the new track size is bigger, new positions + are filled with 0 */ + void resize(int num_frames, EST_StrList &map, bool preserve = 1); + + /** resize the track's auxiliary channels. + */ + void resize_aux(EST_StrList &map, bool preserve = 1); + + /** Change the number of channels while keeping the number of + frames the same. if {\tt preserve} is set to 1, any existing + values in the track are kept, up to the limits imposed by the + new number of frames and channels. If the new track size is + bigger, new positions are filled with 0 */ + void set_num_channels(int n, bool preserve = 1) + { resize(EST_CURRENT, n, preserve); } + + /** Change the number of frames while keeping the number of + channels the same. if {\tt preserve} is set to 1, any + existing values in the track are kept, up to the limits + imposed by the new number of frames and channels. If the new + track size is bigger, new positions are filled with 0 */ + void set_num_frames(int n, bool preserve = 1) + { resize(n, EST_CURRENT, preserve); } + + /// set the name of the channel. + void set_channel_name(const EST_String &name, int channel); + + /// set the name of the auxiliary channel. + void set_aux_channel_name(const EST_String &name, int channel); + + /// copy everything but data + + void copy_setup(const EST_Track& a); + //@} + + /**@name Global track information + */ + //@{ + /// name of track - redundant use access to features + EST_String name() const + { return f_String("name");} + /// set name of track - redundant use access to features + void set_name(const EST_String &n) + {f_set("name",n);} + + //@} + + /**@name Functions for sub tracks, channels and frames. + */ + //@{ + + /** make {\tt fv} a window to frame {\tt n} in the track. + */ + void frame(EST_FVector &fv, int n, int startf=0, int nf=EST_ALL) + { p_values.row(fv, n, startf, nf); } + + /** make {\tt fv} a window to channel {\tt n} in the track. + */ + void channel(EST_FVector &cv, int n, int startf=0, int nf=EST_ALL) + { p_values.column(cv, n, startf, nf); } + + /** make {\tt fv} a window to the named channel in the track. + */ + void channel(EST_FVector &cv, const char * name, int startf=0, + int nf=EST_ALL); + + /** make {\tt st} refer to a portion of the track. No values + are copied - an internal pointer in st is set to the specified + portion of the the track. After this, st behaves like a normal + track. Its first channel is the same as start_channel and its + first frame is the same as start_frame. Any values written into + st will changes values in the main track. st cannot be resized. + + @param start_frame first frame at which sub-track starts + @param nframes number of frames to be included in total + @param start_channel first channel at which sub-track starts + @param nframes number of channels to be included in total + */ + void sub_track(EST_Track &st, + int start_frame=0, int nframes=EST_ALL, + int start_chan=0, int nchans=EST_ALL); + + /** make {\tt st} refer to a portion of the track. No values + are copied - an internal pointer in st is set to the specified + portion of the the track. After this, st behaves like a normal + track. Its first channel is the same as start_channel and its + first frame is the same as start_frame. Any values written into + st will changes values in the main track. st cannot be resized. + + @param start_frame first frame at which sub-track starts + @param nframes number of frames to be included in total + @param start_channel_name name of channel at which sub-track starts + @param end_channel_name name of channel at which sub-track stops + */ + void sub_track(EST_Track &st, + int start_frame, int nframes, + const EST_String &start_chan_name, + int nchans=EST_ALL); + + /** make {\tt st} refer to a portion of the track. No values + are copied - an internal pointer in st is set to the specified + portion of the the track. After this, st behaves like a normal + track. Its first channel is the same as start_channel and its + first frame is the same as start_frame. Any values written into + st will changes values in the main track. st cannot be resized. + + @param start_frame first frame at which sub-track starts + @param nframes number of frames to be included in total + @param start_channel_name name of channel at which sub-track starts + @param end_channel_name name of channel at which sub-track stops + */ + void sub_track(EST_Track &st, + int start_frame, int nframes, + const EST_String &start_chan_name, + const EST_String &end_chan_name); + + /** make {\tt st} refer to a portion of the track. (const version) + No values + are copied - an internal pointer in st is set to the specified + portion of the the track. After this, st behaves like a normal + track. Its first channel is the same as start_channel and its + first frame is the same as start_frame. Any values written into + st will changes values in the main track. st cannot be resized. + + @param start_frame first frame at which sub-track starts + @param nframes number of frames to be included in total + @param start_channel first channel at which sub-track starts + @param nframes number of channels to be included in total + */ + void sub_track(EST_Track &st, + int start_frame=0, int nframes=EST_ALL, + int start_chan=0, int nchans=EST_ALL) const + { ((EST_Track *)this)->sub_track(st, start_frame, nframes, + start_chan, nchans); } + + /** Copy contiguous portion of track into {\tt st}. Unlike the + normal sub_track functions, this makes a completely new track. + values written into this will not affect the main track and it + can be resized. + + @param start_frame first frame at which sub-track starts + @param nframes number of frames to be included in total + @param start_channel first channel at which sub-track starts + @param nframes number of channels to be included in total + */ + + void copy_sub_track( EST_Track &st, + int start_frame=0, int nframes=EST_ALL, + int start_chan=0, int nchans=EST_ALL) const; + + void copy_sub_track_out( EST_Track &st, const EST_FVector& frame_times ) const; + void copy_sub_track_out( EST_Track &st, const EST_IVector& frame_indices ) const; + + /** copy channel {\tt n} into pre-allocated buffer buf */ + void copy_channel_out(int n, float *buf, int offset=0, int num=EST_ALL) + const + { p_values.copy_column(n, buf, offset, num); } + + /** copy channel {\tt n} into EST_FVector */ + void copy_channel_out(int n, EST_FVector &f, int offset=0, int num=EST_ALL) + const + { p_values.copy_column(n, f, offset, num); } + + /** copy frame {\tt n} into pre-allocated buffer buf */ + void copy_frame_out(int n, float *buf, int offset=0, int num=EST_ALL) + const {p_values.copy_row(n, buf, offset, num); } + + /** copy frame {\tt n} into EST_FVector */ + void copy_frame_out(int n, EST_FVector &f, int offset=0, int num=EST_ALL) + const {p_values.copy_row(n, f, offset, num); } + + /** copy buf into pre-allocated channel n of track */ + void copy_channel_in(int n, const float *buf, int offset=0, + int num=EST_ALL) + { p_values.set_column(n, buf, offset, num); } + + /** copy f into pre-allocated channel n of track */ + void copy_channel_in(int n, const EST_FVector &f, int offset=0, + int num=EST_ALL) + { p_values.set_column(n, f, offset, num); } + + /** copy channel buf into pre-allocated channel n of track */ + void copy_channel_in(int c, + const EST_Track &from, int from_c, int from_offset=0, + int offset=0, int num=EST_ALL) + { p_values.set_column(c, from.p_values, from_c, + from_offset, offset, num); } + + /** copy buf into frame n of track */ + void copy_frame_in(int n, const float *buf, int offset=0, + int num=EST_ALL) + { p_values.set_row(n, buf, offset, num); } + + /** copy t into frame n of track */ + void copy_frame_in(int n, const EST_FVector &t, int offset=0, + int num=EST_ALL) + { p_values.set_row(n, t, offset, num); } + + /** copy from into frame n of track */ + void copy_frame_in(int i, + const EST_Track &from, int from_f, int from_offset=0, + int offset=0, int num=EST_ALL) + { p_values.set_row(i, from.p_values, from_f, from_offset, offset, + num); } + + //@} + + /**@name Channel information + */ + + //@{ + + /** Return the position of channel {\tt name} if it exists, + otherwise return -1. + */ + int channel_position(const char *name, int offset=0) const; + + /** Return the position of channel {\tt name} if it exists, + otherwise return -1. + */ + int channel_position(EST_String name, int offset=0) const + { return channel_position((const char *)name, offset); } + + + /** Returns true if the track has a channel named {\tt name}, + otherwise false. + */ + + bool has_channel(const char *name) const + { return channel_position(name) >=0; } + + /** Returns true if the track has a channel named {\tt name}, + otherwise false. + */ + bool has_channel(EST_String name) const + { return has_channel((const char *)name); } + + //@} + + /** @name Accessing amplitudes The following functions can be used + to access to amplitude of the track at certain points. Most of + these functions can be used for reading or writing to this + point, thus + + tr.a(10, 5) = 10.3; + + can be used to set the 10th frame of the 5th channel and + + cout << tr.a(10, 5); + + can be used to print the same information. Most of these functions + have a const equivalent for helping the compiler in + read only operations. + */ + + //@{ + + /** return amplitude of frame i, channel c.*/ + float &a(int i, int c=0); + float a(int i, int c=0) const; + + /** return amplitude of frame i, channel c with no bounds + checking. */ + float &a_no_check(int i, int c=0) { return p_values.a_no_check(i,c); } + float a_no_check(int i, int c=0) const {return p_values.a_no_check(i,c);} + + /** return amplitude of point i, in the channel named name plus + offset. If you have a track with say channels called F0 and + voicing, you can access the 45th frame's F0 as t.a(45, "F0"); + If there are 20 cepstral coefficients for each frame, the 5th can + be accessed as t.a(45, "cepstrum", 5); + */ + + float &a(int i, const char *name, int offset=0); + + float a(int i, const char *name, int offset=0) const + { return ((EST_Track *)this)->a(i, name, offset); } + float &a(int i, EST_String name, int offset=0) + { return a(i, (const char *)name, offset); } + float a(int i, EST_String name, int offset=0) const + { return ((EST_Track *)this)->a(i, (const char *)name, offset); } + + /** return amplitude of time t, channel c. This can be used for + reading or writing to this point. By default the nearest frame + to this time is used. If {\tt interp} is set to {\tt + it_linear}, linear interpolation is performed between the two + amplitudes of the two frames either side of the time point to + give an estimation of what the amplitude would have been at + time {\tt t}. If {\tt interp} is set to {\tt it_linear_nz}, + interpolation is as above, unless the time requested is off + the end of a portion of track in which case the nearest + amplitude is returned. + */ + float &a(float t, int c=0, EST_InterpType interp=it_nearest); + float a(float t, int c=0, EST_InterpType interp=it_nearest) const + { return ((EST_Track *)this)->a(t, c, interp); } + + + /** return amplitude of frame i, channel c. */ + float &operator() (int i, int c) { return a(i,c); } + /** return amplitude of frame i, channel 0. */ + float &operator() (int i) { return a(i,0); } + float operator() (int i, int c) const { return a(i,c); } + float operator() (int i) const { return a(i,0); } + + /** return amplitude of frame nearest time t, channel c. */ + float &operator() (float t, int c) {return a(t,c); } + /** return amplitude of frame nearest time t, channel 0. */ + float &operator() (float t) {return a(t,0); } + float operator() (float t, int c) const {return a(t,c); } + float operator() (float t) const {return a(t,0); } + + //@} + + /** @name Timing + + */ + + //@{ + + /// return time position of frame i + float &t(int i=0) { return p_times[i]; } + float t(int i=0) const { return p_times(i); } + + /// return time of frame i in milli-seconds. + float ms_t(int i) const { return p_times(i) * 1000.0; } + + /** set frame times to regular intervals of time {\tt t}. + The {\tt start} parameter specifies the integer multiple of {\tt t} at + which to start. For example, setting this to 0 will start at time + 0.0 (The default means the first time value = {\tt t} + */ + void fill_time( float t, int start=1 ); + + /** set frame times to regular intervals of time {\tt t}. + The {\tt start} parameter specifies the first time value. + */ + void fill_time( float t, float start ); + + /** fill time channel with times from another track + */ + void fill_time( const EST_Track &t ); + + /** fill all amplitudes with value {\tt v} */ + void fill(float v) { p_values.fill(v); } + + /** resample track at this frame shift, specified in seconds. + This can be used to change a variable frame spaced track into + a fixed frame track, or to change the spacing of an existing + evenly spaced track. + */ + void sample(float shift); + + /// REDO + void change_type(float nshift, bool single_break); + + /** return an estimation of the frame spacing in seconds. + This returns -1 if the track is not a fixed shift track */ + float shift() const; + /// return time of first value in track + float start() const; + /// return time of last value in track + float end() const; + //@} + + /** @name Auxiliary channels + + Auxiliary information is used to store information that goes + along with frames, but which are not amplitudes and hence + not appropriate for operations such as interpolation, + smoothing etc. The aux() array is an array of EST_Vals which + allows points to be int, float or a string. + + The following functions can be used to access to auxiliary + track information. Most of these functions can be used for + reading or writing to this point, thus + + tr.aux(10, "voicing") = 1; + + can be used to set the 10th frame of the "voicing" channel and + + cout << tr.a(10, "voicing"); + + can be used to print the same information. Most of these functions + have a const equivalent for helping the compiler in + read only operations. + + Auxiliary channels are usually accessed by name rather than + numerical index. The names are set using the set_aux_channel_names() + function. + */ + + //@{ + + EST_Val &aux(int i, int c); + EST_Val &aux(int i, int c) const; + + EST_Val &aux(int i, const char *name); + EST_Val aux(int i, const char *name) const + { return ((EST_Track *)this)->aux(i, name); } + + EST_Val &aux(int i, EST_String name) + { return aux(i, (const char *)name); } + + EST_Val aux(int i, EST_String name) const + { return ((EST_Track *)this)->aux(i, (const char *)name); } + + //@} + + /** @name File i/o functions + */ + + //@{ + + /** Load a file called {\tt name} into the track. + The load function attempts to + automatically determine which file type is being loaded from the + file's header. If no header is found, the function assumes the + file is ascii data, with a fixed frame shift, arranged with rows + representing frames and columns channels. In this case, the + frame shift must be specified as an argument to this function. + For those file formats which don't contain provision for specifying + an initial time (e.g. HTK, or ascii...), the {\tt startt} parameter + may be specified. + */ + EST_read_status load(const EST_String name, float ishift = 0.0, float startt = 0.0); + + /** Load character data from an already opened tokenstream {\tt ts} + into the track. + The load function attempts to + automatically determine which file type is being loaded from the + file's header. If no header is found, the function assumes the + file is ascii data, with a fixed frame shift, arranged with rows + representing frames and columns channels. In this case, the + frame shift must be specified as an argument to this function + For those file formats which don't contain provision for specifying + an initial time (e.g. HTK, or ascii...), the {\tt startt} parameter + may be specified. + */ + EST_read_status load(EST_TokenStream &ts, float ishift = 0.0, float startt = 0.0); + + /** Load a file called {\tt name} of format {\tt type} + into the track. If no header is found, the function assumes the + file is ascii data, with a fixed frame shift, arranged with rows + representing frames and columns channels. In this case, the + frame shift must be specified as an argument to this function + For those file formats which don't contain provision for specifying + an initial time (e.g. HTK, or ascii...), the {\tt startt} parameter + may be specified. + */ + EST_read_status load(const EST_String name, const EST_String type, + float ishift = 0.0, float startt = 0.0 ); + + /** Save the track to a file {\tt name} of format {\tt type}. */ + EST_write_status save(const EST_String name, + const EST_String EST_filetype = ""); + + /** Save the track to a already opened file pointer{\tt FP} + and write a file of format {\tt type}. */ + EST_write_status save(FILE *fp, + const EST_String EST_filetype = ""); + + //@} + + /** @name Utility functions */ + + //@{ + /// returns true if no values are set in the frame + int empty() const; + + /// set frame i to be a break + void set_break(int i); + /// set frame i to be a value + void set_value(int i); + /// return true if frame i is a value + int val(int i) const; + /// return true if frame i is a break + int track_break(int i) const { return (p_is_val(i)); } + + /** starting at frame i, return the frame index of the first + value frame before i. If frame i is a value, return i */ + int prev_non_break(int i) const; + + /** starting at frame i, return the frame index of the first + value frame after i. If frame i is a value, return i */ + int next_non_break(int i) const; + + /// return the frame index nearest time t + int index(float t) const; + + /// return the frame index before time t + int index_below(float x) const; + + /// return number of frames in track + int num_frames() const {return p_values.num_rows();} + + /// return number of frames in track + int length() const { return num_frames(); } + + /// return number of channels in track + int num_channels() const {return p_values.num_columns();} + + /// return number of auxiliary channels in track + int num_aux_channels() const {return p_aux.num_columns();} + + void add_trailing_breaks(); + void rm_trailing_breaks(); + /** If the contour has multiple break values between sections + containing values, reduce the break sections so that each has + a single break only. */ + void rm_excess_breaks(); + + /// return true if track has equal (i.e. fixed) frame spacing */ + bool equal_space() const {return p_equal_space;} + + /**return true if track has only single breaks between value sections */ + bool single_break() const {return p_single_break;} + + void set_equal_space(bool t) {p_equal_space = t;} + void set_single_break(bool t) {p_single_break = t;} + + + //@} + + + EST_Track& operator = (const EST_Track& a); + EST_Track& operator+=(const EST_Track &a); // add to existing track + EST_Track& operator|=(const EST_Track &a); // add to existing track in parallel + friend ostream& operator << (ostream& s, const EST_Track &tr); + + // Default constructor + EST_Track(int num_frames, EST_TrackMap &map); + + // assign a known description to a track + void assign_map(EST_TrackMap::P map); + void assign_map(EST_TrackMap &map) { assign_map(&map); } + + // create a description for an unknown track + void create_map(EST_ChannelNameMap &names); + void create_map(void) { create_map(EST_default_channel_names); } + + EST_TrackMap::P map() const { return p_map; } + + int channel_position(EST_ChannelType type, int offset=0) const; + + + + // return amplitude of point i, channel type c (plus offset) + float &a(int i, EST_ChannelType c, int offset=0); + float a(int i, EST_ChannelType c, int offset=0) const + { return ((EST_Track *)this)->a(i,c, offset); } + + // return amplitude at time t, channel type c + float &a(float t, EST_ChannelType c, EST_InterpType interp=it_nearest); + float a(float t, EST_ChannelType c, EST_InterpType interp=it_nearest) const + { return ((EST_Track *)this)->a(t, c, interp); } + + float &operator() (int i, EST_ChannelType c) { return a(i,c); } + float operator() (int i, EST_ChannelType c) const { return a(i,c); } + + float &t_offset() { return p_t_offset; } + float t_offset() const { return p_t_offset; } + + + EST_read_status load_channel_names(const EST_String name); + EST_write_status save_channel_names(const EST_String name); + + const EST_String channel_name(int channel, const EST_ChannelNameMap &map, int strings_override=1) const; + const EST_String channel_name(int channel, int strings_override=1) const + { return channel_name(channel, EST_default_channel_names, strings_override); } + + const EST_String aux_channel_name(int channel) const + { return p_aux_names(channel);} + + void resize(int num_frames, EST_TrackMap &map); + + EST_TrackFileType file_type() const {return (EST_TrackFileType)f_Int("file_type",0);} + + + void set_file_type(EST_TrackFileType t) {f_set("file_type", (int)t);} + + + bool has_channel(EST_ChannelType type) const + { int cp = channel_position(type); + return cp>=0; } + + // Frame iteration support + +protected: + class IPointer_f { + public: + EST_Track *frame; int i; + IPointer_f(); + IPointer_f(const IPointer_f &p); + ~IPointer_f(); + }; + + void point_to_first(IPointer_f &ip) const { ip.i = 0; } + void move_pointer_forwards(IPointer_f &ip) const { ip.i++; } + bool points_to_something(const IPointer_f &ip) const { return ip.i ; + friend class EST_TRwIterator< EST_Track, IPointer_f, EST_Track >; + +public: + typedef EST_Track Entry; + typedef EST_TIterator< EST_Track, IPointer_f, EST_Track > Entries; + typedef EST_TRwIterator< EST_Track, IPointer_f, EST_Track > RwEntries; + +}; + +// list of tracks in serial +typedef EST_TList EST_TrackList; + + +#endif /* __Track_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_TrackMap.h b/aeneas/cfw/speech_tools/EST_TrackMap.h new file mode 100644 index 00000000..dc745dee --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_TrackMap.h @@ -0,0 +1,178 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +#ifndef __EST_TRACKMAP_H__ +#define __EST_TRACKMAP_H__ + +#include +using namespace std; + +#include "EST_TNamedEnum.h" +#include "EST_ChannelType.h" +#include "EST_Handleable.h" +#include "EST_THandle.h" + +/** Track maps provide a mapping from symbolic track names to the + * actual position of the information within a track frame. The + * symbolic names are defined by the EST_ChannelType enumerated type. + * + * Track maps can be declared statically by code which always uses + * tracks of a given style, or they can be built at run time as + * is done by lpc_analysis to record whichinformation the + * user has requested. Finally they can be constructed by the Track + * itself from the names of the channels, for instance when a track has + * just been read in from a file. + * + * @see EST_Track + * @see EST_ChannelType + * @see EST_TrackMap:example + * @author Richard Caley + * @version $Id: EST_TrackMap.h,v 1.4 2004/09/29 08:24:17 robert Exp $ + */ +class EST_TrackMap : public EST_Handleable +{ + +public: + /**@name ChannelMapping + * An auxiliary type used just to define static EST_TrackMaps. + * Defining one of these and then converting it to an EST_TrackMap + * is, unfortunately, the only way C++ allows us to define + * a constant EST_TrackMap. + */ + //@{ + /// structure for the table. + struct ChannelMappingElement { + EST_ChannelType type; + unsigned short channel; + }; + /// Table of type to position pairs. +// typedef struct ChannelMappingElement ChannelMapping[]; + //@} + + typedef EST_THandle P; + +public: + + /// Returned if we ask for a channel not in the map. +# define NO_SUCH_CHANNEL (-1) + +private: + + /// The map itself. + short p_map[num_channel_types]; + + /// Parent is looked at if this map doesn't define the position. + EST_TrackMap::P p_parent; + /// Subtracted from the values in the parent. + int p_offset; + + /// No copy constructor. Don't copy these things. + EST_TrackMap(EST_TrackMap &from); + +protected: + /// Pass to creation function to turn on refcounting. +#define EST_TM_REFCOUNTED (1) + + /// Creation function used by friends to create refcounted maps. + EST_TrackMap(int refcount); + + /// Creation function used by friends to create sub-track maps. + EST_TrackMap(const EST_TrackMap *parent, int offset, int refcount); + + /// copy an exiting map. + void copy(EST_TrackMap &from); + /// Initialise the map. + void init(void); + + short get_parent(EST_ChannelType type) const ; + +public: + /// Default constructor. + EST_TrackMap(void); + /// Copy the mapping. + EST_TrackMap(EST_TrackMap &from, int refcount); + /// Create from static table. + EST_TrackMap(struct ChannelMappingElement map[]); + + + ~EST_TrackMap(); + + /// Empty the map. + void clear(void); + /// Record the position of a channel. + void set(EST_ChannelType type, short pos) + { p_map[(int)type] = pos; } + + /// Get the position of a channel. + short get(EST_ChannelType type) const + { short c = p_map[(int)type]; + return c!=NO_SUCH_CHANNEL?c:get_parent(type); } + /// Get the position of a channel. + short operator() (EST_ChannelType type) const + { return get(type); } + + /// Does the mapping contain a position for this channel? + bool has_channel(EST_ChannelType type) const + { return p_map[(int)type] != NO_SUCH_CHANNEL + || ( p_parent!=0 && p_parent->has_channel(type) ); } + + /// Returns the index of the last known channel. + short last_channel(void) const; + + /// Returns the type of the channel at the given position. + EST_ChannelType channel_type(unsigned short channel) const; + + EST_TrackMap * object_ptr() { return this; } + const EST_TrackMap * object_ptr() const { return this; } + + friend class EST_Track; + friend ostream& operator << (ostream &st, const EST_TrackMap &m); +}; + +/** Channel name maps map textual names for track channels to symbolic + * names, they are just a special case of named enums. + */ +typedef EST_TNamedEnum EST_ChannelNameMap; + +/** Table type used to create EST_ChannelNameMaps. + */ +typedef EST_TValuedEnumDefinition + EST_ChannelNameTable[]; + +/// Definition of standard names we use for channels. +extern EST_ChannelNameMap EST_default_channel_names; +/// Definition of the names ESPS programs use for channels. +extern EST_ChannelNameMap esps_channel_names; + +extern ostream& operator << (ostream &st, const EST_TrackMap &m); +#endif diff --git a/aeneas/cfw/speech_tools/EST_UList.h b/aeneas/cfw/speech_tools/EST_UList.h new file mode 100644 index 00000000..74ebde4a --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_UList.h @@ -0,0 +1,149 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Mon Jul 21 1997 */ + /* -------------------------------------------------------------------- */ + /* Untyped list used as the basis of the TList class */ + /* */ + /*************************************************************************/ + +#ifndef __EST_ULIST_H__ +#define __EST_ULIST_H__ + +#include + +using namespace std; + +#include "EST_common.h" +#include "EST_String.h" + +class EST_UItem { +public: + void init() { n = NULL; p = NULL;} + EST_UItem *n; + EST_UItem *p; + EST_UItem *next() { return n; } + EST_UItem *prev() { return p; } +}; + +class EST_UList { +protected: + EST_UItem *h; + EST_UItem *t; + +protected: + void init() { h = NULL; t = NULL; }; + void clear_and_free(void (*item_free)(EST_UItem *item)); + +public: + EST_UList() { init(); }; + ~ EST_UList() { clear_and_free(NULL); } + + EST_UItem *nth_pointer(int n) const; + + EST_UItem *insert_after(EST_UItem *ptr, EST_UItem *new_item); // returns pointer to inserted item + EST_UItem *insert_before(EST_UItem *ptr, EST_UItem *new_item); // returns pointer to item after inserted item + + // remove single item, return pointer to previous + EST_UItem *remove(EST_UItem *ptr, void (*item_free)(EST_UItem *item)); + EST_UItem *remove(int n, void (*item_free)(EST_UItem *item)); + + void exchange(EST_UItem *a, EST_UItem *b); + void exchange(int i, int j); + + void reverse(); // in place + + int length() const; // number of items in list + int index(EST_UItem *item) const; // position from start of list (head = 0) + + int empty() const // returns true if no items in list + {return (h == NULL) ? 1 : 0;}; + void clear(void) + { clear_and_free(NULL); }; + void append(EST_UItem *item); // add item onto end of list + + void prepend(EST_UItem *item); // add item onto start of list + + EST_UItem *head() const // return pointer to head of list + { return h; }; + EST_UItem *tail() const // return pointer to tail of list + { return t; }; + + + static bool operator_eq(const EST_UList &a, + const EST_UList &b, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2)); + + static int index(const EST_UList &l, + const EST_UItem &b, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2)); + + static void sort(EST_UList &a, + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2)); + static void qsort(EST_UList &a, + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*exchange)(EST_UItem *item1, EST_UItem *item2)); + static void sort_unique(EST_UList &l, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2), + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*item_free)(EST_UItem *item)); + static void merge_sort_unique(EST_UList &l, EST_UList &m, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2), + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*item_free)(EST_UItem *item)); +}; + + +// inline functions in header file +// everything else in EST_UList.C + + +/* Please don't use these - use the member functions instead! +inline EST_UItem *next(EST_UItem *ptr) +{ + if (ptr != 0) + return ptr->n; + return 0; +} + +inline EST_UItem *prev(EST_UItem *ptr) +{ + if (ptr != 0) + return ptr->p; + return 0; +} +*/ + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Val.h b/aeneas/cfw/speech_tools/EST_Val.h new file mode 100644 index 00000000..84d0ab4d --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Val.h @@ -0,0 +1,264 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : May 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A generic container class, originally for ints floats and string now */ +/* extended for some others, eventually allow run addition of new types */ +/* "built-in" types (i.e. ones explicitly mentioned in this file) may */ +/* be accessed by member functions, objects added at run time may only */ +/* be accessed by functions */ +/* */ +/* This is so similar to the LISP class in SIOD it could be viewed as a */ +/* little embarrassing, but this is done without a cons cell heap or gc */ +/* which may or may not be a good thing. */ +/* */ +/*=======================================================================*/ +#ifndef __EST_VAL_H__ +#define __EST_VAL_H__ + +#include "EST_String.h" +#include "EST_error.h" +#include "EST_Contents.h" +#include "EST_Val_defs.h" + +typedef const char *val_type; + +extern val_type val_unset; +extern val_type val_int; +extern val_type val_float; +extern val_type val_string; + +/** The EST_Val class is a container class, used to store a single + item which can be an int, float, string or other user-defined + class. It is often used as the base item in the EST_Features class, to enable features + to take on values of different types. +*/ + +class EST_Val { + private: + val_type t; + union + { int ival; + float fval; + EST_Contents *pval;} v; + // * may have a string name as well as a value + EST_String sval; + const int to_int() const; + const float to_flt() const; + const EST_String &to_str() const; + public: + /**@name Constructor and Destructor functions + */ + + //@{ + /** Default constructor */ + EST_Val() + {t=val_unset;} + + /** Copy constructor for another EST_Val*/ + EST_Val(const EST_Val &val); + + /** Copy constructor for an int*/ + EST_Val(const int i) + {t=val_int; v.ival=i;} + + /** Copy constructor for a float*/ + EST_Val(const float f) + {t=val_float; v.fval=f;} + + /** Copy constructor for a double*/ + EST_Val(const double d) {t=val_float; v.fval=d;} + + /** Copy constructor for a string*/ + // EST_Val(const EST_String &s) {t=val_string; sval = s;} + EST_Val(const EST_String &s) : t(val_string), sval(s) {}; + + /** Copy constructor for a string literal*/ + // EST_Val(const char *s) {t=val_string; sval = s;} + EST_Val(const char *s) : t(val_string), sval(s) {}; + + EST_Val(val_type type,void *p, void (*f)(void *)); + + /** Destructor */ + ~EST_Val(void); + + //@} + + /**@name Getting cast values + */ + + //@{ + + /** returns the type that the val is currently holding */ + const val_type type(void) const + {return t;} + + /** returns the value, cast as an int */ + const int Int(void) const + {if (t==val_int) return v.ival; return to_int();} + + /** returns the value, cast as an int */ + const int I(void) const + { return Int(); } + + /** returns the value, cast as a float */ + const float Float(void) const + {if (t==val_float) return v.fval; return to_flt();} + + /** returns the value, cast as a float */ + const float F(void) const + { return Float(); } + + /** returns the value, cast as a string */ + const EST_String &String(void) const + {if (t!=val_string) to_str(); return sval;} + + /** returns the value, cast as a string */ + const EST_String &string(void) const + {return String();} + + /** returns the value, cast as a string */ + const EST_String &S(void) const + {return String();} + + /** returns the value, cast as a string */ + const EST_String &string_only(void) const {return sval;} + + //@} + + // Humans should never call this only automatic functions + const void *internal_ptr(void) const + { return v.pval->get_contents(); } + + /**@name Setting values + */ + + //@{ + + /** Assignment of val to an int */ + EST_Val &operator=(const int i) { t=val_int; v.ival=i; return *this;} + + /** Assignment of val to a float */ + EST_Val &operator=(const float f) { t=val_float; v.fval=f; return *this;} + + /** Assignment of val to a double */ + EST_Val &operator=(const double d) { t=val_float; v.fval=d; return *this;} + + /** Assignment of val to a string */ + EST_Val &operator=(const EST_String &s) { t=val_string; sval = s; return *this;} + + /** Assignment of val to a string literal*/ + EST_Val &operator=(const char *s) { t=val_string; sval = s; return *this;} + + /** Assignment of val to another val*/ + EST_Val &operator=(const EST_Val &c); + + //@} + + /**@name Equivalence test + */ + + //@{ + + + /** Test whether val is equal to a*/ + int operator ==(const EST_Val &a) const + { if (t != a.t) return (1==0); + else if (t == val_string) return (sval == a.sval); + else if (t == val_int) return (v.ival == a.v.ival); + else if (t == val_float) return (v.fval == a.v.fval); + else return (internal_ptr() == a.internal_ptr()); } + + /** Test whether val is equal to the string a*/ + int operator ==(const EST_String &a) const { return (string() == a); } + /** Test whether val is equal to the char * a*/ + int operator ==(const char *a) const { return (string() == a); } + /** Test whether val is equal to the int a*/ + int operator ==(const int &i) const { return (Int() == i); } + /** Test whether val is equal to the float a*/ + int operator ==(const float &f) const { return (Float() == f); } + /** Test whether val is equal to the double a*/ + int operator ==(const double &d) const { return (Float() == d); } + + + /** Test whether val is not equal to the val a*/ + int operator !=(const EST_Val &a) const { return (!(*this == a)); } + /** Test whether val is not equal to the string a*/ + int operator !=(const EST_String &a) const { return (string() != a); } + /** Test whether val is not equal to the char * a*/ + int operator !=(const char *a) const { return (string() != a); } + /** Test whether val is not equal to the int a*/ + int operator !=(const int &i) const { return (Int() != i);} + /** Test whether val is not equal to the float a*/ + int operator !=(const float &f) const { return (Float() != f); } + /** Test whether val is not equal to the double float a*/ + int operator !=(const double &d) const { return (Float() != d); } + + //@{ + + /**@name Automatic casting + */ + //@{ + + /** Automatically cast val as an int*/ + operator int() const { return Int(); } + /** Automatically cast val as an float*/ + operator float() const { return Float(); } + /** Automatically cast val as an string*/ + operator EST_String() const { return string(); } + //@} + /** print val*/ + friend ostream& operator << (ostream &s, const EST_Val &a) + { if (a.type() == val_unset) s << "[VAL unset]" ; + else if (a.type() == val_int) s << a.v.ival; + else if (a.type() == val_float) s << a.v.fval; + else if (a.type() == val_string) s << a.sval; + else s << "[PVAL " << a.type() << "]"; + return s; + } +}; + +inline const char *error_name(const EST_Val val) { return (EST_String)val;} + +// For consistency with other (user-defined) types in val +inline EST_Val est_val(const EST_String s) { return EST_Val(s); } +inline EST_Val est_val(const char *s) { return EST_Val(s); } +inline int Int(const EST_Val &v) { return v.Int(); } +inline EST_Val est_val(const int i) { return EST_Val(i); } +inline float Float(const EST_Val &v) { return v.Float(); } +inline EST_Val est_val(const float f) { return EST_Val(f); } + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Val_defs.h b/aeneas/cfw/speech_tools/EST_Val_defs.h new file mode 100644 index 00000000..1843f052 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Val_defs.h @@ -0,0 +1,180 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1999 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : March 1999 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Macros definitions for defining anything as vals */ +/* */ +/*=======================================================================*/ +#ifndef __EST_VAL_DEFS_H__ +#define __EST_VAL_DEFS_H__ + +/* Macro for defining new class as values public functions */ +#define VAL_REGISTER_CLASS_DCLS(NAME,CLASS) \ +extern val_type val_type_##NAME; \ +class CLASS *NAME(const EST_Val &v); \ +EST_Val est_val(const class CLASS *v); + +/* For things that aren't classes (typed def something else) */ +#define VAL_REGISTER_TYPE_DCLS(NAME,CLASS) \ +extern val_type val_type_##NAME; \ +CLASS *NAME(const EST_Val &v); \ +EST_Val est_val(const CLASS *v); + +#define VAL_REGISTER_FUNCPTR_DCLS(NAME,TYPE) \ +extern val_type val_type_##NAME; \ +TYPE NAME(const EST_Val &v); \ +EST_Val est_val(const TYPE v); + + +/* Macro for defining new class as values */ +#define VAL_REGISTER_CLASS(NAME,CLASS) \ +val_type val_type_##NAME=#NAME; \ +class CLASS *NAME(const EST_Val &v) \ +{ \ + if (v.type() == val_type_##NAME) \ + return (class CLASS *)v.internal_ptr(); \ + else \ + EST_error("val not of type val_type_"#NAME); \ + return NULL; \ +} \ + \ +static void val_delete_##NAME(void *v) \ +{ \ + delete (class CLASS *)v; \ +} \ + \ +EST_Val est_val(const class CLASS *v) \ +{ \ + return EST_Val(val_type_##NAME, \ + (void *)v,val_delete_##NAME); \ +} \ + +/* Macro for defining new typedef'd things as vals */ +/* You don't need CLASS and TYPE but it often convenient */ +#define VAL_REGISTER_TYPE(NAME,CLASS) \ +val_type val_type_##NAME=#NAME; \ +CLASS *NAME(const EST_Val &v) \ +{ \ + if (v.type() == val_type_##NAME) \ + return (CLASS *)v.internal_ptr(); \ + else \ + EST_error("val not of type val_type_"#NAME); \ + return NULL; \ +} \ + \ +static void val_delete_##NAME(void *v) \ +{ \ + delete (CLASS *)v; \ +} \ + \ +EST_Val est_val(const CLASS *v) \ +{ \ + return EST_Val(val_type_##NAME, \ + (void *)v,val_delete_##NAME); \ +} \ + +/* Macro for defining new typedef'd things as vals that don't get deleted */ +/* You don't need CLASS and TYPE but it often convenient */ +#define VAL_REGISTER_TYPE_NODEL(NAME,CLASS) \ +val_type val_type_##NAME=#NAME; \ +CLASS *NAME(const EST_Val &v) \ +{ \ + if (v.type() == val_type_##NAME) \ + return (CLASS *)v.internal_ptr(); \ + else \ + EST_error("val not of type val_type_"#NAME); \ + return NULL; \ +} \ + \ +static void val_delete_##NAME(void *v) \ +{ \ + (void)v; \ +} \ + \ +EST_Val est_val(const CLASS *v) \ +{ \ + return EST_Val(val_type_##NAME, \ + (void *)v,val_delete_##NAME); \ +} \ + +/* Macro for defining new class as values */ +#define VAL_REGISTER_CLASS_NODEL(NAME,CLASS) \ +val_type val_type_##NAME=#NAME; \ +class CLASS *NAME(const EST_Val &v) \ +{ \ + if (v.type() == val_type_##NAME) \ + return (class CLASS *)v.internal_ptr(); \ + else \ + EST_error("val not of type val_type_"#NAME); \ + return NULL; \ +} \ + \ +static void val_delete_##NAME(void *v) \ +{ \ + (void)v; \ +} \ + \ +EST_Val est_val(const class CLASS *v) \ +{ \ + return EST_Val(val_type_##NAME, \ + (void *)v,val_delete_##NAME); \ +} \ + +/* Macro for defining function pointers as values */ +#define VAL_REGISTER_FUNCPTR(NAME,CLASS) \ +val_type val_type_##NAME=#NAME; \ +CLASS NAME(const EST_Val &v) \ +{ \ + if (v.type() == val_type_##NAME) \ + return (CLASS)v.internal_ptr(); \ + else \ + EST_error("val not of type val_type_"#NAME); \ + return NULL; \ +} \ + \ +static void val_delete_##NAME(void *v) \ +{ \ + (void)v; \ +} \ + \ +EST_Val est_val(const CLASS v) \ +{ \ + return EST_Val(val_type_##NAME, \ + (void *)v,val_delete_##NAME); \ +} \ + + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_WFST.h b/aeneas/cfw/speech_tools/EST_WFST.h new file mode 100644 index 00000000..2da52bc9 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_WFST.h @@ -0,0 +1,407 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : November 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Weighted Finite State Transducers */ +/* */ +/*=======================================================================*/ +#ifndef __EST_WFST_H__ +#define __EST_WFST_H__ + +#include "EST_simplestats.h" +#include "EST_rw_status.h" +#include "EST_Option.h" +#include "EST_TList.h" +#include "EST_TVector.h" +#include "EST_THash.h" +#include "siod.h" +#define wfst_error_msg(WMESS) (cerr << WMESS << endl,siod_error()) + +#define WFST_ERROR_STATE -1 + +class EST_WFST_State; +class EST_WFST; + +/** an internal class for \Ref{EST_WFST} for representing transitions + in an WFST + */ +class EST_WFST_Transition { + private: + float p_weight; + int p_state; + int p_in_symbol; + int p_out_symbol; + public: + EST_WFST_Transition(); + EST_WFST_Transition(const EST_WFST_Transition &t) + { p_weight=t.p_weight; p_state=t.p_state; + p_in_symbol = t.p_in_symbol; p_out_symbol=t.p_out_symbol; } + EST_WFST_Transition(float w, int s, int i, int o) + { p_weight=w; p_state=s; p_in_symbol=i; p_out_symbol=o;} + ~EST_WFST_Transition() { }; + float weight() const { return p_weight; } + int state() const { return p_state; } + int in_symbol() const { return p_in_symbol; } + int out_symbol() const { return p_out_symbol; } + void set_weight(float f) { p_weight = f; } + void set_state(int s) { p_state = s; } + +}; +typedef EST_TList wfst_translist; + +enum wfst_state_type {wfst_final, wfst_nonfinal, wfst_error, wfst_licence}; +/** I'd like to use the enums but I need to binary read/write them **/ +/** I don't believe that's portable so we need to have ints for these **/ +#define WFST_FINAL 0 +#define WFST_NONFINAL 1 +#define WFST_ERROR 2 +#define WFST_LICENCE 3 + + +/** an internal class for \Ref{EST_WFST} used to represent a + state in a WFST +*/ +class EST_WFST_State { + private: + int p_name; + enum wfst_state_type p_type; + int p_tag; // for marking in traversing + public: + wfst_translist transitions; + + EST_WFST_State(int name); + EST_WFST_State(const EST_WFST_State &state); + ~EST_WFST_State(); + + EST_WFST_Transition *add_transition(float w, + int end, + int in, + int out); + int name() const { return p_name; } + int num_transitions() const { return transitions.length(); } + enum wfst_state_type type() const { return p_type; } + void set_type(wfst_state_type t) { p_type = t; } + void set_tag(int v) { p_tag = v;} + int tag() const { return p_tag;} +}; +typedef EST_TVector wfst_state_vector; + +typedef EST_TStringHash EST_WFST_MultiStateIndex; +enum wfst_mstate_type {wfst_ms_set, wfst_ms_list}; + +/** an internal class to \Ref{EST_WFST} used in holding multi-states + when determinizing and find the intersections of other WFSTs. + */ +class EST_WFST_MultiState : public EST_IList { + private: + int p_name; + float p_weight; + enum wfst_mstate_type p_type; + public: + EST_WFST_MultiState() : EST_IList() + { p_name = -1; p_weight = 0.0; p_type = wfst_ms_set; } + EST_WFST_MultiState(enum wfst_mstate_type ty) : EST_IList() + { p_name = -1; p_weight = 0.0; p_type = ty; } + int name() const { return p_name; } + void set_name(int i) { p_name = i; } + float weight() const { return p_weight; } + void set_weight(float w) { p_weight = w; } + void set_type(enum wfst_mstate_type s) { p_type = s; } + enum wfst_mstate_type type() const { return p_type; } + void add(int i); +}; + +int multistate_index(EST_WFST_MultiStateIndex &i,EST_WFST_MultiState *ms); + +/** a call representing a weighted finite-state transducer + */ +class EST_WFST { + private: + EST_Discrete p_in_symbols; + EST_Discrete p_out_symbols; + int p_start_state; + int current_tag; + int p_num_states; + int p_cumulate; + wfst_state_vector p_states; + + int operator_and(LISP l); + int operator_or(LISP l); + int operator_star(LISP l); + int operator_plus(LISP l); + int operator_optional(LISP l); + int operator_not(LISP l); + int terminal(LISP l); + EST_WFST_State *copy_and_map_states(const EST_IVector &state_map, + const EST_WFST_State *s, + const EST_WFST &b) const; + void extend_alphabets(const EST_WFST &b); + int deterministiconstartstates(const EST_WFST &a, const EST_WFST &b) const; + EST_read_status load_transitions_from_lisp(int s, LISP trans); + void more_states(int new_max); + + int can_reach_final(int state); + static int traverse_tag; + public: + /**@name Constructor and initialisation functions */ + //@{ + /// ? + EST_WFST(); + /// ? + EST_WFST(const EST_WFST &wfst) { p_num_states = 0; copy(wfst); } + ~EST_WFST(); + //@} + + /**@name Reseting functions */ + //@{ + /// Clear with (estimation of number of states required) + void init(int init_num_states=10); + /// clear an initialise with given input and out alphabets + void init(LISP in, LISP out); + /// Copy from existing wfst + void copy(const EST_WFST &wfst); + /// clear removing existing states if any + void clear(); + //@} + + /**@name General utility functions */ + //@{ + int num_states() const { return p_num_states; } + int start_state() const { return p_start_state; } + /// Map input symbol to input alphabet index + int in_symbol(const EST_String &s) const + { return p_in_symbols.name(s); } + /// Map input alphabet index to input symbol + const EST_String &in_symbol(int i) const + { return p_in_symbols.name(i); } + /// Map output symbol to output alphabet index + int out_symbol(const EST_String &s) const + { return p_out_symbols.name(s); } + /// Map output alphabet index to output symbol + const EST_String &out_symbol(int i) const + { return p_out_symbols.name(i); } + /// LISP for on epsilon symbols + LISP epsilon_label() const { return rintern("__epsilon__"); } + /// Internal index for input epsilon + int in_epsilon() const { return p_in_symbols.name("__epsilon__"); } + /// Internal index for output epsilon + int out_epsilon() const { return p_out_symbols.name("__epsilon__"); } + /// Return internal state information + const EST_WFST_State *state(int i) const { return p_states(i); } + /// Return internal state information (non-const) + EST_WFST_State *state_non_const(int i) { return p_states(i); } + /// True if state {\tt i} is final + int final(int i) const + { return ((i != WFST_ERROR_STATE) && (state(i)->type() == wfst_final));} + /// Accessing the input alphabet + const EST_Discrete &in_symbols() const { return p_in_symbols; } + /// Accessing the output alphabet + const EST_Discrete &out_symbols() const { return p_out_symbols; } + + //@} + + /**@name file i/o */ + //@{ + /// ? + EST_write_status save(const EST_String &filename, + const EST_String type = "ascii"); + EST_write_status save_binary(FILE *fd); + /// ? + EST_read_status load(const EST_String &filename); + + EST_read_status load_binary(FILE *fd, + EST_Option &hinfo, + int num_states, + int swap); + //@} + + /**@name transduction functions */ + //@{ + /// Find (first) new state given in and out symbols + int transition(int state,int in, int out) const; + int transition(int state,int in, int out, float &prob) const; + /// Find (first) transition given in and out symbols + EST_WFST_Transition *find_transition(int state,int in, int out) const; + /// Find (first) new state given in and out strings + int transition(int state,const EST_String &in,const EST_String &out) const; + /// Find (first) new state given in/out string + int transition(int state,const EST_String &inout) const; + /// Transduce in to out from state + int transduce(int state,int in,int &out) const; + /// Transduce in to out (strings) from state + int transduce(int state,const EST_String &in,EST_String &out) const; + /// Transduce in to list of transitions + void transduce(int state,int in,wfst_translist &out) const; + /// Find all possible transitions for given state/input/output + void transition_all(int state,int in, int out, + EST_WFST_MultiState *ms) const; + + //@} + + /**@name Cumulation functions for adding collective probabilities + for transitions from data */ + //@{ + /// Cumulation condition + int cumulate() const {return p_cumulate;} + /// Clear and start cumulation + void start_cumulate(); + /// Stop cumulation and calculate probabilities on transitions + void stop_cumulate(); + //@} + + /**@name WFST construction functions from external representations **/ + //@{ + /// Add a new state, returns new name + int add_state(enum wfst_state_type state_type); + /// Given a multi-state return type (final, ok, error) + enum wfst_state_type ms_type(EST_WFST_MultiState *ms) const; + + /// Basic regex constructor + void build_wfst(int start, int end,LISP regex); + /// Basic conjunction constructor + void build_and_transition(int start, int end, LISP conjunctions); + /// Basic disjunction constructor + void build_or_transition(int start, int end, LISP disjunctions); + + // from standard REGEX + void build_from_regex(LISP inalpha, LISP outalpha, LISP regex); + // Kay/Kaplan/Koskenniemi rule compile + void kkrule_compile(LISP inalpha, LISP outalpha, LISP fp, + LISP rule, LISP sets); + // Build from regular (or pseudo-CF) grammar + void build_from_rg(LISP inalpha, LISP outalpha, + LISP distinguished, LISP rewrites, + LISP sets, LISP terms, + int max_depth); + // Build simple tree lexicon + void build_tree_lex(LISP inalpha, LISP outalpha, + LISP wlist); + //@} + + /**@name Basic WFST operators */ + //@{ + /// Build determinized form of a + void determinize(const EST_WFST &a); + /// Build minimized form of a + void minimize(const EST_WFST &a); + /// Build complement of a + void complement(const EST_WFST &a); + /** Build intersection of all WFSTs in given list. The new WFST + recognizes the only the strings that are recognized by all WFSTs + in the given list */ + void intersection(EST_TList &wl); + /** Build intersection of WFSTs a and b The new WFST + recognizes the only the strings that are recognized by both a + and b list */ + void intersection(const EST_WFST &a, const EST_WFST &b); + /** Build union of all WFSTs in given list. The new WFST + recognizes the only the strings that are recognized by at least + one WFSTs in the given list */ + void uunion(EST_TList &wl); + /** Build union of WFSTs a and b. The new WFST + recognizes the only the strings that are recognized by either + a or b */ + void uunion(const EST_WFST &a, const EST_WFST &b); + /** Build new WFST by composition of a and b. Outputs of a are + fed to b, given a new WFSTs which has a's input language and b's + output set. a's output and b's input alphabets must be the same */ + void compose(const EST_WFST &a,const EST_WFST &b); + /** Build WFST that accepts only strings in a that aren't also accepted + by strings in b. */ + void difference(const EST_WFST &a,const EST_WFST &b); + /** Build WFST that accepts a language that consists of any string in + a followed by any string in b **/ + void concat(const EST_WFST &a,const EST_WFST &b); + //@} + + /**@name construction support functions */ + //@{ + /// True if WFST is deterministic + int deterministic() const; + /// Transduce a multi-state given n and out + EST_WFST_MultiState *apply_multistate(const EST_WFST &wfst, + EST_WFST_MultiState *ms, + int in, int out) const; + /// Extend multi-state with epsilon reachable states + void add_epsilon_reachable(EST_WFST_MultiState *ms) const; + /// Remove error states from the WFST. + void remove_error_states(const EST_WFST &a); + + EST_String summary() const; + /// ? + EST_WFST & operator = (const EST_WFST &a) { copy(a); return *this; } +}; +typedef EST_TList wfst_list; + +// The basic operations on WFST +void minimize(EST_WFST &a,EST_WFST &b); +void determinize(EST_WFST &a,EST_WFST &b); +void concat(EST_WFST &a,EST_WFST &b,EST_WFST &c); +void compose(EST_WFST &a,EST_WFST &b,EST_WFST &c); +void intersect(wfst_list &wl,EST_WFST &wfst); +void complement(EST_WFST &a,EST_WFST &b); +void difference(EST_WFST &a,EST_WFST &b,EST_WFST &c); +void concatenate(EST_WFST &a,EST_WFST &b,EST_WFST &c); + +// Compile a set of KK rules +void kkcompile(LISP ruleset, EST_WFST &all_wfst); +// Compile a set of LTS rules +void ltscompile(LISP lts_rules, EST_WFST &all_wfst); +// Compile a regular grammar +void rgcompile(LISP rg, EST_WFST &all_wfst); +// Compile a tree lexicon +void tlcompile(LISP rg, EST_WFST &all_wfst); + +// Transduction and recognition functions +int transduce(const EST_WFST &wfst,const EST_StrList &in,EST_StrList &out); +int transduce(const EST_WFST &wfst,const EST_IList &in,EST_IList &out); +int recognize(const EST_WFST &wfst,const EST_StrList &in,int quiet); +int recognize(const EST_WFST &wfst,const EST_IList &in, + const EST_IList &out,int quite); +int recognize_for_perplexity(const EST_WFST &wfst, + const EST_StrList &in, + int quiet, + float &count, + float &sumlogp); +int recognize_for_perplexity(const EST_WFST &wfst, + const EST_IList &in, + const EST_IList &out, + int quiet, + float &count, + float &sumlogp); + +VAL_REGISTER_CLASS_DCLS(wfst,EST_WFST) + +#endif diff --git a/aeneas/cfw/speech_tools/EST_Wagon.h b/aeneas/cfw/speech_tools/EST_Wagon.h new file mode 100644 index 00000000..9c647cf7 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_Wagon.h @@ -0,0 +1,301 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : May 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Public declarations for Wagon (CART builder) */ +/* */ +/*=======================================================================*/ +#ifndef __WAGON_H__ +#define __WAGON_H__ + +#include "EST_String.h" +#include "EST_Val.h" +#include "EST_TVector.h" +#include "EST_TList.h" +#include "EST_simplestats.h" /* For EST_SuffStats class */ +#include "EST_Track.h" +#include "siod.h" +#define wagon_error(WMESS) (cerr << WMESS << endl,exit(-1)) + +// I get floating point exceptions of Alphas when I do any comparisons +// with HUGE_VAL or FLT_MAX so I'll make my own +#define WGN_HUGE_VAL 1.0e20 + +class WVector : public EST_FVector +{ + public: + WVector(int n) : EST_FVector(n) {} + int get_int_val(int n) const { return (int)a_no_check(n); } + float get_flt_val(int n) const { return a_no_check(n); } + void set_int_val(int n,int i) { a_check(n) = (int)i; } + void set_flt_val(int n,float f) { a_check(n) = f; } +}; + +typedef EST_TList WVectorList; +typedef EST_TVector WVectorVector; + +/* Different types of feature */ +enum wn_dtype {/* for predictees and predictors */ + wndt_binary, wndt_float, wndt_class, + /* for predictees only */ + wndt_cluster, wndt_vector, wndt_matrix, wndt_trajectory, + wndt_ols, + /* for ignored features */ + wndt_ignore}; + +class WDataSet : public WVectorList { + private: + int dlength; + EST_IVector p_type; + EST_IVector p_ignore; + EST_StrVector p_name; + public: + void load_description(const EST_String& descfname,LISP ignores); + void ignore_non_numbers(); + + int ftype(const int &i) const {return p_type(i);} + int ignore(int i) const {return p_ignore(i); } + void set_ignore(int i,int value) { p_ignore[i] = value; } + const EST_String &feat_name(const int &i) const {return p_name(i);} + int samples(void) const {return length();} + int width(void) const {return dlength;} +}; +enum wn_oper {wnop_equal, wnop_binary, wnop_greaterthan, + wnop_lessthan, wnop_is, wnop_in, wnop_matches}; + +class WQuestion { + private: + int feature_pos; + wn_oper op; + int yes; + int no; + EST_Val operand1; + EST_IList operandl; + float score; + public: + WQuestion() {;} + WQuestion(const WQuestion &s) + { feature_pos=s.feature_pos; + op=s.op; yes=s.yes; no=s.no; operand1=s.operand1; + operandl = s.operandl; score=s.score;} + ~WQuestion() {;} + WQuestion(int fp, wn_oper o,EST_Val a) + { feature_pos=fp; op=o; operand1=a; } + void set_fp(const int &fp) {feature_pos=fp;} + void set_oper(const wn_oper &o) {op=o;} + void set_operand1(const EST_Val &a) {operand1 = a;} + void set_yes(const int &y) {yes=y;} + void set_no(const int &n) {no=n;} + int get_yes(void) const {return yes;} + int get_no(void) const {return no;} + const int get_fp(void) const {return feature_pos;} + const wn_oper get_op(void) const {return op;} + const EST_Val get_operand1(void) const {return operand1;} + const EST_IList &get_operandl(void) const {return operandl;} + const float get_score(void) const {return score;} + void set_score(const float &f) {score=f;} + const int ask(const WVector &w) const; + friend ostream& operator<<(ostream& s, const WQuestion &q); +}; + +enum wnim_type {wnim_unset, wnim_float, wnim_class, + wnim_cluster, wnim_vector, wnim_matrix, wnim_ols, + wnim_trajectory}; + +// Impurity measure for cumulating impurities from set of data +class WImpurity { + private: + wnim_type t; + EST_SuffStats a; + EST_DiscreteProbDistribution p; + + float cluster_impurity(); + float cluster_member_mean(int i); + float vector_impurity(); + float trajectory_impurity(); + float ols_impurity(); + public: + EST_IList members; // Maybe there should be a cluster class + EST_FList member_counts; // AUP: Implement counts for vectors + EST_SuffStats **trajectory; + const WVectorVector *data; // Needed for ols + float score; + int l,width; + + WImpurity() { t=wnim_unset; a.reset(); trajectory=0; l=0; width=0; data=0;} + ~WImpurity(); + WImpurity(const WVectorVector &ds); + void copy(const WImpurity &s) + { + int i,j; + t=s.t; a=s.a; p=s.p; members=s.members; member_counts = s.member_counts; l=s.l; width=s.width; + score = s.score; + data = s.data; + if (s.trajectory) + { + trajectory = new EST_SuffStats *[l]; + for (i=0; i +#include "EST_Featured.h" +#include "EST_rw_status.h" +#include "EST_types.h" + +class EST_Track; +class EST_String; +class EST_TokenStream; + + +/** A class for storing digital waveforms. The waveform is stored as +an array of 16 bit shorts. Multiple channels are supported, but if no +channel information is given the 0th channel is accessed. +

+ +The waveforms can be of any sample rate, and can be changed to another +sampling rate using the resample function. + +*/ + +class EST_Wave : public EST_Featured +{ +protected: + EST_SMatrix p_values; + + int p_sample_rate; + + void default_vals(int n=0, int c=1); + void free_wave(); + void copy_data(const EST_Wave &w); + void copy_setup(const EST_Wave &w); + +public: + + static const int default_sample_rate; + static const int default_num_channels; + + /// default constructor + EST_Wave(); + /// copy constructor + EST_Wave(const EST_Wave &a); + + EST_Wave(int n, int c, int sr); + + /// Construct from memory supplied by caller + EST_Wave(int samps, int chans, + short *memory, int offset=0, int sample_rate=default_sample_rate, + int free_when_destroyed=0); + + ~EST_Wave(); + + + /**@name Access functions for finding amplitudes of samples */ + //@{ + + /** return amplitude of sample i from channel + channel. By default the 0th channel is selected. This + function can be used for assignment. + */ + short &a(int i, int channel = 0); + short a(int i, int channel = 0) const; + INLINE short &a_no_check(int i, int channel = 0) + { return p_values.a_no_check(i,channel); } + INLINE short a_no_check(int i, int channel = 0) const + { return p_values.a_no_check(i,channel); } + INLINE short &a_no_check_1(int i, int channel = 0) + { return p_values.a_no_check_1(i,channel); } + INLINE short a_no_check_1(int i, int channel = 0) const + { return p_values.a_no_check_1(i,channel); } + + + /** explicit set_a, easier to wrap than assignment + */ + INLINE short set_a(int i, int channel = 0, short val = 0) + { return a(i,channel) = val; } + + /** return amplitude of sample i from channel + channel. By default the 0th channel is selected. + */ + short operator()(int i, int channel) const + { return a(i,channel); } + + /** return amplitude of sample i from channel 0. + */ + short operator()(int i) const + { return a(i,0); } + + /** Version of a() that returns zero if index is out of array + bounds. This is particularly useful in signal processing when + you want to have windows going off the end of the waveform. */ + short &a_safe(int i, int channel = 0); + + /// return the time position in seconds of the ith sample + float t(int i) const { return (float)i/(float)p_sample_rate; } + //@} + + /**@name Information functions */ + //@{ + /// return the number of samples in the waveform + int num_samples() const { return p_values.num_rows();} + /// return the number of channels in the waveform + int num_channels() const { return p_values.num_columns(); } + /// return the sampling rate (frequency) + int sample_rate() const { return p_sample_rate; } + /// Set sampling rate to n + void set_sample_rate(const int n){p_sample_rate = n;} + /// return the size of the waveform, i.e. the number of samples. + int length() const { return num_samples();} + /// return the time position of the last sample. + float end(){ return t(num_samples()-1); } + + /// Can we look N samples to the left? + bool have_left_context(unsigned int n) const + { return p_values.have_rows_before(n); } + + /** returns the file format of the file from which the waveform + was read. If the waveform has not been read from a file, this is set + to the default type */ + + EST_String sample_type() const { return f_String("sample_type","short"); } + void set_sample_type(const EST_String t) { f_set("sample_type", t); } + + EST_String file_type() const { return f_String("file_type","riff"); } + void set_file_type(const EST_String t) { f_set("file_type", t); } + + /// A string identifying the waveform, commonly used to store the filename + EST_String name() const { return f_String("name"); } + + /// Sets name. + void set_name(const EST_String n){ f_set("name", n); } + + //@} + + const EST_SMatrix &values() const { return p_values; } + EST_SMatrix &values() { return p_values; } + + /**@name Waveform manipulation functions */ + //@{ + + /// resize the waveform + void resize(int num_samples, int num_channels = EST_ALL, int set=1) + { p_values.resize(num_samples, num_channels, set); } + + /// Resample waveform to rate + void resample(int rate); + + /** multiply all samples by a factor gain. This checks for + overflows and puts them to the maximum positive or negative value + as appropriate. + */ + void rescale(float gain,int normalize=0); + + // multiply samples by a factor contour. The factor_contour track + // should contains factor targets at time points throughout the wave, + // between which linear interpolation is used to calculate the factor + // for each sample. + void rescale( const EST_Track &factor_contour ); + + /// clear waveform and set size to 0. + void clear() {resize(0,EST_ALL);} + + void copy(const EST_Wave &from); + + void fill(short v=0, int channel=EST_ALL); + + void empty(int channel=EST_ALL) { fill(0,channel); } + + void sample(EST_TVector &sv, int n) + { p_values.row(sv, n); } + void channel(EST_TVector &cv, int n) + { p_values.column(cv, n); } + + void copy_channel(int n, short *buf, int offset=0, int num=EST_ALL) const + { p_values.copy_column(n, buf, offset, num); } + void copy_sample(int n, short *buf, int offset=0, int num=EST_ALL) const + { p_values.copy_row(n, buf, offset, num); } + + void set_channel(int n, const short *buf, int offset=0, int num=EST_ALL) + { p_values.set_column(n, buf, offset, num); } + void set_sample(int n, const short *buf, int offset=0, int num=EST_ALL) + { p_values.set_row(n, buf, offset, num); } + + + void sub_wave(EST_Wave &sw, + int offset=0, int num=EST_ALL, + int start_c=0, int nchan=EST_ALL); + + void sub_wave(EST_Wave &sw, + int offset=0, int num=EST_ALL, + int start_c=0, int nchan=EST_ALL) const + { ((EST_Wave *)this)->sub_wave(sw, offset, num, start_c, nchan); } + + //@} + + /**@name File i/o functions */ + //@{ + + /** Load a file into the waveform. The load routine attempts to + automatically determine which file type is being loaded. A + portion of the waveform can be loaded by setting + offset to the sample position from the beginning and + to the number of required samples after this. */ + + EST_read_status load(const EST_String filename, + int offset=0, + int length = 0, + int rate = default_sample_rate); + + EST_read_status load(EST_TokenStream &ts, + int offset=0, + int length = 0, + int rate = default_sample_rate); + + EST_read_status load(const EST_String filename, + const EST_String filetype, + int offset=0, + int length = 0, + int rate = default_sample_rate); + + EST_read_status load(EST_TokenStream &ts, + const EST_String filetype, + int offset=0, + int length = 0, + int rate = default_sample_rate); + + /** Load a file of type filetype into the waveform. This + can be used to load unheadered files, in which case the fields + sample_rate, sample_type, bo and nc are used + to specify the sample rate, type, byte order and number of + channels. A portion of the waveform can be loaded by setting + offset to the sample position from the beginning and + to the number of required samples after this. + */ + + EST_read_status load_file(const EST_String filename, + const EST_String filetype, int sample_rate, + const EST_String sample_type, int bo, int nc, + int offset = 0, int length = 0); + EST_read_status load_file(EST_TokenStream &ts, + const EST_String filetype, int sample_rate, + const EST_String sample_type, int bo, int nc, + int offset = 0, int length = 0); + + /* Save waveform to a file called filename of file + format EST_filetype. + */ + EST_write_status save(const EST_String filename, + const EST_String EST_filetype = ""); + + EST_write_status save(FILE *fp, + const EST_String EST_filetype = ""); + + EST_write_status save_file(const EST_String filename, + EST_String filetype, + EST_String sample_type, int bo, const char *mode = "wb"); + + EST_write_status save_file(FILE *fp, + EST_String filetype, + EST_String sample_type, int bo); + + EST_write_status save_file_header(FILE *fp, + EST_String ftype, + EST_String stype, int obo); + EST_write_status save_file_data(FILE *fp, + EST_String ftype, + EST_String stype, int obo); + //@} + + /// Assignment operator + EST_Wave& operator = (const EST_Wave& w); + /** Add to existing wave in serial. Waveforms must have the same + number of channels. + */ + EST_Wave& operator +=(const EST_Wave &a); + /** Add wave in parallel, i.e. make wave a become new + channels in existing waveform. + */ + EST_Wave& operator |=(const EST_Wave &a); + + /// print waveform + friend ostream& operator << (ostream& p_values, const EST_Wave &sig); + + // integrity check *** debug + void integrity() const { p_values.integrity() ; } + +}; + +typedef EST_TList EST_WaveList; + +int operator != (EST_Wave a, EST_Wave b); +int operator == (EST_Wave a, EST_Wave b); + +#endif /* __Wave_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_audio.h b/aeneas/cfw/speech_tools/EST_audio.h new file mode 100644 index 00000000..fe596458 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_audio.h @@ -0,0 +1,62 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : February 1995 */ +/*-----------------------------------------------------------------------*/ +/* Audio i/o public interface functions */ +/* */ +/*=======================================================================*/ +#ifndef __EST_AUDIO_H__ +#define __EST_AUDIO_H__ + +#include "EST_Wave.h" +#include "EST_Option.h" + +extern int nas_supported; +extern int pulse_supported; +extern int esd_supported; +extern int sun16_supported; +extern int freebsd16_supported; +extern int linux16_supported; +extern int mplayer_supported; +extern int win32audio_supported; +extern int os2audio_supported; +extern int irix_supported; +extern int macosx_supported; + +int play_wave(EST_Wave &wave, EST_Option &al); +int record_wave(EST_Wave &wave, EST_Option &al); + +EST_String options_supported_audio(); + +#endif /* __EST_AUDIO_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_bool.h b/aeneas/cfw/speech_tools/EST_bool.h new file mode 100644 index 00000000..d7771479 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_bool.h @@ -0,0 +1,124 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Apr 1 1997 */ + /************************************************************************/ + /* */ + /* Temporary bool type definition. */ + /* */ + /************************************************************************/ + +#ifndef __EST_BOOL_H__ +#define __EST_BOOL_H__ + +#if defined(__GNUC__) || defined(SYSTEM_IS_WIN32) + + /* GCC seems to be so very fond of bool -- it's built into + * the compiler and it chokes on my definition. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef TRUE +#define TRUE (1==1) +#endif +#ifndef FALSE +#define FALSE (1==0) +#endif + +#ifdef __cplusplus +} +#endif + +#else /* __GNUC__ */ + + /* For a boring type we still #define everything for code + * which uses ifdef to see if bool is defined. + */ + +#undef true +#undef false +#undef TRUE +#undef FALSE + +#ifdef __cplusplus +#if 0 + + class BoolType { + + private: + int p_val; + + public: + BoolType(int i) { p_val = i!=0;}; + BoolType() { p_val = 1==0;}; + + operator int () const { return p_val; }; + + BoolType operator == (BoolType b) const { return p_val == b.p_val;}; + BoolType operator != (BoolType b) const { return p_val != b.p_val;}; + + }; + +#define true BoolType(1) +#define false BoolType(0) +#define TRUE BoolType(1) +#define FALSE BoolType(0) +#define bool BoolType + +#else /* 0 */ + +/* Because SunCC is stupid we pretend we can't do better than we */ +/* could with C. */ +#if __SUNPRO_CC_COMPAT != 5 +#define bool int +#endif +#define TRUE (1==1) +#define FALSE (1==0) +#define true TRUE +#define false FALSE + +#endif + +#else /* __cplusplus */ + +#define bool int +#define TRUE (1==1) +#define FALSE (1==0) + +#endif /* __cplusplus */ +#endif /* not __GNUC__ */ + +#endif diff --git a/aeneas/cfw/speech_tools/EST_cluster.h b/aeneas/cfw/speech_tools/EST_cluster.h new file mode 100644 index 00000000..0e74b9f7 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_cluster.h @@ -0,0 +1,67 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : July 1995 */ +/*-----------------------------------------------------------------------*/ +/* Clustering routines header file */ +/* */ +/*=======================================================================*/ +#ifndef __Cluster_H__ +#define __Cluster_H__ + +#include "EST_util_class.h" + +int load_names(EST_String file, EST_TList &names); + +float lowestval(EST_FMatrix &m, EST_TList &a, EST_TList &b); +float highestval(EST_FMatrix &m, EST_TList &a, EST_TList &b); + +int fn_cluster(EST_FMatrix &m, float d); +int nn_cluster(EST_FMatrix &m, float d); +float nearest(EST_TList &cbk); +void merge(EST_TList cbk[], int i, int j); + +typedef EST_TList > EST_CBK; + +EST_String print_codebook(EST_CBK &cbk, float d, EST_TList &names); +EST_String print_codebook(EST_CBK &cbk, float d); +int cluster(EST_FMatrix &m, EST_CBK &cbk, float d); +void init_cluster(EST_CBK &cbk, int n); +//EST_FVector sortvals(EST_FMatrix &m); + +int cluster(EST_FMatrix &m, EST_CBK &cbk, EST_TList &ans, EST_String method, + EST_TList &x); + +EST_FVector sort_matrix(EST_FMatrix &m); + +#endif /* Cluster_h */ diff --git a/aeneas/cfw/speech_tools/EST_cmd_line.h b/aeneas/cfw/speech_tools/EST_cmd_line.h new file mode 100644 index 00000000..c3e4547f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_cmd_line.h @@ -0,0 +1,56 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1994 */ +/*-----------------------------------------------------------------------*/ +/* Command line options */ +/* */ +/*=======================================================================*/ +#ifndef __cmd_line_H__ +#define __cmd_line_H__ + +#include "EST_String.h" +#include "EST_Option.h" +#include "EST_types.h" + +int parse_command_line(int argc, + char *argv[], + const EST_String &usage, + EST_StrList &files, + EST_Option &al, int make_stdio=1); + + +int init_lib_ops(EST_Option &al, EST_Option &options); +void override_lib_ops(EST_Option &a_list, EST_Option &al); + +#endif /* __cmd_line_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_cmd_line_options.h b/aeneas/cfw/speech_tools/EST_cmd_line_options.h new file mode 100644 index 00000000..78f2b19a --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_cmd_line_options.h @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1994 */ +/*-----------------------------------------------------------------------*/ +/* Command line options */ +/* */ +/*=======================================================================*/ + +#ifndef __CMD_LINE_OPTIONS_H__ +#define __CMD_LINE_OPTIONS_H__ + +#include "EST_String.h" + +EST_String options_wave_input(void); +EST_String options_wave_output(void); + +EST_String options_track_input(void); +EST_String options_track_output(void); + +EST_String options_pda_general(void); +EST_String options_pda_srpd(void); + +EST_String options_io_general(void); + + +#endif /* __CMD_LINE_OPTIONS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_common.h b/aeneas/cfw/speech_tools/EST_common.h new file mode 100644 index 00000000..ee63e1e1 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_common.h @@ -0,0 +1,74 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Apr 1 1997 */ + /************************************************************************/ + /* */ + /* A place for things to be seen by all of the speech tools. */ + /* */ + /************************************************************************/ + +#ifndef __EST_COMMON_H__ +#define __EST_COMMON_H__ + +/* all this stuff should be common to C and C++ */ + +#if defined __GNUC__ + #define EST_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#elif defined __clang__ + #define EST_WARN_UNUSED_RESULT __attribute__((annotate("lo_warn_unused"))) +#else + #define EST_WARN_UNUSED_RESULT +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + /* Nasty, horrible, yeuch, like gag me with an algebra, man! */ +#include "EST_bool.h" + +#ifdef INCLUDE_DMALLOC +# ifdef __cplusplus +# include +# else +# include +# endif +# include +#endif + +#ifdef __cplusplus + } +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_cutils.h b/aeneas/cfw/speech_tools/EST_cutils.h new file mode 100644 index 00000000..8c8dc315 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_cutils.h @@ -0,0 +1,106 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan W Black */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* Various C utility functions */ +/* */ +/*=======================================================================*/ +#ifndef __EST_CUTILS_H__ +#define __EST_CUTILS_H__ + +#include "EST_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern const char * const est_tools_version; +extern const char * const est_name; +extern const char * const est_libdir; +extern const char * const est_datadir; +extern const char * const est_ostype; + +#include "EST_walloc.h" +#include "EST_system.h" + +#ifndef streq +#define streq(X,Y) (strcmp(X,Y)==0) +#endif + +char *cmake_tmp_filename(); + +/* NOTE perqs (from Three Rivers) have the third byte order, are not */ +/* supported, if you find a working one let me know and I'll add */ +/* support -- awb (hoping no one responds :-) */ +enum EST_bo_t {bo_big, bo_little, bo_perq}; + +extern int est_endian_loc; +/* Sun, HP, SGI Mips, M68000 */ +#define EST_BIG_ENDIAN (((char *)&est_endian_loc)[0] == 0) +/* Intel, Alpha, DEC Mips, Vax */ +#define EST_LITTLE_ENDIAN (((char *)&est_endian_loc)[0] != 0) +#define EST_NATIVE_BO (EST_BIG_ENDIAN ? bo_big : bo_little) +#define EST_SWAPPED_BO (EST_BIG_ENDIAN ? bo_little : bo_big) + +#define SWAPINT(x) ((((unsigned)x) & 0xff) << 24 | \ + (((unsigned)x) & 0xff00) << 8 | \ + (((unsigned)x) & 0xff0000) >> 8 | \ + (((unsigned)x) & 0xff000000) >> 24) +#define SWAPSHORT(x) ((((unsigned)x) & 0xff) << 8 | \ + (((unsigned)x) & 0xff00) >> 8) +void swapdouble(double *d); +void swapfloat(float *f); + +void swap_bytes_ushort(unsigned short *data, int length); +void swap_bytes_short(short *data, int length); +void swap_bytes_uint(unsigned int *data, int length); +void swap_bytes_int(int *data, int length); +void swap_bytes_float(float *data, int length); +void swap_bytes_double(double *data, int length); + +enum EST_bo_t str_to_bo(const char *boname); +const char *bo_to_str(enum EST_bo_t bo); + +/* return the greater of the two values */ +#define Gof(a, b) (((a) > (b)) ? (a) : (b)) +/* return the lesser of the two values */ +#define Lof(a, b) (((a) < (b)) ? (a) : (b)) + + +#ifdef __cplusplus +} +#endif + + +#endif /*__EST_CUTILS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_dynamic_model.h b/aeneas/cfw/speech_tools/EST_dynamic_model.h new file mode 100644 index 00000000..7807f8aa --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_dynamic_model.h @@ -0,0 +1,110 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : June 1998 */ +/*-----------------------------------------------------------------------*/ +/* Dynamical models for ASR */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include "EST.h" +#include "EST_model_types.h" + +// for now, CSMM means continuous-state Markov model +class CSMM +{ +public: + refcounted_EST_DMatrixP F; + refcounted_EST_DMatrixP H; + refcounted_EST_DVectorP u_v; + refcounted_EST_DMatrixP Q_v; + refcounted_EST_DVectorP u_w; + refcounted_EST_DMatrixP Q_w; + refcounted_EST_DVectorP mean_initial_x; + stats_accumulator stats; +}; + +bool operator ==(const CSMM &, const CSMM &); +ostream& operator <<(ostream &s, const CSMM &); + +/// somewhere to keep the model parameters +struct CSMM_parameter_set +{ + EST_TKVL matrix_set; + EST_TKVL vector_set; +}; + +/// somewhere to keep the models +struct CSMM_set +{ + EST_TKVL model_set; + CSMM_parameter_set parameter_set; +}; + + + +ostream& operator <<(ostream &s, const CSMM_set &ms); + +bool +create_empty_CSMM(EST_String model_name, + CSMM_set &model_set, + const int model_order, + const int observation_order); + +/// at last, some interesting functions +bool +E_step(CSMM &model, + EST_DMatrix &mean_initial_covar_x, + const EST_TrackList &train_data, + const bool trace); +bool +M_step(CSMM &model, + const bool trace); +bool +EM_shared_hidden_space(CSMM_set &models, + const int model_order, + EST_DMatrix &mean_initial_covar_x, + const EST_TrackList &train_data, + const int iterations, + const bool trace); +EST_write_status +save_CSMM_set(CSMM_set &model_set, const EST_String &dirname, const EST_String &type); + +EST_read_status +load_CSMM_set(CSMM_set &model_set, const EST_String &dirname); + +double +CSMM_Mahalanobis_distance(const CSMM &model, const EST_Track &t); diff --git a/aeneas/cfw/speech_tools/EST_error.h b/aeneas/cfw/speech_tools/EST_error.h new file mode 100644 index 00000000..53b251f4 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_error.h @@ -0,0 +1,199 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Thu Aug 14 1997 */ + /* -------------------------------------------------------------------- */ + /* Fatal error calls. */ + /* */ + /*************************************************************************/ + +#ifndef __EST_ERROR_H__ +#define __EST_ERROR_H__ + +/* may get included from C */ +#ifdef __cplusplus +#include +#include +#else +#include +#include +#endif + +#include +#include "EST_unix.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +#define MAX_ERROR_MESSAGE_LENGTH 1024 + +typedef void (*EST_error_handler)(const char *format, ...); +extern const char *EST_error_where; +extern char *EST_error_message; + +extern EST_error_handler EST_bug_func; +extern EST_error_handler EST_error_func; +extern EST_error_handler EST_sys_error_func; +extern EST_error_handler EST_warning_func; +extern EST_error_handler old_error_function; +extern EST_error_handler old_sys_error_function; + +extern FILE *EST_error_stream; +extern FILE *EST_warning_stream; + +extern jmp_buf *est_errjmp; +extern long errjmp_ok; + +extern void EST_errors_default(); +extern void EST_errors_quiet(); + +void EST_quiet_error_fn(const char *format, ...); +void EST_quiet_sys_error_fn(const char *format, ...); + + +#define _rxp_S_(X) #X +#define _rxp_s_(X) _rxp_S_(X) + +#define EST_bug (EST_error_where = __FILE__ ", line " _rxp_s_(__LINE__)),\ + (*EST_bug_func) + +#if defined(EST_DEBUGGING) +#define EST_exit(N) abort() +#define EST_error (EST_error_where = __FILE__ ", line " _rxp_s_(__LINE__)),\ + (*EST_error_func) +#define EST_warning (EST_error_where = __FILE__ ", line " _rxp_s_(__LINE__)),\ + (*EST_warning_func) +#define EST_sys_error (EST_error_where = __FILE__ ", line " _rxp_s_(__LINE__)),\ + (*EST_sys_error_func) +#else + +#define EST_exit(N) exit(N) +#define EST_error (EST_error_where = NULL),\ + (*EST_error_func) +#define EST_warning (EST_error_where = NULL),\ + (*EST_warning_func) +#define EST_sys_error (EST_error_where = NULL),\ + (*EST_sys_error_func) +#endif + +#define est_error_throw() (est_errjmp ? longjmp(*est_errjmp,1) : (void)EST_exit(-1)) +#define est_error() est_error_throw() + +#define CATCH_ERRORS_SKEL( INIT,CLEANUP) \ + { \ + INIT \ + jmp_buf *old_errjmp = est_errjmp; \ + int old_errjmp_ok = errjmp_ok; \ + errjmp_ok =1; \ + est_errjmp = (jmp_buf *)malloc(sizeof(jmp_buf)); \ + int jmp_val = setjmp(*est_errjmp); \ + if (jmp_val) { free(est_errjmp); est_errjmp = old_errjmp; errjmp_ok = old_errjmp_ok; CLEANUP} \ + if (jmp_val) + +#define CATCH_ERRORS() \ + CATCH_ERRORS_SKEL(\ + const int est_err_quiet=0; \ + , \ + ;) + +#define CATCH_ERRORS_QUIET() \ + CATCH_ERRORS_SKEL(\ + const int est_err_quiet=1; \ + EST_error_handler old_error_function=EST_error_func; \ + EST_error_handler old_sys_error_function=EST_sys_error_func; \ + EST_error_func = EST_quiet_error_fn; \ + EST_sys_error_func = EST_quiet_sys_error_fn; \ + , \ + EST_error_func=old_error_function; \ + EST_sys_error_func=old_sys_error_function; \ + ) + +#define END_CATCH_ERRORS() \ + free(est_errjmp); \ + est_errjmp = old_errjmp; \ + errjmp_ok = old_errjmp_ok; \ + if (est_err_quiet) { \ + EST_error_func=old_error_function; \ + EST_sys_error_func=old_sys_error_function; \ + } \ + } while (0) + +/** Defines the attitude of a call to possible fatal errors. + * Passing one of these values to a function tells it how much + * care it needs to take to avoid calls to EST_error. + * + * These need snappier names. + * the numbers are their for historical reasons + */ +enum EST_error_behaviour +{ + /** Function will not normally return an error unless something + * really bad has gone wrong. For feature lookup, will return + * default value if feature doesn't exist + */ + est_errors_checked = 0, + + /** Function will throw errors when feature doesn't exist. + */ + est_errors_allowed = 1, + + /** No fatal errors allowed. Function must catch all EST_error calls. + * Will *always* return a default value. + */ + est_errors_never = 2 +}; + +#ifdef __cplusplus + } + +#include "EST_String.h" + +/* These are used to pass values into error functions inside */ +/* templates. For classes we can define a function cast to EST_String, */ +/* but we need the basic versions. */ + +inline const char *error_name(const EST_String val) {return val;} +inline const char *error_name(const void *val) {return EST_String::cat("<>");} +inline const char *error_name(const EST_Regex val) {return val.tostring();} +inline const char *error_name(int val) {return EST_String::Number(val);} +inline const char *error_name(long val) {return EST_String::Number(val);} +inline const char *error_name(float val) {return EST_String::Number(val);} +inline const char *error_name(double val) {return EST_String::Number(val);} +inline const char *error_name(char val) {return EST_String::FromChar(val);} + +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_features_aux.h b/aeneas/cfw/speech_tools/EST_features_aux.h new file mode 100644 index 00000000..fcc20ce2 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_features_aux.h @@ -0,0 +1,104 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_FEATURES_AUX_H__ +#define __EST_FEATURES_AUX_H__ + +/** Non core feature functionality. + * + * @author Richard Caley + * @version $Id: EST_features_aux.h,v 1.3 2004/05/04 00:00:17 awb Exp $ + */ + +//@{ + +#include "EST_Features.h" + +class EST_String; +class EST_Val; + +/** Safe feature access functions. + * + * These functions are guaranteed to return a value even if + * there is an otherwise fatal error. + */ +//@{ + +/** What happened when we tried to look up a value. + */ +enum EST_feat_status +{ + /// All OK, value returned. + efs_ok=0, + + /// No value for feature, default returned + efs_not_set=1, + + /// An error occurred and was caught. + efs_error=2 +}; + + +/// Return the value as an EST_Val. +EST_Val getVal(const EST_Features &f, + const EST_String name, + const EST_Val &def, + EST_feat_status &status); + +/// Return the value as a string. +EST_String getString(const EST_Features &f, + const EST_String name, + const EST_String &def, + EST_feat_status &status); + +/// Return the values as a float. +float getFloat(const EST_Features &f, + const EST_String name, + float def, + EST_feat_status &status); + +/// Return the values as a float. +int getInteger(const EST_Features &f, + const EST_String name, + int def, + EST_feat_status &status); +//@} + + +VAL_REGISTER_FUNCPTR_DCLS(pointer, void *) + + //@} + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_get_function_template.h b/aeneas/cfw/speech_tools/EST_get_function_template.h new file mode 100644 index 00000000..8d8bce5e --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_get_function_template.h @@ -0,0 +1,86 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* -------------------------------------------------------------------- */ + /* Uses preprocessor to define a template for asafe feature access */ + /* function. This is just here to avoid massive duplication. */ + /* */ + /*************************************************************************/ + +#include "EST_features_aux.h" + +#define defineGetFunction(FTYPE, VAL, RTYPE, NAME) \ +RTYPE NAME(const FTYPE &it, \ + const EST_String name, \ + const RTYPE &def, \ + EST_feat_status &status) \ +{ \ + RTYPE val; \ + EST_Val defv; \ + \ + defv = est_val((void *)&defv); \ + \ + CATCH_ERRORS() \ + { \ + if (strncmp(EST_error_message,"{FND}",5)==0) \ + { \ + status = efs_not_set; \ + return def; \ + } \ + \ + status=efs_error; \ + return def; \ + } \ + \ + EST_Val valr = it.VAL(name, defv); \ + \ + if (valr.type() == val_type_pointer && &defv == pointer(valr))\ + { \ + status=efs_not_set; \ + val = def; \ + } \ + else \ + { \ + status=efs_ok; \ + val = (RTYPE)valr; \ + } \ + \ + END_CATCH_ERRORS(); \ + \ + return val; \ +} + + + + diff --git a/aeneas/cfw/speech_tools/EST_grammar.h b/aeneas/cfw/speech_tools/EST_grammar.h new file mode 100644 index 00000000..0139e795 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_grammar.h @@ -0,0 +1,50 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black and Simon King */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* EST Grammar Classes Header */ +/* */ +/*=======================================================================*/ + +#ifndef __GRAMMAR_CLASS_H__ +#define __GRAMMAR_CLASS_H__ + +// Generalised N-grammars +#include "EST_Ngrammar.h" +// Stochastic Context-free Grammars +#include "EST_SCFG.h" +// Weighted Finite-state Transducers +#include "EST_WFST.h" + +#endif /* __GRAMMAR_CLASS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_inline_utils.h b/aeneas/cfw/speech_tools/EST_inline_utils.h new file mode 100644 index 00000000..419a3f95 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_inline_utils.h @@ -0,0 +1,82 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + +/**@name EST_inline_utils.h + * Some simple inline functions gathered together in one place. + *

+ * @author Richard Caley + * @version $Id: EST_inline_utils.h,v 1.3 2004/05/04 00:00:17 awb Exp $ + */ + +//@{ + +#ifndef __EST_INLINE_UTILS_H__ +#define __EST_INLINE_UTILS_H__ + +/// Round to nearest integer +static inline int irint(float f) { return (int)(f+0.5); } +/// Round to nearest integer +static inline int irint(double f) { return (int)(f+0.5); } +/// Round to nearest integer +static inline int srint(float f) { return (short)(f+0.5); } +/// Round to nearest integer +static inline int srint(double f) { return (short)(f+0.5); } +/// Round down +static inline int ifloor(float f) { return (int)(f); } +/// Round up +static inline int iceil(float f) { return (int)(f+0.9999999); } + +/// Smaller of two ints +static inline int min(int a, int b) { return (ab)?a:b; } +/// Smaller of two floats +static inline float min(float a, float b) { return (ab)?a:b; } +/// Smaller of two doubles +static inline double min(double a, double b) { return (ab)?a:b; } + +/// Absolute value. +static inline short absval(short n) { return n<0?-n:n; } +/// Absolute value. +static inline int absval(int n) { return n<0?-n:n; } +/// Absolute value. +static inline float absval(float n) { return n<0.0?-n:n; } +/// Absolute value. +static inline double absval(double n) { return n<0.0?-n:n; } + +#endif +//@} diff --git a/aeneas/cfw/speech_tools/EST_io_aux.h b/aeneas/cfw/speech_tools/EST_io_aux.h new file mode 100644 index 00000000..190b1cb8 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_io_aux.h @@ -0,0 +1,82 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* Utility IO Function header file */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_IO_AUX_H__ +#define __EST_IO_AUX_H__ + +#include "EST_unix.h" +#include "EST_common.h" +#include "EST_String.h" +#include "EST_types.h" + +EST_String make_tmp_filename(); +EST_String stdin_to_file(); +int writable_file(char *filename); +int readable_file(char *filename); + +inline int +delete_file(const EST_String &filename) +{ + // a little unnecessary to wrap this up like this -- except + // if you want to be portable to weird OSs + return (unlink(filename) == 0); + + // could do more with return codes from unlink ... +} + +EST_String uncompress_file_to_temporary(const EST_String &filename,const EST_String &prog_name); + +int compress_file_in_place(const EST_String &filename, const EST_String &prog_name); + +int compress_file(const EST_String &filename, + const EST_String &new_filename, + const EST_String &prog_name); + +#define numeric_char(in) (((in < '9' ) && (in > '0')) ? TRUE : FALSE) + +#ifdef WIN32 +#include "Winsock2.h" +typedef SOCKET SOCKET_FD; +#else +typedef int SOCKET_FD; +#endif +int socket_receive_file(SOCKET_FD fd, const EST_String &filename); +int socket_send_file(SOCKET_FD fd, const EST_String &filename); + +#endif /*__EST_IO_AUX_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_iostream.h b/aeneas/cfw/speech_tools/EST_iostream.h new file mode 100644 index 00000000..a1321774 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_iostream.h @@ -0,0 +1,61 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Wrapper around iostream.h because the visual C++ clashes with some */ + /* unix stuff. */ + /* */ + /*************************************************************************/ + +#if !defined(EST_IOSTREAM_H) +# define EST_IOSTREAM_H 1 + +#include "EST_system.h" + +#if defined(__EMX__) + /* For OS/2 */ +# include +using namespace std; +#elif defined(SYSTEM_IS_UNIX) +# include +using namespace std; +#elif defined(SYSTEM_IS_WIN32) +# include "win32/EST_iostream_win32.h" +#else +# error No System Selected +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_kalman.h b/aeneas/cfw/speech_tools/EST_kalman.h new file mode 100644 index 00000000..782b50a7 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_kalman.h @@ -0,0 +1,60 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : November 1996 */ +/*-----------------------------------------------------------------------*/ +/* Lattice/Finite State Network */ +/* */ +/*=======================================================================*/ + + +#ifndef __EST_KALMAN_H__ +#define __EST_KALMAN_H__ + +bool kalman_filter(EST_FVector &x_state, + EST_FMatrix &P_estimate_error_covariance, + EST_FMatrix &Q_process_noise_covariance, + EST_FMatrix &R_measurement_noise_covariance, + EST_FMatrix &A_state_time_step_model, + EST_FMatrix &H_state_to_measurement_model, + EST_FVector &z_measurement); + +bool kalman_filter_Pinv(EST_FVector &x_state, + EST_FMatrix &Pinv_estimate_error_covariance_inverse, + EST_FMatrix &Q_process_noise_covariance, + EST_FMatrix &Rinv_measurement_noise_covariance_inverse, + EST_FMatrix &A_state_time_step_model, + EST_FMatrix &H_state_to_measurement_model, + EST_FVector &z_measurement); + +#endif diff --git a/aeneas/cfw/speech_tools/EST_lattice.h b/aeneas/cfw/speech_tools/EST_lattice.h new file mode 100644 index 00000000..839e2883 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_lattice.h @@ -0,0 +1,328 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : November 1996 */ +/*-----------------------------------------------------------------------*/ +/* Lattice/Finite State Network */ +/* */ +/*=======================================================================*/ + + +#ifndef __EST_LATTICE_H__ +#define __EST_LATTICE_H__ + +#include "EST_types.h" +#include "EST_Track.h" + +class Lattice { + +public: + + /* + struct quantised_label_table_entry_t{ + int index; + float value; + }; + */ + + /* + struct name_map_entry_t{ + int index; + String name; + }; + */ + + struct symbol_t{ + int qmap_index; + int nmap_index; + + symbol_t operator += (const symbol_t s2); + bool operator != (const symbol_t &s2) const; + }; + + struct Node; + struct Arc; + + struct Arc{ + int label; + Node *to; + }; + + + struct Node{ + EST_IList name; // list of ints, referring to the table of names + EST_TList arcs_out; + }; + +private: + +protected: + + // not necessarily defined or used ... + //friend inline ostream& operator<<(ostream &s, Lattice::quantised_label_table_entry_t &q); + //friend inline ostream& operator<<(ostream& s, Lattice::name_map_entry_t &n); + friend inline ostream& operator<<(ostream& s, const Lattice::symbol_t &sy); + friend inline ostream& operator<<(ostream& s, const Lattice::Node &n); + friend inline ostream& operator<<(ostream& s, const Lattice::Arc &n); + + + // maps, for speed + float qmap_error_margin; // only used in construction, so remove .. to do + + // quantised log probabilities + EST_FVector qmap; + + // 'words' + EST_TVector nmap; + + // not used + EST_String name_as_string(EST_IList &l); // given a list of nmap indices + + // the finite state machines alphabet + EST_TVector alphabet; + int e_move_symbol_index; + //int enter_move_symbol_index; + + //symbol_t* alphabet_lookup(int nmap_index, int qmap_index); + //symbol_t* alphabet_lookup_from_end(int nmap_index, int qmap_index); + + + int alphabet_index_lookup(int nmap_index, int qmap_index); // return index + //int alphabet_index_lookup_from_end(int nmap_index, int qmap_index); // return index + + + // the nodes + EST_TList nodes; + + //Node* start_node; // a subset of nodes + + EST_TList final_nodes; // a subset of nodes + + bool final(Node *n); + + // an alternative representation is a transition function + // useful (fast) for dense networks, but inefficient for sparse ones + int **tf; // indexed [node index][symbol index], contains destination node + bool build_transition_function(); + + bool build_distinguished_state_table(bool ** &dst); + bool build_distinguished_state_table_direct(bool ** &dst); + bool build_distinguished_state_table_from_transition_function(bool ** &dst); + + void sort_arc_lists(); + bool link(Node *n1, Node *n2, int label); //, EST_TList *l = NULL); + + void merge_nodes(EST_TList &l); + void merge_arcs(); + void prune_arc(Node *node, Arc *arc); + void prune_arcs(Node *node, EST_TList arcs); + void remove_arc_from_nodes_out_list(Node *n, Arc *a); + + int node_index(Node *n); // only for output in HTK format + +// SIMONK FIX THIS +// bool build_qmap(Bigram &g, float error_margin=0); +// bool build_nmap(Bigram &g); + +public: + Lattice(); + ~Lattice(); + +// SIMONK FIX THIS +// bool construct_alphabet(Bigram &g); +// bool construct(Bigram &g); + bool determinise(); + bool prune(); + bool minimise(); + bool expand(); + + Node *start_node(); + + // traversing functions + bool accepts(EST_TList &string); + float viterbi_transduce(EST_TList &input, + EST_TList &path, + EST_Litem *current_symbol = NULL, + Node *start_node = NULL); + + // observations are indexed same as wordlist, excluding !ENTER and !EXIT + float viterbi_transduce(EST_Track &observations, + EST_TList &path, + float &score, + int current_frame = 0, + Node *start_node = NULL); + + // map lookup functions + + float qmap_index_to_value(int index); + int qmap_value_to_index(float value); + + EST_String nmap_index_to_name(int index); + int nmap_name_to_index(const EST_String &name); + + symbol_t* alphabet_index_to_symbol(int index); + int alphabet_symbol_to_index(symbol_t *sym); + + + friend bool save(Lattice &lattice, EST_String filename); + friend bool load(Lattice &lattice, EST_String filename); + + friend class Lattice_Language_Model; + +}; + +/* +inline int +operator > (const Lattice::name_map_entry_t &n1, + const Lattice::name_map_entry_t &n2) +{ + return (n1.name > n2.name); +}; + +inline int +operator < (const Lattice::name_map_entry_t &n1, + const Lattice::name_map_entry_t &n2) +{ + return (n1.name < n2.name); +}; +*/ + +inline int +operator > (const Lattice::symbol_t s1, + const Lattice::symbol_t s2) +{ + if(s1.qmap_index > s2.qmap_index) + return true; + + if(s1.qmap_index < s2.qmap_index) + return false; + + return (s1.nmap_index > s2.nmap_index); +} + +inline int +operator < (const Lattice::symbol_t s1, + const Lattice::symbol_t s2) +{ + if(s1.qmap_index < s2.qmap_index) + return true; + + if(s1.qmap_index > s2.qmap_index) + return false; + + return (s1.nmap_index < s2.nmap_index); +} + +inline Lattice::symbol_t +operator + (const Lattice::symbol_t s1, + const Lattice::symbol_t s2) +{ + (void) s1; + (void) s2; + cerr << "operator + makes no sense for Lattice::symbol_t !" << endl; + return Lattice::symbol_t(); + +} + +// used for sorting arcs lists +inline int operator > (Lattice::Arc a1, Lattice::Arc a2) +{ + return (a1.label > a2.label); +} + +inline int operator < (Lattice::Arc a1, Lattice::Arc a2) +{ + return (a1.label < a2.label); +} + +inline int operator >= (Lattice::Arc a1, Lattice::Arc a2) +{ + return (a1.label >= a2.label); +} + +inline int operator <= (Lattice::Arc a1, Lattice::Arc a2) +{ + return (a1.label <= a2.label); +} + +inline int operator == (Lattice::Arc a1, Lattice::Arc a2) +{ + return (a1.label == a2.label); +} + +inline int operator == (Lattice::symbol_t s1, Lattice::symbol_t s2) +{ + return ((s1.nmap_index == s2.nmap_index) && + (s1.qmap_index == s2.qmap_index) ); +} + + +/* +inline ostream& operator<<(ostream &s, Lattice::quantised_label_table_entry_t &q){ + s << q.value; + return s; +} +*/ + +/* +inline ostream& operator<<(ostream& s, Lattice::name_map_entry_t &n) +{ + s << n.index << "=" << n.name; + return s; +} +*/ + +inline ostream& operator<<(ostream& s, const Lattice::symbol_t &sm) +{ + s << "[q=" << sm.qmap_index << ",n=" << sm.nmap_index << "]"; + return s; +} + + +inline ostream& operator<<(ostream& s, const Lattice::Node &n) +{ + s << "Node:" << n.name; + return s; +} + +inline ostream& operator<<(ostream &s, const Lattice::Arc &a) +{ + s << a.label; + return s; +} + + +void sort_by_label(EST_TList &l); + + +#endif diff --git a/aeneas/cfw/speech_tools/EST_lattice_io.h b/aeneas/cfw/speech_tools/EST_lattice_io.h new file mode 100644 index 00000000..7735bcc5 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_lattice_io.h @@ -0,0 +1,52 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : November 1996 */ +/*-----------------------------------------------------------------------*/ +/* Lattice/Finite State Network i/o functions */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_LATTICE_IO_H__ +#define __EST_LATTICE_IO_H__ + +#include +#include +#include "sp_common_types.h" +#include "wp_grammar.h" +#include "EST_lattice.h" + +bool save(Lattice &lattice,String filename); + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_ling_class.h b/aeneas/cfw/speech_tools/EST_ling_class.h new file mode 100644 index 00000000..207db20a --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_ling_class.h @@ -0,0 +1,54 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan Black */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* EST Ling Class Header */ +/* */ +/*=======================================================================*/ + +#ifndef __LING_CLASS_H__ +#define __LING_CLASS_H__ + + +#include "ling_class/EST_Utterance.h" +#include "ling_class/EST_utterance_aux.h" + +#include "ling_class/EST_Relation.h" +#include "ling_class/EST_relation_aux.h" +#include "ling_class/EST_relation_compare.h" + +#include "ling_class/EST_Item.h" +#include "ling_class/EST_item_aux.h" + +#endif /* __LING_CLASS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_math.h b/aeneas/cfw/speech_tools/EST_math.h new file mode 100644 index 00000000..9544ea15 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_math.h @@ -0,0 +1,205 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : August 1996 */ +/*-----------------------------------------------------------------------*/ +/* OS system dependent math routines */ +/* You may use this instead of math.h to get a system independent */ +/* interface to the math functions (or include in addition, it's up to */ +/* you) */ +/*=======================================================================*/ +#ifndef __EST_MATH_H__ +#define __EST_MATH_H__ + +#if defined(__APPLE__) +/* Not sure why I need this here, but I do */ +extern "C" int isnan(double); +#endif + +/* this isn't included from c, but just to be safe... */ +#ifdef __cplusplus +#include +#include +#include +#else +#include +#include +#include +#endif + +using namespace std; + +#ifdef __cplusplus +extern "C" { +#endif + +/* Although isnan(double) exists on all machine isnanf(float) does not */ +/* Automatic conversion between floats to doubles for out of range */ +/* values in ANSI is undefined so we can't depend on that, but I */ + +/* Solaris 2.X and SGIs IRIX*/ +#if defined(__svr4__) || defined(__SYSTYPE_SVR4__) +#include +#endif + +/* SunOS 4.1.X */ +/* It doesn't exist on SunOS. One could use the macro that Solaris uses */ +/* but I can't including it here, besides the follow will almost definitely */ +/* have the same effect */ +/* The defines are of course heuristics, this fails for NetBSD */ +#if defined(__sun__) && defined(__sparc__) && !defined(__svr4__) +#define isnanf(X) isnan(X) +#endif + +/* Linux (and presumably Hurd too as Linux is GNU libc based) */ +/* Sorry I haven't confirmed this cpp symbol yet */ +#if defined(linux) +#define isnanf(X) __isnanf(X) +#endif + +/* OS/2 with gcc EMX */ +#if defined(__EMX__) +#define isnanf(X) isnan(X) +#define finite(X) isfinite(X) +#endif + +/* AIX */ +#if defined(_AIX) +#define isnanf(X) isnan(X) +#endif + +/* Apple OSX */ +#if defined(__APPLE__) +#define isnanf(X) isnan((double)(X)) +/* on some previous versions of OSX we seemed to need the following */ +/* but not on 10.4 */ +/* #define isnan(X) __isnan(X) */ +#endif + +/* FreeBSD *and other 4.4 based systems require anything, isnanf is defined */ +#if defined(__FreeBSD__) + +#endif + +/* Cygwin (at least cygwin 1.7 with gcc 4.3.4) */ +#if defined(__CYGWIN__) +#if __GNUG__ > 3 +#define isnanf(X) isnan(X) +#endif +#endif + +/* WIN32 has stupid names for things */ +#if defined(SYSTEM_IS_WIN32) +#define isfinite(X) _finite(X) +#define finite(X) _finite(X) +#define round(X) win32_round(X) + inline double win32_round(double d) { return (d>0.0)?floor(d+0.5):ceil(d-0.5);} +#endif + +/* These are making assumptions about the under lying architecture */ +/* that could be wrong (though most probably in a conservative way) */ +#ifndef MAXFLOAT +#define MAXFLOAT ((float)3.0e+37) +#endif +#ifndef FLT_MAX +#define FLT_MAX ((float)3.0e+37) +#endif +#ifndef MINFLOAT +#define MINFLOAT ((float)1e-37) +#endif +#ifndef FLT_MAX +#define FLT_MIN ((float)1e-37) +#endif + +#ifndef PI +#define PI 3.14159265358979323846 +#endif +#ifndef M_PI +#define M_PI PI +#endif + +#ifndef RAND_MAX +#define RAND_MAX 32767 +#endif + +#define SAFE_LOG_ZERO -9538 + +#define EST_NINT(X) ((int)((X)+0.5)) + +inline double safe_log(const double x) +{ + double l; + if (x == 0) + return SAFE_LOG_ZERO; + l=log(x); + if (l + * @version $Id: EST_matrix_support.h,v 1.3 2004/05/04 00:00:16 awb Exp $ + */ + +bool EST_matrix_bounds_check(int r, + int c, + int num_rows, + int num_columns, + bool set); + +bool EST_matrix_bounds_check(int r, int nr, + int c, int nc, + int num_rows, + int num_columns, + bool set); + +bool EST_vector_bounds_check(int c, + int num_columns, + bool set); + +bool EST_vector_bounds_check(int c, int nc, + int num_columns, + bool set); + +#endif + diff --git a/aeneas/cfw/speech_tools/EST_model_types.h b/aeneas/cfw/speech_tools/EST_model_types.h new file mode 100644 index 00000000..653d931e --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_model_types.h @@ -0,0 +1,112 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : June 1998 */ +/*-----------------------------------------------------------------------*/ +/* Dynamical models for ASR */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include "EST.h" +#include "EST_Handleable.h" +#include "EST_THandle.h" +#include "EST_TBox.h" +#include "EST_String.h" + + +struct stats_accumulator_HV +{ + EST_DMatrix M1; + EST_DMatrix M2; + EST_DMatrix covar_y; + int points_processed; +}; + +struct stats_accumulator_FW +{ + EST_DMatrix M3; + EST_DMatrix M4; + EST_DMatrix covar_x_t_plus_1_except_last; + int points_processed_except_last; +}; + +typedef EST_TBox Boxed_stats_accumulator_FW; +typedef EST_THandle refcounted_stats_accumulator_FW_P; + +ostream& operator <<(ostream &s, const refcounted_stats_accumulator_FW_P &); + +typedef EST_TBox Boxed_stats_accumulator_HV; +typedef EST_THandle refcounted_stats_accumulator_HV_P; +ostream& operator <<(ostream &s, const refcounted_stats_accumulator_HV_P &); + +/// an accumulator to be owned by every model +/// - the underlying HV or FW accumulators are shared via +/// pointers to reference counted objects +struct stats_accumulator +{ + refcounted_stats_accumulator_HV_P hv; + refcounted_stats_accumulator_FW_P fw; +}; + +ostream& operator <<(ostream &s, const stats_accumulator_FW &); +ostream& operator <<(ostream &s, const stats_accumulator_HV &); + + + +typedef EST_TBox Boxed_EST_DMatrix; +typedef EST_THandle refcounted_EST_DMatrixP; + +ostream& operator <<(ostream &s, const Boxed_EST_DMatrix &); +ostream& operator <<(ostream &s, const refcounted_EST_DMatrixP &); + +//int operator ==(const Boxed_EST_DMatrix &a, +// const Boxed_EST_DMatrix &b); +int operator ==(const refcounted_EST_DMatrixP &a, + const refcounted_EST_DMatrixP &b); + + + + +typedef EST_TBox Boxed_EST_DVector; +typedef EST_THandle refcounted_EST_DVectorP; +int operator ==(const refcounted_EST_DVectorP &, + const refcounted_EST_DVectorP &); + +ostream& operator <<(ostream &s, const refcounted_EST_DVectorP &); + + + + diff --git a/aeneas/cfw/speech_tools/EST_multistats.h b/aeneas/cfw/speech_tools/EST_multistats.h new file mode 100644 index 00000000..d5a38b64 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_multistats.h @@ -0,0 +1,94 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : July 1995 */ +/*-----------------------------------------------------------------------*/ +/* Basic Multivariate Statistical Function Prototypes */ +/* */ +/*=======================================================================*/ +#ifndef __Multistats_H__ +#define __Multistats_H__ + +#include "EST_FMatrix.h" +#include "EST_types.h" + +float mean(EST_FVector &m); + +EST_FVector mean(EST_FMatrix &m); +EST_FVector sample_variance(EST_FMatrix &m); +EST_FVector sample_stdev(EST_FMatrix &m); + +EST_FMatrix sample_covariance(EST_FMatrix &m); +EST_FMatrix sample_correlation(EST_FMatrix &m); + +EST_FMatrix euclidean_distance(EST_FMatrix &m); +EST_FMatrix penrose_distance(EST_FMatrix &m); + + +EST_FMatrix normalise(EST_FMatrix &m, EST_FVector &sub, EST_FVector &div); + +EST_FMatrix penrose_distance(EST_FMatrix &gu, EST_FVector &gv); +EST_FMatrix mahalanobis_distance(EST_FMatrix &gu, EST_FMatrix &v); +EST_FMatrix population_mean(EST_FMatrix *in, int num_pop); +EST_FMatrix add_populations(EST_FMatrix *in, int num_pop); + +EST_FMatrix confusion(EST_StrStr_KVL &list, EST_StrList &lex); +void print_confusion(const EST_FMatrix &a, EST_StrStr_KVL &list, + EST_StrList &lex); + +#define OLS_IGNORE 100 +int ols(const EST_FMatrix &X,const EST_FMatrix &Y, EST_FMatrix &coeffs); +int robust_ols(const EST_FMatrix &X, + const EST_FMatrix &Y, + EST_FMatrix &coeffs); +int robust_ols(const EST_FMatrix &X, + const EST_FMatrix &Y, + EST_IVector &included, + EST_FMatrix &coeffs); +int stepwise_ols(const EST_FMatrix &X, + const EST_FMatrix &Y, + const EST_StrList &feat_names, + float limit, + EST_FMatrix &coeffs, + const EST_FMatrix &Xtest, + const EST_FMatrix &Ytest, + EST_IVector &included); +int ols_apply(const EST_FMatrix &samples, + const EST_FMatrix &coeffs, + EST_FMatrix &res); +int ols_test(const EST_FMatrix &real, + const EST_FMatrix &predicted, + float &correlation, + float &rmse); + +#endif // __Multistats_H__ diff --git a/aeneas/cfw/speech_tools/EST_rw_status.h b/aeneas/cfw/speech_tools/EST_rw_status.h new file mode 100644 index 00000000..b194417f --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_rw_status.h @@ -0,0 +1,164 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1994 */ +/*-----------------------------------------------------------------------*/ +/* Utility Function header file for C and C++ */ +/* */ +/*=======================================================================*/ + +#ifndef __RW_STATUS_H__ +#define __RW_STATUS_H__ + +#ifdef __cplusplus +#include +#include +#else +#include +#include + +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + + /* + * make_status_int builds the status as an integer + * make_X_status casts it to the required type. + */ + +#define make_status_int(STATE, REASON, ERRNO) \ + ( ((((unsigned int)(STATE)) & 0xff) <<24) \ + | ((((unsigned int)(REASON)) & 0xff) <<16) \ + | ((((unsigned int)(ERRNO)) & 0xffff) <<00) \ + ) + +#define make_read_status(STATE, REASON, ERRNO)\ + ((EST_read_status)(make_status_int(STATE, REASON, ERRNO))) + +#define make_write_status(STATE, REASON, ERRNO) \ + ((EST_write_status)(make_status_int(REASON, STATE, ERRNO))) + + /* + * Extract the bits. + */ + +#define get_rw_state(STATUS) \ + ((EST_rw_state)((((unsigned int)(STATUS)) >> 24) & 0xff)) +#define get_rw_reason(STATUS) \ + ((EST_rw_reason)((((unsigned int)(STATUS)) >> 16) & 0xff)) +#define get_rw_errno(STATUS) \ + ((int)((((unsigned int)(STATUS)) >> 0) & 0xff)) + + + /* + * Why we failed. + */ + +enum EST_rw_reason { + rwr_none=0, + rwr_format=1, + rwr_existance=2, + rwr_permission=3, + rwr_system=4, + rwr_unknown=0xff +}; + +/** State of rw */ +enum EST_rw_state { + /// ok + rws_ok=0, + /// partial + rws_partial=1, + /// failed + rws_failed=0xff +}; + + +/** Possible outcomes of a file reading operation. More stuff*/ +enum EST_read_status { + /// The file was read in successfully + read_ok = make_status_int(rws_ok, rwr_none, 0), + /// The file exists but is not in the format specified + read_format_error = make_status_int(rws_failed, rwr_format, 0), + /// The file does not exist. + read_not_found_error = make_status_int(rws_failed, rwr_existance, 0), + /// An error occurred while reading + read_error = make_status_int(rws_failed, rwr_unknown, 0) +}; + + +/** Possible outcomes of a file writing operation */ +enum EST_write_status { + /// The file was written successfully + write_ok = make_status_int(rws_ok, rwr_none, 0), + /// The file was not written successfully + write_fail = make_status_int(rws_failed, rwr_unknown, 0), + /// The file was not written successfully + write_error = make_status_int(rws_failed, rwr_unknown, 0), + /// A valid file was created, but only some of the requested data is in there + write_partial = make_status_int(rws_partial, rwr_unknown, 0) +}; + +/** Possible outcomes of a network connection operation */ +enum EST_connect_status { + /// Connection made. + connect_ok = make_status_int(rws_ok, rwr_none, 0), + /// Connection failed. + connect_not_found_error = make_status_int(rws_failed, rwr_existance, 0), + /// Connection failed. + connect_not_allowed_error = make_status_int(rws_failed, rwr_permission, 0), + /// System failure of some kind + connect_system_error = make_status_int(rws_failed, rwr_system, 0), + /// The file was not written successfully + connect_error = make_status_int(rws_failed, rwr_unknown, 0) +}; + + + +#ifdef __cplusplus +} +#endif + +/* + * Temporary redefinitions of the old values. + */ + +#define format_ok read_ok +#define wrong_format read_format_error +#define misc_read_error read_error +#define misc_write_error write_error + +#endif /*__RW_STATUS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_sigpr.h b/aeneas/cfw/speech_tools/EST_sigpr.h new file mode 100644 index 00000000..527dfd74 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_sigpr.h @@ -0,0 +1,52 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef __EST_SIGPR_H__ +#define __EST_SIGPR_H__ + +#include "EST_Wave.h" +#include "EST_Track.h" +#include "EST_FMatrix.h" +#include "EST_Option.h" +#include "EST_TNamedEnum.h" +#include "EST_TBuffer.h" + +#include "sigpr/EST_filter.h" +#include "sigpr/EST_pitchmark.h" +#include "sigpr/EST_fft.h" +#include "sigpr/EST_sigpr_utt.h" +#include "sigpr/EST_sigpr_frame.h" +#include "sigpr/EST_misc_sigpr.h" + +#endif /* __EST_SIGPR_H__ */ + +//@} diff --git a/aeneas/cfw/speech_tools/EST_simplestats.h b/aeneas/cfw/speech_tools/EST_simplestats.h new file mode 100644 index 00000000..3b78fbfc --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_simplestats.h @@ -0,0 +1,312 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Simple statistics (for discrete probability distributions */ +/* */ +/*=======================================================================*/ +#ifndef __EST_SIMPLESTATS_H__ +#define __EST_SIMPLESTATS_H__ + +#include "EST_String.h" +#include "EST_Token.h" +#include "EST_StringTrie.h" +#include "EST_TList.h" +#include "EST_TKVL.h" +#include "EST_types.h" + +typedef size_t int_iter; + +/** A class for managing mapping string names to integers and back again, + mainly used for representing alphabets in n-grams and grammars etc. + + This offers an efficient way of mapping a known set of string names + to integers. It is initialised from a list of names and builds + a index of those names to a set of integers. + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_Discrete { +private: + // for fast index->name + EST_StrVector namevector; + int p_def_val; + // for fast name->index + EST_StringTrie nametrie; + +public: + /// + EST_Discrete() {nametrie.clear(); p_def_val = -1;} + /// + EST_Discrete(const EST_Discrete &d) { copy(d); } + /// Initialise discrete class from given list of strings + EST_Discrete(const EST_StrList &vocab); + /// + ~EST_Discrete(); + /// + void copy(const EST_Discrete &d); + /// (re-)initialise + bool init(const EST_StrList &vocab); + + /// The number of members in the discrete + const int length(void) const { return namevector.length(); } + /** The int assigned to the given name, if it doesn't exists p\_def\_val + is returned (which is -1 by default) + */ + const int index(const EST_String &n) const { + int *i; + return (((i=(int*)nametrie.lookup(n)) != NULL) ? *i : p_def_val); + }; + + /// The name given the index + const EST_String &name(const int n) const { return namevector(n); } + + /// set the default value when a name isn't found (-1 by default) + void def_val(const EST_String &v) { p_def_val = index(v); } + + /// An alternative method for getting the int form the name + int name(const EST_String &n) const { return index(n); }; + + bool operator == (const EST_Discrete &d); + bool operator != (const EST_Discrete &d); + + EST_String print_to_string(int quote=0); + friend ostream& operator <<(ostream& s, const EST_Discrete &d); + + /// + EST_Discrete & operator = (const EST_Discrete &a) + { copy(a); return *this; } + +}; + +class Discretes { + private: + int max; + int next_free; + EST_Discrete **discretes; + public: + Discretes() {max=50;next_free=0;discretes=new EST_Discrete*[max];} + ~Discretes(); + const int def(const EST_StrList &members); + EST_Discrete &discrete(const int t) const {return *discretes[t-10];} + EST_Discrete &operator [] (const int t) const {return *discretes[t-10];} +}; + +/** A class for cummulating ``sufficient statistics'' for a set of + numbers: sum, count, sum squared. + + This collects the number, sum and sum squared for a set of number. + Offering mean, variance and standard deviation derived from the + cummulated values. + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 + */ +class EST_SuffStats { +private: + double n; // allows frequencies to be non-integers + double p_sum; + double p_sumx; +public: + /// + EST_SuffStats() {n = p_sum = p_sumx = 0.0;} + /// + EST_SuffStats(double in, double isum, double isumx) + {n = in; p_sum = isum; p_sumx = isumx;} + /// + EST_SuffStats(const EST_SuffStats &s) { copy(s); } + /// + void copy(const EST_SuffStats &s) + {n=s.n; p_sum = s.p_sum; p_sumx = s.p_sumx;} + /// reset internal values + void reset(void) {n = p_sum = p_sumx = 0.0;} + void set(double in, double isum, double isumx) + {n = in; p_sum = isum; p_sumx = isumx;} + /// number of samples in set + double samples(void) {return n;} + /// sum of values + double sum() { return p_sum; } + /// sum of squared values + double sumx() { return p_sumx; } + /// mean of currently cummulated values + double mean(void) const { return (n==0)?0.0:(p_sum / n); } + /// variance of currently cummulated values + double variance(void) const + { return ((n*p_sumx)-(p_sum*p_sum))/((double)n*(n-1)); } + /// standard deviation of currently cummulated values + double stddev(void) const { return sqrt(variance()); } + + void cumulate(double a,double count=1.0) + { n+=count; p_sum+=a*count; p_sumx+=count*(a*a); } + + /// Used to cummulate new values + EST_SuffStats &operator +=(double a) + { cumulate(a,1.0); return *this;} + /// Used to cummulate new values + EST_SuffStats &operator + (double a) + { cumulate(a,1.0); return *this;} + /// + EST_SuffStats &operator = (const EST_SuffStats &a) + { copy(a); return *this;} +}; + +enum EST_tprob_type {tprob_string, tprob_int, tprob_discrete}; +/** A class for representing probability distributions for a set of + discrete values. + + This may be used to cummulate the probability distribution of a + class of values. Values are actually help as frequencies so both + frequency and probability information may be available. Note that + frequencies are not integers because using smoothing and backoff + integers are too restrictive so they are actually represented as + doubles. + + Methods are provided to iterate over the values in a distribution, + for example + \begin{verbatim} + EST_DiscreteProbistribution pdf; + for (int i=pdf.item_start(); i < pdf.item_end(); i=pdf.item_next(i)) + { + EST_String name; + double prob; + item_prob(i,name,prob); + cout << name << ": prob " << prob << endl; + } + \end{verbatim} + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_DiscreteProbDistribution { +private: + double num_samples; // because frequencies don't have to be integers + EST_tprob_type type; + /* For known vocabularies: tprob_discrete */ + const EST_Discrete *discrete; + // was int, but frequencies don't have to be integers + EST_DVector icounts; + /* For unknown vocabularies: tprob_string */ + EST_StrD_KVL scounts; +public: + EST_DiscreteProbDistribution() : type(tprob_string), discrete(NULL), icounts(0), scounts() {init();} + /// Create with copying from an existing distribution. + EST_DiscreteProbDistribution(const EST_DiscreteProbDistribution &b); + /// Create with given vocabulary + EST_DiscreteProbDistribution(const EST_TList &vocab) + {init(); (void)init(vocab);} + /// Create using given \Ref{EST_Discrete} class as the vocabulary + EST_DiscreteProbDistribution(const EST_Discrete *d) {init(); init(d);} + /** Create using given \Ref{EST_Discrete} class as vocabulary plus given + counts + */ + EST_DiscreteProbDistribution(const EST_Discrete *d, + const double n_samples, + const EST_DVector &counts); + + /// Destructor function + ~EST_DiscreteProbDistribution() {clear();} + /// Copy all data from another DPD to this + void copy(const EST_DiscreteProbDistribution &b); + + /// Reset, clearing all counts and vocabulary + void clear(void); + /// Initialise using given vocabulary + bool init(const EST_StrList &vocab); + /// Initialise using given \Ref{EST_Discrete} as vocabulary + void init(const EST_Discrete *d); + /// Initialise + void init(); + /// Total number of example found. + double samples(void) const { return num_samples; } + /// Add this observation, may specify number of occurrences + void cumulate(const EST_String &s,double count=1); + /// Add this observation, i must be with in EST\_Discrete range + void cumulate(EST_Litem *i,double count=1); + void cumulate(int i,double count=1); + /// Return the most probable member of the distribution + const EST_String &most_probable(double *prob = NULL) const; + /** Return the entropy of the distribution + \[ -\sum_{i=1}^N(prob(i)*log(prob(i))) \] + */ + double entropy(void) const; + /// + double probability(const EST_String &s) const; + /// + double probability(const int i) const; + /// + double frequency(const EST_String &s) const; + /// + double frequency(const int i) const; + /// Used for iterating through members of the distribution + EST_Litem *item_start() const; + /// Used for iterating through members of the distribution + EST_Litem *item_next(EST_Litem *idx) const; + /// Used for iterating through members of the distribution + int item_end(EST_Litem *idx) const; + + /// During iteration returns name given index + const EST_String &item_name(EST_Litem *idx) const; + /// During iteration returns name and frequency given index + void item_freq(EST_Litem *idx,EST_String &s,double &freq) const; + /// During iteration returns name and probability given index + void item_prob(EST_Litem *idx,EST_String &s,double &prob) const; + + /// Returns discrete vocabulary of distribution + inline const EST_Discrete *const get_discrete() const { return discrete; }; + + /** Sets the frequency of named item, modifies {\tt num\_samples} + accordingly. This is used when smoothing frequencies. + */ + void set_frequency(const EST_String &s,double c); + /** Sets the frequency of named item, modifies {\tt num\_samples} + accordingly. This is used when smoothing frequencies. + */ + void set_frequency(int i,double c); + void set_frequency(EST_Litem *i,double c); + + /// Sets the frequency of named item, without modifying {\tt num\_samples}. + void override_frequency(const EST_String &s,double c); + /// Sets the frequency of named item, without modifying {\tt num\_samples}. + void override_frequency(int i,double c); + void override_frequency(EST_Litem *i,double c); + + /** Sets the number of samples. Care should be taken on setting this + as it will affect how probabilities are calculated. + */ + void set_num_samples(const double c) { num_samples = c;} + +friend ostream & operator <<(ostream &s, const EST_DiscreteProbDistribution &p); + EST_DiscreteProbDistribution &operator=(const EST_DiscreteProbDistribution &a); +}; + +#endif // __EST_SIMPLESTATS_H__ diff --git a/aeneas/cfw/speech_tools/EST_socket.h b/aeneas/cfw/speech_tools/EST_socket.h new file mode 100644 index 00000000..bdc46094 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_socket.h @@ -0,0 +1,57 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Various socket includes and definitions. */ + /* */ + /*************************************************************************/ + +#include "EST_system.h" + +#if defined(SYSTEM_IS_UNIX) +# include +# include +# include +# include +# include +# include "unix/EST_socket_unix.h" +#elif defined(SYSTEM_IS_WIN32) +# include +# include "win32/EST_socket_win32.h" +#else +# error No System Selected +#endif + diff --git a/aeneas/cfw/speech_tools/EST_sort.h b/aeneas/cfw/speech_tools/EST_sort.h new file mode 100644 index 00000000..0b75a3c5 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_sort.h @@ -0,0 +1,54 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : January 1997 */ +/*-----------------------------------------------------------------------*/ +/* Sorting Functions */ +/* */ +/*=======================================================================*/ +#ifndef __EST_SORT_H__ +#define __EST_SORT_H__ + +#include "EST_TList.h" + +// functions in Tlist_aux.C +template bool operator==(const EST_TList &a,const EST_TList &b); +template void sort(EST_TList &a); +template void ptr_sort(EST_TList &a); +template void qsort(EST_TList &a); +template void ptr_qsort(EST_TList &a); +template void sort_unique(EST_TList &l); +template void merge_sort_unique(EST_TList &l, EST_TList &m); + + +#endif // __EST_SORT_H__ diff --git a/aeneas/cfw/speech_tools/EST_speech_class.h b/aeneas/cfw/speech_tools/EST_speech_class.h new file mode 100644 index 00000000..734edc3b --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_speech_class.h @@ -0,0 +1,48 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan Black */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* EST Speech Class Header */ +/* */ +/*=======================================================================*/ + +#ifndef __SPEECH_CLASS_H__ +#define __SPEECH_CLASS_H__ + +#include "EST_Wave.h" +#include "EST_wave_aux.h" +#include "EST_Track.h" +#include "EST_track_aux.h" + +#endif /* __SPEECH_CLASS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_stats.h b/aeneas/cfw/speech_tools/EST_stats.h new file mode 100644 index 00000000..632c0e74 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_stats.h @@ -0,0 +1,50 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* EST Stats Header */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_STATS_H__ +#define __EST_STATS_H__ + +#include "EST_PST.h" +#include "EST_cluster.h" +#include "EST_simplestats.h" +#include "EST_multistats.h" + +#include "EST_viterbi.h" + +#endif /* __EST_STATS__H__ */ diff --git a/aeneas/cfw/speech_tools/EST_strcasecmp.h b/aeneas/cfw/speech_tools/EST_strcasecmp.h new file mode 100644 index 00000000..2fad88ac --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_strcasecmp.h @@ -0,0 +1,71 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Wed Jun 25 1997 */ + /* -------------------------------------------------------------------- */ + /* */ + /* Declaration for string comparison operation. */ + /* */ + /************************************************************************/ + +#ifndef __EST_STRCASECMP_H__ +#define __EST_STRCASECMP_H__ + +#ifdef __cplusplus +#include +using namespace std; +#else +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +int EST_strcasecmp(const char *s1, const char *s2, + const unsigned char *charmap); +int EST_strncasecmp(const char *s1, const char *s2, + size_t n, const unsigned char *charmap); + +#ifdef __cplusplus + } + +inline int EST_strcasecmp(const char *s1, const char *s2) + { return EST_strcasecmp(s1, s2, NULL); }; +inline int EST_strncasecmp(const char *s1, const char *s2, size_t n) + { return EST_strncasecmp(s1, s2, n, NULL); }; + +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_string_aux.h b/aeneas/cfw/speech_tools/EST_string_aux.h new file mode 100644 index 00000000..9b137414 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_string_aux.h @@ -0,0 +1,85 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor, Simon King */ +/* Date : 1994-99 */ +/*-----------------------------------------------------------------------*/ +/* Utility EST_String Functions header file */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_STRING_AUX_H__ +#define __EST_STRING_AUX_H__ + +#include "EST_TList.h" +#include "EST_String.h" +#include "EST_types.h" +#include "EST_rw_status.h" + +void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep=""); +void BracketStringtoStrList(EST_String s, EST_StrList &l, EST_String sep=""); + +EST_read_status load_StrList(EST_String filename, EST_StrList &l); +EST_write_status save_StrList(EST_String filename, EST_StrList &l, + EST_String style="words"); + + +void strip_quotes(EST_String &s, const EST_String quote_char="\""); + +// makes EST_String from integer. +EST_String itoString(int n); +// makes EST_String from float, with variable precision +EST_String ftoString(float n, int pres=3, int width=0, int l=0); +int Stringtoi(EST_String s); + +int StrListtoIList(EST_StrList &s, EST_IList &il); +int StrListtoFList(EST_StrList &s, EST_FList &il); + +void StrList_to_StrVector(EST_StrList &l, EST_StrVector &v); +void StrVector_to_StrList(EST_StrVector &v,EST_StrList &l); +int StrVector_index(const EST_StrVector &v,const EST_String &s); + +int strlist_member(const EST_StrList &l,const EST_String &s); +int strlist_index(const EST_StrList &l,const EST_String &s); + +// strips path off front of filename +EST_String basename(EST_String full, EST_String ext=""); + +// this is not the right place for these +void IList_to_IVector(EST_IList &l, EST_IVector &v); +void IVector_to_IList(EST_IVector &v,EST_IList &l); +int IVector_index(const EST_IVector &v,const int s); + +int ilist_member(const EST_IList &l,int i); +int ilist_index(const EST_IList &l,int i); + +#endif // __EST_STRING_AUX_H__ diff --git a/aeneas/cfw/speech_tools/EST_system.h b/aeneas/cfw/speech_tools/EST_system.h new file mode 100644 index 00000000..dd1d815a --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_system.h @@ -0,0 +1,59 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Make all systems look similar enough to unix to be easy to cope with. */ + /* */ + /*************************************************************************/ + +#if !defined(EST_SYSTEM_H) +# define EST_SYSTEM_H 1 + +#if defined(SYSTEM_IS_WIN32) +# define _WINSOCKAPI_ /* horrible hack */ +# define _LPCVOID_DEFINED /* and another */ +# include +# include "win32/EST_defines_win32.h" +# if defined(_MSC_VER) +# define VISUAL_CPP 1 +# endif +#else +# define SYSTEM_IS_UNIX 1 +# include +# include "unix/EST_defines_unix.h" +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_tilt.h b/aeneas/cfw/speech_tools/EST_tilt.h new file mode 100644 index 00000000..3a4cee8b --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_tilt.h @@ -0,0 +1,183 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : February 1996 */ +/*-----------------------------------------------------------------------*/ +/* Intonational Event Include file */ +/* */ +/*=======================================================================*/ +#ifndef __RFC_H__ +#define __RFC_H__ + +#include "ling_class/EST_Relation.h" +#include "EST_util_class.h" +#include "EST_speech_class.h" +#include "EST_Event.h" + +/**@name Tilt functions + +Functions for: + +Generating RFC and Tilt parameters from F0 contours +Converting RFC to Tilt parameters and vice-versa +Synthesizing F0 contours from RFC and Tilt events + + +*/ +//@{ + +/** Fill op with sensible default parameters for RFC analysis. + */ +void default_rfc_params(EST_Features &op); + +/** Produce a set of RFC parameterized events given approximate event + boundaries and a smoothed F0 contour. See for a description of this process. + + @param f0: Smoothed continuous F0 contour. An error will occur + if any unvoiced regions are detected in the contour. Use the + function smooth_pda to smooth and interpolate a normal contour. + @param ev_list: list of events, each containing approximate start + and end times of the events. On completion each event in this list + will have a set of RFC parameters. + @param op: parameters used to control analysis process. + + */ + +void rfc_analysis(EST_Track &fz, EST_Relation &event_list, EST_Features &op); +/** Fill op with sensible default parameters for RFC analysis + */ + +void tilt_analysis(EST_Track &fz, EST_Relation &event_list, EST_Features &op); +/** Fill op with sensible default parameters for RFC analysis + */ + + +void fill_rise_fall_values(EST_Track &fz, float amp, float start_f0); + +/** Generate an F0 contour given a list RFC events. + +@param f0: Generated F0 contour +@param ev_list: list of events, each containing a set of RFC parameters +@param f_shift: frame shift in seconds of the generated contour +@param no_conn: Do not join events with straight lines if set to 1 +*/ + +void rfc_synthesis(EST_Track &f0, EST_Relation &ev_list, + float f_shift, int no_conn); + +/** Generate an F0 contour given a list Tilt events. + +This function simply calls \Ref{tilt_to_rfc} followed by \Ref{rfc_synthesis}. + +@param f0: Generated F0 contour +@param ev_list: list of events, each containing a set of Tilt parameters +@param f_shift: frame shift in seconds of the generated contour +@param no_conn: Do not join events with straight lines if set to 1 +*/ + +void tilt_synthesis(EST_Track &track, EST_Relation &ev_list, + float f_shift, int no_conn); + +/** Convert a single set of local tilt parameters to local RFC parameters. +amp + +@param tilt: input tilt parameters, named amp, dur and tilt +@param rfc: output RFC parameters, name rise_amp, fall_amp, rise_dur and fall_dur + +*/ +void tilt_to_rfc(EST_Features &tilt, EST_Features &rfc); + +/** Convert a single set of local RFC parameters to local tilt +parameters. See for a description of +how this is performed. + + +@param rfc: input RFC parameters, named rise_amp, fall_amp, rise_dur and fall_dur +@param tilt: output tilt parameters, named amp, dur and tilt */ +void rfc_to_tilt(EST_Features &rfc, EST_Features &tilt); + +/** For each tilt events in ev_tilt, produce a set of RFC parameters. + The tilt parameters are stored as the following features in the event: + + +tilt.amp +tilt.dur +tilt.tilt + + + A set of features with the following names are created: + + +rfc.rise_amp +rfc.rise_dur +rfc.fall_amp +rfc.fall_dur + + + The original tilt features are not deleted. + +*/ +void tilt_to_rfc(EST_Relation &ev_tilt); + +/** For each tilt events in ev_rfc, produce a set of Tiltparameters. + The RFC parameters are stored as the following features in the event: + + +rfc.rise_amp +rfc.rise_dur +rfc.fall_amp +rfc.fall_dur + + + A set of features with the following names are created: + + +tilt.amp +tilt.dur +tilt.tilt + + + The original RFC features are not deleted. + +*/ +void rfc_to_tilt(EST_Relation &ev_rfc); + +int validate_rfc_stream(EST_Relation &ev); +void fill_rfc_types(EST_Relation &ev); + +//@} + + + +#endif /* RFC */ diff --git a/aeneas/cfw/speech_tools/EST_track_aux.h b/aeneas/cfw/speech_tools/EST_track_aux.h new file mode 100644 index 00000000..4e7cf009 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_track_aux.h @@ -0,0 +1,252 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + + +/**@name EST_track_aux.h + * EST_Track Auxiliary functions + * @author Paul Taylor + * @version $Id: EST_track_aux.h,v 1.4 2004/05/24 11:15:51 korin Exp $ + */ + +//@{ + +#ifndef __EST_TRACK_AUX_H__ +#define __EST_TRACK_AUX_H__ + +#include "EST_FMatrix.h" +#include "EST_TList.h" +#include "ling_class/EST_Relation.h" +#include "EST_Option.h" +#include "EST_Track.h" +#include "EST_TBuffer.h" + +void track_smooth(EST_Track &c, float x, EST_String stype = ""); +void time_med_smooth(EST_Track &c, float x); +void time_mean_smooth(EST_Track &c, float x); +void simple_med_smooth(EST_Track &c, int n, int channel=0); +void simple_mean_smooth(EST_Track &c, int n, int channel=0); + +/** Calculate the mean absolute error between the same channel in + * two tracks. This is given by \[\frac{1}{n}\sum_{i=1}^{n}|a_{i} - b_{i}|\] + * @see abs_error, rms_error(EST_Track &a, EST_Track &b) + */ + +float abs_error(EST_Track &a, EST_Track &b, int channel); + +void absolute(EST_Track &tr); +void normalise(EST_Track &tr); +void normalise(EST_Track &tr, float mean, float sd, int channel, + float upper, float lower); +void normalise(EST_Track &tr, EST_FVector &mean, EST_FVector &sd, + float upper, float lower); +void normalise(EST_TrackList &trlist, EST_FVector &mean, + EST_FVector &sd, float upper, float lower); + +/** Calculate the simple derivative of a track. This is given by + * \[a_{i+1} - a_{i}\] The values in the resultant track are spaced + * midway between the values in the input track, resulting in 1 fewer + * frames in the track. This is a very local estimation of the derivative + * of the track at a point in time. A smoother value can be obtained + * using the delta function. + * @see delta + */ + +EST_Track differentiate(EST_Track &c, float samp_int=0.0); +EST_Track difference(EST_Track &a, EST_Track &b); + +float mean( const EST_Track &a, int channel ); +void mean( const EST_Track &a, EST_FVector &m ); + +void meansd(EST_Track &a, float &m, float &sd, int channel); + +/** Calculate the root mean square error between the same channel in + * two tracks. The channel is identified by its index. + * @see abs_error, float rms_error(EST_Track &a, EST_Track &b) + */ +float rms_error(EST_Track &a, EST_Track &b, int channel); + +float correlation(EST_Track &a, EST_Track &b, int channel); + +void meansd(EST_Track &a, EST_FVector &m, EST_FVector &sd); + +/** Calculate the root mean square error between each channels in two + * tracks. For two tracks of M channels, the result is returned as an + * EST_FVector of size M, with element {\it i} representing the + * rms error for channel {\it i}. + * @see abs_error, rms_error + */ +EST_FVector rms_error(EST_Track &a, EST_Track &b); + +EST_FVector abs_error(EST_Track &a, EST_Track &b); EST_FVector +correlation(EST_Track &a, EST_Track &b); + +/// Move the start and end variables to the nearest frame. +void align_to_track(EST_Track &tr, float &start, float &end); +/// Move the start and end variables to the nearest frame. +void align_to_track(EST_Track &tr, int &start, int &end, int sample_rate); +/// Move the start and end variables to the start and end of the nearest frame. +void move_to_frame_ends(EST_Track &tr, + int &start, int &end, + int sample_rate, float offset=0.0); +/// Index of the frame whose start boundary +int nearest_boundary(EST_Track &tr, float time, int sample_rate, float offset=0); + +/// Move the track so that it starts at the indicated time. +void set_start(EST_Track &tr, float start); +/// Move the track by {\it shift} seconds +void move_start(EST_Track &tr, float shift); + +EST_Track error(EST_Track &ref, EST_Track &test, int relax= 0); +void extract(EST_Track &orig, float start, float end, EST_Track &res); + +int track_divide(EST_TrackList &mtfr, EST_Track &fv, EST_Relation &key); +void ParallelTracks(EST_Track &a, EST_TrackList &list,const EST_String &style); +void track_info(EST_Track &track); + +EST_String options_track_filetypes(void); +EST_String options_track_filetypes_long(void); +EST_String options_subtrack(void); + +int read_track(EST_Track &tr, const EST_String &in_file, EST_Option &al); + +/** Return the frame size in {\bf seconds} based on analysis of +current time points. This function basically determines the local +frame size (shift) by subtracting the current time point from the next +time point. If the {\tt prefer_prev} flag is set to {\tt true}, or the +index is the last in the track, the size is determined by subtracting +the previous time point from the current one. + +This is most commonly used in pitch synchronous analysis to determine +the local pitch period. + +@see get_frame_size +*/ + +float get_time_frame_size(EST_Track &pms, int i, int prefer_prev = 0); + +/** Return the frame size in {\bf samples} based on analysis of +current time points. This function basically determines the local +frame size (shift) by subtracting the current time point from the next +time point. If the {\tt prefer_prev} flag is set to {\tt true}, or the +index is the last in the track, the size is determined by subtracting +the previous time point from the current one. + +This is most commonly used in pitch synchronous analysis to determine +the local pitch period. + +@see get_time_frame_size +*/ +int get_frame_size(EST_Track &pms, int current_pos, int sample_rate, + int prefer_prev=0); + + +/// How many coefficients in track (looks for Coef0 and coefN channels) +int get_order(const EST_Track &t, EST_CoefficientType type, int d=0); +int get_order(const EST_Track &t); + +/// Total the length channel values. +int sum_lengths(const EST_Track &t, + int sample_rate, + int start_frame=0, int end_frame=-1); + +/// Find the start point in the signal of the sections of speech related to each frame. +void get_start_positions(const EST_Track &t, + int sample_rate, + EST_TBuffer &pos); + +/**@name Analysis frame position + * Functions which define which part of a single is associated with a + * given frame in a track. + *

+ * This is defined here in one place for consistency. They are inline since + * they tend to be used in inner loops. There are two versions, + * the second for when there are offsets in the track. + */ +//@{ + +/// Get the start and end of a given frame (in samples) +static inline void get_frame(const EST_Track &tr, int sample_rate, + int f, + int &start, int ¢er, int &end) +{ + center = (int)(tr.t(f)*sample_rate + 0.5); + start = center - (int)(tr.a(f, channel_length)/2.0); + end = start + (int)(tr.a(f, channel_length)); +} + +/// Get the start and end of a given frame (in seconds) +static inline void get_frame(const EST_Track &tr, int sample_rate, + int f, + float &start, float ¢er, float &end) +{ + center = tr.t(f); + start = center - tr.a(f, channel_length)/(float)sample_rate/2.0; + end = start + tr.a(f, channel_length)/(float)sample_rate; +} + +/// Get the start and end of a given frame (in samples) +static inline void get_frame_o(const EST_Track &tr, int sample_rate, + int f, + int &start, int ¢er, int &end) +{ + center = (int)(tr.t(f)*sample_rate + tr.a(f,channel_offset) + 0.5); + start = center - (int)(tr.a(f, channel_length)/2.0); + end = start + (int)(tr.a(f, channel_length)); +} + +/// Get the start and end of a given frame (in seconds) +static inline void get_frame_o(const EST_Track &tr, int sample_rate, + int f, + float &start, float ¢er, float &end) +{ + center = tr.t(f) + tr.a(f,channel_offset)/(float)sample_rate; + start = center - tr.a(f, channel_length)/(float)sample_rate/2.0; + end = start + tr.a(f, channel_length)/(float)sample_rate; +} + +//@} + +// take one of the channels as the timeline +void channel_to_time(EST_Track &tr, int channel, float scale=1.0); +void channel_to_time(EST_Track &tr, EST_ChannelType c,float scale=1.0); +void channel_to_time(EST_Track &tr, const EST_String c_name, float scale=1.0); + +void channel_to_time_lengths(EST_Track &tr, int channel, float scale=1.0); +void channel_to_time_lengths(EST_Track &tr, EST_ChannelType c,float scale=1.0); +void channel_to_time_lengths(EST_Track &tr, const EST_String c_name, float scale=1.0); + +/* Allow EST_Track to be used in an EST_Val */ +VAL_REGISTER_CLASS_DCLS(track,EST_Track) + +#endif /* __EST_TRACK_AUX_H__ */ +//@} diff --git a/aeneas/cfw/speech_tools/EST_types.h b/aeneas/cfw/speech_tools/EST_types.h new file mode 100644 index 00000000..acf08ec4 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_types.h @@ -0,0 +1,81 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* Type defines for Common Types */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_TYPES_H__ +#define __EST_TYPES_H__ + +#include "EST_TList.h" +#include "EST_TVector.h" +#include "EST_String.h" +#include "EST_TKVL.h" +#include "EST_FMatrix.h" +#include "EST_DMatrix.h" +#include "EST_IMatrix.h" +#include "EST_SMatrix.h" + +typedef EST_TVector EST_StrVector; + +typedef EST_TSimpleVector EST_IVector; +typedef EST_TSimpleVector EST_SVector; +typedef EST_TSimpleVector EST_CVector; + +// DVector is an inherited TSimpleVector in EST_DMatrix.h +// FVector is an inherited TSimpleVector in EST_FMatrix.h + +typedef EST_TList EST_IList; +typedef EST_TList EST_FList; +typedef EST_TList EST_DList; + +typedef EST_TKVL EST_II_KVL; + +typedef EST_TList > EST_IListList; + +typedef EST_TList EST_StrList; +typedef EST_TList EST_IList; +typedef EST_TList EST_FList; + +typedef EST_TList > EST_StrListList; +typedef EST_TVector EST_StrListVector; +typedef EST_TKVL EST_StrStr_KVL; +typedef EST_TKVL EST_StrI_KVL; +typedef EST_TKVL EST_StrF_KVL; +typedef EST_TKVL EST_StrD_KVL; +//typedef EST_TKVL EST_StrVal_KVL; + +#endif // __EST_TYPES_H__ diff --git a/aeneas/cfw/speech_tools/EST_unix.h b/aeneas/cfw/speech_tools/EST_unix.h new file mode 100644 index 00000000..b9f2f152 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_unix.h @@ -0,0 +1,63 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Declare unix things. On unix just include header files, */ + /* on other systems define compatibility routines */ + /* */ + /*************************************************************************/ + +#if !defined(EST_UNIX_H) +# define EST_UNIX_H 1 + +#include "EST_system.h" + +#if defined(SYSTEM_IS_UNIX) +# include +# include +# include +# include +# include +# include +# include +#elif defined(SYSTEM_IS_WIN32) +# include +# include "win32/EST_unix_win32.h" +#else +# error No System Selected +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_util_class.h b/aeneas/cfw/speech_tools/EST_util_class.h new file mode 100644 index 00000000..d3833c43 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_util_class.h @@ -0,0 +1,58 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan Black */ +/* Date : February 1997 */ +/*-----------------------------------------------------------------------*/ +/* EST Util Class Header */ +/* */ +/*=======================================================================*/ + +#ifndef __UTIL_CLASS_H__ +#define __UTIL_CLASS_H__ + +#include "EST_TKVL.h" +#include "EST_Option.h" +#include "EST_TList.h" +#include "EST_TMatrix.h" +#include "EST_TVector.h" +#include "EST_Token.h" + +#include "EST_FMatrix.h" +#include "EST_DMatrix.h" +#include "EST_StringTrie.h" + +#include "EST_cmd_line.h" +// EST_io_aux.h ??? +// EST_sort.h ??? + +#endif /* __UTIL_CLASS_H__ */ diff --git a/aeneas/cfw/speech_tools/EST_viterbi.h b/aeneas/cfw/speech_tools/EST_viterbi.h new file mode 100644 index 00000000..a1c3fc75 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_viterbi.h @@ -0,0 +1,228 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Authors: Alan W Black */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* A viterbi decoder */ +/* */ +/* User provides the candidates, target and combine score function */ +/* and it searches for the best path through the candidates */ +/* */ +/*=======================================================================*/ + +#ifndef __VERTERBI_H__ +#define __VERTERBI_H__ + +#include "EST_cutils.h" +#include "EST_Features.h" +#include "ling_class/EST_Relation.h" + +/** Internal class to \Ref{EST_Viterbi_Decoder} used to a represent + a candidate. + + These objects need to be created and set by the user of the Viterbi + decoder. + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_VTCandidate { + private: + public: + EST_VTCandidate() {score=0.0; next=0; s=0; } + ~EST_VTCandidate() {if (next != 0) delete next;} + float score; + EST_Val name; + int pos; + EST_Item *s; + EST_VTCandidate *next; +}; + + +/** Internal class to \Ref{EST_Viterbi_Decoder} used to a represent + a link in a path the candidates. + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_VTPath { + private: + public: + EST_VTPath() {score=0.0; from=0; next=0; c=0;} + ~EST_VTPath() {if (next != 0) delete next;} + double score; /* cumulative score for path */ + int state; + EST_Features f; + EST_VTCandidate *c; + EST_VTPath *from; + EST_VTPath *next; +}; + +/** Internal class to \Ref{EST_Viterbi_Decoder used to a node in + the decoder table + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_VTPoint { + private: + public: + EST_VTPoint() {next=0; s=0; paths=0; num_paths=0; cands=0; st_paths=0; num_states=0;} + ~EST_VTPoint(); + EST_Item *s; + int num_states; + int num_paths; + EST_VTCandidate *cands; + EST_VTPath *paths; + EST_VTPath **st_paths; + EST_VTPoint *next; +}; + +typedef EST_VTCandidate *(*uclist_f_t)(EST_Item *s,EST_Features &f); +typedef EST_VTPath *(*unpath_f_t)(EST_VTPath *p,EST_VTCandidate *c, + EST_Features &f); + +/** A class that offers a generalised Viterbi decoder. + + This class can be used to find the best path through a set + of candidates based on likelihoods of the candidates and + some combination function. The candidate list and joining + are not included in the decoder itself but are user defined functions + that are specified at construction time. + + Those functions need to return a list of candidates and score + a join of a path to a candidate and (optionally define a state). + + Although this offers a full Viterbi search it may also be used as + a generalised beam search. + + See {\tt viterbi_main.cc} for an example of using this. + + @author Alan W Black (awb@cstr.ed.ac.uk): July 1996 +*/ +class EST_Viterbi_Decoder { + private: + int num_states; + + /// very detailed info - for developers + int debug; + + /// less detailed info than debug - for users + int trace; + + int beam_width; + int cand_width; + int big_is_good; + uclist_f_t user_clist; + unpath_f_t user_npath; + EST_VTPoint *timeline; + + /// pruning parameters + bool do_pruning; + float overall_path_pruning_envelope_width; + float candidate_pruning_envelope_width; + + void add_path(EST_VTPoint *p, EST_VTPath *np); + void vit_add_path(EST_VTPoint *p, EST_VTPath *np); + void vit_add_paths(EST_VTPoint *p, EST_VTPath *np); + EST_VTPath *find_best_end() const; + const int betterthan(const float a,const float b) const; + void prune_initialize(EST_VTPoint *p, + double &best_score, double &best_candidate_score, + double &score_cutoff, double &candidate_cutoff, + int &cand_count); + public: + + /// For holding values to pass to user called functions + EST_Features f; + + /// Unfortunately using MAX_DOUBLE doesn't do the right thing + /// (e.g. comparison don't work with MAX_DOUBLE on alphas), so + /// we declare our own large number. + const double vit_a_big_number; + + /** Construct a decoder with given candidate function and join function, + as number of states is given this implies a beam search + */ + EST_Viterbi_Decoder(uclist_f_t a, unpath_f_t b); + /** Construct a decoder with given candidate function and join function + with a state size as specified. + */ + EST_Viterbi_Decoder(uclist_f_t a, unpath_f_t b, int num_states); + /// + ~EST_Viterbi_Decoder(); + /// Only for use in beam search mode: number of paths to consider + void set_beam_width(int w) {beam_width = w;} + /// Only for use in beam search mode: number of candidates to consider + void set_cand_width(int w) {cand_width = w;} + /// Output some debugging information + void set_debug(int d) {debug = d;} + + /** Define whether good scores are bigger or smaller. This allows + the search to work for likelihoods probabilities, scores or + whatever + */ + void set_big_is_good(int flag) { big_is_good = flag; } + /** Add a new candidate to list if better than others, pruning + the list if required. + */ + EST_VTCandidate *add_cand_prune(EST_VTCandidate *newcand, + EST_VTCandidate *allcands); + + bool vit_prune_path(double path_score, double score_cutoff); + + /// Build the initial table from a \Ref{EST_Relation} + void initialise(EST_Relation *r); + + /// set beam widths for pruning + void set_pruning_parameters(float beam, float ob_beam); + + void turn_on_debug(); + void turn_on_trace(); + + /// Do the the actual search + void search(void); + /** Extract the result from the table and store it as a feature + on the related \Ref{EST_Item} in the given \Ref{EST_Relation} + named as {\tt n}. Return FALSE if no path is found. + */ + bool result(const EST_String &n); + + /** Extract the end point of the best path found during search. + Return FALSE if no path is found. + */ + bool result( EST_VTPath **bestPathEnd ); + + /// Copy named feature from the best path to related stream item + void copy_feature(const EST_String &n); +}; + + +#endif // __VERTERBI_H__ diff --git a/aeneas/cfw/speech_tools/EST_walloc.h b/aeneas/cfw/speech_tools/EST_walloc.h new file mode 100644 index 00000000..206f1e3b --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_walloc.h @@ -0,0 +1,61 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan W Black */ +/* Date : July 1996 */ +/*-----------------------------------------------------------------------*/ +/* Safe(-er) allocation functions. */ +/* */ +/*=======================================================================*/ + +#if !defined(__EST_WALLOC_H__) + +#if defined(__cplusplus) +extern "C" { +#endif + +void *safe_walloc(int size); +void *safe_wcalloc(int size); +void *safe_wrealloc(void *ptr, int size); +#define walloc(TYPE,SIZE) ((TYPE *)safe_walloc(sizeof(TYPE)*(SIZE))) +#define wcalloc(TYPE,SIZE) ((TYPE *)safe_wcalloc(sizeof(TYPE)*(SIZE))) +#define wrealloc(PTR,TYPE,SIZE) ((TYPE *)safe_wrealloc((void *)(PTR), sizeof(TYPE)*(SIZE))) +char *wstrdup(const char *s); +void wfree(void *p); + +void debug_memory_summary(); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/aeneas/cfw/speech_tools/EST_wave_aux.h b/aeneas/cfw/speech_tools/EST_wave_aux.h new file mode 100644 index 00000000..942ffb34 --- /dev/null +++ b/aeneas/cfw/speech_tools/EST_wave_aux.h @@ -0,0 +1,130 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor and Alan Black */ +/* Date : May 1996 */ +/*-----------------------------------------------------------------------*/ +/* EST_Wave class auxiliary functions */ +/* */ +/*=======================================================================*/ + + +/**@name EST_wave_aux +Auxiliary functions for processing waveforms. + */ + +//@{ + +#ifndef __EST_WAVE_AUX_H__ +#define __EST_WAVE_AUX_H__ + +#include "EST_String.h" +#include "EST_Wave.h" +#include "ling_class/EST_Relation.h" +#include "EST_Option.h" +#include "EST_FMatrix.h" +#include "EST_TNamedEnum.h" + + +int wave_extract_channel(EST_Wave &single, const EST_Wave &multi, int channel); + +void wave_combine_channels(EST_Wave &combined, const EST_Wave &multi); + +int wave_subwave(EST_Wave &subsig,EST_Wave &sig,int offset,int length); + +int wave_divide(EST_WaveList &wl, EST_Wave &sig, EST_Relation &keylab, + const EST_String &ext); + +int wave_extract(EST_Wave &part, EST_Wave &sig, EST_Relation &keylab, + const EST_String &file); + +void add_waves(EST_Wave &s, const EST_Wave &m); + +EST_Wave difference(EST_Wave &a, EST_Wave &b); +float rms_error(EST_Wave &a, EST_Wave &b, int channel); +float abs_error(EST_Wave &a, EST_Wave &b, int channel); +float correlation(EST_Wave &a, EST_Wave &b, int channel); + +EST_FVector rms_error(EST_Wave &a, EST_Wave &b); +EST_FVector abs_error(EST_Wave &a, EST_Wave &b); +EST_FVector correlation(EST_Wave &a, EST_Wave &b); + +EST_Wave error(EST_Wave &ref, EST_Wave &test, int relax); + +void absolute(EST_Wave &a); + +EST_read_status read_wave(EST_Wave &sig, const EST_String &in_file, + EST_Option &al); +EST_write_status write_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al); +void wave_info(EST_Wave &w); +void invert(EST_Wave &sig); + + +void differentiate(EST_Wave &sig); +void reverse(EST_Wave &sig); + +void ulaw_to_short(const unsigned char *ulaw,short *data,int length); +void alaw_to_short(const unsigned char *alaw,short *data,int length); +void uchar_to_short(const unsigned char *chars,short *data,int length); +void short_to_char(const short *data,unsigned char *chars,int length); +void short_to_ulaw(const short *data,unsigned char *ulaw,int length); +void short_to_alaw(const short *data,unsigned char *alaw,int length); + +// Used when setting Waves in Features +VAL_REGISTER_CLASS_DCLS(wave,EST_Wave) + +enum EST_sample_type_t { + st_unknown, + st_schar, + st_uchar, + st_short, + st_shorten, + st_int, + st_float, + st_double, + st_mulaw, + st_adpcm, + st_alaw, + st_ascii}; + + +enum EST_write_status wave_io_save_header(FILE *fp, + const int num_samples, const int num_channels, + const int sample_rate, + const EST_String& stype, const int bo, + const EST_String& ftype); + +extern EST_TNamedEnum EST_sample_type_map; + +#endif /* __EST_WAVE_AUX_H__ */ + +//@} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Complex.cc b/aeneas/cfw/speech_tools/base_class/EST_Complex.cc new file mode 100644 index 00000000..91426a6b --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Complex.cc @@ -0,0 +1,115 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : December 1997 */ +/*-----------------------------------------------------------------------*/ +/* */ +/*=======================================================================*/ + +#include "EST_Complex.h" + + +// Addition + +EST_Complex operator + (const EST_Complex& z1, const EST_Complex &z2) +{ + return EST_Complex(z1.r + z2.r, z1.i + z2.i); +} + +EST_Complex operator + (const EST_Complex& z, float x) +{ + return EST_Complex(z.r + x, z.i); +} + +EST_Complex operator + (float x, const EST_Complex &z) +{ + return EST_Complex(z.r + x, z.i); +} + +// Subtraction + +EST_Complex operator - (const EST_Complex& z1, const EST_Complex &z2) +{ + return EST_Complex(z1.r - z2.r, z1.i - z2.i); +} + +EST_Complex operator - (const EST_Complex& z, float x) +{ + return EST_Complex(z.r -x, z.i); +} + +EST_Complex operator - (float x, const EST_Complex &z) +{ + return EST_Complex(x - z.r, - z.i); +} + +// Multiplication + +EST_Complex operator * (const EST_Complex& z1, const EST_Complex &z2) +{ + return EST_Complex((z1.r * z2.r) - (z1.i * z2.i), (z1.r * z2.i) + (z1.i * z2.r)); +} + +EST_Complex operator * (const EST_Complex& z, float x) +{ + return EST_Complex(z.r * x, z.i *x); +} + +EST_Complex operator * (float x, const EST_Complex &z) +{ + return EST_Complex(z.r * x, z.i *x); +} + +// Division + +EST_Complex operator / (const EST_Complex& z1, const EST_Complex &z2) +{ + double m = z1.mag(); + + EST_Complex inv(z1.r / m, z1.i /m); + + return inv * z2; +} + +EST_Complex operator / (const EST_Complex& z, float x) +{ + return EST_Complex(z.r / x, z.i / x); +} + +EST_Complex operator / (float x, const EST_Complex &z) +{ + EST_Complex a(x, 0.0); + return (z / a); +} + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_DMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_DMatrix.cc new file mode 100644 index 00000000..4040277b --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_DMatrix.cc @@ -0,0 +1,815 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ + /*************************************************************************/ + /* */ + /* Author : Simon King */ + /* Date : February 1999 */ + /* --------------------------------------------------------------------- */ + /* Double matrix class - copied from DMatrix ! */ + /* */ + /*************************************************************************/ + +#include +#include +#include +#include +#include +#include "EST_String.h" +#include "EST_types.h" +#include "EST_FileType.h" +#include "EST_Option.h" +#include "EST_DMatrix.h" +#include "EST_cutils.h" // for swap functions +#include "EST_Token.h" +#include "rateconv.h" + +EST_String EST_DMatrix::default_file_type = "est_ascii"; + +EST_DMatrix::EST_DMatrix(const EST_DMatrix &a, int b) +:EST_TSimpleMatrix(a.num_rows(), a.num_columns()) +{ + double vv = 0.0; + if (b < 0) + return; + if (b == 0) + fill(vv); +} + +EST_DMatrix & EST_DMatrix::operator+=(const EST_DMatrix &a) +{ + int i, j; + if (a.num_columns() != num_columns()) + { + cerr <<"Matrix addition error: bad number of columns\n"; + return *this; + } + if (a.num_rows() != num_rows()) + { + cerr <<"Matrix addition error: bad number of rows\n"; + return *this; + } + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) += a.a_no_check(i,j); + + return *this; +} + +EST_DMatrix & EST_DMatrix::operator-=(const EST_DMatrix &a) +{ + int i, j; + if (a.num_columns() != num_columns()) + { + cerr <<"Matrix subtraction error: bad number of columns\n"; + return *this; + } + if (a.num_rows() != num_rows()) + { + cerr <<"Matrix subtraction error: bad number of rows\n"; + return *this; + } + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) -= a.a_no_check(i,j); + + return *this; +} + +EST_DMatrix & EST_DMatrix::operator*=(const double f) +{ + + int i,j; + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) *= f; + + return *this; +} + +EST_DMatrix & EST_DMatrix::operator/=(const double f) +{ + + int i,j; + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) /= f; + + return *this; +} + +EST_DMatrix operator+(const EST_DMatrix &a, const EST_DMatrix &b) +{ + EST_DMatrix ab; + int i, j; + if (a.num_columns() != b.num_columns()) + { + cerr <<"Matrix addition error: bad number of columns\n"; + return ab; + } + if (a.num_rows() != b.num_rows()) + { + cerr <<"Matrix addition error: bad number of rows\n"; + return ab; + } + ab.resize(a.num_rows(), a.num_columns()); + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + ab.a_no_check(i, j) = a.a_no_check(i, j) + b.a_no_check(i, j); + + return ab; +} + +EST_DMatrix operator-(const EST_DMatrix &a,const EST_DMatrix &b) +{ + EST_DMatrix ab; + int i, j; + + if (a.num_columns() != b.num_columns()) + { + cerr <<"Matrix subtraction error: bad number of columns:" << + a.num_columns() << " and " << b.num_columns() << endl; + return ab; + } + if (a.num_rows() != b.num_rows()) + { + cerr <<"Matrix subtraction error: bad number of rows\n"; + return ab; + } + ab.resize(a.num_rows(), a.num_columns()); + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + ab.a_no_check(i, j) = a.a_no_check(i, j) - b.a_no_check(i, j); + + return ab; +} + +EST_DMatrix operator*(const EST_DMatrix &a, const double x) +{ + EST_DMatrix b(a, 0); + int i, j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + b.a_no_check(i,j) = a.a_no_check(i,j) * x; + + return b; +} + +EST_DVector operator*(const EST_DMatrix &a, const EST_DVector &v) +{ + // treat the vector as a column vector + // multiply each row of the matrix in turn by the vector + + EST_DVector b; + b.resize(a.num_rows()); + + if(a.num_columns() != v.n()) + { + cerr <<"Matrix-vector multiplication error: matrix rows != vector size" + << endl; + return b; + } + + int i, j; + for (i = 0; i < a.num_rows(); ++i){ + b[i] = 0.0; + for (j = 0; j < a.num_columns(); ++j) + b.a_no_check(i) += a.a_no_check(i,j) * v.a_no_check(j); + } + return b; +} + +EST_DVector operator+(const EST_DVector &a, const EST_DVector &b) +{ + EST_DVector ab; + int i; + if (a.length() != b.length()) + { + cerr <<"Vector addition error: mismatched lengths\n"; + return ab; + } + + ab.resize(a.length()); + for (i = 0; i < a.length(); ++i) + ab.a_no_check(i) = a.a_no_check(i) + b.a_no_check(i); + + return ab; +} + +EST_DVector operator-(const EST_DVector &a, const EST_DVector &b) +{ + EST_DVector ab; + int i; + if (a.length() != b.length()) + { + cerr <<"Vector subtraction error: mismatched lengths\n"; + return ab; + } + + ab.resize(a.length()); + for (i = 0; i < a.length(); ++i) + ab.a_no_check(i) = a.a_no_check(i) - b.a_no_check(i); + + return ab; +} + + +EST_DVector operator*(const EST_DVector &v,const EST_DMatrix &a) +{ + // treat the vector as a row vector + // multiply the vector by each column of the matrix in turn + + EST_DVector b; + b.resize(a.num_columns()); + + if(a.num_columns() != v.n()) + { + cerr <<"Matrix-vector multiplication error: matrix rows != vector size" + << endl; + return b; + } + + int i, j; + for (j = 0; j < a.num_columns(); ++j){ + b[j] = 0.0; + for (i = 0; i < a.num_rows(); ++i) + b.a_no_check(i) += a.a_no_check(i,j) * v.a_no_check(j); + } + return b; +} + + +#if 0 +EST_DMatrix operator/(const EST_DMatrix &a, double x) +{ + return (a * (1/x)); +} +#endif + +EST_DMatrix operator*(const EST_DMatrix &a, const EST_DMatrix &b) +{ + EST_DMatrix ab; + multiply(a,b,ab); + return ab; +} + +void multiply(const EST_DMatrix &a, const EST_DMatrix &b, EST_DMatrix &ab) +{ + + if (a.num_columns() != b.num_rows()) + { + cerr <<"Matrix multiply error: a.num_columns() != b.num_rows()\n"; + return; + } + + ab.resize(a.num_rows(), b.num_columns()); + int i, j, k, n; + n = a.num_columns(); // could also be b.num_rows() + + for (i = 0; i < a.num_rows(); ++i) + for (k = 0; k < b.num_columns(); ++k) + { + ab.a_no_check(i, k) = 0.0; + for (j = 0; j < n; ++j) + ab.a_no_check(i, k) += + a.a_no_check(i, j) * b.a_no_check(j, k); + } +} + +void EST_DMatrix::copyin(double **inx, int rows, int cols) +{ + int i, j; + + resize(rows, cols); + + for (i = 0; i < rows; ++i) + for (j = 0; j < cols; ++j) + a_no_check(i,j) = inx[i][j]; + +} + +EST_write_status EST_DMatrix::save(const EST_String &filename, + const EST_String &type) +{ + + if ((type == "est_ascii") || (type == "est_binary")) + return est_save(filename,type); + else + { // the old stuff raw unheadered + int i, j; + ostream *outf; + if (filename == "-") + outf = &cout; + else + outf = new ofstream(filename); + + outf->precision(25); + if (!(*outf)) + { + cerr << "DMatrix: can't open file \"" << filename + <<"\" for writing" << endl; + return misc_write_error; + } + + for (i = 0; i < num_rows(); ++i) + { + for (j = 0; j < num_columns(); ++j) + *outf << a_no_check(i,j) << " "; + *outf << endl; + } + + if (outf != &cout) + delete outf; + + return write_ok; + } +} + +EST_write_status EST_DMatrix::est_save(const EST_String &filename, + const EST_String &type) +{ + // Binary save with short header for byte swap and sizes + int i,j; + FILE *fd; + if (filename == "-") + fd = stdout; + else if ((fd = fopen(filename, "wb")) == NULL) + { + cerr << "EST_DMatrix: binsave: failed to open \"" << filename << + "\" for writing" << endl; + return misc_write_error; + } + + fprintf(fd,"EST_File dmatrix\n"); + fprintf(fd,"version 1\n"); + if (type == "est_binary") + { + fprintf(fd,"DataType binary\n"); + if (EST_LITTLE_ENDIAN) + fprintf(fd,"ByteOrder LittleEndian\n"); + else + fprintf(fd,"ByteOrder BigEndian\n"); + } + else + fprintf(fd,"DataType ascii\n"); + + fprintf(fd,"rows %d\n",num_rows()); + fprintf(fd,"columns %d\n",num_columns()); + + fprintf(fd,"EST_Header_End\n"); + + if (type == "est_binary") + { + for (i = 0; i < num_rows(); ++i) + for (j=0; j < num_columns(); j++) + if (fwrite(&a_no_check(i,j),sizeof(double),1,fd) != 1) + { + cerr << "EST_DMatrix: binsave: failed to write row " + << i << " column " << j + << " to \"" << filename << "\"" << endl; + return misc_write_error; + } + } + else + { // est_ascii + for (i = 0; i < num_rows(); ++i) + { + for (j=0; j < num_columns(); j++) + fprintf(fd,"%.25f ",a_no_check(i,j)); + fprintf(fd,"\n"); + } + } + + if (fd != stdout) + fclose(fd); + + return write_ok; +} + +EST_read_status EST_DMatrix::est_load(const EST_String &filename) +{ + + // ascii/binary load with short header for byte swap and sizes + int i,j,k; + int rows, cols, swap; + EST_TokenStream ts; + EST_read_status r; + EST_EstFileType t; + EST_Option hinfo; + bool ascii; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "DMatrix: can't open DMatrix input file " + << filename << endl; + return misc_read_error; + } + if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok) + return r; + if (t != est_file_dmatrix) + return misc_read_error; + if (hinfo.ival("version") != 1) + { + cerr << "DMatrix load: " << ts.pos_description() << + " wrong version of DMatrix format expected 1 but found " << + hinfo.ival("version") << endl; + return misc_read_error; + } + rows = hinfo.ival("rows"); + cols = hinfo.ival("columns"); + resize(rows,cols); + + if (ascii) + { // an ascii file + for (i = 0; i < num_rows(); ++i) + { + for (j = 0; j < num_columns(); ++j) + a_no_check(i,j) = atof(ts.get().string()); + if (!ts.eoln()) + { + cerr << "DMatrix load: " << ts.pos_description() << + " missing end of line at end of row " << i << endl; + return misc_read_error; + } + } + } + else + { // a binary file + double *buff; + if ((EST_BIG_ENDIAN && (hinfo.sval("ByteOrder")=="LittleEndian")) || + (EST_LITTLE_ENDIAN && (hinfo.sval("ByteOrder") == "BigEndian"))) + swap = TRUE; + else + swap = FALSE; + + buff = walloc(double,rows*cols); + // A single read is *much* faster than multiple reads + if (ts.fread(buff,sizeof(double),rows*cols) != rows*cols) + { + cerr << "EST_DMatrix: binload: short file in \"" + << filename << "\"" << endl; + return misc_read_error; + } + if (swap) + swap_bytes_double(buff,rows*cols); + for (k = i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i,j) = buff[k++]; + wfree(buff); + } + + ts.close(); + return read_ok; +} + +EST_read_status EST_DMatrix::load(const EST_String &filename) +{ + EST_read_status r; + + if ((r = est_load(filename)) == format_ok) + return r; + else if (r == wrong_format) + { // maybe its an ancient ascii file + EST_TokenStream ts, tt; + EST_StrList sl; + int i, j, n_rows=0, n_cols=0; + EST_String t; + EST_Litem *p; + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "Can't open DMatrix file " << filename << endl; + return misc_read_error; + } + // set up the character constant values for this stream + ts.set_SingleCharSymbols(";"); + + // first read in as list + for (n_rows = 0; !ts.eof(); ++n_rows) + sl.append(ts.get_upto_eoln().string()); + + if (n_rows > 0) + { + tt.open_string(sl.first()); + for (n_cols = 0; !tt.eof(); ++n_cols) + tt.get().string(); + } + + // resize track and copy values in + resize(n_rows, n_cols); + + for (p = sl.head(), i = 0; p != 0; ++i, p = p->next()) + { + tt.open_string(sl(p)); + for (j = 0; !tt.eof(); ++j) + a_no_check(i,j) = atof(tt.get().string()); + if (j != n_cols) + { + cerr << "Wrong number of points in row " << i << endl; + cerr << "Expected " << n_cols << " got " << j << endl; + return misc_read_error; + } + } + return format_ok; + } + else + return r; + + return format_ok; +} + +EST_read_status EST_DVector::est_load(const EST_String &filename) +{ + // ascii/binary load with short header for byte swap and sizes + int i,k; + int l, swap; + EST_TokenStream ts; + EST_read_status r; + EST_EstFileType t; + EST_Option hinfo; + bool ascii; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "DVector: can't open DVector input file " + << filename << endl; + return misc_read_error; + } + if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok) + return r; + if (t != est_file_dvector) + return misc_read_error; + if (hinfo.ival("version") != 1) + { + cerr << "DVector load: " << ts.pos_description() << + " wrong version of DVector format expected 1 but found " << + hinfo.ival("version") << endl; + return misc_read_error; + } + l = hinfo.ival("length"); + resize(l); + + if (ascii) + { // an ascii file + for (i = 0; i < length(); ++i) + a_no_check(i) = atof(ts.get().string()); + } + else + { // a binary file + double *buff; + if ((EST_BIG_ENDIAN && (hinfo.sval("ByteOrder")=="LittleEndian")) || + (EST_LITTLE_ENDIAN && (hinfo.sval("ByteOrder") == "BigEndian"))) + swap = TRUE; + else + swap = FALSE; + + buff = walloc(double,l); + // A single read is *much* faster than multiple reads + if (ts.fread(buff,sizeof(double),l) != l) + { + cerr << "EST_DVector: binload: short file in \"" + << filename << "\"" << endl; + return misc_read_error; + } + if (swap) + swap_bytes_double(buff,l); + for (k = i = 0; i < length(); ++i) + a_no_check(i) = buff[k++]; + wfree(buff); + } + + ts.close(); + return read_ok; +} + +EST_read_status EST_DVector::load(const EST_String &filename) +{ + + EST_read_status r; + + if ((r = est_load(filename)) == format_ok) + return r; + else if (r == wrong_format) + { // maybe its an ancient ascii file + EST_TokenStream ts; + EST_String s; + int i; + + i = 0; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + ts.set_SingleCharSymbols(";"); + + while (!ts.eof()) + { + ts.get(); + ++i; + } + resize(i); + + ts.close(); + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + + for (i = 0; !ts.eof(); ++i) + { + s = ts.get().string(); + (*this)[i] = atof(s); // actually returns double + } + ts.close(); + return format_ok; + } + else + return r; + + return format_ok; + +} + + +EST_DVector & EST_DVector::operator+=(const EST_DVector &s) +{ + int i; + if(n() != s.n()){ + cerr << "Cannot elementwise add vectors of differing lengths" + << endl; + return *this; + } + + for (i = 0; i < n(); ++i) + (*this)[i] += s(i); + + + return *this; +} + + +EST_DVector& EST_DVector::operator*=(const EST_DVector &s) +{ + if(n() != s.n()){ + cerr << "Cannot elementwise multiply vectors of differing lengths" + << endl; + return *this; + } + + for (int i = 0; i < n(); ++i) + (*this)[i] *= s(i); + + return *this; +} + +EST_DVector& EST_DVector::operator*=(const double f) +{ + for (int i = 0; i < n(); ++i) + (*this)[i] *= f; + + return *this; +} + +double operator*(const EST_DVector &v1, const EST_DVector &v2) +{ + if(v1.length() != v2.length()) + { + cerr << "Can't do vector dot prod - differing vector sizes !" << endl; + return 0; + } + double p=0; + for (int i = 0; i < v1.length(); ++i) + p += v1.a_no_check(i) * v2.a_no_check(i); + + return p; +} + + +EST_DVector& EST_DVector::operator/=(const double f) +{ + for (int i = 0; i < n(); ++i) + (*this)[i] /= f; + + return *this; +} + + +EST_write_status EST_DVector::save(const EST_String &filename, + const EST_String &type) +{ + + if ((type == "est_ascii") || (type == "est_binary")) + return est_save(filename,type); + else + { // the old stuff raw unheadered + int i; + ostream *outf; + if (filename == "-") + outf = &cout; + else + outf = new ofstream(filename); + + outf->precision(25); + if (!(*outf)) + { + cerr << "DVector: can't open file \"" << filename + <<"\" for writing" << endl; + return misc_write_error; + } + + for (i = 0; i < length(); ++i) + *outf << a_no_check(i) << " "; + *outf << endl; + + if (outf != &cout) + delete outf; + + return write_ok; + } +} + +EST_write_status EST_DVector::est_save(const EST_String &filename, + const EST_String &type) +{ + // Binary save with short header for byte swap and sizes + int i; + FILE *fd; + if (filename == "-") + fd = stdout; + else if ((fd = fopen(filename, "wb")) == NULL) + { + cerr << "EST_DVector: binsave: failed to open \"" << filename << + "\" for writing" << endl; + return misc_write_error; + } + + fprintf(fd,"EST_File dvector\n"); + fprintf(fd,"version 1\n"); + if (type == "est_binary") + { + fprintf(fd,"DataType binary\n"); + if (EST_LITTLE_ENDIAN) + fprintf(fd,"ByteOrder LittleEndian\n"); + else + fprintf(fd,"ByteOrder BigEndian\n"); + } + else + fprintf(fd,"DataType ascii\n"); + + fprintf(fd,"length %d\n",length()); + fprintf(fd,"EST_Header_End\n"); + + if (type == "est_binary") + { + for (i = 0; i < length(); ++i) + if (fwrite(&a_no_check(i),sizeof(double),1,fd) != 1) + { + cerr << "EST_DVector: binsave: failed to write item " + << i << " to \"" << filename << "\"" << endl; + return misc_write_error; + } + } + else + { // est_ascii + for (i = 0; i < length(); ++i) + fprintf(fd,"%.25f ",a_no_check(i)); + fprintf(fd,"\n"); + } + + if (fd != stdout) + fclose(fd); + + return write_ok; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_FMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_FMatrix.cc new file mode 100644 index 00000000..cb5ba089 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_FMatrix.cc @@ -0,0 +1,920 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* Matrix Class for floats */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include +#include +using namespace std; + +#include "EST_String.h" +#include "EST_types.h" +#include "EST_FileType.h" +#include "EST_Option.h" +#include "EST_FMatrix.h" +#include "EST_cutils.h" // for swap functions +#include "EST_Token.h" + + +/* EST_FVector may used as EST_Val */ +VAL_REGISTER_CLASS(fvector,EST_FVector) + +/* EST_FMatrix may used as EST_Val */ +VAL_REGISTER_CLASS(fmatrix,EST_FMatrix) + +EST_String EST_FMatrix::default_file_type = "est_ascii"; + +EST_FMatrix::EST_FMatrix(const EST_FMatrix &a, int b) +:EST_TSimpleMatrix(a.num_rows(), a.num_columns()) +{ + float vv = 0.0; + if (b < 0) + return; + if (b == 0) + fill(vv); +} + +EST_FMatrix & EST_FMatrix::operator+=(const EST_FMatrix &a) +{ + int i, j; + if (a.num_columns() != num_columns()) + { + cerr <<"Matrix addition error: bad number of columns\n"; + return *this; + } + if (a.num_rows() != num_rows()) + { + cerr <<"Matrix addition error: bad number of rows\n"; + return *this; + } + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) += a.a_no_check(i,j); + + return *this; +} + +EST_FMatrix & EST_FMatrix::operator-=(const EST_FMatrix &a) +{ + int i, j; + if (a.num_columns() != num_columns()) + { + cerr <<"Matrix subtraction error: bad number of columns\n"; + return *this; + } + if (a.num_rows() != num_rows()) + { + cerr <<"Matrix subtraction error: bad number of rows\n"; + return *this; + } + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) -= a.a_no_check(i,j); + + return *this; +} + +EST_FMatrix & EST_FMatrix::operator*=(const float f) +{ + + int i,j; + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) *= f; + + return *this; +} + +EST_FMatrix & EST_FMatrix::operator/=(const float f) +{ + + int i,j; + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + a_no_check(i, j) /= f; + + return *this; +} + +EST_FMatrix operator+(const EST_FMatrix &a, const EST_FMatrix &b) +{ + EST_FMatrix ab; + int i, j; + if (a.num_columns() != b.num_columns()) + { + cerr <<"Matrix addition error: bad number of columns\n"; + return ab; + } + if (a.num_rows() != b.num_rows()) + { + cerr <<"Matrix addition error: bad number of rows\n"; + return ab; + } + ab.resize(a.num_rows(), a.num_columns()); + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + ab.a_no_check(i, j) = a.a_no_check(i, j) + b.a_no_check(i, j); + + return ab; +} + +EST_FMatrix operator-(const EST_FMatrix &a,const EST_FMatrix &b) +{ + EST_FMatrix ab; + int i, j; + + if (a.num_columns() != b.num_columns()) + { + cerr <<"Matrix subtraction error: bad number of columns:" << + a.num_columns() << " and " << b.num_columns() << endl; + return ab; + } + if (a.num_rows() != b.num_rows()) + { + cerr <<"Matrix subtraction error: bad number of rows\n"; + return ab; + } + ab.resize(a.num_rows(), a.num_columns()); + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + ab.a_no_check(i, j) = a.a_no_check(i, j) - b.a_no_check(i, j); + + return ab; +} + +EST_FMatrix operator*(const EST_FMatrix &a, const float x) +{ + EST_FMatrix b(a, 0); + int i, j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + b.a_no_check(i,j) = a.a_no_check(i,j) * x; + + return b; +} + +int operator !=(const EST_FVector &fv1, + const EST_FVector &fv2) +{ + int i; + if(fv1.length() != fv2.length()) + return FALSE; + for(i=0;i 0) + { + tt.open_string(sl.first()); + for (n_cols = 0; !tt.eof(); ++n_cols) + tt.get().string(); + } + + // resize track and copy values in + resize(n_rows, n_cols); + + for (p = sl.head(), i = 0; p != 0; ++i, p = p->next()) + { + tt.open_string(sl(p)); + for (j = 0; !tt.eof(); ++j) + a_no_check(i,j) = atof(tt.get().string()); + if (j != n_cols) + { + cerr << "Wrong number of points in row " << i << endl; + cerr << "Expected " << n_cols << " got " << j << endl; + return misc_read_error; + } + } + return format_ok; + } + else + return r; +} + + +EST_FVector & EST_FVector::operator+=(const EST_FVector &s) +{ + int i; + if(n() != s.n()){ + cerr << "Cannot elementwise add vectors of differing lengths" + << endl; + return *this; + } + + for (i = 0; i < n(); ++i) + (*this)[i] += s(i); + + + return *this; +} + + +EST_FVector& EST_FVector::operator*=(const EST_FVector &s) +{ + if(n() != s.n()){ + cerr << "Cannot elementwise multiply vectors of differing lengths" + << endl; + return *this; + } + + for (int i = 0; i < n(); ++i) + (*this)[i] *= s(i); + + return *this; +} + +EST_FVector& EST_FVector::operator*=(const float f) +{ + for (int i = 0; i < n(); ++i) + (*this)[i] *= f; + + return *this; +} + + +EST_FVector& EST_FVector::operator/=(const float f) +{ + for (int i = 0; i < n(); ++i) + (*this)[i] /= f; + + return *this; +} + + + +EST_read_status EST_FVector::est_load(const EST_String &filename) +{ + // ascii/binary load with short header for byte swap and sizes + int i,k; + int l, swap; + EST_TokenStream ts; + EST_read_status r; + EST_EstFileType t; + EST_Option hinfo; + bool ascii; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "FVector: can't open FVector input file " + << filename << endl; + return misc_read_error; + } + if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok) + return r; + if (t != est_file_fvector) + return misc_read_error; + if (hinfo.ival("version") != 1) + { + cerr << "FVector load: " << ts.pos_description() << + " wrong version of FVector format expected 1 but found " << + hinfo.ival("version") << endl; + return misc_read_error; + } + l = hinfo.ival("length"); + resize(l); + + if (ascii) + { // an ascii file + for (i = 0; i < length(); ++i) + a_no_check(i) = atof(ts.get().string()); + } + else + { // a binary file + float *buff; + if ((EST_BIG_ENDIAN && (hinfo.sval("ByteOrder")=="LittleEndian")) || + (EST_LITTLE_ENDIAN && (hinfo.sval("ByteOrder") == "BigEndian"))) + swap = TRUE; + else + swap = FALSE; + + buff = walloc(float,l); + // A single read is *much* faster than multiple reads + if (ts.fread(buff,sizeof(float),l) != l) + { + cerr << "EST_FVector: binload: short file in \"" + << filename << "\"" << endl; + return misc_read_error; + } + if (swap) + swap_bytes_float(buff,l); + for (k = i = 0; i < length(); ++i) + a_no_check(i) = buff[k++]; + wfree(buff); + } + + ts.close(); + return read_ok; +} + +EST_read_status EST_FVector::load(const EST_String &filename) +{ + + EST_read_status r; + + if ((r = est_load(filename)) == format_ok) + return r; + else if (r == wrong_format) + { // maybe its an ancient ascii file + EST_TokenStream ts; + EST_String s; + int i; + + i = 0; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + ts.set_SingleCharSymbols(";"); + + while (!ts.eof()) + { + ts.get(); + ++i; + } + resize(i); + + ts.close(); + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + + for (i = 0; !ts.eof(); ++i) + { + s = ts.get().string(); + (*this)[i] = (float)(atof(s)); // actually returns double + } + ts.close(); + return format_ok; + } + else + return r; + + return format_ok; + +} + +/* + +EST_read_status EST_FVector::load(EST_String &filename) +{ + EST_TokenStream ts; + EST_String s; + int i; + + i = 0; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + ts.set_SingleCharSymbols(";"); + + while (!ts.eof()) + { + ts.get(); + ++i; + } + resize(i); + + ts.close(); + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open vector input file " << filename << endl; + return misc_read_error; + } + + for (i = 0; !ts.eof(); ++i) + { + s = ts.get().string(); + (*this)[i] = atof(s); + } + ts.close(); + return format_ok; +} +*/ + +// EST_read_status EST_DVector::load(EST_String &filename) +// { +// EST_TokenStream ts; +// EST_String s; +// int i; + +// i = 0; + +// if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) +// { +// cerr << "can't open vector input file " << filename << endl; +// return misc_read_error; +// } +// ts.set_SingleCharSymbols(";"); + +// while (!ts.eof()) +// { +// ts.get(); +// ++i; +// } +// resize(i); + +// ts.close(); +// if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) +// { +// cerr << "can't open vector input file " << filename << endl; +// return misc_read_error; +// } + +// for (i = 0; !ts.eof(); ++i) +// { +// s = ts.get().string(); +// (*this)[i] = atof(s); // actually returns double +// } +// ts.close(); +// return format_ok; +// } + + +float operator*(const EST_FVector &v1, const EST_FVector &v2) +{ + // dot product + + float b=0; + + if(v1.length() != v2.length()) + { + cerr <<"Vector dot product error: differing vector size" + << endl; + return b; + } + + int i; + for (i = 0; i < v1.length(); ++i) + b += v1.a_no_check(i) * v2.a_no_check(i); + + return b; +} + + +EST_write_status EST_FVector::save(const EST_String &filename, + const EST_String &type) +{ + + if ((type == "est_ascii") || (type == "est_binary")) + return est_save(filename,type); + else + { // the old stuff raw unheadered + int i; + ostream *outf; + if (filename == "-") + outf = &cout; + else + outf = new ofstream(filename); + + outf->precision(25); + if (!(*outf)) + { + cerr << "FVector: can't open file \"" << filename + <<"\" for writing" << endl; + return misc_write_error; + } + + for (i = 0; i < length(); ++i) + *outf << a_no_check(i) << " "; + *outf << endl; + + if (outf != &cout) + delete outf; + + return write_ok; + } +} + +EST_write_status EST_FVector::est_save(const EST_String &filename, + const EST_String &type) +{ + // Binary save with short header for byte swap and sizes + int i; + FILE *fd; + if (filename == "-") + fd = stdout; + else if ((fd = fopen(filename, "wb")) == NULL) + { + cerr << "EST_FVector: binsave: failed to open \"" << filename << + "\" for writing" << endl; + return misc_write_error; + } + + fprintf(fd,"EST_File fvector\n"); + fprintf(fd,"version 1\n"); + if (type == "est_binary") + { + fprintf(fd,"DataType binary\n"); + if (EST_LITTLE_ENDIAN) + fprintf(fd,"ByteOrder LittleEndian\n"); + else + fprintf(fd,"ByteOrder BigEndian\n"); + } + else + fprintf(fd,"DataType ascii\n"); + + fprintf(fd,"length %d\n",length()); + fprintf(fd,"EST_Header_End\n"); + + if (type == "est_binary") + { + for (i = 0; i < length(); ++i) + if (fwrite(&a_no_check(i),sizeof(float),1,fd) != 1) + { + cerr << "EST_FVector: binsave: failed to write item " + << i << " to \"" << filename << "\"" << endl; + return misc_write_error; + } + } + else + { // est_ascii + for (i = 0; i < length(); ++i) + fprintf(fd,"%.25f ",a_no_check(i)); + fprintf(fd,"\n"); + } + + if (fd != stdout) + fclose(fd); + + return write_ok; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_FeatureData.cc b/aeneas/cfw/speech_tools/base_class/EST_FeatureData.cc new file mode 100644 index 00000000..10264015 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_FeatureData.cc @@ -0,0 +1,529 @@ +/************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/************************************************************************/ +/* */ +/* Author: Paul Taylor Caley */ +/* Date: July 1998 */ +/* -------------------------------------------------------------------- */ +/* Feature Data Class */ +/* */ +/************************************************************************/ + +#include "EST_TMatrix.h" +#include "EST_Val.h" +#include "EST_FeatureData.h" +#include "EST_string_aux.h" +#include "EST_Token.h" +#include "EST_FileType.h" +#include "EST_error.h" +#include +#include + +#include "EST_THash.h" + + +EST_FeatureData::EST_FeatureData() +{ + default_vals(); +} + + + +EST_FeatureData::EST_FeatureData(const EST_FeatureData &a) +{ + default_vals(); + copy(a); +} + +EST_FeatureData::~EST_FeatureData(void) +{ +} + +int EST_FeatureData::num_samples() const +{ + return fd.num_rows(); +} + +int EST_FeatureData::num_features() const +{ + return fd.num_columns(); +} + + +void EST_FeatureData::default_vals() +{ +/* cout << "Default values\n"; + p_sub_fd = false; + p_info = new EST_FeatureInfo; +*/ +} + +void EST_FeatureData::set_num_samples(int num_samples, bool preserve) +{ + fd.resize(num_samples, fd.num_columns(), preserve); +} + +void EST_FeatureData::resize(int num_samples, int num_features, bool preserve) +{ + // If enlargement is required, give new features dummy names + // and set their types to . If preserve is set to 0 + // rename all features this way. + + if (num_features > fd.num_columns()) + { + int i; + if (preserve) + i = fd.num_columns(); + else + i = 0; + for (; i < num_features; ++i) + info.set("unnamed_" + itoString(i), ""); + } + + fd.resize(num_samples, num_features, preserve); +} + +void EST_FeatureData::resize(int num_samples, EST_Features &f, bool preserve) +{ + fd.resize(num_samples, f.length(), preserve); + info = f; +} + +EST_String EST_FeatureData::type(const EST_String &feature_name) +{ + EST_String t = info.S(feature_name); + + if (t.contains("<", 0)) // i.e. a predefined type + return t; + + return "undef"; +} + +EST_StrList EST_FeatureData::values(const EST_String &feature_name) +{ + EST_StrList v; + EST_String t = info.S(feature_name); + + // check for infinite set: + if ((t == "") || (t == "") || (t == "")) + return v; + + StringtoStrList(t, v); + return v; +} + +int EST_FeatureData::feature_position(const EST_String &feature_name) +{ + int i; + + EST_Features::Entries p; + + for (i = 0, p.begin(info); p; ++p, ++i) + { +// cout << "looking at " << info.fname(p) << endl; +// cout << "i = " << i << endl; + if (p->k == feature_name) + return i; + } + + EST_error("No such feature %s\n", (const char *) feature_name); + return 0; +} + +int EST_FeatureData::update_values(const EST_String &feature_name, int max) +{ + // This should be converted back to Hash tables once extra + // iteration functions are added the EST_Hash. + int i, col; + EST_Features values; + EST_String v; + +// EST_TStringHash values(max); + + col = feature_position(feature_name); + + for (i = 0; i < num_samples(); ++i) + values.set(fd.a(i, col).string(), 1); + + // check to see if there are more types than allowed, if so + // just set to open set STRING + if (values.length() > max) + v = ""; + else + { + EST_Features::Entries p; + for(p.begin(values); p; ++p) + v += p->k + " "; + } + + info.set(feature_name, v); + + return values.length(); +} + +EST_FeatureData & EST_FeatureData::copy(const EST_FeatureData &a) +{ + (void) a; +/* // copy on a sub can't alter header information + if (!p_sub_fd) + { + delete p_info; + *p_info = *(a.p_info); + } + // but data can be copied so long as no resizing is involved. + EST_ValMatrix::operator=(a); +*/ + return *this; +} + +/*void EST_FeatureData::a(int i, int j) +{ + return EST_ValMatrix::a(i, j); +} +*/ +/* +EST_Val &EST_FeatureData::operator()(int i, int j) +{ + return a(i, j); +} + +EST_Val &EST_FeatureData::operator()(int s, const EST_String &f) +{ + int i = info().field_index(f); + return a(s, i); +} + +EST_FeatureData &EST_FeatureData::operator=(const EST_FeatureData &f) +{ + return copy(f); +} + +*/ +EST_Val &EST_FeatureData::a(int i, const EST_String &f) +{ + (void)f; + return fd.a(i, 0); +} + +EST_Val &EST_FeatureData::a(int i, int j) +{ + return fd.a(i, j); +} +const EST_Val &EST_FeatureData::a(int i, const EST_String &f) const +{ + (void)f; + return fd.a(i, 0); +} + +const EST_Val &EST_FeatureData::a(int i, int j) const +{ + return fd.a(i, j); +} + + +/* +void EST_FeatureData::sub_samples(EST_FeatureData &f, int start, int num) +{ + sub_matrix(f, start, num); + f.p_info = p_info; + f.p_sub_fd = true; +} + +void EST_FeatureData::extract_named_fields(const EST_String &fields) +{ + EST_FeatureData n; + // there must be a more efficient way than a copy? + extract_named_fields(n, fields); + *this = n; +} + +void EST_FeatureData::extract_named_fields(const EST_StrList &fields) +{ + EST_FeatureData n; + // there must be a more efficient way than a copy? + extract_named_fields(n, fields); + *this = n; +} + +void EST_FeatureData::extract_numbered_fields(const EST_String &fields) +{ + EST_FeatureData n; + // there must be a more efficient way than a copy? + extract_numbered_fields(n, fields); + *this = n; +} + +void EST_FeatureData::extract_numbered_fields(const EST_IList &fields) +{ + EST_FeatureData n; + // there must be a more efficient way than a copy? + extract_numbered_fields(n, fields); + *this = n; +} + + +void EST_FeatureData::extract_named_fields(EST_FeatureData &f, + const EST_String &fields) const +{ + EST_StrList s; + + StringtoStrList(fields, s); + extract_named_fields(f, s); +} +void EST_FeatureData::extract_named_fields(EST_FeatureData &f, + const EST_StrList &n_fields) const +{ + EST_Litem *p; + EST_StrList n_types; + int i, j; + + info().extract_named_fields(*(f.p_info), n_fields); + + for (p = n_fields.head(), i = 0; i < f.num_fields(); ++i, p = p->next()) + for (j = 0; j < f.num_samples(); ++j) + f(j, i) = a(j, n_fields(p)); + +} + +void EST_FeatureData::extract_numbered_fields(EST_FeatureData &f, + const EST_IList &fields) const +{ + EST_Litem *p; + EST_StrList n_fields; + int i, j; + + for (p = fields.head(); p; p = p->next()) + n_fields.append(info().field_name(fields(p))); + + info().extract_named_fields(*(f.p_info), n_fields); + + for (p = fields.head(), i = 0; i < f.num_fields(); ++i, p = p->next()) + for (j = 0; j < f.num_samples(); ++j) + f(j, i) = a(j, fields(p)); + +} + +void EST_FeatureData::extract_numbered_fields(EST_FeatureData &f, + const EST_String &fields) const +{ + EST_StrList s; + EST_IList il; + + StringtoStrList(fields, s); + StrListtoIList(s, il); + extract_numbered_fields(f, il); +} +*/ + +EST_write_status save_est(const EST_FeatureData &f, const EST_String &filename) +{ + (void)f; + (void)filename; +/* + ostream *outf; + EST_Litem *s, *e; + int i; + if (filename == "-") + outf = &cout; + else + outf = new ofstream(filename); + + if (!(*outf)) + return write_fail; + + outf->precision(5); + outf->setf(ios::fixed, ios::floatfield); + outf->width(8); + + *outf << "EST_File feature_data\n"; // EST header identifier + *outf << "DataType ascii\n"; + *outf << "NumSamples " << f.num_samples() << endl; + *outf << "NumFields " << f.num_fields() << endl; + *outf << "FieldNames " << f.info().field_names(); + *outf << "FieldTypes " << f.info().field_types(); + if (f.info().group_start.length() > 0) + for (s = f.info().group_start.head(), e = f.info().group_end.head(); + s; s = s->next(), e = e->next()) + *outf << "Group " << f.info().group_start.key(s) << " " << + f.info().group_start.val(s) << " " << f.info().group_end.val(e) << endl; + + for (i = 0; i < f.num_fields(); ++i) + if (f.info().field_values(i).length() > 0) + *outf << "Field_" << i << "_Values " + << f.info().field_values(i) << endl; + + *outf << "EST_Header_End\n"; // EST end of header identifier + +// *outf << ((EST_ValMatrix ) f); + *outf << f; + */ + + return write_ok; +} + + +EST_write_status EST_FeatureData::save(const EST_String &filename, + const EST_String &file_type) const +{ + if ((file_type == "est") || (file_type == "")) + return save_est(*this, filename); +/* else if (file_type = "octave") + return save_octave(*this, filename); + else if (file_type = "ascii") + return save_ascii(*this, filename); +*/ + + cerr << "Can't save feature data in format \"" << file_type << endl; + return write_fail; +} + + + +EST_read_status EST_FeatureData::load(const EST_String &filename) +{ + int i, j; + EST_Option hinfo; + EST_String k, v; + EST_read_status r; + bool ascii; + EST_TokenStream ts; + EST_EstFileType t; + int ns, nf; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "Can't open track file " << filename << endl; + return misc_read_error; + } + // set up the character constant values for this stream + ts.set_SingleCharSymbols(";"); + ts.set_quotes('"','\\'); + + if ((r = read_est_header(ts, hinfo, ascii, t)) != format_ok) + { + cerr << "Error reading est header of file " << filename << endl; + return r; + } + + if (t != est_file_feature_data) + { + cerr << "Not a EST Feature Data file: " << filename << endl; + return misc_read_error; + } + + ns = hinfo.ival("NumSamples"); + nf = hinfo.ival("NumFeatures"); + + cout << "ns: " << ns << endl; + cout << "nf: " << nf << endl; + resize(ns, nf); + + info.clear(); // because resize will make default names + + for (i = 0; i < nf; ++i) + { + k = "Feature_" + itoString(i+1); + if (hinfo.present(k)) + { + v = hinfo.val(k); + info.set(v.before(" "), v.after(" ")); + cout << "value: " << v.after(" ") << endl; + } + else + EST_error("No feature definition given for feature %d\n", i); + } + + for (i = 0; i < ns; ++i) + { + EST_Features::Entries p; + for (p.begin(info), j = 0; j < nf; ++j, ++p) + { + if (p->k == "") + a(i, j) = atof(ts.get().string()); + else if (p->k == "") + a(i, j) = atoi(ts.get().string()); + else if (p->k == "") + a(i, j) = atoi(ts.get().string()); + else + a(i, j) = ts.get().string(); + } + } + + return format_ok; +} + +/*ostream& operator << (ostream &st, const EST_FeatureInfo &a) +{ + +// st << a.field_names() << endl; +// st << a.field_types() << endl; + + return st; +} +*/ + +ostream& operator << (ostream &st, const EST_FeatureData &d) +{ + int i, j; + EST_String t; + EST_Val v; + +// st << a; + +// EST_ValMatrix::operator<<(st, (EST_ValMatrix)a); + + for (i = 0; i < d.num_samples(); ++i) + { + for (j = 0; j < d.num_features(); ++j) + { + v = d.a(i, j); + st << v << " "; +// cout << "field type " << a.info().field_type(j) << endl; +/* else if (a.info().field_type(j) == "float") + st << a.a(i, j); + else if (a.info().field_type(j) == "int") + st << a.a(i, j); + + else if (a.info().field_type(j) == "string") + { + // st << "\"" << a.a(i, j) << "\""; + t = a.a(i, j); + t.gsub(" ", "_"); + st << t; + } +*/ + } + st << endl; + } + + return st; +} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Featured.cc b/aeneas/cfw/speech_tools/base_class/EST_Featured.cc new file mode 100644 index 00000000..fb6e71fa --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Featured.cc @@ -0,0 +1,105 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* */ + /* -------------------------------------------------------------------- */ + /* A class encapsulating the mechanics of objects which have features. */ + /* */ + /*************************************************************************/ + +#include "EST_Featured.h" + + +EST_Featured::EST_Featured(void) +{ + init_features(); +} + +EST_Featured::EST_Featured(const EST_Featured &f) +{ + init_features(); + copy_features(f); +} + + +EST_Featured::~EST_Featured(void) +{ + clear_features(); +} + +void EST_Featured::init_features() +{ + p_features=NULL; +} + +void EST_Featured::clear_features() +{ + if (p_features) + { + delete p_features; + p_features=NULL; + } + init_features(); +} + +const EST_Val &EST_Featured::f_Val(const char *name) const +{ + if (p_features) + return p_features->val(name); + else + return EST_Features::feature_default_value; +} + +const EST_Val &EST_Featured::f_Val(const char *name, const EST_Val &def) const +{ + if (p_features) + return p_features->val(name); + else + return def; +} + +void EST_Featured::copy_features(const EST_Featured &f) +{ + clear_features(); + + if (f.p_features) + p_features = new EST_Features(*(f.p_features)); +} + +#if defined(INSTANTIATE_TEMPLATES) + +typedef EST_TKVI EST_Featured_Entry; +Instantiate_TStructIterator_T(EST_Featured, EST_Featured::IPointer_feat,EST_Featured_Entry, Featured_itt) +Instantiate_TIterator_T(EST_Featured, EST_Featured::IPointer_feat,EST_Featured_Entry, Featured_itt) +#endif diff --git a/aeneas/cfw/speech_tools/base_class/EST_Features.cc b/aeneas/cfw/speech_tools/base_class/EST_Features.cc new file mode 100644 index 00000000..5181d3ed --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Features.cc @@ -0,0 +1,217 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : March 1998 */ +/*-----------------------------------------------------------------------*/ +/* Feature value pairs in a EST_TKVL */ +/*=======================================================================*/ + +#include +#include "EST_Features.h" +#include "ling_class/EST_Item.h" +#include "EST_error.h" +#include "EST_String.h" +#include "EST_Token.h" + +/* Features may used as Vals */ +VAL_REGISTER_CLASS(feats,EST_Features) + +EST_Val EST_Features::feature_default_value("0"); +// Sometimes we need a set of features even though there aren't any +static EST_Features default_features; + +EST_Features::EST_Features() +{ + features = new EST_TKVL; +} + +EST_Features::EST_Features(const EST_Features &f) +{ + features = new EST_TKVL; + *features = *f.features; +} + +EST_Features::~EST_Features() +{ + if (features != NULL) + { + delete features; + features=NULL; + } +} + +const EST_Val &EST_Features::val(const char *name, const EST_Val &def) const +{ + // Because so many access are from char* literals we all access + // directly rather than requiring the creation of an EST_String + EST_Litem *p; + + for (p=features->list.head(); p; p=p->next()) + { + if (features->list(p).k == name) + return features->list(p).v; + } + return def; +} + +const EST_Val &EST_Features::val(const char *name) const +{ + // Because so many access are from char* literals we all access + // directly rather than requiring the creation of an EST_String + EST_Litem *p; + + for (p=features->list.head(); p; p=p->next()) + { + if (features->list(p).k == name) + return features->list(p).v; + } + + EST_error("{FND} Feature %s not defined\n", name); + return feature_default_value; +} + +const EST_Val &EST_Features::val_path(const EST_String &name, const EST_Val &d) const +{ + // For when name contains references to sub-features + + if (strchr(name,'.') == NULL) + return val(name, d); + else + { + EST_String nname = name; + EST_String fname = nname.before("."); + const EST_Val &v = val(fname, d); + if (v.type() == val_type_feats) + return feats(v)->val_path(nname.after("."), d); + else + return d; + } +} + +const EST_Val &EST_Features::val_path(const EST_String &name) const +{ + // For when name contains references to sub-features + + if (strchr(name,'.') == NULL) + return val(name); + else + { + EST_String nname = name; + EST_String fname = nname.before("."); + const EST_Val &v = val(fname); + if (v.type() == val_type_feats) + return feats(v)->val_path(nname.after(".")); + else + EST_error("Feature %s not feature valued\n", (const char *)fname); + return feature_default_value; // wont get here + } +} + +EST_Features &EST_Features::A(const EST_String &path,EST_Features &def) const +{ + EST_Features *ff = new EST_Features(def); + + return *feats(val(path,est_val(ff))); +} + +int EST_Features::present(const EST_String &name) const +{ + if (strchr(name,'.') == NULL) + return features->present(name); + EST_String nname = name; + if (features->present(nname.before("."))) + { + const EST_Val &v = val(nname.before(".")); + if (v.type() == val_type_feats) + return feats(v)->present(nname.after(".")); + else + return FALSE; + } + else + return FALSE; +} + +void EST_Features::set_path(const EST_String &name, const EST_Val &sval) +{ + // Builds sub features (if necessary) + + if (strchr(name,'.') == NULL) + set_val(name,sval); + else + { + EST_String nname = name; + EST_String fname = nname.before("."); + if (present(fname)) + { + const EST_Val &v = val(fname); + if (v.type() == val_type_feats) + feats(v)->set_path(nname.after("."),sval); + else + EST_error("Feature %s not feature valued\n", + (const char *)fname); + } + else + { + EST_Features f; + set(fname,f); + A(fname).set_path(nname.after("."),sval); + } + } +} + +EST_Features &EST_Features::operator=(const EST_Features &x) +{ + *features = *x.features; + return *this; +} + +void merge_features(EST_Features &to,EST_Features &from) +{ + EST_Features::Entries p; + + for(p.begin(from); p; ++p) + to.set_val(p->k,p->v); +} + + +EST_String error_name(const EST_Features &a) +{ + (void)a; + return "<>"; +} + +#if defined(INSTANTIATE_TEMPLATES) +typedef EST_TKVI EST_Features_Entry; +Instantiate_TStructIterator_T(EST_Features, EST_Features::IPointer, EST_Features_Entry, Features_itt) +Instantiate_TIterator_T(EST_Features, EST_Features::IPointer, EST_Features_Entry, Features_itt) +#endif diff --git a/aeneas/cfw/speech_tools/base_class/EST_IMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_IMatrix.cc new file mode 100644 index 00000000..6a983053 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_IMatrix.cc @@ -0,0 +1,64 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* Matrix Class for ints */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include +#include +#include "EST_String.h" +#include "EST_types.h" +#include "EST_FileType.h" +#include "EST_Option.h" +#include "EST_IMatrix.h" +#include "EST_cutils.h" // for swap functions +#include "EST_Token.h" +#include "rateconv.h" + +EST_IMatrix::EST_IMatrix(EST_IMatrix &a, int b) +:EST_TSimpleMatrix(a.num_rows(), a.num_columns()) +{ + int vv = 0; + if (b < 0) + return; + if (b == 0) + fill(vv); +} + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_Option.cc b/aeneas/cfw/speech_tools/base_class/EST_Option.cc new file mode 100644 index 00000000..f505c50b --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Option.cc @@ -0,0 +1,191 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* EST_Option Class */ +/* */ +/*=======================================================================*/ + +#include +#include "EST_Option.h" +#include "EST_io_aux.h" +#include "EST_Token.h" + +static const EST_String Empty_String(""); + +// Fills in keyval pair. If Key already exists, overwrites value. +int EST_Option::override_val(const EST_String rkey, const EST_String rval) +{ + if (rval == "") + return 0; + + return add_item(rkey, rval); +} + +int EST_Option::override_fval(const EST_String rkey, const float rval) +{ + EST_String tmp; + char ctmp[100]; + sprintf(ctmp, "%f", rval); + tmp = ctmp; + + return override_val(rkey, tmp); +} + +int EST_Option::override_ival(const EST_String rkey, const int rval) +{ + EST_String tmp; + char ctmp[100]; + sprintf(ctmp, "%d", rval); + tmp = ctmp; + + return override_val(rkey, tmp); +} + +int EST_Option::ival(const EST_String &rkey, int must) const +{ + const EST_String &tval = val_def(rkey, Empty_String); + if (tval != "") + return atoi(tval); + + if (must) + cerr << "EST_Option: No value set for " << rkey << endl; + return 0; +} + +const EST_String &EST_Option::sval(const EST_String &rkey, int must) const +{ + const EST_String &tval = val_def(rkey, Empty_String); + if (tval != Empty_String) + return tval; + + if (must) + cerr << "EST_Option: No value set for " << rkey << endl; + return Empty_String; +} + +float EST_Option::fval(const EST_String &rkey, int must) const +{ + const EST_String &tval = val_def(rkey, Empty_String); + if (tval != Empty_String) + return atof(tval); + + if (must) + cerr << "EST_Option: No value set for " << rkey << endl; + return 0.0; +} + +double EST_Option::dval(const EST_String &rkey, int must) const +{ + const EST_String &tval = val_def(rkey,Empty_String); + if (tval != Empty_String) + return atof(tval); + + if (must) + cerr << "EST_Option: No value set for " << rkey << endl; + return 0.0; +} + +int EST_Option::add_iitem(const EST_String &rkey, const int &rval) +{ + char tmp[100]; + sprintf(tmp, "%d", rval); + return add_item(rkey, tmp); +} + +int EST_Option::add_fitem(const EST_String &rkey, const float &rval) +{ + char tmp[100]; + sprintf(tmp, "%f", rval); + return add_item(rkey, tmp); +} + +// load in Options from files. This function has a recursive include +// facility fpr reading nested files. Maybe there should be a check on +// the max number of allowable open files. + +EST_read_status EST_Option::load(const EST_String &filename, + const EST_String &comment) +{ + EST_TokenStream ts; + EST_String k, v; + + if (((filename == "-") ? ts.open(cin) : ts.open(filename)) != 0) + { + cerr << "can't open EST_Option input file " << filename << endl; + return misc_read_error; + } + // set up the character constant values for this stream + + while(!ts.eof()) + { + k = ts.get().string(); + v = ts.get_upto_eoln().string(); + if (v.contains(RXwhite, 0)) + v = v.after(RXwhite); + + if (k.contains("#include")) //recursively load additional files + { + cout << "Include directive\n"; + this->load(v); + } + + if (!k.contains(comment, 0)) + add_item(k, v, 0); // e a search is required. + } + return format_ok; +} + +void EST_Option::add_prefix(EST_String prefix) +{ + EST_Litem *ptr; + + for (ptr = list.head(); ptr; ptr = ptr->next()) + change_key(ptr, prefix + key(ptr)); +} + +void EST_Option::remove_prefix(EST_String prefix) +{ + (void)prefix; +} + +ostream& operator << (ostream& s, const EST_Option &kv) +{ + EST_Litem *ptr; + + for (ptr = kv.list.head(); ptr; ptr = ptr->next()) + s << kv.key(ptr) << "\t" << kv.val((EST_Litem *)ptr) << endl; + + return s; +} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Pathname_unix.cc b/aeneas/cfw/speech_tools/base_class/EST_Pathname_unix.cc new file mode 100644 index 00000000..5bdc7ba5 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Pathname_unix.cc @@ -0,0 +1,206 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Mar 18 1997 */ + /************************************************************************/ + /* */ + /* Implementation of a class for manipulating filenames and so on. */ + /* */ + /* This is all hard coded to be unix filenames. I think the best */ + /* strategy is to have a separate version of this for any other */ + /* pathname format rather than trying to parameterise this. Most of */ + /* it is fairly simple. */ + /* */ + /************************************************************************/ + +#include "EST_unix.h" +#include "EST_Pathname.h" + +void EST_Pathname::setup(void) +{ +} + +int EST_Pathname::is_absolute(void) const +{ + return length()>0 && (*this)(0) == '/'; +} + +int EST_Pathname::is_dirname(void) const +{ + return length()>0 && (*this)(length()-1) == '/'; +} + +EST_Pathname EST_Pathname::directory(void) const { + + if (is_dirname()) + return *this; + + int pos; + if ((pos=index("/", -1)) >=0) + return before(pos+1); + else + return "./"; + } + +EST_Pathname EST_Pathname::as_file(void) const +{ + if (is_filename()) + return *this; + + if (length() > 0) + return before(-1); + + return "."; +} + +EST_Pathname EST_Pathname::as_directory(void) const +{ + if (is_dirname()) + return *this; + + if (length() > 0) + { + EST_String xx; + xx = EST_String(*this) + "/"; + return xx; + } + + return "./"; +} + +EST_Pathname EST_Pathname::construct(EST_Pathname dir, + EST_String filename) +{ + EST_String result(dir.as_directory()); + + result += filename; + return result; +} + +EST_Pathname EST_Pathname::construct(EST_Pathname dir, + EST_String basename, + EST_String extension) +{ + EST_Pathname filename(basename + "." + extension); + return EST_Pathname::construct(dir, filename); +} + +EST_TList EST_Pathname::entries(int check_for_directories) const +{ + DIR *dir; + EST_TList list; + + if ((dir = opendir(this->as_directory()))!=NULL) + { + struct dirent *entry; + + while ((entry = readdir(dir)) != NULL) + { + EST_Pathname name(entry->d_name); + struct stat buf; + + if (check_for_directories && + stat((EST_String)this->as_directory() + (EST_String)name, &buf)==0 && + (buf.st_mode & S_IFDIR)) + list.append(name.as_directory()); + else + list.append(name); + } + closedir(dir); + } + + return list; +} + +EST_Pathname operator + (const EST_Pathname p, const EST_Pathname addition) +{return EST_Pathname::append(p, addition); } + +EST_Pathname operator + (const char *p, const EST_Pathname addition) +{return EST_Pathname::append(p, addition); } + +#if 0 +EST_Pathname operator += (EST_Pathname p, const EST_Pathname addition) +{ EST_String q = EST_Pathname::append(p, addition); return q; } +EST_Pathname operator += (EST_Pathname p, const EST_String addition) +{ EST_String q = EST_Pathname::append(p, addition); return q; } +#endif + +EST_Pathname EST_Pathname::append(EST_Pathname directory, EST_Pathname addition) +{ + if (addition.is_absolute()) + return addition; + + EST_String add(addition); + + EST_String result(directory.as_directory()); + + result += add; + + return result; +} + + +EST_String EST_Pathname::extension(void) const +{ + EST_String result(""); + + if (length() <= 0) + return result; + + if (contains(".")) + result = after(index(".",-1)); + + return result; + +} + +EST_Pathname EST_Pathname::filename(void) const +{ + EST_String result(this->as_file()); + + if (contains("/")) + result = result.after(index("/",-1)); + return result; +} + +EST_String EST_Pathname::basename(int remove_all) const +{ + EST_String result(this->as_file().filename()); + + if (remove_all) + { + if (result.contains(".")) + result = result.before("."); + } + return result; +} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Pathname_win32.cc b/aeneas/cfw/speech_tools/base_class/EST_Pathname_win32.cc new file mode 100644 index 00000000..30ed7411 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Pathname_win32.cc @@ -0,0 +1,201 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Mar 18 1997 */ + /************************************************************************/ + /* */ + /* Implementation of a class for manipulating filenames and so on. */ + /* */ + /* This is all hard coded to be unix filenames. I think the best */ + /* strategy is to have a separate version of this for any other */ + /* pathname format rather than trying to parameterise this. Most of */ + /* it is fairly simple. */ + /* */ + /************************************************************************/ + +#include +#include "EST_System.h" +#include "EST_Pathname.h" + +void EST_Pathname::setup(void) +{ + this->gsub("/", "\\"); +} + +int EST_Pathname::is_absolute(void) const +{ + return length()>0 && (*this)[0] == '\\'; +} + +int EST_Pathname::is_dirname(void) const +{ + return length()>0 && (*this)[length()-1] == '\\'; +} + +EST_Pathname EST_Pathname::directory(void) const { + + if (is_dirname()) + return *this; + + int pos; + if ((pos=index("\\", -1)) >=0) + return before(pos+1); + else + return ".\\"; + } + +EST_Pathname EST_Pathname::as_file(void) const +{ + if (is_filename()) + return *this; + + if (length() > 0) + return before(-1); + + return "."; +} + +EST_Pathname EST_Pathname::as_directory(void) const +{ + if (is_dirname()) + return *this; + + if (length() > 0) + return ((EST_String)(*this) + (EST_String)"\\"); + + return ".\\"; +} + +EST_Pathname EST_Pathname::construct(EST_Pathname dir, + EST_String filename) +{ + EST_Pathname result(dir.as_directory()); + + result += filename; + return result; +} + +EST_Pathname EST_Pathname::construct(EST_Pathname dir, + EST_String basename, + EST_String extension) +{ + EST_Pathname filename(basename + "." + extension); + return EST_Pathname::construct(dir, filename); +} + +EST_TList EST_Pathname::entries(int check_for_directories) const +{ + WIN32_FIND_DATA find_data; + HANDLE handle; + EST_TList list; + EST_Pathname pattern(this->as_directory() + EST_Pathname("*")); + + handle = FindFirstFile(pattern, &find_data); + if (handle != INVALID_HANDLE_VALUE) + while (1==1) + { + EST_Pathname name(find_data.cFileName); + + if (check_for_directories + && (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + list.append(name.as_directory()); + else + list.append(name); + if (!FindNextFile(handle, &find_data)) + break; + } + FindClose(handle); + return list; +} + +EST_Pathname EST_Pathname::append(EST_Pathname directory, EST_Pathname addition) +{ + if (addition.is_absolute()) + return addition; + + EST_String add(addition); + + EST_Pathname result(directory.as_directory()); + + result.EST_String::operator += (add); + + return result; +} + + +EST_String EST_Pathname::extension(void) const +{ + EST_String result(""); + + if (length() <= 0) + return result; + + if (contains(".")) + result = after(index(".",-1)); + + return result; + +} + +EST_Pathname EST_Pathname::filename(void) const +{ + EST_String result(this->as_file()); + + if (contains("\\")) + result = result.after(index("\\",-1)); + return result; +} + +EST_String EST_Pathname::basename(int remove_all) const +{ + EST_String result(this->as_file().filename()); + + if (remove_all) + { + if (result.contains(".")) + result = result.before("."); + } + return result; +} + +EST_Pathname operator + (const EST_Pathname p, const EST_Pathname addition) +{return EST_Pathname::append(p, addition); } + +EST_Pathname operator + (const char *p, const EST_Pathname addition) +{return EST_Pathname::append(p, addition); } + +EST_Pathname &operator += (EST_Pathname p, const EST_Pathname addition) +{ p = EST_Pathname::append(p, addition); return p; } +EST_Pathname &operator += (EST_Pathname p, const EST_String addition) +{ p = EST_Pathname::append(p, addition); return p; } + diff --git a/aeneas/cfw/speech_tools/base_class/EST_SMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_SMatrix.cc new file mode 100644 index 00000000..b6ebd64a --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_SMatrix.cc @@ -0,0 +1,105 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* Matrix Class for shorts */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include +#include +#include "EST_String.h" +#include "EST_types.h" +#include "EST_FileType.h" +#include "EST_Option.h" +#include "EST_SMatrix.h" +#include "EST_cutils.h" // for swap functions +#include "EST_Token.h" +#include "rateconv.h" + +EST_SMatrix::EST_SMatrix(EST_SMatrix &a, int b) +:EST_TSimpleMatrix(a.num_rows(), a.num_columns()) +{ + short vv = 0; + if (b < 0) + return; + if (b == 0) + fill(vv); +} + +int EST_SMatrix::rateconv(int in_samp_freq, int out_samp_freq) +{ + short *in_buf = new short[num_rows()]; + short ** results = new short *[num_columns()]; + int *len = new int[num_columns()]; + int max_len=0; + + for(int c=0; c max_len) + max_len = osize; + } + else + return -1; + } + delete [] in_buf; + + resize(max_len, EST_CURRENT, 0); + fill(0); + + for(int c1=0; c1 + +#define TRIEWIDTH 256 + +static void (* trie_delete_function)(void *n) = 0; + +static inline int char2idx(unsigned char k) +{ +// return k & 0x7f; // only seven significant bits; + return k; +} + +EST_TrieNode::EST_TrieNode(const int width) +{ + // Initialise a node of given width + w=width; + d= new EST_TrieNode *[w]; + contents=0; + memset(d,0,w*sizeof(EST_TrieNode *)); +} + +EST_TrieNode::~EST_TrieNode() +{ + int i; + + if (trie_delete_function != 0) /* user supplied delete function */ + trie_delete_function(contents); + for (i=0; ilookup(key+1); + } +} + +void EST_TrieNode::copy_into(EST_StringTrie &trie, + const EST_String &path) const +{ + // find all items and add them to trie + + if (contents != 0) + trie.add(path,contents); + + for (int i=0; i < w; i++) + { + if (d[i] != 0) + { + char tail[2]; + tail[0] = (char)i; + tail[1] = '\0'; + d[i]->copy_into(trie,path+tail); + } + } +} + +void EST_TrieNode::add(const unsigned char *key,void *value) +{ + // add this value + + if (*key == '\0') + contents = value; + else + { + int idx = char2idx(*key); + if (d[idx] == 0) // need new subnode + d[idx] = new EST_TrieNode(w); + d[idx]->add(key+1,value); + } +} + +EST_StringTrie::EST_StringTrie() +{ + tree = new EST_TrieNode(TRIEWIDTH); +} + +void EST_StringTrie::copy(const EST_StringTrie &trie) +{ + // This can't work because of the void* pointers in contents + delete tree; + tree = new EST_TrieNode(TRIEWIDTH); + trie.tree->copy_into(*this,""); +} + +EST_StringTrie::~EST_StringTrie() +{ + delete tree; +} + +void *EST_StringTrie::lookup(const EST_String &key) const +{ + const unsigned char *ckey = (const unsigned char *)(void *)(const char *)key; + return tree->lookup(ckey); +} + +void EST_StringTrie::add(const EST_String &key,void *item) +{ + const unsigned char *ckey = (const unsigned char *)(void *)(const char *)key; + tree->add(ckey,item); + return; +} + +void EST_StringTrie::clear(void) +{ + delete tree; + tree = new EST_TrieNode(TRIEWIDTH); +} + +void EST_StringTrie::clear(void (*deletenode)(void *n)) +{ + // This wont work if we go multi-thread + trie_delete_function = deletenode; + delete tree; + trie_delete_function = 0; + tree = new EST_TrieNode(TRIEWIDTH); +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TBuffer.cc b/aeneas/cfw/speech_tools/base_class/EST_TBuffer.cc new file mode 100644 index 00000000..75b6af7c --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TBuffer.cc @@ -0,0 +1,157 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Aug 26 1997 */ + /* -------------------------------------------------------------------- */ + /* Extending buffers, i.e. arrays which grow as needed. I got fed up */ + /* of writing this code all over the place. */ + /* */ + /*************************************************************************/ + +#include +#include +#include +#include "EST_unix.h" +#include "EST_TBuffer.h" + +template +EST_TBuffer::EST_TBuffer(unsigned int size, int step) +{ + p_buffer = NULL; + init(size, step); +} + +template +EST_TBuffer::~EST_TBuffer(void) +{ + // save the buffer if we have a slot + for(int i=0; i +void EST_TBuffer::init(unsigned int size, int step) +{ + for(int i=0; i= size) + { + p_buffer = (T *)EST_old_buffers[i].mem; + p_size = EST_old_buffers[i].size/sizeof(T); + EST_old_buffers[i].mem = NULL; + EST_old_buffers[i].size = 0; + break; + } + + if (p_buffer == NULL) + { + p_buffer = new T[size]; + p_size = size; + } + p_step = step; +} + +template +void EST_TBuffer::expand_to(unsigned int req_size, bool copy) +{ + if (req_size > p_size) + { + unsigned int new_size = p_size; + + while(new_size < req_size) + if (p_step >0) + new_size += p_step; + else + new_size = (int)(new_size*(float)(-p_step)/100.0); + + T * new_buffer = new T[new_size]; + + if (copy) + memcpy(new_buffer, p_buffer, p_size*sizeof(T)); + + delete[] p_buffer; + p_buffer = new_buffer; + p_size = new_size; + } +} + +template +void EST_TBuffer::expand_to(unsigned int req_size, const T &set_to, int howmany) +{ + if (req_size > p_size) + { + unsigned int new_size = p_size; + + while(new_size < req_size) + if (p_step >0) + new_size += p_step; + else + new_size = (int)(new_size*(float)(-p_step)/100.0); + + T * new_buffer = new T[new_size]; + + if (howmany<0) + howmany=new_size; + for(int i=0; i +void EST_TBuffer::set(const T &set_to, int howmany) +{ + if (howmany < 0) + howmany = p_size; + + for(int i=0; i +EST_TDeque::EST_TDeque(unsigned int capacity, unsigned int increment) + : p_vector(capacity) +{ + p_increment = increment; + p_front=0; + p_back=0; +} + +template +EST_TDeque::EST_TDeque(unsigned int capacity) + : p_vector(capacity) +{ + p_increment = 10; + p_front=0; + p_back=0; +} + +template +EST_TDeque::EST_TDeque() +{ + p_vector.resize(10); + p_increment = 10; + p_front=0; + p_back=0; +} + + +template +ostream &EST_TDeque::print(ostream &s) const +{ + s << "{" << p_vector.n() << "|"; + + if (p_front >= p_back) + { + for(int i0=0; i0" << "//"; + for(int i=p_back; i" << "//"; + } + else + { + for(int ii=0; ii" << "//"; + for(int i=p_back; i +void EST_TDeque::expand() +{ + EST_TVector tmp(p_vector); + + if (p_back==0) + // special case for pure stack + p_vector.resize(p_vector.n()+p_increment, true); + else + { + p_vector.resize(p_vector.n()+p_increment, false); + + if (p_front >= p_back) + for(int i=p_back, j=0; i +bool EST_TDeque::is_empty() const +{ + return p_front==p_back; +} + +template +void EST_TDeque::clear() +{ + p_front=p_back=0; + for(int i=0; i +void EST_TDeque::push(T& it) +{ + int next_front= p_front+1; + if (next_front >= p_vector.n()) + next_front= 0; + + if (next_front==p_back) + { + expand(); + push(it); + } + else + { + p_vector[p_front] = it; + p_front=next_front; + } +} + +template +T &EST_TDeque::pop() +{ + if (is_empty()) + EST_error("empty stack!"); + + p_front--; + if (p_front < 0) + p_front=p_vector.n()-1; + + return p_vector[p_front]; +} + +template +T &EST_TDeque::nth(int n) +{ + if (is_empty()) + EST_error("empty stack!"); + + int pos = p_front-1-n; + + if (p_front < p_back) + { + if (pos < 0) + { + pos += p_vector.n(); + if (pos < p_back) + EST_error("looking too far up stack!"); + } + } + else + if (pos < p_back) + EST_error("looking too far up stack!"); + + return p_vector[pos]; +} + +template +void EST_TDeque::back_push(T& it) +{ + int next_back = p_back-1; + + if (next_back < 0) + next_back = p_vector.n()-1; + + if (next_back == p_front) + { + expand(); + back_push(it); + } + else + { + p_vector[p_back=next_back] = it; + } +} + +template +T &EST_TDeque::back_pop() +{ + if (is_empty()) + EST_error("empty stack!"); + + int old_back = p_back; + p_back++; + if (p_back >= p_vector.n()) + p_back=0; + + return p_vector[old_back]; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_THash.cc b/aeneas/cfw/speech_tools/base_class/EST_THash.cc new file mode 100644 index 00000000..17c2226c --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_THash.cc @@ -0,0 +1,270 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Apr 4 1997 */ + /************************************************************************/ + /* */ + /* Simple Hash classes. */ + /* */ + /************************************************************************/ + + +#include "EST_THash.h" + +template +EST_THash::EST_THash(int size, unsigned int (*hash_function)(const K &key, unsigned int size)) +{ + p_num_entries =0; + + p_num_buckets = size; + + p_buckets = new EST_Hash_Pair *[size]; + for(int i=0; i +EST_THash::EST_THash(const EST_THash &from) +{ + p_buckets=NULL; + copy(from); +} + +template +void EST_THash::clear(void) +{ + if (p_buckets != NULL) + { + for(unsigned int i=0; i *p, *n; + for(p=p_buckets[i]; p != NULL; p=n) + { + n = p->next; + delete p; + } + p_buckets[i]=NULL; + } + } + p_num_entries=0; +} + +template +EST_THash::~EST_THash(void) +{ + if (p_buckets) + { + clear(); + delete[] p_buckets; + } +} + + +template +int EST_THash::present(const K &key) const +{ + unsigned int b; + if (p_hash_function) + b = (*p_hash_function)(key, p_num_buckets); + else + b = DefaultHashFunction((void *)&key, sizeof(key), p_num_buckets); + + EST_Hash_Pair *p; + + for(p=p_buckets[b]; p!=NULL; p=p->next) + if (p->k == key) + return TRUE; + +return FALSE; +} + +template +V &EST_THash::val(const K &key, int &found) const +{ + unsigned int b; + if (p_hash_function) + b = (*p_hash_function)(key, p_num_buckets); + else + b = DefaultHashFunction((void *)&key, sizeof(key), p_num_buckets); + + EST_Hash_Pair *p; + + for(p=p_buckets[b]; p!=NULL; p=p->next) + if (p->k == key) + { + found=1; + return p->v; + } + +found=0; +return Dummy_Value; +} + +template +const K &EST_THash::key(const V &val, int &found) const +{ + + for(unsigned int b=0; b *p; + for(p=p_buckets[b]; p!=NULL; p=p->next) + if (p->v == val) + { + found=1; + return p->k; + } + } +found=0; +return Dummy_Key; +} + +template +void EST_THash::map(void (*func)(K&, V&)) +{ + for(unsigned int i=0; i *p; + + for(p=p_buckets[i]; p!=NULL; p=p->next) + (*func)(p->k, p->v); + } + +} + +template +int EST_THash::add_item(const K &key, const V &value, int no_search) +{ + unsigned int b; + if (p_hash_function) + b = (*p_hash_function)(key, p_num_buckets); + else + b = DefaultHashFunction((void *)&key, sizeof(key), p_num_buckets); + + EST_Hash_Pair *p; + + if (!no_search) + for(p=p_buckets[b]; p!=NULL; p=p->next) + if (p->k == key) + { + p->v = value; + return FALSE; + } + + p = new EST_Hash_Pair; + p->k = key; + p->v = value; + p->next = p_buckets[b]; + p_buckets[b] = p; + p_num_entries++; + return TRUE; +} + +template +int EST_THash::remove_item(const K &rkey, int quiet) +{ + unsigned int b; + if (p_hash_function) + b = (*p_hash_function)(rkey, p_num_buckets); + else + b = DefaultHashFunction((void *)&rkey, sizeof(rkey), p_num_buckets); + + EST_Hash_Pair **p; + + for (p = &(p_buckets[b]); *p!=NULL; p=&((*p)->next)) + if ( (*p)->k == rkey ) + { + EST_Hash_Pair *n = (*p)->next; + delete *p; + *p = n; + p_num_entries--; + return 0; + } + + if (!quiet) + cerr << "THash: no item labelled \"" << rkey << "\"" << endl; + return -1; +} + +template +EST_THash &EST_THash::operator = (const EST_THash &from) +{ + copy(from); + return *this; +} + +template +void EST_THash::dump(ostream &stream, int all) +{ + for(unsigned int i=0; i *p; + for(p=p_buckets[i]; p!=NULL; p=p->next) + stream << "[" << p->k << "],(" << p->v << ") "; + stream << "\n"; + } +} + +template +void EST_THash::copy(const EST_THash &from) +{ + clear(); + p_num_entries = from.p_num_entries; + p_num_buckets = from.p_num_buckets; + p_hash_function = from.p_hash_function; + + if (p_buckets != NULL) + delete [] p_buckets; + + p_buckets = new EST_Hash_Pair *[p_num_buckets]; + + + for(unsigned int b=0; b *p=from.p_buckets[b]; p; p=p->next) + { + EST_Hash_Pair *n = new EST_Hash_Pair(*p); + n->next = p_buckets[b]; + p_buckets[b]=n; + } + } +} + + + + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TKVL.cc b/aeneas/cfw/speech_tools/base_class/EST_TKVL.cc new file mode 100644 index 00000000..1f9a6955 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TKVL.cc @@ -0,0 +1,304 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : May 1995 */ +/*-----------------------------------------------------------------------*/ +/* Key/value List Class source file */ +/* */ +/*=======================================================================*/ + +#include +#include "EST_TKVL.h" +#include "EST_error.h" + +template EST_TKVL::EST_TKVL(const EST_TKVL &kv) +{ + list = kv.list; +} + +template +void EST_TKVL::clear(void) +{ + list.clear(); +} + +template +EST_Litem *EST_TKVL::find_pair_key(const K &key) const +{ + EST_Litem *ptr; + + for (ptr = list.head(); ptr != 0; ptr= ptr->next()) + if (list.item(ptr).k == key) + return ptr; + return 0; +} + +template +EST_Litem *EST_TKVL::find_pair_val(const V &val) const +{ + EST_Litem *ptr; + +/// cout << "function list\n" << endl; + + for (ptr = list.head(); ptr != 0; ptr= ptr->next()) + { +// cout << "ff: " << list.item(ptr).k << endl; + if (list.item(ptr).v == val) + return ptr; + } + return 0; +} + + +// look for pointer kptr in list. If found, change its value to rval and +// return true, otherwise return false. + +template +int EST_TKVL::change_val(EST_Litem *kptr, const V &rval) +{ + if (list.index(kptr) == -1) + return 0; + else + { + list.item(kptr).v = rval; + return 1; + } +} + +template +int EST_TKVL::change_key(EST_Litem *kptr, const K &rkey) +{ + if (list.index(kptr) == -1) + return 0; + else + { + list.item(kptr).k = rkey; + return 1; + } +} + +// look for key rkey in list. If found, change its value to rval and +// return true, otherwise return false. +template +int EST_TKVL::change_val(const K &rkey,const V &rval) +{ + EST_Litem *ptr=find_pair_key(rkey); + if (ptr == 0) + return 0; + else + { + list.item(ptr).v = rval; + return 1; + } +} + +// NOTE: This _MUST_NOT_ change the EST_TKVL, if it needs to, a separate +// const version will need to replace the dummy one below. + +template +V &EST_TKVL::val(const K &rkey, bool must) +{ + EST_Litem *ptr = find_pair_key(rkey); + + if (ptr == 0) + { + if (must) + EST_error("No value set for '%s'", error_name(rkey)); + + return *default_val; + } + else + return list.item(ptr).v; +} + +template +const V &EST_TKVL::val(const K &rkey, bool must) const +{ + return ((EST_TKVL *)this)->val(rkey, must); +} + +template +const V &EST_TKVL::val_def(const K &rkey, const V &def) const +{ + EST_Litem *ptr = find_pair_key(rkey); + if (ptr == 0) + return def; + else + return list.item(ptr).v; +} + +// NOTE: This _MUST_NOT_ change the EST_TKVL, if it needs to, a separate +// const version will need to replace the dummy one below. + +template +V &EST_TKVL::val(EST_Litem *kptr, bool must) +{ + if (must == 0) + return list.item(kptr).v; + /* check kptr is one of mine */ + if (list.index(kptr) == -1) + { + if (must) + EST_error("No value set in EST_TKVL"); + return *default_val; + } + else + return list.item(kptr).v; +} + + +template +const V &EST_TKVL::val(EST_Litem *kptr, bool must) const +{ + return ((EST_TKVL *)this)->val(kptr, must); +} + +// NOTE: This _MUST_NOT_ change the EST_TKVL, if it needs to, a separate +// const version will need to replace the dummy one below. + +template +K &EST_TKVL::key(EST_Litem *kptr, int must) +{ + if (must == 0) + return list.item(kptr).k; + if (list.index(kptr) == -1) + EST_error("No value set in EST_TKVL"); + + return list.item(kptr).k; +} + +template +const K &EST_TKVL::key(EST_Litem *kptr, int must) const +{ + return ((EST_TKVL *)this)->key(kptr, must); +} + +template +const K &EST_TKVL::key(const V &v, int must) const +{ + EST_Litem *ptr = find_pair_val(v); + if (ptr == 0) + { + if (must) + EST_error("No value set for '%s'", error_name(v)); + + return *default_key; + } + + return list.item(ptr).k; +} + +template +const int EST_TKVL::present(const K &rkey) const +{ + if (find_pair_key(rkey) == 0) + return 0; + else + return 1; +} + +// map a function over the pairs + +template +void EST_TKVL::map(void (*func)(K&, V&)) +{ + EST_Litem *p; + for(p=list.head(); p; p=p->next()) + { + EST_TKVI item = list.item(p); + (*func)(item.k, item.v); + } +} + +// add item to list. By default, the list is searched to see if the +// item exists already. If so, its value is overwritten. This facility +// can be turned off by setting no_search = 1; + +template +int EST_TKVL::add_item(const K &rkey, const V &rval, int no_search) +{ + if (!no_search) + if (change_val(rkey, rval)) // first see if key exists + return 1; + + EST_TKVI item; + item.k = rkey; + item.v = rval; + + list.append(item); + return 1; +} + +template +int EST_TKVL::remove_item(const K &rkey, int quiet) +{ + EST_Litem *ptr = find_pair_key(rkey); + const char *en; + if (ptr == 0) + { + if (!quiet) + { + en = error_name(rkey); + EST_warning("EST_TKVL: no item labelled '%s'", en); + } + return -1; + } + else + { + list.remove(ptr); + return 0; + } +} + +template EST_TKVL &EST_TKVL::operator = +(const EST_TKVL &kv) +{ + list = kv.list; + return *this; +} + +template EST_TKVL &EST_TKVL::operator += +(const EST_TKVL &kv) +{ + list += kv.list; + return *this; +} + +template EST_TKVL EST_TKVL::operator + (const EST_TKVL &kv) +{ + EST_TKVL result; + result = *this; + result += kv; + return result; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TList.cc b/aeneas/cfw/speech_tools/base_class/EST_TList.cc new file mode 100644 index 00000000..783e357f --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TList.cc @@ -0,0 +1,133 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1995 */ + /* --------------------------------------------------------------------- */ + /* Template List Class */ + /* */ + /* Modified by RJC, 21/7/97. Now much of the working code is in the */ + /* UList class, this template class provides a type safe front end to */ + /* the untyped list. */ + /* */ + /*************************************************************************/ + +#include "EST_TList.h" + +template EST_TItem *EST_TItem::make(const T &val) +{ + EST_TItem *it=NULL; + if (s_free!=NULL) + { + void *mem = s_free; + s_free=(EST_TItem *)s_free->n; + s_nfree--; + + // Create an item in the retrieved memory. + it=new (mem) EST_TItem(val); + } + else + it = new EST_TItem(val); + + return it; +} + +template void EST_TItem::release(EST_TItem *it) +{ + if (0) // (s_nfree < s_maxFree) + { + // Destroy the value in case it holds resources. + it->EST_TItem::~EST_TItem(); + + // I suppose it's a bit weird to use 'n' after calling the destructor. + it->n=s_free; + s_free=it; + s_nfree++; + } + else + { + delete it; + } +} + +template void EST_TList::copy_items(const EST_TList &l) +{ + EST_UItem *p; + for (p = l.head(); p; p = p->next()) + append(l.item(p)); +} + +template void EST_TList::free_item(EST_UItem *item) +{ EST_TItem::release((EST_TItem *)item); } + + +template EST_TList::EST_TList(const EST_TList &l) +{ + init(); + copy_items(l); +} + +template void EST_TList::exchange_contents(EST_Litem *a,EST_Litem *b) +{ + + if(a==b) + return; + + T temp; + + temp = ((EST_TItem *)a)->val; + ((EST_TItem *)a)->val = ((EST_TItem *)b)->val; + ((EST_TItem *)b)->val = temp; + +} + + +template EST_TList &EST_TList::operator=(const EST_TList &a) +{ + clear(); // clear out all current items in list. + copy_items(a); + return *this; +} + + +template EST_TList &EST_TList::operator+=(const EST_TList &a) +{ + if (this == &a) + { + cerr << "EST_TList: error: tried to add list to itself\n"; + return *this; + } + copy_items(a); + return *this; +} + + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_TMatrix.cc new file mode 100644 index 00000000..9ded7f9a --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TMatrix.cc @@ -0,0 +1,601 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Rewritten : Richard Caley */ + /* ------------------------------------------------------------------- */ + /* Template EST_TMatrix Class */ + /* */ + /*************************************************************************/ + +#include "EST_TMatrix.h" +#include +#include +#include "EST_bool.h" +#include "EST_matrix_support.h" +#include "EST_TVector.h" +#include "EST_cutils.h" +#include "EST_error.h" + +/* Construction and destruction + */ + +template +void EST_TMatrix::default_vals() +{ + EST_TVector::default_vals(); + p_num_rows = 0; + p_row_step=0; +} + +template +EST_TMatrix::EST_TMatrix() +{ + default_vals(); +} + +template +EST_TMatrix::EST_TMatrix(const EST_TMatrix &in) +{ + default_vals(); + copy(in); +} + +template +EST_TMatrix::EST_TMatrix(int rows, int cols) +{ + default_vals(); + resize(rows, cols); +} + +template +EST_TMatrix::EST_TMatrix(int rows, int cols, + T *memory, int offset, int free_when_destroyed) +{ + default_vals(); + set_memory(memory, offset, rows, cols, free_when_destroyed); +} + +template +EST_TMatrix::~EST_TMatrix() +{ + p_num_rows = 0; + p_row_step=0; +} + +/* Basic access + */ + +template +T &EST_TMatrix::a_check(int row, int col) +{ + + if (!EST_matrix_bounds_check(row, col, num_rows(), num_columns(), FALSE)) + return *this->error_return; + + return a_no_check(row,col); +} + +/* Since we know a() itself doesn't change the matrix, we can cast away + * the const here. Isn't the C++ syntax beautiful! + */ +template +const T &EST_TMatrix::a_check(int row, int col) const +{ + return ((EST_TMatrix *)this)->a(row,col); +} + +template +void EST_TMatrix::copy_data(const EST_TMatrix &a) +{ + + set_values(a.p_memory, + a.p_row_step, a.p_column_step, + 0, a.num_rows(), + 0, a.num_columns()); +} + +template +void EST_TMatrix::set_values(const T *data, + int r_step, int c_step, + int start_r, int num_r, + int start_c, int num_c + ) +{ + for(int r=start_r, i=0, rp=0; i< num_r; i++, r++, rp+=r_step) + for(int c=start_c, j=0, cp=0; j< num_c; j++, c++, cp+=c_step) + a_no_check(r,c) = data[rp+cp]; +} + +template +void EST_TMatrix::get_values(T *data, + int r_step, int c_step, + int start_r, int num_r, + int start_c, int num_c + ) const +{ + for(int r=start_r, i=0, rp=0; i< num_r; i++, r++, rp+=r_step) + for(int c=start_c, j=0, cp=0; j< num_c; j++, c++, cp+=c_step) + data[rp+cp] = a_no_check(r,c); +} + +template +void EST_TMatrix::copy(const EST_TMatrix &a) +{ + resize(a.num_rows(), a.num_columns(), 0); + copy_data(a); +} + +template +EST_TMatrix &EST_TMatrix::operator=(const EST_TMatrix &in) +{ + copy(in); + return *this; +} + +template +EST_TMatrix &EST_TMatrix::add_rows(const EST_TMatrix &in) +{ + if (in.num_columns() != num_columns()) + EST_error("Can't add rows with differnet number of columns (%d vs %d)", + in.num_columns(), + num_columns() + ); + else + { + int old_num_rows = num_rows(); + resize(num_rows()+in.num_rows(), num_columns(), TRUE); + + for(int i=old_num_rows, i1=0; i +EST_TMatrix &EST_TMatrix::add_columns(const EST_TMatrix &in) +{ + if (in.num_rows() != num_rows()) + EST_error("Can't add columns with differnet number of rows (%d vs %d)", + in.num_rows(), + num_rows() + ); + else + { + int old_num_columns = num_columns(); + resize(num_columns()+in.num_columns(), num_rows(), TRUE); + + for(int i=old_num_columns, i1=0; i +void EST_TMatrix::just_resize(int new_rows, + int new_cols, + T** old_vals) +{ + T *new_m; + + if (num_rows() != new_rows || num_columns() != new_cols || this->p_memory == NULL ) + { + if (this->p_sub_matrix) + EST_error("Attempt to resize Sub-Matrix"); + + if (new_cols < 0 || new_rows < 0) + EST_error("Attempt to resize matrix to negative size: %d x %d", + new_rows, + new_cols); + + + new_m = new T[new_rows*new_cols]; + + if (this->p_memory != NULL) + { + if (old_vals != NULL) + *old_vals = this->p_memory; + else if (!this->p_sub_matrix) + delete [] (this->p_memory-this->p_offset); + } + + p_num_rows = new_rows; + this->p_num_columns = new_cols; + this->p_offset=0; + p_row_step=this->p_num_columns; + this->p_column_step=1; + + this->p_memory = new_m; + } + else + *old_vals = this->p_memory; + +} + +template +void EST_TMatrix::resize(int new_rows, int new_cols, int set) +{ + int i,j; + T * old_vals = this->p_memory; + int old_rows = num_rows(); + int old_cols = num_columns(); + int old_row_step = p_row_step; + int old_offset = this->p_offset; + int old_column_step = this->p_column_step; + + if (new_rows<0) + new_rows = old_rows; + if (new_cols<0) + new_cols = old_cols; + + just_resize(new_rows, new_cols, &old_vals); + + if (set) + { + int copy_r = 0; + int copy_c = 0; + + if (old_vals != NULL) + { + copy_r = Lof(num_rows(), old_rows); + copy_c = Lof(num_columns(), old_cols); + + set_values(old_vals, + old_row_step, old_column_step, + 0, copy_r, + 0, copy_c); + } + else + { + copy_r = old_rows; + copy_c = old_cols; + } + + for(i=0; idef_val; + + for(i=copy_r; idef_val; + } + + if (old_vals && old_vals != this->p_memory && !this->p_sub_matrix) + delete [] (old_vals-old_offset); +} + +template +bool EST_TMatrix::have_rows_before(int n) const +{ + return this->p_offset >= n*p_row_step; +} + +template +bool EST_TMatrix::have_columns_before(int n) const +{ + return this->p_offset >= n*this->p_column_step; +} + +template +void EST_TMatrix::fill(const T &v) +{ + int i, j; + for (i = 0; i < num_rows(); ++i) + for (j = 0; j < num_columns(); ++j) + fast_a_m(i,j) = v; +} + + +template +EST_write_status EST_TMatrix::save(const EST_String &filename) const +{ + int i, j; + ostream *outf; + if (filename == "-" || filename == "") + outf = &cout; + else + outf = new ofstream(filename); + + for (i = 0; i < num_rows(); ++i) + { + for (j = 0; j < num_columns(); ++j) + { + *outf +#if 0 + << "{" < +EST_read_status +EST_TMatrix::load(const EST_String &filename) +{ + // this function can only be written if we can find a way of parsing + // an unknown type; + (void) filename; + EST_error("Matrix loading not implemented yet."); + return misc_read_error; + +} + +template +void EST_TMatrix::set_memory(T *buffer, int offset, + int rows, int columns, + int free_when_destroyed) +{ + EST_TVector::set_memory(buffer, offset, columns, free_when_destroyed); + p_num_rows = rows; + p_row_step = columns; +} + +template +void EST_TMatrix::copy_row(int r, T *buf, + int offset, int num) const +{ + int to = num >= 0 ? offset + num : num_columns(); + + if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), FALSE)) + { + if (num_rows()>0) + r=0; + else + return; + } + + for (int j = offset; j < to; j++) + buf[j-offset] = fast_a_m(r, j); +} + +template +void EST_TMatrix::copy_row(int r, EST_TVector &buf, + int offset, int num) const +{ + int to = num >= 0 ? offset + num : num_columns(); + + if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), FALSE)) + { + if (num_rows()>0) + r=0; + else + return; + } + + buf.resize(to - offset); + + for (int j = offset; j < to; j++) + buf[j - offset] = fast_a_m(r, j); +} + + +template +void EST_TMatrix::copy_column(int c, T *buf, + int offset, int num) const +{ + if (num_rows() == 0) + return; + + int to = num >= 0 ? offset + num : num_rows(); + + if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), FALSE)) + { + if (num_columns()>0) + c=0; + else + return; + } + + for (int i = offset; i < to; i++) + buf[i-offset] = fast_a_m(i, c); +} + + +template +void EST_TMatrix::copy_column(int c, EST_TVector &buf, + int offset, int num) const +{ + if (num_rows() == 0) + return; + + int to = num >= 0 ? offset + num : num_rows(); + + if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), FALSE)) + { + if( num_columns()>0 ) + c=0; + else + return; + } + + buf.resize(to - offset); + + for (int i = offset; i < to; i++) + buf[i-offset] = fast_a_m(i, c); +} + + +template +void EST_TMatrix::set_row(int r, const T *buf, int offset, int num) +{ + int to = num>=0?offset+num:num_columns(); + + if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), TRUE)) + return; + + for(int j=offset; j +void EST_TMatrix::set_column(int c, const T *buf, int offset, int num) +{ + int to = num>=0?offset+num:num_rows(); + + if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), TRUE)) + return; + + for(int i=offset; i +void EST_TMatrix::set_row(int r, + const EST_TMatrix &from, int from_r, int from_offset, + int offset, int num) +{ + int to = num>=0?offset+num:num_columns(); + + if (!EST_matrix_bounds_check(r, 0, num_rows(), num_columns(), TRUE)) + return; + + if (!EST_matrix_bounds_check(from_r, 0, from.num_rows(), from.num_columns(), FALSE)) + { + if (from.num_rows()>0) + from_r=0; + else + return; + } + + for(int j=offset; j +void EST_TMatrix::set_column(int c, + const EST_TMatrix &from, int from_c, int from_offset, + int offset, int num) +{ + int to = num>=0?offset+num:num_rows(); + + if (!EST_matrix_bounds_check(0, c, num_rows(), num_columns(), TRUE)) + return; + + if (!EST_matrix_bounds_check(0, from_c, from.num_rows(), from.num_columns(), FALSE)) + { + if (from.num_columns()>0) + from_c=0; + else + return; + } + + for(int i=offset; i +void EST_TMatrix::row(EST_TVector &rv, int r, int start_c, int len) +{ + if (len < 0) + len = num_columns()-start_c; + + if (!EST_matrix_bounds_check(r, 1, start_c, len, num_rows(), num_columns(), 0)) + return; + + if (rv.p_memory != NULL && ! rv.p_sub_matrix) + delete [] (rv.p_memory - rv.p_offset); + + rv.p_sub_matrix = TRUE; + rv.p_num_columns = len; + rv.p_offset = this->p_offset + start_c*this->p_column_step + r*p_row_step; + rv.p_memory = this->p_memory - this->p_offset + rv.p_offset; +// cout << "mrow: mem: " << rv.p_memory << " (" << (int)rv.p_memory << ")\n"; +// cout << "mrow: ofset: " << rv.p_offset << " (" << (int)rv.p_offset << ")\n"; + + rv.p_column_step=this->p_column_step; +} + +template +void EST_TMatrix::column(EST_TVector &cv, int c, int start_r, int len) +{ + if (len < 0) + len = num_rows()-start_r; + + if (!EST_matrix_bounds_check(start_r, len, c, 1,num_rows(), num_columns(), 0)) + return; + + if (cv.p_memory != NULL && ! cv.p_sub_matrix) + delete [] (cv.p_memory - cv.p_offset); + + cv.p_sub_matrix = TRUE; + cv.p_num_columns = len; + cv.p_offset = this->p_offset + c*this->p_column_step + start_r*p_row_step; + cv.p_memory = this->p_memory - this->p_offset + cv.p_offset; +// cout << "mcol: mem: " << cv.p_memory << " (" << (int)cv.p_memory << ")\n"; +// cout << "mcol: offset: " << cv.p_offset << " (" << (int)cv.p_offset << ")\n"; + + cv.p_column_step=p_row_step; +} + +template +void EST_TMatrix::sub_matrix(EST_TMatrix &sm, + int r, int len_r, int c, int len_c) +{ + if (len_r < 0) + len_r = num_rows()-r; + if (len_c < 0) + len_c = num_columns()-c; + + if (!EST_matrix_bounds_check(r, len_r, c, len_c, num_rows(), num_columns(), 0)) + return; + + if (sm.p_memory != NULL && ! sm.p_sub_matrix) + delete [] (sm.p_memory - sm.p_offset); + + sm.p_sub_matrix = TRUE; + sm.p_offset = this->p_offset + c*this->p_column_step + r*p_row_step; + sm.p_memory = this->p_memory - this->p_offset + sm.p_offset; + sm.p_row_step=p_row_step; + sm.p_column_step=this->p_column_step; + sm.p_num_rows = len_r; + sm.p_num_columns = len_c; + +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TNamedEnum.cc b/aeneas/cfw/speech_tools/base_class/EST_TNamedEnum.cc new file mode 100644 index 00000000..ac50c2c3 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TNamedEnum.cc @@ -0,0 +1,330 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Feb 28 1997 */ + /************************************************************************/ + /* */ + /* A template class which allows names (const char *s) to be */ + /* associated with enums, providing conversion. */ + /* */ + /************************************************************************/ + +#include +#include +#include +#include "EST_walloc.h" +#include "EST_TNamedEnum.h" + +// This only takes a void * because I can't manage to get the +// parameter declaration in the definition past gcc with the actual type. + +template +void EST_TValuedEnumI::initialise(const void *vdefs) +{ + int n=0; + typedef EST_TValuedEnumDefinition defn; + const defn *defs = (const defn *)vdefs; + + for(n=1; defs[n].token != defs[0].token; n++) + ; + + this->ndefinitions = n; + this->definitions = new defn[n]; + + this->definitions[0] = defs[0]; + for(n=1; defs[n].token != defs[0].token; n++) + this->definitions[n] = defs[n]; + + this->p_unknown_enum = defs[n].token; + this->p_unknown_value = defs[n].values[0]; +} + +template +void EST_TValuedEnumI::initialise(const void *vdefs, ENUM (*conv)(const char *)) +{ + int n=0; + // const struct EST_TValuedEnumDefinition *defs = (const struct EST_TValuedEnumDefinition *)vdefs; + + typedef EST_TValuedEnumDefinition _EST_TMPNAME; + const _EST_TMPNAME *defs = (const _EST_TMPNAME *)vdefs; + + // fprintf(stderr, "start setup\n"); + + for(n=1; strcmp(defs[n].token, defs[0].token) != 0; n++) + { + //const char *a = defs[0].token; + // const char *b = defs[n].token; + // fprintf(stderr, ": %d '%s' '%s'\n", n, defs[n].token, defs[0].token); + } + + this->ndefinitions = n; + typedef EST_TValuedEnumDefinition defn; + this->definitions = new defn[n]; + + this->definitions[0].token = conv(defs[0].token); + for(int i=0; idefinitions[0].values[i] = defs[0].values[i]; + this->definitions[0].info = defs[0].info; + for(n=1; strcmp(defs[n].token, defs[0].token) != 0; n++) + { + this->definitions[n].token = conv(defs[n].token); + for(int i2=0; i2definitions[n].values[i2] = defs[n].values[i2]; + this->definitions[n].info = defs[n].info; + } + + this->p_unknown_enum = conv(defs[n].token); + this->p_unknown_value = defs[n].values[0]; +} + +template +EST_TValuedEnumI::~EST_TValuedEnumI(void) +{ + if (this->definitions) + delete[] this->definitions; +} + +template +int EST_TValuedEnumI::n(void) const +{ +return this->ndefinitions; +} + +template +VAL EST_TValuedEnumI::value (ENUM token, int n) const +{ + int i; + + for(i=0; indefinitions; i++) + if (this->definitions[i].token == token) + return this->definitions[i].values[n]; + + return this->p_unknown_value; +} + +template +INFO &EST_TValuedEnumI::info (ENUM token) const +{ + int i; + + for(i=0; indefinitions; i++) + if (this->definitions[i].token == token) + return this->definitions[i].info; + + cerr << "Fetching info for invalid entry\n"; + abort(); + + static INFO dummyI; + return dummyI; +} + +template +ENUM EST_TValuedEnumI::nth_token (int n) const +{ + if (n>=0 && n < this->ndefinitions) + return this->definitions[n].token; + + return this->p_unknown_enum; +} + +template +ENUM EST_TValuedEnumI::token (VAL value) const +{ + int i,j; + + for(i=0; indefinitions; i++) + for(j=0; jdefinitions[i].values[j] ; j++) + if (eq_vals(this->definitions[i].values[j], value)) + return this->definitions[i].token; + + return this->p_unknown_enum; +} + +template +EST_read_status EST_TNamedEnum::priv_load(EST_String name, EST_TNamedEnum *definitive) +{ + typedef EST_TValuedEnumDefinition Defn; +#define LINE_LENGTH (1024) + EST_String line(NULL, 'x', LINE_LENGTH); + char *buffer = (char *)line; + EST_String tokens[NAMED_ENUM_MAX_SYNONYMS+2]; + FILE *file; + char quote = '\0'; + int have_unknown=0; + int n=0; + + if ((file=fopen(name, "rb"))==NULL) + return misc_read_error; + + if (this->definitions) + delete[] this->definitions; + + this->ndefinitions= -1; + this->definitions=NULL; + + buffer[LINE_LENGTH-1] = 'x'; + + while (fgets(buffer, LINE_LENGTH, file)) + { + if ( buffer[LINE_LENGTH-1] != 'x') + { + cerr << "line too long .. '" << buffer << "'\n"; + return wrong_format; + } + + if (this->ndefinitions>=0 && quote != '\0' && buffer[0] == '=') + { + // definition by number + + if ( n>= this->ndefinitions) + { + cerr << "too many definitions\n"; + return wrong_format; + } + + int ntokens = split(line, tokens, NAMED_ENUM_MAX_SYNONYMS+2, RXwhite, '"'); + this->definitions[n].token = (ENUM)atoi(tokens[0].after(0,1)); + + for(int i=1; idefinitions[n].values[i-1] = wstrdup(tokens[i].unquote_if_needed(quote)); + for(int j=ntokens-1 ; j< NAMED_ENUM_MAX_SYNONYMS; j++) + this->definitions[n].values[j]=NULL; + + n++; + } + else if (have_unknown && this->ndefinitions>=0 && quote != '\0' && buffer[0] == quote) + { + // definition by standard name + if (!definitive) + { + cerr << "can't use names in this definition\n"; + return wrong_format; + } + if ( n>= this->ndefinitions) + { + cerr << "too many definitions\n"; + return wrong_format; + } + + int ntokens = split(line, tokens, NAMED_ENUM_MAX_SYNONYMS+2, RXwhite, quote); + + this->definitions[n].token = definitive->token(tokens[0].unquote(quote)); + + for(int i=1; idefinitions[n].values[i-1] = wstrdup(tokens[i].unquote_if_needed(quote)); + for(int j=ntokens-1 ; j< NAMED_ENUM_MAX_SYNONYMS; j++) + this->definitions[n].values[j]=NULL; + + n++; + } + else + { + // parameter + + int mlen; + int eq = line.search("=", 1, mlen); + + if (eq <0) + { + cerr << "bad header line '" << line; + return wrong_format; + } + + EST_String key(line.before(eq)); + + if (key == "quote") + { + quote = line[eq+1]; + // cout << "quote = '" << quote << "'\n"; + } + else if (key == "number") + { + this->ndefinitions=atoi(line.after(eq,1)); + // cout << "n = '" << ndefinitions << "'\n"; + this->definitions = new Defn[this->ndefinitions]; + for(int i=0; indefinitions; i++) + this->definitions[i].values[0] =NULL; + n=0; + } + else if (key == "unknown") + { + this->p_unknown_enum=(ENUM)atoi(line.after(eq,1)); + // cout << "unknown = '" << p_unknown_enum << "'\n"; + have_unknown=1; + } + else + { + cerr << "bad header line '" << line; + return wrong_format; + } + + } + } + + + fclose(file); + + return format_ok; +} + +template +EST_write_status EST_TNamedEnum::priv_save(EST_String name, EST_TNamedEnum *definitive, char quote) const +{ + FILE *file; + + if ((file=fopen(name, "wb"))==NULL) + return write_fail; + + fprintf(file, "unknown=%d\n", this->p_unknown_enum); + fprintf(file, "quote=%c\n", quote); + fprintf(file, "number=%d\n", this->ndefinitions); + + for(int i=0; indefinitions; i++) + if (this->definitions[i].values[0]) + { + if (definitive) + fprintf(file, "%s ", (const char *)EST_String(definitive->name(this->definitions[i].token)).quote(quote)); + else + fprintf(file, "=%d ", (int)this->definitions[i].token); + + for(int j=0; jdefinitions[i].values[j] != NULL; j++) + fprintf(file, "%s ", (const char *) EST_String(this->definitions[i].values[j]).quote_if_needed(quote)); + + fputc('\n', file); + } + + fclose(file); + + return write_ok; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TSimpleMatrix.cc b/aeneas/cfw/speech_tools/base_class/EST_TSimpleMatrix.cc new file mode 100644 index 00000000..c804b928 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TSimpleMatrix.cc @@ -0,0 +1,177 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Oct 10 1997 */ + /* -------------------------------------------------------------------- */ + /* A subclass of TMatrix which copies using memcopy. This isn't */ + /* suitable for matrices of class objects which have to be copied */ + /* using a constructor or specialised assignment operator. */ + /* */ + /*************************************************************************/ + +#include "EST_TSimpleMatrix.h" +#include "EST_TVector.h" +#include +#include +#include +#include "EST_cutils.h" + +using std::memcpy; + + +template +void EST_TSimpleMatrix::copy_data(const EST_TSimpleMatrix &a) +{ + + if (!a.p_sub_matrix && !this->p_sub_matrix) + memcpy((void *)&this->a_no_check(0,0), + (const void *)&a.a_no_check(0,0), + this->num_rows()*this->num_columns()*sizeof(T) + ); + else + { + for (int i = 0; i < this->num_rows(); ++i) + for (int j = 0; j < this->num_columns(); ++j) + this->a_no_check(i,j) = a.a_no_check(i,j); + } +} + +template +void EST_TSimpleMatrix::copy(const EST_TSimpleMatrix &a) +{ + if (this->num_rows() != a.num_rows() || this->num_columns() != a.num_columns()) + resize(a.num_rows(), a.num_columns(), 0); + + copy_data(a); +} + +template +EST_TSimpleMatrix::EST_TSimpleMatrix(const EST_TSimpleMatrix &in) +{ + copy(in); +} + +template +void EST_TSimpleMatrix::resize(int new_rows, + int new_cols, + int set) +{ + T* old_vals=NULL; + int old_offset = this->p_offset; + unsigned int q; + + if (new_rows<0) + new_rows = this->num_rows(); + if (new_cols<0) + new_cols = this->num_columns(); + + if (set) + { + if (!this->p_sub_matrix && new_cols == this->num_columns() && new_rows != this->num_rows()) + { + int copy_r = Lof(this->num_rows(), new_rows); + + this->just_resize(new_rows, new_cols, &old_vals); + + for (q=0; q<(copy_r*new_cols*sizeof(T)); q++) /* memcpy */ + ((char *)this->p_memory)[q] = ((char *)old_vals)[q]; + + int i,j; + + if (new_rows > copy_r) + { + if (*this->def_val == 0) + { + for (q=0; q<(new_rows-copy_r)*new_cols*sizeof(T); q++) /* memset */ + ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0; + } + else + { + for(j=0; ja_no_check(i,j) = *this->def_val; + } + } + } + else if (!this->p_sub_matrix) + { + int old_row_step = this->p_row_step; + int old_column_step = this->p_column_step; + int copy_r = Lof(this->num_rows(), new_rows); + int copy_c = Lof(this->num_columns(), new_cols); + + this->just_resize(new_rows, new_cols, &old_vals); + + this->set_values(old_vals, + old_row_step, old_column_step, + 0, copy_r, + 0, copy_c); + + int i,j; + + for(i=0; ia_no_check(i,j) = *this->def_val; + + if (new_rows > copy_r) + { + if (*this->def_val == 0) + { + for (q=0; q<((new_rows-copy_r)*new_cols*sizeof(T)); q++) /* memset */ + ((char *)(this->p_memory + copy_r*this->p_row_step))[q] = 0; + } + else + { + for(j=0; ja_no_check(i,j) = *this->def_val; + } + } + } + else + EST_TMatrix::resize(new_rows, new_cols, 1); + } + else + EST_TMatrix::resize(new_rows, new_cols, 0); + + if (old_vals && old_vals != this->p_memory) + delete [] (old_vals-old_offset); +} + +template EST_TSimpleMatrix &EST_TSimpleMatrix::operator=(const EST_TSimpleMatrix &in) +{ + copy(in); + return *this; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TSimpleVector.cc b/aeneas/cfw/speech_tools/base_class/EST_TSimpleVector.cc new file mode 100644 index 00000000..06191f84 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TSimpleVector.cc @@ -0,0 +1,152 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Oct 10 1997 */ + /* -------------------------------------------------------------------- */ + /* A subclass of TVector which copies using memcopy. This isn't */ + /* suitable for matrices of class objects which have to be copied */ + /* using a constructor or specialised assignment operator. */ + /* */ + /*************************************************************************/ + +#include "EST_TSimpleVector.h" +#include "EST_matrix_support.h" +#include +#include +#include "EST_cutils.h" + +using std::memset; +using std::memcpy; + +template void EST_TSimpleVector::copy(const EST_TSimpleVector &a) +{ + if (this->p_column_step==1 && a.p_column_step==1) + { + resize(a.n(), FALSE); + memcpy((void *)(this->p_memory), (const void *)(a.p_memory), this->n() * sizeof(T)); + } +else + ((EST_TVector *)this)->copy(a); +} + +template EST_TSimpleVector::EST_TSimpleVector(const EST_TSimpleVector &in) +{ + this->default_vals(); + copy(in); +} + +// should copy from and delete old version first +template void EST_TSimpleVector::resize(int newn, int set) +{ + int oldn = this->n(); + T *old_vals =NULL; + int old_offset = this->p_offset; + unsigned int q; + + this->just_resize(newn, &old_vals); + + if (set && old_vals) + { + int copy_c = 0; + if (this->p_memory != NULL) + { + copy_c = Lof(this->n(), oldn); + for (q=0; qp_memory)[q] = ((char *)old_vals)[q]; + } + + for (int i=copy_c; i < this->n(); ++i) + this->p_memory[i] = *this->def_val; + } + + if (old_vals != NULL && old_vals != this->p_memory && !this->p_sub_matrix) + delete [] (old_vals - old_offset); + +} + +template +void EST_TSimpleVector::copy_section(T* dest, int offset, int num) const +{ + unsigned int q; + if (num<0) + num = this->num_columns()-offset; + + if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE)) + return; + + if (!this->p_sub_matrix && this->p_column_step==1) + { + for (q=0; qp_memory+offset))[q]; + } + else + for(int i=0; ia_no_check(offset+i); +} + +template +void EST_TSimpleVector::set_section(const T* src, int offset, int num) +{ + unsigned int q; + if (num<0) + num = this->num_columns()-offset; + + if (!EST_vector_bounds_check(num+offset-1, this->num_columns(), FALSE)) + return; + + if (!this->p_sub_matrix && this->p_column_step==1) + { + for (q=0; qp_memory+offset))[q] = ((char *)(src))[q]; + } + else + for(int i=0; ia_no_check(offset+i) = src[i]; +} + +template EST_TSimpleVector &EST_TSimpleVector::operator=(const EST_TSimpleVector &in) +{ + copy(in); + return *this; +} + +template void EST_TSimpleVector::zero() +{ + if (this->p_column_step==1) + memset((void *)(this->p_memory), 0, this->n() * sizeof(T)); + else + ((EST_TVector *)this)->fill(*this->def_val); +} + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TSortable.cc b/aeneas/cfw/speech_tools/base_class/EST_TSortable.cc new file mode 100644 index 00000000..7750eb93 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TSortable.cc @@ -0,0 +1,70 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Simon King, Richard Caley */ + /* Date : November 1996 */ + /* --------------------------------------------------------------------- */ + /* Sorting order definitions. */ + /* */ + /*************************************************************************/ + + + +//#include +//#include +#include "EST_TSortable.h" + +// requires operators '<' and '>' for type T + +template +bool EST_TSortable::items_eq(const EST_UItem *item1, const EST_UItem *item2) +{ + return ((EST_TItem *)item1)->val == ((EST_TItem *)item2)->val; +} + +template +bool EST_TSortable::items_lt(const EST_UItem *item1, const EST_UItem *item2) +{ + return ((EST_TItem *)item1)->val < ((EST_TItem *)item2)->val; +} + +template +bool EST_TSortable::items_gt(const EST_UItem *item1, const EST_UItem *item2) +{ + return ((EST_TItem *)item1)->val > ((EST_TItem *)item2)->val; +} + + + + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_TTimeIndex.cc b/aeneas/cfw/speech_tools/base_class/EST_TTimeIndex.cc new file mode 100644 index 00000000..db2b5569 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TTimeIndex.cc @@ -0,0 +1,137 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Wed Mar 25 1998 */ + /* -------------------------------------------------------------------- */ + /* Indexing a container by time. */ + /* */ + /*************************************************************************/ + +#include "EST_TTimeIndex.h" + +template +void EST_TTimeIndex::initialise() +{ + p_time_step=0; + p_entries.resize(0); +} + +template +EST_TTimeIndex::EST_TTimeIndex() +{ + initialise(); +} + + +template +EST_TTimeIndex::EST_TTimeIndex(CONTAINER &c, int bunch) +{ + initialise(); + index(c, bunch); +} + +template +void EST_TTimeIndex::index(CONTAINER &c, int bunch) +{ + int n_objects = c.length(); + float total_time = c.end(); + + int n_buckets = n_objects/bunch +1; + + p_time_step = total_time / n_buckets; + p_entries.resize(n_buckets); + p_container = &c; + + Index i; + + i=c.first_index(); + p_entries[0].t = 0.0; + p_entries[0].i = i; + + for(; c.valid_index(i); i=c.next_index(i)) + { + float t = c.time_of(i); + int b = (int)(t/p_time_step); + if (b>=p_entries.num_columns()) + b = p_entries.num_columns()-1; + for (int bb=b+1; bb < n_buckets ; bb++) + if ( t > p_entries(bb).t ) + { + p_entries[bb].t = t; + p_entries[bb].i = i; + } + else + break; + } +} + +template +void EST_TTimeIndex::just_before(float t, + void *inp) const +{ + CONTAINER::Index &in(*(Index *)inp); + in= CONTAINER::bad_index(); + + if (p_container==NULL) + return; + + int b = (int)(t/p_time_step); + + if (b>=p_entries.num_columns()) + b = p_entries.num_columns()-1; + + Index i = p_entries(b).i; + + for(Index j=i; p_container->valid_index(j); j = p_container->next_index(j)) + { + if (p_container->time_of(j) > t) + { + in=i; + return; + } + i=j; + } + in =i; + return; +} + +template +int operator !=(const EST_TTI_Entry &e1, + const EST_TTI_Entry &e2) +{ (void)e1; (void)e2; return 1; } + +template +ostream& operator <<(ostream &s, + const EST_TTI_Entry &e) +{ (void)e; return s << "entry"; } diff --git a/aeneas/cfw/speech_tools/base_class/EST_TVector.cc b/aeneas/cfw/speech_tools/base_class/EST_TVector.cc new file mode 100644 index 00000000..7aec059b --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_TVector.cc @@ -0,0 +1,324 @@ + /*************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1995,1996 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author : Paul Taylor */ + /* Date : April 1995 */ + /* --------------------------------------------------------------------- */ + /* Template Vector Class */ + /* */ + /*************************************************************************/ + + +#include +#include +#include "EST_TVector.h" +#include "EST_matrix_support.h" +#include "EST_cutils.h" +#include "EST_error.h" + +template +void EST_TVector::default_vals() +{ + p_num_columns = 0; + p_offset=0; + p_column_step=0; + + p_memory = NULL; + p_sub_matrix=FALSE; +} + +template +EST_TVector::EST_TVector() +{ + default_vals(); +} + +template +EST_TVector::EST_TVector(int n) +{ + default_vals(); + resize(n); +} + +template +EST_TVector::EST_TVector(const EST_TVector &in) +{ + default_vals(); + copy(in); +} + +template +EST_TVector::EST_TVector(int n, + T *memory, int offset, int free_when_destroyed) +{ + default_vals(); + + set_memory(memory, offset, n, free_when_destroyed); +} + +template +EST_TVector::~EST_TVector() +{ + p_num_columns = 0; + p_offset=0; + p_column_step=0; + + if (p_memory != NULL && !p_sub_matrix) + { + delete [] (p_memory-p_offset); + p_memory = NULL; + } +} + + +template +void EST_TVector::fill(const T &v) +{ + for (int i = 0; i < num_columns(); ++i) + fast_a_v(i) = v; +} + +template +void EST_TVector::set_memory(T *buffer, int offset, int columns, + int free_when_destroyed) +{ + if (p_memory != NULL && !p_sub_matrix) + delete [] (p_memory-p_offset); + + p_memory = buffer-offset; + p_offset=offset; + p_num_columns = columns; + p_column_step=1; + p_sub_matrix = !free_when_destroyed; +} + +template +void EST_TVector::set_values(const T *data, + int step, + int start_c, + int num_c) +{ + for(int i=0, c=start_c, p=0; i +void EST_TVector::get_values(T *data, + int step, + int start_c, + int num_c) const +{ + for(int i=0, c=start_c, p=0; i +void EST_TVector::copy_data(const EST_TVector &a) +{ + set_values(a.p_memory, a.p_column_step, 0, num_columns()); +} + +template +void EST_TVector::copy(const EST_TVector &a) +{ + resize(a.n(), FALSE); + copy_data(a); +} + +template +void EST_TVector::just_resize(int new_cols, T** old_vals) +{ + T *new_m; + + if (num_columns() != new_cols || p_memory == NULL ) + { + if (p_sub_matrix) + EST_error("Attempt to resize Sub-Vector"); + + if (new_cols < 0) + EST_error("Attempt to resize vector to negative size: %d", + new_cols); + + new_m = new T[new_cols]; + + if (p_memory != NULL) + { + if (old_vals != NULL) + *old_vals = p_memory; + else if (!p_sub_matrix) + delete [] (p_memory-p_offset); + } + + p_memory = new_m; + //cout << "vr: mem: " << p_memory << " (" << (int)p_memory << ")\n"; + p_offset=0; + p_num_columns = new_cols; + p_column_step=1; + } + else + *old_vals = p_memory; +} + + +template +void EST_TVector::resize(int new_cols, int set) +{ + int i; + T * old_vals = p_memory; + int old_cols = num_columns(); + int old_offset = p_offset; + int old_column_step = p_column_step; + + just_resize(new_cols, &old_vals); + + if (set) + { + int copy_c = 0; + + if (!old_vals) + copy_c=0; + else if (old_vals != p_memory) + { + copy_c = Lof(num_columns(), old_cols); + + for(i=0; i +EST_TVector &EST_TVector::operator=(const EST_TVector &in) +{ + copy(in); + return *this; +} + +template +T &EST_TVector::a_check(int n) +{ + if (!EST_vector_bounds_check(n, num_columns(), FALSE)) + return *error_return; + + return fast_a_v(n); +} + +template +const T &EST_TVector::a_check(int n) const +{ + return ((EST_TVector *)this)->a(n); +} + +template +int EST_TVector::operator == (const EST_TVector &v) const +{ + if (num_columns() != v.num_columns()) + return 0; + + for(int i=0; i +void EST_TVector::copy_section(T* dest, int offset, int num) const +{ + if (num<0) + num = num_columns()-offset; + + if (!EST_vector_bounds_check(num+offset-1, num_columns(), FALSE)) + return; + + + for(int i=0; i +void EST_TVector::set_section(const T* src, int offset, int num) +{ + if (num<0) + num = num_columns()-offset; + + if (!EST_vector_bounds_check(num+offset-1, num_columns(), FALSE)) + return; + + for(int i=0; i +void EST_TVector::sub_vector(EST_TVector &sv, + int start_c, int len) +{ + if (len < 0) + len = num_columns()-start_c; + + if (sv.p_memory != NULL && ! sv.p_sub_matrix) + delete [] (sv.p_memory - sv.p_offset); + + sv.p_sub_matrix = TRUE; + sv.p_offset = p_offset + start_c*p_column_step; + sv.p_memory = p_memory - p_offset + sv.p_offset; + sv.p_column_step=p_column_step; + sv.p_num_columns = len; +} + +template +void EST_TVector::integrity() const +{ + cout << "integrity: p_memory=" << p_memory << endl; + if(p_memory == (T *)0x00080102) + { + cout << "fatal value!!!\n"; + } +} + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_Token.cc b/aeneas/cfw/speech_tools/base_class/EST_Token.cc new file mode 100644 index 00000000..533467d3 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Token.cc @@ -0,0 +1,871 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : April 1996 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* A Tokenize class, both for Tokens (Strings plus alpha) */ +/* EST_TokenStream for strings, FILE *, files, pipes etc */ +/* */ +/*=======================================================================*/ +#include +#include +#include "EST_unix.h" +#include +#include +#include +#include "EST_math.h" +#include "EST_Token.h" +#include "EST_string_aux.h" +#include "EST_cutils.h" +#include "EST_error.h" + +const EST_String EST_Token_Default_WhiteSpaceChars = " \t\n\r"; +const EST_String EST_Token_Default_SingleCharSymbols = "(){}[]"; +const EST_String EST_Token_Default_PrePunctuationSymbols = "\"'`({["; +const EST_String EST_Token_Default_PunctuationSymbols = "\"'`.,:;!?]})"; +const EST_String Token_Origin_FD = "existing file descriptor"; +const EST_String Token_Origin_Stream = "existing istream"; +const EST_String Token_Origin_String = "existing string"; + +static EST_Regex RXanywhitespace("[ \t\n\r]"); + +static inline char *check_extend_str_in(char *str, int pos, int *max) +{ + // Check we are not at the end of the string, if so get some more + // and copy the old one into the new one + char *newstuff; + + if (pos >= *max) + { + if (pos > *max) + *max = 2 * pos; + else + *max *= 2; + newstuff = new char[*max]; + strncpy(newstuff,str,pos); + delete [] str; + return newstuff; + } + else + return str; +} + +#define check_extend_str(STR, POS, MAX) \ + (((POS)>= *(MAX))?check_extend_str_in((STR),(POS),(MAX)):(STR)) + +ostream& operator<<(ostream& s, const EST_Token &p) +{ + s << "[TOKEN " << p.pname << "]"; + return s; +} + + +EST_Token &EST_Token::operator = (const EST_Token &a) +{ + linenum = a.linenum; + linepos = a.linepos; + p_filepos = a.p_filepos; + p_quoted = a.p_quoted; + space = a.space; + prepunc = a.prepunc; + pname = a.pname; + punc = a.punc; + return *this; +} + +const EST_String EST_Token::pos_description() const +{ + return "line "+itoString(linenum)+" char "+itoString(linepos); +} + +EST_Token &EST_Token::operator = (const EST_String &a) +{ + pname = a; + return *this; +} + +EST_TokenStream::EST_TokenStream() +{ + tok_wspacelen = 64; // will grow if necessary + tok_wspace = new char[tok_wspacelen]; + tok_stufflen = 512; // will grow if necessary + tok_stuff = new char[tok_stufflen]; + tok_prepuncslen = 32; // will grow if necessary + tok_prepuncs = new char[tok_prepuncslen]; + + default_values(); +} + +EST_TokenStream::EST_TokenStream(EST_TokenStream &s) +{ + (void)s; + + cerr << "TokenStream: warning passing TokenStream not as reference" + << endl; + + // You *really* shouldn't use this AT ALL unless you + // fully understand its consequences, you'll be copying open + // files and moving file pointers all over the place + // basically *DON'T* do this, pass the stream by reference + + // Now there may be occasions when you do want to do this for example + // when you need to do far look ahead or check point as you read + // but they are obscure and I'm not sure how to do that for all + // the file forms supported by the TokenStream. If you do + // I can write a clone function that might do it. + +} + +void EST_TokenStream::default_values() +{ + type = tst_none; + peeked_tokp = FALSE; + peeked_charp = FALSE; + eof_flag = FALSE; + quotes = FALSE; + p_filepos = 0; + linepos = 1; + WhiteSpaceChars = EST_Token_Default_WhiteSpaceChars; + SingleCharSymbols = EST_String::Empty; + PrePunctuationSymbols = EST_String::Empty; + PunctuationSymbols = EST_String::Empty; + build_table(); + close_at_end=TRUE; +} + +EST_TokenStream::~EST_TokenStream() +{ + if (type != tst_none) + close(); + delete [] tok_wspace; + delete [] tok_stuff; + delete [] tok_prepuncs; + +} + +ostream& operator<<(ostream& s, EST_TokenStream &p) +{ + s << "[TOKENSTREAM "; + switch (p.type) + { + case tst_none: + cerr << "UNSET"; break; + case tst_file: + cerr << "FILE"; break; + case tst_pipe: + cerr << "PIPE"; break; + case tst_istream: + cerr << "ISTREAM"; break; + case tst_string: + cerr << "STRING"; break; + default: + cerr << "UNKNOWN" << endl; + } + s << "]"; + + return s; +} + +int EST_TokenStream::open(const EST_String &filename) +{ + if (type != tst_none) + close(); + default_values(); + fp = fopen(filename,"rb"); + if (fp == NULL) + { + cerr << "Cannot open file " << filename << " as tokenstream" + << endl; + return -1; + } + Origin = filename; + type = tst_file; + + return 0; +} + +int EST_TokenStream::open(FILE *ofp, int close_when_finished) +{ + // absorb already open stream + if (type != tst_none) + close(); + default_values(); + fp = ofp; + if (fp == NULL) + { + cerr << "Cannot absorb NULL filestream as tokenstream" << endl; + return -1; + } + Origin = Token_Origin_FD; + type = tst_file; + + close_at_end = close_when_finished; + + return 0; +} + +int EST_TokenStream::open(istream &newis) +{ + // absorb already open istream + if (type != tst_none) + close(); + default_values(); + is = &newis; + Origin = Token_Origin_Stream; + type = tst_istream; + + return 0; +} + +int EST_TokenStream::open_string(const EST_String &newbuffer) +{ + // Make a tokenstream from an internal existing string/buffer + const char *buf; + if (type != tst_none) + close(); + default_values(); + buf = (const char *)newbuffer; + buffer_length = newbuffer.length(); + buffer = new char[buffer_length+1]; + memmove(buffer,buf,buffer_length+1); + pos = 0; + Origin = Token_Origin_String; + type = tst_string; + + return 0; +} + +int EST_TokenStream::seek_end() +{ + // This isn't actually useful but people expect it + peeked_charp = FALSE; + peeked_tokp = FALSE; + + switch (type) + { + case tst_none: + cerr << "EST_TokenStream unset" << endl; + return -1; + break; + case tst_file: + fseek(fp,0,SEEK_END); + p_filepos = ftell(fp); + return p_filepos; + case tst_pipe: + cerr << "EST_TokenStream seek on pipe not supported" << endl; + return -1; + break; + case tst_istream: + is->seekg(0,is->end); + p_filepos = is->tellg(); + return p_filepos; + break; + case tst_string: + pos = buffer_length; + return pos; + default: + cerr << "EST_TokenStream: unknown type" << endl; + return -1; + } + + return -1; // can't get here +} + +int EST_TokenStream::seek(int position) +{ + peeked_charp = FALSE; + peeked_tokp = FALSE; + + switch (type) + { + case tst_none: + cerr << "EST_TokenStream unset" << endl; + return -1; + break; + case tst_file: + p_filepos = position; + return fseek(fp,position,SEEK_SET); + case tst_pipe: + cerr << "EST_TokenStream seek on pipe not supported" << endl; + return -1; + break; + case tst_istream: + p_filepos = position; + is->seekg(position, is->beg); + return 0; + break; + case tst_string: + if (position >= pos) + { + pos = position; + return -1; + } + else + { + pos = position; + return 0; + } + break; + default: + cerr << "EST_TokenStream: unknown type" << endl; + return -1; + } + + return -1; // can't get here + +} + +static int stdio_fread(void *buff,int size,int nitems,FILE *fp) +{ + // So it can find the stdio one rather than the TokenStream one + return fread(buff,size,nitems,fp); +} + +int EST_TokenStream::fread(void *buff, int size, int nitems) +{ + // switching into binary mode for current position + int items_read; + + // so we can continue to read afterwards + if (peeked_tokp) + { + cerr << "ERROR " << pos_description() + << " peeked into binary data" << endl; + return 0; + } + + peeked_charp = FALSE; + peeked_tokp = FALSE; + + switch (type) + { + case tst_none: + cerr << "EST_TokenStream unset" << endl; + return 0; + break; + case tst_file: + items_read = stdio_fread(buff,(size_t)size,(size_t)nitems,fp); + p_filepos += items_read*size; + return items_read; + case tst_pipe: + cerr << "EST_TokenStream fread pipe not yet supported" << endl; + return 0; + break; + case tst_istream: + is->read((char*)buff, (size_t) size*nitems); + return is->gcount()/size; + break; + case tst_string: + if ((buffer_length-pos)/size < nitems) + items_read = (buffer_length-pos)/size; + else + items_read = nitems; + memcpy(buff,&buffer[pos],items_read*size); + pos += items_read*size; + return items_read; + default: + cerr << "EST_TokenStream: unknown type" << endl; + return EOF; + } + + return 0; // can't get here + +} + +void EST_TokenStream::close(void) +{ + // close any files (if they were used) + + switch (type) + { + case tst_none: + break; + case tst_file: + if (close_at_end) + fclose(fp); + case tst_pipe: + // close(fd); + break; + case tst_istream: + break; + case tst_string: + delete [] buffer; + buffer = 0; + break; + default: + cerr << "EST_TokenStream: unknown type" << endl; + break; + } + + type = tst_none; + peeked_charp = FALSE; + peeked_tokp = FALSE; + +} + +int EST_TokenStream::restart(void) +{ + // For paul, the only person I know who uses this + + switch (type) + { + case tst_none: + break; + case tst_file: + fp = freopen(Origin,"rb",fp); + p_filepos = 0; + break; + case tst_pipe: + cerr << "EST_TokenStream: can't rewind pipe" << endl; + return -1; + break; + case tst_istream: + cerr << "EST_TokenStream: can't rewind istream" << endl; + break; + case tst_string: + pos = 0; + break; + default: + cerr << "EST_TokenStream: unknown type" << endl; + break; + } + + linepos = 1; + peeked_charp = FALSE; + peeked_tokp = FALSE; + eof_flag = FALSE; + + return 0; +} + +EST_TokenStream & EST_TokenStream::operator >>(EST_Token &p) +{ + return get(p); +} + +EST_TokenStream & EST_TokenStream::operator >>(EST_String &p) +{ + EST_Token t; + + get(t); + p = t.string(); + return *this; +} + +EST_TokenStream &EST_TokenStream::get(EST_Token &tok) +{ + tok = get(); + return *this; +} + +EST_Token EST_TokenStream::get_upto(const EST_String &s) +{ + // Returns a concatenated token form here to next symbol that matches s + // including s (though not adding s on the result) + // Not really for the purist but lots of times very handy + // Note this is not very efficient + EST_String result; + EST_Token t; + + for (result=EST_String::Empty; (t=get()) != s; ) + { + result += t.whitespace() + t.prepunctuation() + + t.string() + t.punctuation(); + if (eof()) + { + cerr << "EST_TokenStream: end of file when looking for \"" << + s << "\"" << endl; + break; + } + } + + return EST_Token(result); +} + +EST_Token EST_TokenStream::get_upto_eoln(void) +{ + // Swallow the lot up to end of line + // assumes \n is a whitespace character + + EST_String result(EST_String::Empty); + + while (!eoln()) + { + EST_Token &t=get(); + result += t.whitespace() + t.prepunctuation(); + + if (quotes) + result += quote_string(t.string()); + else + result += t.string(); + + result += t.punctuation(); + + if (eof()) + { +// cerr << "EST_TokenStream: end of file when looking for end of line" +// << endl; + break; + } + } + // So that the next call works I have to step over the eoln condition + // That involves removing the whitespace upto and including the next + // \n in the peek token. + + char *w = wstrdup(peek().whitespace()); + int i; + for (i=0; w[i] != 0; i++) + if (w[i] == '\n') // maybe not portable + peek().set_whitespace(&w[i+1]); + + wfree(w); + + static EST_Token result_t; + + result_t.set_token(result); + + return result_t; +} + +EST_Token &EST_TokenStream::must_get(EST_String expected, bool *ok) +{ + EST_Token &tok = get(); + + if (tok != expected) + { + if (ok != NULL) + { + *ok=FALSE; + return tok; + } + else + EST_error("Expected '%s' got '%s' at %s", + (const char *)expected, + (const char *)(EST_String)tok, + (const char *)pos_description()); + } + + if (ok != NULL) + *ok=TRUE; + return tok; +} + +void EST_TokenStream::build_table() +{ + int i; + const char *p; + unsigned char c; + + for (i=0; i<256; ++i) + p_table[i]=0; + + for (p=WhiteSpaceChars; *p; ++p) + if (p_table[c=(unsigned char)*p]) + EST_warning("Character '%c' has two classes, '%c' and '%c'", + *p, c, ' '); + else + p_table[c] = ' '; + + for (p=SingleCharSymbols; *p; ++p) + if (p_table[c=(unsigned char)*p]) + EST_warning("Character '%c' has two classes, '%c' and '%c'", + *p, p_table[c], '!'); + else + p_table[c] = '@'; + + for (p=PunctuationSymbols; *p; ++p) + if (p_table[c=(unsigned char)*p] == '@') + continue; + else if (p_table[c]) + EST_warning("Character '%c' has two classes, '%c' and '%c'", + *p, p_table[c], '.'); + else + p_table[c] = '.'; + + for(p=PrePunctuationSymbols; *p; ++p) + if (p_table[c=(unsigned char)*p] == '@') + continue; + else if (p_table[c] == '.') + p_table[c] = '"'; + else if (p_table[c]) + EST_warning("Character '%c' has two classes, '%c' and '%c'", + *p, p_table[c], '$'); + else + p_table[c] = '$'; + + p_table_wrong=0; +} + +inline int EST_TokenStream::getpeeked_internal(void) +{ + peeked_charp = FALSE; + return peeked_char; +} + +inline +int EST_TokenStream::getch_internal() +{ + // Return next character in stream + if (EST_TokenStream::peeked_charp) + { + return getpeeked_internal(); + } + + switch (type) + { + case tst_none: + cerr << "EST_TokenStream unset" << endl; + return EOF; + break; + case tst_file: + p_filepos++; + { + char lc; + if (stdio_fread(&lc,1,1,fp) == 0) + return EOF; + else + return (int)lc; + } +/* return getc(fp); */ + case tst_pipe: + cerr << "EST_TokenStream pipe not yet supported" << endl; + return EOF; + break; + case tst_istream: + p_filepos++; + return is->get(); + case tst_string: + if (pos < buffer_length) + { + p_filepos++; + return buffer[pos++]; + } + else + return EOF; + default: + cerr << "EST_TokenStream: unknown type" << endl; + return EOF; + } + + return EOF; // can't get here +} + +int EST_TokenStream::getch(void) +{ + return getch_internal(); +} + +inline int EST_TokenStream::peekch_internal() +{ + // Return next character in stream (without reading it) + + if (!peeked_charp) + peeked_char = getch_internal(); + peeked_charp = TRUE; + return peeked_char; +} + + +int EST_TokenStream::peekch(void) +{ + return peekch_internal(); + +} + +#define CLASS(C,CL) (p_table[(unsigned char)(C)]==(CL)) + +#define CLASS2(C,CL1,CL2) (p_table[(unsigned char)(C)]==(CL1)||p_table[(unsigned char)(C)]==(CL2)) + +EST_Token &EST_TokenStream::get(void) +{ + if (peeked_tokp) + { + peeked_tokp = FALSE; + return current_tok; + } + + if (p_table_wrong) + build_table(); + + char *word; + int c,i,j; + + for (i=0; (CLASS(c=getch_internal(),' ') && + ( c != EOF )); i++) + { + if (c == '\n') linepos++; + tok_wspace = check_extend_str(tok_wspace,i,&tok_wspacelen); + tok_wspace[i] = c; + } + tok_wspace[i] = '\0'; + + current_tok.init(); + + if (c != EOF) + { + current_tok.set_filepos(p_filepos-1); + + if ((quotes) && // quoted strings (with escapes) are allowed + (c == quote)) + { + for (i=0; + ((c = getch_internal()) != EOF) + ;) + { + if (c == quote) + break; + tok_stuff = check_extend_str(tok_stuff,i,&tok_stufflen); + if (c == escape) + c = getch_internal(); + tok_stuff[i++] = c; + } + current_tok.set_quoted(TRUE); + } + else // standard whitespace separated tokens + { + for (i=0,tok_stuff[i++]=c; + ( + !CLASS(c,'@') && + !CLASS(c=peekch_internal(),' ') && + !CLASS(c,'@') && + ( c != EOF )) ;) + { + tok_stuff = check_extend_str(tok_stuff,i,&tok_stufflen); + // note, we must have peeked to get here. + tok_stuff[i++] = getpeeked_internal(); + } + } + tok_stuff[i] = '\0'; + // Are there any punctuation symbols at the start? + for (j=0; + ((j < i) && CLASS2(tok_stuff[j], '$', '"')); + j++); + if ((j > 0) && (j < i)) // there are + { + tok_prepuncs = check_extend_str(tok_prepuncs,j+1,&tok_prepuncslen); + memmove(tok_prepuncs,tok_stuff,j); + tok_prepuncs[j] = '\0'; + current_tok.set_prepunctuation(tok_prepuncs); + word=&tok_stuff[j]; + i-=j; // reduce size by number of prepuncs + } + else + { + current_tok.set_prepunctuation(EST_String::Empty); + word = tok_stuff; + } + // Are there any punctuation symbols at the end + for (j=i-1; + ((j > 0) && CLASS2(word[j],'.','"')); + j--); + if (word[j+1] != '\0') + { + current_tok.set_punctuation(&word[j+1]); + word[j+1] = '\0'; + } + else + current_tok.set_punctuation(EST_String::Empty); + + current_tok.set_token(word); + if (tok_wspace[0] == '\0') // feature paths will have null whitespace + current_tok.set_whitespace(EST_String::Empty); + else + current_tok.set_whitespace(tok_wspace); + } + else + { + current_tok.set_token(EST_String::Empty); + current_tok.set_whitespace(tok_wspace); + current_tok.set_punctuation(EST_String::Empty); + current_tok.set_prepunctuation(EST_String::Empty); + eof_flag = TRUE; + } + + return current_tok; +} + +int EST_TokenStream::eoln(void) +{ + // This doesn't really work if there are blank lines (and you want + // to know about them) + + if ((peek().whitespace().contains("\n")) || eof()) + return TRUE; + else + return FALSE; + +} + +EST_String quote_string(const EST_String &s, + const EST_String "e, + const EST_String &escape, + int force) +{ + // Quotes s always if force true, or iff s contains whitespace, + // quotes or escapes force is false + // Note quote and escape are assumed to be string of length 1 + EST_String quoted_form; + if ((force) || + (s.contains(quote)) || + (s.contains(escape)) || + (s.contains(RXanywhitespace)) || + (s.length() == 0)) + { + // bigger than the quoted form could ever be + int i,j; + char *quoted = new char[s.length()*(quote.length()+escape.length())+ + 1+quote.length()+quote.length()]; + quoted[0] = quote(0); + for (i=1,j=0; j < s.length(); j++,i++) + { + if (s(j) == quote(0)) + quoted[i++] = escape(0); + else if (s(j) == escape(0)) + quoted[i++] = escape(0); + quoted[i] = s(j); + } + quoted[i++] = quote(0); + quoted[i] = '\0'; + quoted_form = quoted; + delete [] quoted; + return quoted_form; + } + else + return s; +} + +const EST_String EST_TokenStream::pos_description() +{ + return Origin+":"+itoString(linepos); +} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Tvectlist.cc b/aeneas/cfw/speech_tools/base_class/EST_Tvectlist.cc new file mode 100644 index 00000000..7c11d775 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Tvectlist.cc @@ -0,0 +1,67 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* Vector/List conversion templates */ +/* only include this if you define both vectors and lists for the class */ +/* */ +/*=======================================================================*/ +#include +#include "EST_TList.h" +#include "EST_TVector.h" +#include "EST_cutils.h" + +template +EST_TVector &set(EST_TVector &v, const EST_TList &in) +{ + v.resize(in.length(), FALSE); + EST_Litem *p; + int i = 0; + for (p = in.head(); p!= 0; p = p->next(), ++i) + v.a_no_check(i) = in.item(p); + + return v; +} + +template EST_TVector ©(EST_TVector a,const EST_TList &in) +{ + a.resize(in.length(), FALSE); + EST_Litem *p; + int i = 0; + for (p = in.head(); p!= 0; p = p->next(), ++i) + a[i] = in.item(p); + + return a; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_UList.cc b/aeneas/cfw/speech_tools/base_class/EST_UList.cc new file mode 100644 index 00000000..461086f4 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_UList.cc @@ -0,0 +1,461 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1997,1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* */ +/* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ +/* Date: Mon Jul 21 1997 */ +/* -------------------------------------------------------------------- */ +/* Untyped list used as the basis of the TList class */ +/* */ +/*************************************************************************/ +#include + +void EST_UList::clear_and_free(void (*item_free)(EST_UItem *p)) +{ + EST_UItem *q, *np; + + for (q=head(); q != 0; q = np) + { + np=q->next(); + if (item_free) + item_free(q); + else + delete q; + } + h = t = 0; +} + +int EST_UList::length() const +{ + EST_UItem *ptr; + int n = 0; + + for (ptr = head(); ptr != 0; ptr = ptr->next()) + ++n; + return n; +} + +int EST_UList::index(EST_UItem *item) const +{ + EST_UItem *ptr; + int n = 0; + + for (ptr = head(); ptr != 0; ptr = ptr->next(), ++n) + if (item == ptr) + return n; + + return -1; +} + +EST_UItem *EST_UList::nth_pointer(int n) const +{ + EST_UItem *ptr; + int i; + + for (i = 0, ptr = head(); ptr != 0; ptr = ptr->next(), ++i) + if (i == n) + return ptr; + + cerr << "Requested item #" << n << " off end of list" << endl; + return head(); +} + +EST_UItem * EST_UList::remove(EST_UItem *item, + void (*free_item)(EST_UItem *item)) +{ + if (item == 0) + return 0; + + EST_UItem *prev = item->p; + if (item->p == 0) // at start + h = item->n; + else + item->p->n = item->n; + if (item->n == 0) // at end + t = item->p; + else + item->n->p = item->p; + + if (free_item) + free_item(item); + else + delete item; + + return prev; +} + +EST_UItem * EST_UList::remove(int n, + void (*item_free)(EST_UItem *item)) +{ + return remove(nth_pointer(n), item_free); +} + +// This should check if the incoming prev_item actually is in the list + +EST_UItem *EST_UList::insert_after(EST_UItem *prev_item, EST_UItem *new_item) +{ + if (new_item == 0) + return new_item; + if (prev_item == 0) // insert it at start of list + { + new_item->n = h; + h = new_item; + } + else + { + new_item->n = prev_item->n; + prev_item->n = new_item; + } + new_item->p = prev_item; + if (new_item->n == 0) + t = new_item; + else + new_item->n->p = new_item; + + return new_item; +} + +EST_UItem *EST_UList::insert_before(EST_UItem *next_item, EST_UItem *new_item) +{ + if (new_item == 0) + return new_item; + if (next_item == 0) // put it on the end of the list + { + new_item->p = t; + t = new_item; + } + else + { + new_item->p = next_item->p; + next_item->p = new_item; + } + new_item->n = next_item; + if (new_item->p == 0) + h = new_item; + else + new_item->p->n = new_item; + + return next_item; +} + +void EST_UList::exchange(EST_UItem *a, EST_UItem *b) +{ + + if (a==b) + return; + + if ((a==0) || (b==0)) + { + cerr << "EST_UList:exchange: can't exchange NULL items" << endl; + return; + } + + // I know this isn't very readable but there are eight pointers + // that need to be changed, and half of them are trivial back pointers + // care need only be taken when b and a are adjacent, this actual + // sets p and n twice if they are adjacent but still gets the right answer + EST_UItem *ap=a->p,*an=a->n,*bn=b->n,*bp=b->p; + + a->n = bn == a ? b : bn; + if (a->n) + a->n->p = a; + a->p = bp == a ? b : bp; + if (a->p) + a->p->n = a; + + b->n = an == b ? a : an; + if (b->n) + b->n->p = b; + b->p = ap == b ? a : ap; + if (b->p) + b->p->n = b; + + // Fix t and h + if (a == h) + h = b; + else if (b == h) + h = a; + else if (a == t) + t = b; + else if (b == t) + t = a; + +} + +void EST_UList::exchange(int i, int j) +{ + + EST_UItem *p; + EST_UItem *a=0,*b=0; + int k; + + for (k=0,p = head(); p != 0; p = p->next(),k++) + { + if(i==k) + a = p; + if(j==k) + b = p; + } + + if ((a==0) || (b==0)) + { + cerr << "EST_UList:exchange: can't exchange items " << i << + " and " << j << " (off end of list)" << endl; + return; + } + + exchange(a,b); +} + +void EST_UList::reverse() +{ + EST_UItem *p,*q; + + for (p=head(); p != 0; p=q) + { + q = p->n; + p->n = p->p; + p->p = q; + } + q = h; + h = t; + t = q; +} + +void EST_UList::append(EST_UItem *new_item) +{ + + if (new_item == 0) return; + + new_item->n = 0; + new_item->p = t; + if (t == 0) + h = new_item; + else + t->n = new_item; + t = new_item; +} + +void EST_UList::prepend(EST_UItem *new_item) +{ + if (new_item == 0) return; + + new_item->p = 0; + new_item->n = h; + if (h == 0) + t = new_item; + else + h->p = new_item; + h = new_item; +} + +bool EST_UList::operator_eq(const EST_UList &a, + const EST_UList &b, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2)) +{ + EST_UItem *p,*q; + q=b.head(); + for (p = a.head(); p != NULL; p = p->next()){ + if(q == NULL) + return false; + if(eq(q, p)) + q=q->next(); + else + return false; + } + + if(q == NULL) + return true; + else + return false; +} + +int EST_UList::index(const EST_UList &l, + const EST_UItem &val, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2)) +{ + + EST_UItem *ptr; + int n = 0; + + for (ptr = l.head(); ptr != 0; ptr = ptr->next(), ++n) + if (eq(&val,ptr)) + return n; + + return -1; +} + +void EST_UList::sort(EST_UList &l, + bool (*gt)(const EST_UItem *item1, + const EST_UItem *item2)) +{ + + // just bubble sort for now + // use EST_String::operator > for comparisons + + EST_UItem *l_ptr,*m_ptr; + bool sorted=false; + + while(!sorted){ + sorted=true; + + for(l_ptr=l.head(); l_ptr != 0; l_ptr=l_ptr->next()){ + + m_ptr=l_ptr->next(); + if(m_ptr != 0) + if(gt(l_ptr, m_ptr)){ + l.exchange(l_ptr,m_ptr); + sorted=false; + } + } + } + +} + +// quicksort from 'Algorithms' +// by Cormen, Leiserson & Rivest + +static EST_UItem *partition(EST_UItem *p, EST_UItem *r, + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*exchange)(EST_UItem *item1, EST_UItem *item2)) +{ + // this can be tidied up / sped up + + EST_UItem *i,*j,*i2,*j2; + EST_UItem *x = p; + + i = p; + j = r; + + while(true){ + + while(gt(j, x) ) + j = j->prev(); + + while(gt(x, i)) + i = i->next(); + + if((i != j) && (i->prev() != j)){ + i2=i; + j2=j; + i=i->next(); + j=j->prev(); + exchange(i2,j2); + + }else + return j; + + } + return NULL; +} + + +static void qsort_sub(EST_UList &l, EST_UItem *p, EST_UItem *r, + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*exchange)(EST_UItem *item1, EST_UItem *item2)) +{ + EST_UItem *q; + if(p != r){ + q = partition(p,r, gt, exchange); + qsort_sub(l,p,q, gt, exchange); + qsort_sub(l,q->next(),r, gt, exchange); + } +} + +void EST_UList::qsort(EST_UList &l, + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*exchange)(EST_UItem *item1, EST_UItem *item2)) +{ + qsort_sub(l,l.head(),l.tail(), gt, exchange); +} + + +void EST_UList::sort_unique(EST_UList &l, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2), + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*item_free)(EST_UItem *item)) +{ + // as sort(..) but delete any repeated items + + EST_UItem *l_ptr,*m_ptr; + bool sorted=false; + + while(!sorted){ + sorted=true; + + for(l_ptr=l.head(); l_ptr != 0; l_ptr=l_ptr->next()){ + + m_ptr=l_ptr->next(); + if(m_ptr != 0) + { + if(gt(l_ptr, m_ptr)){ + l.exchange(l_ptr,m_ptr); + sorted=false; + } else if(eq(l_ptr, m_ptr)){ + l.remove(m_ptr, item_free); + sorted=false; + } + } + } + } +} + +void EST_UList::merge_sort_unique(EST_UList &l, EST_UList &m, + bool (*eq)(const EST_UItem *item1, const EST_UItem *item2), + bool (*gt)(const EST_UItem *item1, const EST_UItem *item2), + void (*item_free)(EST_UItem *item)) +{ + // keep all unique items in l, and add any new items from m to l + + EST_UItem *l_ptr,*m_ptr; + bool flag; + + // make sure + sort_unique(l, eq, gt, item_free); + + for(m_ptr=m.head(); m_ptr != 0; m_ptr=m_ptr->next()){ + + // try and put item from m in list + flag=false; + for(l_ptr=l.head(); l_ptr != 0; l_ptr=l_ptr->next()){ + if( gt(l_ptr, m_ptr) ){ + l.insert_before(l_ptr, m_ptr); + flag=true; + break; + }else if( eq(m_ptr, l_ptr) ){ + flag=true; + break; + } + } + // or try and append it + if(!flag && ( gt(m_ptr, l.tail()) ) ) + l.append(m_ptr); + } +} diff --git a/aeneas/cfw/speech_tools/base_class/EST_Val.cc b/aeneas/cfw/speech_tools/base_class/EST_Val.cc new file mode 100644 index 00000000..e015feb6 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_Val.cc @@ -0,0 +1,150 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : May 1996 */ +/*-----------------------------------------------------------------------*/ +/* Class to represent ints, floats and strings */ +/* and other arbitrary objects */ +/*=======================================================================*/ +#include +#include "EST_Val.h" +#include "EST_string_aux.h" + +val_type val_unset = "unset"; +val_type val_int = "int"; +val_type val_float = "float"; +val_type val_string = "string"; + +EST_Val::EST_Val(const EST_Val &c) +{ + if (c.t == val_string) + sval = c.sval; + else if (c.t == val_int) + v.ival = c.v.ival; + else if (c.t == val_float) + v.fval = c.v.fval; + else if (c.t != val_unset) + { // does references not a real copy + v.pval = new EST_Contents; + *v.pval = *c.v.pval; + } + t=c.t; +} + +EST_Val::EST_Val(val_type type,void *p, void (*f)(void *)) +{ + t=type; + v.pval = new EST_Contents; + v.pval->set_contents(p,f); +} + +EST_Val::~EST_Val(void) +{ + if ((t != val_int) && + (t != val_float) && + (t != val_unset) && + (t != val_string)) + delete v.pval; +} + +EST_Val &EST_Val::operator=(const EST_Val &c) +{ + // Have to be careful with the case where they are different types + if ((t != val_int) && + (t != val_float) && + (t != val_unset) && + (t != val_string)) + delete v.pval; + + if (c.t == val_string) + sval = c.sval; + else if (c.t == val_int) + v.ival = c.v.ival; + else if (c.t == val_float) + v.fval = c.v.fval; + else if (c.t != val_unset) + { // does references not a real copy + v.pval = new EST_Contents; + *v.pval = *c.v.pval; + } + t=c.t; + return *this; +} + +const int EST_Val::to_int(void) const +{ + // coerce this to an int + if (t==val_float) + return (int)v.fval; + else if (t==val_string) + return atoi(sval); + else + return v.ival; // just for completeness +} + +const float EST_Val::to_flt(void) const +{ + // coerce this to a float + if (t==val_int) + return (float)v.ival; + else if (t==val_string) + return atof(sval); + else + return v.fval; // just for completeness +} + +const EST_String &EST_Val::to_str(void) const +{ + // coerce this to and save it for later + // This requires the following casting, so we can still tell the + // compiler this is a const function. If this was properly declared + // non-const vast amounts of the rest of this would also have to be + // non-const. So we do one nasty bit here for uniformity elsewhere. + // Not saving the result is also a possibility but probably too + // inefficient (maybe not with rjc's string class) + EST_String *n = (EST_String *)((void *)&sval); + if (t==val_int) + *n = itoString(v.ival); + else if (t==val_float) + { + if (v.fval == 0) + *n = "0"; // to be compatible with other's notion of fstrings + else + *n = ftoString(v.fval); + } + else if (t != val_string) + *n = EST_String("[Val ")+t+"]"; + + return sval; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_features_aux.cc b/aeneas/cfw/speech_tools/base_class/EST_features_aux.cc new file mode 100644 index 00000000..276b542c --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_features_aux.cc @@ -0,0 +1,97 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* -------------------------------------------------------------------- */ + /* Auxiliary functions related to features. */ + /* */ + /*************************************************************************/ + +#include "EST_features_aux.h" +#include "EST_Features.h" +#include "EST_String.h" +#include "EST_error.h" + +#include "EST_get_function_template.h" + +defineGetFunction(EST_Features, val, EST_Val, getVal) +defineGetFunction(EST_Features, val, EST_String, getString) +defineGetFunction(EST_Features, val, float, getFloat) +defineGetFunction(EST_Features, val, int, getInteger) +VAL_REGISTER_FUNCPTR(pointer, void *) + + +void value_sort(EST_Features &f, const EST_String &field) +{ + int work_to_do = 1; + (void)field; + + EST_Features::RwEntries p; + EST_Features::RwEntries n; + EST_Features::Entry e; + + float p_score, n_score; + + while(work_to_do) + { + work_to_do = 0; + for (p.begin(f); p; ++p) + { + n = p; + n++; + if (n == 0) + break; + + if (p->v.type() != val_type_feats) + { + cerr << "Not a features in val\n"; + break; + } + p_score = feats(p->v)->F(field, 1.0); + n_score = feats(n->v)->F(field, 1.0); + if (n_score > p_score) + { + cout << "swapping\n"; + e = *p; + *p = *n; + *n = e; + work_to_do = 1; + } + } + } +} + + + + + diff --git a/aeneas/cfw/speech_tools/base_class/EST_features_io.cc b/aeneas/cfw/speech_tools/base_class/EST_features_io.cc new file mode 100644 index 00000000..c51a3a30 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_features_io.cc @@ -0,0 +1,313 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : March 1998 */ +/*-----------------------------------------------------------------------*/ +/* Features i/o */ +/* This is kept separate from EST_Features to help reduce dependencies */ +/*=======================================================================*/ +#include +#include "EST_Features.h" +#include "ling_class/EST_Item.h" +#include "EST_error.h" +#include "EST_String.h" +#include "EST_Token.h" + +void EST_Features::set_function(const EST_String &name, + const EST_String &funcname) +{ + EST_Item_featfunc f = get_featfunc(funcname,1); + + set_path(name, est_val(f)); +} + +void EST_Features::save_fpair(ostream &outf, + const EST_String &fname, + const EST_Val &fvalue) const +{ + /* Feature valued features themselves (so can't denot empty ones) */ + if (fvalue.type() == val_type_feats) + { + EST_Features *f = feats(fvalue); + if (f->features->list.head() == 0) + { + // An empty feature set + outf << fname << " () ; "; + } + else + for (EST_Litem *q=f->features->list.head(); + q != 0; q=q->next() ) + save_fpair(outf, + fname+"."+f->features->list(q).k, + f->features->list(q).v); + return; + } + /* a non feature valued one */ + // in case someone has () in their feature names (ought to be shot) + if (fname.contains("(") || + fname.contains(")") || + fname.contains(" ") || // bang, bang + fname.contains("\t") || // what smoking gun ? + fname.contains(";") || + (fname == "")) + outf << quote_string(fname,"\"","\\",1) << " "; + else + outf << fname << " "; + if (fvalue == ";") + outf << "\";\""; + else if ((fvalue.type() == val_string) && + ((fvalue.string().matches(RXint)) || + (fvalue.string().matches(RXdouble)) || + (fvalue.string().contains("(")) || + (fvalue.string().contains(")")) || + (fvalue.string().contains(";")) )) + // force quoting, cause it looks like a number but isn't + outf << quote_string(fvalue.string(),"\"","\\",1); + else if (fvalue.type() == val_float) + { + char b[20]; + sprintf(b,"%g",fvalue.Float()); + outf << b; + } + else if (fvalue.type() == val_type_featfunc) + { + outf << "F:"<list.head() == 0) + outf << "()"; + else + for (EST_Litem *p=features->list.head(); p != 0; p=p->next() ) + save_fpair(outf, + features->list(p).k, + features->list(p).v); + + return write_ok; +} + +EST_write_status EST_Features::save_sexpr(ostream &outf) const +{ + // Save features as an sexpression + outf << "("; + for (EST_Litem *p=features->list.head(); p != 0; p=p->next() ) + { + const EST_String &fname = features->list(p).k; + const EST_Val &fvalue = features->list(p).v; + outf << "("; + // in case someone has () in their feature names (ought to be shot) + if (fname.contains("(") || + fname.contains(")") || + fname.contains(" ") || + fname.contains("\t") || + fname.contains(";")) + outf << quote_string(fname,"\"","\\",1); + else + outf << fname; + outf << " "; + if (fvalue == ";") + outf << "\";\""; + else if ((fvalue.type() == val_string) && + ((fvalue.string().matches(RXint)) || + (fvalue.string().matches(RXdouble)) || + (fvalue.string().contains("(")) || + (fvalue.string().contains(")")))) + // force quoting, cause it looks like a number but isn't + // or contains a paren + outf << quote_string(fvalue.string(),"\"","\\",1); + else if (fvalue.type() == val_float) + { + char b[20]; + sprintf(b,"%g",fvalue.Float()); + outf << b; + } + else if (fvalue.type() == val_type_featfunc) + { + outf << "F:"<save_sexpr(outf); + } + else + outf << quote_string(fvalue.string()); + outf << ")"; + if (p->next()) + outf << " "; + } + outf << ")"; + + return write_ok; +} + +EST_read_status EST_Features::load_sexpr(EST_TokenStream &ts) +{ + /* Load in feature structure from sexpression */ + + if (ts.peek() != "(") + { + cerr << "load_features: no sexpression found\n"; + return misc_read_error; + } + else + { + EST_String f; + EST_Token v; + ts.get(); /* skip opening paren */ + for (; ts.peek() != ")"; ) + { + if (ts.peek() != "(") + { + cerr << "load_features: no sexpression found\n"; + return misc_read_error; + } + ts.get(); + f = ts.get().string(); /* feature name */ + if ((ts.peek() == "(") && (ts.peek().quoted() == FALSE)) + { + EST_Features fv; + set(f,fv); + A(f).load_sexpr(ts); + } + else + { + v = ts.get(); + if (v.quoted()) + set(f,v.string()); + else if (v.string().matches(RXint)) + set(f,atoi(v.string())); + else if (v.string().matches(RXdouble)) + set(f,atof(v.string())); + else if (v.string().contains("F:")) + { + EST_Item_featfunc func = + get_featfunc(v.string().after("F:")); + if (func != NULL) + set_val(f,est_val(func)); + else + { + cerr << "load_features: Unknown Function '" << f <<"'\n"; + set_val(f,feature_default_value); + } + } + else + set(f,v.string()); + + } + if (ts.get() != ")") + { + cerr << "load_features: no sexpression found\n"; + return misc_read_error; + } + } + if (ts.get() != ")") + { + cerr << "load_features: no sexpression found\n"; + return misc_read_error; + } + } + return format_ok; +} + +EST_read_status EST_Features::load(EST_TokenStream &ts) +{ + // load features from here to end of line separated by semicolons + EST_String f; + EST_Token v; + static EST_Val val0 = EST_Val(0); + + while (!ts.eoln()) + { + if (ts.eof()) + { + cerr << "load_features: unexpected end of file\n"; + return misc_read_error; + } + f = ts.get().string(); + v = EST_String::Empty; + while (((ts.peek() != ";") || (ts.peek().quoted())) && + (!ts.eof()) && (!ts.eoln())) + if (v == "") + v = ts.get(); + else + v = v.string() + + ts.peek().whitespace() + + ts.get().string(); + if (v.quoted() || (v.string() == "")) + set_path(f,EST_Val(v.string())); + else if (v.string() == "0") // very common cases for speed + set_path(f,val0); + else if ((strchr("0123456789-.",v.string()(0)) != NULL) && + (v.string().matches(RXdouble))) + { + if (v.string().matches(RXint)) + set_path(f, EST_Val(atoi(v.string()))); + else + set_path(f, EST_Val(atof(v.string()))); + } + else if (v.string().contains("F:")) + { + EST_Item_featfunc func = get_featfunc(v.string().after("F:")); + if (func != NULL) + set_path(f, est_val(func)); + else + { + cerr << "load_features: Unknown Function '" << f <<"'\n"; + set_path(f, feature_default_value); + } + } + else if (v.string() == "()") + { // An empty feature set + EST_Features *fs = new EST_Features; + set_path(f,est_val(fs)); + } + else if (v != "") // unsupported type + set_path(f,EST_Val(v.string())); + if (ts.peek() == ";") + ts.get(); + else if (!ts.eoln()) + { + cerr << "load_features: " << ts.pos_description() << + " missing semicolon in feature list\n"; + return misc_read_error; + } + } + return format_ok; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_ilist_aux.cc b/aeneas/cfw/speech_tools/base_class/EST_ilist_aux.cc new file mode 100644 index 00000000..2b23faf9 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_ilist_aux.cc @@ -0,0 +1,110 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor, Simon King */ +/* Date : 1994-99 */ +/*-----------------------------------------------------------------------*/ +/* IList i/o utility functions */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include +#include +#include +#include "EST_types.h" +#include "EST_String.h" +#include "EST_Pathname.h" +#include "EST_string_aux.h" +#include "EST_cutils.h" +#include "EST_Token.h" + +int ilist_member(const EST_IList &l,int i) +{ + EST_Litem *p; + for (p = l.head(); p != 0; p = p->next()) + if (l.item(p) == i) + return TRUE; + + return FALSE; +} + +int ilist_index(const EST_IList &l,int i) +{ + EST_Litem *p; + int j=0; + for (p = l.head(); p != 0; p = p->next()) + { + if (l.item(p) == i) + return j; + j++; + } + + return -1; +} + + +void IList_to_IVector(EST_IList &l, EST_IVector &v) +{ + int len,i; + + len = l.length(); + v.resize(len); + + //EST_TBI *p; + EST_Litem *p; + for (p = l.head(),i=0; p != 0; p = p->next(),i++) + v[i] = l(p); +} + + +void IVector_to_IList(EST_IVector &v, EST_IList &l) +{ + int i; + l.clear(); + for (i=0;i +#include "EST_TVector.h" +#include "EST_matrix_support.h" +#include "EST_bool.h" + +const int EST_CURRENT=-1; +const int EST_ALL=-1; + +bool EST_matrix_bounds_check(int r, + int c, + int num_rows, + int num_columns, + bool set) +{ + const char *what = set?"set":"access"; + + if ((r < 0) || (r >= num_rows)) + { + cerr << "Tried to " << what << " row " << r << " of " << num_rows << " row matrix\n"; + return FALSE; + } + if ((c < 0) || (c >= num_columns)) + { + cerr << "Tried to " << what << " column " << c << " of " << num_columns << " column matrix\n"; + return FALSE; + } + + return TRUE; +} + +bool EST_matrix_bounds_check(int r, int nr, + int c, int nc, + int num_rows, + int num_columns, + bool set) +{ + const char *what = set?"set":"access"; + + if (nr>0) + { + if ((r < 0) || (r >= num_rows)) + { + cerr << "Tried to " << what << " row " << r << " of " << num_rows << " row matrix\n"; + return FALSE; + } + if (r+nr-1 >= num_rows) + { + cerr << "Tried to " << what << " row " << r+nr-1 << " of " << num_rows << " row matrix\n"; + return FALSE; + } + } + if (nc>0) + { + if ((c < 0) || (c >= num_columns)) + { + cerr << "Tried to " << what << " column " << c << " of " << num_columns << " column matrix\n"; + return FALSE; + } + if (c+nc-1 >= num_columns) + { + cerr << "Tried to " << what << " column " << c+nc-1 << " of " << num_columns << " column matrix\n"; + return FALSE; + } + } + + return TRUE; +} + +bool EST_vector_bounds_check(int c, + int num_columns, + bool set) +{ + const char *what = set?"set":"access"; + + if ((c < 0) || (c >= num_columns)) + { + cerr << "Tried to " << what << " column " << c << " of " << num_columns << " column vector\n"; + return FALSE; + } + + return TRUE; +} + +bool EST_vector_bounds_check(int c, int nc, + int num_columns, + bool set) +{ + const char *what = set?"set":"access"; + + if (nc>0) + { + if ((c < 0) || (c >= num_columns)) + { + cerr << "Tried to " << what << " column " << c << " of " << num_columns << " column vector\n"; + return FALSE; + } + if (c+nc-1 >= num_columns) + { + cerr << "Tried to " << what << " column " << c+nc-1 << " of " << num_columns << " column vector\n"; + return FALSE; + } + } + return TRUE; +} + diff --git a/aeneas/cfw/speech_tools/base_class/EST_slist_aux.cc b/aeneas/cfw/speech_tools/base_class/EST_slist_aux.cc new file mode 100644 index 00000000..300f0080 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/EST_slist_aux.cc @@ -0,0 +1,240 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : May 1994 */ +/*-----------------------------------------------------------------------*/ +/* StrList/Vector i/o utility functions */ +/* */ +/*=======================================================================*/ + +#include +#include +#include +#include +#include +#include +#include "EST_types.h" +#include "EST_String.h" +#include "EST_Pathname.h" +#include "EST_string_aux.h" +#include "EST_cutils.h" +#include "EST_Token.h" + +int StrListtoFList(EST_StrList &s, EST_FList &f) +{ + EST_Litem *p; + + for (p = s.head(); p; p = p->next()) + if (!s(p).matches(RXdouble)) + { + cout << + "Expecting a floating point value in StrListtoFlist(): got " + << s(p) << endl; + return -1; + } + else + f.append(atof(s(p))); + + return 0; +} + +int StrListtoIList(EST_StrList &s, EST_IList &il) +{ + EST_Litem *p; + + for (p = s.head(); p; p = p->next()) + if (!s(p).matches(RXint)) + { + cout << + "Expecting a integer value in StrListtoIList(): got " + << s(p) << endl; + return -1; + } + else + il.append(atoi(s(p))); + + return 0; +} + +// read string list eclosed in brackets. Simply a place holder for +// future use with more complicate lists. +void BracketStringtoStrList(EST_String s, EST_StrList &l, EST_String sep) +{ + s.gsub("(", ""); + s.gsub(")", ""); + StringtoStrList(s, l, sep); +} + +void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep) +{ + EST_TokenStream ts; + EST_String tmp; + + ts.open_string(s); + + (void)sep; + if (sep != "") // default is standard white space + ts.set_WhiteSpaceChars(sep); + ts.set_SingleCharSymbols(";"); + + // modified by simonk - was appending an empty + // string at end of list. + // unmodified back again by pault + while (!ts.eof()) + l.append(ts.get().string()); + + ts.close(); + return; +} + +void StrListtoString(EST_StrList &l, EST_String &s, EST_String sep) +{ + for (EST_Litem *p = l.head(); p; p = p->next()) + s += l(p) + sep; +} + +EST_read_status load_StrList(EST_String filename, EST_StrList &l) +{ + EST_TokenStream ts; + EST_String s; + + if(ts.open(filename) != 0){ + cerr << "Can't open EST_StrList file " << filename << endl; + return misc_read_error; + } + + ts.set_SingleCharSymbols(""); + ts.set_PunctuationSymbols(""); + + while (!ts.eof()) + l.append(ts.get().string()); + + ts.close(); + return format_ok; +} + +EST_write_status save_StrList(EST_String filename, EST_StrList &l, + EST_String style) +{ + ostream *outf; + EST_Litem *p; + if (filename == "-") + outf = &cout; + else + outf = new ofstream(filename); + + if (!(*outf)) + return write_fail; + + if (style == "words") + { + for (p = l.head(); p; p = p->next()) + { + *outf << l(p); + if (p->next() != 0) + *outf << " "; + } + *outf << endl; + } + + else if (style == "lines") + for (p = l.head(); p; p = p->next()) + *outf << l(p) << endl; + else + { + cerr << "Unknown style for writing StrLists: " << style << endl; + return misc_write_error; + } + + delete outf; + + return write_ok; +} + +int strlist_member(const EST_StrList &l,const EST_String &s) +{ + EST_Litem *p; + for (p = l.head(); p != 0; p = p->next()) + if (l.item(p) == s) + return TRUE; + + return FALSE; +} + +int strlist_index(const EST_StrList &l,const EST_String &s) +{ + EST_Litem *p; + int j=0; + for (p = l.head(); p != 0; p = p->next()) + { + if (l.item(p) == s) + return j; + j++; + } + + return -1; +} + +void StrList_to_StrVector(EST_StrList &l, EST_StrVector &v) +{ + int len,i; + + len = l.length(); + v.resize(len); + + //EST_TBI *p; + EST_Litem *p; + for (p = l.head(),i=0; p != 0; p = p->next(),i++) + v[i] = l(p); +} + + +void StrVector_to_StrList(EST_StrVector &v, EST_StrList &l) +{ + int i; + l.clear(); + for (i=0;i +#include +#include +#include +#include +#include +#include "EST_types.h" +#include "EST_String.h" +#include "EST_Pathname.h" +#include "EST_string_aux.h" +#include "EST_cutils.h" +#include "EST_Token.h" + +EST_read_status load_TList_of_StrVector(EST_TList &w, + const EST_String &filename, + const int vec_len) +{ + + EST_TokenStream ts; + EST_String s; + EST_StrVector v; + int c; + + if(ts.open(filename) != 0){ + cerr << "Can't open EST_TList file " << filename << endl; + return misc_read_error; + } + + v.resize(vec_len); +// ts.set_SingleCharSymbols(""); +// ts.set_PunctuationSymbols(""); + + c=0; + while (!ts.eof()) + { + + s = ts.get().string(); + if(s != "") + { + if(c == vec_len) + { + cerr << "Too many points in line - expected " << vec_len << endl; + return wrong_format; + } + else + v[c++] = s; + } + + if(ts.eoln()) + { + if(c != vec_len) + { + cerr << "Too few points in line - got " + << c << ", expected " << vec_len << endl; + return wrong_format; + } + else + { + w.append(v); + c=0; + } + } + } + + ts.close(); + return format_ok; + +} + diff --git a/aeneas/cfw/speech_tools/base_class/THash_aux.cc b/aeneas/cfw/speech_tools/base_class/THash_aux.cc new file mode 100644 index 00000000..a5d367ca --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/THash_aux.cc @@ -0,0 +1,54 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /************************************************************************/ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Fri Apr 4 1997 */ + /************************************************************************/ + /* */ + /* Default has functions and stuff. */ + /* */ + /************************************************************************/ + +#include "EST_THash.h" + +// default hash function, something like the one in Perl. + +unsigned int EST_HashFunctions::DefaultHash(const void *data, size_t size, unsigned int n) +{ + return DefaultHashFunction(data, size, n); +} + +unsigned int EST_HashFunctions::StringHash(const EST_String &key, unsigned int n) +{ + return DefaultHashFunction((const char *)key, key.length(), n); +} + diff --git a/aeneas/cfw/speech_tools/base_class/rateconv.cc b/aeneas/cfw/speech_tools/base_class/rateconv.cc new file mode 100644 index 00000000..10ef4651 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/rateconv.cc @@ -0,0 +1,599 @@ +/* + * $Id: rateconv.cc,v 1.5 2014/04/07 15:32:10 robert Exp $ + * + * RATECONV.C + * + * Convert sampling rate stdin to stdout + * + * Copyright (c) 1992, 1995 by Markus Mummert + * + ***************************************************************************** + * MODIFIED BY Alan W Black (awb@cstr.ed.ac.uk) + * Make it compilable under C++ + * and integrate into Edinburgh Speech Tools (i.e. no longer + * reads from stdin / writes to stdout) + * Removed interface functions + * ansified function calls + * made it work in floats rather than ints + * I got the original from a random linux site, the original + * author's email is + ***************************************************************************** + * + * Redistribution and use of this software, modification and inclusion + * into other forms of software are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of this software must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. If this software is redistributed in a modified condition + * it must reveal clearly that it has been modified. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * + * history: 2.9.92 begin coding + * 5.9.92 fully operational + * 14.2.95 provide BIG_ENDIAN, SWAPPED_BYTES_DEFAULT + * switches, Copyright note and References + * 25.11.95 changed XXX_ENDIAN to I_AM_XXX_ENDIAN + * default gain set to 0.8 + * 3.12.95 stereo implementation + * SWAPPED_BYTES_DEFAULT now HBYTE1ST_DEFAULT + * changed [L/2] to (L-1)/2 for exact symmetry + * + * + * IMPLEMENTATION NOTES + * + * Converting is achieved by interpolating the input samples in + * order to evaluate the represented continuous input slope at + * sample instances of the new rate (resampling). It is implemented + * as a polyphase FIR-filtering process (see reference). The rate + * conversion factor is determined by a rational factor. Its + * nominator and denominator are integers of almost arbitrary + * value, limited only by coefficient memory size. + * + * General rate conversion formula: + * + * out(n*Tout) = SUM in(m*Tin) * g((n*d/u-m)*Tin) * Tin + * over all m + * + * FIR-based rate conversion formula for polyphase processing: + * + * L-1 + * out(n*Tout) = SUM in(A(i,n)*Tin) * g(B(i,n)*Tin) * Tin + * i=0 + * + * A(i,n) = i - (L-1)/2 + [n*d/u] + * = i - (L-1)/2 + [(n%u)*d/u] + [n/u]*d + * B(i,n) = n*d/u - [n*d/u] + (L-1)/2 - i + * = ((n%u)*d/u)%1 + (L-1)/2 - i + * Tout = Tin * d/u + * + * where: + * n,i running integers + * out(t) output signal sampled at t=n*Tout + * in(t) input signal sampled in intervals Tin + * u,d up- and downsampling factor, integers + * g(t) interpolating function + * L FIR-length of realized g(t), integer + * / float-division-operator + * % float-modulo-operator + * [] integer-operator + * + * note: + * (L-1)/2 in A(i,n) can be omitted as pure time shift yielding + * a causal design with a delay of ((L-1)/2)*Tin. + * n%u is a cyclic modulo-u counter clocked by out-rate + * [n/u]*d is a d-increment counter, advanced when n%u resets + * B(i,n)*Tin can take on L*u different values, at which g(t) + * has to be sampled as a coefficient array + * + * Interpolation function design: + * + * The interpolation function design is based on a sinc-function + * windowed by a gaussian function. The former determines the + * cutoff frequency, the latter limits the necessary FIR-length by + * pushing the outer skirts of the resulting impulse response below + * a certain threshold fast enough. The drawback is a smoothed + * cutoff inducing some aliasing. Due to the symmetry of g(t) the + * group delay of the filtering process is constant (linear phase). + * + * g(t) = 2*fgK*sinc(pi*2*fgK*t) * exp(-pi*(2*fgG*t)**2) + * + * where: + * fgK cutoff frequency of sinc function in f-domain + * fgG key frequency of gaussian window in f-domain + * reflecting the 6.82dB-down point + * + * note: + * Taking fsin=1/Tin as the input sampling frequency, it turns out + * that in conjunction with L, u and d only the ratios fgK/(fsin/2) + * and fgG/(fsin/2) specify the whole process. Requiring fsin, fgK + * and fgG as input purposely keeps the notion of absolute + * frequencies. + * + * Numerical design: + * + * Samples are expected to be 16bit-signed integers, alternating + * left and right channel in case of stereo mode- The byte order + * per sample is selectable. FIR-filtering is implemented using + * 32bit-signed integer arithmetic. Coefficients are scaled to + * find the output sample in the high word of accumulated FIR-sum. + * + * Interpolation can lead to sample magnitudes exceeding the + * input maximum. Worst case is a full scale step function on the + * input. In this case the sinc-function exhibits an overshoot of + * 2*9=18percent (Gibb's phenomenon). In any case sample overflow + * can be avoided by a gain of 0.8. + * + * If u=d=1 and if the input stream contains only a single sample, + * the whole length of the FIR-filter will be written to the output. + * In general the resulting output signal will be (L-1)*fsout/fsin + * samples longer than the input signal. The effect is that a + * finite input sequence is viewed as padded with zeros before the + * `beginning' and after the `end'. + * + * The output lags ((L-1)/2)*Tin behind to implement g(t) as a + * causal system corresponding to a causal relationship of the + * discrete-time sequences in(m/fsin) and out(n/fsout) with + * respect to a sequence time origin at t=n*Tin=m*Tout=0. + * + * + * REFERENCES + * + * Crochiere, R. E., Rabiner, L. R.: "Multirate Digital Signal + * Processing", Prentice-Hall, Englewood Cliffs, New Jersey, 1983 + * + * Zwicker, E., Fastl, H.: "Psychoacoustics - Facts and Models", + * Springer-Verlag, Berlin, Heidelberg, New-York, Tokyo, 1990 + */ + +#include +#include +#include +#include +#include "rateconv.h" + +/* + * adaptable defines and globals + */ +#define BYTE char /* signed or unsigned */ +#define WORD short /* signed or unsigned, fit two BYTEs */ +#define LONG int /* signed, fit two WORDs */ + +#ifndef MAXUP +#define MAXUP 0x400 /* MAXUP*MAXLENGTH worst case malloc */ +#endif + +#ifndef MAXLENGTH +#define MAXLENGTH 0x400 /* max FIR length */ +#endif + /* accounts for mono samples, means */ +#define OUTBUFFSIZE (2*MAXLENGTH) /* fit >=MAXLENGHT stereo samples */ +#define INBUFFSIZE (4*MAXLENGTH) /* fit >=2*MAXLENGTH stereo samples */ +#define sqr(a) ((a)*(a)) + +#ifndef M_PI +#define M_PI 3.14159265358979 +#endif + +/* AWB deleted previous byte swap globals, byteswap is done external to */ +/* this function */ + +#ifdef STEREO_DEFAULT +static int g_monoflag = 0; +#else +static int g_monoflag = -1; +#endif + +/* + * other globals + */ +static double g_ampli = 0.8; /* default gain, don't change */ +static int +/* g_infilehandle = 0, */ /* stdin */ +/* g_outfilehandle = 1, */ /* stdout */ + g_firlen, /* FIR-length */ + g_up, /* upsampling factor */ + g_down /* downsampling factor */ +; + +static float + g_sin[INBUFFSIZE], /* input buffer */ + g_sout[OUTBUFFSIZE], /* output buffer */ + *g_coep; /* coefficient array pointer */ + +static double + g_fsi, /* input sampling frequency */ + g_fgk, /* sinc-filter cutoff frequency */ + g_fgg /* gaussian window key frequency */ +; /* (6.8dB down freq. in f-domain) */ + + +/* + * evaluate sinc(x) = sin(x)/x safely + */ +static double sinc(double x) +{ + return(fabs(x) < 1E-50 ? 1.0 : sin(fmod(x,2*M_PI))/x); +} + +/* + * evaluate interpolation function g(t) at t + * integral of g(t) over all times is expected to be one + */ +static double interpol_func(double t,double fgk,double fgg) +{ + return (2*fgk*sinc(M_PI*2*fgk*t)*exp(-M_PI*sqr(2*fgg*t))); +} + +/* + * evaluate coefficient from i, q=n%u by sampling interpolation function + * and scale it for integer multiplication used by FIR-filtering + */ +static float coefficient(int i,int q,int firlen,double fgk,double fgg, + double fsi,int up,int down,double amp) +{ + float val; + double d; + + d = interpol_func((fmod(q*down/(double)up,1.) + (firlen-1)/2. - i) / fsi, + fgk, + fgg); + val = amp * d/fsi; + return val; +} + +/* + * transfer n floats from s to d + */ +static void transfer_int(float *s,float *d,int n) +{ + memmove(d,s,sizeof(float)*n); +} + +/* + * zerofill n floats from s + */ +static void zerofill(float *s,int n) +{ + memset(s,0,n*(sizeof(float))); +} + +/* + * FIR-routines, mono and stereo + * this is where we need all the MIPS + */ +void fir_mono(float *inp,float *coep,int firlen,float *outp) +{ + float akku = 0, *endp; + int n1 = (firlen / 8) * 8, n0 = firlen % 8; + + endp = coep + n1; + while (coep != endp) { + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + akku += *inp++ * *coep++; + } + + endp = coep + n0; + while (coep != endp) { + akku += *inp++ * *coep++; + } + + *outp = akku; +} + +static void fir_stereo(float *inp,float *coep,int firlen,float *out1p,float *out2p) +{ + float akku1 = 0, akku2 = 0, *endp; + int n1 = (firlen / 8) * 8, n0 = firlen % 8; + + endp = coep + n1; + while (coep != endp) { + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + } + + endp = coep + n0; + while (coep != endp) { + akku1 += *inp++ * *coep; + akku2 += *inp++ * *coep++; + } + *out1p = akku1; + *out2p = akku2; +} + +/* + * filtering from input buffer to output buffer; + * returns number of processed samples in output buffer: + * if it is not equal to output buffer size, + * the input buffer is expected to be refilled upon entry, so that + * the last firlen numbers of the old input buffer are + * the first firlen numbers of the new input buffer; + * if it is equal to output buffer size, the output buffer + * is full and is expected to be stowed away; + * + */ +static int inbaseidx = 0, inoffset = 0, cycctr = 0, outidx = 0; + +static int filtering_on_buffers + (float *inp,int insize,float *outp, int outsize, + float *coep,int firlen,int up,int down,int monoflag) +{ + + if (monoflag) { + while (-1) { + inoffset = (cycctr * down)/up; + if ((inbaseidx + inoffset + firlen) > insize) { + inbaseidx -= insize - firlen + 1; + return(outidx); + } + fir_mono(inp + inoffset + inbaseidx, + coep + cycctr * firlen, + firlen, outp + outidx++); + cycctr++; + if (!(cycctr %= up)) + inbaseidx += down; + if (!(outidx %= outsize)) + return(outsize); + } + } + else { + /* + * rule how to convert mono routine to stereo routine: + * firlen, up, down and cycctr relate to samples in general, + * wether mono or stereo; inbaseidx, inoffset and outidx as + * well as insize and outsize still account for mono samples. + */ + while (-1) { + inoffset = 2*((cycctr * down)/up); + if ((inbaseidx + inoffset + 2*firlen) > insize) { + inbaseidx -= insize - 2*firlen + 2; + return(outidx); + } +/* order? + fir_stereo(inp + inoffset + inbaseidx, + coep + cycctr * firlen, firlen, + outp + outidx++, outp + outidx++); + +*/ + fir_stereo(inp + inoffset + inbaseidx, + coep + cycctr * firlen, firlen, + outp + outidx, outp + outidx+1); + outidx += 2; + + cycctr++; + if (!(cycctr %= up)) + inbaseidx += 2*down; + if (!(outidx %= outsize)) + return(outsize); + } + } +} + +/* + * set up coefficient array + */ +static void make_coe(void) +{ + int i, q; + + for (i = 0; i < g_firlen; i++) { + for (q = 0; q < g_up; q++) { + g_coep[q * g_firlen + i] = coefficient(i, q, g_firlen, + g_fgk, g_fgg, g_fsi, g_up, g_down, g_ampli); + } + } +} + +/***********************************************************************/ +/* Serious modifications by Alan W Black (awb@cstr.ed.ac.uk) */ +/* to interface with rest of system // deleted various io functions */ +/* too. */ +/***********************************************************************/ +static WORD *inbuff = NULL; +static int inpos; +static int inmax; +static WORD *outbuff = NULL; +static int outpos; +static int outmax; + +static int ioerr(void) +{ + delete[] g_coep; + return -1; +} + +static int gcd(int x, int y) +{ + int remainder,a,b; + + if ((x < 1) || (y < 1)) + return -1; + + for (a=x,b=y; b != 0; ) + { + remainder = a % b; + a = b; + b = remainder; + } + return a; +} + +static int find_ratios(int in_samp_freq,int out_samp_freq,int *up,int *down) +{ + // Find ratios + int d; + + d = gcd(in_samp_freq,out_samp_freq); + if (d == -1) return -1; + *down = in_samp_freq / d; + *up = out_samp_freq / d; + + if ((*up > 1024) || (*down > 1024)) + return -1; // should try harder + + return 0; +} + +static int intimport(float *buff, int n) +{ + /* Import n more samples from PWave into buff */ + int i,end; + + if ((inpos+n) >= inmax) + end = inmax - inpos; + else + end = n; + for (i=0;i < end; i++) + buff[i] = inbuff[inpos++]; + + return i; +} + +static int intexport(float *buff, int n) +{ + /* Export n samples from buff into end of PWave */ + int i,end; + + if ((outpos+n) >= outmax) + end = outmax - inpos; + else + end = n; + for (i=0;i < end; i++) + outbuff[outpos++] = (short)buff[i]; + + return i; +} + +static int init_globs(WORD *in,int insize, WORD **out, int *outsize, + int in_samp_freq, int out_samp_freq) +{ + int new_size; + g_monoflag = 1; /* always mono */ + if (find_ratios(in_samp_freq,out_samp_freq,&g_up,&g_down) == -1) + return -1; + g_fsi = 1.0; /* ? in_samp_freq ? */ + if (g_up > g_down) + { // upsampling + g_fgg = 0.0116; + g_fgk = 0.461; + g_firlen = (int)(162 * (float)g_up/(float)g_down); + } + else + { // downsampling + g_fgg = (float)g_up/(float)g_down * 0.0116; + g_fgk = (float)g_up/(float)g_down * 0.461; + g_firlen = (int)(162 * (float)g_down/(float)g_up); + } + if (g_firlen < 1 || g_firlen > MAXLENGTH) + return -1; + g_ampli = 0.8; + g_coep = new float[g_firlen * g_up]; + + inpos = 0; + inmax = insize; + inbuff = in; + new_size = (int)(((float)out_samp_freq/(float)in_samp_freq)* + 1.1*insize)+2000; + *out = new WORD[new_size]; + outbuff = *out; + outmax = new_size; + *outsize = 0; + outpos = 0; + + /* For filter_on_buffers */ + inbaseidx = 0; + inoffset = 0; + cycctr = 0; + outidx = 0; + + return 0; +} + + +/* + * External call added by Alan W Black, 4th June 1996 + * a combination of parse args and main + */ +int rateconv(short *in,int isize, short **out, int *osize, + int in_samp_freq, int out_samp_freq) +{ + int insize = 0, outsize = 0, skirtlen; + + if (init_globs(in,isize,out,osize,in_samp_freq,out_samp_freq) == -1) + return -1; + + make_coe(); + skirtlen = (g_firlen - 1) * (g_monoflag ? 1 : 2); + zerofill(g_sin, skirtlen); + do { + insize = intimport(g_sin + skirtlen, INBUFFSIZE - skirtlen); + if (insize < 0 || insize > INBUFFSIZE - skirtlen) + return ioerr(); + do { + outsize = filtering_on_buffers(g_sin, skirtlen + insize, + g_sout, OUTBUFFSIZE, + g_coep, g_firlen, g_up, g_down, + g_monoflag); + if (outsize != OUTBUFFSIZE) { + transfer_int(g_sin + insize, g_sin, skirtlen); + break; + } + if (intexport(g_sout, outsize) != outsize) + return ioerr(); + } while (-1); + } while (insize > 0); + zerofill(g_sin + skirtlen, skirtlen); + do { + outsize = filtering_on_buffers(g_sin, skirtlen + skirtlen, + g_sout, OUTBUFFSIZE, + g_coep, g_firlen, g_up, g_down, + g_monoflag); + if (intexport(g_sout, outsize) != outsize) + return ioerr(); + } while (outsize == OUTBUFFSIZE); + + delete[] g_coep; + + *osize = outpos; + + /* The new signal will be offset by half firlen window so fix it */ + memmove(*out,*out+g_firlen/4,*osize*2); + *osize -= g_firlen/4; + + return 0; + +} + diff --git a/aeneas/cfw/speech_tools/base_class/vec_mat_aux.cc b/aeneas/cfw/speech_tools/base_class/vec_mat_aux.cc new file mode 100644 index 00000000..ab034736 --- /dev/null +++ b/aeneas/cfw/speech_tools/base_class/vec_mat_aux.cc @@ -0,0 +1,734 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Simon King */ +/* Date : April 1995 */ +/*-----------------------------------------------------------------------*/ +/* EST_FMatrix Class auxiliary functions */ +/* */ +/*=======================================================================*/ +#include "EST_FMatrix.h" +#include "EST_system.h" +#include +#include +#include "EST_unix.h" +#include "EST_math.h" +#include + +bool polynomial_fit(EST_FVector &x, EST_FVector &y, + EST_FVector &co_effs, int order) +{ + EST_FVector weights; + weights.resize(x.n()); + for(int i=0; i= 1" << endl; + return false; + } + + if(x.n() != y.n()){ + cerr << "polynomial_fit : x and y must have same dimension" << endl; + return false; + } + + if(weights.n() != y.n()){ + cerr << "polynomial_fit : weights must have same dimension as x and y" << endl; + return false; + } + + if(x.n() <= order){ + cerr << "polynomial_fit : x and y must have at least order+1 elements" + << endl; + return false; + } + + + // matrix of basis function values + EST_FMatrix A; + A.resize(x.n(),order+1); + + EST_FVector y1; + y1.resize(y.n()); + + for(int row=0;row v) + v = a.a_no_check(i, j); + + return v; +} + +int square(const EST_FMatrix &a) +{ + return a.num_rows() == a.num_columns(); +} +// add all elements in matrix. +float sum(const EST_FMatrix &a) +{ + int i, j; + float t = 0.0; + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + t += a.a_no_check(i, j); + return t; +} + +// set all elements not on the diagonal to zero. +EST_FMatrix diagonalise(const EST_FMatrix &a) +{ + int i; + EST_FMatrix b(a, 0); // initialise and fill b with zeros + + if (a.num_rows() != a.num_columns()) + { + cerr << "diagonalise: non-square matrix "; + return b; + } + + for (i = 0; i < a.num_rows(); ++i) + b(i, i) = a.a_no_check(i, i); + + return b; +} + +// set all elements not on the diagonal to zero. +void inplace_diagonalise(EST_FMatrix &a) +{ + // NB - will work on non-square matrices without warning + int i,j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + if(i != j) + a.a_no_check(i, j) = 0; +} + +EST_FMatrix sub(const EST_FMatrix &a, int row, int col) +{ + int i, j, I, J; + int n = a.num_rows() - 1; + EST_FMatrix s(n, n); + + for (i = I = 0; i < n; ++i, ++I) + { + if (I == row) + ++I; + for (j = J = 0; j < n; ++j, ++J) + { + if (J == col) + ++J; + s(i, j) = a.a(I, J); + } + } + + // cout << "sub: row " << row << " col " << col << "\n" << s; + + return s; +} + +EST_FMatrix row(const EST_FMatrix &a, int row) +{ + EST_FMatrix s(1, a.num_columns()); + int i; + + for (i = 0; i < a.num_columns(); ++i) + s(0, i) = a.a(row, i); + + return s; +} + +EST_FMatrix column(const EST_FMatrix &a, int col) +{ + EST_FMatrix s(a.num_rows(), 1); + int i; + + for (i = 0; i < a.num_rows(); ++i) + s(i, 0) = a.a(i, col); + + return s; +} + +EST_FMatrix triangulate(const EST_FMatrix &a) +{ + EST_FMatrix b(a, 0); + int i, j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = i; j < a.num_rows(); ++j) + b(j, i) = a.a(j, i); + + return b; +} + +void transpose(const EST_FMatrix &a,EST_FMatrix &b) +{ + int i, j; + b.resize(a.num_columns(), a.num_rows()); + + for (i = 0; i < b.num_rows(); ++i) + for (j = 0; j < b.num_columns(); ++j) + b.a_no_check(i, j) = a.a_no_check(j, i); +} + +EST_FMatrix backwards(EST_FMatrix &a) +{ + int i, j, n; + n = a.num_columns(); + EST_FMatrix t(n, n); + + for (i = 0; i < n; ++i) + for (j = 0; j < n; ++j) + t(n - i - 1, n - j - 1) = a.a(i, j); + + return t; +} + + +// changed name from abs as it causes on at least on POSIX machine +// where int abs(int) is a macro +EST_FMatrix fmatrix_abs(const EST_FMatrix &a) +{ + int i, j; + EST_FMatrix b(a, 0); + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + b.a_no_check(i, j) = fabs(a.a_no_check(i, j)); + + return b; +} + +static void row_swap(int from, int to, EST_FMatrix &a) +{ + int i; + float f; + + if (from == to) + return; + + for (i=0; i < a.num_columns(); i++) + { + f = a.a_no_check(to,i); + a.a_no_check(to,i) = a.a_no_check(from,i); + a.a_no_check(from,i) = f; + } +} + +int inverse(const EST_FMatrix &a,EST_FMatrix &inv) +{ + int singularity=0; + return inverse(a,inv,singularity); +} + +int inverse(const EST_FMatrix &a,EST_FMatrix &inv,int &singularity) +{ + + // Used to use a function written by Richard Tobin (in C) but + // we no longer need C functionality any more. This algorithm + // follows that in "Introduction to Algorithms", Cormen, Leiserson + // and Rivest (p759) and the term Gauss-Jordon is used for some part, + // As well as looking back at Richard's. + // This also keeps a record of which rows are which from the original + // so that it can return which column actually has the singularity + // in it if it fails to find an inverse. + int i, j, k; + int n = a.num_rows(); + EST_FMatrix b = a; // going to destructively manipulate b to get inv + EST_FMatrix pos; // the original position + float biggest,s; + int r=0,this_row,all_zeros; + + singularity = -1; + if (a.num_rows() != a.num_columns()) + return FALSE; + + // Make the inverse the identity matrix. + inv.resize(n,n); + pos.resize(n,1); + for (i=0; i biggest) + { + r = j; + biggest = fabs(b.a_no_check(j,i)); + } + } + + if (biggest == 0.0) // oops found a singularity + { + singularity = (int)pos.a_no_check(i,0); + return FALSE; + } + + // Swap current with biggest + this_row = (int)pos.a_no_check(i,0); // in case we need this number + row_swap(r,i,b); + row_swap(r,i,inv); + row_swap(r,i,pos); + + // Make b(i,i) = 1 + s = b(i,i); + for (k=0; k j) ? this_row : j); + return FALSE; + } + } + } + + return TRUE; +} + +int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv) +{ + int singularity=0; + return pseudo_inverse(a,inv,singularity); +} + +int pseudo_inverse(const EST_FMatrix &a, EST_FMatrix &inv,int &singularity) +{ + // for non-square matrices + // useful for solving linear eqns + // (e.g. polynomial fitting) + + // is it square ? + if( a.num_rows() == a.num_columns() ) + return inverse(a,inv,singularity); + + if( a.num_rows() < a.num_columns() ) + return FALSE; + + EST_FMatrix a_trans,atrans_a,atrans_a_inverse; + + transpose(a,a_trans); + multiply(a_trans,a,atrans_a); + if (!inverse(atrans_a,atrans_a_inverse,singularity)) + return FALSE; + multiply(atrans_a_inverse,a_trans,inv); + + return TRUE; +} + + +float determinant(const EST_FMatrix &a) +{ + int i, j; + int n = a.num_rows(); + float det; + if (!square(a)) + { + cerr << "Tried to take determinant of non-square matrix\n"; + return 0.0; + } + + EST_FVector A(n); + + if (n == 2) // special case of 2x2 determinant + return (a.a_no_check(0,0) * a.a_no_check(1,1)) - + (a.a_no_check(0,1) * a.a_no_check(1,0)); + + float p; + + // create cofactor matrix + j = 1; + for (i = 0; i < n; ++i) + { + p = (float)(i + j + 2); // because i & j should start at 1 + // cout << "power " <

+#include "EST_DMatrix.h" +#include +#include "EST_math.h" +#include "EST_unix.h" + +bool polynomial_fit(EST_DVector &x, EST_DVector &y, + EST_DVector &co_effs, int order) +{ + EST_DVector weights; + weights.resize(x.n()); + for(int i=0; i= 1" << endl; + return false; + } + + if(x.n() != y.n()){ + cerr << "polynomial_fit : x and y must have same dimension" << endl; + return false; + } + + if(weights.n() != y.n()){ + cerr << "polynomial_fit : weights must have same dimension as x and y" << endl; + return false; + } + + if(x.n() <= order){ + cerr << "polynomial_fit : x and y must have at least order+1 elements" + << endl; + return false; + } + + + // matrix of basis function values + EST_DMatrix A; + A.resize(x.n(),order+1); + + EST_DVector y1; + y1.resize(y.n()); + + for(int row=0;row v) + v = a.a_no_check(i, j); + + return v; +} + +int square(const EST_DMatrix &a) +{ + return a.num_rows() == a.num_columns(); +} +// add all elements in matrix. +double sum(const EST_DMatrix &a) +{ + int i, j; + double t = 0.0; + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + t += a.a_no_check(i, j); + return t; +} + +// set all elements not on the diagonal to zero. +EST_DMatrix diagonalise(const EST_DMatrix &a) +{ + int i; + EST_DMatrix b(a, 0); // initialise and fill b with zeros + + if (a.num_rows() != a.num_columns()) + { + cerr << "diagonalise: non-square matrix "; + return b; + } + + for (i = 0; i < a.num_rows(); ++i) + b(i, i) = a.a_no_check(i, i); + + return b; +} + +// set all elements not on the diagonal to zero. +void inplace_diagonalise(EST_DMatrix &a) +{ + // NB - will work on non-square matrices without warning + int i,j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + if(i != j) + a.a_no_check(i, j) = 0; +} + +EST_DMatrix sub(const EST_DMatrix &a, int row, int col) +{ + int i, j, I, J; + int n = a.num_rows() - 1; + EST_DMatrix s(n, n); + + for (i = I = 0; i < n; ++i, ++I) + { + if (I == row) + ++I; + for (j = J = 0; j < n; ++j, ++J) + { + if (J == col) + ++J; + s(i, j) = a.a(I, J); + } + } + + // cout << "sub: row " << row << " col " << col << "\n" << s; + + return s; +} + +EST_DMatrix row(const EST_DMatrix &a, int row) +{ + EST_DMatrix s(1, a.num_columns()); + int i; + + for (i = 0; i < a.num_columns(); ++i) + s(0, i) = a.a(row, i); + + return s; +} + +EST_DMatrix column(const EST_DMatrix &a, int col) +{ + EST_DMatrix s(a.num_rows(), 1); + int i; + + for (i = 0; i < a.num_rows(); ++i) + s(i, 0) = a.a(i, col); + + return s; +} + +EST_DMatrix triangulate(const EST_DMatrix &a) +{ + EST_DMatrix b(a, 0); + int i, j; + + for (i = 0; i < a.num_rows(); ++i) + for (j = i; j < a.num_rows(); ++j) + b(j, i) = a.a(j, i); + + return b; +} + +void transpose(const EST_DMatrix &a,EST_DMatrix &b) +{ + int i, j; + b.resize(a.num_columns(), a.num_rows()); + + for (i = 0; i < b.num_rows(); ++i) + for (j = 0; j < b.num_columns(); ++j) + b.a_no_check(i, j) = a.a_no_check(j, i); +} + +EST_DMatrix backwards(EST_DMatrix &a) +{ + int i, j, n; + n = a.num_columns(); + EST_DMatrix t(n, n); + + for (i = 0; i < n; ++i) + for (j = 0; j < n; ++j) + t(n - i - 1, n - j - 1) = a.a(i, j); + + return t; +} + + +// changed name from abs as it causes on at least on POSIX machine +// where int abs(int) is a macro +EST_DMatrix DMatrix_abs(const EST_DMatrix &a) +{ + int i, j; + EST_DMatrix b(a, 0); + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + b.a_no_check(i, j) = fabs(a.a_no_check(i, j)); + + return b; +} + +static void row_swap(int from, int to, EST_DMatrix &a) +{ + int i; + double f; + + if (from == to) + return; + + for (i=0; i < a.num_columns(); i++) + { + f = a.a_no_check(to,i); + a.a_no_check(to,i) = a.a_no_check(from,i); + a.a_no_check(from,i) = f; + } +} + +int inverse(const EST_DMatrix &a,EST_DMatrix &inv) +{ + int singularity=0; + return inverse(a,inv,singularity); +} + +int inverse(const EST_DMatrix &a,EST_DMatrix &inv,int &singularity) +{ + + // Used to use a function written by Richard Tobin (in C) but + // we no longer need C functionality any more. This algorithm + // follows that in "Introduction to Algorithms", Cormen, Leiserson + // and Rivest (p759) and the term Gauss-Jordon is used for some part, + // As well as looking back at Richard's. + // This also keeps a record of which rows are which from the original + // so that it can return which column actually has the singularity + // in it if it fails to find an inverse. + int i, j, k; + int n = a.num_rows(); + EST_DMatrix b = a; // going to destructively manipulate b to get inv + EST_DMatrix pos; // the original position + double biggest,s; + int r=0,this_row,all_zeros; + + singularity = -1; + if (a.num_rows() != a.num_columns()) + return FALSE; + + // Make the inverse the identity matrix. + inv.resize(n,n); + pos.resize(n,1); + for (i=0; i biggest) + { + r = j; + biggest = fabs(b.a_no_check(j,i)); + } + } + + if (biggest == 0.0) // oops found a singularity + { + singularity = (int)pos.a_no_check(i,0); + return FALSE; + } + + // Swap current with biggest + this_row = (int)pos.a_no_check(i,0); // in case we need this number + row_swap(r,i,b); + row_swap(r,i,inv); + row_swap(r,i,pos); + + // Make b(i,i) = 1 + s = b(i,i); + for (k=0; k j) ? this_row : j); + return FALSE; + } + } + } + + return TRUE; +} + +int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv) +{ + int singularity=0; + return pseudo_inverse(a,inv,singularity); +} + +int pseudo_inverse(const EST_DMatrix &a, EST_DMatrix &inv,int &singularity) +{ + // for non-square matrices + // useful for solving linear eqns + // (e.g. polynomial fitting) + + // is it square ? + if( a.num_rows() == a.num_columns() ) + return inverse(a,inv,singularity); + + if( a.num_rows() < a.num_columns() ) + return FALSE; + + EST_DMatrix a_trans,atrans_a,atrans_a_inverse; + + transpose(a,a_trans); + multiply(a_trans,a,atrans_a); + if (!inverse(atrans_a,atrans_a_inverse,singularity)) + return FALSE; + multiply(atrans_a_inverse,a_trans,inv); + + return TRUE; +} + + +double determinant(const EST_DMatrix &a) +{ + int i, j; + int n = a.num_rows(); + double det; + if (!square(a)) + { + cerr << "Tried to take determinant of non-square matrix\n"; + return 0.0; + } + + EST_DVector A(n); + + if (n == 2) // special case of 2x2 determinant + return (a.a_no_check(0,0) * a.a_no_check(1,1)) - + (a.a_no_check(0,1) * a.a_no_check(1,0)); + + double p; + + // create cofactor matrix + j = 1; + for (i = 0; i < n; ++i) + { + p = (double)(i + j + 2); // because i & j should start at 1 + // cout << "power " <

resize(0); + return *ans; + }; + + ans->resize(a.length()); + + for(i=0;ia_no_check(i) = a.a_no_check(i) + b.a_no_check(i); + + return *ans; +} + +EST_DVector subtract(const EST_DVector &a,const EST_DVector &b) +{ + // a - b + EST_DVector *ans = new EST_DVector; + int i; + + if(a.length() != b.length()) + { + cerr << "Can't subtract vectors of differing lengths !" << endl; + ans->resize(0); + return *ans; + }; + + ans->resize(a.length()); + + for(i=0;ia_no_check(i) = a.a_no_check(i) - b.a_no_check(i); + + return *ans; +} + +EST_DVector diagonal(const EST_DMatrix &a) +{ + + EST_DVector ans; + if(a.num_rows() != a.num_columns()) + { + cerr << "Can't extract diagonal of non-square matrix !" << endl; + return ans; + } + int i; + ans.resize(a.num_rows()); + for(i=0;i +#include +#include "EST_unix.h" +#include "EST_math.h" +#include + +int matrix_max(const EST_IMatrix &a) +{ + int i, j; + int v = INT_MIN; + + for (i = 0; i < a.num_rows(); ++i) + for (j = 0; j < a.num_columns(); ++j) + if (a.a_no_check(i, j) > v) + v = a.a_no_check(i, j); + + return v; +} + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TDequeI.h b/aeneas/cfw/speech_tools/instantiate/EST_TDequeI.h new file mode 100644 index 00000000..b446362b --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TDequeI.h @@ -0,0 +1,72 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TDEQ_I_H__ +#define __EST_TDEQ_I_H__ + +/** Instantiate rules for list template. + * + * @author Richard Caley + * @version $Id: EST_TDequeI.h,v 1.4 2006/07/19 21:52:12 awb Exp $ + */ + +// Instantiation Macros + +// the typedef is purely to get the type name through the following macro. + +#define Instantiate_TDEQ_T(T, TAG) \ + template class EST_TDeque; + +#define Instantiate_TDEQ(T) Instantiate_TDEQ_T(T, T) + +#define Declare_TDEQ_T(T, TAG) \ + template <> const T *EST_TDeque< T >::Filler=NULL; + +#define Declare_TDEQ_Base_T(T, FILLER ,TAG) \ + const T TAG ## _deq_filler=FILLER; \ + template <> const T *EST_TDeque< T >::Filler=& TAG ## _deq_filler; + + +#define Declare_TDEQ_Class_T(T, FILLER,TAG) \ + const T TAG ## _deq_filler(FILLER); \ + template <> const T *EST_TDeque< T >::Filler=& TAG ## _deq_filler; + +#define Declare_TDEQ(T) Declare_TDEQ_T(T, T) + +#define Declare_TDEQ_Base(T, FILLER) Declare_TDEQ_Base_T(T, FILLER, T) +#define Declare_TDEQ_Class(T, FILLER) Declare_TDEQ_Class_T(T, FILLER, T) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_THashI.h b/aeneas/cfw/speech_tools/instantiate/EST_THashI.h new file mode 100644 index 00000000..06102312 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_THashI.h @@ -0,0 +1,102 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_THASH_I_H__ +#define __EST_THASH_I_H__ + +#include "EST_system.h" +#include "instantiate/EST_TIteratorI.h" + +/** Instantiate rules for hash template. + * + * @author Richard Caley + * @version $Id: EST_THashI.h,v 1.3 2004/05/04 00:00:17 awb Exp $ + */ + +// Instantiation Macros + +#define Instantiate_THash_T_IT_IP(KEY, VAL, TAG, IP, IPK) \ + typedef EST_THash< KEY, VAL > HASH_ ## TAG ## _t; \ + typedef EST_Hash_Pair< KEY, VAL > HASHPAIR_ ## TAG ## _t; \ + Instantiate_TStructIterator_T(HASH_ ## TAG ## _t, HASH_ ## TAG ## _t:: IP, HASHPAIR_ ## TAG ## _t, HASH_ ## TAG ## _itt) \ + Instantiate_TIterator_T(HASH_ ## TAG ## _t, HASH_ ## TAG ## _t:: IP, HASHPAIR_ ## TAG ## _t, HASH_ ## TAG ## _itt) \ + Instantiate_TIterator_T(HASH_ ## TAG ## _t, HASH_ ## TAG ## _t:: IPK, KEY, HASH_ ## TAG ## _itt) + +#if defined(VISUAL_CPP) +# define Instantiate_THash_T_IT(KEY, VAL, TAG) \ + Instantiate_THash_T_IT_IP(KEY, VAL, TAG, IPointer_s, IPointer_k_s) +#else +# define Instantiate_THash_T_IT(KEY, VAL, TAG) \ + Instantiate_THash_T_IT_IP(KEY, VAL, TAG, IPointer, IPointer_k) +#endif + +#define Instantiate_THash_T_MIN(KEY, VAL, TAG) \ + template class EST_Hash_Pair< KEY, VAL >; \ + template class EST_THash< KEY, VAL >; + +#define Instantiate_THash_T(KEY, VAL, TAG) \ + Instantiate_THash_T_MIN(KEY, VAL, TAG) \ + Instantiate_THash_T_IT(KEY, VAL, TAG) + +#define Instantiate_THash(KEY, VAL) Instantiate_THash_T(KEY, VAL, KEY ## VAL) +#define Instantiate_THash_MIN(KEY, VAL) Instantiate_THash_T_MIN(KEY, VAL, KEY ## VAL) +#define Instantiate_THash_IT(KEY, VAL, IP) \ + Instantiate_THash_T_IT(KEY, VAL, KEY ## VAL, IP) + + +/* disabled. it's INVALID !!! +#define Declare_THash_T(KEY, VAL, TAG) \ + VAL EST_THash< KEY, VAL >::Dummy_Value; \ + KEY EST_THash< KEY, VAL >::Dummy_Key; \ + EST_THash< KEY, VAL > TAG ## _hash_dummy(0); +*/ + +#define Declare_THash_Base_T(KEY, VAL, DEFAULTK, DEFAULT, ERROR,TAG) \ + template <> KEY EST_THash< KEY, VAL >::Dummy_Key=DEFAULTK; \ + template <> VAL EST_THash::Dummy_Value=DEFAULT; + +#define Declare_THash_Class_T(KEY, VAL, DEFAULTK, DEFAULT, ERROR,TAG) \ + template <> KEY EST_THash< KEY, VAL >::Dummy_Key(DEFAULTK); \ + template <> VAL EST_THash::Dummy_Value(DEFAULT); + +#define Declare_THash(KEY, VAL) Declare_THash_T(KEY, VAL, KEY ## VAL) + +#define Declare_THash_Base(KEY, VAL, DEFAULTK, DEFAULT, ERROR) \ + Declare_THash_Base_T(KEY, VAL, DEFAULTK, DEFAULT, ERROR, KEY ## VAL) +#define Declare_THash_Class(KEY, VAL, DEFAULTK, DEFAULT, ERROR) \ + Declare_THash_Class_T(KEY, VAL, DEFAULTK, DEFAULT, ERROR, KEY ## VAL) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TIteratorI.h b/aeneas/cfw/speech_tools/instantiate/EST_TIteratorI.h new file mode 100644 index 00000000..36cff19e --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TIteratorI.h @@ -0,0 +1,59 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TITERATORI_H__ +#define __EST_TITERATORI_H__ + +/** Instantiate an iterator. + * + * @author Richard Caley + * @version $Id: EST_TIteratorI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#define Instantiate_TIterator_T(CONTAINER, IP, ENTRY, TAG) \ + template class EST_TIterator; \ + template class EST_TRwIterator; + +#define Declare_TIterator_T(CONTAINER, IP, ENTRY, TAG)\ + /* EMPTY */ + +#define Instantiate_TStructIterator_T(CONTAINER, IP, ENTRY, TAG) \ + template class EST_TStructIterator; \ + template class EST_TRwStructIterator; + +#define Declare_TIterator_T(CONTAINER, IP, ENTRY, TAG)\ + /* EMPTY */ + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TKVLI.h b/aeneas/cfw/speech_tools/instantiate/EST_TKVLI.h new file mode 100644 index 00000000..66219568 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TKVLI.h @@ -0,0 +1,123 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_KVL_I_H__ +#define __EST_KVL_I_H__ + +/** Instantiate rules for list template. + * + * @author Richard Caley + * @version $Id: EST_TKVLI.h,v 1.4 2006/07/19 21:52:12 awb Exp $ + */ + +#include "instantiate/EST_TListI.h" +#include "instantiate/EST_TIteratorI.h" + +// Instantiation Macros + +// the typedef is purely to get the type name through the following macro. + +#define Instantiate_KVL_T(KEY, VAL, TAG) \ + template class EST_TKVL; \ + template class EST_TKVI; \ + Instantiate_TIterator_T(KVL_ ## TAG ## _t, KVL_ ## TAG ## _t::IPointer_k, KEY, KVL_ ## TAG ##_kitt) \ + Instantiate_TStructIterator_T(KVL_ ## TAG ## _t, KVL_ ## TAG ## _t::IPointer, KVI_ ## TAG ## _t, KVL_ ## TAG ##_itt) \ + Instantiate_TIterator_T(KVL_ ## TAG ## _t, KVL_ ## TAG ## _t::IPointer, KVI_ ## TAG ## _t, KVL_ ## TAG ##_itt) \ + Instantiate_TList(KVI_ ## TAG ## _t) + +// template ostream & operator<<(ostream &s, EST_TKVI const &i); + +#define Instantiate_KVL(KEY, VAL) \ + Instantiate_KVL_T(KEY, VAL, KEY ## VAL) + +#define Declare_KVL_TN(KEY, VAL, MaxFree, TAG) \ + typedef EST_TKVI KVI_ ## TAG ## _t; \ + typedef EST_TKVL KVL_ ## TAG ## _t; \ + \ + static VAL TAG##_kv_def_val_s; \ + static KEY TAG##_kv_def_key_s; \ + \ + template <> VAL *EST_TKVL< KEY, VAL >::default_val=&TAG##_kv_def_val_s; \ + template <> KEY *EST_TKVL< KEY, VAL >::default_key=&TAG##_kv_def_key_s; \ + \ + Declare_TList_N(KVI_ ## TAG ## _t, MaxFree) +#define Declare_KVL_T(KEY, VAL, TAG) \ + Declare_KVL_TN(KEY, VAL, 0, TAG) + +#define Declare_KVL_Base_TN(KEY, VAL, DEFV, DEFK, MaxFree, TAG) \ + typedef EST_TKVI KVI_ ## TAG ## _t; \ + typedef EST_TKVL KVL_ ## TAG ## _t; \ + \ + static VAL TAG##_kv_def_val_s=DEFV; \ + static KEY TAG##_kv_def_key_s=DEFK; \ + \ + template <> VAL *EST_TKVL< KEY, VAL >::default_val=&TAG##_kv_def_val_s; \ + template <> KEY *EST_TKVL< KEY, VAL >::default_key=&TAG##_kv_def_key_s; \ + \ + Declare_TList_N(KVI_ ## TAG ## _t, MaxFree) +#define Declare_KVL_Base_T(KEY, VAL, DEFV, DEFK, TAG) \ + Declare_KVL_Base_TN(KEY, VAL, DEFV, DEFK, 0, TAG) + +#define Declare_KVL_Class_TN(KEY, VAL, DEFV, DEFK, MaxFree, TAG) \ + typedef EST_TKVI KVI_ ## TAG ## _t; \ + typedef EST_TKVL KVL_ ## TAG ## _t; \ + \ + static VAL TAG##_kv_def_val_s(DEFV); \ + static KEY TAG##_kv_def_key_s(DEFK); \ + \ + template <> VAL *EST_TKVL< KEY, VAL >::default_val=&TAG##_kv_def_val_s; \ + template <> KEY *EST_TKVL< KEY, VAL >::default_key=&TAG##_kv_def_key_s; \ + \ + Declare_TList_N(KVI_ ## TAG ## _t, MaxFree) +#define Declare_KVL_Class_T(KEY, VAL, DEFV, DEFK,TAG) \ + Declare_KVL_Class_TN(KEY, VAL, DEFV, DEFK, 0, TAG) + +#define Declare_KVL_N(KEY, VAL, MaxFree) \ + Declare_KVL_TN(KEY, VAL, MaxFree, KEY ## VAL) +#define Declare_KVL(KEY, VAL) \ + Declare_KVL_N(KEY, VAL, 0) + +#define Declare_KVL_Base_N(KEY, VAL, DEFV, DEFK, MaxFree) \ + Declare_KVL_Base_TN(KEY, VAL, DEFV, DEFK, , MaxFree, KEY ## VAL) +#define Declare_KVL_Base(KEY, VAL, DEFV, DEFK) \ + Declare_KVL_Base_N(KEY, VAL, DEFV, DEFK, 0) + +#define Declare_KVL_Class_N(KEY, VAL, DEFV, DEFK, MaxFree) \ + Declare_KVL_Class_TN(KEY, VAL, DEFV, DEFK, MaxFree, KEY ## VAL) +#define Declare_KVL_Class(KEY, VAL, DEFV, DEFK) \ + Declare_KVL_Class_N(KEY, VAL, DEFV, DEFK, 0) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TListI.h b/aeneas/cfw/speech_tools/instantiate/EST_TListI.h new file mode 100644 index 00000000..d47e5c14 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TListI.h @@ -0,0 +1,92 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TLIST_I_H__ +#define __EST_TLIST_I_H__ + +/** Instantiate rules for list template. + * + * @author Richard Caley + * @version $Id: EST_TListI.h,v 1.5 2006/07/19 21:52:12 awb Exp $ + */ + + +// Instantiation Macros + +#include + +using namespace std; + +#include "instantiate/EST_TIteratorI.h" + +#define Instantiate_TList_T_MIN(TYPE, TAG) \ + template class EST_TList< TLIST_ ## TAG ## _VAL >; \ + template class EST_TItem< TLIST_ ## TAG ## _VAL >; \ + template const char *error_name(EST_TList< TYPE > val); \ + Instantiate_TIterator_T( EST_TList, EST_TList::IPointer, TYPE, TList_ ## TAG ## _itt); + +#define Instantiate_TList_T(TYPE, TAG) \ + Instantiate_TList_T_MIN(TYPE, TAG) + +#define Instantiate_TList(TYPE) Instantiate_TList_T(TYPE, TYPE) + +#define Declare_TList_TN(TYPE,MaxFree,TAG) \ + typedef TYPE TLIST_ ## TAG ## _VAL; \ + template <> EST_TItem< TYPE > * EST_TItem< TYPE >::s_free=NULL; \ + template <> unsigned int EST_TItem< TYPE >::s_maxFree=MaxFree; \ + template <> unsigned int EST_TItem< TYPE >::s_nfree=0; +#define Declare_TList_T(TYPE,TAG) \ + Declare_TList_TN(TYPE,0,TAG) + +#define Declare_TList_Base_TN(TYPE,MaxFree,TAG) \ + Declare_TList_TN(TYPE,MaxFree,TAG) +#define Declare_TList_Base_T(TYPE,TAG) \ + Declare_TList_Base_TN(TYPE,0,TAG) \ + +#define Declare_TList_Class_TN(TYPE,MaxFree,TAG) \ + Declare_TList_TN(TYPE,MaxFree,TAG) +#define Declare_TList_Class_T(TYPE,TAG) \ + Declare_TList_Class_TN(TYPE,0,TAG) \ + +#define Declare_TList_N(TYPE,MaxFree) Declare_TList_TN(TYPE,MaxFree,TYPE) +#define Declare_TList_Base_N(TYPE,MaxFree) Declare_TList_Base_TN(TYPE,MaxFree,TYPE) +#define Declare_TList_Class_N(TYPE,MaxFree) Declare_TList_Class_TN(TYPE,MaxFree,TYPE) + +#define Declare_TList(TYPE) Declare_TList_N(TYPE,0) +#define Declare_TList_Base(TYPE) Declare_TList_Base_N(TYPE,0) +#define Declare_TList_Class(TYPE) Declare_TList_Class_N(TYPE,0) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TMatrixI.h b/aeneas/cfw/speech_tools/instantiate/EST_TMatrixI.h new file mode 100644 index 00000000..a4a9b4ed --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TMatrixI.h @@ -0,0 +1,62 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TMATRIX_I_H__ +#define __EST_TMATRIX_I_H__ + +/** Instantiate rules for matrix template. + * + * @author Richard Caley + * @version $Id: EST_TMatrixI.h,v 1.3 2006/07/19 21:52:12 awb Exp $ + */ + + + + +// Instantiation Macros + +#define Instantiate_TMatrix(TYPE) \ + template class EST_TMatrix< TYPE >; + +#define Declare_TMatrix_T(TYPE,TAG) +#define Declare_TMatrix_Base_T(TYPE,DEFAULT,ERROR,TAG) + +#define Declare_TMatrix_Class_T(TYPE,DEFAULT,ERROR,TAG) + +#define Declare_TMatrix(TYPE) Declare_TMatrix_T(TYPE,TYPE) +#define Declare_TMatrix_Base(TYPE,DEFAULT,ERROR) Declare_TMatrix_Base_T(TYPE,DEFAULT,ERROR,TYPE) +#define Declare_TMatrix_Class(TYPE,DEFAULT,ERROR) Declare_TMatrix_Class_T(TYPE,DEFAULT,ERROR,TYPE) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TNamedEnumI.h b/aeneas/cfw/speech_tools/instantiate/EST_TNamedEnumI.h new file mode 100644 index 00000000..4090e5bf --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TNamedEnumI.h @@ -0,0 +1,157 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TNamedEnum_I_H__ +#define __EST_TNamedEnum_I_H__ + +/** Instantiate rules for named enum template. + * + * @author Richard Caley + * @version $Id: EST_TNamedEnumI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +// Instantiation Macros + +#define Instantiate_TValuedEnumI_T(ENUM, VAL, INFO, TAG) \ + template class EST_TValuedEnumI< ENUM, VAL, INFO >; \ + + +#define Instantiate_TValuedEnum_T(ENUM, VAL, TAG) \ + Instantiate_TValuedEnumI_T(ENUM, VAL, NO_INFO, TAG) + +#define Instantiate_TNamedEnumI_T(ENUM, INFO, TAG) \ + template class EST_TNamedEnumI< ENUM, INFO >; \ + Instantiate_TValuedEnumI_T(ENUM, const char *, INFO, TAG) + +#define Instantiate_TNamedEnum_T(ENUM, TAG) \ + Instantiate_TValuedEnumI_T(ENUM, const char *, NO_INFO, TAG) \ + template class EST_TNamedEnum< ENUM >; \ + template class EST_TNamedEnumI< ENUM, NO_INFO >; + + +#define Instantiate_TValuedEnumI(ENUM, VAL, INFO) \ + Instantiate_TValuedEnumI_T(ENUM, VAL, INFO, ENUM ## VAL ## INFO ) + +#define Instantiate_TValuedEnum(ENUM, VAL) \ + template class EST_TValuedEnum< ENUM, VAL >; \ + Instantiate_TValuedEnum_T(ENUM, VAL, ENUM ## VAL) + + +#define Instantiate_TNamedEnumI(ENUM, INFO) \ + Instantiate_TNamedEnumI_T(ENUM, INFO, ENUM ## INFO) + +#define Instantiate_TNamedEnum(ENUM) \ + Instantiate_TNamedEnum_T(ENUM, ENUM) \ + +// declaration macros. NULL at the moment. + +#define Declare_TValuedEnumI_T(ENUM, VAL, INFO, TAG) \ + /* EMPTY */ + +#define Declare_TValuedEnum_T(ENUM, VAL, TAG) \ + Declare_TValuedEnumI_T(ENUM, VAL, NO_INFO, TAG) + +#define Declare_TNamedEnumI_T(ENUM, INFO, TAG) \ + Declare_TValuedEnumI_T(ENUM, const char *, INFO, TAG) + +#define Declare_TNamedEnum_T(ENUM, TAG) \ + Declare_TNamedEnumI_T(ENUM, NO_INFO, TAG) + + +#define Declare_TValuedEnumI(ENUM, VAL, INFO) \ + Declare_TValuedEnumI_T(ENUM, VAL, INFO, ENUM ## VAL ## INFO ) + +#define Declare_TValuedEnum(ENUM, VAL) \ + Declare_TValuedEnum_T(ENUM, VAL, ENUM ## VAL) + +#define Declare_TNamedEnumI(ENUM, INFO) \ + Declare_TNamedEnumI_T(ENUM, INFO, ENUM ## INFO) + +#define Declare_TNamedEnum(ENUM) \ + Declare_TNamedEnum_T(ENUM, ENUM) + +// Actual table declaration macros + +#define Create_TValuedEnumDefinition(ENUM, VAL, INFO, TAG) \ + static EST_TValuedEnumDefinition< ENUM, VAL, INFO> TAG ## _names[] + +#define Create_TNamedEnumDefinition(ENUM, INFO, TAG) \ + Create_TValuedEnumDefinition(ENUM, const char *, INFO, TAG) + +#define Start_TValuedEnumI_T(ENUM, VAL, INFO, NAME, TAG) \ + Create_TValuedEnumDefinition(ENUM, VAL, INFO, TAG) = { +#define End_TValuedEnumI_T(ENUM, VAL, INFO, NAME, TAG) \ + }; \ + EST_TValuedEnumI< ENUM, VAL, INFO > NAME (TAG ## _names); + +#define Start_TNamedEnumI_T(ENUM, INFO, NAME, TAG) \ + Create_TValuedEnumDefinition(ENUM, const char *, INFO, TAG) = { +#define End_TNamedEnumI_T(ENUM, INFO, NAME, TAG) \ + }; \ + EST_TNamedEnumI< ENUM, INFO > NAME (TAG ## _names); + +#define Start_TValuedEnumI(ENUM, VAL, INFO, NAME) \ + Start_TValuedEnumI_T(ENUM, VAL, INFO, NAME, NAME) +#define End_TValuedEnumI(ENUM, VAL, INFO, NAME) \ + End_TValuedEnumI_T(ENUM, VAL, INFO, NAME, NAME) + +#define Start_TNamedEnumI(ENUM, INFO, NAME) \ + Start_TNamedEnumI_T(ENUM, INFO, NAME, NAME) +#define End_TNamedEnumI(ENUM, INFO, NAME) \ + End_TNamedEnumI_T(ENUM, INFO, NAME, NAME) + +#define Start_TValuedEnum_T(ENUM, VAL, NAME, TAG) \ + Create_TValuedEnumDefinition(ENUM, VAL, NO_INFO, TAG) = { +#define End_TValuedEnum_T(ENUM, VAL, NAME, TAG) \ + }; \ + EST_TValuedEnum< ENUM, VAL > NAME (TAG ## _names); + +#define Start_TNamedEnum_T(ENUM, NAME, TAG) \ + Create_TValuedEnumDefinition(ENUM, const char *, NO_INFO, TAG) = { +#define End_TNamedEnum_T(ENUM, NAME, TAG) \ + }; \ + EST_TNamedEnum< ENUM > NAME (TAG ## _names); + +#define Start_TValuedEnum(ENUM, VAL, NAME) \ + Start_TValuedEnum_T(ENUM, VAL, NAME, NAME) +#define End_TValuedEnum(ENUM, VAL, NAME) \ + End_TValuedEnum_T(ENUM, VAL, NAME, NAME) + +#define Start_TNamedEnum(ENUM, NAME) \ + Start_TNamedEnum_T(ENUM, NAME, NAME) +#define End_TNamedEnum(ENUM, NAME) \ + End_TNamedEnum_T(ENUM, NAME, NAME) +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TSimpleMatrixI.h b/aeneas/cfw/speech_tools/instantiate/EST_TSimpleMatrixI.h new file mode 100644 index 00000000..3ef389fb --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TSimpleMatrixI.h @@ -0,0 +1,52 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TSIMPLEMATRIX_I_H__ +#define __EST_TSIMPLEMATRIX_I_H__ + +/** Instantiate rules for simple-matrix template. + * + * @author Richard Caley + * @version $Id: EST_TSimpleMatrixI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#define Instantiate_TSimpleMatrix(TYPE) \ + template class EST_TSimpleMatrix; + +#define Declare_TSimpleMatrix_T(TYPE,TAG) + +#define Declare_TSimpleMatrix(TYPE) Declare_TSimpleMatrix_T(TYPE,TYPE) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TSimpleVectorI.h b/aeneas/cfw/speech_tools/instantiate/EST_TSimpleVectorI.h new file mode 100644 index 00000000..72b64c57 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TSimpleVectorI.h @@ -0,0 +1,52 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TSIMPLEVECTOR_I_H__ +#define __EST_TSIMPLEVECTOR_I_H__ + +/** Instantiate rules for simple-vector template. + * + * @author Richard Caley + * @version $Id: EST_TSimpleVectorI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#define Instantiate_TSimpleVector(TYPE) \ + template class EST_TSimpleVector; \ + +#define Declare_TSimpleVector_T(TYPE,TAG) + +#define Declare_TSimpleVector(TYPE) Declare_TSimpleVector_T(TYPE,TYPE) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TSortableI.h b/aeneas/cfw/speech_tools/instantiate/EST_TSortableI.h new file mode 100644 index 00000000..66955e03 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TSortableI.h @@ -0,0 +1,75 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TSORTABLE_I_H__ +#define __EST_TSORTABLE_I_H__ + +/** Instantiate rules for sortable template. + * + * @author Richard Caley + * @version $Id: EST_TSortableI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + + +// Instantiation Macros + +#define Instantiate_TSortable_T(TYPE, TAG) \ + template class EST_TSortable; \ + template class EST_TSortable; \ + \ + template void sort(EST_TList &, bool (*gt)(const EST_UItem *, const EST_UItem *)); \ + template void qsort(EST_TList &, bool (*gt)(const EST_UItem *, const EST_UItem *)); \ + template void ptr_sort(EST_TList &a); \ + template void ptr_qsort(EST_TList &a); \ + template void sort_unique(EST_TList &a); \ + template void merge_sort_unique(EST_TList &l, EST_TList &m); \ + \ + template bool operator==(const EST_TList &a, const EST_TList &b); + +#define Instantiate_TSortable(TYPE) Instantiate_TSortable_T(TYPE, TYPE) + +#define Declare_TSortable_T(TYPE,TAG) + +#define Declare_TSortable_Base_T(TYPE,DEFAULT,ERROR,TAG) + +#define Declare_TSortable_Class_T(TYPE,DEFAULT,ERROR,TAG) + +#define Declare_TSortable(TYPE) Declare_TSortable_T(TYPE,TYPE) + +#define Declare_TSortable_Base(TYPE,DEFAULT,ERROR) Declare_TSortable_Base_T(TYPE,DEFAULT,ERROR,TYPE) +#define Declare_TSortable_Class(TYPE,DEFAULT,ERROR) Declare_TSortable_Class_T(TYPE,DEFAULT,ERROR,TYPE) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TStringHashI.h b/aeneas/cfw/speech_tools/instantiate/EST_TStringHashI.h new file mode 100644 index 00000000..e64ffcb7 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TStringHashI.h @@ -0,0 +1,80 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TSTRINGHASH_I_H__ +#define __EST_TSTRINGHASH_I_H__ + +/** Instantiate rules for hash template. + * + * @author Richard Caley + * @version $Id: EST_TStringHashI.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +// Instantiation Macros + +#define Instantiate_TStringHash_T_IT(VAL, TAG, IP) \ + Instantiate_THash_T_IT(EST_String, VAL, TAG, IP) + +#define Instantiate_TStringHash_T_MIN(VAL, TAG) \ + template class EST_TStringHash< VAL >; \ + Instantiate_THash_T_MIN(EST_String,VAL,TAG) + +#define Instantiate_TStringHash_T(VAL, TAG) \ + template class EST_TStringHash< VAL >; \ + Instantiate_THash_T(EST_String,VAL,TAG) + +#define Instantiate_TStringHash(VAL) Instantiate_TStringHash_T(VAL, KEY ## VAL) +#define Instantiate_TStringHash_MIN(VAL) Instantiate_TStringHash_T_MIN(VAL, KEY ## VAL) +#define Instantiate_TStringHash_IT(VAL, IP) Instantiate_TStringHash_T_IT(VAL, KEY ## VAL, IP) + +#define Declare_TStringHash_T(VAL, TAG) \ + Declare_THash_T(EST_String, VAL, TAG) + +#define Declare_TStringHash_Base_T(VAL, DEFAULT, ERROR,TAG) \ + Declare_THash_Base_T(EST_String, VAL, "DUMMY", DEFAULT, ERROR,TAG) + + +#define Declare_TStringHash_Class_T(VAL, DEFAULT, ERROR,TAG) \ + Declare_THash_Class_T(EST_String, VAL, "DUMMY", DEFAULT, ERROR,TAG) + +#define Declare_TStringHash(VAL) Declare_TStringHash_T(VAL, VAL) + +#define Declare_TStringHash_Base(VAL, DEFAULT, ERROR) \ + Declare_TStringHash_Base_T(VAL, DEFAULT, ERROR, VAL) +#define Declare_TStringHash_Class(VAL, DEFAULT, ERROR) \ + Declare_TStringHash_Class_T(VAL, DEFAULT, ERROR, VAL) + +#endif + diff --git a/aeneas/cfw/speech_tools/instantiate/EST_TVectorI.h b/aeneas/cfw/speech_tools/instantiate/EST_TVectorI.h new file mode 100644 index 00000000..7c08d428 --- /dev/null +++ b/aeneas/cfw/speech_tools/instantiate/EST_TVectorI.h @@ -0,0 +1,83 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_TVECTOR_I_H__ +#define __EST_TVECTOR_I_H__ + +/** Instantiate rules for vector template. + * + * @author Richard Caley + * @version $Id: EST_TVectorI.h,v 1.4 2006/07/19 21:52:12 awb Exp $ + */ + + + + +// Instantiation Macros + +#define Instantiate_TVector_T_MIN(TYPE,TAG) \ + template class EST_TVector< TYPE >; + +#define Instantiate_TVector_T(TYPE,TAG) \ + Instantiate_TVector_T_MIN(TYPE,TAG) + +#define Instantiate_TVector(TYPE) Instantiate_TVector_T(TYPE,TYPE) + +#define Declare_TVector_T(TYPE,TAG) \ + static TYPE const TAG##_vec_def_val_s; \ + static TYPE TAG##_vec_error_return_s; \ + \ + template <> TYPE const *EST_TVector< TYPE >::def_val=&TAG##_vec_def_val_s; \ + template <> TYPE *EST_TVector< TYPE >::error_return=&TAG##_vec_error_return_s; + +#define Declare_TVector_Base_T(TYPE,DEFAULT,ERROR,TAG) \ + static TYPE const TAG##_vec_def_val_s=DEFAULT; \ + static TYPE TAG##_vec_error_return_s=ERROR; \ + \ + template <> TYPE const *EST_TVector::def_val=&TAG##_vec_def_val_s; \ + template <> TYPE *EST_TVector::error_return=&TAG##_vec_error_return_s; + +#define Declare_TVector_Class_T(TYPE,DEFAULT,ERROR,TAG) \ + static TYPE const TAG##_vec_def_val_s(DEFAULT); \ + static TYPE TAG##_vec_error_return_s(ERROR); \ + \ + template <> TYPE const *EST_TVector::def_val=&TAG##_vec_def_val_s; \ + template <> TYPE *EST_TVector::error_return=&TAG##_vec_error_return_s; + +#define Declare_TVector(TYPE) Declare_TVector_T(TYPE,TYPE) +#define Declare_TVector_Base(TYPE,DEFAULT,ERROR) Declare_TVector_Base_T(TYPE,DEFAULT,ERROR,TYPE) +#define Declare_TVector_Class(TYPE,DEFAULT,ERROR) Declare_TVector_Class_T(TYPE,DEFAULT,ERROR,TYPE) + +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_FeatureFunctionPackage.h b/aeneas/cfw/speech_tools/ling_class/EST_FeatureFunctionPackage.h new file mode 100644 index 00000000..feb59ba8 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_FeatureFunctionPackage.h @@ -0,0 +1,74 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_FEATUREFUNCTIONPACKAGE_H__ +#define __EST_FEATUREFUNCTIONPACKAGE_H__ + +/** Represents a named collection of named functions. + * + * @author Richard Caley + * @version $Id: EST_FeatureFunctionPackage.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#include "ling_class/EST_Item.h" +#include "EST_THash.h" + + +class EST_FeatureFunctionPackage { +public: + struct Entry + { + EST_Item_featfunc func; + }; +private: + const EST_String p_name; + EST_TStringHash p_entries; + + +public: + EST_FeatureFunctionPackage(const EST_String name, int n); + EST_FeatureFunctionPackage(const char *name, int n); + + const EST_String name(void) { return p_name; } + + void register_func(const EST_String &name, const EST_Item_featfunc func); + + const Entry &lookup(const EST_String &name, int &found) const; + + const EST_String lookup(const EST_Item_featfunc func, int &found) const; +}; + +int operator == (const EST_FeatureFunctionPackage::Entry &a, const EST_FeatureFunctionPackage::Entry &b); +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Item.h b/aeneas/cfw/speech_tools/ling_class/EST_Item.h new file mode 100644 index 00000000..e1159250 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Item.h @@ -0,0 +1,448 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1998 */ +/* --------------------------------------------------------------------- */ +/* */ +/* General class for representing linguistic information within a */ +/* EST_Relation. This consists of two parts, the relation specific */ +/* part and the information content part. The information content */ +/* part may be shared between multiple EST_Items. */ +/* */ +/* This is typically used to represent things like words, phones but */ +/* more abstract entities like NPs and nodes in metrical trees. */ +/* */ +/*************************************************************************/ +#ifndef __EST_ITEM_H__ +#define __EST_ITEM_H__ + +#include "EST_String.h" +#include "EST_Features.h" +#include "ling_class/EST_Item_Content.h" + +typedef EST_Val (*EST_Item_featfunc)(EST_Item *s); +extern val_type val_type_featfunc; +const EST_Item_featfunc featfunc(const EST_Val &v); +EST_Val est_val(const EST_Item_featfunc f); + +class EST_Relation; +class ling_class_init; + + +/** A class for containing individual linguistic objects such as +words or phones. + +These contain two types of information. This first is specific to the +\Ref{EST_Relation} we are viewing this ling item from, the second part +consists of a set of features. These features may be shared by +instances of this ling item in different EST_Relation within the same EST_Utterances + +The shared part of an EST_Item is +represented by the class EST_Item_Content. It should not normally be +accessed by the general users as reverse links from the contents to +each of the EST_Items it is part of are held ensure the +integrity of the structures. Changing these without maintain the +appropriate links is unlikely to be stable. + +We believe this structure is the most efficient for the most natural +use we envisage. Traversal of the items .... + +*/ + +class EST_Item +{ + private: + EST_Item_Content *p_contents; + EST_Relation *p_relation; + // In general (when we need it) + // EST_TKVL arcs; + // but specifically + EST_Item *n; + EST_Item *p; + EST_Item *u; + EST_Item *d; + + void unref_contents(); + void ref_contents(); + void copy(const EST_Item &s); + + // Internal manipulation functions + // Get the daughters of node, removing reference to them + EST_Item *grab_daughters(void); + /* Get the contents, removing reference to them, this doesn't + delete the contents if this item is the only reference */ + EST_Item_Content *grab_contents(void); + +protected: + static void class_init(void); + + public: + /**@name Constructor Functions */ + //@{ + /// Default constructor + EST_Item(); + /// Copy constructor only makes reference to contents + EST_Item(const EST_Item &item); + /// Includes reference to relation + EST_Item(EST_Relation *rel); + /// Most common form of construction + EST_Item(EST_Relation *rel, EST_Item *si); + /// Deletes it and references to it in its contents + ~EST_Item(); + //@} + + + /**@name Feature access functions. + These functions are wrap-around functions to the basic access + functions in the \Ref{EST_Features} class. In all these + functions, if the optional argument m} is set to 1, an + error is thrown if the feature does not exist*/ + + //@{ + /** return the value of the feature name + cast as a float */ + const float F(const EST_String &name) const {return f(name).Float();} + + /** return the value of the feature name cast + as a float, returning def if not found.*/ + const float F(const EST_String &name,float def) const + {return f(name,def).Float();} + + /** return the value of the feature name + cast as a EST_String */ + const EST_String S(const EST_String &name) const {return f(name).string();} + + /** return the value of the feature name + cast as a EST_String, + returning def if not found. + */ + const EST_String S(const EST_String &name, const EST_String &def) const + {return f(name, def).string();} + + /** return the value of the feature name + cast as a int */ + const int I(const EST_String &name) const {return f(name).Int();} + + /** return the value of the feature name cast as a int + returning def if not found.*/ + const int I(const EST_String &name, int def) const + {return f(name, def).Int();} + + /** return the value of the feature name + cast as a EST_Features */ + EST_Features &A(const EST_String &name) const {return *feats(f(name));} + + /** return the value of the feature name + cast as a EST_Features, + returning def if not found. + */ + EST_Features &A(const EST_String &name,EST_Features &def) const + {EST_Features *ff = new EST_Features(def); + return *feats(f(name, est_val(ff)));} + //@} + + /**@name Feature setting functions. + A separate function is provided for each permissible value type + */ + //@{ + /** set feature name to val */ + void set(const EST_String &name, int ival) + { EST_Val pv(ival);features().set_path(name, pv); } + + /** set feature name to val */ + void set(const EST_String &name, float fval) + { EST_Val pv(fval); features().set_path(name,pv); } + + /** set feature name to val */ + void set(const EST_String &name, double fval) + { EST_Val pv((float)fval);features().set_path(name,pv); } + + /** set feature name to val */ + void set(const EST_String &name, const EST_String &sval) + { EST_Val pv(sval);features().set_path(name,pv); } + + /** set feature name to val */ + void set(const EST_String &name, const char *cval) + { EST_Val pv(cval);features().set_path(name,pv); } + + /** set feature name to val, + a function registered in + the feature function list. */ + void set_function(const EST_String &name, const EST_String &funcname) + { features().set_function(name,funcname); } + + /** set feature name to f, + a set of features, which is copied into the object. + */ + void set(const EST_String &name, EST_Features &f) + { EST_Features *ff = new EST_Features(f); + features().set_path(name, est_val(ff)); } + + /** set feature name to f, + whose type is EST_Val. + */ + void set_val(const EST_String &name, const EST_Val &sval) + { features().set_path(name,sval); } + //@} + + /**@name Utility feature functions + */ + //@{ + /** remove feature name */ + void f_remove(const EST_String &name) + { features().remove(name); } + + /** find all the attributes whose values are functions, and + replace them with their evaluation. */ + void evaluate_features(); + + /** TRUE if feature is present, FALSE otherwise */ + int f_present(const EST_String &name) const + {return features().present(name); } + + // Number of items (including this) until no next item. + int length() const; + //@} + + // get contents from item + EST_Item_Content *contents() const { return (this == 0) ? 0 : p_contents;} + // used by tree manipulation functions + void set_contents(EST_Item_Content *li); + + // These should be deleted. + // The item's name + const EST_String name() const + { return (this == 0) ? EST_String::Empty : f("name",0).string(); } + + // Set item's name + void set_name(const EST_String &name) const + { if (this != 0) p_contents->set_name(name); } + + // Shouldn't normally be needed, except for iteration + EST_Features &features() const { return p_contents->f; } + + const EST_Val f(const EST_String &name) const + { + EST_Val v; + if (this == 0) + { + EST_error("item is null so has no %s feature",(const char *)name); + } + else + { + for (v=p_contents->f.val_path(name); + v.type() == val_type_featfunc && featfunc(v) != NULL; + v=(featfunc(v))((EST_Item *)(void *)this)); + if (v.type() == val_type_featfunc) + EST_error("NULL %s function",(const char *)name); + } + return v; + } + +#if 0 + const EST_Val &f(const EST_String &name, const EST_Val &def) const + { + if (this == 0) + return def; + else + { + const EST_Val *v; + for (v=&(p_contents->f.val_path(name, def)); + v->type() == val_type_featfunc && featfunc(*v) != NULL; + v=&(featfunc(*v))((EST_Item *)(void *)this)); + if (v->type() == val_type_featfunc) + v = &def; + return *v; + } + } +#endif + + const EST_Val f(const EST_String &name, const EST_Val &def) const + { + if (this == 0) + return def; + else + { + EST_Val v; + for (v=p_contents->f.val_path(name, def); + v.type() == val_type_featfunc && featfunc(v) != NULL; + v=(featfunc(v))((EST_Item *)(void *)this)); + if (v.type() == val_type_featfunc) + v = def; + return v; + } + } + + /**@name Cross relational access */ + //@{ + + /// View item from another relation (const char *) method + EST_Item *as_relation(const char *relname) const + { return (this == 0) ? 0 : p_contents->Relation(relname); } + + /// TRUE if this item is in named relation + int in_relation(const EST_String &relname) const + { return (this == 0) ? 0 : p_contents->in_relation(relname); } + + /// Access to the relation links + EST_TKVL &relations() {return p_contents->relations;} + + /// The relation name of this particular item + const EST_String &relation_name() const; + + /// The relation of this particular item + EST_Relation *relation(void) const + { return (this == 0) ? 0 : p_relation; } + + /// True if li is the same item ignoring its relation viewpoint + int same_item(const EST_Item *li) const + { return contents() && li->contents() && (contents() == li->contents()); } + //@} + + // The remaining functions should not be accessed, they are should be + // regarded as private member functions + + // Splice together a broken list. + + static void splice(EST_Item *a, EST_Item *b) + { if(a !=NULL) a->n = b; if (b != NULL) b->p=a; } + + // Internal traversal - nnot recommended - use relation traversal functions + // + EST_Item *next() const { return this == 0 ? 0 : n; } + // + EST_Item *prev() const { return this == 0 ? 0 : p; } + // + EST_Item *down() const { return this == 0 ? 0 : d; } + // + EST_Item *up() const { return this == 0 ? 0 : u; } + // Last item (most next) at this level + EST_Item *last() const; + // First item (most prev) at this level + EST_Item *first() const; + // Highest (most up) + EST_Item *top() const; + // Lowest (most down) + EST_Item *bottom() const; + // First item which has no down, within the descendants of this item + EST_Item *first_leaf() const; + // Next item which has no down, following above this item if necessary + EST_Item *next_leaf() const; + // Last item which has no down, following above this item if necessary + EST_Item *last_leaf() const; + // Next item in pre order (root, daughters, siblings) + EST_Item *next_item() const; + + + // Insert a new item after this, with li's contents + EST_Item *insert_after(EST_Item *li=0); + // Insert a new item before this, with li's contents + EST_Item *insert_before(EST_Item *li=0); + // Insert a new item below this, with li's contents (see tree methods) + EST_Item *insert_below(EST_Item *li=0); + // Insert a new item above this, with li's contents (see tree methods) + EST_Item *insert_above(EST_Item *li=0); + + // Append a new daughter to this, with li's contents + EST_Item *append_daughter(EST_Item *li=0); + // Prepend a new daughter to this, with li's contents + EST_Item *prepend_daughter(EST_Item *li=0); + // Insert a new parent above this, with li's contents + EST_Item *insert_parent(EST_Item *li=0); + + // Delete this item and all its occurrences in other relations + void unref_all(); + + // Verification, double links are consistent (used after reading in) + int verify() const; + + friend int i_same_item(const EST_Item *l1,const EST_Item *l2); + friend int move_item(EST_Item *from, EST_Item *to); + friend int merge_item(EST_Item *from, EST_Item *to); + friend int move_sub_tree(EST_Item *from, EST_Item *to); + friend int exchange_sub_trees(EST_Item *from,EST_Item *to); + + EST_Item &operator=(const EST_Item &s); + friend ostream& operator << (ostream &s, const EST_Item &a); + friend bool operator !=(const EST_Item &a, const EST_Item &b) + { return !i_same_item(&a,&b); } + friend bool operator ==(const EST_Item &a, const EST_Item &b) + { return i_same_item(&a,&b); } + + friend class EST_Relation; + friend class ling_class_init; +}; + +inline int i_same_item(const EST_Item *l1,const EST_Item *l2) +{ + return l1->contents() && l2->contents() && + (l1->contents() == l2->contents()); +} + + +inline EST_Item *as(const EST_Item *n,const char *relname) + { return n->as_relation(relname); } + +// Relation structure functions +#include "ling_class/EST_Relation_list.h" +#include "ling_class/EST_Relation_tree.h" +#include "ling_class/EST_Relation_mls.h" + +inline EST_Item *next_item(const EST_Item *node) + { return node->next_item(); } + +void remove_item(EST_Item *l, const char *relname); + +void copy_node_tree(EST_Item *from, EST_Item *to); +void copy_node_tree_contents(EST_Item *from, EST_Item *to); + +void evaluate(EST_Item *a,EST_Features &f); + + +#include "ling_class/EST_FeatureFunctionPackage.h" + +// Feature function support +void EST_register_feature_function_package(const char *name, void (*init_fn)(EST_FeatureFunctionPackage &p)); + +void register_featfunc(const EST_String &name, const EST_Item_featfunc func); +const EST_Item_featfunc get_featfunc(const EST_String &name,int must=0); +EST_String get_featname(const EST_Item_featfunc func); + +#define EST_register_feature_functions(PACKAGE) \ + do { \ + extern void register_ ## PACKAGE ## _feature_functions(EST_FeatureFunctionPackage &p); \ + EST_register_feature_function_package( #PACKAGE , register_ ## PACKAGE ## _feature_functions); \ + } while(0) + + +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Item_Content.h b/aeneas/cfw/speech_tools/ling_class/EST_Item_Content.h new file mode 100644 index 00000000..0d88e2a4 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Item_Content.h @@ -0,0 +1,122 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : May 1998 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* The shared part of an EST_Item, containing the linguistic */ +/* contents, such as part of speech, stress etc. Basically holds a */ +/* list of feature value pairs are required. */ +/* */ +/* This class is effectively private to the EST_Item class and */ +/* shouldn't be referenced outside that clase */ +/* */ +/*=======================================================================*/ +#ifndef __EST_ITEM_CONTENT_H__ +#define __EST_ITEM_CONTENT_H__ + +#include "EST_String.h" +#include "EST_Features.h" + +VAL_REGISTER_CLASS_DCLS(icontent,EST_Item_Content) +VAL_REGISTER_CLASS_DCLS(item,EST_Item) +class EST_Item; + +/** A class for containing individual linguistic features and references +to relations. + +This class contents the potentially shared part of an \Ref{EST_Item}. +It contains a list of features allowing string names to be related to +string, floats, ints and arbitrary objects. It also contains a reference +list to the \Ref{EST_Item}s indexed by the relation names. + +This class should not normally be accessed by anyone other than the +\Ref{EST_Item}. + +*/ +class EST_Item_Content { + private: + void copy(const EST_Item_Content &x); + public: + + /**@name Constructor Functions */ + //@{ + /// Default constructor + EST_Item_Content() {} + /// Copy constructor + EST_Item_Content(const EST_Item_Content &content) { copy(content); } + /// destructor + ~EST_Item_Content(); + //@} + + /// General features for this item + EST_Features f; + + /** return the name of the item, e.g. the name of the phone or the + text of the word*/ + + // this should be changed back to a reference once we figure + // out how to do it. + const EST_String name() const {return f.S("name");} + + /// set name + void set_name(const EST_String &s) {f.set("name",s);} + + // Links to ling_items that share this information + EST_TKVL relations; + + EST_Item_Content& operator = (const EST_Item_Content& a); + friend ostream& operator << (ostream &s, const EST_Item_Content &a); + + /**@name Relation related member functions */ + //@{ + + EST_Item *Relation(const char *name) + { + EST_Item *d = 0; + return ::item(relations.val_def(name,est_val(d))); + } + + int in_relation(const EST_String &name) const + { return relations.present(name); } + // Unreference this ling_content from the named relation + int unref_relation(const EST_String &relname); + // Unreference this ling_content from all relations and delete + int unref_and_delete(); + //@} + + friend class EST_Item; +}; + +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Relation.h b/aeneas/cfw/speech_tools/ling_class/EST_Relation.h new file mode 100644 index 00000000..f7fa9407 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Relation.h @@ -0,0 +1,234 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1998 */ +/* --------------------------------------------------------------------- */ +/* another architecture */ +/* */ +/*************************************************************************/ +#ifndef __EST_RELATION_H__ +#define __EST_RELATION_H__ + +#include "EST_String.h" +#include "EST_TList.h" +#include "EST_TKVL.h" +#include "EST_THash.h" +#include "EST_Val.h" +#include "EST_types.h" +#include "EST_Token.h" +#include "EST_Features.h" +#include "ling_class/EST_Item.h" + +class EST_Utterance; + +class EST_Relation_Iterator; + +/** Relations are a container class for EST_Items. Three types of +relation structure are supported: + + + +Linear lists +Trees +Multi-linear structures as used +in autosegmental phonology etc + + +*/ + +class EST_Relation +{ + EST_String p_name; + EST_Utterance *p_utt; + EST_Item *p_head; + EST_Item *p_tail; // less meaningful in a tree + + EST_Item *get_item_from_name(EST_THash &inames,int name); + EST_Item *get_item_from_name(EST_TVector< EST_Item * > &inames,int name); + EST_write_status save_items(EST_Item *item, + ostream &outf, + EST_TKVL &contentnames, + EST_TKVL &itemnames, + int &node_count) const; + + static void node_tidy_up_val(int &k, EST_Val &v); + static void node_tidy_up(int &k, EST_Item *node); + + EST_read_status load_items(EST_TokenStream &ts, + const EST_THash &contents); + EST_read_status load_items(EST_TokenStream &ts, + const EST_TVector < EST_Item_Content * > &contents + ); + void copy(const EST_Relation &r); + public: + + /** default constructor */ + EST_Relation(); + /** Constructor which sets name of relation */ + EST_Relation(const EST_String &name); + /** Constructor which copies relation r */ + EST_Relation(const EST_Relation &r) { copy(r); } + /** default destructor */ + ~EST_Relation(); + + /** Features which belong to the relation rather than its items*/ + EST_Features f; + + /** Evaluate the relation's feature functions */ + //void evaluate_features(); + /** Evaluate the feature functions of all the items in the relation */ + void evaluate_item_features(); + + /** Clear the relation of items */ + void clear(); + + /** Return the EST_Utterance + to which this relation belongs */ + EST_Utterance *utt(void) { return p_utt; } + + /** Set the EST_Utterance + to which this relation belongs */ + void set_utt(EST_Utterance *u) { p_utt = u; } + + /** Return the name of the relation */ + const EST_String &name() const { return (this == 0) ? EST_String::Empty : p_name; } + + /** Return the head (first) item of the relation */ + EST_Item *head() const {return (this == 0) ? 0 : p_head;} + + /** Return the root item of the relation */ + EST_Item *root() const {return head();} + + /** Return the tail (last) item of the relation */ + EST_Item *tail() const {return (this == 0) ? 0 : p_tail;} + + // This have been replaced by Relation_Tree functions + EST_Item *first() const { return head(); } + EST_Item *first_leaf() const; + EST_Item *last() const { return tail(); } + EST_Item *last_leaf() const; + + /** Return the tail (last) item of the relation */ +// EST_Item *id(int i); + + /** number of items in this relation */ + int length() const; +// int num_nodes() const; +// int num_leafs() const; + /** return true if relation does not contain any items */ + int empty() const { return p_head == 0; } + + /** remove EST_Item item from relation */ + void remove_item(EST_Item *item); + + /** remove all occurrences of feature + name from relation's items + */ + void remove_item_feature(const EST_String &name); + + /** Load relation from file */ + EST_read_status load(const EST_String &filename, + const EST_String &type="esps"); + + /** Load relation from already open tokenstream */ +// EST_read_status load(EST_TokenStream &ts, +// const EST_THash &contents); + + /** Load relation from already open tokenstream */ + EST_read_status load(EST_TokenStream &ts, + const EST_TVector < EST_Item_Content * > &contents + ); + + /** Load relation from already open tokenstream */ + EST_read_status load(const EST_String &filename, + EST_TokenStream &ts, + const EST_String &type); + + /** Save relation to file */ + EST_write_status save(const EST_String &filename, + bool evaluate_ff = false) const; + + /** Save relation to file, evaluating all feature functions before hand */ + EST_write_status save(const EST_String &filename, + const EST_String &type, + bool evaluate_ff = false) const; + + /** Save relation from already open ostream */ + EST_write_status save(ostream &outf,EST_TKVL contents) const; + + /** Save relation from already open ostream */ + EST_write_status save(ostream &outf, + const EST_String &type, + bool evaluate_ff) const; + /** Iteration */ + typedef EST_Relation_Iterator Iterator; + + EST_Relation &operator=(const EST_Relation &s); + friend ostream& operator << (ostream &s, const EST_Relation &u); + + EST_Item *append(EST_Item *si); + EST_Item *append(); + EST_Item *prepend(EST_Item *si); + EST_Item *prepend(); + + friend class EST_Item; +}; + +VAL_REGISTER_CLASS_DCLS(relation,EST_Relation) + +inline bool operator==(const EST_Relation &a, const EST_Relation &b) + { return (&a == &b); } + +void copy_relation(const EST_Relation &from, EST_Relation &to); + +EST_Utterance *get_utt(EST_Item *s); + +class EST_Relation_Iterator +{ +private: + const EST_Relation &rel; + EST_Item *next; + +public: + EST_Relation_Iterator(const EST_Relation &r) + : rel(r), next(NULL) { reset();}; + + void reset() + { next=rel.head(); } + bool has_more_elements() + { return next != NULL; } + + EST_Item *next_element(); +}; + +typedef EST_TList EST_RelationList; + +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Relation_list.h b/aeneas/cfw/speech_tools/ling_class/EST_Relation_list.h new file mode 100644 index 00000000..afc014b2 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Relation_list.h @@ -0,0 +1,119 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1998 */ +/* --------------------------------------------------------------------- */ +/* Functions for LIST relations */ +/* */ +/*************************************************************************/ +#ifndef __EST_RELATION_LIST_H__ +#define __EST_RELATION_LIST_H__ + + +#if 0 +/**@name Functions for building and traversing list relations + */ + +//@{ +/**@name List traversal functions */ +//@{ + +/** return next item of n + */ +inline EST_Item *next(const EST_Item *n) { return n->next(); } + +/** return previous item of n + */ +inline EST_Item *prev(const EST_Item *n) { return n->prev(); } + +/** return last item in n's relation + */ +inline EST_Item *last(const EST_Item *n) { return n->last(); } + +/** return first item in n's relation + */ +inline EST_Item *first(const EST_Item *n) { return n->first(); } + +/** return next item of n as seen from relation +relname */ +inline EST_Item *next(const EST_Item *n,const char *relname) + { return next(as(n,relname)); } + +/** return previous item of n as seen from relation +relname */ +inline EST_Item *prev(const EST_Item *n,const char *relname) + { return prev(as(n,relname)); } + +/** return first item of n as seen from relation +relname */ +inline EST_Item *first(const EST_Item *n,const char *relname) + { return first(as(n,relname)); } + +/** return last item of n as seen from relation +relname */ +inline EST_Item *last(const EST_Item *n,const char *relname) + { return last(as(n,relname)); } + +#endif + +/** Given a node l, return true if + c after it in a list relation. */ +int in_list(const EST_Item *c, const EST_Item *l); + + +/** Add a item after node n, and return the new +item. If n is the first item in the list, the +new item becomes the head of the list, otherwise it is inserted between +n and it's previous current item. +If p is 0, make a new node for the new +item, otherwise add p to this relation as the +next item in n's relation. */ + +EST_Item *add_after(const EST_Item *n, EST_Item *p=0); + +/** Add a item before node n, and return the new +item. If n is the first item in the list, the +new item becomes the head of the list, otherwise it is inserted between +n and it's previous current item. +If p is 0, make a new node for the new +item, otherwise add p to this relation as the +previous item in n's relation. */ + +EST_Item *add_before(const EST_Item *n, EST_Item *p=0); + +/** Remove the given item. +*/ + +void remove_item_list(EST_Relation *rel, EST_Item *n); + +//@} +//@} +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Relation_mls.h b/aeneas/cfw/speech_tools/ling_class/EST_Relation_mls.h new file mode 100644 index 00000000..6c4efa2b --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Relation_mls.h @@ -0,0 +1,56 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1998 */ +/* --------------------------------------------------------------------- */ +/* Functions for Multi-linear structure relations */ +/* */ +/*************************************************************************/ +#ifndef __EST_RELATION_MLS_H__ +#define __EST_RELATION_MLS_H__ + +inline EST_Item *link1(EST_Item *n) { return n->down()->down()->down(); } +inline EST_Item *link2(EST_Item *n) + { return n->down()->down()->next()->down(); } +inline EST_Item *linkn(EST_Item *n) + { return n->down()->down()->last()->down(); } +EST_Item *link(int l,EST_Item *n); + +inline EST_Item *linkedfrom(EST_Item *n) + { return n->up()->first()->up()->up(); } +int linked(EST_Item *from, EST_Item *to); + +inline EST_Item *next_link(EST_Item *n) { return n->up()->next()->down(); } + +void add_link(EST_Item *from, EST_Item *to); +void remove_link(EST_Item *from, EST_Item *to); + +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Relation_tree.h b/aeneas/cfw/speech_tools/ling_class/EST_Relation_tree.h new file mode 100644 index 00000000..4de8e318 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Relation_tree.h @@ -0,0 +1,191 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1998 */ +/* All Rights Reserved. */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1998 */ +/* --------------------------------------------------------------------- */ +/* Functions for TREE relations */ +/* */ +/*************************************************************************/ +#ifndef __EST_RELATION_TREE_H__ +#define __EST_RELATION_TREE_H__ + +/**@name Functions for building and traversing tree relations + + */ +//@{ + +/**@name Tree traversal functions +*/ + +//@{ + +/// return parent of n +inline EST_Item *parent(const EST_Item *n) { return n->first()->up(); } + +/// return first daughter of n +inline EST_Item *daughter1(const EST_Item *n) { return n->down(); } + +/// return second daughter of n +inline EST_Item *daughter2(const EST_Item *n) { return n->down()->next(); } + +/// return nth daughter of n +EST_Item *daughtern(const EST_Item *n, int nth); + +/// return last daughter of n +inline EST_Item *daughtern(const EST_Item *n) { return n->down()->last(); } + +/// return next sibling (sister) of n +inline EST_Item *next_sibling(const EST_Item *n) { return n->next(); } + +/// return previous sibling (sister) of n +inline EST_Item *prev_sibling(const EST_Item *n) { return n->prev(); } + +/// return root node of treeprevious sibling (sister) of n +inline EST_Item *root(const EST_Item *n) { return n->top(); } + +/** return parent of n as seen from relation +relname */ +inline EST_Item *parent(const EST_Item *n,const char *relname) + { return parent(as(n,relname)); } + +//inline EST_Item *daughters(const EST_Item *n,const char *relname) +// { return daughters(as(n,relname)); } + +/** return first daughter of n as seen from relation + relname */ +inline EST_Item *daughter1(const EST_Item *n,const char *relname) + { return daughter1(as(n,relname)); } + +/** return second daughter of n as seen from relation + relname */ +inline EST_Item *daughter2(const EST_Item *n,const char *relname) + { return daughter2(as(n,relname)); } + +/** return last daughter of n as seen from relation + relname */ +inline EST_Item *daughtern(const EST_Item *n,const char *relname) + { return daughtern(as(n,relname)); } + +/** return next sibling (sister) of n as seen + from relation relname */ +inline EST_Item *next_sibling(const EST_Item *n,const char *relname) + { return next_sibling(as(n,relname)); } + +/** return previous sibling (sister) of n as seen + from relation relname */ +inline EST_Item *prev_sibling(const EST_Item *n,const char *relname) + { return prev_sibling(as(n,relname)); } + +/** return root of tree of n as seen from + relation relname */ +inline EST_Item *root(const EST_Item *n,const char *relname) + { return root(as(n,relname)); } + +// should be deleted. +EST_Item *first_leaf_in_tree(const EST_Item *root); + +// should be deleted. +EST_Item *last_leaf_in_tree(const EST_Item *root); + +/** return the first leaf (terminal node) which is dominated by + n. Note that this is different from daughter1 etc +as this descends the tree to find the leftmost terminal node (it +is like the transitive closure of daughter1). +*/ +inline EST_Item *first_leaf(const EST_Item *n) {return first_leaf_in_tree(n);} + +/** return the last leaf (terminal node) which is dominated by + n. Note that this is different from daughter1 etc +as this descends the tree to find the right terminal node (it is +like the transitive closure of daughtern). +*/ +inline EST_Item *last_leaf(const EST_Item *n) { return last_leaf_in_tree(n); } + +/** Return next leaf in tree given n. If +n is a terminal node, next_leaf() will return +the next leaf in the tree. If n is not +terminal, this will return the leftmost terminal node dominated by +n. This will return 0 only when the last leaf in +the relation has been passed. +*/ +inline EST_Item *next_leaf(const EST_Item *n) { return n->next_leaf(); } + +/** Return number of leaves (terminal nodes) under n + */ +int num_leaves(const EST_Item *n); + +/** Given a node t, return true if + c is under it in a tree */ +int in_tree(const EST_Item *c,const EST_Item *t); + +//@} + +/**@name Tree building functions */ +//@{ + +/** Add a daughter to node n, after any +existing daughters, and return the next daughter. If +p is 0, make a new node for the daughter, +otherwise add p to this relation as +n's daughter. */ + +EST_Item *append_daughter(EST_Item *n, EST_Item *p=0); + +/** Add a daughter to node n as seen from +relation relname, after any existing +daughters, and return the next daughter. If p +is 0, make a new node for the daughter, otherwise add +p to this relation as +n's daughter. */ + +EST_Item *append_daughter(EST_Item *n, const char *relname, EST_Item *p=0); + +/** Add a daughter to node n, before any +existing daughters, and return the next daughter. If +p is 0, make a new node for the daughter, +otherwise add p to this relation as +n's daughter. */ + +EST_Item *prepend_daughter(EST_Item *n, EST_Item *p=0); + +/** Add a daughter to node n as seen from +relation relname, before any existing +daughters, and return the next daughter. If p +is 0, make a new node for the daughter, otherwise add +p to this relation as +n's daughter. */ + +EST_Item *prepend_daughter(EST_Item *n, const char *relname, EST_Item *p=0); + +//@} + +//@} +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_Utterance.h b/aeneas/cfw/speech_tools/ling_class/EST_Utterance.h new file mode 100644 index 00000000..0b20900f --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_Utterance.h @@ -0,0 +1,163 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : May 1995 (redone May 1998) */ +/*-----------------------------------------------------------------------*/ +/* EST_Utterance Class header file */ +/* */ +/*=======================================================================*/ +#ifndef __Utterance_H__ +#define __Utterance_H__ + +#include "EST_String.h" +#include "EST_TList.h" +#include "ling_class/EST_Relation.h" +#include "ling_class/EST_Item.h" +#include "EST_Features.h" + +/** A class that contains EST_Items +and EST_Relations between them. +Used for holding interrelated linguistic structures. + +*/ + +class EST_Utterance{ +private: + void copy(const EST_Utterance &u); + int highest_id; +public: + /**@name Constructor and initialisation Functions */ + //@{ + /// default constructor + EST_Utterance(); + EST_Utterance(const EST_Utterance &u) { copy(u); } + ~EST_Utterance() {clear();} + //@} + /// + + /**@name Utility Functions */ + //@{ + /// initialise utterance + void init(); + + /// remove everything in utterance + void clear(); + + /// clear the contents of the relations only + void clear_relations(); + + /// set the next id to be n + void set_highest_id(int n) {highest_id=n;} + /// return the id of the next item + int next_id(); + //@} + + /**@name File i/o */ + //@{ + /** load an utterance from an ascii file + */ + EST_read_status load(const EST_String &filename); + /** load an utterance from a already opened token stream + */ + EST_read_status load(EST_TokenStream &ts); + + /** save an utterance to an ascii file + */ + EST_write_status save(const EST_String &filename, + const EST_String &type="est_ascii") const; + + /** save an utterance to an ostream + */ + EST_write_status save(ostream &outf,const EST_String &type) const; + //@} + + EST_Utterance &operator=(const EST_Utterance &s); + friend ostream& operator << (ostream &s, const EST_Utterance &u); + EST_Relation * operator() (const EST_String &name) + { return relation(name);} + + /** Utterance access + */ + //@{ + + /// Utterance level features + EST_Features f; + + /// Evaluate all feature functions in utterance + void evaluate_all_features(); + + /// The list of named relations + EST_Features relations; + + /// number of relations in this utterance + int num_relations() const { return relations.length(); } + + /** returns true if utterance contains named relations. + {\bf name} can be either a single string or a bracketed list + of strings e.g. "(Word Phone Syl)". */ + bool relation_present(const EST_String name) const; + + /** returns true if utterance contains all the relations + named in the list {\bf name}. */ + bool relation_present(EST_StrList &names) const; + + /// get relation by name + EST_Relation *relation(const char *name,int err_on_not_found=1) const; + + /// return EST_Item whose id is n. + EST_Item *id(const EST_String &n) const; + + /// create a new relation called n. + EST_Relation *create_relation(const EST_String &relname); + + /// remove the relation called n. + void remove_relation(const EST_String &relname); + + void sub_utterance(EST_Item *i); +}; + +void utt_2_flat_repr( const EST_Utterance &utt, + EST_String &flat_repr ); + +int utterance_merge(EST_Utterance &utt, + EST_Utterance &sub_utt, + EST_Item *utt_root, + EST_Item *sub_root); + +int utterance_merge(EST_Utterance &utt, + EST_Utterance &extra, + EST_String feature); + +void sub_utterance(EST_Utterance &sub,EST_Item *i); + +#endif diff --git a/aeneas/cfw/speech_tools/ling_class/EST_item_aux.h b/aeneas/cfw/speech_tools/ling_class/EST_item_aux.h new file mode 100644 index 00000000..1cf1cfce --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_item_aux.h @@ -0,0 +1,96 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_ITEM_AUX_H__ +#define __EST_ITEM_AUX_H__ + +/** Non core feature funtionality. + * + * @author Richard Caley + * @version $Id: EST_item_aux.h,v 1.3 2004/05/24 10:32:30 korin Exp $ + */ + +//@{ + +#include "EST_features_aux.h" + +class EST_Item; +class EST_String; + +/** Safe feature access functions. + * + * These functions are guaranteed to return a value even if + * there is an otherwise fatal error. + */ +//@{ + +/// Return the value as an EST_Val. +EST_Val getVal(const EST_Item &f, + const EST_String name, + const EST_Val &def, + EST_feat_status &s); + +/// Return the value as a string. +EST_String getString(const EST_Item &f, + const EST_String name, + const EST_String &def, + EST_feat_status &s); + +/// Return the values as a float. +float getFloat(const EST_Item &f, + const EST_String name, + const float &def, + EST_feat_status &s); + +/// Return the values as an integer. +int getInteger(const EST_Item &f, + const EST_String name, + const int &def, + EST_feat_status &s); +//@} + +float start(const EST_Item &item); +float mid(const EST_Item &item); +float time(const EST_Item &item); +float end(const EST_Item &item); + + +//@} + +//Rob's function for jumping around HRG structures more easily +EST_Item *item_jump(EST_Item *from, const EST_String &to); + + +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_item_content_aux.h b/aeneas/cfw/speech_tools/ling_class/EST_item_content_aux.h new file mode 100644 index 00000000..30759706 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_item_content_aux.h @@ -0,0 +1,90 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_ITEM_CONTENT_AUX_H__ +#define __EST_ITEM_CONTENT_AUX_H__ + +/** Non core feature funtionality. + * + * @author Richard Caley + * @version $Id: EST_item_content_aux.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +//@{ + +#include "EST_features_aux.h" + +class EST_Item_Content; +class EST_String; + +/** Safe feature access functions. + * + * These functions are guaranteed to return a value even if + * there is an otherwise fatal error. + */ +//@{ + +/// Return the value as an EST_Val. +EST_Val getVal(const EST_Item_Content &f, + const EST_String name, + const EST_Val &def, + EST_feat_status &s); + +/// Return the value as a string. +EST_String getString(const EST_Item_Content &f, + const EST_String name, + const EST_String &def, + EST_feat_status &s); + +/// Return the values as a float. +float getFloat(const EST_Item_Content &f, + const EST_String name, + const float &def, + EST_feat_status &s); + +/// Return the values as an integer. +int getInteger(const EST_Item &f, + const EST_String name, + const int &def, + EST_feat_status &s); +//@} + +float start(const EST_Item_Content &itemc); +float mid(const EST_Item_Content &itemc); +float time(const EST_Item_Content &itemc); +float end(const EST_Item_Content &itemc); + +//@} +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_relation_aux.h b/aeneas/cfw/speech_tools/ling_class/EST_relation_aux.h new file mode 100644 index 00000000..591a1c2a --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_relation_aux.h @@ -0,0 +1,142 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : May 1994 */ +/* Release: 0.9 */ +/*-----------------------------------------------------------------------*/ +/* Auxiliary Label Routines header file */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_RELATION_AUX_H__ +#define __EST_RELATION_AUX_H__ + +#include "EST_String.h" +#include "EST_Track.h" +#include "ling_class/EST_Utterance.h" +#include "EST_Option.h" +#include "EST_THash.h" + +typedef EST_TList EST_RelationList; +typedef EST_TStringHash EST_hashedRelationList; + +EST_String options_relation_filetypes(void); + + +void convert_to_broad(EST_Relation &seg, EST_StrList &pos_list, + EST_String broad_name ="", int polarity = 1); +void convert_to_broad_class(EST_Relation &seg, const EST_String &class_type, + EST_Option &options); + +int merge_label(EST_Relation &seg, const EST_String &labtype); + +void change_label(EST_Relation &seg, const EST_String &oname, const EST_String &nname); + +void merge_all_label(EST_Relation &seg, const EST_String &labtype); + +void track_to_label(const EST_Track &tr, EST_Relation &lab, float thresh=0.0); +void track_to_pm(const EST_Track &tr, int sample_rate, EST_Relation &lab); + +void label_to_track(const EST_Relation &lab, + const EST_Option &al, + const EST_Option &op, + EST_Track &tr); +void label_to_track(const EST_Relation &lab, EST_Track &tr, + float shift, float offset=0.0, + float range = 1.0, float req_length = -1.0, + const EST_String &pad="low"); + +void shift_label(EST_Relation &seg, float shift); +void label_map(EST_Relation &seg, EST_Option &map); +void quantize(EST_Relation &a, float q); +int edit_labels(EST_Relation &a, EST_String sedfile); + +void RelationList_select(EST_RelationList &mlf, EST_StrList filenames, bool + exact_match); +EST_Relation RelationList_extract(EST_RelationList &mlf, + const EST_String &filename, + bool base); +EST_Relation RelationList_combine(EST_RelationList &mlf); +EST_Relation RelationList_combine(EST_RelationList &mlf, EST_Relation &key); + +int relation_divide(EST_RelationList &mlf, EST_Relation &lab, + EST_Relation &keylab, EST_String ext); + +int relation_divide(EST_RelationList &mlf, EST_Relation &lab, + EST_Relation &keylab, + EST_StrList &list, EST_String ext); + +EST_Litem *RelationList_ptr_extract(EST_RelationList &mlf, + const EST_String &filename, + bool base); + +void relation_convert(EST_Relation &lab, EST_Option &al, EST_Option &op); + +EST_read_status load_RelationList(const EST_String &filename, + EST_RelationList &plist); +EST_write_status save_RelationList(const EST_String& filename, + const EST_RelationList &plist); +EST_write_status save_RelationList(const EST_String &filename, + const EST_RelationList &plist, + int time=1, int path = 1); +EST_write_status save_ind_RelationList(const EST_String &filename, + const EST_RelationList &plist, + const EST_String &features, + int path); + +EST_write_status save_WordList(const EST_String &filename, + const EST_RelationList &plist, + int n); + +EST_write_status save_SentenceList(EST_String filename, + EST_RelationList &plist, int n); + + + +EST_read_status read_RelationList(EST_RelationList &mlf, + EST_StrList &files, EST_Option &al); + +float start(EST_Item *n); +float duration(EST_Item *n); + +/// hashed relation lists for super speed +void build_RelationList_hash_table(EST_RelationList &mlf, + EST_hashedRelationList &hash_table, + const bool base); + +bool hashed_RelationList_extract(EST_Relation* &rel, + const EST_hashedRelationList &hash_table, + const EST_String &filename, bool base); + + +#endif /* __EST_RELATION_AUX_H__ */ diff --git a/aeneas/cfw/speech_tools/ling_class/EST_relation_compare.h b/aeneas/cfw/speech_tools/ling_class/EST_relation_compare.h new file mode 100644 index 00000000..5adbe763 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_relation_compare.h @@ -0,0 +1,92 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1994,1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Paul Taylor */ +/* Date : May 1994 */ +/*-----------------------------------------------------------------------*/ +/* Label Comparison Routines */ +/* */ +/*=======================================================================*/ + +#ifndef __EST_RELATION_COMPARE_H__ +#define __EST_RELATION_COMPARE_H__ + +float label_distance1(EST_Item &ref, EST_Item &test); +EST_Item *nthpos(EST_Relation &a, int n); +void compare_labels(EST_Relation &reflab, EST_Relation &testlab); +void relation_match(EST_Relation &a, EST_Relation &b); +void function_match(EST_II_KVL &u, EST_Relation &a, EST_Relation &b); +void monotonic_match(EST_II_KVL &a, EST_II_KVL &b); +void show_links(EST_Relation &a, EST_Relation &b); +int close_enough(EST_Item &a, EST_Item &b); +int matrix_deletions(EST_FMatrix &m); +int matrix_insertions(EST_FMatrix &m); +void matrix_ceiling(EST_FMatrix &m, float max); +void minimise_matrix_by_row(EST_FMatrix &m); +void minimise_matrix_by_column(EST_FMatrix &m); +int lowest_pos(EST_FMatrix &m, int j); +float label_distance2(EST_Item &ref, EST_Item &test); + + +void print_results(EST_Relation &ref, EST_Relation &test, EST_FMatrix &m, int tot, + int del, int ins, int v); +void print_aligned_trans(EST_Relation &ref, EST_Relation &test, EST_FMatrix &m); +void pos_only(EST_Relation &a); +void print_s_trans(EST_Relation &a, int width=3); +int num_b_deletions(EST_FMatrix &m, int last, int current); +int num_b_insertions(EST_FMatrix &m, int last, int current); +int column_hit(EST_FMatrix &m, int c); +int row_hit(EST_FMatrix &m, int r); +void print_matrix_scores(EST_Relation &ref, EST_Relation &test, EST_FMatrix &a); +void print_i_d_scores(EST_FMatrix &m); +void test_labels(EST_Utterance &ref, EST_Utterance &test, EST_Option &op); +int commutate(EST_Item *a_ptr, EST_II_KVL &f1, EST_II_KVL &f2, + EST_II_KVL &lref, EST_II_KVL <est); + +void reassign_links(EST_Relation &a, EST_II_KVL &u, EST_String stream_type); +void reassign_links(EST_Relation &a, EST_Relation &b, EST_II_KVL &ua, EST_II_KVL &ub); +int compare_labels(EST_Utterance &ref, EST_Utterance &test, EST_String name, + EST_II_KVL &uref, EST_II_KVL &utest); + +int insdel(EST_II_KVL &a); +void error_location(EST_Relation &e, EST_FMatrix &m, int ref); +void multiple_matrix_compare(EST_TList &rmlf, EST_TList + &tmlf, EST_FMatrix &m, EST_String rpos, EST_String tpos, int + method, float t, int v); + +EST_FMatrix matrix_compare(EST_Relation &reflab, EST_Relation &testlab, int method, + float t, int v); + +void multiple_labels(EST_Relation &reflab); +void threshold_labels(EST_Relation &reflab, float t); + +#endif //__EST_RELATION_COMPARE_H__ diff --git a/aeneas/cfw/speech_tools/ling_class/EST_utterance_aux.h b/aeneas/cfw/speech_tools/ling_class/EST_utterance_aux.h new file mode 100644 index 00000000..93f53ee7 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_utterance_aux.h @@ -0,0 +1,58 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_UTTERANCE_AUX_H__ +#define __EST_UTTERANCE_AUX_H__ + +#include "EST_String.h" +#include "EST_types.h" + +/** Auxiliary operations on utterance structures. + * + * @author Richard Caley + * @version $Id: EST_utterance_aux.h,v 1.3 2004/05/04 00:00:17 awb Exp $ + */ + +//@{ + +EST_String options_utterance_filetypes(void); +EST_String options_utterance_filetypes_long(void); +void utterance_xml_register_id(const EST_String pattern, + const EST_String result); +void utterance_xml_registered_ids(EST_StrList &list); + +//@} + +#endif + diff --git a/aeneas/cfw/speech_tools/ling_class/EST_utterance_xml.h b/aeneas/cfw/speech_tools/ling_class/EST_utterance_xml.h new file mode 100644 index 00000000..41403c06 --- /dev/null +++ b/aeneas/cfw/speech_tools/ling_class/EST_utterance_xml.h @@ -0,0 +1,49 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_UTTERANCE_XML_H__ +#define __EST_UTTERANCE_XML_H__ + +/** Export some RXP functionality for use in festival. + * + * @author Richard Caley + * @version $Id: EST_utterance_xml.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#include "rxp/XML_Parser.h" + +InputSource utterance_xml_try_and_open(Entity ent); + +#endif + diff --git a/aeneas/cfw/speech_tools/rateconv.h b/aeneas/cfw/speech_tools/rateconv.h new file mode 100644 index 00000000..9b000ef5 --- /dev/null +++ b/aeneas/cfw/speech_tools/rateconv.h @@ -0,0 +1,50 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +/**@name rateconv.h + * + * Just declares the resampling function. + * + * @author Richard Caley + * @version $Id: rateconv.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ +//@{ +#ifndef __RATECONV_H__ +#define __RATECONV_H__ + +int rateconv(short *in,int isize, short **out, int *osize, + int in_samp_freq, int out_samp_freq); + +#endif +//@} diff --git a/aeneas/cfw/speech_tools/rxp/XML_Parser.h b/aeneas/cfw/speech_tools/rxp/XML_Parser.h new file mode 100644 index 00000000..36ec3fd1 --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/XML_Parser.h @@ -0,0 +1,322 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __XML_PARSER_H__ +#define __XML_PARSER_H__ + +#if !defined(CHAR_SIZE) +# define CHAR_SIZE 8 +#endif + +#if (CHAR_SIZE!=8) +# error EST can only handle 8 bit characters +#endif + +#include "EST_String.h" +#include "EST_Regex.h" +#include "EST_TKVL.h" +#include "EST_THash.h" +#include "EST_TDeque.h" +#include "EST_TList.h" +#include "rxp/rxp.h" + +// We only use types and functions from rxp.h, so we can throw away +// some of the macros which cause problems. + +#undef get + + +/**@name XML Parser + * Recursive descent parsing skeleton with hooks for processing. + * A C++ wrapper around the rxp parser. + * + * @author Richard Caley + * @version $Id: XML_Parser.h,v 1.3 2004/05/04 00:00:17 awb Exp $ + */ +//@{ + +class XML_Parser; +class XML_Parser_Class; + +/// Nice name for list of attribute-value pairs. +typedef EST_TStringHash XML_Attribute_List; + +/** A Class of parsers, All parsers share callbacks and a + * list of known public IDs. + */ +class XML_Parser_Class { + +private: + + /** Map PUBLIC and SYSTEM IDs to places on the local system. + */ + EST_TKVL known_ids; + +protected: + /** Do any necessary remappings and open a stream which reads the given + * entity. + */ + static InputSource open_entity(Entity ent, void *arg); + + + /**@name The callbacks. + * + * These methods can be overridden in a subclass to create a class + * of parsers to do whatever you want. + */ + //@{ + + /** Called when starting a document. + */ + virtual void document_open(XML_Parser_Class &c, + XML_Parser &p, + void *data); + + /** Called at the end of a document. + */ + virtual void document_close(XML_Parser_Class &c, + XML_Parser &p, + void *data); + + /** Called when an element starts. + */ + virtual void element_open(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *name, + XML_Attribute_List &attributes); + + /** Called when an element ends. + */ + virtual void element_close(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *name); + + /** Called for empty elements. + * + * Defaults to element_open(...) followed by element_closed(...). + */ + virtual void element(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *name, + XML_Attribute_List &attributes); + + /** Called for parsed character data sequences. + */ + virtual void pcdata(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *chars); + /** Called for unparsed character data sequences. + */ + virtual void cdata(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *chars); + + /** Called for processing directives. + */ + virtual void processing(XML_Parser_Class &c, + XML_Parser &p, + void *data, + const char *instruction); + + /** Called when there is an error in parsing. + */ + virtual void error(XML_Parser_Class &c, + XML_Parser &p, + void *data); + //@} + + /** This can be called from any of the callbacks to present "message" + * as an error through the error callback, thus getting filename and + * line information into the message. + */ + void error(XML_Parser_Class &c, + XML_Parser &p, + void *data, + EST_String message); + + /// Get the error message for the last error. + const char *get_error(XML_Parser &p); + +public: + + /** Create an object representing the class of parsers. + */ + XML_Parser_Class(); + + virtual ~XML_Parser_Class() { } + + /** Add a mapping from entity ID (SYSTEM or PUBLIC) to filename. + * + * The string can contain escapes like \2 which are replaced by + * the text matching the Nth bracketed part of the regular expression. + */ + void register_id(EST_Regex id_pattern, EST_String directory); + + /** Fill in the list with the known entity ID mappings. + */ + + void registered_ids(EST_TList &list); + + /**@name Creating a parser + * + * Each of these methods creates a one-shot parser which will run over the + * indicated text. + */ + //@{ + + /// Create a parser for the RXP InputSource. + XML_Parser *make_parser(InputSource source, void *data); + + /// Create a parser for the RXP InputSource. + XML_Parser *make_parser(InputSource source, Entity initial_entity, void *data); + + /// Create a parser for a stdio input stream. + XML_Parser *make_parser(FILE *input, void *data); + + /** Create a parser for a stdio input stream, giving a description for + * use in errors. + */ + XML_Parser *make_parser(FILE *input, const EST_String desc, void *data); + + // Create a parser for the named file. + XML_Parser *make_parser(const EST_String filename, void *data); + + //@} + + /** Utility which tries to open an entity called ID at places + * specified in the mapping of this parser class. + */ + + InputSource try_and_open(Entity ent); + + /** XML_Parser defines the behaviour of an individual one-shot + * parser. + */ + friend class XML_Parser; +}; + +/** An actual parser. Each such instance parses just one stream which is + * given when the parser is created. + * + * The behaviour of the parser is given by the class to which it belongs. + */ + +class XML_Parser { + +private: + /// Last error message from the parser. + EST_String p_error_message; + + /// Set true when context is being remembered. + bool p_track_context; + + /// Set true when contents is being remembered. (not yet implemented) + bool p_track_contents; + +protected: + /** The class to which this parser belongs. Defines the behaviour of + * the parser. + */ + XML_Parser_Class *pclass; + + /// The piece of markup being processed. + XBit current_bit; + + /// Where we are reading from. + InputSource source; + + /** The entity we started from. May need to be freed at the end of the + * parse. + */ + Entity initial_entity; + + /// Arbitrary data which can be used by callbacks. + void *data; + + /// The RXP parser object. + Parser p; + + /// If context is being tracked, this is a stack of element names. + EST_TDeque p_context; + + + /// Creator used by XML_Parser_Class::make_parser() + XML_Parser(XML_Parser_Class &parent, + InputSource source, + Entity initial_entity, + void *data); + + /// Open. Asks the parser class to do the work. + InputSource open(Entity ent); + + /// Get the error message for the last error. + const char *get_error(); + +public: + + /// Destructor, may close input if required. + ~XML_Parser(); + + /** Request that parser keep track of the currently open elements. + * + * These are recorded on a atsck. Use context() to access the information. + */ + void track_context(bool flag); + /** Keep track of the content of open elements. + * + * Not yet implemented. + */ + void track_contents(bool flag); + + /** Get the name of the nth enclosing element. + * + * context(0) is the element we are directly inside. + */ + EST_String context(int n); + + /// Run the parser. + void go(); + + friend class XML_Parser_Class; +}; + +//@} + +#endif + diff --git a/aeneas/cfw/speech_tools/rxp/charset.h b/aeneas/cfw/speech_tools/rxp/charset.h new file mode 100644 index 00000000..fbdf3fad --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/charset.h @@ -0,0 +1,83 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef CHARSET_H +#define CHARSET_H + +#ifndef FOR_LT +#define STD_API +#endif + +STD_API void init_charset(void); + +/* + * We'd like char8 to be unsigned char, but it causes too many problems. + * For example: + * char8 name; ...; return name ? name : "" + * produces a warning with many compilers if char8 is unsigned. + */ + +typedef char char8; +typedef unsigned short char16; +typedef unsigned int char32; + +#if !defined(CHAR_SIZE) +# error CHAR_SIZE not defined +#endif + +#if CHAR_SIZE == 8 +typedef char8 Char; +#elif CHAR_SIZE == 16 +typedef char16 Char; +#else +#error CHAR_SIZE must be 8 or 16 +#endif + +/* Character encodings */ + +enum character_encoding { + CE_unknown, CE_unspecified_ascii_superset, + CE_UTF_8, CE_ISO_646, + CE_ISO_8859_1, + + CE_ISO_8859_2, CE_ISO_8859_3, CE_ISO_8859_4, CE_ISO_8859_5, + CE_ISO_8859_6, CE_ISO_8859_7, CE_ISO_8859_8, CE_ISO_8859_9, + + CE_UTF_16B, CE_UTF_16L, CE_ISO_10646_UCS_2B, CE_ISO_10646_UCS_2L, + CE_enum_count +}; + +typedef enum character_encoding CharacterEncoding; + +extern STD_API CharacterEncoding InternalCharacterEncoding; + +extern STD_API const char8 *CharacterEncodingName[CE_enum_count]; +extern STD_API const char8 *CharacterEncodingNameAndByteOrder[CE_enum_count]; + +struct character_encoding_alias {const char8 *name; CharacterEncoding enc;}; +extern STD_API struct character_encoding_alias CharacterEncodingAlias[]; +extern STD_API const int CE_alias_count; + +STD_API int EncodingIsAsciiSuperset(CharacterEncoding enc); +STD_API int EncodingsCompatible(CharacterEncoding enc1, CharacterEncoding enc2, + CharacterEncoding *enc3); +STD_API CharacterEncoding FindEncoding(char8 *name); + +/* Translation tables for Latin-N - do this right sometime! XXX */ + +extern STD_API int iso_to_unicode[8][256]; +extern STD_API int iso_max_val[8]; +extern STD_API char8 *unicode_to_iso[8]; + +#endif /* CHARSET_H */ diff --git a/aeneas/cfw/speech_tools/rxp/ctype16.h b/aeneas/cfw/speech_tools/rxp/ctype16.h new file mode 100644 index 00000000..e2e8d48f --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/ctype16.h @@ -0,0 +1,54 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef CTYPE16_H +#define CTYPE16_H + +#ifndef FOR_LT +#define STD_API +#endif + +/* XML character types */ + +STD_API void init_ctype16(void); +STD_API int Toupper(int c); +STD_API int Tolower(int c); + +extern STD_API unsigned char xml_char_map[]; + +#define xml_legal 0x01 +#define xml_namestart 0x02 +#define xml_namechar 0x04 +#define xml_whitespace 0x08 + +#if CHAR_SIZE == 8 + +/* And with 0xff so that it works if char is signed */ +#define is_xml_legal(c) (xml_char_map[(int)(c) & 0xff] & xml_legal) +#define is_xml_namestart(c) (xml_char_map[(int)(c) & 0xff] & xml_namestart) +#define is_xml_namechar(c) (xml_char_map[(int)(c) & 0xff] & xml_namechar) +#define is_xml_whitespace(c) (xml_char_map[(int)(c) & 0xff] & xml_whitespace) + +#else + +/* Note! these macros evaluate their argument more than once! */ + +#define is_xml_legal(c) (c < 0x110000 && (c >= 0x10000 || (xml_char_map[c] & xml_legal))) +#define is_xml_namestart(c) (c < 0x10000 && (xml_char_map[c] & xml_namestart)) +#define is_xml_namechar(c) (c < 0x10000 && (xml_char_map[c] & xml_namechar)) +#define is_xml_whitespace(c) (c < 0x10000 && (xml_char_map[c] & xml_whitespace)) + +#endif + +#endif /* CTYPE16_H */ diff --git a/aeneas/cfw/speech_tools/rxp/dtd.h b/aeneas/cfw/speech_tools/rxp/dtd.h new file mode 100644 index 00000000..b32dcc8c --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/dtd.h @@ -0,0 +1,254 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef DTD_H +#define DTD_H + +#ifndef FOR_LT +#define XML_API +#endif + +#include "charset.h" + +/* Typedefs */ + +typedef struct dtd *Dtd; + +typedef struct entity *Entity; + +typedef struct element_definition *ElementDefinition; + +typedef struct attribute_definition *AttributeDefinition; +AttributeDefinition NextAttributeDefinition(ElementDefinition element, + AttributeDefinition previous); + +typedef struct notation_definition *NotationDefinition; + +/* DTDs */ + +struct dtd { + const Char *name; /* The doctype name */ + Entity internal_part, external_part; + Entity entities; + Entity parameter_entities; + Entity predefined_entities; +#ifdef FOR_LT + NSL_Doctype_I *doctype; +#else + ElementDefinition elements; +#endif + NotationDefinition notations; +}; + +/* Entities */ + +enum entity_type {ET_external, ET_internal}; +typedef enum entity_type EntityType; + +enum markup_language {ML_xml, ML_nsl, ML_unspecified}; +typedef enum markup_language MarkupLanguage; + +enum standalone_declaration { + /* NB must match NSL's rmdCode */ + SDD_unspecified, SDD_no, SDD_yes, SDD_enum_count +}; +typedef enum standalone_declaration StandaloneDeclaration; + +extern const char8 *StandaloneDeclarationName[SDD_enum_count]; + + +struct entity { + /* All entities */ + + const Char *name; /* The name in the entity declaration */ + EntityType type; /* ET_external or ET_internal */ + const char8 *base_url; /* If different from expected */ + struct entity *next; /* For chaining a document's entity defns */ + CharacterEncoding encoding; /* The character encoding of the entity */ + Entity parent; /* The entity in which it is defined */ + const char8 *url; /* URL of entity */ + + /* Internal entities */ + + const Char *text; /* Text of the entity */ + int line_offset; /* Line offset of definition */ + int line1_char_offset; /* Char offset on first line */ + int matches_parent_text; /* False if might contain expanded PEs */ + + /* External entities */ + + const char8 *systemid; /* Declared public ID */ + const char8 *publicid; /* Declared public ID */ + NotationDefinition notation; /* Binary entity's declared notation */ + MarkupLanguage ml_decl; /* XML, NSL or not specified */ + const char8 *version_decl; /* XML declarations found in entity, if any */ + CharacterEncoding encoding_decl; + StandaloneDeclaration standalone_decl; + const char8 *ddb_filename; /* filename in NSL declaration */ +}; + +/* Elements */ + +enum content_type { + /* NB this must match NSL's ctVals */ + CT_mixed, CT_any, CT_bogus1, CT_bogus2, CT_empty, CT_element, CT_enum_count +}; +typedef enum content_type ContentType; + +extern XML_API const char8 *ContentTypeName[CT_enum_count]; + +struct element_definition { + const Char *name; /* The element name */ + int namelen; + int tentative; +#ifdef FOR_LT + NSL_Doctype_I *doctype; + NSL_ElementSummary_I *elsum; +#else + ContentType type; /* The declared content */ + Char *content; /* Element content */ + AttributeDefinition attributes; + struct element_definition *next; +#endif +}; + +/* Attributes */ + +enum default_type { + /* NB this must match NSL's NSL_ADefType */ + DT_required, DT_bogus1, DT_implied, + DT_bogus2, DT_none, DT_fixed, DT_enum_count +}; +typedef enum default_type DefaultType; + +extern XML_API const char8 *DefaultTypeName[DT_enum_count]; + +enum attribute_type { + /* NB this must match NSL's NSL_Attr_Dec_Value */ + AT_cdata, AT_bogus1, AT_bogus2, AT_nmtoken, AT_bogus3, AT_entity, + AT_idref, AT_bogus4, AT_bogus5, AT_nmtokens, AT_bogus6, AT_entities, + AT_idrefs, AT_id, AT_notation, AT_enumeration, AT_enum_count +}; +typedef enum attribute_type AttributeType; + +extern XML_API const char8 *AttributeTypeName[AT_enum_count]; + +struct attribute_definition { +#ifdef FOR_LT + /* NB this must match NSL's AttributeSummary */ + /* We never really have one of these structures; only an AttributeSummary + cast to this type. We need to be able to access the type, so that + we can normalise if appropriate. We need to be able to refer to + the default_type and default_value, but these don't need to work + since we will never have ReturnDefaultedAttributes true in NSL. */ + int a, b, c; + short d; + char type, default_type; + /* This had better never be accessed! */ + Char *default_value; +#else + const Char *name; /* The attribute name */ + int namelen; + AttributeType type; /* The declared type */ + Char **allowed_values; /* List of allowed values, argv style */ + DefaultType default_type; /* The type of the declared default */ + const Char *default_value; /* The declared default value */ + struct attribute_definition *next; +#endif +}; + +/* Notations */ + +struct notation_definition { + const Char *name; /* The notation name */ + int tentative; + const char8 *systemid; /* System identifier */ + const char8 *publicid; /* Public identifier */ + struct notation_definition *next; +}; + +/* Public functions */ + +XML_API Dtd NewDtd(void); +XML_API void FreeDtd(Dtd dtd); + +XML_API Entity NewExternalEntityN(const Char *name, int namelen, + const char8 *publicid, const char8 *systemid, + NotationDefinition notation, + Entity parent); +XML_API Entity NewInternalEntityN(const Char *name, int namelen, + const Char *text, Entity parent, + int line_offset, int line1_char_offset, + int matches_parent_text); +XML_API void FreeEntity(Entity e); + +XML_API const char8 *EntityURL(Entity e); +XML_API const char8 *EntityDescription(Entity e); +XML_API void EntitySetBaseURL(Entity e, const char8 *url); +XML_API const char8 *EntityBaseURL(Entity e); + +XML_API Entity DefineEntity(Dtd dtd, Entity entity, int pe); +XML_API Entity FindEntityN(Dtd dtd, const Char *name, int namelen, int pe); + +#define NewExternalEntity(name, pub, sys, nnot, parent) \ + NewExternalEntityN(name, name ? Strlen(name) : 0, pub, sys, nnot, parent) +#define NewInternalEntity(name, test, parent, l, l1, mat) \ + NewInternalEntityN(name, name ? Strlen(name) : 0, test, parent, l, l1, mat) +#define FindEntity(dtd, name, pe) FindEntityN(dtd, name, Strlen(name), pe) + +XML_API ElementDefinition DefineElementN(Dtd dtd, const Char *name, int namelen, + ContentType type, Char *content); +XML_API ElementDefinition TentativelyDefineElementN(Dtd dtd, + const Char *name, int namelen); +XML_API ElementDefinition RedefineElement(ElementDefinition e, ContentType type, + Char *content); +XML_API ElementDefinition FindElementN(Dtd dtd, const Char *name, int namelen); +XML_API void FreeElementDefinition(ElementDefinition e); + +#define DefineElement(dtd, name, type, content) \ + DefineElementN(dtd, name, Strlen(name), type, content) +#define TentativelyDefineElement(dtd, name) \ + TentativelyDefineElementN(dtd, name, Strlen(name)) +#define FindElement(dtd, name) FindElementN(dtd, name, Strlen(name)) + +XML_API AttributeDefinition DefineAttributeN(ElementDefinition element, + const Char *name, int namelen, + AttributeType type, Char **allowed_values, + DefaultType default_type, + const Char *default_value); +XML_API AttributeDefinition FindAttributeN(ElementDefinition element, + const Char *name, int namelen); +XML_API void FreeAttributeDefinition(AttributeDefinition a); + +#define DefineAttribute(element, name, type, all, dt, dv) \ + DefineAttributeN(element, name, Strlen(name), type, all, dt, dv) +#define FindAttribute(element, name) \ + FindAttributeN(element, name, Strlen(name)) + +XML_API NotationDefinition DefineNotationN(Dtd dtd, const Char *name, int namelen, + const char8 *publicid, const char8 *systemid); +XML_API NotationDefinition TentativelyDefineNotationN(Dtd dtd, + const Char *name, int namelen); +XML_API NotationDefinition RedefineNotation(NotationDefinition n, + const char8 *publicid, const char8 *systemid); +XML_API NotationDefinition FindNotationN(Dtd dtd, const Char *name, int namelen); +XML_API void FreeNotationDefinition(NotationDefinition n); + +#define DefineNotation(dtd, name, pub, sys) \ + DefineNotationN(dtd, name, Strlen(name), pub, sys) +#define TentativelyDefineNotation(dtd, name) \ + TentativelyDefineNotationN(dtd, name, Strlen(name)) +#define FindNotation(dtd, name) FindNotationN(dtd, name, Strlen(name)) + +#endif /* DTD_H */ diff --git a/aeneas/cfw/speech_tools/rxp/input.h b/aeneas/cfw/speech_tools/rxp/input.h new file mode 100644 index 00000000..134b55ce --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/input.h @@ -0,0 +1,80 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef INPUT_H +#define INPUT_H + +#ifndef FOR_LT +#define XML_API +#endif + +#include +#include "charset.h" +#include "stdio16.h" +#include "dtd.h" + +/* Typedefs */ + +typedef struct input_source *InputSource; +typedef struct stream_ops *StreamOps; +typedef int ReadProc(StreamOps ops, unsigned char *buf, int max_count); +typedef int WriteProc(StreamOps ops, unsigned char *buf, int count); +typedef void CloseProc(StreamOps ops); +typedef int SeekProc(StreamOps ops, int offset); + +/* Input sources */ + +XML_API InputSource SourceFromStream(const char8 *description, FILE *file); +XML_API InputSource EntityOpen(Entity e); +XML_API InputSource NewInputSource(Entity e, FILE16 *f16); +XML_API int SourceTell(InputSource s); +XML_API int SourceSeek(InputSource s, int offset); +XML_API int SourceLineAndChar(InputSource s, int *linenum, int *charnum); +XML_API void SourcePosition(InputSource s, Entity *entity, int *char_number); +XML_API int get_with_fill(InputSource s); +XML_API void determine_character_encoding(InputSource s); + +struct input_source { + Entity entity; /* The entity from which the source reads */ + + FILE16 *file16; + + Char *line; + int line_alloc, line_length; + int next; + + int seen_eoe; + int complicated_utf8_line; + int bytes_consumed; + int bytes_before_current_line; + int line_end_was_cr; + + int line_number; + int not_read_yet; + + struct input_source *parent; + + int nextin; + int insize; + unsigned char inbuf[4096]; +}; + +/* EOE used to be -2, but that doesn't work if Char is signed char */ +#define XEOE (-999) + +#define at_eol(s) ((s)->next == (s)->line_length) +#define get(s) (at_eol(s) ? get_with_fill(s) : (s)->line[(s)->next++]) +#define unget(s) ((s)->seen_eoe ? (s)->seen_eoe= 0 : (s)->next--) + +#endif /* INPUT_H */ diff --git a/aeneas/cfw/speech_tools/rxp/rxp.h b/aeneas/cfw/speech_tools/rxp/rxp.h new file mode 100644 index 00000000..f6baed5c --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/rxp.h @@ -0,0 +1,29 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ + +#if defined(__cplusplus) +extern "C" { +#endif + +#include "charset.h" +#include "string16.h" +#include "dtd.h" +#include "input.h" +#include "xmlparser.h" +#include "stdio16.h" + +#if defined(__cplusplus) +} +#endif diff --git a/aeneas/cfw/speech_tools/rxp/stdio16.h b/aeneas/cfw/speech_tools/rxp/stdio16.h new file mode 100644 index 00000000..f3a5233e --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/stdio16.h @@ -0,0 +1,67 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef STDIO16_H +#define STDIO16_H + +#ifndef FOR_LT +#define STD_API +#endif + +#include +#include +#ifdef HAVE_LIBZ +#include "zlib.h" +#endif +#include "charset.h" + +typedef struct _FILE16 FILE16; + +extern STD_API FILE16 *Stdin, *Stdout, *Stderr; + +STD_API FILE16 *MakeFILE16FromFILE(FILE *f, const char *type); +STD_API FILE16 *MakeFILE16FromString(void *buf, long size, const char *type); +#ifdef WIN32 +#ifdef SOCKETS_IMPLEMENTED +STD_API FILE16 *MakeFILE16FromWinsock(int sock, const char *type); +#endif +#endif +#ifdef HAVE_LIBZ +STD_API FILE16 *MakeFILE16FromGzip(gzFile file, const char *type); +#endif + +STD_API int Readu(FILE16 *file, unsigned char *buf, int max_count); +STD_API int Writeu(FILE16 *file, unsigned char *buf, int count); +STD_API int Fclose(FILE16 *file); +STD_API int Fflush(FILE16 *file); +STD_API int Fseek(FILE16 *file, long offset, int ptrname); + +STD_API FILE *GetFILE(FILE16 *file); +STD_API void SetCloseUnderlying(FILE16 *file, int cu); +STD_API void SetFileEncoding(FILE16 *file, CharacterEncoding encoding); +STD_API CharacterEncoding GetFileEncoding(FILE16 *file); + +STD_API int Fprintf(FILE16 *file, const char *format, ...); +STD_API int Vfprintf(FILE16 *file, const char *format, va_list args); + +STD_API int Printf(const char *format, ...); +STD_API int Vprintf(const char *format, va_list args); + +STD_API int Sprintf(void *buf, CharacterEncoding enc, const char *format, ...); +STD_API int Vsprintf(void *buf, CharacterEncoding enc, const char *format, + va_list args); + +STD_API void init_stdio16(void); + +#endif /* STDIO16_H */ diff --git a/aeneas/cfw/speech_tools/rxp/string16.h b/aeneas/cfw/speech_tools/rxp/string16.h new file mode 100644 index 00000000..17e56477 --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/string16.h @@ -0,0 +1,113 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +#ifndef STRING16_H +#define STRING16_H + +#ifndef FOR_LT +#define STD_API +#define WIN_IMP +#endif + +#include "charset.h" +#include /* for size_t */ + +/* String functions */ +#include +#if 0 +/* Don't want to include string.h while testing */ + +int strcmp(const char *, const char *); +WIN_IMP int strncmp(const char *, const char *, size_t); +int strcasecmp(const char *, const char *); +size_t strlen(const char *); +WIN_IMP char *strchr(const char *, int); +char *strcpy(char *, const char *); +WIN_IMP char *strncpy(char *, const char *, size_t); +char *strcat(char *, const char *); +WIN_IMP char *strstr(const char *, const char *); +int memcmp(const void *, const void *, size_t); +#ifndef memcpy +void *memcpy(void *, const void *, size_t); +#endif +void *memset(void *, int, size_t); +WIN_IMP size_t strspn(const char *, const char *); +WIN_IMP size_t strcspn(const char *, const char *); +#endif + +STD_API char8 *strdup8(const char8 *s); +#define strchr8(s, c)strchr((s), c) +#define strlen8(s) strlen((s)) +#define strcmp8(s1, s2) strcmp((s1), (s2)) +#define strncmp8(s1, s2, n) strncmp((s1), (s2), n) +#define strcpy8(s1, s2) strcpy((s1), (s2)) +#define strncpy8(s1, s2, n) strncpy((s1), (s2), n) + +#define strcat8(s1, s2) strcat((s1), (s2)) +STD_API int strcasecmp8(const char8 *, const char8 *); +STD_API int strncasecmp8(const char8 *, const char8 *, size_t); +#define strstr8(s1, s2) strstr(s1, s2) + +STD_API char16 *strdup16(const char16 *s); +STD_API char16 *strchr16(const char16 *, int); +STD_API size_t strlen16(const char16 *); +STD_API int strcmp16(const char16 *, const char16 *); +STD_API int strncmp16(const char16 *, const char16 *, size_t); +STD_API char16 *strcpy16(char16 *, const char16 *); +STD_API char16 *strncpy16(char16 *, const char16 *, size_t); +STD_API char16 *strcat16(char16 *, const char16 *); +STD_API int strcasecmp16(const char16 *, const char16 *); +STD_API int strncasecmp16(const char16 *, const char16 *, size_t); +STD_API char16 *strstr16(const char16 *, const char16 *); + +STD_API char16 *char8tochar16(const char8 *s); +STD_API char8 *char16tochar8(const char16 *s); + +#if CHAR_SIZE == 8 + +#define Strdup strdup8 +#define Strchr strchr8 +#define Strlen strlen8 +#define Strcmp strcmp8 +#define Strncmp strncmp8 +#define Strcpy strcpy8 +#define Strncpy strncpy8 +#define Strcat strcat8 +#define Strcasecmp strcasecmp8 +#define Strncasecmp strncasecmp8 +#define Strstr strstr8 + +#define char8toChar(x) (x) +#define Chartochar8(x) (x) + +#else + +#define Strdup strdup16 +#define Strchr strchr16 +#define Strlen strlen16 +#define Strcmp strcmp16 +#define Strncmp strncmp16 +#define Strcpy strcpy16 +#define Strncpy strncpy16 +#define Strcat strcat16 +#define Strcasecmp strcasecmp16 +#define Strncasecmp strncasecmp16 +#define Strstr strstr16 + +#define char8toChar char8tochar16 +#define Chartochar8 char16tochar8 + +#endif + +#endif /* STRING16_H */ diff --git a/aeneas/cfw/speech_tools/rxp/system.h b/aeneas/cfw/speech_tools/rxp/system.h new file mode 100644 index 00000000..23d1bee2 --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/system.h @@ -0,0 +1,36 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +void *Malloc(int bytes); +void *Realloc(void *mem, int bytes); +void Free(void *mem); + +#ifndef SEEK_SET +#define SEEK_SET 0 +#endif + +#ifndef SEEK_CUR +#define SEEK_CUR 1 +#endif + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +#define SOCKETS_IMPLEMENTED + +#ifdef SYSTEM_IS_WIN32 +#define WIN32 1 +#endif + diff --git a/aeneas/cfw/speech_tools/rxp/url.h b/aeneas/cfw/speech_tools/rxp/url.h new file mode 100644 index 00000000..3f4879d0 --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/url.h @@ -0,0 +1,39 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +/* url.h -- Henry Thompson + * + * $Header: /home/CVS/speech_tools/include/rxp/url.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#ifndef _URL_H +#define _URL_H + +#ifndef FOR_LT +#define STD_API +#define EXPRT +#endif + +#include +#include "stdio16.h" +#include "charset.h" + +extern STD_API char8 * EXPRT + url_merge(const char8 *url, const char8 *base, + char8 **scheme, char8 **host, int *port, char8 **path); +extern STD_API FILE16 *url_open(const char8 *url, const char8 *base, + const char8 *type, char8 **merged_url); +extern STD_API char8 *EXPRT default_base_url(void); + +#endif diff --git a/aeneas/cfw/speech_tools/rxp/xmlparser.h b/aeneas/cfw/speech_tools/rxp/xmlparser.h new file mode 100644 index 00000000..6f2326b5 --- /dev/null +++ b/aeneas/cfw/speech_tools/rxp/xmlparser.h @@ -0,0 +1,194 @@ +/*************************************************************************/ +/* */ +/* Copyright (c) 1997-98 Richard Tobin, Language Technology Group, HCRC, */ +/* University of Edinburgh. */ +/* */ +/* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF EDINBURGH BE LIABLE */ +/* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */ +/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION */ +/* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* */ +/*************************************************************************/ +/* $Id: xmlparser.h,v 1.2 2001/04/04 13:11:27 awb Exp $ */ + +#ifndef XMLPARSER_H +#define XMLPARSER_H + +#include "dtd.h" +#include "input.h" + +/* Typedefs */ + +typedef struct parser_state *Parser; +typedef struct attribute *Attribute; +typedef struct content_particle *ContentParticle; +typedef struct xbit *XBit; +typedef void CallbackProc(XBit bit, void *arg); +typedef InputSource EntityOpenerProc(Entity e, void *arg); + +/* Bits */ + +enum xbit_type { + XBIT_dtd, + XBIT_start, XBIT_empty, XBIT_end, XBIT_eof, XBIT_pcdata, + XBIT_pi, XBIT_comment, XBIT_cdsect, XBIT_xml, + XBIT_error, XBIT_warning, XBIT_none, + XBIT_enum_count +}; +typedef enum xbit_type XBitType; + +extern XML_API const char8 *XBitTypeName[XBIT_enum_count]; + +struct attribute { + AttributeDefinition definition; /* The definition of this attribute */ + Char *value; /* The (possibly normalised) value */ + int quoted; /* Was it quoted? */ + struct attribute *next; /* The next attribute or null */ +}; + +enum cp_type { + CP_pcdata, CP_name, CP_seq, CP_choice +}; +typedef enum cp_type CPType; + +struct content_particle { + enum cp_type type; + int repetition; + Char *name; + int nchildren; + struct content_particle **children; +}; + +struct xbit { + Entity entity; + int byte_offset; + enum xbit_type type; + char8 *s1, *s2; + Char *S1, *S2; + int i1, i2; + Attribute attributes; + ElementDefinition element_definition; +#ifndef FOR_LT + int nchildren; + struct xbit *parent; + struct xbit **children; +#endif +}; + +#define pcdata_chars S1 + +#define pi_name S1 +#define pi_chars S2 + +#define comment_chars S1 + +#define cdsect_chars S1 + +#define xml_version s1 +#define xml_encoding_name s2 +#define xml_standalone i1 +#define xml_encoding i2 + +#define error_message s1 + +/* Parser flags */ + +enum parser_flag { + ExpandCharacterEntities, + ExpandGeneralEntities, + XMLPiEnd, + XMLEmptyTagEnd, + XMLPredefinedEntities, + ErrorOnUnquotedAttributeValues, + NormaliseAttributeValues, + NormalizeAttributeValues, + ErrorOnBadCharacterEntities, + ErrorOnUndefinedEntities, + ReturnComments, + CaseInsensitive, + ErrorOnUndefinedElements, + WarnOnUndefinedElements, + ErrorOnUndefinedAttributes, + WarnOnUndefinedAttributes, + WarnOnRedefinitions, + TrustSDD, + XMLExternalIDs, + ReturnDefaultedAttributes, + MergePCData, + XMLMiscWFErrors, + XMLStrictWFErrors, + AllowMultipleElements, + CheckEndTagsMatch, + IgnoreEntities, + XMLLessThan +}; +typedef enum parser_flag ParserFlag; + +/* Parser */ + +enum parse_state + {PS_prolog1, PS_prolog2, PS_body, PS_epilog, PS_end, PS_error}; + +struct element_info { + ElementDefinition definition; + Entity entity; +}; + +struct parser_state { + enum parse_state state; + Entity document_entity; + int have_dtd; /* True if dtd has been processed */ + StandaloneDeclaration standalone; + struct input_source *source; + Char *name, *pbuf; + int namelen, pbufsize, pbufnext; + struct xbit xbit; + int peeked; + Dtd dtd; /* The document's DTD */ + CallbackProc *dtd_callback; + CallbackProc *warning_callback; + EntityOpenerProc *entity_opener; + unsigned int flags; + struct element_info *element_stack; + int element_stack_alloc; + int element_depth; + void *callback_arg; + int external_pe_depth; /* To keep track of whether we're in the */ + /* internal subset: 0 <=> yes */ +}; + +XML_API int ParserInit(void); +XML_API Parser NewParser(void); +XML_API void FreeParser(Parser p); + +XML_API Entity ParserRootEntity(Parser p); +XML_API InputSource ParserRootSource(Parser p); + +XML_API XBit ReadXBit(Parser p); +XML_API XBit PeekXBit(Parser p); +XML_API void FreeXBit(XBit xbit); + +#ifndef FOR_LT +XBit ReadXTree(Parser p); +void FreeXTree(XBit tree); +#endif + +XML_API XBit ParseDtd(Parser p, Entity e); + +XML_API void ParserSetWarningCallback(Parser p, CallbackProc cb); +XML_API void ParserSetDtdCallback(Parser p, CallbackProc cb); +XML_API void ParserSetEntityOpener(Parser p, EntityOpenerProc opener); +XML_API void ParserSetCallbackArg(Parser p, void *arg); + +XML_API int ParserPush(Parser p, InputSource source); +XML_API void ParserPop(Parser p); + +XML_API void ParserSetFlag(Parser p, ParserFlag flag, int value); +#define ParserGetFlag(p, flag) ((p)->flags & (1 << (flag))) + +XML_API void ParserPerror(Parser p, XBit bit); + +#endif /* XMLPARSER_H */ diff --git a/aeneas/cfw/speech_tools/sigpr/EST_Window.h b/aeneas/cfw/speech_tools/sigpr/EST_Window.h new file mode 100644 index 00000000..511cedf5 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_Window.h @@ -0,0 +1,233 @@ +/************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996,1997 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/************************************************************************/ +/* */ +/* Author: Richard Caley and Paul Taylor */ +/* Date: May 1997, April 98 */ +/* ------------------------------------------------------------------- */ +/* Class for windowing speech waveforms */ +/* */ +/************************************************************************/ + +#ifndef __EST_WINDOW_H__ +#define __EST_WINDOW_H__ + +#include "EST_TBuffer.h" +#include "EST_Wave.h" + +/**@name Function types used for parameters to functions. +*/ +//@{ + +/// Function which creates a window. +typedef void EST_WindowFunc(int size, EST_TBuffer &r_window, int window_centre ); + +//@} + + +/** The EST_Window class provides functions for the creation and use +of signal processing windows. + +Signal processing algorithms often work by on small sections of the +speech waveform known as {\em frames}. A full signal must first be +divided into these frames before these algorithms can work. While it +would be simple to just "cut out" the required frames from the +waveforms, this is usually undesirable as large discontinuities can +occur at the frame edges. Instead it is customary to cut out the frame +by means of a \{em window} function, which tapers the signal in the +frame so that it has high values in the middle and low or zero values +near the frame edges. The \Ref{EST_Window} class provides a wrap +around for such windowing operations. + +There are several types of window function, including: + +\begin{itemize} + +\item {\bf Rectangular}, which is used to give a simple copy of the the +values between the window limits. + +\[w_{n} = \left\{ \begin{array}{ll} +1 & \mbox{$0 \leq n \leq N$} \\ +0 & \mbox{otherwise} +\end{array} +\right. \] + +\item {\bf Hanning}. The rectangular window can cause sharp discontinuities +at window edges. The hanning window solves this by ensuring that the +window edges taper to 0. + +\[w_{n} = \left\{ \begin{array}{ll} +0.5 - 0.5 \cos(2\pi n / (N-1)) & \mbox{$0 \leq n \leq N$} \\ +0 & \mbox{otherwise} +\end{array} +\right. \] + +\item {\bf Hamming.} The hanning window causes considerable energy +loss, which the hamming window attempts to rectify. + +\[w_{n} = \left\{ \begin{array}{ll} +0.54 - 0.46 \cos(2\pi n / (N-1)) & \mbox{$0 \leq n \leq N$} \\ +0 & \mbox{otherwise} +\end{array} +\right. \] + +\end{itemize} + +The particular choice of window depends on the application. For +instance in most speech synthesis applications Hanning windows are the +most suitable as they don't have time domain discontinuities. For +analysis applications hamming windows are normally used. + + +For example code, see \Ref{Windowing} + + +*/ + +class EST_Window { +public: + + /// A function which creates a window + typedef EST_WindowFunc Func; + + /**@name Functions for making windows. + + */ + //@{ + + /** Make a Buffer of containing a window function of specified type. + If window_centre < 0 (default -1), then a symmetric window is + returned. For positive values of the window_centre argument, + asymmetric windows are returned. + */ + static void make_window(EST_TBuffer &window_vals, int size, + const char *name, int window_centre); + + /** Make a EST_FVector containing a window function of specified type. + If window_centre < 0 (default -1), then a symmetric window is + returned. For positive values of the window_centre argument, + asymmetric windows are returned. + */ + static void make_window(EST_FVector &window_vals, int size, + const char *name, int window_centre); + + /// Return the creation function for the given window type. + static Func *creator(const char *name, bool report_error = false); + //@} + +/** @name Performing windowing on a section of speech. + + */ + +//@{ + + /** Window the waveform {\tt sig} starting at point {\tt start} for + a duration of {\tt size} samples. The windowing function required + is given as a function pointer {\tt *make_window} which has + already been created by a function such as \Ref{creator}. + The output windowed frame is placed in the buffer {\tt frame} which + will have been resized accordingly within the function. + */ + + static void window_signal(const EST_Wave &sig, + EST_WindowFunc *make_window, + int start, int size, + EST_TBuffer &frame); + + /** Window the waveform {\tt sig} starting at point {\tt start} for + a duration of {\tt size} samples. The windowing function required + is given as a function pointer {\tt *make_window} which has + already been created by a function such as \Ref{creator}. + The output windowed frame is placed in the EST_FVector {\tt frame}. + By default, it is assumed that this is already the correct size + (i.e. {\tt size} samples long), but if resizing is required the + last argument should be set to 1. + */ + + static void window_signal(const EST_Wave &sig, + EST_WindowFunc *make_window, + int start, int size, + EST_FVector &frame,int resize=0); + + /** Window the waveform {\tt sig} starting at point {\tt start} for + a duration of {\tt size} samples. The windowing function required + is given as a string: this function will make a temporary window + of this type. The output windowed frame is placed in the + EST_FVector {\tt frame}. By default, it is assumed that this is + already the correct size (i.e. {\tt size} samples long), but if + resizing is required the last argument should be set to 1. */ + + static void window_signal(const EST_Wave &sig, + const EST_String &window_name, + int start, int size, + EST_FVector &frame, int resize=0); + + + /** Window the waveform {\tt sig} starting at point {\tt start} for + a duration of {\tt size} samples. The window shape required + is given as an array of floats. The output windowed frame is placed in the + EST_FVector {\tt frame}. By default, it is assumed that this is + already the correct size (i.e. {\tt size} samples long), but if + resizing is required the last argument should be set to 1. */ + static void window_signal(const EST_Wave &sig, + EST_TBuffer &window_vals, + int start, int size, + EST_FVector &frame, int resize=0); + +//@} + + +/**@name Utility window functions. + +*/ +//@{ + /// Return the description for a given window type. + static EST_String description(const char *name); + + /// Return a paragraph describing the available windows. + static EST_String options_supported(void); + + /// Return a comma separated list of the available window types. + static EST_String options_short(void); + +//@} +}; + +///For example code, see \Ref{Windowing}. + +//@see Windowing mechanisms + + + + + +#endif diff --git a/aeneas/cfw/speech_tools/sigpr/EST_fft.h b/aeneas/cfw/speech_tools/sigpr/EST_fft.h new file mode 100644 index 00000000..640990fe --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_fft.h @@ -0,0 +1,137 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + + +#ifndef __EST_FFT_H__ +#define __EST_FFT_H__ + +#include "EST_Wave.h" +#include "EST_Track.h" +#include "EST_FMatrix.h" + +/**@name Fast Fourier Transform functions + + +These are the low level functions where the actual FFT is +performed. Both slow and fast implementations are available for +historical reasons. They have identical functionality. At this time, +vectors of complex numbers are handled as pairs of vectors of real and +imaginary numbers. + + + What is a Fourier Transform ? + + +The Fourier transform of a signal gives us a frequency-domain +representation of a time-domain signal. In discrete time, the Fourier +Transform is called a Discrete Fourier Transform (DFT) and is given +by: + +\[y_k = \sum_{t=0}^{n-1} x_t \; \omega_{n}^{kt} \; ; \; k=0...n-1 \] + +where \(y = (y_0,y_1,... y_{n-1})\) is the DFT (of order \(n\) ) of the +signal \(x = (x_0,x_1,... x_{n-1})\), where +\(\omega_{n}^{0},\omega_{n}^{1},... \omega_{n}^{n-1}\) are the n +complex nth roots of 1. + + + +The Fast Fourier Transform (FFT) is a very efficient implementation of +a Discrete Fourier Transform. See, for example "Algorithms" by Thomas +H. Cormen, Charles E. Leiserson and Ronald L. Rivest (pub. MIT Press), +or any signal processing textbook. + + + + +*/ + +//@{ + +/** Basic in-place FFT. + +There's no point actually using this - use \Ref{fastFFT} +instead. However, the code in this function closely matches the +classic FORTRAN version given in many text books, so is at least easy +to follow for new users. + +The length of real and +imag must be the same, and must be a power of 2 +(e.g. 128). + +@see slowIFFT +@see FastFFT */ +int slowFFT(EST_FVector &real, EST_FVector &imag); + +/** Alternate name for slowFFT +*/ +inline int FFT(EST_FVector &real, EST_FVector &imag){ + return slowFFT(real, imag); +} + +/** Basic inverse in-place FFT +int slowFFT +*/ +int slowIFFT(EST_FVector &real, EST_FVector &imag); + +/** Alternate name for slowIFFT +*/ +inline int IFFT(EST_FVector &real, EST_FVector &imag){ + return slowIFFT(real, imag); +} + +/** Power spectrum using the fastFFT function. +The power spectrum is simply the squared magnitude of the +FFT. The result real and imaginary parts are both set equal +to the power spectrum (you only need one of them !) +*/ +int power_spectrum(EST_FVector &real, EST_FVector &imag); + +/** Power spectrum using the slowFFT function +*/ +int power_spectrum_slow(EST_FVector &real, EST_FVector &imag); + +/** Fast FFT +An optimised implementation by Tony Robinson to be used +in preference to slowFFT +*/ +int fastFFT(EST_FVector &invec); + +// Auxiliary for fastFFT +int fastlog2(int); + +//@} + + +#endif // __EST_FFT_H__ + diff --git a/aeneas/cfw/speech_tools/sigpr/EST_filter.h b/aeneas/cfw/speech_tools/sigpr/EST_filter.h new file mode 100644 index 00000000..e70d0c9e --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_filter.h @@ -0,0 +1,404 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + + + + +#ifndef __EST_FILTER_H__ +#define __EST_FILTER_H__ + +#include "EST_Wave.h" +#include "EST_FMatrix.h" +#include "EST_Track.h" + +#define DEFAULT_PRE_EMPH_FACTOR 0.95 +#define DEFAULT_FILTER_ORDER 199 + +/**@name FIR filters + +Finite impulse response (FIR) filters which are useful for band-pass, +low-pass and high-pass filtering. + +FIR filters perform the following operation: + +\[y_t=\sum_{i=0}^{O-1} c_i \; x_{t-i}\] + +where \(O\) is the filter order, \(c_i\) are the filter coefficients, +\(x_t\) is the input at time \(t\) and \(y_t\) is the output at time +\(t\). Functions are provided for designing the filter (i.e. finding +the coefficients). + +*/ + +//@{ + +/** General purpose FIR filter. This function will filter the +waveform {\tt sig} with a previously designed filter, given as {\tt +numerator}. The filter coefficients can be designed using one of the +designed functions, e.g. \Ref{design_FIR_filter}. + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter + +*/ + +void FIRfilter(EST_Wave &in_sig, const EST_FVector &numerator, + int delay_correction=0); + + +/** General purpose FIR filter. This function will filter the +waveform {\tt sig} with a previously designed filter, given as {\tt +numerator}. The filter coefficients can be designed using one of the +designed functions, e.g. \Ref{design_FIR_filter} . + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter +*/ + +void FIRfilter(const EST_Wave &in_sig, EST_Wave &out_sig, + const EST_FVector &numerator, int delay_correction=0); + +/** General purpose FIR double (zero-phase) filter. This function +will double filter the waveform {\tt sig} with a previously designed +filter, given as {\tt numerator}. The filter coefficients can be +designed using one of the designed functions, +e.g. \Ref{design_FIR_filter}. Double filtering is performed by +filtering the signal normally, reversing the waveform, filtering +again and reversing the waveform again. Normal filtering will impose a +lag on the signal depending on the order of the filter. By filtering +the signal forwards and backwards, the lags cancel each other out and +the output signal is in phase with the input signal. + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter +*/ +void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig, + const EST_FVector &numerator); + +/** Quick function for one-off low pass filtering. If repeated lowpass +filtering is needed, first design the required filter using +\Ref{design_lowpass_filter}, and then use \Ref{FIRfilter} to do the actual +filtering. + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, FIRfilter, FIRhighpass_filter, FIRlowpass_filter + +@param in_sig input waveform, which will be overwritten +@param freq +@param order number of filter coefficients, eg. 99 +*/ + +void FIRlowpass_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER); + +/** Quick function for one-off low pass filtering. If repeated lowpass +filtering is needed, first design the required filter using +\Ref{design_lowpass_filter}, and then use \Ref{FIRfilter} to do the actual +filtering. + +@param in_sig input waveform +@param out_sig output waveform +@param freq cutoff frequency in Hertz +@param order number of filter coefficients , e.g. 99 + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, FIRfilter, FIRhighpass_filter +*/ + +void FIRlowpass_filter(const EST_Wave &in_sig, EST_Wave &out_sig, + int freq, int order=DEFAULT_FILTER_ORDER); + +/** Quick function for one-off high pass filtering. If repeated lowpass +filtering is needed, first design the required filter using +design_lowpass_filter, and then use FIRfilter to do the actual +filtering. + +@param in_sig input waveform, which will be overwritten +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter +*/ + +void FIRhighpass_filter(EST_Wave &in_sig, int freq, int order); + +/** Quick function for one-off high pass filtering. If repeated highpass +filtering is needed, first design the required filter using +design_highpass_filter, and then use FIRfilter to do the actual +filtering. + +@param in_sig input waveform +@param out_sig output waveform +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter +*/ +void FIRhighpass_filter(const EST_Wave &sigin, EST_Wave &out_sig, + int freq, int order=DEFAULT_FILTER_ORDER); + + +/** Quick function for one-off double low pass filtering. + +Normal low pass filtering (\Ref{FIRlowpass_filter}) introduces a time delay. +This function filters the signal twice, first forward and then backwards, +which ensures a zero phase lag. Hence the order parameter need only be +half what it is for (\Ref{FIRlowpass_filter} to achieve the same effect. + +@param in_sig input waveform, which will be overwritten +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see FIRhighpass_filter +*/ +void FIRhighpass_double_filter(EST_Wave &sigin, int freq, + int order=DEFAULT_FILTER_ORDER); + +/** Quick function for one-off double low pass filtering. + +Normal low pass filtering (\Ref{FIRlowpass_filter}) introduces a time delay. +This function filters the signal twice, first forward and then backwards, +which ensures a zero phase lag. Hence the order parameter need only be +half what it is for (\Ref{FIRlowpass_filter} to achieve the same effect. + +@param in_sig input waveform +@param out_sig output waveform +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see FIRhighpass_filter + +*/ +void FIRhighpass_double_filter(const EST_Wave &int_sig, EST_Wave &out_sig, + int freq, int order=DEFAULT_FILTER_ORDER); + +/** Quick function for one-off zero phase high pass filtering. + +Normal high pass filtering (\Ref{FIRhighpass_filter}) introduces a time delay. +This function filters the signal twice, first forward and then backwards, +which ensures a zero phase lag. Hence the order parameter need only be +half what it is for (\Ref{FIRhighpass_filter} to achieve the same effect. + +@param in_sig input waveform, which will be overwritten +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see FIRlowpass_filter +*/ +void FIRlowpass_double_filter(EST_Wave &sigin, int freq, + int order=DEFAULT_FILTER_ORDER); + +/** Quick function for one-off zero phase high pass filtering. + +Normal high pass filtering (\Ref{FIRhighpass_filter}) introduces a time delay. +This function filters the signal twice, first forward and then backwards, +which ensures a zero phase lag. Hence the order parameter need only be +half what it is for (\Ref{FIRhighpass_filter} to achieve the same effect. + +@param in_sig input waveform +@param out_sig output waveform +@param freq cutoff frequency in Hertz +@param order number of filter coefficients, eg. 99 + +@see FIRlowpass_filter +*/ +void FIRlowpass_double_filter(const EST_Wave &in_sig, EST_Wave &out_sig, + int freq, int order=DEFAULT_FILTER_ORDER); + +//@} + +/**@name Linear Prediction filters + +The linear prediction filters are used for the analysis and synthesis of +waveforms according the to linear prediction all-pole model. + +The linear prediction states that the value of a signal at a given +point is equal to a weighted sum of the previous P values, plus a +correction value for that point: + +\[s_{n} = \sum_{i=1}^{P} a_{i}.s_{n-i} + e_{n}\] + +Given a set of coefficients and the original signal, we can use this +equation to work out e, the {\it residual}. Conversely given the +coefficients and the residual signal, an estimation of the original +signal can be calculated. + +If a single set of coefficients were used for the entire waveform, the +filtering process would be simple. It is usual however to have a +different set of coefficients for every frame, and there are many +possible ways to switch from one coefficient set to another so as not +to cause discontinuities at the frame boundaries. +*/ + +//@{ + +/** Synthesize a signal from a single set of linear prediction +coefficients and the residual values. + +@param sig the waveform to be synthesized +@param a a single set of LP coefficients +@param res the input residual waveform +*/ +void lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res); + +/** Filter the waveform using a single set of coefficients so as to +produce a residual signal. + +@param sig the speech waveform to be filtered +@param a a single set of LP coefficients +@param res the output residual waveform +*/ + +void inv_lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res); + +/** Synthesize a signal from a track of linear prediction coefficients. +This function takes a set of LP frames and a residual and produces a +synthesized signal. + +For each frame, the function picks an end point, which is half-way +between the current frame's time position and the next frame's. A +start point is defined as being the previous frame's end. Using these +two values, a portion of residual is extracted and passed to +\Ref{lpc_filter} along with the LP coefficients for that frame. This +function writes directly into the signal for the values between start +and end; + +@param sig the waveform to be synthesized +@param lpc a track of time positioned LP coefficients +@param res the input residual waveform +*/ + +void lpc_filter_1(EST_Track &lpc, EST_Wave & res, EST_Wave &sig); + +/** Synthesize a signal from a track of linear prediction coefficients. +This function takes a set of LP frames and a residual and produces a +synthesized signal. + +This is functionally equivalent to \Ref{lpc_filter_1} except it +reduces the residual by 0.5 before filtering. Importantly it is +about three times faster than \Ref{lpc_filter_1} but in doing so uses +direct C buffers rather than the neat C++ access function. This +function should be regarded as temporary and will be deleted after +we restructure the low level classes to give better access. + +@param sig the waveform to be synthesized +@param lpc a track of time positioned LP coefficients +@param res the input residual waveform +*/ + +void lpc_filter_fast(EST_Track &lpc, EST_Wave & res, EST_Wave &sig); + +/** Produce a residual from a track of linear prediction coefficients +and a signal using an overlap add technique. + +For each frame, the function estimates the local pitch period and +picks a start point one period before the current time position and an +end point one period after it. + +A portion of residual corresponding to these times is then produced +using \Ref{inv_lpc_filter}. The resultant section of residual is then +overlap-added into the main residual wave object. + +@param sig the speech waveform to be filtered +@param lpc a track of time positioned LP coefficients +@param res the output residual waveform +*/ + +void inv_lpc_filter_ola(EST_Wave &sig, EST_Track &lpc, EST_Wave &res); + +//@} + +/**@name Pre/Post Emphasis filters. + +These functions adjust the spectral tilt of the input waveform. + +*/ + +//@{ + +/** Pre-emphasis filtering. This performs simple high pass +filtering with a one tap filter of value {\tt a}. Normal values of a +range between 0.95 and 0.99. */ + +void pre_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR); + +/** Pre-emphasis filtering. This performs simple high pass +filtering with a one tap filter of value {\tt a}. Normal values of a +range between 0.95 and 0.99. */ + + +void pre_emphasis(EST_Wave &sig, EST_Wave &out, + float a=DEFAULT_PRE_EMPH_FACTOR); + +/** Post-emphasis filtering. This performs simple low pass +filtering with a one tap filter of value a. Normal values of a range +between 0.95 and 0.99. The same values of {\tt a} should be used when +pre- and post-emphasizing the same signal. */ + +void post_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR); + +/** Post-emphasis filtering. This performs simple low pass +filtering with a one tap filter of value a. Normal values of a range +between 0.95 and 0.99. The same values of {\tt a} should be used when +pre- and post-emphasizing the same signal. */ + +void post_emphasis(EST_Wave &sig, EST_Wave &out, + float a=DEFAULT_PRE_EMPH_FACTOR); + +//@} + +/**@name Miscellaneous filters. + +Some of these filters are non-linear and therefore don't fit the +normal paradigm. + +*/ //@{ + +/** Filters the waveform by means of median smoothing. + +This is a sort of low pass filter which aims to remove extreme values. +Median smoothing works examining each sample in the wave, taking all +the values in a window of size {\tt n} around that sample, sorting +them and replacing that sample with the middle ranking sample in the +sorted samples. + +@param sig waveform to be filtered +@param n size of smoothing window + +*/ + +void simple_mean_smooth(EST_Wave &c, int n); + +//@} + +#endif /* __EST_FILTER_H__ */ + diff --git a/aeneas/cfw/speech_tools/sigpr/EST_filter_design.h b/aeneas/cfw/speech_tools/sigpr/EST_filter_design.h new file mode 100644 index 00000000..1392a6d8 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_filter_design.h @@ -0,0 +1,97 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + +#ifndef __EST_FILTER_DESIGN_H__ +#define __EST_FILTER_DESIGN_H__ + +#include "EST_Wave.h" +#include "EST_FMatrix.h" +#include "EST_Track.h" + +/**@name Filter Design + +FIR Filtering is a 2 stage process, first involving design and then +the filtering itself. As the design is somewhat costly, it is usually +desirable to design a filter outside the main loop. + +For one off filtering operations, functions are +provided which design and filter the waveform in a single go. + +It is impossible to design an ideal filter, i.e. one which exactly +obeys the desired frequency response. The "quality" of a filter is +given by the order parameter, with high values indicating good +approximations to desired responses. High orders are slower. The +default is 199 which gives a pretty good filter, but a value as low as +19 is still usable if speech is important. + +*/ +//@{ + +/** Create an arbitrary filter or order {\tt order} that attempts to +give the frequency response given by {\tt freq_response}. The vector +{\tt freq_response} should be any size 2**N and contain a plot of the +desired frequency response with values ranging between 0.0 and +1.0. The actual filtering is done by \Ref{FIRfilter}. + +@see design_lowpass_FIR_filter, design_highpass_FIR_filter +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter + +*/ +EST_FVector design_FIR_filter(const EST_FVector &freq_response, int + filter_order); + +/** Design a FIR lowpass filter of order {\tt order} and cut-off +frequency {\tt freq}. The filter coefficients are returned in the +FVector and should be used in conjunction with \Ref{FIRfilter}. + +@see design_FIR_filter, design_highpass_FIR_filter, FIRfilter, +FIRlowpass_filter, FIRhighpass_filter +*/ + +EST_FVector design_lowpass_FIR_filter(int sample_rate, int freq, int + order); + +/** Design a FIR highpass filter of order {\tt order} and cut-off frequency +{\tt freq}. The filter coefficients are returned in the FVector and should be used in conjunction with \Ref{FIRfilter} +@see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter +@see FIRfilter, FIRlowpass_filter, FIRhighpass_filter + +*/ +EST_FVector design_highpass_FIR_filter(int sample_rate, int + freq, int order); + +//@} + + +#endif /* __EST_FILTER_DESIGN_H__ */ + diff --git a/aeneas/cfw/speech_tools/sigpr/EST_misc_sigpr.h b/aeneas/cfw/speech_tools/sigpr/EST_misc_sigpr.h new file mode 100644 index 00000000..77b64b33 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_misc_sigpr.h @@ -0,0 +1,68 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_MISC_SIGPR_H__ +#define __EST_MISC_SIGPR_H__ + +/** Some generally useful things. + * + * @author Richard Caley + * @version $Id: EST_misc_sigpr.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +#include "EST_Wave.h" + +/**@name Miscellaneous Signal Processing Functions. + * + * Some Functions which deon't fit elsewhere. + */ +//@{ + +/**@name Preemphasis + * + * Used in LPC analysis and spectrogram creation. + * These two functions are inverses of one another. + */ +//@{ + +/// Pre process to emphasise higher frequencies +void EST_pre_emphasis(EST_Wave &signal, EST_Wave &psignal, float a); +/// Post process to get the original back (eg after resynthesis). +void EST_post_deemphasis(EST_Wave &signal, EST_Wave &dsignal, float a); +//@} + +//@} + +#endif + diff --git a/aeneas/cfw/speech_tools/sigpr/EST_pitchmark.h b/aeneas/cfw/speech_tools/sigpr/EST_pitchmark.h new file mode 100644 index 00000000..217c1719 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_pitchmark.h @@ -0,0 +1,143 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + +#ifndef __EST_PITCHMARK_H__ +#define __EST_PITCHMARK_H__ + +#include "EST_Wave.h" +#include "EST_Option.h" +#include "EST_Track.h" +#include "EST_TBuffer.h" + +#define LX_LOW_FREQUENCY 400 +#define LX_LOW_ORDER 19 +#define LX_HIGH_FREQUENCY 40 +#define LX_HIGH_ORDER 19 +#define DF_LOW_FREQUENCY 1000 +#define DF_LOW_ORDER 19 +#define MIN_PERIOD 0.003 +#define MAX_PERIOD 0.02 +#define DEF_PERIOD 0.01 +#define PM_END -1.0 + +/** @name Pitchmarking Functions + +Pitchmarking involves finding some pre-defined pitch related instant +for every pitch period in the speech. At present, only functions for +analysing laryngograph waveforms are available - the much harder +problem of doing this on actual speech has not been attempted. + + */ +//@{ + +/** Find pitchmarks in Laryngograph (lx) signal. + +This high level function places a pitchmark on each positive peak in +the voiced portions of the lx signal. Pitchmarks are stored in the +time component of a EST_Track object and returned. The function works +by high and low pass filtering the signal using forward and backward +filtering to remove phase shift. The negative going points in the +smoothed differentiated signal, corresponding to peaks in the original +are then chosen. + +@param lx laryngograph waveform +@param op options, mainly for filter control: +\begin{itemize} +\item {\bf lx_low_frequency} low pass cut off for lx filtering : typical value {\tt 400} +\item {\bf lx_low_order} order of low pass lx filter: typical value 19 +\item {\bf lx_high_frequency} high pass cut off for lx filtering: typical value 40 +\item {\bf lx_high_order} order of high pass lx filter: typical value 19 +\item {\bf median_order} order of high pass lx filter: typical value 19 +\end{itemize} +*/ + +EST_Track pitchmark(EST_Wave &lx, EST_Features &op); + +/** Find pitchmarks in Laryngograph (lx) signal. The function is the +same as \Ref{pitchmark} but with more explicit control over +the parameters. + +@param lx laryngograph waveform +@param lx_lf low pass cut off for lx filtering : typical value 400 +@param lx_fo order of low pass lx filter : typical value 19 +@param lx_hf high pass cut off for lx filtering : typical value 40 +@param lx_ho : typical value 19 +@param mo order of median smoother used to smoother differentiated lx : typical value 19 + +*/ + +EST_Track pitchmark(EST_Wave &lx, int lx_lf, int lx_lo, int lx_hf, + int lx_ho, int df_lf, int df_lo, int mo, int debug=0); + + +/** Find times where waveform cross zero axis in negative direction. + +@param sig waveform +@param pm pitchmark track which stores time positions of negative crossings +*/ + +void neg_zero_cross_pick(EST_Wave &lx, EST_Track &pm); + +/** Produce a set of sensible pitchmarks. + +Given a set of raw pitchmarks, this function makes sure no pitch +period is shorter that {\tt min} seconds and no longer than {\tt max} +seconds. Periods that are too short are eliminated. If a period is too +long, extra pitchmarks are inserted whose period is {\it +approximately} {\tt def} seconds in duration. The approximation is to +ensure that the pitch period in the interval, D, is constant, and so +the actual pitch period is given by \[T = D / floor(D/def)\] */ + +void pm_fill(EST_Track &pm, float new_end, float max, float min, float def); + +/** Remove pitchmarks which are too close together. + +This doesn't work in a particularly sophisticated way, in that it +removes a sequence of too close pitchmarks left to right, and doesn't +attempt to find which ones in the sequence are actually spurious. */ + +void pm_min_check(EST_Track &pm, float min); + + +void pm_to_f0(EST_Track &pm, EST_Track &f0); + +// for constant shift pitchmarks +void pm_to_f0(EST_Track &pm, EST_Track &fz, float shift); + + +//@} + +#endif /* __EST_PITCHMARK_H__ */ + + +//@} diff --git a/aeneas/cfw/speech_tools/sigpr/EST_sigpr_frame.h b/aeneas/cfw/speech_tools/sigpr/EST_sigpr_frame.h new file mode 100644 index 00000000..7bc16877 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_sigpr_frame.h @@ -0,0 +1,323 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + +#ifndef __EST_SIGPR_FRAME_H__ +#define __EST_SIGPR_FRAME_H__ + +#include "EST_FMatrix.h" + + + +/**@name Linear Prediction functions +Including, generation of coefficients from the signal, reflection +coefficients, line spectral frequencies, areas. +*/ +//@{ + +/** Produce the full set of linear prediction coefficients from a + frame of speech waveform. + +@param sig: the frame of input waveform +@param acf: the autocorrelation coefficients +@param ref: the reflection coefficients +@param lpc: the LPC coefficients + +The order of the lpc analysis is given as the size of the +lpc vector - 1. The coefficients are placed in the +locations 1 - size, and the energy is placed in location 0. +*/ + +void sig2lpc(const EST_FVector &sig, EST_FVector &acf, + EST_FVector &ref, EST_FVector &lpc); + + +/** Calculate cepstral coefficients from lpc coefficients. + +It is possible to calculate a set of cepstral coefficients from +lpc coefficients using the relationship: + +\[c_{k}= a_{k} + \frac{1}{k}\sum_{i=1}^{k-1} i c_{i} a_{k-1}\] + +The order of the cepstral analysis can be different from the lpc +order. If the cepstral order is greater, interpolation is used (FINISH +add equation). Both orders are taken from the lengths of the +respective vectors. Note that these cepstral coefficients take on the +assumptions (and errors) of the lpc model and hence will not be the +same as cepstral coefficients calculated using DFT functions. + +@param lpc: the LPC coefficients (input) +@param lpc: the cepstral coefficients (output) +*/ + +void lpc2cep(const EST_FVector &lpc, EST_FVector &cep); + + + +/** Produce a set linear prediction coefficients from a + frame of speech waveform. {\tt sig} is the frame of input waveform, + and {\tt lpc} are the LPC coefficients. The + {\bf order} of the lpc analysis is given as the size of the {\tt lpc} + vector -1. The coefficients are placed in the locations 1 - size, and + the energy is placed in location 0. +*/ +void sig2lpc(const EST_FVector &sig, EST_FVector &lpc); + +/** Produce a set of reflection coefficients from a + frame of speech waveform. {\tt sig} is the frame of input waveform, + and {\tt ref} are the LPC coefficients. The + {\bf order} of the lpc analysis is given as the size of the {\tt lpc} + vector -1. The coefficients are placed in the locations 1 - size, and + the energy is placed in location 0. +*/ +void sig2ref(const EST_FVector &sig, EST_FVector &ref); + + +/**@name Area Functions +Using the analogy of the lossless tube, the +cross-sectional areas of the sections of this tube are related to the reflection coefficients and can be calculated from the following relationship: + +\[\frac{A_{i+1}}{A_{i}} = \frac{i - k_{i}}{1 + k_{i}} \] + +*/ +//@{ +/** The area according to the formula. */ +void ref2truearea(const EST_FVector &ref, EST_FVector &area); + +/** An approximation of the area is calculate by skipping the denominator +in the formula. */ +void ref2area(const EST_FVector &ref, EST_FVector &area); + +/** The logs of the areas. */ +void ref2logarea(const EST_FVector &ref, EST_FVector &logarea); +//@} + +/** Calculate the reflection coefficients from the lpc +coefficients. Note that in the standard linear prediction analysis, +the reflection coefficients are generated as a by-product. @see +sig2lpc */ + +void lpc2ref(const EST_FVector &lpc, EST_FVector &ref); + +/** Calculate the linear prediction coefficients from the reflection +coefficients. +Use the equation: +\[power=\frac{1}{n}\sum_{i=1}^{n}a_{i}^2\] + +@see lpc2ref*/ + +void ref2lpc(const EST_FVector &ref, EST_FVector &lpc); + +/** Calculate line spectral frequencies from linear prediction coefficients. +Use the equation: +\[power=\frac{1}{n}\sum_{i=1}^{n}a_{i}^2\] + +@see lsf2lpc +*/ + +void lpc2lsf(const EST_FVector &lpc, EST_FVector &lsf); + +/** Calculate line spectral frequencies from linear prediction coefficients. +Use the equation: +\[power=\frac{1}{n}\sum_{i=1}^{n}a_{i}^2\] + +@see lpc2lsf +*/ + +void lsf2lpc(const EST_FVector &lsf, EST_FVector &lpc); +//@} + +void frame_convert(const EST_FVector &in_frame, const EST_String &in_type, + EST_FVector &out_frame, const EST_String &out_type); + + + +// end of lpc functions + +/**@name Energy and power frame functions +*/ + +//@{ + +/** Calculate the power for a frame of speech. This is defined as +\[power=\frac{1}{n}\sum_{i=1}^{n}a_{i}^2\] +*/ + + +void sig2pow(EST_FVector &frame, float &power); + +/** Calculate the root mean square energy for a frame of speech. This +is defined as \[energy=\sqrt{\frac{1}{n}\sum_{i=1}^{n}a_{i}^2}\] */ + +void sig2rms(EST_FVector &frame, float &rms_energy); + +//@} +// end of power and energy + +/**@name Frame based filter bank and cepstral analysis + +These functions are \Ref{Frame based signal processing functions}. +*/ + +//@{ + +/** Calculate the (log) energy (or power) in each channel of a Mel +scale filter bank for a frame of speech. The filters are triangular, are +evenly spaced and are all of equal width, on a Mel scale. The upper and lower +cutoffs of each filter are at the centre frequencies of the adjacent filters. +The Mel scale is described under {\tt Hz2Mel}. + +@see Hz2Mel +@see sig2fft +@see fft2fbank +*/ + +void sig2fbank(const EST_FVector &sig, + EST_FVector &fbank_frame, + const float sample_rate, + const bool use_power_rather_than_energy, + const bool take_log); + +/** Calculate the energy (or power) spectrum of a frame of speech. The FFT +order is determined by the number of samples in the frame of speech, and is +a power of 2. Note that the FFT vector returned corresponds to frequencies +from 0 to half the sample rate. Energy is the magnitude of the FFT; power is +the squared magnitude. + +@see fft2fbank +@see sig2fbank +*/ + +void sig2fft(const EST_FVector &sig, + EST_FVector &fft_vec, + const bool use_power_rather_than_energy); + +/** Given a Mel filter bank description, bin the FFT coefficients +to compute the output of the filters. The first and last elements of +{\tt mel_fbank_frequencies} define the lower and upper bound of +the first and last filters respectively and the intervening elements +give the filter centre frequencies. That is, {\tt mel_fbank_frequencies} has +two more elements than {\tt fbank_vec}. + +@see fastFFT +@see sig2fft +@see sig2fbank +@see fbank2melcep +*/ + +void fft2fbank(const EST_FVector &fft_frame, + EST_FVector &fbank_vec, + const float Hz_per_fft_coeff, + const EST_FVector &mel_fbank_frequencies); + +/** Compute the discrete cosine transform of log Mel-scale filter bank output +to get the Mel cepstral coefficients for a frame of speech. +Optional liftering (filtering in the cepstral domain) can be applied to +normalise the magnitudes of the coefficients. This is useful because, +typically, the higher order cepstral coefficients are significantly +smaller than the lower ones and it is often desirable to normalise +the means and variances across coefficients. + +The lifter (cepstral filter) used is: +\[c_i' = \{ 1 + \frac{L}{2} sin \frac{\Pi i}{L} \} \; c_i\] + +A typical value of L used in speech recognition is 22. A value of L=0 is taken +to mean no liftering. This is equivalent to L=1. + +@see sig2fft +@see fft2fbank +@see sig2fbank +*/ + +void fbank2melcep(const EST_FVector &fbank_vec, + EST_FVector &mfcc, + const float liftering_parameter, + const bool include_c0 = false); + +/** Make a triangular Mel scale filter. The filter is centred at +{\tt this_mel_centre} and +extends from {\tt this_mel_low} to {\tt this_mel_high}. {\tt half_fft_order} +is the length of a power/energy spectrum covering 0Hz to half the sampling +frequency with a resolution of {\tt Hz_per_fft_coeff}. + +The routine returns a vector of weights to be applied to the energy/power +spectrum starting at element {\tt fft_index_start}. +The number of points (FFT coefficients) covered +by the filter is given by the length of the returned vector {\tt filter}. + +@see fft2fbank +@see Hz2Mel +@see Mel2Hz +*/ + +void make_mel_triangular_filter(const float this_mel_centre, + const float this_mel_low, + const float this_mel_high, + const float Hz_per_fft_coeff, + const int half_fft_order, + int &fft_index_start, + EST_FVector &filter); + +/**@name Frequency conversion functions + +These are functions used in \Ref{Filter bank and cepstral analysis}. +*/ + +//@{ + +/** Convert Hertz to Mel. The Mel scale is defined by +\[f_{\mbox{Mel}} = 1127 \; log( 1 + \frac{f_{\mbox{Hertz}}}{700} )\] + +@see Mel2Hz +@see Frequency conversion functions +*/ + +float Hz2Mel(float frequency_in_Hertz); + +/** +Convert Mel to Hertz. + +@see Hz2Mel +*/ + +float Mel2Hz(float frequency_in_Mel); + +//@} +// end of frequency conversion functions + +//@} +// end of filter bank and cepstral analysis + + + + +#endif /* __EST_SIGPR_FRAME_H__ */ diff --git a/aeneas/cfw/speech_tools/sigpr/EST_sigpr_utt.h b/aeneas/cfw/speech_tools/sigpr/EST_sigpr_utt.h new file mode 100644 index 00000000..4de699e8 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_sigpr_utt.h @@ -0,0 +1,454 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1995,1996 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ + +#ifndef __EST_SIGPR_UTT_H__ +#define __EST_SIGPR_UTT_H__ + +#include "sigpr/EST_sigpr_frame.h" +#include "sigpr/EST_Window.h" +#include "EST_Track.h" +#include "EST_Wave.h" + +#define DEFAULT_WINDOW_NAME "hamming" +#define DEFAULT_FRAME_FACTOR 2.0 + +/* Note: some of these functions deliberately don't have + doc++ style comments, mainly because they are, or will be + superseded soon. +*/ + +/**@name Functions for use with frame based processing + +In the following functions, the input is a \Ref{EST_Wave} waveform, +and the output is a (usually multi-channel) \Ref{EST_Track}. The +track must be set up appropriately before hand. This means the track +must be resized accordingly with the correct numbers of frame and +channels. + +The positions of the frames are found by examination of the {\bf time} +array in the EST_Track, which must be filled prior to the function +call. The usual requirement is for fixed frame analysis, where each +analysis frame is, say, 10ms after the previous one. + +A common alternative is to perform pitch-synchronous +analysis where the time shift is related to the local pitch period. + +*/ + +//@{ + +/** Produce a single set of coefficients from a waveform. The type of + coefficient required is given in the argument type. + Possible types are: + + + +lpclinear predictive coding + +cepcepstrum coding from lpc coefficients + +melcepMel scale cepstrum coding via fbank + +fbankMel scale log filterbank analysis + +lsfline spectral frequencies + +refLinear prediction reflection coefficients + +power + +f0srpd algorithm + +energyroot mean square energy + + + +The order of the analysis is calculated from the number of +channels in fv. The positions of the analysis +windows must be given by filling in the track's time array. + +This function windows the waveform at the intervals given by the track +time array. The length of each window is factor +* the local time shift. The windowing function is giveb by +wf. + +@param sig: input waveform +@param fv: output coefficients. These have been pre-allocated and the + number of channels in a indicates the order of the analysis. +@param type: the types of coefficients to be produced. "lpc", "cep" etc +@param factor: the frame length factor, i.e. the analysis frame length + will be this times the local pitch period. + +@param wf: function for windowing. See \Ref{Windowing mechanisms} +*/ + +void sig2coef(EST_Wave &sig, EST_Track &a, EST_String type, + float factor = 2.0, + EST_WindowFunc *wf = EST_Window::creator(DEFAULT_WINDOW_NAME)); + +/** Produce multiple coefficients from a waveform by repeated calls to + sig2coef. + +@param sig: input waveform +@param fv: output coefficients. These have been pre-allocated and the + number of channels in a indicates the order of the analysis. +@param op: Features structure containing options for analysis order, + frame shift etc. +@param slist: list of types of coefficients required, from the set of +possible types that sig2coef can take. +*/ + +void sigpr_base(EST_Wave &sig, EST_Track &fv, EST_Features &op, + const EST_StrList &slist); + +/** Calculate the power for each frame of the waveform. + +@param sig: input waveform +@param a: output power track +@param factor: the frame length factor, i.e. the analysis frame length + will be this times the local pitch period. +*/ + +void power(EST_Wave &sig, EST_Track &a, float factor); + +/** Calculate the rms energy for each frame of the waveform. + +This function calls +\Ref{sig2energy} + + +@param sig input waveform +@param a output coefficients +@param factor optional: the frame length factor, i.e. the analysis frame length + will be this times the local pitch period. + +*/ + +void energy(EST_Wave &sig, EST_Track &a, float factor); + + +/** Mel scale filter bank analysis. The Mel scale triangular filters +are computed via an FFT (see \Ref{fastFFT}). This routine is required +for Mel cepstral analysis (see \Ref{melcep}). The analysis of each +frame is done by \Ref{sig2fbank}. + +A typical filter bank analysis for speech recognition might use log +energy outputs from 20 filters. + +@param sig: input waveform +@param fbank: the output. The number of filters is determined from the number + size of this track. +@param factor: the frame length factor, i.e. the analysis frame length + will be this times the local pitch period +@param wf: function for windowing. See \Ref{Windowing mechanisms} +@param up: whether the filterbank analysis should use + power rather than energy. +@param take_log: whether to take logs of the filter outputs + +@see sig2fbank +@see melcep +*/ + +void fbank(EST_Wave &sig, + EST_Track &fbank, + const float factor, + EST_WindowFunc *wf = EST_Window::creator(DEFAULT_WINDOW_NAME), + const bool up = false, + const bool take_log = true); + +/** Mel scale cepstral analysis via filter bank analysis. Cepstral +parameters are computed for each frame of speech. The analysis +requires \Ref{fbank}. The cepstral analysis of the filterbank outputs +is performed by \Ref{fbank2melcep}. + +A typical Mel cepstral coefficient (MFCC) analysis for speech recognition +might use 12 cepstral coefficients computed from a 20 channel filterbank. + + +@param sig input: waveform +@param mfcc_track: the output +@param factor: the frame length factor, i.e. the analysis frame length + will be this times the local pitch period +@param fbank_order: the number of Mel scale filters used for the analysis +@param liftering_parameter: for filtering in the cepstral domain + See \Ref{fbank2melcep} +@param wf: function for windowing. See \Ref{Windowing mechanisms} +@param include_c0: whether the zero'th cepstral coefficient is to be included +@param up: whether the filterbank analysis should use + power rather than energy. + +@see fbank +@see fbank2melcep +*/ + +void melcep(EST_Wave &sig, + EST_Track &mfcc_track, + float factor, + int fbank_order, + float liftering_parameter, + EST_WindowFunc *wf = EST_Window::creator(DEFAULT_WINDOW_NAME), + const bool include_c0 = false, + const bool up = false); + +//@} + + +/**@name Pitch/F0 Detection Algorithm functions + +These functions are used to produce a track of fundamental frequency +(F0) against time of a waveform. +*/ + +//@{ + + +/** Top level pitch (F0) detection algorithm. Returns a track +containing evenly spaced frames of speech, each containing a F0 value +for that point. + +At present, only the \Rref{srpd} pitch tracker is implemented, so +this is always called regardless of what method +is set to. + +@param sig: input waveform +@param fz: output f0 contour +@param op: parameters for pitch tracker +@param method: pda method to be used. +*/ + + +void pda(EST_Wave &sig, EST_Track &fz, EST_Features &op, EST_String method=""); + + +/** Top level intonation contour detection algorithm. Returns a track +containing evenly spaced frames of speech, each containing a F0 for that point. {\tt icda} differs from \Ref{pda} in that the contour is +smoothed, and unvoiced portions have interpolated F0 +values. + +@param sig: input waveform +@param fz: output f0 contour +@param speech: Interpolation is controlled by the speech track. When +a point has a positive value in the speech track, it is a candidate +for interpolation. +@param op: parameters for pitch tracker +@param method: pda method to be used. +*/ + +void icda(EST_Wave &sig, EST_Track &fz, EST_Track &speech, + EST_Option &op, EST_String method = ""); + +/** Create a set sensible defaults for use in pda and icda. + +*/ +void default_pda_options(EST_Features &al); + + +/** Super resolution pitch tracker. + +srpd is a pitch detection algorithm that produces a fundamental +frequency contour from a speech waveform. At present only the super +resolution pitch determination algorithm is implemented. See (Medan, +Yair, and Chazan, 1991) and (Bagshaw et al., 1993) for a detailed +description of the algorithm. + +Frames of data are read in from sig in +chronological order such that each frame is shifted in time from its +predecessor by pda_frame_shift. Each frame is +analysed in turn. + + + +The maximum and minimum signal amplitudes are initially found over the +duration of two segments, each of length N_min samples. If the sum of +their absolute values is below two times +noise_floor, the frame is classified as +representing silence and no coefficients are calculated. Otherwise, a +cross correlation coefficient is calculated for all n from a period in +samples corresponding to min_pitch + to a period in samples corresponding to +max_pitch, in steps +of decimation_factor. In calculating the +coefficient only one in decimation_factor +samples of the two segments are used. Such down-sampling permits rapid +estimates of the coefficients to be calculated over the range +N_min <= n <= N_max. This results in a cross-correlation track for the +frame being analysed. + +Local maxima of the track with a coefficient value above a specified +threshold form candidates for the fundamental period. The threshold is +adaptive and dependent upon the values v2uv_coeff_thresh +, min_v2uv_coef_thresh , and + v2uv_coef_thresh_rati_ratio. If the previously +analysed frame was classified as unvoiced or silent (which is the +initial state) then the threshold is set to +v2uv_coef_thresh. Otherwise, the previous +frame was classified as being voiced, and the threshold is set equal +to [\-r] v2uv_coef_thresh_rati_ratio + times the cross-correlation coefficient +value at the point of the previous fundamental period in the former +coefficients track. This product is not permitted to drop below +v2uv_coef_thresh. + + + +If no candidates for the fundamental period are found, the frame is classified +as being unvoiced. Otherwise, the candidates are further processed to identify +the most likely true pitch period. During this additional processing, a +threshold given by anti_doubling_thres is used. + + + +If the peak_tracking flag is set to true, +biasing is applied to the cross-correlation track as described in +(Bagshaw et al., 1993). + + +@param sig: input waveform +@param op: options regarding pitch tracking parameters +@param op.min_pitch: minimum permitted F0 value +@param op.max_pitch: maximum permitted F0 value +@param op.pda_frame_shift: analysis frame shift +@param op.pda_frame_length: analysis frame length +@param op.lpf_cutoff: cut off frequency for low pass filtering +@param op.lpf_order: order of low pass filtering (must be odd) +@param op.decimation +@param op.noise_floor +@param op.min_v2uv_coef_thresh +@param op.v2uv_coef_thresh_ratio +@param op.v2uv_coef_thresh +@param op.anti_doubling_thresh +@param op.peak_tracking + +*/ +void srpd(EST_Wave &sig, EST_Track &fz, EST_Features &options); + +/** Smooth selected parts of an f0 contour. Interpolation is +controlled by the speech track. When a point has a positive +value in the speech track, it is a candidate for interpolation. +*/ +void smooth_phrase(EST_Track &c, EST_Track &speech, EST_Features &options, + EST_Track &sm); + +/** Smooth all the points in an F0 contour*/ +void smooth_portion(EST_Track &c, EST_Option &op); + +//@} + + +/**@name Delta and Acceleration coefficients + +Produce delta and acceleration coefficients from a set of coefficients +or the waveform. +*/ + +//@{ + +/** Produce a set of delta coefficients for a track + +The delta function is used to produce a set of coefficients which +estimate the rate of change of a set of parameters. The output track +d must be setup before hand, i.e. it must have +the same number of frames and channels as tr. + +@param tr: input track of base coefficients +@param d: output track of delta coefficients. +@param regression_length: number of previous frames on which delta + estimation is calculated on. +*/ + +void delta(EST_Track &tr, EST_Track &d, int regression_length = 3); + +/** Produce multiple sets of delta coefficients from a waveform. + + Calculate specified types of delta coefficients. This function is + used when the base types of coefficients haven't been calculated. + This function calls sig2coef to calculate the base types from which + the deltas are calculated, and hence the requirements governing the + setup of fv for sig2coef also hold here. + +@param sig: input waveform +@param fv: output coefficients. These have been pre-allocated and the + number of channels in a indicates the order of the analysis. +@param op: Features structure containing options for analysis order, + frame shift etc. +@param slist: list of types of delta coefficients required. +*/ + +void sigpr_delta(EST_Wave &sig, EST_Track &fv, EST_Features &op, + const EST_StrList &slist); + +/** Produce multiple sets of acceleration coefficients from a waveform + + Calculate specified types of acceleration coefficients. This function + is used when the base types of coefficient haven't been calculated. + This function calls sig2coef to calculate the base types from which + the deltas are calculated, and hence the requirements governing the + setup of fv for sig2coef also hold here. + +@param sig: input waveform +@param fv: output coefficients. These have been pre-allocated and the + number of channels in a indicates the order of the analysis. +@param op: Features structure containing options for analysis order, + frame shift etc. +@param slist: list of types of acceleration coefficients required. + + +The delta function is used to produce a set of coefficients which +estimate the rate of change of a set of parameters. +*/ + +void sigpr_acc(EST_Wave &sig, EST_Track &fv, EST_Features &op, + const EST_StrList &slist); + +//@} + +/* Convert a track containing coefficients of one type to a track +containing coefficients of another. + +@param in_track input set of coefficients +@param out_track input set of coefficients +@param out_name name of desired output coefficients. +@param in_name optional: often it is possible to determine the type of +the input coefficients from the channel names. If this is not possible or +these names should be ignored, the {\tt in_type} parameter can be used. + +*/ + +void convert_track(EST_Track &in_track, EST_Track &out_track, + const EST_String &out_type, + const EST_String &in_type = ""); + + + +#endif /* __EST_SIGPR_UTT_H__ */ + diff --git a/aeneas/cfw/speech_tools/sigpr/EST_spectrogram.h b/aeneas/cfw/speech_tools/sigpr/EST_spectrogram.h new file mode 100644 index 00000000..ebb09375 --- /dev/null +++ b/aeneas/cfw/speech_tools/sigpr/EST_spectrogram.h @@ -0,0 +1,63 @@ + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + + +#ifndef __EST_SPECTROGRAM_H__ +#define __EST_SPECTROGRAM_H__ + +#include "EST_Wave.h" +#include "EST_Track.h" +#include "EST_Option.h" + +/**@name Spectrogram generation + * + * @author Richard Caley + * @version $Id: EST_spectrogram.h,v 1.2 2001/04/04 13:11:27 awb Exp $ + */ + +//@{ + +/// Compute the power-spectrogram +void raw_spectrogram(EST_Track &sp, EST_Wave &sig, float length, float shift, + int order, bool slow=0); + +/// Manipulate the spectrogram to +void scale_spectrogram(EST_Track &s, float range, float b, float w); + + +void make_spectrogram(EST_Wave &sig, EST_Track &sp, EST_Features &op); + +//@} + +#endif + diff --git a/aeneas/cfw/speech_tools/siod.h b/aeneas/cfw/speech_tools/siod.h new file mode 100644 index 00000000..bbb87c11 --- /dev/null +++ b/aeneas/cfw/speech_tools/siod.h @@ -0,0 +1,214 @@ +/* Scheme In One Defun, but in C this time. + + * COPYRIGHT (c) 1988-1994 BY * + * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. * + * See the source file SLIB.C for more information. * + +*/ + +/*===========================================================*/ +/* */ +/* Public LISP functions */ +/* */ +/*===========================================================*/ +#ifndef __SIOD_H__ +#define __SIOD_H__ + +#include "EST_String.h" +#include "EST_string_aux.h" +#include "EST_error.h" +#include "EST_Val.h" +#include "siod_defs.h" + +int siod_init(int heap_size=DEFAULT_HEAP_SIZE); +int siod_repl(int interactive); +void siod_print_welcome(EST_String extra_info); +void siod_print_welcome(void); + +const char *get_c_string(LISP x); +int get_c_int(LISP x); +double get_c_double(LISP x); +float get_c_float(LISP x); +LISP flocons(double x); +FILE *get_c_file(LISP p,FILE *deflt); +LISP siod_make_typed_cell(long type, void *s); +LISP cintern(const char *name); +LISP rintern(const char *name); +LISP strintern(const char *data); +LISP strcons(long length,const char *data); +LISP cstrcons(const char *data); + +void init_subr(const char *name, long type, SUBR_FUNC fcn, const char *doc); +void init_subr_0(const char *name, LISP (*fcn)(void), const char *doc); +void init_subr_1(const char *name, LISP (*fcn)(LISP), const char *doc); +void init_subr_2(const char *name, LISP (*fcn)(LISP,LISP), const char *doc); +void init_subr_3(const char *name, LISP (*fcn)(LISP,LISP,LISP), const char *doc); +void init_subr_4(const char *name, LISP (*fcn)(LISP,LISP,LISP,LISP), const char *doc); +void init_lsubr(const char *name, LISP (*fcn)(LISP), const char *doc); +void init_fsubr(const char *name, LISP (*fcn)(LISP,LISP), const char *doc); +void init_msubr(const char *name, LISP (*fcn)(LISP *,LISP *), const char *doc); +void setdoc(LISP name,LISP doc); + +int siod_register_user_type(const char *name); +void set_gc_hooks(long type, + int gc_free_once, + LISP (*rel)(LISP), + LISP (*mark)(LISP), + void (*scan)(LISP), + void (*free)(LISP), + void (*clear)(LISP), + long *kind); +void set_eval_hooks(long type,LISP (*fcn)(LISP, LISP *, LISP *)); +void set_type_hooks(long type, long (*c_sxhash)(LISP,long), LISP (*equal)(LISP,LISP)); +void set_print_hooks(long type,void (*prin1)(LISP, FILE *), void (*print_string)(LISP, char *)); +void set_io_hooks(long type, LISP (*fast_print)(LISP,LISP), LISP (*fast_read)(int,LISP)); + +void set_fatal_exit_hook(void (*fcn)(void)); + +extern long nointerrupt; +extern LISP current_env; +extern LISP truth; +extern int audsp_mode; +extern int siod_ctrl_c; +extern const char *siod_prog_name; +extern const char *siod_primary_prompt; +extern const char *siod_secondary_prompt; + +void siod_reset_prompt(void); + +LISP siod_get_lval(const char *name,const char *message); +LISP siod_set_lval(const char *name,LISP val); +LISP siod_assoc_str(const char *key,LISP alist); +LISP siod_member_str(const char *key,LISP list); +LISP siod_regex_member_str(const EST_String &key,LISP list); +EST_Regex &make_regex(const char *r); +LISP siod_member_int(const int key,LISP list); +LISP siod_nth(int nth,LISP list); +LISP siod_last(LISP list); +int siod_llength(LISP list); +int siod_atomic_list(LISP list); +LISP siod_flatten(LISP tree); +int siod_eof(LISP item); +EST_String siod_sprint(LISP exp); +LISP symbol_boundp(LISP x,LISP env); + +LISP get_param_lisp(const char *name, LISP params, LISP defval); +int get_param_int(const char *name, LISP params, int defval); +float get_param_float(const char *name, LISP params, float defval); +const char *get_param_str(const char *name, LISP params,const char *defval); +LISP make_param_int(const char *name, int val); +LISP make_param_float(const char *name, float val); +LISP make_param_str(const char *name,const char *val); +LISP make_param_lisp(const char *name,LISP val); +LISP apply_hooks(LISP hook,LISP arg); +LISP apply_hooks_right(LISP hook,LISP args); +LISP apply(LISP func,LISP args); + +int parse_url(const EST_String &url, + EST_String &protocol, + EST_String &host, + EST_String &port, + EST_String &path); + +LISP err(const char *message, LISP x); +LISP err(const char *message, const char *s); +LISP errswitch(void); + +void siod_list_to_strlist(LISP l, EST_StrList &a); +LISP siod_strlist_to_list(EST_StrList &a); +void siod_tidy_up(); +LISP siod_quit(void); +const char *siod_version(void); + +void gc_protect(LISP *location); +void gc_unprotect(LISP *location); +void gc_protect_n(LISP *location,long n); +void gc_protect_sym(LISP *location,const char *st); +LISP user_gc(LISP args); + +// Siod internal function that lots of people use +LISP equal(LISP,LISP); +LISP eql(LISP x,LISP y); +LISP reverse(LISP obj); +LISP append(LISP l1, LISP l2); +LISP cons(LISP x,LISP y); +LISP car(LISP x); +LISP cdr(LISP x); +LISP consp(LISP x); +LISP numberp(LISP x); +LISP atomp(LISP x); +LISP assoc(LISP x,LISP alist); +LISP setcar(LISP cell, LISP value); +LISP setcdr(LISP cell, LISP value); +LISP assq(LISP x,LISP alist); +LISP delq(LISP elem,LISP l); +LISP leval(LISP x,LISP env); +LISP symbol_value(LISP x,LISP env); +LISP setvar(LISP var,LISP val,LISP env); +LISP copy_list(LISP x); +LISP quote(LISP item); +LISP read_from_lstring(LISP x); +LISP symbolexplode(LISP name); + +LISP fopen_c(const char *name, const char *how); +LISP fclose_l(LISP p); +LISP lprin1f(LISP exp,FILE *f); +void pprint(LISP exp); +LISP lprint(LISP exp); +void pprint_to_fd(FILE *fd,LISP exp); +LISP lread(void); +LISP lreadtk(long j); +LISP lreadf(FILE *f); +#ifdef WIN32 +LISP lreadwinsock(void); +#endif +void set_read_hooks(char *all_set,char *end_set, + LISP (*fcn1)(int, struct gen_readio *), + LISP (*fcn2)(char *,long, int *)); +LISP vload(const char *fname,long cflag); +LISP read_from_string(const char *); +long repl_c_string(char *,long want_sigint,long want_init,long want_print); +long repl_from_socket(int fd); +void init_subrs(void); +LISP stringexplode(const char *str); +void fput_st(FILE *f,const char *st); +LISP get_eof_val(void); + + +#if 0 +void print_hs_1(void); +void print_hs_2(void); +void set_repl_hooks(void (*puts_f)(char *), + LISP (*read_f)(void), + LISP (*eval_f)(LISP), + void (*print_f)(LISP)); +long repl(struct repl_hooks *); +LISP lerr(LISP message, LISP x); +LISP eq(LISP x,LISP y); +LISP symcons(char *pname,LISP vcell); +LISP symbol_value_p(LISP x,LISP env,int *set); +LISP subrcons(long type, const char *name, SUBR_FUNC f); + +void init_storage(int heap_size=DEFAULT_HEAP_SIZE); + +LISP gc_status(LISP args); + +/* For user defined types in OBJ */ + +LISP oblistfn(void); +LISP save_forms(LISP fname,LISP forms,LISP how); +LISP intern(LISP x); +void init_trace(void); +LISP siod_fdopen_c(int fd,const char *name,char *how); + +LISP probe_file(LISP fname); + +LISP fopen_l(LISP name,LISP how); +LISP fopen_l(LISP name,const char *how); + +#endif +#define siod_error() (errjmp_ok ? longjmp(*est_errjmp,1) : exit(-1)) + +#include "siod_est.h" + +#endif diff --git a/aeneas/cfw/speech_tools/siod_defs.h b/aeneas/cfw/speech_tools/siod_defs.h new file mode 100644 index 00000000..24bed935 --- /dev/null +++ b/aeneas/cfw/speech_tools/siod_defs.h @@ -0,0 +1,285 @@ +/* Scheme In One Defun, but in C this time. + + * COPYRIGHT (c) 1988-1994 BY * + * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. * + * See the source file SLIB.C for more information. * + +*/ + +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : March 1999 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Struct and macro definitions for SIOD */ +/* */ +/*=======================================================================*/ +#ifndef __EST_SIOD_DEFS_H__ +#define __EST_SIOD_DEFS_H__ + +/* This states the default heap size is effective unset */ +/* The size if no heap is specified by a command argument, the */ +/* value of the environment variable SIODHEAPSIZE will be used */ +/* otherwise ACTUAL_DEFAULT_HEAP_SIZE is used. This is *not* */ +/* documented because environment variables can cause so many */ +/* problems I'd like to discourage this use unless absolutely */ +/* necessary. */ +#define DEFAULT_HEAP_SIZE -1 +#define ACTUAL_DEFAULT_HEAP_SIZE 210000 + +struct obj +{union {struct {struct obj * car; + struct obj * cdr;} cons; + struct {double data;} flonum; + struct {const char *pname; + struct obj * vcell;} symbol; + struct {const char *name; + struct obj * (*f)(void);} subr0; + struct {const char *name; + struct obj * (*f)(struct obj *);} subr1; + struct {const char *name; + struct obj * (*f)(struct obj *, struct obj *);} subr2; + struct {const char *name; + struct obj * (*f)(struct obj *, struct obj *, struct obj *); + } subr3; + struct {const char *name; + struct obj * (*f)(struct obj *, struct obj *, + struct obj *, struct obj *); + } subr4; + struct {const char *name; + struct obj * (*f)(struct obj **, struct obj **);} subrm; + struct {const char *name; + struct obj * (*f)(void *,...);} subr; + struct {struct obj *env; + struct obj *code;} closure; + struct {long dim; + long *data;} long_array; + struct {long dim; + double *data;} double_array; + struct {long dim; + char *data;} string; + struct {long dim; + struct obj **data;} lisp_array; + struct {FILE *f; + char *name;} c_file; + struct {EST_Val *v;} val; + struct {void *p;} user; +} + storage_as; + char *pname; // This is currently only used by FLONM + short gc_mark; + short type; +}; + +#define CAR(x) ((*x).storage_as.cons.car) +#define CDR(x) ((*x).storage_as.cons.cdr) +#define PNAME(x) ((*x).storage_as.symbol.pname) +#define VCELL(x) ((*x).storage_as.symbol.vcell) +#define SUBR0(x) (*((*x).storage_as.subr0.f)) +#define SUBR1(x) (*((*x).storage_as.subr1.f)) +#define SUBR2(x) (*((*x).storage_as.subr2.f)) +#define SUBR3(x) (*((*x).storage_as.subr3.f)) +#define SUBR4(x) (*((*x).storage_as.subr4.f)) +#define SUBRM(x) (*((*x).storage_as.subrm.f)) +#define SUBRF(x) (*((*x).storage_as.subr.f)) +#define FLONM(x) ((*x).storage_as.flonum.data) +#define FLONMPNAME(x) ((*x).pname) +#define USERVAL(x) ((*x).storage_as.user.p) +#define UNTYPEDVAL(x) ((*x).storage_as.user.p) + +#define NIL ((struct obj *) 0) +#define EQ(x,y) ((x) == (y)) +#define NEQ(x,y) ((x) != (y)) +#define NULLP(x) EQ(x,NIL) +#define NNULLP(x) NEQ(x,NIL) + +#define TYPE(x) (((x) == NIL) ? 0 : ((*(x)).type)) + +#define TYPEP(x,y) (TYPE(x) == (y)) +#define NTYPEP(x,y) (TYPE(x) != (y)) + +#define tc_nil 0 +#define tc_cons 1 +#define tc_flonum 2 +#define tc_symbol 3 +#define tc_subr_0 4 +#define tc_subr_1 5 +#define tc_subr_2 6 +#define tc_subr_3 7 +#define tc_lsubr 8 +#define tc_fsubr 9 +#define tc_msubr 10 +#define tc_closure 11 +#define tc_free_cell 12 +#define tc_string 13 +#define tc_double_array 14 +#define tc_long_array 15 +#define tc_lisp_array 16 +#define tc_c_file 17 +#define tc_untyped 18 +#define tc_subr_4 19 + +#define tc_sys_1 31 +#define tc_sys_2 32 +#define tc_sys_3 33 +#define tc_sys_4 34 +#define tc_sys_5 35 + +// older method for adding application specific types +#define tc_application_1 41 +#define tc_application_2 42 +#define tc_application_3 43 +#define tc_application_4 44 +#define tc_application_5 45 +#define tc_application_6 46 +#define tc_application_7 47 + +// Application specific types may be added using siod_register_user_type() +// Will increment from tc_first_user_type to tc_table_dim +#define tc_first_user_type 50 + +#define tc_table_dim 100 + +#define FO_fetch 127 +#define FO_store 126 +#define FO_list 125 +#define FO_listd 124 + +typedef struct obj* LISP; +typedef LISP (*SUBR_FUNC)(void); + +#define CONSP(x) TYPEP(x,tc_cons) +#define FLONUMP(x) TYPEP(x,tc_flonum) +#define SYMBOLP(x) TYPEP(x,tc_symbol) +#define STRINGP(x) TYPEP(x,tc_string) + +#define NCONSP(x) NTYPEP(x,tc_cons) +#define NFLONUMP(x) NTYPEP(x,tc_flonum) +#define NSYMBOLP(x) NTYPEP(x,tc_symbol) + +// Not for the purists, but I find these more readable than the equivalent +// code inline. + +#define CAR1(x) CAR(x) +#define CDR1(x) CDR(x) +#define CAR2(x) CAR(CDR1(x)) +#define CDR2(x) CDR(CDR1(x)) +#define CAR3(x) CAR(CDR2(x)) +#define CDR3(x) CDR(CDR2(x)) +#define CAR4(x) CAR(CDR3(x)) +#define CDR4(x) CDR(CDR3(x)) +#define CAR5(x) CAR(CDR4(x)) +#define CDR5(x) CDR(CDR4(x)) + +#define LISTP(x) (NULLP(x) || CONSP(x)) +#define LIST1P(x) (CONSP(x) && NULLP(CDR(x))) +#define LIST2P(x) (CONSP(x) && CONSP(CDR1(x)) && NULLP(CDR2(x))) +#define LIST3P(x) (CONSP(x) && CONSP(CDR1(x)) && CONSP(CDR2(x)) && NULLP(CDR3(x))) +#define LIST4P(x) (CONSP(x) && CONSP(CDR1(x)) && CONSP(CDR2(x)) && CONSP(CDR3(x)) && NULLP(CDR4(x))) +#define LIST5P(x) (CONSP(x) && CONSP(CDR1(x)) && CONSP(CDR2(x)) && CONSP(CDR3(x)) && CONSP(CDR4(x)) && NULLP(CDR5(x))) + +#define MKPTR(x) (siod_make_ptr((void *)x)) + +struct gen_readio +{int (*getc_fcn)(char *); + void (*ungetc_fcn)(int, char *); + char *cb_argument;}; + +#define GETC_FCN(x) (*((*x).getc_fcn))((*x).cb_argument) +#define UNGETC_FCN(c,x) (*((*x).ungetc_fcn))(c,(*x).cb_argument) + +struct repl_hooks +{void (*repl_puts)(char *); + LISP (*repl_read)(void); + LISP (*repl_eval)(LISP); + void (*repl_print)(LISP);}; + +/* Macro for defining new class as values public functions */ +#define SIOD_REGISTER_CLASS_DCLS(NAME,CLASS) \ +class CLASS *NAME(LISP x); \ +int NAME##_p(LISP x); \ +EST_Val est_val(const class CLASS *v); \ +LISP siod(const class CLASS *v); + +/* Macro for defining new class as siod */ +#define SIOD_REGISTER_CLASS(NAME,CLASS) \ +class CLASS *NAME(LISP x) \ +{ \ + return NAME(val(x)); \ +} \ + \ +int NAME##_p(LISP x) \ +{ \ + if (val_p(x) && \ + (val_type_##NAME == val(x).type())) \ + return TRUE; \ + else \ + return FALSE; \ +} \ + \ +LISP siod(const class CLASS *v) \ +{ \ + if (v == 0) \ + return NIL; \ + else \ + return siod(est_val(v)); \ +} \ + + +/* Macro for defining typedefed something as values public functions */ +#define SIOD_REGISTER_TYPE_DCLS(NAME,CLASS) \ +CLASS *NAME(LISP x); \ +int NAME##_p(LISP x); \ +EST_Val est_val(const CLASS *v); \ +LISP siod(const CLASS *v); + +/* Macro for defining new class as siod */ +#define SIOD_REGISTER_TYPE(NAME,CLASS) \ +CLASS *NAME(LISP x) \ +{ \ + return NAME(val(x)); \ +} \ + \ +int NAME##_p(LISP x) \ +{ \ + if (val_p(x) && \ + (val_type_##NAME == val(x).type())) \ + return TRUE; \ + else \ + return FALSE; \ +} \ + \ +LISP siod(const CLASS *v) \ +{ \ + if (v == 0) \ + return NIL; \ + else \ + return siod(est_val(v)); \ +} \ + + +/* Macro for defining function ptr as siod */ +#define SIOD_REGISTER_FUNCPTR(NAME,CLASS) \ +CLASS NAME(LISP x) \ +{ \ + return NAME(val(x)); \ +} \ + \ +int NAME##_p(LISP x) \ +{ \ + if (val_p(x) && \ + (val_type_##NAME == val(x).type())) \ + return TRUE; \ + else \ + return FALSE; \ +} \ + \ +LISP siod(const CLASS v) \ +{ \ + if (v == 0) \ + return NIL; \ + else \ + return siod(est_val(v)); \ +} \ + +#endif diff --git a/aeneas/cfw/speech_tools/siod_est.h b/aeneas/cfw/speech_tools/siod_est.h new file mode 100644 index 00000000..96fd2746 --- /dev/null +++ b/aeneas/cfw/speech_tools/siod_est.h @@ -0,0 +1,71 @@ +/*************************************************************************/ +/* */ +/* Centre for Speech Technology Research */ +/* University of Edinburgh, UK */ +/* Copyright (c) 1996-1998 */ +/* All Rights Reserved. */ +/* */ +/* Permission is hereby granted, free of charge, to use and distribute */ +/* this software and its documentation without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of this work, and to */ +/* permit persons to whom this work is furnished to do so, subject to */ +/* the following conditions: */ +/* 1. The code must retain the above copyright notice, this list of */ +/* conditions and the following disclaimer. */ +/* 2. Any modifications must be clearly marked as such. */ +/* 3. Original authors' names are not deleted. */ +/* 4. The authors' names are not used to endorse or promote products */ +/* derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ +/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ +/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ +/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ +/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ +/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ +/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ +/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ +/* THIS SOFTWARE. */ +/* */ +/*************************************************************************/ +/* Author : Alan W Black */ +/* Date : February 1999 */ +/*-----------------------------------------------------------------------*/ +/* */ +/* Siod additions specific to Speech Tools */ +/* */ +/*=======================================================================*/ +#ifndef __SIOD_EST_H__ +#define __SIOD_EST_H__ + +void siod_est_init(); +void siod_fringe_init(); + +class EST_Val &val(LISP x); +int val_p(LISP x); +LISP siod(const class EST_Val v); + +SIOD_REGISTER_CLASS_DCLS(wave,EST_Wave) +SIOD_REGISTER_CLASS_DCLS(track,EST_Track) +SIOD_REGISTER_CLASS_DCLS(feats,EST_Features) +SIOD_REGISTER_CLASS_DCLS(utterance,EST_Utterance) +SIOD_REGISTER_CLASS_DCLS(item,EST_Item) +/* SIOD_REGISTER_CLASS_DCLS(scheme,obj) */ /* removed for clang -- 14/10/13 */ + +#define get_c_utt(x) (utterance(x)) +#define get_c_item(x) (item(x)) + +LISP lisp_val(const EST_Val &pv); +EST_Val val_lisp(LISP v); + +LISP features_to_lisp(EST_Features &f); +void lisp_to_features(LISP lf,EST_Features &f); + +LISP kvlss_to_lisp(const EST_TKVL &kvl); +void lisp_to_kvlss(LISP l, EST_TKVL &kvl); + +EST_Features &Param(); + +#endif diff --git a/aeneas/cfw/speech_tools/unix/EST_defines_unix.h b/aeneas/cfw/speech_tools/unix/EST_defines_unix.h new file mode 100644 index 00000000..3077ce51 --- /dev/null +++ b/aeneas/cfw/speech_tools/unix/EST_defines_unix.h @@ -0,0 +1,45 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Defines for things which have to be hidden from some compilers. */ + /* */ + /*************************************************************************/ + +#if !defined(__EST_DEFINES_UNIX_H__) +#define __EST_DEFINES_UNIX_H__ 1 + +#endif diff --git a/aeneas/cfw/speech_tools/unix/EST_socket_unix.h b/aeneas/cfw/speech_tools/unix/EST_socket_unix.h new file mode 100644 index 00000000..0751725a --- /dev/null +++ b/aeneas/cfw/speech_tools/unix/EST_socket_unix.h @@ -0,0 +1,90 @@ + + /************************************************************************/ + /* */ + /* Centre for Speech Technology Research */ + /* University of Edinburgh, UK */ + /* Copyright (c) 1996,1997 */ + /* All Rights Reserved. */ + /* */ + /* Permission is hereby granted, free of charge, to use and distribute */ + /* this software and its documentation without restriction, including */ + /* without limitation the rights to use, copy, modify, merge, publish, */ + /* distribute, sublicense, and/or sell copies of this work, and to */ + /* permit persons to whom this work is furnished to do so, subject to */ + /* the following conditions: */ + /* 1. The code must retain the above copyright notice, this list of */ + /* conditions and the following disclaimer. */ + /* 2. Any modifications must be clearly marked as such. */ + /* 3. Original authors' names are not deleted. */ + /* 4. The authors' names are not used to endorse or promote products */ + /* derived from this software without specific prior written */ + /* permission. */ + /* */ + /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */ + /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ + /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ + /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */ + /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ + /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ + /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ + /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ + /* THIS SOFTWARE. */ + /* */ + /*************************************************************************/ + /* */ + /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */ + /* Date: Tue Sep4th 1997 */ + /* -------------------------------------------------------------------- */ + /* Defines of things which may not be here on all unixes. */ + /* */ + /*************************************************************************/ + +#if !defined(__EST_SOCKET_UNIX_H__) +#define __EST_SOCKET_UNIX_H__ 1 + +#include + +/* Solaris defines this, linux doesn't */ +#if defined(sun) && !defined(SVR4) +typedef int ssize_t; +#endif + +#if defined(older_solaris) +/* older versions of Solaris don't have this */ +typedef int socklen_t; +#endif + +#if defined(__FreeBSD__) && __FreeBSD__ < 4 +typedef int socklen_t; +#endif + +#if defined(__APPLE__) +#endif + +#if defined(__CYGWIN__) && __GNUC__ < 3 +typedef int socklen_t; +#endif + +#if defined(__osf__) +typedef int socklen_t; +#endif + +#if defined(_AIX) +#include +#endif + +#define NOT_A_SOCKET(FD) ((FD) <0) +#define socket_error() errno + +#if defined(__cplusplus) +extern "C" { +#endif + +int socket_initialise(void); + +#if defined(__cplusplus) +} +#endif + +#endif + diff --git a/aeneas/globalfunctions.py b/aeneas/globalfunctions.py index b69c0d3e..7433a0f5 100644 --- a/aeneas/globalfunctions.py +++ b/aeneas/globalfunctions.py @@ -810,20 +810,31 @@ def can_run_cmfcc(): return False def can_run_cew(): - """ Python C extension for synthesizing with espeak """ + """ Python C extension for synthesizing with eSpeak """ try: import aeneas.cew.cew return True except ImportError: return False + def can_run_cfw(): + """ Python C extension for synthesizing with Festival """ + try: + import aeneas.cfw.cfw + return True + except ImportError: + return False + if name == "cdtw": return can_run_cdtw() elif name == "cmfcc": return can_run_cmfcc() elif name == "cew": return can_run_cew() + elif name == "cfw": + return can_run_cfw() else: + # NOTE cfw is still experimental! return can_run_cdtw() and can_run_cmfcc() and can_run_cew() @@ -854,17 +865,19 @@ def run_c_extension_with_fallback( computed = False if not rconf[u"c_extensions"]: log_function(u"C extensions disabled") + elif extension not in rconf: + log_function([u"C extension '%s' not recognized", extension]) elif not rconf[extension]: - log_function([u"C extension %s disabled", extension]) + log_function([u"C extension '%s' disabled", extension]) else: - log_function([u"C extension %s enabled", extension]) + log_function([u"C extension '%s' enabled", extension]) if c_function is None: log_function(u"C function is None") elif can_run_c_extension(extension): - log_function([u"C extension %s enabled and it can be loaded", extension]) + log_function([u"C extension '%s' enabled and it can be loaded", extension]) computed, result = c_function(*args) else: - log_function([u"C extension %s enabled but it cannot be loaded", extension]) + log_function([u"C extension '%s' enabled but it cannot be loaded", extension]) if not computed: if py_function is None: log_function(u"Python function is None") diff --git a/aeneas/runtimeconfiguration.py b/aeneas/runtimeconfiguration.py index 860873b0..42ec9eef 100644 --- a/aeneas/runtimeconfiguration.py +++ b/aeneas/runtimeconfiguration.py @@ -95,6 +95,16 @@ class RuntimeConfiguration(Configuration): .. versionadded:: 1.5.1 """ + CFW = "cfw" + """ + If ``True`` and Python C extension ``cfw`` is available, use it. + Otherwise, use pure Python code. + + Default: ``True``. + + .. versionadded:: 1.6.0 + """ + CEW_SUBPROCESS_ENABLED = "cew_subprocess_enabled" """ If ``True``, calls to ``aeneas.cew`` will be done via ``subprocess``. @@ -668,6 +678,7 @@ class RuntimeConfiguration(Configuration): (C_EXTENSIONS, (True, bool, [])), (CDTW, (True, bool, [])), (CEW, (True, bool, [])), + (CFW, (True, bool, [])), (CMFCC, (True, bool, [])), (CEW_SUBPROCESS_ENABLED, (False, bool, [])), diff --git a/aeneas/tests/base_ttswrapper.py b/aeneas/tests/base_ttswrapper.py index 23c2dae3..c1fb9be5 100644 --- a/aeneas/tests/base_ttswrapper.py +++ b/aeneas/tests/base_ttswrapper.py @@ -39,7 +39,6 @@ class TestBaseTTSWrapper(unittest.TestCase): TTS_CLASS = BaseTTSWrapper TTS_LANGUAGE = u"eng" TTS_LANGUAGE_VARIATION = None - MULTIRUN = False def synthesize(self, text_file, ofp=None, quit_after=None, backwards=False, zero_length=False, expected_exc=None): if (self.TTS == u"") or (self.TTS_PATH == u"") or (not os.path.exists(self.TTS_PATH)): @@ -78,14 +77,18 @@ def inner(c_ext, cew_subprocess, cache): tts_engine.clear_cache() with self.assertRaises(expected_exc): raise exc - if self.MULTIRUN: + if self.TTS == "espeak": for c_ext in [True, False]: for cew_subprocess in [True, False]: for cache in [True, False]: - inner(c_ext, cew_subprocess, cache) + inner(c_ext=c_ext, cew_subprocess=cew_subprocess, cache=cache) + elif self.TTS == "festival": + for c_ext in [True, False]: + for cache in [True, False]: + inner(c_ext=c_ext, cew_subprocess=False, cache=cache) else: for cache in [True, False]: - inner(True, False, cache) + inner(c_ext=True, cew_subprocess=False, cache=cache) def tfl(self, frags): tfl = TextFile() diff --git a/aeneas/tests/test_espeakttswrapper.py b/aeneas/tests/test_espeakttswrapper.py index f0dae50b..4f70e858 100644 --- a/aeneas/tests/test_espeakttswrapper.py +++ b/aeneas/tests/test_espeakttswrapper.py @@ -29,7 +29,6 @@ class TestESPEAKTTSWrapper(TestBaseTTSWrapper): - MULTIRUN = True TTS = u"espeak" TTS_PATH = u"/usr/bin/espeak" TTS_CLASS = ESPEAKTTSWrapper diff --git a/aeneas/tools/execute_task.py b/aeneas/tools/execute_task.py index 49401a7e..fd8d2d45 100644 --- a/aeneas/tools/execute_task.py +++ b/aeneas/tools/execute_task.py @@ -131,7 +131,7 @@ class ExecuteTaskCLI(AbstractCLIProgram): u"description": u"input: plain text, output: TSV, tts engine: Festival", u"audio": AUDIO_FILE, u"text": gf.relative_path("res/plain.txt", __file__), - u"config": u"task_language=eng-GBR|is_text_type=plain|os_task_file_format=tsv", + u"config": u"task_language=eng|is_text_type=plain|os_task_file_format=tsv", u"syncmap": "output/sonnet.festival.tsv", u"options": u"-r=\"tts=festival\"", u"show": False diff --git a/aeneas/ttswrappers/basettswrapper.py b/aeneas/ttswrappers/basettswrapper.py index 8a221282..bb741fb1 100644 --- a/aeneas/ttswrappers/basettswrapper.py +++ b/aeneas/ttswrappers/basettswrapper.py @@ -260,6 +260,13 @@ class BaseTTSWrapper(Loggable): via a direct Python call. """ + C_EXTENSION_NAME = "" + """ + If the TTS wrapper can invoke the TTS engine + via a C extension call, + set here the name of the corresponding Python C/C++ extension. + """ + TAG = u"BaseTTSWrapper" def __init__(self, rconf=None, logger=None): @@ -418,7 +425,7 @@ def synthesize_multiple(self, text_file, output_file_path, quit_after=None, back subprocess_function = self._synthesize_multiple_subprocess if self.HAS_SUBPROCESS_CALL else None return gf.run_c_extension_with_fallback( self.log, - "cew", + self.C_EXTENSION_NAME, c_extension_function, subprocess_function, (text_file, output_file_path, quit_after, backwards), diff --git a/aeneas/ttswrappers/espeakttswrapper.py b/aeneas/ttswrappers/espeakttswrapper.py index d3c26076..572c7df3 100644 --- a/aeneas/ttswrappers/espeakttswrapper.py +++ b/aeneas/ttswrappers/espeakttswrapper.py @@ -642,6 +642,8 @@ class ESPEAKTTSWrapper(BaseTTSWrapper): HAS_C_EXTENSION_CALL = True + C_EXTENSION_NAME = "cew" + TAG = u"ESPEAKTTSWrapper" def __init__(self, rconf=None, logger=None): diff --git a/aeneas/ttswrappers/festivalttswrapper.py b/aeneas/ttswrappers/festivalttswrapper.py index b237a926..70ee0d26 100644 --- a/aeneas/ttswrappers/festivalttswrapper.py +++ b/aeneas/ttswrappers/festivalttswrapper.py @@ -33,7 +33,9 @@ from aeneas.language import Language from aeneas.runtimeconfiguration import RuntimeConfiguration +from aeneas.timevalue import TimeValue from aeneas.ttswrappers.basettswrapper import BaseTTSWrapper +import aeneas.globalfunctions as gf class FESTIVALTTSWrapper(BaseTTSWrapper): @@ -41,7 +43,9 @@ class FESTIVALTTSWrapper(BaseTTSWrapper): A wrapper for the ``Festival`` TTS engine. This wrapper supports calling the TTS engine - via ``subprocess`` only. + via ``subprocess`` or via Python C extension. + + .. note:: The latter call method is experimental and probably works only on Linux at the moment. In abstract terms, it performs one or more calls like :: @@ -129,6 +133,10 @@ class FESTIVALTTSWrapper(BaseTTSWrapper): HAS_SUBPROCESS_CALL = True + HAS_C_EXTENSION_CALL = True + + C_EXTENSION_NAME = "cfw" + TAG = u"FESTIVALTTSWrapper" def __init__(self, rconf=None, logger=None): @@ -143,3 +151,100 @@ def __init__(self, rconf=None, logger=None): def _voice_code_to_subprocess(self, voice_code): return [u"-eval", self.VOICE_CODE_TO_SUBPROCESS[voice_code]] + + def _synthesize_multiple_c_extension(self, text_file, output_file_path, quit_after=None, backwards=False): + """ + Synthesize multiple text fragments, using the cfw extension. + + Return a tuple (anchors, total_time, num_chars). + + :rtype: (bool, (list, :class:`~aeneas.timevalue.TimeValue`, int)) + """ + self.log(u"Synthesizing using C extension...") + + # convert parameters from Python values to C values + try: + c_quit_after = float(quit_after) + except TypeError: + c_quit_after = 0.0 + c_backwards = 0 + if backwards: + c_backwards = 1 + self.log([u"output_file_path: %s", output_file_path]) + self.log([u"c_quit_after: %.3f", c_quit_after]) + self.log([u"c_backwards: %d", c_backwards]) + self.log(u"Preparing u_text...") + u_text = [] + fragments = text_file.fragments + for fragment in fragments: + f_lang = fragment.language + f_text = fragment.filtered_text + if f_lang is None: + f_lang = self.DEFAULT_LANGUAGE + f_voice_code = self.VOICE_CODE_TO_SUBPROCESS[self._language_to_voice_code(f_lang)] + if f_text is None: + f_text = u"" + u_text.append((f_voice_code, f_text)) + self.log(u"Preparing u_text... done") + + # call C extension + sr = None + sf = None + intervals = None + + self.log(u"Preparing c_text...") + if gf.PY2: + # Python 2 => pass byte strings + c_text = [(gf.safe_bytes(t[0]), gf.safe_bytes(t[1])) for t in u_text] + else: + # Python 3 => pass Unicode strings + c_text = [(gf.safe_unicode(t[0]), gf.safe_unicode(t[1])) for t in u_text] + self.log(u"Preparing c_text... done") + + self.log(u"Calling aeneas.cfw directly") + try: + self.log(u"Importing aeneas.cfw...") + import aeneas.cfw.cfw + self.log(u"Importing aeneas.cfw... done") + self.log(u"Calling aeneas.cfw...") + sr, sf, intervals = aeneas.cfw.cfw.synthesize_multiple( + output_file_path, + c_quit_after, + c_backwards, + c_text + ) + self.log(u"Calling aeneas.cfw... done") + except Exception as exc: + self.log_exc(u"An unexpected error occurred while running cfw", exc, False, None) + return (False, None) + + self.log([u"sr: %d", sr]) + self.log([u"sf: %d", sf]) + + # create output + anchors = [] + current_time = TimeValue("0.000") + num_chars = 0 + if backwards: + fragments = fragments[::-1] + for i in range(sf): + # get the correct fragment + fragment = fragments[i] + # store for later output + anchors.append([ + TimeValue(intervals[i][0]), + fragment.identifier, + fragment.filtered_text + ]) + # increase the character counter + num_chars += fragment.characters + # update current_time + current_time = TimeValue(intervals[i][1]) + + # return output + # NOTE anchors do not make sense if backwards == True + self.log([u"Returning %d time anchors", len(anchors)]) + self.log([u"Current time %.3f", current_time]) + self.log([u"Synthesized %d characters", num_chars]) + self.log(u"Synthesizing using C extension... done") + return (True, (anchors, current_time, num_chars)) diff --git a/docs/source/cfw.rst b/docs/source/cfw.rst new file mode 100644 index 00000000..2ac5b268 --- /dev/null +++ b/docs/source/cfw.rst @@ -0,0 +1,5 @@ +aeneas.cfw +========== + +.. automodule:: aeneas.cfw + :members: diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 49d39575..30f6249f 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -1,7 +1,7 @@ Changelog ========= -v1.6.0 (2016-09-??) +v1.6.0 (2016-09-26) ------------------- #. Fixed bug #102 by checking that the audio file produced by the TTS engine is mono WAVE and has correct sample rate: slightly slower but safe @@ -20,8 +20,9 @@ v1.6.0 (2016-09-??) #. When working on multilevel sync, user can specify a different TTS for each level #. Added an optional TTS caching mechanism to reduce subprocess/API calls to the TTS engine (closes #87) #. Added wrapper for eSpeak-ng (subprocess only) +#. Added ``aeneas.cfw`` Python C++ Extension to call ``Festival`` via its C++ API, disabled by default (closes #109) #. Unified unit tests for eSpeak, eSpeak-ng, and Festival -#. Python C extension compilation can be disabled in setup.py via env vars +#. Python C extension compilation can be disabled/forced in setup.py via env vars #. Added check on head/process/tail length which should not exceed the audio file length (closes #80) #. Moved package metadata from ``setup.py`` into ``setupmeta.py`` #. Added AGPL header to all source files @@ -31,7 +32,7 @@ v1.6.0 (2016-09-??) #. Using Sphinx theme from readthedocs.org if available #. Updated dependencies: BeautifulSoup4>=4.5.1 and lxml>=3.6.4 (see discussion in #93) #. Updated documentation -#. Several minor code improvements +#. Several other minor code improvements v1.5.1 (2016-07-25) ------------------- @@ -254,7 +255,7 @@ v1.1.1 (2015-08-23) ------------------- #. Added ``compile_c_extensions.bat`` and directions for Windows users (courtesy of Richard Margetts) -#. Added warning to ``aeneas.tools.*`` when running without Python C Extensions compiled +#. Added warning to ``aeneas.tools.*`` when running without Python C extensions compiled #. Improved ``README.md`` v1.1.0 (2015-08-21) diff --git a/docs/source/clitutorial.rst b/docs/source/clitutorial.rst index 4abea080..a2cdc2f4 100644 --- a/docs/source/clitutorial.rst +++ b/docs/source/clitutorial.rst @@ -363,11 +363,11 @@ Examples: python -m aeneas.tools.execute_task --example-json -r="allow_unlisted_languages=True" -#. disable the Python C extensions, running the pure Python code:: +#. disable the Python C/C++ extensions, running the pure Python code:: python -m aeneas.tools.execute_task --example-json -r="c_extensions=False" -#. disable only the ``cew`` Python C extension, while ``cdtw`` and ``cmfcc`` will still run (if compiled):: +#. disable only the ``cew`` Python C/C++ extension, while ``cdtw`` and ``cmfcc`` will still run (if compiled):: python -m aeneas.tools.execute_task --example-json -r="cew=False" diff --git a/docs/source/libtutorial.rst b/docs/source/libtutorial.rst index 86aa5ffb..a2ee1f88 100644 --- a/docs/source/libtutorial.rst +++ b/docs/source/libtutorial.rst @@ -126,8 +126,7 @@ Only ``numpy`` is actually needed, as it is heavily used for the alignment compu The other two dependencies (``lxml`` and ``BeautifulSoup``) are needed only if you use XML-like input or output formats. However, since they are popular Python packages, to avoid complex import testing, -they are listed as requirements as they are imported -module-wise where they are used. +they are listed as requirements. Depending on what ``aeneas`` classes you want to use, you might need to install the following optional dependencies: @@ -138,8 +137,8 @@ you might need to install the following optional dependencies: -Speeding Critical Sections Up: Python C Extensions --------------------------------------------------- +Speeding Critical Sections Up: Python C/C++ Extensions +------------------------------------------------------ Forced alignment is a computationally demanding task, both CPU-intensive and memory-intensive. @@ -147,10 +146,10 @@ Aligning a dozen minutes of audio might require an hour if done with pure Python code. Hence, critical sections of the alignment code are written -as Python C extensions, that is, C code that receives input +as Python C/C++ extensions, that is, C/C++ code that receives input from Python code, performs the heavy computation, and returns results to the Python code. -The rule of thumb is that the C code only perform +The rule of thumb is that the C/C++ code only perform "computation-like", low-level functions, while "house-keeping", high-level functions are done in Python land. @@ -159,23 +158,25 @@ With this approach, aligning a dozen minutes of audio requires only few seconds, and even aligning hours of audio can be done in few minutes. The drawback is that your environment must be able to compile -Python C extensions. If you install ``aeneas`` via ``PyPI`` +Python C/C++ extensions. If you install ``aeneas`` via ``PyPI`` (e.g., ``pip install aeneas``), the compilation step is done automatically for you. -Note that, due to the Python C extension compile and setup mechanism, +Note that, due to the Python C/C++ extension compile and setup mechanism, you must install ``numpy`` before installing ``aeneas``. -The Python C extensions included in ``aeneas`` are: +The Python C/C++ extensions included in ``aeneas`` are: .. toctree:: :maxdepth: 3 cdtw cew + cfw cmfcc cwave * :mod:`aeneas.cdtw`, for computing the DTW; * :mod:`aeneas.cew`, for synthesizing text via the ``eSpeak`` C API; +* :mod:`aeneas.cfw`, for synthesizing text via the ``Festival`` C++ API; * :mod:`aeneas.cmfcc`, for computing a MFCC representation of a WAVE (RIFF) audio file; * :mod:`aeneas.cwave`, for reading WAVE (RIFF) audio files. @@ -191,6 +192,17 @@ The Python C extensions included in ``aeneas`` are: Unless you work with thousands of text fragments, the performance difference is negligible. +.. note:: + + Currently :mod:`aeneas.cfw` is experimental and disabled by default. + Probably it works only on Linux. + To compile it, make sure you have installed + the ``Festival`` and ``speech_tools`` libraries + (e.g., install the ``festival-dev`` package on DEB-based OSes) and + set the environment variable + ``AENEAS_FORCE_CFW=True`` + before running ``pip`` or ``python setup.py``. + .. note:: Currently :mod:`aeneas.cwave` is not used. @@ -316,6 +328,7 @@ The main ``aeneas`` package contains several subpackages: * :mod:`aeneas.cdtw` (Python C extension) * :mod:`aeneas.cew` (Python C extension) +* :mod:`aeneas.cfw` (Python C++ extension) * :mod:`aeneas.cmfcc` (Python C extension) * :mod:`aeneas.cwave` (Python C extension) * :mod:`aeneas.extra` diff --git a/licenses/EST.txt b/licenses/EST.txt new file mode 100644 index 00000000..88e3dedf --- /dev/null +++ b/licenses/EST.txt @@ -0,0 +1,251 @@ + EDINBURGH SPEECH TOOLS LIBRARY + VERSION 2.4 December 2014 + +Principal authors: Alan W Black, Paul Taylor, Richard Caley, + Simon King, Rob Clark and Korin Richmond + +Centre for Speech Technology +University of Edinburgh +Informatics Forum +10 Crichton Street +Edinburgh EH8 9AB, UK +http://www.cstr.ed.ac.uk/projects/speech_tools + +Although this is a considered stable release, there are still a number +of known parts are missing or incomplete. + +------------------------------------------------------------------------ + +The Edinburgh speech tools system is a library of C++ classes, functions +and utility programs that are frequently used in speech software. The +system compiles to a single unix library .a file which can be linked +with software. At present, C++ classes for several useful speech and +language classes have been written, along with audio software and some +basic signal processing software. + +======================================================================== + + + Edinburgh Speech Tools Library version 2.4 + Centre for Speech Technology Research + University of Edinburgh, UK + Copyright (c) 1994-2014 + All Rights Reserved. + +Permission is hereby granted, free of charge, to use and distribute +this software and its documentation without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of this work, and to +permit persons to whom this work is furnished to do so, subject to +the following conditions: + 1. The code must retain the above copyright notice, this list of + conditions and the following disclaimer. + 2. Any modifications must be clearly marked as such. + 3. Original authors' names are not deleted. + 4. The authors' names are not used to endorse or promote products + derived from this software without specific prior written + permission. + +THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK +DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT +SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE +FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN +AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF +THIS SOFTWARE. + +======================================================================== + +The following c++ programs are available: + + na_play: generic playback program for use with net_audio and CSTR ao. + + ch_wave: Waveform file conversion program. + + ch_lab: label file conversion program. + + ch_track: Track file conversion program. + + wagon: a CART tree build and test program + + And others + +The following C++ sub-libraries are available + + audio: C++ audio functions for Network Audio system, Suns + Linux and FreeBSD + + speech_class: C++ speech classes, including waveform and track. + + ling_class: C++ linguistic classes. + + sigpr: Signal processing + + utils: Various utilities. + + +COPYING +------------------------------------------------------------------------ + +Some files in the distribution do not fall under the above copyright. +However none have any commercial restrictions, and no files fall under +the GPL. Specifically files that have different licences are as +follows: + +grammar/wfst/wfst_train.cc + Copyright Carnegie Mellon University (under the same + open licences as much of the rest of the code). +siod/ +include/siod*.h +lib/siod/siod.scm + Apart from some new functionality under our licence the SIOD + files fall under the licence given in siod/slib.cc copyright + Paradigm Associates. This code has been substantially modified + by us though our changes are under that licence. + + editline.c editline.h el_complete.c el_sysunix.c el_unix.h + are copyright 1992 Simmule Turner and Rich Salz under the + "BSD like" licence in siod/editline.c Again substantial + changes have been made by us which continue to be under that + licence + +rxp/ +include/rxp/ + Is Copyright Richard Tobin. We have a specific version of RXP + which allows free (including non-commercial) use. + +base_class/raveconv.cc + Is copyright 1992, 1995 by Markus Mummert + +base_class/string/reg*.{c,cc} + Copyright (c) 1986 by University of Toronto. + +base_class/string/EST_strcasecmp.c + Copyright (c) 1987, 1993 + The Regents of the University of California. All rights reserved. + + The standard BSD licence + + +ACKNOWLEDGEMENTS +------------------------------------------------------------------------ + +Most of the software was written by Paul Taylor, Alan W Black, Simon +King, Richard Caley, Rob Clark and Korin Richmond. Additional +contributions have been made by the following people. Slightly +different licence agreements accompany these pieces of code, so it is +important that the various agreements be properly understood. + +Tony Robinson: some signal processing code. +Markus Mummert: sample rate converter (rateconv.cc). +Craig Reese and Joe Campbell: ulaw conversion code. +Paul Bagshaw: pitch tracker. +Theo Veenker: IRIX audio support. +Stan Chen: AIX support +Paradigm Assoc. and George Carrett: For Scheme In One Defun +Simmule Turner and Rich Salz: for editline +Henry Spencer: for regex code +The Regents of the University of California: some string comparison code +Richard Tobin: RXP, XML parser. +Arthur Toth: Windows server code +Pierre Lorenzon gcc-4.7 patches +Sergio Oller: for making the documentation work again + +INSTALLATION +************ + +see INSTALL file for details + +FILE/DIRECTORY STRUCTURE +------------------------------------------------------------------------ + +The main directory contains minimal wrap-around main() functions +which compile to form executables which use the algorithms in the +speech tools. All these main() functions really do is to read +command line options and read and write files, the algorithms +themselves are completely contained within the libraries. + +The directory structure is as follows: + +config: + shared make rules and configuration make rules + +speech_class: + general speech class files, including waveforms, contours etc. + +ling_class: + general linguistic class files, including a stream-based architecture. + +sigpr: + signal processing code. + +doc: + basic documentation, generates html, info and postscript + +include: + class and general header files. + +lib: + where the .a files are kept + +main: + Directory containing main files which compile to executables. + + +DOCUMENTATION +------------------------------------------------------------------------ + +There are two sources of documentation. + +Man pages exist for the more important stand-alone programs. These are +found in the main directory. + +The library documentation has substantiall improved in this version +using an integrated DOC++, JADE and docbook combination. Classes are +described at the overview, theorectical, practical and code level. +The build process for the documentation is still dependent on some +more obscure packages so it is recommended that you take the +pre-built documentation. Or access it on-line through + http://www.cstr.ed.ac.uk/projects/speech_tools + +The library documentation is in texinfo format which can be converted +into GNU info HTML and postscript, see Makefile in doc/ + +Note the documentation is not up to date. We are in the process of moving the documentation to Doxygen. + +BUGS/COMMENTS ETC +------------------------------------------------------------------------ + +Substantial parts of this release are considered mature and should be +relatively free of bugs, however there are newer parts which have not +been rigourously tested yet. If you find any bugs, please send a bug +report to speechtools@cstr.ed.ac.uk. Any useful comments will also be +appreciated. See also the bugs file in the doc directory. + +If you have some software that you think could be integrated into the +speechtools system, please contact us at +speechtools@cstr.ed.ac.uk. The whole point of writing and releasing +code such as this is to prevent duplication of effort, and therefore +we would appreciate greatly any code which will add to the usefulness +of the system. Signal processing routines are particularly welcome. + + +====================================================================== +ADDITIONAL SOFTWARE +====================================================================== + +CSTR supplies a number of different speech technology systems +including speech synthesis systems and (in the future) a recognition +system. To find out more information checkout http://www.cstr.ed.ac.uk/ + + + + + + + + + + diff --git a/licenses/Festival.txt b/licenses/Festival.txt new file mode 100644 index 00000000..ff734e38 --- /dev/null +++ b/licenses/Festival.txt @@ -0,0 +1,113 @@ +The system as a whole and most of the files in it are distributed +under the following copyright and conditions + + The Festival Speech Synthesis System + Centre for Speech Technology Research + University of Edinburgh, UK + Copyright (c) 1996-2014 + All Rights Reserved. + + Permission is hereby granted, free of charge, to use and distribute + this software and its documentation without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of this work, and to + permit persons to whom this work is furnished to do so, subject to + the following conditions: + 1. The code must retain the above copyright notice, this list of + conditions and the following disclaimer. + 2. Any modifications must be clearly marked as such. + 3. Original authors' names are not deleted. + 4. The authors' names are not used to endorse or promote products + derived from this software without specific prior written + permission. + + THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK + DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING + ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT + SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE + FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + +Some further comments: + +Every effort has been made to ensure that Festival does not contain +any violation of intellectual property rights through disclosure of +trade secrets, copyright or patent violation. Considerable time and +effort has been spent to ensure that this is the case. However, +especially with patent problems, it is not always within our control +to know what has or has not been restricted. If you do suspect that +some part of Festival cannot be legally distributed please inform us +so that an alternative may be sought. Festival is only useful if it +is truly free to distribute. + +As of 1.4.0 the core distribution (and speech tools) is free. Unlike +previous versions which had a commercial restriction. You are free to +incorporate Festival in commercial (and of course non-commercial +systems), without any further communication or licence from us. +However if you are seriously using Festival within a commercial +application we would like to know, both so we know we are contributing +and so we can keep you informed of future developments. Also if you +require maintenance, support or wish us to provide consultancy feel +free to contact us. + +The voices however aren't all free. At present the US voices, kal and +ked are free. Our British voices are free themselves but they use OALD +which is restricted for non-commercial use. Our Spanish voice is also +so restricted. + +Note other modules that festival supports e.g MBROLA and OGI +extensions, may have different licencing please take care when using +the system to understand what you are actually using. + +-------------------------------------------------- + +A number of individual files in the system fall under a different +copyright from the above. All however are termed "free software" +but most people. + +./src/arch/festival/tcl.c + * Copyright (C)1997 Jacques H. de Villiers + * Copyright (C)1997 Center for Spoken Language Understanding, + * Oregon Graduate Institute of Science & Technology + See conditions in file. This is the standard TCL licence and hence + shouldn't cause problems from most people. + +./examples/festival_client.pl +# Copyright (C) 1997 +# Kevin A. Lenzo (lenzo@cs.cmu.edu) 7/97 + See condition in file + +./src/modules/clunits/* +./lib/*clunits* + Joint copyright University of Edinburgh and Carnegie Mellon University + Conditions remain as free software like the rest of distribution + +./src/modules/clustergen/* + Copyright Carnegie Mellon University + Nagoya Insitute of Technology (derived code from HTS) + HTS_vocoder_me.c HTS_vocoder_me.h + contains additions from Aby Louw + Copyright (c) 2012 The Department of Arts and Culture, + The Government of the Republic of South Africa. + under a free software licence compatible with the other + free software copyrights in this work + +./src/modules/hts_engine/* +./lib/hts.scm + The HMM-based speech synthesis system (HTS) + hts_engine API version 1.07 (http://hts-engine.sourceforge.net/) + Copyright (C) 2001-2012 Nagoya Institute of Technology + 2001-2008 Tokyo Institute of Technology + All rights reserved. + distributed under the Modified BSD licence. + +./lib/festival.el +;;; Copyright (C) Alan W Black 1996 +copyright under FSF General Public Licence + +Please also read the COPYING section of speech_tools/README for the +conditions on those files. + diff --git a/licenses/README.md b/licenses/README.md index 77038c04..9f093586 100644 --- a/licenses/README.md +++ b/licenses/README.md @@ -8,7 +8,7 @@ See [licenses/sphinx3.txt](https://raw.githubusercontent.com/readbeyond/aeneas/m The pure Python code for reading and writing WAVE files `aeneas/wavfile.py` -is a verbatim copy from the +is a slightly modified version from the [scipy project](https://github.com/scipy/scipy/), included here to avoid installing the whole `scipy` package. See [licenses/scipy.txt](https://raw.githubusercontent.com/readbeyond/aeneas/master/licenses/scipy.txt) for details. @@ -18,6 +18,17 @@ is a verbatim copy from the [espeak project](http://espeak.sourceforge.net/). See [licenses/eSpeak.txt](https://raw.githubusercontent.com/readbeyond/aeneas/master/licenses/eSpeak.txt) for details. +The C++ headers in `festival` and `speech_tools` for the Python C++ extension `cfw` +are verbatim copies from the +[Festival project](http://www.cstr.ed.ac.uk/projects/festival/) +and the +[Edinburgh Speech Tools Library project](http://www.cstr.ed.ac.uk/projects/speech_tools/). +See +[licenses/Festival.txt](https://raw.githubusercontent.com/readbeyond/aeneas/master/licenses/Festival.txt) +and +[licenses/speech_tools.txt](https://raw.githubusercontent.com/readbeyond/aeneas/master/licenses/EST.txt) +for details. + The HTML file `aeneas/res/finetuneas.html` is a verbatim copy from the [finetuneas project](https://github.com/ozdefir/finetuneas), diff --git a/setup.py b/setup.py index 6d07441c..eff07b93 100644 --- a/setup.py +++ b/setup.py @@ -175,6 +175,7 @@ def prepare_cew_for_windows(): WITHOUT_CMFCC = os.getenv("AENEAS_WITH_CMFCC", "True") not in TRUE_VALUES WITHOUT_CEW = os.getenv("AENEAS_WITH_CEW", "True") not in TRUE_VALUES FORCE_CEW = os.getenv("AENEAS_FORCE_CEW", "False") in TRUE_VALUES +FORCE_CFW = os.getenv("AENEAS_FORCE_CFW", "False") in TRUE_VALUES ############################################################################## @@ -214,6 +215,18 @@ def prepare_cew_for_windows(): get_include() ] ) +EXTENSION_CMFCC = Extension( + name="aeneas.cmfcc.cmfcc", + sources=[ + "aeneas/cmfcc/cmfcc_py.c", + "aeneas/cmfcc/cmfcc_func.c", + "aeneas/cwave/cwave_func.c", + "aeneas/cint/cint.c" + ], + include_dirs=[ + get_include() + ] +) EXTENSION_CEW = Extension( name="aeneas.cew.cew", sources=[ @@ -224,16 +237,21 @@ def prepare_cew_for_windows(): "espeak" ] ) -EXTENSION_CMFCC = Extension( - name="aeneas.cmfcc.cmfcc", +EXTENSION_CFW = Extension( + name="aeneas.cfw.cfw", sources=[ - "aeneas/cmfcc/cmfcc_py.c", - "aeneas/cmfcc/cmfcc_func.c", - "aeneas/cwave/cwave_func.c", - "aeneas/cint/cint.c" + "aeneas/cfw/cfw_py.cc", + "aeneas/cfw/cfw_func.cc" ], include_dirs=[ - get_include() + "aeneas/cfw/festival", + "aeneas/cfw/speech_tools" + ], + libraries=[ + "Festival", + "estools", + "estbase", + "eststring", ] ) # cwave is ready, but currently not used @@ -273,9 +291,9 @@ def prepare_cew_for_windows(): print("[INFO] **********************************************************") print("[INFO] ") elif FORCE_CEW: - print("[INFO] *************************************************************************************") - print("[INFO] The user specified AENEAS_FORCE_CEW=True: attempting to build cew without checking OS") - print("[INFO] *************************************************************************************") + print("[INFO] ********************************************************************************") + print("[INFO] The user specified AENEAS_FORCE_CEW=True: attempting to build cew without checks") + print("[INFO] ********************************************************************************") print("[INFO] ") EXTENSIONS.append(EXTENSION_CEW) else: @@ -311,6 +329,13 @@ def prepare_cew_for_windows(): else: print("[INFO] The C extension cew is not available for your OS.") +if FORCE_CFW: + print("[INFO] ********************************************************************************") + print("[INFO] The user specified AENEAS_FORCE_CFW=True: attempting to build cfw without checks") + print("[INFO] ********************************************************************************") + print("[INFO] ") + EXTENSIONS.append(EXTENSION_CFW) + # now we are ready to call setup() setup( name=PKG_NAME, diff --git a/setupmeta.py b/setupmeta.py index 830767c4..8d20a9de 100644 --- a/setupmeta.py +++ b/setupmeta.py @@ -89,6 +89,7 @@ "aeneas", "aeneas.cdtw", "aeneas.cew", + "aeneas.cfw", "aeneas.cmfcc", "aeneas.cwave", "aeneas.extra", @@ -114,6 +115,11 @@ "*.md", "*.dll" ], + "aeneas.cew": [ + "*.cc", + "*.h", + "*.md" + ], "aeneas.cmfcc": [ "*.c", "*.h", diff --git a/wiki/HISTORY.md b/wiki/HISTORY.md index dc4d26e0..09e7cf97 100644 --- a/wiki/HISTORY.md +++ b/wiki/HISTORY.md @@ -54,3 +54,8 @@ custom TTS support, and more with cew C extension support for Mac OS X, and installers for Mac OS X and Windows +**September 2016**: release of v1.6.0, +with refactored TTS engine wrappers, +added TTS cache, and +experimental cfw C++ extension for Festival. + diff --git a/wiki/INSTALL.md b/wiki/INSTALL.md index cbd278cd..07ec3284 100644 --- a/wiki/INSTALL.md +++ b/wiki/INSTALL.md @@ -27,7 +27,11 @@ in that case, just keep in mind: of the following environment variables: ``AENEAS_WITH_CDTW=False``, ``AENEAS_WITH_CEW=False``, or - ``AENEAS_WITH_CMFCC=False``. + ``AENEAS_WITH_CMFCC=False``; +* you can enable force compiling Python C extensions by setting one or more + of the following environment variables: + ``AENEAS_FORCE_CEW=True`` or + ``AENEAS_FORCE_CFW=True``. Below you can find detailed procedures for each operating system. diff --git a/wiki/PLATFORMS.md b/wiki/PLATFORMS.md index 77b660c0..73b8bd1a 100644 --- a/wiki/PLATFORMS.md +++ b/wiki/PLATFORMS.md @@ -4,6 +4,7 @@ | OS | 32/64 bit | Python 2.7 | Python 3.4/3.5 | |----------------|-----------|------------|-----------------| +| Arch | 64 | Yes | Yes | | Debian | 64 | Yes | Yes | | Debian | 32 | Yes | Yes | | Ubuntu | 64 | Yes | Yes | @@ -22,11 +23,16 @@ | Windows 10 | 64 | Yes (1) | Yes (1, 2) | **Notes** + (1) The ``cew`` Python C extension to speed up text synthesis is available on Linux, Mac OS X, and Windows 32 bit at the moment. + (2) On Windows and Python 3.4/3.5, compiling the Python C extensions is quite complex; however, running **aeneas** in pure Python mode has been confirmed to work. +(*) The ``cfw`` Python C++ extension to speed up text synthesis +with Festival is experimental and probably it only works on Linux. + Do you need official support for another OS? Consider **sponsoring** this project!