summaryrefslogtreecommitdiff
path: root/kaldi_io/src/init.c
blob: efe3ff7cbfaba5aa03e051449125da696a71c2ff (plain) (blame)
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
#include "nerv/lib/common.h"
#include "nerv/matrix/matrix.h"
#include "cwrapper_kaldi.h"
#include <stdio.h>

const char *nerv_kaldi_feat_repo_tname = "nerv.KaldiFeatureRepo";
const char *nerv_kaldi_lookup_feat_repo_tname = "nerv.KaldiLookupFeatureRepo";
const char *nerv_kaldi_label_repo_tname = "nerv.KaldiLabelRepo";
const char *nerv_matrix_host_float_tname = "nerv.MMatrixFloat";

static int feat_repo_new(lua_State *L) {
    const char *feature_rsepcifier = luaL_checkstring(L, 1);
    KaldiFeatureRepo *repo = kaldi_feature_repo_new(feature_rsepcifier);
    luaT_pushudata(L, repo, nerv_kaldi_feat_repo_tname);
    return 1;
}

static int feat_repo_destroy(lua_State *L) {
    KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
    kaldi_feature_repo_destroy(repo);
    return 0;
}

static int feat_repo_current_utterance(lua_State *L) {
    MContext *context;
    MMATRIX_GET_CONTEXT(L, 3);
    KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
    int debug;
    if (!lua_isboolean(L, 2))
        nerv_error(L, "debug flag should be a boolean");
    debug = lua_toboolean(L, 2);
    Matrix *utter = kaldi_feature_repo_read_utterance(repo, L, debug, context);
    luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
    return 1;
}

static int feat_repo_next(lua_State *L) {
    KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
    kaldi_feature_repo_next(repo);
    return 0;
}

static int feat_repo_is_end(lua_State *L) {
    KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
    lua_pushboolean(L, kaldi_feature_repo_is_end(repo));
    return 1;
}

static int feat_repo_key(lua_State *L) {
    KaldiFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_feat_repo_tname);
    lua_pushstring(L, kaldi_feature_repo_key(repo));
    return 1;
}

static const luaL_Reg feat_repo_methods[] = {
    {"cur_utter", feat_repo_current_utterance},
    {"next", feat_repo_next},
    {"is_end", feat_repo_is_end},
    {"key", feat_repo_key},
    {NULL, NULL}
};

static int lookup_feat_repo_new(lua_State *L) {
    const char *feature_rsepcifier = luaL_checkstring(L, 1);
    const char *map_rspecifier = luaL_checkstring(L, 2);
    KaldiLookupFeatureRepo *repo = kaldi_lookup_feature_repo_new(feature_rsepcifier, map_rspecifier);
    luaT_pushudata(L, repo, nerv_kaldi_lookup_feat_repo_tname);
    return 1;
}

static int lookup_feat_repo_destroy(lua_State *L) {
    KaldiLookupFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_lookup_feat_repo_tname);
    kaldi_lookup_feature_repo_destroy(repo);
    return 0;
}

static int lookup_feat_repo_read_utterance(lua_State *L) {
    MContext *context;
    MMATRIX_GET_CONTEXT(L, 5);
    KaldiLookupFeatureRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_lookup_feat_repo_tname);
    KaldiFeatureRepo *feat_repo = luaT_checkudata(L, 2, nerv_kaldi_feat_repo_tname);
    int nframes, debug;
    if (!lua_isnumber(L, 3))
        nerv_error(L, "nframes should be a number");
    nframes = lua_tonumber(L, 3);
    if (!lua_isboolean(L, 4))
        nerv_error(L, "debug flag should be a boolean");
    debug = lua_toboolean(L, 4);
    Matrix *utter = kaldi_lookup_feature_repo_read_utterance(repo, feat_repo,
                                                            nframes, L, debug, context);
    luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
    return 1;
}

static const luaL_Reg lookup_feat_repo_methods[] = {
    {"get_utter", lookup_feat_repo_read_utterance},
    {NULL, NULL}
};

static int label_repo_new(lua_State *L) {
    const char *targets_rspecifier = luaL_checkstring(L, 1);
    KaldiLabelRepo *repo = kaldi_label_repo_new(targets_rspecifier);
    luaT_pushudata(L, repo, nerv_kaldi_label_repo_tname);
    return 1;
}

static int label_repo_read_utterance(lua_State *L) {
    MContext *context;
    MMATRIX_GET_CONTEXT(L, 5);
    KaldiLabelRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_label_repo_tname);
    KaldiFeatureRepo *feat_repo = luaT_checkudata(L, 2, nerv_kaldi_feat_repo_tname);
    int nframes, debug;
    if (!lua_isnumber(L, 3))
        nerv_error(L, "nframes should be a number");
    nframes = lua_tonumber(L, 3);
    if (!lua_isboolean(L, 4))
        nerv_error(L, "debug flag should be a boolean");
    debug = lua_toboolean(L, 4);
    Matrix *utter = kaldi_label_repo_read_utterance(repo, feat_repo, nframes,
                                                    L, debug, context);
    luaT_pushudata(L, utter, nerv_matrix_host_float_tname);
    return 1;
}

static int label_repo_destroy(lua_State *L) {
    KaldiLabelRepo *repo = luaT_checkudata(L, 1, nerv_kaldi_label_repo_tname);
    kaldi_label_repo_destroy(repo);
    return 0;
}

static const luaL_Reg label_repo_methods[] = {
    {"get_utter", label_repo_read_utterance},
    {NULL, NULL}
};

static void feat_repo_init(lua_State *L) {
    luaT_newmetatable(L, nerv_kaldi_feat_repo_tname, NULL,
                        feat_repo_new, feat_repo_destroy, NULL);
    luaL_register(L, NULL, feat_repo_methods);
    lua_pop(L, 1);
}

static void lookup_feat_repo_init(lua_State *L) {
    luaT_newmetatable(L, nerv_kaldi_lookup_feat_repo_tname, NULL,
                        lookup_feat_repo_new, lookup_feat_repo_destroy, NULL);
    luaL_register(L, NULL, lookup_feat_repo_methods);
    lua_pop(L, 1);
}

static void label_repo_init(lua_State *L) {
    luaT_newmetatable(L, nerv_kaldi_label_repo_tname, NULL,
                        label_repo_new, label_repo_destroy, NULL);
    luaL_register(L, NULL, label_repo_methods);
    lua_pop(L, 1);
}

void kaldi_io_init(lua_State *L) {
    feat_repo_init(L);
    lookup_feat_repo_init(L);
    label_repo_init(L);
}