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