File: test_ps.c

package info (click to toggle)
pocketsphinx 5.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,236 kB
  • sloc: ansic: 54,519; python: 2,438; sh: 566; cpp: 410; perl: 342; yacc: 93; lex: 50; makefile: 30
file content (162 lines) | stat: -rw-r--r-- 5,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <pocketsphinx.h>
#include <stdio.h>
#include <string.h>

#include "pocketsphinx_internal.h"

#include "test_macros.h"

int
ps_decoder_test(cmd_ln_t *config, char const *sname, char const *expected)
{
    ps_decoder_t *ps;
    mfcc_t **cepbuf;
    float32 **floatbuf;
    FILE *rawfh;
    int16 *buf;
    int16 const *bptr;
    size_t nread;
    size_t nsamps;
    int32 nfr, i, score, prob;
    char const *hyp;
    double n_speech, n_cpu, n_wall;
    ps_seg_t *seg;
    char *prev_cmn;

    TEST_ASSERT(ps = ps_init(config));
    prev_cmn = ckd_salloc(ps_get_cmn(ps, FALSE));
    printf("CMN: %s\n", prev_cmn);
    err_set_loglevel(ERR_INFO);
    /* Test it first with pocketsphinx_decode_raw() */
    TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb"));
    ps_decode_raw(ps, rawfh, -1);
    hyp = ps_get_hyp(ps, &score);
    prob = ps_get_prob(ps);
    printf("%s: %s (%d, %d)\n", sname, hyp, score, prob);
    TEST_EQUAL(0, strcmp(hyp, expected));
    TEST_ASSERT(prob <= 0);
    ps_get_utt_time(ps, &n_speech, &n_cpu, &n_wall);
    printf("%.2f seconds speech, %.2f seconds CPU, %.2f seconds wall\n",
           n_speech, n_cpu, n_wall);
    printf("%.2f xRT (CPU), %.2f xRT (elapsed)\n",
           n_cpu / n_speech, n_wall / n_speech);
    TEST_ASSERT(0 != strcmp(prev_cmn, ps_get_cmn(ps, FALSE)));
    ckd_free(prev_cmn);
    prev_cmn = ckd_salloc(ps_get_cmn(ps, FALSE));
    printf("CMN: %s\n", prev_cmn);

    /* Test it with ps_process_raw() */
    clearerr(rawfh);
    fseek(rawfh, 0, SEEK_END);
    nsamps = ftell(rawfh) / sizeof(*buf);
    fseek(rawfh, 0, SEEK_SET);
    ps_start_stream(ps);
    TEST_EQUAL(0, ps_start_utt(ps));
    nsamps = 2048;
    buf = ckd_calloc(nsamps, sizeof(*buf));
    i = 0;
    while (!feof(rawfh)) {
        nread = fread(buf, sizeof(*buf), nsamps, rawfh);
        ps_process_raw(ps, buf, nread, FALSE, FALSE);
        if (i++ == 1) {
            /* Test updating CMN while decoding */
            TEST_ASSERT(0 != strcmp(prev_cmn, ps_get_cmn(ps, TRUE)));
            ckd_free(prev_cmn);
            prev_cmn = ckd_salloc(ps_get_cmn(ps, FALSE));
            printf("CMN: %s\n", prev_cmn);
        }
    }
    TEST_EQUAL(0, ps_end_utt(ps));
    hyp = ps_get_hyp(ps, &score);
    prob = ps_get_prob(ps);
    printf("%s: %s (%d, %d)\n", sname, hyp, score, prob);
    TEST_EQUAL(0, strcmp(hyp, expected));
    ps_get_utt_time(ps, &n_speech, &n_cpu, &n_wall);
    printf("%.2f seconds speech, %.2f seconds CPU, %.2f seconds wall\n",
           n_speech, n_cpu, n_wall);
    printf("%.2f xRT (CPU), %.2f xRT (elapsed)\n",
           n_cpu / n_speech, n_wall / n_speech);

    /* Now read the whole file and produce an MFCC buffer. */
    ps_start_stream(ps);
    clearerr(rawfh);
    fseek(rawfh, 0, SEEK_END);
    nsamps = ftell(rawfh) / sizeof(*buf);
    fseek(rawfh, 0, SEEK_SET);
    bptr = buf = ckd_realloc(buf, nsamps * sizeof(*buf));
    TEST_EQUAL(nsamps, fread(buf, sizeof(*buf), nsamps, rawfh));
    fe_process_frames(ps->acmod->fe, &bptr, &nsamps, NULL, &nfr);
    cepbuf = ckd_calloc_2d(nfr + 1,
                   fe_get_output_size(ps->acmod->fe),
                   sizeof(**cepbuf));
    fe_start_utt(ps->acmod->fe);
    fe_process_frames(ps->acmod->fe, &bptr, &nsamps, cepbuf, &nfr);
    fe_end_utt(ps->acmod->fe, cepbuf[nfr], &i);

    /* Decode it with process_cep() */
#ifdef FIXED_POINT
    floatbuf = ckd_calloc_2d(nfr + 1,
                             fe_get_output_size(ps->acmod->fe),
                             sizeof(**floatbuf));
    fe_mfcc_to_float(ps->acmod->fe, cepbuf, floatbuf, nfr + 1);
#else
    floatbuf = cepbuf;
#endif

    TEST_EQUAL(0, ps_start_utt(ps));
    for (i = 0; i < nfr; ++i) {
        ps_process_cep(ps, floatbuf + i, 1, FALSE, FALSE);
        if (i == nfr - 5) {
            /* Test updating CMN while decoding */
            TEST_ASSERT(0 != strcmp(prev_cmn, ps_get_cmn(ps, TRUE)));
            ckd_free(prev_cmn);
            prev_cmn = ckd_salloc(ps_get_cmn(ps, FALSE));
            printf("CMN: %s\n", prev_cmn);
        }
    }
    TEST_EQUAL(0, ps_end_utt(ps));
    hyp = ps_get_hyp(ps, &score);
    prob = ps_get_prob(ps);
    printf("%s: %s (%d, %d)\n", sname, hyp, score, prob);
    TEST_EQUAL(0, strcmp(hyp, expected));
    TEST_ASSERT(prob <= 0);
    for (seg = ps_seg_iter(ps); seg;
         seg = ps_seg_next(seg)) {
        char const *word;
        int sf, ef;
        int32 post, lscr, ascr, lback;

        word = ps_seg_word(seg);
        ps_seg_frames(seg, &sf, &ef);
        post = ps_seg_prob(seg, &ascr, &lscr, &lback);
        printf("%s (%d:%d) P(w|o) = %f ascr = %d lscr = %d lback = %d post=%d\n", word, sf, ef,
               logmath_exp(ps_get_logmath(ps), post), ascr, lscr, lback, post);
    }

    ps_get_utt_time(ps, &n_speech, &n_cpu, &n_wall);
    printf("%.2f seconds speech, %.2f seconds CPU, %.2f seconds wall\n",
           n_speech, n_cpu, n_wall);
    printf("%.2f xRT (CPU), %.2f xRT (elapsed)\n",
           n_cpu / n_speech, n_wall / n_speech);
    ps_get_all_time(ps, &n_speech, &n_cpu, &n_wall);
    printf("TOTAL: %.2f seconds speech, %.2f seconds CPU, %.2f seconds wall\n",
           n_speech, n_cpu, n_wall);
    printf("TOTAL: %.2f xRT (CPU), %.2f xRT (elapsed)\n",
           n_cpu / n_speech, n_wall / n_speech);
    TEST_ASSERT(0 != strcmp(prev_cmn, ps_get_cmn(ps, FALSE)));
    ckd_free(prev_cmn);
    prev_cmn = ckd_salloc(ps_get_cmn(ps, FALSE));
    printf("CMN: %s\n", prev_cmn);

    fclose(rawfh);
    ps_free(ps);
    ps_config_free(config);
#ifdef FIXED_POINT
    ckd_free_2d(floatbuf);
#endif
    ckd_free_2d(cepbuf);
    ckd_free(buf);
    ckd_free(prev_cmn);

    return 0;
}