diff options
Diffstat (limited to 'kaldi_io/src/cwrapper_kaldi.cpp')
-rw-r--r-- | kaldi_io/src/cwrapper_kaldi.cpp | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/kaldi_io/src/cwrapper_kaldi.cpp b/kaldi_io/src/cwrapper_kaldi.cpp index 542f1d0..788128b 100644 --- a/kaldi_io/src/cwrapper_kaldi.cpp +++ b/kaldi_io/src/cwrapper_kaldi.cpp @@ -10,10 +10,11 @@ extern "C" { #include "cwrapper_kaldi.h" #include "string.h" #include "assert.h" -#include "nerv/common.h" +#include "nerv/lib/common.h" +#include "nerv/lib/matrix/mmatrix.h" - extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status); - extern Matrix *nerv_matrix_host_double_create(long nrow, long ncol, Status *status); + extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, MContext *context, Status *status); + extern Matrix *nerv_matrix_host_double_create(long nrow, long ncol, MContext *context, Status *status); struct KaldiFeatureRepo { kaldi::SequentialBaseFloatMatrixReader* feature_reader; @@ -26,7 +27,8 @@ extern "C" { return repo; } - Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L, int debug) { + Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L, + int debug, MContext *context) { Matrix *mat; /* nerv implementation */ repo->utt = repo->feature_reader->Key(); @@ -37,9 +39,9 @@ extern "C" { Status status; assert(sizeof(BaseFloat) == sizeof(float)); if(sizeof(BaseFloat) == sizeof(float)) - mat = nerv_matrix_host_float_create(n, m, &status); + mat = nerv_matrix_host_float_create(n, m, context, &status); else if(sizeof(BaseFloat) == sizeof(double)) - mat = nerv_matrix_host_double_create(n, m, &status); + mat = nerv_matrix_host_double_create(n, m, context, &status); NERV_LUA_CHECK_STATUS(L, status); size_t stride = mat->stride; if (debug) @@ -80,26 +82,33 @@ extern "C" { KaldiLookupFeatureRepo *kaldi_lookup_feature_repo_new(const char *feature_rspecifier, const char *map_rspecifier) { KaldiLookupFeatureRepo *repo = new KaldiLookupFeatureRepo(); - kaldi::SequentialBaseFloatMatrixReader feature_reader = kaldi::SequentialBaseFloatMatrixReader(string(feature_rspecifier)); - for (; !feature_reader.Done(); feature_reader.Next()) + kaldi::SequentialBaseFloatMatrixReader *feature_reader = \ + new kaldi::SequentialBaseFloatMatrixReader(string(feature_rspecifier)); + for (; !feature_reader->Done(); feature_reader->Next()) { - const std::string &key = feature_reader.Key(); - const kaldi::Matrix<BaseFloat> &feat = feature_reader.Value(); + const std::string &key = feature_reader->Key(); + const kaldi::Matrix<BaseFloat> &feat = feature_reader->Value(); if (repo->key2mat.find(key) != repo->key2mat.end()) fprintf(stderr, "[kaldi] warning: lookup feature for key %s already exists", key.c_str()); repo->key2mat[key] = feat; } - kaldi::SequentialTokenVectorReader map_reader = kaldi::SequentialTokenVectorReader(string(map_rspecifier)); - for (; !map_reader.Done(); map_reader.Next()) + delete feature_reader; + kaldi::SequentialTokenVectorReader *map_reader = \ + new kaldi::SequentialTokenVectorReader(string(map_rspecifier)); + for (; !map_reader->Done(); map_reader->Next()) { - const std::vector<std::string> target = map_reader.Value(); + const std::vector<std::string> target = map_reader->Value(); assert(target.size() >= 1); - repo->map[map_reader.Key()] = *target.begin(); + repo->map[map_reader->Key()] = *target.begin(); } + delete map_reader; return repo; } - Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo, KaldiFeatureRepo *frepo, int nframes, lua_State *L, int debug) { + Matrix *kaldi_lookup_feature_repo_read_utterance(KaldiLookupFeatureRepo *repo, + KaldiFeatureRepo *frepo, + int nframes, lua_State *L, + int debug, MContext *context) { Matrix *mat; /* nerv implementation */ StringToString_t::iterator mit = repo->map.find(frepo->utt); if (mit == repo->map.end()) @@ -115,9 +124,9 @@ extern "C" { Status status; assert(sizeof(BaseFloat) == sizeof(float)); if(sizeof(BaseFloat) == sizeof(float)) - mat = nerv_matrix_host_float_create(n, m, &status); + mat = nerv_matrix_host_float_create(n, m, context, &status); else if(sizeof(BaseFloat) == sizeof(double)) - mat = nerv_matrix_host_double_create(n, m, &status); + mat = nerv_matrix_host_double_create(n, m, context, &status); NERV_LUA_CHECK_STATUS(L, status); size_t stride = mat->stride; if (debug) @@ -149,13 +158,14 @@ extern "C" { Matrix *kaldi_label_repo_read_utterance(KaldiLabelRepo *repo, KaldiFeatureRepo *frepo, int nframes, lua_State *L, - int debug) { + int debug, MContext *context) { Matrix *mat = NULL; /* check if the alignment of the utterance exists, otherwise return NULL */ kaldi::Posterior targets; if (repo->targets_reader->HasKey(frepo->utt)) targets = repo->targets_reader->Value(frepo->utt); - else return mat; + else + return mat; int n = targets.size() < nframes ? targets.size() : nframes; int m = (int)targets[0].size(); @@ -163,9 +173,9 @@ extern "C" { Status status; assert(sizeof(BaseFloat) == sizeof(float)); if(sizeof(BaseFloat) == sizeof(float)) - mat = nerv_matrix_host_float_create(n, m, &status); + mat = nerv_matrix_host_float_create(n, m, context, &status); else if(sizeof(BaseFloat) == sizeof(double)) - mat = nerv_matrix_host_double_create(n, m, &status); + mat = nerv_matrix_host_double_create(n, m, context, &status); NERV_LUA_CHECK_STATUS(L, status); size_t stride = mat->stride; if (debug) |