require 'libkaldiio' require 'speech_utils' local KaldiReader = nerv.class("nerv.KaldiReader", "nerv.DataReader") function KaldiReader:__init(global_conf, reader_conf) self.feat_id = reader_conf.id self.frm_ext = reader_conf.frm_ext self.gconf = global_conf self.debug = global_conf.debug if self.debug == nil then self.debug = false end self.feat_repo = nerv.KaldiFeatureRepo(reader_conf.feature_rspecifier) self.lab_repo = {} for id, mlf_spec in pairs(reader_conf.mlfs) do if mlf_spec.format == nil then nerv.error("format spec is expected for label %s", id) end self.lab_repo[id] = nerv.KaldiLabelRepo(mlf_spec.targets_rspecifier, mlf_spec.format) end self.lookup_repo = {} for id, lookup_spec in pairs(reader_conf.lookup) do if lookup_spec.map_rspecifier == nil then nerv.error("map spec is expected for lookup %s", id) end self.lookup_repo[id] = nerv.KaldiLookupFeatureRepo(lookup_spec.targets_rspecifier, lookup_spec.map_rspecifier) end end function KaldiReader:get_data() if self.feat_repo:is_end() then return nil end local res = {} -- read Kaldi feature local feat_utter = self.feat_repo:cur_utter(self.debug) -- global transf res[self.feat_id] = feat_utter -- add corresponding labels for id, repo in pairs(self.lab_repo) do local lab_utter = repo:get_utter(self.feat_repo, feat_utter:nrow(), self.debug) res[id] = lab_utter end -- add corresponding lookup features for id, repo in pairs(self.lookup_repo) do local lookup_utter = repo:get_utter(self.feat_repo, feat_utter:nrow(), self.debug) local nrow = lookup_utter:nrow() if nrow < feat_utter:nrow() then -- repeat the last frame local nlu = lookup_utter:create(feat_utter:nrow()) nlu:copy_from(lookup_utter, 0, nrow) row = nlu[nrow - 1] for i = 0, feat_utter:nrow() - nrow - 1 do nlu:copy_from(nlu, nrow - 1, nrow, nrow + i) end lookup_utter = nlu end res[id] = lookup_utter end -- move the pointer to next self.feat_repo:next() collectgarbage("collect") return res end