Ticket #6414: tests_libs_sphinx.patch

File tests_libs_sphinx.patch, 297.0 KB (added by Karvjorm, 14 years ago)

Test files for sphinx

  • src/tests/libs/sphinx/test_hash/_hash_delete3.res

     
     1Hash with chaining representation of the hash table
     2|key:-bla|len:4|val=8|->NULL
     3|key:-hmmdump|len:8|val=1|->|key:-subvq|len:6|val=7|->|key:-outlatdir|len:10|val=3|->NULL
     4|key:-lminmemory|len:11|val=6|->NULL
     5|key:-lm|len:3|val=4|->NULL
     6|key:-beam|len:5|val=5|->NULL
     7The total number of keys =7
  • src/tests/libs/sphinx/test_hash/_hash_delete4.res

     
     1Hash with chaining representation of the hash table
     2|key:-bla|len:4|val=8|->NULL
     3|key:-subvq|len:6|val=7|->|key:-outlatdir|len:10|val=3|->NULL
     4|key:-svq4svq|len:8|val=2|->|key:-lminmemory|len:11|val=6|->NULL
     5|key:-lm|len:3|val=4|->NULL
     6|key:-beam|len:5|val=5|->NULL
     7The total number of keys =7
  • src/tests/libs/sphinx/test_hash/_hash_delete5.res

     
     1Failed as expected
  • src/tests/libs/sphinx/test_hash/test_hash_iter.c

     
     1/**
     2 * @file test_hash_iter.c Test hash table iterators
     3 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     4 */
     5
     6#include "hash_table.h"
     7#include "test_macros.h"
     8
     9#include <stdio.h>
     10#include <stdlib.h>
     11#include <string.h>
     12
     13int
     14main(int argc, char *argv[])
     15{
     16    hash_table_t *h;
     17    hash_iter_t *itor;
     18
     19    /* Test insertion */
     20    TEST_ASSERT(h = hash_table_new(42, FALSE));
     21    TEST_EQUAL((void*)0xdeadbeef, hash_table_enter(h, "foo", (void*)0xdeadbeef));
     22    TEST_EQUAL((void*)0xdeadbeef, hash_table_enter(h, "foo", (void*)0xd0d0feed));
     23    TEST_EQUAL((void*)0xcafec0de, hash_table_enter(h, "bar", (void*)0xcafec0de));
     24    TEST_EQUAL((void*)0xeeefeeef, hash_table_enter(h, "baz", (void*)0xeeefeeef));
     25    TEST_EQUAL((void*)0xbabababa, hash_table_enter(h, "quux", (void*)0xbabababa));
     26
     27    /* Now test iterators. */
     28    for (itor = hash_table_iter(h); itor; itor = hash_table_iter_next(itor)) {
     29        printf("%s %p\n", itor->ent->key, itor->ent->val);
     30        if (0 == strcmp(itor->ent->key, "foo")) {
     31            TEST_EQUAL(itor->ent->val, (void*)0xdeadbeef);
     32        }
     33        else if (0 == strcmp(itor->ent->key, "bar")) {
     34            TEST_EQUAL(itor->ent->val, (void*)0xcafec0de);
     35        }
     36        else if (0 == strcmp(itor->ent->key, "baz")) {
     37            TEST_EQUAL(itor->ent->val, (void*)0xeeefeeef);
     38        }
     39        else if (0 == strcmp(itor->ent->key, "quux")) {
     40            TEST_EQUAL(itor->ent->val, (void*)0xbabababa);
     41        }
     42    }
     43
     44    return 0;
     45}
  • src/tests/libs/sphinx/test_hash/deletehash.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2#include <stdio.h>
     3#include <string.h>
     4#include <stdlib.h>
     5#include <hash_table.h>
     6#include <err.h>
     7
     8/* Explore a more complicated case for deletion */
     9int
     10main(int argc, char **argv)
     11{
     12    hash_table_t *ht;
     13    void *val;
     14
     15    if (argc != 2) {
     16        printf("deletehash <key>\n");
     17        exit(-1);
     18    }
     19
     20    ht = hash_table_new(75, 0);
     21
     22    if (hash_table_enter(ht, "-hmmdump", (void *)1) != (void *)1) {
     23        E_FATAL("Insertion of -hmmdump failed\n");
     24    }
     25
     26    if (hash_table_enter(ht, "-svq4svq", (void *)2) != (void *)2) {
     27        E_FATAL("Insertion of -svq4svq failed\n");
     28    }
     29
     30    if (hash_table_enter(ht, "-outlatdir", (void *)3) != (void *)3) {
     31        E_FATAL("Insertion of -svq4svq failed\n");
     32    }
     33
     34    if (hash_table_enter(ht, "-lm", (void *)4) != (void *)4) {
     35        E_FATAL("Insertion of -lm failed\n");
     36    }
     37
     38    if (hash_table_enter(ht, "-beam", (void *)5) != (void *)5) {
     39        E_FATAL("Insertion of -beam failed\n");
     40    }
     41
     42    if (hash_table_enter(ht, "-lminmemory", (void *)6) != (void *)6) {
     43        E_FATAL("Insertion of -lminmemory failed\n");
     44    }
     45
     46    if (hash_table_enter(ht, "-subvq", (void *)7) != (void *)7) {
     47        E_FATAL("Insertion of -outlatdir failed\n");
     48    }
     49
     50    if (hash_table_enter(ht, "-bla", (void *)8) != (void *)8) {
     51        E_FATAL("Insertion of -bla failed\n");
     52    }
     53
     54    /*  hash_table_display(ht,1); */
     55    if (hash_table_delete(ht, argv[1]) == NULL) {
     56        E_INFOCONT("Failed as expected\n");
     57        return 0;
     58    }
     59    else {
     60        hash_table_display(ht, 1);
     61    }
     62
     63    /* Test emptying */
     64    hash_table_empty(ht);
     65    if (hash_table_lookup(ht, "-beam", &val) == 0) {
     66        E_FATAL("Emptying hash table failed\n");
     67    }
     68
     69    hash_table_free(ht);
     70    ht = NULL;
     71    return 0;
     72}
     73
     74
     75#if 0
     76E_INFO("Hash table in the command line\n");
     77hash_table_display(ht, 1);
     78
     79E_INFO("After deletion of -lm\n");
     80hash_table_delete(ht, "-lm");
     81hash_table_display(ht, 1);
     82
     83E_INFO("After deletion of -lm\n");
     84
     85hash_table_delete(ht, "-lm");
     86hash_table_display(ht, 1);
     87
     88E_INFO("After deletion of -svq4svq\n");
     89hash_table_delete(ht, "-svq4svq");
     90hash_table_display(ht, 1);
     91
     92E_INFO("After deletion of -beam\n");
     93hash_table_delete(ht, "-beam");
     94hash_table_display(ht, 1);
     95#endif
  • src/tests/libs/sphinx/test_hash/displayhash.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2#include <stdio.h>
     3#include <string.h>
     4#include <stdlib.h>
     5#include <hash_table.h>
     6#include <err.h>
     7
     8/* Insert -hmmdump, -lm, -svq4svq, -beam, -lminmemory into a hash and display it. */
     9int
     10main(int argc, char **argv)
     11{
     12    hash_table_t *ht;
     13    ht = hash_table_new(75, 0);
     14
     15    if (hash_table_enter(ht, "-hmmdump", (void *)1) != (void *)1) {
     16        E_FATAL("Insertion of -hmmdump failed\n");
     17    }
     18
     19    if (hash_table_enter(ht, "-svq4svq", (void *)1) != (void *)1) {
     20        E_FATAL("Insertion of -svq4svq failed\n");
     21    }
     22
     23    if (hash_table_enter(ht, "-lm", (void *)1) != (void *)1) {
     24        E_FATAL("Insertion of -lm failed\n");
     25    }
     26
     27    if (hash_table_enter(ht, "-beam", (void *)1) != (void *)1) {
     28        E_FATAL("Insertion of -beam failed\n");
     29    }
     30
     31    if (hash_table_enter(ht, "-lminmemory", (void *)1) != (void *)1) {
     32        E_FATAL("Insertion of -lminmemory failed\n");
     33    }
     34
     35    hash_table_display(ht, 1);
     36
     37    hash_table_free(ht);
     38    ht = NULL;
     39    return 0;
     40}
     41
     42
     43#if 0
     44E_INFO("Hash table in the command line\n");
     45hash_table_display(ht, 1);
     46
     47E_INFO("After deletion of -lm\n");
     48hash_table_delete(ht, "-lm");
     49hash_table_display(ht, 1);
     50
     51E_INFO("After deletion of -lm\n");
     52
     53hash_table_delete(ht, "-lm");
     54hash_table_display(ht, 1);
     55
     56E_INFO("After deletion of -svq4svq\n");
     57hash_table_delete(ht, "-svq4svq");
     58hash_table_display(ht, 1);
     59
     60E_INFO("After deletion of -beam\n");
     61hash_table_delete(ht, "-beam");
     62hash_table_display(ht, 1);
     63#endif
  • src/tests/libs/sphinx/test_hash/display.res

     
     1Hash with chaining representation of the hash table
     2|key:-hmmdump|len:8|val=1|->NULL
     3|key:-svq4svq|len:8|val=1|->|key:-lminmemory|len:11|val=1|->NULL
     4|key:-lm|len:3|val=1|->NULL
     5|key:-beam|len:5|val=1|->NULL
     6The total number of keys =5
  • src/tests/libs/sphinx/test_hash/_hash_delete1.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./deletehash -lm > _hash_delete1.out 2>&1
     6compare_table delete1 _hash_delete1.out $tests/unit/test_hash/_hash_delete1.res
     7rm -f _hash_delete1.out
  • src/tests/libs/sphinx/test_hash/_hash_delete2.test

    Property changes on: src/tests/libs/sphinx/test_hash/_hash_delete1.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./deletehash -subvq > _hash_delete2.out 2>&1
     6compare_table delete2 _hash_delete2.out $tests/unit/test_hash/_hash_delete2.res
     7rm -f _hash_delete2.out
     8 No newline at end of file
  • src/tests/libs/sphinx/test_hash/_hash_delete3.test

    Property changes on: src/tests/libs/sphinx/test_hash/_hash_delete2.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./deletehash -svq4svq > _hash_delete3.out 2>&1
     6compare_table delete3 _hash_delete3.out $tests/unit/test_hash/_hash_delete3.res
     7rm -f _hash_delete3.out
     8 No newline at end of file
  • src/tests/libs/sphinx/test_hash/_hash_delete4.test

    Property changes on: src/tests/libs/sphinx/test_hash/_hash_delete3.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./deletehash -hmmdump > _hash_delete4.out 2>&1
     6compare_table delete4 _hash_delete4.out $tests/unit/test_hash/_hash_delete4.res
     7rm -f _hash_delete4.out
     8 No newline at end of file
  • src/tests/libs/sphinx/test_hash/Jamfile

    Property changes on: src/tests/libs/sphinx/test_hash/_hash_delete4.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_hash ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest deletehash : deletehash.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest displayhash : displayhash.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_hash_iter : test_hash_iter.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_hash/_hash_delete5.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./deletehash -foo > _hash_delete5.out 2>&1
     6compare_table delete5 _hash_delete5.out $tests/unit/test_hash/_hash_delete5.res
     7rm -f _hash_delete5.out
     8 No newline at end of file
  • src/tests/libs/sphinx/test_hash/test_macros.h

    Property changes on: src/tests/libs/sphinx/test_hash/_hash_delete5.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     5#define EPSILON 0.001
     6#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     7#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
  • src/tests/libs/sphinx/test_hash/_hash_delete1.res

     
     1Hash with chaining representation of the hash table
     2|key:-bla|len:4|val=8|->NULL
     3|key:-hmmdump|len:8|val=1|->|key:-subvq|len:6|val=7|->|key:-outlatdir|len:10|val=3|->NULL
     4|key:-svq4svq|len:8|val=2|->|key:-lminmemory|len:11|val=6|->NULL
     5|key:-beam|len:5|val=5|->NULL
     6The total number of keys =7
  • src/tests/libs/sphinx/test_hash/_hash_delete2.res

     
     1Hash with chaining representation of the hash table
     2|key:-bla|len:4|val=8|->NULL
     3|key:-hmmdump|len:8|val=1|->|key:-outlatdir|len:10|val=3|->NULL
     4|key:-svq4svq|len:8|val=2|->|key:-lminmemory|len:11|val=6|->NULL
     5|key:-lm|len:3|val=4|->NULL
     6|key:-beam|len:5|val=5|->NULL
     7The total number of keys =7
  • src/tests/libs/sphinx/sphinx_lmtools/sphinx_lm_convert.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 2009 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37/**
     38 * \file sphinx_lm_convert.c
     39 * Language model conversion tool.
     40 */
     41#include <logmath.h>
     42#include <ngram_model.h>
     43#include <cmd_ln.h>
     44#include <ckd_alloc.h>
     45#include <err.h>
     46#include <pio.h>
     47#include <strfuncs.h>
     48
     49#include <stdio.h>
     50#include <string.h>
     51#include <math.h>
     52
     53static const arg_t defn[] = {
     54  { "-help",
     55    ARG_BOOLEAN,
     56    "no",
     57    "Shows the usage of the tool"},
     58
     59  { "-logbase",
     60    ARG_FLOAT64,
     61    "1.0001",
     62    "Base in which all log-likelihoods calculated" },
     63
     64  { "-i",
     65    REQARG_STRING,
     66    NULL,
     67    "Input language model file (required)"},
     68
     69  { "-o",
     70    REQARG_STRING,
     71    NULL,
     72    "Output language model file (required)"},
     73
     74  { "-ifmt",
     75    ARG_STRING,
     76    NULL,
     77    "Input language model format (will guess if not specified)"},
     78
     79  { "-ofmt",
     80    ARG_STRING,
     81    NULL,
     82    "Output language model file (will guess if not specified)"},
     83
     84  { "-ienc",
     85    ARG_STRING,
     86    NULL,
     87    "Input language model text encoding (no conversion done if not specified)"},
     88
     89  { "-oenc",
     90    ARG_STRING,
     91    "utf8",
     92    "Output language model text encoding"},
     93
     94  { "-case",
     95    ARG_STRING,
     96    NULL,
     97    "Ether 'lower' or 'upper' - case fold to lower/upper case (NOT UNICODE AWARE)" },
     98
     99  { "-mmap",
     100    ARG_BOOLEAN,
     101    "no",
     102    "Use memory-mapped I/O for reading binary LM files"},
     103
     104  { "-debug",
     105    ARG_INT32,
     106    NULL,
     107    "Verbosity level for debugging messages"
     108  },
     109
     110  { NULL, 0, NULL, NULL }
     111};
     112
     113int
     114main(int argc, char *argv[])
     115{
     116    cmd_ln_t *config;
     117    ngram_model_t *lm = NULL;
     118    logmath_t *lmath;
     119        int itype, otype;
     120        char const *kase;
     121
     122    if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
     123        return 1;
     124
     125        err_set_debug_level(cmd_ln_int32_r(config, "-debug"));
     126
     127    /* Create log math object. */
     128    if ((lmath = logmath_init
     129         (cmd_ln_float64_r(config, "-logbase"), 0, 0)) == NULL) {
     130        E_FATAL("Failed to initialize log math\n");
     131    }
     132
     133    /* Load the input language model. */
     134        if (cmd_ln_str_r(config, "-ifmt")) {
     135            if ((itype = ngram_str_to_type(cmd_ln_str_r(config, "-ifmt")))
     136                == NGRAM_INVALID) {
     137                E_ERROR("Invaild input type %s\n", cmd_ln_str_r(config, "-ifmt"));
     138                goto error_out;
     139            }
     140            lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"),
     141                                  itype, lmath);
     142        }
     143        else {
     144            lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"),
     145                                  NGRAM_AUTO, lmath);
     146    }
     147
     148        /* Guess or set the output language model type. */
     149        if (cmd_ln_str_r(config, "-ofmt")) {
     150            if ((otype = ngram_str_to_type(cmd_ln_str_r(config, "-ofmt")))
     151                == NGRAM_INVALID) {
     152                E_ERROR("Invaild output type %s\n", cmd_ln_str_r(config, "-ofmt"));
     153                goto error_out;
     154            }
     155        }
     156        else {
     157            otype = ngram_file_name_to_type(cmd_ln_str_r(config, "-o"));
     158        }
     159
     160        /* Recode the language model if desired. */
     161        if (cmd_ln_str_r(config, "-ienc")) {
     162            if (ngram_model_recode(lm, cmd_ln_str_r(config, "-ienc"),
     163                                   cmd_ln_str_r(config, "-oenc")) != 0) {
     164                E_ERROR("Failed to recode language model from %s to %s\n",
     165                        cmd_ln_str_r(config, "-ienc"),
     166                        cmd_ln_str_r(config, "-oenc"));
     167                goto error_out;
     168            }
     169        }
     170
     171        /* Case fold if requested. */
     172        if ((kase = cmd_ln_str_r(config, "-case"))) {
     173            if (0 == strcmp(kase, "lower")) {
     174                ngram_model_casefold(lm, NGRAM_LOWER);
     175            }
     176            else if (0 == strcmp(kase, "upper")) {
     177                ngram_model_casefold(lm, NGRAM_UPPER);
     178            }
     179            else {
     180                E_ERROR("Unknown value for -case: %s\n", kase);
     181                goto error_out;
     182            }
     183        }
     184
     185        /* Write the output language model. */
     186        if (ngram_model_write(lm, cmd_ln_str_r(config, "-o"), otype) != 0) {
     187            E_ERROR("Failed to write language model in format %s to %s\n",
     188                    ngram_type_to_str(otype), cmd_ln_str_r(config, "-o"));
     189            goto error_out;
     190        }
     191
     192        /* That's all folks! */
     193        ngram_model_free(lm);
     194    return 0;
     195
     196error_out:
     197        ngram_model_free(lm);
     198    return 1;
     199}
  • src/tests/libs/sphinx/sphinx_lmtools/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx sphinx_lmtools ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libssphinxbase ;
     7
     8SimpleTest sphinx_lm_eval : sphinx_lm_eval.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest sphinx_lm_convert : sphinx_lm_convert.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/sphinx_lmtools/sphinx_lm_eval.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 2008 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37/**
     38 * \file sphinx_lm_eval.c
     39 * Language model evaluation tool.
     40 */
     41#include <logmath.h>
     42#include <ngram_model.h>
     43#include <cmd_ln.h>
     44#include <ckd_alloc.h>
     45#include <err.h>
     46#include <pio.h>
     47#include <strfuncs.h>
     48
     49#include <stdio.h>
     50#include <string.h>
     51#include <math.h>
     52
     53static const arg_t defn[] = {
     54  { "-help",
     55    ARG_BOOLEAN,
     56    "no",
     57    "Shows the usage of the tool"},
     58
     59  { "-logbase",
     60    ARG_FLOAT64,
     61    "1.0001",
     62    "Base in which all log-likelihoods calculated" },
     63
     64  { "-lm",
     65    ARG_STRING,
     66    NULL,
     67    "Language model file"},
     68
     69  { "-probdef",
     70    ARG_STRING,
     71    NULL,
     72    "Probability definition file for classes in LM"},
     73
     74  { "-lmctlfn",
     75    ARG_STRING,
     76    NULL,
     77    "Control file listing a set of language models"},
     78
     79  { "-lmname",
     80    ARG_STRING,
     81    NULL,
     82    "Name of language model in -lmctlfn to use for all utterances" },
     83
     84  { "-lsn",
     85    ARG_STRING,
     86    NULL,
     87    "Transcription file to evaluate"},
     88
     89  { "-text",
     90    ARG_STRING,
     91    "Text string to evaluate"},
     92
     93  { "-mmap",
     94    ARG_BOOLEAN,
     95    "no",
     96    "Use memory-mapped I/O for reading binary LM files"},
     97
     98  { "-lw",
     99    ARG_FLOAT32,
     100    "1.0",
     101    "Language model weight" },
     102
     103  { "-wip",
     104    ARG_FLOAT32,
     105    "1.0",
     106    "Word insertion probability" },
     107
     108  { "-uw",
     109    ARG_FLOAT32,
     110    "1.0",
     111    "Unigram probability weight (interpolated with uniform distribution)"},
     112
     113  /* FIXME: Support -lmstartsym, -lmendsym, -lmctlfn, -ctl_lm */
     114  { NULL, 0, NULL, NULL }
     115};
     116
     117static int
     118calc_entropy(ngram_model_t *lm, char **words, int32 n,
     119         int32 *out_n_ccs, int32 *out_n_oovs)
     120{
     121    int32 *wids;
     122    int32 startwid;
     123    int32 i, ch, nccs, noovs, unk;
     124
     125        if (n == 0)
     126            return 0;
     127
     128        unk = ngram_unknown_wid(lm);
     129
     130    /* Reverse this array into an array of word IDs. */
     131    wids = ckd_calloc(n, sizeof(*wids));
     132    for (i = 0; i < n; ++i)
     133        wids[n-i-1] = ngram_wid(lm, words[i]);
     134    /* Skip <s> as it's a context cue (HACK, this should be configurable). */
     135    startwid = ngram_wid(lm, "<s>");
     136
     137    /* Now evaluate the list of words in reverse using the
     138     * remainder of the array as the history. */
     139    ch = noovs = nccs = 0;
     140    for (i = 0; i < n; ++i) {
     141        int32 n_used;
     142        int32 prob;
     143
     144        /* Skip <s> as it's a context cue (HACK, this should be configurable). */
     145        if (wids[i] == startwid) {
     146            ++nccs;
     147            continue;
     148        }
     149        /* Skip and count OOVs. */
     150        if (wids[i] == NGRAM_INVALID_WID || wids[i] == unk) {
     151            ++noovs;
     152            continue;
     153        }
     154        /* Sum up information for each N-gram */
     155        prob = ngram_ng_score(lm,
     156                      wids[i], wids + i + 1,
     157                      n - i - 1, &n_used);
     158        ch -= prob;
     159    }
     160
     161    if (out_n_ccs) *out_n_ccs = nccs;
     162    if (out_n_oovs) *out_n_oovs = noovs;
     163
     164    /* Calculate cross-entropy CH = - 1/N sum log P(W|H) */
     165        n -= (nccs + noovs);
     166        if (n <= 0)
     167            return 0;
     168    return ch / n;
     169}
     170
     171static void
     172evaluate_file(ngram_model_t *lm, logmath_t *lmath, const char *lsnfn)
     173{
     174    FILE *fh;
     175        lineiter_t *litor;
     176    int32 nccs, noovs, nwords;
     177    float64 ch, log_to_log2;;
     178
     179    if ((fh = fopen(lsnfn, "r")) == NULL)
     180        E_FATAL_SYSTEM("failed to open transcript file %s", lsnfn);
     181
     182    /* We have to keep ch in floating-point to avoid overflows, so
     183     * we might as well use log2. */
     184    log_to_log2 = log(logmath_get_base(lmath)) / log(2);
     185    nccs = noovs = nwords = 0;
     186    ch = 0.0;
     187        for (litor = lineiter_start(fh); litor; litor = lineiter_next(litor)) {
     188        char **words;
     189        int32 n, tmp_ch, tmp_noovs, tmp_nccs;
     190
     191        n = str2words(litor->buf, NULL, 0);
     192        if (n < 0)
     193            E_FATAL("str2words(line, NULL, 0) = %d, should not happen\n", n);
     194        if (n == 0) /* Do nothing! */
     195            continue;
     196        words = ckd_calloc(n, sizeof(*words));
     197        str2words(litor->buf, words, n);
     198
     199        /* Remove any utterance ID (FIXME: has to be a single "word") */
     200        if (words[n-1][0] == '('
     201            && words[n-1][strlen(words[n-1])-1] == ')')
     202            n = n - 1;
     203
     204        tmp_ch = calc_entropy(lm, words, n, &tmp_nccs, &tmp_noovs);
     205
     206        ch += (float64) tmp_ch * (n - tmp_nccs - tmp_noovs) * log_to_log2;
     207        nccs += tmp_nccs;
     208        noovs += tmp_noovs;
     209        nwords += n;
     210       
     211        ckd_free(words);
     212    }
     213
     214    ch /= (nwords - nccs - noovs);
     215    printf("cross-entropy: %f bits\n", ch);
     216
     217    /* Calculate perplexity pplx = exp CH */
     218    printf("perplexity: %f\n", pow(2.0, ch));
     219
     220    /* Report OOVs and CCs */
     221    printf("%d words evaluated\n", nwords);
     222    printf("%d OOVs (%.2f%%), %d context cues removed\n",
     223           noovs, (double)noovs / nwords * 100, nccs);
     224}
     225
     226static void
     227evaluate_string(ngram_model_t *lm, logmath_t *lmath, const char *text)
     228{
     229    char *textfoo;
     230    char **words;
     231    int32 n, ch, noovs, nccs;
     232
     233    /* Split it into an array of strings. */
     234    textfoo = ckd_salloc(text);
     235    n = str2words(textfoo, NULL, 0);
     236    if (n < 0)
     237        E_FATAL("str2words(textfoo, NULL, 0) = %d, should not happen\n", n);
     238    if (n == 0) /* Do nothing! */
     239        return;
     240    words = ckd_calloc(n, sizeof(*words));
     241    str2words(textfoo, words, n);
     242
     243    ch = calc_entropy(lm, words, n, &nccs, &noovs);
     244
     245    printf("input: %s\n", text);
     246    printf("cross-entropy: %f bits\n",
     247           ch * log(logmath_get_base(lmath)) / log(2));
     248
     249    /* Calculate perplexity pplx = exp CH */
     250    printf("perplexity: %f\n", logmath_exp(lmath, ch));
     251
     252    /* Report OOVs and CCs */
     253    printf("%d words evaluated\n", n);
     254    printf("%d OOVs, %d context cues removed\n",
     255          noovs, nccs);
     256
     257    ckd_free(textfoo);
     258    ckd_free(words);
     259}
     260
     261int
     262main(int argc, char *argv[])
     263{
     264    cmd_ln_t *config;
     265    ngram_model_t *lm = NULL;
     266    logmath_t *lmath;
     267    const char *lmfn, *probdefn, *lsnfn, *text;
     268
     269    if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
     270        return 1;
     271
     272    /* Create log math object. */
     273    if ((lmath = logmath_init
     274         (cmd_ln_float64_r(config, "-logbase"), 0, 0)) == NULL) {
     275        E_FATAL("Failed to initialize log math\n");
     276    }
     277
     278    /* Load the language model. */
     279    lmfn = cmd_ln_str_r(config, "-lm");
     280    if (lmfn == NULL
     281        || (lm = ngram_model_read(config, lmfn,
     282                      NGRAM_AUTO, lmath)) == NULL) {
     283        E_FATAL("Failed to load language model from %s\n",
     284            cmd_ln_str_r(config, "-lm"));
     285    }
     286        if ((probdefn = cmd_ln_str_r(config, "-probdef")) != NULL)
     287            ngram_model_read_classdef(lm, probdefn);
     288        ngram_model_apply_weights(lm,
     289                                  cmd_ln_float32_r(config, "-lw"),
     290                                  cmd_ln_float32_r(config, "-wip"),
     291                                  cmd_ln_float32_r(config, "-uw"));
     292
     293    /* Now evaluate some text. */
     294    lsnfn = cmd_ln_str_r(config, "-lsn");
     295    text = cmd_ln_str_r(config, "-text");
     296    if (lsnfn) {
     297        evaluate_file(lm, lmath, lsnfn);
     298    }
     299    else if (text) {
     300        evaluate_string(lm, lmath, text);
     301    }
     302
     303    return 0;
     304}
  • src/tests/libs/sphinx/sphinx_fe/sphinx_wave2feat.h

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 1996-2004 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37#ifndef __SPHINX_FE_H__
     38#define __SPHINX_FE_H__
     39
     40/**
     41 * Waveform to feature converter object.
     42 */
     43typedef struct sphinx_wave2feat_s sphinx_wave2feat_t;
     44
     45/**
     46 * Initialize waveform to feature converter.
     47 */
     48sphinx_wave2feat_t *sphinx_wave2feat_init(cmd_ln_t *config);
     49
     50/**
     51 * Release a waveform to feature converter.
     52 */
     53int sphinx_wave2feat_free(sphinx_wave2feat_t *w2f);
     54
     55/**
     56 * Retain a waveform to feature converter.
     57 */
     58sphinx_wave2feat_t *sphinx_wave2feat_retain(sphinx_wave2feat_t *w2f);
     59
     60/**
     61 * Convert one file.
     62 */
     63int sphinx_wave2feat_convert_file(sphinx_wave2feat_t *w2f,
     64                  char const *infile, char const *outfile);
     65
     66#endif /* __SPHINX_FE_H__ */
  • src/tests/libs/sphinx/sphinx_fe/sphinx_fe.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 1996-2004 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37#include <stdio.h>
     38#include <stdlib.h>
     39#include <string.h>
     40#include <time.h>
     41#include <assert.h>
     42
     43#ifdef HAVE_CONFIG_H
     44#include <config.h>
     45#endif
     46
     47#include "fe.h"
     48#include "strfuncs.h"
     49#include "pio.h"
     50#include "filename.h"
     51#include "cmd_ln.h"
     52#include "err.h"
     53#include "ckd_alloc.h"
     54#include "byteorder.h"
     55
     56#include "sphinx_wave2feat.h"
     57#include "cmd_ln_defn.h"
     58
     59typedef struct audio_type_s {
     60    char const *name;
     61    int (*detect)(sphinx_wave2feat_t *wtf, char const *infile);
     62    int (*decode)(sphinx_wave2feat_t *wtf);
     63} audio_type_t;
     64
     65typedef struct output_type_s {
     66    char const *name;
     67    int (*output_header)(sphinx_wave2feat_t *wtf, int nfloat);
     68    int (*output_frames)(sphinx_wave2feat_t *wtf, mfcc_t **frames, int nfr);
     69} output_type_t;
     70
     71struct sphinx_wave2feat_s {
     72    int refcount;     /**< Reference count. */
     73    cmd_ln_t *config; /**< Configuration parameters. */
     74    fe_t *fe;         /**< Front end object. */
     75    char *infile;     /**< Path to input file. */
     76    char *outfile;    /**< Path to output file. */
     77    FILE *infh;       /**< Input file handle. */
     78    FILE *outfh;      /**< Output file handle. */
     79    short *audio;     /**< Audio buffer. */
     80    mfcc_t **feat;    /**< Feature buffer. */
     81    int blocksize;    /**< Size of audio buffer. */
     82    int featsize;     /**< Size of feature buffer. */
     83    int veclen;       /**< Length of each output vector. */
     84    int in_veclen;    /**< Length of each input vector (for cep<->spec). */
     85    int byteswap;     /**< Whether byteswapping is necessary. */
     86    output_type_t const *ot;/**< Output type object. */
     87};
     88
     89/** RIFF 44-byte header structure for MS wav files. */
     90typedef struct RIFFHeader{
     91    char rifftag[4];      /* "RIFF" string */
     92    int32 TotalLength;      /* Total length */
     93    char wavefmttag[8];   /* "WAVEfmt " string (note space after 't') */
     94    int32 RemainingLength;  /* Remaining length */
     95    int16 data_format;    /* data format tag, 1 = PCM */
     96    int16 numchannels;    /* Number of channels in file */
     97    int32 SamplingFreq;     /* Sampling frequency */
     98    int32 BytesPerSec;      /* Average bytes/sec */
     99    int16 BlockAlign;     /* Block align */
     100    int16 BitsPerSample;  /* 8 or 16 bit */
     101    char datatag[4];      /* "data" string */
     102    int32 datalength;       /* Raw data length */
     103} MSWAV_hdr;
     104
     105/**
     106 * Detect RIFF file and parse its header if detected.
     107 *
     108 * @return TRUE if it's a RIFF file, FALSE if not, -1 if an error occurred.
     109 */
     110static int
     111detect_riff(sphinx_wave2feat_t *wtf, char const *infile)
     112{
     113    FILE *fh;
     114    MSWAV_hdr hdr;
     115
     116    if ((fh = fopen(infile, "rb")) == NULL) {
     117        E_ERROR_SYSTEM("Failed to open %s", infile);
     118        return -1;
     119    }
     120    if (fread(&hdr, sizeof(hdr), 1, fh) != 1) {
     121        E_ERROR_SYSTEM("Failed to read RIFF header");
     122        fclose(fh);
     123        return -1;
     124    }
     125    /* Make sure it is actually a RIFF file. */
     126    if (0 != memcmp(hdr.rifftag, "RIFF", 4))
     127        return FALSE;
     128
     129    /* Get relevant information. */
     130    cmd_ln_set_int32_r(wtf->config, "-nchans", hdr.numchannels);
     131    cmd_ln_set_float32_r(wtf->config, "-samprate", hdr.SamplingFreq);
     132    wtf->infile = ckd_salloc(infile);
     133    wtf->infh = fh;
     134
     135    return TRUE;
     136}
     137
     138/**
     139 * Detect NIST file and parse its header if detected.
     140 *
     141 * @return TRUE if it's a NIST file, FALSE if not, -1 if an error occurred.
     142 */
     143static int
     144detect_nist(sphinx_wave2feat_t *wtf, char const *infile)
     145{
     146    char nist[7];
     147    lineiter_t *li;
     148    FILE *fh;
     149
     150    if ((fh = fopen(infile, "rb")) == NULL) {
     151        E_ERROR_SYSTEM("Failed to open %s", infile);
     152        return -1;
     153    }
     154    if (fread(&nist, 1, 7, fh) != 7) {
     155        E_ERROR_SYSTEM("Failed to read NIST header");
     156        fclose(fh);
     157        return -1;
     158    }
     159    /* Is this actually a NIST file? */
     160    if (0 != strncmp(nist, "NIST_1A", 7)) {
     161        fclose(fh);
     162        return FALSE;
     163    }
     164    /* Rewind, parse lines. */
     165    fseek(fh, 0, SEEK_SET);
     166    for (li = lineiter_start(fh); li; li = lineiter_next(li)) {
     167        char **words;
     168        int nword;
     169
     170        string_trim(li->buf, STRING_BOTH);
     171        if (strlen(li->buf) == 0)
     172            break;
     173        nword = str2words(li->buf, NULL, 0);
     174        if (nword != 3)
     175            continue;
     176        words = ckd_calloc(nword, sizeof(*words));
     177        str2words(li->buf, words, nword);
     178        if (0 == strcmp(words[0], "sample_rate")) {
     179            cmd_ln_set_float32_r(wtf->config, "-samprate", atof_c(words[2]));
     180        }
     181        if (0 == strcmp(words[0], "channel_count")) {
     182            cmd_ln_set_float32_r(wtf->config, "-nchans", atoi(words[2]));
     183        }
     184        if (0 == strcmp(words[0], "sample_byte_format")) {
     185            cmd_ln_set_str_r(wtf->config, "-input_endian",
     186                             (0 == strcmp(words[2], "10")) ? "big" : "little");
     187        }
     188        /* FIMXE: Warn about shorten-compressed data. */
     189        ckd_free(words);
     190    }
     191    fseek(fh, 1024, SEEK_SET);
     192    wtf->infile = ckd_salloc(infile);
     193    wtf->infh = fh;
     194    return TRUE;
     195}
     196
     197
     198/**
     199 * Default "detection" function, just opens the file and keeps the
     200 * default configuration parameters.
     201 *
     202 * @return TRUE, or -1 on error.
     203 */
     204static int
     205detect_raw(sphinx_wave2feat_t *wtf, char const *infile)
     206{
     207    FILE *fh;
     208
     209    if ((fh = fopen(infile, "rb")) == NULL) {
     210        E_ERROR_SYSTEM("Failed to open %s", infile);
     211        return -1;
     212    }
     213    wtf->infile = ckd_salloc(infile);
     214    wtf->infh = fh;
     215    return TRUE;
     216}
     217
     218/**
     219 * "Detect" Sphinx MFCC files, meaning verify their lousy headers, and
     220 * set up some parameters from the config object.
     221 *
     222 * @return TRUE, or -1 on error.
     223 */
     224static int
     225detect_sphinx_mfc(sphinx_wave2feat_t *wtf, char const *infile)
     226{
     227    FILE *fh;
     228    int32 len;
     229    long flen;
     230
     231    if ((fh = fopen(infile, "rb")) == NULL) {
     232        E_ERROR_SYSTEM("Failed to open %s", infile);
     233        return -1;
     234    }
     235    if (fread(&len, 4, 1, fh) != 1) {
     236        E_ERROR_SYSTEM("Failed to read header from %s\n", infile);
     237        return -1;
     238    }
     239    fseek(fh, 0, SEEK_END);
     240    flen = ftell(fh);
     241
     242    /* figure out whether to byteswap */
     243    flen = (flen / 4) - 1;
     244    if (flen != len) {
     245        /* First make sure this is an endianness problem, otherwise fail. */
     246        SWAP_INT32(&len);
     247        if (flen != len) {
     248            SWAP_INT32(&len);
     249            E_ERROR("Mismatch in header/file lengths: 0x%08x vs 0x%08x\n",
     250                    len, flen);
     251            return -1;
     252        }
     253        /* Set the input endianness to the opposite of the machine endianness... */
     254        cmd_ln_set_str_r(wtf->config, "-input_endian",
     255                         (0 == strcmp("big", cmd_ln_str_r(wtf->config, "-mach_endian"))
     256                          ? "little" : "big"));
     257    }
     258   
     259    fseek(fh, 4, SEEK_SET);
     260    wtf->infile = ckd_salloc(infile);
     261    wtf->infh = fh;
     262    if (cmd_ln_boolean_r(wtf->config, "-spec2cep")) {
     263        wtf->in_veclen = cmd_ln_int32_r(wtf->config, "-nfilt");
     264    }
     265    else if (cmd_ln_boolean_r(wtf->config, "-cep2spec")) {
     266        wtf->in_veclen = cmd_ln_int32_r(wtf->config, "-ncep");
     267        wtf->veclen = cmd_ln_int32_r(wtf->config, "-nfilt");
     268    }
     269    else {
     270        /* Should not happen. */
     271        E_ERROR("Sphinx MFCC file reading requested but -spec2cep/-cep2spec not given\n");
     272        assert(FALSE);
     273    }
     274           
     275    return TRUE;
     276}
     277
     278/**
     279 * Process PCM audio from a filehandle.  Assume that wtf->infh is
     280 * positioned just after the file header.
     281 */
     282static int
     283decode_pcm(sphinx_wave2feat_t *wtf)
     284{
     285    size_t nsamp;
     286    int32 nfr;
     287    int nfloat, n;
     288
     289    fe_start_utt(wtf->fe);
     290    nfloat = 0;
     291    while ((nsamp = fread(wtf->audio, 2, wtf->blocksize, wtf->infh)) != 0) {
     292        size_t nvec;
     293        int16 const *inspeech;
     294
     295        /* Byteswap stuff here if necessary. */
     296        if (wtf->byteswap) {
     297            for (n = 0; n < nsamp; ++n)
     298                SWAP_INT16(wtf->audio + n);
     299        }
     300           
     301        inspeech = wtf->audio;
     302        nvec = wtf->featsize;
     303        /* Consume all samples. */
     304        while (nsamp) {
     305            nfr = nvec;
     306            fe_process_frames(wtf->fe, &inspeech, &nsamp, wtf->feat, &nfr);
     307            if (nfr) {
     308                if ((n = (*wtf->ot->output_frames)(wtf, wtf->feat, nfr)) < 0)
     309                    return -1;
     310                nfloat += n;
     311            }
     312        }
     313        inspeech = wtf->audio;
     314    }
     315    /* Now process any leftover audio frames. */
     316    fe_end_utt(wtf->fe, wtf->feat[0], &nfr);
     317    if (nfr) {
     318        if ((n = (*wtf->ot->output_frames)(wtf, wtf->feat, nfr)) < 0)
     319            return -1;
     320        nfloat += n;
     321    }
     322
     323    fclose(wtf->infh);
     324    wtf->infh = NULL;
     325    return nfloat;
     326}
     327
     328/**
     329 * Process Sphinx MFCCs/logspectra from a filehandle.  Assume that
     330 * wtf->infh is positioned just after the file header.
     331 */
     332static int
     333decode_sphinx_mfc(sphinx_wave2feat_t *wtf)
     334{
     335    int nfloat = 0, n;
     336    int featsize = wtf->featsize;
     337
     338    /* If the input vector length is less than the output length, we
     339     * need to do this one frame at a time, because there's empty
     340     * space at the end of each vector in wtf->feat. */
     341    if (wtf->in_veclen < wtf->veclen)
     342        featsize = 1;
     343    while ((n = fread(wtf->feat[0], sizeof(**wtf->feat),
     344                      featsize * wtf->in_veclen, wtf->infh)) != 0) {
     345        int i, nfr = n / wtf->in_veclen;
     346        if (n % wtf->in_veclen) {
     347            E_ERROR("Size of file %d not a multiple of veclen %d\n",
     348                    n, wtf->in_veclen);
     349            return -1;
     350        }
     351        /* Byteswap stuff here if necessary. */
     352        if (wtf->byteswap) {
     353            for (i = 0; i < n; ++i)
     354                SWAP_FLOAT32(wtf->feat[0] + i);
     355        }
     356        fe_float_to_mfcc(wtf->fe, (float32 **)wtf->feat, wtf->feat, nfr);
     357        for (i = 0; i < nfr; ++i) {
     358            if (cmd_ln_boolean_r(wtf->config, "-spec2cep")) {
     359                if (0 == strcmp(cmd_ln_str_r(wtf->config, "-transform"), "legacy"))
     360                    fe_logspec_to_mfcc(wtf->fe, wtf->feat[i], wtf->feat[i]);
     361                else
     362                    fe_logspec_dct2(wtf->fe, wtf->feat[i], wtf->feat[i]);
     363            }
     364            else if (cmd_ln_boolean_r(wtf->config, "-cep2spec")) {
     365                fe_mfcc_dct3(wtf->fe, wtf->feat[i], wtf->feat[i]);
     366            }
     367        }
     368        if ((n = (*wtf->ot->output_frames)(wtf, wtf->feat, nfr)) < 0)
     369            return -1;
     370        nfloat += n;
     371    }
     372
     373    fclose(wtf->infh);
     374    wtf->infh = NULL;
     375    return nfloat;
     376}
     377
     378static const audio_type_t types[] = {
     379    { "-mswav", &detect_riff, &decode_pcm },
     380    { "-nist", &detect_nist, &decode_pcm },
     381#ifdef HAVE_SNDFILE
     382    { "-sndfile", &detect_sndfile, &decode_sndfile },
     383#endif
     384    { "-raw", &detect_raw, &decode_pcm }
     385};
     386static const int ntypes = sizeof(types)/sizeof(types[0]);
     387static const audio_type_t mfcc_type = {
     388    "sphinx_mfc", &detect_sphinx_mfc, &decode_sphinx_mfc
     389};
     390
     391/**
     392 * Output sphinx format "header"
     393 *
     394 * @return 0 for success, <0 for error.
     395 */
     396static int
     397output_header_sphinx(sphinx_wave2feat_t *wtf, int32 nfloat)
     398{
     399    if (fwrite(&nfloat, 4, 1, wtf->outfh) != 1) {
     400        E_ERROR_SYSTEM("Failed to write to %s", wtf->outfile);
     401        return -1;
     402    }
     403    return 0;
     404}
     405
     406/**
     407 * Output frames in sphinx format.
     408 *
     409 * @return 0 for success, <0 for error.
     410 */
     411static int
     412output_frames_sphinx(sphinx_wave2feat_t *wtf, mfcc_t **frames, int nfr)
     413{
     414    int i, nfloat = 0;
     415
     416    fe_mfcc_to_float(wtf->fe, frames, (float32 **)frames, nfr);
     417    for (i = 0; i < nfr; ++i) {
     418        if (fwrite(frames[i], sizeof(float32), wtf->veclen, wtf->outfh) != wtf->veclen) {
     419            E_ERROR_SYSTEM("Writing %d values to %s failed",
     420                           wtf->veclen, wtf->outfile);
     421            return -1;
     422        }
     423        nfloat += wtf->veclen;
     424    }
     425    return nfloat;
     426}
     427
     428typedef enum htk_feature_kind_e {
     429    WAVEFORM = 0,   /* PCM audio (rarely used) */
     430    LPC = 1,        /* LPC filter coefficients */
     431    LPCREFC = 2,    /* LPC reflection coefficients */
     432    LPCEPSTRA = 3,  /* LPC-based cepstral coefficients */
     433    LPCDELCEP = 4,  /* LPCC plus deltas */
     434    IREFC = 5,      /* 16-bit integer LPC reflection coefficients */
     435    MFCC = 6,       /* MFCCs */
     436    FBANK = 7,      /* Log mel spectrum */
     437    MELSPEC = 8,    /* Linear mel spectrum */
     438    USER = 9,       /* User defined */
     439    DISCRETE = 10,  /* Vector quantized data */
     440    PLP = 11        /* PLP coefficients */
     441} htk_feature_kind_t;
     442
     443typedef enum htk_feature_flag_e {
     444    _E = 0000100, /* has energy */
     445    _N = 0000200, /* absolute energy supressed */
     446    _D = 0000400, /* has delta coefficients */
     447    _A = 0001000, /* has acceleration (delta-delta) coefficients */
     448    _C = 0002000, /* is compressed */
     449    _Z = 0004000, /* has zero mean static coefficients (i.e. CMN) */
     450    _K = 0010000, /* has CRC checksum */
     451    _O = 0020000, /* has 0th cepstral coefficient */
     452    _V = 0040000, /* has VQ data */
     453    _T = 0100000  /* has third differential coefficients */
     454} htk_feature_flag_t;
     455
     456/**
     457 * Output HTK format header.
     458 */
     459static int
     460output_header_htk(sphinx_wave2feat_t *wtf, int32 nfloat)
     461{
     462    int32 samp_period;
     463    int16 samp_size;
     464    int16 param_kind;
     465    int swap = FALSE;
     466
     467    /* HTK files are big-endian. */
     468    if (0 == strcmp("little", cmd_ln_str_r(wtf->config, "-mach_endian")))
     469        swap = TRUE;
     470    /* Same file size thing as in Sphinx files (I think) */
     471    if (swap) SWAP_INT32(&nfloat);
     472    if (fwrite(&nfloat, 4, 1, wtf->outfh) != 1)
     473        return -1;
     474    /* Sample period in 100ns units. */
     475    samp_period = (int32)(1e+7 / cmd_ln_float32_r(wtf->config, "-frate"));
     476    if (swap) SWAP_INT32(&samp_period);
     477    if (fwrite(&samp_period, 4, 1, wtf->outfh) != 1)
     478        return -1;
     479    /* Sample size - veclen * sizeof each sample. */
     480    samp_size = wtf->veclen * 4;
     481    if (swap) SWAP_INT16(&samp_size);
     482    if (fwrite(&samp_size, 2, 1, wtf->outfh) != 1)
     483        return -1;
     484    /* Format and flags. */
     485    if (cmd_ln_boolean_r(wtf->config, "-logspec")
     486        || cmd_ln_boolean_r(wtf->config, "-cep2spec"))
     487        param_kind = FBANK; /* log mel-filter bank outputs */
     488    else
     489        param_kind = MFCC | _O; /* MFCC + CEP0 (note reordering...) */
     490    if (swap) SWAP_INT16(&param_kind);
     491    if (fwrite(&param_kind, 2, 1, wtf->outfh) != 1)
     492        return -1;
     493
     494    return 0;
     495}
     496
     497/**
     498 * Output frames in HTK format.
     499 */
     500static int
     501output_frames_htk(sphinx_wave2feat_t *wtf, mfcc_t **frames, int nfr)
     502{
     503    int i, j, swap, htk_reorder, nfloat = 0;
     504
     505    fe_mfcc_to_float(wtf->fe, frames, (float32 **)frames, nfr);
     506    /* This is possibly inefficient, but probably not a big deal. */
     507    swap = (0 == strcmp("little", cmd_ln_str_r(wtf->config, "-mach_endian")));
     508    htk_reorder = (0 == strcmp("htk", wtf->ot->name)
     509                   && !(cmd_ln_boolean_r(wtf->config, "-logspec")
     510                        || cmd_ln_boolean_r(wtf->config, "-cep2spec")));
     511    for (i = 0; i < nfr; ++i) {
     512        if (htk_reorder) {
     513            mfcc_t c0 = frames[i][0];
     514            memmove(frames[i] + 1, frames[i], (wtf->veclen - 1) * 4);
     515            frames[i][wtf->veclen - 1] = c0;
     516        }
     517        if (swap)
     518            for (j = 0; j < wtf->veclen; ++j)
     519                SWAP_FLOAT32(frames[i] + j);
     520        if (fwrite(frames[i], sizeof(float32), wtf->veclen, wtf->outfh) != wtf->veclen) {
     521            E_ERROR_SYSTEM("Writing %d values to %s failed",
     522                           wtf->veclen, wtf->outfile);
     523            return -1;
     524        }
     525        nfloat += wtf->veclen;
     526    }
     527    return nfloat;
     528}
     529
     530/**
     531 * Output frames in text format.
     532 */
     533static int
     534output_frames_text(sphinx_wave2feat_t *wtf, mfcc_t **frames, int nfr)
     535{
     536    int i, j, nfloat = 0;
     537
     538    fe_mfcc_to_float(wtf->fe, frames, (float32 **)frames, nfr);
     539    for (i = 0; i < nfr; ++i) {
     540        for (j = 0; j < wtf->veclen; ++j) {
     541            fprintf(wtf->outfh, "%.5g", frames[i][j]);
     542            if (j == wtf->veclen - 1)
     543                fprintf(wtf->outfh, "\n");
     544            else
     545                fprintf(wtf->outfh, " ");
     546        }
     547        nfloat += wtf->veclen;
     548    }
     549    return nfloat;
     550}
     551
     552static const output_type_t outtypes[] = {
     553    { "sphinx", &output_header_sphinx, &output_frames_sphinx },
     554    { "htk", &output_header_htk, &output_frames_htk },
     555    { "text", NULL, &output_frames_text }
     556};
     557static const int nouttypes = sizeof(outtypes)/sizeof(outtypes[0]);
     558
     559sphinx_wave2feat_t *
     560sphinx_wave2feat_init(cmd_ln_t *config)
     561{
     562    sphinx_wave2feat_t *wtf;
     563    int i;
     564
     565    wtf = ckd_calloc(1, sizeof(*wtf));
     566    wtf->refcount = 1;
     567    wtf->config = cmd_ln_retain(config);
     568    wtf->fe = fe_init_auto_r(wtf->config);
     569    wtf->ot = outtypes; /* Default (sphinx) type. */
     570    for (i = 0; i < nouttypes; ++i) {
     571        output_type_t const *otype = &outtypes[i];
     572        if (0 == strcmp(cmd_ln_str_r(config, "-ofmt"), otype->name)) {
     573            wtf->ot = otype;
     574            break;
     575        }
     576    }
     577    if (i == nouttypes) {
     578        E_ERROR("Unknown output type: '%s'\n",
     579                cmd_ln_str_r(config, "-ofmt"));
     580        sphinx_wave2feat_free(wtf);
     581        return NULL;
     582    }
     583
     584    return wtf;
     585}
     586
     587int
     588sphinx_wave2feat_free(sphinx_wave2feat_t *wtf)
     589{
     590    if (wtf == NULL)
     591        return 0;
     592    if (--wtf->refcount > 0)
     593        return wtf->refcount;
     594
     595    ckd_free(wtf->audio);
     596    ckd_free_2d(wtf->feat);
     597    ckd_free(wtf->infile);
     598    ckd_free(wtf->outfile);
     599    if (wtf->infh)
     600        fclose(wtf->infh);
     601    if (wtf->outfh)
     602        fclose(wtf->outfh);
     603    cmd_ln_free_r(wtf->config);
     604    fe_free(wtf->fe);
     605    ckd_free(wtf);
     606
     607    return 0;
     608}
     609
     610sphinx_wave2feat_t *
     611sphinx_wave2feat_retain(sphinx_wave2feat_t *wtf)
     612{
     613    ++wtf->refcount;
     614    return wtf;
     615}
     616
     617static audio_type_t const *
     618detect_audio_type(sphinx_wave2feat_t *wtf, char const *infile)
     619{
     620    audio_type_t const *atype;
     621    int i;
     622
     623    /* Special case audio type for Sphinx MFCC inputs. */
     624    if (cmd_ln_boolean_r(wtf->config, "-spec2cep")
     625        || cmd_ln_boolean_r(wtf->config, "-cep2spec")) {
     626        int rv = mfcc_type.detect(wtf, infile);
     627        if (rv == -1)
     628            return NULL;
     629        return &mfcc_type;
     630    }
     631
     632    /* Try to use the type of infile given on the command line. */
     633    for (i = 0; i < ntypes; ++i) {
     634        int rv;
     635        atype = &types[i];
     636        if (cmd_ln_boolean_r(wtf->config, atype->name)) {
     637            rv = (*atype->detect)(wtf, infile);
     638            if (rv == -1)
     639                return NULL;
     640            break;
     641        }
     642    }
     643    if (i == ntypes) {
     644        /* Detect file type of infile and get parameters. */
     645        for (i = 0; i < ntypes; ++i) {
     646            int rv;
     647            atype = &types[i];
     648            rv = (*atype->detect)(wtf, infile);
     649            if (rv == -1)
     650                return NULL;
     651            else if (rv == TRUE)
     652            break;
     653        }
     654        if (i == ntypes)
     655            atype = NULL;
     656    }
     657    return atype;
     658}
     659
     660int
     661sphinx_wave2feat_convert_file(sphinx_wave2feat_t *wtf,
     662                              char const *infile, char const *outfile)
     663{
     664    int minfft, nfft, nfloat, veclen;
     665    audio_type_t const *atype;
     666    int fshift, fsize;
     667
     668    if (cmd_ln_boolean_r(wtf->config, "-verbose"))
     669        E_INFO("Converting %s to %s\n", infile, outfile);
     670
     671    /* Detect input file type. */
     672    if ((atype = detect_audio_type(wtf, infile)) == NULL)
     673        return -1;
     674
     675    /* Determine whether to byteswap input. */
     676    wtf->byteswap = strcmp(cmd_ln_str_r(wtf->config, "-mach_endian"),
     677                           cmd_ln_str_r(wtf->config, "-input_endian"));
     678
     679    /* Make sure the FFT size is sufficiently large. */
     680    minfft = (int)(cmd_ln_float32_r(wtf->config, "-samprate")
     681                   * cmd_ln_float32_r(wtf->config, "-wlen") + 0.5);
     682    for (nfft = 1; nfft < minfft; nfft <<= 1)
     683        ;
     684    if (nfft > cmd_ln_int32_r(wtf->config, "-nfft")) {
     685        E_WARN("Value of -nfft = %d is too small, increasing to %d\n",
     686               cmd_ln_int32_r(wtf->config, "-nfft"), nfft);
     687        cmd_ln_set_int32_r(wtf->config, "-nfft", nfft);
     688        fe_free(wtf->fe);
     689        wtf->fe = fe_init_auto_r(wtf->config);
     690    }
     691
     692    /* Get the output frame size (if not already set). */
     693    if (wtf->veclen == 0)
     694        wtf->veclen = fe_get_output_size(wtf->fe);
     695
     696    /* Set up the input and output buffers. */
     697    fe_get_input_size(wtf->fe, &fshift, &fsize);
     698    /* Want to get at least a whole frame plus shift in here. */
     699    wtf->blocksize = cmd_ln_int32_r(wtf->config, "-blocksize");
     700    if (wtf->blocksize < fsize + fshift) {
     701        E_INFO("Block size of %d too small, increasing to %d\n",
     702               wtf->blocksize, fsize + fshift);
     703        wtf->blocksize = fsize + fshift;
     704    }
     705    wtf->audio = ckd_calloc(wtf->blocksize, sizeof(*wtf->audio));
     706    wtf->featsize = (wtf->blocksize - fsize) / fshift;
     707
     708    /* Use the maximum of the input and output frame sizes to allocate this. */
     709    veclen = wtf->veclen;
     710    if (wtf->in_veclen > veclen) veclen = wtf->in_veclen;
     711    wtf->feat = ckd_calloc_2d(wtf->featsize, veclen, sizeof(**wtf->feat));
     712
     713    /* Let's go! */
     714    if ((wtf->outfh = fopen(outfile, "wb")) == NULL) {
     715        E_ERROR_SYSTEM("Failed to open %s for writing", outfile);
     716        return -1;
     717    }
     718    /* Write an empty header, which we'll fill in later. */
     719    if (wtf->ot->output_header &&
     720        (*wtf->ot->output_header)(wtf, 0) < 0) {
     721        E_ERROR_SYSTEM("Failed to write empty header to %s\n", outfile);
     722        goto error_out;
     723    }
     724    wtf->outfile = ckd_salloc(outfile);
     725
     726    if ((nfloat = (*atype->decode)(wtf)) < 0)
     727        return -1;
     728
     729    if (wtf->ot->output_header) {
     730        if (fseek(wtf->outfh, 0, SEEK_SET) < 0) {
     731            E_ERROR_SYSTEM("Failed to seek to beginning of %s\n", outfile);
     732            goto error_out;
     733        }
     734        if ((*wtf->ot->output_header)(wtf, nfloat) < 0) {
     735            E_ERROR_SYSTEM("Failed to write header to %s\n", outfile);
     736            goto error_out;
     737        }
     738    }
     739    fclose(wtf->outfh);
     740    wtf->outfh = NULL;
     741
     742    return 0;
     743error_out:
     744    if (wtf->outfh) {
     745        fclose(wtf->outfh);
     746        wtf->outfh = NULL;
     747    }
     748    return -1;
     749}
     750
     751void
     752build_filenames(cmd_ln_t *config, char const *basename,
     753                char **out_infile, char **out_outfile)
     754{
     755    char const *di, *do_, *ei, *eo;
     756
     757    di = cmd_ln_str_r(config, "-di");
     758    do_ = cmd_ln_str_r(config, "-do");
     759    ei = cmd_ln_str_r(config, "-ei");
     760    eo = cmd_ln_str_r(config, "-eo");
     761
     762    *out_infile = string_join(di ? di : "",
     763                              di ? "/" : "",
     764                              basename,
     765                              ei ? "." : "",
     766                              ei ? ei : "",
     767                              NULL);
     768    *out_outfile = string_join(do_ ? do_ : "",
     769                               do_ ? "/" : "",
     770                               basename,
     771                               eo ? "." : "",
     772                               eo ? eo : "",
     773                              NULL);
     774    /* Build output directory structure if possible/requested (it is
     775     * by default). */
     776    if (cmd_ln_boolean_r(config, "-build_outdirs")) {
     777        char *dirname = ckd_salloc(*out_outfile);
     778        path2dirname(*out_outfile, dirname);
     779        build_directory(dirname);
     780        ckd_free(dirname);
     781    }
     782}
     783
     784static int
     785run_control_file(sphinx_wave2feat_t *wtf, char const *ctlfile)
     786{
     787    lineiter_t *li;
     788    FILE *ctlfh;
     789    int nskip, runlen, npart;
     790
     791    if ((ctlfh = fopen(ctlfile, "r")) == NULL) {
     792        E_ERROR_SYSTEM("Failed to open control file %s", ctlfile);
     793        return -1;
     794    }
     795    nskip = cmd_ln_int32_r(wtf->config, "-nskip");
     796    runlen = cmd_ln_int32_r(wtf->config, "-runlen");
     797    if ((npart = cmd_ln_int32_r(wtf->config, "-npart"))) {
     798        /* Count lines in the file. */
     799        int nlines, partlen, part;
     800        part = cmd_ln_int32_r(wtf->config, "-part");
     801        for (nlines = 0, li = lineiter_start(ctlfh); li; li = lineiter_next(li))
     802            ++nlines;
     803        fseek(ctlfh, 0, SEEK_SET);
     804        partlen = nlines / npart;
     805        nskip = partlen * (part - 1);
     806        if (part == npart)
     807            runlen = -1;
     808        else
     809            runlen = partlen;
     810    }
     811    if (runlen != -1)
     812        E_INFO("Processing %d utterances at position %d\n", runlen, nskip);
     813    else
     814        E_INFO("Processing all remaining utterances at position %d\n", nskip);
     815    for (li = lineiter_start(ctlfh); li; li = lineiter_next(li)) {
     816        char *infile, *outfile;
     817        int rv;
     818
     819        if (nskip-- > 0)
     820            continue;
     821        if (runlen == 0)
     822            break;
     823        --runlen;
     824
     825        string_trim(li->buf, STRING_BOTH);
     826        build_filenames(wtf->config, li->buf, &infile, &outfile);
     827        rv = sphinx_wave2feat_convert_file(wtf, infile, outfile);
     828        ckd_free(infile);
     829        ckd_free(outfile);
     830        if (rv != 0) {
     831            lineiter_free(li);
     832            fclose(ctlfh);
     833            return rv;
     834        }
     835    }
     836    return 0;
     837}
     838
     839int
     840main(int argc, char *argv[])
     841{
     842    sphinx_wave2feat_t *wtf;
     843    cmd_ln_t *config;
     844    int rv;
     845
     846    /* Initialize config. */
     847    if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL)
     848        return 2;
     849
     850    /* Parse an argument file if there's one in there. */
     851    if (cmd_ln_str_r(config, "-argfile"))
     852        config = cmd_ln_parse_file_r(config, defn,
     853                                     cmd_ln_str_r(config, "-argfile"), FALSE);
     854    if (config == NULL) {
     855        E_ERROR("Command line parsing failed\n");
     856        return 1;
     857    }
     858    if ((wtf = sphinx_wave2feat_init(config)) == NULL) {
     859        E_ERROR("Failed to initialize wave2feat object\n");
     860        return 1;
     861    }
     862
     863    /* If there's a control file run through it, otherwise we will do
     864     * a single file (which is what run_control_file will do
     865     * internally too) */
     866    if (cmd_ln_str_r(config, "-c"))
     867        rv = run_control_file(wtf, cmd_ln_str_r(config, "-c"));
     868    else
     869        rv = sphinx_wave2feat_convert_file(wtf, cmd_ln_str_r(config, "-i"),
     870                                           cmd_ln_str_r(config, "-o"));
     871
     872    sphinx_wave2feat_free(wtf);
     873    return rv;
     874}
  • src/tests/libs/sphinx/sphinx_fe/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx sphinx_fe ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest sphinx_fe : sphinx_fe.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/sphinx_fe/cmd_ln_defn.h

     
     1/* ====================================================================
     2 * Copyright (c) 1998-2000 Carnegie Mellon University.  All rights
     3 * reserved.
     4 *
     5 * Redistribution and use in source and binary forms, with or without
     6 * modification, are permitted provided that the following conditions
     7 * are met:
     8 *
     9 * 1. Redistributions of source code must retain the above copyright
     10 *    notice, this list of conditions and the following disclaimer.
     11 *
     12 * 2. Redistributions in binary form must reproduce the above copyright
     13 *    notice, this list of conditions and the following disclaimer in
     14 *    the documentation and/or other materials provided with the
     15 *    distribution.
     16 *
     17 * This work was supported in part by funding from the Defense Advanced
     18 * Research Projects Agency and the National Science Foundation of the
     19 * United States of America, and the CMU Sphinx Speech Consortium.
     20 *
     21 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     22 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     25 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32 *
     33 * ====================================================================
     34 *
     35 */
     36/*********************************************************************
     37 *
     38 * File: cmd_ln_defn.h
     39 *
     40 * Description:
     41 *      Command line argument definition
     42 *
     43 * Author:
     44 *     
     45 *********************************************************************/
     46
     47#ifndef CMD_LN_DEFN_H
     48#define CMD_LN_DEFN_H
     49
     50#include "cmd_ln.h"
     51#include "fe.h"
     52
     53const char helpstr[] =
     54  "Description: \n\
     55Extract acoustic features form from audio file.\n\
     56\n\
     57The main parameters that affect the final output, with typical values, are:\n\
     58\n\
     59samprate, typically 8000, 11025, or 16000\n\
     60lowerf, 130, 200, 130, for the respective sampling rates above\n\
     61upperf, 3700, 5200, 6800, for the respective sampling rates above\n\
     62nfilt, 31, 37, 40, for the respective sampling rates above\n\
     63nfft, 256 or 512\n\
     64format, raw or nist or mswav\n\
     65\"";
     66
     67const char examplestr[] =
     68  "Example: \n\
     69This example creates a cepstral file named \"output.mfc\" from an input audio file named \"input.raw\", which is a raw audio file (no header information), which was originally sampled at 16kHz. \n\
     70\n\
     71sphinx_fe -i  input.raw \n\
     72        -o   output.mfc \n\
     73        -input_endian little \n\
     74        -samprate  16000 \n\
     75        -lowerf    130 \n\
     76        -upperf    6800 \n\
     77        -nfilt     40 \n\
     78        -nfft      512";
     79
     80static arg_t defn[] = {
     81  { "-help",
     82    ARG_BOOLEAN,
     83    "no",
     84    "Shows the usage of the tool"},
     85 
     86  { "-example",
     87    ARG_BOOLEAN,
     88    "no",
     89    "Shows example of how to use the tool"},
     90
     91  waveform_to_cepstral_command_line_macro(),
     92
     93  { "-argfile",
     94    ARG_STRING,
     95    NULL,
     96    "Argument file (e.g. feat.params from an acoustic model) to read parameters from.  This will override anything set in other command line arguments." },
     97 
     98  { "-i",
     99    ARG_STRING,
     100    NULL,
     101    "Single audio input file" },
     102 
     103  { "-o",
     104    ARG_STRING,
     105    NULL,
     106    "Single cepstral output file" },
     107 
     108  { "-c",
     109    ARG_STRING,
     110    NULL,
     111    "Control file for batch processing" },
     112 
     113  { "-nskip",
     114    ARG_INT32,
     115    "0",
     116    "If a control file was specified, the number of utterances to skip at the head of the file" },
     117 
     118  { "-runlen",
     119    ARG_INT32,
     120    "-1",
     121    "If a control file was specified, the number of utterances to process, or -1 for all" },
     122
     123  { "-part",
     124    ARG_INT32,
     125    "0",
     126    "Index of the part to run (supersedes -nskip and -runlen if non-zero)" },
     127 
     128  { "-npart",
     129    ARG_INT32,
     130    "0",
     131    "Number of parts to run in (supersedes -nskip and -runlen if non-zero)" },
     132 
     133  { "-di",
     134    ARG_STRING,
     135    NULL,
     136    "Input directory, input file names are relative to this, if defined" },
     137 
     138  { "-ei",
     139    ARG_STRING,
     140    NULL,
     141    "Input extension to be applied to all input files" },
     142 
     143  { "-do",
     144    ARG_STRING,
     145    NULL,
     146    "Output directory, output files are relative to this" },
     147 
     148  { "-eo",
     149    ARG_STRING,
     150    NULL,
     151    "Output extension to be applied to all output files" },
     152 
     153  { "-build_outdirs",
     154    ARG_BOOLEAN,
     155    "yes",
     156    "Create missing subdirectories in output directory" },
     157
     158  { "-nist",
     159    ARG_BOOLEAN,
     160    "no",
     161    "Defines input format as NIST sphere" },
     162 
     163  { "-raw",
     164    ARG_BOOLEAN,
     165    "no",
     166    "Defines input format as raw binary data" },
     167 
     168  { "-mswav",
     169    ARG_BOOLEAN,
     170    "no",
     171    "Defines input format as Microsoft Wav (RIFF)" },
     172 
     173  { "-nchans",
     174    ARG_INT32,
     175    "1",
     176    "Number of channels of data (interlaced samples assumed)" },
     177 
     178  { "-whichchan",
     179    ARG_INT32,
     180    "1",
     181    "Channel to process" },
     182 
     183  { "-ofmt",
     184    ARG_STRING,
     185    "sphinx",
     186    "Format of output files - one of sphinx, htk, text." },
     187 
     188  { "-mach_endian",
     189    ARG_STRING,
     190#ifdef WORDS_BIGENDIAN
     191    "big",
     192#else
     193    "little",
     194#endif
     195    "Endianness of machine, big or little" },
     196 
     197  { "-blocksize",
     198    ARG_INT32,
     199    "2048",
     200    "Number of samples to read at a time." },
     201
     202  { "-spec2cep",
     203    ARG_BOOLEAN,
     204    "no",
     205    "Input is log spectral files, output is cepstral files" },
     206
     207  { "-cep2spec",
     208    ARG_BOOLEAN,
     209    "no",
     210    "Input is cepstral files, output is log spectral files" },
     211
     212  { NULL, 0, NULL, NULL }
     213};
     214
     215   
     216#define CMD_LN_DEFN_H
     217
     218#endif /* CMD_LN_DEFN_H */
     219
    0220
     221/*
     222 * Log record.  Maintained by RCS.
     223 *
     224 * $Log: cmd_ln_defn.h,v $
     225 * Revision 1.7  2006/02/25 00:53:48  egouvea
     226 * Added the flag "-seed". If dither is being used and the seed is less
     227 * than zero, the random number generator is initialized with time(). If
     228 * it is at least zero, it's initialized with the provided seed. This way
     229 * we have the benefit of having dither, and the benefit of being
     230 * repeatable.
     231 *
     232 * This is consistent with what sphinx3 does. Well, almost. The random
     233 * number generator is still what the compiler provides.
     234 *
     235 * Also, moved fe_init_params to fe_interface.c, so one can initialize a
     236 * variable of type param_t with meaningful values.
     237 *
     238 * Revision 1.6  2006/02/17 00:31:34  egouvea
     239 * Removed switch -melwarp. Changed the default for window length to
     240 * 0.025625 from 0.256 (so that a window at 16kHz sampling rate has
     241 * exactly 410 samples). Cleaned up include's. Replaced some E_FATAL()
     242 * with E_WARN() and return.
     243 *
     244 * Revision 1.5  2006/02/16 00:18:26  egouvea
     245 * Implemented flexible warping function. The user can specify at run
     246 * time which of several shapes they want to use. Currently implemented
     247 * are an affine function (y = ax + b), an inverse linear (y = a/x) and a
     248 * piecewise linear (y = ax, up to a frequency F, and then it "breaks" so
     249 * Nyquist frequency matches in both scales.
     250 *
     251 * Added two switches, -warp_type and -warp_params. The first specifies
     252 * the type, which valid values:
     253 *
     254 * -inverse or inverse_linear
     255 * -linear or affine
     256 * -piecewise or piecewise_linear
     257 *
     258 * The inverse_linear is the same as implemented by EHT. The -mel_warp
     259 * switch was kept for compatibility (maybe remove it in the
     260 * future?). The code is compatible with EHT's changes: cepstra created
     261 * from code after his changes should be the same as now. Scripts that
     262 * worked with his changes should work now without changes. Tested a few
     263 * cases, same results.
     264 *
     265 * Revision 1.4  2006/02/14 20:56:54  eht
     266 * Implement an argument -melwarp that changes the standard mel-scale
     267 * equation from:
     268 *      M(f) = 2595 * log10( 1 + f/700 )
     269 * to:
     270 *      M(f,w) = 2595 * log10( 1 + f/(700*w))
     271 *
     272 * So, 1.0 means no warp,  w > 1.0 means linear compression w < 1.0 means
     273 * linear expansion.
     274 *
     275 * Implement argument -nskip and -runlen arguments so that a subset of the
     276 * utterances in the control file can be executed.  Allows a simple
     277 * distribution of wave2feat processing over N processors.
     278 *
     279 * Revision 1.3  2005/05/19 21:21:55  egouvea
     280 * Bug #1176394: example bug
     281 *
     282 * Revision 1.2  2004/11/23 04:14:06  egouvea
     283 * Fixed bug in cmd_ln.c in which a wrong boolean argument led into an
     284 * infinite loop, and fixed the help and example strings, getting rid of
     285 * spaces, so that the appearance is better.
     286 *
     287 * Revision 1.1  2004/09/09 17:59:30  egouvea
     288 * Adding missing files to wave2feat
     289 *
     290 *
     291 *
  • src/tests/libs/sphinx/test_ad/test_ad_read.c

    + */
     
     1/* -*- c-basic-offset: 4 -*- */
     2#ifdef HAVE_CONFIG
     3#include <config.h>
     4#endif
     5
     6#include "ad.h"
     7#include "cont_ad.h"
     8#include "byteorder.h"
     9#include "test_macros.h"
     10
     11#include <stdio.h>
     12
     13#ifndef WORDS_BIGENDIAN
     14#define WORDS_BIGENDIAN 0
     15#endif
     16
     17#ifndef TESTDATADIR
     18#define TESTDATADIR "../regression"
     19#endif
     20
     21FILE *infp;
     22
     23static int32
     24file_ad_read(ad_rec_t * r, int16 * buf, int32 max)
     25{
     26    int32 i, k;
     27
     28    k = fread(buf, sizeof(int16), max, infp);
     29    if (WORDS_BIGENDIAN) {
     30        for (i = 0; i < k; i++) {
     31        SWAP_INT16(&buf[i]);
     32        }
     33    }
     34
     35    return ((k > 0) ? k : -1);
     36}
     37
     38int
     39main(int argc, char *argv[])
     40{
     41    cont_ad_t *cont;
     42    ad_rec_t ad;
     43    int16 buf[512];
     44    int listening;
     45
     46    ad.sps = 16000;
     47    ad.bps = 2;
     48
     49    TEST_ASSERT(infp = fopen(TESTDATADIR "/chan3.raw", "rb"));
     50    TEST_ASSERT(cont = cont_ad_init(&ad, file_ad_read));
     51
     52    printf("Calibrating ...");
     53    fflush(stdout);
     54    if (cont_ad_calib(cont) < 0)
     55        printf(" failed; file too short?\n");
     56    else
     57        printf(" done after %ld samples\n", ftell(infp) / 2);
     58    rewind(infp);
     59
     60    listening = FALSE;
     61    while (1) {
     62    int k = cont_ad_read(cont, buf, 512);
     63    /* End of file. */
     64    if (k < 0) {
     65        if (listening) {
     66        printf("End of file at %.3f seconds\n",
     67               (double)(cont->read_ts - k) / 16000);
     68        }
     69        break;
     70    }
     71
     72    if (cont->state == CONT_AD_STATE_SIL) {
     73        /* Has there been enough silence to cut the utterance? */
     74        if (listening && cont->seglen > 8000) {
     75        printf("End of utterance at %.3f seconds\n",
     76               (double)(cont->read_ts - k - cont->seglen) / 16000);
     77        listening = FALSE;
     78        }
     79    }
     80    else {
     81        if (!listening) {
     82        printf("Start of utterance at %.3f seconds\n",
     83               (double)(cont->read_ts - k) / 16000);
     84        listening = TRUE;
     85        }
     86    }
     87    }
     88
     89    cont_ad_close(cont);
     90    fclose(infp);
     91    return 0;
     92}
  • src/tests/libs/sphinx/test_ad/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_ad ;
     2
     3UseLibraryHeaders sphinx ;
     4UseLibraryHeaders sphinx/libsphinxbase ;
     5
     6SimpleTest test_ad_copy : test_ad_copy.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a  ;
     7SimpleTest test_ad_read : test_ad_read.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a  ;
     8
     9
  • src/tests/libs/sphinx/test_ad/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define EPSILON 0.01
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_ad/test_ad_copy.c

     
     1/* -*- c-basic-offset: 4 -*- */
     2#ifdef HAVE_CONFIG
     3#include <config.h>
     4#endif
     5
     6#include "ad.h"
     7#include "cont_ad.h"
     8#include "ckd_alloc.h"
     9#include "byteorder.h"
     10#include "test_macros.h"
     11
     12#include <stdio.h>
     13#include <string.h>
     14
     15#ifndef WORDS_BIGENDIAN
     16#define WORDS_BIGENDIAN 0
     17#endif
     18
     19#ifndef TESTDATADIR
     20#define TESTDATADIR "../regression"
     21#endif
     22
     23int
     24main(int argc, char *argv[])
     25{
     26    cont_ad_t *cont;
     27    FILE *infp;
     28    int16 buf[512];
     29    int listening;
     30    int k, n_calib_samp;
     31    int16 *calib, *cptr;
     32
     33    TEST_ASSERT(infp = fopen(TESTDATADIR "/chan3.raw", "rb"));
     34    TEST_ASSERT(cont = cont_ad_init(NULL, NULL));
     35
     36    n_calib_samp = cont_ad_calib_size(cont);
     37    calib = ckd_malloc(n_calib_samp * 2);
     38    printf("Reading %d calibration samples\n", n_calib_samp);
     39    TEST_ASSERT(fread(calib, 2, n_calib_samp, infp) == n_calib_samp);
     40    printf("Calibrating...\n");
     41    TEST_EQUAL(0, cont_ad_calib_loop(cont, calib, n_calib_samp));
     42    printf("Calibrated!\n");
     43
     44    listening = FALSE;
     45    cptr = calib;
     46    while (1) {
     47    /* Use up the calibration samples first. */
     48    if (n_calib_samp) {
     49        k = n_calib_samp;
     50        if (k > 512)
     51        k = 512;
     52        memcpy(buf, cptr, 512 * 2);
     53        cptr += k;
     54        n_calib_samp -= k;
     55        if (k < 512)
     56        k = fread(buf + k, 2, 512-k, infp);
     57    }
     58    else {
     59        k = fread(buf, 2, 512, infp);
     60    }
     61
     62    /* End of file. */
     63    if (k < 256) { /* FIXME: It should do something useful with fewer samples. */
     64        if (listening) {
     65        printf("End of file at %.3f seconds\n",
     66               (double)(cont->read_ts - k) / 16000);
     67        }
     68        break;
     69    }
     70
     71    k = cont_ad_read(cont, buf, k);
     72
     73    if (cont->state == CONT_AD_STATE_SIL) {
     74        /* Has there been enough silence to cut the utterance? */
     75        if (listening && cont->seglen > 8000) {
     76        printf("End of utterance at %.3f seconds\n",
     77               (double)(cont->read_ts - k - cont->seglen) / 16000);
     78        listening = FALSE;
     79        }
     80    }
     81    else {
     82        if (!listening) {
     83        printf("Start of utterance at %.3f seconds\n",
     84               (double)(cont->read_ts - k) / 16000);
     85        listening = TRUE;
     86        }
     87    }
     88    }
     89
     90    ckd_free(calib);
     91    cont_ad_close(cont);
     92    fclose(infp);
     93    return 0;
     94}
  • src/tests/libs/sphinx/testfuncs.sh

     
     1# Utility functions and parameters for regression tests
     2
     3# Predefined directories you may need
     4# Stupid broken CMU Facilities autoconf doesn't do @abs_top_srcdir@
     5builddir=../"@top_builddir@"
     6sourcedir=../"@top_srcdir@"
     7tests=$sourcedir/test
     8
     9# Automatically report failures on exit
     10failures=""
     11trap "report_failures" 0
     12
     13run_program() {
     14    program="$1"
     15    shift
     16    $builddir/libtool --mode=execute "$builddir/src/$program/$program" $@
     17}
     18
     19pass() {
     20    title="$1"
     21    echo "$title PASSED"
     22}
     23
     24fail() {
     25    title="$1"
     26    echo "$title FAILED"
     27    failures="$failures,$title"
     28}
     29
     30assert() {
     31    title="$1"
     32    shift
     33    if $@ >/dev/null 2>&1; then
     34    pass "$title"
     35    else
     36    fail "$title"
     37    fi
     38}
     39
     40compare_table() {
     41    title="$1"
     42    shift
     43    if perl "$tests/compare_table.pl" $@ | grep SUCCESS >/dev/null 2>&1; then
     44    pass "$title"
     45    else
     46    fail "$title"
     47    fi
     48}
     49
     50report_failures() {
     51    if test x"$failures" = x; then
     52    echo "All sub-tests passed"
     53    exit 0
     54    else
     55    echo "Sub-tests failed:$failures" | sed -e 's/,/\n/g'
     56    exit 1
     57    fi
     58}
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc_abort.c

    Property changes on: src/tests/libs/sphinx/testfuncs.sh
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#include <stdio.h>
     2
     3#include <ckd_alloc.h>
     4
     5#include "test_macros.h"
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    int *alloc1;
     11    int bad_alloc_did_not_fail = FALSE;
     12
     13    ckd_set_jump(NULL, TRUE);
     14    /* Guaranteed to fail, we hope!. */
     15    alloc1 = ckd_calloc(-1,-1);
     16    TEST_ASSERT(bad_alloc_did_not_fail);
     17
     18    return 0;
     19}
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc_fail.c

     
     1#include <stdio.h>
     2
     3#include <ckd_alloc.h>
     4
     5#include "test_macros.h"
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    int *alloc1;
     11    int bad_alloc_did_not_fail = FALSE;
     12
     13    ckd_set_jump(NULL, FALSE);
     14    /* Guaranteed to fail, we hope!. */
     15    alloc1 = ckd_calloc(-1,-1);
     16    TEST_ASSERT(bad_alloc_did_not_fail);
     17
     18    return 0;
     19}
  • src/tests/libs/sphinx/test_alloc/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_alloc ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libssphinxbase ;
     7
     8SimpleTest test_ckd_alloc : test_ckd_alloc.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_ckd_alloc_abort : test_ckd_alloc_abort.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_ckd_alloc_catch : test_ckd_alloc_catch.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     11SimpleTest test_ckd_alloc_fail : test_ckd_alloc_fail.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     12SimpleTest test_listelem_alloc : test_listelem_alloc.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_alloc/test_listelem_alloc.c

     
     1#include <stdio.h>
     2#include <string.h>
     3
     4#include <listelem_alloc.h>
     5
     6#include "test_macros.h"
     7
     8struct bogus {
     9    char const *str;
     10    long foobie;
     11};
     12
     13int
     14main(int argc, char *argv[])
     15{
     16    listelem_alloc_t *le;
     17    struct bogus *bogus1, *bogus2;
     18    int i;
     19
     20    TEST_ASSERT(le = listelem_alloc_init(sizeof(struct bogus)));
     21    bogus1 = listelem_malloc(le);
     22    bogus1->str = "hello";
     23    bogus1->foobie = 42;
     24    bogus2 = listelem_malloc(le);
     25    bogus2->str = "goodbye";
     26    bogus2->foobie = 69;
     27    TEST_EQUAL(bogus1->foobie, 42);
     28    TEST_EQUAL(0, strcmp(bogus1->str, "hello"));
     29    listelem_free(le, bogus1);
     30    listelem_free(le, bogus2);
     31    listelem_alloc_free(le);
     32
     33    TEST_ASSERT(le = listelem_alloc_init(sizeof(struct bogus)));
     34    listelem_stats(le);
     35    for (i = 0; i < 60; ++i)
     36        bogus1 = listelem_malloc(le);
     37    listelem_stats(le);
     38    listelem_alloc_free(le);
     39
     40    return 0;
     41}
  • src/tests/libs/sphinx/test_alloc/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define EPSILON 0.01
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc_abort.sh

     
     1#!/bin/sh
     2
     3. ../testfuncs.sh
     4
     5ulimit -c 0
     6if ./test_ckd_alloc_abort; then
     7    fail expected_failure
     8else
     9    pass expected_failure
     10fi
     11
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc.c

    Property changes on: src/tests/libs/sphinx/test_alloc/test_ckd_alloc_abort.sh
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#include <stdio.h>
     2
     3#include <ckd_alloc.h>
     4
     5#include "test_macros.h"
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    int *alloc1;
     11    int **alloc2;
     12    int ***alloc3;
     13    int i;
     14
     15    TEST_ASSERT(alloc1 = ckd_calloc(3*3*3, sizeof(*alloc1)));
     16    TEST_ASSERT(alloc2 = ckd_calloc_2d(3, 3, sizeof(**alloc2)));
     17    TEST_ASSERT(alloc3 = ckd_calloc_3d(3, 3, 3, sizeof(***alloc3)));
     18
     19    for (i = 0; i < 27; ++i) {
     20        TEST_EQUAL(alloc1[i], 0);
     21        alloc1[i] = i + 1;
     22    }
     23    for (i = 0; i < 27; ++i)
     24        TEST_EQUAL(alloc1[i], i+1);
     25
     26    for (i = 0; i < 3; ++i) {
     27        int j;
     28        for (j = 0; j < 3; ++j) {
     29            TEST_EQUAL(alloc2[i][j], 0);
     30            alloc2[i][j] = i * 3 + j + 1;
     31        }
     32    }
     33    /* Verify that row-major ordering is in use. */
     34    for (i = 0; i < 9; ++i) {
     35        TEST_EQUAL(alloc2[0][i], i+1);
     36        TEST_EQUAL(alloc2[0][i], alloc1[i]);
     37    }
     38    for (i = 0; i < 3; ++i) {
     39        int j;
     40        for (j = 0; j < 3; ++j) {
     41            TEST_EQUAL(alloc2[i][j], i * 3 + j + 1);
     42        }
     43    }
     44    /* Now test alloc_ptr. */
     45    ckd_free_2d(alloc2);
     46    alloc2 = ckd_alloc_2d_ptr(3, 3, alloc1, sizeof(*alloc1));
     47    for (i = 0; i < 9; ++i) {
     48        TEST_EQUAL(alloc2[0][i], i+1);
     49        TEST_EQUAL(alloc2[0][i], alloc1[i]);
     50    }
     51    for (i = 0; i < 3; ++i) {
     52        int j;
     53        for (j = 0; j < 3; ++j) {
     54            TEST_EQUAL(alloc2[i][j], i * 3 + j + 1);
     55        }
     56    }
     57    ckd_free_2d_ptr(alloc2);
     58
     59    for (i = 0; i < 3; ++i) {
     60        int j;
     61        for (j = 0; j < 3; ++j) {
     62            int k;
     63            for (k = 0; k < 3; ++k) {
     64                TEST_EQUAL(alloc3[i][j][k], 0);
     65                alloc3[i][j][k] = i * 3 * 3 + j * 3 + k + 1;
     66            }
     67        }
     68    }
     69    /* Verify that row-major ordering is in use. */
     70    for (i = 0; i < 27; ++i) {
     71        TEST_EQUAL(alloc3[0][0][i], i+1);
     72        TEST_EQUAL(alloc3[0][0][i], alloc1[i]);
     73    }
     74    for (i = 0; i < 3; ++i) {
     75        int j;
     76        for (j = 0; j < 3; ++j) {
     77            int k;
     78            for (k = 0; k < 3; ++k) {
     79                TEST_EQUAL(alloc3[i][j][k], i * 3 * 3 + j * 3 + k + 1);
     80            }
     81        }
     82    }
     83    /* Now test alloc_ptr. */
     84    ckd_free_3d(alloc3);
     85    alloc3 = ckd_alloc_3d_ptr(3, 3, 3, alloc1, sizeof(*alloc1));
     86    for (i = 0; i < 27; ++i) {
     87        TEST_EQUAL(alloc3[0][0][i], i+1);
     88        TEST_EQUAL(alloc3[0][0][i], alloc1[i]);
     89    }
     90    for (i = 0; i < 3; ++i) {
     91        int j;
     92        for (j = 0; j < 3; ++j) {
     93            int k;
     94            for (k = 0; k < 3; ++k) {
     95                TEST_EQUAL(alloc3[i][j][k], i * 3 * 3 + j * 3 + k + 1);
     96            }
     97        }
     98    }
     99    ckd_free_3d_ptr(alloc3);
     100    ckd_free(alloc1);
     101
     102    return 0;
     103}
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc_catch.c

     
     1#include <stdio.h>
     2
     3#include <ckd_alloc.h>
     4
     5#include "test_macros.h"
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    int *alloc1;
     11    jmp_buf env;
     12
     13    ckd_set_jump(&env, FALSE);
     14    if (setjmp(env)) {
     15        printf("Successfully caught bad allocation!\n");
     16    }
     17    else {
     18        int failed_to_catch_bad_alloc = FALSE;
     19
     20        /* Guaranteed to fail, we hope!. */
     21        alloc1 = ckd_calloc(-1,-1);
     22        TEST_ASSERT(failed_to_catch_bad_alloc);
     23    }
     24
     25    return 0;
     26}
  • src/tests/libs/sphinx/test_alloc/test_ckd_alloc_fail.sh

     
     1#!/bin/sh
     2
     3. ../testfuncs.sh
     4
     5./test_ckd_alloc_fail
     6if [ $? = 255 ]; then
     7    pass expected_failure
     8else
     9    fail expected_failure
     10fi
     11
  • src/tests/libs/sphinx/test_fe/Jamfile

    Property changes on: src/tests/libs/sphinx/test_alloc/test_ckd_alloc_fail.sh
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_fe ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_fe : test_fe.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_pitch : test_pitch.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_fe/test_pitch.c

     
     1#include <stdio.h>
     2
     3#include "yin.h"
     4#include "ckd_alloc.h"
     5
     6#include "test_macros.h"
     7
     8#ifndef TESTDATADIR
     9#define TESTDATADIR "../regression"
     10#endif
     11
     12int
     13main(int argc, char *argv[])
     14{
     15    /* This is 11025Hz data (yikes) */
     16    static const int frame_shift = 110, frame_size = 265;
     17    FILE *raw;
     18    yin_t *pe;
     19    int16 *buf;
     20    size_t nsamp, start;
     21    uint16 period, bestdiff;
     22    int nfr;
     23
     24    /* To make life easier, read the whole thing. */
     25    TEST_ASSERT(raw = fopen(TESTDATADIR "/chan3.raw", "rb"));
     26    fseek(raw, 0, SEEK_END);
     27    nsamp = ftell(raw) / 2;
     28    buf = ckd_calloc(nsamp, 2);
     29    fseek(raw, 0, SEEK_SET);
     30    TEST_EQUAL(nsamp, fread(buf, 2, nsamp, raw));
     31    fclose(raw);
     32
     33    TEST_ASSERT(pe = yin_init(frame_size, 0.1, 0.2, 2));
     34    yin_start(pe);
     35    nfr = 0;
     36    for (start = 0; start + frame_size < nsamp; start += frame_shift) {
     37        yin_write(pe, buf + start);
     38        if (yin_read(pe, &period, &bestdiff)) {
     39            if (bestdiff < 0.2 * 32768)
     40                printf("%d ", period ? 11025/period : 0);
     41            else
     42                printf("0 ");
     43            ++nfr;
     44        }
     45    }
     46    yin_end(pe);
     47    while (yin_read(pe, &period, &bestdiff)) {
     48        if (bestdiff < 0.2 * 32768)
     49            printf("%d ", period ? 11025/period : 0);
     50        else
     51            printf("0 ");
     52        ++nfr;
     53    }
     54    printf("\n");
     55    yin_free(pe);
     56    ckd_free(buf);
     57
     58    return 0;
     59}
  • src/tests/libs/sphinx/test_fe/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define EPSILON 0.01
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_fe/test_fe.c

     
     1#include <stdio.h>
     2#include <string.h>
     3
     4#include "fe.h"
     5#include "cmd_ln.h"
     6#include "ckd_alloc.h"
     7
     8#include "test_macros.h"
     9
     10#ifndef TESTDATADIR
     11#define TESTDATADIR "../regression"
     12#endif
     13
     14int
     15main(int argc, char *argv[])
     16{
     17    static const arg_t fe_args[] = {
     18        waveform_to_cepstral_command_line_macro(),
     19        { NULL, 0, NULL, NULL }
     20    };
     21    FILE *raw;
     22    cmd_ln_t *config;
     23    fe_t *fe;
     24    int16 buf[1024];
     25    int16 const *inptr;
     26    int32 frame_shift, frame_size;
     27    mfcc_t **cepbuf1, **cepbuf2, **cptr;
     28    int32 nfr, i;
     29    size_t nsamp;
     30
     31    TEST_ASSERT(config = cmd_ln_parse_r(NULL, fe_args, argc, argv, FALSE));
     32    TEST_ASSERT(fe = fe_init_auto_r(config));
     33
     34    TEST_EQUAL(fe_get_output_size(fe), DEFAULT_NUM_CEPSTRA);
     35
     36    fe_get_input_size(fe, &frame_shift, &frame_size);
     37    TEST_EQUAL(frame_shift, DEFAULT_FRAME_SHIFT);
     38    TEST_EQUAL(frame_size, (int)(DEFAULT_WINDOW_LENGTH*DEFAULT_SAMPLING_RATE));
     39
     40    TEST_ASSERT(raw = fopen(TESTDATADIR "/chan3.raw", "rb"));
     41
     42    TEST_EQUAL(0, fe_start_utt(fe));
     43    TEST_EQUAL(1024, fread(buf, sizeof(int16), 1024, raw));
     44
     45    nsamp = 1024;
     46    TEST_ASSERT(fe_process_frames(fe, NULL, &nsamp, NULL, &nfr) >= 0);
     47    TEST_EQUAL(1024, nsamp);
     48    TEST_EQUAL(4, nfr);
     49
     50    cepbuf1 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf1));
     51    inptr = &buf[0];
     52    nfr = 1;
     53
     54    printf("frame_size %d frame_shift %d\n", frame_size, frame_shift);
     55    /* Process the first frame. */
     56    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[0], &nfr) >= 0);
     57    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, nfr);
     58    TEST_EQUAL(nfr, 1);
     59
     60    /* Note that this next one won't actually consume any frames
     61     * of input, because it already got sufficient overflow
     62     * samples last time around.  This is implementation-dependent
     63     * so we shouldn't actually test for it. */
     64    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[1], &nfr) >= 0);
     65    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, nfr);
     66    TEST_EQUAL(nfr, 1);
     67
     68    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[2], &nfr) >= 0);
     69    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, nfr);
     70    TEST_EQUAL(nfr, 1);
     71
     72    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, &cepbuf1[3], &nfr) >= 0);
     73    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, nfr);
     74    TEST_EQUAL(nfr, 1);
     75
     76    TEST_ASSERT(fe_end_utt(fe, cepbuf1[4], &nfr) >= 0);
     77    printf("nfr %d\n", nfr);
     78    TEST_EQUAL(nfr, 1);
     79
     80    /* What we *should* test is that the output we get by
     81     * processing one frame at a time is exactly the same as what
     82     * we get from doing them all at once.  So let's do that */
     83    cepbuf2 = ckd_calloc_2d(5, DEFAULT_NUM_CEPSTRA, sizeof(**cepbuf2));
     84    inptr = &buf[0];
     85    nfr = 5;
     86    nsamp = 1024;
     87    TEST_EQUAL(0, fe_start_utt(fe));
     88    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cepbuf2, &nfr) >= 0);
     89    printf("nfr %d\n", nfr);
     90    TEST_EQUAL(nfr, 4);
     91    nfr = 1;
     92    TEST_ASSERT(fe_end_utt(fe, cepbuf2[4], &nfr) >= 0);
     93    printf("nfr %d\n", nfr);
     94    TEST_EQUAL(nfr, 1);
     95
     96    for (i = 0; i < 5; ++i) {
     97        int j;
     98        printf("%d: ", i);
     99        for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
     100            printf("%.2f,%.2f ",
     101                   MFCC2FLOAT(cepbuf1[i][j]),
     102                   MFCC2FLOAT(cepbuf2[i][j]));
     103            TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
     104        }
     105        printf("\n");
     106    }
     107
     108    /* Now, also test to make sure that even if we feed data in
     109     * little tiny bits we can still make things work. */
     110    memset(cepbuf2[0], 0, 5 * DEFAULT_NUM_CEPSTRA * sizeof(**cepbuf2));
     111    inptr = &buf[0];
     112    cptr = &cepbuf2[0];
     113    nfr = 5;
     114    i = 5;
     115    nsamp = 256;
     116    TEST_EQUAL(0, fe_start_utt(fe));
     117    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
     118    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, i);
     119    cptr += i;
     120    nfr -= i;
     121    i = nfr;
     122    nsamp = 256;
     123    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
     124    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, i);
     125    cptr += i;
     126    nfr -= i;
     127    i = nfr;
     128    nsamp = 256;
     129    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
     130    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, i);
     131    cptr += i;
     132    nfr -= i;
     133    i = nfr;
     134    nsamp = 256;
     135    TEST_ASSERT(fe_process_frames(fe, &inptr, &nsamp, cptr, &i) >= 0);
     136    printf("inptr %ld nsamp %ld nfr %d\n", inptr - buf, nsamp, i);
     137    cptr += i;
     138    nfr -= i;
     139    TEST_ASSERT(fe_end_utt(fe, *cptr, &nfr) >= 0);
     140    printf("nfr %d\n", nfr);
     141    TEST_EQUAL(nfr, 1);
     142
     143    for (i = 0; i < 5; ++i) {
     144        int j;
     145        printf("%d: ", i);
     146        for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
     147            printf("%.2f,%.2f ",
     148                   MFCC2FLOAT(cepbuf1[i][j]),
     149                   MFCC2FLOAT(cepbuf2[i][j]));
     150            TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
     151        }
     152        printf("\n");
     153    }
     154
     155    /* And now, finally, test fe_process_utt() */
     156    inptr = &buf[0];
     157    i = 0;
     158    TEST_EQUAL(0, fe_start_utt(fe));
     159    TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
     160    printf("i %d nfr %d\n", i, nfr);
     161    if (nfr)
     162        memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
     163    ckd_free_2d(cptr);
     164    i += nfr;
     165    inptr += 256;
     166    TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
     167    printf("i %d nfr %d\n", i, nfr);
     168    if (nfr)
     169        memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
     170    ckd_free_2d(cptr);
     171    i += nfr;
     172    inptr += 256;
     173    TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
     174    printf("i %d nfr %d\n", i, nfr);
     175    if (nfr)
     176        memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
     177    ckd_free_2d(cptr);
     178    i += nfr;
     179    inptr += 256;
     180    TEST_ASSERT(fe_process_utt(fe, inptr, 256, &cptr, &nfr) >= 0);
     181    printf("i %d nfr %d\n", i, nfr);
     182    if (nfr)
     183        memcpy(cepbuf2[i], cptr[0], nfr * DEFAULT_NUM_CEPSTRA * sizeof(**cptr));
     184    ckd_free_2d(cptr);
     185    i += nfr;
     186    inptr += 256;
     187    TEST_ASSERT(fe_end_utt(fe, cepbuf2[i], &nfr) >= 0);
     188    printf("i %d nfr %d\n", i, nfr);
     189    TEST_EQUAL(nfr, 1);
     190
     191    for (i = 0; i < 5; ++i) {
     192        int j;
     193        printf("%d: ", i);
     194        for (j = 0; j < DEFAULT_NUM_CEPSTRA; ++j) {
     195            printf("%.2f,%.2f ",
     196                   MFCC2FLOAT(cepbuf1[i][j]),
     197                   MFCC2FLOAT(cepbuf2[i][j]));
     198            TEST_EQUAL_FLOAT(cepbuf1[i][j], cepbuf2[i][j]);
     199        }
     200        printf("\n");
     201    }
     202
     203    ckd_free_2d(cepbuf1);
     204    ckd_free_2d(cepbuf2);
     205    fclose(raw);
     206    fe_free(fe);
     207
     208    return 0;
     209}
  • src/tests/libs/sphinx/test_logmath/test_log_int16.c

     
     1#include <logmath.h>
     2
     3#include "test_macros.h"
     4
     5#define LOG_EPSILON 1500
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    logmath_t *lmath;
     11    int32 rv;
     12
     13    lmath = logmath_init(1.0001, 0, 1);
     14    TEST_ASSERT(lmath);
     15    printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
     16    TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050);
     17    printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
     18    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
     19    printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
     20    printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
     21    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
     22    printf("log(42) = %d\n", logmath_log(lmath, 42));
     23    TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378);
     24    printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
     25    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 42);
     26    printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
     27           logmath_log(lmath, 1e-3),
     28           logmath_log(lmath, 5e-3),
     29           logmath_add(lmath, logmath_log(lmath, 1e-3),
     30               logmath_log(lmath, 5e-3)));
     31    printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
     32           logmath_exp(lmath, logmath_log(lmath, 1e-3)),
     33           logmath_exp(lmath, logmath_log(lmath, 5e-3)),
     34           logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
     35                          logmath_log(lmath, 5e-3))));
     36    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     37                   logmath_log(lmath, 5e-48)),
     38               logmath_log(lmath, 6e-48));
     39    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     40                   logmath_log(lmath, 42)),
     41               logmath_log(lmath, 42));
     42
     43    rv = logmath_write(lmath, "tmp.logadd");
     44    TEST_EQUAL(rv, 0);
     45    logmath_free(lmath);
     46    lmath = logmath_read("tmp.logadd");
     47    TEST_ASSERT(lmath);
     48    printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
     49    TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050);
     50    printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
     51    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
     52    printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
     53    printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
     54    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
     55    printf("log(42) = %d\n", logmath_log(lmath, 42));
     56    TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378);
     57    printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
     58    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 41.99);
     59    printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
     60           logmath_log(lmath, 1e-3),
     61           logmath_log(lmath, 5e-3),
     62           logmath_add(lmath, logmath_log(lmath, 1e-3),
     63               logmath_log(lmath, 5e-3)));
     64    printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
     65           logmath_exp(lmath, logmath_log(lmath, 1e-3)),
     66           logmath_exp(lmath, logmath_log(lmath, 5e-3)),
     67           logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
     68                          logmath_log(lmath, 5e-3))));
     69    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     70                   logmath_log(lmath, 5e-48)),
     71               logmath_log(lmath, 6e-48));
     72    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     73                   logmath_log(lmath, 42)),
     74               logmath_log(lmath, 42));
     75
     76    return 0;
     77}
  • src/tests/libs/sphinx/test_logmath/test_log_int8.c

     
     1#include <logmath.h>
     2
     3#include "test_macros.h"
     4
     5#define LOG_EPSILON 1500
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    logmath_t *lmath;
     11    int32 rv;
     12
     13    lmath = logmath_init(1.003, 0, 1);
     14    TEST_ASSERT(lmath);
     15    printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
     16    TEST_EQUAL_LOG(logmath_log(lmath, 1e-48), -36896);
     17    printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, -36896));
     18    TEST_EQUAL_FLOAT(logmath_exp(lmath, -36896), 1e-48);
     19    printf("log(42) = %d\n", logmath_log(lmath, 42));
     20    TEST_EQUAL_LOG(logmath_log(lmath, 42), 1247);
     21    printf("exp(log(42)) = %f\n",logmath_exp(lmath, 1247));
     22    TEST_EQUAL_FLOAT(logmath_exp(lmath, 1247), 41.9);
     23    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     24                   logmath_log(lmath, 5e-48)),
     25               logmath_log(lmath, 6e-48));
     26    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     27                   logmath_log(lmath, 42)), 1247);
     28
     29    rv = logmath_write(lmath, "tmp.logadd");
     30    TEST_EQUAL(rv, 0);
     31    logmath_free(lmath);
     32    lmath = logmath_read("tmp.logadd");
     33    TEST_ASSERT(lmath);
     34    TEST_EQUAL_LOG(logmath_log(lmath, 1e-48), -36896);
     35    TEST_EQUAL_LOG(logmath_log(lmath, 42), 1247);
     36    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     37                   logmath_log(lmath, 5e-48)),
     38               logmath_log(lmath, 6e-48));
     39    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     40                   logmath_log(lmath, 42)), 1247);
     41    logmath_free(lmath);
     42
     43    return 0;
     44}
  • src/tests/libs/sphinx/test_logmath/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_logmath ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_log_int16 : test_log_int16.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_log_int8 : test_log_int8.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_log_shifted : test_log_shifted.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_logmath/test_log_shifted.c

     
     1#include <logmath.h>
     2
     3#include "test_macros.h"
     4
     5#define LOG_EPSILON 1500
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    logmath_t *lmath;
     11    int32 rv;
     12
     13    lmath = logmath_init(1.0001, 8, 1);
     14    TEST_ASSERT(lmath);
     15    printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
     16    TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -13493);
     17    printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
     18    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
     19    printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
     20    printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
     21    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
     22    printf("log(42) = %d\n", logmath_log(lmath, 42));
     23    TEST_EQUAL_LOG(logmath_log(lmath, 42), 146);
     24    printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
     25    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 41.99);
     26    printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
     27           logmath_log(lmath, 1e-3),
     28           logmath_log(lmath, 5e-3),
     29           logmath_add(lmath, logmath_log(lmath, 1e-3),
     30               logmath_log(lmath, 5e-3)));
     31    printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
     32           logmath_exp(lmath, logmath_log(lmath, 1e-3)),
     33           logmath_exp(lmath, logmath_log(lmath, 5e-3)),
     34           logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
     35                          logmath_log(lmath, 5e-3))));
     36    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     37                   logmath_log(lmath, 5e-48)),
     38               logmath_log(lmath, 6e-48));
     39    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     40                   logmath_log(lmath, 42)),
     41               logmath_log(lmath, 42));
     42
     43    rv = logmath_write(lmath, "tmp.logadd");
     44    TEST_EQUAL(rv, 0);
     45    logmath_free(lmath);
     46    lmath = logmath_read("tmp.logadd");
     47    TEST_ASSERT(lmath);
     48    printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150));
     49    TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -13493);
     50    printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150)));
     51    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150);
     52    printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48));
     53    printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48)));
     54    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48);
     55    printf("log(42) = %d\n", logmath_log(lmath, 42));
     56    TEST_EQUAL_LOG(logmath_log(lmath, 42), 146);
     57    printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42)));
     58    TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 41.99);
     59    printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n",
     60           logmath_log(lmath, 1e-3),
     61           logmath_log(lmath, 5e-3),
     62           logmath_add(lmath, logmath_log(lmath, 1e-3),
     63               logmath_log(lmath, 5e-3)));
     64    printf("log(1e-3 + 5e-3) = %e + %e = %e\n",
     65           logmath_exp(lmath, logmath_log(lmath, 1e-3)),
     66           logmath_exp(lmath, logmath_log(lmath, 5e-3)),
     67           logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3),
     68                          logmath_log(lmath, 5e-3))));
     69    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     70                   logmath_log(lmath, 5e-48)),
     71               logmath_log(lmath, 6e-48));
     72    TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48),
     73                   logmath_log(lmath, 42)),
     74               logmath_log(lmath, 42));
     75
     76    return 0;
     77}
  • src/tests/libs/sphinx/test_logmath/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     5#define EPSILON 0.01
     6#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     7#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     8#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/sphinx_cepview/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx sphinx_cepview ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6
     7SimpleTest sphinx_cepview : main_cepview.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/sphinx_cepview/main_cepview.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 1994-2001 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 *
     37 * HISTORY
     38 *
     39 * circa 1994   P J Moreno at Carnegie Mellon
     40 *      Created.
     41 *
     42 * For history information, please use 'cvs log'
     43 * $Log$
     44 * Revision 1.11  2006/02/24  04:06:43  arthchan2003
     45 * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
     46 *
     47 *
     48 * Revision 1.10  2005/08/18 21:18:09  egouvea
     49 * Added E_INFO displaying information about how many columns are being printed, and how many frames
     50 *
     51 * Revision 1.9.4.2  2005/09/07 23:51:05  arthchan2003
     52 * Fixed keyword expansion problem
     53 *
     54 * Revision 1.9.4.1  2005/07/18 23:21:23  arthchan2003
     55 * Tied command-line arguments with marcos
     56 *
     57 * Revision 1.10  2005/08/18 21:18:09  egouvea
     58 * Added E_INFO displaying information about how many columns are being printed, and how many frames
     59 *
     60 * Revision 1.9  2005/06/22 05:38:45  arthchan2003
     61 * Add
     62 *
     63 * Revision 1.2  2005/03/30 00:43:41  archan
     64 *
     65 * Add $Log$
     66 * Revision 1.11  2006/02/24  04:06:43  arthchan2003
     67 * Merged from branch SPHINX3_5_2_RCI_IRII_BRANCH: Changed commands to macro.  Used E_INFO instead of printf in displaying no. of friends
     68 *
     69 *
     70 */
     71
     72#include <stdio.h>
     73#include <stdlib.h>
     74#include <string.h>
     75#include <strfuncs.h>
     76
     77#ifdef _WIN32
     78#pragma warning (disable: 4996)
     79#endif
     80
     81#ifdef HAVE_CONFIG_H
     82#include <config.h>
     83#endif
     84
     85#include "prim_type.h"
     86#include "cmd_ln.h"
     87#include "ckd_alloc.h"
     88#include "info.h"
     89#include "err.h"
     90#include "bio.h"
     91
     92/* Silvio Moioli: switched to stat_reply that's Windows CE friendly. */
     93#include "pio.h"
     94
     95/** \file main_cepview.c
     96    \brief Main driver of cepview
     97 */
     98#define IO_ERR  (-1)
     99#define IO_SUCCESS  (0)
     100
     101#define SHOW_ALL "-1"
     102
     103/* Default cepstral vector size */
     104#define NUM_COEFF  "13"
     105
     106/* Default display size, i.e., number of coefficients displayed, less
     107 * than the vector size so we display one frame per line.
     108 */
     109#define DISPLAY_SIZE "10"
     110#define STR_MAX_INT "2147483647"
     111
     112static arg_t arg[] = {
     113
     114    {"-logfn",
     115     ARG_STRING,
     116     NULL,
     117     "Log file (default stdout/stderr)"},
     118    {"-i",
     119     ARG_INT32,
     120     NUM_COEFF,
     121     "Number of coefficients in the feature vector."},
     122    {"-d",
     123     ARG_INT32,
     124     DISPLAY_SIZE,
     125     "Number of displayed coefficients."},
     126    {"-header",
     127     ARG_INT32,
     128     "0",
     129     "Whether header is shown."},
     130    {"-describe",
     131     ARG_INT32,
     132     "0",
     133     "Whether description will be shown."},
     134    {"-b",
     135     ARG_INT32,
     136     "0",
     137     "The beginning frame 0-based."},
     138    {"-e",
     139     ARG_INT32,
     140     "2147483647",
     141     "The ending frame."},
     142    {"-f",
     143     ARG_STRING,
     144     NULL,
     145     "Input feature file."},
     146    {NULL, ARG_INT32, NULL, NULL}
     147};
     148
     149int read_cep(char const *file, float ***cep, int *nframes, int numcep);
     150
     151int
     152main(int argc, char *argv[])
     153{
     154    int i, j, offset;
     155    int32 noframe, vsize, dsize, column;
     156    int32 frm_begin, frm_end;
     157    int is_header, is_describe;
     158    float *z, **cep;
     159    char const *cepfile;
     160
     161    print_appl_info(argv[0]);
     162    cmd_ln_appl_enter(argc, argv, "default.arg", arg);
     163
     164    vsize = cmd_ln_int32("-i");
     165    dsize = cmd_ln_int32("-d");
     166    frm_begin = cmd_ln_int32("-b");
     167    frm_end = cmd_ln_int32("-e");
     168    is_header = cmd_ln_int32("-header");
     169    is_describe = cmd_ln_int32("-describe");
     170
     171    if (vsize < 0)
     172        E_FATAL("-i : Input vector size should be larger than 0.\n");
     173    if (dsize < 0)
     174        E_FATAL("-d : Column size should be larger than 0\n");
     175    if (frm_begin < 0)
     176        E_FATAL("-b : Beginning frame should be larger than 0\n");
     177    /* The following condition is redundant
     178     * if (frm_end < 0) E_FATAL("-e : Ending frame should be larger than 0\n");
     179     */
     180    if (frm_begin >= frm_end)
     181        E_FATAL
     182            ("Ending frame (-e) should be larger than beginning frame (-b).\n");
     183
     184    if ((cepfile = cmd_ln_str("-f")) == NULL) {
     185        E_FATAL("Input file was not specified with (-f)\n");
     186    }
     187    if (read_cep(cepfile, &cep, &noframe, vsize) == IO_ERR)
     188        E_FATAL("ERROR opening %s for reading\n", cepfile);
     189
     190    z = cep[0];
     191
     192    offset = 0;
     193    column = (vsize > dsize) ? dsize : vsize;
     194    frm_end = (frm_end > noframe) ? noframe : frm_end;
     195
     196    E_INFO("Displaying %d out of %d columns per frame\n", column, vsize);
     197    E_INFO("Total %d frames\n\n", noframe);
     198
     199    /* This part should be moved to a special library if this file is
     200       longer than 300 lines. */
     201
     202    if (is_header) {
     203        if (is_describe) {
     204            printf("\n%6s", "frame#:");
     205        }
     206
     207        for (j = 0; j < column; ++j) {
     208            printf("%3s%3d%s ", "c[", j, "]");
     209        }
     210        printf("\n");
     211    }
     212
     213    offset += frm_begin * vsize;
     214    for (i = frm_begin; i < frm_end; ++i) {
     215        if (is_describe) {
     216            printf("%6d:", i);
     217        }
     218        for (j = 0; j < column; ++j)
     219            printf("%7.3f ", z[offset + j]);
     220        printf("\n");
     221
     222        offset += vsize;
     223    }
     224    fflush(stdout);
     225    cmd_ln_appl_exit();
     226    ckd_free_2d(cep);
     227
     228    return (IO_SUCCESS);
     229
     230}
     231
     232int
     233read_cep(char const *file, float ***cep, int *numframes, int cepsize)
     234{
     235    FILE *fp;
     236    int n_float;
     237    struct stat statbuf;
     238    int i, n, byterev, sf, ef;
     239    float32 **mfcbuf;
     240
     241    if (stat_retry(file, &statbuf) < 0) {
     242        printf("stat(%s) failed\n", file);
     243        return IO_ERR;
     244    }
     245
     246    if ((fp = fopen(file, "rb")) == NULL) {
     247        printf("fopen(%s, rb) failed\n", file);
     248        return IO_ERR;
     249    }
     250
     251    /* Read #floats in header */
     252    if (fread(&n_float, sizeof(int), 1, fp) != 1) {
     253        fclose(fp);
     254        return IO_ERR;
     255    }
     256
     257    /* Check if n_float matches file size */
     258    byterev = FALSE;
     259    if ((int) (n_float * sizeof(float) + 4) != statbuf.st_size) {
     260        n = n_float;
     261        SWAP_INT32(&n);
     262
     263        if ((int) (n * sizeof(float) + 4) != statbuf.st_size) {
     264            printf
     265                ("Header size field: %d(%08x); filesize: %d(%08x)\n",
     266                 n_float, n_float, (int) statbuf.st_size,
     267                 (int) statbuf.st_size);
     268            fclose(fp);
     269            return IO_ERR;
     270        }
     271
     272        n_float = n;
     273        byterev = TRUE;
     274    }
     275    if (n_float <= 0) {
     276        printf("Header size field: %d\n", n_float);
     277        fclose(fp);
     278        return IO_ERR;
     279    }
     280
     281    /* n = #frames of input */
     282    n = n_float / cepsize;
     283    if (n * cepsize != n_float) {
     284        printf("Header size field: %d; not multiple of %d\n",
     285               n_float, cepsize);
     286        fclose(fp);
     287        return IO_ERR;
     288    }
     289    sf = 0;
     290    ef = n;
     291
     292    mfcbuf = (float **) ckd_calloc_2d(n, cepsize, sizeof(float32));
     293
     294    /* Read mfc data and byteswap if necessary */
     295    n_float = n * cepsize;
     296    if ((int) fread(mfcbuf[0], sizeof(float), n_float, fp) != n_float) {
     297        printf("Error reading mfc data\n");
     298        fclose(fp);
     299        return IO_ERR;
     300    }
     301    if (byterev) {
     302        for (i = 0; i < n_float; i++)
     303            SWAP_FLOAT32(&(mfcbuf[0][i]));
     304    }
     305    fclose(fp);
     306
     307    *numframes = n;
     308    *cep = mfcbuf;
     309    return IO_SUCCESS;
     310}
     311
     312/** Silvio Moioli: Windows CE/Mobile entry point added. */
     313#if defined(_WIN32_WCE)
     314#pragma comment(linker,"/entry:mainWCRTStartup")
     315
     316//Windows Mobile has the Unicode main only
     317int wmain(int32 argc, wchar_t *wargv[]) {
     318    char** argv;
     319    size_t wlen;
     320    size_t len;
     321    int i;
     322
     323    argv = malloc(argc*sizeof(char*));
     324    for (i=0; i<argc; i++){
     325        wlen = lstrlenW(wargv[i]);
     326        len = wcstombs(NULL, wargv[i], wlen);
     327        argv[i] = malloc(len+1);
     328        wcstombs(argv[i], wargv[i], wlen);
     329    }
     330
     331    //assuming ASCII parameters
     332    return main(argc, argv);
     333}
     334#endif
  • src/tests/libs/sphinx/test_cmdln/_test_parse_multiple.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5testname=`basename $0 .test`
     6./cmdln_parse_multiple -a 42 -d 4.5 > $testname.out 2>/dev/null
     7compare_table defaults $testname.out $tests/unit/test_cmdln/$testname.res
     8rm -f $testname.out
  • src/tests/libs/sphinx/test_cmdln/_test_parse_badargs.res

    Property changes on: src/tests/libs/sphinx/test_cmdln/_test_parse_multiple.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1ERROR: Bad argument value for -a: foobar
     2ERROR: cmd_ln_parse_r failed
     3ERROR: cmd_ln_parse failed, forced exit
  • src/tests/libs/sphinx/test_cmdln/cmdln_parse_multiple.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2
     3#include <stdio.h>
     4#include <string.h>
     5
     6#include "cmd_ln.h"
     7#include "ckd_alloc.h"
     8
     9const arg_t defs[] = {
     10    { "-a", ARG_INT32, "42", "This is the first argument." },
     11    { "-b", ARG_STRING, NULL, "This is the second argument." },
     12    { "-c", ARG_BOOLEAN, "no", "This is the third argument." },
     13    { "-d", ARG_FLOAT64, "1e-50", "This is the fourth argument." },
     14    { NULL, 0, NULL, NULL }
     15};
     16
     17static char *orig_argv[] = {
     18    "foobie",
     19    "-a", "69"
     20};
     21static int orig_argc = sizeof(orig_argv) / sizeof(orig_argv[0]);
     22
     23int
     24main(int argc, char *argv[])
     25{
     26    cmd_ln_parse(defs, orig_argc, orig_argv, TRUE);
     27    cmd_ln_parse(defs, argc, argv, FALSE);
     28    printf("%d %s %d %f\n",
     29           cmd_ln_int32("-a"),
     30           cmd_ln_str("-b") ? cmd_ln_str("-b") : "(null)",
     31           cmd_ln_boolean("-c"),
     32           cmd_ln_float64("-d"));
     33    cmd_ln_free();
     34           
     35    return 0;
     36}
  • src/tests/libs/sphinx/test_cmdln/_test_parse_goodargs.res

     
     136 hello 1 4.500000
  • src/tests/libs/sphinx/test_cmdln/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_cmdln ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest cmdln_parse : cmdln_parse.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest cmdln_parse_multiple : cmdln_parse_multiple.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest cmdln_parse_r : cmdln_parse_r.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_cmdln/_test_parse_defaults.res

     
     1-a  42  This is the first argument.
     2-b      This is the second argument.
     3-c  no  This is the third argument.
     4-d  1e-50   This is the fourth argument.
  • src/tests/libs/sphinx/test_cmdln/_test_parse_defaults_r.res

     
     1-a  42  42
     2-b     
     3-c  no  no
     4-d  1e-50   1.000000e-50
     5-a  42  42
     6-b      foobie
     7-c  no  no
     8-d  1e-50   1.000000e-50
  • src/tests/libs/sphinx/test_cmdln/cmdln_parse.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2
     3#include <stdio.h>
     4#include <string.h>
     5
     6#include "cmd_ln.h"
     7#include "ckd_alloc.h"
     8
     9const arg_t defs[] = {
     10    { "-a", ARG_INT32, "42", "This is the first argument." },
     11    { "-b", ARG_STRING, NULL, "This is the second argument." },
     12    { "-c", ARG_BOOLEAN, "no", "This is the third argument." },
     13    { "-d", ARG_FLOAT64, "1e-50", "This is the fourth argument." },
     14    { NULL, 0, NULL, NULL }
     15};
     16
     17int
     18main(int argc, char *argv[])
     19{
     20    cmd_ln_parse(defs, argc, argv, TRUE);
     21    printf("%d %s %d %f\n",
     22           cmd_ln_int32("-a"),
     23           cmd_ln_str("-b") ? cmd_ln_str("-b") : "(null)",
     24           cmd_ln_boolean("-c"),
     25           cmd_ln_float64("-d"));
     26           
     27    cmd_ln_free();
     28
     29    return 0;
     30}
  • src/tests/libs/sphinx/test_cmdln/_test_parse_badargs.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5testname=`basename $0 .test`
     6./cmdln_parse -a foobar > $testname.out 2>&1 && exit 1
     7sed -ne 's,^ERROR:.*line [0-9]*:,ERROR:,p' < $testname.out > $testname.tmp.out \
     8    && mv $testname.tmp.out $testname.out
     9compare_table defaults $testname.out $tests/unit/test_cmdln/$testname.res 100
     10rm -f $testname.out
  • src/tests/libs/sphinx/test_cmdln/cmdln_parse_r.c

    Property changes on: src/tests/libs/sphinx/test_cmdln/_test_parse_badargs.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2
     3#include <stdio.h>
     4#include <string.h>
     5
     6#include "cmd_ln.h"
     7#include "ckd_alloc.h"
     8
     9const arg_t defs[] = {
     10    { "-a", ARG_INT32, "42", "This is the first argument." },
     11    { "-b", ARG_STRING, NULL, "This is the second argument." },
     12    { "-c", ARG_BOOLEAN, "no", "This is the third argument." },
     13    { "-d", ARG_FLOAT64, "1e-50", "This is the fourth argument." },
     14    { NULL, 0, NULL, NULL }
     15};
     16
     17int
     18main(int argc, char *argv[])
     19{
     20    cmd_ln_t *config;
     21
     22    config = cmd_ln_parse_r(NULL, defs, argc, argv, TRUE);
     23    if (config == NULL)
     24        return 1;
     25    printf("%ld %s %d %f\n",
     26           cmd_ln_int32_r(config, "-a"),
     27           cmd_ln_str_r(config, "-b") ? cmd_ln_str_r(config, "-b") : "(null)",
     28           cmd_ln_boolean_r(config, "-c"),
     29           cmd_ln_float64_r(config, "-d"));
     30    cmd_ln_free_r(config);
     31
     32    config = cmd_ln_init(NULL, NULL, FALSE,
     33                         "-b", "foobie", NULL);
     34    if (config == NULL)
     35        return 1;
     36    cmd_ln_free_r(config);
     37
     38    config = cmd_ln_init(NULL, defs, TRUE,
     39                         "-b", "foobie", NULL);
     40    if (config == NULL)
     41        return 1;
     42    printf("%ld %s %d %f\n",
     43           cmd_ln_int32_r(config, "-a"),
     44           cmd_ln_str_r(config, "-b") ? cmd_ln_str_r(config, "-b") : "(null)",
     45           cmd_ln_boolean_r(config, "-c"),
     46           cmd_ln_float64_r(config, "-d"));
     47    cmd_ln_free_r(config);
     48
     49    config = cmd_ln_init(NULL, NULL, FALSE,
     50                         "-b", "foobie", NULL);
     51    if (config == NULL)
     52        return 1;
     53    printf("%s\n",
     54           cmd_ln_str_r(config, "-b") ? cmd_ln_str_r(config, "-b") : "(null)");
     55    cmd_ln_set_str_r(config, "-b", "blatz");
     56    printf("%s\n",
     57           cmd_ln_str_r(config, "-b") ? cmd_ln_str_r(config, "-b") : "(null)");
     58    cmd_ln_free_r(config);
     59           
     60    return 0;
     61}
  • src/tests/libs/sphinx/test_cmdln/_test_parse_goodargs.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5testname=`basename $0 .test`
     6./cmdln_parse -a 36 -b hello -c yes -d 4.5 > $testname.out 2>/dev/null
     7compare_table defaults $testname.out $tests/unit/test_cmdln/$testname.res
     8rm -f $testname.out
  • src/tests/libs/sphinx/test_cmdln/_test_parse_multiple.res

    Property changes on: src/tests/libs/sphinx/test_cmdln/_test_parse_goodargs.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     142 (null) 0 4.500000
  • src/tests/libs/sphinx/test_cmdln/_test_parse_defaults.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5testname=`basename $0 .test`
     6./cmdln_parse > $testname.out 2>&1 || true
     7sed -ne 's,^-,-,p' < $testname.out > $testname.tmp.out \
     8    && mv $testname.tmp.out $testname.out
     9compare_table defaults $testname.out $tests/unit/test_cmdln/$testname.res 100
     10rm -f $testname.out
  • src/tests/libs/sphinx/test_cmdln/_test_parse_defaults_r.test

    Property changes on: src/tests/libs/sphinx/test_cmdln/_test_parse_defaults.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5testname=`basename $0 .test`
     6./cmdln_parse_r -a 42 > $testname.out 2>&1
     7sed -ne 's,^-,-,p' < $testname.out > $testname.tmp.out \
     8    && mv $testname.tmp.out $testname.out
     9compare_table defaults $testname.out $tests/unit/test_cmdln/$testname.res 100
     10rm -f $testname.out
  • src/tests/libs/sphinx/sphinx_adtools/sphinx_pitch.c

    Property changes on: src/tests/libs/sphinx/test_cmdln/_test_parse_defaults_r.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 2008 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37
     38#include <stdio.h>
     39#include <string.h>
     40
     41#include <cmd_ln.h>
     42#include <yin.h>
     43#include <ckd_alloc.h>
     44#include <byteorder.h>
     45#include <strfuncs.h>
     46#include <err.h>
     47#include <pio.h>
     48
     49#ifndef WORDS_BIGENDIAN
     50#define WORDS_BIGENDIAN 0
     51#endif
     52
     53static arg_t defn[] = {
     54  { "-i",
     55    ARG_STRING,
     56    NULL,
     57    "Single audio input file" },
     58
     59  { "-o",
     60    ARG_STRING,
     61    NULL,
     62    "Single text output file (standard output will be used if not given)" },
     63 
     64  { "-c",
     65    ARG_STRING,
     66    NULL,
     67    "Control file for batch processing" },
     68 
     69  { "-nskip",
     70    ARG_INT32,
     71    "0",
     72    "If a control file was specified, the number of utterances to skip at the head of the file" },
     73 
     74  { "-runlen",
     75    ARG_INT32,
     76    "-1",
     77    "If a control file was specified, the number of utterances to process (see -nskip too)" },
     78 
     79  { "-di",
     80    ARG_STRING,
     81    NULL,
     82    "Input directory, input file names are relative to this, if defined" },
     83 
     84  { "-ei",
     85    ARG_STRING,
     86    NULL,
     87    "Input extension to be applied to all input files" },
     88 
     89  { "-do",
     90    ARG_STRING,
     91    NULL,
     92    "Output directory, output files are relative to this" },
     93 
     94  { "-eo",
     95    ARG_STRING,
     96    NULL,
     97    "Output extension to be applied to all output files" },
     98 
     99  { "-nist",
     100    ARG_BOOLEAN,
     101    "no",
     102    "Defines input format as NIST sphere" },
     103 
     104  { "-raw",
     105    ARG_BOOLEAN,
     106    "no",
     107    "Defines input format as raw binary data" },
     108 
     109  { "-mswav",
     110    ARG_BOOLEAN,
     111    "no",
     112    "Defines input format as Microsoft Wav (RIFF)" },
     113
     114  { "-samprate",
     115    ARG_INT32,
     116    "0",
     117    "Sampling rate of audio data (will be determined automatically if 0)" },
     118
     119  { "-input_endian",
     120    ARG_STRING,
     121    NULL,
     122    "Endianness of audio data (will be determined automatically if not given)" },
     123
     124  { "-fshift",
     125    ARG_FLOAT32,
     126    "0.01",
     127    "Frame shift: number of seconds between each analysis frame." },
     128
     129  { "-flen",
     130    ARG_FLOAT32,
     131    "0.025",
     132    "Number of seconds in each analysis frame (needs to be greater than twice the longest period you wish to detect - to detect down to 80Hz you need a frame length of 2.0/80 = 0.025)." },
     133
     134  { "-smooth_window",
     135    ARG_INT32,
     136    "2",
     137    "Number of frames on either side of the current frame to use for smoothing." },
     138
     139  { "-voice_thresh",
     140    ARG_FLOAT32,
     141    "0.1",
     142    "Threshold of normalized difference under which to search for the fundamental period." },
     143
     144  { "-search_range",
     145    ARG_FLOAT32,
     146    "0.2",
     147    "Fraction of the best local estimate to use as a search range for smoothing." },
     148
     149  { NULL, 0, NULL, NULL }
     150};
     151
     152static int extract_pitch(const char *in, const char *out);
     153static int run_control_file(const char *ctl);
     154
     155int
     156main(int argc, char *argv[])
     157{
     158    cmd_ln_parse(defn, argc, argv, TRUE);
     159
     160    /* Run a control file if requested. */
     161    if (cmd_ln_str("-c")) {
     162        if (run_control_file(cmd_ln_str("-c")) < 0)
     163            return 1;
     164    }
     165    else {
     166        if (extract_pitch(cmd_ln_str("-i"), cmd_ln_str("-o")) < 0)
     167            return 1;
     168    }
     169
     170    cmd_ln_free();
     171    return 0;
     172}
     173
     174static int
     175guess_file_type(char const *file, FILE *infh)
     176{
     177    char header[4];
     178
     179    fseek(infh, 0, SEEK_SET);
     180    if (fread(header, 1, 4, infh) != 4) {
     181        E_ERROR_SYSTEM("Failed to read 4 byte header");
     182        return -1;
     183    }
     184    if (0 == memcmp(header, "RIFF", 4)) {
     185        E_INFO("%s appears to be a WAV file\n", file);
     186        cmd_ln_set_boolean("-mswav", TRUE);
     187        cmd_ln_set_boolean("-nist", FALSE);
     188        cmd_ln_set_boolean("-raw", FALSE);
     189    }
     190    else if (0 == memcmp(header, "NIST", 4)) {
     191        E_INFO("%s appears to be a NIST SPHERE file\n", file);
     192        cmd_ln_set_boolean("-mswav", FALSE);
     193        cmd_ln_set_boolean("-nist", TRUE);
     194        cmd_ln_set_boolean("-raw", FALSE);
     195    }
     196    else {
     197        E_INFO("%s appears to be raw data\n", file);
     198        cmd_ln_set_boolean("-mswav", FALSE);
     199        cmd_ln_set_boolean("-nist", FALSE);
     200        cmd_ln_set_boolean("-raw", TRUE);
     201    }
     202    fseek(infh, 0, SEEK_SET);
     203    return 0;
     204}
     205
     206#define TRY_FREAD(ptr, size, nmemb, stream)                             \
     207    if (fread(ptr, size, nmemb, stream) != (nmemb)) {                   \
     208        E_ERROR_SYSTEM("Failed to read %d bytes", size * nmemb);       \
     209        goto error_out;                                                 \
     210    }
     211
     212static int
     213read_riff_header(FILE *infh)
     214{
     215    char id[4];
     216    int32 intval, header_len;
     217    int16 shortval;
     218
     219    /* RIFF files are little-endian by definition. */
     220    cmd_ln_set_str("-input_endian", "little");
     221
     222    /* Read in all the header chunks and etcetera. */
     223    TRY_FREAD(id, 1, 4, infh);
     224    /* Total file length (we don't care) */
     225    TRY_FREAD(&intval, 4, 1, infh);
     226    /* 'WAVE' */
     227    TRY_FREAD(id, 1, 4, infh);
     228    if (0 != memcmp(id, "WAVE", 4)) {
     229        E_ERROR("This is not a WAVE file\n");
     230        goto error_out;
     231    }
     232    /* 'fmt ' */
     233    TRY_FREAD(id, 1, 4, infh);
     234    if (0 != memcmp(id, "fmt ", 4)) {
     235        E_ERROR("Format chunk missing\n");
     236        goto error_out;
     237    }
     238    /* Length of 'fmt ' chunk */
     239    TRY_FREAD(&intval, 4, 1, infh);
     240    if (WORDS_BIGENDIAN) SWAP_INT32(&intval);
     241    header_len = intval;
     242
     243    /* Data format. */
     244    TRY_FREAD(&shortval, 2, 1, infh);
     245    if (WORDS_BIGENDIAN) SWAP_INT16(&shortval);
     246    if (shortval != 1) { /* PCM */
     247        E_ERROR("WAVE file is not in PCM format\n");
     248        goto error_out;
     249    }
     250
     251    /* Number of channels. */
     252    TRY_FREAD(&shortval, 2, 1, infh);
     253    if (WORDS_BIGENDIAN) SWAP_INT16(&shortval);
     254    if (shortval != 1) { /* PCM */
     255        E_ERROR("WAVE file is not single channel\n");
     256        goto error_out;
     257    }
     258
     259    /* Sampling rate (finally!) */
     260    TRY_FREAD(&intval, 4, 1, infh);
     261    if (WORDS_BIGENDIAN) SWAP_INT32(&intval);
     262    if (cmd_ln_int32("-samprate") == 0)
     263        cmd_ln_set_int32("-samprate", intval);
     264    else if (cmd_ln_int32("-samprate") != intval) {
     265        E_WARN("WAVE file sampling rate %d != -samprate %d\n",
     266               intval, cmd_ln_int32("-samprate"));
     267    }
     268
     269    /* Average bytes per second (we don't care) */
     270    TRY_FREAD(&intval, 4, 1, infh);
     271
     272    /* Block alignment (we don't care) */
     273    TRY_FREAD(&shortval, 2, 1, infh);
     274
     275    /* Bits per sample (must be 16) */
     276    TRY_FREAD(&shortval, 2, 1, infh);
     277    if (WORDS_BIGENDIAN) SWAP_INT16(&shortval);
     278    if (shortval != 16) {
     279        E_ERROR("WAVE file is not 16-bit\n");
     280        goto error_out;
     281    }
     282
     283    /* Any extra parameters. */
     284    if (header_len > 16)
     285        fseek(infh, header_len - 16, SEEK_CUR);
     286
     287    /* Now skip to the 'data' chunk. */
     288    while (1) {
     289        TRY_FREAD(id, 1, 4, infh);
     290        if (0 == memcmp(id, "data", 4)) {
     291            /* Total number of bytes of data (we don't care). */
     292            TRY_FREAD(&intval, 4, 1, infh);
     293            break;
     294        }
     295        else {
     296            /* Some other stuff... */
     297            /* Number of bytes of ... whatever */
     298            TRY_FREAD(&intval, 4, 1, infh);
     299            if (WORDS_BIGENDIAN) SWAP_INT32(&intval);
     300            fseek(infh, intval, SEEK_CUR);
     301        }
     302    }
     303
     304    /* We are ready to rumble. */
     305    return 0;
     306error_out:
     307    return -1;
     308}
     309
     310static int
     311read_nist_header(FILE *infh)
     312{
     313    char hdr[1024];
     314    char *line, *c;
     315
     316    TRY_FREAD(hdr, 1, 1024, infh);
     317    hdr[1023] = '\0';
     318
     319    /* Roughly parse it to find the sampling rate and byte order
     320     * (don't bother with other stuff) */
     321    if ((line = strstr(hdr, "sample_rate")) == NULL) {
     322        E_ERROR("No sampling rate in NIST header!\n");
     323        goto error_out;
     324    }
     325    c = strchr(line, '\n');
     326    if (c) *c = '\0';
     327    c = strrchr(line, ' ');
     328    if (c == NULL) {
     329        E_ERROR("Could not find sampling rate!\n");
     330        goto error_out;
     331    }
     332    ++c;
     333    if (cmd_ln_int32("-samprate") == 0)
     334        cmd_ln_set_int32("-samprate", atoi(c));
     335    else if (cmd_ln_int32("-samprate") != atoi(c)) {
     336        E_WARN("NIST file sampling rate %d != -samprate %d\n",
     337               atoi(c), cmd_ln_int32("-samprate"));
     338    }
     339
     340    if (line + strlen(line) < hdr + 1023)
     341        line[strlen(line)] = ' ';
     342    if ((line = strstr(hdr, "sample_byte_format")) == NULL) {
     343        E_ERROR("No sample byte format in NIST header!\n");
     344        goto error_out;
     345    }
     346    c = strchr(line, '\n');
     347    if (c) *c = '\0';
     348    c = strrchr(line, ' ');
     349    if (c == NULL) {
     350        E_ERROR("Could not find sample byte order!\n");
     351        goto error_out;
     352    }
     353    ++c;
     354    if (0 == memcmp(c, "01", 2)) {
     355        cmd_ln_set_str("-input_endian", "little");
     356    }
     357    else if (0 == memcmp(c, "10", 2)) {
     358        cmd_ln_set_str("-input_endian", "big");
     359    }
     360    else {
     361        E_ERROR("Unknown byte order %s\n", c);
     362        goto error_out;
     363    }
     364
     365    /* We are ready to rumble. */
     366    return 0;
     367error_out:
     368    return -1;
     369}
     370
     371static int
     372extract_pitch(const char *in, const char *out)
     373{
     374    FILE *infh = NULL, *outfh = NULL;
     375    size_t flen, fshift, nsamps;
     376    int16 *buf = NULL;
     377    yin_t *yin = NULL;
     378    uint16 period, bestdiff;
     379    int32 sps;
     380
     381    if (out) {
     382        if ((outfh = fopen(out, "w")) == NULL) {
     383            E_ERROR_SYSTEM("Failed to open %s for writing", outfh);
     384            goto error_out;
     385        }
     386    }
     387    else {
     388        outfh = stdout;
     389    }
     390    if ((infh = fopen(in, "rb")) == NULL) {
     391        E_ERROR_SYSTEM("Failed to open %s for reading", infh);
     392        goto error_out;
     393    }
     394
     395    /* If we weren't told what the file type is, weakly try to
     396     * determine it (actually it's pretty obvious) */
     397    if (!(cmd_ln_boolean("-raw")
     398          || cmd_ln_boolean("-mswav")
     399          || cmd_ln_boolean("-nist"))) {
     400        if (guess_file_type(in, infh) < 0)
     401            goto error_out;
     402    }
     403   
     404    /* Grab the sampling rate and byte order from the header and also
     405     * make sure this is 16-bit linear PCM. */
     406    if (cmd_ln_boolean("-mswav")) {
     407        if (read_riff_header(infh) < 0)
     408            goto error_out;
     409    }
     410    else if (cmd_ln_boolean("-nist")) {
     411        if (read_nist_header(infh) < 0)
     412            goto error_out;
     413    }
     414    else if (cmd_ln_boolean("-raw")) {
     415        /* Just use some defaults for sampling rate and endian. */
     416        if (cmd_ln_str("-input_endian") == NULL) {
     417            if (WORDS_BIGENDIAN)
     418                cmd_ln_set_str("-input_endian", "big");
     419            else
     420                cmd_ln_set_str("-input_endian", "little");
     421        }
     422        if (cmd_ln_int32("-samprate") == 0)
     423            cmd_ln_set_int32("-samprate", 16000);
     424    }
     425
     426    /* Now read frames and write pitch estimates. */
     427    sps = cmd_ln_int32("-samprate");
     428    flen = (size_t)(0.5 + sps * cmd_ln_float32("-flen"));
     429    fshift = (size_t)(0.5 + sps * cmd_ln_float32("-fshift"));
     430    yin = yin_init(flen, cmd_ln_float32("-voice_thresh"),
     431                   cmd_ln_float32("-search_range"),
     432                   cmd_ln_int32("-smooth_window"));
     433    if (yin == NULL) {
     434        E_ERROR("Failed to initialize YIN\n");
     435        goto error_out;
     436    }
     437    buf = ckd_calloc(flen, sizeof(*buf));
     438    /* Read the first full frame of data. */
     439    if (fread(buf, sizeof(*buf), flen, infh) != flen) {
     440        /* Fail silently, which is probably okay. */
     441    }
     442    yin_start(yin);
     443    nsamps = 0;
     444    while (!feof(infh)) {
     445        /* Process a frame of data. */
     446        yin_write(yin, buf);
     447        if (yin_read(yin, &period, &bestdiff)) {
     448            fprintf(outfh, "%.3f %.2f %.2f\n",
     449                    /* Time point. */
     450                    (double)nsamps/sps,
     451                    /* "Probability" of voicing. */
     452                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
     453                    /* Pitch (possibly bogus) */
     454                    period == 0 ? sps : (double)sps / period);
     455            nsamps += fshift;
     456        }
     457        /* Shift it back and get the next frame's overlap. */
     458        memmove(buf, buf + fshift, (flen - fshift) * sizeof(*buf));
     459        if (fread(buf + flen - fshift, sizeof(*buf), fshift, infh) != fshift) {
     460            /* Fail silently (FIXME: really?) */
     461        }
     462    }
     463    yin_end(yin);
     464    /* Process trailing frames of data. */
     465    while (yin_read(yin, &period, &bestdiff)) {
     466            fprintf(outfh, "%.3f %.2f %.2f\n",
     467                    /* Time point. */
     468                    (double)nsamps/sps,
     469                    /* "Probability" of voicing. */
     470                    bestdiff > 32768 ? 0.0 : 1.0 - (double)bestdiff / 32768,
     471                    /* Pitch (possibly bogus) */
     472                    period == 0 ? sps : (double)sps / period);
     473    }
     474
     475    if (yin)
     476        yin_free(yin);
     477    ckd_free(buf);
     478    fclose(infh);
     479    if (outfh != stdout)
     480        fclose(outfh);
     481    return 0;
     482
     483error_out:
     484    yin_free(yin);
     485    ckd_free(buf);
     486    if (infh) fclose(infh);
     487    if (outfh && outfh != stdout) fclose(outfh);
     488    return -1;
     489}
     490
     491static int
     492run_control_file(const char *ctl)
     493{
     494    FILE *ctlfh;
     495    char *line;
     496    char *di, *dout, *ei, *eio;
     497    size_t len;
     498    int rv, guess_type, guess_sps, guess_endian;
     499    int32 skip, runlen;
     500
     501    skip = cmd_ln_int32("-nskip");
     502    runlen = cmd_ln_int32("-runlen");
     503
     504    /* Whether to guess file types */
     505    guess_type = !(cmd_ln_boolean("-raw")
     506                   || cmd_ln_boolean("-mswav")
     507                   || cmd_ln_boolean("-nist"));
     508    /* Whether to guess sampling rate */
     509    guess_sps = (cmd_ln_int32("-samprate") == 0);
     510    /* Whether to guess endian */
     511    guess_endian = (cmd_ln_str("-input_endian") == NULL);
     512
     513    if ((ctlfh = fopen(ctl, "r")) == NULL) {
     514        E_ERROR_SYSTEM("Failed to open control file %s", ctl);
     515        return -1;
     516    }
     517    if (cmd_ln_str("-di"))
     518        di = string_join(cmd_ln_str("-di"), "/", NULL);
     519    else
     520        di = ckd_salloc("");
     521    if (cmd_ln_str("-do"))
     522        dout = string_join(cmd_ln_str("-do"), "/", NULL);
     523    else
     524        dout = ckd_salloc("");
     525    if (cmd_ln_str("-ei"))
     526        ei = string_join(".", cmd_ln_str("-ei"), NULL);
     527    else
     528        ei = ckd_salloc("");
     529    if (cmd_ln_str("-eo"))
     530        eio = string_join(".", cmd_ln_str("-eo"), NULL);
     531    else
     532        eio = ckd_salloc("");
     533    rv = 0;
     534    while ((line = fread_line(ctlfh, &len)) != NULL) {
     535        char *infile, *outfile;
     536
     537        if (skip-- > 0) {
     538            ckd_free(line);
     539            continue;
     540        }
     541        if (runlen == 0) {
     542            ckd_free(line);
     543            break;
     544        }
     545        --runlen;
     546
     547        if (line[len-1] == '\n')
     548            line[len-1] = '\0';
     549
     550        infile = string_join(di, line, ei, NULL);
     551        outfile = string_join(dout, line, eio, NULL);
     552
     553        /* Reset various guessed information */
     554        if (guess_type) {
     555            cmd_ln_set_boolean("-nist", FALSE);
     556            cmd_ln_set_boolean("-mswav", FALSE);
     557            cmd_ln_set_boolean("-raw", FALSE);
     558        }
     559        if (guess_sps)
     560            cmd_ln_set_int32("-samprate", 0);
     561        if (guess_endian)
     562            cmd_ln_set_str("-input_endian", NULL);
     563
     564        rv = extract_pitch(infile, outfile);
     565
     566        ckd_free(infile);
     567        ckd_free(outfile);
     568        ckd_free(line);
     569
     570        if (rv != 0)
     571            break;
     572    }
     573    ckd_free(di);
     574    ckd_free(dout);
     575    ckd_free(ei);
     576    ckd_free(eio);
     577    fclose(ctlfh);
     578    return rv;
     579}
  • src/tests/libs/sphinx/sphinx_adtools/cont_fileseg.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 1999-2001 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37/*
     38 * cont_fileseg.c -- Read input file, filter silence regions, and segment into utterances.
     39 *
     40 * HISTORY
     41 *
     42 * $Log: cont_fileseg.c,v $
     43 * Revision 1.1.1.1  2006/05/23 18:45:02  dhuggins
     44 * re-importation
     45 *
     46 * Revision 1.13  2005/06/30 00:28:46  rkm
     47 * Kept within-utterance silences in rawmode
     48 *
     49 *
     50 * 28-Jun-2005  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
     51 *      Modified to use new state variables in cont_ad_t.
     52 *
     53 * Revision 1.12  2005/05/31 15:54:38  rkm
     54 * *** empty log message ***
     55 *
     56 * Revision 1.11  2005/05/24 20:56:58  rkm
     57 * Added min/max-noise parameters to cont_fileseg
     58 *
     59 * Revision 1.10  2005/05/13 23:28:43  egouvea
     60 * Changed null device to system dependent one: NUL for windows, /dev/null for everything else
     61 *
     62 * $Log: cont_fileseg.c,v $
     63 * Revision 1.1.1.1  2006/05/23 18:45:02  dhuggins
     64 * re-importation
     65 *
     66 * Revision 1.13  2005/06/30 00:28:46  rkm
     67 * Kept within-utterance silences in rawmode
     68 *
     69 * Revision 1.12  2005/05/31 15:54:38  rkm
     70 * *** empty log message ***
     71 *
     72 * Revision 1.11  2005/05/24 20:56:58  rkm
     73 * Added min/max-noise parameters to cont_fileseg
     74 *
     75 * Revision 1.9  2005/02/13 01:29:48  rkm
     76 * Fixed cont_ad_read to never cross sil/speech boundary, and rawmode
     77 *
     78 * Revision 1.8  2005/02/01 22:21:13  rkm
     79 * Added raw data logging, and raw data pass-through mode to cont_ad
     80 *
     81 * Revision 1.7  2004/07/16 00:57:11  egouvea
     82 * Added Ravi's implementation of FSG support.
     83 *
     84 * Revision 1.3  2004/06/25 14:58:05  rkm
     85 * *** empty log message ***
     86 *
     87 * Revision 1.2  2004/06/23 20:32:08  rkm
     88 * Exposed several cont_ad config parameters
     89 *
     90 *
     91 * 27-Jun-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
     92 *      Created.
     93 */
     94
     95#include <stdio.h>
     96#include <stdlib.h>
     97#include <string.h>
     98#include <assert.h>
     99#include <math.h>
     100
     101#include <prim_type.h>
     102#include <ad.h>
     103#include <cont_ad.h>
     104#include <err.h>
     105
     106static FILE *infp;              /* File being segmented */
     107static int32 swap;
     108
     109/* Max size read by file_ad_read function on each invocation, for debugging */
     110static int32 max_ad_read_size;
     111
     112#if defined(WIN32) && !defined(GNUWINCE)
     113#define NULL_DEVICE "NUL"
     114#else
     115#define NULL_DEVICE "/dev/null"
     116#endif
     117
     118
     119/*
     120 * Need to provide cont_ad_init with a read function to read the input file.
     121 * This is it.  The ad_rec_t *r argument is ignored since there is no A/D
     122 * device involved.
     123 */
     124static int32
     125file_ad_read(ad_rec_t * r, int16 * buf, int32 max)
     126{
     127    int32 i, k;
     128
     129    if (max > max_ad_read_size)
     130        max = max_ad_read_size;
     131
     132    k = fread(buf, sizeof(int16), max, infp);
     133    if (swap) {
     134        for (i = 0; i < k; i++) {
     135            buf[i] = ((buf[i] >> 8) & 0x00ff) | ((buf[i] << 8) & 0xff00);
     136        }
     137    }
     138
     139    return ((k > 0) ? k : -1);
     140}
     141
     142
     143static void
     144usagemsg(char *pgm)
     145{
     146    E_INFO("Usage: %s \\\n", pgm);
     147    E_INFOCONT("\t[-? | -h] \\\n");
     148    E_INFOCONT("\t[-d | -debug] \\\n");
     149    E_INFOCONT("\t[-sps <sampling-rate> (16000)] \\\n");
     150    E_INFOCONT("\t[-b | -byteswap] \\\n");
     151    E_INFOCONT
     152        ("\t[{-s | -silsep} <length-silence-separator(sec) (0.5)]> \\\n");
     153    E_INFOCONT("\t[-w | -writeseg] \\\n");
     154    E_INFOCONT("\t[-min-noise <min-noise>] \\\n");
     155    E_INFOCONT("\t[-max-noise <max-noise>] \\\n");
     156    E_INFOCONT("\t[-delta-sil <delta-sil>] \\\n");
     157    E_INFOCONT("\t[-delta-speech <delta-speech>] \\\n");
     158    E_INFOCONT("\t[-sil-onset <sil-onset>] \\\n");
     159    E_INFOCONT("\t[-speech-onset <speech-onset>] \\\n");
     160    E_INFOCONT("\t[-adapt-rate <adapt-rate>] \\\n");
     161    E_INFOCONT("\t[-max-adreadsize <ad_read_blksize>] \\\n");
     162    E_INFOCONT("\t[-c <copy-input-file>] \\\n");
     163    E_INFOCONT("\t[-r | -rawmode] \\\n");
     164    E_INFOCONT("\t-i <input-file>\n");
     165
     166    exit(0);
     167}
     168
     169/*
     170 * Read specified input file, segment it into utterances wherever a silence segment of
     171 * a given minimum duration is encountered.  Filter out long silences.
     172 * Utterances are written to files named 00000000.raw, 00000001.raw, 00000002.raw, etc.
     173 */
     174int
     175main(int32 argc, char **argv)
     176{
     177    cont_ad_t *cont;
     178    int32 uttid, uttlen, starttime, siltime, sps, debug, writeseg, rawmode;
     179    int16 buf[4096];
     180    char *infile, *copyfile, segfile[1024];
     181    FILE *fp;
     182    float endsil;
     183    ad_rec_t ad;
     184    int32 i, k;
     185    int32 winsize, leader, trailer;
     186    int32 orig_min_noise, orig_max_noise;
     187    int32 orig_delta_sil, orig_delta_speech;
     188    int32 orig_speech_onset, orig_sil_onset;
     189    int32 min_noise, max_noise;
     190    int32 delta_sil, delta_speech;
     191    int32 sil_onset, speech_onset;
     192    float32 orig_adapt_rate;
     193    float32 adapt_rate;
     194    int32 total_speech_samples;
     195    float32 total_speech_sec;
     196    FILE *rawfp;
     197
     198    /* Set argument defaults */
     199    cont = NULL;
     200    sps = 16000;
     201    swap = 0;
     202    endsil = 0.5;
     203    writeseg = 0;
     204    min_noise = max_noise = -1;
     205    delta_sil = delta_speech = -1;
     206    sil_onset = speech_onset = -1;
     207    adapt_rate = -1.0;
     208    max_ad_read_size = (int32) 0x7ffffff0;
     209    debug = 0;
     210    infile = NULL;
     211    copyfile = NULL;
     212    rawfp = NULL;
     213    rawmode = 0;
     214
     215    /* Parse arguments */
     216    for (i = 1; i < argc; i++) {
     217        if ((strcmp(argv[i], "-help") == 0)
     218            || (strcmp(argv[i], "-h") == 0)
     219            || (strcmp(argv[i], "-?") == 0)) {
     220            usagemsg(argv[0]);
     221        }
     222        else if ((strcmp(argv[i], "-debug") == 0)
     223                 || (strcmp(argv[i], "-d") == 0)) {
     224            debug = 1;
     225        }
     226        else if (strcmp(argv[i], "-sps") == 0) {
     227            i++;
     228            if ((i == argc)
     229                || (sscanf(argv[i], "%d", &sps) != 1)
     230                || (sps <= 0)) {
     231                E_ERROR("Invalid -sps argument\n");
     232                usagemsg(argv[0]);
     233            }
     234        }
     235        else if ((strcmp(argv[i], "-byteswap") == 0)
     236                 || (strcmp(argv[i], "-b") == 0)) {
     237            swap = 1;
     238        }
     239        else if ((strcmp(argv[i], "-silsep") == 0)
     240                 || (strcmp(argv[i], "-s") == 0)) {
     241            i++;
     242            if ((i == argc)
     243                || (sscanf(argv[i], "%f", &endsil) != 1)
     244                || (endsil <= 0.0)) {
     245                E_ERROR("Invalid -silsep argument\n");
     246                usagemsg(argv[0]);
     247            }
     248        }
     249        else if ((strcmp(argv[i], "-writeseg") == 0)
     250                 || (strcmp(argv[i], "-w") == 0)) {
     251            writeseg = 1;
     252        }
     253        else if (strcmp(argv[i], "-min-noise") == 0) {
     254            i++;
     255            if ((i == argc) ||
     256                (sscanf(argv[i], "%d", &min_noise) != 1) ||
     257                (min_noise < 0)) {
     258                E_ERROR("Invalid -min-noise argument\n");
     259                usagemsg(argv[0]);
     260            }
     261        }
     262        else if (strcmp(argv[i], "-max-noise") == 0) {
     263            i++;
     264            if ((i == argc) ||
     265                (sscanf(argv[i], "%d", &max_noise) != 1) ||
     266                (max_noise < 0)) {
     267                E_ERROR("Invalid -max-noise argument\n");
     268                usagemsg(argv[0]);
     269            }
     270        }
     271        else if (strcmp(argv[i], "-delta-sil") == 0) {
     272            i++;
     273            if ((i == argc) ||
     274                (sscanf(argv[i], "%d", &delta_sil) != 1) ||
     275                (delta_sil < 0)) {
     276                E_ERROR("Invalid -delta-sil argument\n");
     277                usagemsg(argv[0]);
     278            }
     279        }
     280        else if (strcmp(argv[i], "-delta-speech") == 0) {
     281            i++;
     282            if ((i == argc) ||
     283                (sscanf(argv[i], "%d", &delta_speech) != 1) ||
     284                (delta_speech < 0)) {
     285                E_ERROR("Invalid -delta-speech argument\n");
     286                usagemsg(argv[0]);
     287            }
     288        }
     289        else if (strcmp(argv[i], "-sil-onset") == 0) {
     290            i++;
     291            if ((i == argc) ||
     292                (sscanf(argv[i], "%d", &sil_onset) != 1) ||
     293                (sil_onset < 1)) {
     294                E_ERROR("Invalid -sil-onset argument\n");
     295                usagemsg(argv[0]);
     296            }
     297        }
     298        else if (strcmp(argv[i], "-speech-onset") == 0) {
     299            i++;
     300            if ((i == argc) ||
     301                (sscanf(argv[i], "%d", &speech_onset) != 1) ||
     302                (speech_onset < 1)) {
     303                E_ERROR("Invalid -speech-onset argument\n");
     304                usagemsg(argv[0]);
     305            }
     306        }
     307        else if (strcmp(argv[i], "-adapt-rate") == 0) {
     308            i++;
     309            if ((i == argc) ||
     310                (sscanf(argv[i], "%f", &adapt_rate) != 1) ||
     311                (adapt_rate < 0.0) || (adapt_rate > 1.0)) {
     312                E_ERROR("Invalid -adapt-rate argument\n");
     313                usagemsg(argv[0]);
     314            }
     315        }
     316        else if (strcmp(argv[i], "-max-adreadsize") == 0) {
     317            i++;
     318            if ((i == argc) ||
     319                (sscanf(argv[i], "%d", &max_ad_read_size) != 1) ||
     320                (max_ad_read_size < 1)) {
     321                E_ERROR("Invalid -max-adreadsize argument\n");
     322                usagemsg(argv[0]);
     323            }
     324        }
     325        else if (strcmp(argv[i], "-c") == 0) {
     326            i++;
     327            if (i == argc) {
     328                E_ERROR("Invalid -c argument\n");
     329                usagemsg(argv[0]);
     330            }
     331            copyfile = argv[i];
     332        }
     333        else if ((strcmp(argv[i], "-rawmode") == 0)
     334                 || (strcmp(argv[i], "-r") == 0)) {
     335            rawmode = 1;
     336        }
     337        else if (strcmp(argv[i], "-i") == 0) {
     338            i++;
     339            if (i == argc) {
     340                E_ERROR("Invalid -i argument\n");
     341                usagemsg(argv[0]);
     342            }
     343            infile = argv[i];
     344        }
     345        else {
     346            usagemsg(argv[0]);
     347        }
     348    }
     349
     350    if (infile == NULL) {
     351        E_ERROR("No input file specified\n");
     352        usagemsg(argv[0]);
     353    }
     354
     355    if ((infp = fopen(infile, "rb")) == NULL)
     356        E_FATAL("fopen(%s,rb) failed\n", infile);
     357
     358    /*
     359     * Associate continuous listening module with opened input file and read function.
     360     * No A/D device is involved, but need to fill in ad->sps.
     361     * Calibrate input data using first few seconds of file, but then rewind it!!
     362     */
     363    ad.sps = sps;
     364    ad.bps = sizeof(int16);
     365    if (!rawmode)
     366        cont = cont_ad_init(&ad, file_ad_read);
     367    else
     368        cont = cont_ad_init_rawmode(&ad, file_ad_read);
     369
     370    printf("Calibrating ...");
     371    fflush(stdout);
     372    if (cont_ad_calib(cont) < 0)
     373        printf(" failed; file too short?\n");
     374    else
     375        printf(" done\n");
     376    rewind(infp);
     377
     378    /* Convert desired min. inter-utterance silence duration to #samples */
     379    siltime = (int32) (endsil * sps);
     380
     381    /* Enable writing raw input to output by the cont module if specified */
     382    if (copyfile) {
     383        if ((rawfp = fopen(copyfile, "wb")) == NULL)
     384            E_ERROR("fopen(%s,wb) failed; not dumping raw file\n",
     385                    copyfile);
     386        else
     387            cont_ad_set_rawfp(cont, rawfp);
     388    }
     389
     390    cont_ad_get_params(cont,
     391                       &orig_delta_sil, &orig_delta_speech,
     392                       &orig_min_noise, &orig_max_noise,
     393                       &winsize,
     394                       &orig_speech_onset, &orig_sil_onset,
     395                       &leader, &trailer, &orig_adapt_rate);
     396
     397    E_INFO("Default parameters:\n");
     398    E_INFOCONT("\tmin-noise = %d, max-noise = %d\n",
     399               orig_min_noise, orig_max_noise);
     400    E_INFOCONT("\tdelta-sil = %d, delta-speech = %d\n",
     401               orig_delta_sil, orig_delta_speech);
     402    E_INFOCONT("\tsil-onset = %d, speech-onset = %d\n",
     403               orig_sil_onset, orig_speech_onset);
     404    E_INFOCONT("\tadapt_rate = %.3f\n", orig_adapt_rate);
     405
     406    if (min_noise < 0)
     407        min_noise = orig_min_noise;
     408    if (max_noise < 0)
     409        max_noise = orig_max_noise;
     410    if (delta_sil < 0)
     411        delta_sil = orig_delta_sil;
     412    if (delta_speech < 0)
     413        delta_speech = orig_delta_speech;
     414    if (sil_onset < 0)
     415        sil_onset = orig_sil_onset;
     416    if (speech_onset < 0)
     417        speech_onset = orig_speech_onset;
     418    if (adapt_rate < 0.0)
     419        adapt_rate = orig_adapt_rate;
     420
     421    cont_ad_set_params(cont,
     422                       delta_sil, delta_speech,
     423                       min_noise, max_noise,
     424                       winsize,
     425                       speech_onset, sil_onset,
     426                       leader, trailer, adapt_rate);
     427
     428    E_INFO("Current parameters:\n");
     429    E_INFOCONT("\tmin-noise = %d, max-noise = %d\n", min_noise, max_noise);
     430    E_INFOCONT("\tdelta-sil = %d, delta-speech = %d\n", delta_sil,
     431               delta_speech);
     432    E_INFOCONT("\tsil-onset = %d, speech-onset = %d\n", sil_onset,
     433               speech_onset);
     434    E_INFOCONT("\tadapt_rate = %.3f\n", adapt_rate);
     435
     436    E_INFO("Sampling rate: %d", sps);
     437    E_INFOCONT("; Byteswap: %s", swap ? "Yes" : "No");
     438    E_INFOCONT("; Max ad-read size: %d\n", max_ad_read_size);
     439
     440    if (debug)
     441        cont_ad_set_logfp(cont, stdout);
     442
     443    total_speech_samples = 0;
     444    total_speech_sec = 0.0;
     445
     446    uttid = 0;
     447    uttlen = 0;
     448    starttime = 0;
     449    fp = NULL;
     450
     451    /* Process data */
     452    for (;;) {
     453        /* Get audio data from continuous listening module */
     454        k = cont_ad_read(cont, buf, 4096);
     455
     456        if (k < 0) {            /* End of input audio file; close any open output file and exit */
     457            if (fp != NULL) {
     458                fclose(fp);
     459                fp = NULL;
     460
     461                printf
     462                    ("Utt %08d, st= %8.2fs, et= %8.2fs, seg= %7.2fs (#samp= %10d)\n",
     463                     uttid, (double) starttime / (double) sps,
     464                     (double) (starttime + uttlen) / (double) sps,
     465                     (double) uttlen / (double) sps, uttlen);
     466                fflush(stdout);
     467
     468                total_speech_samples += uttlen;
     469                total_speech_sec += (double) uttlen / (double) sps;
     470
     471                uttid++;
     472            }
     473
     474            break;
     475        }
     476
     477        if (cont->state == CONT_AD_STATE_SIL) { /* Silence data got */
     478            if (fp != NULL) {   /* Currently in an utterance */
     479                if (cont->seglen > siltime) {   /* Long enough silence detected; end the utterance */
     480                    fclose(fp);
     481                    fp = NULL;
     482
     483                    printf
     484                        ("Utt %08d, st= %8.2fs, et= %8.2fs, seg= %7.2fs (#samp= %10d)\n",
     485                         uttid, (double) starttime / (double) sps,
     486                         (double) (starttime + uttlen) / (double) sps,
     487                         (double) uttlen / (double) sps, uttlen);
     488                    fflush(stdout);
     489
     490                    total_speech_samples += uttlen;
     491                    total_speech_sec += (double) uttlen / (double) sps;
     492
     493                    uttid++;
     494                }
     495                else {
     496                    /*
     497                     * Short silence within utt; write it to output.  (Some extra trailing silence
     498                     * is included in the utterance, as a result.  Not to worry about it.)
     499                     */
     500                    if (k > 0) {
     501                        fwrite(buf, sizeof(int16), k, fp);
     502                        uttlen += k;
     503                    }
     504                }
     505            }
     506        }
     507        else {
     508            assert(cont->state == CONT_AD_STATE_SPEECH);
     509
     510            if (fp == NULL) {   /* Not in an utt; open a new output file */
     511                if (writeseg)
     512                    sprintf(segfile, "%08d.raw", uttid);
     513                else
     514                    strcpy(segfile, NULL_DEVICE);
     515                if ((fp = fopen(segfile, "wb")) == NULL)
     516                    E_FATAL("fopen(%s,wb) failed\n", segfile);
     517
     518                starttime = cont->read_ts - k;
     519                uttlen = 0;
     520            }
     521
     522            /* Write data obtained to output file */
     523            if (k > 0) {
     524                fwrite(buf, sizeof(int16), k, fp);
     525                uttlen += k;
     526            }
     527        }
     528    }
     529
     530    if (rawfp)
     531        fclose(rawfp);
     532
     533    E_INFO("Total raw input speech = %d frames, %d samples, %.2f sec\n",
     534           cont->tot_frm, cont->tot_frm * cont->spf,
     535           (cont->tot_frm * cont->spf) / (float32) cont->sps);
     536    E_INFO("Total speech detected = %d samples, %.2f sec\n",
     537           total_speech_samples, total_speech_sec);
     538
     539    cont_ad_close(cont);
     540
     541    return 0;
     542}
  • src/tests/libs/sphinx/sphinx_adtools/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx sphinx_adtools ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6
     7SimpleTest sphinx_cont_adseg : cont_adseg.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     8SimpleTest sphinx_cont_fileseg : cont_fileseg.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest sphinx_pitch : sphinx_pitch.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/sphinx_adtools/cont_adseg.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/* ====================================================================
     3 * Copyright (c) 1999-2001 Carnegie Mellon University.  All rights
     4 * reserved.
     5 *
     6 * Redistribution and use in source and binary forms, with or without
     7 * modification, are permitted provided that the following conditions
     8 * are met:
     9 *
     10 * 1. Redistributions of source code must retain the above copyright
     11 *    notice, this list of conditions and the following disclaimer.
     12 *
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in
     15 *    the documentation and/or other materials provided with the
     16 *    distribution.
     17 *
     18 * This work was supported in part by funding from the Defense Advanced
     19 * Research Projects Agency and the National Science Foundation of the
     20 * United States of America, and the CMU Sphinx Speech Consortium.
     21 *
     22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
     23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
     26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 *
     34 * ====================================================================
     35 *
     36 */
     37/*
     38 * cont_adseg.c -- Continuously listen and segment input speech into utterances.
     39 *
     40 * HISTORY
     41 *
     42 * 27-Jun-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
     43 *      Created.
     44 */
     45
     46#include <stdio.h>
     47#include <stdlib.h>
     48#include <string.h>
     49#include <assert.h>
     50#include <math.h>
     51
     52#include <prim_type.h>
     53#include <ad.h>
     54#include <cont_ad.h>
     55#include <err.h>
     56
     57/*
     58 * Segment raw A/D input data into utterances whenever silence region of given
     59 * duration is encountered.
     60 * Utterances are written to files named 0001.raw, 0002.raw, 0003.raw, etc.
     61 */
     62int
     63main(int32 argc, char **argv)
     64{
     65    ad_rec_t *ad;
     66    cont_ad_t *cont;
     67    int32 k, uttno, ts, uttlen, sps, endsilsamples;
     68    float endsil;
     69    int16 buf[4096];
     70    FILE *fp;
     71    char file[1024];
     72
     73    if ((argc != 3) ||
     74        (sscanf(argv[1], "%d", &sps) != 1) ||
     75        (sscanf(argv[2], "%f", &endsil) != 1) || (endsil <= 0.0)) {
     76        E_FATAL("Usage: %s <sampling-rate> <utt-end-sil(sec)>\n", argv[0]);
     77    }
     78
     79    /* Convert desired min. inter-utterance silence duration to #samples */
     80    endsilsamples = (int32) (endsil * sps);
     81
     82    /* Open raw A/D device */
     83    if ((ad = ad_open_sps(sps)) == NULL)
     84        E_FATAL("ad_open_sps(%d) failed\n", sps);
     85
     86    /* Associate new continuous listening module with opened raw A/D device */
     87    if ((cont = cont_ad_init(ad, ad_read)) == NULL)
     88        E_FATAL("cont_ad_init failed\n");
     89
     90    /* Calibrate continuous listening for background noise/silence level */
     91    printf("Calibrating ...");
     92    fflush(stdout);
     93    ad_start_rec(ad);
     94    if (cont_ad_calib(cont) < 0)
     95        printf(" failed\n");
     96    else
     97        printf(" done\n");
     98
     99    /* Forever listen for utterances */
     100    printf("You may speak now\n");
     101    fflush(stdout);
     102    uttno = 0;
     103    for (;;) {
     104        /* Wait for beginning of next utterance; for non-silence data */
     105        while ((k = cont_ad_read(cont, buf, 4096)) == 0);
     106        if (k < 0)
     107            E_FATAL("cont_ad_read failed\n");
     108
     109        /* Non-silence data received; open and write to new logging file */
     110        uttno++;
     111        sprintf(file, "%04d.raw", uttno);
     112        if ((fp = fopen(file, "wb")) == NULL)
     113            E_FATAL("fopen(%s,wb) failed\n", file);
     114        fwrite(buf, sizeof(int16), k, fp);
     115        uttlen = k;
     116        printf("Utterance %04d, logging to %s\n", uttno, file);
     117
     118        /* Note current timestamp */
     119        ts = cont->read_ts;
     120
     121        /* Read utterance data until a gap of at least 1 sec observed */
     122        for (;;) {
     123            if ((k = cont_ad_read(cont, buf, 4096)) < 0)
     124                E_FATAL("cont_ad_read failed\n");
     125            if (k == 0) {
     126                /*
     127                 * No speech data available; check current timestamp.  End of
     128                 * utterance if no non-silence data been read for at least 1 sec.
     129                 */
     130                if ((cont->read_ts - ts) > endsilsamples)
     131                    break;
     132            }
     133            else {
     134                /* Note timestamp at the end of most recently read speech data */
     135                ts = cont->read_ts;
     136                uttlen += k;
     137                fwrite(buf, sizeof(int16), k, fp);
     138            }
     139        }
     140        fclose(fp);
     141
     142        printf("\tUtterance %04d = %d samples (%.1fsec)\n\n",
     143               uttno, uttlen, (double) uttlen / (double) sps);
     144    }
     145
     146    ad_stop_rec(ad);
     147    cont_ad_close(cont);
     148    ad_close(ad);
     149    return 0;
     150}
  • src/tests/libs/sphinx/test_matrix/_test_determinant.res

     
     15.22
     2-1.00
  • src/tests/libs/sphinx/test_matrix/_test_invert.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./test_invert | sed -e 's,-0\.0,0.0,g' > _test_invert.out
     6compare_table invert _test_invert.out $tests/unit/test_matrix/_test_invert.res
     7rm -f _test_invert.out
  • src/tests/libs/sphinx/test_matrix/_test_solve.test

    Property changes on: src/tests/libs/sphinx/test_matrix/_test_invert.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./test_solve > _test_solve.out
     6compare_table solve _test_solve.out $tests/unit/test_matrix/_test_solve.res
     7rm -f _test_solve.out
  • src/tests/libs/sphinx/test_matrix/_test_determinant.test

    Property changes on: src/tests/libs/sphinx/test_matrix/_test_solve.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./test_determinant | sed -e 's,-0\.0,0.0,g' > _test_determinant.out
     6compare_table determinant _test_determinant.out $tests/unit/test_matrix/_test_determinant.res
     7rm -f _test_determinant.out
  • src/tests/libs/sphinx/test_matrix/Jamfile

    Property changes on: src/tests/libs/sphinx/test_matrix/_test_determinant.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_matrix ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_solve : test_solve.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_determinant : test_determinant.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_invert : test_invert.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_matrix/test_invert.c

     
     1#include <stdio.h>
     2#include <string.h>
     3
     4#include "matrix.h"
     5#include "ckd_alloc.h"
     6
     7const float32 foo[3][3] = {
     8    {2, 1, 1},
     9    {1, 2, 1},
     10    {1, 1, 2}
     11};
     12const float32 bar[3][3] = {
     13    {2, 0.5, 1},
     14    {0.5, 2, 1},
     15    {1, 1, 2}
     16};
     17
     18int
     19main(int argc, char *argv[])
     20{
     21    float32 **a, **ainv, **ii;
     22    int i, j;
     23
     24    a = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32));
     25    ainv = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32));
     26    ii = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32));
     27
     28    memcpy(a[0], foo, sizeof(float32) * 3 * 3);
     29    printf("%d\n", invert(ainv, a, 3));
     30    /* Should see:
     31       0.75 -0.25 -0.25
     32       -0.25 0.75 -0.25
     33       -0.25 -0.25 0.75
     34    */
     35    for (i = 0; i < 3; ++i) {
     36        for (j = 0; j < 3; ++j) {
     37            printf("%.2f ", ainv[i][j]);
     38        }
     39        printf("\n");
     40    }
     41    /* Should see:
     42       1.00 0.00 0.00
     43       0.00 1.00 0.00
     44       0.00 0.00 1.00
     45    */
     46    matrixmultiply(ii, ainv, a, 3);
     47    for (i = 0; i < 3; ++i) {
     48        for (j = 0; j < 3; ++j) {
     49            printf("%.2f ", ii[i][j]);
     50        }
     51        printf("\n");
     52    }
     53
     54    memcpy(a[0], bar, sizeof(float32) * 3 * 3);
     55    printf("%d\n", invert(ainv, a, 3));
     56    /* Should see:
     57    */
     58    for (i = 0; i < 3; ++i) {
     59        for (j = 0; j < 3; ++j) {
     60            printf("%.2f ", ainv[i][j]);
     61        }
     62        printf("\n");
     63    }
     64    /* Should see:
     65       1.00 0.00 0.00
     66       0.00 1.00 0.00
     67       0.00 0.00 1.00
     68    */
     69    memset(ii[0], 0, sizeof(float32) * 3 * 3);
     70    matrixmultiply(ii, ainv, a, 3);
     71    for (i = 0; i < 3; ++i) {
     72        for (j = 0; j < 3; ++j) {
     73            printf("%.2f ", ii[i][j]);
     74        }
     75        printf("\n");
     76    }
     77
     78    /* Should see:
     79       -1
     80    */
     81    a[0][0] = 1.0;
     82    printf("%d\n", invert(ainv, a, 3));
     83
     84    ckd_free_2d((void **)a);
     85    ckd_free_2d((void **)ainv);
     86    ckd_free_2d((void **)ii);
     87
     88    return 0;
     89}
  • src/tests/libs/sphinx/test_matrix/test_solve.c

     
     1#include <stdio.h>
     2#include <string.h>
     3
     4#include "matrix.h"
     5#include "ckd_alloc.h"
     6
     7const float32 foo[3][3] = {
     8    {2, 1, 1},
     9    {1, 2, 1},
     10    {1, 1, 2}
     11};
     12float32 bar[3] = {1, 3, 1};
     13
     14int
     15main(int argc, char *argv[])
     16{
     17    float32 **a, *x;
     18    int i;
     19
     20    a = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32));
     21    memcpy(a[0], foo, sizeof(float32) * 3 * 3);
     22    x = ckd_calloc(3, sizeof(float32));
     23
     24    /* Should see:
     25       -0.25 1.75 -0.25
     26    */
     27    solve(a, bar, x, 3);
     28    for (i = 0; i < 3; ++i)
     29        printf("%.2f ", x[i]);
     30    printf("\n");
     31
     32    ckd_free_2d((void **)a);
     33    ckd_free(x);
     34
     35    return 0;
     36}
  • src/tests/libs/sphinx/test_matrix/test_determinant.c

     
     1#include <stdio.h>
     2#include <string.h>
     3
     4#include "matrix.h"
     5#include "ckd_alloc.h"
     6
     7const float32 foo[3][3] = {
     8    {2, 0.42, 1},
     9    {0.42, 2, -0.3},
     10    {1, -0.3, 2}
     11};
     12const float32 bar[3][3] = {
     13    {1, 0, 1},
     14    {0, 1, 0},
     15    {0, 0, 1}
     16};
     17
     18int
     19main(int argc, char *argv[])
     20{
     21    float32 **a;
     22
     23    a = (float32 **)ckd_calloc_2d(3, 3, sizeof(float32));
     24
     25    memcpy(a[0], foo, sizeof(float32) * 3 * 3);
     26    /* Should see 5.22 */
     27    printf("%.2f\n", determinant(a, 3));
     28
     29    /* Should see -1.0 */
     30    memcpy(a[0], bar, sizeof(float32) * 3 * 3);
     31    printf("%.2f\n", determinant(a, 3));
     32
     33    ckd_free_2d((void **)a);
     34
     35    return 0;
     36}
  • src/tests/libs/sphinx/test_matrix/_test_invert.res

     
     10
     20.75 -0.25 -0.25
     3-0.25 0.75 -0.25
     4-0.25 -0.25 0.75
     51.00 0.00 0.00
     60.00 1.00 0.00
     70.00 0.00 1.00
     80
     90.67 0.00 -0.33
     100.00 0.67 -0.33
     11-0.33 -0.33 0.83
     121.00 0.00 0.00
     130.00 1.00 0.00
     140.00 0.00 1.00
     150
  • src/tests/libs/sphinx/test_matrix/_test_solve.res

     
     1-0.25 1.75 -0.25
  • src/tests/libs/sphinx/test_ngram/100.probdef

     
     1LMCLASS scylla
     2scylla:scylla       0.4
     3karybdis:scylla     0.4
     4scooby:scylla       0.1
     5redwood:scylla      0.1
     6END scylla
     7
     8LMCLASS zero
     9zero:zero   0.3
     10oh:zero     0.7
     11END zero
  • src/tests/libs/sphinx/test_ngram/test_lm_score.c

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/100_2.arpa.DMP
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/100_2.arpa.gz
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11#ifndef LMDIR
     12#define LMDIR "."
     13#endif
     14
     15
     16void
     17run_tests(ngram_model_t *model)
     18{
     19    int32 n_used;
     20
     21    ngram_tg_score(model,
     22               ngram_wid(model, "daines"),
     23               ngram_wid(model, "huggins"),
     24               ngram_wid(model, "huggins"), &n_used);
     25    TEST_EQUAL(n_used, 2);
     26    ngram_tg_score(model,
     27               ngram_wid(model, "david"),
     28               ngram_wid(model, "david"),
     29               ngram_wid(model, "david"), &n_used);
     30    TEST_EQUAL(n_used, 1);
     31
     32    /* Apply weights. */
     33    ngram_model_apply_weights(model, 7.5, 0.5, 1.0);
     34    /* -9452 * 7.5 + log(0.5) = -77821 */
     35    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL),
     36           -77821);
     37    /* Recover original score. */
     38    TEST_EQUAL_LOG(ngram_prob(model, "daines", "huggins", "david", NULL),
     39           -9452);
     40    TEST_EQUAL_LOG(ngram_prob(model, "huggins", "david", NULL), -831);
     41
     42    /* Un-apply weights. */
     43    ngram_model_apply_weights(model, 1.0, 1.0, 1.0);
     44    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL),
     45               -9452);
     46    TEST_EQUAL_LOG(ngram_score(model, "huggins", "david", NULL), -831);
     47    /* Recover original score. */
     48    TEST_EQUAL_LOG(ngram_prob(model, "daines", "huggins", "david", NULL),
     49               -9452);
     50
     51    /* Pre-weighting, this should give the "raw" score. */
     52    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL),
     53               -9452);
     54    TEST_EQUAL_LOG(ngram_score(model, "huggins", "david", NULL), -831);
     55    /* Verify that backoff mode calculations work. */
     56    ngram_bg_score(model,
     57               ngram_wid(model, "huggins"),
     58               ngram_wid(model, "david"), &n_used);
     59    TEST_EQUAL(n_used, 2);
     60    ngram_bg_score(model,
     61               ngram_wid(model, "blorglehurfle"),
     62               ngram_wid(model, "david"), &n_used);
     63    TEST_EQUAL(n_used, 1);
     64    ngram_bg_score(model,
     65               ngram_wid(model, "david"),
     66               ngram_wid(model, "david"), &n_used);
     67    TEST_EQUAL(n_used, 1);
     68    ngram_tg_score(model,
     69               ngram_wid(model, "daines"),
     70               ngram_wid(model, "huggins"),
     71               ngram_wid(model, "david"), &n_used);
     72    TEST_EQUAL(n_used, 3);
     73}
     74
     75int
     76main(int argc, char *argv[])
     77{
     78    logmath_t *lmath;
     79    ngram_model_t *model;
     80
     81    lmath = logmath_init(1.0001, 0, 0);
     82
     83    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     84    run_tests(model);
     85    ngram_model_free(model);
     86
     87    model = ngram_model_read(NULL, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
     88    run_tests(model);
     89    ngram_model_free(model);
     90
     91    logmath_free(lmath);
     92    return 0;
     93}
  • src/tests/libs/sphinx/test_ngram/test_lm_read.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <stdlib.h>
     9#include <string.h>
     10#include <math.h>
     11
     12#ifndef LMDIR
     13#define LMDIR "."
     14#endif
     15
     16
     17static int
     18test_lm_vals(ngram_model_t *model)
     19{
     20    int32 n_used;
     21    TEST_ASSERT(model);
     22    TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
     23    TEST_EQUAL(strcmp(ngram_word(model, 0), "<UNK>"), 0);
     24    TEST_EQUAL(ngram_wid(model, "absolute"), 13);
     25    TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
     26    /* Test unigrams. */
     27    TEST_EQUAL(ngram_score(model, "<UNK>", NULL), -75346);
     28    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
     29                  NGRAM_INVALID_WID, &n_used), -75346);
     30    TEST_EQUAL(n_used, 1);
     31    TEST_EQUAL(ngram_score(model, "sphinxtrain", NULL), -64208);
     32    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
     33                  NGRAM_INVALID_WID, &n_used), -64208);
     34    TEST_EQUAL(n_used, 1);
     35    /* Test bigrams. */
     36    TEST_EQUAL(ngram_score(model, "huggins", "david", NULL), -831);
     37    /* Test trigrams. */
     38    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL), -9450);
     39    return 0;
     40}
     41
     42
     43int
     44main(int argc, char *argv[])
     45{
     46    logmath_t *lmath;
     47    ngram_model_t *model;
     48
     49    /* Initialize a logmath object to pass to ngram_read */
     50    lmath = logmath_init(1.0001, 0, 0);
     51    /* Read a language model */
     52    model = ngram_model_read(NULL, LMDIR "/100.arpa.bz2", NGRAM_ARPA, lmath);
     53    test_lm_vals(model);
     54    TEST_EQUAL(0, ngram_model_free(model));
     55
     56    /* Read a language model */
     57    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     58    test_lm_vals(model);
     59
     60    /* Test refcounting. */
     61    model = ngram_model_retain(model);
     62    TEST_EQUAL(1, ngram_model_free(model));
     63    TEST_EQUAL(ngram_score(model, "daines", "huggins", "david", NULL), -9452);
     64    TEST_EQUAL(0, ngram_model_free(model));
     65    logmath_free(lmath);
     66
     67    return 0;
     68}
  • src/tests/libs/sphinx/test_ngram/turtle.lm

     
     1Language model created by QuickLM on Wed Aug  4 14:21:56 EDT 1999
     2Carnegie Mellon University (c) 1996
     3
     4This model based on a corpus of  sentences and  words
     5The (fixed) discount mass is 0.5
     6
     7\data\
     8ngram 1=91
     9ngram 2=212
     10ngram 3=177
     11
     12\1-grams:
     13-0.9129 </s> -0.3010
     14-0.9129 <s> -0.2144
     15-2.6031 A -0.2999
     16-2.6031 AND -0.2989
     17-2.3021 ARE -0.2978
     18-2.6031 AROUND -0.2444
     19-2.3021 BACKWARD -0.2338
     20-2.9042 BACKWARDS -0.2444
     21-2.6031 BYE -0.2432
     22-2.9042 CENTIMETER -0.2444
     23-2.9042 CENTIMETERS -0.2444
     24-2.9042 CHASE -0.2989
     25-2.9042 COLOR -0.2999
     26-1.9499 DEGREES -0.2444
     27-2.6031 DISPLAY -0.2999
     28-2.9042 DO -0.2983
     29-2.9042 DOING -0.2444
     30-2.9042 EIGHT -0.2444
     31-2.9042 EIGHTEEN -0.2444
     32-2.4271 EIGHTY -0.2388
     33-2.9042 ELEVEN -0.2444
     34-2.9042 EXIT -0.2444
     35-2.9042 EXPLORE -0.2444
     36-2.6031 FIFTEEN -0.2388
     37-2.9042 FIFTY -0.2444
     38-2.9042 FIND -0.2989
     39-2.9042 FINISH -0.2444
     40-2.1260 FIVE -0.2338
     41-2.3021 FORTY -0.2407
     42-2.0011 FORWARD -0.2281
     43-2.9042 FOUR -0.2444
     44-2.9042 FOURTEEN -0.2444
     45-1.7001 GO -0.2923
     46-2.9042 GREY -0.2999
     47-2.9042 GUARD -0.2989
     48-2.4271 HALF -0.2420
     49-2.9042 HALL -0.2444
     50-2.9042 HALLWAY -0.2444
     51-2.9042 HALT -0.2444
     52-2.9042 HELLO -0.2444
     53-2.9042 HOME -0.2444
     54-2.6031 HUNDRED -0.2432
     55-2.9042 KEVIN -0.2444
     56-2.6031 LAB -0.2444
     57-2.2052 LEFT -0.2338
     58-2.9042 LISTENING -0.2444
     59-2.9042 LOST -0.2444
     60-2.3021 METER -0.2444
     61-2.0011 METERS -0.2444
     62-2.6031 MINUS -0.2983
     63-2.9042 NINE -0.2444
     64-2.9042 NINETEEN -0.2444
     65-2.4271 NINETY -0.2388
     66-2.9042 OFFICE -0.2444
     67-2.0011 ONE -0.2351
     68-2.9042 PERSON -0.2444
     69-2.4271 QUARTER -0.2444
     70-2.9042 QUARTERS -0.2444
     71-2.9042 QUIT -0.2444
     72-2.9042 READY -0.2444
     73-2.9042 REID -0.2444
     74-2.1260 RIGHT -0.2325
     75-2.9042 ROBOMAN -0.2444
     76-2.9042 ROOM -0.2444
     77-2.0591 ROTATE -0.2950
     78-2.6031 SAY -0.2994
     79-2.9042 SEBASTIAN -0.2444
     80-2.6031 SEVEN -0.2395
     81-2.9042 SEVENTEEN -0.2444
     82-2.9042 SEVENTY -0.2444
     83-2.9042 SIX -0.2444
     84-2.9042 SIXTEEN -0.2444
     85-2.9042 SIXTY -0.2444
     86-2.9042 STOP -0.2444
     87-2.4271 TEN -0.2338
     88-2.3021 THE -0.2432
     89-2.9042 THEN -0.2444
     90-2.9042 THIRTEEN -0.2444
     91-2.6031 THIRTY -0.2388
     92-2.3021 THREE -0.2370
     93-2.6031 TO -0.2420
     94-2.9042 TOM -0.2444
     95-2.2052 TURN -0.2939
     96-2.9042 TWELVE -0.2444
     97-2.9042 TWENTY -0.2444
     98-2.4271 TWO -0.2395
     99-2.9042 UNDERSTAND -0.2444
     100-2.9042 WANDER -0.2999
     101-2.9042 WHAT -0.2989
     102-2.6031 WINDOW -0.2444
     103-2.2052 YOU -0.2967
     104
     105\2-grams:
     106-2.2923 <s> AND -0.1761
     107-1.9912 <s> ARE -0.0969
     108-2.2923 <s> CENTIMETER 0.0000
     109-2.2923 <s> CENTIMETERS 0.0000
     110-2.2923 <s> CHASE 0.0000
     111-1.9912 <s> DISPLAY 0.0000
     112-2.2923 <s> DO 0.0000
     113-2.2923 <s> EIGHT 0.0000
     114-2.2923 <s> EIGHTEEN 0.0000
     115-2.2923 <s> EIGHTY -0.2218
     116-2.2923 <s> ELEVEN 0.0000
     117-2.2923 <s> EXIT 0.0000
     118-2.2923 <s> EXPLORE 0.0000
     119-2.2923 <s> FIFTEEN -0.1761
     120-2.2923 <s> FIFTY 0.0000
     121-2.2923 <s> FIND 0.0000
     122-2.2923 <s> FINISH 0.0000
     123-2.2923 <s> FIVE -0.2218
     124-1.9912 <s> FORTY 0.0000
     125-2.2923 <s> FOUR 0.0000
     126-2.2923 <s> FOURTEEN 0.0000
     127-1.0881 <s> GO 0.0000
     128-2.2923 <s> GUARD 0.0000
     129-2.2923 <s> HALF -0.1249
     130-2.2923 <s> HALL 0.0000
     131-2.2923 <s> HALLWAY 0.0000
     132-2.2923 <s> HALT 0.0000
     133-2.2923 <s> HUNDRED -0.1761
     134-2.2923 <s> KEVIN 0.0000
     135-2.2923 <s> LAB 0.0000
     136-2.2923 <s> METER 0.0000
     137-2.2923 <s> METERS 0.0000
     138-2.2923 <s> NINE 0.0000
     139-2.2923 <s> NINETEEN 0.0000
     140-2.2923 <s> NINETY -0.2218
     141-2.2923 <s> OFFICE 0.0000
     142-1.8151 <s> ONE -0.1761
     143-2.2923 <s> PERSON 0.0000
     144-2.2923 <s> QUARTER 0.0000
     145-2.2923 <s> QUIT 0.0000
     146-2.2923 <s> REID 0.0000
     147-2.2923 <s> ROBOMAN 0.0000
     148-2.2923 <s> ROOM 0.0000
     149-1.4472 <s> ROTATE 0.0000
     150-1.9912 <s> SAY 0.0000
     151-2.2923 <s> SEBASTIAN 0.0000
     152-2.2923 <s> SEVEN -0.1761
     153-2.2923 <s> SEVENTEEN 0.0000
     154-2.2923 <s> SEVENTY 0.0000
     155-2.2923 <s> SIX 0.0000
     156-2.2923 <s> SIXTEEN 0.0000
     157-2.2923 <s> SIXTY 0.0000
     158-2.2923 <s> STOP 0.0000
     159-2.2923 <s> TEN -0.2218
     160-2.2923 <s> THIRTEEN 0.0000
     161-2.2923 <s> THIRTY -0.1761
     162-1.8151 <s> THREE -0.0969
     163-2.2923 <s> TOM 0.0000
     164-1.5933 <s> TURN 0.0000
     165-2.2923 <s> TWELVE 0.0000
     166-2.2923 <s> TWENTY 0.0000
     167-2.2923 <s> TWO -0.2218
     168-2.2923 <s> WANDER 0.0000
     169-2.2923 <s> WHAT 0.0000
     170-2.2923 <s> YOU -0.2553
     171-0.6021 A COLOR 0.0000
     172-0.6021 A GREY 0.0000
     173-0.6021 AND EIGHTY -0.1249
     174-0.6021 AND THEN 0.0000
     175-0.9031 ARE LOST 0.0000
     176-0.4260 ARE YOU -0.1461
     177-0.3010 AROUND </s> -0.3010
     178-0.9031 BACKWARD </s> -0.3010
     179-0.9031 BACKWARD FIVE -0.2218
     180-0.9031 BACKWARD ONE -0.2430
     181-0.9031 BACKWARD TWO -0.1249
     182-0.3010 BACKWARDS </s> -0.3010
     183-0.6021 BYE </s> -0.3010
     184-0.6021 BYE BYE -0.1761
     185-0.3010 CENTIMETER </s> -0.3010
     186-0.3010 CENTIMETERS </s> -0.3010
     187-0.3010 CHASE THE -0.0969
     188-0.3010 COLOR WINDOW 0.0000
     189-0.3010 DEGREES </s> -0.3010
     190-0.3010 DISPLAY A 0.0000
     191-0.3010 DO YOU -0.2553
     192-0.3010 DOING </s> -0.3010
     193-0.3010 EIGHT </s> -0.3010
     194-0.3010 EIGHTEEN </s> -0.3010
     195-0.7782 EIGHTY </s> -0.3010
     196-0.4771 EIGHTY DEGREES 0.0000
     197-0.3010 ELEVEN </s> -0.3010
     198-0.3010 EXIT </s> -0.3010
     199-0.3010 EXPLORE </s> -0.3010
     200-0.6021 FIFTEEN </s> -0.3010
     201-0.6021 FIFTEEN DEGREES 0.0000
     202-0.3010 FIFTY </s> -0.3010
     203-0.3010 FIND THE -0.0969
     204-0.3010 FINISH </s> -0.3010
     205-0.7782 FIVE </s> -0.3010
     206-0.7782 FIVE DEGREES 0.0000
     207-0.7782 FIVE METERS 0.0000
     208-0.9031 FORTY </s> -0.3010
     209-0.4260 FORTY FIVE -0.1249
     210-1.2041 FORWARD </s> -0.3010
     211-1.2041 FORWARD FIVE -0.2218
     212-0.9031 FORWARD ONE -0.1761
     213-1.2041 FORWARD SEVEN -0.1761
     214-1.2041 FORWARD TEN -0.2218
     215-1.2041 FORWARD THREE -0.2430
     216-1.2041 FORWARD TWO -0.1249
     217-0.3010 FOUR </s> -0.3010
     218-0.3010 FOURTEEN </s> -0.3010
     219-0.9031 GO BACKWARD 0.0000
     220-1.5051 GO BACKWARDS 0.0000
     221-0.6021 GO FORWARD 0.0000
     222-1.5051 GO HOME 0.0000
     223-1.2041 GO TO 0.0000
     224-0.3010 GREY WINDOW 0.0000
     225-0.3010 GUARD THE -0.0969
     226-0.4771 HALF </s> -0.3010
     227-0.7782 HALF METER 0.0000
     228-0.3010 HALL </s> -0.3010
     229-0.3010 HALLWAY </s> -0.3010
     230-0.3010 HALT </s> -0.3010
     231-0.3010 HELLO </s> -0.3010
     232-0.3010 HOME </s> -0.3010
     233-0.6021 HUNDRED </s> -0.3010
     234-0.6021 HUNDRED AND -0.1761
     235-0.3010 KEVIN </s> -0.3010
     236-0.3010 LAB </s> -0.3010
     237-1.0000 LEFT </s> -0.3010
     238-1.0000 LEFT FORTY -0.0969
     239-1.0000 LEFT MINUS -0.1761
     240-1.0000 LEFT NINETY -0.1249
     241-1.0000 LEFT ONE -0.2730
     242-0.3010 LISTENING </s> -0.3010
     243-0.3010 LOST </s> -0.3010
     244-0.3010 METER </s> -0.3010
     245-0.3010 METERS </s> -0.3010
     246-0.6021 MINUS NINETY -0.1249
     247-0.6021 MINUS THIRTY -0.1761
     248-0.3010 NINE </s> -0.3010
     249-0.3010 NINETEEN </s> -0.3010
     250-0.7782 NINETY </s> -0.3010
     251-0.4771 NINETY DEGREES 0.0000
     252-0.3010 OFFICE </s> -0.3010
     253-1.2041 ONE </s> -0.3010
     254-1.2041 ONE EIGHTY -0.1249
     255-0.9031 ONE HALF 0.0000
     256-1.2041 ONE HUNDRED -0.1761
     257-0.9031 ONE METER 0.0000
     258-1.2041 ONE QUARTER 0.0000
     259-0.3010 PERSON </s> -0.3010
     260-0.3010 QUARTER </s> -0.3010
     261-0.3010 QUARTERS </s> -0.3010
     262-0.3010 QUIT </s> -0.3010
     263-0.3010 READY </s> -0.3010
     264-0.3010 REID </s> -0.3010
     265-1.0792 RIGHT </s> -0.3010
     266-1.0792 RIGHT FIFTEEN -0.1761
     267-1.0792 RIGHT FORTY -0.0969
     268-1.0792 RIGHT MINUS -0.1761
     269-1.0792 RIGHT ONE -0.2730
     270-1.0792 RIGHT TEN -0.2218
     271-0.3010 ROBOMAN </s> -0.3010
     272-0.3010 ROOM </s> -0.3010
     273-0.6690 ROTATE LEFT -0.1461
     274-0.5441 ROTATE RIGHT -0.1249
     275-0.6021 SAY BYE -0.1761
     276-0.6021 SAY HELLO 0.0000
     277-0.3010 SEBASTIAN </s> -0.3010
     278-0.6021 SEVEN </s> -0.3010
     279-0.6021 SEVEN METERS 0.0000
     280-0.3010 SEVENTEEN </s> -0.3010
     281-0.3010 SEVENTY </s> -0.3010
     282-0.3010 SIX </s> -0.3010
     283-0.3010 SIXTEEN </s> -0.3010
     284-0.3010 SIXTY </s> -0.3010
     285-0.3010 STOP </s> -0.3010
     286-0.7782 TEN </s> -0.3010
     287-0.7782 TEN DEGREES 0.0000
     288-0.7782 TEN METERS 0.0000
     289-0.4260 THE </s> -0.3010
     290-0.9031 THE LAB 0.0000
     291-0.3010 THEN </s> -0.3010
     292-0.3010 THIRTEEN </s> -0.3010
     293-0.6021 THIRTY </s> -0.3010
     294-0.6021 THIRTY DEGREES 0.0000
     295-0.9031 THREE </s> -0.3010
     296-0.9031 THREE METERS 0.0000
     297-0.9031 THREE QUARTER 0.0000
     298-0.9031 THREE QUARTERS 0.0000
     299-0.6021 TO </s> -0.3010
     300-0.6021 TO THE -0.2430
     301-0.3010 TOM </s> -0.3010
     302-1.0000 TURN AROUND 0.0000
     303-0.6990 TURN LEFT -0.2041
     304-0.6990 TURN RIGHT -0.2218
     305-0.3010 TWELVE </s> -0.3010
     306-0.3010 TWENTY </s> -0.3010
     307-0.7782 TWO </s> -0.3010
     308-0.4771 TWO METERS 0.0000
     309-0.3010 UNDERSTAND </s> -0.3010
     310-0.3010 WANDER AROUND 0.0000
     311-0.3010 WHAT ARE -0.0969
     312-0.3010 WINDOW </s> -0.3010
     313-1.0000 YOU ARE -0.2430
     314-1.0000 YOU DOING 0.0000
     315-1.0000 YOU LISTENING 0.0000
     316-1.0000 YOU READY 0.0000
     317-1.0000 YOU UNDERSTAND 0.0000
     318
     319\3-grams:
     320-0.3010 <s> AND THEN
     321-0.3010 <s> ARE YOU
     322-0.3010 <s> CENTIMETER </s>
     323-0.3010 <s> CENTIMETERS </s>
     324-0.3010 <s> CHASE THE
     325-0.3010 <s> DISPLAY A
     326-0.3010 <s> DO YOU
     327-0.3010 <s> EIGHT </s>
     328-0.3010 <s> EIGHTEEN </s>
     329-0.3010 <s> EIGHTY </s>
     330-0.3010 <s> ELEVEN </s>
     331-0.3010 <s> EXIT </s>
     332-0.3010 <s> EXPLORE </s>
     333-0.3010 <s> FIFTEEN </s>
     334-0.3010 <s> FIFTY </s>
     335-0.3010 <s> FIND THE
     336-0.3010 <s> FINISH </s>
     337-0.3010 <s> FIVE </s>
     338-0.6021 <s> FORTY </s>
     339-0.6021 <s> FORTY FIVE
     340-0.3010 <s> FOUR </s>
     341-0.3010 <s> FOURTEEN </s>
     342-0.9031 <s> GO BACKWARD
     343-1.5051 <s> GO BACKWARDS
     344-0.6021 <s> GO FORWARD
     345-1.5051 <s> GO HOME
     346-1.2041 <s> GO TO
     347-0.3010 <s> GUARD THE
     348-0.3010 <s> HALF </s>
     349-0.3010 <s> HALL </s>
     350-0.3010 <s> HALLWAY </s>
     351-0.3010 <s> HALT </s>
     352-0.3010 <s> HUNDRED </s>
     353-0.3010 <s> KEVIN </s>
     354-0.3010 <s> LAB </s>
     355-0.3010 <s> METER </s>
     356-0.3010 <s> METERS </s>
     357-0.3010 <s> NINE </s>
     358-0.3010 <s> NINETEEN </s>
     359-0.3010 <s> NINETY </s>
     360-0.3010 <s> OFFICE </s>
     361-0.7782 <s> ONE </s>
     362-0.7782 <s> ONE HALF
     363-0.7782 <s> ONE QUARTER
     364-0.3010 <s> PERSON </s>
     365-0.3010 <s> QUARTER </s>
     366-0.3010 <s> QUIT </s>
     367-0.3010 <s> REID </s>
     368-0.3010 <s> ROBOMAN </s>
     369-0.3010 <s> ROOM </s>
     370-0.6690 <s> ROTATE LEFT
     371-0.5441 <s> ROTATE RIGHT
     372-0.6021 <s> SAY BYE
     373-0.6021 <s> SAY HELLO
     374-0.3010 <s> SEBASTIAN </s>
     375-0.3010 <s> SEVEN </s>
     376-0.3010 <s> SEVENTEEN </s>
     377-0.3010 <s> SEVENTY </s>
     378-0.3010 <s> SIX </s>
     379-0.3010 <s> SIXTEEN </s>
     380-0.3010 <s> SIXTY </s>
     381-0.3010 <s> STOP </s>
     382-0.3010 <s> TEN </s>
     383-0.3010 <s> THIRTEEN </s>
     384-0.3010 <s> THIRTY </s>
     385-0.7782 <s> THREE </s>
     386-0.7782 <s> THREE QUARTER
     387-0.7782 <s> THREE QUARTERS
     388-0.3010 <s> TOM </s>
     389-1.0000 <s> TURN AROUND
     390-0.6990 <s> TURN LEFT
     391-0.6990 <s> TURN RIGHT
     392-0.3010 <s> TWELVE </s>
     393-0.3010 <s> TWENTY </s>
     394-0.3010 <s> TWO </s>
     395-0.3010 <s> WANDER AROUND
     396-0.3010 <s> WHAT ARE
     397-0.3010 <s> YOU ARE
     398-0.3010 A COLOR WINDOW
     399-0.3010 A GREY WINDOW
     400-0.3010 AND EIGHTY DEGREES
     401-0.3010 AND THEN </s>
     402-0.3010 ARE LOST </s>
     403-0.7782 ARE YOU DOING
     404-0.7782 ARE YOU LISTENING
     405-0.7782 ARE YOU READY
     406-0.3010 BACKWARD FIVE METERS
     407-0.3010 BACKWARD ONE METER
     408-0.3010 BACKWARD TWO METERS
     409-0.3010 BYE BYE </s>
     410-0.3010 CHASE THE </s>
     411-0.3010 COLOR WINDOW </s>
     412-0.6021 DISPLAY A COLOR
     413-0.6021 DISPLAY A GREY
     414-0.3010 DO YOU UNDERSTAND
     415-0.3010 EIGHTY DEGREES </s>
     416-0.3010 FIFTEEN DEGREES </s>
     417-0.3010 FIND THE </s>
     418-0.3010 FIVE DEGREES </s>
     419-0.3010 FIVE METERS </s>
     420-0.7782 FORTY FIVE </s>
     421-0.4771 FORTY FIVE DEGREES
     422-0.3010 FORWARD FIVE METERS
     423-0.6021 FORWARD ONE HALF
     424-0.6021 FORWARD ONE METER
     425-0.3010 FORWARD SEVEN METERS
     426-0.3010 FORWARD TEN METERS
     427-0.3010 FORWARD THREE METERS
     428-0.3010 FORWARD TWO METERS
     429-0.9031 GO BACKWARD </s>
     430-0.9031 GO BACKWARD FIVE
     431-0.9031 GO BACKWARD ONE
     432-0.9031 GO BACKWARD TWO
     433-0.3010 GO BACKWARDS </s>
     434-1.2041 GO FORWARD </s>
     435-1.2041 GO FORWARD FIVE
     436-0.9031 GO FORWARD ONE
     437-1.2041 GO FORWARD SEVEN
     438-1.2041 GO FORWARD TEN
     439-1.2041 GO FORWARD THREE
     440-1.2041 GO FORWARD TWO
     441-0.3010 GO HOME </s>
     442-0.6021 GO TO </s>
     443-0.6021 GO TO THE
     444-0.3010 GREY WINDOW </s>
     445-0.3010 GUARD THE </s>
     446-0.3010 HALF METER </s>
     447-0.3010 HUNDRED AND EIGHTY
     448-0.3010 LEFT FORTY FIVE
     449-0.3010 LEFT MINUS NINETY
     450-0.3010 LEFT NINETY DEGREES
     451-0.3010 LEFT ONE EIGHTY
     452-0.3010 MINUS NINETY DEGREES
     453-0.3010 MINUS THIRTY DEGREES
     454-0.3010 NINETY DEGREES </s>
     455-0.3010 ONE EIGHTY DEGREES
     456-0.6021 ONE HALF </s>
     457-0.6021 ONE HALF METER
     458-0.3010 ONE HUNDRED AND
     459-0.3010 ONE METER </s>
     460-0.3010 ONE QUARTER </s>
     461-0.3010 RIGHT FIFTEEN DEGREES
     462-0.3010 RIGHT FORTY FIVE
     463-0.3010 RIGHT MINUS THIRTY
     464-0.3010 RIGHT ONE HUNDRED
     465-0.3010 RIGHT TEN DEGREES
     466-0.7782 ROTATE LEFT FORTY
     467-0.7782 ROTATE LEFT MINUS
     468-0.7782 ROTATE LEFT ONE
     469-0.9031 ROTATE RIGHT FIFTEEN
     470-0.9031 ROTATE RIGHT MINUS
     471-0.9031 ROTATE RIGHT ONE
     472-0.9031 ROTATE RIGHT TEN
     473-0.3010 SAY BYE BYE
     474-0.3010 SAY HELLO </s>
     475-0.3010 SEVEN METERS </s>
     476-0.3010 TEN DEGREES </s>
     477-0.3010 TEN METERS </s>
     478-0.3010 THE LAB </s>
     479-0.3010 THIRTY DEGREES </s>
     480-0.3010 THREE METERS </s>
     481-0.3010 THREE QUARTER </s>
     482-0.3010 THREE QUARTERS </s>
     483-0.3010 TO THE LAB
     484-0.3010 TURN AROUND </s>
     485-0.6021 TURN LEFT </s>
     486-0.6021 TURN LEFT NINETY
     487-0.6021 TURN RIGHT </s>
     488-0.6021 TURN RIGHT FORTY
     489-0.3010 TWO METERS </s>
     490-0.3010 WANDER AROUND </s>
     491-0.3010 WHAT ARE YOU
     492-0.3010 YOU ARE LOST
     493-0.3010 YOU DOING </s>
     494-0.3010 YOU LISTENING </s>
     495-0.3010 YOU READY </s>
     496-0.3010 YOU UNDERSTAND </s>
     497
     498\end\
  • src/tests/libs/sphinx/test_ngram/test_lm_casefold.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11int
     12main(int argc, char *argv[])
     13{
     14    logmath_t *lmath;
     15    ngram_model_t *model;
     16
     17    /* Initialize a logmath object to pass to ngram_read */
     18    lmath = logmath_init(1.0001, 0, 0);
     19
     20    /* Read a language model */
     21    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     22    TEST_ASSERT(model);
     23
     24    ngram_model_casefold(model, NGRAM_UPPER);
     25
     26    TEST_EQUAL(0, strcmp("</s>", ngram_word(model, 5)));
     27    TEST_EQUAL(0, strcmp("BE", ngram_word(model, 42)));
     28    TEST_EQUAL(0, strcmp("FLOORED", ngram_word(model, 130)));
     29    TEST_EQUAL(0, strcmp("ZERO", ngram_word(model, 398)));
     30    TEST_EQUAL(0, strcmp("~", ngram_word(model, 399)));
     31
     32    ngram_model_casefold(model, NGRAM_LOWER);
     33
     34    TEST_EQUAL(0, strcmp("</s>", ngram_word(model, 5)));
     35    TEST_EQUAL(0, strcmp("be", ngram_word(model, 42)));
     36    TEST_EQUAL(0, strcmp("floored", ngram_word(model, 130)));
     37    TEST_EQUAL(0, strcmp("zero", ngram_word(model, 398)));
     38    TEST_EQUAL(0, strcmp("~", ngram_word(model, 399)));
     39
     40    ngram_model_free(model);
     41    logmath_free(lmath);
     42
     43    return 0;
     44}
  • src/tests/libs/sphinx/test_ngram/test_lm_add.c

    Property changes on: src/tests/libs/sphinx/test_ngram/test_lm_casefold.c
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11void
     12run_tests(logmath_t *lmath, ngram_model_t *model)
     13{
     14    int32 wid, score;
     15
     16    wid = ngram_model_add_word(model, "foobie", 1.0);
     17    score = ngram_score(model, "foobie", NULL);
     18    TEST_EQUAL_LOG(score, logmath_log(lmath, 1.0/400.0)); /* #unigrams */
     19
     20    wid = ngram_model_add_word(model, "quux", 0.5);
     21    score = ngram_score(model, "quux", NULL);
     22    TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0)); /* #unigrams */
     23
     24    ngram_model_apply_weights(model, 1.0, 1.0, 0.9);
     25    score = ngram_score(model, "quux", NULL);
     26    TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0*0.9 + 1.0/400.0*0.1));
     27
     28    wid = ngram_model_add_word(model, "bazbar", 0.5);
     29    score = ngram_score(model, "bazbar", NULL);
     30    TEST_EQUAL_LOG(score, logmath_log(lmath, 0.5/400.0*0.9 + 1.0/400.0*0.1));
     31}
     32
     33int
     34main(int argc, char *argv[])
     35{
     36    logmath_t *lmath;
     37    ngram_model_t *model;
     38
     39    lmath = logmath_init(1.0001, 0, 0);
     40
     41    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     42    run_tests(lmath, model);
     43    ngram_model_free(model);
     44
     45    model = ngram_model_read(NULL, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
     46    run_tests(lmath, model);
     47    ngram_model_free(model);
     48
     49    logmath_free(lmath);
     50    return 0;
     51}
  • src/tests/libs/sphinx/test_ngram/test_lm_write.c

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/100.arpa.DMP
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/100.arpa.gz
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4#include <err.h>
     5
     6#include "test_macros.h"
     7
     8#include <stdio.h>
     9#include <stdlib.h>
     10#include <string.h>
     11
     12#ifndef LMDIR
     13#define LMDIR "."
     14#endif
     15
     16
     17static int
     18test_lm_vals(ngram_model_t *model)
     19{
     20    int32 n_used;
     21
     22    TEST_ASSERT(model);
     23    TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
     24    TEST_EQUAL(strcmp(ngram_word(model, 0), "<UNK>"), 0);
     25    TEST_EQUAL(ngram_wid(model, "absolute"), 13);
     26    TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
     27    /* Test unigrams. */
     28    TEST_EQUAL_LOG(ngram_score(model, "<UNK>", NULL), -75346);
     29    TEST_EQUAL_LOG(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
     30                  NGRAM_INVALID_WID, &n_used), -75346);
     31    TEST_EQUAL(n_used, 1);
     32    TEST_EQUAL_LOG(ngram_score(model, "sphinxtrain", NULL), -64208);
     33    TEST_EQUAL_LOG(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
     34                  NGRAM_INVALID_WID, &n_used), -64208);
     35    TEST_EQUAL(n_used, 1);
     36    printf("FOO %d\n", ngram_score(model, "huggins", "david", NULL));
     37    printf("FOO %d\n", ngram_score(model, "daines", "huggins", "david", NULL));
     38    /* Test bigrams. */
     39    TEST_EQUAL_LOG(ngram_score(model, "huggins", "david", NULL), -831);
     40    /* Test trigrams. */
     41    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL), -9450);
     42    return 0;
     43}
     44
     45int
     46main(int argc, char *argv[])
     47{
     48    logmath_t *lmath;
     49    ngram_model_t *model;
     50
     51    /* Initialize a logmath object to pass to ngram_read */
     52    lmath = logmath_init(1.0001, 0, 0);
     53
     54    /* Convert ARPA to DMP */
     55    E_INFO("Converting ARPA to DMP\n");
     56    model = ngram_model_read(NULL, LMDIR "/100.arpa.bz2", NGRAM_ARPA, lmath);
     57    test_lm_vals(model);
     58    TEST_EQUAL(0, ngram_model_write(model, "100.tmp.DMP", NGRAM_DMP));
     59    ngram_model_free(model);
     60
     61    /* Convert DMP to ARPA */
     62    E_INFO("Converting DMP to ARPA\n");
     63    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     64    test_lm_vals(model);
     65    TEST_EQUAL(0, ngram_model_write(model, "100.tmp.arpa", NGRAM_ARPA));
     66    ngram_model_free(model);
     67
     68    /* Test converted DMP */
     69    E_INFO("Testing converted DMP\n");
     70    model = ngram_model_read(NULL, "100.tmp.DMP", NGRAM_DMP, lmath);
     71    test_lm_vals(model);
     72    ngram_model_free(model);
     73
     74    /* Test converted ARPA */
     75    E_INFO("Testing converted ARPA\n");
     76    model = ngram_model_read(NULL, "100.tmp.arpa", NGRAM_ARPA, lmath);
     77    test_lm_vals(model);
     78    ngram_model_free(model);
     79
     80    /* Convert DMP back to ARPA*/
     81    E_INFO("Converting ARPA back to DMP\n");
     82    model = ngram_model_read(NULL, "100.tmp.arpa", NGRAM_ARPA, lmath);
     83    test_lm_vals(model);
     84    TEST_EQUAL(0, ngram_model_write(model, "100.tmp.DMP", NGRAM_DMP));
     85    ngram_model_free(model);
     86
     87    /* Convert ARPA back to DMP */
     88    E_INFO("Converting DMP back to ARPA\n");
     89    model = ngram_model_read(NULL, "100.tmp.DMP", NGRAM_DMP, lmath);
     90    test_lm_vals(model);
     91    TEST_EQUAL(0, ngram_model_write(model, "100.tmp.arpa", NGRAM_ARPA));
     92    ngram_model_free(model);
     93
     94    logmath_free(lmath);
     95    return 0;
     96}
  • src/tests/libs/sphinx/test_ngram/test_lm_mmap.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11#ifndef LMDIR
     12#define LMDIR "."
     13#endif
     14
     15static const arg_t defn[] = {
     16    { "-mmap", ARG_BOOLEAN, "yes", "use mmap" },
     17    { "-lw", ARG_FLOAT32, "1.0", "language weight" },
     18    { "-wip", ARG_FLOAT32, "1.0", "word insertion penalty" },
     19    { "-uw", ARG_FLOAT32, "1.0", "unigram weight" },
     20    { NULL, 0, NULL, NULL }
     21};
     22
     23int
     24main(int argc, char *argv[])
     25{
     26    logmath_t *lmath;
     27    ngram_model_t *model;
     28    cmd_ln_t *config;
     29    int32 n_used;
     30
     31    /* Initialize a logmath object to pass to ngram_read */
     32    lmath = logmath_init(1.0001, 0, 0);
     33    /* Initialize a cmd_ln_t with -mmap yes */
     34    config = cmd_ln_parse_r(NULL, defn, 0, NULL, FALSE);
     35
     36    /* Read a language model (this won't mmap) */
     37    model = ngram_model_read(config, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
     38    TEST_ASSERT(model);
     39    TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
     40    TEST_EQUAL(ngram_wid(model, "absolute"), 13);
     41    TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
     42    /* Test unigrams. */
     43    TEST_EQUAL(ngram_score(model, "<UNK>", NULL), -75346);
     44    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
     45                  NGRAM_INVALID_WID, &n_used), -75346);
     46    TEST_EQUAL(n_used, 1);
     47    TEST_EQUAL(ngram_score(model, "sphinxtrain", NULL), -64208);
     48    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
     49                  NGRAM_INVALID_WID, &n_used), -64208);
     50    TEST_EQUAL(n_used, 1);
     51    /* Test bigrams. */
     52    TEST_EQUAL(ngram_score(model, "huggins", "david", NULL), -831);
     53    /* Test trigrams. */
     54    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL), -9450);
     55
     56    ngram_model_free(model);
     57
     58    /* Read a language model (this will mmap) */
     59    model = ngram_model_read(config, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     60    TEST_ASSERT(model);
     61    TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
     62    TEST_EQUAL(strcmp(ngram_word(model, 0), "<UNK>"), 0);
     63    TEST_EQUAL(ngram_wid(model, "absolute"), 13);
     64    TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
     65    /* Test unigrams. */
     66    TEST_EQUAL(ngram_score(model, "<UNK>", NULL), -75346);
     67    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
     68                  NGRAM_INVALID_WID, &n_used), -75346);
     69    TEST_EQUAL(n_used, 1);
     70    TEST_EQUAL(ngram_score(model, "sphinxtrain", NULL), -64208);
     71    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
     72                  NGRAM_INVALID_WID, &n_used), -64208);
     73    TEST_EQUAL(n_used, 1);
     74    /* Test bigrams. */
     75    TEST_EQUAL(ngram_score(model, "huggins", "david", NULL), -831);
     76    /* Test trigrams. */
     77    TEST_EQUAL(ngram_score(model, "daines", "huggins", "david", NULL), -9452);
     78
     79    ngram_model_free(model);
     80
     81    /* Test language weights on the command line. */
     82    cmd_ln_set_float32_r(config, "-lw", 2.0);
     83    cmd_ln_set_float32_r(config, "-wip", 0.5);
     84    model = ngram_model_read(config, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
     85    TEST_ASSERT(model);
     86    TEST_EQUAL(ngram_wid(model, "<UNK>"), 0);
     87    TEST_EQUAL(ngram_wid(model, "absolute"), 13);
     88    TEST_EQUAL(strcmp(ngram_word(model, 13), "absolute"), 0);
     89    /* Test unigrams. */
     90    TEST_EQUAL(ngram_score(model, "<UNK>", NULL), -75346
     91           * 2 + logmath_log(lmath, 0.5));
     92    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "<UNK>"),
     93                  NGRAM_INVALID_WID, &n_used), -75346
     94           * 2 + logmath_log(lmath, 0.5));
     95    TEST_EQUAL(n_used, 1);
     96    TEST_EQUAL(ngram_score(model, "sphinxtrain", NULL), -64208
     97           * 2 + logmath_log(lmath, 0.5));
     98    TEST_EQUAL(ngram_bg_score(model, ngram_wid(model, "sphinxtrain"),
     99                  NGRAM_INVALID_WID, &n_used), -64208
     100           * 2 + logmath_log(lmath, 0.5));
     101    TEST_EQUAL(n_used, 1);
     102    /* Test bigrams. */
     103    TEST_EQUAL(ngram_score(model, "huggins", "david", NULL),
     104           -831 * 2 + logmath_log(lmath, 0.5));
     105    /* Test trigrams. */
     106    TEST_EQUAL_LOG(ngram_score(model, "daines", "huggins", "david", NULL),
     107               -9450 * 2 + logmath_log(lmath, 0.5));
     108
     109    ngram_model_free(model);
     110
     111    logmath_free(lmath);
     112    cmd_ln_free_r(config);
     113
     114    return 0;
     115}
  • src/tests/libs/sphinx/test_ngram/test_lm_set.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <stdlib.h>
     9#include <string.h>
     10#include <math.h>
     11
     12#ifndef LMDIR
     13#define LMDIR "."
     14#endif
     15
     16
     17int
     18main(int argc, char *argv[])
     19{
     20    logmath_t *lmath;
     21    ngram_model_t *lms[3];
     22    ngram_model_t *lmset;
     23    const char *names[] = { "100", "100_2" };
     24    const char *words[] = {
     25        "<UNK>",
     26        "ROBOMAN",
     27        "libio",
     28        "sphinxtrain",
     29        "bigbird",
     30        "quuxfuzz"
     31    };
     32    const int32 n_words = sizeof(words) / sizeof(words[0]);
     33    float32 weights[] = { 0.6, 0.4 };
     34
     35    lmath = logmath_init(1.0001, 0, 0);
     36
     37    lms[0] = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     38    lms[1] = ngram_model_read(NULL, LMDIR "/100_2.arpa.DMP", NGRAM_DMP, lmath);
     39
     40    lmset = ngram_model_set_init(NULL, lms, (char **)names, NULL, 2);
     41    TEST_ASSERT(lmset);
     42    TEST_EQUAL(ngram_model_set_select(lmset, "100_2"), lms[1]);
     43    TEST_EQUAL(ngram_model_set_select(lmset, "100"), lms[0]);
     44    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     45           logmath_log10_to_log(lmath, -2.7884));
     46    TEST_EQUAL(ngram_score(lmset, "huggins", "david", NULL),
     47           logmath_log10_to_log(lmath, -0.0361));
     48    TEST_EQUAL_LOG(ngram_score(lmset, "daines", "huggins", "david", NULL),
     49               logmath_log10_to_log(lmath, -0.4105));
     50
     51    TEST_EQUAL(ngram_model_set_select(lmset, "100_2"), lms[1]);
     52    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     53           logmath_log10_to_log(lmath, -2.8192));
     54    TEST_EQUAL(ngram_score(lmset, "huggins", "david", NULL),
     55           logmath_log10_to_log(lmath, -0.1597));
     56    TEST_EQUAL_LOG(ngram_score(lmset, "daines", "huggins", "david", NULL),
     57               logmath_log10_to_log(lmath, -0.0512));
     58
     59    /* Test interpolation with default weights. */
     60    TEST_ASSERT(ngram_model_set_interp(lmset, NULL, NULL));
     61    TEST_EQUAL_LOG(ngram_score(lmset, "sphinxtrain", NULL),
     62               logmath_log(lmath,
     63                   0.5 * pow(10, -2.7884)
     64                   + 0.5 * pow(10, -2.8192)));
     65
     66    /* Test interpolation with set weights. */
     67    TEST_ASSERT(ngram_model_set_interp(lmset, names, weights));
     68    TEST_EQUAL_LOG(ngram_score(lmset, "sphinxtrain", NULL),
     69               logmath_log(lmath,
     70                   0.6 * pow(10, -2.7884)
     71                   + 0.4 * pow(10, -2.8192)));
     72
     73    /* Test switching back to selected mode. */
     74    TEST_EQUAL(ngram_model_set_select(lmset, "100_2"), lms[1]);
     75    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     76           logmath_log10_to_log(lmath, -2.8192));
     77    TEST_EQUAL(ngram_score(lmset, "huggins", "david", NULL),
     78           logmath_log10_to_log(lmath, -0.1597));
     79    TEST_EQUAL_LOG(ngram_score(lmset, "daines", "huggins", "david", NULL),
     80               logmath_log10_to_log(lmath, -0.0512));
     81
     82    /* Test interpolation with previously set weights. */
     83    TEST_ASSERT(ngram_model_set_interp(lmset, NULL, NULL));
     84    TEST_EQUAL_LOG(ngram_score(lmset, "sphinxtrain", NULL),
     85               logmath_log(lmath,
     86                   0.6 * pow(10, -2.7884)
     87                   + 0.4 * pow(10, -2.8192)));
     88
     89    /* Test interpolation with closed-vocabulary models and OOVs. */
     90    lms[2] = ngram_model_read(NULL, LMDIR "/turtle.lm", NGRAM_ARPA, lmath);
     91    TEST_ASSERT(ngram_model_set_add(lmset, lms[2], "turtle", 1.0, FALSE));
     92    TEST_EQUAL_LOG(ngram_score(lmset, "sphinxtrain", NULL),
     93               logmath_log(lmath,
     94                   0.6 * (2.0 / 3.0) * pow(10, -2.7884)
     95                   + 0.4 * (2.0 / 3.0) * pow(10, -2.8192)));
     96    ngram_model_free(lmset);
     97
     98    /* Test adding and removing language models with preserved
     99     * word ID mappings. */
     100    lms[0] = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     101    lms[1] = ngram_model_read(NULL, LMDIR "/100_2.arpa.DMP", NGRAM_DMP, lmath);
     102    lms[2] = ngram_model_read(NULL, LMDIR "/turtle.lm", NGRAM_ARPA, lmath);
     103    lmset = ngram_model_set_init(NULL, lms, (char **)names, NULL, 1);
     104    {
     105        int32 wid;
     106        wid = ngram_wid(lmset, "sphinxtrain");
     107        TEST_ASSERT(ngram_model_set_add(lmset, lms[1], "100_2", 1.0, TRUE));
     108        /* Verify that it is the same. */
     109        TEST_EQUAL(wid, ngram_wid(lmset, "sphinxtrain"));
     110        /* Now add another model and verify that its words
     111         * don't actually get added. */
     112        TEST_ASSERT(ngram_model_set_add(lmset, lms[2], "turtle", 1.0, TRUE));
     113        TEST_EQUAL(wid, ngram_wid(lmset, "sphinxtrain"));
     114        TEST_EQUAL(ngram_unknown_wid(lmset), ngram_wid(lmset, "FORWARD"));
     115        /* Remove language model, make sure this doesn't break horribly. */
     116        TEST_EQUAL(lms[1], ngram_model_set_remove(lmset, "100_2", TRUE));
     117        ngram_model_free(lms[1]);
     118        TEST_EQUAL(wid, ngram_wid(lmset, "sphinxtrain"));
     119        /* Now enable remapping of word IDs and verify that it works. */
     120        TEST_EQUAL(lms[2], ngram_model_set_remove(lmset, "turtle", TRUE));
     121        TEST_ASSERT(ngram_model_set_add(lmset, lms[2], "turtle", 1.0, FALSE));
     122        printf("FORWARD = %d\n", ngram_wid(lmset, "FORWARD"));
     123    }
     124
     125    ngram_model_free(lmset);
     126
     127    /* Now test lmctl files. */
     128    lmset = ngram_model_set_read(NULL, LMDIR "/100.lmctl", lmath);
     129    TEST_ASSERT(lmset);
     130    /* Test iterators. */
     131    {
     132        ngram_model_set_iter_t *itor;
     133        ngram_model_t *lm;
     134        char const *lmname;
     135
     136        itor = ngram_model_set_iter(lmset);
     137        TEST_ASSERT(itor);
     138        lm = ngram_model_set_iter_model(itor, &lmname);
     139        printf("1: %s\n", lmname);
     140        itor = ngram_model_set_iter_next(itor);
     141        lm = ngram_model_set_iter_model(itor, &lmname);
     142        printf("2: %s\n", lmname);
     143        itor = ngram_model_set_iter_next(itor);
     144        lm = ngram_model_set_iter_model(itor, &lmname);
     145        printf("3: %s\n", lmname);
     146        itor = ngram_model_set_iter_next(itor);
     147        TEST_EQUAL(itor, NULL);
     148    }
     149
     150    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     151           logmath_log10_to_log(lmath, -2.7884));
     152
     153    TEST_ASSERT(ngram_model_set_interp(lmset, NULL, NULL));
     154    TEST_EQUAL_LOG(ngram_score(lmset, "sphinxtrain", NULL),
     155               logmath_log(lmath,
     156                   (1.0 / 3.0) * pow(10, -2.7884)
     157                   + (1.0 / 3.0) * pow(10, -2.8192)));
     158
     159    ngram_model_set_select(lmset, "100_2");
     160    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     161           logmath_log10_to_log(lmath, -2.8192));
     162    TEST_EQUAL(ngram_score(lmset, "huggins", "david", NULL),
     163           logmath_log10_to_log(lmath, -0.1597));
     164    TEST_EQUAL_LOG(ngram_score(lmset, "daines", "huggins", "david", NULL),
     165               logmath_log10_to_log(lmath, -0.0512));
     166
     167    ngram_model_set_select(lmset, "100");
     168    TEST_EQUAL(ngram_score(lmset, "sphinxtrain", NULL),
     169           logmath_log10_to_log(lmath, -2.7884));
     170    TEST_EQUAL(ngram_score(lmset, "huggins", "david", NULL),
     171           logmath_log10_to_log(lmath, -0.0361));
     172    TEST_EQUAL_LOG(ngram_score(lmset, "daines", "huggins", "david", NULL),
     173               logmath_log10_to_log(lmath, -0.4105));
     174
     175    /* Test class probabilities. */
     176    ngram_model_set_select(lmset, "100");
     177    TEST_EQUAL_LOG(ngram_score(lmset, "scylla:scylla", NULL),
     178               logmath_log10_to_log(lmath, -2.7884) + logmath_log(lmath, 0.4));
     179    TEST_EQUAL_LOG(ngram_score(lmset, "scooby:scylla", NULL),
     180               logmath_log10_to_log(lmath, -2.7884) + logmath_log(lmath, 0.1));
     181    TEST_EQUAL_LOG(ngram_score(lmset, "apparently", "karybdis:scylla", NULL),
     182               logmath_log10_to_log(lmath, -0.5172));
     183
     184    /* Test word ID mapping. */
     185    ngram_model_set_select(lmset, "turtle");
     186    TEST_EQUAL(ngram_wid(lmset, "ROBOMAN"),
     187           ngram_wid(lmset, ngram_word(lmset, ngram_wid(lmset, "ROBOMAN"))));
     188    TEST_EQUAL(ngram_wid(lmset, "bigbird"),
     189           ngram_wid(lmset, ngram_word(lmset, ngram_wid(lmset, "bigbird"))));
     190    TEST_EQUAL(ngram_wid(lmset, "quuxfuzz"), ngram_unknown_wid(lmset));
     191    TEST_EQUAL(ngram_score(lmset, "quuxfuzz", NULL), ngram_zero(lmset));
     192    ngram_model_set_map_words(lmset, words, n_words);
     193    TEST_EQUAL(ngram_wid(lmset, "ROBOMAN"),
     194           ngram_wid(lmset, ngram_word(lmset, ngram_wid(lmset, "ROBOMAN"))));
     195    TEST_EQUAL(ngram_wid(lmset, "bigbird"),
     196           ngram_wid(lmset, ngram_word(lmset, ngram_wid(lmset, "bigbird"))));
     197    TEST_EQUAL(ngram_wid(lmset, "quuxfuzz"), 5);
     198    TEST_EQUAL(ngram_score(lmset, "quuxfuzz", NULL), ngram_zero(lmset));
     199
     200    ngram_model_free(lmset);
     201    logmath_free(lmath);
     202    return 0;
     203}
  • src/tests/libs/sphinx/test_ngram/Jamfile

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/turtle.lm.DMP
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_ngram ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_lm_class : test_lm_class.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_lm_iter : test_lm_iter.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_lm_mmap : test_lm_mmap.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     11SimpleTest test_lm_read : test_lm_read.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     12SimpleTest test_lm_recode : test_lm_recode.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     13SimpleTest test_lm_score : test_lm_score.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     14SimpleTest test_lm_set : test_lm_set.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     15SimpleTest test_lm_write : test_lm_write.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_ngram/test_lm_recode.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11#ifndef LMDIR
     12#define LMDIR "."
     13#endif
     14
     15
     16int
     17main(int argc, char *argv[])
     18{
     19    logmath_t *lmath;
     20    ngram_model_t *model;
     21
     22    /* Initialize a logmath object to pass to ngram_read */
     23    lmath = logmath_init(1.0001, 0, 0);
     24
     25    /* Read a language model */
     26    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     27    TEST_ASSERT(model);
     28
     29    ngram_model_recode(model, "iso8859-1", "utf-8");
     30    TEST_EQUAL(strcmp(ngram_word(model, 0), "<UNK>"), 0);
     31    ngram_model_free(model);
     32    logmath_free(lmath);
     33
     34    return 0;
     35}
  • src/tests/libs/sphinx/test_ngram/test_macros.h

     
     1#include <stdlib.h>
     2#include <stdio.h>
     3#include <math.h>
     4
     5#include "logmath.h"
     6
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_ngram/100.lmctl

     
     1{ 100.probdef }
     2100.arpa.DMP 100 { scylla zero }
     3100_2.arpa.gz 100_2
     4turtle.lm turtle
  • src/tests/libs/sphinx/test_ngram/test_lm_iter.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <stdlib.h>
     9#include <string.h>
     10#include <math.h>
     11
     12#ifndef LMDIR
     13#define LMDIR "."
     14#endif
     15
     16int
     17main(int argc, char *argv[])
     18{
     19    logmath_t *lmath;
     20    ngram_model_t *model;
     21    ngram_iter_t *itor;
     22    int i;
     23
     24    /* Initialize a logmath object to pass to ngram_read */
     25    lmath = logmath_init(1.0001, 0, 0);
     26    /* Read a language model */
     27    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     28    TEST_ASSERT(model);
     29
     30    for (i = 0, itor = ngram_model_mgrams(model, 0);
     31         itor; ++i, itor = ngram_iter_next(itor)) {
     32        int32 score, bowt;
     33        int32 const *wids = ngram_iter_get(itor, &score, &bowt);
     34
     35        /*
     36        printf("%.4f %s %.4f\n",
     37               logmath_log_to_log10(lmath, score),
     38               ngram_word(model, wids[0]),
     39               logmath_log_to_log10(lmath, bowt));
     40        */
     41
     42        if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "<UNK>"));
     43        if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
     44    }
     45
     46    for (i = 0, itor = ngram_model_mgrams(model, 1);
     47         itor; ++i, itor = ngram_iter_next(itor)) {
     48        int32 score, bowt;
     49        int32 const *wids = ngram_iter_get(itor, &score, &bowt);
     50
     51        /*
     52        printf("%.4f %s %s %.4f\n",
     53               logmath_log_to_log10(lmath, score),
     54               ngram_word(model, wids[0]),
     55               ngram_word(model, wids[1]),
     56               logmath_log_to_log10(lmath, bowt));
     57        */
     58
     59        /* FIXME: These tests are not sufficient - actually we
     60         * need to make sure all word IDs line up
     61         * correctly. */
     62        if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
     63        if (i == 0) TEST_EQUAL(wids[1], ngram_wid(model, "an"));
     64        if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
     65        if (i == 1) TEST_EQUAL(wids[1], ngram_wid(model, "going"));
     66    }
     67
     68    for (i = 0, itor = ngram_model_mgrams(model, 2);
     69         itor; ++i, itor = ngram_iter_next(itor)) {
     70        int32 score, bowt;
     71        int32 const *wids = ngram_iter_get(itor, &score, &bowt);
     72
     73        /*
     74        printf("%.4f %s %s %s\n",
     75               logmath_log_to_log10(lmath, score),
     76               ngram_word(model, wids[0]),
     77               ngram_word(model, wids[1]),
     78               ngram_word(model, wids[2]));
     79        */
     80
     81        /* FIXME: These tests are not sufficient - actually we
     82         * need to make sure all word IDs line up
     83         * correctly. */
     84        if (i == 0) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
     85        if (i == 0) TEST_EQUAL(wids[1], ngram_wid(model, "an"));
     86        if (i == 0) TEST_EQUAL(wids[2], ngram_wid(model, "r"));
     87        if (i == 1) TEST_EQUAL(wids[0], ngram_wid(model, "'s"));
     88        if (i == 1) TEST_EQUAL(wids[1], ngram_wid(model, "going"));
     89        if (i == 1) TEST_EQUAL(wids[2], ngram_wid(model, "so"));
     90    }
     91
     92    {
     93        ngram_iter_t *itor2, *itor3;
     94        int32 score, bowt;
     95        int32 const *wids;
     96
     97        /* Test the boundary condition - successors of last 1-gram. */
     98        itor = ngram_ng_iter(model, ngram_model_get_counts(model)[0] - 1,
     99                     NULL, 0);
     100        wids = ngram_iter_get(itor, &score, &bowt);
     101        printf("%.4f %s %.4f\n",
     102               logmath_log_to_log10(lmath, score),
     103               ngram_word(model, wids[0]),
     104               logmath_log_to_log10(lmath, bowt));
     105        TEST_EQUAL(wids[0], ngram_wid(model, "~"));
     106
     107        for (itor2 = ngram_iter_successors(itor);
     108             itor2; itor2 = ngram_iter_next(itor2)) {
     109            wids = ngram_iter_get(itor2, &score, &bowt);
     110            printf("%.4f %s %s %.4f\n",
     111                   logmath_log_to_log10(lmath, score),
     112                   ngram_word(model, wids[0]),
     113                   ngram_word(model, wids[1]),
     114                   logmath_log_to_log10(lmath, bowt));
     115            TEST_EQUAL(wids[0], ngram_wid(model, "~"));
     116            TEST_EQUAL(wids[1], ngram_wid(model, "eleven"));
     117        }
     118        itor2 = ngram_iter_successors(itor);
     119        for (itor3 = ngram_iter_successors(itor2);
     120             itor3; itor3 = ngram_iter_next(itor3)) {
     121            wids = ngram_iter_get(itor3, &score, &bowt);
     122            printf("%.4f %s %s %s\n",
     123                   logmath_log_to_log10(lmath, score),
     124                   ngram_word(model, wids[0]),
     125                   ngram_word(model, wids[1]),
     126                   ngram_word(model, wids[2]));
     127            TEST_EQUAL(wids[0], ngram_wid(model, "~"));
     128            TEST_EQUAL(wids[1], ngram_wid(model, "eleven"));
     129            TEST_EQUAL(wids[2], ngram_wid(model, "per"));
     130        }
     131        ngram_iter_free(itor2);
     132        ngram_iter_free(itor);
     133    }
     134   
     135    TEST_EQUAL(0, ngram_model_free(model));
     136    logmath_free(lmath);
     137
     138    return 0;
     139}
  • src/tests/libs/sphinx/test_ngram/test_lm_class.c

     
     1#include <ngram_model.h>
     2#include <logmath.h>
     3#include <strfuncs.h>
     4
     5#include "test_macros.h"
     6
     7#include <stdio.h>
     8#include <string.h>
     9#include <math.h>
     10
     11#ifndef LMDIR
     12#define LMDIR "."
     13#endif
     14
     15void
     16run_tests(logmath_t *lmath, ngram_model_t *model)
     17{
     18    int32 rv, i;
     19
     20    TEST_ASSERT(model);
     21
     22    TEST_EQUAL(ngram_wid(model, "scylla"), 285);
     23    TEST_EQUAL(strcmp(ngram_word(model, 285), "scylla"), 0);
     24
     25    rv = ngram_model_read_classdef(model, LMDIR "/100.probdef");
     26    TEST_EQUAL(rv, 0);
     27
     28    /* Verify that class word IDs remain the same. */
     29    TEST_EQUAL(ngram_wid(model, "scylla"), 285);
     30    TEST_EQUAL(strcmp(ngram_word(model, 285), "scylla"), 0);
     31
     32    /* Verify in-class word IDs. */
     33    TEST_EQUAL(ngram_wid(model, "scylla:scylla"), 0x80000000 | 400);
     34
     35    /* Verify in-class and out-class unigram scores. */
     36    TEST_EQUAL_LOG(ngram_score(model, "scylla:scylla", NULL),
     37               logmath_log10_to_log(lmath, -2.7884) + logmath_log(lmath, 0.4));
     38    TEST_EQUAL_LOG(ngram_score(model, "scooby:scylla", NULL),
     39               logmath_log10_to_log(lmath, -2.7884) + logmath_log(lmath, 0.1));
     40    TEST_EQUAL_LOG(ngram_score(model, "scylla", NULL),
     41               logmath_log10_to_log(lmath, -2.7884));
     42    TEST_EQUAL_LOG(ngram_score(model, "oh:zero", NULL),
     43               logmath_log10_to_log(lmath, -1.9038) + logmath_log(lmath, 0.7));
     44    TEST_EQUAL_LOG(ngram_score(model, "zero", NULL),
     45               logmath_log10_to_log(lmath, -1.9038));
     46
     47    /* Verify class bigram scores. */
     48    TEST_EQUAL_LOG(ngram_score(model, "scylla", "on", NULL),
     49               logmath_log10_to_log(lmath, -1.2642));
     50    TEST_EQUAL_LOG(ngram_score(model, "scylla:scylla", "on", NULL),
     51               logmath_log10_to_log(lmath, -1.2642) + logmath_log(lmath, 0.4));
     52    TEST_EQUAL_LOG(ngram_score(model, "apparently", "scylla", NULL),
     53               logmath_log10_to_log(lmath, -0.5172));
     54    TEST_EQUAL_LOG(ngram_score(model, "apparently", "karybdis:scylla", NULL),
     55               logmath_log10_to_log(lmath, -0.5172));
     56    TEST_EQUAL_LOG(ngram_score(model, "apparently", "scooby:scylla", NULL),
     57               logmath_log10_to_log(lmath, -0.5172));
     58
     59    /* Verify class trigram scores. */
     60    TEST_EQUAL_LOG(ngram_score(model, "zero", "be", "will", NULL),
     61               logmath_log10_to_log(lmath, -0.5725));
     62    TEST_EQUAL_LOG(ngram_score(model, "oh:zero", "be", "will", NULL),
     63               logmath_log10_to_log(lmath, -0.5725) + logmath_log(lmath, 0.7));
     64    TEST_EQUAL_LOG(ngram_score(model, "should", "variance", "zero", NULL),
     65               logmath_log10_to_log(lmath, -0.9404));
     66    TEST_EQUAL_LOG(ngram_score(model, "should", "variance", "zero:zero", NULL),
     67               logmath_log10_to_log(lmath, -0.9404));
     68
     69    /* Add words to classes. */
     70    rv = ngram_model_add_class_word(model, "scylla", "scrappy:scylla", 1.0);
     71    TEST_ASSERT(rv >= 0);
     72    TEST_EQUAL(ngram_wid(model, "scrappy:scylla"), 0x80000196);
     73    TEST_EQUAL_LOG(ngram_score(model, "scrappy:scylla", NULL),
     74               logmath_log10_to_log(lmath, -2.7884) + logmath_log(lmath, 0.2));
     75    printf("scrappy:scylla %08x %d %f\n",
     76           ngram_wid(model, "scrappy:scylla"),
     77           ngram_score(model, "scrappy:scylla", NULL),
     78           logmath_exp(lmath, ngram_score(model, "scrappy:scylla", NULL)));
     79    /* Add a lot of words to a class. */
     80    for (i = 0; i < 129; ++i) {
     81        char word[32];
     82        sprintf(word, "%d:scylla", i);
     83        rv = ngram_model_add_class_word(model, "scylla", word, 1.0);
     84        printf("%s %08x %d %f\n", word,
     85               ngram_wid(model, word),
     86               ngram_score(model, word, NULL),
     87               logmath_exp(lmath, ngram_score(model, word, NULL)));
     88        TEST_ASSERT(rv >= 0);
     89        TEST_EQUAL(ngram_wid(model, word), 0x80000197 + i);
     90    }
     91
     92    /* Add a new class. */
     93    {
     94        const char *words[] = { "blatz:foobie", "hurf:foobie" };
     95        float32 weights[] = { 0.6, 0.4 };
     96        int32 foobie_prob;
     97        rv = ngram_model_add_class(model, "[foobie]", 1.0,
     98                       words, weights, 2);
     99        TEST_ASSERT(rv >= 0);
     100        foobie_prob = ngram_score(model, "[foobie]", NULL);
     101        TEST_EQUAL_LOG(ngram_score(model, "blatz:foobie", NULL),
     102                   foobie_prob + logmath_log(lmath, 0.6));
     103        TEST_EQUAL_LOG(ngram_score(model, "hurf:foobie", NULL),
     104                   foobie_prob + logmath_log(lmath, 0.4));
     105    }
     106}
     107
     108int
     109main(int argc, char *argv[])
     110{
     111    logmath_t *lmath;
     112    ngram_model_t *model;
     113
     114    lmath = logmath_init(1.0001, 0, 0);
     115
     116    model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath);
     117    run_tests(lmath, model);
     118    ngram_model_free(model);
     119
     120    model = ngram_model_read(NULL, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath);
     121    run_tests(lmath, model);
     122    ngram_model_free(model);
     123
     124    logmath_free(lmath);
     125
     126    return 0;
     127}
  • src/tests/libs/sphinx/test_string/_string_join.test

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: src/tests/libs/sphinx/test_ngram/100.arpa.bz2
    ___________________________________________________________________
    Added: svn:mime-type
       + application/octet-stream
    
     
     1#!/bin/sh
     2exec ./strtest string_join
  • src/tests/libs/sphinx/test_string/_fread_line.test

    Property changes on: src/tests/libs/sphinx/test_string/_string_join.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2exec ./strtest fread_line
  • src/tests/libs/sphinx/test_string/_fread_line.txt

    Property changes on: src/tests/libs/sphinx/test_string/_fread_line.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1Hello world!
     2123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456
     3All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy. 
  • src/tests/libs/sphinx/test_string/_str2words.test

     
     1#!/bin/sh
     2exec ./strtest str2words
  • src/tests/libs/sphinx/test_string/Jamfile

    Property changes on: src/tests/libs/sphinx/test_string/_str2words.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_string ;
     2
     3SetSubDirSupportedPlatforms $(HAIKU_BONE_COMPATIBLE_PLATFORMS) ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8AddResources strtest : strtest.rdef ;
     9AddResources test_atof : test_atof.rdef ;
     10
     11SimpleTest strtest : strtest.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     12
     13SimpleTest test_atof : test_atof.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     14
  • src/tests/libs/sphinx/test_string/_nextword.test

     
     1#!/bin/sh
     2exec ./strtest nextword
  • src/tests/libs/sphinx/test_string/strtest.rdef

    Property changes on: src/tests/libs/sphinx/test_string/_nextword.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1resource app_signature "application/x-vnd.Haiku-strtest";
     2
     3resource app_version
     4{
     5    major = 0,
     6    middle = 6,
     7    minor = 0,
     8    variety = B_APPV_FINAL,
     9    internal = 0,
     10    short_info = "strtest",
     11    long_info  = "strtest from CMU sphinxbase-0.6"
     12};
     13
     14resource app_flags B_MULTIPLE_LAUNCH | B_ARGV_ONLY | B_BACKGROUND_APP ;
  • src/tests/libs/sphinx/test_string/test_atof.rdef

     
     1resource app_signature "application/x-vnd.Haiku-test_atof";
     2
     3resource app_version
     4{
     5    major = 0,
     6    middle = 6,
     7    minor = 0,
     8    variety = B_APPV_BETA,
     9    internal = 0,
     10    short_info = "test_atof",
     11    long_info  = "test_atof from CMU sphinxbase-0.6"
     12};
     13
     14resource app_flags B_MULTIPLE_LAUNCH | B_ARGV_ONLY | B_BACKGROUND_APP ;
  • src/tests/libs/sphinx/test_string/strtest.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2
     3#include <stdio.h>
     4#include <string.h>
     5
     6#include "strfuncs.h"
     7#include "pio.h"
     8#include "ckd_alloc.h"
     9
     10#ifndef TESTDATADIR
     11#define TESTDATADIR "."
     12#endif
     13
     14
     15int
     16main(int argc, char *argv[])
     17{
     18    if (argc < 2)
     19        return 1;
     20
     21    if (!strcmp(argv[1], "string_join")) {
     22        char *foo = string_join("bar", "baz", "quux", NULL);
     23        if (strcmp(foo, "barbazquux") != 0) {
     24            printf("%s != barbazquux\n", foo);
     25            return 1;
     26        }
     27        foo = string_join("hello", NULL);
     28        if (strcmp(foo, "hello") != 0) {
     29            printf("%s != hello\n", foo);
     30            return 1;
     31        }
     32        return 0;
     33    }
     34    else if (!strcmp(argv[1], "fread_line")) {
     35        FILE *fp = fopen(TESTDATADIR "/_fread_line.txt", "r");
     36        char *line;
     37        size_t len;
     38
     39        if (fp == NULL) {
     40            perror("Failed to open " TESTDATADIR "/_fread_line.txt");
     41            return 1;
     42        }
     43        line = fread_line(fp, &len);
     44        printf("len = %d orig = %ld\n", (int32)len,
     45               strlen("Hello world!\n"));
     46        if (strcmp(line, "Hello world!\n") != 0) {
     47            printf("'%s' != 'Hello world!\\n'\n", line);
     48            return 1;
     49        }
     50        ckd_free(line);
     51        line = fread_line(fp, &len);
     52        /* A line of exactly 127 characters. */
     53        printf("len = %d orig = %ld\n", (int32)len,
     54               strlen("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n"));
     55        if (strcmp(line, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\n") != 0) {
     56            printf("'%s' != '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456\\n'\n", line);
     57            return 1;
     58        }
     59        ckd_free(line);
     60        /* A very long line. */
     61        line = fread_line(fp, &len);
     62        printf("len = %d orig = %ld\n", (int32)len,
     63               strlen("All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  \n"));
     64        if (strcmp(line, "All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  \n") != 0) {
     65            printf("'%s' != 'All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  All work and no play makes Jack a very dull boy.  \\n'\n", line);
     66            return 1;
     67        }
     68        ckd_free(line);
     69        line = fread_line(fp, &len);
     70        if (line != NULL) {
     71            printf("%p != NULL\n", line);
     72            return 1;
     73        }
     74    }
     75    else if (!strcmp(argv[1], "string_trim")) {
     76        char *foo = ckd_salloc("\t foo bar baz  \n");
     77        string_trim(foo, STRING_BOTH);
     78        if (strcmp(foo, "foo bar baz") != 0) {
     79            printf("'%s' != 'foo bar baz'\n", foo);
     80            return 1;
     81        }
     82        string_trim(foo, STRING_BOTH);
     83        if (strcmp(foo, "foo bar baz") != 0) {
     84            printf("'%s' != 'foo bar baz'\n", foo);
     85            return 1;
     86        }
     87        strcpy(foo, "foo\nbar\n\n");
     88        string_trim(foo, STRING_END);
     89        if (strcmp(foo, "foo\nbar") != 0) {
     90            printf("'%s' != 'foo\\nbar'\n", foo);
     91            return 1;
     92        }
     93        strcpy(foo, " \t \t foobar\n");
     94        string_trim(foo, STRING_START);
     95        if (strcmp(foo, "foobar\n") != 0) {
     96            printf("'%s' != 'foobar\\n'\n", foo);
     97            return 1;
     98        }
     99    }
     100    else if (!strcmp(argv[1], "str2words")) {
     101        char *line = strdup("    foo bar baz argh");
     102        char **words;
     103        int n;
     104
     105        n = str2words(line, NULL, 0);
     106        if (n != 4) {
     107            printf("%d != 4\n", n);
     108            return 1;
     109        }
     110        words = ckd_calloc(n, sizeof(*words));
     111        n = str2words(line, words, n);
     112        if (n != 4) {
     113            printf("%d != 4\n", n);
     114            return 1;
     115        }
     116        if (strcmp(words[0], "foo") != 0
     117            || strcmp(words[1], "bar") != 0
     118            || strcmp(words[2], "baz") != 0
     119            || strcmp(words[3], "argh") != 0) {
     120            printf("%s, %s, %s, %s != foo, bar, baz, argh\n",
     121                   words[0], words[1], words[2], words[3]);
     122            return 1;
     123        }
     124        return 0;
     125    }
     126    else if (!strcmp(argv[1], "nextword")) {
     127        char *line = strdup(" \tfoo bar\nbaz argh");
     128        char *word;
     129        const char *delim = " \t\n";
     130        char delimfound;
     131        int n;
     132
     133        n = nextword(line, delim, &word, &delimfound);
     134        if (strcmp(word, "foo") != 0) {
     135            printf("%s != foo\n", word);
     136            return 1;
     137        }
     138        if (delimfound != ' ') {
     139            printf("didn't find ' '\n");
     140            return 1;
     141        }
     142        word[n] = delimfound;
     143        line = word + n;
     144        n = nextword(line, delim, &word, &delimfound);
     145        if (strcmp(word, "bar") != 0) {
     146            printf("%s != bar\n", word);
     147            return 1;
     148        }
     149        if (delimfound != '\n') {
     150            printf("didn't find '\\n'\n");
     151            return 1;
     152        }
     153        word[n] = delimfound;
     154        line = word + n;
     155        n = nextword(line, delim, &word, &delimfound);
     156        if (strcmp(word, "baz") != 0) {
     157            printf("%s != baz\n", word);
     158            return 1;
     159        }
     160        if (delimfound != ' ') {
     161            printf("didn't find ' '\n");
     162            return 1;
     163        }
     164        word[n] = delimfound;
     165        line = word + n;
     166        n = nextword(line, delim, &word, &delimfound);
     167        if (strcmp(word, "argh") != 0) {
     168            printf("%s != argh\n", word);
     169            return 1;
     170        }
     171        if (delimfound != '\0') {
     172            printf("didn't find NUL\n");
     173            return 1;
     174        }
     175        word[n] = delimfound;
     176        line = word + n;
     177        n = nextword(line, delim, &word, &delimfound);
     178        if (n != -1) {
     179            printf("didn't get -1 at end of string\n");
     180        }
     181
     182        line = strdup("FOO!");
     183        n = nextword(line, delim, &word, &delimfound);
     184        if (strcmp(word, "FOO!") != 0) {
     185            printf("%s != FOO!\n", word);
     186            return 1;
     187        }
     188        if (delimfound != '\0') {
     189            printf("didn't find NUL\n");
     190            return 1;
     191        }
     192
     193        return 0;
     194    }
     195    return 0;
     196}
  • src/tests/libs/sphinx/test_string/test_atof.c

     
     1#include <stdio.h>
     2#include <string.h>
     3#include <math.h>
     4#include <locale.h>
     5#include <stdlib.h>
     6
     7#include "strfuncs.h"
     8
     9int
     10main(int argc, char *argv[])
     11{
     12    double foo;
     13
     14    /* Ensure that it's really locale-independent. */
     15    if (setlocale(LC_ALL, "fr_CA.UTF-8") == NULL)
     16        fprintf(stderr, "Note: setlocale(LC_ALL, fr_CA.UTF-8) failed\n");
     17   
     18    foo = atof("1.5324523524523423");
     19    printf("atof(): 1.5324523524523423 %f\n", foo);
     20
     21    foo = atof_c("1.5324523524523423");
     22    printf("1.5324523524523423 %f\n", foo);
     23    if (fabs(foo - 1.532) > 0.01)
     24        return 1;
     25
     26    foo = atof_c("5e-3");
     27    printf("5e-3 %f\n", foo);
     28    if (fabs(foo - 0.005) > 0.01)
     29        return 1;
     30
     31    foo = atof_c("1.2e+2");
     32    printf("1.2e+2 %f\n", foo);
     33    if (fabs(foo - 120.0) > 0.01)
     34        return 1;
     35
     36    foo = atof_c("1e-80");
     37    printf("1e-80 %g\n", foo);
     38
     39    return 0;
     40}
  • src/tests/libs/sphinx/test_string/_string_trim.test

     
     1#!/bin/sh
     2exec ./strtest string_trim
  • src/tests/libs/sphinx/test_thread/test_event.c

    Property changes on: src/tests/libs/sphinx/test_string/_string_trim.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#include <stdio.h>
     2#include <sbthread.h>
     3#include <err.h>
     4
     5int
     6worker_main(sbthread_t *th)
     7{
     8    sbevent_t *cond;
     9
     10    cond = sbthread_arg(th);
     11
     12    /* Get the first signal. */
     13    sbevent_wait(cond, -1, -1);
     14    E_INFO("Got signal\n");
     15
     16    /* Now wait a while and exit. */
     17    sbevent_wait(cond, 1, 500*1000*1000);
     18    return 0;
     19}
     20
     21int
     22main(int argc, char *argv[])
     23{
     24    sbthread_t *worker;
     25    sbevent_t *cond;
     26
     27    cond = sbevent_init();
     28    worker = sbthread_start(NULL, worker_main, cond);
     29
     30    E_INFO("Signalling condition\n");
     31    sbevent_signal(cond);
     32
     33    E_INFO("Waiting (about 1.5 sec) for thread termination\n");
     34    sbthread_free(worker);
     35    sbevent_free(cond);
     36    return 0;
     37}
  • src/tests/libs/sphinx/test_thread/test_tls_log.c

     
     1#include <string.h>
     2#include <stdio.h>
     3#include <sbthread.h>
     4#include <strfuncs.h>
     5#include <fe.h>
     6#include <ckd_alloc.h>
     7#include <err.h>
     8
     9#include "test_macros.h"
     10
     11#ifndef TESTDATADIR
     12#define TESTDATADIR "../regression"
     13#endif
     14
     15
     16static const arg_t fe_args[] = {
     17    waveform_to_cepstral_command_line_macro(),
     18    { NULL, 0, NULL, NULL }
     19};
     20
     21
     22static int
     23process(sbthread_t *th)
     24{
     25    FILE *raw, *logfh;
     26    int16 *buf;
     27    mfcc_t **cepbuf;
     28    size_t nsamps;
     29    fe_t *fe;
     30    long fsize;
     31    int32 nfr;
     32    char outfile[16];
     33
     34    sprintf(outfile, "%03ld.log", (long)sbthread_arg(th));
     35    if ((logfh = fopen(outfile, "w")) == NULL)
     36        return -1;
     37    err_set_logfp(logfh);
     38    if ((fe = fe_init_auto_r(sbthread_config(th))) == NULL)
     39        return -1;
     40    if ((raw = fopen(TESTDATADIR "/chan3.raw", "rb")) == NULL)
     41        return -1;
     42    fseek(raw, 0, SEEK_END);
     43    fsize = ftell(raw);
     44    fseek(raw, 0, SEEK_SET);
     45    buf = ckd_malloc(fsize);
     46    fread(buf, 1, fsize, raw);
     47    nsamps = fsize / 2;
     48
     49    fe_process_utt(fe, buf, nsamps, &cepbuf, &nfr);
     50    E_INFO("nfr = %d\n", nfr);
     51    fe_free_2d(cepbuf);
     52    ckd_free(buf);
     53    fclose(raw);
     54    fe_free(fe);
     55    fclose(logfh);
     56
     57    return 0;
     58}
     59
     60int
     61main(int argc, char *argv[])
     62{
     63    sbthread_t *threads[10];
     64    cmd_ln_t *config;
     65    int i;
     66
     67    E_INFO("Processing chan3.raw in 10 threads\n");
     68    if ((config = cmd_ln_parse_r(NULL, fe_args, 0, NULL, FALSE)) == NULL)
     69        return -1;
     70    for (i = 0; i < 10; ++i) {
     71        config = cmd_ln_retain(config);
     72        threads[i] = sbthread_start(config, process, (void *)(long)i);
     73    }
     74    for (i = 0; i < 10; ++i) {
     75        int rv;
     76        rv = sbthread_wait(threads[i]);
     77        E_INFO("Thread %d exited with status %d\n", i, rv);
     78        sbthread_free(threads[i]);
     79    }
     80    /* Now check to make sure they all created logfiles with the
     81     * correct contents. */
     82    for (i = 0; i < 10; ++i) {
     83        char logfile[16], line[256];
     84        FILE *logfh;
     85
     86        sprintf(logfile, "%03d.log", i);
     87        TEST_ASSERT(logfh = fopen(logfile, "r"));
     88        while (fgets(line, sizeof(line), logfh)) {
     89            string_trim(line, STRING_BOTH);
     90            printf("%s: |%s|\n", logfile, line);
     91            TEST_EQUAL(0, strcmp(line, "INFO: test_tls_log.c(44): nfr = 1436"));
     92        }
     93        fclose(logfh);
     94    }
     95    cmd_ln_free_r(config);
     96    return 0;
     97}
  • src/tests/libs/sphinx/test_thread/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_thread ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_event : test_event.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_msgq : test_msgq.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a  ;
     10SimpleTest test_thread : test_thread.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a  ;
     11SimpleTest test_tls_log : test_tls_log.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a  ;
  • src/tests/libs/sphinx/test_thread/test_msgq.c

     
     1#include <stdio.h>
     2#include <sbthread.h>
     3#include <err.h>
     4
     5int
     6worker_main(sbthread_t *th)
     7{
     8    sbmsgq_t *msgq;
     9    void *data;
     10    size_t len;
     11
     12    msgq = sbthread_msgq(th);
     13    while ((data = sbmsgq_wait(msgq, &len, -1, -1)) != NULL) {
     14        int msg = *(int *)data;
     15        E_INFO("Got message: %d\n", msg);
     16        if (msg == 32)
     17            break;
     18    }
     19
     20    return 0;
     21}
     22
     23int
     24main(int argc, char *argv[])
     25{
     26    sbthread_t *worker;
     27    int i;
     28   
     29    worker = sbthread_start(NULL, worker_main, NULL);
     30    for (i = 0; i <= 32; ++i) {
     31        int ii[128];
     32        E_INFO("Sending message: %d\n", i);
     33        ii[0] = i;
     34        if (sbthread_send(worker, sizeof(ii), &ii) < 0) {
     35            E_ERROR("sbthread_send failed\n");
     36            return 1;
     37        }
     38    }
     39    sbthread_free(worker);
     40
     41    return 0;
     42}
  • src/tests/libs/sphinx/test_thread/test_thread.c

     
     1#include <stdio.h>
     2#include <sbthread.h>
     3#include <fe.h>
     4#include <ckd_alloc.h>
     5#include <err.h>
     6
     7#ifndef TESTDATADIR
     8#define TESTDATADIR "../regression"
     9#endif
     10
     11static const arg_t fe_args[] = {
     12    waveform_to_cepstral_command_line_macro(),
     13    { NULL, 0, NULL, NULL }
     14};
     15
     16static int
     17process(sbthread_t *th)
     18{
     19    FILE *raw;
     20    int16 *buf;
     21    mfcc_t **cepbuf;
     22    size_t nsamps;
     23    fe_t *fe;
     24    long fsize;
     25    int32 nfr;
     26
     27    if ((fe = fe_init_auto_r(sbthread_config(th))) == NULL)
     28        return -1;
     29
     30    if ((raw = fopen(TESTDATADIR "/chan3.raw", "rb")) == NULL)
     31        return -1;
     32    fseek(raw, 0, SEEK_END);
     33    fsize = ftell(raw);
     34    fseek(raw, 0, SEEK_SET);
     35    buf = ckd_malloc(fsize);
     36    fread(buf, 1, fsize, raw);
     37    nsamps = fsize / 2;
     38
     39    fe_process_utt(fe, buf, nsamps, &cepbuf, &nfr);
     40    E_INFO("nfr = %d\n", nfr);
     41    fe_free_2d(cepbuf);
     42    ckd_free(buf);
     43    fclose(raw);
     44    fe_free(fe);
     45
     46    return 0;
     47}
     48
     49int
     50main(int argc, char *argv[])
     51{
     52    sbthread_t *threads[10];
     53    cmd_ln_t *config;
     54    int i;
     55
     56    E_INFO("Processing chan3.raw in 10 threads\n");
     57    if ((config = cmd_ln_parse_r(NULL, fe_args, 0, NULL, FALSE)) == NULL)
     58        return -1;
     59    for (i = 0; i < 10; ++i) {
     60        config = cmd_ln_retain(config);
     61        threads[i] = sbthread_start(config, process, NULL);
     62    }
     63    for (i = 0; i < 10; ++i) {
     64        int rv;
     65        rv = sbthread_wait(threads[i]);
     66        E_INFO("Thread %d exited with status %d\n", i, rv);
     67        sbthread_free(threads[i]);
     68    }
     69    cmd_ln_free_r(config);
     70    return 0;
     71}
  • src/tests/libs/sphinx/test_thread/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     7#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     8#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     9#define LOG_EPSILON 20
     10#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx ;
     2
     3#SubInclude HAIKU_TOP src tests libs sphinx sphinx_adtools ;
     4SubInclude HAIKU_TOP src tests libs sphinx sphinx_cepview ;
     5SubInclude HAIKU_TOP src tests libs sphinx sphinx_fe ;
     6SubInclude HAIKU_TOP src tests libs sphinx sphinx_lmtools ;
     7#SubInclude HAIKU_TOP src tests libs sphinx test_ad ;
     8SubInclude HAIKU_TOP src tests libs sphinx test_alloc ;
     9SubInclude HAIKU_TOP src tests libs sphinx test_bitvec ;
     10SubInclude HAIKU_TOP src tests libs sphinx test_case ;
     11SubInclude HAIKU_TOP src tests libs sphinx test_cmdln ;
     12SubInclude HAIKU_TOP src tests libs sphinx test_fe ;
     13SubInclude HAIKU_TOP src tests libs sphinx test_feat ;
     14SubInclude HAIKU_TOP src tests libs sphinx test_fsg ;
     15SubInclude HAIKU_TOP src tests libs sphinx test_hash ;
     16SubInclude HAIKU_TOP src tests libs sphinx test_logmath ;
     17SubInclude HAIKU_TOP src tests libs sphinx test_matrix ;
     18SubInclude HAIKU_TOP src tests libs sphinx test_ngram ;
     19SubInclude HAIKU_TOP src tests libs sphinx test_string ;
     20SubInclude HAIKU_TOP src tests libs sphinx test_thread ;
     21SubInclude HAIKU_TOP src tests libs sphinx test_util ;
  • src/tests/libs/sphinx/test_case/_ucase2.test

     
     1#!/bin/sh
     2./chgCase ucase 1 1
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/_ucase3.test

    Property changes on: src/tests/libs/sphinx/test_case/_ucase2.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase ucase
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/_lcase1.test

    Property changes on: src/tests/libs/sphinx/test_case/_ucase3.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase lcase 5 3
  • src/tests/libs/sphinx/test_case/_lcase2.test

    Property changes on: src/tests/libs/sphinx/test_case/_lcase1.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase lcase 1 1
  • src/tests/libs/sphinx/test_case/_strcmp1.test

    Property changes on: src/tests/libs/sphinx/test_case/_lcase2.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase strcmp_nocase 2 3
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/Jamfile

    Property changes on: src/tests/libs/sphinx/test_case/_strcmp1.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1SubDir HAIKU_TOP src tests libs sphinx test_case ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest chgcase : chgCase.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_case/_lcase3.test

     
     1#!/bin/sh
     2./chgCase lcase
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/_strcmp2.test

    Property changes on: src/tests/libs/sphinx/test_case/_lcase3.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase strcmp_nocase 1 1
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/_strcmp3.test

    Property changes on: src/tests/libs/sphinx/test_case/_strcmp2.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase strcmp_nocase
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/_ucase1.test

    Property changes on: src/tests/libs/sphinx/test_case/_strcmp3.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#!/bin/sh
     2./chgCase ucase 2 4
     3 No newline at end of file
  • src/tests/libs/sphinx/test_case/chgCase.c

    Property changes on: src/tests/libs/sphinx/test_case/_ucase1.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2#include <stdio.h>
     3#include <string.h>
     4#include <stdlib.h>
     5#include <case.h>
     6#include <err.h>
     7
     8#define MAX_STR_LEN 64
     9#define NUM_STRS 6
     10
     11#define STR0 "this string should NEVER show up"
     12#define STR1 ""
     13#define STR2 "az3o%\tW@^#\\\n\r[]{}|\() '\""
     14#define STR3 "az3o%\tw@^#\\\n\r[]{}|\() '\""
     15#define STR4 "AZ3O%\tW@^#\\\n\r[]{}|\() '\""
     16#define STR5 "AZ3O%\tw@^#\\\n\r[]{}|\() '\""
     17
     18
     19int
     20main(int argc, char **argv)
     21{
     22    int cmp;
     23    char *n1 = NULL;
     24    char *n2 = NULL;
     25
     26    char s1[MAX_STR_LEN];
     27    char s2[MAX_STR_LEN];
     28
     29    char strs[NUM_STRS][MAX_STR_LEN] = { STR0,
     30        STR1,
     31        STR2,
     32        STR3,
     33        STR4,
     34        STR5
     35    };
     36
     37    if (argc < 2 ||
     38        3 == argc ||
     39        argc > 4 ||
     40        (strcmp(argv[1], "lcase") &&
     41         strcmp(argv[1], "ucase") && strcmp(argv[1], "strcmp_nocase")
     42        )) {
     43        /*printf("INVALID PARAMETERS to chgCase\n"); */
     44        exit(1);
     45    }
     46
     47
     48    if (2 == argc) {
     49        if (0 == strcmp(argv[1], "ucase")) {
     50            ucase(n1);
     51        }
     52        else if (0 == strcmp(argv[1], "lcase")) {
     53            lcase(n1);
     54        }
     55        else {
     56            strcmp_nocase(n1, n2);
     57        }
     58        /*
     59           if we're still alive we obviously didn't segfault
     60         */
     61        exit(0);
     62    }
     63
     64    if (4 == argc) {
     65
     66        if (0 >= atoi(argv[2]) ||
     67            atoi(argv[2]) >= NUM_STRS ||
     68            0 >= atoi(argv[3]) || atoi(argv[3]) >= NUM_STRS) {
     69            E_INFO("INVALID PARAMS TO chkCase\n");
     70            exit(1);
     71        }
     72
     73        strcpy(s1, strs[atoi(argv[2])]);
     74        strcpy(s2, strs[atoi(argv[3])]);
     75
     76        if (0 == strcmp(argv[1], "ucase")) {
     77            ucase(s1);
     78            cmp = strcmp(s1, s2);
     79        }
     80        else if (0 == strcmp(argv[1], "lcase")) {
     81            lcase(s1);
     82            cmp = strcmp(s1, s2);
     83        }
     84        else {
     85            cmp = strcmp_nocase(s1, s2);
     86        }
     87
     88        /*    E_INFO("Value of cmp %d\n", cmp); */
     89        if (0 != cmp) {
     90            E_FATAL("test failed\nstr1:|%s|\nstr2:|%s|\n", s1, s2);
     91        }
     92
     93        return (cmp != 0);
     94    }
     95
     96    /*somehow we got here and we shouldn't have */
     97
     98    exit(1);
     99}
  • src/tests/libs/sphinx/test_bitvec/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_bitvec ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_bitvec : test_bitvec.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_bitvec/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define EPSILON 0.01
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_bitvec/test_bitvec.c

     
     1#include <stdio.h>
     2#include <time.h>
     3
     4#include "bitvec.h"
     5#include "test_macros.h"
     6
     7int
     8main(int argc, char *argv[])
     9{
     10    bitvec_t *bv;
     11    int i, j;
     12    clock_t c;
     13
     14    TEST_ASSERT(bv = bitvec_alloc(199));
     15    bitvec_set(bv,198);
     16    bitvec_set(bv,0);
     17    bitvec_set(bv,42);
     18    bitvec_set(bv,43);
     19    bitvec_set(bv,44);
     20    TEST_ASSERT(bitvec_is_set(bv,198));
     21    TEST_ASSERT(bitvec_is_set(bv,0));
     22    TEST_ASSERT(bitvec_is_set(bv,42));
     23    TEST_ASSERT(bitvec_is_set(bv,43));
     24    TEST_ASSERT(bitvec_is_set(bv,44));
     25    TEST_EQUAL(5, bitvec_count_set(bv, 199));
     26    bitvec_clear(bv, 43);
     27    TEST_EQUAL(0, bitvec_is_set(bv,43));
     28
     29    c = clock();
     30    for (j = 0; j < 1000000; ++j)
     31        bitvec_count_set(bv, 199);
     32    c = clock() - c;
     33    printf("1000000 * 199 bitvec_count_set in %.2f sec\n",
     34           (double)c / CLOCKS_PER_SEC);
     35    bitvec_free(bv);
     36
     37    bv = bitvec_alloc(1314);
     38    c = clock();
     39    for (j = 0; j < 50000; ++j)
     40        for (i = 0; i < 1314; ++i)
     41            bitvec_set(bv, i);
     42    c = clock() - c;
     43    printf("50000 * 1314 bitvec_set in %.2f sec\n",
     44           (double)c / CLOCKS_PER_SEC);
     45    bitvec_free(bv);
     46
     47    return 0;
     48}
  • src/tests/libs/sphinx/test_util/test_filename.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/**
     3 * @file test_filename.c Test file name operations
     4 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     5 */
     6
     7#include "filename.h"
     8#include "test_macros.h"
     9
     10#include <stdio.h>
     11#include <string.h>
     12#include <stdlib.h>
     13
     14int
     15main(int argc, char *argv[])
     16{
     17    char const testname[] = "/foo/bar/baz/quux.argh";
     18    char const testname2[] = "foo/bar/baz";
     19    char const testname3[] = "/foo";
     20    char testout[32];
     21
     22    path2basename(testname, testout);
     23    TEST_EQUAL(0, strcmp("quux.argh", testout));
     24
     25    path2dirname(testname, testout);
     26    TEST_EQUAL(0, strcmp("/foo/bar/baz", testout));
     27
     28    path2dirname(testname2, testout);
     29    TEST_EQUAL(0, strcmp("foo/bar", testout));
     30
     31    path2dirname(testname3, testout);
     32    TEST_EQUAL(0, strcmp("", testout));
     33
     34    return 0;
     35}
  • src/tests/libs/sphinx/test_util/test_huff_code.c

     
     1/**
     2 * @file test_huff_code.c Test Huffman coding
     3 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     4 */
     5
     6#include "huff_code.h"
     7#include "test_macros.h"
     8
     9#include <stdio.h>
     10#include <stdlib.h>
     11#include <string.h>
     12
     13int32 const ivalues[10] = {
     14    1, 2, 3, 5, 7, 11, 13, 17, 19, 23
     15};
     16int32 const frequencies[10] = {
     17    42, 4, 5, 6, 225, 15001, 3, 2, 87, 1003
     18};
     19char * const svalues[10] = {
     20    "foo", "bar", "baz", "quux", "argh",
     21    "hurf", "burf", "blatz", "unf", "woof"
     22};
     23char * const svalues2[10] = {
     24    "1","2","3","4","5","6","7","8","9","10"
     25};
     26char const cdata[7] = { 0x08, 0x30, 0x40, 0x4c, 0x00, 0x04, 0x50 };
     27
     28void
     29test_intcode(huff_code_t *hc)
     30{
     31    FILE *fh;
     32    char const *dptr;
     33    int i, offset;
     34    size_t dlen;
     35    int32 val;
     36
     37    fh = fopen("hufftest.out", "wb");
     38    huff_code_attach(hc, fh, "wb");
     39    for (i = 0; i < 10; ++i) {
     40        huff_code_encode_int(hc, ivalues[i], NULL);
     41    }
     42    huff_code_detach(hc);
     43    fclose(fh);
     44    fh = fopen("hufftest.out", "rb");
     45    huff_code_attach(hc, fh, "rb");
     46    for (i = 0; i < 10; ++i) {
     47        int32 val;
     48        huff_code_decode_int(hc, &val, NULL, NULL, 0);
     49        printf("%d ", val);
     50        TEST_EQUAL(val, ivalues[i]);
     51    }
     52    printf("\n");
     53    huff_code_detach(hc);
     54    fclose(fh);
     55
     56    dptr = cdata;
     57    dlen = 7;
     58    offset = 0;
     59    for (i = 0; i < 10; ++i) {
     60        huff_code_decode_int(hc, &val, &dptr, &dlen, &offset);
     61        printf("%d ", val);
     62        TEST_EQUAL(val, ivalues[i]);
     63    }
     64    TEST_EQUAL(dlen, 1);
     65    TEST_EQUAL(offset, 4);
     66    printf("\n");
     67
     68    dptr = cdata;
     69    dlen = 7;
     70    offset = 0;
     71    i = 0;
     72    while (huff_code_decode_int(hc, &val, &dptr, &dlen, &offset) != -1) {
     73        printf("%d ", val);
     74        TEST_EQUAL(val, ivalues[i++]);
     75    }
     76    TEST_EQUAL(dlen, 1);
     77    TEST_EQUAL(offset, 4);
     78    printf("\n");
     79}
     80
     81void
     82test_strcode(huff_code_t *hc, char * const *svalues)
     83{
     84    FILE *fh;
     85    char const *dptr;
     86    int i, offset;
     87    size_t dlen;
     88
     89    fh = fopen("hufftest.out", "wb");
     90    huff_code_attach(hc, fh, "wb");
     91    for (i = 9; i >= 0; --i) {
     92        huff_code_encode_str(hc, svalues[i], NULL);
     93    }
     94    huff_code_detach(hc);
     95    fclose(fh);
     96    fh = fopen("hufftest.out", "rb");
     97    huff_code_attach(hc, fh, "rb");
     98    for (i = 9; i >= 0; --i) {
     99        char const *val = huff_code_decode_str(hc, NULL, NULL, 0);
     100        printf("%s ", val);
     101        TEST_EQUAL(0, strcmp(val, svalues[i]));
     102    }
     103    printf("\n");
     104    huff_code_detach(hc);
     105    fclose(fh);
     106
     107    dptr = cdata;
     108    dlen = 7;
     109    offset = 0;
     110    for (i = 0; i < 10; ++i) {
     111        char const *val = huff_code_decode_str(hc, &dptr, &dlen, &offset);
     112        printf("%s ", val);
     113        TEST_EQUAL(0, strcmp(val, svalues[i]));
     114    }
     115    TEST_EQUAL(dlen, 1);
     116    TEST_EQUAL(offset, 4);
     117    printf("\n");
     118}
     119
     120int
     121main(int argc, char *argv[])
     122{
     123    huff_code_t *hc;
     124    FILE *fh;
     125
     126    hc = huff_code_build_int(ivalues, frequencies, 10);
     127    huff_code_dump(hc, stdout);
     128    test_intcode(hc);
     129    fh = fopen("huffcode.out", "wb");
     130    huff_code_write(hc, fh);
     131    fclose(fh);
     132    huff_code_free(hc);
     133
     134    fh = fopen("huffcode.out", "rb");
     135    hc = huff_code_read(fh);
     136    fclose(fh);
     137    test_intcode(hc);
     138    huff_code_free(hc);
     139
     140    hc = huff_code_build_str(svalues, frequencies, 10);
     141    huff_code_dump(hc, stdout);
     142    test_strcode(hc, svalues);
     143    fh = fopen("huffcode.out", "wb");
     144    huff_code_write(hc, fh);
     145    fclose(fh);
     146    huff_code_free(hc);
     147
     148    fh = fopen("huffcode.out", "rb");
     149    hc = huff_code_read(fh);
     150    fclose(fh);
     151    test_strcode(hc, svalues);
     152    huff_code_free(hc);
     153
     154    hc = huff_code_build_str(svalues2, frequencies, 10);
     155    huff_code_dump(hc, stdout);
     156    test_strcode(hc, svalues2);
     157    huff_code_free(hc);
     158
     159    return 0;
     160}
     161
  • src/tests/libs/sphinx/test_util/test_build_directory.c

     
     1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
     2/**
     3 * @file test_build_directory.c Test recursive directory creation
     4 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     5 */
     6
     7#include "pio.h"
     8#include "test_macros.h"
     9
     10#include <stdio.h>
     11#include <stdlib.h>
     12
     13int
     14main(int argc, char *argv[])
     15{
     16    TEST_EQUAL(0, build_directory("foo/bar/baz"));
     17    TEST_ASSERT(stat_mtime("foo/bar/baz") != -1);
     18    TEST_EQUAL(0, build_directory("./quux/"));
     19    TEST_ASSERT(stat_mtime("quux") != -1);
     20    TEST_EQUAL(0, build_directory("./foo/bar/baz"));
     21    TEST_ASSERT(stat_mtime("foo/bar/baz") != -1);
     22    TEST_EQUAL(0, build_directory("/tmp/sphinxbase_foo_bar_baz"));
     23    TEST_ASSERT(stat_mtime("/tmp/sphinxbase_foo_bar_baz") != -1);
     24
     25    return 0;
     26}
  • src/tests/libs/sphinx/test_util/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_util ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_bit_encode : test_bit_encode.c :  libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_build_directory : test_build_directory.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_filename : test_filename.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     11SimpleTest test_fopen : test_fopen.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     12SimpleTest test_huff_code : test_huff_code.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_util/test_fopen.c

     
     1/**
     2 * @file test_fopen.c Test file opening
     3 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     4 */
     5
     6#include "pio.h"
     7#include "test_macros.h"
     8
     9#include <stdio.h>
     10#include <stdlib.h>
     11
     12#ifndef LMDIR
     13#define LMDIR "../regression"
     14#endif
     15
     16int
     17main(int argc, char *argv[])
     18{
     19    FILE *fh;
     20    char line[256], *c;
     21    int ispipe;
     22
     23    fh = fopen_comp(LMDIR "/100.arpa.gz", "r", &ispipe);
     24    TEST_ASSERT(fh != NULL);
     25    c = fgets(line, sizeof(line), fh);
     26    TEST_EQUAL('#', line[0]);
     27    fclose_comp(fh, ispipe);
     28
     29    fh = fopen_compchk(LMDIR "/100.arpa.gz", &ispipe);
     30    TEST_ASSERT(fh != NULL);
     31    c = fgets(line, sizeof(line), fh);
     32    TEST_EQUAL('#', line[0]);
     33    fclose_comp(fh, ispipe);
     34
     35    fh = fopen_compchk(LMDIR "/100.arpa.bz2", &ispipe);
     36    TEST_ASSERT(fh != NULL);
     37    c = fgets(line, sizeof(line), fh);
     38    TEST_EQUAL('#', line[0]);
     39    fclose_comp(fh, ispipe);
     40
     41    fh = fopen_compchk(LMDIR "/100.arpa", &ispipe);
     42    TEST_ASSERT(fh != NULL);
     43    c = fgets(line, sizeof(line), fh);
     44    TEST_EQUAL('#', line[0]);
     45    fclose_comp(fh, ispipe);
     46
     47    return 0;
     48}
  • src/tests/libs/sphinx/test_util/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     7#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     8#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     9#define LOG_EPSILON 20
     10#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_util/test_bit_encode.c

     
     1/**
     2 * @file test_bit_encode.c Test bitstream encoding
     3 * @author David Huggins-Daines <dhuggins@cs.cmu.edu>
     4 */
     5
     6#include "pio.h"
     7#include "test_macros.h"
     8
     9#include <stdio.h>
     10#include <stdlib.h>
     11#include <string.h>
     12
     13int
     14main(int argc, char *argv[])
     15{
     16    FILE *fh;
     17    bit_encode_t *be;
     18    int i;
     19    static unsigned char const bits[] = "\xde\xad\xbe\xef";
     20    uint32 cw = 0xdeadbeef;
     21    unsigned char inbits[16];
     22
     23    fh = fopen("bittest.out", "wb");
     24    be = bit_encode_attach(fh);
     25    bit_encode_write(be, bits, 8);
     26    bit_encode_write(be, bits + 1, 16);
     27    bit_encode_write(be, bits + 3, 8);
     28    bit_encode_write_cw(be, cw >> 24, 8);
     29    bit_encode_write_cw(be, cw >> 16, 8);
     30    bit_encode_write_cw(be, cw >> 8, 8);
     31    bit_encode_write_cw(be, cw, 8);
     32    bit_encode_write_cw(be, cw >> 26, 6);
     33    bit_encode_write_cw(be, cw >> 14, 12);
     34    bit_encode_write_cw(be, cw >> 8, 6);
     35    bit_encode_write_cw(be, cw, 8);
     36    for (i = 0; i < 32; ++i) {
     37        bit_encode_write_cw(be, cw >> (31-i), 1);
     38    }
     39    bit_encode_flush(be);
     40    bit_encode_free(be);
     41    fclose(fh);
     42    fh = fopen("bittest.out", "rb");
     43    i = fread(inbits, 1, 16, fh);
     44    TEST_ASSERT(0 == memcmp(inbits, bits, 4));
     45    TEST_ASSERT(0 == memcmp(inbits + 4, bits, 4));
     46    TEST_ASSERT(0 == memcmp(inbits + 8, bits, 4));
     47    TEST_ASSERT(0 == memcmp(inbits + 12, bits, 4));
     48    fclose(fh);
     49
     50    return 0;
     51}
  • src/tests/libs/sphinx/test_fsg/polite.gram

     
     1#JSGF V1.0;
     2
     3/**
     4 * JSGF Grammar for Hello World example
     5 */
     6
     7grammar polite;
     8
     9public <startPolite> = [please | kindly | could you | oh mighty computer];
     10public <endPolite> = [please | thanks | thank you];
  • src/tests/libs/sphinx/test_fsg/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_fsg ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_fsg_jsgf : test_fsg_jsgf.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_fsg_read : test_fsg_read.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_fsg_write_fsm : test_fsg_write_fsm.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_fsg/goforward.fsg

     
     1FSG_BEGIN turtle
     2NUM_STATES 7
     3START_STATE 0
     4FINAL_STATE 6
     5
     6# Transitions
     7TRANSITION 0 1 1.0 GO
     8TRANSITION 1 2 0.5 FORWARD
     9TRANSITION 1 3 0.5 BACKWARD
     10TRANSITION 2 4 1.0
     11TRANSITION 3 4 1.0
     12TRANSITION 4 5 0.1 ONE
     13TRANSITION 4 5 0.1 TWO
     14TRANSITION 4 5 0.1 THREE
     15TRANSITION 4 5 0.1 FOUR
     16TRANSITION 4 5 0.1 FIVE
     17TRANSITION 4 5 0.1 SIX
     18TRANSITION 4 5 0.1 SEVEN
     19TRANSITION 4 5 0.1 EIGHT
     20TRANSITION 4 5 0.1 NINE
     21TRANSITION 4 5 0.1 TEN
     22TRANSITION 5 6 0.1 METER
     23TRANSITION 5 6 0.9 METERS
     24FSG_END
  • src/tests/libs/sphinx/test_fsg/test_fsg_jsgf.c

     
     1#include <jsgf.h>
     2#include <fsg_model.h>
     3
     4#include "test_macros.h"
     5
     6#ifndef LMDIR
     7#define LMDIR "."
     8#endif
     9
     10
     11int
     12main(int argc, char *argv[])
     13{
     14    logmath_t *lmath;
     15    fsg_model_t *fsg;
     16    jsgf_t *jsgf;
     17    jsgf_rule_t *rule;
     18
     19    /* Initialize a logmath object to pass to fsg_model_read */
     20    lmath = logmath_init(1.0001, 0, 0);
     21    jsgf = jsgf_parse_file(LMDIR "/polite.gram", NULL);
     22    TEST_ASSERT(jsgf);
     23    rule = jsgf_get_rule(jsgf, "<polite.startPolite>");
     24    TEST_ASSERT(rule);
     25    fsg = jsgf_build_fsg(jsgf, rule, lmath, 7.5);
     26    TEST_ASSERT(fsg);
     27
     28    TEST_ASSERT(fsg_model_add_silence(fsg, "<sil>", -1, 0.3));
     29    TEST_ASSERT(fsg_model_add_silence(fsg, "++NOISE++", -1, 0.3));
     30    TEST_ASSERT(fsg_model_add_alt(fsg, "please", "please(2)"));
     31
     32    jsgf_grammar_free(jsgf);
     33    fsg_model_write(fsg, stdout);
     34    fsg_model_free(fsg);
     35    logmath_free(lmath);
     36
     37    return 0;
     38}
  • src/tests/libs/sphinx/test_fsg/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     7#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     8#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     9#define LOG_EPSILON 20
     10#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_fsg/test_fsg_read.c

     
     1#include <fsg_model.h>
     2
     3#include "test_macros.h"
     4
     5#ifndef LMDIR
     6#define LMDIR "."
     7#endif
     8
     9
     10int
     11main(int argc, char *argv[])
     12{
     13    logmath_t *lmath;
     14    fsg_model_t *fsg;
     15
     16    /* Initialize a logmath object to pass to fsg_model_read */
     17    lmath = logmath_init(1.0001, 0, 0);
     18    /* Read a FSG. */
     19    fsg = fsg_model_readfile(LMDIR "/goforward.fsg", lmath, 7.5);
     20    TEST_ASSERT(fsg);
     21
     22    TEST_ASSERT(fsg_model_add_silence(fsg, "<sil>", -1, 0.3));
     23    TEST_ASSERT(fsg_model_add_silence(fsg, "++NOISE++", -1, 0.3));
     24    TEST_ASSERT(fsg_model_add_alt(fsg, "FORWARD", "FORWARD(2)"));
     25
     26    fsg_model_write(fsg, stdout);
     27
     28    /* Test reference counting. */
     29    TEST_ASSERT(fsg = fsg_model_retain(fsg));
     30    TEST_EQUAL(1, fsg_model_free(fsg));
     31    fsg_model_write(fsg, stdout);
     32
     33    TEST_EQUAL(0, fsg_model_free(fsg));
     34    logmath_free(lmath);
     35
     36    return 0;
     37}
  • src/tests/libs/sphinx/test_fsg/test_fsg_write_fsm.c

     
     1#include <jsgf.h>
     2#include <fsg_model.h>
     3
     4#include "test_macros.h"
     5
     6#ifndef LMDIR
     7#define LMDIR "."
     8#endif
     9
     10
     11int
     12main(int argc, char *argv[])
     13{
     14    logmath_t *lmath;
     15    fsg_model_t *fsg;
     16    jsgf_t *jsgf;
     17    jsgf_rule_t *rule;
     18
     19    /* Initialize a logmath object to pass to fsg_model_read */
     20    lmath = logmath_init(1.0001, 0, 0);
     21    jsgf = jsgf_parse_file(LMDIR "/polite.gram", NULL);
     22    TEST_ASSERT(jsgf);
     23    rule = jsgf_get_rule(jsgf, "<polite.startPolite>");
     24    TEST_ASSERT(rule);
     25    fsg = jsgf_build_fsg(jsgf, rule, lmath, 7.5);
     26    TEST_ASSERT(fsg);
     27
     28    TEST_ASSERT(fsg_model_add_silence(fsg, "<sil>", -1, 0.3));
     29    TEST_ASSERT(fsg_model_add_silence(fsg, "++NOISE++", -1, 0.3));
     30    TEST_ASSERT(fsg_model_add_alt(fsg, "please", "please(2)"));
     31
     32    jsgf_grammar_free(jsgf);
     33    fsg_model_write_fsm(fsg, stdout);
     34    fsg_model_write_symtab(fsg, stdout);
     35    fsg_model_free(fsg);
     36    logmath_free(lmath);
     37
     38    return 0;
     39}
  • src/tests/libs/sphinx/test_feat/test_feat.c

     
     1#ifdef HAVE_CONFIG_H
     2#include <config.h>
     3#endif
     4
     5#include <stdio.h>
     6#include <string.h>
     7#include <math.h>
     8
     9#include "feat.h"
     10#include "ckd_alloc.h"
     11
     12#ifndef TESTDATADIR
     13#define TESTDATADIR "../regression"
     14#endif
     15
     16
     17const mfcc_t data[6][13] = {
     18    { FLOAT2MFCC(15.114), FLOAT2MFCC(-1.424), FLOAT2MFCC(-0.953),
     19      FLOAT2MFCC(0.186), FLOAT2MFCC(-0.656), FLOAT2MFCC(-0.226),
     20      FLOAT2MFCC(-0.105), FLOAT2MFCC(-0.412), FLOAT2MFCC(-0.024),
     21      FLOAT2MFCC(-0.091), FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.158), FLOAT2MFCC(-0.197)},
     22    { FLOAT2MFCC(14.729), FLOAT2MFCC(-1.313), FLOAT2MFCC(-0.892),
     23      FLOAT2MFCC(0.140), FLOAT2MFCC(-0.676), FLOAT2MFCC(-0.089),
     24      FLOAT2MFCC(-0.313), FLOAT2MFCC(-0.422), FLOAT2MFCC(-0.058),
     25      FLOAT2MFCC(-0.101), FLOAT2MFCC(-0.100), FLOAT2MFCC(-0.128), FLOAT2MFCC(-0.123)},
     26    { FLOAT2MFCC(14.502), FLOAT2MFCC(-1.351), FLOAT2MFCC(-1.028),
     27      FLOAT2MFCC(-0.189), FLOAT2MFCC(-0.718), FLOAT2MFCC(-0.139),
     28      FLOAT2MFCC(-0.121), FLOAT2MFCC(-0.365), FLOAT2MFCC(-0.139),
     29      FLOAT2MFCC(-0.154), FLOAT2MFCC(0.041), FLOAT2MFCC(0.009), FLOAT2MFCC(-0.073)},
     30    { FLOAT2MFCC(14.557), FLOAT2MFCC(-1.676), FLOAT2MFCC(-0.864),
     31      FLOAT2MFCC(0.118), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.168),
     32      FLOAT2MFCC(-0.069), FLOAT2MFCC(-0.503), FLOAT2MFCC(-0.013),
     33      FLOAT2MFCC(0.007), FLOAT2MFCC(-0.056), FLOAT2MFCC(-0.075), FLOAT2MFCC(-0.237)},
     34    { FLOAT2MFCC(14.665), FLOAT2MFCC(-1.498), FLOAT2MFCC(-0.582),
     35      FLOAT2MFCC(0.209), FLOAT2MFCC(-0.487), FLOAT2MFCC(-0.247),
     36      FLOAT2MFCC(-0.142), FLOAT2MFCC(-0.439), FLOAT2MFCC(0.059),
     37      FLOAT2MFCC(-0.058), FLOAT2MFCC(-0.265), FLOAT2MFCC(-0.109), FLOAT2MFCC(-0.196)},
     38    { FLOAT2MFCC(15.025), FLOAT2MFCC(-1.199), FLOAT2MFCC(-0.607),
     39      FLOAT2MFCC(0.235), FLOAT2MFCC(-0.499), FLOAT2MFCC(-0.080),
     40      FLOAT2MFCC(-0.062), FLOAT2MFCC(-0.554), FLOAT2MFCC(-0.209),
     41      FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.352), FLOAT2MFCC(-0.400)},
     42};
     43
     44int
     45main(int argc, char *argv[])
     46{
     47    feat_t *fcb;
     48    mfcc_t **in_feats, ***out_feats;
     49    int32 i, j, ncep;
     50
     51    /* Test "raw" features without concatenation */
     52    fcb = feat_init("13", CMN_NONE, 0, AGC_NONE, 1, 13);
     53
     54    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
     55    out_feats = (mfcc_t ***)ckd_calloc_3d(6, 1, 13, sizeof(mfcc_t));
     56    ncep = 6;
     57    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
     58
     59    for (i = 0; i < 6; ++i) {
     60        for (j = 0; j < 13; ++j) {
     61            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
     62        }
     63        printf("\n");
     64    }
     65    feat_free(fcb);
     66    ckd_free(in_feats);
     67    ckd_free_3d(out_feats);
     68
     69    /* Test "raw" features with concatenation */
     70    fcb = feat_init("13:1", CMN_NONE, 0, AGC_NONE, 1, 13);
     71
     72    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
     73    out_feats = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
     74    ncep = 6;
     75    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
     76
     77    for (i = 0; i < 6; ++i) {
     78        for (j = 0; j < 39; ++j) {
     79            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
     80        }
     81        printf("\n");
     82    }
     83    feat_free(fcb);
     84
     85    /* Test 1s_c_d_dd features */
     86    fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
     87    ncep = 6;
     88    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
     89
     90    for (i = 0; i < 6; ++i) {
     91        for (j = 0; j < 39; ++j) {
     92            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
     93        }
     94        printf("\n");
     95    }
     96
     97    /* Verify that the deltas are correct. */
     98    for (i = 2; i < 4; ++i) {
     99        for (j = 0; j < 13; ++j) {
     100            if (fabs(MFCC2FLOAT(out_feats[i][0][13+j] -
     101                        (out_feats[i+2][0][j]
     102                         - out_feats[i-2][0][j]))) > 0.01) {
     103                printf("Delta mismatch in [%d][%d]\n", i, j);
     104                return 1;
     105            }
     106        }
     107    }
     108    feat_free(fcb);
     109    ckd_free(in_feats);
     110    ckd_free_3d(out_feats);
     111
     112    return 0;
     113}
  • src/tests/libs/sphinx/test_feat/test_subvq.c

     
     1#ifdef HAVE_CONFIG_H
     2#include <config.h>
     3#endif
     4
     5#include <stdio.h>
     6#include <string.h>
     7#include <math.h>
     8
     9#include "feat.h"
     10#include "test_macros.h"
     11#include "ckd_alloc.h"
     12
     13const mfcc_t data[6][13] = {
     14    { FLOAT2MFCC(15.114), FLOAT2MFCC(-1.424), FLOAT2MFCC(-0.953),
     15      FLOAT2MFCC(0.186), FLOAT2MFCC(-0.656), FLOAT2MFCC(-0.226),
     16      FLOAT2MFCC(-0.105), FLOAT2MFCC(-0.412), FLOAT2MFCC(-0.024),
     17      FLOAT2MFCC(-0.091), FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.158), FLOAT2MFCC(-0.197)},
     18    { FLOAT2MFCC(14.729), FLOAT2MFCC(-1.313), FLOAT2MFCC(-0.892),
     19      FLOAT2MFCC(0.140), FLOAT2MFCC(-0.676), FLOAT2MFCC(-0.089),
     20      FLOAT2MFCC(-0.313), FLOAT2MFCC(-0.422), FLOAT2MFCC(-0.058),
     21      FLOAT2MFCC(-0.101), FLOAT2MFCC(-0.100), FLOAT2MFCC(-0.128), FLOAT2MFCC(-0.123)},
     22    { FLOAT2MFCC(14.502), FLOAT2MFCC(-1.351), FLOAT2MFCC(-1.028),
     23      FLOAT2MFCC(-0.189), FLOAT2MFCC(-0.718), FLOAT2MFCC(-0.139),
     24      FLOAT2MFCC(-0.121), FLOAT2MFCC(-0.365), FLOAT2MFCC(-0.139),
     25      FLOAT2MFCC(-0.154), FLOAT2MFCC(0.041), FLOAT2MFCC(0.009), FLOAT2MFCC(-0.073)},
     26    { FLOAT2MFCC(14.557), FLOAT2MFCC(-1.676), FLOAT2MFCC(-0.864),
     27      FLOAT2MFCC(0.118), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.168),
     28      FLOAT2MFCC(-0.069), FLOAT2MFCC(-0.503), FLOAT2MFCC(-0.013),
     29      FLOAT2MFCC(0.007), FLOAT2MFCC(-0.056), FLOAT2MFCC(-0.075), FLOAT2MFCC(-0.237)},
     30    { FLOAT2MFCC(14.665), FLOAT2MFCC(-1.498), FLOAT2MFCC(-0.582),
     31      FLOAT2MFCC(0.209), FLOAT2MFCC(-0.487), FLOAT2MFCC(-0.247),
     32      FLOAT2MFCC(-0.142), FLOAT2MFCC(-0.439), FLOAT2MFCC(0.059),
     33      FLOAT2MFCC(-0.058), FLOAT2MFCC(-0.265), FLOAT2MFCC(-0.109), FLOAT2MFCC(-0.196)},
     34    { FLOAT2MFCC(15.025), FLOAT2MFCC(-1.199), FLOAT2MFCC(-0.607),
     35      FLOAT2MFCC(0.235), FLOAT2MFCC(-0.499), FLOAT2MFCC(-0.080),
     36      FLOAT2MFCC(-0.062), FLOAT2MFCC(-0.554), FLOAT2MFCC(-0.209),
     37      FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.352), FLOAT2MFCC(-0.400)},
     38};
     39
     40int
     41main(int argc, char *argv[])
     42{
     43    static char const svspec[] = "1-12/14-25/0,13,26/27-38";
     44    int32 **subvecs, i, j, k, ncep;
     45    mfcc_t **in_feats, ***out_feats;
     46    feat_t *fcb;
     47
     48    /* Test parsing of a subvector spec. */
     49    subvecs = parse_subvecs(svspec);
     50    TEST_ASSERT(subvecs);
     51    for (i = 0; i < 12; ++i) {
     52        TEST_EQUAL(subvecs[0][i], i+1);
     53    }
     54    for (i = 0; i < 12; ++i) {
     55        TEST_EQUAL(subvecs[1][i], i+14);
     56    }
     57    TEST_EQUAL(subvecs[2][0], 0);
     58    TEST_EQUAL(subvecs[2][1], 13);
     59    TEST_EQUAL(subvecs[2][2], 26);
     60    for (i = 0; i < 12; ++i) {
     61        TEST_EQUAL(subvecs[3][i], i+27);
     62    }
     63
     64    /* Create a 1s_c_d_dd feature stream and split it into subvectors. */
     65    fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
     66    TEST_ASSERT(fcb);
     67    feat_set_subvecs(fcb, subvecs);
     68
     69    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
     70    out_feats = feat_array_alloc(fcb, 6);
     71    TEST_ASSERT(out_feats);
     72
     73    ncep = 6;
     74    feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
     75
     76    for (i = 0; i < 6; ++i) {
     77        for (j = 0; j < feat_dimension1(fcb); ++j) {
     78            for (k = 0; k < feat_dimension2(fcb, j); ++k) {
     79                printf("%.3f ", MFCC2FLOAT(out_feats[i][j][k]));
     80            }
     81            printf("\n");
     82        }
     83        printf("\n");
     84    }
     85
     86    feat_array_free(out_feats);
     87    ckd_free(in_feats);
     88    feat_free(fcb);
     89
     90    return 0;
     91}
  • src/tests/libs/sphinx/test_feat/_test_feat.res

     
     115.114 -1.424 -0.953 0.186 -0.656 -0.226 -0.105 -0.412 -0.024 -0.091 -0.124 -0.158 -0.197
     214.729 -1.313 -0.892 0.140 -0.676 -0.089 -0.313 -0.422 -0.058 -0.101 -0.100 -0.128 -0.123
     314.502 -1.351 -1.028 -0.189 -0.718 -0.139 -0.121 -0.365 -0.139 -0.154 0.041 0.009 -0.073
     414.557 -1.676 -0.864 0.118 -0.445 -0.168 -0.069 -0.503 -0.013 0.007 -0.056 -0.075 -0.237
     514.665 -1.498 -0.582 0.209 -0.487 -0.247 -0.142 -0.439 0.059 -0.058 -0.265 -0.109 -0.196
     615.025 -1.199 -0.607 0.235 -0.499 -0.080 -0.062 -0.554 -0.209 -0.124 -0.445 -0.352 -0.400
     715.114 -1.424 -0.953 0.186 -0.656 -0.226 -0.105 -0.412 -0.024 -0.091 -0.124 -0.158 -0.197 15.114 -1.424 -0.953 0.186 -0.656 -0.226 -0.105 -0.412 -0.024 -0.091 -0.124 -0.158 -0.197 14.729 -1.313 -0.892 0.140 -0.676 -0.089 -0.313 -0.422 -0.058 -0.101 -0.100 -0.128 -0.123
     815.114 -1.424 -0.953 0.186 -0.656 -0.226 -0.105 -0.412 -0.024 -0.091 -0.124 -0.158 -0.197 14.729 -1.313 -0.892 0.140 -0.676 -0.089 -0.313 -0.422 -0.058 -0.101 -0.100 -0.128 -0.123 14.502 -1.351 -1.028 -0.189 -0.718 -0.139 -0.121 -0.365 -0.139 -0.154 0.041 0.009 -0.073
     914.729 -1.313 -0.892 0.140 -0.676 -0.089 -0.313 -0.422 -0.058 -0.101 -0.100 -0.128 -0.123 14.502 -1.351 -1.028 -0.189 -0.718 -0.139 -0.121 -0.365 -0.139 -0.154 0.041 0.009 -0.073 14.557 -1.676 -0.864 0.118 -0.445 -0.168 -0.069 -0.503 -0.013 0.007 -0.056 -0.075 -0.237
     1014.502 -1.351 -1.028 -0.189 -0.718 -0.139 -0.121 -0.365 -0.139 -0.154 0.041 0.009 -0.073 14.557 -1.676 -0.864 0.118 -0.445 -0.168 -0.069 -0.503 -0.013 0.007 -0.056 -0.075 -0.237 14.665 -1.498 -0.582 0.209 -0.487 -0.247 -0.142 -0.439 0.059 -0.058 -0.265 -0.109 -0.196
     1114.557 -1.676 -0.864 0.118 -0.445 -0.168 -0.069 -0.503 -0.013 0.007 -0.056 -0.075 -0.237 14.665 -1.498 -0.582 0.209 -0.487 -0.247 -0.142 -0.439 0.059 -0.058 -0.265 -0.109 -0.196 15.025 -1.199 -0.607 0.235 -0.499 -0.080 -0.062 -0.554 -0.209 -0.124 -0.445 -0.352 -0.400
     1214.665 -1.498 -0.582 0.209 -0.487 -0.247 -0.142 -0.439 0.059 -0.058 -0.265 -0.109 -0.196 15.025 -1.199 -0.607 0.235 -0.499 -0.080 -0.062 -0.554 -0.209 -0.124 -0.445 -0.352 -0.400 15.025 -1.199 -0.607 0.235 -0.499 -0.080 -0.062 -0.554 -0.209 -0.124 -0.445 -0.352 -0.400
     1315.114 -1.424 -0.953 0.186 -0.656 -0.226 -0.105 -0.412 -0.024 -0.091 -0.124 -0.158 -0.197 -0.612 0.073 -0.075 -0.375 -0.062 0.087 -0.016 0.047 -0.115 -0.063 0.165 0.167 0.124 -0.172 -0.363 0.028 -0.022 0.231 -0.079 0.244 -0.081 0.045 0.108 0.044 0.053 -0.114
     1414.729 -1.313 -0.892 0.140 -0.676 -0.089 -0.313 -0.422 -0.058 -0.101 -0.100 -0.128 -0.123 -0.557 -0.252 0.089 -0.068 0.211 0.058 0.036 -0.091 0.011 0.098 0.068 0.083 -0.040 0.163 -0.147 0.446 0.398 0.231 -0.108 -0.021 -0.074 0.198 0.096 -0.306 -0.118 -0.123
     1514.502 -1.351 -1.028 -0.189 -0.718 -0.139 -0.121 -0.365 -0.139 -0.154 0.041 0.009 -0.073 -0.449 -0.074 0.371 0.023 0.169 -0.021 -0.037 -0.027 0.083 0.033 -0.141 0.049 0.001 0.853 0.366 0.196 0.163 -0.034 -0.049 0.215 -0.041 -0.162 -0.121 -0.413 -0.307 -0.237
     1614.557 -1.676 -0.864 0.118 -0.445 -0.168 -0.069 -0.503 -0.013 0.007 -0.056 -0.075 -0.237 0.296 0.114 0.285 0.095 0.177 0.009 0.251 -0.132 -0.151 -0.023 -0.345 -0.224 -0.277 0.972 0.226 0.050 0.401 0.050 0.080 0.096 -0.162 -0.153 -0.003 -0.345 -0.410 -0.328
     1714.665 -1.498 -0.582 0.209 -0.487 -0.247 -0.142 -0.439 0.059 -0.058 -0.265 -0.109 -0.196 0.523 0.152 0.421 0.424 0.219 0.059 0.059 -0.189 -0.070 0.030 -0.486 -0.361 -0.327 0.172 0.363 -0.028 0.022 -0.231 0.079 -0.244 0.081 -0.045 -0.108 -0.044 -0.053 0.114
     1815.025 -1.199 -0.607 0.235 -0.499 -0.080 -0.062 -0.554 -0.209 -0.124 -0.445 -0.352 -0.400 0.468 0.477 0.257 0.117 -0.054 0.088 0.007 -0.051 -0.196 -0.131 -0.389 -0.277 -0.163 -0.163 0.147 -0.446 -0.398 -0.231 0.108 0.021 0.074 -0.198 -0.096 0.306 0.118 0.123
  • src/tests/libs/sphinx/test_feat/Jamfile

     
     1SubDir HAIKU_TOP src tests libs sphinx test_feat ;
     2
     3SetSubDirSupportedPlatformsBeOSCompatible ;
     4
     5UseLibraryHeaders sphinx ;
     6UseLibraryHeaders sphinx/libsphinxbase ;
     7
     8SimpleTest test_feat : test_feat.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     9SimpleTest test_feat_fe : test_feat_fe.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     10SimpleTest test_feat_live : test_feat_live.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
     11SimpleTest test_subvq : test_subvq.c : libsphinxfe.a libsphinxfeat.a libsphinxlm.a libsphinxutil.a ;
  • src/tests/libs/sphinx/test_feat/test_feat_fe.c

     
     1#include <stdio.h>
     2#include <errno.h>
     3
     4#include "fe.h"
     5#include "feat.h"
     6#include "cmd_ln.h"
     7#include "ckd_alloc.h"
     8
     9#include "test_macros.h"
     10
     11#ifndef TESTDATADIR
     12#define TESTDATADIR "../regression"
     13#endif
     14
     15
     16int
     17main(int argc, char *argv[])
     18{
     19    static const arg_t fe_args[] = {
     20        waveform_to_cepstral_command_line_macro(),
     21        { NULL, 0, NULL, NULL }
     22    };
     23    FILE *raw;
     24    cmd_ln_t *config;
     25    fe_t *fe;
     26    feat_t *fcb;
     27    int16 buf[2048];
     28    mfcc_t **cepbuf, **cptr;
     29    mfcc_t ***featbuf1, ***featbuf2, ***fptr;
     30    size_t nsamp;
     31    int32 total_frames, ncep, nfr, i;
     32
     33    if ((raw = fopen(TESTDATADIR "/chan3.raw", "rb")) == NULL) {
     34        perror(TESTDATADIR "/chan3.raw");
     35        return 1;
     36    }
     37
     38    config = cmd_ln_parse_r(NULL, fe_args, argc, argv, FALSE);
     39    fe = fe_init_auto_r(config);
     40    fcb = feat_init("1s_c_d_dd", CMN_NONE, FALSE, AGC_NONE,
     41            TRUE, fe_get_output_size(fe));
     42
     43    /* Determine how much data and how many MFCC frames we need. */
     44    fseek(raw, 0, SEEK_END);
     45    nsamp = ftell(raw) / sizeof(int16);
     46    fe_process_frames(fe, NULL, &nsamp, NULL, &total_frames);
     47    printf("%d samples, %d + 1 frames\n", nsamp, total_frames);
     48    total_frames++; /* For the possible fe_end_utt() frame */
     49    cepbuf = ckd_calloc_2d(total_frames + 1, fe_get_output_size(fe), sizeof(**cepbuf));
     50    fseek(raw, 0, SEEK_SET);
     51
     52    /* Pay close attention, kids.  This is how you use fe_process_frames(). */
     53    fe_start_utt(fe);
     54    cptr = cepbuf;
     55    nfr = total_frames;
     56    while ((nsamp = fread(buf, sizeof(int16), 2048, raw)) > 0) {
     57        int16 const *bptr = buf;
     58        while (nsamp) {
     59            int32 ncep = nfr;
     60            fe_process_frames(fe, &bptr, &nsamp, cptr, &ncep);
     61            cptr += ncep;
     62            nfr -= ncep;
     63        }
     64    }
     65    fe_end_utt(fe, *cptr, &nfr);
     66
     67    /* Now test some feature extraction problems. */
     68    featbuf1 = feat_array_alloc(fcb, total_frames);
     69    featbuf2 = feat_array_alloc(fcb, total_frames);
     70
     71    /* Whole utterance: canonical, assumed to be correct. */
     72    ncep = total_frames;
     73    TEST_EQUAL(total_frames,
     74           feat_s2mfc2feat_live(fcb, cepbuf,
     75                    &ncep, TRUE, TRUE,
     76                    featbuf1));
     77    TEST_EQUAL(ncep, total_frames);
     78
     79    /* Process one frame at a time. */
     80    cptr = cepbuf;
     81    fptr = featbuf2;
     82    ncep = 1;
     83    nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, TRUE, FALSE, fptr);
     84    TEST_EQUAL(nfr, 0); /* Not possible to make any frames yet. */
     85    TEST_EQUAL(ncep, 1); /* But we shold have consumed one. */
     86    cptr += ncep;
     87    for (i = 1; i < total_frames - 1; ++i) {
     88        ncep = 1;
     89        nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, FALSE, fptr);
     90        cptr += ncep;
     91        fptr += nfr;
     92    }
     93    nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, TRUE, fptr);
     94    TEST_EQUAL(nfr, 4); /* This should have dumped the trailing window. */
     95    TEST_EQUAL(ncep, 1); /* And only consumed one frame of MFCCs. */
     96    cptr += ncep;
     97    fptr += nfr;
     98    /* Verify that we actually got the correct number of frames. */
     99    TEST_EQUAL(cptr - cepbuf, total_frames);
     100    TEST_EQUAL(fptr - featbuf2, total_frames);
     101
     102    /* Now verify that the results are equal. */
     103    for (i = 0; i < total_frames; ++i) {
     104        int32 j;
     105        printf("%-4d ", i);
     106        for (j = 0; j < feat_dimension(fcb); ++j) {
     107            TEST_EQUAL_FLOAT(featbuf1[i][0][j], featbuf2[i][0][j]);
     108        }
     109        if (i % 10 == 9)
     110            printf("\n");
     111    }
     112    printf("\n");
     113
     114    /* Process large chunks of frames at once, so as to exceed the
     115     * internal ringbuffer size in feat_s2mfc2feat_live(). */
     116    cptr = cepbuf;
     117    fptr = featbuf2;
     118    ncep = total_frames;
     119    nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, TRUE, FALSE, fptr);
     120    TEST_ASSERT(ncep != nfr);
     121    cptr += ncep;
     122    fptr += nfr;
     123    ncep = total_frames - ncep;
     124    while (ncep) {
     125        int32 tmp_ncep;
     126        tmp_ncep = ncep;
     127        nfr = feat_s2mfc2feat_live(fcb, cptr, &tmp_ncep, FALSE, FALSE, fptr);
     128        cptr += tmp_ncep;
     129        fptr += nfr;
     130        ncep -= tmp_ncep;
     131    }
     132    nfr = feat_s2mfc2feat_live(fcb, cptr, &ncep, FALSE, TRUE, fptr);
     133    cptr += ncep;
     134    fptr += nfr;
     135    TEST_EQUAL(cptr - cepbuf, total_frames);
     136    TEST_EQUAL(fptr - featbuf2, total_frames);
     137
     138    /* Now verify that the results are equal. */
     139    for (i = 0; i < total_frames; ++i) {
     140        int32 j;
     141        printf("%-4d ", i);
     142        for (j = 0; j < feat_dimension(fcb); ++j)
     143            TEST_EQUAL_FLOAT(featbuf1[i][0][j], featbuf2[i][0][j]);
     144        if (i % 10 == 9)
     145            printf("\n");
     146    }
     147    printf("\n");
     148
     149    fclose(raw);
     150    fe_free(fe);
     151    feat_array_free(featbuf1);
     152    feat_array_free(featbuf2);
     153    feat_free(fcb);
     154    ckd_free_2d(cepbuf);
     155
     156    return 0;
     157}
  • src/tests/libs/sphinx/test_feat/test_macros.h

     
     1#include <stdio.h>
     2#include <math.h>
     3
     4#include "logmath.h"
     5
     6#define EPSILON 0.01
     7#define TEST_ASSERT(x) if (!(x)) { fprintf(stderr, "FAIL: %s\n", #x); exit(1); }
     8#define TEST_EQUAL(a,b) TEST_ASSERT((a) == (b))
     9#define TEST_EQUAL_FLOAT(a,b) TEST_ASSERT(fabs((a) - (b)) < EPSILON)
     10#define LOG_EPSILON 20
     11#define TEST_EQUAL_LOG(a,b) TEST_ASSERT(abs((a) - (b)) < LOG_EPSILON)
  • src/tests/libs/sphinx/test_feat/_test_feat.test

     
     1#!/bin/sh
     2. ../testfuncs.sh
     3
     4set -e
     5./test_feat > _test_feat.out
     6compare_table feat _test_feat.out $tests/unit/test_feat/_test_feat.res
     7rm -f _test_feat.out
  • src/tests/libs/sphinx/test_feat/test_feat_live.c

    Property changes on: src/tests/libs/sphinx/test_feat/_test_feat.test
    ___________________________________________________________________
    Added: svn:executable
       + *
    
     
     1#ifdef HAVE_CONFIG_H
     2#include <config.h>
     3#endif
     4
     5#include <stdio.h>
     6#include <string.h>
     7#include <math.h>
     8
     9#include "feat.h"
     10#include "ckd_alloc.h"
     11#include "test_macros.h"
     12
     13const mfcc_t data[6][13] = {
     14    { FLOAT2MFCC(15.114), FLOAT2MFCC(-1.424), FLOAT2MFCC(-0.953),
     15      FLOAT2MFCC(0.186), FLOAT2MFCC(-0.656), FLOAT2MFCC(-0.226),
     16      FLOAT2MFCC(-0.105), FLOAT2MFCC(-0.412), FLOAT2MFCC(-0.024),
     17      FLOAT2MFCC(-0.091), FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.158), FLOAT2MFCC(-0.197)},
     18    { FLOAT2MFCC(14.729), FLOAT2MFCC(-1.313), FLOAT2MFCC(-0.892),
     19      FLOAT2MFCC(0.140), FLOAT2MFCC(-0.676), FLOAT2MFCC(-0.089),
     20      FLOAT2MFCC(-0.313), FLOAT2MFCC(-0.422), FLOAT2MFCC(-0.058),
     21      FLOAT2MFCC(-0.101), FLOAT2MFCC(-0.100), FLOAT2MFCC(-0.128), FLOAT2MFCC(-0.123)},
     22    { FLOAT2MFCC(14.502), FLOAT2MFCC(-1.351), FLOAT2MFCC(-1.028),
     23      FLOAT2MFCC(-0.189), FLOAT2MFCC(-0.718), FLOAT2MFCC(-0.139),
     24      FLOAT2MFCC(-0.121), FLOAT2MFCC(-0.365), FLOAT2MFCC(-0.139),
     25      FLOAT2MFCC(-0.154), FLOAT2MFCC(0.041), FLOAT2MFCC(0.009), FLOAT2MFCC(-0.073)},
     26    { FLOAT2MFCC(14.557), FLOAT2MFCC(-1.676), FLOAT2MFCC(-0.864),
     27      FLOAT2MFCC(0.118), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.168),
     28      FLOAT2MFCC(-0.069), FLOAT2MFCC(-0.503), FLOAT2MFCC(-0.013),
     29      FLOAT2MFCC(0.007), FLOAT2MFCC(-0.056), FLOAT2MFCC(-0.075), FLOAT2MFCC(-0.237)},
     30    { FLOAT2MFCC(14.665), FLOAT2MFCC(-1.498), FLOAT2MFCC(-0.582),
     31      FLOAT2MFCC(0.209), FLOAT2MFCC(-0.487), FLOAT2MFCC(-0.247),
     32      FLOAT2MFCC(-0.142), FLOAT2MFCC(-0.439), FLOAT2MFCC(0.059),
     33      FLOAT2MFCC(-0.058), FLOAT2MFCC(-0.265), FLOAT2MFCC(-0.109), FLOAT2MFCC(-0.196)},
     34    { FLOAT2MFCC(15.025), FLOAT2MFCC(-1.199), FLOAT2MFCC(-0.607),
     35      FLOAT2MFCC(0.235), FLOAT2MFCC(-0.499), FLOAT2MFCC(-0.080),
     36      FLOAT2MFCC(-0.062), FLOAT2MFCC(-0.554), FLOAT2MFCC(-0.209),
     37      FLOAT2MFCC(-0.124), FLOAT2MFCC(-0.445), FLOAT2MFCC(-0.352), FLOAT2MFCC(-0.400)},
     38};
     39
     40int
     41main(int argc, char *argv[])
     42{
     43    feat_t *fcb;
     44    mfcc_t **in_feats, ***out_feats, ***out_feats2, ***optr;
     45    int32 i, j, ncep, nfr, nfr1, nfr2;
     46
     47    in_feats = (mfcc_t **)ckd_alloc_2d_ptr(6, 13, data, sizeof(mfcc_t));
     48    out_feats = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
     49    /* Test 1s_c_d_dd features */
     50    fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
     51    ncep = 6;
     52    nfr1 = feat_s2mfc2feat_live(fcb, in_feats, &ncep, 1, 1, out_feats);
     53    printf("Processed %d input %d output frames\n", ncep, nfr1);
     54    for (i = 0; i < nfr1; ++i) {
     55        printf("%d: ", i);
     56        for (j = 0; j < 39; ++j) {
     57            printf("%.3f ", MFCC2FLOAT(out_feats[i][0][j]));
     58        }
     59        printf("\n");
     60    }
     61    feat_free(fcb);
     62
     63    /* Test in "live" mode. */
     64    fcb = feat_init("1s_c_d_dd", CMN_NONE, 0, AGC_NONE, 1, 13);
     65    optr = out_feats2 = (mfcc_t ***)ckd_calloc_3d(8, 1, 39, sizeof(mfcc_t));
     66    nfr2 = 0;
     67    ncep = 2;
     68    nfr = feat_s2mfc2feat_live(fcb, in_feats, &ncep, TRUE, FALSE, optr);
     69    printf("Processed %d input %d output frames\n", ncep, nfr);
     70    nfr2 += nfr;
     71    for (i = 0; i < nfr; ++i) {
     72        printf("%d: ", i);
     73        for (j = 0; j < 39; ++j) {
     74            printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
     75        }
     76        printf("\n");
     77    }
     78    optr += nfr;
     79
     80    ncep = 2;
     81    nfr = feat_s2mfc2feat_live(fcb, in_feats + 2, &ncep, FALSE, FALSE, optr);
     82    nfr2 += nfr;
     83    printf("Processed %d input %d output frames\n", ncep, nfr);
     84    for (i = 0; i < nfr; ++i) {
     85        printf("%d: ", i);
     86        for (j = 0; j < 39; ++j) {
     87            printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
     88        }
     89        printf("\n");
     90    }
     91    optr += nfr;
     92
     93    ncep = 2;
     94    nfr = feat_s2mfc2feat_live(fcb, in_feats + 4, &ncep, FALSE, TRUE, optr);
     95    nfr2 += nfr;
     96    printf("Processed %d input %d output frames\n", ncep, nfr);
     97    for (i = 0; i < nfr; ++i) {
     98        printf("%d: ", i);
     99        for (j = 0; j < 39; ++j) {
     100            printf("%.3f ", MFCC2FLOAT(optr[i][0][j]));
     101        }
     102        printf("\n");
     103    }
     104    optr += nfr;
     105    feat_free(fcb);
     106
     107    TEST_EQUAL(nfr1, nfr2);
     108    for (i = 0; i < nfr1; ++i) {
     109        for (j = 0; j < 39; ++j) {
     110            TEST_EQUAL(out_feats[i][0][j], out_feats2[i][0][j]);
     111        }
     112    }
     113    ckd_free_3d(out_feats2);
     114    ckd_free_3d(out_feats);
     115    ckd_free(in_feats);
     116
     117    return 0;
     118}