summaryrefslogtreecommitdiff
path: root/kaldi_io/src/cwrapper_kaldi.cpp
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2015-08-14 11:51:42 +0800
committerDeterminant <[email protected]>2015-08-14 11:51:42 +0800
commit96a32415ab43377cf1575bd3f4f2980f58028209 (patch)
tree30a2d92d73e8f40ac87b79f6f56e227bfc4eea6e /kaldi_io/src/cwrapper_kaldi.cpp
parentc177a7549bd90670af4b29fa813ddea32cfe0f78 (diff)
add implementation for kaldi io (by ymz)
Diffstat (limited to 'kaldi_io/src/cwrapper_kaldi.cpp')
-rw-r--r--kaldi_io/src/cwrapper_kaldi.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/kaldi_io/src/cwrapper_kaldi.cpp b/kaldi_io/src/cwrapper_kaldi.cpp
new file mode 100644
index 0000000..3dd055f
--- /dev/null
+++ b/kaldi_io/src/cwrapper_kaldi.cpp
@@ -0,0 +1,111 @@
+#include <string>
+#include "kaldi/base/kaldi-common.h"
+#include "kaldi/hmm/posterior.h"
+#include "kaldi/util/table-types.h"
+typedef kaldi::BaseFloat BaseFloat;
+
+extern "C" {
+#include "cwrapper_kaldi.h"
+#include "string.h"
+#include "assert.h"
+#include "nerv/common.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);
+
+ struct KaldiFeatureRepo {
+ kaldi::SequentialBaseFloatMatrixReader* feature_reader;
+ string utt;
+ };
+
+ KaldiFeatureRepo *kaldi_feature_repo_new(const char *feature_rspecifier) {
+ KaldiFeatureRepo *repo = new KaldiFeatureRepo();
+ repo->feature_reader = new kaldi::SequentialBaseFloatMatrixReader(string(feature_rspecifier));
+ return repo;
+ }
+
+ Matrix *kaldi_feature_repo_read_utterance(KaldiFeatureRepo *repo, lua_State *L, int debug) {
+ Matrix *mat; /* nerv implementation */
+
+ repo->utt = repo->feature_reader->Key();
+ kaldi::Matrix<BaseFloat> kmat = repo->feature_reader->Value();
+
+ int n = kmat.NumRows();
+ int m = kmat.NumCols();
+ Status status;
+ assert(sizeof(BaseFloat) == sizeof(float));
+ if(sizeof(BaseFloat) == sizeof(float))
+ mat = nerv_matrix_host_float_create(n, m, &status);
+ else if(sizeof(BaseFloat) == sizeof(double))
+ mat = nerv_matrix_host_double_create(n, m, &status);
+ NERV_LUA_CHECK_STATUS(L, status);
+ size_t stride = mat->stride;
+ if (debug)
+ fprintf(stderr, "[kaldi] feature: %s %d %d\n", repo->utt.c_str(), n, m);
+
+ for (int i = 0; i < n; i++)
+ {
+ const BaseFloat *row = kmat.RowData(i);
+ BaseFloat *nerv_row = (BaseFloat *)((char *)mat->data.f + i * stride);
+ /* use memmove to copy the row, since KaldiLib uses compact storage */
+ memmove(nerv_row, row, sizeof(BaseFloat) * m);
+ }
+ return mat;
+ }
+
+ void kaldi_feature_repo_next(KaldiFeatureRepo *repo) {
+ repo->feature_reader->Next();
+ }
+
+ int kaldi_feature_repo_is_end(KaldiFeatureRepo *repo) {
+ return repo->feature_reader->Done();
+ }
+
+ void kaldi_feature_repo_destroy(KaldiFeatureRepo *repo) {
+ if (repo->feature_reader)
+ delete repo->feature_reader;
+ delete repo;
+ }
+
+ struct KaldiLabelRepo {
+ kaldi::RandomAccessPosteriorReader *targets_reader;
+ };
+
+ KaldiLabelRepo *kaldi_label_repo_new(const char *targets_rspecifier, const char *fmt) {
+ KaldiLabelRepo *repo = new KaldiLabelRepo();
+ repo->targets_reader = new kaldi::RandomAccessPosteriorReader(string(targets_rspecifier));
+ return repo;
+ }
+
+ Matrix *kaldi_label_repo_read_utterance(KaldiLabelRepo *repo, KaldiFeatureRepo *frepo, int frm_ext, int nframes,
+ lua_State *L,
+ int debug) {
+ Matrix *mat;
+ kaldi::Posterior targets = repo->targets_reader->Value(frepo->utt);
+
+ int n = targets.size() < nframes ? targets.size() : nframes;
+ int m = (int)targets[0].size();
+
+ Status status;
+ assert(sizeof(BaseFloat) == sizeof(float));
+ if(sizeof(BaseFloat) == sizeof(float))
+ mat = nerv_matrix_host_float_create(n, m, &status);
+ else if(sizeof(BaseFloat) == sizeof(double))
+ mat = nerv_matrix_host_double_create(n, m, &status);
+ NERV_LUA_CHECK_STATUS(L, status);
+ size_t stride = mat->stride;
+
+ if (debug)
+ fprintf(stderr, "[kaldi] label: %s %d %d\n", frepo->utt.c_str(), n, m);
+ for (int i = 0; i < n; i++)
+ for(int j = 0; j < m; j++)
+ *((BaseFloat *)((char *)mat->data.f + (i * stride + j))) = (BaseFloat)targets[i][j].first;
+ return mat;
+ }
+
+ void kaldi_label_repo_destroy(KaldiLabelRepo *repo) {
+ if(repo->targets_reader)
+ delete repo->targets_reader;
+ delete repo;
+ }
+}