diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99461947..6afa7c9c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: entry: yapf args: [-i, -vv] types: [python] - exclude: (?=speechx/speechx/kaldi|audio/paddleaudio/src|third_party).*(\.cpp|\.cc|\.h\.hpp|\.py)$ + exclude: (?=runtime/engine/kaldi|audio/paddleaudio/src|third_party).*(\.cpp|\.cc|\.h\.hpp|\.py)$ - repo: https://github.com/pre-commit/pre-commit-hooks rev: a11d9314b22d8f8c7556443875b731ef05965464 @@ -35,7 +35,7 @@ repos: - --ignore=E501,E228,E226,E261,E266,E128,E402,W503 - --builtins=G,request - --jobs=1 - exclude: (?=speechx/speechx/kaldi|audio/paddleaudio/src|third_party).*(\.cpp|\.cc|\.h\.hpp|\.py)$ + exclude: (?=runtime/engine/kaldi|audio/paddleaudio/src|third_party).*(\.cpp|\.cc|\.h\.hpp|\.py)$ - repo : https://github.com/Lucas-C/pre-commit-hooks rev: v1.0.1 @@ -57,16 +57,16 @@ repos: entry: bash .pre-commit-hooks/clang-format.hook -i language: system files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ - exclude: (?=speechx/speechx/kaldi|audio/paddleaudio/src|speechx/patch|speechx/tools/fstbin|speechx/tools/lmbin|third_party/ctc_decoders|speechx/speechx/common/utils).*(\.cpp|\.cc|\.h|\.hpp|\.py)$ + exclude: (?=runtime/engine/kaldi|audio/paddleaudio/src|runtime/patch|runtime/tools/fstbin|runtime/tools/lmbin|third_party/ctc_decoders|runtime/engine/common/utils).*(\.cpp|\.cc|\.h|\.hpp|\.py)$ - id: cpplint name: cpplint description: Static code analysis of C/C++ files language: python files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ - exclude: (?=speechx/speechx/kaldi|audio/paddleaudio/src|speechx/patch|speechx/tools/fstbin|speechx/tools/lmbin|third_party/ctc_decoders|speechx/speechx/common/utils).*(\.cpp|\.cc|\.h|\.hpp|\.py)$ + exclude: (?=runtime/engine/kaldi|runtime/engine/common/matrix|audio/paddleaudio/src|runtime/patch|runtime/tools/fstbin|runtime/tools/lmbin|third_party/ctc_decoders|runtime/engine/common/utils).*(\.cpp|\.cc|\.h|\.hpp|\.py)$ entry: cpplint --filter=-build,-whitespace,+whitespace/comma,-whitespace/indent - repo: https://github.com/asottile/reorder_python_imports rev: v2.4.0 hooks: - id: reorder-python-imports - exclude: (?=speechx/speechx/kaldi|audio/paddleaudio/src|speechx/patch|speechx/tools/fstbin|speechx/tools/lmbin|third_party/ctc_decoders).*(\.cpp|\.cc|\.h\.hpp|\.py)$ + exclude: (?=runtime/engine/kaldi|audio/paddleaudio/src|runtime/patch|runtime/tools/fstbin|runtime/tools/lmbin|third_party/ctc_decoders).*(\.cpp|\.cc|\.h\.hpp|\.py)$ diff --git a/README.md b/README.md index dbdf6a4f..0a12ec04 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ Via the easy-to-use, efficient, flexible and scalable implementation, our vision - 👑 2022.11.18: Add [Whisper CLI and Demos](https://github.com/PaddlePaddle/PaddleSpeech/pull/2640), support multi language recognition and translation. - 🔥 2022.11.18: Add [Wav2vec2 CLI and Demos](https://github.com/PaddlePaddle/PaddleSpeech/blob/develop/demos/speech_ssl), Support ASR and Feature Extraction. - 🎉 2022.11.17: Add [male voice for TTS](https://github.com/PaddlePaddle/PaddleSpeech/pull/2660). -- 🔥 2022.11.07: Add [U2/U2++ C++ High Performance Streaming ASR Deployment](https://github.com/PaddlePaddle/PaddleSpeech/blob/develop/speechx/examples/u2pp_ol/wenetspeech). +- 🔥 2022.11.07: Add [U2/U2++ C++ High Performance Streaming ASR Deployment](https://github.com/PaddlePaddle/PaddleSpeech/blob/develop/runtime/examples/u2pp_ol/wenetspeech). - 👑 2022.11.01: Add [Adversarial Loss](https://arxiv.org/pdf/1907.04448.pdf) for [Chinese English mixed TTS](./examples/zh_en_tts/tts3). - 🔥 2022.10.26: Add [Prosody Prediction](https://github.com/PaddlePaddle/PaddleSpeech/tree/develop/examples/other/rhy) for TTS. - 🎉 2022.10.21: Add [SSML](https://github.com/PaddlePaddle/PaddleSpeech/discussions/2538) for TTS Chinese Text Frontend. diff --git a/speechx/.clang-format b/runtime/.clang-format similarity index 100% rename from speechx/.clang-format rename to runtime/.clang-format diff --git a/speechx/.gitignore b/runtime/.gitignore similarity index 65% rename from speechx/.gitignore rename to runtime/.gitignore index 9a93805c..0783b138 100644 --- a/speechx/.gitignore +++ b/runtime/.gitignore @@ -1,2 +1,3 @@ tools/valgrind* *log +fc_patch/* diff --git a/speechx/CMakeLists.txt b/runtime/CMakeLists.txt similarity index 92% rename from speechx/CMakeLists.txt rename to runtime/CMakeLists.txt index d056ebbc..8bd3f28c 100644 --- a/speechx/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -93,7 +93,7 @@ endif() # paddle libpaddle.so # paddle include and link option -# -L/workspace/DeepSpeech-2.x/speechx/venv/lib/python3.7/site-packages/paddle/libs -L/workspace/DeepSpeech-2.x/speechx/venv/lib/python3.7/site-packages/paddle/fluid -l:libpaddle.so -l:libdnnl.so.2 -l:libiomp5.so +# -L/workspace/DeepSpeech-2.x/engine/venv/lib/python3.7/site-packages/paddle/libs -L/workspace/DeepSpeech-2.x/speechx/venv/lib/python3.7/site-packages/paddle/fluid -l:libpaddle.so -l:libdnnl.so.2 -l:libiomp5.so execute_process( COMMAND python -c "\ import os;\ @@ -112,7 +112,7 @@ message(STATUS PADDLE_LINK_FLAGS= ${PADDLE_LINK_FLAGS}) string(STRIP ${PADDLE_LINK_FLAGS} PADDLE_LINK_FLAGS) # paddle compile option -# -I/workspace/DeepSpeech-2.x/speechx/venv/lib/python3.7/site-packages/paddle/include +# -I/workspace/DeepSpeech-2.x/engine/venv/lib/python3.7/site-packages/paddle/include execute_process( COMMAND python -c "\ import paddle; \ @@ -143,6 +143,6 @@ message(STATUS PADDLE_LIB_DIRS= ${PADDLE_LIB_DIRS}) ############################################################################### # Add local library ############################################################################### -set(SPEECHX_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/speechx) +set(ENGINE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/engine) -add_subdirectory(speechx) +add_subdirectory(engine) diff --git a/speechx/README.md b/runtime/README.md similarity index 96% rename from speechx/README.md rename to runtime/README.md index 70136ea0..40aa9444 100644 --- a/speechx/README.md +++ b/runtime/README.md @@ -1,4 +1,3 @@ -# SpeechX -- All in One Speech Task Inference ## Environment @@ -9,7 +8,7 @@ We develop under: * gcc/g++/gfortran - 8.2.0 * cmake - 3.16.0 -> Please use `tools/env.sh` to create python `venv`, then `source venv/bin/activate` to build speechx. +> Please use `tools/env.sh` to create python `venv`, then `source venv/bin/activate` to build engine. > We make sure all things work fun under docker, and recommend using it to develop and deploy. @@ -33,7 +32,7 @@ docker run --privileged --net=host --ipc=host -it --rm -v /path/to/paddlespeech bash tools/venv.sh ``` -2. Build `speechx` and `examples`. +2. Build `engine` and `examples`. For now we are using feature under `develop` branch of paddle, so we need to install `paddlepaddle` nightly build version. For example: diff --git a/speechx/build.sh b/runtime/build.sh similarity index 100% rename from speechx/build.sh rename to runtime/build.sh diff --git a/speechx/cmake/EnableCMP0048.cmake b/runtime/cmake/EnableCMP0048.cmake similarity index 100% rename from speechx/cmake/EnableCMP0048.cmake rename to runtime/cmake/EnableCMP0048.cmake diff --git a/speechx/cmake/FindGFortranLibs.cmake b/runtime/cmake/FindGFortranLibs.cmake similarity index 100% rename from speechx/cmake/FindGFortranLibs.cmake rename to runtime/cmake/FindGFortranLibs.cmake diff --git a/speechx/cmake/absl.cmake b/runtime/cmake/absl.cmake similarity index 100% rename from speechx/cmake/absl.cmake rename to runtime/cmake/absl.cmake diff --git a/speechx/cmake/boost.cmake b/runtime/cmake/boost.cmake similarity index 100% rename from speechx/cmake/boost.cmake rename to runtime/cmake/boost.cmake diff --git a/speechx/cmake/eigen.cmake b/runtime/cmake/eigen.cmake similarity index 100% rename from speechx/cmake/eigen.cmake rename to runtime/cmake/eigen.cmake diff --git a/speechx/cmake/gflags.cmake b/runtime/cmake/gflags.cmake similarity index 61% rename from speechx/cmake/gflags.cmake rename to runtime/cmake/gflags.cmake index 36bebc87..d01eaf60 100644 --- a/speechx/cmake/gflags.cmake +++ b/runtime/cmake/gflags.cmake @@ -2,10 +2,10 @@ include(FetchContent) FetchContent_Declare( gflags - URL https://github.com/gflags/gflags/archive/v2.2.2.zip + URL https://paddleaudio.bj.bcebos.com/build/gflag-2.2.2.zip URL_HASH SHA256=19713a36c9f32b33df59d1c79b4958434cb005b5b47dc5400a7a4b078111d9b5 ) FetchContent_MakeAvailable(gflags) # openfst need -include_directories(${gflags_BINARY_DIR}/include) \ No newline at end of file +include_directories(${gflags_BINARY_DIR}/include) diff --git a/speechx/cmake/glog.cmake b/runtime/cmake/glog.cmake similarity index 77% rename from speechx/cmake/glog.cmake rename to runtime/cmake/glog.cmake index dcfd86c3..8cc9999b 100644 --- a/speechx/cmake/glog.cmake +++ b/runtime/cmake/glog.cmake @@ -1,7 +1,7 @@ include(FetchContent) FetchContent_Declare( glog - URL https://github.com/google/glog/archive/v0.4.0.zip + URL https://paddleaudio.bj.bcebos.com/build/glog-0.4.0.zip URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc ) FetchContent_MakeAvailable(glog) diff --git a/speechx/cmake/gtest.cmake b/runtime/cmake/gtest.cmake similarity index 76% rename from speechx/cmake/gtest.cmake rename to runtime/cmake/gtest.cmake index 365f25cf..f3e72d26 100644 --- a/speechx/cmake/gtest.cmake +++ b/runtime/cmake/gtest.cmake @@ -2,7 +2,7 @@ include(FetchContent) FetchContent_Declare( gtest - URL https://github.com/google/googletest/archive/release-1.11.0.zip + URL https://paddleaudio.bj.bcebos.com/build/gtest-release-1.11.0.zip URL_HASH SHA256=353571c2440176ded91c2de6d6cd88ddd41401d14692ec1f99e35d013feda55a ) FetchContent_MakeAvailable(gtest) @@ -12,4 +12,4 @@ include_directories(${gtest_BINARY_DIR} ${gtest_SOURCE_DIR}/src) if(WITH_TESTING) enable_testing() -endif() \ No newline at end of file +endif() diff --git a/speechx/cmake/kenlm.cmake b/runtime/cmake/kenlm.cmake similarity index 100% rename from speechx/cmake/kenlm.cmake rename to runtime/cmake/kenlm.cmake diff --git a/speechx/cmake/libsndfile.cmake b/runtime/cmake/libsndfile.cmake similarity index 100% rename from speechx/cmake/libsndfile.cmake rename to runtime/cmake/libsndfile.cmake diff --git a/speechx/cmake/openblas.cmake b/runtime/cmake/openblas.cmake similarity index 100% rename from speechx/cmake/openblas.cmake rename to runtime/cmake/openblas.cmake diff --git a/speechx/cmake/openfst.cmake b/runtime/cmake/openfst.cmake similarity index 96% rename from speechx/cmake/openfst.cmake rename to runtime/cmake/openfst.cmake index 8861f4f4..2e2f82f2 100644 --- a/speechx/cmake/openfst.cmake +++ b/runtime/cmake/openfst.cmake @@ -1,8 +1,8 @@ -include(FetchContent) set(openfst_PREFIX_DIR ${fc_patch}/openfst) set(openfst_SOURCE_DIR ${fc_patch}/openfst-src) set(openfst_BINARY_DIR ${fc_patch}/openfst-build) +include(FetchContent) # openfst Acknowledgments: #Cyril Allauzen, Michael Riley, Johan Schalkwyk, Wojciech Skut and Mehryar Mohri, #"OpenFst: A General and Efficient Weighted Finite-State Transducer Library", @@ -25,5 +25,7 @@ ExternalProject_Add(openfst ) link_directories(${openfst_PREFIX_DIR}/lib) include_directories(${openfst_PREFIX_DIR}/include) + + message(STATUS "OpenFST inc dir: ${openfst_PREFIX_DIR}/include") -message(STATUS "OpenFST lib dir: ${openfst_PREFIX_DIR}/lib") \ No newline at end of file +message(STATUS "OpenFST lib dir: ${openfst_PREFIX_DIR}/lib") diff --git a/speechx/cmake/paddleinference.cmake b/runtime/cmake/paddleinference.cmake similarity index 100% rename from speechx/cmake/paddleinference.cmake rename to runtime/cmake/paddleinference.cmake diff --git a/speechx/cmake/system.cmake b/runtime/cmake/system.cmake similarity index 100% rename from speechx/cmake/system.cmake rename to runtime/cmake/system.cmake diff --git a/speechx/docker/.gitkeep b/runtime/docker/.gitkeep similarity index 100% rename from speechx/docker/.gitkeep rename to runtime/docker/.gitkeep diff --git a/speechx/speechx/CMakeLists.txt b/runtime/engine/CMakeLists.txt similarity index 100% rename from speechx/speechx/CMakeLists.txt rename to runtime/engine/CMakeLists.txt diff --git a/speechx/speechx/asr/CMakeLists.txt b/runtime/engine/asr/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/CMakeLists.txt rename to runtime/engine/asr/CMakeLists.txt diff --git a/speechx/speechx/asr/decoder/CMakeLists.txt b/runtime/engine/asr/decoder/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/decoder/CMakeLists.txt rename to runtime/engine/asr/decoder/CMakeLists.txt diff --git a/speechx/speechx/asr/decoder/common.h b/runtime/engine/asr/decoder/common.h similarity index 100% rename from speechx/speechx/asr/decoder/common.h rename to runtime/engine/asr/decoder/common.h diff --git a/speechx/speechx/asr/decoder/ctc_beam_search_opt.h b/runtime/engine/asr/decoder/ctc_beam_search_opt.h similarity index 100% rename from speechx/speechx/asr/decoder/ctc_beam_search_opt.h rename to runtime/engine/asr/decoder/ctc_beam_search_opt.h diff --git a/speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder.cc b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc similarity index 99% rename from speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder.cc rename to runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc index 8361f06d..3e3ca2c2 100644 --- a/speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder.cc +++ b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.cc @@ -63,9 +63,7 @@ void CTCPrefixBeamSearch::Reset() { times_.emplace_back(empty); } -void CTCPrefixBeamSearch::InitDecoder() { - Reset(); -} +void CTCPrefixBeamSearch::InitDecoder() { Reset(); } void CTCPrefixBeamSearch::AdvanceDecode( const std::shared_ptr& decodable) { diff --git a/speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder.h b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.h similarity index 100% rename from speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder.h rename to runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder.h diff --git a/speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder_main.cc b/runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder_main.cc similarity index 100% rename from speechx/speechx/asr/decoder/ctc_prefix_beam_search_decoder_main.cc rename to runtime/engine/asr/decoder/ctc_prefix_beam_search_decoder_main.cc diff --git a/speechx/speechx/asr/decoder/ctc_prefix_beam_search_score.h b/runtime/engine/asr/decoder/ctc_prefix_beam_search_score.h similarity index 100% rename from speechx/speechx/asr/decoder/ctc_prefix_beam_search_score.h rename to runtime/engine/asr/decoder/ctc_prefix_beam_search_score.h diff --git a/speechx/speechx/asr/decoder/ctc_tlg_decoder.cc b/runtime/engine/asr/decoder/ctc_tlg_decoder.cc similarity index 97% rename from speechx/speechx/asr/decoder/ctc_tlg_decoder.cc rename to runtime/engine/asr/decoder/ctc_tlg_decoder.cc index ca7d65c8..ac30da92 100644 --- a/speechx/speechx/asr/decoder/ctc_tlg_decoder.cc +++ b/runtime/engine/asr/decoder/ctc_tlg_decoder.cc @@ -29,6 +29,11 @@ TLGDecoder::TLGDecoder(TLGDecoderOptions opts) : opts_(opts) { void TLGDecoder::Reset() { decoder_->InitDecoding(); + hypotheses_.clear(); + likelihood_.clear(); + olabels_.clear(); + times_.clear(); + num_frame_decoded_ = 0; return; } @@ -103,7 +108,7 @@ void TLGDecoder::FinalizeSearch() { time.push_back(idx); // fake time, todo later hypotheses_.push_back(hypothese); times_.push_back(time); - olabels.push_back(words_id); + olabels_.push_back(words_id); likelihood_.push_back(-(weight.Value2() + weight.Value1())); } } diff --git a/speechx/speechx/asr/decoder/ctc_tlg_decoder.h b/runtime/engine/asr/decoder/ctc_tlg_decoder.h similarity index 95% rename from speechx/speechx/asr/decoder/ctc_tlg_decoder.h rename to runtime/engine/asr/decoder/ctc_tlg_decoder.h index 1ea6d634..4540bc46 100644 --- a/speechx/speechx/asr/decoder/ctc_tlg_decoder.h +++ b/runtime/engine/asr/decoder/ctc_tlg_decoder.h @@ -24,6 +24,7 @@ DECLARE_string(graph_path); DECLARE_int32(max_active); DECLARE_double(beam); DECLARE_double(lattice_beam); +DECLARE_int32(nbest); namespace ppspeech { @@ -46,7 +47,7 @@ struct TLGDecoderOptions { decoder_opts.opts.max_active = FLAGS_max_active; decoder_opts.opts.beam = FLAGS_beam; decoder_opts.opts.lattice_beam = FLAGS_lattice_beam; - // decoder_opts.nbest = FLAGS_lattice_nbest; + decoder_opts.nbest = FLAGS_nbest; LOG(INFO) << "LatticeFasterDecoder max active: " << decoder_opts.opts.max_active; LOG(INFO) << "LatticeFasterDecoder beam: " << decoder_opts.opts.beam; @@ -85,7 +86,7 @@ class TLGDecoder : public DecoderBase { return hypotheses_; } const std::vector>& Outputs() const override { - return olabels; + return olabels_; } // outputs_; } const std::vector& Likelihood() const override { return likelihood_; @@ -111,8 +112,9 @@ class TLGDecoder : public DecoderBase { private: void AdvanceDecoding(kaldi::DecodableInterface* decodable); + int num_frame_decoded_; std::vector> hypotheses_; - std::vector> olabels; + std::vector> olabels_; std::vector likelihood_; std::vector> times_; @@ -123,4 +125,4 @@ class TLGDecoder : public DecoderBase { }; -} // namespace ppspeech \ No newline at end of file +} // namespace ppspeech diff --git a/speechx/speechx/asr/decoder/ctc_tlg_decoder_main.cc b/runtime/engine/asr/decoder/ctc_tlg_decoder_main.cc similarity index 100% rename from speechx/speechx/asr/decoder/ctc_tlg_decoder_main.cc rename to runtime/engine/asr/decoder/ctc_tlg_decoder_main.cc diff --git a/speechx/speechx/asr/decoder/decoder_itf.h b/runtime/engine/asr/decoder/decoder_itf.h similarity index 100% rename from speechx/speechx/asr/decoder/decoder_itf.h rename to runtime/engine/asr/decoder/decoder_itf.h diff --git a/speechx/speechx/asr/decoder/param.h b/runtime/engine/asr/decoder/param.h similarity index 98% rename from speechx/speechx/asr/decoder/param.h rename to runtime/engine/asr/decoder/param.h index 83e2c7fb..b9e9fd20 100644 --- a/speechx/speechx/asr/decoder/param.h +++ b/runtime/engine/asr/decoder/param.h @@ -15,7 +15,6 @@ #pragma once #include "base/common.h" -//#include "decoder/ctc_tlg_decoder.h" // feature DEFINE_bool(use_fbank, false, "False for fbank; or linear feature"); diff --git a/speechx/speechx/asr/nnet/CMakeLists.txt b/runtime/engine/asr/nnet/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/nnet/CMakeLists.txt rename to runtime/engine/asr/nnet/CMakeLists.txt diff --git a/speechx/speechx/asr/nnet/decodable.cc b/runtime/engine/asr/nnet/decodable.cc similarity index 100% rename from speechx/speechx/asr/nnet/decodable.cc rename to runtime/engine/asr/nnet/decodable.cc diff --git a/speechx/speechx/asr/nnet/decodable.h b/runtime/engine/asr/nnet/decodable.h similarity index 100% rename from speechx/speechx/asr/nnet/decodable.h rename to runtime/engine/asr/nnet/decodable.h diff --git a/speechx/speechx/asr/nnet/nnet_itf.h b/runtime/engine/asr/nnet/nnet_itf.h similarity index 100% rename from speechx/speechx/asr/nnet/nnet_itf.h rename to runtime/engine/asr/nnet/nnet_itf.h diff --git a/speechx/speechx/asr/nnet/nnet_producer.cc b/runtime/engine/asr/nnet/nnet_producer.cc similarity index 100% rename from speechx/speechx/asr/nnet/nnet_producer.cc rename to runtime/engine/asr/nnet/nnet_producer.cc diff --git a/speechx/speechx/asr/nnet/nnet_producer.h b/runtime/engine/asr/nnet/nnet_producer.h similarity index 100% rename from speechx/speechx/asr/nnet/nnet_producer.h rename to runtime/engine/asr/nnet/nnet_producer.h diff --git a/speechx/speechx/asr/nnet/u2_nnet.cc b/runtime/engine/asr/nnet/u2_nnet.cc similarity index 100% rename from speechx/speechx/asr/nnet/u2_nnet.cc rename to runtime/engine/asr/nnet/u2_nnet.cc diff --git a/speechx/speechx/asr/nnet/u2_nnet.h b/runtime/engine/asr/nnet/u2_nnet.h similarity index 100% rename from speechx/speechx/asr/nnet/u2_nnet.h rename to runtime/engine/asr/nnet/u2_nnet.h diff --git a/speechx/speechx/asr/nnet/u2_nnet_main.cc b/runtime/engine/asr/nnet/u2_nnet_main.cc similarity index 97% rename from speechx/speechx/asr/nnet/u2_nnet_main.cc rename to runtime/engine/asr/nnet/u2_nnet_main.cc index e60ae7e8..699f4258 100644 --- a/speechx/speechx/asr/nnet/u2_nnet_main.cc +++ b/runtime/engine/asr/nnet/u2_nnet_main.cc @@ -13,13 +13,13 @@ // limitations under the License. +#include "nnet/u2_nnet.h" #include "base/common.h" #include "decoder/param.h" #include "frontend/assembler.h" #include "frontend/data_cache.h" #include "kaldi/util/table-types.h" #include "nnet/decodable.h" -#include "nnet/u2_nnet.h" DEFINE_string(feature_rspecifier, "", "test feature rspecifier"); @@ -93,9 +93,9 @@ int main(int argc, char* argv[]) { ori_feature_len - chunk_idx * chunk_stride, chunk_size); } if (this_chunk_size < receptive_field_length) { - LOG(WARNING) - << "utt: " << utt << " skip last " << this_chunk_size - << " frames, expect is " << receptive_field_length; + LOG(WARNING) << "utt: " << utt << " skip last " + << this_chunk_size << " frames, expect is " + << receptive_field_length; break; } diff --git a/speechx/speechx/asr/nnet/u2_nnet_thread_main.cc b/runtime/engine/asr/nnet/u2_nnet_thread_main.cc similarity index 99% rename from speechx/speechx/asr/nnet/u2_nnet_thread_main.cc rename to runtime/engine/asr/nnet/u2_nnet_thread_main.cc index ce523e59..4339bdbe 100644 --- a/speechx/speechx/asr/nnet/u2_nnet_thread_main.cc +++ b/runtime/engine/asr/nnet/u2_nnet_thread_main.cc @@ -13,13 +13,13 @@ // limitations under the License. +#include "nnet/u2_nnet.h" #include "base/common.h" #include "decoder/param.h" -#include "frontend/wave-reader.h" #include "frontend/feature_pipeline.h" +#include "frontend/wave-reader.h" #include "kaldi/util/table-types.h" #include "nnet/decodable.h" -#include "nnet/u2_nnet.h" #include "nnet/nnet_producer.h" DEFINE_string(wav_rspecifier, "", "test wav rspecifier"); @@ -104,7 +104,7 @@ int main(int argc, char* argv[]) { CHECK(sample_offset == tot_samples); std::vector> prob_vec; - while(1) { + while (1) { std::vector logprobs; bool isok = nnet_producer->Read(&logprobs); if (nnet_producer->IsFinished()) break; diff --git a/speechx/speechx/asr/recognizer/CMakeLists.txt b/runtime/engine/asr/recognizer/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/recognizer/CMakeLists.txt rename to runtime/engine/asr/recognizer/CMakeLists.txt diff --git a/speechx/speechx/asr/recognizer/u2_recognizer.cc b/runtime/engine/asr/recognizer/u2_recognizer.cc similarity index 97% rename from speechx/speechx/asr/recognizer/u2_recognizer.cc rename to runtime/engine/asr/recognizer/u2_recognizer.cc index f31ceb3b..da1348f5 100644 --- a/speechx/speechx/asr/recognizer/u2_recognizer.cc +++ b/runtime/engine/asr/recognizer/u2_recognizer.cc @@ -33,12 +33,12 @@ U2Recognizer::U2Recognizer(const U2RecognizerResource& resource) decodable_.reset(new Decodable(nnet_producer_, am_scale)); CHECK_NE(resource.vocab_path, ""); - if (resource.decoder_opts.tlg_decoder_opts.fst_path == "") { + if (resource.decoder_opts.tlg_decoder_opts.fst_path.empty()) { LOG(INFO) << resource.decoder_opts.tlg_decoder_opts.fst_path; - decoder_.reset(new CTCPrefixBeamSearch( - resource.vocab_path, resource.decoder_opts.ctc_prefix_search_opts)); + decoder_ = std::make_unique( + resource.vocab_path, resource.decoder_opts.ctc_prefix_search_opts); } else { - decoder_.reset(new TLGDecoder(resource.decoder_opts.tlg_decoder_opts)); + decoder_ = std::make_unique(resource.decoder_opts.tlg_decoder_opts); } symbol_table_ = decoder_->WordSymbolTable(); @@ -268,4 +268,4 @@ void U2Recognizer::SetInputFinished() { } -} // namespace ppspeech \ No newline at end of file +} // namespace ppspeech diff --git a/speechx/speechx/asr/recognizer/u2_recognizer.h b/runtime/engine/asr/recognizer/u2_recognizer.h similarity index 95% rename from speechx/speechx/asr/recognizer/u2_recognizer.h rename to runtime/engine/asr/recognizer/u2_recognizer.h index 889da85b..299b64ed 100644 --- a/speechx/speechx/asr/recognizer/u2_recognizer.h +++ b/runtime/engine/asr/recognizer/u2_recognizer.h @@ -31,11 +31,9 @@ DECLARE_double(rescoring_weight); DECLARE_double(reverse_weight); DECLARE_int32(nbest); DECLARE_int32(blank); - DECLARE_double(acoustic_scale); DECLARE_string(vocab_path); DECLARE_string(word_symbol_table); -// DECLARE_string(fst_path); namespace ppspeech { @@ -74,10 +72,6 @@ struct DecodeOptions { decoder_opts.ctc_prefix_search_opts.blank = FLAGS_blank; decoder_opts.ctc_prefix_search_opts.first_beam_size = FLAGS_nbest; decoder_opts.ctc_prefix_search_opts.second_beam_size = FLAGS_nbest; - // decoder_opts.tlg_decoder_opts.fst_path = "";//FLAGS_fst_path; - // decoder_opts.tlg_decoder_opts.word_symbol_table = - // FLAGS_word_symbol_table; - // decoder_opts.tlg_decoder_opts.nbest = FLAGS_nbest; decoder_opts.tlg_decoder_opts = ppspeech::TLGDecoderOptions::InitFromFlags(); @@ -183,4 +177,4 @@ class U2Recognizer { std::thread thread_; }; -} // namespace ppspeech \ No newline at end of file +} // namespace ppspeech diff --git a/speechx/speechx/asr/recognizer/u2_recognizer_batch_main.cc b/runtime/engine/asr/recognizer/u2_recognizer_batch_main.cc similarity index 100% rename from speechx/speechx/asr/recognizer/u2_recognizer_batch_main.cc rename to runtime/engine/asr/recognizer/u2_recognizer_batch_main.cc diff --git a/speechx/speechx/asr/recognizer/u2_recognizer_main.cc b/runtime/engine/asr/recognizer/u2_recognizer_main.cc similarity index 100% rename from speechx/speechx/asr/recognizer/u2_recognizer_main.cc rename to runtime/engine/asr/recognizer/u2_recognizer_main.cc index 178c91db..fb37d050 100644 --- a/speechx/speechx/asr/recognizer/u2_recognizer_main.cc +++ b/runtime/engine/asr/recognizer/u2_recognizer_main.cc @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "recognizer/u2_recognizer.h" #include "decoder/param.h" #include "frontend/wave-reader.h" #include "kaldi/util/table-types.h" -#include "recognizer/u2_recognizer.h" DEFINE_string(wav_rspecifier, "", "test feature rspecifier"); DEFINE_string(result_wspecifier, "", "test result wspecifier"); diff --git a/speechx/speechx/asr/recognizer/u2_recognizer_thread_main.cc b/runtime/engine/asr/recognizer/u2_recognizer_thread_main.cc similarity index 98% rename from speechx/speechx/asr/recognizer/u2_recognizer_thread_main.cc rename to runtime/engine/asr/recognizer/u2_recognizer_thread_main.cc index 3f45294d..b86853fa 100644 --- a/speechx/speechx/asr/recognizer/u2_recognizer_thread_main.cc +++ b/runtime/engine/asr/recognizer/u2_recognizer_thread_main.cc @@ -100,7 +100,7 @@ int main(int argc, char* argv[]) { continue; } - tot_decode_time += local_timer.Elapsed(); + tot_decode_time += local_timer.Elapsed(); LOG(INFO) << utt << " " << result; LOG(INFO) << " RTF: " << local_timer.Elapsed() / dur << " dur: " << dur << " cost: " << local_timer.Elapsed(); diff --git a/speechx/speechx/asr/server/CMakeLists.txt b/runtime/engine/asr/server/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/server/CMakeLists.txt rename to runtime/engine/asr/server/CMakeLists.txt diff --git a/speechx/speechx/asr/server/websocket/CMakeLists.txt b/runtime/engine/asr/server/websocket/CMakeLists.txt similarity index 100% rename from speechx/speechx/asr/server/websocket/CMakeLists.txt rename to runtime/engine/asr/server/websocket/CMakeLists.txt diff --git a/speechx/speechx/asr/server/websocket/websocket_client.cc b/runtime/engine/asr/server/websocket/websocket_client.cc similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_client.cc rename to runtime/engine/asr/server/websocket/websocket_client.cc diff --git a/speechx/speechx/asr/server/websocket/websocket_client.h b/runtime/engine/asr/server/websocket/websocket_client.h similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_client.h rename to runtime/engine/asr/server/websocket/websocket_client.h diff --git a/speechx/speechx/asr/server/websocket/websocket_client_main.cc b/runtime/engine/asr/server/websocket/websocket_client_main.cc similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_client_main.cc rename to runtime/engine/asr/server/websocket/websocket_client_main.cc index 7c5a4f2f..7ad36e3a 100644 --- a/speechx/speechx/asr/server/websocket/websocket_client_main.cc +++ b/runtime/engine/asr/server/websocket/websocket_client_main.cc @@ -12,10 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "websocket/websocket_client.h" #include "kaldi/feat/wave-reader.h" #include "kaldi/util/kaldi-io.h" #include "kaldi/util/table-types.h" -#include "websocket/websocket_client.h" DEFINE_string(host, "127.0.0.1", "host of websocket server"); DEFINE_int32(port, 8082, "port of websocket server"); diff --git a/speechx/speechx/asr/server/websocket/websocket_server.cc b/runtime/engine/asr/server/websocket/websocket_server.cc similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_server.cc rename to runtime/engine/asr/server/websocket/websocket_server.cc diff --git a/speechx/speechx/asr/server/websocket/websocket_server.h b/runtime/engine/asr/server/websocket/websocket_server.h similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_server.h rename to runtime/engine/asr/server/websocket/websocket_server.h diff --git a/speechx/speechx/asr/server/websocket/websocket_server_main.cc b/runtime/engine/asr/server/websocket/websocket_server_main.cc similarity index 100% rename from speechx/speechx/asr/server/websocket/websocket_server_main.cc rename to runtime/engine/asr/server/websocket/websocket_server_main.cc index 5c32caf2..5f805ac9 100644 --- a/speechx/speechx/asr/server/websocket/websocket_server_main.cc +++ b/runtime/engine/asr/server/websocket/websocket_server_main.cc @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "decoder/param.h" #include "websocket/websocket_server.h" +#include "decoder/param.h" DEFINE_int32(port, 8082, "websocket listening port"); diff --git a/speechx/speechx/codelab/CMakeLists.txt b/runtime/engine/codelab/CMakeLists.txt similarity index 100% rename from speechx/speechx/codelab/CMakeLists.txt rename to runtime/engine/codelab/CMakeLists.txt diff --git a/speechx/speechx/codelab/README.md b/runtime/engine/codelab/README.md similarity index 100% rename from speechx/speechx/codelab/README.md rename to runtime/engine/codelab/README.md diff --git a/speechx/speechx/codelab/glog/CMakeLists.txt b/runtime/engine/codelab/glog/CMakeLists.txt similarity index 100% rename from speechx/speechx/codelab/glog/CMakeLists.txt rename to runtime/engine/codelab/glog/CMakeLists.txt diff --git a/speechx/speechx/codelab/glog/README.md b/runtime/engine/codelab/glog/README.md similarity index 100% rename from speechx/speechx/codelab/glog/README.md rename to runtime/engine/codelab/glog/README.md diff --git a/speechx/speechx/codelab/glog/glog_logtostderr_main.cc b/runtime/engine/codelab/glog/glog_logtostderr_main.cc similarity index 100% rename from speechx/speechx/codelab/glog/glog_logtostderr_main.cc rename to runtime/engine/codelab/glog/glog_logtostderr_main.cc diff --git a/speechx/speechx/codelab/glog/glog_main.cc b/runtime/engine/codelab/glog/glog_main.cc similarity index 100% rename from speechx/speechx/codelab/glog/glog_main.cc rename to runtime/engine/codelab/glog/glog_main.cc diff --git a/speechx/speechx/common/CMakeLists.txt b/runtime/engine/common/CMakeLists.txt similarity index 100% rename from speechx/speechx/common/CMakeLists.txt rename to runtime/engine/common/CMakeLists.txt diff --git a/speechx/speechx/common/base/basic_types.h b/runtime/engine/common/base/basic_types.h similarity index 97% rename from speechx/speechx/common/base/basic_types.h rename to runtime/engine/common/base/basic_types.h index 2b15a61f..c7fdc924 100644 --- a/speechx/speechx/common/base/basic_types.h +++ b/runtime/engine/common/base/basic_types.h @@ -28,7 +28,7 @@ typedef int int32; // NOLINT #if defined(__LP64__) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) typedef long int64; // NOLINT #else -typedef long long int64; // NOLINT +typedef long long int64; // NOLINT #endif typedef unsigned char uint8; // NOLINT diff --git a/speechx/speechx/common/base/common.h b/runtime/engine/common/base/common.h similarity index 100% rename from speechx/speechx/common/base/common.h rename to runtime/engine/common/base/common.h index 06fcd9fd..d94dc8a8 100644 --- a/speechx/speechx/common/base/common.h +++ b/runtime/engine/common/base/common.h @@ -21,6 +21,8 @@ #include #include #include +#include +#include #include #include #include @@ -42,8 +44,6 @@ #include #include #include -#include -#include #include "base/basic_types.h" #include "base/flags.h" diff --git a/speechx/speechx/common/base/flags.h b/runtime/engine/common/base/flags.h similarity index 100% rename from speechx/speechx/common/base/flags.h rename to runtime/engine/common/base/flags.h diff --git a/speechx/speechx/common/base/log.h b/runtime/engine/common/base/log.h similarity index 100% rename from speechx/speechx/common/base/log.h rename to runtime/engine/common/base/log.h diff --git a/speechx/speechx/common/base/macros.h b/runtime/engine/common/base/macros.h similarity index 100% rename from speechx/speechx/common/base/macros.h rename to runtime/engine/common/base/macros.h diff --git a/speechx/speechx/common/base/safe_queue.h b/runtime/engine/common/base/safe_queue.h similarity index 100% rename from speechx/speechx/common/base/safe_queue.h rename to runtime/engine/common/base/safe_queue.h diff --git a/speechx/speechx/kaldi/.gitkeep b/runtime/engine/common/base/safe_queue_inl.h similarity index 100% rename from speechx/speechx/kaldi/.gitkeep rename to runtime/engine/common/base/safe_queue_inl.h diff --git a/speechx/speechx/common/base/thread_pool.h b/runtime/engine/common/base/thread_pool.h similarity index 100% rename from speechx/speechx/common/base/thread_pool.h rename to runtime/engine/common/base/thread_pool.h diff --git a/speechx/speechx/common/frontend/CMakeLists.txt b/runtime/engine/common/frontend/CMakeLists.txt similarity index 100% rename from speechx/speechx/common/frontend/CMakeLists.txt rename to runtime/engine/common/frontend/CMakeLists.txt diff --git a/speechx/speechx/common/frontend/assembler.cc b/runtime/engine/common/frontend/assembler.cc similarity index 97% rename from speechx/speechx/common/frontend/assembler.cc rename to runtime/engine/common/frontend/assembler.cc index 5f019c42..487951cd 100644 --- a/speechx/speechx/common/frontend/assembler.cc +++ b/runtime/engine/common/frontend/assembler.cc @@ -97,8 +97,8 @@ bool Assembler::Compute(vector* feats) { CHECK(val.size() == dim_) << val.size(); int32 start = counter * dim_; - std::memcpy(feats->data() + start, - val.data(), val.size() * sizeof(BaseFloat)); + std::memcpy( + feats->data() + start, val.data(), val.size() * sizeof(BaseFloat)); if (this_chunk_size - counter <= cache_size_) { feature_cache_.push(val); diff --git a/speechx/speechx/common/frontend/assembler.h b/runtime/engine/common/frontend/assembler.h similarity index 100% rename from speechx/speechx/common/frontend/assembler.h rename to runtime/engine/common/frontend/assembler.h diff --git a/speechx/speechx/common/frontend/audio_cache.cc b/runtime/engine/common/frontend/audio_cache.cc similarity index 100% rename from speechx/speechx/common/frontend/audio_cache.cc rename to runtime/engine/common/frontend/audio_cache.cc diff --git a/speechx/speechx/common/frontend/audio_cache.h b/runtime/engine/common/frontend/audio_cache.h similarity index 100% rename from speechx/speechx/common/frontend/audio_cache.h rename to runtime/engine/common/frontend/audio_cache.h diff --git a/speechx/speechx/common/frontend/cmvn.cc b/runtime/engine/common/frontend/cmvn.cc similarity index 90% rename from speechx/speechx/common/frontend/cmvn.cc rename to runtime/engine/common/frontend/cmvn.cc index 2fac1506..8375d3d1 100644 --- a/speechx/speechx/common/frontend/cmvn.cc +++ b/runtime/engine/common/frontend/cmvn.cc @@ -84,11 +84,12 @@ void CMVN::Compute(vector* feats) const { KALDI_ASSERT(feats != NULL); if (feats->size() % dim_ != 0) { - LOG(ERROR)<< "Dim mismatch: cmvn " << mean_stats_.size() << ',' - << var_stats_.size() - 1 << ", feats " << feats->size() << 'x'; + LOG(ERROR) << "Dim mismatch: cmvn " << mean_stats_.size() << ',' + << var_stats_.size() - 1 << ", feats " << feats->size() + << 'x'; } if (var_stats_.size() == 0 && var_norm_) { - LOG(ERROR) + LOG(ERROR) << "You requested variance normalization but no variance stats_ " << "are supplied."; } @@ -98,8 +99,8 @@ void CMVN::Compute(vector* feats) const { // computing an offset and representing it as stats_, we use a count of one. if (count < 1.0) LOG(ERROR) << "Insufficient stats_ for cepstral mean and variance " - "normalization: " - << "count = " << count; + "normalization: " + << "count = " << count; if (!var_norm_) { vector offset(feats->size()); @@ -112,11 +113,12 @@ void CMVN::Compute(vector* feats) const { // with the dim_ of feature. // the dim_ of feats = dim_ * num_frames; for (int32 idx = 0; idx < feats->size() / dim_; ++idx) { - std::memcpy(mean_stats_apply.data() + dim_ * idx, - mean_stats.data(), dim_* sizeof(double)); + std::memcpy(mean_stats_apply.data() + dim_ * idx, + mean_stats.data(), + dim_ * sizeof(double)); } for (size_t idx = 0; idx < feats->size(); ++idx) { - feats->at(idx) += offset[idx]; + feats->at(idx) += offset[idx]; } return; } @@ -130,7 +132,7 @@ void CMVN::Compute(vector* feats) const { double var = (var_stats_[d] / count) - mean * mean, floor = 1.0e-20; if (var < floor) { LOG(WARNING) << "Flooring cepstral variance from " << var << " to " - << floor; + << floor; var = floor; } scale = 1.0 / sqrt(var); @@ -146,7 +148,7 @@ void CMVN::Compute(vector* feats) const { } // Apply the normalization. for (size_t idx = 0; idx < feats->size(); ++idx) { - feats->at(idx) *= norm1[idx]; + feats->at(idx) *= norm1[idx]; } for (size_t idx = 0; idx < feats->size(); ++idx) { diff --git a/speechx/speechx/common/frontend/cmvn.h b/runtime/engine/common/frontend/cmvn.h similarity index 100% rename from speechx/speechx/common/frontend/cmvn.h rename to runtime/engine/common/frontend/cmvn.h diff --git a/speechx/speechx/common/frontend/compute_fbank_main.cc b/runtime/engine/common/frontend/compute_fbank_main.cc similarity index 100% rename from speechx/speechx/common/frontend/compute_fbank_main.cc rename to runtime/engine/common/frontend/compute_fbank_main.cc diff --git a/speechx/speechx/common/frontend/compute_linear_spectrogram_main.cc b/runtime/engine/common/frontend/compute_linear_spectrogram_main.cc similarity index 100% rename from speechx/speechx/common/frontend/compute_linear_spectrogram_main.cc rename to runtime/engine/common/frontend/compute_linear_spectrogram_main.cc diff --git a/speechx/speechx/common/frontend/data_cache.h b/runtime/engine/common/frontend/data_cache.h similarity index 100% rename from speechx/speechx/common/frontend/data_cache.h rename to runtime/engine/common/frontend/data_cache.h diff --git a/speechx/speechx/common/frontend/db_norm.cc b/runtime/engine/common/frontend/db_norm.cc similarity index 100% rename from speechx/speechx/common/frontend/db_norm.cc rename to runtime/engine/common/frontend/db_norm.cc diff --git a/speechx/speechx/common/frontend/db_norm.h b/runtime/engine/common/frontend/db_norm.h similarity index 100% rename from speechx/speechx/common/frontend/db_norm.h rename to runtime/engine/common/frontend/db_norm.h diff --git a/speechx/speechx/common/frontend/fbank.cc b/runtime/engine/common/frontend/fbank.cc similarity index 100% rename from speechx/speechx/common/frontend/fbank.cc rename to runtime/engine/common/frontend/fbank.cc diff --git a/speechx/speechx/common/frontend/fbank.h b/runtime/engine/common/frontend/fbank.h similarity index 100% rename from speechx/speechx/common/frontend/fbank.h rename to runtime/engine/common/frontend/fbank.h index 61d9c9aa..4398e72f 100644 --- a/speechx/speechx/common/frontend/fbank.h +++ b/runtime/engine/common/frontend/fbank.h @@ -15,8 +15,8 @@ #pragma once #include "base/common.h" -#include "frontend/feature_common.h" #include "frontend/feature-fbank.h" +#include "frontend/feature_common.h" namespace ppspeech { diff --git a/speechx/speechx/common/frontend/feature-fbank.cc b/runtime/engine/common/frontend/feature-fbank.cc similarity index 100% rename from speechx/speechx/common/frontend/feature-fbank.cc rename to runtime/engine/common/frontend/feature-fbank.cc diff --git a/speechx/speechx/common/frontend/feature-fbank.h b/runtime/engine/common/frontend/feature-fbank.h similarity index 100% rename from speechx/speechx/common/frontend/feature-fbank.h rename to runtime/engine/common/frontend/feature-fbank.h diff --git a/speechx/speechx/common/frontend/feature-functions.cc b/runtime/engine/common/frontend/feature-functions.cc similarity index 100% rename from speechx/speechx/common/frontend/feature-functions.cc rename to runtime/engine/common/frontend/feature-functions.cc diff --git a/speechx/speechx/common/frontend/feature-functions.h b/runtime/engine/common/frontend/feature-functions.h similarity index 100% rename from speechx/speechx/common/frontend/feature-functions.h rename to runtime/engine/common/frontend/feature-functions.h diff --git a/speechx/speechx/common/frontend/feature-window.cc b/runtime/engine/common/frontend/feature-window.cc similarity index 100% rename from speechx/speechx/common/frontend/feature-window.cc rename to runtime/engine/common/frontend/feature-window.cc diff --git a/speechx/speechx/common/frontend/feature-window.h b/runtime/engine/common/frontend/feature-window.h similarity index 100% rename from speechx/speechx/common/frontend/feature-window.h rename to runtime/engine/common/frontend/feature-window.h diff --git a/speechx/speechx/common/frontend/feature_cache.cc b/runtime/engine/common/frontend/feature_cache.cc similarity index 97% rename from speechx/speechx/common/frontend/feature_cache.cc rename to runtime/engine/common/frontend/feature_cache.cc index c166bd64..bdbe6931 100644 --- a/speechx/speechx/common/frontend/feature_cache.cc +++ b/runtime/engine/common/frontend/feature_cache.cc @@ -67,7 +67,7 @@ bool FeatureCache::Compute() { for (int chunk_idx = 0; chunk_idx < num_chunk; ++chunk_idx) { int32 start = chunk_idx * dim_; - vector feature_chunk(feature.data() + start, + vector feature_chunk(feature.data() + start, feature.data() + start + dim_); // feed cache cache_.push(feature_chunk); diff --git a/speechx/speechx/common/frontend/feature_cache.h b/runtime/engine/common/frontend/feature_cache.h similarity index 97% rename from speechx/speechx/common/frontend/feature_cache.h rename to runtime/engine/common/frontend/feature_cache.h index b87612d6..25cb86f8 100644 --- a/speechx/speechx/common/frontend/feature_cache.h +++ b/runtime/engine/common/frontend/feature_cache.h @@ -57,7 +57,7 @@ class FeatureCache : public FrontendInterface { bool Compute(); int32 dim_; - size_t max_size_; // cache capacity + size_t max_size_; // cache capacity std::unique_ptr base_extractor_; std::queue> cache_; // feature cache diff --git a/speechx/speechx/common/frontend/feature_common.h b/runtime/engine/common/frontend/feature_common.h similarity index 100% rename from speechx/speechx/common/frontend/feature_common.h rename to runtime/engine/common/frontend/feature_common.h index 7864bd30..fcc9100c 100644 --- a/speechx/speechx/common/frontend/feature_common.h +++ b/runtime/engine/common/frontend/feature_common.h @@ -14,8 +14,8 @@ #pragma once -#include "frontend_itf.h" #include "frontend/feature-window.h" +#include "frontend_itf.h" namespace ppspeech { diff --git a/speechx/speechx/common/frontend/feature_common_inl.h b/runtime/engine/common/frontend/feature_common_inl.h similarity index 100% rename from speechx/speechx/common/frontend/feature_common_inl.h rename to runtime/engine/common/frontend/feature_common_inl.h diff --git a/speechx/speechx/common/frontend/feature_pipeline.cc b/runtime/engine/common/frontend/feature_pipeline.cc similarity index 100% rename from speechx/speechx/common/frontend/feature_pipeline.cc rename to runtime/engine/common/frontend/feature_pipeline.cc diff --git a/speechx/speechx/common/frontend/feature_pipeline.h b/runtime/engine/common/frontend/feature_pipeline.h similarity index 100% rename from speechx/speechx/common/frontend/feature_pipeline.h rename to runtime/engine/common/frontend/feature_pipeline.h index c9a649fd..7509814f 100644 --- a/speechx/speechx/common/frontend/feature_pipeline.h +++ b/runtime/engine/common/frontend/feature_pipeline.h @@ -18,11 +18,11 @@ #include "frontend/assembler.h" #include "frontend/audio_cache.h" +#include "frontend/cmvn.h" #include "frontend/data_cache.h" #include "frontend/fbank.h" #include "frontend/feature_cache.h" #include "frontend/frontend_itf.h" -#include "frontend/cmvn.h" // feature DECLARE_bool(fill_zero); diff --git a/speechx/speechx/common/frontend/fftsg.c b/runtime/engine/common/frontend/fftsg.c similarity index 100% rename from speechx/speechx/common/frontend/fftsg.c rename to runtime/engine/common/frontend/fftsg.c diff --git a/speechx/speechx/common/frontend/frontend_itf.h b/runtime/engine/common/frontend/frontend_itf.h similarity index 100% rename from speechx/speechx/common/frontend/frontend_itf.h rename to runtime/engine/common/frontend/frontend_itf.h diff --git a/speechx/speechx/common/frontend/linear_spectrogram.cc b/runtime/engine/common/frontend/linear_spectrogram.cc similarity index 100% rename from speechx/speechx/common/frontend/linear_spectrogram.cc rename to runtime/engine/common/frontend/linear_spectrogram.cc diff --git a/speechx/speechx/common/frontend/linear_spectrogram.h b/runtime/engine/common/frontend/linear_spectrogram.h similarity index 100% rename from speechx/speechx/common/frontend/linear_spectrogram.h rename to runtime/engine/common/frontend/linear_spectrogram.h diff --git a/speechx/speechx/common/frontend/mel-computations.cc b/runtime/engine/common/frontend/mel-computations.cc similarity index 100% rename from speechx/speechx/common/frontend/mel-computations.cc rename to runtime/engine/common/frontend/mel-computations.cc diff --git a/speechx/speechx/common/frontend/mel-computations.h b/runtime/engine/common/frontend/mel-computations.h similarity index 100% rename from speechx/speechx/common/frontend/mel-computations.h rename to runtime/engine/common/frontend/mel-computations.h diff --git a/speechx/speechx/common/frontend/mfcc.cc b/runtime/engine/common/frontend/mfcc.cc similarity index 100% rename from speechx/speechx/common/frontend/mfcc.cc rename to runtime/engine/common/frontend/mfcc.cc diff --git a/speechx/speechx/common/frontend/mfcc.h b/runtime/engine/common/frontend/mfcc.h similarity index 100% rename from speechx/speechx/common/frontend/mfcc.h rename to runtime/engine/common/frontend/mfcc.h diff --git a/speechx/speechx/common/frontend/normalizer.h b/runtime/engine/common/frontend/normalizer.h similarity index 100% rename from speechx/speechx/common/frontend/normalizer.h rename to runtime/engine/common/frontend/normalizer.h diff --git a/speechx/speechx/common/frontend/rfft.cc b/runtime/engine/common/frontend/rfft.cc similarity index 100% rename from speechx/speechx/common/frontend/rfft.cc rename to runtime/engine/common/frontend/rfft.cc diff --git a/speechx/speechx/common/frontend/rfft.h b/runtime/engine/common/frontend/rfft.h similarity index 100% rename from speechx/speechx/common/frontend/rfft.h rename to runtime/engine/common/frontend/rfft.h diff --git a/runtime/engine/common/frontend/wave-reader.cc b/runtime/engine/common/frontend/wave-reader.cc new file mode 100644 index 00000000..b64dcc9e --- /dev/null +++ b/runtime/engine/common/frontend/wave-reader.cc @@ -0,0 +1,376 @@ +// feat/wave-reader.cc + +// Copyright 2009-2011 Karel Vesely; Petr Motlicek +// 2013 Florent Masson +// 2013 Johns Hopkins University (author: Daniel Povey) + +// See ../../COPYING for clarification regarding multiple authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +// MERCHANTABLITY OR NON-INFRINGEMENT. +// See the Apache 2 License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include + +#include "base/kaldi-error.h" +#include "base/kaldi-utils.h" +#include "frontend/wave-reader.h" + +namespace kaldi { + +// A utility class for reading wave header. +struct WaveHeaderReadGofer { + std::istream &is; + bool swap; + char tag[5]; + + WaveHeaderReadGofer(std::istream &is) : is(is), swap(false) { + memset(tag, '\0', sizeof tag); + } + + void Expect4ByteTag(const char *expected) { + is.read(tag, 4); + if (is.fail()) + KALDI_ERR << "WaveData: expected " << expected + << ", failed to read anything"; + if (strcmp(tag, expected)) + KALDI_ERR << "WaveData: expected " << expected << ", got " << tag; + } + + void Read4ByteTag() { + is.read(tag, 4); + if (is.fail()) + KALDI_ERR << "WaveData: expected 4-byte chunk-name, got read error"; + } + + uint32 ReadUint32() { + union { + char result[4]; + uint32 ans; + } u; + is.read(u.result, 4); + if (swap) KALDI_SWAP4(u.result); + if (is.fail()) + KALDI_ERR << "WaveData: unexpected end of file or read error"; + return u.ans; + } + + uint16 ReadUint16() { + union { + char result[2]; + int16 ans; + } u; + is.read(u.result, 2); + if (swap) KALDI_SWAP2(u.result); + if (is.fail()) + KALDI_ERR << "WaveData: unexpected end of file or read error"; + return u.ans; + } +}; + +static void WriteUint32(std::ostream &os, int32 i) { + union { + char buf[4]; + int i; + } u; + u.i = i; +#ifdef __BIG_ENDIAN__ + KALDI_SWAP4(u.buf); +#endif + os.write(u.buf, 4); + if (os.fail()) KALDI_ERR << "WaveData: error writing to stream."; +} + +static void WriteUint16(std::ostream &os, int16 i) { + union { + char buf[2]; + int16 i; + } u; + u.i = i; +#ifdef __BIG_ENDIAN__ + KALDI_SWAP2(u.buf); +#endif + os.write(u.buf, 2); + if (os.fail()) KALDI_ERR << "WaveData: error writing to stream."; +} + +void WaveInfo::Read(std::istream &is) { + WaveHeaderReadGofer reader(is); + reader.Read4ByteTag(); + if (strcmp(reader.tag, "RIFF") == 0) + reverse_bytes_ = false; + else if (strcmp(reader.tag, "RIFX") == 0) + reverse_bytes_ = true; + else + KALDI_ERR << "WaveData: expected RIFF or RIFX, got " << reader.tag; + +#ifdef __BIG_ENDIAN__ + reverse_bytes_ = !reverse_bytes_; +#endif + reader.swap = reverse_bytes_; + + uint32 riff_chunk_size = reader.ReadUint32(); + reader.Expect4ByteTag("WAVE"); + + uint32 riff_chunk_read = 0; + riff_chunk_read += 4; // WAVE included in riff_chunk_size. + + // Possibly skip any RIFF tags between 'WAVE' and 'fmt '. + // Apple devices produce a filler tag 'JUNK' for memory alignment. + reader.Read4ByteTag(); + riff_chunk_read += 4; + while (strcmp(reader.tag, "fmt ") != 0) { + uint32 filler_size = reader.ReadUint32(); + riff_chunk_read += 4; + for (uint32 i = 0; i < filler_size; i++) { + is.get(); // read 1 byte, + } + riff_chunk_read += filler_size; + // get next RIFF tag, + reader.Read4ByteTag(); + riff_chunk_read += 4; + } + + KALDI_ASSERT(strcmp(reader.tag, "fmt ") == 0); + uint32 subchunk1_size = reader.ReadUint32(); + uint16 audio_format = reader.ReadUint16(); + num_channels_ = reader.ReadUint16(); + uint32 sample_rate = reader.ReadUint32(), byte_rate = reader.ReadUint32(), + block_align = reader.ReadUint16(), + bits_per_sample = reader.ReadUint16(); + samp_freq_ = static_cast(sample_rate); + + uint32 fmt_chunk_read = 16; + if (audio_format == 1) { + if (subchunk1_size < 16) { + KALDI_ERR << "WaveData: expect PCM format data to have fmt chunk " + << "of at least size 16."; + } + } else if (audio_format == 0xFFFE) { // WAVE_FORMAT_EXTENSIBLE + uint16 extra_size = reader.ReadUint16(); + if (subchunk1_size < 40 || extra_size < 22) { + KALDI_ERR + << "WaveData: malformed WAVE_FORMAT_EXTENSIBLE format data."; + } + reader.ReadUint16(); // Unused for PCM. + reader.ReadUint32(); // Channel map: we do not care. + uint32 guid1 = reader.ReadUint32(), guid2 = reader.ReadUint32(), + guid3 = reader.ReadUint32(), guid4 = reader.ReadUint32(); + fmt_chunk_read = 40; + + // Support only KSDATAFORMAT_SUBTYPE_PCM for now. Interesting formats: + // ("00000001-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_PCM) + // ("00000003-0000-0010-8000-00aa00389b71", + // KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) + // ("00000006-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_ALAW) + // ("00000007-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_MULAW) + if (guid1 != 0x00000001 || guid2 != 0x00100000 || guid3 != 0xAA000080 || + guid4 != 0x719B3800) { + KALDI_ERR << "WaveData: unsupported WAVE_FORMAT_EXTENSIBLE format."; + } + } else { + KALDI_ERR << "WaveData: can read only PCM data, format id in file is: " + << audio_format; + } + + for (uint32 i = fmt_chunk_read; i < subchunk1_size; ++i) + is.get(); // use up extra data. + + if (num_channels_ == 0) KALDI_ERR << "WaveData: no channels present"; + if (bits_per_sample != 16) + KALDI_ERR << "WaveData: unsupported bits_per_sample = " + << bits_per_sample; + if (byte_rate != sample_rate * bits_per_sample / 8 * num_channels_) + KALDI_ERR << "Unexpected byte rate " << byte_rate << " vs. " + << sample_rate << " * " << (bits_per_sample / 8) << " * " + << num_channels_; + if (block_align != num_channels_ * bits_per_sample / 8) + KALDI_ERR << "Unexpected block_align: " << block_align << " vs. " + << num_channels_ << " * " << (bits_per_sample / 8); + + riff_chunk_read += 4 + subchunk1_size; + // size of what we just read, 4 for subchunk1_size + subchunk1_size itself. + + // We support an optional "fact" chunk (which is useless but which + // we encountered), and then a single "data" chunk. + + reader.Read4ByteTag(); + riff_chunk_read += 4; + + // Skip any subchunks between "fmt" and "data". Usually there will + // be a single "fact" subchunk, but on Windows there can also be a + // "list" subchunk. + while (strcmp(reader.tag, "data") != 0) { + // We will just ignore the data in these chunks. + uint32 chunk_sz = reader.ReadUint32(); + if (chunk_sz != 4 && strcmp(reader.tag, "fact") == 0) + KALDI_WARN << "Expected fact chunk to be 4 bytes long."; + for (uint32 i = 0; i < chunk_sz; i++) is.get(); + riff_chunk_read += + 4 + chunk_sz; // for chunk_sz (4) + chunk contents (chunk-sz) + + // Now read the next chunk name. + reader.Read4ByteTag(); + riff_chunk_read += 4; + } + + KALDI_ASSERT(strcmp(reader.tag, "data") == 0); + uint32 data_chunk_size = reader.ReadUint32(); + riff_chunk_read += 4; + + // Figure out if the file is going to be read to the end. Values as + // observed in the wild: + bool is_stream_mode = + riff_chunk_size == 0 || riff_chunk_size == 0xFFFFFFFF || + data_chunk_size == 0 || data_chunk_size == 0xFFFFFFFF || + data_chunk_size == 0x7FFFF000; // This value is used by SoX. + + if (is_stream_mode) + KALDI_VLOG(1) << "Read in RIFF chunk size: " << riff_chunk_size + << ", data chunk size: " << data_chunk_size + << ". Assume 'stream mode' (reading data to EOF)."; + + if (!is_stream_mode && + std::abs(static_cast(riff_chunk_read) + + static_cast(data_chunk_size) - + static_cast(riff_chunk_size)) > 1) { + // We allow the size to be off by one without warning, because there is + // a + // weirdness in the format of RIFF files that means that the input may + // sometimes be padded with 1 unused byte to make the total size even. + KALDI_WARN << "Expected " << riff_chunk_size + << " bytes in RIFF chunk, but " + << "after first data block there will be " << riff_chunk_read + << " + " << data_chunk_size << " bytes " + << "(we do not support reading multiple data chunks)."; + } + + if (is_stream_mode) + samp_count_ = -1; + else + samp_count_ = data_chunk_size / block_align; +} + +void WaveData::Read(std::istream &is) { + const uint32 kBlockSize = 1024 * 1024; + + WaveInfo header; + header.Read(is); + + data_.Resize(0, 0); // clear the data. + samp_freq_ = header.SampFreq(); + + std::vector buffer; + uint32 bytes_to_go = header.IsStreamed() ? kBlockSize : header.DataBytes(); + + // Once in a while header.DataBytes() will report an insane value; + // read the file to the end + while (is && bytes_to_go > 0) { + uint32 block_bytes = std::min(bytes_to_go, kBlockSize); + uint32 offset = buffer.size(); + buffer.resize(offset + block_bytes); + is.read(&buffer[offset], block_bytes); + uint32 bytes_read = is.gcount(); + buffer.resize(offset + bytes_read); + if (!header.IsStreamed()) bytes_to_go -= bytes_read; + } + + if (is.bad()) KALDI_ERR << "WaveData: file read error"; + + if (buffer.size() == 0) KALDI_ERR << "WaveData: empty file (no data)"; + + if (!header.IsStreamed() && buffer.size() < header.DataBytes()) { + KALDI_WARN << "Expected " << header.DataBytes() + << " bytes of wave data, " + << "but read only " << buffer.size() << " bytes. " + << "Truncated file?"; + } + + uint16 *data_ptr = reinterpret_cast(&buffer[0]); + + // The matrix is arranged row per channel, column per sample. + data_.Resize(header.NumChannels(), buffer.size() / header.BlockAlign()); + for (uint32 i = 0; i < data_.NumCols(); ++i) { + for (uint32 j = 0; j < data_.NumRows(); ++j) { + int16 k = *data_ptr++; + if (header.ReverseBytes()) KALDI_SWAP2(k); + data_(j, i) = k; + } + } +} + + +// Write 16-bit PCM. + +// note: the WAVE chunk contains 2 subchunks. +// +// subchunk2size = data.NumRows() * data.NumCols() * 2. + + +void WaveData::Write(std::ostream &os) const { + os << "RIFF"; + if (data_.NumRows() == 0) + KALDI_ERR << "Error: attempting to write empty WAVE file"; + + int32 num_chan = data_.NumRows(), num_samp = data_.NumCols(), + bytes_per_samp = 2; + + int32 subchunk2size = (num_chan * num_samp * bytes_per_samp); + int32 chunk_size = 36 + subchunk2size; + WriteUint32(os, chunk_size); + os << "WAVE"; + os << "fmt "; + WriteUint32(os, 16); + WriteUint16(os, 1); + WriteUint16(os, num_chan); + KALDI_ASSERT(samp_freq_ > 0); + WriteUint32(os, static_cast(samp_freq_)); + WriteUint32(os, static_cast(samp_freq_) * num_chan * bytes_per_samp); + WriteUint16(os, num_chan * bytes_per_samp); + WriteUint16(os, 8 * bytes_per_samp); + os << "data"; + WriteUint32(os, subchunk2size); + + const BaseFloat *data_ptr = data_.Data(); + int32 stride = data_.Stride(); + + int num_clipped = 0; + for (int32 i = 0; i < num_samp; i++) { + for (int32 j = 0; j < num_chan; j++) { + int32 elem = static_cast(trunc(data_ptr[j * stride + i])); + int16 elem_16 = static_cast(elem); + if (elem < std::numeric_limits::min()) { + elem_16 = std::numeric_limits::min(); + ++num_clipped; + } else if (elem > std::numeric_limits::max()) { + elem_16 = std::numeric_limits::max(); + ++num_clipped; + } +#ifdef __BIG_ENDIAN__ + KALDI_SWAP2(elem_16); +#endif + os.write(reinterpret_cast(&elem_16), 2); + } + } + if (os.fail()) KALDI_ERR << "Error writing wave data to stream."; + if (num_clipped > 0) + KALDI_WARN << "WARNING: clipped " << num_clipped + << " samples out of total " << num_chan * num_samp + << ". Reduce volume?"; +} + + +} // end namespace kaldi diff --git a/runtime/engine/common/frontend/wave-reader.h b/runtime/engine/common/frontend/wave-reader.h new file mode 100644 index 00000000..6cd471b8 --- /dev/null +++ b/runtime/engine/common/frontend/wave-reader.h @@ -0,0 +1,248 @@ +// feat/wave-reader.h + +// Copyright 2009-2011 Karel Vesely; Microsoft Corporation +// 2013 Florent Masson +// 2013 Johns Hopkins University (author: Daniel Povey) + +// See ../../COPYING for clarification regarding multiple authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +// MERCHANTABLITY OR NON-INFRINGEMENT. +// See the Apache 2 License for the specific language governing permissions and +// limitations under the License. + + +/* +// THE WAVE FORMAT IS SPECIFIED IN: +// https:// ccrma.stanford.edu/courses/422/projects/WaveFormat/ +// +// +// +// RIFF +// | +// WAVE +// | \ \ \ +// fmt_ data ... data +// +// +// Riff is a general container, which usually contains one WAVE chunk +// each WAVE chunk has header sub-chunk 'fmt_' +// and one or more data sub-chunks 'data' +// +// [Note from Dan: to say that the wave format was ever "specified" anywhere is +// not quite right. The guy who invented the wave format attempted to create +// a formal specification but it did not completely make sense. And there +// doesn't seem to be a consensus on what makes a valid wave file, +// particularly where the accuracy of header information is concerned.] +*/ + + +#ifndef KALDI_FEAT_WAVE_READER_H_ +#define KALDI_FEAT_WAVE_READER_H_ + +#include + +#include "base/kaldi-types.h" +#include "matrix/kaldi-matrix.h" +#include "matrix/kaldi-vector.h" + + +namespace kaldi { + +/// For historical reasons, we scale waveforms to the range +/// (2^15-1)*[-1, 1], not the usual default DSP range [-1, 1]. +const BaseFloat kWaveSampleMax = 32768.0; + +/// This class reads and hold wave file header information. +class WaveInfo { + public: + WaveInfo() + : samp_freq_(0), samp_count_(0), num_channels_(0), reverse_bytes_(0) {} + + /// Is stream size unknown? Duration and SampleCount not valid if true. + bool IsStreamed() const { return samp_count_ < 0; } + + /// Sample frequency, Hz. + BaseFloat SampFreq() const { return samp_freq_; } + + /// Number of samples in stream. Invalid if IsStreamed() is true. + uint32 SampleCount() const { return samp_count_; } + + /// Approximate duration, seconds. Invalid if IsStreamed() is true. + BaseFloat Duration() const { return samp_count_ / samp_freq_; } + + /// Number of channels, 1 to 16. + int32 NumChannels() const { return num_channels_; } + + /// Bytes per sample. + size_t BlockAlign() const { return 2 * num_channels_; } + + /// Wave data bytes. Invalid if IsStreamed() is true. + size_t DataBytes() const { return samp_count_ * BlockAlign(); } + + /// Is data file byte order different from machine byte order? + bool ReverseBytes() const { return reverse_bytes_; } + + /// 'is' should be opened in binary mode. Read() will throw on error. + /// On success 'is' will be positioned at the beginning of wave data. + void Read(std::istream &is); + + private: + BaseFloat samp_freq_; + int32 samp_count_; // 0 if empty, -1 if undefined length. + uint8 num_channels_; + bool reverse_bytes_; // File endianness differs from host. +}; + +/// This class's purpose is to read in Wave files. +class WaveData { + public: + WaveData(BaseFloat samp_freq, const MatrixBase &data) + : data_(data), samp_freq_(samp_freq) {} + + WaveData() : samp_freq_(0.0) {} + + /// Read() will throw on error. It's valid to call Read() more than once-- + /// in this case it will destroy what was there before. + /// "is" should be opened in binary mode. + void Read(std::istream &is); + + /// Write() will throw on error. os should be opened in binary mode. + void Write(std::ostream &os) const; + + // This function returns the wave data-- it's in a matrix + // because there may be multiple channels. In the normal case + // there's just one channel so Data() will have one row. + const Matrix &Data() const { return data_; } + + BaseFloat SampFreq() const { return samp_freq_; } + + // Returns the duration in seconds + BaseFloat Duration() const { return data_.NumCols() / samp_freq_; } + + void CopyFrom(const WaveData &other) { + samp_freq_ = other.samp_freq_; + data_.CopyFromMat(other.data_); + } + + void Clear() { + data_.Resize(0, 0); + samp_freq_ = 0.0; + } + + void Swap(WaveData *other) { + data_.Swap(&(other->data_)); + std::swap(samp_freq_, other->samp_freq_); + } + + private: + static const uint32 kBlockSize = 1024 * 1024; // Use 1M bytes. + Matrix data_; + BaseFloat samp_freq_; +}; + + +// Holder class for .wav files that enables us to read (but not write) .wav +// files. c.f. util/kaldi-holder.h we don't use the KaldiObjectHolder template +// because we don't want to check for the \0B binary header. We could have faked +// it by pretending to read in the wave data in text mode after failing to find +// the \0B header, but that would have been a little ugly. +class WaveHolder { + public: + typedef WaveData T; + + static bool Write(std::ostream &os, bool binary, const T &t) { + // We don't write the binary-mode header here [always binary]. + if (!binary) + KALDI_ERR << "Wave data can only be written in binary mode."; + try { + t.Write(os); // throws exception on failure. + return true; + } catch (const std::exception &e) { + KALDI_WARN << "Exception caught in WaveHolder object (writing). " + << e.what(); + return false; // write failure. + } + } + void Copy(const T &t) { t_.CopyFrom(t); } + + static bool IsReadInBinary() { return true; } + + void Clear() { t_.Clear(); } + + T &Value() { return t_; } + + WaveHolder &operator=(const WaveHolder &other) { + t_.CopyFrom(other.t_); + return *this; + } + WaveHolder(const WaveHolder &other) : t_(other.t_) {} + + WaveHolder() {} + + bool Read(std::istream &is) { + // We don't look for the binary-mode header here [always binary] + try { + t_.Read(is); // Throws exception on failure. + return true; + } catch (const std::exception &e) { + KALDI_WARN << "Exception caught in WaveHolder::Read(). " + << e.what(); + return false; + } + } + + void Swap(WaveHolder *other) { t_.Swap(&(other->t_)); } + + bool ExtractRange(const WaveHolder &other, const std::string &range) { + KALDI_ERR << "ExtractRange is not defined for this type of holder."; + return false; + } + + private: + T t_; +}; + +// This is like WaveHolder but when you just want the metadata- +// it leaves the actual data undefined, it doesn't read it. +class WaveInfoHolder { + public: + typedef WaveInfo T; + + void Clear() { info_ = WaveInfo(); } + void Swap(WaveInfoHolder *other) { std::swap(info_, other->info_); } + T &Value() { return info_; } + static bool IsReadInBinary() { return true; } + + bool Read(std::istream &is) { + try { + info_.Read(is); // Throws exception on failure. + return true; + } catch (const std::exception &e) { + KALDI_WARN << "Exception caught in WaveInfoHolder::Read(). " + << e.what(); + return false; + } + } + + bool ExtractRange(const WaveInfoHolder &other, const std::string &range) { + KALDI_ERR << "ExtractRange is not defined for this type of holder."; + return false; + } + + private: + WaveInfo info_; +}; + + +} // namespace kaldi + +#endif // KALDI_FEAT_WAVE_READER_H_ diff --git a/speechx/speechx/common/matrix/CMakeLists.txt b/runtime/engine/common/matrix/CMakeLists.txt similarity index 100% rename from speechx/speechx/common/matrix/CMakeLists.txt rename to runtime/engine/common/matrix/CMakeLists.txt diff --git a/speechx/speechx/common/matrix/kaldi-matrix-inl.h b/runtime/engine/common/matrix/kaldi-matrix-inl.h similarity index 100% rename from speechx/speechx/common/matrix/kaldi-matrix-inl.h rename to runtime/engine/common/matrix/kaldi-matrix-inl.h diff --git a/speechx/speechx/common/matrix/kaldi-matrix.cc b/runtime/engine/common/matrix/kaldi-matrix.cc similarity index 100% rename from speechx/speechx/common/matrix/kaldi-matrix.cc rename to runtime/engine/common/matrix/kaldi-matrix.cc diff --git a/speechx/speechx/common/matrix/kaldi-matrix.h b/runtime/engine/common/matrix/kaldi-matrix.h similarity index 100% rename from speechx/speechx/common/matrix/kaldi-matrix.h rename to runtime/engine/common/matrix/kaldi-matrix.h diff --git a/speechx/speechx/common/matrix/kaldi-vector-inl.h b/runtime/engine/common/matrix/kaldi-vector-inl.h similarity index 100% rename from speechx/speechx/common/matrix/kaldi-vector-inl.h rename to runtime/engine/common/matrix/kaldi-vector-inl.h diff --git a/speechx/speechx/common/matrix/kaldi-vector.cc b/runtime/engine/common/matrix/kaldi-vector.cc similarity index 100% rename from speechx/speechx/common/matrix/kaldi-vector.cc rename to runtime/engine/common/matrix/kaldi-vector.cc diff --git a/speechx/speechx/common/matrix/kaldi-vector.h b/runtime/engine/common/matrix/kaldi-vector.h similarity index 100% rename from speechx/speechx/common/matrix/kaldi-vector.h rename to runtime/engine/common/matrix/kaldi-vector.h diff --git a/speechx/speechx/common/matrix/matrix-common.h b/runtime/engine/common/matrix/matrix-common.h similarity index 100% rename from speechx/speechx/common/matrix/matrix-common.h rename to runtime/engine/common/matrix/matrix-common.h diff --git a/speechx/speechx/common/utils/CMakeLists.txt b/runtime/engine/common/utils/CMakeLists.txt similarity index 100% rename from speechx/speechx/common/utils/CMakeLists.txt rename to runtime/engine/common/utils/CMakeLists.txt diff --git a/speechx/speechx/common/utils/file_utils.cc b/runtime/engine/common/utils/file_utils.cc similarity index 100% rename from speechx/speechx/common/utils/file_utils.cc rename to runtime/engine/common/utils/file_utils.cc diff --git a/speechx/speechx/common/utils/file_utils.h b/runtime/engine/common/utils/file_utils.h similarity index 100% rename from speechx/speechx/common/utils/file_utils.h rename to runtime/engine/common/utils/file_utils.h diff --git a/speechx/speechx/common/utils/math.cc b/runtime/engine/common/utils/math.cc similarity index 100% rename from speechx/speechx/common/utils/math.cc rename to runtime/engine/common/utils/math.cc index e5832cbd..1f0c9c93 100644 --- a/speechx/speechx/common/utils/math.cc +++ b/runtime/engine/common/utils/math.cc @@ -20,8 +20,8 @@ #include #include #include -#include #include +#include #include diff --git a/speechx/speechx/common/utils/math.h b/runtime/engine/common/utils/math.h similarity index 100% rename from speechx/speechx/common/utils/math.h rename to runtime/engine/common/utils/math.h diff --git a/runtime/engine/common/utils/picojson.h b/runtime/engine/common/utils/picojson.h new file mode 100644 index 00000000..2ac265f5 --- /dev/null +++ b/runtime/engine/common/utils/picojson.h @@ -0,0 +1,1230 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011-2014 Kazuho Oku + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef picojson_h +#define picojson_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PICOJSON_USE_INT64 1 + +// for isnan/isinf +#if __cplusplus >= 201103L +#include +#else +extern "C" { +#ifdef _MSC_VER +#include +#elif defined(__INTEL_COMPILER) +#include +#else +#include +#endif +} +#endif + +#ifndef PICOJSON_USE_RVALUE_REFERENCE +#if (defined(__cpp_rvalue_references) && __cpp_rvalue_references >= 200610) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) +#define PICOJSON_USE_RVALUE_REFERENCE 1 +#else +#define PICOJSON_USE_RVALUE_REFERENCE 0 +#endif +#endif // PICOJSON_USE_RVALUE_REFERENCE + +#ifndef PICOJSON_NOEXCEPT +#if PICOJSON_USE_RVALUE_REFERENCE +#define PICOJSON_NOEXCEPT noexcept +#else +#define PICOJSON_NOEXCEPT throw() +#endif +#endif + +// experimental support for int64_t (see README.mkdn for detail) +#ifdef PICOJSON_USE_INT64 +#define __STDC_FORMAT_MACROS +#include +#if __cplusplus >= 201103L +#include +#else +extern "C" { +#include +} +#endif +#endif + +// to disable the use of localeconv(3), set PICOJSON_USE_LOCALE to 0 +#ifndef PICOJSON_USE_LOCALE +#define PICOJSON_USE_LOCALE 1 +#endif +#if PICOJSON_USE_LOCALE +extern "C" { +#include +} +#endif + +#ifndef PICOJSON_ASSERT +#define PICOJSON_ASSERT(e) \ + do { \ + if (!(e)) throw std::runtime_error(#e); \ + } while (0) +#endif + +#ifdef _MSC_VER +#define SNPRINTF _snprintf_s +#pragma warning(push) +#pragma warning(disable : 4244) // conversion from int to char +#pragma warning(disable : 4127) // conditional expression is constant +#pragma warning(disable : 4702) // unreachable code +#pragma warning(disable : 4706) // assignment within conditional expression +#else +#define SNPRINTF snprintf +#endif + +namespace picojson { + +enum { + null_type, + boolean_type, + number_type, + string_type, + array_type, + object_type +#ifdef PICOJSON_USE_INT64 + , + int64_type +#endif +}; + +enum { INDENT_WIDTH = 2, DEFAULT_MAX_DEPTHS = 100 }; + +struct null {}; + +class value { + public: + typedef std::vector array; + typedef std::map object; + union _storage { + bool boolean_; + double number_; +#ifdef PICOJSON_USE_INT64 + int64_t int64_; +#endif + std::string *string_; + array *array_; + object *object_; + }; + + protected: + int type_; + _storage u_; + + public: + value(); + value(int type, bool); + explicit value(bool b); +#ifdef PICOJSON_USE_INT64 + explicit value(int64_t i); +#endif + explicit value(double n); + explicit value(const std::string &s); + explicit value(const array &a); + explicit value(const object &o); +#if PICOJSON_USE_RVALUE_REFERENCE + explicit value(std::string &&s); + explicit value(array &&a); + explicit value(object &&o); +#endif + explicit value(const char *s); + value(const char *s, size_t len); + ~value(); + value(const value &x); + value &operator=(const value &x); +#if PICOJSON_USE_RVALUE_REFERENCE + value(value &&x) PICOJSON_NOEXCEPT; + value &operator=(value &&x) PICOJSON_NOEXCEPT; +#endif + void swap(value &x) PICOJSON_NOEXCEPT; + template + bool is() const; + template + const T &get() const; + template + T &get(); + template + void set(const T &); +#if PICOJSON_USE_RVALUE_REFERENCE + template + void set(T &&); +#endif + bool evaluate_as_boolean() const; + const value &get(const size_t idx) const; + const value &get(const std::string &key) const; + value &get(const size_t idx); + value &get(const std::string &key); + + bool contains(const size_t idx) const; + bool contains(const std::string &key) const; + std::string to_str() const; + template + void serialize(Iter os, bool prettify = false) const; + std::string serialize(bool prettify = false) const; + + private: + template + value(const T *); // intentionally defined to block implicit conversion of + // pointer to bool + template + static void _indent(Iter os, int indent); + template + void _serialize(Iter os, int indent) const; + std::string _serialize(int indent) const; + void clear(); +}; + +typedef value::array array; +typedef value::object object; + +inline value::value() : type_(null_type), u_() {} + +inline value::value(int type, bool) : type_(type), u_() { + switch (type) { +#define INIT(p, v) \ + case p##type: \ + u_.p = v; \ + break + INIT(boolean_, false); + INIT(number_, 0.0); +#ifdef PICOJSON_USE_INT64 + INIT(int64_, 0); +#endif + INIT(string_, new std::string()); + INIT(array_, new array()); + INIT(object_, new object()); +#undef INIT + default: + break; + } +} + +inline value::value(bool b) : type_(boolean_type), u_() { u_.boolean_ = b; } + +#ifdef PICOJSON_USE_INT64 +inline value::value(int64_t i) : type_(int64_type), u_() { u_.int64_ = i; } +#endif + +inline value::value(double n) : type_(number_type), u_() { + if ( +#ifdef _MSC_VER + !_finite(n) +#elif __cplusplus >= 201103L + std::isnan(n) || std::isinf(n) +#else + isnan(n) || isinf(n) +#endif + ) { + throw std::overflow_error(""); + } + u_.number_ = n; +} + +inline value::value(const std::string &s) : type_(string_type), u_() { + u_.string_ = new std::string(s); +} + +inline value::value(const array &a) : type_(array_type), u_() { + u_.array_ = new array(a); +} + +inline value::value(const object &o) : type_(object_type), u_() { + u_.object_ = new object(o); +} + +#if PICOJSON_USE_RVALUE_REFERENCE +inline value::value(std::string &&s) : type_(string_type), u_() { + u_.string_ = new std::string(std::move(s)); +} + +inline value::value(array &&a) : type_(array_type), u_() { + u_.array_ = new array(std::move(a)); +} + +inline value::value(object &&o) : type_(object_type), u_() { + u_.object_ = new object(std::move(o)); +} +#endif + +inline value::value(const char *s) : type_(string_type), u_() { + u_.string_ = new std::string(s); +} + +inline value::value(const char *s, size_t len) : type_(string_type), u_() { + u_.string_ = new std::string(s, len); +} + +inline void value::clear() { + switch (type_) { +#define DEINIT(p) \ + case p##type: \ + delete u_.p; \ + break + DEINIT(string_); + DEINIT(array_); + DEINIT(object_); +#undef DEINIT + default: + break; + } +} + +inline value::~value() { clear(); } + +inline value::value(const value &x) : type_(x.type_), u_() { + switch (type_) { +#define INIT(p, v) \ + case p##type: \ + u_.p = v; \ + break + INIT(string_, new std::string(*x.u_.string_)); + INIT(array_, new array(*x.u_.array_)); + INIT(object_, new object(*x.u_.object_)); +#undef INIT + default: + u_ = x.u_; + break; + } +} + +inline value &value::operator=(const value &x) { + if (this != &x) { + value t(x); + swap(t); + } + return *this; +} + +#if PICOJSON_USE_RVALUE_REFERENCE +inline value::value(value &&x) PICOJSON_NOEXCEPT : type_(null_type), u_() { + swap(x); +} +inline value &value::operator=(value &&x) PICOJSON_NOEXCEPT { + swap(x); + return *this; +} +#endif +inline void value::swap(value &x) PICOJSON_NOEXCEPT { + std::swap(type_, x.type_); + std::swap(u_, x.u_); +} + +#define IS(ctype, jtype) \ + template <> \ + inline bool value::is() const { \ + return type_ == jtype##_type; \ + } +IS(null, null) +IS(bool, boolean) +#ifdef PICOJSON_USE_INT64 +IS(int64_t, int64) +#endif +IS(std::string, string) +IS(array, array) +IS(object, object) +#undef IS +template <> +inline bool value::is() const { + return type_ == number_type +#ifdef PICOJSON_USE_INT64 + || type_ == int64_type +#endif + ; +} + +#define GET(ctype, var) \ + template <> \ + inline const ctype &value::get() const { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" && \ + is()); \ + return var; \ + } \ + template <> \ + inline ctype &value::get() { \ + PICOJSON_ASSERT("type mismatch! call is() before get()" && \ + is()); \ + return var; \ + } +GET(bool, u_.boolean_) +GET(std::string, *u_.string_) +GET(array, *u_.array_) +GET(object, *u_.object_) +#ifdef PICOJSON_USE_INT64 +GET(double, + (type_ == int64_type && + (const_cast(this)->type_ = number_type, + (const_cast(this)->u_.number_ = u_.int64_)), + u_.number_)) +GET(int64_t, u_.int64_) +#else +GET(double, u_.number_) +#endif +#undef GET + +#define SET(ctype, jtype, setter) \ + template <> \ + inline void value::set(const ctype &_val) { \ + clear(); \ + type_ = jtype##_type; \ + setter \ + } +SET(bool, boolean, u_.boolean_ = _val;) +SET(std::string, string, u_.string_ = new std::string(_val);) +SET(array, array, u_.array_ = new array(_val);) +SET(object, object, u_.object_ = new object(_val);) +SET(double, number, u_.number_ = _val;) +#ifdef PICOJSON_USE_INT64 +SET(int64_t, int64, u_.int64_ = _val;) +#endif +#undef SET + +#if PICOJSON_USE_RVALUE_REFERENCE +#define MOVESET(ctype, jtype, setter) \ + template <> \ + inline void value::set(ctype && _val) { \ + clear(); \ + type_ = jtype##_type; \ + setter \ + } +MOVESET(std::string, string, u_.string_ = new std::string(std::move(_val));) +MOVESET(array, array, u_.array_ = new array(std::move(_val));) +MOVESET(object, object, u_.object_ = new object(std::move(_val));) +#undef MOVESET +#endif + +inline bool value::evaluate_as_boolean() const { + switch (type_) { + case null_type: + return false; + case boolean_type: + return u_.boolean_; + case number_type: + return u_.number_ != 0; +#ifdef PICOJSON_USE_INT64 + case int64_type: + return u_.int64_ != 0; +#endif + case string_type: + return !u_.string_->empty(); + default: + return true; + } +} + +inline const value &value::get(const size_t idx) const { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; +} + +inline value &value::get(const size_t idx) { + static value s_null; + PICOJSON_ASSERT(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; +} + +inline const value &value::get(const std::string &key) const { + static value s_null; + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; +} + +inline value &value::get(const std::string &key) { + static value s_null; + PICOJSON_ASSERT(is()); + object::iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; +} + +inline bool value::contains(const size_t idx) const { + PICOJSON_ASSERT(is()); + return idx < u_.array_->size(); +} + +inline bool value::contains(const std::string &key) const { + PICOJSON_ASSERT(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end(); +} + +inline std::string value::to_str() const { + switch (type_) { + case null_type: + return "null"; + case boolean_type: + return u_.boolean_ ? "true" : "false"; +#ifdef PICOJSON_USE_INT64 + case int64_type: { + char buf[sizeof("-9223372036854775808")]; + SNPRINTF(buf, sizeof(buf), "%" PRId64, u_.int64_); + return buf; + } +#endif + case number_type: { + char buf[256]; + double tmp; + SNPRINTF( + buf, + sizeof(buf), + fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 + ? "%.f" + : "%.17g", + u_.number_); +#if PICOJSON_USE_LOCALE + char *decimal_point = localeconv()->decimal_point; + if (strcmp(decimal_point, ".") != 0) { + size_t decimal_point_len = strlen(decimal_point); + for (char *p = buf; *p != '\0'; ++p) { + if (strncmp(p, decimal_point, decimal_point_len) == 0) { + return std::string(buf, p) + "." + + (p + decimal_point_len); + } + } + } +#endif + return buf; + } + case string_type: + return *u_.string_; + case array_type: + return "array"; + case object_type: + return "object"; + default: + PICOJSON_ASSERT(0); +#ifdef _MSC_VER + __assume(0); +#endif + } + return std::string(); +} + +template +void copy(const std::string &s, Iter oi) { + std::copy(s.begin(), s.end(), oi); +} + +template +struct serialize_str_char { + Iter oi; + void operator()(char c) { + switch (c) { +#define MAP(val, sym) \ + case val: \ + copy(sym, oi); \ + break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); +#undef MAP + default: + if (static_cast(c) < 0x20 || c == 0x7f) { + char buf[7]; + SNPRINTF(buf, sizeof(buf), "\\u%04x", c & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = c; + } + break; + } + } +}; + +template +void serialize_str(const std::string &s, Iter oi) { + *oi++ = '"'; + serialize_str_char process_char = {oi}; + std::for_each(s.begin(), s.end(), process_char); + *oi++ = '"'; +} + +template +void value::serialize(Iter oi, bool prettify) const { + return _serialize(oi, prettify ? 0 : -1); +} + +inline std::string value::serialize(bool prettify) const { + return _serialize(prettify ? 0 : -1); +} + +template +void value::_indent(Iter oi, int indent) { + *oi++ = '\n'; + for (int i = 0; i < indent * INDENT_WIDTH; ++i) { + *oi++ = ' '; + } +} + +template +void value::_serialize(Iter oi, int indent) const { + switch (type_) { + case string_type: + serialize_str(*u_.string_, oi); + break; + case array_type: { + *oi++ = '['; + if (indent != -1) { + ++indent; + } + for (array::const_iterator i = u_.array_->begin(); + i != u_.array_->end(); + ++i) { + if (i != u_.array_->begin()) { + *oi++ = ','; + } + if (indent != -1) { + _indent(oi, indent); + } + i->_serialize(oi, indent); + } + if (indent != -1) { + --indent; + if (!u_.array_->empty()) { + _indent(oi, indent); + } + } + *oi++ = ']'; + break; + } + case object_type: { + *oi++ = '{'; + if (indent != -1) { + ++indent; + } + for (object::const_iterator i = u_.object_->begin(); + i != u_.object_->end(); + ++i) { + if (i != u_.object_->begin()) { + *oi++ = ','; + } + if (indent != -1) { + _indent(oi, indent); + } + serialize_str(i->first, oi); + *oi++ = ':'; + if (indent != -1) { + *oi++ = ' '; + } + i->second._serialize(oi, indent); + } + if (indent != -1) { + --indent; + if (!u_.object_->empty()) { + _indent(oi, indent); + } + } + *oi++ = '}'; + break; + } + default: + copy(to_str(), oi); + break; + } + if (indent == 0) { + *oi++ = '\n'; + } +} + +inline std::string value::_serialize(int indent) const { + std::string s; + _serialize(std::back_inserter(s), indent); + return s; +} + +template +class input { + protected: + Iter cur_, end_; + bool consumed_; + int line_; + + public: + input(const Iter &first, const Iter &last) + : cur_(first), end_(last), consumed_(false), line_(1) {} + int getc() { + if (consumed_) { + if (*cur_ == '\n') { + ++line_; + } + ++cur_; + } + if (cur_ == end_) { + consumed_ = false; + return -1; + } + consumed_ = true; + return *cur_ & 0xff; + } + void ungetc() { consumed_ = false; } + Iter cur() const { + if (consumed_) { + input *self = const_cast *>(this); + self->consumed_ = false; + ++self->cur_; + } + return cur_; + } + int line() const { return line_; } + void skip_ws() { + while (1) { + int ch = getc(); + if (!(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; + } + } + } + bool expect(const int expected) { + skip_ws(); + if (getc() != expected) { + ungetc(); + return false; + } + return true; + } + bool match(const std::string &pattern) { + for (std::string::const_iterator pi(pattern.begin()); + pi != pattern.end(); + ++pi) { + if (getc() != *pi) { + ungetc(); + return false; + } + } + return true; + } +}; + +template +inline int _parse_quadhex(input &in) { + int uni_ch = 0, hex; + for (int i = 0; i < 4; i++) { + if ((hex = in.getc()) == -1) { + return -1; + } + if ('0' <= hex && hex <= '9') { + hex -= '0'; + } else if ('A' <= hex && hex <= 'F') { + hex -= 'A' - 0xa; + } else if ('a' <= hex && hex <= 'f') { + hex -= 'a' - 0xa; + } else { + in.ungetc(); + return -1; + } + uni_ch = uni_ch * 16 + hex; + } + return uni_ch; +} + +template +inline bool _parse_codepoint(String &out, input &in) { + int uni_ch; + if ((uni_ch = _parse_quadhex(in)) == -1) { + return false; + } + if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { + if (0xdc00 <= uni_ch) { + // a second 16-bit of a surrogate pair appeared + return false; + } + // first 16-bit of surrogate pair, get the next one + if (in.getc() != '\\' || in.getc() != 'u') { + in.ungetc(); + return false; + } + int second = _parse_quadhex(in); + if (!(0xdc00 <= second && second <= 0xdfff)) { + return false; + } + uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); + uni_ch += 0x10000; + } + if (uni_ch < 0x80) { + out.push_back(static_cast(uni_ch)); + } else { + if (uni_ch < 0x800) { + out.push_back(static_cast(0xc0 | (uni_ch >> 6))); + } else { + if (uni_ch < 0x10000) { + out.push_back(static_cast(0xe0 | (uni_ch >> 12))); + } else { + out.push_back(static_cast(0xf0 | (uni_ch >> 18))); + out.push_back( + static_cast(0x80 | ((uni_ch >> 12) & 0x3f))); + } + out.push_back(static_cast(0x80 | ((uni_ch >> 6) & 0x3f))); + } + out.push_back(static_cast(0x80 | (uni_ch & 0x3f))); + } + return true; +} + +template +inline bool _parse_string(String &out, input &in) { + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) \ + case sym: \ + out.push_back(val); \ + break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); +#undef MAP + case 'u': + if (!_parse_codepoint(out, in)) { + return false; + } + break; + default: + return false; + } + } else { + out.push_back(static_cast(ch)); + } + } + return false; +} + +template +inline bool _parse_array(Context &ctx, input &in) { + if (!ctx.parse_array_start()) { + return false; + } + size_t idx = 0; + if (in.expect(']')) { + return ctx.parse_array_stop(idx); + } + do { + if (!ctx.parse_array_item(in, idx)) { + return false; + } + idx++; + } while (in.expect(',')); + return in.expect(']') && ctx.parse_array_stop(idx); +} + +template +inline bool _parse_object(Context &ctx, input &in) { + if (!ctx.parse_object_start()) { + return false; + } + if (in.expect('}')) { + return ctx.parse_object_stop(); + } + do { + std::string key; + if (!in.expect('"') || !_parse_string(key, in) || !in.expect(':')) { + return false; + } + if (!ctx.parse_object_item(in, key)) { + return false; + } + } while (in.expect(',')); + return in.expect('}') && ctx.parse_object_stop(); +} + +template +inline std::string _parse_number(input &in) { + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == 'e' || + ch == 'E') { + num_str.push_back(static_cast(ch)); + } else if (ch == '.') { +#if PICOJSON_USE_LOCALE + num_str += localeconv()->decimal_point; +#else + num_str.push_back('.'); +#endif + } else { + in.ungetc(); + break; + } + } + return num_str; +} + +template +inline bool _parse(Context &ctx, input &in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, op) \ + case ch: \ + if (in.match(text) && op) { \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", ctx.set_null()); + IS('f', "alse", ctx.set_bool(false)); + IS('t', "rue", ctx.set_bool(true)); +#undef IS + case '"': + return ctx.parse_string(in); + case '[': + return _parse_array(ctx, in); + case '{': + return _parse_object(ctx, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + double f; + char *endp; + in.ungetc(); + std::string num_str(_parse_number(in)); + if (num_str.empty()) { + return false; + } +#ifdef PICOJSON_USE_INT64 + { + errno = 0; + intmax_t ival = strtoimax(num_str.c_str(), &endp, 10); + if (errno == 0 && + std::numeric_limits::min() <= ival && + ival <= std::numeric_limits::max() && + endp == num_str.c_str() + num_str.size()) { + ctx.set_int64(ival); + return true; + } + } +#endif + f = strtod(num_str.c_str(), &endp); + if (endp == num_str.c_str() + num_str.size()) { + ctx.set_number(f); + return true; + } + return false; + } + break; + } + in.ungetc(); + return false; +} + +class deny_parse_context { + public: + bool set_null() { return false; } + bool set_bool(bool) { return false; } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t) { return false; } +#endif + bool set_number(double) { return false; } + template + bool parse_string(input &) { + return false; + } + bool parse_array_start() { return false; } + template + bool parse_array_item(input &, size_t) { + return false; + } + bool parse_array_stop(size_t) { return false; } + bool parse_object_start() { return false; } + template + bool parse_object_item(input &, const std::string &) { + return false; + } +}; + +class default_parse_context { + protected: + value *out_; + size_t depths_; + + public: + default_parse_context(value *out, size_t depths = DEFAULT_MAX_DEPTHS) + : out_(out), depths_(depths) {} + bool set_null() { + *out_ = value(); + return true; + } + bool set_bool(bool b) { + *out_ = value(b); + return true; + } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t i) { + *out_ = value(i); + return true; + } +#endif + bool set_number(double f) { + *out_ = value(f); + return true; + } + template + bool parse_string(input &in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + if (depths_ == 0) return false; + --depths_; + *out_ = value(array_type, false); + return true; + } + template + bool parse_array_item(input &in, size_t) { + array &a = out_->get(); + a.push_back(value()); + default_parse_context ctx(&a.back(), depths_); + return _parse(ctx, in); + } + bool parse_array_stop(size_t) { + ++depths_; + return true; + } + bool parse_object_start() { + if (depths_ == 0) return false; + *out_ = value(object_type, false); + return true; + } + template + bool parse_object_item(input &in, const std::string &key) { + object &o = out_->get(); + default_parse_context ctx(&o[key], depths_); + return _parse(ctx, in); + } + bool parse_object_stop() { + ++depths_; + return true; + } + + private: + default_parse_context(const default_parse_context &); + default_parse_context &operator=(const default_parse_context &); +}; + +class null_parse_context { + protected: + size_t depths_; + + public: + struct dummy_str { + void push_back(int) {} + }; + + public: + null_parse_context(size_t depths = DEFAULT_MAX_DEPTHS) : depths_(depths) {} + bool set_null() { return true; } + bool set_bool(bool) { return true; } +#ifdef PICOJSON_USE_INT64 + bool set_int64(int64_t) { return true; } +#endif + bool set_number(double) { return true; } + template + bool parse_string(input &in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { + if (depths_ == 0) return false; + --depths_; + return true; + } + template + bool parse_array_item(input &in, size_t) { + return _parse(*this, in); + } + bool parse_array_stop(size_t) { + ++depths_; + return true; + } + bool parse_object_start() { + if (depths_ == 0) return false; + --depths_; + return true; + } + template + bool parse_object_item(input &in, const std::string &) { + ++depths_; + return _parse(*this, in); + } + bool parse_object_stop() { return true; } + + private: + null_parse_context(const null_parse_context &); + null_parse_context &operator=(const null_parse_context &); +}; + +// obsolete, use the version below +template +inline std::string parse(value &out, Iter &pos, const Iter &last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; +} + +template +inline Iter _parse(Context &ctx, + const Iter &first, + const Iter &last, + std::string *err) { + input in(first, last); + if (!_parse(ctx, in) && err != NULL) { + char buf[64]; + SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(static_cast(ch)); + } + } + } + return in.cur(); +} + +template +inline Iter parse(value &out, + const Iter &first, + const Iter &last, + std::string *err) { + default_parse_context ctx(&out); + return _parse(ctx, first, last, err); +} + +inline std::string parse(value &out, const std::string &s) { + std::string err; + parse(out, s.begin(), s.end(), &err); + return err; +} + +inline std::string parse(value &out, std::istream &is) { + std::string err; + parse(out, + std::istreambuf_iterator(is.rdbuf()), + std::istreambuf_iterator(), + &err); + return err; +} + +template +struct last_error_t { + static std::string s; +}; +template +std::string last_error_t::s; + +inline void set_last_error(const std::string &s) { last_error_t::s = s; } + +inline const std::string &get_last_error() { return last_error_t::s; } + +inline bool operator==(const value &x, const value &y) { + if (x.is()) return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) return y.is() && x.get() == y.get() + PICOJSON_CMP(bool); + PICOJSON_CMP(double); + PICOJSON_CMP(std::string); + PICOJSON_CMP(array); + PICOJSON_CMP(object); +#undef PICOJSON_CMP + PICOJSON_ASSERT(0); +#ifdef _MSC_VER + __assume(0); +#endif + return false; +} + +inline bool operator!=(const value &x, const value &y) { return !(x == y); } +} + +#if !PICOJSON_USE_RVALUE_REFERENCE +namespace std { +template <> +inline void swap(picojson::value &x, picojson::value &y) { + x.swap(y); +} +} +#endif + +inline std::istream &operator>>(std::istream &is, picojson::value &x) { + picojson::set_last_error(std::string()); + const std::string err(picojson::parse(x, is)); + if (!err.empty()) { + picojson::set_last_error(err); + is.setstate(std::ios::failbit); + } + return is; +} + +inline std::ostream &operator<<(std::ostream &os, const picojson::value &x) { + x.serialize(std::ostream_iterator(os)); + return os; +} +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#endif \ No newline at end of file diff --git a/speechx/speechx/common/utils/strings.cc b/runtime/engine/common/utils/strings.cc similarity index 80% rename from speechx/speechx/common/utils/strings.cc rename to runtime/engine/common/utils/strings.cc index 6aa8af47..e453cf65 100644 --- a/speechx/speechx/common/utils/strings.cc +++ b/runtime/engine/common/utils/strings.cc @@ -18,15 +18,17 @@ namespace ppspeech { -std::vector StrSplit(const std::string& str, const char *delim, bool omit_empty_string){ +std::vector StrSplit(const std::string& str, + const char* delim, + bool omit_empty_string) { std::vector outs; int start = 0; int end = str.size(); int found = 0; - while(found != std::string::npos){ + while (found != std::string::npos) { found = str.find_first_of(delim, start); // start != end condition is for when the delimiter is at the end - if (!omit_empty_string || (found != start && start != end)){ + if (!omit_empty_string || (found != start && start != end)) { outs.push_back(str.substr(start, found - start)); } start = found + 1; @@ -38,13 +40,13 @@ std::vector StrSplit(const std::string& str, const char *delim, boo std::string StrJoin(const std::vector& strs, const char* delim) { std::stringstream ss; - for (ssize_t i = 0; i < strs.size(); ++i){ + for (ssize_t i = 0; i < strs.size(); ++i) { ss << strs[i]; - if ( i < strs.size() -1){ + if (i < strs.size() - 1) { ss << std::string(delim); } } return ss.str(); } -} // namespace ppspeech \ No newline at end of file +} // namespace ppspeech \ No newline at end of file diff --git a/speechx/speechx/common/utils/strings.h b/runtime/engine/common/utils/strings.h similarity index 79% rename from speechx/speechx/common/utils/strings.h rename to runtime/engine/common/utils/strings.h index e2629164..175506a5 100644 --- a/speechx/speechx/common/utils/strings.h +++ b/runtime/engine/common/utils/strings.h @@ -14,13 +14,15 @@ #pragma once -#include #include +#include namespace ppspeech { -std::vector StrSplit(const std::string& str, const char *delim, bool omit_empty_string=true); +std::vector StrSplit(const std::string& str, + const char* delim, + bool omit_empty_string = true); std::string StrJoin(const std::vector& strs, const char* delim); -} // namespace ppspeech \ No newline at end of file +} // namespace ppspeech \ No newline at end of file diff --git a/speechx/speechx/common/utils/strings_test.cc b/runtime/engine/common/utils/strings_test.cc similarity index 81% rename from speechx/speechx/common/utils/strings_test.cc rename to runtime/engine/common/utils/strings_test.cc index a2950d32..f158a532 100644 --- a/speechx/speechx/common/utils/strings_test.cc +++ b/runtime/engine/common/utils/strings_test.cc @@ -15,16 +15,16 @@ #include "utils/strings.h" -#include #include +#include TEST(StringTest, StrSplitTest) { - using ::testing::ElementsAre; + using ::testing::ElementsAre; - std::string test_str = "hello world"; - std::vector outs = ppspeech::StrSplit(test_str, " \t"); - EXPECT_THAT(outs, ElementsAre("hello", "world")); + std::string test_str = "hello world"; + std::vector outs = ppspeech::StrSplit(test_str, " \t"); + EXPECT_THAT(outs, ElementsAre("hello", "world")); } diff --git a/speechx/speechx/kaldi/CMakeLists.txt b/runtime/engine/kaldi/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/CMakeLists.txt rename to runtime/engine/kaldi/CMakeLists.txt diff --git a/speechx/speechx/kaldi/base/CMakeLists.txt b/runtime/engine/kaldi/base/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/base/CMakeLists.txt rename to runtime/engine/kaldi/base/CMakeLists.txt diff --git a/speechx/speechx/kaldi/base/io-funcs-inl.h b/runtime/engine/kaldi/base/io-funcs-inl.h similarity index 100% rename from speechx/speechx/kaldi/base/io-funcs-inl.h rename to runtime/engine/kaldi/base/io-funcs-inl.h diff --git a/speechx/speechx/kaldi/base/io-funcs.cc b/runtime/engine/kaldi/base/io-funcs.cc similarity index 100% rename from speechx/speechx/kaldi/base/io-funcs.cc rename to runtime/engine/kaldi/base/io-funcs.cc diff --git a/speechx/speechx/kaldi/base/io-funcs.h b/runtime/engine/kaldi/base/io-funcs.h similarity index 100% rename from speechx/speechx/kaldi/base/io-funcs.h rename to runtime/engine/kaldi/base/io-funcs.h diff --git a/speechx/speechx/kaldi/base/kaldi-common.h b/runtime/engine/kaldi/base/kaldi-common.h similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-common.h rename to runtime/engine/kaldi/base/kaldi-common.h diff --git a/speechx/speechx/kaldi/base/kaldi-error.cc b/runtime/engine/kaldi/base/kaldi-error.cc similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-error.cc rename to runtime/engine/kaldi/base/kaldi-error.cc diff --git a/speechx/speechx/kaldi/base/kaldi-error.h b/runtime/engine/kaldi/base/kaldi-error.h similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-error.h rename to runtime/engine/kaldi/base/kaldi-error.h diff --git a/speechx/speechx/kaldi/base/kaldi-math.cc b/runtime/engine/kaldi/base/kaldi-math.cc similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-math.cc rename to runtime/engine/kaldi/base/kaldi-math.cc diff --git a/speechx/speechx/kaldi/base/kaldi-math.h b/runtime/engine/kaldi/base/kaldi-math.h similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-math.h rename to runtime/engine/kaldi/base/kaldi-math.h diff --git a/speechx/speechx/kaldi/base/kaldi-types.h b/runtime/engine/kaldi/base/kaldi-types.h similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-types.h rename to runtime/engine/kaldi/base/kaldi-types.h diff --git a/speechx/speechx/kaldi/base/kaldi-utils.cc b/runtime/engine/kaldi/base/kaldi-utils.cc similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-utils.cc rename to runtime/engine/kaldi/base/kaldi-utils.cc diff --git a/speechx/speechx/kaldi/base/kaldi-utils.h b/runtime/engine/kaldi/base/kaldi-utils.h similarity index 100% rename from speechx/speechx/kaldi/base/kaldi-utils.h rename to runtime/engine/kaldi/base/kaldi-utils.h diff --git a/speechx/speechx/kaldi/base/timer.cc b/runtime/engine/kaldi/base/timer.cc similarity index 100% rename from speechx/speechx/kaldi/base/timer.cc rename to runtime/engine/kaldi/base/timer.cc diff --git a/speechx/speechx/kaldi/base/timer.h b/runtime/engine/kaldi/base/timer.h similarity index 100% rename from speechx/speechx/kaldi/base/timer.h rename to runtime/engine/kaldi/base/timer.h diff --git a/speechx/speechx/kaldi/base/version.h b/runtime/engine/kaldi/base/version.h similarity index 100% rename from speechx/speechx/kaldi/base/version.h rename to runtime/engine/kaldi/base/version.h diff --git a/speechx/speechx/kaldi/decoder/CMakeLists.txt b/runtime/engine/kaldi/decoder/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/decoder/CMakeLists.txt rename to runtime/engine/kaldi/decoder/CMakeLists.txt diff --git a/speechx/speechx/kaldi/decoder/decodable-itf.h b/runtime/engine/kaldi/decoder/decodable-itf.h similarity index 100% rename from speechx/speechx/kaldi/decoder/decodable-itf.h rename to runtime/engine/kaldi/decoder/decodable-itf.h diff --git a/speechx/speechx/kaldi/decoder/lattice-faster-decoder.cc b/runtime/engine/kaldi/decoder/lattice-faster-decoder.cc similarity index 100% rename from speechx/speechx/kaldi/decoder/lattice-faster-decoder.cc rename to runtime/engine/kaldi/decoder/lattice-faster-decoder.cc diff --git a/speechx/speechx/kaldi/decoder/lattice-faster-decoder.h b/runtime/engine/kaldi/decoder/lattice-faster-decoder.h similarity index 100% rename from speechx/speechx/kaldi/decoder/lattice-faster-decoder.h rename to runtime/engine/kaldi/decoder/lattice-faster-decoder.h diff --git a/speechx/speechx/kaldi/decoder/lattice-faster-online-decoder.cc b/runtime/engine/kaldi/decoder/lattice-faster-online-decoder.cc similarity index 100% rename from speechx/speechx/kaldi/decoder/lattice-faster-online-decoder.cc rename to runtime/engine/kaldi/decoder/lattice-faster-online-decoder.cc diff --git a/speechx/speechx/kaldi/decoder/lattice-faster-online-decoder.h b/runtime/engine/kaldi/decoder/lattice-faster-online-decoder.h similarity index 100% rename from speechx/speechx/kaldi/decoder/lattice-faster-online-decoder.h rename to runtime/engine/kaldi/decoder/lattice-faster-online-decoder.h diff --git a/speechx/speechx/kaldi/fstbin/CMakeLists.txt b/runtime/engine/kaldi/fstbin/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/fstbin/CMakeLists.txt rename to runtime/engine/kaldi/fstbin/CMakeLists.txt diff --git a/speechx/speechx/kaldi/fstbin/fstaddselfloops.cc b/runtime/engine/kaldi/fstbin/fstaddselfloops.cc similarity index 100% rename from speechx/speechx/kaldi/fstbin/fstaddselfloops.cc rename to runtime/engine/kaldi/fstbin/fstaddselfloops.cc diff --git a/speechx/speechx/kaldi/fstbin/fstdeterminizestar.cc b/runtime/engine/kaldi/fstbin/fstdeterminizestar.cc similarity index 100% rename from speechx/speechx/kaldi/fstbin/fstdeterminizestar.cc rename to runtime/engine/kaldi/fstbin/fstdeterminizestar.cc diff --git a/speechx/speechx/kaldi/fstbin/fstisstochastic.cc b/runtime/engine/kaldi/fstbin/fstisstochastic.cc similarity index 100% rename from speechx/speechx/kaldi/fstbin/fstisstochastic.cc rename to runtime/engine/kaldi/fstbin/fstisstochastic.cc diff --git a/speechx/speechx/kaldi/fstbin/fstminimizeencoded.cc b/runtime/engine/kaldi/fstbin/fstminimizeencoded.cc similarity index 100% rename from speechx/speechx/kaldi/fstbin/fstminimizeencoded.cc rename to runtime/engine/kaldi/fstbin/fstminimizeencoded.cc diff --git a/speechx/speechx/kaldi/fstbin/fsttablecompose.cc b/runtime/engine/kaldi/fstbin/fsttablecompose.cc similarity index 100% rename from speechx/speechx/kaldi/fstbin/fsttablecompose.cc rename to runtime/engine/kaldi/fstbin/fsttablecompose.cc diff --git a/speechx/speechx/kaldi/fstext/CMakeLists.txt b/runtime/engine/kaldi/fstext/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/fstext/CMakeLists.txt rename to runtime/engine/kaldi/fstext/CMakeLists.txt diff --git a/speechx/speechx/kaldi/fstext/determinize-lattice-inl.h b/runtime/engine/kaldi/fstext/determinize-lattice-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/determinize-lattice-inl.h rename to runtime/engine/kaldi/fstext/determinize-lattice-inl.h diff --git a/speechx/speechx/kaldi/fstext/determinize-lattice.h b/runtime/engine/kaldi/fstext/determinize-lattice.h similarity index 100% rename from speechx/speechx/kaldi/fstext/determinize-lattice.h rename to runtime/engine/kaldi/fstext/determinize-lattice.h diff --git a/speechx/speechx/kaldi/fstext/determinize-star-inl.h b/runtime/engine/kaldi/fstext/determinize-star-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/determinize-star-inl.h rename to runtime/engine/kaldi/fstext/determinize-star-inl.h diff --git a/speechx/speechx/kaldi/fstext/determinize-star.h b/runtime/engine/kaldi/fstext/determinize-star.h similarity index 100% rename from speechx/speechx/kaldi/fstext/determinize-star.h rename to runtime/engine/kaldi/fstext/determinize-star.h diff --git a/speechx/speechx/kaldi/fstext/fstext-lib.h b/runtime/engine/kaldi/fstext/fstext-lib.h similarity index 100% rename from speechx/speechx/kaldi/fstext/fstext-lib.h rename to runtime/engine/kaldi/fstext/fstext-lib.h diff --git a/speechx/speechx/kaldi/fstext/fstext-utils-inl.h b/runtime/engine/kaldi/fstext/fstext-utils-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/fstext-utils-inl.h rename to runtime/engine/kaldi/fstext/fstext-utils-inl.h diff --git a/speechx/speechx/kaldi/fstext/fstext-utils.h b/runtime/engine/kaldi/fstext/fstext-utils.h similarity index 100% rename from speechx/speechx/kaldi/fstext/fstext-utils.h rename to runtime/engine/kaldi/fstext/fstext-utils.h diff --git a/speechx/speechx/kaldi/fstext/kaldi-fst-io-inl.h b/runtime/engine/kaldi/fstext/kaldi-fst-io-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/kaldi-fst-io-inl.h rename to runtime/engine/kaldi/fstext/kaldi-fst-io-inl.h diff --git a/speechx/speechx/kaldi/fstext/kaldi-fst-io.cc b/runtime/engine/kaldi/fstext/kaldi-fst-io.cc similarity index 100% rename from speechx/speechx/kaldi/fstext/kaldi-fst-io.cc rename to runtime/engine/kaldi/fstext/kaldi-fst-io.cc diff --git a/speechx/speechx/kaldi/fstext/kaldi-fst-io.h b/runtime/engine/kaldi/fstext/kaldi-fst-io.h similarity index 100% rename from speechx/speechx/kaldi/fstext/kaldi-fst-io.h rename to runtime/engine/kaldi/fstext/kaldi-fst-io.h diff --git a/speechx/speechx/kaldi/fstext/lattice-utils-inl.h b/runtime/engine/kaldi/fstext/lattice-utils-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/lattice-utils-inl.h rename to runtime/engine/kaldi/fstext/lattice-utils-inl.h diff --git a/speechx/speechx/kaldi/fstext/lattice-utils.h b/runtime/engine/kaldi/fstext/lattice-utils.h similarity index 100% rename from speechx/speechx/kaldi/fstext/lattice-utils.h rename to runtime/engine/kaldi/fstext/lattice-utils.h diff --git a/speechx/speechx/kaldi/fstext/lattice-weight.h b/runtime/engine/kaldi/fstext/lattice-weight.h similarity index 100% rename from speechx/speechx/kaldi/fstext/lattice-weight.h rename to runtime/engine/kaldi/fstext/lattice-weight.h diff --git a/speechx/speechx/kaldi/fstext/pre-determinize-inl.h b/runtime/engine/kaldi/fstext/pre-determinize-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/pre-determinize-inl.h rename to runtime/engine/kaldi/fstext/pre-determinize-inl.h diff --git a/speechx/speechx/kaldi/fstext/pre-determinize.h b/runtime/engine/kaldi/fstext/pre-determinize.h similarity index 100% rename from speechx/speechx/kaldi/fstext/pre-determinize.h rename to runtime/engine/kaldi/fstext/pre-determinize.h diff --git a/speechx/speechx/kaldi/fstext/remove-eps-local-inl.h b/runtime/engine/kaldi/fstext/remove-eps-local-inl.h similarity index 100% rename from speechx/speechx/kaldi/fstext/remove-eps-local-inl.h rename to runtime/engine/kaldi/fstext/remove-eps-local-inl.h diff --git a/speechx/speechx/kaldi/fstext/remove-eps-local.h b/runtime/engine/kaldi/fstext/remove-eps-local.h similarity index 100% rename from speechx/speechx/kaldi/fstext/remove-eps-local.h rename to runtime/engine/kaldi/fstext/remove-eps-local.h diff --git a/speechx/speechx/kaldi/fstext/table-matcher.h b/runtime/engine/kaldi/fstext/table-matcher.h similarity index 100% rename from speechx/speechx/kaldi/fstext/table-matcher.h rename to runtime/engine/kaldi/fstext/table-matcher.h diff --git a/speechx/speechx/kaldi/lat/CMakeLists.txt b/runtime/engine/kaldi/lat/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/lat/CMakeLists.txt rename to runtime/engine/kaldi/lat/CMakeLists.txt diff --git a/speechx/speechx/kaldi/lat/determinize-lattice-pruned.cc b/runtime/engine/kaldi/lat/determinize-lattice-pruned.cc similarity index 100% rename from speechx/speechx/kaldi/lat/determinize-lattice-pruned.cc rename to runtime/engine/kaldi/lat/determinize-lattice-pruned.cc diff --git a/speechx/speechx/kaldi/lat/determinize-lattice-pruned.h b/runtime/engine/kaldi/lat/determinize-lattice-pruned.h similarity index 100% rename from speechx/speechx/kaldi/lat/determinize-lattice-pruned.h rename to runtime/engine/kaldi/lat/determinize-lattice-pruned.h diff --git a/speechx/speechx/kaldi/lat/kaldi-lattice.cc b/runtime/engine/kaldi/lat/kaldi-lattice.cc similarity index 100% rename from speechx/speechx/kaldi/lat/kaldi-lattice.cc rename to runtime/engine/kaldi/lat/kaldi-lattice.cc diff --git a/speechx/speechx/kaldi/lat/kaldi-lattice.h b/runtime/engine/kaldi/lat/kaldi-lattice.h similarity index 100% rename from speechx/speechx/kaldi/lat/kaldi-lattice.h rename to runtime/engine/kaldi/lat/kaldi-lattice.h diff --git a/speechx/speechx/kaldi/lat/lattice-functions.cc b/runtime/engine/kaldi/lat/lattice-functions.cc similarity index 100% rename from speechx/speechx/kaldi/lat/lattice-functions.cc rename to runtime/engine/kaldi/lat/lattice-functions.cc diff --git a/speechx/speechx/kaldi/lat/lattice-functions.h b/runtime/engine/kaldi/lat/lattice-functions.h similarity index 100% rename from speechx/speechx/kaldi/lat/lattice-functions.h rename to runtime/engine/kaldi/lat/lattice-functions.h diff --git a/speechx/speechx/kaldi/lm/CMakeLists.txt b/runtime/engine/kaldi/lm/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/lm/CMakeLists.txt rename to runtime/engine/kaldi/lm/CMakeLists.txt diff --git a/speechx/speechx/kaldi/lm/arpa-file-parser.cc b/runtime/engine/kaldi/lm/arpa-file-parser.cc similarity index 100% rename from speechx/speechx/kaldi/lm/arpa-file-parser.cc rename to runtime/engine/kaldi/lm/arpa-file-parser.cc diff --git a/speechx/speechx/kaldi/lm/arpa-file-parser.h b/runtime/engine/kaldi/lm/arpa-file-parser.h similarity index 100% rename from speechx/speechx/kaldi/lm/arpa-file-parser.h rename to runtime/engine/kaldi/lm/arpa-file-parser.h diff --git a/speechx/speechx/kaldi/lm/arpa-lm-compiler.cc b/runtime/engine/kaldi/lm/arpa-lm-compiler.cc similarity index 100% rename from speechx/speechx/kaldi/lm/arpa-lm-compiler.cc rename to runtime/engine/kaldi/lm/arpa-lm-compiler.cc diff --git a/speechx/speechx/kaldi/lm/arpa-lm-compiler.h b/runtime/engine/kaldi/lm/arpa-lm-compiler.h similarity index 100% rename from speechx/speechx/kaldi/lm/arpa-lm-compiler.h rename to runtime/engine/kaldi/lm/arpa-lm-compiler.h diff --git a/speechx/speechx/kaldi/lmbin/CMakeLists.txt b/runtime/engine/kaldi/lmbin/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/lmbin/CMakeLists.txt rename to runtime/engine/kaldi/lmbin/CMakeLists.txt diff --git a/speechx/speechx/kaldi/lmbin/arpa2fst.cc b/runtime/engine/kaldi/lmbin/arpa2fst.cc similarity index 100% rename from speechx/speechx/kaldi/lmbin/arpa2fst.cc rename to runtime/engine/kaldi/lmbin/arpa2fst.cc diff --git a/speechx/speechx/kaldi/util/CMakeLists.txt b/runtime/engine/kaldi/util/CMakeLists.txt similarity index 100% rename from speechx/speechx/kaldi/util/CMakeLists.txt rename to runtime/engine/kaldi/util/CMakeLists.txt diff --git a/speechx/speechx/kaldi/util/basic-filebuf.h b/runtime/engine/kaldi/util/basic-filebuf.h similarity index 100% rename from speechx/speechx/kaldi/util/basic-filebuf.h rename to runtime/engine/kaldi/util/basic-filebuf.h diff --git a/speechx/speechx/kaldi/util/common-utils.h b/runtime/engine/kaldi/util/common-utils.h similarity index 100% rename from speechx/speechx/kaldi/util/common-utils.h rename to runtime/engine/kaldi/util/common-utils.h diff --git a/speechx/speechx/kaldi/util/const-integer-set-inl.h b/runtime/engine/kaldi/util/const-integer-set-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/const-integer-set-inl.h rename to runtime/engine/kaldi/util/const-integer-set-inl.h diff --git a/speechx/speechx/kaldi/util/const-integer-set.h b/runtime/engine/kaldi/util/const-integer-set.h similarity index 100% rename from speechx/speechx/kaldi/util/const-integer-set.h rename to runtime/engine/kaldi/util/const-integer-set.h diff --git a/speechx/speechx/kaldi/util/edit-distance-inl.h b/runtime/engine/kaldi/util/edit-distance-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/edit-distance-inl.h rename to runtime/engine/kaldi/util/edit-distance-inl.h diff --git a/speechx/speechx/kaldi/util/edit-distance.h b/runtime/engine/kaldi/util/edit-distance.h similarity index 100% rename from speechx/speechx/kaldi/util/edit-distance.h rename to runtime/engine/kaldi/util/edit-distance.h diff --git a/speechx/speechx/kaldi/util/hash-list-inl.h b/runtime/engine/kaldi/util/hash-list-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/hash-list-inl.h rename to runtime/engine/kaldi/util/hash-list-inl.h diff --git a/speechx/speechx/kaldi/util/hash-list.h b/runtime/engine/kaldi/util/hash-list.h similarity index 100% rename from speechx/speechx/kaldi/util/hash-list.h rename to runtime/engine/kaldi/util/hash-list.h diff --git a/speechx/speechx/kaldi/util/kaldi-cygwin-io-inl.h b/runtime/engine/kaldi/util/kaldi-cygwin-io-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-cygwin-io-inl.h rename to runtime/engine/kaldi/util/kaldi-cygwin-io-inl.h diff --git a/speechx/speechx/kaldi/util/kaldi-holder-inl.h b/runtime/engine/kaldi/util/kaldi-holder-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-holder-inl.h rename to runtime/engine/kaldi/util/kaldi-holder-inl.h diff --git a/speechx/speechx/kaldi/util/kaldi-holder.cc b/runtime/engine/kaldi/util/kaldi-holder.cc similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-holder.cc rename to runtime/engine/kaldi/util/kaldi-holder.cc diff --git a/speechx/speechx/kaldi/util/kaldi-holder.h b/runtime/engine/kaldi/util/kaldi-holder.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-holder.h rename to runtime/engine/kaldi/util/kaldi-holder.h diff --git a/speechx/speechx/kaldi/util/kaldi-io-inl.h b/runtime/engine/kaldi/util/kaldi-io-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-io-inl.h rename to runtime/engine/kaldi/util/kaldi-io-inl.h diff --git a/speechx/speechx/kaldi/util/kaldi-io.cc b/runtime/engine/kaldi/util/kaldi-io.cc similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-io.cc rename to runtime/engine/kaldi/util/kaldi-io.cc diff --git a/speechx/speechx/kaldi/util/kaldi-io.h b/runtime/engine/kaldi/util/kaldi-io.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-io.h rename to runtime/engine/kaldi/util/kaldi-io.h diff --git a/speechx/speechx/kaldi/util/kaldi-pipebuf.h b/runtime/engine/kaldi/util/kaldi-pipebuf.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-pipebuf.h rename to runtime/engine/kaldi/util/kaldi-pipebuf.h diff --git a/speechx/speechx/kaldi/util/kaldi-semaphore.cc b/runtime/engine/kaldi/util/kaldi-semaphore.cc similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-semaphore.cc rename to runtime/engine/kaldi/util/kaldi-semaphore.cc diff --git a/speechx/speechx/kaldi/util/kaldi-semaphore.h b/runtime/engine/kaldi/util/kaldi-semaphore.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-semaphore.h rename to runtime/engine/kaldi/util/kaldi-semaphore.h diff --git a/speechx/speechx/kaldi/util/kaldi-table-inl.h b/runtime/engine/kaldi/util/kaldi-table-inl.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-table-inl.h rename to runtime/engine/kaldi/util/kaldi-table-inl.h diff --git a/speechx/speechx/kaldi/util/kaldi-table.cc b/runtime/engine/kaldi/util/kaldi-table.cc similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-table.cc rename to runtime/engine/kaldi/util/kaldi-table.cc diff --git a/speechx/speechx/kaldi/util/kaldi-table.h b/runtime/engine/kaldi/util/kaldi-table.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-table.h rename to runtime/engine/kaldi/util/kaldi-table.h diff --git a/speechx/speechx/kaldi/util/kaldi-thread.cc b/runtime/engine/kaldi/util/kaldi-thread.cc similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-thread.cc rename to runtime/engine/kaldi/util/kaldi-thread.cc diff --git a/speechx/speechx/kaldi/util/kaldi-thread.h b/runtime/engine/kaldi/util/kaldi-thread.h similarity index 100% rename from speechx/speechx/kaldi/util/kaldi-thread.h rename to runtime/engine/kaldi/util/kaldi-thread.h diff --git a/speechx/speechx/kaldi/util/options-itf.h b/runtime/engine/kaldi/util/options-itf.h similarity index 100% rename from speechx/speechx/kaldi/util/options-itf.h rename to runtime/engine/kaldi/util/options-itf.h diff --git a/speechx/speechx/kaldi/util/parse-options.cc b/runtime/engine/kaldi/util/parse-options.cc similarity index 100% rename from speechx/speechx/kaldi/util/parse-options.cc rename to runtime/engine/kaldi/util/parse-options.cc diff --git a/speechx/speechx/kaldi/util/parse-options.h b/runtime/engine/kaldi/util/parse-options.h similarity index 100% rename from speechx/speechx/kaldi/util/parse-options.h rename to runtime/engine/kaldi/util/parse-options.h diff --git a/speechx/speechx/kaldi/util/simple-io-funcs.cc b/runtime/engine/kaldi/util/simple-io-funcs.cc similarity index 100% rename from speechx/speechx/kaldi/util/simple-io-funcs.cc rename to runtime/engine/kaldi/util/simple-io-funcs.cc diff --git a/speechx/speechx/kaldi/util/simple-io-funcs.h b/runtime/engine/kaldi/util/simple-io-funcs.h similarity index 100% rename from speechx/speechx/kaldi/util/simple-io-funcs.h rename to runtime/engine/kaldi/util/simple-io-funcs.h diff --git a/speechx/speechx/kaldi/util/simple-options.cc b/runtime/engine/kaldi/util/simple-options.cc similarity index 100% rename from speechx/speechx/kaldi/util/simple-options.cc rename to runtime/engine/kaldi/util/simple-options.cc diff --git a/speechx/speechx/kaldi/util/simple-options.h b/runtime/engine/kaldi/util/simple-options.h similarity index 100% rename from speechx/speechx/kaldi/util/simple-options.h rename to runtime/engine/kaldi/util/simple-options.h diff --git a/speechx/speechx/kaldi/util/stl-utils.h b/runtime/engine/kaldi/util/stl-utils.h similarity index 100% rename from speechx/speechx/kaldi/util/stl-utils.h rename to runtime/engine/kaldi/util/stl-utils.h diff --git a/speechx/speechx/kaldi/util/table-types.h b/runtime/engine/kaldi/util/table-types.h similarity index 100% rename from speechx/speechx/kaldi/util/table-types.h rename to runtime/engine/kaldi/util/table-types.h diff --git a/speechx/speechx/kaldi/util/text-utils.cc b/runtime/engine/kaldi/util/text-utils.cc similarity index 100% rename from speechx/speechx/kaldi/util/text-utils.cc rename to runtime/engine/kaldi/util/text-utils.cc diff --git a/speechx/speechx/kaldi/util/text-utils.h b/runtime/engine/kaldi/util/text-utils.h similarity index 100% rename from speechx/speechx/kaldi/util/text-utils.h rename to runtime/engine/kaldi/util/text-utils.h diff --git a/speechx/examples/.gitignore b/runtime/examples/.gitignore similarity index 80% rename from speechx/examples/.gitignore rename to runtime/examples/.gitignore index b7075fa5..38290f34 100644 --- a/speechx/examples/.gitignore +++ b/runtime/examples/.gitignore @@ -1,2 +1,3 @@ *.ark +*.scp paddle_asr_model/ diff --git a/speechx/examples/README.md b/runtime/examples/README.md similarity index 100% rename from speechx/examples/README.md rename to runtime/examples/README.md diff --git a/speechx/examples/codelab/README.md b/runtime/examples/codelab/README.md similarity index 100% rename from speechx/examples/codelab/README.md rename to runtime/examples/codelab/README.md diff --git a/speechx/examples/codelab/decoder/.gitignore b/runtime/examples/codelab/decoder/.gitignore similarity index 100% rename from speechx/examples/codelab/decoder/.gitignore rename to runtime/examples/codelab/decoder/.gitignore diff --git a/speechx/examples/codelab/decoder/README.md b/runtime/examples/codelab/decoder/README.md similarity index 100% rename from speechx/examples/codelab/decoder/README.md rename to runtime/examples/codelab/decoder/README.md diff --git a/speechx/examples/codelab/decoder/path.sh b/runtime/examples/codelab/decoder/path.sh similarity index 100% rename from speechx/examples/codelab/decoder/path.sh rename to runtime/examples/codelab/decoder/path.sh diff --git a/speechx/examples/codelab/decoder/run.sh b/runtime/examples/codelab/decoder/run.sh similarity index 100% rename from speechx/examples/codelab/decoder/run.sh rename to runtime/examples/codelab/decoder/run.sh diff --git a/speechx/examples/codelab/decoder/valgrind.sh b/runtime/examples/codelab/decoder/valgrind.sh similarity index 100% rename from speechx/examples/codelab/decoder/valgrind.sh rename to runtime/examples/codelab/decoder/valgrind.sh diff --git a/speechx/examples/codelab/feat/.gitignore b/runtime/examples/codelab/feat/.gitignore similarity index 100% rename from speechx/examples/codelab/feat/.gitignore rename to runtime/examples/codelab/feat/.gitignore diff --git a/speechx/examples/codelab/feat/README.md b/runtime/examples/codelab/feat/README.md similarity index 100% rename from speechx/examples/codelab/feat/README.md rename to runtime/examples/codelab/feat/README.md diff --git a/speechx/examples/codelab/feat/path.sh b/runtime/examples/codelab/feat/path.sh similarity index 100% rename from speechx/examples/codelab/feat/path.sh rename to runtime/examples/codelab/feat/path.sh diff --git a/speechx/examples/codelab/feat/run.sh b/runtime/examples/codelab/feat/run.sh similarity index 100% rename from speechx/examples/codelab/feat/run.sh rename to runtime/examples/codelab/feat/run.sh diff --git a/speechx/examples/codelab/feat/valgrind.sh b/runtime/examples/codelab/feat/valgrind.sh similarity index 100% rename from speechx/examples/codelab/feat/valgrind.sh rename to runtime/examples/codelab/feat/valgrind.sh diff --git a/speechx/examples/codelab/nnet/.gitignore b/runtime/examples/codelab/nnet/.gitignore similarity index 100% rename from speechx/examples/codelab/nnet/.gitignore rename to runtime/examples/codelab/nnet/.gitignore diff --git a/speechx/examples/codelab/nnet/README.md b/runtime/examples/codelab/nnet/README.md similarity index 100% rename from speechx/examples/codelab/nnet/README.md rename to runtime/examples/codelab/nnet/README.md diff --git a/speechx/examples/codelab/nnet/path.sh b/runtime/examples/codelab/nnet/path.sh similarity index 100% rename from speechx/examples/codelab/nnet/path.sh rename to runtime/examples/codelab/nnet/path.sh diff --git a/speechx/examples/codelab/nnet/run.sh b/runtime/examples/codelab/nnet/run.sh similarity index 100% rename from speechx/examples/codelab/nnet/run.sh rename to runtime/examples/codelab/nnet/run.sh diff --git a/speechx/examples/codelab/nnet/valgrind.sh b/runtime/examples/codelab/nnet/valgrind.sh similarity index 100% rename from speechx/examples/codelab/nnet/valgrind.sh rename to runtime/examples/codelab/nnet/valgrind.sh diff --git a/speechx/examples/codelab/u2/.gitignore b/runtime/examples/codelab/u2/.gitignore similarity index 100% rename from speechx/examples/codelab/u2/.gitignore rename to runtime/examples/codelab/u2/.gitignore diff --git a/speechx/examples/codelab/u2/README.md b/runtime/examples/codelab/u2/README.md similarity index 100% rename from speechx/examples/codelab/u2/README.md rename to runtime/examples/codelab/u2/README.md diff --git a/speechx/examples/codelab/u2/local/decode.sh b/runtime/examples/codelab/u2/local/decode.sh similarity index 100% rename from speechx/examples/codelab/u2/local/decode.sh rename to runtime/examples/codelab/u2/local/decode.sh diff --git a/speechx/examples/codelab/u2/local/feat.sh b/runtime/examples/codelab/u2/local/feat.sh similarity index 100% rename from speechx/examples/codelab/u2/local/feat.sh rename to runtime/examples/codelab/u2/local/feat.sh diff --git a/speechx/examples/codelab/u2/local/nnet.sh b/runtime/examples/codelab/u2/local/nnet.sh similarity index 100% rename from speechx/examples/codelab/u2/local/nnet.sh rename to runtime/examples/codelab/u2/local/nnet.sh diff --git a/speechx/examples/codelab/u2/local/recognizer.sh b/runtime/examples/codelab/u2/local/recognizer.sh similarity index 100% rename from speechx/examples/codelab/u2/local/recognizer.sh rename to runtime/examples/codelab/u2/local/recognizer.sh diff --git a/speechx/examples/codelab/u2/path.sh b/runtime/examples/codelab/u2/path.sh similarity index 100% rename from speechx/examples/codelab/u2/path.sh rename to runtime/examples/codelab/u2/path.sh diff --git a/speechx/examples/codelab/u2/run.sh b/runtime/examples/codelab/u2/run.sh similarity index 100% rename from speechx/examples/codelab/u2/run.sh rename to runtime/examples/codelab/u2/run.sh diff --git a/speechx/examples/codelab/u2/utils b/runtime/examples/codelab/u2/utils similarity index 100% rename from speechx/examples/codelab/u2/utils rename to runtime/examples/codelab/u2/utils diff --git a/speechx/examples/custom_asr/README.md b/runtime/examples/custom_asr/README.md similarity index 100% rename from speechx/examples/custom_asr/README.md rename to runtime/examples/custom_asr/README.md diff --git a/speechx/examples/custom_asr/local/compile_lexicon_token_fst.sh b/runtime/examples/custom_asr/local/compile_lexicon_token_fst.sh similarity index 100% rename from speechx/examples/custom_asr/local/compile_lexicon_token_fst.sh rename to runtime/examples/custom_asr/local/compile_lexicon_token_fst.sh diff --git a/speechx/examples/custom_asr/local/mk_slot_graph.sh b/runtime/examples/custom_asr/local/mk_slot_graph.sh similarity index 100% rename from speechx/examples/custom_asr/local/mk_slot_graph.sh rename to runtime/examples/custom_asr/local/mk_slot_graph.sh diff --git a/speechx/examples/custom_asr/local/mk_tlg_with_slot.sh b/runtime/examples/custom_asr/local/mk_tlg_with_slot.sh similarity index 100% rename from speechx/examples/custom_asr/local/mk_tlg_with_slot.sh rename to runtime/examples/custom_asr/local/mk_tlg_with_slot.sh diff --git a/speechx/examples/custom_asr/local/train_lm_with_slot.sh b/runtime/examples/custom_asr/local/train_lm_with_slot.sh similarity index 100% rename from speechx/examples/custom_asr/local/train_lm_with_slot.sh rename to runtime/examples/custom_asr/local/train_lm_with_slot.sh diff --git a/speechx/examples/custom_asr/path.sh b/runtime/examples/custom_asr/path.sh similarity index 70% rename from speechx/examples/custom_asr/path.sh rename to runtime/examples/custom_asr/path.sh index 1907c79f..3f5dd476 100644 --- a/speechx/examples/custom_asr/path.sh +++ b/runtime/examples/custom_asr/path.sh @@ -1,8 +1,8 @@ # This contains the locations of binarys build required for running the examples. MAIN_ROOT=`realpath $PWD/../../../` -SPEECHX_ROOT=`realpath $MAIN_ROOT/speechx` -SPEECHX_EXAMPLES=$SPEECHX_ROOT/build/examples +RUNTIME_ROOT=`realpath $MAIN_ROOT/runtime` +RUNTIME_EXAMPLES=$RUNTIME_ROOT/build/examples export LC_AL=C @@ -12,6 +12,6 @@ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${LIBLBFGS}/lib/.libs export SRILM=${MAIN_ROOT}/tools/srilm # kaldi lm -KALDI_DIR=$SPEECHX_ROOT/build/speechx/kaldi/ -OPENFST_DIR=$SPEECHX_ROOT/fc_patch/openfst-build/src +KALDI_DIR=$RUNTIME_ROOT/build/engine/kaldi/ +OPENFST_DIR=$RUNTIME_ROOT/fc_patch/openfst-build/src export PATH=${PATH}:${SRILM}/bin:${SRILM}/bin/i686-m64:$KALDI_DIR/lmbin:$KALDI_DIR/fstbin:$OPENFST_DIR/bin:$SPEECHX_EXAMPLES/ds2_ol/decoder diff --git a/speechx/examples/custom_asr/run.sh b/runtime/examples/custom_asr/run.sh similarity index 100% rename from speechx/examples/custom_asr/run.sh rename to runtime/examples/custom_asr/run.sh diff --git a/speechx/examples/custom_asr/utils b/runtime/examples/custom_asr/utils similarity index 100% rename from speechx/examples/custom_asr/utils rename to runtime/examples/custom_asr/utils diff --git a/speechx/examples/text_lm/.gitignore b/runtime/examples/text_lm/.gitignore similarity index 100% rename from speechx/examples/text_lm/.gitignore rename to runtime/examples/text_lm/.gitignore diff --git a/speechx/examples/text_lm/README.md b/runtime/examples/text_lm/README.md similarity index 100% rename from speechx/examples/text_lm/README.md rename to runtime/examples/text_lm/README.md diff --git a/speechx/examples/text_lm/local/data/chars.dic b/runtime/examples/text_lm/local/data/chars.dic similarity index 100% rename from speechx/examples/text_lm/local/data/chars.dic rename to runtime/examples/text_lm/local/data/chars.dic diff --git a/speechx/examples/text_lm/local/data/words.dic b/runtime/examples/text_lm/local/data/words.dic similarity index 100% rename from speechx/examples/text_lm/local/data/words.dic rename to runtime/examples/text_lm/local/data/words.dic diff --git a/speechx/examples/text_lm/local/mmseg.py b/runtime/examples/text_lm/local/mmseg.py similarity index 100% rename from speechx/examples/text_lm/local/mmseg.py rename to runtime/examples/text_lm/local/mmseg.py diff --git a/runtime/examples/text_lm/path.sh b/runtime/examples/text_lm/path.sh new file mode 100644 index 00000000..dc8fc8dd --- /dev/null +++ b/runtime/examples/text_lm/path.sh @@ -0,0 +1,4 @@ +MAIN_ROOT=`realpath $PWD/../../../` +ENGINE_ROOT=`realpath $MAIN_ROOT/runtime` + +export LC_AL=C diff --git a/speechx/examples/text_lm/run.sh b/runtime/examples/text_lm/run.sh similarity index 100% rename from speechx/examples/text_lm/run.sh rename to runtime/examples/text_lm/run.sh diff --git a/speechx/examples/text_lm/utils b/runtime/examples/text_lm/utils similarity index 100% rename from speechx/examples/text_lm/utils rename to runtime/examples/text_lm/utils diff --git a/speechx/examples/u2pp_ol/README.md b/runtime/examples/u2pp_ol/README.md similarity index 100% rename from speechx/examples/u2pp_ol/README.md rename to runtime/examples/u2pp_ol/README.md diff --git a/speechx/examples/u2pp_ol/wenetspeech/.gitignore b/runtime/examples/u2pp_ol/wenetspeech/.gitignore similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/.gitignore rename to runtime/examples/u2pp_ol/wenetspeech/.gitignore diff --git a/speechx/examples/u2pp_ol/wenetspeech/README.md b/runtime/examples/u2pp_ol/wenetspeech/README.md similarity index 90% rename from speechx/examples/u2pp_ol/wenetspeech/README.md rename to runtime/examples/u2pp_ol/wenetspeech/README.md index 6999fe3c..d66aacc7 100644 --- a/speechx/examples/u2pp_ol/wenetspeech/README.md +++ b/runtime/examples/u2pp_ol/wenetspeech/README.md @@ -50,10 +50,10 @@ This stage using `u2_recognizer_main` to recognize wav file. The input is `scp` file which look like this: ```text # head data/split1/1/aishell_test.scp -BAC009S0764W0121 /workspace/PaddleSpeech/speechx/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0121.wav -BAC009S0764W0122 /workspace/PaddleSpeech/speechx/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0122.wav +BAC009S0764W0121 /workspace/PaddleSpeech/runtime/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0121.wav +BAC009S0764W0122 /workspace/PaddleSpeech/runtime/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0122.wav ... -BAC009S0764W0125 /workspace/PaddleSpeech/speechx/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0125.wav +BAC009S0764W0125 /workspace/PaddleSpeech/runtime/examples/u2pp_ol/wenetspeech/data/test/S0764/BAC009S0764W0125.wav ``` If you want to recognize one wav, you can make `scp` file like this: diff --git a/speechx/examples/u2pp_ol/wenetspeech/RESULTS.md b/runtime/examples/u2pp_ol/wenetspeech/RESULTS.md similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/RESULTS.md rename to runtime/examples/u2pp_ol/wenetspeech/RESULTS.md diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/aishell_train_lms.sh b/runtime/examples/u2pp_ol/wenetspeech/local/aishell_train_lms.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/aishell_train_lms.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/aishell_train_lms.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/decode.sh b/runtime/examples/u2pp_ol/wenetspeech/local/decode.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/decode.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/decode.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/feat.sh b/runtime/examples/u2pp_ol/wenetspeech/local/feat.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/feat.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/feat.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/nnet.sh b/runtime/examples/u2pp_ol/wenetspeech/local/nnet.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/nnet.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/nnet.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/recognizer.sh b/runtime/examples/u2pp_ol/wenetspeech/local/recognizer.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/recognizer.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/recognizer.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/recognizer_quant.sh b/runtime/examples/u2pp_ol/wenetspeech/local/recognizer_quant.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/recognizer_quant.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/recognizer_quant.sh diff --git a/runtime/examples/u2pp_ol/wenetspeech/local/run_build_tlg.sh b/runtime/examples/u2pp_ol/wenetspeech/local/run_build_tlg.sh new file mode 100755 index 00000000..30ea2020 --- /dev/null +++ b/runtime/examples/u2pp_ol/wenetspeech/local/run_build_tlg.sh @@ -0,0 +1,84 @@ +#!/bin/bash +set -eo pipefail + +#. path.sh + +# attention, please replace the vocab is only for this script. +# different acustic model has different vocab +ckpt_dir=data/model/asr1_chunk_conformer_u2pp_wenetspeech_static_1.3.0.model +unit=$ckpt_dir/vocab.txt # vocab file, line: char/spm_pice +model_dir=$ckpt_dir/exp/deepspeech2_online/checkpoints/ + +stage=2 +stop_stage=100 +corpus=aishell +lexicon=data/lexicon.txt # line: word ph0 ... phn, aishell/resource_aishell/lexicon.txt +text=data/text # line: utt text, aishell/data_aishell/transcript/aishell_transcript_v0.8.txt + +. utils/parse_options.sh + +data=$PWD/data +mkdir -p $data + +if [ $stage -le -1 ] && [ $stop_stage -ge -1 ]; then + if [ ! -f $data/speech.ngram.zh.tar.gz ];then + # download ngram + pushd $data + wget -c http://paddlespeech.bj.bcebos.com/speechx/examples/ngram/zh/speech.ngram.zh.tar.gz + tar xvzf speech.ngram.zh.tar.gz + popd + fi +fi + +if [ ! -f $unit ]; then + echo "$0: No such file $unit" + exit 1; +fi + +if ! which ngram-count; then + # need srilm install + pushd $MAIN_ROOT/tools + make srilm.done + popd +fi + +echo "done." +mkdir -p data/local/dict +if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then + # Prepare dict + # line: char/spm_pices + cp $unit data/local/dict/units.txt + + if [ ! -f $lexicon ];then + utils/text_to_lexicon.py --has_key true --text $text --lexicon $lexicon + echo "Generate $lexicon from $text" + fi + + # filter by vocab + # line: word ph0 ... phn -> line: word char0 ... charn + utils/fst/prepare_dict.py \ + --unit_file $unit \ + --in_lexicon ${lexicon} \ + --out_lexicon data/local/dict/lexicon.txt +fi + +lm=data/local/lm +mkdir -p $lm + +if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then + # Train ngram lm + cp $text $lm/text + local/aishell_train_lms.sh + echo "build LM done." +fi + +# build TLG +if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then + # build T & L + utils/fst/compile_lexicon_token_fst.sh \ + data/local/dict data/local/tmp data/local/lang + + # build G & TLG + utils/fst/make_tlg.sh data/local/lm data/local/lang data/lang_test || exit 1; + +fi diff --git a/speechx/examples/u2pp_ol/wenetspeech/local/split_data.sh b/runtime/examples/u2pp_ol/wenetspeech/local/split_data.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/local/split_data.sh rename to runtime/examples/u2pp_ol/wenetspeech/local/split_data.sh diff --git a/runtime/examples/u2pp_ol/wenetspeech/path.sh b/runtime/examples/u2pp_ol/wenetspeech/path.sh new file mode 100644 index 00000000..ad3a7358 --- /dev/null +++ b/runtime/examples/u2pp_ol/wenetspeech/path.sh @@ -0,0 +1,18 @@ +# This contains the locations of binarys build required for running the examples. + +unset GREP_OPTIONS + +ENGINE_ROOT=$PWD/../../../ +ENGINE_BUILD=$ENGINE_ROOT/build/engine/asr + +ENGINE_TOOLS=$ENGINE_ROOT/tools +TOOLS_BIN=$ENGINE_TOOLS/valgrind/install/bin + +[ -d $ENGINE_BUILD ] || { echo "Error: 'build/runtime' directory not found. please ensure that the project build successfully"; } + +export LC_AL=C + +export PATH=$PATH:$TOOLS_BIN:$ENGINE_BUILD/nnet:$ENGINE_BUILD/decoder:$ENGINE_BUILD/../common/frontend/audio:$ENGINE_BUILD/recognizer + +#PADDLE_LIB_PATH=$(python -c "import os; import paddle; include_dir=paddle.sysconfig.get_include(); paddle_dir=os.path.split(include_dir)[0]; libs_dir=os.path.join(paddle_dir, 'libs'); fluid_dir=os.path.join(paddle_dir, 'fluid'); out=':'.join([libs_dir, fluid_dir]); print(out);") +export LD_LIBRARY_PATH=$PADDLE_LIB_PATH:$LD_LIBRARY_PATH diff --git a/speechx/examples/u2pp_ol/wenetspeech/run.sh b/runtime/examples/u2pp_ol/wenetspeech/run.sh similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/run.sh rename to runtime/examples/u2pp_ol/wenetspeech/run.sh diff --git a/speechx/examples/u2pp_ol/wenetspeech/utils b/runtime/examples/u2pp_ol/wenetspeech/utils similarity index 100% rename from speechx/examples/u2pp_ol/wenetspeech/utils rename to runtime/examples/u2pp_ol/wenetspeech/utils diff --git a/speechx/patch/CPPLINT.cfg b/runtime/patch/CPPLINT.cfg similarity index 100% rename from speechx/patch/CPPLINT.cfg rename to runtime/patch/CPPLINT.cfg diff --git a/speechx/patch/README.md b/runtime/patch/README.md similarity index 100% rename from speechx/patch/README.md rename to runtime/patch/README.md diff --git a/speechx/patch/openfst/src/include/fst/flags.h b/runtime/patch/openfst/src/include/fst/flags.h similarity index 100% rename from speechx/patch/openfst/src/include/fst/flags.h rename to runtime/patch/openfst/src/include/fst/flags.h diff --git a/speechx/patch/openfst/src/include/fst/log.h b/runtime/patch/openfst/src/include/fst/log.h similarity index 100% rename from speechx/patch/openfst/src/include/fst/log.h rename to runtime/patch/openfst/src/include/fst/log.h diff --git a/speechx/patch/openfst/src/lib/flags.cc b/runtime/patch/openfst/src/lib/flags.cc similarity index 100% rename from speechx/patch/openfst/src/lib/flags.cc rename to runtime/patch/openfst/src/lib/flags.cc diff --git a/speechx/tools/clang-format.sh b/runtime/tools/clang-format.sh similarity index 100% rename from speechx/tools/clang-format.sh rename to runtime/tools/clang-format.sh diff --git a/speechx/tools/setup_valgrind.sh b/runtime/tools/setup_valgrind.sh similarity index 100% rename from speechx/tools/setup_valgrind.sh rename to runtime/tools/setup_valgrind.sh diff --git a/speechx/tools/venv.sh b/runtime/tools/venv.sh similarity index 100% rename from speechx/tools/venv.sh rename to runtime/tools/venv.sh diff --git a/speechx/examples/text_lm/path.sh b/speechx/examples/text_lm/path.sh deleted file mode 100644 index 541f852c..00000000 --- a/speechx/examples/text_lm/path.sh +++ /dev/null @@ -1,4 +0,0 @@ -MAIN_ROOT=`realpath $PWD/../../../../` -SPEECHX_ROOT=`realpath $MAIN_ROOT/speechx` - -export LC_AL=C diff --git a/speechx/examples/u2pp_ol/wenetspeech/path.sh b/speechx/examples/u2pp_ol/wenetspeech/path.sh deleted file mode 100644 index 9518db11..00000000 --- a/speechx/examples/u2pp_ol/wenetspeech/path.sh +++ /dev/null @@ -1,18 +0,0 @@ -# This contains the locations of binarys build required for running the examples. - -unset GREP_OPTIONS - -SPEECHX_ROOT=$PWD/../../../ -SPEECHX_BUILD=$SPEECHX_ROOT/build/speechx/asr - -SPEECHX_TOOLS=$SPEECHX_ROOT/tools -TOOLS_BIN=$SPEECHX_TOOLS/valgrind/install/bin - -[ -d $SPEECHX_BUILD ] || { echo "Error: 'build/speechx' directory not found. please ensure that the project build successfully"; } - -export LC_AL=C - -export PATH=$PATH:$TOOLS_BIN:$SPEECHX_BUILD/nnet:$SPEECHX_BUILD/decoder:$SPEECHX_BUILD/../common/frontend/audio:$SPEECHX_BUILD/recognizer - -PADDLE_LIB_PATH=$(python -c "import os; import paddle; include_dir=paddle.sysconfig.get_include(); paddle_dir=os.path.split(include_dir)[0]; libs_dir=os.path.join(paddle_dir, 'libs'); fluid_dir=os.path.join(paddle_dir, 'fluid'); out=':'.join([libs_dir, fluid_dir]); print(out);") -export LD_LIBRARY_PATH=$PADDLE_LIB_PATH:$LD_LIBRARY_PATH diff --git a/speechx/speechx/common/frontend/wave-reader.cc b/speechx/speechx/common/frontend/wave-reader.cc deleted file mode 100644 index 42bf79c6..00000000 --- a/speechx/speechx/common/frontend/wave-reader.cc +++ /dev/null @@ -1,387 +0,0 @@ -// feat/wave-reader.cc - -// Copyright 2009-2011 Karel Vesely; Petr Motlicek -// 2013 Florent Masson -// 2013 Johns Hopkins University (author: Daniel Povey) - -// See ../../COPYING for clarification regarding multiple authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -// MERCHANTABLITY OR NON-INFRINGEMENT. -// See the Apache 2 License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include - -#include "frontend/wave-reader.h" -#include "base/kaldi-error.h" -#include "base/kaldi-utils.h" - -namespace kaldi { - -// A utility class for reading wave header. -struct WaveHeaderReadGofer { - std::istream &is; - bool swap; - char tag[5]; - - WaveHeaderReadGofer(std::istream &is) : is(is), swap(false) { - memset(tag, '\0', sizeof tag); - } - - void Expect4ByteTag(const char *expected) { - is.read(tag, 4); - if (is.fail()) - KALDI_ERR << "WaveData: expected " << expected - << ", failed to read anything"; - if (strcmp(tag, expected)) - KALDI_ERR << "WaveData: expected " << expected << ", got " << tag; - } - - void Read4ByteTag() { - is.read(tag, 4); - if (is.fail()) - KALDI_ERR << "WaveData: expected 4-byte chunk-name, got read error"; - } - - uint32 ReadUint32() { - union { - char result[4]; - uint32 ans; - } u; - is.read(u.result, 4); - if (swap) - KALDI_SWAP4(u.result); - if (is.fail()) - KALDI_ERR << "WaveData: unexpected end of file or read error"; - return u.ans; - } - - uint16 ReadUint16() { - union { - char result[2]; - int16 ans; - } u; - is.read(u.result, 2); - if (swap) - KALDI_SWAP2(u.result); - if (is.fail()) - KALDI_ERR << "WaveData: unexpected end of file or read error"; - return u.ans; - } -}; - -static void WriteUint32(std::ostream &os, int32 i) { - union { - char buf[4]; - int i; - } u; - u.i = i; -#ifdef __BIG_ENDIAN__ - KALDI_SWAP4(u.buf); -#endif - os.write(u.buf, 4); - if (os.fail()) - KALDI_ERR << "WaveData: error writing to stream."; -} - -static void WriteUint16(std::ostream &os, int16 i) { - union { - char buf[2]; - int16 i; - } u; - u.i = i; -#ifdef __BIG_ENDIAN__ - KALDI_SWAP2(u.buf); -#endif - os.write(u.buf, 2); - if (os.fail()) - KALDI_ERR << "WaveData: error writing to stream."; -} - -void WaveInfo::Read(std::istream &is) { - WaveHeaderReadGofer reader(is); - reader.Read4ByteTag(); - if (strcmp(reader.tag, "RIFF") == 0) - reverse_bytes_ = false; - else if (strcmp(reader.tag, "RIFX") == 0) - reverse_bytes_ = true; - else - KALDI_ERR << "WaveData: expected RIFF or RIFX, got " << reader.tag; - -#ifdef __BIG_ENDIAN__ - reverse_bytes_ = !reverse_bytes_; -#endif - reader.swap = reverse_bytes_; - - uint32 riff_chunk_size = reader.ReadUint32(); - reader.Expect4ByteTag("WAVE"); - - uint32 riff_chunk_read = 0; - riff_chunk_read += 4; // WAVE included in riff_chunk_size. - - // Possibly skip any RIFF tags between 'WAVE' and 'fmt '. - // Apple devices produce a filler tag 'JUNK' for memory alignment. - reader.Read4ByteTag(); - riff_chunk_read += 4; - while (strcmp(reader.tag,"fmt ") != 0) { - uint32 filler_size = reader.ReadUint32(); - riff_chunk_read += 4; - for (uint32 i = 0; i < filler_size; i++) { - is.get(); // read 1 byte, - } - riff_chunk_read += filler_size; - // get next RIFF tag, - reader.Read4ByteTag(); - riff_chunk_read += 4; - } - - KALDI_ASSERT(strcmp(reader.tag,"fmt ") == 0); - uint32 subchunk1_size = reader.ReadUint32(); - uint16 audio_format = reader.ReadUint16(); - num_channels_ = reader.ReadUint16(); - uint32 sample_rate = reader.ReadUint32(), - byte_rate = reader.ReadUint32(), - block_align = reader.ReadUint16(), - bits_per_sample = reader.ReadUint16(); - samp_freq_ = static_cast(sample_rate); - - uint32 fmt_chunk_read = 16; - if (audio_format == 1) { - if (subchunk1_size < 16) { - KALDI_ERR << "WaveData: expect PCM format data to have fmt chunk " - << "of at least size 16."; - } - } else if (audio_format == 0xFFFE) { // WAVE_FORMAT_EXTENSIBLE - uint16 extra_size = reader.ReadUint16(); - if (subchunk1_size < 40 || extra_size < 22) { - KALDI_ERR << "WaveData: malformed WAVE_FORMAT_EXTENSIBLE format data."; - } - reader.ReadUint16(); // Unused for PCM. - reader.ReadUint32(); // Channel map: we do not care. - uint32 guid1 = reader.ReadUint32(), - guid2 = reader.ReadUint32(), - guid3 = reader.ReadUint32(), - guid4 = reader.ReadUint32(); - fmt_chunk_read = 40; - - // Support only KSDATAFORMAT_SUBTYPE_PCM for now. Interesting formats: - // ("00000001-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_PCM) - // ("00000003-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) - // ("00000006-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_ALAW) - // ("00000007-0000-0010-8000-00aa00389b71", KSDATAFORMAT_SUBTYPE_MULAW) - if (guid1 != 0x00000001 || guid2 != 0x00100000 || - guid3 != 0xAA000080 || guid4 != 0x719B3800) { - KALDI_ERR << "WaveData: unsupported WAVE_FORMAT_EXTENSIBLE format."; - } - } else { - KALDI_ERR << "WaveData: can read only PCM data, format id in file is: " - << audio_format; - } - - for (uint32 i = fmt_chunk_read; i < subchunk1_size; ++i) - is.get(); // use up extra data. - - if (num_channels_ == 0) - KALDI_ERR << "WaveData: no channels present"; - if (bits_per_sample != 16) - KALDI_ERR << "WaveData: unsupported bits_per_sample = " << bits_per_sample; - if (byte_rate != sample_rate * bits_per_sample/8 * num_channels_) - KALDI_ERR << "Unexpected byte rate " << byte_rate << " vs. " - << sample_rate << " * " << (bits_per_sample/8) - << " * " << num_channels_; - if (block_align != num_channels_ * bits_per_sample/8) - KALDI_ERR << "Unexpected block_align: " << block_align << " vs. " - << num_channels_ << " * " << (bits_per_sample/8); - - riff_chunk_read += 4 + subchunk1_size; - // size of what we just read, 4 for subchunk1_size + subchunk1_size itself. - - // We support an optional "fact" chunk (which is useless but which - // we encountered), and then a single "data" chunk. - - reader.Read4ByteTag(); - riff_chunk_read += 4; - - // Skip any subchunks between "fmt" and "data". Usually there will - // be a single "fact" subchunk, but on Windows there can also be a - // "list" subchunk. - while (strcmp(reader.tag, "data") != 0) { - // We will just ignore the data in these chunks. - uint32 chunk_sz = reader.ReadUint32(); - if (chunk_sz != 4 && strcmp(reader.tag, "fact") == 0) - KALDI_WARN << "Expected fact chunk to be 4 bytes long."; - for (uint32 i = 0; i < chunk_sz; i++) - is.get(); - riff_chunk_read += 4 + chunk_sz; // for chunk_sz (4) + chunk contents (chunk-sz) - - // Now read the next chunk name. - reader.Read4ByteTag(); - riff_chunk_read += 4; - } - - KALDI_ASSERT(strcmp(reader.tag, "data") == 0); - uint32 data_chunk_size = reader.ReadUint32(); - riff_chunk_read += 4; - - // Figure out if the file is going to be read to the end. Values as - // observed in the wild: - bool is_stream_mode = - riff_chunk_size == 0 - || riff_chunk_size == 0xFFFFFFFF - || data_chunk_size == 0 - || data_chunk_size == 0xFFFFFFFF - || data_chunk_size == 0x7FFFF000; // This value is used by SoX. - - if (is_stream_mode) - KALDI_VLOG(1) << "Read in RIFF chunk size: " << riff_chunk_size - << ", data chunk size: " << data_chunk_size - << ". Assume 'stream mode' (reading data to EOF)."; - - if (!is_stream_mode - && std::abs(static_cast(riff_chunk_read) + - static_cast(data_chunk_size) - - static_cast(riff_chunk_size)) > 1) { - // We allow the size to be off by one without warning, because there is a - // weirdness in the format of RIFF files that means that the input may - // sometimes be padded with 1 unused byte to make the total size even. - KALDI_WARN << "Expected " << riff_chunk_size << " bytes in RIFF chunk, but " - << "after first data block there will be " << riff_chunk_read - << " + " << data_chunk_size << " bytes " - << "(we do not support reading multiple data chunks)."; - } - - if (is_stream_mode) - samp_count_ = -1; - else - samp_count_ = data_chunk_size / block_align; -} - -void WaveData::Read(std::istream &is) { - const uint32 kBlockSize = 1024 * 1024; - - WaveInfo header; - header.Read(is); - - data_.Resize(0, 0); // clear the data. - samp_freq_ = header.SampFreq(); - - std::vector buffer; - uint32 bytes_to_go = header.IsStreamed() ? kBlockSize : header.DataBytes(); - - // Once in a while header.DataBytes() will report an insane value; - // read the file to the end - while (is && bytes_to_go > 0) { - uint32 block_bytes = std::min(bytes_to_go, kBlockSize); - uint32 offset = buffer.size(); - buffer.resize(offset + block_bytes); - is.read(&buffer[offset], block_bytes); - uint32 bytes_read = is.gcount(); - buffer.resize(offset + bytes_read); - if (!header.IsStreamed()) - bytes_to_go -= bytes_read; - } - - if (is.bad()) - KALDI_ERR << "WaveData: file read error"; - - if (buffer.size() == 0) - KALDI_ERR << "WaveData: empty file (no data)"; - - if (!header.IsStreamed() && buffer.size() < header.DataBytes()) { - KALDI_WARN << "Expected " << header.DataBytes() << " bytes of wave data, " - << "but read only " << buffer.size() << " bytes. " - << "Truncated file?"; - } - - uint16 *data_ptr = reinterpret_cast(&buffer[0]); - - // The matrix is arranged row per channel, column per sample. - data_.Resize(header.NumChannels(), - buffer.size() / header.BlockAlign()); - for (uint32 i = 0; i < data_.NumCols(); ++i) { - for (uint32 j = 0; j < data_.NumRows(); ++j) { - int16 k = *data_ptr++; - if (header.ReverseBytes()) - KALDI_SWAP2(k); - data_(j, i) = k; - } - } -} - - -// Write 16-bit PCM. - -// note: the WAVE chunk contains 2 subchunks. -// -// subchunk2size = data.NumRows() * data.NumCols() * 2. - - -void WaveData::Write(std::ostream &os) const { - os << "RIFF"; - if (data_.NumRows() == 0) - KALDI_ERR << "Error: attempting to write empty WAVE file"; - - int32 num_chan = data_.NumRows(), - num_samp = data_.NumCols(), - bytes_per_samp = 2; - - int32 subchunk2size = (num_chan * num_samp * bytes_per_samp); - int32 chunk_size = 36 + subchunk2size; - WriteUint32(os, chunk_size); - os << "WAVE"; - os << "fmt "; - WriteUint32(os, 16); - WriteUint16(os, 1); - WriteUint16(os, num_chan); - KALDI_ASSERT(samp_freq_ > 0); - WriteUint32(os, static_cast(samp_freq_)); - WriteUint32(os, static_cast(samp_freq_) * num_chan * bytes_per_samp); - WriteUint16(os, num_chan * bytes_per_samp); - WriteUint16(os, 8 * bytes_per_samp); - os << "data"; - WriteUint32(os, subchunk2size); - - const BaseFloat *data_ptr = data_.Data(); - int32 stride = data_.Stride(); - - int num_clipped = 0; - for (int32 i = 0; i < num_samp; i++) { - for (int32 j = 0; j < num_chan; j++) { - int32 elem = static_cast(trunc(data_ptr[j * stride + i])); - int16 elem_16 = static_cast(elem); - if (elem < std::numeric_limits::min()) { - elem_16 = std::numeric_limits::min(); - ++num_clipped; - } else if (elem > std::numeric_limits::max()) { - elem_16 = std::numeric_limits::max(); - ++num_clipped; - } -#ifdef __BIG_ENDIAN__ - KALDI_SWAP2(elem_16); -#endif - os.write(reinterpret_cast(&elem_16), 2); - } - } - if (os.fail()) - KALDI_ERR << "Error writing wave data to stream."; - if (num_clipped > 0) - KALDI_WARN << "WARNING: clipped " << num_clipped - << " samples out of total " << num_chan * num_samp - << ". Reduce volume?"; -} - - -} // end namespace kaldi diff --git a/speechx/speechx/common/frontend/wave-reader.h b/speechx/speechx/common/frontend/wave-reader.h deleted file mode 100644 index dae74139..00000000 --- a/speechx/speechx/common/frontend/wave-reader.h +++ /dev/null @@ -1,248 +0,0 @@ -// feat/wave-reader.h - -// Copyright 2009-2011 Karel Vesely; Microsoft Corporation -// 2013 Florent Masson -// 2013 Johns Hopkins University (author: Daniel Povey) - -// See ../../COPYING for clarification regarding multiple authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -// MERCHANTABLITY OR NON-INFRINGEMENT. -// See the Apache 2 License for the specific language governing permissions and -// limitations under the License. - - -/* -// THE WAVE FORMAT IS SPECIFIED IN: -// https:// ccrma.stanford.edu/courses/422/projects/WaveFormat/ -// -// -// -// RIFF -// | -// WAVE -// | \ \ \ -// fmt_ data ... data -// -// -// Riff is a general container, which usually contains one WAVE chunk -// each WAVE chunk has header sub-chunk 'fmt_' -// and one or more data sub-chunks 'data' -// -// [Note from Dan: to say that the wave format was ever "specified" anywhere is -// not quite right. The guy who invented the wave format attempted to create -// a formal specification but it did not completely make sense. And there -// doesn't seem to be a consensus on what makes a valid wave file, -// particularly where the accuracy of header information is concerned.] -*/ - - -#ifndef KALDI_FEAT_WAVE_READER_H_ -#define KALDI_FEAT_WAVE_READER_H_ - -#include - -#include "base/kaldi-types.h" -#include "matrix/kaldi-vector.h" -#include "matrix/kaldi-matrix.h" - - -namespace kaldi { - -/// For historical reasons, we scale waveforms to the range -/// (2^15-1)*[-1, 1], not the usual default DSP range [-1, 1]. -const BaseFloat kWaveSampleMax = 32768.0; - -/// This class reads and hold wave file header information. -class WaveInfo { - public: - WaveInfo() : samp_freq_(0), samp_count_(0), - num_channels_(0), reverse_bytes_(0) {} - - /// Is stream size unknown? Duration and SampleCount not valid if true. - bool IsStreamed() const { return samp_count_ < 0; } - - /// Sample frequency, Hz. - BaseFloat SampFreq() const { return samp_freq_; } - - /// Number of samples in stream. Invalid if IsStreamed() is true. - uint32 SampleCount() const { return samp_count_; } - - /// Approximate duration, seconds. Invalid if IsStreamed() is true. - BaseFloat Duration() const { return samp_count_ / samp_freq_; } - - /// Number of channels, 1 to 16. - int32 NumChannels() const { return num_channels_; } - - /// Bytes per sample. - size_t BlockAlign() const { return 2 * num_channels_; } - - /// Wave data bytes. Invalid if IsStreamed() is true. - size_t DataBytes() const { return samp_count_ * BlockAlign(); } - - /// Is data file byte order different from machine byte order? - bool ReverseBytes() const { return reverse_bytes_; } - - /// 'is' should be opened in binary mode. Read() will throw on error. - /// On success 'is' will be positioned at the beginning of wave data. - void Read(std::istream &is); - - private: - BaseFloat samp_freq_; - int32 samp_count_; // 0 if empty, -1 if undefined length. - uint8 num_channels_; - bool reverse_bytes_; // File endianness differs from host. -}; - -/// This class's purpose is to read in Wave files. -class WaveData { - public: - WaveData(BaseFloat samp_freq, const MatrixBase &data) - : data_(data), samp_freq_(samp_freq) {} - - WaveData() : samp_freq_(0.0) {} - - /// Read() will throw on error. It's valid to call Read() more than once-- - /// in this case it will destroy what was there before. - /// "is" should be opened in binary mode. - void Read(std::istream &is); - - /// Write() will throw on error. os should be opened in binary mode. - void Write(std::ostream &os) const; - - // This function returns the wave data-- it's in a matrix - // because there may be multiple channels. In the normal case - // there's just one channel so Data() will have one row. - const Matrix &Data() const { return data_; } - - BaseFloat SampFreq() const { return samp_freq_; } - - // Returns the duration in seconds - BaseFloat Duration() const { return data_.NumCols() / samp_freq_; } - - void CopyFrom(const WaveData &other) { - samp_freq_ = other.samp_freq_; - data_.CopyFromMat(other.data_); - } - - void Clear() { - data_.Resize(0, 0); - samp_freq_ = 0.0; - } - - void Swap(WaveData *other) { - data_.Swap(&(other->data_)); - std::swap(samp_freq_, other->samp_freq_); - } - - private: - static const uint32 kBlockSize = 1024 * 1024; // Use 1M bytes. - Matrix data_; - BaseFloat samp_freq_; -}; - - -// Holder class for .wav files that enables us to read (but not write) .wav -// files. c.f. util/kaldi-holder.h we don't use the KaldiObjectHolder template -// because we don't want to check for the \0B binary header. We could have faked -// it by pretending to read in the wave data in text mode after failing to find -// the \0B header, but that would have been a little ugly. -class WaveHolder { - public: - typedef WaveData T; - - static bool Write(std::ostream &os, bool binary, const T &t) { - // We don't write the binary-mode header here [always binary]. - if (!binary) - KALDI_ERR << "Wave data can only be written in binary mode."; - try { - t.Write(os); // throws exception on failure. - return true; - } catch (const std::exception &e) { - KALDI_WARN << "Exception caught in WaveHolder object (writing). " - << e.what(); - return false; // write failure. - } - } - void Copy(const T &t) { t_.CopyFrom(t); } - - static bool IsReadInBinary() { return true; } - - void Clear() { t_.Clear(); } - - T &Value() { return t_; } - - WaveHolder &operator = (const WaveHolder &other) { - t_.CopyFrom(other.t_); - return *this; - } - WaveHolder(const WaveHolder &other): t_(other.t_) {} - - WaveHolder() {} - - bool Read(std::istream &is) { - // We don't look for the binary-mode header here [always binary] - try { - t_.Read(is); // Throws exception on failure. - return true; - } catch (const std::exception &e) { - KALDI_WARN << "Exception caught in WaveHolder::Read(). " << e.what(); - return false; - } - } - - void Swap(WaveHolder *other) { - t_.Swap(&(other->t_)); - } - - bool ExtractRange(const WaveHolder &other, const std::string &range) { - KALDI_ERR << "ExtractRange is not defined for this type of holder."; - return false; - } - - private: - T t_; -}; - -// This is like WaveHolder but when you just want the metadata- -// it leaves the actual data undefined, it doesn't read it. -class WaveInfoHolder { - public: - typedef WaveInfo T; - - void Clear() { info_ = WaveInfo(); } - void Swap(WaveInfoHolder *other) { std::swap(info_, other->info_); } - T &Value() { return info_; } - static bool IsReadInBinary() { return true; } - - bool Read(std::istream &is) { - try { - info_.Read(is); // Throws exception on failure. - return true; - } catch (const std::exception &e) { - KALDI_WARN << "Exception caught in WaveInfoHolder::Read(). " << e.what(); - return false; - } - } - - bool ExtractRange(const WaveInfoHolder &other, const std::string &range) { - KALDI_ERR << "ExtractRange is not defined for this type of holder."; - return false; - } - - private: - WaveInfo info_; -}; - - -} // namespace kaldi - -#endif // KALDI_FEAT_WAVE_READER_H_ diff --git a/speechx/speechx/common/utils/picojson.h b/speechx/speechx/common/utils/picojson.h deleted file mode 100644 index 28c5b7fa..00000000 --- a/speechx/speechx/common/utils/picojson.h +++ /dev/null @@ -1,1202 +0,0 @@ -/* - * Copyright 2009-2010 Cybozu Labs, Inc. - * Copyright 2011-2014 Kazuho Oku - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#ifndef picojson_h -#define picojson_h - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define PICOJSON_USE_INT64 1 - -// for isnan/isinf -#if __cplusplus >= 201103L -#include -#else -extern "C" { -#ifdef _MSC_VER -#include -#elif defined(__INTEL_COMPILER) -#include -#else -#include -#endif -} -#endif - -#ifndef PICOJSON_USE_RVALUE_REFERENCE -#if (defined(__cpp_rvalue_references) && __cpp_rvalue_references >= 200610) || (defined(_MSC_VER) && _MSC_VER >= 1600) -#define PICOJSON_USE_RVALUE_REFERENCE 1 -#else -#define PICOJSON_USE_RVALUE_REFERENCE 0 -#endif -#endif // PICOJSON_USE_RVALUE_REFERENCE - -#ifndef PICOJSON_NOEXCEPT -#if PICOJSON_USE_RVALUE_REFERENCE -#define PICOJSON_NOEXCEPT noexcept -#else -#define PICOJSON_NOEXCEPT throw() -#endif -#endif - -// experimental support for int64_t (see README.mkdn for detail) -#ifdef PICOJSON_USE_INT64 -#define __STDC_FORMAT_MACROS -#include -#if __cplusplus >= 201103L -#include -#else -extern "C" { -#include -} -#endif -#endif - -// to disable the use of localeconv(3), set PICOJSON_USE_LOCALE to 0 -#ifndef PICOJSON_USE_LOCALE -#define PICOJSON_USE_LOCALE 1 -#endif -#if PICOJSON_USE_LOCALE -extern "C" { -#include -} -#endif - -#ifndef PICOJSON_ASSERT -#define PICOJSON_ASSERT(e) \ - do { \ - if (!(e)) \ - throw std::runtime_error(#e); \ - } while (0) -#endif - -#ifdef _MSC_VER -#define SNPRINTF _snprintf_s -#pragma warning(push) -#pragma warning(disable : 4244) // conversion from int to char -#pragma warning(disable : 4127) // conditional expression is constant -#pragma warning(disable : 4702) // unreachable code -#pragma warning(disable : 4706) // assignment within conditional expression -#else -#define SNPRINTF snprintf -#endif - -namespace picojson { - -enum { - null_type, - boolean_type, - number_type, - string_type, - array_type, - object_type -#ifdef PICOJSON_USE_INT64 - , - int64_type -#endif -}; - -enum { INDENT_WIDTH = 2, DEFAULT_MAX_DEPTHS = 100 }; - -struct null {}; - -class value { -public: - typedef std::vector array; - typedef std::map object; - union _storage { - bool boolean_; - double number_; -#ifdef PICOJSON_USE_INT64 - int64_t int64_; -#endif - std::string *string_; - array *array_; - object *object_; - }; - -protected: - int type_; - _storage u_; - -public: - value(); - value(int type, bool); - explicit value(bool b); -#ifdef PICOJSON_USE_INT64 - explicit value(int64_t i); -#endif - explicit value(double n); - explicit value(const std::string &s); - explicit value(const array &a); - explicit value(const object &o); -#if PICOJSON_USE_RVALUE_REFERENCE - explicit value(std::string &&s); - explicit value(array &&a); - explicit value(object &&o); -#endif - explicit value(const char *s); - value(const char *s, size_t len); - ~value(); - value(const value &x); - value &operator=(const value &x); -#if PICOJSON_USE_RVALUE_REFERENCE - value(value &&x) PICOJSON_NOEXCEPT; - value &operator=(value &&x) PICOJSON_NOEXCEPT; -#endif - void swap(value &x) PICOJSON_NOEXCEPT; - template bool is() const; - template const T &get() const; - template T &get(); - template void set(const T &); -#if PICOJSON_USE_RVALUE_REFERENCE - template void set(T &&); -#endif - bool evaluate_as_boolean() const; - const value &get(const size_t idx) const; - const value &get(const std::string &key) const; - value &get(const size_t idx); - value &get(const std::string &key); - - bool contains(const size_t idx) const; - bool contains(const std::string &key) const; - std::string to_str() const; - template void serialize(Iter os, bool prettify = false) const; - std::string serialize(bool prettify = false) const; - -private: - template value(const T *); // intentionally defined to block implicit conversion of pointer to bool - template static void _indent(Iter os, int indent); - template void _serialize(Iter os, int indent) const; - std::string _serialize(int indent) const; - void clear(); -}; - -typedef value::array array; -typedef value::object object; - -inline value::value() : type_(null_type), u_() { -} - -inline value::value(int type, bool) : type_(type), u_() { - switch (type) { -#define INIT(p, v) \ - case p##type: \ - u_.p = v; \ - break - INIT(boolean_, false); - INIT(number_, 0.0); -#ifdef PICOJSON_USE_INT64 - INIT(int64_, 0); -#endif - INIT(string_, new std::string()); - INIT(array_, new array()); - INIT(object_, new object()); -#undef INIT - default: - break; - } -} - -inline value::value(bool b) : type_(boolean_type), u_() { - u_.boolean_ = b; -} - -#ifdef PICOJSON_USE_INT64 -inline value::value(int64_t i) : type_(int64_type), u_() { - u_.int64_ = i; -} -#endif - -inline value::value(double n) : type_(number_type), u_() { - if ( -#ifdef _MSC_VER - !_finite(n) -#elif __cplusplus >= 201103L - std::isnan(n) || std::isinf(n) -#else - isnan(n) || isinf(n) -#endif - ) { - throw std::overflow_error(""); - } - u_.number_ = n; -} - -inline value::value(const std::string &s) : type_(string_type), u_() { - u_.string_ = new std::string(s); -} - -inline value::value(const array &a) : type_(array_type), u_() { - u_.array_ = new array(a); -} - -inline value::value(const object &o) : type_(object_type), u_() { - u_.object_ = new object(o); -} - -#if PICOJSON_USE_RVALUE_REFERENCE -inline value::value(std::string &&s) : type_(string_type), u_() { - u_.string_ = new std::string(std::move(s)); -} - -inline value::value(array &&a) : type_(array_type), u_() { - u_.array_ = new array(std::move(a)); -} - -inline value::value(object &&o) : type_(object_type), u_() { - u_.object_ = new object(std::move(o)); -} -#endif - -inline value::value(const char *s) : type_(string_type), u_() { - u_.string_ = new std::string(s); -} - -inline value::value(const char *s, size_t len) : type_(string_type), u_() { - u_.string_ = new std::string(s, len); -} - -inline void value::clear() { - switch (type_) { -#define DEINIT(p) \ - case p##type: \ - delete u_.p; \ - break - DEINIT(string_); - DEINIT(array_); - DEINIT(object_); -#undef DEINIT - default: - break; - } -} - -inline value::~value() { - clear(); -} - -inline value::value(const value &x) : type_(x.type_), u_() { - switch (type_) { -#define INIT(p, v) \ - case p##type: \ - u_.p = v; \ - break - INIT(string_, new std::string(*x.u_.string_)); - INIT(array_, new array(*x.u_.array_)); - INIT(object_, new object(*x.u_.object_)); -#undef INIT - default: - u_ = x.u_; - break; - } -} - -inline value &value::operator=(const value &x) { - if (this != &x) { - value t(x); - swap(t); - } - return *this; -} - -#if PICOJSON_USE_RVALUE_REFERENCE -inline value::value(value &&x) PICOJSON_NOEXCEPT : type_(null_type), u_() { - swap(x); -} -inline value &value::operator=(value &&x) PICOJSON_NOEXCEPT { - swap(x); - return *this; -} -#endif -inline void value::swap(value &x) PICOJSON_NOEXCEPT { - std::swap(type_, x.type_); - std::swap(u_, x.u_); -} - -#define IS(ctype, jtype) \ - template <> inline bool value::is() const { \ - return type_ == jtype##_type; \ - } -IS(null, null) -IS(bool, boolean) -#ifdef PICOJSON_USE_INT64 -IS(int64_t, int64) -#endif -IS(std::string, string) -IS(array, array) -IS(object, object) -#undef IS -template <> inline bool value::is() const { - return type_ == number_type -#ifdef PICOJSON_USE_INT64 - || type_ == int64_type -#endif - ; -} - -#define GET(ctype, var) \ - template <> inline const ctype &value::get() const { \ - PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ - return var; \ - } \ - template <> inline ctype &value::get() { \ - PICOJSON_ASSERT("type mismatch! call is() before get()" && is()); \ - return var; \ - } -GET(bool, u_.boolean_) -GET(std::string, *u_.string_) -GET(array, *u_.array_) -GET(object, *u_.object_) -#ifdef PICOJSON_USE_INT64 -GET(double, - (type_ == int64_type && (const_cast(this)->type_ = number_type, (const_cast(this)->u_.number_ = u_.int64_)), - u_.number_)) -GET(int64_t, u_.int64_) -#else -GET(double, u_.number_) -#endif -#undef GET - -#define SET(ctype, jtype, setter) \ - template <> inline void value::set(const ctype &_val) { \ - clear(); \ - type_ = jtype##_type; \ - setter \ - } -SET(bool, boolean, u_.boolean_ = _val;) -SET(std::string, string, u_.string_ = new std::string(_val);) -SET(array, array, u_.array_ = new array(_val);) -SET(object, object, u_.object_ = new object(_val);) -SET(double, number, u_.number_ = _val;) -#ifdef PICOJSON_USE_INT64 -SET(int64_t, int64, u_.int64_ = _val;) -#endif -#undef SET - -#if PICOJSON_USE_RVALUE_REFERENCE -#define MOVESET(ctype, jtype, setter) \ - template <> inline void value::set(ctype && _val) { \ - clear(); \ - type_ = jtype##_type; \ - setter \ - } -MOVESET(std::string, string, u_.string_ = new std::string(std::move(_val));) -MOVESET(array, array, u_.array_ = new array(std::move(_val));) -MOVESET(object, object, u_.object_ = new object(std::move(_val));) -#undef MOVESET -#endif - -inline bool value::evaluate_as_boolean() const { - switch (type_) { - case null_type: - return false; - case boolean_type: - return u_.boolean_; - case number_type: - return u_.number_ != 0; -#ifdef PICOJSON_USE_INT64 - case int64_type: - return u_.int64_ != 0; -#endif - case string_type: - return !u_.string_->empty(); - default: - return true; - } -} - -inline const value &value::get(const size_t idx) const { - static value s_null; - PICOJSON_ASSERT(is()); - return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; -} - -inline value &value::get(const size_t idx) { - static value s_null; - PICOJSON_ASSERT(is()); - return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; -} - -inline const value &value::get(const std::string &key) const { - static value s_null; - PICOJSON_ASSERT(is()); - object::const_iterator i = u_.object_->find(key); - return i != u_.object_->end() ? i->second : s_null; -} - -inline value &value::get(const std::string &key) { - static value s_null; - PICOJSON_ASSERT(is()); - object::iterator i = u_.object_->find(key); - return i != u_.object_->end() ? i->second : s_null; -} - -inline bool value::contains(const size_t idx) const { - PICOJSON_ASSERT(is()); - return idx < u_.array_->size(); -} - -inline bool value::contains(const std::string &key) const { - PICOJSON_ASSERT(is()); - object::const_iterator i = u_.object_->find(key); - return i != u_.object_->end(); -} - -inline std::string value::to_str() const { - switch (type_) { - case null_type: - return "null"; - case boolean_type: - return u_.boolean_ ? "true" : "false"; -#ifdef PICOJSON_USE_INT64 - case int64_type: { - char buf[sizeof("-9223372036854775808")]; - SNPRINTF(buf, sizeof(buf), "%" PRId64, u_.int64_); - return buf; - } -#endif - case number_type: { - char buf[256]; - double tmp; - SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); -#if PICOJSON_USE_LOCALE - char *decimal_point = localeconv()->decimal_point; - if (strcmp(decimal_point, ".") != 0) { - size_t decimal_point_len = strlen(decimal_point); - for (char *p = buf; *p != '\0'; ++p) { - if (strncmp(p, decimal_point, decimal_point_len) == 0) { - return std::string(buf, p) + "." + (p + decimal_point_len); - } - } - } -#endif - return buf; - } - case string_type: - return *u_.string_; - case array_type: - return "array"; - case object_type: - return "object"; - default: - PICOJSON_ASSERT(0); -#ifdef _MSC_VER - __assume(0); -#endif - } - return std::string(); -} - -template void copy(const std::string &s, Iter oi) { - std::copy(s.begin(), s.end(), oi); -} - -template struct serialize_str_char { - Iter oi; - void operator()(char c) { - switch (c) { -#define MAP(val, sym) \ - case val: \ - copy(sym, oi); \ - break - MAP('"', "\\\""); - MAP('\\', "\\\\"); - MAP('/', "\\/"); - MAP('\b', "\\b"); - MAP('\f', "\\f"); - MAP('\n', "\\n"); - MAP('\r', "\\r"); - MAP('\t', "\\t"); -#undef MAP - default: - if (static_cast(c) < 0x20 || c == 0x7f) { - char buf[7]; - SNPRINTF(buf, sizeof(buf), "\\u%04x", c & 0xff); - copy(buf, buf + 6, oi); - } else { - *oi++ = c; - } - break; - } - } -}; - -template void serialize_str(const std::string &s, Iter oi) { - *oi++ = '"'; - serialize_str_char process_char = {oi}; - std::for_each(s.begin(), s.end(), process_char); - *oi++ = '"'; -} - -template void value::serialize(Iter oi, bool prettify) const { - return _serialize(oi, prettify ? 0 : -1); -} - -inline std::string value::serialize(bool prettify) const { - return _serialize(prettify ? 0 : -1); -} - -template void value::_indent(Iter oi, int indent) { - *oi++ = '\n'; - for (int i = 0; i < indent * INDENT_WIDTH; ++i) { - *oi++ = ' '; - } -} - -template void value::_serialize(Iter oi, int indent) const { - switch (type_) { - case string_type: - serialize_str(*u_.string_, oi); - break; - case array_type: { - *oi++ = '['; - if (indent != -1) { - ++indent; - } - for (array::const_iterator i = u_.array_->begin(); i != u_.array_->end(); ++i) { - if (i != u_.array_->begin()) { - *oi++ = ','; - } - if (indent != -1) { - _indent(oi, indent); - } - i->_serialize(oi, indent); - } - if (indent != -1) { - --indent; - if (!u_.array_->empty()) { - _indent(oi, indent); - } - } - *oi++ = ']'; - break; - } - case object_type: { - *oi++ = '{'; - if (indent != -1) { - ++indent; - } - for (object::const_iterator i = u_.object_->begin(); i != u_.object_->end(); ++i) { - if (i != u_.object_->begin()) { - *oi++ = ','; - } - if (indent != -1) { - _indent(oi, indent); - } - serialize_str(i->first, oi); - *oi++ = ':'; - if (indent != -1) { - *oi++ = ' '; - } - i->second._serialize(oi, indent); - } - if (indent != -1) { - --indent; - if (!u_.object_->empty()) { - _indent(oi, indent); - } - } - *oi++ = '}'; - break; - } - default: - copy(to_str(), oi); - break; - } - if (indent == 0) { - *oi++ = '\n'; - } -} - -inline std::string value::_serialize(int indent) const { - std::string s; - _serialize(std::back_inserter(s), indent); - return s; -} - -template class input { -protected: - Iter cur_, end_; - bool consumed_; - int line_; - -public: - input(const Iter &first, const Iter &last) : cur_(first), end_(last), consumed_(false), line_(1) { - } - int getc() { - if (consumed_) { - if (*cur_ == '\n') { - ++line_; - } - ++cur_; - } - if (cur_ == end_) { - consumed_ = false; - return -1; - } - consumed_ = true; - return *cur_ & 0xff; - } - void ungetc() { - consumed_ = false; - } - Iter cur() const { - if (consumed_) { - input *self = const_cast *>(this); - self->consumed_ = false; - ++self->cur_; - } - return cur_; - } - int line() const { - return line_; - } - void skip_ws() { - while (1) { - int ch = getc(); - if (!(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { - ungetc(); - break; - } - } - } - bool expect(const int expected) { - skip_ws(); - if (getc() != expected) { - ungetc(); - return false; - } - return true; - } - bool match(const std::string &pattern) { - for (std::string::const_iterator pi(pattern.begin()); pi != pattern.end(); ++pi) { - if (getc() != *pi) { - ungetc(); - return false; - } - } - return true; - } -}; - -template inline int _parse_quadhex(input &in) { - int uni_ch = 0, hex; - for (int i = 0; i < 4; i++) { - if ((hex = in.getc()) == -1) { - return -1; - } - if ('0' <= hex && hex <= '9') { - hex -= '0'; - } else if ('A' <= hex && hex <= 'F') { - hex -= 'A' - 0xa; - } else if ('a' <= hex && hex <= 'f') { - hex -= 'a' - 0xa; - } else { - in.ungetc(); - return -1; - } - uni_ch = uni_ch * 16 + hex; - } - return uni_ch; -} - -template inline bool _parse_codepoint(String &out, input &in) { - int uni_ch; - if ((uni_ch = _parse_quadhex(in)) == -1) { - return false; - } - if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { - if (0xdc00 <= uni_ch) { - // a second 16-bit of a surrogate pair appeared - return false; - } - // first 16-bit of surrogate pair, get the next one - if (in.getc() != '\\' || in.getc() != 'u') { - in.ungetc(); - return false; - } - int second = _parse_quadhex(in); - if (!(0xdc00 <= second && second <= 0xdfff)) { - return false; - } - uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); - uni_ch += 0x10000; - } - if (uni_ch < 0x80) { - out.push_back(static_cast(uni_ch)); - } else { - if (uni_ch < 0x800) { - out.push_back(static_cast(0xc0 | (uni_ch >> 6))); - } else { - if (uni_ch < 0x10000) { - out.push_back(static_cast(0xe0 | (uni_ch >> 12))); - } else { - out.push_back(static_cast(0xf0 | (uni_ch >> 18))); - out.push_back(static_cast(0x80 | ((uni_ch >> 12) & 0x3f))); - } - out.push_back(static_cast(0x80 | ((uni_ch >> 6) & 0x3f))); - } - out.push_back(static_cast(0x80 | (uni_ch & 0x3f))); - } - return true; -} - -template inline bool _parse_string(String &out, input &in) { - while (1) { - int ch = in.getc(); - if (ch < ' ') { - in.ungetc(); - return false; - } else if (ch == '"') { - return true; - } else if (ch == '\\') { - if ((ch = in.getc()) == -1) { - return false; - } - switch (ch) { -#define MAP(sym, val) \ - case sym: \ - out.push_back(val); \ - break - MAP('"', '\"'); - MAP('\\', '\\'); - MAP('/', '/'); - MAP('b', '\b'); - MAP('f', '\f'); - MAP('n', '\n'); - MAP('r', '\r'); - MAP('t', '\t'); -#undef MAP - case 'u': - if (!_parse_codepoint(out, in)) { - return false; - } - break; - default: - return false; - } - } else { - out.push_back(static_cast(ch)); - } - } - return false; -} - -template inline bool _parse_array(Context &ctx, input &in) { - if (!ctx.parse_array_start()) { - return false; - } - size_t idx = 0; - if (in.expect(']')) { - return ctx.parse_array_stop(idx); - } - do { - if (!ctx.parse_array_item(in, idx)) { - return false; - } - idx++; - } while (in.expect(',')); - return in.expect(']') && ctx.parse_array_stop(idx); -} - -template inline bool _parse_object(Context &ctx, input &in) { - if (!ctx.parse_object_start()) { - return false; - } - if (in.expect('}')) { - return ctx.parse_object_stop(); - } - do { - std::string key; - if (!in.expect('"') || !_parse_string(key, in) || !in.expect(':')) { - return false; - } - if (!ctx.parse_object_item(in, key)) { - return false; - } - } while (in.expect(',')); - return in.expect('}') && ctx.parse_object_stop(); -} - -template inline std::string _parse_number(input &in) { - std::string num_str; - while (1) { - int ch = in.getc(); - if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == 'e' || ch == 'E') { - num_str.push_back(static_cast(ch)); - } else if (ch == '.') { -#if PICOJSON_USE_LOCALE - num_str += localeconv()->decimal_point; -#else - num_str.push_back('.'); -#endif - } else { - in.ungetc(); - break; - } - } - return num_str; -} - -template inline bool _parse(Context &ctx, input &in) { - in.skip_ws(); - int ch = in.getc(); - switch (ch) { -#define IS(ch, text, op) \ - case ch: \ - if (in.match(text) && op) { \ - return true; \ - } else { \ - return false; \ - } - IS('n', "ull", ctx.set_null()); - IS('f', "alse", ctx.set_bool(false)); - IS('t', "rue", ctx.set_bool(true)); -#undef IS - case '"': - return ctx.parse_string(in); - case '[': - return _parse_array(ctx, in); - case '{': - return _parse_object(ctx, in); - default: - if (('0' <= ch && ch <= '9') || ch == '-') { - double f; - char *endp; - in.ungetc(); - std::string num_str(_parse_number(in)); - if (num_str.empty()) { - return false; - } -#ifdef PICOJSON_USE_INT64 - { - errno = 0; - intmax_t ival = strtoimax(num_str.c_str(), &endp, 10); - if (errno == 0 && std::numeric_limits::min() <= ival && ival <= std::numeric_limits::max() && - endp == num_str.c_str() + num_str.size()) { - ctx.set_int64(ival); - return true; - } - } -#endif - f = strtod(num_str.c_str(), &endp); - if (endp == num_str.c_str() + num_str.size()) { - ctx.set_number(f); - return true; - } - return false; - } - break; - } - in.ungetc(); - return false; -} - -class deny_parse_context { -public: - bool set_null() { - return false; - } - bool set_bool(bool) { - return false; - } -#ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t) { - return false; - } -#endif - bool set_number(double) { - return false; - } - template bool parse_string(input &) { - return false; - } - bool parse_array_start() { - return false; - } - template bool parse_array_item(input &, size_t) { - return false; - } - bool parse_array_stop(size_t) { - return false; - } - bool parse_object_start() { - return false; - } - template bool parse_object_item(input &, const std::string &) { - return false; - } -}; - -class default_parse_context { -protected: - value *out_; - size_t depths_; - -public: - default_parse_context(value *out, size_t depths = DEFAULT_MAX_DEPTHS) : out_(out), depths_(depths) { - } - bool set_null() { - *out_ = value(); - return true; - } - bool set_bool(bool b) { - *out_ = value(b); - return true; - } -#ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t i) { - *out_ = value(i); - return true; - } -#endif - bool set_number(double f) { - *out_ = value(f); - return true; - } - template bool parse_string(input &in) { - *out_ = value(string_type, false); - return _parse_string(out_->get(), in); - } - bool parse_array_start() { - if (depths_ == 0) - return false; - --depths_; - *out_ = value(array_type, false); - return true; - } - template bool parse_array_item(input &in, size_t) { - array &a = out_->get(); - a.push_back(value()); - default_parse_context ctx(&a.back(), depths_); - return _parse(ctx, in); - } - bool parse_array_stop(size_t) { - ++depths_; - return true; - } - bool parse_object_start() { - if (depths_ == 0) - return false; - *out_ = value(object_type, false); - return true; - } - template bool parse_object_item(input &in, const std::string &key) { - object &o = out_->get(); - default_parse_context ctx(&o[key], depths_); - return _parse(ctx, in); - } - bool parse_object_stop() { - ++depths_; - return true; - } - -private: - default_parse_context(const default_parse_context &); - default_parse_context &operator=(const default_parse_context &); -}; - -class null_parse_context { -protected: - size_t depths_; - -public: - struct dummy_str { - void push_back(int) { - } - }; - -public: - null_parse_context(size_t depths = DEFAULT_MAX_DEPTHS) : depths_(depths) { - } - bool set_null() { - return true; - } - bool set_bool(bool) { - return true; - } -#ifdef PICOJSON_USE_INT64 - bool set_int64(int64_t) { - return true; - } -#endif - bool set_number(double) { - return true; - } - template bool parse_string(input &in) { - dummy_str s; - return _parse_string(s, in); - } - bool parse_array_start() { - if (depths_ == 0) - return false; - --depths_; - return true; - } - template bool parse_array_item(input &in, size_t) { - return _parse(*this, in); - } - bool parse_array_stop(size_t) { - ++depths_; - return true; - } - bool parse_object_start() { - if (depths_ == 0) - return false; - --depths_; - return true; - } - template bool parse_object_item(input &in, const std::string &) { - ++depths_; - return _parse(*this, in); - } - bool parse_object_stop() { - return true; - } - -private: - null_parse_context(const null_parse_context &); - null_parse_context &operator=(const null_parse_context &); -}; - -// obsolete, use the version below -template inline std::string parse(value &out, Iter &pos, const Iter &last) { - std::string err; - pos = parse(out, pos, last, &err); - return err; -} - -template inline Iter _parse(Context &ctx, const Iter &first, const Iter &last, std::string *err) { - input in(first, last); - if (!_parse(ctx, in) && err != NULL) { - char buf[64]; - SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); - *err = buf; - while (1) { - int ch = in.getc(); - if (ch == -1 || ch == '\n') { - break; - } else if (ch >= ' ') { - err->push_back(static_cast(ch)); - } - } - } - return in.cur(); -} - -template inline Iter parse(value &out, const Iter &first, const Iter &last, std::string *err) { - default_parse_context ctx(&out); - return _parse(ctx, first, last, err); -} - -inline std::string parse(value &out, const std::string &s) { - std::string err; - parse(out, s.begin(), s.end(), &err); - return err; -} - -inline std::string parse(value &out, std::istream &is) { - std::string err; - parse(out, std::istreambuf_iterator(is.rdbuf()), std::istreambuf_iterator(), &err); - return err; -} - -template struct last_error_t { static std::string s; }; -template std::string last_error_t::s; - -inline void set_last_error(const std::string &s) { - last_error_t::s = s; -} - -inline const std::string &get_last_error() { - return last_error_t::s; -} - -inline bool operator==(const value &x, const value &y) { - if (x.is()) - return y.is(); -#define PICOJSON_CMP(type) \ - if (x.is()) \ - return y.is() && x.get() == y.get() - PICOJSON_CMP(bool); - PICOJSON_CMP(double); - PICOJSON_CMP(std::string); - PICOJSON_CMP(array); - PICOJSON_CMP(object); -#undef PICOJSON_CMP - PICOJSON_ASSERT(0); -#ifdef _MSC_VER - __assume(0); -#endif - return false; -} - -inline bool operator!=(const value &x, const value &y) { - return !(x == y); -} -} - -#if !PICOJSON_USE_RVALUE_REFERENCE -namespace std { -template <> inline void swap(picojson::value &x, picojson::value &y) { - x.swap(y); -} -} -#endif - -inline std::istream &operator>>(std::istream &is, picojson::value &x) { - picojson::set_last_error(std::string()); - const std::string err(picojson::parse(x, is)); - if (!err.empty()) { - picojson::set_last_error(err); - is.setstate(std::ios::failbit); - } - return is; -} - -inline std::ostream &operator<<(std::ostream &os, const picojson::value &x) { - x.serialize(std::ostream_iterator(os)); - return os; -} -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -#endif \ No newline at end of file