├── .gitignore ├── LICENSE ├── bashrc └── src ├── Makefile.am ├── README ├── align ├── Makefile.am ├── align-info.h ├── phone-to-word.cc ├── phone-to-word.h ├── run.sh ├── test-align-info.cc └── test-phone-to-word.cc ├── arm.mk ├── audio-convert ├── makefile ├── stream2pcm.cc └── stream2pcm.h ├── biglm ├── Makefile.am └── lattice-read-and-write.cc ├── build.sh ├── client ├── Makefile.am ├── asr-client-task.cc ├── asr-client-task.h ├── asr-client-thread.h └── py-client │ ├── Makefile.am │ ├── asr-client-api-test.cc │ ├── asr-client-api.cc │ ├── asr-client-api.h │ └── client.py ├── clientbin ├── Makefile.am └── thread-client.cc ├── configure.ac ├── depend.mk ├── fst_format_convert_tool ├── Makefile.am ├── README.txt ├── convert_fst.c ├── convert_hmm.c ├── fst.h ├── fst_io.h ├── read_fst.c └── write_fst.c ├── gpu-asr ├── Makefile.am ├── README ├── conf │ ├── config.txt │ ├── fbank.80.conf │ └── socket.conf ├── gpu-service-run.sh ├── gpu-worker-pool-itf.h ├── v1-gpu-asr-service.cc ├── v1-gpu-asr-task.h ├── v1-gpu-asr-work-thread.h └── v1-gpu-kaldi-worker-pool.h ├── hmm ├── Makefile.am ├── hmm-topology.cc ├── hmm-topology.h ├── kaldi.final.mdl ├── test-hmm.cc ├── test-transition-model.cc ├── transition-model.cc └── transition-model.h ├── hubo.mk ├── itf └── decodable-itf.h ├── kaldi-bin ├── Makefile.am └── bin │ ├── Makefile.am │ ├── gpu-forward.cc │ ├── latgen-faster.cc │ ├── lattice-best-path-score.cc │ ├── matrix-row-min.cc │ └── nbest-compute-wer.cc ├── kaldi-nnet3 ├── Makefile.am ├── config-decoder.txt ├── kaldi-online-nnet3-my-decoder-test.cc ├── kaldi-online-nnet3-my-decoder.cc ├── kaldi-online-nnet3-my-decoder.h └── run.sh ├── kaldi-nnet3bin ├── Makefile.am ├── kaldi-hclg-my-decoder-biglm.cc ├── kaldi-hclg-my-decoder.cc ├── kaldi-my-decoder.cc ├── kaldi-online-nnet3-my-decoder-bin.cc └── run.sh ├── lib └── cblas.h ├── my-decoder ├── Makefile.am ├── clg-fst.h ├── decoder-itf.h ├── lattice-faster-decoder-conf.h ├── lattice-faster-decoder.cc ├── lattice-faster-decoder.h ├── mem-optimize-clg-lattice-faster-online-decoder.cc ├── mem-optimize-clg-lattice-faster-online-decoder.h ├── mem-optimize-hclg-lattice-faster-online-decoder.cc ├── mem-optimize-hclg-lattice-faster-online-decoder.h ├── online-clg-decoder-mempool-base.h ├── online-decoder-base-inl.h ├── online-decoder-base.h ├── online-decoder-mempool-base-biglm.h ├── online-decoder-mempool-base.h ├── optimize-clg-lattice-online-faster-decoder-kaldifeature-test.cc └── wordid-to-wordstr.h ├── newfst ├── Makefile.am ├── arc-iter.h ├── arc.h ├── compose-lat-inl.h ├── compose-lat.h ├── connect-fst-all.h ├── connect-fst.cc ├── connect-fst.h ├── const-fst-read.cc ├── const-fst.h ├── dfs-visit-fst.cc ├── dfs-visit-fst.h ├── fst-queue.h ├── invert.cc ├── invert.h ├── lattice-determinize-api.cc ├── lattice-determinize-api.h ├── lattice-determinize.h ├── lattice-fst.cc ├── lattice-fst.h ├── lattice-functions.cc ├── lattice-functions.h ├── lattice-test.cc ├── lattice-to-nbest.cc ├── lattice-to-nbest.h ├── optimize-fst.h ├── reverse.cc ├── reverse.h ├── rmepsilon.cc ├── rmepsilon.h ├── test-code.cc ├── test-connect-fst.cc ├── test-heap.cc ├── topsort.cc ├── topsort.h └── weigth.h ├── newlm ├── Makefile.am ├── README.txt ├── arpa2fsa-bin.cc ├── arpa2fsa.cc ├── arpa2fsa.h ├── compose-arpalm.cc ├── compose-arpalm.h ├── compose-diff-text.cc ├── compose-text.cc ├── diff-lm.h ├── lat-compose.cc ├── lmrescale.cc └── lmscore.cc ├── nnet ├── FeatureExtractor.h ├── Makefile.am ├── README.txt ├── cudnn-lstm-layer.cc ├── cudnn-lstm-layer.h ├── decodable-itf.h ├── fbanks.cfg ├── forward-test.cc ├── kaldi-format-pitch-forward-test.cc ├── kaldi-forward-test.cc ├── lstm-layer.cc ├── lstm-layer.h ├── lstm-projected-layer.cc ├── lstm-projected-layer.h ├── matrix.cc ├── matrix.h ├── nnet-component.cc ├── nnet-component.h ├── nnet-feature-api-test.cc ├── nnet-feature-api.cc ├── nnet-feature-api.h ├── nnet-feature-pitch-api.cc ├── nnet-forward.h ├── nnet-layer.cc ├── nnet-layer.h ├── nnet-nnet.cc ├── nnet-nnet.h ├── nnet-read-test.cc ├── nnet-simple-recurrent.cc ├── nnet-simple-recurrent.h ├── nnet-util.cc ├── nnet-util.h ├── nnetread.cc ├── online_pitch.conf ├── pitch-forward-test.cc ├── stream-kaldi-forward-test.cc ├── tf-lstm-layer.cc └── tf-lstm-layer.h ├── old-decoder ├── clg-lattice-faster-online-decoder.cpp ├── clg-lattice-faster-online-decoder.h ├── conf │ └── conf.txt ├── decoder-graph.h ├── faster-decoder.cc ├── faster-decoder.h ├── graph.h ├── log.h ├── makefile ├── optimize-clg-lattice-online-faster-decoder-kaldifeature-test.cpp ├── optimize-ctc-faster-decoder-kaldifeature-test.cpp ├── optimize-ctc-faster-decoder.cpp ├── optimize-ctc-faster-decoder.h ├── optimize-ctc-lattice-faster-decoder-kaldifeature-test.cpp ├── optimize-faster-decoder-kaldifeat-test.cpp ├── optimize-faster-decoder-kaldifeature-test.cpp ├── optimize-faster-decoder-test.cpp ├── optimize-faster-decoder.cc ├── optimize-faster-decoder.h ├── optimize-fst.cpp ├── optimize-fst.h ├── test-optimize-fst.cpp └── wordid-to-wordstr.h ├── online-vad ├── Makefile.am ├── new-online-energy-vad-stream-test.cc ├── online-energy-vad-stream-test.cc ├── online-nnet3-vad-stream-test.cc ├── online-nnet3-vad-test.cc ├── online-vad.h └── run.sh ├── pitch ├── Makefile.am ├── cblas-wrappers.h ├── compute-and-process-kaldi-pitch-feats.cc ├── hb-stream-compute-and-process-kaldi-pitch-feats.cc ├── kaldi-common.cc ├── kaldi-common.h ├── kaldi-matrix.cc ├── kaldi-matrix.h ├── kaldi-type.h ├── kaldi-vector.cc ├── kaldi-vector.h ├── online-feature-itf.h ├── online-feature.cc ├── online-feature.h ├── pitch-functions.cc ├── pitch-functions.h ├── resample.cc ├── resample.h └── stream-compute-and-process-kaldi-pitch-feats.cc ├── post-processing-service ├── Makefile.am ├── asr-post-process-service.cc ├── asr-post-process-task.h ├── asr-post-process-work-thread.h ├── client-asr-post-process-service.cc ├── client-asr-post-process-task.h ├── client-asr-post-process-work-thread.h ├── conf │ ├── post-config.txt │ └── post-socket.conf ├── const-lm-rescore.h ├── lattice-to-result.h ├── post-package-test.cc └── post-package.h ├── service ├── Makefile.am ├── client.cc ├── service.cc ├── task.h ├── thread-class.h ├── thread-pool.cc └── thread-pool.h ├── service2 ├── Makefile.am ├── net-data-package-test.cc ├── net-data-package.cc ├── net-data-package.h ├── pthread-util.cc ├── pthread-util.h ├── socket-class.h ├── test-client.cc ├── test-service.cc ├── test-task.h ├── test-work-thread.h ├── testv1-service.cc ├── thread-class.cc ├── thread-class.h ├── thread-info.h ├── thread-pool-inl.h ├── thread-pool-work-thread.h └── thread-pool.h ├── service2bin ├── Makefile.am ├── asr-client-v1.cc ├── asr-client-v2.cc ├── asr-client.cc └── run.sh ├── util ├── Makefile.am ├── config-parse-options.cc ├── config-parse-options.h ├── hash-key-test.cc ├── hash-key.h ├── hash-list-inl.h ├── hash-list-test.cc ├── hash-list.h ├── io-funcs.cc ├── io-funcs.h ├── log-message-test.cc ├── log-message.cc ├── log-message.h ├── mem-pool.h ├── namespace-end.h ├── namespace-start.h ├── read-vector.cc ├── read-vector.h ├── stl-util.h ├── test-config-parse-options.cc ├── test-text-util.cc ├── text-util.cc ├── text-util.h ├── util-common.h ├── util-time.h └── util-type.h ├── v1-asr ├── Makefile.am ├── README ├── asr-source.cc ├── asr-source.h ├── asr-task.h ├── asr-work-thread.cc ├── asr-work-thread.h ├── kaldi-v1-asr-online-test.cc ├── kaldi-v1-asr-online.h ├── online-nnet3-feature-pipeline-io.h ├── online2-tcp1.cc ├── v1-asr-task.h ├── v1-asr-work-thread.h └── v1-online-nnet3-decoding.h ├── v1-asrbin ├── Makefile.am ├── README ├── asr-service.cc ├── conf │ ├── conf.txt │ ├── decoder.conf │ ├── energy-vad.conf │ ├── fbank.80.conf │ ├── nnet3-vad.conf │ └── socket.conf ├── v1-asr-service.cc └── v1-asr-service.sh ├── v2-asr ├── Makefile.am ├── README ├── v2-asr-task.h ├── v2-asr-work-thread.cc └── v2-asr-work-thread.h ├── v2-asrbin ├── Makefile.am ├── README ├── conf │ ├── conf.txt │ ├── decoder.conf │ ├── fbank.80.conf │ ├── socket.conf │ └── v2-conf.txt └── v2-asr-service.cc ├── vad ├── Makefile.am ├── energy-vad.h ├── test-energy-vad.cc └── vad-test.cc └── wakeup ├── dtw.cc ├── dtw.h ├── wakeup-search.cc └── wakeup-search.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # make tmp file 35 | Makefile 36 | Makefile.in 37 | .libs 38 | .deps 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015 hubo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bashrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | export KALDI_ROOT=/home/hubo/git/github-online/github/kaldi/ 4 | # ffmpeg install dir 5 | export FFMPEG_ROOT=/ 6 | export ASR_PROJECT_INSTALL_ROOT=/home/hubo/git/github-online/git-private/ASR-decoder 7 | 8 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = fst_format_convert_tool \ 2 | util align \ 3 | newfst newlm \ 4 | pitch hmm service nnet \ 5 | my-decoder \ 6 | service2 \ 7 | service2bin \ 8 | kaldi-nnet3 kaldi-nnet3bin \ 9 | v1-asr v2-asr \ 10 | v1-asrbin v2-asrbin \ 11 | client clientbin \ 12 | vad online-vad \ 13 | biglm \ 14 | post-processing-service \ 15 | kaldi-bin \ 16 | gpu-asr 17 | 18 | lib : util align newfst newlm decoder service2 kaldi-nnet3 v1-asr v2-asr vad client \ 19 | fst_format_convert_tool pitch hmm service nnet 20 | 21 | kaldi-nnet3bin service2bin v1-asrbin : lib 22 | 23 | online-vad : util 24 | 25 | service2 : util 26 | 27 | post-processing-service : service2 28 | biglm : service2 29 | 30 | include_HEADERS = client/py-client/asr-client-api.h 31 | confdir=$(prefix)/conf 32 | dist_conf_DATA = v1-asrbin/conf/conf.txt \ 33 | v1-asrbin/conf/decoder.conf \ 34 | v1-asrbin/conf/fbank.80.conf \ 35 | v1-asrbin/conf/socket.conf \ 36 | v1-asrbin/conf/nnet3-vad.conf \ 37 | v1-asrbin/conf/energy-vad.conf 38 | 39 | #service2bin/conf/conf.txt \ 40 | # service2bin/conf/socket.conf \ 41 | # service2bin/conf/decoder.conf \ 42 | # service2bin/conf/v2-conf.txt \ 43 | # service2bin/conf/fbank.80.conf \ 44 | # 45 | 46 | installdir=$(prefix) 47 | dist_install_DATA= service2bin/run.sh \ 48 | v1-asrbin/v1-asr-service.sh v1-asrbin/README 49 | 50 | ACLOCAL_AMFLAGS = -I m4 51 | -------------------------------------------------------------------------------- /src/README: -------------------------------------------------------------------------------- 1 | 2 | # project depend kaldi, so you must be download kaldi 3 | # and install. You can reference build.sh. 4 | 5 | # if you need LibTorch , 6 | # you can download in https://download.pytorch.org/libtorch/cu111/libtorch-cxx11-abi-shared-with-deps-1.8.1%2Bcu111.zip 7 | # gcc version > 5 8 | 9 | # you must be set bashrc envionment variables and then 10 | source ../bashrc 11 | # Install 12 | # need you install automake 13 | autoreconf --install 14 | 15 | mkdir build 16 | cd build 17 | mkdir -p $PWD/install 18 | 19 | # if you kaldi use cuda and mkl and ues torch nnet forward, you must be 20 | # KALDI_ROOT is kaldi/src dir 21 | ../configure --prefix=$PWD/install --enable-usecuda=yes --with-cuda=/usr/local/cuda/ --with-mkl=/opt/intel/mkl --with-kaldi=$(KALDI_ROOT) 22 | 23 | # if you don't use cuda 24 | ../configure --prefix=$PWD/install --enable-usecuda=no --with-mkl=/opt/intel/mkl --with-kaldi=$(KALDI_ROOT) 25 | 26 | # if you want use atlas 27 | ../configure --prefix=$PWD/install --with-atlas=$(ATLAS_ROOT) --with-kaldi=$(KALDI_ROOT) 28 | 29 | make -j 10 30 | make check -j 10 31 | make install 32 | 33 | # if you want get tar 34 | # make dist 35 | 36 | # you can run build.sh 37 | 38 | # tag 39 | # git tag -a vxxx -m "version vxxx" commit_id 40 | # git push origin vxxx | git push origin --tags 41 | # if you want delete service tag you can run 42 | # git push origin --delete 43 | # if you want checkout tag, you can 44 | # git checkout -b 45 | -------------------------------------------------------------------------------- /src/align/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | #lib_LTLIBRARIES = libalign.la 3 | noinst_LTLIBRARIES = libalign.la 4 | 5 | libalign_la_SOURCES = phone-to-word.cc 6 | 7 | check_PROGRAMS = test-phone-to-word test-align-info 8 | 9 | test_phone_to_word_LDADD = ../align/libalign.la ../util/libutil.la 10 | test_phone_to_word_SOURCES = test-phone-to-word.cc 11 | 12 | test_align_info_LDADD = ../util/libutil.la 13 | test_align_info_SOURCES = test-align-info.cc 14 | -------------------------------------------------------------------------------- /src/align/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash +x 2 | 3 | 4 | ./test-phone-to-word data/words.txt data/tokens.txt data/lexicon.txt.20150120.format aaa phone.test.list 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/align/test-phone-to-word.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "src/align/phone-to-word.h" 8 | #include "src/util/text-util.h" 9 | 10 | #ifdef NAMESPACE 11 | using namespace datemoon ; 12 | #endif 13 | 14 | int main(int argc,char *argv[]) 15 | { 16 | if(argc != 6) 17 | { 18 | std::cerr << argv[0] 19 | << " worddict phonedict prondict output test.list" << std::endl; 20 | exit(-1); 21 | } 22 | PhoneToWord pronounce; 23 | if( 0 != pronounce.ReadText(argv[1],argv[2])) 24 | { 25 | std::cerr << "ReadText" << argv[1] << argv[2] << 26 | "failed." << std::endl; 27 | return -1; 28 | } 29 | if(0 != pronounce.ProcessPronDict(argv[3])) 30 | { 31 | std::cerr << "ProcessPronDict" << argv[3] << "failed." << std::endl; 32 | return -1; 33 | } 34 | std::ifstream fin(argv[5]); 35 | std::string line; 36 | std::vector str; 37 | int wordid = 1; 38 | int pinyins[30] ; 39 | int pinyin_num = 0; 40 | while(getline(fin, line)) 41 | { 42 | pinyin_num = 0; 43 | CutLineToStr(line,str); 44 | wordid = atoi(str[0].c_str()); 45 | for(size_t k = 1; k < str.size();++k) 46 | { 47 | pinyins[k-1] = atoi(str[k].c_str()); 48 | pinyin_num++; 49 | } 50 | assert((size_t)pinyin_num == str.size()-1); 51 | int ret = pronounce.SearchWord(pinyins,pinyin_num,wordid); 52 | 53 | if(ret == -1) 54 | { 55 | std::cerr << line << "not find this word" << std::endl; 56 | } 57 | else 58 | { 59 | std::cerr << line << " find this word use " << ret << " phone." << std::endl; 60 | } 61 | } 62 | pronounce.PrintStruct(); 63 | /* 64 | wordid = 7; 65 | int pinyinseq[10] = {12, 12, 169,48,51,35}; 66 | int ret = pronounce.SearchWord(pinyinseq,6,wordid); 67 | if(ret == 0) 68 | { 69 | std::cerr << "not find this word" << std::endl; 70 | return -1; 71 | } 72 | std::cerr << "find this word use " << ret << " phone." << std::endl; 73 | */ 74 | return 0; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/arm.mk: -------------------------------------------------------------------------------- 1 | 2 | WORK_DIR=`pwd` 3 | #KALDI_DIR_NAME=kaldi-arm 4 | #ATLAS_PATH=$WORK_DIR/$KALDI_DIR_NAME/tools/ATLAS/build_pic_fix2/lib 5 | 6 | PATH=/opt/android-toolchain-5-arm/bin:$PATH 7 | 8 | CROSS=arm-linux-androideabi 9 | CC_=$(CROSS)-gcc 10 | CXX_=$(CROSS)-g++ 11 | AR_=$(CROSS)-ar 12 | RANLIB_=$(CROSS)-ranlib 13 | STRIP_=$(CROSS)-strip 14 | LD_=$(CROSS)-ld 15 | 16 | CC=$(CROSS)-g++ 17 | CXX=$(CROSS)-g++ 18 | AR=$(CROSS)-ar 19 | RANLIB=$(CROSS)-ranlib 20 | STRIP=$(CROSS)-strip 21 | LD=$(CROSS)-ld 22 | GCC=$(CROSS)-gcc 23 | GXX=$(CROSS)-g++ 24 | 25 | CBLASLIB=../lib/libcblas_arm.a \ 26 | ../lib/libatlas_arm.a 27 | 28 | FEATLIB=../lib/libfrontend_arm.a 29 | 30 | CXXFLAGS=-I.. -fpermissive -Wall -DKALDI_DOUBLEPRECISION=0 -DHAVE_MEMALIGN -Wno-sign-compare -Winit-self -rdynamic -DHAVE_CXXABI_H -DHAVE_ATLAS -O3 -mcpu=cortex-a8 -mtune=cortex-a8 -mfpu=neon -mfloat-abi=softfp -fno-expensive-optimizations -std=c++11 -fpermissive 31 | 32 | -------------------------------------------------------------------------------- /src/audio-convert/makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FFMPEG_INC=/usr/local/include/ 6 | FFMPEG_LIBS=/usr/local/lib/libavformat.a \ 7 | /usr/local/lib/libswresample.a \ 8 | /usr/local/lib/libavcodec.a \ 9 | /usr/local/lib/libavutil.a 10 | 11 | LD_FLAGS=-pthread -liconv -lm -llzma -lz -lva -lbz2 -lva-drm -lva -lva-x11 -lva -lm -lva -lXv -lX11 -lXext 12 | CXXFLAGS=-std=c++11 13 | CXX=g++ -g 14 | all:stream2pcm 15 | 16 | stream2pcm:stream2pcm.o 17 | $(CXX) $< $(FFMPEG_LIBS) -o $@ $(LD_FLAGS) 18 | 19 | %.o:%.cc 20 | $(CXX) $(CXXFLAGS) -I$(FFMPEG_INC) -c -o $@ $< 21 | 22 | 23 | .PHONY: 24 | 25 | clean: 26 | rm -f *.o stream2pcm 27 | 28 | -------------------------------------------------------------------------------- /src/audio-convert/stream2pcm.cc: -------------------------------------------------------------------------------- 1 | #include "stream2pcm.h" 2 | 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | struct AudioConvertOpt opt; 7 | char *filename = argv[1]; 8 | Stream2Pcm stream2pcm(opt, 1024*4, filename); 9 | 10 | stream2pcm.ConvertData(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /src/biglm/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 8 | $(KALDI_DEPLDFLAGS) \ 9 | $(MATRIX_DEPLDFLAGS) 10 | 11 | LDADD = 12 | 13 | LDADD += $(KALDI_LIBS) \ 14 | $(MATRIX_LIBS) \ 15 | $(CUDA_LIBS) \ 16 | -lm -lpthread $(DL_LIBS) 17 | 18 | bin_PROGRAMS = 19 | 20 | noinst_PROGRAMS = lattice-read-and-write 21 | 22 | lattice_read_and_write_SOURCES = lattice-read-and-write.cc 23 | 24 | -------------------------------------------------------------------------------- /src/biglm/lattice-read-and-write.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "lat/kaldi-lattice.h" 7 | 8 | 9 | using namespace kaldi; 10 | using namespace fst; 11 | int main(int argc, char *argv[]) 12 | { 13 | bool binary=false; 14 | std::string infile(argv[1]); 15 | 16 | std::ifstream ifs; 17 | 18 | std::ostringstream oss; 19 | std::ostream os(oss.rdbuf()); 20 | 21 | ifs.open(infile.c_str(), 22 | binary ? std::ios_base::in | std::ios_base::binary 23 | : std::ios_base::in); 24 | if(!ifs.is_open()) 25 | { 26 | std::cerr << "Open " << infile << " failed." << std::endl; 27 | return -1; 28 | } 29 | CompactLattice *clat=NULL; 30 | bool read_ret = ReadCompactLattice(ifs, binary, &clat); 31 | if(read_ret != true) 32 | { 33 | std::cerr << "ReadCompactLattice failed." << std::endl; 34 | return -1; 35 | } 36 | 37 | bool w_binary=true; 38 | bool write_ret = WriteCompactLattice(os, w_binary, *clat); 39 | if(write_ret != true) 40 | { 41 | std::cerr << "WriteCompactLattice failed." << std::endl; 42 | return -1; 43 | } 44 | delete clat; 45 | if(w_binary != true) 46 | { 47 | std::cout << oss.str() < 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "src/client/asr-client-thread.h" 15 | #include "src/util/log-message.h" 16 | 17 | #include "src/util/namespace-start.h" 18 | 19 | bool CreateResultHandle(std::string result_file = "result.txt", 20 | std::string mode = "a", std::string align_file = "align.txt"); 21 | 22 | void CloseResultHandle(); 23 | 24 | struct ASRClientOpts 25 | { 26 | public: 27 | ASRClientOpts(bool realtime_res=false, 28 | int32 nbest=0, 29 | bool do_align=false, 30 | bool do_lattice=false): 31 | _realtime_res(realtime_res), _nbest(nbest), 32 | _do_align(do_align), _do_lattice(do_lattice) { } 33 | 34 | void Check() 35 | { 36 | if(_realtime_res == true) 37 | if (_nbest <= 0) 38 | _nbest = 1; 39 | } 40 | 41 | bool _realtime_res; 42 | int32 _nbest; 43 | bool _do_align; 44 | bool _do_lattice; 45 | 46 | }; 47 | 48 | class ASRClientTask:public TaskBase 49 | { 50 | public: 51 | typedef TaskBase::int32 int32; 52 | public: 53 | ASRClientTask(const ASRClientOpts &opts, 54 | std::string wav_file, 55 | int32 wavhead_len=0, 56 | int32 send_len = 16000*2, 57 | std::string task_name=""): 58 | TaskBase(task_name), _wav_file(wav_file), 59 | _wavhead_len(wavhead_len), _send_len(send_len), 60 | _sockfd(-1), _cli_opts(opts) {} 61 | 62 | ~ASRClientTask() 63 | { 64 | Stop(); 65 | } 66 | int32 Run(void *data); 67 | 68 | int32 Stop() 69 | { 70 | if(_sockfd != -1) 71 | { 72 | close(_sockfd); 73 | _sockfd = -1; 74 | } 75 | return 0; 76 | } 77 | void GetInfo() 78 | { 79 | LOG_COM << "Task name is : " << _task_name ; 80 | LOG_COM << "wav file is : " << _wav_file; 81 | } 82 | 83 | private: 84 | static pthread_mutex_t _outfile_mutex; 85 | static void *RecvThreadFunc(void *data); 86 | 87 | std::string _wav_file; 88 | int32 _wavhead_len; 89 | int32 _send_len; 90 | int _sockfd; 91 | const ASRClientOpts &_cli_opts; 92 | }; 93 | #include "src/util/namespace-end.h" 94 | #endif 95 | -------------------------------------------------------------------------------- /src/client/asr-client-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASR_CLIENT_THREAD_H__ 2 | #define __ASR_CLIENT_THREAD_H__ 3 | #include 4 | #include 5 | #include 6 | #include "src/service2/thread-pool.h" 7 | #include "src/service2/net-data-package.h" 8 | #include "src/service2/thread-pool-work-thread.h" 9 | 10 | #include "src/util/namespace-start.h" 11 | 12 | /// This is class use kaldi feature , nnet and mydecoder 13 | /// package asr service class work thread. 14 | class ASRClinetThread:public ThreadPoolWorkThread 15 | { 16 | public: 17 | friend class ASRClientTask; 18 | typedef ThreadBase::int32 int32; 19 | public: 20 | // create connect. 21 | ASRClinetThread(ThreadPoolBase *thread_pool, 22 | int32 port=8000, 23 | std::string ip="127.0.0.1") 24 | :ThreadPoolWorkThread(thread_pool) 25 | { 26 | //struct sockaddr_in ser; 27 | memset(&_ser, 0x00, sizeof(_ser)); 28 | _ser.sin_family = AF_INET; 29 | inet_aton(ip.c_str(), &_ser.sin_addr); 30 | _ser.sin_port = htons(port); 31 | //int res = connect(_sockfd, (struct sockaddr *) &ser, sizeof(ser)); 32 | //assert(res >=0); 33 | } 34 | 35 | ~ASRClinetThread() { } 36 | private: 37 | struct sockaddr_in _ser; 38 | // recv data from service 39 | C2SPackageAnalysis _client_c2s_package_analysys; 40 | // send data to service 41 | S2CPackageAnalysis _client_s2c_package_analysys; 42 | 43 | }; 44 | 45 | #include "src/util/namespace-end.h" 46 | #endif 47 | -------------------------------------------------------------------------------- /src/client/py-client/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = 2 | 3 | lib_LTLIBRARIES = libclient.la 4 | 5 | libclient_la_SOURCES = asr-client-api.cc 6 | 7 | libclient_la_LIBADD = ../../service2/libservice2.la \ 8 | ../../util/libutil.la 9 | libclient_la_LDFLAGS = -rpath '$(libdir)' 10 | 11 | 12 | check_PROGRAMS = asr-client-api-test 13 | 14 | #AM_DEFAULT_SOURCE_EXT = .cc 15 | asr_client_api_test_SOURCES = asr-client-api-test.cc 16 | 17 | asr_client_api_test_LDADD = libclient.la \ 18 | ../../service2/libservice2.la \ 19 | ../../util/libutil.la 20 | 21 | -------------------------------------------------------------------------------- /src/client/py-client/asr-client-api-test.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/client/py-client/asr-client-api.h" 9 | 10 | #define LEN 4096 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | if(argc != 3) 15 | { 16 | std::cout << argv[0] << " port wavfile" << std::endl; 17 | return -1; 18 | } 19 | int port = atoi(argv[1]); 20 | int ret = TcpConnect("127.0.0.1", port); 21 | SetAlign(); 22 | if(ret < 0) 23 | { 24 | std::cout << "TcpConnect failed!!!" << std::endl; 25 | return -1; 26 | } 27 | char *wav_file = argv[2]; 28 | FILE *fp = fopen(wav_file, "r"); 29 | while(true) 30 | { 31 | char sentbuf[LEN]; 32 | char recvbuf[LEN]; 33 | memset(sentbuf,0x00,sizeof(sentbuf)); 34 | memset(recvbuf,0x00,sizeof(recvbuf)); 35 | size_t sent_len = fread((void*)sentbuf, 1, LEN, fp); 36 | if(sent_len > 0) 37 | { 38 | SendPack((short*)sentbuf, sent_len/2, 0); 39 | } 40 | else 41 | { 42 | SendLastPack(); 43 | int len = 0; 44 | //unsigned char* result = GetResult(&len); 45 | unsigned char* result = RecvResult(&len); 46 | std::cout << wav_file << " " << result << std::endl; 47 | break; 48 | } 49 | 50 | } 51 | TcpClose(); 52 | fclose(fp); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /src/client/py-client/asr-client-api.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASR_CLIENT_API_H__ 2 | #define __ASR_CLIENT_API_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" 6 | { 7 | #endif 8 | 9 | // 需要依赖 libclient.so libservice2.so libutil.so 10 | int TcpConnect(const char* ip_addr, int port); 11 | 12 | void SetAlign(); 13 | 14 | void TcpClose(); 15 | 16 | void SendPack(short* packet_data, int sample_num, int flag = 0); 17 | 18 | void SendLastPack(); 19 | 20 | unsigned char* GetResult(int *len); 21 | 22 | void* PthreadRecv(void *socket_id); 23 | 24 | unsigned char* RecvResult(int *len); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/clientbin/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | 3 | LDADD = ../client/libclientv1.la \ 4 | ../service2/libservice2.la \ 5 | ../util/libutil.la 6 | 7 | LDADD += -lm -lpthread $(DL_LIBS) 8 | 9 | bin_PROGRAMS = 10 | noinst_PROGRAMS = thread-client 11 | #thread_client_LDADD = ../service2/libservice2.la ../v2-asr/libv2asr.la ../util/libutil.la 12 | 13 | #thread_client_PROGRAMS = thread-client 14 | 15 | thread_client_SOURCES = thread-client.cc 16 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | bin_PROGRAMS = convert_fst \ 3 | convert_hmm 4 | 5 | convert_fst_SOURCES = read_fst.c convert_fst.c write_fst.c 6 | 7 | convert_hmm_SOURCES = convert_hmm.c read_fst.c write_fst.c 8 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/README.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | make 4 | 5 | # convert clg.fst to the used format. 6 | ./convert_fst det_min_clg.fst use_clg.fst 7 | 8 | # hmm generate the format used. 9 | # hmm.list has to be sorted. 10 | ./convert_hmm hmm.list hmm.fst 11 | 12 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/convert_fst.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "fst_io.h" 4 | 5 | int main(int argc,char *argv[]) 6 | { 7 | if(argc != 3) 8 | { 9 | fprintf(stderr,"input error!\nplease input %s fst out\n",argv[0]); 10 | return -1; 11 | } 12 | struct FstHead fsthead; 13 | memset(&fsthead,0x00,sizeof(fsthead)); 14 | struct Fst fst; 15 | memset(&fst,0x00,sizeof(struct Fst)); 16 | if(0 != read_fst(argv[1],&fsthead,&fst)) 17 | { 18 | fprintf(stderr,"read_fst error!\n"); 19 | return -1; 20 | } 21 | if(0 != write_fst(argv[2],&fst)) 22 | { 23 | fprintf(stderr,"write_fst error!\n"); 24 | return -1; 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/convert_hmm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "fst_io.h" 4 | 5 | int main(int argc,char *argv[]) 6 | { 7 | if(argc != 3) 8 | { 9 | fprintf(stderr,"input error!\nplease input %s fstlist out\n",argv[0]); 10 | return -1; 11 | } 12 | struct FstHead fsthead; 13 | memset(&fsthead,0x00,sizeof(fsthead)); 14 | struct Fst fst; 15 | char line[128]; 16 | FILE *fp=fopen(argv[1],"r"); 17 | if( fp == NULL) 18 | { 19 | fprintf(stderr,"open %s failed!\n",argv[1]); 20 | return -1; 21 | } 22 | FILE *fp_out=fopen(argv[2],"w"); 23 | if(fp_out == NULL) 24 | { 25 | fprintf(stderr,"open %s failed!\n",argv[1]); 26 | return -1; 27 | } 28 | int numhmm=0; 29 | fwrite(&numhmm,sizeof(int),1,fp_out); 30 | fclose(fp_out); 31 | 32 | while(fgets(line,sizeof(line),fp)!= NULL) 33 | { 34 | line[strlen(line)-1] = '\0'; 35 | memset(&fst,0x00,sizeof(struct Fst)); 36 | if(0 != read_fst(line,&fsthead,&fst)) 37 | { 38 | fprintf(stderr,"read_fst error!\n"); 39 | return -1; 40 | } 41 | if(0 != write_fst(argv[2],&fst)) 42 | { 43 | fprintf(stderr,"write_fst error!\n"); 44 | return -1; 45 | } 46 | numhmm++; 47 | } 48 | fp_out=fopen(argv[2],"r+"); 49 | fseek(fp_out,0,SEEK_SET); 50 | printf("%ld\n",ftell(fp_out)); 51 | fwrite(&numhmm,sizeof(int),1,fp_out); 52 | printf("%ld\n",ftell(fp_out)); 53 | fclose(fp_out); 54 | fclose(fp); 55 | printf("load %d hmm\n",numhmm); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/fst.h: -------------------------------------------------------------------------------- 1 | #ifndef __FST__ 2 | #define __FST__ 3 | typedef int int32; 4 | typedef long long int64; 5 | typedef unsigned long long uint64; 6 | 7 | struct Arc 8 | { 9 | int ilabel; 10 | int olabel; 11 | float weight; 12 | int nextstate; 13 | }; 14 | 15 | struct State 16 | { 17 | float final; 18 | struct Arc *arc_arr; 19 | int num_arc; 20 | int niepsilons; 21 | int noepsilons; 22 | }; 23 | 24 | struct Fst 25 | { 26 | struct State *state_arr; 27 | int start_state; 28 | int final_state; 29 | int total_state; 30 | int total_arc; 31 | int total_niepsilons; 32 | int total_noepsilons; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/fst_io.h: -------------------------------------------------------------------------------- 1 | #ifndef __FST_IO__ 2 | #define __FST_IO__ 3 | #include "fst.h" 4 | 5 | struct FstHead 6 | { 7 | char fsttype_[64]; // E.g. "vector" 8 | char arctype_[64]; // E.g. "standard" 9 | int32 version_; // Type version # 10 | int32 flags_; // File format bits 11 | uint64 properties_; // FST property bits 12 | int64 start_; // Start state 13 | int64 numstates_; // # of states 14 | int64 numarcs_; // # of arcs 15 | }; 16 | 17 | int read_fst(const char *filename , struct FstHead *fsthead , struct Fst *fst); 18 | 19 | int write_fst(const char *filename,struct Fst *fst); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /src/fst_format_convert_tool/write_fst.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "fst_io.h" 5 | 6 | int write_fst(const char *filename,struct Fst *fst) 7 | { 8 | FILE *fp = fopen(filename,"a"); 9 | if(1 != fwrite(&(fst->start_state),sizeof(int),1,fp)) 10 | { 11 | return -1; 12 | } 13 | if(1 != fwrite(&(fst->final_state),sizeof(int),1,fp)); 14 | if(1 != fwrite(&(fst->total_state),sizeof(int),1,fp)); 15 | if(1 != fwrite(&(fst->total_arc),sizeof(int),1,fp)); 16 | if(1 != fwrite(&(fst->total_niepsilons),sizeof(int),1,fp)); 17 | if(1 != fwrite(&(fst->total_noepsilons),sizeof(int),1,fp)); 18 | 19 | int i=0; 20 | int arc_offset = 0; 21 | // frist write fst state 22 | for(i = 0 ; i < fst->total_state ; ++i) 23 | { 24 | struct State *state = &fst->state_arr[i]; 25 | if(1 != fwrite(&state->num_arc,sizeof(int),1,fp)) 26 | { 27 | return -1; 28 | } 29 | if(1 != fwrite(&state->niepsilons, sizeof(int), 1, fp)) 30 | { 31 | return -1; 32 | } 33 | if(1 != fwrite(&state->noepsilons, sizeof(int), 1, fp)) 34 | { 35 | return -1; 36 | } 37 | arc_offset += state->num_arc; 38 | } 39 | // write fst arc 40 | for(i = 0 ; i < fst->total_state ; ++i) 41 | { 42 | struct State *state = &fst->state_arr[i]; 43 | if(state->num_arc > 0) 44 | { 45 | if(state->num_arc !=fwrite(state->arc_arr,sizeof(struct Arc),state->num_arc,fp)) 46 | { 47 | return -1; 48 | } 49 | #ifdef DEBUG 50 | int n_arc= 0; 51 | for(n_arc = 0;n_arc < state->num_arc;++n_arc) 52 | { 53 | fprintf(stdout,"%d %d %d %d %f\n",i,state->arc_arr[n_arc].nextstate,state->arc_arr[n_arc].ilabel,state->arc_arr[n_arc].olabel,state->arc_arr[n_arc].weight); 54 | } 55 | #endif 56 | } 57 | 58 | } 59 | assert(arc_offset == fst->total_arc); 60 | fclose(fp); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /src/gpu-asr/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | if USECUDA 8 | AM_CPPFLAGS += -DHAVE_CUDA 9 | endif 10 | 11 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 12 | $(CUDA_DEPLDFLAGSCONST) \ 13 | $(KALDI_DEPLDFLAGS) \ 14 | $(MATRIX_DEPLDFLAGS) 15 | 16 | LDADD = ../service2/libservice2.la \ 17 | ../util/libutil.la 18 | 19 | # 添加cudadecoder相关库 20 | LDADD += $(KALDI_ROOT)/cudadecoder/kaldi-cudadecoder.a \ 21 | $(KALDI_ROOT)/cudafeat/kaldi-cudafeat.a 22 | 23 | LDADD += $(KALDI_LIBS) \ 24 | $(MATRIX_LIBS) \ 25 | $(CUDA_LIBS) \ 26 | $(CUDA_LIBSCONST) \ 27 | -lm -lpthread $(DL_LIBS) 28 | if USECUDA 29 | if USA_CUDA_DECODER 30 | bin_PROGRAMS = v1-gpu-asr-service 31 | else 32 | noinst_PROGRAMS = v1-gpu-asr-service 33 | endif 34 | endif 35 | 36 | v1_gpu_asr_service_SOURCES = v1-gpu-asr-service.cc 37 | 38 | -------------------------------------------------------------------------------- /src/gpu-asr/README: -------------------------------------------------------------------------------- 1 | 2 | 3 | # 这是一个gpudecoder的代码目录,可以提供gpu版本的语音识别服务, 4 | # 其中nnet前向会拼batch使用gpu解码,使用kaldi-nnet3, 5 | # 由于kaldi-nnet3限制,所以每次前向计算会有部分重复性计算, 6 | # decoder部分使用kaldi的cudadecoder,现阶段已经调试好,添加能量vad 7 | # 配置文件见conf目录 8 | # 运行详见run.sh脚本,如修改配置,可详见命令行help 9 | 10 | to do: 11 | 1. 在工程化方向nnet前向拼batch部分有待进一步优化和支持更广范围的nnet模型 12 | 2. 前向,特征,解码线程池部分代码的同步还有问题,有待进一步解决,对同一个 13 | corr_id的使用结束外部无法知道,所以尽量不用同一个corr_id在服务中,但这 14 | 几乎不可避免,这块有待进一步解决. 15 | 针对corr_id的冲突问题,当使用vad切音频产生多次corr_id时,不能由主线程分配 16 | 所以不能保证corr_id不会有冲突,引入GPU_CORRID_OFFSET宏,保证corr_id的增长 17 | 间隔是GPU_CORRID_OFFSET(默认100),如果vad切割获得非静音少于100段,就不会有 18 | corr_id冲突,但依然没有从本质上解决这个问题,有待修改注册函数。 19 | -------------------------------------------------------------------------------- /src/gpu-asr/conf/config.txt: -------------------------------------------------------------------------------- 1 | 2 | # vad config 3 | --use-energy-vad=true 4 | --use-realtime-vad=true 5 | --print-vad-ali-info=false 6 | 7 | # endpoint 8 | # sil 1 9 | --endpoint.silence-phones=1 10 | #--endpoint.rule1.max-relative-cost=inf 11 | --endpoint.rule1.min-trailing-silence=5 12 | --endpoint.rule1.min-utterance-length=0 13 | --endpoint.rule1.must-contain-nonsilence=false 14 | 15 | 16 | #--verbose=3 17 | --print-thread-pool-info=true 18 | --gpu-feature-extract=true 19 | 20 | --rec-use-final=false 21 | --beam=13.0 22 | --lattice-beam=7.0 23 | --max-active=10000 24 | --acoustic-scale=1.0 25 | --max-batch-size=300 26 | --num-channels=900 27 | --num-parallel-streaming-channels=900 28 | --frame-subsampling-factor=3 29 | --frames-per-chunk=150 30 | 31 | --nthread=800 32 | --config-socket=conf/socket.conf 33 | --feature-type=fbank 34 | --fbank-config=conf/fbank.80.conf 35 | 36 | #--word-symbol-table=tdnnf_fbank80_train_5kh_aishell_add_10epoch/HCLG-1e-9-modefy-lex-v7-cvte-manual-v2/words.txt 37 | 38 | -------------------------------------------------------------------------------- /src/gpu-asr/conf/fbank.80.conf: -------------------------------------------------------------------------------- 1 | # No non-default options for now. 2 | #--sample-frequency=8000 3 | --num-mel-bins=80 4 | #--dither=0 5 | -------------------------------------------------------------------------------- /src/gpu-asr/conf/socket.conf: -------------------------------------------------------------------------------- 1 | 2 | #--ip= 3 | --port=8100 4 | --rec-timeout=5000000 5 | --n-listen=15 6 | --keepalive=100 7 | --keeptime=5 8 | --keepinterval=100 9 | --keepcount=3 10 | -------------------------------------------------------------------------------- /src/gpu-asr/gpu-service-run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | 5 | if [ $# != 1 ] 6 | then 7 | echo $0 modeldir 8 | exit 1; 9 | fi 10 | #am=tdnnf_fbank80_train_5kh_aishell_add_10epoch/am/final.mdl 11 | #hclg=tdnnf_fbank80_train_5kh_aishell_add_10epoch/HCLG-1e-9-modefy-lex-v7-cvte-manual-v2/HCLG.fst 12 | dir=$1 13 | am=$dir/final.mdl 14 | hclg=$dir/HCLG.fst 15 | words=$dir/words.txt 16 | export LD_LIBRARY_PATH=$PWD/lib 17 | export CUDA_DEVICE_ORDER=PCI_BUS_ID 18 | CUDA_VISIBLE_DEVICES="0" ./bin/v1-gpu-asr-service --config=conf/config.txt --word-symbol-table=$words \ 19 | $am $hclg 20 | -------------------------------------------------------------------------------- /src/gpu-asr/v1-gpu-asr-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __V1_GPU_ASR_WORK_THREAD_H__ 2 | #define __V1_GPU_ASR_WORK_THREAD_H__ 3 | 4 | #include "src/service2/net-data-package.h" 5 | #include "src/service2/thread-pool-work-thread.h" 6 | //#include "src/gpu-asr/v1-gpu-kaldi-worker-pool.h" 7 | 8 | #include "src/gpu-asr/gpu-worker-pool-itf.h" 9 | 10 | #include "src/online-vad/online-vad.h" 11 | 12 | #include "src/util/namespace-start.h" 13 | 14 | struct V1GpuASRWorkThreadOpt 15 | { 16 | bool _use_energy_vad; 17 | kaldi::VadProcessOpts _vad_process_opts; 18 | 19 | 20 | V1GpuASRWorkThreadOpt(std::string vad_judge_prefix= "energy-vad-judge", 21 | std::string energy_vad_prefix = "energy-vad"): 22 | _use_energy_vad(true), 23 | _vad_process_opts(vad_judge_prefix, energy_vad_prefix) { } 24 | 25 | void Register(kaldi::OptionsItf *opts) 26 | { 27 | _vad_process_opts.Register(opts); 28 | opts->Register("use-energy-vad", &_use_energy_vad, 29 | "Use energy vad or not."); 30 | } 31 | }; 32 | 33 | class V1GpuASRWorkThread:public ThreadPoolWorkThread 34 | { 35 | public: 36 | friend class V1GpuASRServiceTask; 37 | friend class V1GpuASRServiceTask1; 38 | typedef ThreadBase::int32 int32; 39 | 40 | V1GpuASRWorkThread(ThreadPoolBase *tp, 41 | kaldi::cuda_decoder::BatchThreadCudaPipelineItf &cuda_pipeline_warp, 42 | const V1GpuASRWorkThreadOpt &v1_gpu_asr_work_thread_opts): 43 | ThreadPoolWorkThread(tp), _cuda_pipeline_warp(cuda_pipeline_warp), 44 | _v1_gpu_asr_work_thread_opts(v1_gpu_asr_work_thread_opts), 45 | _energy_vad(_v1_gpu_asr_work_thread_opts._vad_process_opts){ } 46 | 47 | ~V1GpuASRWorkThread() { } 48 | private: 49 | kaldi::cuda_decoder::BatchThreadCudaPipelineItf &_cuda_pipeline_warp; 50 | // recv data from client 51 | C2SPackageAnalysis _ser_c2s_package_analysys; 52 | // send data to client 53 | S2CPackageAnalysis _ser_s2c_package_analysys; 54 | 55 | const V1GpuASRWorkThreadOpt &_v1_gpu_asr_work_thread_opts; 56 | kaldi::EnergyVadProcess _energy_vad; 57 | }; 58 | 59 | #include "src/util/namespace-end.h" 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /src/hmm/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | 3 | lib_LTLIBRARIES = libhmm.la 4 | 5 | libhmm_la_SOURCES = hmm-topology.cc \ 6 | transition-model.cc 7 | 8 | LDADD = libhmm.la \ 9 | ../util/libutil.la 10 | 11 | check_PROGRAMS = test-hmm \ 12 | test-transition-model 13 | 14 | test_hmm_SOURCES = test-hmm.cc 15 | 16 | test_transition_model_SOURCES = test-transition-model.cc 17 | 18 | -------------------------------------------------------------------------------- /src/hmm/hmm-topology.h: -------------------------------------------------------------------------------- 1 | /* 2 | * author:hubo 3 | * data: 2016-10-12 4 | * 5 | * */ 6 | #ifndef __HMM_TOPOLOGY_H__ 7 | #define __HMM_TOPOLOGY_H__ 8 | 9 | #include 10 | #include 11 | 12 | #include "src/util/namespace-start.h" 13 | 14 | class HmmTopology 15 | { 16 | public: 17 | struct HmmState 18 | { 19 | int _pdf_class; 20 | std::vector > _transitions; 21 | 22 | explicit HmmState(int p): _pdf_class(p) { } 23 | HmmState(): _pdf_class(-1) { } 24 | 25 | bool operator == (const HmmState &other) const 26 | { 27 | return (_pdf_class == other._pdf_class && _transitions == other._transitions); 28 | } 29 | }; 30 | 31 | typedef std::vector TopologyEntry; 32 | 33 | void Read(std::istream &is, bool binary); 34 | 35 | void Write(std::ostream &os, bool binary) const; 36 | 37 | // Returns the topology entry (i.e. vector of HmmState) for this phone; 38 | // will throw exception if phone not covered by the topology. 39 | const TopologyEntry &TopologyForPhone(int phone) const; 40 | private: 41 | std::vector _phones; // list of all phones we have topology for. Sorted, uniq. no epsilon (zero) phone. 42 | std::vector _phone2idx; // map from phones to indexes into the entries vector (or -1 for not present). 43 | std::vector _entries; 44 | }; 45 | 46 | #include "src/util/namespace-end.h" 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/hmm/test-hmm.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "src/hmm/hmm-topology.h" 5 | #ifdef NAMESPACE 6 | using namespace datemoon ; 7 | #endif 8 | 9 | 10 | int main(int argc,char *argv[]) 11 | { 12 | std::ifstream ifs; 13 | bool binary = false; 14 | ifs.open(argv[1], 15 | binary ? std::ios_base::in | std::ios_base::binary 16 | : std::ios_base::in); 17 | 18 | std::string str; 19 | ifs >> str; 20 | HmmTopology hmm; 21 | 22 | if(ifs.is_open()) 23 | { 24 | hmm.Read(ifs, binary); 25 | } 26 | else 27 | { 28 | std::cerr << "hmm file open fail." << std::endl; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/hmm/test-transition-model.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "src/hmm/transition-model.h" 5 | #ifdef NAMESPACE 6 | using namespace datemoon ; 7 | #endif 8 | 9 | 10 | int main(int argc,char *argv[]) 11 | { 12 | std::ifstream ifs; 13 | std::ofstream ofs; 14 | bool binary = false; 15 | ifs.open(argv[1], 16 | binary ? std::ios_base::in | std::ios_base::binary 17 | : std::ios_base::in); 18 | 19 | ofs.open(argv[2], 20 | binary ? std::ios_base::out | std::ios_base::binary 21 | : std::ios_base::out); 22 | 23 | TransitionModel trans_model; 24 | 25 | if(ifs.is_open()) 26 | { 27 | trans_model.Read(ifs, binary); 28 | } 29 | else 30 | { 31 | std::cerr << "trainsmodel file open fail. " << argv[1] << std::endl; 32 | } 33 | 34 | // test TransitionIdToPdf 35 | for(int i=1;i::max()), 38 | _min_active(200), 39 | _lattice_beam(10.0), 40 | _prune_interval(25), 41 | _determinize_lattice(true), 42 | _beam_delta(0.5), 43 | _hash_ratio(2.0), 44 | _prune_scale(0.1) { } 45 | 46 | void Register(ConfigParseOptions *conf) 47 | { 48 | conf->Register("beam", &_beam, "Decoding beam. Larger->slower, more accurate."); 49 | conf->Register("max-active", &_max_active, "Decoder max active states. Larger->slower; more accurate"); 50 | conf->Register("min-active", &_min_active, "Decoder minimum #active states."); 51 | conf->Register("lattice-beam", &_lattice_beam, "Lattice generation beam. Larger->slower, and deeper lattices"); 52 | conf->Register("prune-interval", &_prune_interval, "Interval (in frames) at which to prune tokens"); 53 | conf->Register("determinize-lattice", &_determinize_lattice, "If true, " 54 | "determinize the lattice (lattice-determinization, keeping only " 55 | "best pdf-sequence for each word-sequence)."); 56 | conf->Register("beam-delta", &_beam_delta, "Increment used in decoding-- this " 57 | "parameter is obscure and relates to a speedup in the way the " 58 | "max-active constraint is applied. Larger is more accurate."); 59 | conf->Register("hash-ratio", &_hash_ratio, "Setting used in decoder to " 60 | "control hash behavior"); 61 | } 62 | void Check() const 63 | { 64 | assert(_beam > 0.0 && _max_active > 1 && _lattice_beam > 0.0 65 | && _prune_interval > 0 && _beam_delta > 0.0 && _hash_ratio >= 1.0 66 | && _prune_scale > 0.0 && _prune_scale < 1.0); 67 | } 68 | }; 69 | #include "src/util/namespace-end.h" 70 | #endif 71 | -------------------------------------------------------------------------------- /src/my-decoder/online-decoder-mempool-base.h: -------------------------------------------------------------------------------- 1 | #ifndef __ONLINE_DECODER_MEMPOOL_H__ 2 | #define __ONLINE_DECODER_MEMPOOL_H__ 3 | 4 | #include "src/my-decoder/online-decoder-base.h" 5 | #include "src/util/mem-pool.h" 6 | 7 | #include "src/util/namespace-start.h" 8 | // This is the same decoder strategy, 9 | // but use mempool control token and forwardlink 10 | // new and delete space. 11 | template 12 | class OnlineLatticeDecoderMempoolBase: 13 | public OnlineLatticeDecoderBase 14 | { 15 | public: 16 | using ForwardLinkT = ForwardLink; 17 | 18 | OnlineLatticeDecoderMempoolBase(FST *fst, 19 | const LatticeFasterDecoderConfig &config): 20 | OnlineLatticeDecoderBase(fst, config) 21 | { 22 | LOG_COM << "--- Construct OnlineLatticeDecoderMempoolBase clss ---"; 23 | } 24 | 25 | virtual ~OnlineLatticeDecoderMempoolBase() 26 | { 27 | this->ClearActiveTokens(); 28 | } 29 | protected: 30 | // add memory pool save token and forwardlink. 31 | MemPool _token_pools; 32 | MemPool _link_pools; 33 | 34 | inline virtual Token *NewToken(BaseFloat tot_cost, 35 | BaseFloat extra_cost, ForwardLinkT *links, 36 | Token *next, Token *backpointer) 37 | { 38 | Token *tok = _token_pools.NewT(); 39 | tok->_tot_cost = tot_cost; 40 | tok->_extra_cost = extra_cost; 41 | tok->_links = links; 42 | tok->_next = next; 43 | tok->_backpointer = backpointer; 44 | this->_num_toks++; 45 | return tok; 46 | } 47 | 48 | inline virtual ForwardLinkT *NewForwardLink(Token *next_tok, 49 | Label ilabel, Label olabel, BaseFloat graph_cost, 50 | BaseFloat acoustic_cost, ForwardLinkT *next) 51 | { 52 | ForwardLinkT *link = _link_pools.NewT(); 53 | link->_next_tok = next_tok; 54 | link->_ilabel = ilabel; 55 | link->_olabel = olabel; 56 | link->_graph_cost = graph_cost; 57 | link->_acoustic_cost = acoustic_cost; 58 | link->_next = next; 59 | this->_num_links++; 60 | return link; 61 | } 62 | 63 | inline virtual void DeleteToken(Token *tok) 64 | { 65 | _token_pools.DeleteT(tok); 66 | this->_num_toks--; 67 | } 68 | 69 | inline virtual void DeleteForwardLink(ForwardLinkT *link) 70 | { 71 | _link_pools.DeleteT(link); 72 | this->_num_links--; 73 | } 74 | 75 | }; 76 | 77 | typedef OnlineLatticeDecoderMempoolBase OnlineLatticeDecoderMempool; 78 | 79 | //typedef OnlineLatticeDecoderMempoolBase OnlineClgLatticeDecoderMempool; 80 | 81 | #include "src/util/namespace-end.h" 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /src/my-decoder/wordid-to-wordstr.h: -------------------------------------------------------------------------------- 1 | #ifndef __WORDID_TO_WORDSTR_H__ 2 | #define __WORDID_TO_WORDSTR_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/util/namespace-start.h" 9 | typedef int WordIdType; 10 | 11 | class WordSymbol 12 | { 13 | private: 14 | WordIdType *_words; // word offset 15 | char *_mem; 16 | WordIdType _word_maxid; 17 | int _word_range; 18 | size_t _mem_offset; 19 | size_t _mem_len; 20 | size_t _mem_range; 21 | public: 22 | WordSymbol() 23 | { 24 | _word_maxid = 1e4 * 5; 25 | _words = new WordIdType[_word_maxid]; 26 | _mem_len = 1e4 * 5 * 8;//default word list is 5w 27 | _mem = new char[_mem_len]; 28 | _mem_offset = 0; 29 | _word_range = 1000; 30 | _mem_range = 1000; 31 | } 32 | 33 | ~WordSymbol() 34 | { 35 | if(NULL != _words) 36 | delete []_words; 37 | if(NULL != _mem) 38 | delete []_mem; 39 | } 40 | int AddWord(char *word,WordIdType wordid) 41 | { 42 | if(wordid >= _word_maxid) 43 | { 44 | // realloc words 45 | WordIdType needlen = wordid + _word_range; 46 | WordIdType *tmp = new WordIdType[needlen]; 47 | memcpy(tmp, _words,sizeof(WordIdType)*_word_maxid); 48 | delete []_words; 49 | _words = tmp; 50 | _word_maxid = needlen; 51 | if(_word_range < 8000) 52 | _word_range *= 2; 53 | } 54 | int wordlen = strlen(word); 55 | if(wordlen + 1 + _mem_offset > _mem_len) 56 | { 57 | // realloc memery 58 | size_t needlen = wordlen + 1 + _mem_offset + _mem_range; 59 | char *tmp = new char[needlen]; 60 | memcpy(tmp, _mem, sizeof(char) * _mem_len); 61 | delete []_mem; 62 | _mem = tmp; 63 | _mem_len = needlen; 64 | if(_mem_range < 8000 * 5) 65 | _mem_range *= 2; 66 | } 67 | // add word to _mem 68 | 69 | _words[wordid] = _mem_offset; 70 | memcpy(_mem+_mem_offset, word, wordlen); 71 | _mem[_mem_offset+wordlen] = '\0'; 72 | _mem_offset += wordlen+1; 73 | return wordlen; 74 | } 75 | 76 | int ReadText(const char *wordfile) 77 | { 78 | FILE *fp = fopen(wordfile, "r"); 79 | if(NULL == fp) 80 | { 81 | fprintf(stderr,"fopen %s failed.\n",wordfile); 82 | return -1; 83 | } 84 | 85 | char line[256]; 86 | memset(line,0x00,sizeof(line)); 87 | int wordid = 0; 88 | char word[256]; 89 | memset(word,0x00,sizeof(word)); 90 | while(NULL != fgets(line,sizeof(line),fp)) 91 | { 92 | line[strlen(line)-1] = '\0'; 93 | if(2 != sscanf(line,"%s %d",word,&wordid)) 94 | { 95 | fprintf(stderr," read line \"%s\" error\n",line); 96 | return -1; 97 | } 98 | int ret = AddWord(word, wordid); 99 | assert(ret != 0); 100 | } 101 | fclose(fp); 102 | return 0; 103 | } 104 | 105 | char *FindWordStr(int wordid) 106 | { 107 | if(wordid > _word_maxid) 108 | { 109 | fprintf(stderr,"it's error id %d, no word.\n",wordid); 110 | return NULL; 111 | } 112 | size_t offset = _words[wordid]; 113 | return _mem + offset; 114 | } 115 | }; 116 | 117 | #include "src/util/namespace-end.h" 118 | #endif 119 | -------------------------------------------------------------------------------- /src/newfst/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = 2 | LDADD = ../newfst/liblatfst.la ../util/libutil.la 3 | lib_LTLIBRARIES = liblatfst.la 4 | 5 | liblatfst_la_SOURCES = lattice-determinize-api.cc \ 6 | lattice-to-nbest.cc \ 7 | connect-fst.cc lattice-fst.cc \ 8 | reverse.cc \ 9 | dfs-visit-fst.cc lattice-functions.cc \ 10 | rmepsilon.cc topsort.cc \ 11 | invert.cc 12 | liblatfst_la_LDFLAGS = -rpath '$(libdir)' 13 | 14 | 15 | check_PROGRAMS = test-connect-fst \ 16 | lattice-test \ 17 | test-code \ 18 | test-heap \ 19 | const-fst-read 20 | 21 | AM_DEFAULT_SOURCE_EXT = .cc 22 | 23 | #test_connect_fst_SOURCES = test-connect-fst.cc 24 | #lattice_test_SOURCES = lattice-test.cc 25 | #test_code_SOURCES = test-code.cc 26 | #test_heap_SOURCES = test-heap.cc 27 | #const_fst_read_SOURCES = const-fst-read.cc 28 | -------------------------------------------------------------------------------- /src/newfst/arc-iter.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARC_ITER_H__ 2 | #define __ARC_ITER_H__ 3 | 4 | #include "src/newfst/arc.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | 8 | // Add ArcIterator for clg and hclg or other extend method. 9 | template 10 | class ArcIterator 11 | { 12 | public: 13 | //using StateId = typename Arc::StateId; 14 | using Arc = typename FST::Arc; 15 | 16 | ArcIterator(FST *fst, StateId s): 17 | _arcs(fst->GetState(s)->Arcs()), 18 | _narcs(fst->GetState(s)->GetArcSize()), 19 | _i(0) { } 20 | 21 | ArcIterator(StdState *state): 22 | _arcs(state->Arcs()), 23 | _narcs(state->GetArcSize()), 24 | _i(0) { } 25 | 26 | bool Done() const { return _i >= _narcs; } 27 | 28 | const Arc &Value() const { return _arcs[_i]; } 29 | 30 | inline StateId NextState() {return _arcs[_i]._to;} 31 | 32 | void Next() { ++_i; } 33 | void Reset() { _i=0;} 34 | void Seek(size_t o) {_i = o;} 35 | size_t Position() const { return _i;} 36 | 37 | unsigned int Flags() const { return 0;} 38 | void SetFlags(unsigned , unsigned) { } 39 | private: 40 | const Arc *_arcs; 41 | size_t _narcs; 42 | size_t _i; 43 | }; 44 | 45 | #include "src/util/namespace-end.h" 46 | #endif 47 | -------------------------------------------------------------------------------- /src/newfst/compose-lat.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPOSE_LAT_H__ 2 | #define __COMPOSE_LAT_H__ 3 | 4 | #include "src/newfst/lattice-fst.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | template 8 | class LatticeComposeItf 9 | { 10 | public: 11 | virtual ~LatticeComposeItf() { } 12 | 13 | virtual I Start() = 0; 14 | 15 | virtual float Final(I s) = 0; 16 | 17 | virtual bool GetArc(I s, Label ilabel, LatticeArc* oarc) = 0; 18 | 19 | virtual bool GetArc(I s, Label ilabel, I *nextstate, LatticeWeight *lweight, Label *olabel) = 0; 20 | }; 21 | 22 | // fst w.Value2() * scale 23 | //template 24 | //void ComposeLattice(Lattice *clat, LatticeComposeItf *fst, Lattice *olat, float scale = 1.0); 25 | #include "src/util/namespace-end.h" 26 | 27 | #include "src/newfst/compose-lat-inl.h" 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /src/newfst/connect-fst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | using std::vector; 5 | 6 | #include "src/newfst/dfs-visit-fst.h" 7 | 8 | #include "src/util/namespace-start.h" 9 | 10 | void Connect(Lattice *lat) 11 | { 12 | vector access; 13 | vector coaccess; 14 | DfsVisit(lat, &access, &coaccess); 15 | 16 | vector dstates; 17 | for(StateId s = 0; s < static_cast(access.size()); ++s) 18 | if (!access[s] || !coaccess[s]) 19 | dstates.push_back(s); 20 | 21 | lat->DeleteStates(dstates); 22 | } 23 | #include "src/util/namespace-end.h" 24 | -------------------------------------------------------------------------------- /src/newfst/connect-fst.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONNECT_FST_H__ 2 | #define __CONNECT_FST_H__ 3 | 4 | #include "src/newfst/dfs-visit-fst.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | 8 | void Connect(Lattice *lat); 9 | #include "src/util/namespace-end.h" 10 | #endif 11 | -------------------------------------------------------------------------------- /src/newfst/const-fst-read.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include "src/newfst/const-fst.h" 4 | #include "src/newfst/optimize-fst.h" 5 | #include "src/newfst/arc.h" 6 | 7 | #ifdef NAMESPACE 8 | using namespace datemoon; 9 | #endif 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | if(argc != 2) 14 | { 15 | std::cout << argv[0] << " constfst." << std::endl; 16 | return -1; 17 | } 18 | std::string constfstfile(argv[1]); 19 | 20 | ConstFst constfst; 21 | if(constfst.Read(constfstfile) != true) 22 | { 23 | std::cerr << "read " << constfstfile << " failed!!!" << std::endl; 24 | return -1; 25 | } 26 | 27 | Fst fst(constfst); 28 | 29 | fst.PrintFst(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/newfst/invert.cc: -------------------------------------------------------------------------------- 1 | #include "src/newfst/lattice-fst.h" 2 | #include "src/newfst/invert.h" 3 | 4 | #include "src/util/namespace-start.h" 5 | void Invert(Lattice &fst) 6 | { 7 | StateId num_states = fst.NumStates(); 8 | for(StateId s = 0 ; s < num_states; ++s) 9 | { 10 | LatticeState *state = fst.GetState(s); 11 | for(unsigned i = 0; i < state->GetArcSize();++i) 12 | { 13 | //LatticeArc *arc = state->GetArc(i); 14 | state->GetArc(i)->Invert(); 15 | } 16 | } 17 | } 18 | #include "src/util/namespace-end.h" 19 | 20 | -------------------------------------------------------------------------------- /src/newfst/invert.h: -------------------------------------------------------------------------------- 1 | #ifndef __INVERT_H__ 2 | #define __INVERT_H__ 3 | 4 | 5 | #include "src/util/namespace-start.h" 6 | 7 | void Invert(Lattice &fst); 8 | #include "src/util/namespace-end.h" 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/newfst/lattice-determinize-api.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "src/newfst/lattice-determinize.h" 3 | 4 | #include "src/util/namespace-start.h" 5 | bool DeterminizeLatticeWrapper(Lattice *ifst, Lattice *ofst, 6 | DeterminizeLatticeOptions opts, bool *debug_ptr) 7 | { 8 | // first must invert ifst. 9 | Invert(*ifst); 10 | // second ifst input must be sort. 11 | ifst->ArcSort(); 12 | // third determinize. 13 | LatticeDeterminizer det(ifst, opts); 14 | 15 | if(!det.Determinize(debug_ptr)) 16 | return false; 17 | det.OutputNoolabel(ofst); 18 | // end invert ofst 19 | Invert(*ofst); 20 | return true; 21 | } 22 | 23 | #include "src/util/namespace-end.h" 24 | -------------------------------------------------------------------------------- /src/newfst/lattice-determinize-api.h: -------------------------------------------------------------------------------- 1 | #ifndef __LATTICE_DETERMINIZE_API_H__ 2 | #define __LATTICE_DETERMINIZE_API_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "src/newfst/lattice-fst.h" 9 | 10 | #include "src/util/namespace-start.h" 11 | //const float kDelta = 1.0/1024; 12 | 13 | struct DeterminizeLatticeOptions 14 | { 15 | float _delta; // A small offset used to measure equality of weights. 16 | int _max_mem; // If >0, determinization will fail and return false 17 | // when the algorithm's (approximate) memory consumption crosses this threshold. 18 | int _max_loop; // If >0, can be used to detect non-determinizable input 19 | // (a case that wouldn't be caught by max_mem). 20 | 21 | DeterminizeLatticeOptions():_delta(kDelta), _max_mem(-1), _max_loop(-1) { } 22 | }; 23 | 24 | bool DeterminizeLatticeWrapper(Lattice *ifst, Lattice *ofst, 25 | DeterminizeLatticeOptions opts, bool *debug_ptr); 26 | #include "src/util/namespace-end.h" 27 | 28 | #include "src/newfst/lattice-determinize.h" 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/newfst/lattice-fst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/newfst/lattice-fst.h" 4 | 5 | #include "src/util/namespace-start.h" 6 | void Lattice::Invert() 7 | { 8 | for(size_t i = 0; i < static_cast(NumStates()); ++i) 9 | { 10 | LatticeState *state = _state[i]; 11 | for(size_t j = 0; j < static_cast(state->GetArcSize()); ++j) 12 | { 13 | LatticeArc *arc = state->GetArc(j); 14 | arc->Invert(); 15 | } 16 | } 17 | } 18 | 19 | void Lattice::ArcSort() 20 | { 21 | for(size_t i = 0; i < static_cast(NumStates()); ++i) 22 | { 23 | LatticeState *state = _state[i]; 24 | state->SortArc(0,state->GetArcSize()-1); 25 | } 26 | } 27 | 28 | void Lattice::DelSameArc() 29 | { 30 | this->ArcSort(); 31 | for(size_t i = 0; i < static_cast(NumStates()); ++i) 32 | { 33 | LatticeState *state = _state[i]; 34 | state->DelSameArc(); 35 | } 36 | } 37 | 38 | bool Lattice::Write(FILE *fp) 39 | { 40 | if(NULL == fp) 41 | return false; 42 | // write state number 43 | size_t size = _state.size(); 44 | if(fwrite((const void *)&size, sizeof(_state.size()), 1, fp) != 1) 45 | { 46 | std::cerr << "Write lattice state number error." << std::endl; 47 | return false; 48 | } 49 | if(fwrite((const void *)&_startid, sizeof(_startid) , 1, fp) != 1) 50 | { 51 | std::cerr << "Write lattice start state error." << std::endl; 52 | return false; 53 | } 54 | for(size_t i=0; i<_state.size(); ++i) 55 | { 56 | if(_state[i]->Write(fp) != true) 57 | { 58 | std::cerr << "Write lattice state " << i << " error" << std::endl; 59 | return false; 60 | } 61 | } 62 | return true; 63 | } 64 | 65 | bool Lattice::Read(FILE *fp) 66 | { 67 | DeleteStates(); 68 | if(NULL == fp) 69 | return false; 70 | // write state number 71 | size_t size = 0; 72 | if(fread((void *)&size, sizeof(_state.size()), 1, fp) != 1) 73 | { 74 | std::cerr << "Read lattice state number error." << std::endl; 75 | return false; 76 | } 77 | _state.resize(size); 78 | if(fread((void *)&_startid, sizeof(_startid) , 1, fp) != 1) 79 | { 80 | std::cerr << "Read lattice start state error." << std::endl; 81 | return false; 82 | } 83 | for(size_t i=0; i<_state.size(); ++i) 84 | { 85 | _state[i] = new LatticeState; 86 | if(_state[i]->Read(fp) != true) 87 | { 88 | std::cerr << "Read lattice state " << i << " error" << std::endl; 89 | return false; 90 | } 91 | } 92 | return true; 93 | } 94 | #include "src/util/namespace-end.h" 95 | -------------------------------------------------------------------------------- /src/newfst/lattice-functions.h: -------------------------------------------------------------------------------- 1 | #ifndef __LATTICE_FUNCTIONS_H__ 2 | #define __LATTICE_FUNCTIONS_H__ 3 | 4 | #include "src/newfst/lattice-fst.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | void LatticeShortestPath(Lattice *ilat, Lattice *shortest_lat); 8 | 9 | // check top fst 10 | bool TopCheck(Lattice &fst); 11 | 12 | bool LatticeCheckFormat(Lattice *fst); 13 | 14 | void LatticeRmInput(Lattice &fst); 15 | 16 | void AddSuperFinalState(Lattice &fst); 17 | 18 | bool LatticeToVector(Lattice &best_path, 19 | std::vector &best_words_arr, std::vector &best_phones_arr, 20 | float &best_tot_score, float &best_lm_score); 21 | #include "src/util/namespace-end.h" 22 | #endif 23 | -------------------------------------------------------------------------------- /src/newfst/lattice-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include "src/newfst/optimize-fst.h" 4 | #include "src/newfst/connect-fst.h" 5 | 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | 10 | 11 | int main(int argc,char *argv[]) 12 | { 13 | std::string file("test.lat"); 14 | Lattice lat; 15 | lat.SetStart(0); 16 | for(int i=0;i<6;++i) 17 | lat.AddState(); 18 | LatticeArc arc1(1,0,1,LatticeWeight(0.5,0)); 19 | LatticeArc arc2(2,0,2,LatticeWeight(0.5,0)); 20 | LatticeArc arc3(3,0,3,LatticeWeight(0.5,0)); 21 | LatticeArc arc4(4,0,4,LatticeWeight(0.5,0)); 22 | LatticeArc arc5(5,0,5,LatticeWeight(0.5,0)); 23 | LatticeArc arc6(6,0,3,LatticeWeight(0.5,0)); 24 | lat.AddArc(0,arc1); 25 | lat.AddArc(1,arc2); 26 | lat.AddArc(1,arc3); 27 | lat.AddArc(1,arc4); 28 | lat.AddArc(2,arc5); 29 | lat.AddArc(4,arc6); 30 | lat.SetFinal(3); 31 | 32 | std::cout << "old fst" << std::endl; 33 | lat.Print(); 34 | if(lat.Write(file) != true) 35 | { 36 | std::cout << "Write lat " << file << " error." << std::endl; 37 | return 1; 38 | } 39 | if(lat.Read(file) != true) 40 | { 41 | std::cout << "Read lat " << file << " error." << std::endl; 42 | return 1; 43 | } 44 | std::cout << "write and read" << std::endl; 45 | lat.Print(); 46 | Connect(&lat); 47 | std::cout << "new fst" << std::endl; 48 | lat.Print(); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /src/newfst/reverse.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/newfst/lattice-fst.h" 3 | #include "src/newfst/reverse.h" 4 | 5 | #include "src/util/namespace-start.h" 6 | void Reverse(Lattice &ifst, Lattice *ofst) 7 | { 8 | StateId num_states = ifst.NumStates(); 9 | 10 | StateId istart = ifst.Start(); 11 | StateId ostart = ofst->AddState(); 12 | ofst->SetStart(ostart); 13 | 14 | for(StateId i = 0; i < num_states; ++i) 15 | { 16 | LatticeState * icur_state = ifst.GetState(i); 17 | StateId is = i; // ifst state id. 18 | StateId os = i + 1; // ofst corresponding state id. 19 | while(ofst->NumStates() <= os) 20 | ofst->AddState(); 21 | if(is == istart) 22 | ofst->SetFinal(os); 23 | 24 | // if ifst state is final link this state to start. 25 | if(icur_state->IsFinal()) 26 | { 27 | LatticeArc oarc(0 , 0, os, LatticeWeight::One()); 28 | ofst->AddArc(0, oarc); 29 | } 30 | 31 | for(unsigned j = 0; j < icur_state->GetArcSize(); ++j) 32 | { 33 | LatticeArc *iarc = icur_state->GetArc(j); 34 | LatticeArc oarc(iarc->_input, iarc->_output, os, iarc->_w); 35 | StateId nos = iarc->_to + 1; // arrive state 36 | while(ofst->NumStates() <= nos) 37 | ofst->AddState(); 38 | ofst->AddArc(nos, oarc); 39 | } 40 | } 41 | } 42 | #include "src/util/namespace-end.h" 43 | -------------------------------------------------------------------------------- /src/newfst/reverse.h: -------------------------------------------------------------------------------- 1 | #ifndef __FST_REVERSE_H__ 2 | #define __FST_REVERSE_H__ 3 | 4 | #include "src/newfst/lattice-fst.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | void Reverse(Lattice &ifst, Lattice *ofst); 8 | #include "src/util/namespace-end.h" 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/newfst/rmepsilon.h: -------------------------------------------------------------------------------- 1 | #ifndef __RMEPSILON_H__ 2 | #define __RMEPSILON_H__ 3 | 4 | #include "src/newfst/lattice-fst.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | void RmEpsilon(Lattice &ifst, int ilabel = 0); 8 | #include "src/util/namespace-end.h" 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/newfst/test-code.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | vector a; 9 | for(int i=0;i<10;++i) 10 | { 11 | int *b = new int(i); 12 | a.push_back(b); 13 | std::cout << *(a[i]) << std::endl; 14 | } 15 | { 16 | for(size_t i=0;i s; 19 | s.swap(a); 20 | 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/newfst/test-connect-fst.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //#include "src/newfst/optimize-fst.h" 4 | #include "src/newfst/connect-fst.h" 5 | 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | 10 | 11 | int main(int argc,char *argv[]) 12 | { 13 | Lattice lat; 14 | lat.SetStart(0); 15 | for(int i=0;i<6;++i) 16 | lat.AddState(); 17 | LatticeArc arc1(1,0,1,LatticeWeight(0.5,0)); 18 | LatticeArc arc2(2,0,2,LatticeWeight(0.5,0)); 19 | LatticeArc arc3(3,0,3,LatticeWeight(0.5,0)); 20 | LatticeArc arc4(4,0,4,LatticeWeight(0.5,0)); 21 | LatticeArc arc5(5,0,5,LatticeWeight(0.5,0)); 22 | LatticeArc arc6(6,0,3,LatticeWeight(0.5,0)); 23 | lat.AddArc(0,arc1); 24 | lat.AddArc(1,arc2); 25 | lat.AddArc(1,arc3); 26 | lat.AddArc(1,arc4); 27 | lat.AddArc(2,arc5); 28 | lat.AddArc(4,arc6); 29 | lat.SetFinal(3); 30 | 31 | std::cout << "old fst" << std::endl; 32 | lat.Print(); 33 | Connect(&lat); 34 | std::cout << "new fst" << std::endl; 35 | lat.Print(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /src/newfst/test-heap.cc: -------------------------------------------------------------------------------- 1 | // range heap example 2 | #include // std::cout 3 | #include // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap 4 | #include // std::vector 5 | 6 | class Compare 7 | { 8 | public: 9 | bool operator()(const int x, const int y) const 10 | { 11 | std::cout << x << " " << y << std::endl; 12 | return x > y; 13 | } 14 | }; 15 | int main () 16 | { 17 | Compare compare; 18 | int myints[] = {10,20,30,5,15}; 19 | std::vector v(myints,myints+5); 20 | 21 | std::make_heap (v.begin(),v.end(),compare); 22 | std::cout << "initial max heap : " << v.front() << '\n'; 23 | 24 | for (unsigned i=0; i &order) 8 | { 9 | if(order.size() != (unsigned)(fst->NumStates())) 10 | { 11 | LOG_WARN << "StateSort: bad order vector size: " << order.size() ; 12 | return ; 13 | } 14 | if(fst->Start() == kNoStateId) 15 | return; 16 | 17 | StateId startid = fst->Start(); 18 | fst->SetStart(order[startid]); 19 | // reorder fst state. 20 | LatticeState *tmp = NULL; 21 | LatticeState *cur = NULL; 22 | StateId nstates = fst->NumStates(); 23 | vector reorder(nstates, false); 24 | for(StateId id = 0; id < nstates ; ++id) 25 | { // reorder fst state 26 | if(reorder[id] == false) 27 | { 28 | StateId true_posi = order[id]; 29 | cur = fst->GetState(id); 30 | while(true_posi != id) 31 | { 32 | tmp = fst->GetState(true_posi); 33 | fst->SetState(true_posi, cur); 34 | reorder[true_posi] = true; 35 | true_posi = order[true_posi]; 36 | cur = tmp; 37 | } 38 | fst->SetState(true_posi,cur); 39 | } 40 | } 41 | } 42 | 43 | void TopSort(Lattice *fst) 44 | { 45 | vector access,coaccess; 46 | vector order; 47 | DfsVisit(fst, &access, &coaccess, &order); 48 | 49 | StateSort(fst, order); 50 | StateId nstates = fst->NumStates(); 51 | // modefy tostate 52 | for(StateId id = 0; id < nstates ; ++id) 53 | { 54 | LatticeState *state = fst->GetState(id); 55 | for(size_t i = 0 ; i < state->GetArcSize(); ++i) 56 | { 57 | //LatticeArc *arc = state->GetArc(i); 58 | //arc->_to = order[arc->_to]; 59 | StateId nextstate = state->GetArc(i)->_to; 60 | state->GetArc(i)->_to = order[nextstate]; 61 | } 62 | } 63 | } 64 | 65 | #include "src/util/namespace-end.h" 66 | -------------------------------------------------------------------------------- /src/newfst/topsort.h: -------------------------------------------------------------------------------- 1 | #ifndef __FST_TOPSORT_H__ 2 | #define __FST_TOPSORT_H__ 3 | 4 | #include 5 | #include "src/newfst/lattice-fst.h" 6 | 7 | #include "src/util/namespace-start.h" 8 | void StateSort(Lattice *fst,const std::vector &order); 9 | 10 | void TopSort(Lattice *fst); 11 | #include "src/util/namespace-end.h" 12 | #endif 13 | -------------------------------------------------------------------------------- /src/newlm/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | lib_LTLIBRARIES = liblm.la 3 | 4 | liblm_la_SOURCES = arpa2fsa.cc \ 5 | compose-arpalm.cc 6 | 7 | LDADD = liblm.la \ 8 | ../newfst/liblatfst.la \ 9 | ../util/libutil.la -lpthread 10 | 11 | liblm_la_LDFLAGS = -rpath '$(libdir)' 12 | 13 | noinst_PROGRAMS = arpa2fsa-bin \ 14 | lmscore \ 15 | compose-text \ 16 | lmrescale \ 17 | compose-diff-text \ 18 | lat-compose 19 | 20 | arpa2fsa_bin_SOURCES = arpa2fsa-bin.cc 21 | lmscore_SOURCES = lmscore.cc 22 | compose_text_SOURCES = compose-text.cc 23 | lmrescale_SOURCES = lmrescale.cc 24 | compose_diff_text_SOURCES = compose-diff-text.cc 25 | lat_compose_SOURCES = lat-compose.cc 26 | 27 | -------------------------------------------------------------------------------- /src/newlm/README.txt: -------------------------------------------------------------------------------- 1 | This is big lm code and second decode. 2 | 3 | -------------------------------------------------------------------------------- /src/newlm/arpa2fsa-bin.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/newlm/arpa2fsa.h" 3 | 4 | #ifdef NAMESPACE 5 | using namespace datemoon; 6 | #endif 7 | 8 | int main(int argc,char *argv[]) 9 | { 10 | if(argc != 4) 11 | { 12 | std::cerr << argv[0] << " arpafile wordlist outputfile" << std::endl; 13 | return -1; 14 | } 15 | std::string arpafile = argv[1]; 16 | std::string wordlist = argv[2]; 17 | std::string outputfile = argv[3]; 18 | Arpa2Fsa *arpa2fsa = new Arpa2Fsa(1, arpafile, wordlist); 19 | 20 | if(arpa2fsa->ConvertArpa2Fsa() != true) 21 | { 22 | std::cerr << "Convert arpa to fsa failed."<< std::endl; 23 | return -1; 24 | } 25 | if(arpa2fsa->Write(outputfile.c_str()) != true) 26 | { 27 | std::cerr << "Write " + outputfile + " failed." << std::endl; 28 | return -1; 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /src/newlm/compose-arpalm.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPOSE_ARPALM_H__ 2 | #define __COMPOSE_ARPALM_H__ 3 | #include "src/newlm/arpa2fsa.h" 4 | #include "src/newfst/compose-lat.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | class ComposeArpaLm:public LatticeComposeItf 8 | { 9 | private: 10 | ArpaLm *_lm; 11 | public: 12 | explicit ComposeArpaLm(ArpaLm *lm):_lm(lm) { } 13 | 14 | virtual FsaStateId Start(); 15 | 16 | // We cannot use "const" because the pure virtual function in the interface is 17 | // not const. 18 | virtual float Final(FsaStateId s); 19 | 20 | virtual bool GetArc(FsaStateId s, Label ilabel, LatticeArc* oarc); 21 | 22 | virtual bool GetArc(FsaStateId s, Label ilabel, FsaStateId *nextstate, LatticeWeight *lweight, Label *olabel); 23 | 24 | }; 25 | 26 | class ArpaLmScore:public ArpaLm 27 | { 28 | private: 29 | public: 30 | bool ComputerText(char *text); 31 | void ConvertText2Lat(char *text, Lattice *lat); 32 | void ComposeText(); 33 | }; 34 | 35 | void CutLine(char *line, std::vector &cut_line); 36 | #include "src/util/namespace-end.h" 37 | #endif 38 | -------------------------------------------------------------------------------- /src/newlm/compose-diff-text.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "src/newfst/compose-lat.h" 6 | #include "src/newlm/compose-arpalm.h" 7 | #include "src/newlm/diff-lm.h" 8 | 9 | #ifdef NAMESPACE 10 | using namespace datemoon; 11 | #endif 12 | int main(int argc, char *argv[]) 13 | { 14 | if(argc != 5) 15 | { 16 | std::cerr << argv[0] << " arpafile1 arpafile2 wordlist inputfile" << std::endl; 17 | return -1; 18 | } 19 | std::string fsafile1 = argv[1]; 20 | std::string fsafile2 = argv[2]; 21 | 22 | std::string wordlist = argv[3]; 23 | std::string textfile = argv[4]; 24 | ArpaLm arpalm1,arpalm2; 25 | if(arpalm1.Read(fsafile1.c_str()) != true) 26 | { 27 | std::cerr << "read arpafile1 failed." << fsafile1 << std::endl; 28 | return -1; 29 | } 30 | if(arpalm2.Read(fsafile2.c_str()) != true) 31 | { 32 | std::cerr << "read arpafile1 failed." << fsafile2 << std::endl; 33 | return -1; 34 | } 35 | 36 | arpalm1.Rescale(-1.0); 37 | DiffArpaLm diff_lm(&arpalm1, &arpalm2); 38 | 39 | ArpaLmScore *arpalmscore = new ArpaLmScore(); 40 | arpalmscore->ReadSymbols(wordlist.c_str()); 41 | 42 | std::fstream fp(textfile); 43 | std::string line; 44 | while(getline(fp,line)) 45 | { 46 | Lattice lat, olat; 47 | arpalmscore->ConvertText2Lat((char*)line.c_str(), &lat); 48 | 49 | ComposeLattice( &lat, 50 | static_cast* >(&diff_lm), 51 | &olat); 52 | olat.Print(); 53 | } 54 | delete arpalmscore; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /src/newlm/compose-text.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include "src/newfst/compose-lat.h" 6 | #include "src/newlm/compose-arpalm.h" 7 | 8 | #ifdef NAMESPACE 9 | using namespace datemoon; 10 | #endif 11 | int main(int argc, char *argv[]) 12 | { 13 | if(argc != 4) 14 | { 15 | std::cerr << argv[0] << " arpafile wordlist inputfile" << std::endl; 16 | return -1; 17 | } 18 | std::string fsafile = argv[1]; 19 | std::string wordlist = argv[2]; 20 | std::string textfile = argv[3]; 21 | ArpaLmScore *arpalmscore = new ArpaLmScore(); 22 | if(arpalmscore->Read(fsafile.c_str()) != true) 23 | { 24 | std::cerr << "read fsa failed." << std::endl; 25 | return -1; 26 | } 27 | arpalmscore->ReadSymbols(wordlist.c_str()); 28 | ComposeArpaLm *composearpalm = new ComposeArpaLm(static_cast(arpalmscore)); 29 | 30 | std::fstream fp(textfile); 31 | std::string line; 32 | while(getline(fp,line)) 33 | { 34 | Lattice lat, olat; 35 | arpalmscore->ConvertText2Lat((char*)line.c_str(), &lat); 36 | 37 | ComposeLattice( &lat, 38 | static_cast* >(composearpalm), 39 | &olat); 40 | olat.Print(); 41 | } 42 | delete arpalmscore; 43 | delete composearpalm; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /src/newlm/lat-compose.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "src/newfst/compose-lat.h" 8 | #include "src/newlm/compose-arpalm.h" 9 | #include "src/newfst/lattice-functions.h" 10 | #include "src/newfst/lattice-to-nbest.h" 11 | #include "src/my-decoder/wordid-to-wordstr.h" 12 | 13 | #ifdef NAMESPACE 14 | using namespace datemoon; 15 | #endif 16 | void PrintNbest(Lattice &lat, WordSymbol &wordsymbol, int n = 5) 17 | { 18 | Lattice nbest_lat; 19 | vector nbest_paths; 20 | NShortestPath(lat, &nbest_lat, n); 21 | ConvertNbestToVector(nbest_lat, &nbest_paths); 22 | for(unsigned i = 0; i < nbest_paths.size(); ++i) 23 | { 24 | vector nbest_words_arr; 25 | vector nbest_phones_arr; 26 | float nbest_tot_score=0 ,nbest_lm_score=0; 27 | if(LatticeToVector(nbest_paths[i], nbest_words_arr, nbest_phones_arr, nbest_tot_score, nbest_lm_score)) 28 | { 29 | for(unsigned j = 0; j < nbest_words_arr.size(); ++j) 30 | { 31 | printf("%s ",wordsymbol.FindWordStr(nbest_words_arr[j])); 32 | } 33 | printf("%f %f\n",nbest_tot_score,nbest_lm_score); 34 | } 35 | } 36 | } 37 | int main(int argc, char *argv[]) 38 | { 39 | if(argc != 6) 40 | { 41 | std::cerr << argv[0] << " arpafile wordlist scale latfile latout" << std::endl; 42 | return -1; 43 | } 44 | std::string fsafile = argv[1]; 45 | std::string wordlist = argv[2]; 46 | float scale = atof(argv[3]); 47 | std::string latfile = argv[4]; 48 | std::string latoutfile = argv[5]; 49 | ArpaLm arpalmscore; 50 | WordSymbol wordsymbol; 51 | if(0 != wordsymbol.ReadText(wordlist.c_str())) 52 | { 53 | LOG_WARN << "read wordlist " << wordlist << " failed!!!"; 54 | return -1; 55 | } 56 | if(arpalmscore.Read(fsafile.c_str()) != true) 57 | { 58 | std::cerr << "read fsa failed." << std::endl; 59 | return -1; 60 | } 61 | ComposeArpaLm composearpalm(static_cast(&arpalmscore)); 62 | 63 | FILE *fp = fopen(latfile.c_str(), "rb"); 64 | FILE *fpout = fopen(latoutfile.c_str(), "wb"); 65 | Lattice lat; 66 | struct timeval start,end; 67 | float tot_time = 0.0; 68 | int nlat = 0; 69 | while(lat.Read(fp)) 70 | { 71 | nlat++; 72 | Lattice olat,olat_tmp; 73 | 74 | gettimeofday(&start,NULL); 75 | ComposeLattice( &lat, 76 | static_cast* >(&composearpalm), 77 | &olat, scale); 78 | gettimeofday(&end,NULL); 79 | tot_time += end.tv_sec-start.tv_sec + 80 | (end.tv_usec - start.tv_usec)*1.0/(1000*1000); 81 | 82 | PrintNbest(olat, wordsymbol, 10); 83 | olat.Write(fpout); 84 | } 85 | std::cout << "total time is : " << tot_time << " s " << 86 | " rt is : " << tot_time/nlat << std::endl; 87 | 88 | fclose(fp); 89 | fclose(fpout); 90 | return 0; 91 | } 92 | 93 | -------------------------------------------------------------------------------- /src/newlm/lmrescale.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "src/newlm/arpa2fsa.h" 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | if(argc != 4) 12 | { 13 | std::cerr << argv[0] << " inarpafile rescale scalearpafile" << std::endl; 14 | return -1; 15 | } 16 | std::string fsafile = argv[1]; 17 | float scale = atof(argv[2]); 18 | std::string scalefsa = argv[3]; 19 | ArpaLm arpalm; 20 | if(arpalm.Read(fsafile.c_str()) != true) 21 | { 22 | std::cerr << "read fsa failed." << std::endl; 23 | return -1; 24 | } 25 | arpalm.Rescale(scale); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /src/newlm/lmscore.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "src/newlm/compose-arpalm.h" 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | if(argc != 4) 12 | { 13 | std::cerr << argv[0] << " arpafile wordlist inputfile" << std::endl; 14 | return -1; 15 | } 16 | std::string fsafile = argv[1]; 17 | std::string wordlist = argv[2]; 18 | std::string textfile = argv[3]; 19 | ArpaLmScore *arpalmscore = new ArpaLmScore(); 20 | if(arpalmscore->Read(fsafile.c_str()) != true) 21 | { 22 | std::cerr << "read fsa failed." << std::endl; 23 | return -1; 24 | } 25 | arpalmscore->ReadSymbols(wordlist.c_str()); 26 | std::fstream fp(textfile); 27 | std::string line; 28 | while(getline(fp,line)) 29 | { 30 | if(arpalmscore->ComputerText((char*)line.c_str()) != true) 31 | { 32 | std::cerr << "Computer \"" << line << "\" failed." << std::endl; 33 | } 34 | } 35 | delete arpalmscore; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /src/nnet/FeatureExtractor.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/datemoon/ASR-decoder/73a283c6d78942d038165dbee9d85019c09f6fb5/src/nnet/FeatureExtractor.h -------------------------------------------------------------------------------- /src/nnet/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(MATRIX_INC) -DCBLAS 3 | lib_LTLIBRARIES = libnnet.la 4 | 5 | libnnet_la_SOURCES = nnet-layer.cc \ 6 | matrix.cc \ 7 | nnet-component.cc \ 8 | nnet-nnet.cc \ 9 | nnet-feature-api.cc \ 10 | lstm-layer.cc \ 11 | nnet-util.cc \ 12 | cudnn-lstm-layer.cc \ 13 | nnet-feature-pitch-api.cc \ 14 | lstm-projected-layer.cc \ 15 | nnet-simple-recurrent.cc \ 16 | tf-lstm-layer.cc 17 | 18 | AM_LDFLAGS = $(MATRIX_DEPLDFLAGS) $(KALDI_DEPLDFLAGS) $(CUDA_DEPLDFLAGS) 19 | LDADD = libnnet.la \ 20 | ../pitch/libpitch.la \ 21 | ../hmm/libhmm.la \ 22 | ../util/libutil.la \ 23 | $(MATRIX_LIBS) $(FEATLIB) \ 24 | -lm -lpthread -ldl 25 | 26 | 27 | check_PROGRAMS = nnetread \ 28 | nnet-feature-api-test \ 29 | forward-test \ 30 | kaldi-forward-test \ 31 | nnet-read-test \ 32 | pitch-forward-test \ 33 | stream-kaldi-forward-test \ 34 | kaldi-format-pitch-forward-test 35 | 36 | nnetread_SOURCES = nnetread.cc 37 | nnet_feature_api_test_SOURCES = nnet-feature-api-test.cc 38 | forward_test_SOURCES = forward-test.cc 39 | kaldi_forward_test_SOURCES = kaldi-forward-test.cc 40 | nnet_read_test_SOURCES = nnet-read-test.cc 41 | pitch_forward_test_SOURCES = pitch-forward-test.cc 42 | stream_kaldi_forward_test_SOURCES = stream-kaldi-forward-test.cc 43 | kaldi_format_pitch_forward_test_SOURCES = kaldi-format-pitch-forward-test.cc 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/nnet/README.txt: -------------------------------------------------------------------------------- 1 | current code is nnet forward calculate. 2 | The feature extracte have been package for nnet forward, 3 | now it's can work, but have some optimize can be done. 4 | -------------------------------------------------------------------------------- /src/nnet/fbanks.cfg: -------------------------------------------------------------------------------- 1 | [global] 2 | 3 | TARGETRATE=100000.0 4 | SOURCEKIND=PK_WAVEFORM 5 | SOURCERATE=1250 6 | ZMEANSOURCE=false 7 | WINDOWSIZE=250000.0 8 | USEHAMMING=true 9 | 10 | NUMCHANS=40 11 | NUMCEPS=40 12 | CEPLIFTER=22 13 | USEPOWER=false 14 | 15 | CEPSCALE=10.0 16 | KLOWSHIFT=0 17 | 18 | #HTK 19 | ENERGY=_0 20 | 21 | #ENORMALISE is only for _E 22 | #it is offline AGN before delta 23 | ENORMALISE=false 24 | ESCALE=1.00 25 | SILFLOOR=50 26 | LPCORDER=12 27 | COMPRESSFACT=0.33 28 | DELTAORDER=_D_A 29 | #eigen 30 | #XFORMMATRIX=./eigen.bin 31 | #CMNFILE=./CMNTrain/hmm_0/cms.bin 32 | #DataProcessorChain=FBANKS|LiveCMN 33 | DataProcessorChain=FBANKS 34 | NATURALWRITEORDER=true 35 | 36 | 37 | NORMVAR=true 38 | #Variance normalization, default setting as last version 39 | #NORMVARFLOOR=1.0 40 | #NORMVARFORGETTINGFACTOR=0.9 41 | #NORMLOOKAHEADFRM=0 42 | #NORMVARSCALE=4.0 43 | 44 | #Another setting, maybe optimzed 45 | NORMVARFLOOR=0.0001 46 | NORMVARFORGETTINGFACTOR=0.992f 47 | NORMLOOKAHEADFRM=20 48 | NORMVARSCALE=0.25 49 | 50 | ADDDITHER=0.1 51 | 52 | -------------------------------------------------------------------------------- /src/nnet/forward-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/nnet/nnet-nnet.h" 3 | #include "src/nnet/nnet-feature-api.h" 4 | 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | 9 | int main(int argc,char *argv[]) 10 | { 11 | if(argc != 4) 12 | { 13 | fprintf(stderr,"%s conf nnetmodel wav\n",argv[0]); 14 | return -1; 15 | } 16 | Nnet *nnet = new Nnet; 17 | nnet->ReadNnet(argv[2]); 18 | // new source. 19 | NnetForward *forward = new NnetForward(nnet); 20 | int left = 0, right =0 ; 21 | 22 | forward->GetLRoffset(left,right); 23 | DnnFeat *dnnfeat = new DnnFeat; 24 | dnnfeat->Init(argv[1],"./"); 25 | dnnfeat->InitParameters(left,right); 26 | #define LEN (1024) 27 | char data[LEN]; 28 | FILE *fp = fopen(argv[3],"rb"); 29 | if(NULL == fp) 30 | { 31 | fprintf(stderr,"fopen %s error.\n",argv[3]); 32 | return -1; 33 | } 34 | fread(data,sizeof(char),44,fp); 35 | int frame = 0; 36 | int len = LEN; 37 | int nnet_in_frame = 0; 38 | int dim = 0; 39 | float *feats = NULL; 40 | while(0 != (len = fread(data,sizeof(char),len,fp))) 41 | { 42 | int end = 0; 43 | if(feof(fp)) 44 | end = 1; 45 | dnnfeat->GetFeats(data,len,&feats,&nnet_in_frame,&dim,end); 46 | // use features. 47 | 48 | forward->FeedForward(feats,nnet_in_frame,dim); 49 | int outdim = forward->GetOutdim(); 50 | for(int i = 0;iExamineFrame(frame+i)) 54 | { 55 | fprintf(stderr,"it's nnet forward error.\n"); 56 | return -1; 57 | } 58 | for(int j=0;jLogLikelihood(frame+i,j)); 60 | printf("\n"); 61 | } 62 | frame +=nnet_in_frame; 63 | dnnfeat->RemoveFeats(nnet_in_frame); 64 | } 65 | 66 | fclose(fp); 67 | delete dnnfeat; 68 | delete forward; 69 | delete nnet; 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /src/nnet/kaldi-forward-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/nnet/nnet-nnet.h" 4 | #include "src/nnet/nnet-feature-api.h" 5 | #include "src/nnet/nnet-util.h" 6 | 7 | #ifdef NAMESPACE 8 | using namespace datemoon; 9 | #endif 10 | 11 | int main(int argc,char *argv[]) 12 | { 13 | const char *usage = "This is a nnet forward.\n\n" 14 | "Usage:kaldi-forward-test featconf nnetmodel feat.list\n";; 15 | ConfigParseOptions conf(usage); 16 | 17 | NnetForwardOptions nnetopt; 18 | nnetopt.Register(&conf); 19 | conf.Read(argc, argv); 20 | 21 | if(conf.NumArgs() != 4) 22 | { 23 | conf.PrintUsage(); 24 | //fprintf(stderr,"%s conf nnetmodel feat\n",argv[0]); 25 | return -1; 26 | } 27 | std::string featconf = conf.GetArg(1); 28 | std::string am_filename = conf.GetArg(2), 29 | feat_list = conf.GetArg(3); 30 | 31 | Nnet *nnet = new Nnet; 32 | nnet->ReadNnet(am_filename.c_str()); 33 | // new source. 34 | NnetForward *forward = new NnetForward(nnet, &nnetopt); 35 | int left = 0, right =0 ; 36 | 37 | forward->GetLRoffset(left,right); 38 | DnnFeat *dnnfeat = new DnnFeat; 39 | dnnfeat->Init(featconf.c_str(),"./"); 40 | dnnfeat->InitParameters(left,right); 41 | #define LEN (1024) 42 | int nnet_in_frame = 0; 43 | int dim = 0; 44 | float *feats = NULL; 45 | 46 | FILE *featfp = fopen(feat_list.c_str(),"r"); 47 | if(NULL == featfp) 48 | { 49 | fprintf(stderr,"fopen %s failed.\n", feat_list.c_str()); 50 | return -1; 51 | } 52 | float all_wavtime = 0; 53 | float all_nnettime = 0; 54 | std::string key,file; 55 | long offset = 0; 56 | while(ReadScp(featfp, key, file, offset) == true) 57 | { 58 | #define DEBUG 59 | #ifdef DEBUG 60 | int frame = 0; 61 | #endif 62 | while(1) 63 | { 64 | forward->Reset(); 65 | dnnfeat->Reset(); 66 | dnnfeat->GetFeats(file.c_str() ,&feats,&nnet_in_frame,&dim); 67 | if(feats == NULL) 68 | { 69 | fprintf(stderr,"GetFeats failed.\n"); 70 | return -1; 71 | } 72 | #define TESTTIME 73 | #ifdef TESTTIME 74 | struct timeval start,end; 75 | gettimeofday(&start,NULL); 76 | #endif 77 | forward->FeedForward(feats,nnet_in_frame,dim); 78 | #ifdef TESTTIME 79 | gettimeofday(&end,NULL); 80 | float usetime = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1000000.0; 81 | all_nnettime += usetime; 82 | all_wavtime += nnet_in_frame/100.0; 83 | gettimeofday(&start,NULL); 84 | #endif 85 | #ifdef DEBUG 86 | int outdim = forward->GetOutdim(); 87 | printf("%s [\n",key.c_str()); 88 | for(int i = 0;iExamineFrame(frame+i)) 91 | { 92 | break; 93 | } 94 | for(int j=0;jLogLikelihood(frame+i,j+1)); 96 | if(false == forward->ExamineFrame(frame+i+1)) 97 | printf("]"); 98 | printf("\n"); 99 | } 100 | frame +=nnet_in_frame; 101 | #endif 102 | dnnfeat->RemoveFeats(nnet_in_frame); 103 | break; 104 | } 105 | } 106 | // printf("nnet rt is %f\n",all_nnettime/all_wavtime); 107 | // fclose(fp); 108 | delete dnnfeat; 109 | delete forward; 110 | delete nnet; 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /src/nnet/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef __MATRIX_H__ 2 | #define __MATRIX_H__ 3 | #include 4 | #define Exp(x) expf(x) 5 | #define Log(x) logf(x) 6 | 7 | #include "src/util/namespace-start.h" 8 | /* alpha*A*B + beta * C **/ 9 | // m : A matrix row , C matrix col 10 | // n : B matrix col , C matrix row 11 | // k : A matrix col , B matrix row 12 | void MatrixMulMatrix(float* A,float* B,float* C,int m,int n,int k,float alpha,float beta); 13 | 14 | void MatrixMulVector(float *A,float *B,float *C,int arow,int acol,float alpha,float beta); 15 | 16 | void VectorMulVector(float *A,float *B,float *C,int dim,float alpha,float beta); 17 | 18 | void AddVecToRows(float *A,int row,int col,float *B,float alpha,float beta); 19 | 20 | void MulColsVec(float *A,int row,int col,float *B); 21 | 22 | void MatAddMat(float *A,int row,int col, float *B, float alpha); 23 | 24 | void AddVecToRowsRange(float *A,int row,int col,float *B, int B_col, int offset,float alpha,float beta); 25 | 26 | void DoSigmoid(float *A,int row,int col,float *B); 27 | 28 | float DoMax(float *data,int dim); 29 | 30 | void DoSoftmax(float *A,int row,int col,float *B); 31 | 32 | void DoLog(float *A,int row,int col,float *B); 33 | 34 | void DoTanH(float *A,int row,int col,float *B); 35 | 36 | #include "src/util/namespace-end.h" 37 | #endif 38 | -------------------------------------------------------------------------------- /src/nnet/nnet-component.h: -------------------------------------------------------------------------------- 1 | #ifndef __NNET_COMPONENT_H__ 2 | #define __NNET_COMPONENT_H__ 3 | 4 | #include 5 | #include "src/util/namespace-start.h" 6 | 7 | using namespace std; 8 | typedef enum 9 | { 10 | kUnknown = 0x00, 11 | 12 | kAffineTransform = 0x0100, 13 | kLinearTransform, 14 | kLstmProjectedStreams, 15 | kLstm, 16 | kLstmCudnn, 17 | kSRU, 18 | kTfLstm, 19 | kLstmProjected, 20 | 21 | kActivationFunction = 0x0200, 22 | kSoftmax, 23 | kSigmoid, 24 | 25 | kTranform = 0x0400, 26 | kSplice, 27 | kAddShift, 28 | kRescale, 29 | 30 | kPrior = 0x0600 31 | }ComponentType; 32 | 33 | class Component 34 | { 35 | public: 36 | Component(int input_dim, int output_dim): 37 | _input_dim(input_dim), _output_dim(output_dim) { } 38 | 39 | virtual ~Component() { } 40 | 41 | virtual ComponentType GetType() const = 0; 42 | 43 | virtual void GetType(string &type) const = 0; 44 | 45 | int InputDim() { return _input_dim; } 46 | 47 | int OutputDim() { return _output_dim; } 48 | 49 | void Propagate(float *in,int frames,int dim,float *out,int &outdim) ; 50 | 51 | static Component* Read(FILE *fp); 52 | 53 | static Component* Read(FILE *fp, bool binary); 54 | 55 | bool Write(FILE *fp); 56 | 57 | 58 | virtual int GetLeft() {return 0;} 59 | virtual int GetRight() {return 0;} 60 | protected: 61 | virtual bool ReadData(FILE *fp) { return true;} 62 | 63 | virtual bool ReadData(FILE *fp, bool binary) { return true;} 64 | 65 | virtual bool WriteData(FILE *fp) { return true;} 66 | private: 67 | virtual void PropagateFnc(float *in, int frames, int cols, float *out ) = 0; 68 | private: 69 | int _input_dim; 70 | int _output_dim; 71 | private: 72 | static Component* NewComponentOfType(ComponentType t, int input_dim, int output_dim); 73 | }; 74 | ComponentType GetTypeInOutDim(char *str,int &indim, int &outdim, bool essen); 75 | #include "src/util/namespace-end.h" 76 | #endif 77 | -------------------------------------------------------------------------------- /src/nnet/nnet-feature-api-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/nnet/nnet-feature-api.h" 3 | 4 | #ifdef NAMESPACE 5 | using namespace datemoon; 6 | #endif 7 | 8 | int main(int argc,char *argv[]) 9 | { 10 | DnnFeat *dnnfeat = new DnnFeat; 11 | dnnfeat->Init(argv[1],"./"); 12 | int left = 35; 13 | int right = 10; 14 | dnnfeat->InitParameters(left,right); 15 | #define LEN (1024) 16 | char data[LEN]; 17 | FILE *fp = fopen(argv[2],"rb"); 18 | if(fp == NULL) 19 | { 20 | fprintf(stderr,"fopen %s error.\n",argv[2]); 21 | return -1; 22 | } 23 | fread(data,sizeof(char),44,fp); 24 | int frame = 0; 25 | int len = LEN; 26 | int nnet_in_frame = 0; 27 | int dim = 0; 28 | float *feats = NULL; 29 | while(0 != (len = fread(data,sizeof(char),len,fp))) 30 | { 31 | dnnfeat->GetFeats(data,len,&feats,&nnet_in_frame,&dim,0); 32 | // use features 33 | for(int k=0;kRemoveFeats(nnet_in_frame); 43 | } 44 | dnnfeat->GetFeats(data,len,&feats,&nnet_in_frame,&dim,1); 45 | for(int k=0;kRemoveFeats(nnet_in_frame); 54 | fclose(fp); 55 | delete dnnfeat; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/nnet/nnet-forward.h: -------------------------------------------------------------------------------- 1 | #ifndef __NNET_FORWARD_H__ 2 | #define __NNET_FORWARD_H__ 3 | 4 | #include 5 | 6 | #include "src/util/namespace-start.h" 7 | class Lstm 8 | { 9 | }; 10 | 11 | class DnnItf 12 | { 13 | public: 14 | // return frames >= 0. 15 | int CalDnnScore(float *feat,int frames,int dim); 16 | 17 | float LogLikelihood(int frame, int ilabel); 18 | 19 | int NumFrame() { return _num_frame;} 20 | 21 | void DecodeOneFrame() { _have_process_frames++ ;} 22 | 23 | bool ExamineFrame() 24 | { 25 | assert(_num_frames >=_have_process_frames); 26 | if(_num_frames == _have_process_frames) 27 | return false; 28 | return true; 29 | } 30 | private: 31 | float *_feats; 32 | int _feat_capacity; // default 1 minute. 33 | int _feat_size; 34 | int _feat_dim; 35 | 36 | int _num_frames; 37 | int _have_process_frames; 38 | int _shiks; 39 | 40 | float *_post; 41 | }; 42 | 43 | 44 | #include "src/util/namespace-end.h" 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/nnet/nnet-layer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/nnet/nnet-layer.h" 4 | #include "src/nnet/matrix.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | void Splice::PropagateFnc(float *in, int frames, int cols, float *out) 8 | { 9 | assert(InputDim() == cols); 10 | // here _offsets must be continues. 11 | for(int i=0;i 2 | #include "src/nnet/nnet-nnet.h" 3 | #include "src/nnet/nnet-feature-api.h" 4 | 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | 9 | int main(int argc,char *argv[]) 10 | { 11 | if(argc != 2) 12 | { 13 | fprintf(stderr,"%s nnetmodel\n",argv[0]); 14 | return -1; 15 | } 16 | Nnet *nnet = new Nnet; 17 | nnet->ReadNnet(argv[1],false); 18 | return 0; 19 | // new source. 20 | NnetForward *forward = new NnetForward(nnet,2,false,false); 21 | int left = 0, right =0 ; 22 | 23 | forward->GetLRoffset(left,right); 24 | DnnFeat *dnnfeat = new DnnFeat; 25 | dnnfeat->Init(argv[1],"./"); 26 | dnnfeat->InitParameters(left,right); 27 | #define LEN (1024) 28 | /* 29 | char data[LEN]; 30 | FILE *fp = fopen(argv[3],"rb"); 31 | if(NULL == fp) 32 | { 33 | fprintf(stderr,"fopen %s error.\n",argv[3]); 34 | return -1; 35 | } 36 | fread(data,sizeof(char),44,fp); 37 | int len = LEN; 38 | */ int frame = 0; 39 | int nnet_in_frame = 0; 40 | int dim = 0; 41 | float *feats = NULL; 42 | while(1) 43 | { 44 | dnnfeat->GetFeats(argv[3],&feats,&nnet_in_frame,&dim); 45 | // use features. 46 | 47 | forward->ResetRnnBuffer(); 48 | forward->FeedForward(feats,nnet_in_frame,dim); 49 | #define DEBUG 50 | #ifdef DEBUG 51 | int outdim = forward->GetOutdim(); 52 | for(int i = 0;iExamineFrame(frame+i)) 56 | { 57 | fprintf(stderr,"it's nnet forward error.\n"); 58 | return -1; 59 | } 60 | for(int j=0;jLogLikelihood(frame+i,j)); 62 | printf("\n"); 63 | } 64 | #endif 65 | frame +=nnet_in_frame; 66 | dnnfeat->RemoveFeats(nnet_in_frame); 67 | break; 68 | } 69 | 70 | // fclose(fp); 71 | delete dnnfeat; 72 | delete forward; 73 | delete nnet; 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /src/nnet/nnet-simple-recurrent.h: -------------------------------------------------------------------------------- 1 | #ifndef __NNET_SIMPLE_RECURRENT_H__ 2 | #define __NNET_SIMPLE_RECURRENT_H__ 3 | #include 4 | #include "src/nnet/matrix.h" 5 | #include "src/nnet/nnet-component.h" 6 | 7 | 8 | #include "src/util/namespace-start.h" 9 | 10 | class SRUcell :public Component 11 | { 12 | public: 13 | SRUcell(int input_dim,int output_dim): 14 | Component( input_dim, output_dim), _xfrh_buffer(NULL), _capacity(0), _buffer(NULL),_gh_buffer(NULL), 15 | _hidden_rows(0),_input_dims(0),_hidden_dim(0),_out_dim(0), _wei_xfrh(NULL), _bias_f(NULL), _bias_r(NULL){ } 16 | 17 | ~SRUcell() 18 | { 19 | if(_xfrh_buffer != NULL) delete[] _xfrh_buffer; 20 | if(_buffer != NULL) delete[] _buffer; 21 | if(_gh_buffer != NULL) delete[] _gh_buffer; 22 | if(_wei_xfrh != NULL) delete[] _wei_xfrh; 23 | if(_bias_f != NULL) delete[] _bias_f; 24 | if(_bias_r != NULL) delete[] _bias_r; 25 | } 26 | 27 | ComponentType GetType() const { return kSRU;} 28 | 29 | void GetType(string &type) const { type = "SRU";} 30 | 31 | void ResetBuf(); 32 | 33 | void InitBuf(); 34 | 35 | bool ReadData(FILE *fp,bool binary); 36 | 37 | bool ReadData(FILE *fp) 38 | { 39 | int input_dim = InputDim(); 40 | int output_dim = OutputDim(); 41 | _input_dims = input_dim; 42 | _out_dim = output_dim; 43 | _hidden_rows = _out_dim*4; 44 | 45 | // malloc xfrh weight space. 46 | _wei_xfrh = new float[_hidden_rows * _input_dims]; 47 | _bias_f = new float[_out_dim]; 48 | _bias_r = new float[_out_dim]; 49 | 50 | // read _wei_xfrh 51 | if (_hidden_rows * _input_dims != (int)fread(_wei_xfrh,sizeof(float),_hidden_rows * _input_dims,fp)) 52 | return false; 53 | // read _bias_f 54 | if(_out_dim != (int)fread(_bias_f, sizeof(float),_out_dim,fp)) 55 | return false; 56 | // read _bias_r 57 | if(_out_dim != (int)fread(_bias_r, sizeof(float),_out_dim,fp)) 58 | return false; 59 | return true; 60 | } 61 | 62 | bool WriteData(FILE *fp) 63 | { 64 | if (_hidden_rows * _input_dims != (int)fwrite(_wei_xfrh,sizeof(float),_hidden_rows * _input_dims,fp)) 65 | return false; 66 | if(_out_dim != (int)fwrite(_bias_f, sizeof(float),_out_dim,fp)) 67 | return false; 68 | if(_out_dim != (int)fwrite(_bias_r, sizeof(float),_out_dim,fp)) 69 | return false; 70 | return true; 71 | } 72 | 73 | void PropagateFnc(float *in, int frames, int cols, float *out ); 74 | private: 75 | void MallocXfrhMemory(int frames); 76 | 77 | private: 78 | float *_xfrh_buffer; 79 | int _capacity; 80 | float *_buffer; // 2 * _out_dim 81 | float *_gh_buffer; //_out_dim 82 | 83 | private: 84 | int _hidden_rows; // output dim * 4(xfrh) 85 | int _input_dims; // input dim 86 | int _hidden_dim; // hidden cell dim (it's should equal outdim) 87 | int _out_dim; // output dim 88 | 89 | float *_wei_xfrh; 90 | float *_bias_f; 91 | float *_bias_r; 92 | }; 93 | 94 | #include "src/util/namespace-end.h" 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/nnet/nnet-util.h: -------------------------------------------------------------------------------- 1 | #ifndef __NNET_UTIL_H__ 2 | #define __NNET_UTIL_H__ 3 | 4 | #include "src/util/namespace-start.h" 5 | bool ReadScp(FILE *fp, std::string &key, std::string &file, long &offset); 6 | 7 | void ReadMatrixData(FILE *fp,float *mat,int rows,int cols); 8 | 9 | void ReadMatrix(FILE *fp,float *mat,int row,int col); 10 | 11 | void ReadMatrix(FILE *fp,int *mat,int row,int col); 12 | 13 | bool ReadLine(FILE *fp,char *str,int len); 14 | 15 | template 16 | void PrintPoint(Real mat,int rows,int cols); 17 | 18 | template 19 | int MaxVec(Real vec,int n); 20 | #include "src/util/namespace-end.h" 21 | #endif 22 | -------------------------------------------------------------------------------- /src/nnet/nnetread.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/nnet/nnet-nnet.h" 3 | 4 | 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | int main(int argc,char *argv[]) 9 | { 10 | Nnet *nnet = new Nnet; 11 | 12 | //#define BINARY 13 | #ifdef BINARY 14 | if(false == nnet->ReadNnet(argv[1])) 15 | { 16 | fprintf(stderr,"read nnet %s error.\n",argv[1]); 17 | return -1; 18 | } 19 | #else 20 | nnet->ReadNnet(argv[1],true); 21 | nnet->ReadNnet(argv[2],true); 22 | nnet->ReadNnet(argv[3],true); 23 | if(false == nnet->WriteNnet(argv[4])) 24 | std::cout << "write nnet error!" << std::endl; 25 | #endif 26 | delete nnet; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /src/nnet/stream-kaldi-forward-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/nnet/nnet-nnet.h" 4 | #include "src/nnet/nnet-feature-api.h" 5 | 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | 10 | int main(int argc,char *argv[]) 11 | { 12 | if(argc != 4) 13 | { 14 | fprintf(stderr,"%s conf nnetmodel feat\n",argv[0]); 15 | return -1; 16 | } 17 | struct timeval start,end; 18 | float all_nnettime = 0; 19 | 20 | Nnet *nnet = new Nnet; 21 | //nnet->ReadNnet(argv[2]); 22 | nnet->ReadNnet(argv[2],true); 23 | // new source. 24 | //NnetForward *forward = new NnetForward(nnet,2,false,false); 25 | NnetForward *forward = new NnetForward(nnet,0,false,false); 26 | int left = 0, right =0 ; 27 | 28 | forward->GetLRoffset(left,right); 29 | DnnFeat *dnnfeat = new DnnFeat; 30 | dnnfeat->Init(argv[1],"./"); 31 | dnnfeat->InitParameters(left,right); 32 | #define LEN (1024) 33 | /* 34 | char data[LEN]; 35 | FILE *fp = fopen(argv[3],"rb"); 36 | if(NULL == fp) 37 | { 38 | fprintf(stderr,"fopen %s error.\n",argv[3]); 39 | return -1; 40 | } 41 | fread(data,sizeof(char),44,fp); 42 | int len = LEN; 43 | */ int frame = 0; 44 | int nnet_in_frame = 0; 45 | int dim = 0; 46 | float *feats = NULL; 47 | gettimeofday(&start,NULL); 48 | while(1) 49 | { 50 | dnnfeat->GetFeats(argv[3],&feats,&nnet_in_frame,&dim); 51 | all_nnettime += nnet_in_frame; 52 | // use features. 53 | 54 | forward->ResetRnnBuffer(); 55 | int send_frame = 1; 56 | frame=0; 57 | for(int i=0;iFeedForward(feats,send_frame,dim); 60 | #define DEBUG 61 | #ifdef DEBUG 62 | int outdim = forward->GetOutdim(); 63 | int k=0; 64 | for(k = frame;kExamineFrame(frame)) 67 | { 68 | break; 69 | fprintf(stderr,"it's nnet forward error.\n"); 70 | return -1; 71 | } 72 | printf("%d@ ",frame); 73 | for(int j=0;jLogLikelihood(frame,j+1)); 75 | printf("\n"); 76 | ++frame; 77 | } 78 | frame=k; 79 | #endif 80 | dnnfeat->RemoveFeats(send_frame); 81 | } 82 | break; 83 | } 84 | gettimeofday(&end,NULL); 85 | float usetime = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1000000.0; 86 | printf("nnet rt is %f\n",usetime*100/all_nnettime); 87 | // fclose(fp); 88 | delete dnnfeat; 89 | delete forward; 90 | delete nnet; 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /src/old-decoder/conf/conf.txt: -------------------------------------------------------------------------------- 1 | #decode config 2 | --beam=13 3 | --max-active=5000 4 | --min-active=200 5 | --beam-delta=0.5 6 | --hash-ratio=2.0 7 | --prune-interval=25 8 | --lattice-beam=7.0 9 | 10 | --hmmfile=/search/speech/hubo/git/ASR-decoder/nnet/tf-model/graph/final.mdl.txt 11 | #--phonedict=/home/hubo/git/PachiraCTC/src/graph/data/hclg/phones.txt 12 | #--prondict=/home/hubo/git/PachiraCTC/src/graph/lang/lexicon.txt 13 | #--wordlist=/search/speech/hubo/git/ASR-decoder/nnet/tf-model/graph/words.txt 14 | --wordlist=/search/speech/hubo/git/ASR-decoder/nnet/tf-model/source/words.txt 15 | 16 | #nnet options 17 | --skip=2 18 | --skip-block=1 19 | #--sub-prior=true 20 | --do-log=true 21 | #--acoustic-scale=0.3 22 | #--block-scale=1.0 23 | 24 | --block-pdf-pdfid=4222 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/old-decoder/decoder-graph.h: -------------------------------------------------------------------------------- 1 | #ifndef __DECODE_GRAPH_H__ 2 | #define __DECODE_GRAPH_H__ 3 | 4 | 5 | #include "src/decoder/graph.h" 6 | 7 | #include "src/util/namespace-start.h" 8 | typedef Arc StdArc; 9 | typedef Node StdNode; 10 | typedef int StateId; 11 | 12 | class DecodeNet 13 | { 14 | public: 15 | DecodeNet(){}; 16 | ~DecodeNet(){}; 17 | const StdNode *GetNode(unsigned stateid) 18 | { 19 | if(stateid < _state.size()) 20 | return &_state[stateid]; 21 | else 22 | return NULL; 23 | } 24 | unsigned int GetNetSize() { return _state.size();} 25 | int Start() { return _start_state;} 26 | private: 27 | vector _state; 28 | int _start_state; // default 0 29 | int _tot_state; 30 | int _tot_arc; 31 | int _tot_in_eps; 32 | int _tot_out_eps; 33 | }; 34 | #include "src/util/namespace-end.h" 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /src/old-decoder/log.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOG_H__ 2 | #define __LOG_H__ 3 | 4 | #include 5 | 6 | #define __func__ __FUNCTION__ 7 | 8 | #include "src/util/namespace-start.h" 9 | #define LOGERR(format,args...) \ 10 | fprintf(stderr,"file:%s line:%d func:%s:" format,__FILE__,__LINE__,__func__,##args) 11 | 12 | #ifdef DEBUG 13 | #define LOGDEBUG(format,args...) \ 14 | fprintf(stderr,"file:%s line:%d func:%s:" format,__FILE__,__LINE__,__func__,##args) 15 | #else 16 | #define LOGDEBUG(format,args...) 17 | #endif 18 | #include "src/util/namespace-end.h" 19 | #endif 20 | -------------------------------------------------------------------------------- /src/old-decoder/makefile: -------------------------------------------------------------------------------- 1 | 2 | 3 | include ../hubo.mk 4 | 5 | CXXFLAGS+= -I. #-DPHONEQUEUE #-DDEBUGPRINT #-DTOKEN_ACTIVE_PRINT 6 | 7 | 8 | TESTBIN=test-optimize-fst optimize-faster-decoder-kaldifeature-test \ 9 | optimize-faster-decoder-test \ 10 | optimize-ctc-faster-decoder-kaldifeature-test \ 11 | optimize-ctc-lattice-faster-decoder-kaldifeature-test \ 12 | optimize-faster-decoder-kaldifeat-test \ 13 | optimize-clg-lattice-online-faster-decoder-kaldifeature-test 14 | 15 | LIBS=../hmm/libhmm.a ../nnet/libnnet.a ../decoder/libdecode.a \ 16 | $(CBLASLIB) ../align/libalign.a ../util/libutil.a ../fst/liblatfst.a $(FEATLIB) 17 | 18 | all:libdecode.a 19 | 20 | test:$(TESTBIN) 21 | 22 | test-optimize-fst:test-optimize-fst.o 23 | $(CXX) $(CXXFLAGS) -o $@ $^ 24 | 25 | libdecode.a:optimize-faster-decoder.o optimize-ctc-faster-decoder.o lattice-faster-decoder.o clg-lattice-faster-online-decoder.o 26 | $(AR) -rcu $@ $^ 27 | 28 | optimize-faster-decoder-test:optimize-faster-decoder-test.o 29 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 30 | 31 | optimize-faster-decoder-kaldifeat-test:optimize-faster-decoder-kaldifeat-test.o 32 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 33 | 34 | optimize-ctc-faster-decoder-kaldifeature-test:optimize-ctc-faster-decoder-kaldifeature-test.o 35 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 36 | 37 | optimize-faster-decoder-kaldifeature-test:optimize-faster-decoder-kaldifeature-test.o 38 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 39 | 40 | optimize-ctc-lattice-faster-decoder-kaldifeature-test:optimize-ctc-lattice-faster-decoder-kaldifeature-test.o 41 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 42 | 43 | optimize-clg-lattice-online-faster-decoder-kaldifeature-test:optimize-clg-lattice-online-faster-decoder-kaldifeature-test.o 44 | $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS) 45 | 46 | %.o:%.c 47 | $(CXX) $(CXXFLAGS) -c -o $@ $^ 48 | 49 | %.o:%.cpp 50 | $(CXX) $(CXXFLAGS) -c -o $@ $^ 51 | 52 | %.o:%.cc 53 | $(CXX) $(CXXFLAGS) -c -o $@ $^ 54 | .PHONY: 55 | 56 | clean: 57 | rm -f *.o $(TESTBIN) libdecode.a 58 | -------------------------------------------------------------------------------- /src/old-decoder/optimize-fst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/decoder/optimize-fst.h" 4 | #include "src/decoder/log.h" 5 | 6 | #include "src/util/namespace-start.h" 7 | 8 | bool Fst::ReadFst(const char *file) 9 | { 10 | FILE *fp = fopen(file,"rb"); 11 | if(NULL == fp) 12 | { 13 | LOGERR("fopen %s failed.\n",file); 14 | return false; 15 | } 16 | ReadFst(fp); 17 | fclose(fp); 18 | return true; 19 | } 20 | bool Fst::ReadFst(FILE *fp) 21 | { 22 | if(1 != fread(&(_start_stateid),sizeof(int),1,fp)) 23 | return false; 24 | if(1 != fread(&(_final_stateid),sizeof(int),1,fp)) 25 | return false; 26 | if(1 != fread(&(_total_states),sizeof(int),1,fp)) 27 | return false; 28 | if(1 != fread(&(_total_arcs),sizeof(int),1,fp)) 29 | return false; 30 | if(1 != fread(&(_total_niepsilons),sizeof(int),1,fp)) 31 | return false; 32 | if(1 != fread(&(_total_noepsilons),sizeof(int),1,fp)) 33 | return false; 34 | 35 | // malloc memery 36 | _state_arr = new State[_total_states]; 37 | _arc_arr = new Arc[_total_arcs]; 38 | int i = 0; 39 | int arc_offset = 0; 40 | for(i = 0 ; i < _total_states ; ++i) 41 | { 42 | unsigned int num_arc = 0; 43 | if(1 != fread(&num_arc,sizeof(int),1,fp)) 44 | return false; 45 | _state_arr[i]._num_arcs = num_arc; 46 | _state_arr[i]._arcs = &_arc_arr[arc_offset]; 47 | if(num_arc > 0) 48 | { 49 | if(num_arc != fread(_state_arr[i]._arcs,sizeof(Arc),num_arc,fp)) 50 | { 51 | LOGERR("fread arc error!\n"); 52 | return false; 53 | } 54 | } 55 | arc_offset += num_arc; 56 | } 57 | assert(arc_offset == _total_arcs && "arc read is error.\n"); 58 | return true; 59 | } 60 | 61 | void Fst::PrintFst() 62 | { 63 | for(int i =0 ; i < _total_states ; ++i) 64 | { 65 | Arc* arc = _state_arr[i]._arcs; 66 | for(int j = 0; j < _state_arr[i]._num_arcs ; ++j) 67 | { 68 | printf("%d\t%d\t%d\t%d\t%f\n", i, arc[j]._to, arc[j]._input, arc[j]._output, arc[j]._w); 69 | } 70 | } 71 | } 72 | 73 | void Fst::RmOlalel() 74 | { 75 | for(int i =0 ; i < _total_states ; ++i) 76 | { 77 | Arc* arc = _state_arr[i]._arcs; 78 | for(int j = 0; j < _state_arr[i]._num_arcs ; ++j) 79 | { 80 | arc[j]._output=0; 81 | } 82 | } 83 | } 84 | #include "src/util/namespace-end.h" 85 | -------------------------------------------------------------------------------- /src/old-decoder/test-optimize-fst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/decoder/optimize-fst.h" 3 | 4 | #ifdef NAMESPACE 5 | using namespace datemoon; 6 | #endif 7 | 8 | int main(int argc,char *argv[]) 9 | { 10 | if(argc != 2) 11 | { 12 | fprintf(stderr,"%s fst\n",argv[0]); 13 | return -1; 14 | } 15 | Fst fst; 16 | if(true != fst.ReadFst(argv[1])) 17 | { 18 | fprintf(stderr,"ReadFst %s error.\n",argv[1]); 19 | return -1; 20 | } 21 | fst.PrintFst(); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /src/old-decoder/wordid-to-wordstr.h: -------------------------------------------------------------------------------- 1 | #ifndef __WORDID_TO_WORDSTR_H__ 2 | #define __WORDID_TO_WORDSTR_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "src/util/namespace-start.h" 9 | typedef int WordIdType; 10 | 11 | class WordSymbol 12 | { 13 | private: 14 | WordIdType *_words; // word offset 15 | char *_mem; 16 | WordIdType _word_maxid; 17 | int _word_range; 18 | size_t _mem_offset; 19 | size_t _mem_len; 20 | size_t _mem_range; 21 | public: 22 | WordSymbol() 23 | { 24 | _word_maxid = 1e4 * 5; 25 | _words = new WordIdType[_word_maxid]; 26 | _mem_len = 1e4 * 5 * 8;//default word list is 5w 27 | _mem = new char[_mem_len]; 28 | _mem_offset = 0; 29 | _word_range = 1000; 30 | _mem_range = 1000; 31 | } 32 | 33 | ~WordSymbol() 34 | { 35 | if(NULL != _words) 36 | delete []_words; 37 | if(NULL != _mem) 38 | delete []_mem; 39 | } 40 | int AddWord(char *word,WordIdType wordid) 41 | { 42 | if(wordid >= _word_maxid) 43 | { 44 | // realloc words 45 | WordIdType needlen = wordid + _word_range; 46 | WordIdType *tmp = new WordIdType[needlen]; 47 | memcpy(tmp, _words,sizeof(WordIdType)*_word_maxid); 48 | delete []_words; 49 | _words = tmp; 50 | _word_maxid = needlen; 51 | if(_word_range < 8000) 52 | _word_range *= 2; 53 | } 54 | int wordlen = strlen(word); 55 | if(wordlen + 1 + _mem_offset > _mem_len) 56 | { 57 | // realloc memery 58 | size_t needlen = wordlen + 1 + _mem_offset + _mem_range; 59 | char *tmp = new char[needlen]; 60 | memcpy(tmp, _mem, sizeof(char) * _mem_len); 61 | delete []_mem; 62 | _mem = tmp; 63 | _mem_len = needlen; 64 | if(_mem_range < 8000 * 5) 65 | _mem_range *= 2; 66 | } 67 | // add word to _mem 68 | 69 | _words[wordid] = _mem_offset; 70 | memcpy(_mem+_mem_offset, word, wordlen); 71 | _mem[_mem_offset+wordlen] = '\0'; 72 | _mem_offset += wordlen+1; 73 | return wordlen; 74 | } 75 | 76 | int ReadText(const char *wordfile) 77 | { 78 | FILE *fp = fopen(wordfile, "r"); 79 | if(NULL == fp) 80 | { 81 | fprintf(stderr,"fopen %s failed.\n",wordfile); 82 | return -1; 83 | } 84 | 85 | char line[256]; 86 | memset(line,0x00,sizeof(line)); 87 | int wordid = 0; 88 | char word[256]; 89 | memset(word,0x00,sizeof(word)); 90 | while(NULL != fgets(line,sizeof(line),fp)) 91 | { 92 | line[strlen(line)-1] = '\0'; 93 | if(2 != sscanf(line,"%s %d",word,&wordid)) 94 | { 95 | fprintf(stderr," read line \"%s\" error\n",line); 96 | return -1; 97 | } 98 | int ret = AddWord(word, wordid); 99 | assert(ret != 0); 100 | } 101 | fclose(fp); 102 | return 0; 103 | } 104 | 105 | char *FindWordStr(int wordid) 106 | { 107 | if(wordid > _word_maxid) 108 | { 109 | fprintf(stderr,"it's error id %d, no word.\n",wordid); 110 | return NULL; 111 | } 112 | size_t offset = _words[wordid]; 113 | return _mem + offset; 114 | } 115 | }; 116 | 117 | #include "src/util/namespace-end.h" 118 | #endif 119 | -------------------------------------------------------------------------------- /src/online-vad/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 8 | $(KALDI_DEPLDFLAGS) \ 9 | $(MATRIX_DEPLDFLAGS) 10 | 11 | lib_LTLIBRARIES = 12 | 13 | check_PROGRAMS = online-nnet3-vad-test \ 14 | online-nnet3-vad-stream-test \ 15 | online-energy-vad-stream-test \ 16 | new-online-energy-vad-stream-test 17 | 18 | 19 | online_nnet3_vad_test_SOURCES = online-nnet3-vad-test.cc 20 | online_nnet3_vad_test_LDADD = $(KALDI_LIBS) \ 21 | $(MATRIX_LIBS) $(CUDA_LIBS) \ 22 | $(DL_LIBS) 23 | 24 | online_nnet3_vad_stream_test_SOURCES = online-nnet3-vad-stream-test.cc 25 | online_nnet3_vad_stream_test_LDADD = $(KALDI_LIBS) \ 26 | $(MATRIX_LIBS) $(CUDA_LIBS) \ 27 | $(DL_LIBS) 28 | 29 | online_energy_vad_stream_test_SOURCES = online-energy-vad-stream-test.cc 30 | online_energy_vad_stream_test_LDADD = $(KALDI_LIBS) \ 31 | $(MATRIX_LIBS) $(CUDA_LIBS) \ 32 | $(DL_LIBS) 33 | 34 | new_online_energy_vad_stream_test_SOURCES = new-online-energy-vad-stream-test.cc 35 | new_online_energy_vad_stream_test_LDADD = $(KALDI_LIBS) \ 36 | $(MATRIX_LIBS) $(CUDA_LIBS) \ 37 | $(DL_LIBS) 38 | 39 | -------------------------------------------------------------------------------- /src/online-vad/new-online-energy-vad-stream-test.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "feat/wave-reader.h" 3 | #include "util/common-utils.h" 4 | #include "src/online-vad/online-vad.h" 5 | 6 | int main(int argc, char* argv[]) 7 | { 8 | try 9 | { 10 | using namespace kaldi; 11 | const char *usage = "Test online energy vad code.\n" 12 | "online-energy-vad-stream-test scp:wav.scp"; 13 | ParseOptions po(usage); 14 | 15 | VadProcessOpts vad_process_opt("energy-vad-judge", "energy-vad"); 16 | //VadJudgeOptions vad_judge_opt("energy-vad-judge"); 17 | vad_process_opt.Register(&po); 18 | 19 | po.Read(argc, argv); 20 | 21 | if(po.NumArgs() != 1) 22 | { 23 | po.PrintUsage(); 24 | exit(1); 25 | } 26 | std::string wav_rspecifier = po.GetArg(1); 27 | EnergyVadProcess energy_vad(vad_process_opt); 28 | 29 | //V1EnergyVad v1_energv_vad(vad_judge_opt, 30 | // "sum_square_root", 31 | // 16000, 0.025, 0.01, 0, 32768*0.005, 32768*0.05); 32 | 33 | SequentialTableReader reader(wav_rspecifier); 34 | 35 | BaseFloat min_duration = 0.0; 36 | int32 num_utts = 0;// num_success = 0; 37 | for(; !reader.Done(); reader.Next()) 38 | { 39 | energy_vad.Reset(); 40 | num_utts++; 41 | std::string utt = reader.Key(); 42 | const WaveData &wave_data = reader.Value(); 43 | if (wave_data.Duration() < min_duration) 44 | { 45 | KALDI_WARN << "File: " << utt << " is too short (" 46 | << wave_data.Duration() << " sec): producing no output."; 47 | continue; 48 | } 49 | int32 this_chan = 0; 50 | 51 | SubVector waveform(wave_data.Data(), this_chan); 52 | int32 wav_len = waveform.Dim(); 53 | int32 send_len = 8000; 54 | VadSeg tot_ali; 55 | for(int32 offset=0; offset < wav_len ; ) 56 | { 57 | if(offset + send_len > wav_len) 58 | send_len = wav_len - offset; 59 | SubVector cur_wav(waveform, offset, send_len); 60 | offset += send_len; 61 | 62 | energy_vad.PushData(cur_wav); 63 | bool eos = false; 64 | if(offset == wav_len) 65 | { 66 | eos = true; 67 | } 68 | std::vector > vad_data; 69 | bool cur_end = energy_vad.GetData(vad_data, eos); 70 | } 71 | int sil_frames=0, nosil_frames=0; 72 | energy_vad.GetInfo(sil_frames, nosil_frames); 73 | } 74 | return 0; 75 | } 76 | catch(const std::exception &e) 77 | { 78 | std::cerr << e.what(); 79 | return -1; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/online-vad/online-energy-vad-stream-test.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "feat/wave-reader.h" 3 | #include "util/common-utils.h" 4 | #include "src/online-vad/online-vad.h" 5 | 6 | int main(int argc, char* argv[]) 7 | { 8 | try 9 | { 10 | using namespace kaldi; 11 | const char *usage = "Test online energy vad code.\n" 12 | "online-energy-vad-stream-test scp:wav.scp"; 13 | ParseOptions po(usage); 14 | 15 | VadJudgeOptions vad_judge_opt("energy-vad-judge"); 16 | vad_judge_opt.Register(&po); 17 | 18 | po.Read(argc, argv); 19 | 20 | if(po.NumArgs() != 1) 21 | { 22 | po.PrintUsage(); 23 | exit(1); 24 | } 25 | std::string wav_rspecifier = po.GetArg(1); 26 | 27 | V1EnergyVad v1_energv_vad(vad_judge_opt, 28 | "sum_square_root", 29 | 16000, 0.025, 0.01, 0, 32768*0.005, 32768*0.05); 30 | 31 | SequentialTableReader reader(wav_rspecifier); 32 | 33 | BaseFloat min_duration = 0.0; 34 | int32 num_utts = 0;// num_success = 0; 35 | for(; !reader.Done(); reader.Next()) 36 | { 37 | v1_energv_vad.Reset(); 38 | num_utts++; 39 | std::string utt = reader.Key(); 40 | const WaveData &wave_data = reader.Value(); 41 | if (wave_data.Duration() < min_duration) 42 | { 43 | KALDI_WARN << "File: " << utt << " is too short (" 44 | << wave_data.Duration() << " sec): producing no output."; 45 | continue; 46 | } 47 | int32 this_chan = 0; 48 | 49 | SubVector waveform(wave_data.Data(), this_chan); 50 | int32 wav_len = waveform.Dim(); 51 | int32 send_len = 8000; 52 | VadSeg tot_ali; 53 | for(int32 offset=0; offset < wav_len ; ) 54 | { 55 | if(offset + send_len > wav_len) 56 | send_len = wav_len - offset; 57 | SubVector cur_wav(waveform, offset, send_len); 58 | offset += send_len; 59 | 60 | v1_energv_vad.AcceptData(cur_wav); 61 | if(offset == wav_len) 62 | { 63 | VadSeg cur_ali = v1_energv_vad.ComputerVad(1); 64 | tot_ali.insert(tot_ali.end(), 65 | cur_ali.begin(), cur_ali.end()); 66 | } 67 | else 68 | { 69 | VadSeg cur_ali = v1_energv_vad.ComputerVad(); 70 | tot_ali.insert(tot_ali.end(), 71 | cur_ali.begin(), cur_ali.end()); 72 | } 73 | } 74 | PrintVadSeg(tot_ali, "tot-> ", 0); 75 | VadSeg compress_vad_ali; 76 | CompressAlignVad(tot_ali, compress_vad_ali, 50); 77 | PrintVadSeg(compress_vad_ali, "compress-vad-ali-> ", 0); 78 | } 79 | return 0; 80 | } 81 | catch(const std::exception &e) 82 | { 83 | std::cerr << e.what(); 84 | return -1; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/online-vad/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | set -x 5 | if [ $# != 1 ] 6 | then 7 | echo $0 scpfile 8 | exit 1; 9 | fi 10 | 11 | ./new-online-energy-vad-stream-test --use-realtime-vad=false \ 12 | --print-vad-ali-info=true \ 13 | --max-nosil-frames=-1 \ 14 | --verbose=3 \ 15 | scp:$1 16 | 17 | feat_conf=fbank.80.conf 18 | model=nnet.model 19 | scp=scp 20 | 21 | ./online-nnet3-vad-stream-test --vad-nnet.acoustic-scale=1.0 --apply-exp=true --use-priors=false \ 22 | --verbose=3 --fbank-config=$feat_conf --feature-type=fbank \ 23 | $model scp:$scp 24 | -------------------------------------------------------------------------------- /src/pitch/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | lib_LTLIBRARIES = libpitch.la 3 | 4 | libpitch_la_SOURCES = kaldi-vector.cc \ 5 | pitch-functions.cc \ 6 | online-feature.cc \ 7 | resample.cc \ 8 | kaldi-matrix.cc \ 9 | kaldi-common.cc 10 | 11 | include ../depend.mk 12 | LDADD = libpitch.la ../util/libutil.la $(MATRIX_LIBS) 13 | check_PROGRAMS = compute-and-process-kaldi-pitch-feats \ 14 | stream-compute-and-process-kaldi-pitch-feats \ 15 | hb-stream-compute-and-process-kaldi-pitch-feats 16 | 17 | compute_and_process_kaldi_pitch_feats_SOURCES = compute-and-process-kaldi-pitch-feats.cc 18 | 19 | stream_compute_and_process_kaldi_pitch_feats_SOURCES = stream-compute-and-process-kaldi-pitch-feats.cc 20 | 21 | hb_stream_compute_and_process_kaldi_pitch_feats_SOURCES = hb-stream-compute-and-process-kaldi-pitch-feats.cc 22 | 23 | -------------------------------------------------------------------------------- /src/pitch/compute-and-process-kaldi-pitch-feats.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "src/util/config-parse-options.h" 6 | #include "src/pitch/pitch-functions.h" 7 | #include "src/pitch/kaldi-type.h" 8 | #include "src/pitch/kaldi-matrix.h" 9 | 10 | using namespace std; 11 | 12 | int main(int argc,char *argv[]) 13 | { 14 | const char *usage = "This is a pitch test code\n" 15 | "compute-and-process-kaldi-pitch-feats --simulate-first-pass-online=true\\\n" 16 | "--frames-per-chunk=10 --sample-frequency=8000 wav outputfeat\n"; 17 | ConfigParseOptions conf(usage); 18 | 19 | PitchExtractionOptions pitch_opts; 20 | ProcessPitchOptions process_opts; 21 | 22 | pitch_opts.Register(&conf); 23 | process_opts.Register(&conf); 24 | 25 | conf.Read(argc, argv); 26 | 27 | if(conf.NumArgs() != 3) 28 | { 29 | conf.PrintUsage(); 30 | exit(-1); 31 | } 32 | 33 | std::string wav = conf.GetArg(1), 34 | feat = conf.GetArg(2); 35 | 36 | FILE *wavfp = fopen(wav.c_str(),"rb"); 37 | if(NULL == wavfp) 38 | { 39 | std::cerr << "fopen " << wav << "failed" << std::endl; 40 | return -1; 41 | } 42 | #define LENGTH 102400 43 | char wav_data[LENGTH]; 44 | int len=0; 45 | fread(wav_data,sizeof(char),44,wavfp); 46 | while((len = fread(wav_data,sizeof(char),LENGTH,wavfp))) 47 | { 48 | if(len == 0) 49 | break; 50 | Vector wavform; 51 | int wav_len = len/sizeof(short); 52 | wavform.Resize(wav_len, kUndefined); 53 | for(int i =0 ; i < wav_len ; ++i) 54 | wavform(i) = ((short*)wav_data)[i]; 55 | //memcpy(wavform.Data(), wav_data, len); 56 | Matrix features; 57 | try 58 | { 59 | ComputeAndProcessKaldiPitch(pitch_opts, process_opts, 60 | wavform, &features); 61 | } 62 | catch (...) 63 | { 64 | std::cerr << "Failed to compute pitch for utterance " << wav << std::endl; 65 | exit(-1); 66 | } 67 | features.Print(); 68 | } 69 | fclose(wavfp); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /src/pitch/hb-stream-compute-and-process-kaldi-pitch-feats.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "src/util/config-parse-options.h" 7 | #include "src/pitch/pitch-functions.h" 8 | #include "src/pitch/kaldi-type.h" 9 | #include "src/pitch/kaldi-matrix.h" 10 | 11 | using namespace std; 12 | 13 | int main(int argc,char *argv[]) 14 | { 15 | const char *usage = "This is a pitch test code\n" 16 | "compute-and-process-kaldi-pitch-feats --simulate-first-pass-online=true\\\n" 17 | "--frames-per-chunk=10 --sample-frequency=8000 wav outputfeat\n"; 18 | ConfigParseOptions conf(usage); 19 | 20 | PitchExtractionOptions pitch_opts; 21 | ProcessPitchOptions process_opts; 22 | 23 | pitch_opts.Register(&conf); 24 | process_opts.Register(&conf); 25 | 26 | conf.Read(argc, argv); 27 | 28 | if(conf.NumArgs() != 3) 29 | { 30 | conf.PrintUsage(); 31 | exit(-1); 32 | } 33 | 34 | std::string wav = conf.GetArg(1), 35 | feat = conf.GetArg(2); 36 | 37 | FILE *wavfp = fopen(wav.c_str(),"rb"); 38 | if(NULL == wavfp) 39 | { 40 | std::cerr << "fopen " << wav << "failed" << std::endl; 41 | return -1; 42 | } 43 | // first 44 | // now init online pitch 45 | StreamPitch stream_pitch(pitch_opts, process_opts); 46 | #define LENGTH (160*5) 47 | char wav_data[LENGTH]; 48 | int len=0; 49 | fread(wav_data,sizeof(char),44,wavfp); 50 | bool is_end = false; 51 | Matrix features; 52 | Vector wavform; 53 | int wav_len = 0; 54 | while((len = fread(wav_data,sizeof(char),LENGTH,wavfp))) 55 | { 56 | wav_len = len/sizeof(short); 57 | wavform.Resize(wav_len, kUndefined); 58 | for(int i =0 ; i < wav_len ; ++i) 59 | wavform(i) = ((short*)wav_data)[i]; 60 | if(len != LENGTH) 61 | { 62 | is_end = true; 63 | break; 64 | } 65 | try 66 | { 67 | #ifdef API 68 | stream_pitch.ProcessWave(pitch_opts,wavform,features, is_end); 69 | features.Print(); 70 | #else 71 | float *pitch_feat = NULL; 72 | int pitch_dim = 0; 73 | int pitch_rows = 0; 74 | stream_pitch.ProcessWave(pitch_opts,(short*)wav_data,len/sizeof(short), &pitch_feat, pitch_dim, pitch_rows, is_end); 75 | for(int r =0 ; r < pitch_rows; ++r) 76 | { 77 | for(int c = 0 ; c 3 | #include 4 | #include 5 | #include 6 | #include "src/pitch/kaldi-common.h" 7 | 8 | 9 | static pthread_mutex_t _RandMutex = PTHREAD_MUTEX_INITIALIZER; 10 | 11 | RandomState::RandomState() { 12 | seed = Rand() + 27437; 13 | } 14 | 15 | int Rand(struct RandomState* state){ 16 | if (state) { 17 | #ifdef __ANDROID_API__ 18 | return rand(); 19 | #else 20 | return rand_r(&(state->seed)); 21 | #endif 22 | } else { 23 | int rs = pthread_mutex_lock(&_RandMutex); 24 | assert(rs == 0); 25 | int val = rand(); 26 | // std::cout << val << std::endl; 27 | rs = pthread_mutex_unlock(&_RandMutex); 28 | assert(rs == 0); 29 | return val; 30 | } 31 | } 32 | double Exp(double x) { return exp(x);} 33 | 34 | float Exp(float x) { return expf(x);} 35 | 36 | double Log(double x) { return log(x);} 37 | 38 | float Log(float x) { return logf(x);} 39 | 40 | -------------------------------------------------------------------------------- /src/pitch/kaldi-common.h: -------------------------------------------------------------------------------- 1 | #ifndef __KALDI_COMMON_H__ 2 | #define __KALDI_COMMON_H__ 1 3 | 4 | #include 5 | #include 6 | #include 7 | extern "C" 8 | { 9 | #include "src/lib/cblas.h" 10 | } 11 | template class VectorBase; 12 | template class Vector; 13 | template class SubVector; 14 | 15 | template class MatrixBase; 16 | template class SubMatrix; 17 | template class Matrix; 18 | 19 | typedef int MatrixIndexT; 20 | typedef unsigned int UnsignedMatrixIndexT; 21 | 22 | typedef enum { 23 | kSetZero, 24 | kUndefined, 25 | kCopyData 26 | } MatrixResizeType; 27 | 28 | 29 | typedef enum { 30 | kTrans = CblasTrans, 31 | kNoTrans = CblasNoTrans 32 | } MatrixTransposeType; 33 | 34 | typedef enum { 35 | kDefaultStride, 36 | kStrideEqualNumCols, 37 | } MatrixStrideType; 38 | 39 | double Exp(double x); 40 | 41 | float Exp(float x); 42 | 43 | double Log(double x); 44 | 45 | float Log(float x); 46 | 47 | #ifdef __ANDROID_API__ 48 | #define KALDI_MEMALIGN(align, size, pp_orig) \ 49 | malloc(size) 50 | #else 51 | #define KALDI_MEMALIGN(align, size, pp_orig) \ 52 | (!posix_memalign(pp_orig, align, size) ? *(pp_orig) : NULL) 53 | #endif 54 | #define KALDI_MEMALIGN_FREE(x) free(x) 55 | 56 | /// return abs(a - b) <= relative_tolerance * (abs(a)+abs(b)). 57 | static inline bool ApproxEqual(float a, float b, 58 | float relative_tolerance = 0.001) { 59 | // a==b handles infinities. 60 | if (a == b) return true; 61 | float diff = std::abs(a-b); 62 | if (diff == std::numeric_limits::infinity() 63 | || diff != diff) return false; // diff is +inf or nan. 64 | return (diff <= relative_tolerance*(std::abs(a)+std::abs(b))); 65 | } 66 | 67 | // State for thread-safe random number generator 68 | struct RandomState { 69 | RandomState(); 70 | unsigned seed; 71 | }; 72 | 73 | int Rand(struct RandomState* state = NULL); 74 | 75 | inline float RandUniform(struct RandomState* state = NULL) { 76 | return static_cast((Rand(state) + 1.0) / (RAND_MAX+2.0)); 77 | } 78 | 79 | #ifndef M_PI 80 | #define M_PI 3.1415926535897932384626433832795 81 | #endif 82 | 83 | #ifndef M_2PI 84 | #define M_2PI 6.283185307179586476925286766559005 85 | #endif 86 | inline float RandGauss(struct RandomState* state = NULL) { 87 | return static_cast(sqrtf (-2 * Log(RandUniform(state))) 88 | * cosf(2*M_PI*RandUniform(state))); 89 | } 90 | 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /src/pitch/kaldi-type.h: -------------------------------------------------------------------------------- 1 | #ifndef __KALDI_TYPE_H__ 2 | #define __KALDI_TYPE_H__ 3 | 4 | typedef int int32; 5 | typedef long long int int64; 6 | typedef float BaseFloat; 7 | 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /src/post-processing-service/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | 3 | KALDI_THIS_LIBS=$(KALDI_ROOT)/lm/kaldi-lm.a \ 4 | $(KALDI_ROOT)/lat/kaldi-lat.a \ 5 | $(KALDI_ROOT)/fstext/kaldi-fstext.a \ 6 | $(KALDI_ROOT)/hmm/kaldi-hmm.a \ 7 | $(KALDI_ROOT)/tree/kaldi-tree.a \ 8 | $(KALDI_ROOT)/util/kaldi-util.a \ 9 | $(KALDI_ROOT)/matrix/kaldi-matrix.a \ 10 | $(KALDI_ROOT)/base/kaldi-base.a \ 11 | $(FST_LIBS) \ 12 | $(MATRIX_LIBS) 13 | 14 | AM_CPPFLAGS = -I$(KALDI_INC) \ 15 | -I$(OPENFST_INC) \ 16 | -I$(MATRIX_INC) \ 17 | -DHAVE_CUDA 18 | 19 | #-DDEBUG 20 | 21 | AM_LDFLAGS = $(KALDI_DEPLDFLAGS) \ 22 | $(MATRIX_DEPLDFLAGS) 23 | 24 | 25 | LDADD = ../service2/libservice2.la \ 26 | ../util/libutil.la 27 | 28 | LDADD += $(KALDI_THIS_LIBS) \ 29 | -lm -lpthread $(DL_LIBS) 30 | 31 | bin_PROGRAMS = asr-post-process-service 32 | 33 | asr_post_process_service_SOURCES = asr-post-process-service.cc 34 | 35 | noinst_PROGRAMS = client-asr-post-process-service 36 | 37 | client_asr_post_process_service_SOURCES = client-asr-post-process-service.cc 38 | 39 | check_PROGRAMS = post-package-test 40 | 41 | post_package_test_SOURCES = post-package-test.cc 42 | post_package_test_LDADD = ../service2/libservice2.la \ 43 | ../util/libutil.la \ 44 | $(KALDI_THIS_LIBS) \ 45 | -lm -lpthread $(DL_LIBS) 46 | 47 | 48 | -------------------------------------------------------------------------------- /src/post-processing-service/asr-post-process-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASR_POST_PROCESS_WORK_THREAD_H__ 2 | #define __ASR_POST_PROCESS_WORK_THREAD_H__ 3 | 4 | #include "src/service2/net-data-package.h" 5 | #include "src/service2/thread-pool-work-thread.h" 6 | 7 | #include "src/post-processing-service/post-package.h" 8 | #include "src/post-processing-service/const-lm-rescore.h" 9 | #include "src/post-processing-service/asr-post-process-task.h" 10 | #include "src/post-processing-service/lattice-to-result.h" 11 | 12 | #include "src/util/namespace-start.h" 13 | 14 | struct AsrPostProcessWorkThreadOpt 15 | { 16 | bool _null; // no use 17 | void Register(kaldi::OptionsItf *opts) 18 | { ; } 19 | }; 20 | 21 | class AsrPostProcessWorkThread: public ThreadPoolWorkThread 22 | { 23 | public: 24 | friend class AsrPostProcessServiceTask; 25 | typedef ThreadBase::int32 int32; 26 | 27 | AsrPostProcessWorkThread(ThreadPoolBase *tp, 28 | const AsrPostProcessWorkThreadOpt &opts, 29 | const kaldi::ConstArpaLm& oldlm, 30 | const kaldi::ConstArpaLm& newlm, 31 | const fst::SymbolTable &word_syms, 32 | BaseFloat oldscale=-1.0, BaseFloat newscale=1.0, 33 | bool determinize=false): 34 | ThreadPoolWorkThread(tp), 35 | _opts(opts), 36 | _old_kaldi_const_arpa_lmrescore(oldlm, oldscale, determinize), 37 | _new_kaldi_const_arpa_lmrescore(newlm, newscale, determinize), 38 | _word_syms(word_syms) { } 39 | 40 | ~AsrPostProcessWorkThread() {} 41 | 42 | private: 43 | 44 | // composed_lat 会在内部申请内存,所以需要*&,在外部释放 45 | template 46 | bool LmRescore(fst::VectorFst &lat, 47 | fst::VectorFst *composed_lat) 48 | { 49 | fst::VectorFst compose_old_lat; 50 | if(_old_kaldi_const_arpa_lmrescore.Compose(lat, &compose_old_lat) 51 | != true) 52 | { 53 | LOG_WARN << "_old_kaldi_const_arpa_lmrescore.Compose failed!!!"; 54 | return false; 55 | } 56 | if(_new_kaldi_const_arpa_lmrescore.Compose(compose_old_lat, composed_lat) != true) 57 | { 58 | LOG_WARN << "_new_kaldi_const_arpa_lmrescore.Compose failed!!!"; 59 | return false; 60 | } 61 | return true; 62 | } 63 | template 64 | void ConvertLatticeToResult(const fst::Fst &fst, 65 | size_t n, std::vector *nbest_str) 66 | { 67 | n = n > 0 ? n : 1; 68 | std::vector > alignments; 69 | std::vector > words; 70 | std::vector weights; 71 | kaldi::FstToNbestVector(fst, n, &alignments, &words, &weights); 72 | 73 | for(size_t i = 0 ; i < words.size(); ++i) 74 | { 75 | nbest_str->push_back( 76 | kaldi::ConvertWordId2Str(_word_syms, words[i])); 77 | } 78 | } 79 | private: 80 | // recv data from request 81 | AsrReturnPackageAnalysis _ser_recv_package_analysis; 82 | // send data to request 83 | AsrReturnPackageAnalysis _ser_send_package_analysis; 84 | const AsrPostProcessWorkThreadOpt &_opts; 85 | 86 | kaldi::KaldiConstArpaLmRescore _old_kaldi_const_arpa_lmrescore; 87 | kaldi::KaldiConstArpaLmRescore _new_kaldi_const_arpa_lmrescore; 88 | const fst::SymbolTable &_word_syms; 89 | }; // class AsrPostProcessWorkThread ok 90 | 91 | #include "src/util/namespace-end.h" 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /src/post-processing-service/client-asr-post-process-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __CLIENT_ASR_POST_PROCESS_WORK_THREAD_H__ 2 | #define __CLIENT_ASR_POST_PROCESS_WORK_THREAD_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include "src/service2/thread-pool.h" 8 | #include "src/service2/thread-pool-work-thread.h" 9 | 10 | #include "src/util/namespace-start.h" 11 | 12 | class AsrPostProcessClientWorkThread: public ThreadPoolWorkThread 13 | { 14 | public: 15 | template friend class AsrPostProcessClinetTask; 16 | typedef ThreadBase::int32 int32; 17 | 18 | AsrPostProcessClientWorkThread(ThreadPoolBase *thread_pool, 19 | int32 port=8000, 20 | std::string ip="127.0.0.1"): 21 | ThreadPoolWorkThread(thread_pool) 22 | { 23 | memset(&_ser, 0x00, sizeof(_ser)); 24 | _ser.sin_family = AF_INET; 25 | inet_aton(ip.c_str(), &_ser.sin_addr); 26 | _ser.sin_port = htons(port); 27 | } 28 | 29 | ~AsrPostProcessClientWorkThread() { } 30 | private: 31 | struct sockaddr_in _ser; 32 | 33 | }; 34 | 35 | #include "src/util/namespace-end.h" 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/post-processing-service/conf/post-config.txt: -------------------------------------------------------------------------------- 1 | --config-socket=conf/post-socket.conf 2 | --nthread=1 3 | --determinize=false 4 | 5 | --print-thread-pool-info=true 6 | --timeout-times=-1 7 | 8 | --new-const-arpa-lm=source/const-arpa-1e-9 9 | --newscale=1.0 10 | --old-const-arpa-lm=source/const-arpa-1e-9_5e-9 11 | --oldscale=-1.0 12 | --word-syms-filename=source/words.txt 13 | -------------------------------------------------------------------------------- /src/post-processing-service/conf/post-socket.conf: -------------------------------------------------------------------------------- 1 | 2 | #--ip= 3 | --port=8110 4 | --rec-timeout=5000000 5 | --n-listen=50 6 | --keepalive=1 7 | --keeptime=10 8 | --keepinterval=1 9 | --keepcount=3 10 | 11 | --verbose=1 12 | 13 | -------------------------------------------------------------------------------- /src/post-processing-service/lattice-to-result.h: -------------------------------------------------------------------------------- 1 | #ifndef __LATTICE_TO_RESULT_H__ 2 | #define __LATTICE_TO_RESULT_H__ 3 | 4 | #include "fstext/fstext-lib.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace kaldi 12 | { 13 | 14 | template 15 | void FstToNbestVector(const fst::Fst &fst, 16 | size_t n, 17 | std::vector > *alignments, 18 | std::vector > *words, 19 | std::vector *weights = NULL) 20 | { 21 | std::vector > fsts_out; 22 | fst::VectorFst nbest_fst; 23 | fst::ShortestPath(fst, &nbest_fst, n); 24 | fst::ConvertNbestToVector(nbest_fst, &fsts_out); 25 | //fst::NbestAsFsts(fst, n, &fsts_out); 26 | for(size_t i = 0 ; i < fsts_out.size(); ++i) 27 | { 28 | std::vector alignment; 29 | std::vector word; 30 | typename Arc::Weight weight; 31 | GetLinearSymbolSequence(fsts_out[i], 32 | &alignment, 33 | &word, 34 | &weight);//, NULL); 35 | alignments->push_back(alignment); 36 | words->push_back(word); 37 | if(weights != NULL) 38 | weights->push_back(weight); 39 | } 40 | } 41 | 42 | template 43 | std::string ConvertWordId2Str(const fst::SymbolTable &word_syms, 44 | const std::vector &words) 45 | { 46 | std::ostringstream result; 47 | for(size_t i = 0 ; i < words.size() ; ++i) 48 | { 49 | std::string s = word_syms.Find(words[i]); 50 | if(s.empty()) 51 | { 52 | KALDI_WARN << "wrod-id " << std::to_string(words[i]) 53 | << " not in symbol table!!!" 54 | << result.str() << "<#" << std::to_string(words[i]) << "> "; 55 | } 56 | else 57 | result << s << " "; 58 | } 59 | return result.str(); 60 | } 61 | 62 | } // namespace kaldi 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/service/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | lib_LTLIBRARIES = libservice.la 3 | 4 | libservice_la_SOURCES = thread-pool.cc 5 | 6 | LDADD = libservice.la ../util/libutil.la -lpthread 7 | check_PROGRAMS = service \ 8 | client 9 | 10 | service_SOURCES = service.cc 11 | client_SOURCES = client.cc 12 | 13 | -------------------------------------------------------------------------------- /src/service/client.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); 15 | 16 | struct sockaddr_in ser, cli; 17 | memset(&ser, 0x00, sizeof(ser)); 18 | ser.sin_family = AF_INET; 19 | inet_aton("127.0.0.1",&ser.sin_addr); 20 | ser.sin_port = htons(atoi(argv[1])); 21 | 22 | int res = connect(sockfd, (struct sockaddr *) &ser, sizeof(ser)); 23 | 24 | while(true) 25 | { 26 | printf("please input:"); 27 | fflush(stdout); 28 | char buff[128] = {0}; 29 | char recvbuf[1024]; 30 | memset(recvbuf,0x00,sizeof(recvbuf)); 31 | memset(buff,0x00, 128); 32 | fgets(buff,128, stdin); 33 | 34 | if(strncmp(buff,"end",3) == 0) 35 | { 36 | // close(sockfd); 37 | break; 38 | } 39 | if(strlen(buff)-1 <=0) 40 | continue; 41 | send(sockfd, buff, strlen(buff)-1,0); 42 | int len = recv(sockfd, recvbuf, sizeof(recvbuf), 0 ); 43 | if(len<=0) 44 | { 45 | printf("close...\n"); 46 | break; 47 | } 48 | //recv(sockfd, buff, sizeof(buff), 0 ); 49 | printf("recv %s\n",recvbuf); 50 | } 51 | close(sockfd); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /src/service/service.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "src/service/thread-pool.h" 18 | #include "src/service/task.h" 19 | #include "src/util/log-message.h" 20 | 21 | int main(int argc, char *argv[]) 22 | { 23 | int sockfd = socket(AF_INET, SOCK_STREAM, 0 ); 24 | if(sockfd == -1) 25 | { 26 | LOG_COM << "socket error."; 27 | return -1; 28 | } 29 | struct sockaddr_in ser,cli; 30 | memset(&ser, 0, sizeof(ser)); 31 | ser.sin_family = AF_INET; 32 | inet_aton("127.0.0.1",&ser.sin_addr); 33 | ser.sin_port = htons(6500); 34 | 35 | int keepalive = 1; // open keepalive 36 | int keeptime = 5; // 5s no data 37 | int keepinterval = 1; 38 | int keepcount = 3; 39 | 40 | setsockopt(sockfd,SOL_SOCKET, SO_KEEPALIVE, (void*)&keepalive,sizeof(keepalive)); 41 | setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (void*)&keeptime, sizeof(keeptime)); 42 | setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void*)&keepinterval, sizeof(keepinterval)); 43 | setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void*)&keepcount, sizeof(keepcount)); 44 | 45 | struct timeval rec_timeout; 46 | rec_timeout.tv_sec = 5; 47 | rec_timeout.tv_usec =0; 48 | 49 | if (-1 ==setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void*)&rec_timeout, sizeof(rec_timeout))) 50 | { 51 | printf("Cannot set socket option SO_RCVTIMEO!"); 52 | return -1; 53 | } 54 | 55 | int flag = 1; 56 | int len = sizeof(int); 57 | if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) 58 | { 59 | printf("Cannot set socket option SO_REUSEADDR!"); 60 | return -1; 61 | } 62 | 63 | int res=bind(sockfd, (struct sockaddr *) & ser, sizeof(ser)); 64 | if(res != 0) 65 | { 66 | LOG_COM << "bind error."; 67 | return -1; 68 | } 69 | int nthread = 1; 70 | listen(sockfd, 5); 71 | 72 | ThreadPoolBase pool(nthread); 73 | while(1) 74 | { 75 | socklen_t len = sizeof(cli); 76 | int connectfd = accept(sockfd, (struct sockaddr *)&cli, &len); 77 | if(connectfd < 0) 78 | { 79 | if(errno == EAGAIN) 80 | { 81 | printf("."); 82 | //printf("no cli connect requst %d %d %d %d.\n", connectfd,errno,EINPROGRESS,EAGAIN); 83 | } 84 | else 85 | printf("cli connect error.\n"); 86 | 87 | } 88 | else 89 | { 90 | ASRServiceTask *ta = new ASRServiceTask(connectfd); 91 | pool.AddTask(ta); 92 | } 93 | } 94 | 95 | close(sockfd); 96 | return 0; 97 | } 98 | -------------------------------------------------------------------------------- /src/service/task.h: -------------------------------------------------------------------------------- 1 | #ifndef __TASK_H__ 2 | #define __TASK_H__ 3 | 4 | #include 5 | #include 6 | #include "src/service/thread-pool.h" 7 | #include "src/util/log-message.h" 8 | 9 | class ASRServiceTask:public TaskBase 10 | { 11 | public: 12 | typedef TaskBase::int32 int32; 13 | public: 14 | ASRServiceTask(int32 connfd, std::string task_name = ""): 15 | TaskBase(task_name), _connfd(connfd) {} 16 | 17 | int32 Run(void *data) 18 | { 19 | int n=0; // read times 20 | while(1) 21 | { 22 | char recvbuf[1024]; 23 | char sendbuf[1024]; 24 | memset(recvbuf,0x00,sizeof(recvbuf)); 25 | memset(sendbuf,0x00, sizeof(sendbuf)); 26 | 27 | int len = recv(_connfd, recvbuf, sizeof(recvbuf), 0 ); 28 | if(len <= 0) 29 | { 30 | LOG_COM << "on buf " << len << "!!!"; 31 | if(errno == EAGAIN || errno == EINPROGRESS) 32 | { 33 | LOG_COM << "|" << _connfd << "| timeout and continue"; 34 | n++; 35 | if(n>2) 36 | break; 37 | continue; 38 | } 39 | break; 40 | } 41 | 42 | n=0; 43 | VLOG_COM(0) << "from |" << _connfd << "| receive \"" << recvbuf << "\""; 44 | if(strncmp(recvbuf,"end",3) == 0) 45 | { 46 | break; 47 | } 48 | sprintf(sendbuf, "from |%d| receive \"%s\" ", _connfd, recvbuf); 49 | send(_connfd, sendbuf, sizeof(sendbuf), 0); 50 | printf("send |%d| : %s ok\n",_connfd, sendbuf); 51 | } 52 | close(_connfd); 53 | printf("close |%d| ok.\n",_connfd); 54 | return 0; 55 | } 56 | 57 | int32 Stop() 58 | { 59 | char *str = "no idle thread."; 60 | send(_connfd, str, strlen(str), 0); 61 | printf("send |%d| : %s ok\n",_connfd, str); 62 | return 0; 63 | } 64 | void GetInfo() 65 | { 66 | LOG_COM << "Task name is : " << _task_name ; 67 | LOG_COM << "Task connetid is : " << _connfd; 68 | } 69 | 70 | private: 71 | int32 _connfd; // connect fd 72 | void SetConnFd(int32 connfd); // set socket id 73 | }; 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /src/service/thread-class.h: -------------------------------------------------------------------------------- 1 | #ifndef __THREAD_CALSS_H__ 2 | #define __THREAD_CALSS_H__ 3 | 4 | 5 | 6 | class ThreadClass 7 | { 8 | public: 9 | ThreadClass(){} 10 | ThreadClass(int id):_id(id){ } 11 | 12 | virtual ~CThread() {} 13 | static void* ThreadFunc(void* para); 14 | int Run(); 15 | int Join(); 16 | int GetThreadId() const; 17 | int Start(); 18 | private: 19 | pthread_t _thread_id; 20 | int _id; 21 | }; 22 | 23 | 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/service/thread-pool.h: -------------------------------------------------------------------------------- 1 | #ifndef __THREAD_POOL_H__ 2 | #define __THREAD_POOL_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class TaskBase 10 | { 11 | public: 12 | typedef int int32; 13 | protected: 14 | std::string _task_name; 15 | public: 16 | TaskBase(std::string task_name=std::string("")): 17 | _task_name(task_name) { } 18 | virtual int32 Run(void *data) = 0; 19 | virtual int32 Stop() = 0; 20 | virtual void GetInfo() = 0; 21 | virtual ~TaskBase() { } 22 | }; 23 | 24 | class ThreadPoolBase 25 | { 26 | public: 27 | typedef int int32; 28 | enum ThreadPoolRetVal 29 | { 30 | TPERROR = -1, 31 | TPOK = 0 32 | }; 33 | protected: 34 | std::deque _task_list; // task list 35 | bool _shutdown; // thread exit 36 | int32 _thread_num; // thread pool thread number. 37 | std::vector _pthread_id; // all thread id 38 | std::vector _idle_pthread_id; // idle thread id 39 | std::vector _busy_pthread_id; // busy thread id 40 | 41 | pthread_mutex_t _pthread_pool_mutex; // thread synchronization lock 42 | pthread_cond_t _pthread_pool_cond; // thread condition lock 43 | 44 | void OneToTwo(pthread_t tid, std::vector &one, std::vector &two) 45 | { 46 | std::vector::iterator iter = one.begin(); 47 | while(iter != one.end()) 48 | { 49 | if (tid == *iter) 50 | break; 51 | iter++; 52 | } 53 | one.erase(iter); 54 | two.push_back(tid); 55 | } 56 | static void *ThreadFunc(void *thread_para); // thread callback function 57 | 58 | public: 59 | // pthread move to idle list 60 | void MoveToBusy(pthread_t tid) 61 | { 62 | OneToTwo(tid, _idle_pthread_id, _busy_pthread_id); 63 | } 64 | // pthread move to busy list 65 | void MoveToIdle(pthread_t tid) 66 | { 67 | OneToTwo(tid, _busy_pthread_id, _idle_pthread_id); 68 | } 69 | // thread synchronization lock 70 | pthread_mutex_t *GetPthreadMutex() 71 | { 72 | return &_pthread_pool_mutex; 73 | } 74 | // thread condition lock 75 | pthread_cond_t *GetPthreadCond() 76 | { 77 | return &_pthread_pool_cond; 78 | } 79 | 80 | TaskBase *GetTask() 81 | { 82 | TaskBase *task = _task_list.front(); 83 | _task_list.pop_front(); 84 | return task; 85 | } 86 | 87 | bool Shutdown() 88 | { 89 | return _shutdown; 90 | } 91 | 92 | 93 | ThreadPoolBase(int32 thread_num=10); 94 | // release thread pool source 95 | ~ThreadPoolBase(); 96 | // add task 97 | int32 AddTask(TaskBase *task); 98 | // stop all thread 99 | int32 StopAll(); 100 | 101 | int32 GetTaskSize() 102 | { 103 | return _task_list.size(); 104 | } 105 | }; 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /src/service2/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | lib_LTLIBRARIES = libservice2.la 3 | libservice2_la_SOURCES = thread-class.cc \ 4 | net-data-package.cc \ 5 | pthread-util.cc 6 | 7 | libservice2_la_LDFLAGS = -rpath '$(libdir)' 8 | 9 | LDADD = libservice2.la ../util/libutil.la -lpthread 10 | 11 | check_PROGRAMS = testv1-service \ 12 | test-client \ 13 | test-service \ 14 | net-data-package-test 15 | 16 | testv1_service_SOURCES = testv1-service.cc 17 | test_client_SOURCES = test-client.cc 18 | test_service_SOURCES = test-service.cc 19 | net_data_package_test_SOURCES = net-data-package-test.cc 20 | 21 | -------------------------------------------------------------------------------- /src/service2/pthread-util.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "src/service2/pthread-util.h" 3 | 4 | #include "src/util/namespace-start.h" 5 | void ThreadSigPipeIng() 6 | { 7 | sigset_t set; 8 | sigemptyset(&set); 9 | sigaddset(&set, SIGPIPE); 10 | pthread_sigmask(SIG_BLOCK, &set, NULL); 11 | } 12 | #include "src/util/namespace-end.h" 13 | -------------------------------------------------------------------------------- /src/service2/pthread-util.h: -------------------------------------------------------------------------------- 1 | // author: hubo 2 | // time : 2020/09/01 3 | #ifndef __PTHREAD_UTIL_H__ 4 | #define __PTHREAD_UTIL_H__ 5 | 6 | #include 7 | #include 8 | 9 | #include "src/util/namespace-start.h" 10 | // Block SIGPIPE before starting any other threads; other threads 11 | // created by main() will inherit a copy of the signal mask. 12 | // If you try using singal(SIGPIPE, SIG_IGN), it will be ignored by the thread 13 | // because signal is undefined with threads. 14 | void ThreadSigPipeIng(); 15 | #include "src/util/namespace-end.h" 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /src/service2/test-client.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); 15 | 16 | struct sockaddr_in ser; 17 | memset(&ser, 0x00, sizeof(ser)); 18 | ser.sin_family = AF_INET; 19 | inet_aton("127.0.0.1",&ser.sin_addr); 20 | ser.sin_port = htons(atoi(argv[1])); 21 | 22 | int res = connect(sockfd, (struct sockaddr *) &ser, sizeof(ser)); 23 | if(res < 0) 24 | { 25 | std::cerr << "connect " << sockfd << " failed." << std::endl; 26 | return -1; 27 | } 28 | 29 | while(true) 30 | { 31 | printf("please input:"); 32 | fflush(stdout); 33 | char buff[128] = {0}; 34 | char recvbuf[1024]; 35 | memset(recvbuf,0x00,sizeof(recvbuf)); 36 | memset(buff,0x00, 128); 37 | fgets(buff,128, stdin); 38 | 39 | if(strncmp(buff,"end",3) == 0) 40 | { 41 | // close(sockfd); 42 | break; 43 | } 44 | if(strlen(buff)-1 <=0) 45 | continue; 46 | send(sockfd, buff, strlen(buff)-1,0); 47 | int len = recv(sockfd, recvbuf, sizeof(recvbuf), 0 ); 48 | if(len<=0) 49 | { 50 | printf("close...\n"); 51 | break; 52 | } 53 | //recv(sockfd, buff, sizeof(buff), 0 ); 54 | printf("recv %s\n",recvbuf); 55 | } 56 | close(sockfd); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /src/service2/test-service.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "src/service2/thread-pool.h" 19 | #include "src/service2/test-work-thread.h" 20 | #include "src/service2/test-task.h" 21 | #include "src/util/log-message.h" 22 | #include "src/service2/socket-class.h" 23 | #include "src/service2/pthread-util.h" 24 | 25 | using namespace std; 26 | 27 | #ifdef NAMESPACE 28 | using namespace datemoon; 29 | #endif 30 | int main(int argc, char *argv[]) 31 | { 32 | ThreadSigPipeIng(); 33 | const char *usage = "This is a test service code.\n"; 34 | ConfigParseOptions conf(usage); 35 | SocketConf net_conf; 36 | net_conf.Register(&conf); 37 | conf.Read(argc, argv); 38 | //conf.PrintUsage(); 39 | SocketBase net_io(&net_conf); 40 | 41 | if(net_io.Init() < 0) 42 | { 43 | LOG_ERR << "net_io.Init failed!!!"; 44 | return -1; 45 | } 46 | 47 | if (0 != net_io.Bind()) 48 | { 49 | LOG_ERR << "net_io.Bind failed !!!"; 50 | return -1; 51 | } 52 | 53 | if ( 0 != net_io.Listen()) 54 | { 55 | LOG_ERR << "net_io.Listen failed!!!"; 56 | return -1; 57 | } 58 | 59 | int nthread = 3; 60 | ThreadPoolBase pool(nthread); 61 | { 62 | // create thread 63 | vector tmp_threads; 64 | for(int i =0;i 7 | #include 8 | #include "src/service2/thread-pool.h" 9 | #include "src/util/log-message.h" 10 | 11 | #include "src/util/namespace-start.h" 12 | 13 | class TestServiceTask:public TaskBase 14 | { 15 | public: 16 | typedef TaskBase::int32 int32; 17 | public: 18 | TestServiceTask(int32 connfd, std::string task_name = ""): 19 | TaskBase(task_name), _connfd(connfd) {} 20 | 21 | int32 Run(void *data) 22 | { 23 | int n=0; // read times 24 | while(1) 25 | { 26 | char recvbuf[1024]; 27 | char sendbuf[1024]; 28 | memset(recvbuf,0x00,sizeof(recvbuf)); 29 | memset(sendbuf,0x00, sizeof(sendbuf)); 30 | 31 | int len = recv(_connfd, recvbuf, sizeof(recvbuf), 0 ); 32 | if(len <= 0) 33 | { 34 | LOG_COM << "on buf " << len << "!!!"; 35 | if(errno == EAGAIN || errno == EINPROGRESS) 36 | { 37 | LOG_COM << "|" << _connfd << "| timeout and continue"; 38 | n++; 39 | if(n>2) 40 | break; 41 | continue; 42 | } 43 | break; 44 | } 45 | 46 | n=0; 47 | VLOG_COM(0) << "from |" << _connfd << "| receive \"" << recvbuf << "\""; 48 | if(strncmp(recvbuf,"end",3) == 0) 49 | { 50 | break; 51 | } 52 | sprintf(sendbuf, "from |%d| receive \"%s\" ", _connfd, recvbuf); 53 | send(_connfd, sendbuf, sizeof(sendbuf), 0); 54 | printf("send |%d| : %s ok\n",_connfd, sendbuf); 55 | } 56 | close(_connfd); 57 | printf("close |%d| ok.\n",_connfd); 58 | return 0; 59 | } 60 | 61 | int32 Stop() 62 | { 63 | const char *str = "no idle thread."; 64 | send(_connfd, str, strlen(str), 0); 65 | printf("send |%d| : %s ok\n",_connfd, str); 66 | close(_connfd); 67 | return 0; 68 | } 69 | void GetInfo() 70 | { 71 | LOG_COM << "Task name is : " << _task_name ; 72 | LOG_COM << "Task connetid is : " << _connfd; 73 | } 74 | 75 | private: 76 | int32 _connfd; // connect fd 77 | void SetConnFd(int32 connfd); // set socket id 78 | }; 79 | #include "src/util/namespace-end.h" 80 | #endif 81 | -------------------------------------------------------------------------------- /src/service2/test-work-thread.h: -------------------------------------------------------------------------------- 1 | // author: hubo 2 | // time : 2020/08/19 3 | #ifndef __TEST_WORK_THREAD_H__ 4 | #define __TEST_WORK_THREAD_H__ 5 | 6 | #include "src/service2/thread-class.h" 7 | #include "src/service2/thread-pool.h" 8 | #include "src/util/log-message.h" 9 | 10 | #include "src/util/namespace-start.h" 11 | 12 | class TestWorkThread:public ThreadBase 13 | { 14 | public: 15 | typedef ThreadBase::int32 int32; 16 | public: 17 | TestWorkThread(ThreadPoolBase *tp) 18 | { 19 | _thread_pool = tp; 20 | } 21 | ~TestWorkThread() { } 22 | void Run(); 23 | int32 InitASRSource(); 24 | private: 25 | ThreadPoolBase *_thread_pool; 26 | // feature 27 | // nnet 28 | // fst 29 | 30 | }; 31 | 32 | void TestWorkThread::Run() 33 | { 34 | pthread_t tid = GetThreadId(); 35 | // thread synchronization lock 36 | pthread_mutex_t *pthread_pool_mutex = _thread_pool->GetPthreadMutex(); 37 | // thread condition lock 38 | pthread_cond_t *pthread_pool_cond = _thread_pool->GetPthreadCond(); 39 | 40 | while(1) 41 | { 42 | pthread_mutex_lock(pthread_pool_mutex); 43 | while(_thread_pool->GetTaskSize() == 0 && !_thread_pool->Shutdown()) 44 | { 45 | // set thread ready ok, because pthread_cond_t can be loss, 46 | // make sure pthread_pool_cond be get when task arrive. 47 | if(_ready_ok == false) 48 | {// run ok 49 | _ready_ok = true; 50 | } 51 | LOG_COM << "Thread " << tid << " wait task."; 52 | pthread_cond_wait(pthread_pool_cond, pthread_pool_mutex); 53 | } 54 | if(_thread_pool->Shutdown()) 55 | { 56 | pthread_mutex_unlock(pthread_pool_mutex); 57 | LOG_COM << "Thread " << tid << " will exit."; 58 | pthread_exit(NULL); 59 | } 60 | LOG_COM << "tid " << tid << " run."; 61 | 62 | TaskBase *task = _thread_pool->GetTask(); 63 | 64 | _thread_pool->MoveToBusy(tid); // add busy list 65 | pthread_mutex_unlock(pthread_pool_mutex); 66 | 67 | if(0 != task->Run(NULL)) 68 | { 69 | LOG_WARN << "task run error!!!"; 70 | } 71 | delete task; 72 | 73 | pthread_mutex_lock(pthread_pool_mutex); 74 | _thread_pool->MoveToIdle(tid); // add idle list 75 | pthread_mutex_unlock(pthread_pool_mutex); 76 | } 77 | } 78 | 79 | #include "src/util/namespace-end.h" 80 | #endif 81 | -------------------------------------------------------------------------------- /src/service2/thread-class.cc: -------------------------------------------------------------------------------- 1 | #include "src/service2/thread-class.h" 2 | #include "src/util/log-message.h" 3 | 4 | #include "src/util/namespace-start.h" 5 | 6 | void* ThreadBase::ThreadFunc(void *para) 7 | { 8 | ThreadBase *th = static_cast(para); 9 | th->Run(); 10 | return NULL; 11 | } 12 | 13 | ThreadBase::int32 ThreadBase::Join(void **ret) 14 | { 15 | return pthread_join(_thread_id, ret); 16 | } 17 | 18 | ThreadBase::int32 ThreadBase::Create() 19 | { 20 | if(pthread_create(&_thread_id ,NULL ,&ThreadBase::ThreadFunc ,(void*)this) != 0) 21 | { 22 | LOG_COM << "Create pthread failed!!!"; 23 | return TERROR; 24 | } 25 | return TOK; 26 | } 27 | 28 | pthread_t ThreadBase::GetThreadId() const 29 | { 30 | return _thread_id; 31 | } 32 | 33 | ThreadBase::int32 ThreadBase::GetThreadIndex() const 34 | { 35 | return _index; 36 | } 37 | 38 | #include "src/util/namespace-end.h" 39 | -------------------------------------------------------------------------------- /src/service2/thread-class.h: -------------------------------------------------------------------------------- 1 | // author: hubo 2 | // time : 2020/08/14 3 | #ifndef __THREAD_CALSS_H__ 4 | #define __THREAD_CALSS_H__ 5 | 6 | #include 7 | 8 | #include "src/util/namespace-start.h" 9 | 10 | class ThreadBase 11 | { 12 | public: 13 | typedef int int32; 14 | enum ThreadRetVal 15 | { 16 | TERROR = -1, 17 | TOK = 0 18 | }; 19 | public: 20 | ThreadBase():_thread_id(0),_index(-1),_ready_ok(false){} 21 | ThreadBase(int32 index):_thread_id(0),_index(index),_ready_ok(false){ } 22 | 23 | virtual ~ThreadBase() {} 24 | static void* ThreadFunc(void* para); // thread function 25 | virtual void Run() = 0; // real process data 26 | int32 Create(); // create thread 27 | int32 Join(void **ret); // join thread 28 | pthread_t GetThreadId() const; // get pthread id 29 | int32 GetThreadIndex() const; // get pthread index 30 | bool IsReady() { return _ready_ok;} 31 | void Reset(); 32 | protected: 33 | pthread_t _thread_id; 34 | int32 _index; 35 | bool _ready_ok; 36 | }; 37 | 38 | #include "src/util/namespace-end.h" 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/service2/thread-info.h: -------------------------------------------------------------------------------- 1 | // author: hubo 2 | // time : 2020/09 3 | #ifndef __THREAD_INFO_H__ 4 | #define __THREAD_INFO_H__ 5 | 6 | #include 7 | 8 | #include "src/util/namespace-start.h" 9 | 10 | class ThreadTimeInfo 11 | { 12 | protected: 13 | // data time and work time and for calculate efficiency 14 | double _data_time; 15 | double _work_time; 16 | double _effect_data_time; 17 | public: 18 | ThreadTimeInfo(): _data_time(0.0), _work_time(0.0), _effect_data_time(0.0) { } 19 | virtual ~ThreadTimeInfo() { } 20 | virtual double GetDataTime() { return _data_time;} 21 | virtual double GetWorkTime() { return _work_time;} 22 | virtual double GetInvalidDataTime() { return _effect_data_time;} 23 | virtual void SetTime(double data_time, double work_time, double effect_data_time = -1) 24 | { 25 | _data_time += data_time; 26 | if(effect_data_time < 0) 27 | effect_data_time = data_time; 28 | _work_time += work_time; 29 | _effect_data_time += effect_data_time; 30 | } 31 | virtual double GetEfficiency() 32 | { 33 | if(_data_time == 0) 34 | return -1; 35 | return _work_time/_data_time; 36 | } 37 | virtual void TimeInfo() 38 | { 39 | std::cout << "Data time is : " << _data_time << std::endl; 40 | std::cout << "effect data time is : " << _effect_data_time << std::endl; 41 | std::cout << "Wrok time is : " << _work_time << std::endl; 42 | } 43 | }; 44 | #include "src/util/namespace-end.h" 45 | #endif 46 | -------------------------------------------------------------------------------- /src/service2bin/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | 3 | LDADD = ../service2/libservice2.la \ 4 | ../util/libutil.la 5 | 6 | LDADD += -lm -lpthread $(DL_LIBS) 7 | 8 | bin_PROGRAMS = 9 | noinst_PROGRAMS = asr-client \ 10 | asr-client-v1 \ 11 | asr-client-v2 12 | 13 | asr_client_SOURCES = asr-client.cc 14 | asr_client_v1_SOURCES = asr-client-v1.cc 15 | asr_client_v2_SOURCES = asr-client-v2.cc 16 | -------------------------------------------------------------------------------- /src/service2bin/asr-client-v1.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "src/service2/net-data-package.h" 13 | 14 | #ifdef NAMESPACE 15 | using namespace datemoon; 16 | #endif 17 | 18 | #define LEN 4096 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | if(argc != 3) 23 | { 24 | LOG_WARN << argv[0] << " port wavfile"; 25 | return -1; 26 | } 27 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); 28 | 29 | C2SPackageAnalysis c2s_cli; 30 | S2CPackageAnalysis s2c_cli; 31 | struct sockaddr_in ser; 32 | memset(&ser, 0x00, sizeof(ser)); 33 | ser.sin_family = AF_INET; 34 | inet_aton("127.0.0.1",&ser.sin_addr); 35 | ser.sin_port = htons(atoi(argv[1])); 36 | 37 | char *wav_file = argv[2]; 38 | int res = connect(sockfd, (struct sockaddr *) &ser, sizeof(ser)); 39 | if(res < 0) 40 | { 41 | LOG_WARN << "connect error!!!"; 42 | return -1; 43 | } 44 | FILE *fp = fopen(wav_file, "r"); 45 | while(true) 46 | { 47 | char sentbuf[LEN]; 48 | char recvbuf[LEN]; 49 | memset(sentbuf,0x00,sizeof(sentbuf)); 50 | memset(recvbuf,0x00,sizeof(recvbuf)); 51 | size_t sent_len = fread((void*)sentbuf, 1, LEN, fp); 52 | if(sent_len > 0) 53 | { 54 | if(true != c2s_cli.C2SWrite(sockfd, sentbuf, sent_len, 0)) 55 | { 56 | LOG_WARN << "C2SWrite failed!!!"; 57 | return -1; 58 | } 59 | c2s_cli.Print("c2s_cli"); 60 | } 61 | else 62 | { 63 | if(true != c2s_cli.C2SWrite(sockfd, sentbuf, sent_len, 1)) 64 | { 65 | LOG_WARN << "C2SWrite end failed!!!"; 66 | return -1; 67 | } 68 | c2s_cli.Print("c2s_cli"); 69 | 70 | if(true != s2c_cli.S2CRead(sockfd)) 71 | { 72 | LOG_WARN << "S2CRead failed!!!"; 73 | return false; 74 | } 75 | s2c_cli.Print("s2c_cli"); 76 | break; 77 | } 78 | 79 | if(true != s2c_cli.S2CRead(sockfd)) 80 | { 81 | LOG_WARN << "S2CRead failed!!!"; 82 | return false; 83 | } 84 | s2c_cli.Print("s2c_cli"); 85 | } 86 | close(sockfd); 87 | fclose(fp); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /src/service2bin/asr-client-v2.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "src/service2/net-data-package.h" 14 | #include "src/service2/pthread-util.h" 15 | 16 | #ifdef NAMESPACE 17 | using namespace datemoon; 18 | #endif 19 | 20 | #define LEN 4096 21 | 22 | void *RecvThreadFunc(void *connect_fd) 23 | { 24 | int sockfd = *(int*)connect_fd; 25 | S2CPackageAnalysis s2c_cli; 26 | while(1) 27 | { 28 | if(true != s2c_cli.S2CRead(sockfd)) 29 | { 30 | LOG_WARN << "S2CRead failed!!!"; 31 | return NULL; 32 | } 33 | if(true == s2c_cli.IsAllEnd()) 34 | { 35 | s2c_cli.Print("s2c_cli"); 36 | return NULL; 37 | } 38 | } 39 | } 40 | 41 | 42 | int main(int argc, char *argv[]) 43 | { 44 | // Block SIGPIPE before starting any other threads; other threads 45 | // created by main() will inherit a copy of the signal mask. 46 | ThreadSigPipeIng(); 47 | 48 | if(argc != 3) 49 | { 50 | LOG_WARN << argv[0] << " port wavfile"; 51 | return -1; 52 | } 53 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); 54 | 55 | C2SPackageAnalysis c2s_cli; 56 | c2s_cli.SetNbest(1); 57 | struct sockaddr_in ser; 58 | memset(&ser, 0x00, sizeof(ser)); 59 | ser.sin_family = AF_INET; 60 | inet_aton("127.0.0.1",&ser.sin_addr); 61 | ser.sin_port = htons(atoi(argv[1])); 62 | 63 | char *wav_file = argv[2]; 64 | int res = connect(sockfd, (struct sockaddr *) &ser, sizeof(ser)); 65 | if(res < 0) 66 | { 67 | LOG_WARN << "connect error!!!"; 68 | return -1; 69 | } 70 | pthread_t thread_id; 71 | if(pthread_create(&thread_id ,NULL ,&RecvThreadFunc ,(void*)&sockfd) != 0) 72 | { 73 | LOG_WARN << "pthread_create failed!!!"; 74 | return -1; 75 | } 76 | 77 | FILE *fp = fopen(wav_file, "r"); 78 | while(true) 79 | { 80 | char sentbuf[LEN]; 81 | char recvbuf[LEN]; 82 | memset(sentbuf,0x00,sizeof(sentbuf)); 83 | memset(recvbuf,0x00,sizeof(recvbuf)); 84 | size_t sent_len = fread((void*)sentbuf, 1, LEN, fp); 85 | if(sent_len > 0) 86 | { 87 | c2s_cli.SetNbest(1); 88 | if(true != c2s_cli.C2SWrite(sockfd, sentbuf, sent_len, 0)) 89 | { 90 | LOG_WARN << "C2SWrite failed!!!"; 91 | return -1; 92 | } 93 | c2s_cli.Print("c2s_cli"); 94 | } 95 | else 96 | { 97 | c2s_cli.SetNbest(5); 98 | if(true != c2s_cli.C2SWrite(sockfd, sentbuf, sent_len, 1)) 99 | { 100 | LOG_WARN << "C2SWrite end failed!!!"; 101 | return -1; 102 | } 103 | c2s_cli.Print("c2s_cli"); 104 | 105 | break; 106 | } 107 | 108 | } 109 | pthread_join(thread_id,NULL); 110 | close(sockfd); 111 | fclose(fp); 112 | return 0; 113 | } 114 | -------------------------------------------------------------------------------- /src/service2bin/asr-client.cc: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int sockfd = socket(AF_INET, SOCK_STREAM, 0); 15 | 16 | struct sockaddr_in ser; 17 | memset(&ser, 0x00, sizeof(ser)); 18 | ser.sin_family = AF_INET; 19 | inet_aton("127.0.0.1",&ser.sin_addr); 20 | ser.sin_port = htons(atoi(argv[1])); 21 | 22 | char *wav_file = argv[2]; 23 | int res = connect(sockfd, (struct sockaddr *) &ser, sizeof(ser)); 24 | if(res < 0) 25 | { 26 | std::cout << "connect failed!!!" << std::endl; 27 | return -1; 28 | } 29 | FILE *fp = fopen(wav_file, "r"); 30 | while(true) 31 | { 32 | char sentbuf[1024]; 33 | char recvbuf[1024]; 34 | memset(sentbuf,0x00,sizeof(sentbuf)); 35 | memset(recvbuf,0x00,sizeof(recvbuf)); 36 | size_t sent_len = fread((void*)sentbuf, 1, 1024, fp); 37 | if(sent_len > 0) 38 | { 39 | send(sockfd, sentbuf, sent_len, 0); 40 | } 41 | else 42 | { 43 | send(sockfd, sentbuf, 0, 0); 44 | break; 45 | } 46 | } 47 | close(sockfd); 48 | fclose(fp); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /src/service2bin/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | export LD_LIBRARY_PATH=$PWD/lib 5 | 6 | ./bin/asr-service --config=conf/conf.txt final.mdl HCLG.fst words.txt 7 | -------------------------------------------------------------------------------- /src/util/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | 3 | AM_CPPFLAGS = 4 | 5 | lib_LTLIBRARIES = libutil.la 6 | 7 | libutil_la_SOURCES = config-parse-options.cc \ 8 | text-util.cc \ 9 | io-funcs.cc \ 10 | read-vector.cc \ 11 | log-message.cc 12 | 13 | libutil_la_LDFLAGS = -rpath '$(libdir)' 14 | 15 | LDADD = ../util/libutil.la 16 | 17 | check_PROGRAMS = test-config-parse-options \ 18 | test-text-util \ 19 | hash-list-test \ 20 | log-message-test \ 21 | hash-key-test 22 | 23 | test_config_parse_options_SOURCES = test-config-parse-options.cc 24 | test_text_util_SOURCES = test-text-util.cc 25 | hash_list_test_SOURCES = hash-list-test.cc 26 | log_message_test_SOURCES = log-message-test.cc 27 | hash_key_test_SOURCES = hash-key-test.cc 28 | -------------------------------------------------------------------------------- /src/util/hash-key-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/util/hash-key.h" 4 | 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | int main(int argc,char*argv[]) 9 | { 10 | typedef std::unordered_map WordHash; 11 | WordHash word_dict; 12 | word_dict["abc"] = 1; 13 | word_dict["bcd"] = 2; 14 | std::cout << word_dict["abc"] << " " << word_dict["bcd"] << std::endl; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /src/util/hash-key.h: -------------------------------------------------------------------------------- 1 | #ifndef __HASH_KEY_H__ 2 | #define __HASH_KEY_H__ 3 | 4 | #include 5 | 6 | #include "src/util/namespace-start.h" 7 | /* bitmask used to compute hash 8 | * code modulo maxEntries */ 9 | #define hashMask(nbits) (~((~(size_t)0L)<<(nbits))) 10 | 11 | class StringKey 12 | { 13 | public: 14 | inline size_t HashKey(unsigned long key, unsigned maxBits) const 15 | { 16 | return (((key * 1103515245 + 12345) >> (30-maxBits)) & hashMask(maxBits)); 17 | } 18 | 19 | size_t operator ()(const std::string &str) const 20 | // hashes only the state and string. 21 | { 22 | unsigned maxBits = 16; 23 | unsigned long i = 0; 24 | const char *key = str.c_str(); 25 | for (; *key; key++) 26 | { 27 | i += (i << 3) + *key; 28 | } 29 | return HashKey(i, maxBits); 30 | } 31 | }; 32 | 33 | class StringEqual 34 | { 35 | public: 36 | bool operator ()(const std::string &key1, const std::string &key2) const 37 | { 38 | return (strcmp(key1.c_str(), key2.c_str()) == 0); 39 | } 40 | }; 41 | 42 | #include "src/util/namespace-end.h" 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/util/hash-list-test.cc: -------------------------------------------------------------------------------- 1 | #include // for baseline. 2 | #include 3 | #include 4 | #include "src/util/hash-list.h" 5 | 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | template void TestHashList() 10 | { 11 | typedef typename HashList::Elem Elem; 12 | 13 | HashList hash; 14 | hash.SetSize(200); // must be called before use. 15 | std::map m1; 16 | for (size_t j = 0; j < 50; j++) { 17 | Int key = random() % 200; 18 | T val = random() % 50; 19 | m1[key] = val; 20 | Elem *e = hash.Find(key); 21 | if (e) e->val = val; 22 | else hash.Insert(key, val); 23 | } 24 | 25 | 26 | std::map m2; 27 | 28 | for (int i = 0; i < 100; i++) { 29 | m2.clear(); 30 | for (typename std::map::const_iterator iter = m1.begin(); 31 | iter != m1.end(); 32 | iter++) { 33 | m2[iter->first + 1] = iter->second; 34 | } 35 | std::swap(m1, m2); 36 | 37 | Elem *h = hash.Clear(), *tmp; 38 | 39 | hash.SetSize(100 + random() % 100); // note, SetSize is relatively cheap 40 | // operation as long as we are not increasing the size more than it's ever 41 | // previously been increased to. 42 | 43 | for (; h != NULL; h = tmp) { 44 | hash.Insert(h->key + 1, h->val); 45 | tmp = h->tail; 46 | hash.Delete(h); // think of this like calling delete. 47 | } 48 | 49 | // Now make sure h and m2 are the same. 50 | const Elem *list = hash.GetList(); 51 | size_t count = 0; 52 | for (; list != NULL; list = list->tail, count++) { 53 | assert(m1[list->key] == list->val); 54 | } 55 | 56 | for (size_t j = 0; j < 10; j++) { 57 | Int key = random() % 200; 58 | bool found_m1 = (m1.find(key) != m1.end()); 59 | if (found_m1) m1[key]; 60 | Elem *e = hash.Find(key); 61 | assert((e != NULL) == found_m1); 62 | if (found_m1) 63 | assert(m1[key] == e->val); 64 | } 65 | 66 | assert(m1.size() == count); 67 | } 68 | Elem *tmp = hash.Clear(); 69 | Elem *e_tail = NULL; 70 | for(;tmp != NULL; tmp = e_tail) 71 | { 72 | e_tail = tmp->tail; 73 | hash.Delete(tmp); 74 | } 75 | } 76 | 77 | int main() { 78 | for (size_t i = 0;i < 3;i++) { 79 | TestHashList(); 80 | TestHashList(); 81 | TestHashList(); 82 | TestHashList(); 83 | TestHashList(); 84 | TestHashList(); 85 | } 86 | LOG_COM << "Test OK.\n"; 87 | } 88 | -------------------------------------------------------------------------------- /src/util/log-message-test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include "src/util/log-message.h" 3 | 4 | 5 | #ifdef NAMESPACE 6 | using namespace datemoon; 7 | #endif 8 | int main(int argc,char *argv[]) 9 | { 10 | if(argc == 2) 11 | { 12 | CreateLogHandler(argv[1]); 13 | LOG_COM << "it's log test code"; 14 | LOG_WARN << "it's warning test code"; 15 | LOG_ERR << "it's error test code"; 16 | CloseLogHandler(); 17 | } 18 | else 19 | { 20 | LOG_COM << "it's log test code"; 21 | LOG_WARN << "it's warning test code"; 22 | LOG_ASSERT(1); 23 | LOG_ERR << "it's error test code"; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/util/mem-pool.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTIL_MEM_POOL_H__ 2 | #define __UTIL_MEM_POOL_H__ 3 | #include 4 | #include 5 | #include 6 | 7 | #include "src/util/namespace-start.h" 8 | 9 | class TypeBase 10 | { 11 | private: 12 | public: 13 | TypeBase *_next; 14 | }; 15 | 16 | template 17 | class MemPool 18 | { 19 | public: 20 | MemPool(unsigned int allocate_block_size = 128):_pool_head(NULL), _pool_size(0), _allocate_block_size(allocate_block_size){ } 21 | 22 | T* NewT() 23 | { 24 | if(_pool_head == NULL) 25 | { 26 | T* val = new T[_allocate_block_size]; 27 | memset(val, 0x00, sizeof(T)*_allocate_block_size); 28 | for(size_t i=0; i+1<_allocate_block_size; ++i) 29 | val[i]._next = val+i+1; 30 | val[_allocate_block_size-1]._next = NULL; 31 | _pool_head = val; 32 | _pool_size += _allocate_block_size; 33 | _pool.push_back(val); 34 | } 35 | T *ret = _pool_head; 36 | _pool_head = _pool_head->_next; 37 | _pool_size--; 38 | return ret; 39 | } 40 | 41 | void DeleteT(T* val) 42 | { 43 | val->_next = _pool_head; 44 | _pool_head = val; 45 | _pool_size++; 46 | } 47 | 48 | ~MemPool() 49 | { 50 | for(size_t i=0;i<_pool.size();++i) 51 | { 52 | delete []_pool[i]; 53 | _pool_size -= _allocate_block_size; 54 | } 55 | _pool.clear(); 56 | _pool_head = NULL; 57 | assert(_pool_size == 0 && _pool_head == NULL); 58 | } 59 | private: 60 | std::vector _pool; 61 | T *_pool_head; 62 | size_t _pool_size; 63 | unsigned int _allocate_block_size; 64 | 65 | }; 66 | 67 | #include "src/util/namespace-end.h" 68 | #endif 69 | -------------------------------------------------------------------------------- /src/util/namespace-end.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef NAMESPACE 3 | } 4 | #endif 5 | 6 | -------------------------------------------------------------------------------- /src/util/namespace-start.h: -------------------------------------------------------------------------------- 1 | 2 | #ifdef NAMESPACE 3 | namespace datemoon { 4 | #endif 5 | 6 | -------------------------------------------------------------------------------- /src/util/read-vector.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "src/util/read-vector.h" 4 | #include "src/util/io-funcs.h" 5 | #include "src/util/log-message.h" 6 | 7 | #include "src/util/namespace-start.h" 8 | 9 | void ReadVector(std::istream &is, std::vector &vec, bool binary) 10 | { 11 | int pos_at_start = is.tellg(); 12 | if(is.fail()) 13 | { 14 | LOG_ERR << "Failed to read token [started at file position " 15 | << pos_at_start << "]" << std::endl; 16 | } 17 | if(!binary) 18 | is >> std::ws; // consume whitespace. 19 | ExpectToken(is, binary, "["); 20 | while(1) 21 | { 22 | std::string str; 23 | ReadToken(is, binary, &str); 24 | if(str == "]") 25 | break; 26 | vec.push_back(atof(str.c_str())); 27 | } 28 | } 29 | #include "src/util/namespace-end.h" 30 | -------------------------------------------------------------------------------- /src/util/read-vector.h: -------------------------------------------------------------------------------- 1 | #ifndef __READ_VECTOR_H__ 2 | #define __READ_VECTOR_H__ 3 | 4 | #include 5 | #include 6 | 7 | #include "src/util/namespace-start.h" 8 | 9 | void ReadVector(std::istream &is , std::vector &vec , bool binary); 10 | 11 | #include "src/util/namespace-end.h" 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /src/util/stl-util.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTIL_STL_UTIL_H__ 2 | #define __UTIL_STL_UTIL_H__ 3 | 4 | #include 5 | 6 | #include "src/util/namespace-start.h" 7 | 8 | template 9 | struct PairHasher { // hashing function for pair 10 | size_t operator()(const std::pair &x) const noexcept 11 | { // 7853 was chosen at random from a list of primes. 12 | //return x.first + x.second * 7853; 13 | return x.first * 7853 + x.second ; 14 | //return x.first * 389 + x.second * 7853; 15 | } 16 | PairHasher() { } 17 | }; 18 | template 19 | struct PairCmp 20 | { 21 | bool operator()(const std::pair &x1, 22 | const std::pair &x2) const 23 | { 24 | return x1.first == x2.first && x1.second == x2.second; 25 | } 26 | PairCmp() { } 27 | }; 28 | 29 | #include "src/util/namespace-end.h" 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/util/test-config-parse-options.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "config-parse-options.h" 4 | 5 | using namespace std; 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | int main(int argc,char *argv[]) 10 | { 11 | bool binary = true; 12 | int integer = 0; 13 | unsigned uinteger = 0; 14 | float fl = 0.0; 15 | string str("yes"); 16 | double db = 0.0; 17 | 18 | const char *usage = "This is a test code\n"; 19 | ConfigParseOptions conf(usage); 20 | conf.Register("binary",&binary,"default is true"); 21 | conf.Register("integer",&integer,"default is 0"); 22 | conf.Register("uinteger",&uinteger,"default is 0"); 23 | conf.Register("fl",&fl,"default is 0.0"); 24 | conf.Register("str",&str,"default is yes"); 25 | conf.Register("db",&db,"default is 0.0"); 26 | 27 | conf.Read(argc, argv); 28 | conf.PrintUsage(); 29 | std::cout << "--binary=" << (binary ? "true" : "false") 30 | << "\n--integer=" << integer 31 | << "\n--uinteger=" << uinteger 32 | << "\n--fl=" << fl 33 | << "\n--str=" << str 34 | << "\n--db=" << db < 3 | #include 4 | #include 5 | 6 | #ifdef NAMESPACE 7 | using namespace datemoon; 8 | #endif 9 | int main(int argc,char *argv[]) 10 | { 11 | std::string line("hello. \n today is good day. \n"); 12 | std::vector str; 13 | 14 | CutLineToStr(line, str); 15 | for(unsigned i=0;i 3 | #include 4 | #include 5 | #include "text-util.h" 6 | 7 | #include "src/util/namespace-start.h" 8 | 9 | bool ConvertStringToInteger(const std::string &str, int *out) 10 | { 11 | sscanf(str.c_str(), "%d",out); 12 | return true; 13 | } 14 | 15 | void TrimString(std::string &str) 16 | { 17 | const char *white_chars = " \t\n\r\f\v"; 18 | 19 | std::string::size_type pos = str.find_last_not_of(white_chars); 20 | if (pos != std::string::npos) 21 | { 22 | str.erase(pos + 1); 23 | pos = str.find_first_not_of(white_chars); 24 | if (pos != std::string::npos) str.erase(0, pos); 25 | } 26 | else 27 | { 28 | str.erase(str.begin(), str.end()); 29 | } 30 | 31 | /* 32 | size_t i=0,j=0; 33 | size_t len = str.length(); 34 | char *s = (char*)str.c_str(); 35 | for(i=0,j=0; i < len;++i) 36 | { 37 | switch(s[i]) 38 | { 39 | case ' ': 40 | case '\t': 41 | case '\n': 42 | case '\r': 43 | case '\f': 44 | case '\v': 45 | break; 46 | default: 47 | s[j] = s[i]; 48 | ++j; 49 | break; 50 | } 51 | } 52 | s[j] = '\0'; 53 | */ 54 | } 55 | 56 | void CutLineToStr(std::string line,std::vector &str)//, char ch) 57 | { 58 | str.clear(); 59 | size_t i=0,j=0; 60 | size_t len = line.length(); 61 | char *s = (char*)line.c_str(); 62 | int k = 0; 63 | for(i=0,j=0;i 5 | #include 6 | #include 7 | 8 | #include "src/util/namespace-start.h" 9 | bool ConvertStringToInteger(const std::string &str, int *out); 10 | 11 | void TrimString(std::string &str); 12 | 13 | void CutLineToStr(std::string line,std::vector &str);//, char ch = ' '); 14 | #include "src/util/namespace-end.h" 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/util/util-common.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTIL_COMMOND_H__ 2 | #define __UTIL_COMMOND_H__ 3 | 4 | #include 5 | #include 6 | #include // C string stuff like strcpy 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "src/util/log-message.h" 16 | #include "src/util/io-funcs.h" 17 | #include "src/util/text-util.h" 18 | #include "src/util/read-vector.h" 19 | #include "src/util/config-parse-options.h" 20 | #include "src/util/util-type.h" 21 | #include "src/util/hash-key.h" 22 | #include "src/util/hash-list.h" 23 | #include "src/util/util-time.h" 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/util/util-time.h: -------------------------------------------------------------------------------- 1 | #ifndef __DATEMOON_TIME_H__ 2 | #define __DATEMOON_TIME_H__ 3 | 4 | #include 5 | 6 | #include "src/util/namespace-start.h" 7 | 8 | class Time 9 | { 10 | private: 11 | struct timeval _tv_start; 12 | struct timezone _tz; 13 | public: 14 | Time() 15 | { 16 | Reset(); 17 | } 18 | 19 | void Reset() 20 | { 21 | gettimeofday(&_tv_start, &_tz); 22 | } 23 | double Esapsed() 24 | { 25 | struct timeval tv_end; 26 | struct timezone tz; 27 | gettimeofday(&tv_end, &tz); 28 | double start_t = _tv_start.tv_sec + 29 | static_cast(_tv_start.tv_usec)/(1000*1000); 30 | double end_t = tv_end.tv_sec + 31 | static_cast(tv_end.tv_usec)/(1000*1000); 32 | Reset(); 33 | return end_t - start_t; 34 | } 35 | }; 36 | 37 | #include "src/util/namespace-end.h" 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/util/util-type.h: -------------------------------------------------------------------------------- 1 | #ifndef __UTIL_TYPE_H__ 2 | #define __UTIL_TYPE_H__ 3 | 4 | #include "src/util/namespace-start.h" 5 | 6 | #define CONST_GLOBAL_SIZE (2<<10) 7 | 8 | typedef float BaseFloat; 9 | typedef float float32; 10 | typedef double double64; 11 | typedef short int16; 12 | typedef int int32; 13 | typedef long long int int64; 14 | typedef unsigned short uint16; 15 | typedef unsigned int uint32; 16 | typedef unsigned long long int uint64; 17 | typedef float BaseFloat; 18 | enum RetVal 19 | { 20 | ERROR = -1, 21 | OK = 0 22 | }; 23 | 24 | #include "src/util/namespace-end.h" 25 | #endif 26 | -------------------------------------------------------------------------------- /src/v1-asr/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 8 | $(KALDI_DEPLDFLAGS) \ 9 | $(MATRIX_DEPLDFLAGS) 10 | 11 | lib_LTLIBRARIES = libv1asr.la 12 | 13 | libv1asr_la_SOURCES = asr-work-thread.cc asr-source.cc 14 | libv1asr_la_LDFLAGS = -rpath '$(libdir)' 15 | 16 | check_PROGRAMS = kaldi-v1-asr-online-test 17 | 18 | kaldi_v1_asr_online_test_SOURCES = kaldi-v1-asr-online-test.cc 19 | kaldi_v1_asr_online_test_LDADD = $(KALDI_LIBS) \ 20 | $(MATRIX_LIBS) $(CUDA_LIBS) \ 21 | $(DL_LIBS) 22 | -------------------------------------------------------------------------------- /src/v1-asr/README: -------------------------------------------------------------------------------- 1 | 2 | 这是一个基于kaldi decoder的语音识别服务,已经接入能量vad和模型vad, 3 | 并且支持同时开启,详见v1-asrbin目录的可执行文件 4 | -------------------------------------------------------------------------------- /src/v1-asr/asr-source.cc: -------------------------------------------------------------------------------- 1 | 2 | #include "src/v1-asr/asr-source.h" 3 | 4 | namespace kaldi { 5 | std::string LatticeToString(const Lattice &lat, const fst::SymbolTable &word_syms) 6 | { 7 | LatticeWeight weight; 8 | std::vector alignment; 9 | std::vector words; 10 | GetLinearSymbolSequence(lat, &alignment, &words, &weight); 11 | 12 | std::ostringstream msg; 13 | for (size_t i = 0; i < words.size(); i++) 14 | { 15 | std::string s = word_syms.Find(words[i]); 16 | if (s.empty()) 17 | { 18 | KALDI_LOG << "Word-id " << words[i] << " not in symbol table."; 19 | msg << "<#" << std::to_string(i) << "> "; 20 | } else 21 | msg << s << " "; 22 | } 23 | KALDI_LOG << "Cost for utterance " << weight.Value1() << " + " << weight.Value2(); 24 | return msg.str(); 25 | } 26 | std::string GetTimeString(int32 t_beg, int32 t_end, BaseFloat time_unit) 27 | { 28 | char buffer[100]; 29 | double t_beg2 = t_beg * time_unit; 30 | double t_end2 = t_end * time_unit; 31 | snprintf(buffer, 100, "%.2f %.2f", t_beg2, t_end2); 32 | return std::string(buffer); 33 | } 34 | int32 GetLatticeTimeSpan(const Lattice& lat) 35 | { 36 | std::vector times; 37 | LatticeStateTimes(lat, ×); 38 | return times.back(); 39 | } 40 | std::string LatticeToString(const CompactLattice &clat, const fst::SymbolTable &word_syms) 41 | { 42 | if (clat.NumStates() == 0) { 43 | KALDI_LOG << "Empty lattice."; 44 | return ""; 45 | } 46 | CompactLattice best_path_clat; 47 | CompactLatticeShortestPath(clat, &best_path_clat); 48 | 49 | Lattice best_path_lat; 50 | ConvertLattice(best_path_clat, &best_path_lat); 51 | return LatticeToString(best_path_lat, word_syms); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/v1-asr/asr-work-thread.cc: -------------------------------------------------------------------------------- 1 | #include "src/v1-asr/asr-work-thread.h" 2 | 3 | #include "src/util/namespace-start.h" 4 | 5 | bool ASRWorkThread::InitASRSource(kaldi::ASROpts *asr_opts, kaldi::ASRSource *asr_source) 6 | { 7 | _decodable_info = new kaldi::nnet3::DecodableNnetSimpleLoopedInfo(asr_opts->_decodable_opts, 8 | (kaldi::nnet3::AmNnetSimple*)&(asr_source->_am_nnet)); 9 | 10 | _feature_info = new kaldi::OnlineNnet2FeaturePipelineInfo(asr_opts->_feature_opts); 11 | 12 | _asr_work = new kaldi::ASRWorker(asr_opts, asr_source, _decodable_info, _feature_info); 13 | return true; 14 | } 15 | /* 16 | void ASRWorkThread::Run() 17 | { 18 | pthread_t tid = GetThreadId(); 19 | // thread synchronization lock 20 | pthread_mutex_t *pthread_pool_mutex = _thread_pool->GetPthreadMutex(); 21 | // thread condition lock 22 | pthread_cond_t *pthread_pool_cond = _thread_pool->GetPthreadCond(); 23 | while(1) 24 | { 25 | pthread_mutex_lock(pthread_pool_mutex); 26 | while(_thread_pool->GetTaskSize() == 0 && !_thread_pool->Shutdown()) 27 | { 28 | if(_ready_ok == false) 29 | {// run ok 30 | _ready_ok = true; 31 | } 32 | LOG_COM << "Thread " << tid << " wait task."; 33 | pthread_cond_wait(pthread_pool_cond, pthread_pool_mutex); 34 | } 35 | if(_thread_pool->Shutdown()) 36 | { 37 | pthread_mutex_unlock(pthread_pool_mutex); 38 | LOG_COM << "Thread " << tid << " will exit."; 39 | pthread_exit(NULL); 40 | } 41 | LOG_COM << "tid " << tid << " run."; 42 | 43 | TaskBase *task = _thread_pool->GetTask(); 44 | 45 | _thread_pool->MoveToBusy(tid); // add busy list 46 | pthread_mutex_unlock(pthread_pool_mutex); 47 | 48 | if( 0 != task->Run((void*)this)) 49 | { 50 | LOG_WARN << "task run error!!!"; 51 | } 52 | delete task; 53 | 54 | pthread_mutex_lock(pthread_pool_mutex); 55 | _thread_pool->MoveToIdle(tid); // add idle list 56 | pthread_mutex_unlock(pthread_pool_mutex); 57 | } 58 | } 59 | */ 60 | #include "src/util/namespace-end.h" 61 | -------------------------------------------------------------------------------- /src/v1-asr/asr-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __ASR_WORK_THREAD_H__ 2 | #define __ASR_WORK_THREAD_H__ 3 | #include "src/v1-asr/asr-source.h" 4 | #include "src/service2/net-data-package.h" 5 | #include "src/service2/thread-pool-work-thread.h" 6 | #include "src/vad/energy-vad.h" 7 | 8 | #include "src/util/namespace-start.h" 9 | 10 | class ASRWorkThread:public ThreadPoolWorkThread 11 | { 12 | public: 13 | friend class ASRServiceTask; 14 | typedef ThreadBase::int32 int32; 15 | public: 16 | ASRWorkThread(ThreadPoolBase *tp, 17 | kaldi::ASROpts *asr_opts, kaldi::ASRSource *asr_source): 18 | ThreadPoolWorkThread(tp), 19 | _decodable_info(NULL),_feature_info(NULL), 20 | _asr_work(NULL), 21 | _energy_vad(16000, 0.025, 0.01, -1, EnergyVad::SIL, 0.3, 0.8, 50, 50, 32768*0.005, 32768*0.05, "sum_square_root") 22 | { 23 | InitASRSource(asr_opts, asr_source); 24 | } 25 | ~ASRWorkThread() 26 | { 27 | if(_asr_work != NULL) 28 | delete _asr_work; 29 | _asr_work = NULL; 30 | if(_decodable_info != NULL) 31 | delete _decodable_info; 32 | _decodable_info = NULL; 33 | if(_feature_info != NULL) 34 | delete _feature_info; 35 | _feature_info = NULL; 36 | } 37 | // void Run(); 38 | private: 39 | bool InitASRSource(kaldi::ASROpts *asr_opts, kaldi::ASRSource *asr_source); 40 | 41 | kaldi::nnet3::DecodableNnetSimpleLoopedInfo *_decodable_info; 42 | kaldi::OnlineNnet2FeaturePipelineInfo *_feature_info; 43 | 44 | // recv data from client 45 | C2SPackageAnalysis _ser_c2s_package_analysys; 46 | // send data to client 47 | S2CPackageAnalysis _ser_s2c_package_analysys; 48 | // feature 49 | // nnet 50 | // fst 51 | kaldi::ASRWorker *_asr_work; 52 | EnergyVad _energy_vad; 53 | }; 54 | 55 | #include "src/util/namespace-end.h" 56 | #endif 57 | -------------------------------------------------------------------------------- /src/v1-asr/kaldi-v1-asr-online-test.cc: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include "src/v1-asr/kaldi-v1-asr-online.h" 5 | 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | try 10 | { 11 | using namespace kaldi; 12 | const char *usage = "Test kaldi nnet3 decoder with vad code.\n" 13 | "./kaldi-v1-asr-online-test final.mdl fst words.txt scp:wav.scp"; 14 | ParseOptions po(usage); 15 | std::string vad_nnet_file=""; 16 | 17 | po.Register("vad-nnet-file", &vad_nnet_file, 18 | "vad model file name"); 19 | V1AsrOpts v1_asr_opts; 20 | v1_asr_opts.Register(&po); 21 | 22 | po.Read(argc, argv); 23 | if(po.NumArgs() != 4) 24 | { 25 | po.PrintUsage(); 26 | exit(1); 27 | } 28 | 29 | std::string nnet3_filename = po.GetArg(1); 30 | std::string fst_filename = po.GetArg(2), 31 | words_filename = po.GetArg(3), 32 | wav_rspecifier = po.GetArg(4); 33 | 34 | V1AsrSource v1_asr_source(v1_asr_opts, 35 | nnet3_filename, fst_filename, words_filename, vad_nnet_file); 36 | 37 | V1AsrWorker v1_asr_worker(v1_asr_opts, 38 | v1_asr_source); 39 | 40 | SequentialTableReader reader(wav_rspecifier); 41 | 42 | int32 num_utts = 0; 43 | for(; !reader.Done(); reader.Next()) 44 | { 45 | num_utts++; 46 | std::string utt = reader.Key(); 47 | const WaveData &wave_data = reader.Value(); 48 | SubVector waveform(wave_data.Data(), 0); 49 | v1_asr_worker.Init(true, 0); 50 | int32 nbest = 0; 51 | bool eos = true; 52 | std::vector nbest_result; 53 | std::string result = v1_asr_worker.Process(waveform, nbest_result, nbest, eos); 54 | KALDI_LOG << utt << " : " << result ; 55 | } 56 | return 0; 57 | } 58 | catch(const std::exception &e) 59 | { 60 | std::cerr << e.what(); 61 | return -1; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/v1-asr/v1-asr-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __V1_ASR_WORK_THREAD_H__ 2 | #define __V1_ASR_WORK_THREAD_H__ 3 | #include "src/v1-asr/kaldi-v1-asr-online.h" 4 | #include "src/service2/net-data-package.h" 5 | #include "src/service2/thread-pool-work-thread.h" 6 | //#include "src/vad/energy-vad.h" 7 | #include "src/online-vad/online-vad.h" 8 | 9 | #include "src/util/namespace-start.h" 10 | 11 | class V1ASRWorkThread:public ThreadPoolWorkThread 12 | { 13 | public: 14 | friend class V1ASRServiceTask; 15 | typedef ThreadBase::int32 int32; 16 | public: 17 | V1ASRWorkThread(ThreadPoolBase *tp, 18 | kaldi::V1AsrOpts &v1_asr_opts, kaldi::V1AsrSource &v1_asr_source): 19 | ThreadPoolWorkThread(tp), 20 | _v1_asr_worker(v1_asr_opts, v1_asr_source), 21 | _energy_vad(v1_asr_source._energy_vad_judge_opts, "sum_square_root", 16000, 0.025, 0.01, 0, 32768*0.005, 32768*0.05), 22 | _use_energy_vad(v1_asr_opts._use_energy_vad) 23 | { } 24 | 25 | ~V1ASRWorkThread() { } 26 | // void Run(); 27 | private: 28 | // recv data from client 29 | C2SPackageAnalysis _ser_c2s_package_analysys; 30 | // send data to client 31 | S2CPackageAnalysis _ser_s2c_package_analysys; 32 | // feature 33 | // nnet 34 | // fst 35 | kaldi::V1AsrWorker _v1_asr_worker; 36 | kaldi::V1EnergyVad _energy_vad; 37 | bool _use_energy_vad; 38 | }; 39 | 40 | #include "src/util/namespace-end.h" 41 | #endif 42 | -------------------------------------------------------------------------------- /src/v1-asrbin/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 8 | $(KALDI_DEPLDFLAGS) \ 9 | $(MATRIX_DEPLDFLAGS) 10 | 11 | LDADD = ../v1-asr/libv1asr.la \ 12 | ../service2/libservice2.la \ 13 | ../util/libutil.la 14 | 15 | LDADD += $(KALDI_LIBS) \ 16 | $(MATRIX_LIBS) \ 17 | $(CUDA_LIBS) \ 18 | -lm -lpthread $(DL_LIBS) 19 | 20 | if USA_CUDA_DECODER 21 | noinst_PROGRAMS = v1-asr-service \ 22 | asr-service 23 | else 24 | bin_PROGRAMS = v1-asr-service \ 25 | asr-service 26 | endif 27 | 28 | #thread_client_LDADD = ../service2/libservice2.la ../v2-asr/libv2asr.la ../util/libutil.la 29 | 30 | #thread_client_PROGRAMS = thread-client 31 | 32 | v1_asr_service_SOURCES = v1-asr-service.cc 33 | 34 | asr_service_SOURCES = asr-service.cc 35 | asr_service_LDADD = ../v1-asr/libv1asr.la \ 36 | $(LDADD) 37 | -------------------------------------------------------------------------------- /src/v1-asrbin/README: -------------------------------------------------------------------------------- 1 | 2 | # 将所需资源准备好,然后配置参数,运行v1-asr-service.sh脚本即可 3 | # 必须资源:final.mdl,HCLG.fst,words.txt 4 | # 如果使用模型vad,需要模型vad的final.mdl 5 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/conf.txt: -------------------------------------------------------------------------------- 1 | 2 | # config file 3 | #--nnet-vad-config=conf/nnet3-vad.conf 4 | #--energy-vad-config=conf/energy-vad.conf 5 | #--vad-model-filename=vad-nnet3-model/final.mdl 6 | #--fbank-config=conf/fbank.80.conf 7 | #--config-socket=conf/socket.conf 8 | 9 | 10 | # vad conf 11 | --use-realtime-vad=false 12 | #--use-model-vad=true 13 | #--use-energy-vad=false 14 | --decoder-use-final=false 15 | --min-sil-frames-interval=100 16 | 17 | 18 | # 19 | #--nthread=60 20 | --acoustic-scale=1.0 21 | 22 | #--verbose=3 23 | 24 | #service 25 | --use-endpoint=false 26 | 27 | 28 | #feature 29 | --add-pitch=false 30 | #--online-pitch-config="" 31 | --feature-type=fbank 32 | #--global-cmvn-stats="" 33 | 34 | 35 | 36 | #nnet 37 | #--computation.debug=true 38 | #--debug-computation=true 39 | --frames-per-chunk=150 40 | --extra-left-context-initial=0 41 | --frame-subsampling-factor=3 42 | 43 | #decoder 44 | --min-active=200 45 | --max-active=7000 46 | --beam=10.0 47 | --beam-delta=0.5 48 | --lattice-beam=7.0 49 | --determinize-lattice=true 50 | --delta=0.000976562 51 | --minimize=true 52 | --hash-ratio=2.0 53 | --phone-determinize=true 54 | --word-determinize=true 55 | --prune-interval=25 56 | 57 | #--endpoint 58 | --endpoint.silence-phones=1 59 | 60 | 61 | #--optimization.allow-left-merge 62 | #--optimization.allow-right-merge 63 | #--optimization.backprop-in-place 64 | #--optimization.consolidate-model-update 65 | #--optimization.convert-addition 66 | #--optimization.extend-matrices 67 | #--optimization.initialize-undefined 68 | #--optimization.max-deriv-time 69 | #--optimization.max-deriv-time-relative 70 | #--optimization.memory-compression-level 71 | 72 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/decoder.conf: -------------------------------------------------------------------------------- 1 | 2 | #--verbose=2 3 | 4 | --min-active=200 5 | --max-active=7000 6 | --beam=10.0 7 | --beam-delta=0.5 8 | --lattice-beam=7.0 9 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/energy-vad.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | --energy-vad-judge.audio2sil-sil-ratio=0.8 4 | --energy-vad-judge.start-sil=true 5 | --energy-vad-judge.end-sil=true 6 | --energy-vad-judge.left-context=30 7 | --energy-vad-judge.right-context=30 8 | 9 | --energy-vad-judge.sil2audio-audio-ratio=0.33 10 | --energy-vad-judge.smooth-version=v2 11 | 12 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/fbank.80.conf: -------------------------------------------------------------------------------- 1 | # No non-default options for now. 2 | #--sample-frequency=8000 3 | --num-mel-bins=80 4 | #--dither=0 5 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/nnet3-vad.conf: -------------------------------------------------------------------------------- 1 | 2 | --vad-nnet.frames-per-chunk=150 3 | --vad-nnet.frame-subsampling-factor=1 4 | --vad-nnet.acoustic-scale=1.0 5 | --vad-nnet.apply-exp=true 6 | 7 | #--use-priors=false 8 | #--verbose=1 9 | 10 | --nnet-vad-judge.audio2sil-sil-ratio=0.8 11 | --nnet-vad-judge.start-sil=true 12 | --nnet-vad-judge.end-sil=true 13 | --nnet-vad-judge.left-context=20 14 | --nnet-vad-judge.right-context=20 15 | --nnet-vad-judge.right-extra-context=20 16 | 17 | --nnet-vad-judge.sil2audio-audio-ratio=0.33 18 | --nnet-vad-judge.smooth-version=v2 19 | 20 | -------------------------------------------------------------------------------- /src/v1-asrbin/conf/socket.conf: -------------------------------------------------------------------------------- 1 | 2 | #--ip= 3 | --port=8100 4 | --rec-timeout=5000000 5 | --n-listen=15 6 | --keepalive=1 7 | --keeptime=5 8 | --keepinterval=1 9 | --keepcount=3 10 | -------------------------------------------------------------------------------- /src/v1-asrbin/v1-asr-service.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -x 4 | 5 | #./bin/v1-asr-service --config=conf/conf.txt \ 6 | # --use-energy-vad=false --energy-vad-config=conf/energy-vad.conf \ 7 | # --use-model-vad=true --nnet-vad-config=conf/nnet3-vad.conf \ 8 | # --vad-model-filename=vad-nnet3-model/final.mdl \ 9 | # --fbank-config=conf/fbank.80.conf \ 10 | # --config-socket=conf/socket.conf \ 11 | # final.mdl HCLG.fst words.txt 12 | 13 | 14 | # 使用模型vad 15 | ./v1-asr-service --config=conf/conf.txt \ 16 | --use-model-vad=false --nnet-vad-config=conf/nnet3-vad.conf \ 17 | --vad-model-filename=vad-nnet3-model/final.mdl \ 18 | --fbank-config=conf/fbank.80.conf \ 19 | --config-socket=conf/socket.conf \ 20 | --nthread=60 \ 21 | source/final.mdl source/HCLG.fst source/words.txt 22 | -------------------------------------------------------------------------------- /src/v2-asr/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | lib_LTLIBRARIES = libv2asr.la 8 | 9 | libv2asr_la_SOURCES = v2-asr-work-thread.cc 10 | libv2asr_la_LDFLAGS = -rpath '$(libdir)' 11 | -------------------------------------------------------------------------------- /src/v2-asr/README: -------------------------------------------------------------------------------- 1 | 2 | # 这是使用我自己实现的解码器和kaldi的nnet3前向计算完成的识别服务, 3 | # 服务代码使用的service2内的线程池接口 4 | # 这个服务和v1-asr是相同的,只是解码器部分使用自己实现的代码,并未使用openfst 5 | # 6 | # 7 | -------------------------------------------------------------------------------- /src/v2-asr/v2-asr-work-thread.cc: -------------------------------------------------------------------------------- 1 | #include "src/util/log-message.h" 2 | #include "src/v2-asr/v2-asr-work-thread.h" 3 | 4 | #include "src/util/namespace-start.h" 5 | 6 | void V2ASRWorkThread::Run() 7 | { 8 | pthread_t tid = GetThreadId(); 9 | // thread synchronization lock 10 | pthread_mutex_t *pthread_pool_mutex = _thread_pool->GetPthreadMutex(); 11 | // thread condition lock 12 | pthread_cond_t *pthread_pool_cond = _thread_pool->GetPthreadCond(); 13 | 14 | LOG_COM << "thread " << tid << " run"; 15 | while(1) 16 | { 17 | pthread_mutex_lock(pthread_pool_mutex); 18 | while(_thread_pool->GetTaskSize() == 0 && !_thread_pool->Shutdown()) 19 | { 20 | if(_ready_ok == false) 21 | {// run ok 22 | _ready_ok = true; 23 | } 24 | LOG_COM << "Thread " << tid << " wait task."; 25 | pthread_cond_wait(pthread_pool_cond, pthread_pool_mutex); 26 | } 27 | if(_thread_pool->Shutdown()) 28 | { 29 | pthread_mutex_unlock(pthread_pool_mutex); 30 | LOG_COM << "Thread " << tid << " will exit."; 31 | pthread_exit(NULL); 32 | } 33 | LOG_COM << "tid " << tid << " run."; 34 | 35 | TaskBase *task = _thread_pool->GetTask(); 36 | 37 | _thread_pool->MoveToBusy(tid); // add busy list 38 | pthread_mutex_unlock(pthread_pool_mutex); 39 | 40 | if(0 != task->Run((void*)this)) 41 | { 42 | LOG_WARN << "task run error!!!"; 43 | } 44 | delete task; 45 | WorkEfficiencyInfo(); 46 | 47 | pthread_mutex_lock(pthread_pool_mutex); 48 | _thread_pool->MoveToIdle(tid); // add idle list 49 | pthread_mutex_unlock(pthread_pool_mutex); 50 | } 51 | } 52 | 53 | #include "src/util/namespace-end.h" 54 | -------------------------------------------------------------------------------- /src/v2-asr/v2-asr-work-thread.h: -------------------------------------------------------------------------------- 1 | #ifndef __V2_ASR_WORK_THREAD_H__ 2 | #define __V2_ASR_WORK_THREAD_H__ 3 | #include "src/service2/thread-class.h" 4 | #include "src/service2/thread-pool.h" 5 | #include "src/service2/net-data-package.h" 6 | #include "src/service2/thread-info.h" 7 | 8 | #include "src/vad/energy-vad.h" 9 | 10 | #include "src/kaldi-nnet3/kaldi-online-nnet3-my-decoder.h" 11 | 12 | #include "src/util/namespace-start.h" 13 | 14 | /// This is class use kaldi feature , nnet and mydecoder 15 | /// package asr service class work thread. 16 | class V2ASRWorkThread:public ThreadBase, public ThreadTimeInfo 17 | { 18 | public: 19 | friend class V2ASRServiceTask; 20 | typedef ThreadBase::int32 int32; 21 | public: 22 | 23 | // 24 | V2ASRWorkThread(ThreadPoolBase *thread_pool, 25 | OnlineDecoderInfo &online_info, int32 index=-1): 26 | ThreadBase(index), 27 | _thread_pool(thread_pool), 28 | _online_clg_decoder(online_info), 29 | _energy_vad(16000, 0.025, 0.01, -1, EnergyVad::SIL, 0.3, 0.8, 50, 50, 32768*0.005, 32768*0.05, "sum_square_root") { } 30 | 31 | ~V2ASRWorkThread() { } 32 | void Run(); 33 | void TimeInfo() { WorkEfficiencyInfo(); } 34 | void SetTime(double data_time, double work_time, double nosil_time = -1) 35 | { 36 | _data_time += data_time; 37 | _work_time += work_time; 38 | if(nosil_time < 0) 39 | nosil_time = data_time; 40 | _effect_data_time += nosil_time; 41 | _thread_pool->SetTime(data_time, work_time, nosil_time); 42 | } 43 | private: 44 | double GetEfficiency() 45 | { 46 | if(_data_time == 0) 47 | return -1; 48 | return _work_time/_data_time; 49 | } 50 | 51 | void WorkEfficiencyInfo() 52 | { 53 | LOG_COM << "ThreadIndex = " << GetThreadIndex() 54 | << " -> wav data time is : " << _data_time; 55 | LOG_COM << "ThreadIndex = " << GetThreadIndex() 56 | << " -> nosil data time is : " << _effect_data_time; 57 | LOG_COM << "ThreadIndex = " << GetThreadIndex() 58 | << " -> work time is : " << _work_time; 59 | LOG_COM << "ThreadIndex = " << GetThreadIndex() 60 | << " -> WorkEfficiencyInfo is rt : " << GetEfficiency(); 61 | } 62 | 63 | 64 | ThreadPoolBase *_thread_pool; 65 | // online decoder instance 66 | OnlineClgLatticeFastDecoder _online_clg_decoder; 67 | 68 | // vad 69 | EnergyVad _energy_vad; 70 | // recv data from client 71 | C2SPackageAnalysis _ser_c2s_package_analysys; 72 | // send data to client 73 | S2CPackageAnalysis _ser_s2c_package_analysys; 74 | }; 75 | 76 | #include "src/util/namespace-end.h" 77 | #endif 78 | -------------------------------------------------------------------------------- /src/v2-asrbin/Makefile.am: -------------------------------------------------------------------------------- 1 | include ../depend.mk 2 | AM_CPPFLAGS = -I$(CUDA_INC) \ 3 | -I$(KALDI_INC) \ 4 | -I$(MATRIX_INC) \ 5 | -I$(OPENFST_INC) 6 | 7 | AM_LDFLAGS = $(CUDA_DEPLDFLAGS) \ 8 | $(KALDI_DEPLDFLAGS) \ 9 | $(MATRIX_DEPLDFLAGS) 10 | 11 | LDADD = ../v2-asr/libv2asr.la \ 12 | ../kaldi-nnet3/libonlinedecode.la \ 13 | ../service2/libservice2.la \ 14 | ../my-decoder/liboptimizedecode.la \ 15 | ../newlm/liblm.la \ 16 | ../newfst/liblatfst.la \ 17 | ../util/libutil.la 18 | 19 | # ../newlm/liblm.la 20 | 21 | LDADD += $(KALDI_LIBS) \ 22 | $(MATRIX_LIBS) \ 23 | $(CUDA_LIBS) \ 24 | -lm -lpthread $(DL_LIBS) 25 | 26 | if USA_CUDA_DECODER 27 | bin_PROGRAMS = 28 | noinst_PROGRAMS = v2-asr-service 29 | 30 | else 31 | bin_PROGRAMS = v2-asr-service 32 | 33 | noinst_PROGRAMS = 34 | 35 | endif 36 | 37 | v2_asr_service_SOURCES = v2-asr-service.cc 38 | -------------------------------------------------------------------------------- /src/v2-asrbin/README: -------------------------------------------------------------------------------- 1 | 2 | # This is asr service and client programs. 3 | # v2-asr-service is call my decoder and kaldi nnet3 feat. 4 | # asr-service is kaldi nnet3 feat and decoder and my socket and threadpool. 5 | # You can use kaldi HCLG.fst and final.mdl test v2-asr-service and asr-service. 6 | # if you want use clg.fst, you must be reconsitution fst. 7 | # kaldi decoder 8 | ./asr-service --config=conf/conf.txt final.mdl HCLG.fst words.txt 9 | 10 | # my decoder 11 | ./v2-asr-service --config=conf/v2-conf.txt final.mdl HCLG.fst words.txt 12 | -------------------------------------------------------------------------------- /src/v2-asrbin/conf/conf.txt: -------------------------------------------------------------------------------- 1 | 2 | --config-socket=conf/socket.conf 3 | --nthread=60 4 | --acoustic-scale=1.0 5 | 6 | --use-energy-vad=true 7 | #--verbose=1 8 | 9 | #service 10 | --output-period=1 11 | --use-endpoint=false 12 | --produce-time=false 13 | 14 | 15 | #feature 16 | --add-pitch=false 17 | #--online-pitch-config="" 18 | --feature-type=fbank 19 | --fbank-config=fbank.80.conf 20 | #--global-cmvn-stats="" 21 | --samp-freq=16000 22 | 23 | 24 | 25 | #nnet 26 | #--computation.debug=true 27 | #--debug-computation=true 28 | --chunk-length=1 29 | --frames-per-chunk=150 30 | --extra-left-context-initial=0 31 | --frame-subsampling-factor=3 32 | 33 | #decoder 34 | --min-active=200 35 | --max-active=7000 36 | --beam=10.0 37 | --beam-delta=0.5 38 | --lattice-beam=7.0 39 | --determinize-lattice=true 40 | --delta=0.000976562 41 | --minimize=true 42 | --hash-ratio=2.0 43 | --phone-determinize=true 44 | --word-determinize=true 45 | --prune-interval=25 46 | 47 | #--endpoint 48 | --endpoint.silence-phones=1 49 | 50 | 51 | #--optimization.allow-left-merge 52 | #--optimization.allow-right-merge 53 | #--optimization.backprop-in-place 54 | #--optimization.consolidate-model-update 55 | #--optimization.convert-addition 56 | #--optimization.extend-matrices 57 | #--optimization.initialize-undefined 58 | #--optimization.max-deriv-time 59 | #--optimization.max-deriv-time-relative 60 | #--optimization.memory-compression-level 61 | 62 | -------------------------------------------------------------------------------- /src/v2-asrbin/conf/decoder.conf: -------------------------------------------------------------------------------- 1 | 2 | #--verbose=2 3 | 4 | --min-active=200 5 | --max-active=7000 6 | --beam=10.0 7 | --beam-delta=0.5 8 | --lattice-beam=7.0 9 | -------------------------------------------------------------------------------- /src/v2-asrbin/conf/fbank.80.conf: -------------------------------------------------------------------------------- 1 | # No non-default options for now. 2 | #--sample-frequency=8000 3 | --num-mel-bins=80 4 | #--dither=0 5 | -------------------------------------------------------------------------------- /src/v2-asrbin/conf/socket.conf: -------------------------------------------------------------------------------- 1 | 2 | #--ip= 3 | --port=8100 4 | --rec-timeout=5000000 5 | --n-listen=15 6 | --keepalive=1 7 | --keeptime=5 8 | --keepinterval=1 9 | --keepcount=3 10 | -------------------------------------------------------------------------------- /src/v2-asrbin/conf/v2-conf.txt: -------------------------------------------------------------------------------- 1 | 2 | --config-socket=conf/socket.conf 3 | --config-decoder=conf/decoder.conf 4 | --nthread=60 5 | 6 | --acoustic-scale=1.0 7 | #--hmmfst-file=9-2-source/clg-epsnumber/hmm.tid.fst 8 | --graph-type=mem-hclg 9 | 10 | --constfst=true 11 | 12 | --use-energy-vad=true 13 | 14 | #clg,hclg,biglm-hclg,mem-hclg 15 | #--lm1-file=9-2-source/lm/fsa.1e-9 16 | #--lm2-file=9-2-source/lm/fsa.1e-9 17 | --graph-type=mem-hclg 18 | #--lm1-scale=-1.0 19 | #--lm2-scale=1.0 20 | 21 | --acoustic-scale=1.0 22 | #--verbose=2 23 | 24 | #feature 25 | --cmvn-config= 26 | --add-pitch=false 27 | --online-pitch-config= 28 | --feature-type=fbank 29 | --fbank-config=fbank.80.conf 30 | --global-cmvn-stats= 31 | --ivector-extraction-config= 32 | 33 | #nnet 34 | #--computation.debug=true 35 | #--debug-computation=true 36 | --frames-per-chunk=150 37 | --extra-left-context-initial=0 38 | --frame-subsampling-factor=3 39 | 40 | #--endpoint 41 | #--endpoint.silence-phones=1 42 | 43 | #--optimization.allow-left-merge 44 | #--optimization.allow-right-merge 45 | #--optimization.backprop-in-place 46 | #--optimization.consolidate-model-update 47 | #--optimization.convert-addition 48 | #--optimization.extend-matrices 49 | #--optimization.initialize-undefined 50 | #--optimization.max-deriv-time 51 | #--optimization.max-deriv-time-relative 52 | #--optimization.memory-compression-level 53 | 54 | -------------------------------------------------------------------------------- /src/vad/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = 2 | 3 | lib_LTLIBRARIES = 4 | 5 | 6 | LDADD = ../util/libutil.la 7 | 8 | check_PROGRAMS = test-energy-vad vad-test 9 | 10 | test_energy_vad_SOURCES = test-energy-vad.cc 11 | 12 | vad_test_SOURCES = vad-test.cc 13 | 14 | -------------------------------------------------------------------------------- /src/wakeup/dtw.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 3 | * 4 | * File Type : C++ Header File(*.h) 5 | * File Name : dtw.h 6 | * Module : 7 | * Create on :2015/12/21 8 | * Author :hubo 9 | * 10 | * DTW(Dynamic Time Warping) is a DP algorithm. At here it's used alignment. 11 | * */ 12 | 13 | 14 | #ifndef __DTW_H__ 15 | #define __DTW_H__ 16 | 17 | #include 18 | #include 19 | 20 | #define DTWINF (1.0e30) 21 | 22 | struct Align_res 23 | { 24 | int frame; 25 | int state; // it's col 26 | float total_score; 27 | Align_res():frame(0),state(0),total_score(0.0){} 28 | }; 29 | 30 | class DtwAlign 31 | { 32 | public: 33 | 34 | DtwAlign(int rows,int cols): dtw_(NULL),dtw_total_(NULL),align_(NULL), 35 | dtw_rows_(rows),dtw_cols_(cols),start_location_(0),end_location_(0),total_load_rows_(0),cur_cols_(0){ } 36 | 37 | ~DtwAlign(){ 38 | if(dtw_ != NULL){delete[]dtw_;dtw_=NULL;} 39 | if(dtw_total_ != NULL){delete[]dtw_total_;dtw_total_=NULL;} 40 | if(align_ != NULL){delete[]align_;align_=NULL;} 41 | dtw_rows_=0,dtw_cols_=0; 42 | start_location_=0,end_location_=0; 43 | total_load_rows_=0,cur_cols_=0; 44 | } 45 | /* 46 | * initialize dtw space 47 | * state:state sequence 48 | * cols: state sequence length 49 | * */ 50 | int Init(); 51 | /* 52 | * reset dtw 53 | * */ 54 | void Reset(); 55 | /* 56 | * load data to dtw space 57 | * */ 58 | int loadData2Dtw(float *data,int rows,int cols); 59 | 60 | /* 61 | * set dtw data ,once set one data. 62 | * */ 63 | void setData2Dtw(float data); 64 | 65 | /* 66 | * start computer align 67 | * */ 68 | Align_res* startAlign(int *res_len); 69 | 70 | /* 71 | * the other way for align 72 | * */ 73 | Align_res* StartAlign(int *res_len); 74 | 75 | int getStateDim(){return dtw_cols_;} 76 | 77 | int GetCols() {return dtw_cols_;} 78 | 79 | int GetRows() {return dtw_rows_;} 80 | 81 | int GetAlignLength() { return (dtw_rows_+dtw_cols_-1);} 82 | 83 | private: 84 | float *dtw_; //dtw_ be recycled 85 | float *dtw_total_; 86 | Align_res *align_; 87 | int dtw_rows_; //dtw row ,it's time 88 | int dtw_cols_; //dtw col ,it's state 89 | int start_location_; //dtw_ start location 90 | int end_location_; //dtw_ end location 91 | int total_load_rows_;//record total send in data rows ,increment 92 | int cur_cols_; //record current location 93 | }; 94 | 95 | struct Align_State 96 | { 97 | int state; 98 | int start; 99 | int end; 100 | float state_score; 101 | float state_average_score; 102 | Align_State():start(0),end(0),state_score(0.0),state_average_score(0){} 103 | void Print() 104 | { 105 | std::cout << state << " " << start << " " << end << " " << state_average_score << std::endl; 106 | } 107 | }; 108 | 109 | void judge_align_res(Align_State *align_word,Align_res *ali,int ali_len,int *states,int num); 110 | #endif 111 | -------------------------------------------------------------------------------- /src/wakeup/wakeup-search.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017-2018 3 | * 4 | * File Type : C++ Source File(*.cpp) 5 | * File Name : wakeup-search.h 6 | * Module : 7 | * Create on : 2018/1/24 8 | * Author : hubo 9 | * 10 | * This class is used wake up search . 11 | * */ 12 | 13 | 14 | #ifndef __WAKEUP_SEARCH_H__ 15 | #define __WAKEUP_SEARCH_H__ 16 | 17 | #include 18 | #include 19 | #include "dtw.h" 20 | 21 | using namespace std; 22 | 23 | class WakeupSearch 24 | { 25 | public: 26 | WakeupSearch(bool adjust=false):adjust_(adjust),cur_frame_(0),max_frame_len_(0), dtw_(NULL) {} 27 | ~WakeupSearch() 28 | { 29 | delete dtw_; 30 | } 31 | 32 | void Init(vector &states, vector &weights, int max_frame_len) 33 | { 34 | assert(states.size() == weights.size()); 35 | int state_len = states.size(); 36 | states_.resize(state_len); 37 | weight_.resize(state_len); 38 | align_states_.resize(state_len); 39 | for(int i=0;iInit(); 48 | max_frame_len_ = max_frame_len; 49 | } 50 | // add data 51 | void InputDataOneFrame(float *data, int cols); 52 | 53 | float ProcessData(float *data, int rows, int cols); 54 | 55 | float JudgeWakeup(int &start, int &end); 56 | // get state align res 57 | Align_res* GetAliState(); 58 | 59 | void PrintAliState() 60 | { 61 | for(int i = 0 ; i < states_.size(); ++i) 62 | { 63 | align_states_[i].Print(); 64 | } 65 | } 66 | 67 | void Reset() 68 | { 69 | dtw_->Reset(); 70 | cur_frame_ = 0; 71 | } 72 | private: 73 | 74 | bool adjust_; 75 | int cur_frame_; 76 | int max_frame_len_; 77 | 78 | vector states_; // state list 79 | vector weight_; // state weight 80 | //DP(dtw) need space and source 81 | DtwAlign *dtw_; 82 | vector align_states_; 83 | }; 84 | 85 | 86 | #endif 87 | --------------------------------------------------------------------------------