正在显示
63 个修改的文件
包含
362 行增加
和
218 行删除
.clang-tidy
0 → 100644
| 1 | +--- | ||
| 2 | +# NOTE there must be no spaces before the '-', so put the comma last. | ||
| 3 | +# The check bugprone-unchecked-optional-access is also turned off atm | ||
| 4 | +# because it causes clang-tidy to hang randomly. The tracking issue | ||
| 5 | +# can be found at https://github.com/llvm/llvm-project/issues/69369. | ||
| 6 | +# | ||
| 7 | +# Modified from | ||
| 8 | +# https://github.com/pytorch/pytorch/blob/main/.clang-tidy | ||
| 9 | +InheritParentConfig: true | ||
| 10 | +Checks: ' | ||
| 11 | +bugprone-*, | ||
| 12 | +-bugprone-easily-swappable-parameters, | ||
| 13 | +-bugprone-forward-declaration-namespace, | ||
| 14 | +-bugprone-implicit-widening-of-multiplication-result, | ||
| 15 | +-bugprone-macro-parentheses, | ||
| 16 | +-bugprone-lambda-function-name, | ||
| 17 | +-bugprone-narrowing-conversions, | ||
| 18 | +-bugprone-reserved-identifier, | ||
| 19 | +-bugprone-swapped-arguments, | ||
| 20 | +-bugprone-unchecked-optional-access, | ||
| 21 | +clang-diagnostic-missing-prototypes, | ||
| 22 | +cppcoreguidelines-*, | ||
| 23 | +-cppcoreguidelines-avoid-const-or-ref-data-members, | ||
| 24 | +-cppcoreguidelines-avoid-do-while, | ||
| 25 | +-cppcoreguidelines-avoid-magic-numbers, | ||
| 26 | +-cppcoreguidelines-avoid-non-const-global-variables, | ||
| 27 | +-cppcoreguidelines-interfaces-global-init, | ||
| 28 | +-cppcoreguidelines-macro-usage, | ||
| 29 | +-cppcoreguidelines-narrowing-conversions, | ||
| 30 | +-cppcoreguidelines-owning-memory, | ||
| 31 | +-cppcoreguidelines-pro-bounds-array-to-pointer-decay, | ||
| 32 | +-cppcoreguidelines-pro-bounds-constant-array-index, | ||
| 33 | +-cppcoreguidelines-pro-bounds-pointer-arithmetic, | ||
| 34 | +-cppcoreguidelines-pro-type-const-cast, | ||
| 35 | +-cppcoreguidelines-pro-type-cstyle-cast, | ||
| 36 | +-cppcoreguidelines-pro-type-reinterpret-cast, | ||
| 37 | +-cppcoreguidelines-pro-type-static-cast-downcast, | ||
| 38 | +-cppcoreguidelines-pro-type-union-access, | ||
| 39 | +-cppcoreguidelines-pro-type-vararg, | ||
| 40 | +-cppcoreguidelines-special-member-functions, | ||
| 41 | +-cppcoreguidelines-non-private-member-variables-in-classes, | ||
| 42 | +-facebook-hte-RelativeInclude, | ||
| 43 | +hicpp-exception-baseclass, | ||
| 44 | +hicpp-avoid-goto, | ||
| 45 | +misc-*, | ||
| 46 | +-misc-const-correctness, | ||
| 47 | +-misc-include-cleaner, | ||
| 48 | +-misc-use-anonymous-namespace, | ||
| 49 | +-misc-unused-parameters, | ||
| 50 | +-misc-no-recursion, | ||
| 51 | +-misc-non-private-member-variables-in-classes, | ||
| 52 | +-misc-confusable-identifiers, | ||
| 53 | +modernize-*, | ||
| 54 | +-modernize-macro-to-enum, | ||
| 55 | +-modernize-pass-by-value, | ||
| 56 | +-modernize-return-braced-init-list, | ||
| 57 | +-modernize-use-auto, | ||
| 58 | +-modernize-use-default-member-init, | ||
| 59 | +-modernize-use-using, | ||
| 60 | +-modernize-use-trailing-return-type, | ||
| 61 | +-modernize-use-nodiscard, | ||
| 62 | +performance-*, | ||
| 63 | +readability-container-size-empty, | ||
| 64 | +readability-delete-null-pointer, | ||
| 65 | +readability-duplicate-include | ||
| 66 | +readability-misplaced-array-index, | ||
| 67 | +readability-redundant-function-ptr-dereference, | ||
| 68 | +readability-redundant-smartptr-get, | ||
| 69 | +readability-simplify-subscript-expr, | ||
| 70 | +readability-string-compare, | ||
| 71 | +' | ||
| 72 | +WarningsAsErrors: '*' | ||
| 73 | +... |
.github/workflows/clang-tidy.yaml
0 → 100644
| 1 | +name: clang-tidy | ||
| 2 | + | ||
| 3 | +on: | ||
| 4 | + push: | ||
| 5 | + branches: | ||
| 6 | + - master | ||
| 7 | + - clang-tidy | ||
| 8 | + paths: | ||
| 9 | + - 'sherpa-onnx/csrc/**' | ||
| 10 | + | ||
| 11 | + pull_request: | ||
| 12 | + branches: | ||
| 13 | + - master | ||
| 14 | + paths: | ||
| 15 | + - 'sherpa-onnx/csrc/**' | ||
| 16 | + | ||
| 17 | + workflow_dispatch: | ||
| 18 | + | ||
| 19 | +concurrency: | ||
| 20 | + group: clang-tidy-${{ github.ref }} | ||
| 21 | + cancel-in-progress: true | ||
| 22 | + | ||
| 23 | +jobs: | ||
| 24 | + clang-tidy: | ||
| 25 | + runs-on: ubuntu-latest | ||
| 26 | + strategy: | ||
| 27 | + matrix: | ||
| 28 | + python-version: [3.8] | ||
| 29 | + fail-fast: false | ||
| 30 | + | ||
| 31 | + steps: | ||
| 32 | + - uses: actions/checkout@v4 | ||
| 33 | + with: | ||
| 34 | + fetch-depth: 0 | ||
| 35 | + | ||
| 36 | + - name: Setup Python ${{ matrix.python-version }} | ||
| 37 | + uses: actions/setup-python@v5 | ||
| 38 | + with: | ||
| 39 | + python-version: ${{ matrix.python-version }} | ||
| 40 | + | ||
| 41 | + - name: Install clang-tidy | ||
| 42 | + shell: bash | ||
| 43 | + run: | | ||
| 44 | + pip install clang-tidy | ||
| 45 | + | ||
| 46 | + - name: Configure | ||
| 47 | + shell: bash | ||
| 48 | + run: | | ||
| 49 | + mkdir build | ||
| 50 | + cd build | ||
| 51 | + cmake -DSHERPA_ONNX_ENABLE_PYTHON=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. | ||
| 52 | + | ||
| 53 | + - name: Check with clang-tidy | ||
| 54 | + shell: bash | ||
| 55 | + run: | | ||
| 56 | + cd build | ||
| 57 | + make check |
| @@ -184,6 +184,7 @@ jobs: | @@ -184,6 +184,7 @@ jobs: | ||
| 184 | path: ./*.tar.bz2 | 184 | path: ./*.tar.bz2 |
| 185 | 185 | ||
| 186 | - name: Publish to huggingface | 186 | - name: Publish to huggingface |
| 187 | + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release' | ||
| 187 | env: | 188 | env: |
| 188 | HF_TOKEN: ${{ secrets.HF_TOKEN }} | 189 | HF_TOKEN: ${{ secrets.HF_TOKEN }} |
| 189 | uses: nick-fields/retry@v3 | 190 | uses: nick-fields/retry@v3 |
| @@ -133,6 +133,7 @@ jobs: | @@ -133,6 +133,7 @@ jobs: | ||
| 133 | shell: bash | 133 | shell: bash |
| 134 | run: | | 134 | run: | |
| 135 | d=$PWD | 135 | d=$PWD |
| 136 | + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) | ||
| 136 | 137 | ||
| 137 | pushd sherpa-onnx/flutter | 138 | pushd sherpa-onnx/flutter |
| 138 | dart pub get | 139 | dart pub get |
| @@ -159,6 +160,7 @@ jobs: | @@ -159,6 +160,7 @@ jobs: | ||
| 159 | path: ./*.tar.bz2 | 160 | path: ./*.tar.bz2 |
| 160 | 161 | ||
| 161 | - name: Publish to huggingface | 162 | - name: Publish to huggingface |
| 163 | + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release' | ||
| 162 | env: | 164 | env: |
| 163 | HF_TOKEN: ${{ secrets.HF_TOKEN }} | 165 | HF_TOKEN: ${{ secrets.HF_TOKEN }} |
| 164 | uses: nick-fields/retry@v3 | 166 | uses: nick-fields/retry@v3 |
| @@ -167,7 +167,7 @@ if(SHERPA_ONNX_ENABLE_WASM_KWS) | @@ -167,7 +167,7 @@ if(SHERPA_ONNX_ENABLE_WASM_KWS) | ||
| 167 | endif() | 167 | endif() |
| 168 | 168 | ||
| 169 | if(NOT CMAKE_CXX_STANDARD) | 169 | if(NOT CMAKE_CXX_STANDARD) |
| 170 | - set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.") | 170 | + set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ version to be used.") |
| 171 | endif() | 171 | endif() |
| 172 | set(CMAKE_CXX_EXTENSIONS OFF) | 172 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 173 | message(STATUS "C++ Standard version: ${CMAKE_CXX_STANDARD}") | 173 | message(STATUS "C++ Standard version: ${CMAKE_CXX_STANDARD}") |
| @@ -3,18 +3,18 @@ | @@ -3,18 +3,18 @@ | ||
| 3 | function(download_openfst) | 3 | function(download_openfst) |
| 4 | include(FetchContent) | 4 | include(FetchContent) |
| 5 | 5 | ||
| 6 | - set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz") | ||
| 7 | - set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz") | ||
| 8 | - set(openfst_HASH "SHA256=f10a71c6b64d89eabdc316d372b956c30c825c7c298e2f20c780320e8181ffb6") | 6 | + set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz") |
| 7 | + set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz") | ||
| 8 | + set(openfst_HASH "SHA256=5c98e82cc509c5618502dde4860b8ea04d843850ed57e6d6b590b644b268853d") | ||
| 9 | 9 | ||
| 10 | # If you don't have access to the Internet, | 10 | # If you don't have access to the Internet, |
| 11 | # please pre-download it | 11 | # please pre-download it |
| 12 | set(possible_file_locations | 12 | set(possible_file_locations |
| 13 | - $ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-13.tar.gz | ||
| 14 | - ${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz | ||
| 15 | - ${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz | ||
| 16 | - /tmp/openfst-sherpa-onnx-2024-06-13.tar.gz | ||
| 17 | - /star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-13.tar.gz | 13 | + $ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-19.tar.gz |
| 14 | + ${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz | ||
| 15 | + ${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz | ||
| 16 | + /tmp/openfst-sherpa-onnx-2024-06-19.tar.gz | ||
| 17 | + /star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-19.tar.gz | ||
| 18 | ) | 18 | ) |
| 19 | 19 | ||
| 20 | foreach(f IN LISTS possible_file_locations) | 20 | foreach(f IN LISTS possible_file_locations) |
| @@ -534,3 +534,17 @@ if(SHERPA_ONNX_ENABLE_TESTS) | @@ -534,3 +534,17 @@ if(SHERPA_ONNX_ENABLE_TESTS) | ||
| 534 | sherpa_onnx_add_test(${source}) | 534 | sherpa_onnx_add_test(${source}) |
| 535 | endforeach() | 535 | endforeach() |
| 536 | endif() | 536 | endif() |
| 537 | + | ||
| 538 | +set(srcs_to_check) | ||
| 539 | +foreach(s IN LISTS sources) | ||
| 540 | + list(APPEND srcs_to_check ${CMAKE_CURRENT_LIST_DIR}/${s}) | ||
| 541 | +endforeach() | ||
| 542 | + | ||
| 543 | +# For clang-tidy | ||
| 544 | +add_custom_target( | ||
| 545 | + clang-tidy-check | ||
| 546 | + clang-tidy -p ${CMAKE_BINARY_DIR}/compile_commands.json --config-file ${CMAKE_SOURCE_DIR}/.clang-tidy ${srcs_to_check} | ||
| 547 | + DEPENDS ${sources}) | ||
| 548 | + | ||
| 549 | +add_custom_target(check DEPENDS clang-tidy-check) | ||
| 550 | + |
| @@ -60,7 +60,7 @@ void AudioTaggingLabels::Init(std::istream &is) { | @@ -60,7 +60,7 @@ void AudioTaggingLabels::Init(std::istream &is) { | ||
| 60 | 60 | ||
| 61 | std::size_t pos{}; | 61 | std::size_t pos{}; |
| 62 | int32_t i = std::stoi(index, &pos); | 62 | int32_t i = std::stoi(index, &pos); |
| 63 | - if (index.size() == 0 || pos != index.size()) { | 63 | + if (index.empty() || pos != index.size()) { |
| 64 | SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str()); | 64 | SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str()); |
| 65 | exit(-1); | 65 | exit(-1); |
| 66 | } | 66 | } |
| @@ -34,7 +34,7 @@ std::string Base64Decode(const std::string &s) { | @@ -34,7 +34,7 @@ std::string Base64Decode(const std::string &s) { | ||
| 34 | exit(-1); | 34 | exit(-1); |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | - int32_t n = s.size() / 4 * 3; | 37 | + int32_t n = static_cast<int32_t>(s.size()) / 4 * 3; |
| 38 | 38 | ||
| 39 | std::string ans; | 39 | std::string ans; |
| 40 | ans.reserve(n); | 40 | ans.reserve(n); |
| @@ -46,16 +46,16 @@ std::string Base64Decode(const std::string &s) { | @@ -46,16 +46,16 @@ std::string Base64Decode(const std::string &s) { | ||
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | int32_t first = (Ord(s[i]) << 2) + ((Ord(s[i + 1]) & 0x30) >> 4); | 48 | int32_t first = (Ord(s[i]) << 2) + ((Ord(s[i + 1]) & 0x30) >> 4); |
| 49 | - ans.push_back(first); | 49 | + ans.push_back(static_cast<char>(first)); |
| 50 | 50 | ||
| 51 | if (i + 2 < static_cast<int32_t>(s.size()) && s[i + 2] != '=') { | 51 | if (i + 2 < static_cast<int32_t>(s.size()) && s[i + 2] != '=') { |
| 52 | int32_t second = | 52 | int32_t second = |
| 53 | ((Ord(s[i + 1]) & 0x0f) << 4) + ((Ord(s[i + 2]) & 0x3c) >> 2); | 53 | ((Ord(s[i + 1]) & 0x0f) << 4) + ((Ord(s[i + 2]) & 0x3c) >> 2); |
| 54 | - ans.push_back(second); | 54 | + ans.push_back(static_cast<char>(second)); |
| 55 | 55 | ||
| 56 | if (i + 3 < static_cast<int32_t>(s.size()) && s[i + 3] != '=') { | 56 | if (i + 3 < static_cast<int32_t>(s.size()) && s[i + 3] != '=') { |
| 57 | int32_t third = ((Ord(s[i + 2]) & 0x03) << 6) + Ord(s[i + 3]); | 57 | int32_t third = ((Ord(s[i + 2]) & 0x03) << 6) + Ord(s[i + 3]); |
| 58 | - ans.push_back(third); | 58 | + ans.push_back(static_cast<char>(third)); |
| 59 | } | 59 | } |
| 60 | } | 60 | } |
| 61 | i += 4; | 61 | i += 4; |
| @@ -82,9 +82,9 @@ Ort::Value Cat(OrtAllocator *allocator, | @@ -82,9 +82,9 @@ Ort::Value Cat(OrtAllocator *allocator, | ||
| 82 | T *dst = ans.GetTensorMutableData<T>(); | 82 | T *dst = ans.GetTensorMutableData<T>(); |
| 83 | 83 | ||
| 84 | for (int32_t i = 0; i != leading_size; ++i) { | 84 | for (int32_t i = 0; i != leading_size; ++i) { |
| 85 | - for (int32_t n = 0; n != static_cast<int32_t>(values.size()); ++n) { | ||
| 86 | - auto this_dim = values[n]->GetTensorTypeAndShapeInfo().GetShape()[dim]; | ||
| 87 | - const T *src = values[n]->GetTensorData<T>(); | 85 | + for (auto value : values) { |
| 86 | + auto this_dim = value->GetTensorTypeAndShapeInfo().GetShape()[dim]; | ||
| 87 | + const T *src = value->GetTensorData<T>(); | ||
| 88 | src += i * this_dim * trailing_size; | 88 | src += i * this_dim * trailing_size; |
| 89 | 89 | ||
| 90 | std::copy(src, src + this_dim * trailing_size, dst); | 90 | std::copy(src, src + this_dim * trailing_size, dst); |
| @@ -20,7 +20,7 @@ CircularBuffer::CircularBuffer(int32_t capacity) { | @@ -20,7 +20,7 @@ CircularBuffer::CircularBuffer(int32_t capacity) { | ||
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | void CircularBuffer::Resize(int32_t new_capacity) { | 22 | void CircularBuffer::Resize(int32_t new_capacity) { |
| 23 | - int32_t capacity = buffer_.size(); | 23 | + int32_t capacity = static_cast<int32_t>(buffer_.size()); |
| 24 | if (new_capacity <= capacity) { | 24 | if (new_capacity <= capacity) { |
| 25 | SHERPA_ONNX_LOGE("new_capacity (%d) <= original capacity (%d). Skip it.", | 25 | SHERPA_ONNX_LOGE("new_capacity (%d) <= original capacity (%d). Skip it.", |
| 26 | new_capacity, capacity); | 26 | new_capacity, capacity); |
| @@ -86,7 +86,7 @@ void CircularBuffer::Resize(int32_t new_capacity) { | @@ -86,7 +86,7 @@ void CircularBuffer::Resize(int32_t new_capacity) { | ||
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | void CircularBuffer::Push(const float *p, int32_t n) { | 88 | void CircularBuffer::Push(const float *p, int32_t n) { |
| 89 | - int32_t capacity = buffer_.size(); | 89 | + int32_t capacity = static_cast<int32_t>(buffer_.size()); |
| 90 | int32_t size = Size(); | 90 | int32_t size = Size(); |
| 91 | if (n + size > capacity) { | 91 | if (n + size > capacity) { |
| 92 | int32_t new_capacity = std::max(capacity * 2, n + size); | 92 | int32_t new_capacity = std::max(capacity * 2, n + size); |
| @@ -126,7 +126,7 @@ std::vector<float> CircularBuffer::Get(int32_t start_index, int32_t n) const { | @@ -126,7 +126,7 @@ std::vector<float> CircularBuffer::Get(int32_t start_index, int32_t n) const { | ||
| 126 | return {}; | 126 | return {}; |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | - int32_t capacity = buffer_.size(); | 129 | + int32_t capacity = static_cast<int32_t>(buffer_.size()); |
| 130 | 130 | ||
| 131 | if (start_index - head_ + n > size) { | 131 | if (start_index - head_ + n > size) { |
| 132 | SHERPA_ONNX_LOGE("Invalid start_index: %d and n: %d. head_: %d, size: %d", | 132 | SHERPA_ONNX_LOGE("Invalid start_index: %d and n: %d. head_: %d, size: %d", |
| @@ -67,8 +67,8 @@ void ContextGraph::Build(const std::vector<std::vector<int32_t>> &token_ids, | @@ -67,8 +67,8 @@ void ContextGraph::Build(const std::vector<std::vector<int32_t>> &token_ids, | ||
| 67 | std::tuple<float, const ContextState *, const ContextState *> | 67 | std::tuple<float, const ContextState *, const ContextState *> |
| 68 | ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, | 68 | ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, |
| 69 | bool strict_mode /*= true*/) const { | 69 | bool strict_mode /*= true*/) const { |
| 70 | - const ContextState *node; | ||
| 71 | - float score; | 70 | + const ContextState *node = nullptr; |
| 71 | + float score = 0; | ||
| 72 | if (1 == state->next.count(token)) { | 72 | if (1 == state->next.count(token)) { |
| 73 | node = state->next.at(token).get(); | 73 | node = state->next.at(token).get(); |
| 74 | score = node->token_score; | 74 | score = node->token_score; |
| @@ -84,7 +84,10 @@ ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, | @@ -84,7 +84,10 @@ ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, | ||
| 84 | score = node->node_score - state->node_score; | 84 | score = node->node_score - state->node_score; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | - SHERPA_ONNX_CHECK(nullptr != node); | 87 | + if (!node) { |
| 88 | + SHERPA_ONNX_LOGE("Some bad things happened."); | ||
| 89 | + exit(-1); | ||
| 90 | + } | ||
| 88 | 91 | ||
| 89 | const ContextState *matched_node = | 92 | const ContextState *matched_node = |
| 90 | node->is_end ? node : (node->output != nullptr ? node->output : nullptr); | 93 | node->is_end ? node : (node->output != nullptr ? node->output : nullptr); |
| @@ -73,10 +73,15 @@ std::string EndpointConfig::ToString() const { | @@ -73,10 +73,15 @@ std::string EndpointConfig::ToString() const { | ||
| 73 | return os.str(); | 73 | return os.str(); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | -bool Endpoint::IsEndpoint(int num_frames_decoded, int trailing_silence_frames, | 76 | +bool Endpoint::IsEndpoint(int32_t num_frames_decoded, |
| 77 | + int32_t trailing_silence_frames, | ||
| 77 | float frame_shift_in_seconds) const { | 78 | float frame_shift_in_seconds) const { |
| 78 | - float utterance_length = num_frames_decoded * frame_shift_in_seconds; | ||
| 79 | - float trailing_silence = trailing_silence_frames * frame_shift_in_seconds; | 79 | + float utterance_length = |
| 80 | + static_cast<float>(num_frames_decoded) * frame_shift_in_seconds; | ||
| 81 | + | ||
| 82 | + float trailing_silence = | ||
| 83 | + static_cast<float>(trailing_silence_frames) * frame_shift_in_seconds; | ||
| 84 | + | ||
| 80 | if (RuleActivated(config_.rule1, "rule1", trailing_silence, | 85 | if (RuleActivated(config_.rule1, "rule1", trailing_silence, |
| 81 | utterance_length) || | 86 | utterance_length) || |
| 82 | RuleActivated(config_.rule2, "rule2", trailing_silence, | 87 | RuleActivated(config_.rule2, "rule2", trailing_silence, |
| @@ -64,7 +64,7 @@ class Endpoint { | @@ -64,7 +64,7 @@ class Endpoint { | ||
| 64 | 64 | ||
| 65 | /// This function returns true if this set of endpointing rules thinks we | 65 | /// This function returns true if this set of endpointing rules thinks we |
| 66 | /// should terminate decoding. | 66 | /// should terminate decoding. |
| 67 | - bool IsEndpoint(int num_frames_decoded, int trailing_silence_frames, | 67 | + bool IsEndpoint(int32_t num_frames_decoded, int32_t trailing_silence_frames, |
| 68 | float frame_shift_in_seconds) const; | 68 | float frame_shift_in_seconds) const; |
| 69 | 69 | ||
| 70 | private: | 70 | private: |
| @@ -103,6 +103,7 @@ class JiebaLexicon::Impl { | @@ -103,6 +103,7 @@ class JiebaLexicon::Impl { | ||
| 103 | 103 | ||
| 104 | if (w == "。" || w == "!" || w == "?" || w == ",") { | 104 | if (w == "。" || w == "!" || w == "?" || w == ",") { |
| 105 | ans.push_back(std::move(this_sentence)); | 105 | ans.push_back(std::move(this_sentence)); |
| 106 | + this_sentence = {}; | ||
| 106 | } | 107 | } |
| 107 | } // for (const auto &w : words) | 108 | } // for (const auto &w : words) |
| 108 | 109 |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/keyword-spotter.h" | 5 | #include "sherpa-onnx/csrc/keyword-spotter.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <fstream> | 9 | #include <fstream> |
| 11 | #include <iomanip> | 10 | #include <iomanip> |
| 12 | #include <memory> | 11 | #include <memory> |
| @@ -82,7 +82,7 @@ std::unordered_map<std::string, int32_t> ReadTokens(std::istream &is) { | @@ -82,7 +82,7 @@ std::unordered_map<std::string, int32_t> ReadTokens(std::istream &is) { | ||
| 82 | std::string line; | 82 | std::string line; |
| 83 | 83 | ||
| 84 | std::string sym; | 84 | std::string sym; |
| 85 | - int32_t id; | 85 | + int32_t id = -1; |
| 86 | while (std::getline(is, line)) { | 86 | while (std::getline(is, line)) { |
| 87 | std::istringstream iss(line); | 87 | std::istringstream iss(line); |
| 88 | iss >> sym; | 88 | iss >> sym; |
| @@ -254,6 +254,7 @@ std::vector<std::vector<int64_t>> Lexicon::ConvertTextToTokenIdsChinese( | @@ -254,6 +254,7 @@ std::vector<std::vector<int64_t>> Lexicon::ConvertTextToTokenIdsChinese( | ||
| 254 | this_sentence.push_back(eos); | 254 | this_sentence.push_back(eos); |
| 255 | } | 255 | } |
| 256 | ans.push_back(std::move(this_sentence)); | 256 | ans.push_back(std::move(this_sentence)); |
| 257 | + this_sentence = {}; | ||
| 257 | 258 | ||
| 258 | if (sil != -1) { | 259 | if (sil != -1) { |
| 259 | this_sentence.push_back(sil); | 260 | this_sentence.push_back(sil); |
| @@ -324,6 +325,7 @@ std::vector<std::vector<int64_t>> Lexicon::ConvertTextToTokenIdsNotChinese( | @@ -324,6 +325,7 @@ std::vector<std::vector<int64_t>> Lexicon::ConvertTextToTokenIdsNotChinese( | ||
| 324 | if (w != ",") { | 325 | if (w != ",") { |
| 325 | this_sentence.push_back(blank); | 326 | this_sentence.push_back(blank); |
| 326 | ans.push_back(std::move(this_sentence)); | 327 | ans.push_back(std::move(this_sentence)); |
| 328 | + this_sentence = {}; | ||
| 327 | } | 329 | } |
| 328 | 330 | ||
| 329 | continue; | 331 | continue; |
| @@ -62,8 +62,8 @@ class Lexicon : public OfflineTtsFrontend { | @@ -62,8 +62,8 @@ class Lexicon : public OfflineTtsFrontend { | ||
| 62 | std::unordered_map<std::string, std::vector<int32_t>> word2ids_; | 62 | std::unordered_map<std::string, std::vector<int32_t>> word2ids_; |
| 63 | std::unordered_set<std::string> punctuations_; | 63 | std::unordered_set<std::string> punctuations_; |
| 64 | std::unordered_map<std::string, int32_t> token2id_; | 64 | std::unordered_map<std::string, int32_t> token2id_; |
| 65 | - Language language_; | ||
| 66 | - bool debug_; | 65 | + Language language_ = Language::kUnknown; |
| 66 | + bool debug_ = false; | ||
| 67 | }; | 67 | }; |
| 68 | 68 | ||
| 69 | } // namespace sherpa_onnx | 69 | } // namespace sherpa_onnx |
| @@ -67,7 +67,7 @@ class OfflineCtTransformerModel::Impl { | @@ -67,7 +67,7 @@ class OfflineCtTransformerModel::Impl { | ||
| 67 | std::vector<std::string> tokens; | 67 | std::vector<std::string> tokens; |
| 68 | SHERPA_ONNX_READ_META_DATA_VEC_STRING_SEP(tokens, "tokens", "|"); | 68 | SHERPA_ONNX_READ_META_DATA_VEC_STRING_SEP(tokens, "tokens", "|"); |
| 69 | 69 | ||
| 70 | - int32_t vocab_size; | 70 | + int32_t vocab_size = 0; |
| 71 | SHERPA_ONNX_READ_META_DATA(vocab_size, "vocab_size"); | 71 | SHERPA_ONNX_READ_META_DATA(vocab_size, "vocab_size"); |
| 72 | if (static_cast<int32_t>(tokens.size()) != vocab_size) { | 72 | if (static_cast<int32_t>(tokens.size()) != vocab_size) { |
| 73 | SHERPA_ONNX_LOGE("tokens.size() %d != vocab_size %d", | 73 | SHERPA_ONNX_LOGE("tokens.size() %d != vocab_size %d", |
| @@ -4,11 +4,11 @@ | @@ -4,11 +4,11 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/offline-stream.h" | 5 | #include "sherpa-onnx/csrc/offline-stream.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <cmath> | 9 | #include <cmath> |
| 11 | #include <iomanip> | 10 | #include <iomanip> |
| 11 | +#include <utility> | ||
| 12 | 12 | ||
| 13 | #include "kaldi-native-fbank/csrc/online-feature.h" | 13 | #include "kaldi-native-fbank/csrc/online-feature.h" |
| 14 | #include "sherpa-onnx/csrc/macros.h" | 14 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -56,7 +56,7 @@ class OfflineStream::Impl { | @@ -56,7 +56,7 @@ class OfflineStream::Impl { | ||
| 56 | public: | 56 | public: |
| 57 | explicit Impl(const FeatureExtractorConfig &config, | 57 | explicit Impl(const FeatureExtractorConfig &config, |
| 58 | ContextGraphPtr context_graph) | 58 | ContextGraphPtr context_graph) |
| 59 | - : config_(config), context_graph_(context_graph) { | 59 | + : config_(config), context_graph_(std::move(context_graph)) { |
| 60 | if (config.is_mfcc) { | 60 | if (config.is_mfcc) { |
| 61 | mfcc_opts_.frame_opts.dither = config_.dither; | 61 | mfcc_opts_.frame_opts.dither = config_.dither; |
| 62 | mfcc_opts_.frame_opts.snip_edges = config_.snip_edges; | 62 | mfcc_opts_.frame_opts.snip_edges = config_.snip_edges; |
| @@ -266,7 +266,7 @@ class OfflineStream::Impl { | @@ -266,7 +266,7 @@ class OfflineStream::Impl { | ||
| 266 | 266 | ||
| 267 | OfflineStream::OfflineStream(const FeatureExtractorConfig &config /*= {}*/, | 267 | OfflineStream::OfflineStream(const FeatureExtractorConfig &config /*= {}*/, |
| 268 | ContextGraphPtr context_graph /*= nullptr*/) | 268 | ContextGraphPtr context_graph /*= nullptr*/) |
| 269 | - : impl_(std::make_unique<Impl>(config, context_graph)) {} | 269 | + : impl_(std::make_unique<Impl>(config, std::move(context_graph))) {} |
| 270 | 270 | ||
| 271 | OfflineStream::OfflineStream(WhisperTag tag) | 271 | OfflineStream::OfflineStream(WhisperTag tag) |
| 272 | : impl_(std::make_unique<Impl>(tag)) {} | 272 | : impl_(std::make_unique<Impl>(tag)) {} |
| @@ -42,7 +42,7 @@ OfflineTransducerModifiedBeamSearchDecoder::Decode( | @@ -42,7 +42,7 @@ OfflineTransducerModifiedBeamSearchDecoder::Decode( | ||
| 42 | std::vector<ContextGraphPtr> context_graphs(batch_size, nullptr); | 42 | std::vector<ContextGraphPtr> context_graphs(batch_size, nullptr); |
| 43 | 43 | ||
| 44 | for (int32_t i = 0; i < batch_size; ++i) { | 44 | for (int32_t i = 0; i < batch_size; ++i) { |
| 45 | - const ContextState *context_state; | 45 | + const ContextState *context_state = nullptr; |
| 46 | if (ss != nullptr) { | 46 | if (ss != nullptr) { |
| 47 | context_graphs[i] = | 47 | context_graphs[i] = |
| 48 | ss[packed_encoder_out.sorted_indexes[i]]->GetContextGraph(); | 48 | ss[packed_encoder_out.sorted_indexes[i]]->GetContextGraph(); |
| @@ -30,7 +30,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) { | @@ -30,7 +30,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) { | ||
| 30 | 30 | ||
| 31 | std::string sym; | 31 | std::string sym; |
| 32 | std::u32string s; | 32 | std::u32string s; |
| 33 | - int32_t id; | 33 | + int32_t id = 0; |
| 34 | while (std::getline(is, line)) { | 34 | while (std::getline(is, line)) { |
| 35 | std::istringstream iss(line); | 35 | std::istringstream iss(line); |
| 36 | iss >> sym; | 36 | iss >> sym; |
| @@ -138,6 +138,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( | @@ -138,6 +138,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( | ||
| 138 | } | 138 | } |
| 139 | 139 | ||
| 140 | ans.push_back(std::move(this_sentence)); | 140 | ans.push_back(std::move(this_sentence)); |
| 141 | + this_sentence = {}; | ||
| 141 | 142 | ||
| 142 | // re-initialize this_sentence | 143 | // re-initialize this_sentence |
| 143 | if (use_eos_bos) { | 144 | if (use_eos_bos) { |
| @@ -172,6 +173,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( | @@ -172,6 +173,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( | ||
| 172 | } | 173 | } |
| 173 | 174 | ||
| 174 | ans.push_back(std::move(this_sentence)); | 175 | ans.push_back(std::move(this_sentence)); |
| 176 | + this_sentence = {}; | ||
| 175 | 177 | ||
| 176 | // re-initialize this_sentence | 178 | // re-initialize this_sentence |
| 177 | if (use_eos_bos) { | 179 | if (use_eos_bos) { |
| @@ -5,6 +5,7 @@ | @@ -5,6 +5,7 @@ | ||
| 5 | #include "sherpa-onnx/csrc/offline-tts.h" | 5 | #include "sherpa-onnx/csrc/offline-tts.h" |
| 6 | 6 | ||
| 7 | #include <string> | 7 | #include <string> |
| 8 | +#include <utility> | ||
| 8 | 9 | ||
| 9 | #include "sherpa-onnx/csrc/file-utils.h" | 10 | #include "sherpa-onnx/csrc/file-utils.h" |
| 10 | #include "sherpa-onnx/csrc/macros.h" | 11 | #include "sherpa-onnx/csrc/macros.h" |
| @@ -87,7 +88,7 @@ OfflineTts::~OfflineTts() = default; | @@ -87,7 +88,7 @@ OfflineTts::~OfflineTts() = default; | ||
| 87 | GeneratedAudio OfflineTts::Generate( | 88 | GeneratedAudio OfflineTts::Generate( |
| 88 | const std::string &text, int64_t sid /*=0*/, float speed /*= 1.0*/, | 89 | const std::string &text, int64_t sid /*=0*/, float speed /*= 1.0*/, |
| 89 | GeneratedAudioCallback callback /*= nullptr*/) const { | 90 | GeneratedAudioCallback callback /*= nullptr*/) const { |
| 90 | - return impl_->Generate(text, sid, speed, callback); | 91 | + return impl_->Generate(text, sid, speed, std::move(callback)); |
| 91 | } | 92 | } |
| 92 | 93 | ||
| 93 | int32_t OfflineTts::SampleRate() const { return impl_->SampleRate(); } | 94 | int32_t OfflineTts::SampleRate() const { return impl_->SampleRate(); } |
| @@ -22,9 +22,9 @@ class OfflineWhisperModel::Impl { | @@ -22,9 +22,9 @@ class OfflineWhisperModel::Impl { | ||
| 22 | explicit Impl(const OfflineModelConfig &config) | 22 | explicit Impl(const OfflineModelConfig &config) |
| 23 | : config_(config), | 23 | : config_(config), |
| 24 | env_(ORT_LOGGING_LEVEL_ERROR), | 24 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 25 | + debug_(config.debug), | ||
| 25 | sess_opts_(GetSessionOptions(config)), | 26 | sess_opts_(GetSessionOptions(config)), |
| 26 | allocator_{} { | 27 | allocator_{} { |
| 27 | - debug_ = config_.debug; | ||
| 28 | { | 28 | { |
| 29 | auto buf = ReadFile(config.whisper.encoder); | 29 | auto buf = ReadFile(config.whisper.encoder); |
| 30 | InitEncoder(buf.data(), buf.size()); | 30 | InitEncoder(buf.data(), buf.size()); |
| @@ -39,9 +39,9 @@ class OfflineWhisperModel::Impl { | @@ -39,9 +39,9 @@ class OfflineWhisperModel::Impl { | ||
| 39 | explicit Impl(const SpokenLanguageIdentificationConfig &config) | 39 | explicit Impl(const SpokenLanguageIdentificationConfig &config) |
| 40 | : lid_config_(config), | 40 | : lid_config_(config), |
| 41 | env_(ORT_LOGGING_LEVEL_ERROR), | 41 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 42 | + debug_(config_.debug), | ||
| 42 | sess_opts_(GetSessionOptions(config)), | 43 | sess_opts_(GetSessionOptions(config)), |
| 43 | allocator_{} { | 44 | allocator_{} { |
| 44 | - debug_ = config_.debug; | ||
| 45 | { | 45 | { |
| 46 | auto buf = ReadFile(config.whisper.encoder); | 46 | auto buf = ReadFile(config.whisper.encoder); |
| 47 | InitEncoder(buf.data(), buf.size()); | 47 | InitEncoder(buf.data(), buf.size()); |
| @@ -148,7 +148,6 @@ class OfflineWhisperModel::Impl { | @@ -148,7 +148,6 @@ class OfflineWhisperModel::Impl { | ||
| 148 | cross_v = std::move(std::get<4>(decoder_out)); | 148 | cross_v = std::move(std::get<4>(decoder_out)); |
| 149 | 149 | ||
| 150 | const float *p_logits = std::get<0>(decoder_out).GetTensorData<float>(); | 150 | const float *p_logits = std::get<0>(decoder_out).GetTensorData<float>(); |
| 151 | - int32_t vocab_size = VocabSize(); | ||
| 152 | const auto &all_language_ids = GetAllLanguageIDs(); | 151 | const auto &all_language_ids = GetAllLanguageIDs(); |
| 153 | 152 | ||
| 154 | int32_t lang_id = all_language_ids[0]; | 153 | int32_t lang_id = all_language_ids[0]; |
| @@ -317,18 +316,18 @@ class OfflineWhisperModel::Impl { | @@ -317,18 +316,18 @@ class OfflineWhisperModel::Impl { | ||
| 317 | std::unordered_map<int32_t, std::string> id2lang_; | 316 | std::unordered_map<int32_t, std::string> id2lang_; |
| 318 | 317 | ||
| 319 | // model meta data | 318 | // model meta data |
| 320 | - int32_t n_text_layer_; | ||
| 321 | - int32_t n_text_ctx_; | ||
| 322 | - int32_t n_text_state_; | ||
| 323 | - int32_t n_vocab_; | ||
| 324 | - int32_t sot_; | ||
| 325 | - int32_t eot_; | ||
| 326 | - int32_t blank_; | ||
| 327 | - int32_t translate_; | ||
| 328 | - int32_t transcribe_; | ||
| 329 | - int32_t no_timestamps_; | ||
| 330 | - int32_t no_speech_; | ||
| 331 | - int32_t is_multilingual_; | 319 | + int32_t n_text_layer_ = 0; |
| 320 | + int32_t n_text_ctx_ = 0; | ||
| 321 | + int32_t n_text_state_ = 0; | ||
| 322 | + int32_t n_vocab_ = 0; | ||
| 323 | + int32_t sot_ = 0; | ||
| 324 | + int32_t eot_ = 0; | ||
| 325 | + int32_t blank_ = 0; | ||
| 326 | + int32_t translate_ = 0; | ||
| 327 | + int32_t transcribe_ = 0; | ||
| 328 | + int32_t no_timestamps_ = 0; | ||
| 329 | + int32_t no_speech_ = 0; | ||
| 330 | + int32_t is_multilingual_ = 0; | ||
| 332 | std::vector<int64_t> sot_sequence_; | 331 | std::vector<int64_t> sot_sequence_; |
| 333 | }; | 332 | }; |
| 334 | 333 |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/online-conformer-transducer-model.h" | 5 | #include "sherpa-onnx/csrc/online-conformer-transducer-model.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <memory> | 9 | #include <memory> |
| 11 | #include <sstream> | 10 | #include <sstream> |
| 12 | #include <string> | 11 | #include <string> |
| @@ -52,8 +52,9 @@ static void DecodeOne(const float *log_probs, int32_t num_rows, | @@ -52,8 +52,9 @@ static void DecodeOne(const float *log_probs, int32_t num_rows, | ||
| 52 | if (ok) { | 52 | if (ok) { |
| 53 | std::vector<int32_t> isymbols_out; | 53 | std::vector<int32_t> isymbols_out; |
| 54 | std::vector<int32_t> osymbols_out; | 54 | std::vector<int32_t> osymbols_out; |
| 55 | - ok = fst::GetLinearSymbolSequence(fst_out, &isymbols_out, &osymbols_out, | ||
| 56 | - nullptr); | 55 | + /*ok =*/fst::GetLinearSymbolSequence(fst_out, &isymbols_out, |
| 56 | + &osymbols_out, nullptr); | ||
| 57 | + // TODO(fangjun): handle ok is false | ||
| 57 | std::vector<int64_t> tokens; | 58 | std::vector<int64_t> tokens; |
| 58 | tokens.reserve(isymbols_out.size()); | 59 | tokens.reserve(isymbols_out.size()); |
| 59 | 60 |
| @@ -3,9 +3,8 @@ | @@ -3,9 +3,8 @@ | ||
| 3 | // Copyright (c) 2023 Xiaomi Corporation | 3 | // Copyright (c) 2023 Xiaomi Corporation |
| 4 | #include "sherpa-onnx/csrc/online-lstm-transducer-model.h" | 4 | #include "sherpa-onnx/csrc/online-lstm-transducer-model.h" |
| 5 | 5 | ||
| 6 | -#include <assert.h> | ||
| 7 | - | ||
| 8 | #include <algorithm> | 6 | #include <algorithm> |
| 7 | +#include <cassert> | ||
| 9 | #include <memory> | 8 | #include <memory> |
| 10 | #include <sstream> | 9 | #include <sstream> |
| 11 | #include <string> | 10 | #include <string> |
| @@ -265,16 +265,16 @@ class OnlineNeMoCtcModel::Impl { | @@ -265,16 +265,16 @@ class OnlineNeMoCtcModel::Impl { | ||
| 265 | std::vector<std::string> output_names_; | 265 | std::vector<std::string> output_names_; |
| 266 | std::vector<const char *> output_names_ptr_; | 266 | std::vector<const char *> output_names_ptr_; |
| 267 | 267 | ||
| 268 | - int32_t window_size_; | ||
| 269 | - int32_t chunk_shift_; | ||
| 270 | - int32_t subsampling_factor_; | ||
| 271 | - int32_t vocab_size_; | ||
| 272 | - int32_t cache_last_channel_dim1_; | ||
| 273 | - int32_t cache_last_channel_dim2_; | ||
| 274 | - int32_t cache_last_channel_dim3_; | ||
| 275 | - int32_t cache_last_time_dim1_; | ||
| 276 | - int32_t cache_last_time_dim2_; | ||
| 277 | - int32_t cache_last_time_dim3_; | 268 | + int32_t window_size_ = 0; |
| 269 | + int32_t chunk_shift_ = 0; | ||
| 270 | + int32_t subsampling_factor_ = 0; | ||
| 271 | + int32_t vocab_size_ = 0; | ||
| 272 | + int32_t cache_last_channel_dim1_ = 0; | ||
| 273 | + int32_t cache_last_channel_dim2_ = 0; | ||
| 274 | + int32_t cache_last_channel_dim3_ = 0; | ||
| 275 | + int32_t cache_last_time_dim1_ = 0; | ||
| 276 | + int32_t cache_last_time_dim2_ = 0; | ||
| 277 | + int32_t cache_last_time_dim3_ = 0; | ||
| 278 | 278 | ||
| 279 | Ort::Value cache_last_channel_{nullptr}; | 279 | Ort::Value cache_last_channel_{nullptr}; |
| 280 | Ort::Value cache_last_time_{nullptr}; | 280 | Ort::Value cache_last_time_{nullptr}; |
| @@ -5,9 +5,8 @@ | @@ -5,9 +5,8 @@ | ||
| 5 | 5 | ||
| 6 | #include "sherpa-onnx/csrc/online-recognizer.h" | 6 | #include "sherpa-onnx/csrc/online-recognizer.h" |
| 7 | 7 | ||
| 8 | -#include <assert.h> | ||
| 9 | - | ||
| 10 | #include <algorithm> | 8 | #include <algorithm> |
| 9 | +#include <cassert> | ||
| 11 | #include <iomanip> | 10 | #include <iomanip> |
| 12 | #include <memory> | 11 | #include <memory> |
| 13 | #include <sstream> | 12 | #include <sstream> |
| @@ -8,6 +8,7 @@ | @@ -8,6 +8,7 @@ | ||
| 8 | #include <vector> | 8 | #include <vector> |
| 9 | 9 | ||
| 10 | #include "sherpa-onnx/csrc/features.h" | 10 | #include "sherpa-onnx/csrc/features.h" |
| 11 | +#include "sherpa-onnx/csrc/transducer-keyword-decoder.h" | ||
| 11 | 12 | ||
| 12 | namespace sherpa_onnx { | 13 | namespace sherpa_onnx { |
| 13 | 14 | ||
| @@ -15,7 +16,7 @@ class OnlineStream::Impl { | @@ -15,7 +16,7 @@ class OnlineStream::Impl { | ||
| 15 | public: | 16 | public: |
| 16 | explicit Impl(const FeatureExtractorConfig &config, | 17 | explicit Impl(const FeatureExtractorConfig &config, |
| 17 | ContextGraphPtr context_graph) | 18 | ContextGraphPtr context_graph) |
| 18 | - : feat_extractor_(config), context_graph_(context_graph) {} | 19 | + : feat_extractor_(config), context_graph_(std::move(context_graph)) {} |
| 19 | 20 | ||
| 20 | void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) { | 21 | void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) { |
| 21 | feat_extractor_.AcceptWaveform(sampling_rate, waveform, n); | 22 | feat_extractor_.AcceptWaveform(sampling_rate, waveform, n); |
| @@ -146,7 +147,7 @@ class OnlineStream::Impl { | @@ -146,7 +147,7 @@ class OnlineStream::Impl { | ||
| 146 | 147 | ||
| 147 | OnlineStream::OnlineStream(const FeatureExtractorConfig &config /*= {}*/, | 148 | OnlineStream::OnlineStream(const FeatureExtractorConfig &config /*= {}*/, |
| 148 | ContextGraphPtr context_graph /*= nullptr */) | 149 | ContextGraphPtr context_graph /*= nullptr */) |
| 149 | - : impl_(std::make_unique<Impl>(config, context_graph)) {} | 150 | + : impl_(std::make_unique<Impl>(config, std::move(context_graph))) {} |
| 150 | 151 | ||
| 151 | OnlineStream::~OnlineStream() = default; | 152 | OnlineStream::~OnlineStream() = default; |
| 152 | 153 |
| @@ -15,7 +15,6 @@ | @@ -15,7 +15,6 @@ | ||
| 15 | #include "sherpa-onnx/csrc/online-ctc-decoder.h" | 15 | #include "sherpa-onnx/csrc/online-ctc-decoder.h" |
| 16 | #include "sherpa-onnx/csrc/online-paraformer-decoder.h" | 16 | #include "sherpa-onnx/csrc/online-paraformer-decoder.h" |
| 17 | #include "sherpa-onnx/csrc/online-transducer-decoder.h" | 17 | #include "sherpa-onnx/csrc/online-transducer-decoder.h" |
| 18 | -#include "sherpa-onnx/csrc/transducer-keyword-decoder.h" | ||
| 19 | 18 | ||
| 20 | namespace sherpa_onnx { | 19 | namespace sherpa_onnx { |
| 21 | 20 |
| @@ -45,13 +45,13 @@ OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( | @@ -45,13 +45,13 @@ OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( | ||
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | OnlineTransducerDecoderResult::OnlineTransducerDecoderResult( | 47 | OnlineTransducerDecoderResult::OnlineTransducerDecoderResult( |
| 48 | - OnlineTransducerDecoderResult &&other) | 48 | + OnlineTransducerDecoderResult &&other) noexcept |
| 49 | : OnlineTransducerDecoderResult() { | 49 | : OnlineTransducerDecoderResult() { |
| 50 | *this = std::move(other); | 50 | *this = std::move(other); |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( | 53 | OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( |
| 54 | - OnlineTransducerDecoderResult &&other) { | 54 | + OnlineTransducerDecoderResult &&other) noexcept { |
| 55 | if (this == &other) { | 55 | if (this == &other) { |
| 56 | return *this; | 56 | return *this; |
| 57 | } | 57 | } |
| @@ -44,10 +44,10 @@ struct OnlineTransducerDecoderResult { | @@ -44,10 +44,10 @@ struct OnlineTransducerDecoderResult { | ||
| 44 | OnlineTransducerDecoderResult &operator=( | 44 | OnlineTransducerDecoderResult &operator=( |
| 45 | const OnlineTransducerDecoderResult &other); | 45 | const OnlineTransducerDecoderResult &other); |
| 46 | 46 | ||
| 47 | - OnlineTransducerDecoderResult(OnlineTransducerDecoderResult &&other); | 47 | + OnlineTransducerDecoderResult(OnlineTransducerDecoderResult &&other) noexcept; |
| 48 | 48 | ||
| 49 | OnlineTransducerDecoderResult &operator=( | 49 | OnlineTransducerDecoderResult &operator=( |
| 50 | - OnlineTransducerDecoderResult &&other); | 50 | + OnlineTransducerDecoderResult &&other) noexcept; |
| 51 | }; | 51 | }; |
| 52 | 52 | ||
| 53 | class OnlineStream; | 53 | class OnlineStream; |
| @@ -5,10 +5,9 @@ | @@ -5,10 +5,9 @@ | ||
| 5 | 5 | ||
| 6 | #include "sherpa-onnx/csrc/online-transducer-nemo-model.h" | 6 | #include "sherpa-onnx/csrc/online-transducer-nemo-model.h" |
| 7 | 7 | ||
| 8 | -#include <assert.h> | ||
| 9 | -#include <math.h> | ||
| 10 | - | ||
| 11 | #include <algorithm> | 8 | #include <algorithm> |
| 9 | +#include <cassert> | ||
| 10 | +#include <cmath> | ||
| 12 | #include <memory> | 11 | #include <memory> |
| 13 | #include <numeric> | 12 | #include <numeric> |
| 14 | #include <sstream> | 13 | #include <sstream> |
| @@ -429,8 +428,8 @@ class OnlineTransducerNeMoModel::Impl { | @@ -429,8 +428,8 @@ class OnlineTransducerNeMoModel::Impl { | ||
| 429 | std::vector<std::string> joiner_output_names_; | 428 | std::vector<std::string> joiner_output_names_; |
| 430 | std::vector<const char *> joiner_output_names_ptr_; | 429 | std::vector<const char *> joiner_output_names_ptr_; |
| 431 | 430 | ||
| 432 | - int32_t window_size_; | ||
| 433 | - int32_t chunk_shift_; | 431 | + int32_t window_size_ = 0; |
| 432 | + int32_t chunk_shift_ = 0; | ||
| 434 | int32_t vocab_size_ = 0; | 433 | int32_t vocab_size_ = 0; |
| 435 | int32_t subsampling_factor_ = 8; | 434 | int32_t subsampling_factor_ = 8; |
| 436 | std::string normalize_type_; | 435 | std::string normalize_type_; |
| @@ -438,12 +437,12 @@ class OnlineTransducerNeMoModel::Impl { | @@ -438,12 +437,12 @@ class OnlineTransducerNeMoModel::Impl { | ||
| 438 | int32_t pred_hidden_ = -1; | 437 | int32_t pred_hidden_ = -1; |
| 439 | 438 | ||
| 440 | // encoder states | 439 | // encoder states |
| 441 | - int32_t cache_last_channel_dim1_; | ||
| 442 | - int32_t cache_last_channel_dim2_; | ||
| 443 | - int32_t cache_last_channel_dim3_; | ||
| 444 | - int32_t cache_last_time_dim1_; | ||
| 445 | - int32_t cache_last_time_dim2_; | ||
| 446 | - int32_t cache_last_time_dim3_; | 440 | + int32_t cache_last_channel_dim1_ = 0; |
| 441 | + int32_t cache_last_channel_dim2_ = 0; | ||
| 442 | + int32_t cache_last_channel_dim3_ = 0; | ||
| 443 | + int32_t cache_last_time_dim1_ = 0; | ||
| 444 | + int32_t cache_last_time_dim2_ = 0; | ||
| 445 | + int32_t cache_last_time_dim3_ = 0; | ||
| 447 | 446 | ||
| 448 | // init encoder states | 447 | // init encoder states |
| 449 | Ort::Value cache_last_channel_{nullptr}; | 448 | Ort::Value cache_last_channel_{nullptr}; |
| @@ -192,15 +192,15 @@ class OnlineWenetCtcModel::Impl { | @@ -192,15 +192,15 @@ class OnlineWenetCtcModel::Impl { | ||
| 192 | std::vector<std::string> output_names_; | 192 | std::vector<std::string> output_names_; |
| 193 | std::vector<const char *> output_names_ptr_; | 193 | std::vector<const char *> output_names_ptr_; |
| 194 | 194 | ||
| 195 | - int32_t head_; | ||
| 196 | - int32_t num_blocks_; | ||
| 197 | - int32_t output_size_; | ||
| 198 | - int32_t cnn_module_kernel_; | ||
| 199 | - int32_t right_context_; | ||
| 200 | - int32_t subsampling_factor_; | ||
| 201 | - int32_t vocab_size_; | ||
| 202 | - | ||
| 203 | - int32_t required_cache_size_; | 195 | + int32_t head_ = 0; |
| 196 | + int32_t num_blocks_ = 0; | ||
| 197 | + int32_t output_size_ = 0; | ||
| 198 | + int32_t cnn_module_kernel_ = 0; | ||
| 199 | + int32_t right_context_ = 0; | ||
| 200 | + int32_t subsampling_factor_ = 0; | ||
| 201 | + int32_t vocab_size_ = 0; | ||
| 202 | + | ||
| 203 | + int32_t required_cache_size_ = 0; | ||
| 204 | 204 | ||
| 205 | Ort::Value attn_cache_{nullptr}; | 205 | Ort::Value attn_cache_{nullptr}; |
| 206 | Ort::Value conv_cache_{nullptr}; | 206 | Ort::Value conv_cache_{nullptr}; |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/online-zipformer-transducer-model.h" | 5 | #include "sherpa-onnx/csrc/online-zipformer-transducer-model.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <memory> | 9 | #include <memory> |
| 11 | #include <sstream> | 10 | #include <sstream> |
| 12 | #include <string> | 11 | #include <string> |
| @@ -4,10 +4,8 @@ | @@ -4,10 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/online-zipformer2-ctc-model.h" | 5 | #include "sherpa-onnx/csrc/online-zipformer2-ctc-model.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | -#include <math.h> | ||
| 9 | - | ||
| 10 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 11 | #include <cmath> | 9 | #include <cmath> |
| 12 | #include <numeric> | 10 | #include <numeric> |
| 13 | #include <string> | 11 | #include <string> |
| @@ -90,7 +88,6 @@ class OnlineZipformer2CtcModel::Impl { | @@ -90,7 +88,6 @@ class OnlineZipformer2CtcModel::Impl { | ||
| 90 | std::vector<Ort::Value> StackStates( | 88 | std::vector<Ort::Value> StackStates( |
| 91 | std::vector<std::vector<Ort::Value>> states) const { | 89 | std::vector<std::vector<Ort::Value>> states) const { |
| 92 | int32_t batch_size = static_cast<int32_t>(states.size()); | 90 | int32_t batch_size = static_cast<int32_t>(states.size()); |
| 93 | - int32_t num_encoders = static_cast<int32_t>(num_encoder_layers_.size()); | ||
| 94 | 91 | ||
| 95 | std::vector<const Ort::Value *> buf(batch_size); | 92 | std::vector<const Ort::Value *> buf(batch_size); |
| 96 | 93 | ||
| @@ -168,7 +165,6 @@ class OnlineZipformer2CtcModel::Impl { | @@ -168,7 +165,6 @@ class OnlineZipformer2CtcModel::Impl { | ||
| 168 | assert(states.size() == m * 6 + 2); | 165 | assert(states.size() == m * 6 + 2); |
| 169 | 166 | ||
| 170 | int32_t batch_size = states[0].GetTensorTypeAndShapeInfo().GetShape()[1]; | 167 | int32_t batch_size = states[0].GetTensorTypeAndShapeInfo().GetShape()[1]; |
| 171 | - int32_t num_encoders = num_encoder_layers_.size(); | ||
| 172 | 168 | ||
| 173 | std::vector<std::vector<Ort::Value>> ans; | 169 | std::vector<std::vector<Ort::Value>> ans; |
| 174 | ans.resize(batch_size); | 170 | ans.resize(batch_size); |
| @@ -4,10 +4,9 @@ | @@ -4,10 +4,9 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/online-zipformer2-transducer-model.h" | 5 | #include "sherpa-onnx/csrc/online-zipformer2-transducer-model.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | -#include <math.h> | ||
| 9 | - | ||
| 10 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 9 | +#include <cmath> | ||
| 11 | #include <memory> | 10 | #include <memory> |
| 12 | #include <numeric> | 11 | #include <numeric> |
| 13 | #include <sstream> | 12 | #include <sstream> |
| @@ -281,11 +281,12 @@ CopyableOrtValue &CopyableOrtValue::operator=(const CopyableOrtValue &other) { | @@ -281,11 +281,12 @@ CopyableOrtValue &CopyableOrtValue::operator=(const CopyableOrtValue &other) { | ||
| 281 | return *this; | 281 | return *this; |
| 282 | } | 282 | } |
| 283 | 283 | ||
| 284 | -CopyableOrtValue::CopyableOrtValue(CopyableOrtValue &&other) { | 284 | +CopyableOrtValue::CopyableOrtValue(CopyableOrtValue &&other) noexcept { |
| 285 | *this = std::move(other); | 285 | *this = std::move(other); |
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | -CopyableOrtValue &CopyableOrtValue::operator=(CopyableOrtValue &&other) { | 288 | +CopyableOrtValue &CopyableOrtValue::operator=( |
| 289 | + CopyableOrtValue &&other) noexcept { | ||
| 289 | if (this == &other) { | 290 | if (this == &other) { |
| 290 | return *this; | 291 | return *this; |
| 291 | } | 292 | } |
| @@ -110,9 +110,9 @@ struct CopyableOrtValue { | @@ -110,9 +110,9 @@ struct CopyableOrtValue { | ||
| 110 | 110 | ||
| 111 | CopyableOrtValue &operator=(const CopyableOrtValue &other); | 111 | CopyableOrtValue &operator=(const CopyableOrtValue &other); |
| 112 | 112 | ||
| 113 | - CopyableOrtValue(CopyableOrtValue &&other); | 113 | + CopyableOrtValue(CopyableOrtValue &&other) noexcept; |
| 114 | 114 | ||
| 115 | - CopyableOrtValue &operator=(CopyableOrtValue &&other); | 115 | + CopyableOrtValue &operator=(CopyableOrtValue &&other) noexcept; |
| 116 | }; | 116 | }; |
| 117 | 117 | ||
| 118 | std::vector<CopyableOrtValue> Convert(std::vector<Ort::Value> values); | 118 | std::vector<CopyableOrtValue> Convert(std::vector<Ort::Value> values); |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/packed-sequence.h" | 5 | #include "sherpa-onnx/csrc/packed-sequence.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <numeric> | 9 | #include <numeric> |
| 11 | #include <utility> | 10 | #include <utility> |
| 12 | 11 | ||
| @@ -57,7 +56,7 @@ PackedSequence PackPaddedSequence(OrtAllocator *allocator, | @@ -57,7 +56,7 @@ PackedSequence PackPaddedSequence(OrtAllocator *allocator, | ||
| 57 | 56 | ||
| 58 | int64_t max_T = p_length[indexes[0]]; | 57 | int64_t max_T = p_length[indexes[0]]; |
| 59 | 58 | ||
| 60 | - int32_t sum_T = std::accumulate(p_length, p_length + n, 0); | 59 | + auto sum_T = std::accumulate(p_length, p_length + n, static_cast<int64_t>(0)); |
| 61 | 60 | ||
| 62 | std::array<int64_t, 2> data_shape{sum_T, v_shape[2]}; | 61 | std::array<int64_t, 2> data_shape{sum_T, v_shape[2]}; |
| 63 | 62 |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/pad-sequence.h" | 5 | #include "sherpa-onnx/csrc/pad-sequence.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <vector> | 9 | #include <vector> |
| 11 | 10 | ||
| 12 | namespace sherpa_onnx { | 11 | namespace sherpa_onnx { |
| @@ -11,9 +11,8 @@ | @@ -11,9 +11,8 @@ | ||
| 11 | 11 | ||
| 12 | #include "sherpa-onnx/csrc/parse-options.h" | 12 | #include "sherpa-onnx/csrc/parse-options.h" |
| 13 | 13 | ||
| 14 | -#include <ctype.h> | ||
| 15 | - | ||
| 16 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | +#include <array> | ||
| 17 | #include <cctype> | 16 | #include <cctype> |
| 18 | #include <cstring> | 17 | #include <cstring> |
| 19 | #include <fstream> | 18 | #include <fstream> |
| @@ -33,7 +32,7 @@ ParseOptions::ParseOptions(const std::string &prefix, ParseOptions *po) | @@ -33,7 +32,7 @@ ParseOptions::ParseOptions(const std::string &prefix, ParseOptions *po) | ||
| 33 | } else { | 32 | } else { |
| 34 | other_parser_ = po; | 33 | other_parser_ = po; |
| 35 | } | 34 | } |
| 36 | - if (po != nullptr && po->prefix_ != "") { | 35 | + if (po != nullptr && !po->prefix_.empty()) { |
| 37 | prefix_ = po->prefix_ + std::string(".") + prefix; | 36 | prefix_ = po->prefix_ + std::string(".") + prefix; |
| 38 | } else { | 37 | } else { |
| 39 | prefix_ = prefix; | 38 | prefix_ = prefix; |
| @@ -179,10 +178,10 @@ void ParseOptions::DisableOption(const std::string &name) { | @@ -179,10 +178,10 @@ void ParseOptions::DisableOption(const std::string &name) { | ||
| 179 | string_map_.erase(name); | 178 | string_map_.erase(name); |
| 180 | } | 179 | } |
| 181 | 180 | ||
| 182 | -int ParseOptions::NumArgs() const { return positional_args_.size(); } | 181 | +int32_t ParseOptions::NumArgs() const { return positional_args_.size(); } |
| 183 | 182 | ||
| 184 | -std::string ParseOptions::GetArg(int i) const { | ||
| 185 | - if (i < 1 || i > static_cast<int>(positional_args_.size())) { | 183 | +std::string ParseOptions::GetArg(int32_t i) const { |
| 184 | + if (i < 1 || i > static_cast<int32_t>(positional_args_.size())) { | ||
| 186 | SHERPA_ONNX_LOGE("ParseOptions::GetArg, invalid index %d", i); | 185 | SHERPA_ONNX_LOGE("ParseOptions::GetArg, invalid index %d", i); |
| 187 | exit(-1); | 186 | exit(-1); |
| 188 | } | 187 | } |
| @@ -191,7 +190,7 @@ std::string ParseOptions::GetArg(int i) const { | @@ -191,7 +190,7 @@ std::string ParseOptions::GetArg(int i) const { | ||
| 191 | } | 190 | } |
| 192 | 191 | ||
| 193 | // We currently do not support any other options. | 192 | // We currently do not support any other options. |
| 194 | -enum ShellType { kBash = 0 }; | 193 | +enum ShellType : std::uint8_t { kBash = 0 }; |
| 195 | 194 | ||
| 196 | // This can be changed in the code if it ever does need to be changed (as it's | 195 | // This can be changed in the code if it ever does need to be changed (as it's |
| 197 | // unlikely that one compilation of this tool-set would use both shells). | 196 | // unlikely that one compilation of this tool-set would use both shells). |
| @@ -213,7 +212,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { | @@ -213,7 +212,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { | ||
| 213 | if (*c == '\0') { | 212 | if (*c == '\0') { |
| 214 | return true; // Must quote empty string | 213 | return true; // Must quote empty string |
| 215 | } else { | 214 | } else { |
| 216 | - const char *ok_chars[2]; | 215 | + std::array<const char *, 2> ok_chars{}; |
| 217 | 216 | ||
| 218 | // These seem not to be interpreted as long as there are no other "bad" | 217 | // These seem not to be interpreted as long as there are no other "bad" |
| 219 | // characters involved (e.g. "," would be interpreted as part of something | 218 | // characters involved (e.g. "," would be interpreted as part of something |
| @@ -229,7 +228,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { | @@ -229,7 +228,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { | ||
| 229 | // are OK. All others are forbidden (this is easier since the shell | 228 | // are OK. All others are forbidden (this is easier since the shell |
| 230 | // interprets most non-alphanumeric characters). | 229 | // interprets most non-alphanumeric characters). |
| 231 | if (!isalnum(*c)) { | 230 | if (!isalnum(*c)) { |
| 232 | - const char *d; | 231 | + const char *d = nullptr; |
| 233 | for (d = ok_chars[st]; *d != '\0'; ++d) { | 232 | for (d = ok_chars[st]; *d != '\0'; ++d) { |
| 234 | if (*c == *d) break; | 233 | if (*c == *d) break; |
| 235 | } | 234 | } |
| @@ -269,22 +268,22 @@ static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) { | @@ -269,22 +268,22 @@ static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) { | ||
| 269 | escape_str = "\\\""; // should never be accessed. | 268 | escape_str = "\\\""; // should never be accessed. |
| 270 | } | 269 | } |
| 271 | 270 | ||
| 272 | - char buf[2]; | 271 | + std::array<char, 2> buf{}; |
| 273 | buf[1] = '\0'; | 272 | buf[1] = '\0'; |
| 274 | 273 | ||
| 275 | buf[0] = quote_char; | 274 | buf[0] = quote_char; |
| 276 | - std::string ans = buf; | 275 | + std::string ans = buf.data(); |
| 277 | const char *c = str.c_str(); | 276 | const char *c = str.c_str(); |
| 278 | for (; *c != '\0'; ++c) { | 277 | for (; *c != '\0'; ++c) { |
| 279 | if (*c == quote_char) { | 278 | if (*c == quote_char) { |
| 280 | ans += escape_str; | 279 | ans += escape_str; |
| 281 | } else { | 280 | } else { |
| 282 | buf[0] = *c; | 281 | buf[0] = *c; |
| 283 | - ans += buf; | 282 | + ans += buf.data(); |
| 284 | } | 283 | } |
| 285 | } | 284 | } |
| 286 | buf[0] = quote_char; | 285 | buf[0] = quote_char; |
| 287 | - ans += buf; | 286 | + ans += buf.data(); |
| 288 | return ans; | 287 | return ans; |
| 289 | } | 288 | } |
| 290 | 289 | ||
| @@ -293,11 +292,11 @@ std::string ParseOptions::Escape(const std::string &str) { | @@ -293,11 +292,11 @@ std::string ParseOptions::Escape(const std::string &str) { | ||
| 293 | return MustBeQuoted(str, kShellType) ? QuoteAndEscape(str, kShellType) : str; | 292 | return MustBeQuoted(str, kShellType) ? QuoteAndEscape(str, kShellType) : str; |
| 294 | } | 293 | } |
| 295 | 294 | ||
| 296 | -int ParseOptions::Read(int argc, const char *const argv[]) { | 295 | +int32_t ParseOptions::Read(int32_t argc, const char *const *argv) { |
| 297 | argc_ = argc; | 296 | argc_ = argc; |
| 298 | argv_ = argv; | 297 | argv_ = argv; |
| 299 | std::string key, value; | 298 | std::string key, value; |
| 300 | - int i; | 299 | + int32_t i = 0; |
| 301 | 300 | ||
| 302 | // first pass: look for config parameter, look for priority | 301 | // first pass: look for config parameter, look for priority |
| 303 | for (i = 1; i < argc; ++i) { | 302 | for (i = 1; i < argc; ++i) { |
| @@ -306,13 +305,13 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | @@ -306,13 +305,13 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | ||
| 306 | // a lone "--" marks the end of named options | 305 | // a lone "--" marks the end of named options |
| 307 | break; | 306 | break; |
| 308 | } | 307 | } |
| 309 | - bool has_equal_sign; | 308 | + bool has_equal_sign = false; |
| 310 | SplitLongArg(argv[i], &key, &value, &has_equal_sign); | 309 | SplitLongArg(argv[i], &key, &value, &has_equal_sign); |
| 311 | NormalizeArgName(&key); | 310 | NormalizeArgName(&key); |
| 312 | Trim(&value); | 311 | Trim(&value); |
| 313 | - if (key.compare("config") == 0) { | 312 | + if (key == "config") { |
| 314 | ReadConfigFile(value); | 313 | ReadConfigFile(value); |
| 315 | - } else if (key.compare("help") == 0) { | 314 | + } else if (key == "help") { |
| 316 | PrintUsage(); | 315 | PrintUsage(); |
| 317 | exit(0); | 316 | exit(0); |
| 318 | } | 317 | } |
| @@ -330,7 +329,7 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | @@ -330,7 +329,7 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | ||
| 330 | double_dash_seen = true; | 329 | double_dash_seen = true; |
| 331 | break; | 330 | break; |
| 332 | } | 331 | } |
| 333 | - bool has_equal_sign; | 332 | + bool has_equal_sign = false; |
| 334 | SplitLongArg(argv[i], &key, &value, &has_equal_sign); | 333 | SplitLongArg(argv[i], &key, &value, &has_equal_sign); |
| 335 | NormalizeArgName(&key); | 334 | NormalizeArgName(&key); |
| 336 | Trim(&value); | 335 | Trim(&value); |
| @@ -349,14 +348,14 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | @@ -349,14 +348,14 @@ int ParseOptions::Read(int argc, const char *const argv[]) { | ||
| 349 | if ((std::strcmp(argv[i], "--") == 0) && !double_dash_seen) { | 348 | if ((std::strcmp(argv[i], "--") == 0) && !double_dash_seen) { |
| 350 | double_dash_seen = true; | 349 | double_dash_seen = true; |
| 351 | } else { | 350 | } else { |
| 352 | - positional_args_.push_back(std::string(argv[i])); | 351 | + positional_args_.emplace_back(argv[i]); |
| 353 | } | 352 | } |
| 354 | } | 353 | } |
| 355 | 354 | ||
| 356 | // if the user did not suppress this with --print-args = false.... | 355 | // if the user did not suppress this with --print-args = false.... |
| 357 | if (print_args_) { | 356 | if (print_args_) { |
| 358 | std::ostringstream strm; | 357 | std::ostringstream strm; |
| 359 | - for (int j = 0; j < argc; ++j) strm << Escape(argv[j]) << " "; | 358 | + for (int32_t j = 0; j < argc; ++j) strm << Escape(argv[j]) << " "; |
| 360 | strm << '\n'; | 359 | strm << '\n'; |
| 361 | SHERPA_ONNX_LOGE("%s", strm.str().c_str()); | 360 | SHERPA_ONNX_LOGE("%s", strm.str().c_str()); |
| 362 | } | 361 | } |
| @@ -368,14 +367,14 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | @@ -368,14 +367,14 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | ||
| 368 | os << '\n' << usage_ << '\n'; | 367 | os << '\n' << usage_ << '\n'; |
| 369 | // first we print application-specific options | 368 | // first we print application-specific options |
| 370 | bool app_specific_header_printed = false; | 369 | bool app_specific_header_printed = false; |
| 371 | - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { | ||
| 372 | - if (it->second.is_standard_ == false) { // application-specific option | 370 | + for (const auto &it : doc_map_) { |
| 371 | + if (it.second.is_standard_ == false) { // application-specific option | ||
| 373 | if (app_specific_header_printed == false) { // header was not yet printed | 372 | if (app_specific_header_printed == false) { // header was not yet printed |
| 374 | os << "Options:" << '\n'; | 373 | os << "Options:" << '\n'; |
| 375 | app_specific_header_printed = true; | 374 | app_specific_header_printed = true; |
| 376 | } | 375 | } |
| 377 | - os << " --" << std::setw(25) << std::left << it->second.name_ << " : " | ||
| 378 | - << it->second.use_msg_ << '\n'; | 376 | + os << " --" << std::setw(25) << std::left << it.second.name_ << " : " |
| 377 | + << it.second.use_msg_ << '\n'; | ||
| 379 | } | 378 | } |
| 380 | } | 379 | } |
| 381 | if (app_specific_header_printed == true) { | 380 | if (app_specific_header_printed == true) { |
| @@ -384,17 +383,17 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | @@ -384,17 +383,17 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | ||
| 384 | 383 | ||
| 385 | // then the standard options | 384 | // then the standard options |
| 386 | os << "Standard options:" << '\n'; | 385 | os << "Standard options:" << '\n'; |
| 387 | - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { | ||
| 388 | - if (it->second.is_standard_ == true) { // we have standard option | ||
| 389 | - os << " --" << std::setw(25) << std::left << it->second.name_ << " : " | ||
| 390 | - << it->second.use_msg_ << '\n'; | 386 | + for (const auto &it : doc_map_) { |
| 387 | + if (it.second.is_standard_ == true) { // we have standard option | ||
| 388 | + os << " --" << std::setw(25) << std::left << it.second.name_ << " : " | ||
| 389 | + << it.second.use_msg_ << '\n'; | ||
| 391 | } | 390 | } |
| 392 | } | 391 | } |
| 393 | os << '\n'; | 392 | os << '\n'; |
| 394 | if (print_command_line) { | 393 | if (print_command_line) { |
| 395 | std::ostringstream strm; | 394 | std::ostringstream strm; |
| 396 | strm << "Command line was: "; | 395 | strm << "Command line was: "; |
| 397 | - for (int j = 0; j < argc_; ++j) strm << Escape(argv_[j]) << " "; | 396 | + for (int32_t j = 0; j < argc_; ++j) strm << Escape(argv_[j]) << " "; |
| 398 | strm << '\n'; | 397 | strm << '\n'; |
| 399 | os << strm.str(); | 398 | os << strm.str(); |
| 400 | } | 399 | } |
| @@ -405,9 +404,9 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | @@ -405,9 +404,9 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { | ||
| 405 | void ParseOptions::PrintConfig(std::ostream &os) const { | 404 | void ParseOptions::PrintConfig(std::ostream &os) const { |
| 406 | os << '\n' << "[[ Configuration of UI-Registered options ]]" << '\n'; | 405 | os << '\n' << "[[ Configuration of UI-Registered options ]]" << '\n'; |
| 407 | std::string key; | 406 | std::string key; |
| 408 | - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { | ||
| 409 | - key = it->first; | ||
| 410 | - os << it->second.name_ << " = "; | 407 | + for (const auto &it : doc_map_) { |
| 408 | + key = it.first; | ||
| 409 | + os << it.second.name_ << " = "; | ||
| 411 | if (bool_map_.end() != bool_map_.find(key)) { | 410 | if (bool_map_.end() != bool_map_.find(key)) { |
| 412 | os << (*bool_map_.at(key) ? "true" : "false"); | 411 | os << (*bool_map_.at(key) ? "true" : "false"); |
| 413 | } else if (int_map_.end() != int_map_.find(key)) { | 412 | } else if (int_map_.end() != int_map_.find(key)) { |
| @@ -442,13 +441,13 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { | @@ -442,13 +441,13 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { | ||
| 442 | while (std::getline(is, line)) { | 441 | while (std::getline(is, line)) { |
| 443 | ++line_number; | 442 | ++line_number; |
| 444 | // trim out the comments | 443 | // trim out the comments |
| 445 | - size_t pos; | ||
| 446 | - if ((pos = line.find_first_of('#')) != std::string::npos) { | 444 | + size_t pos = line.find_first_of('#'); |
| 445 | + if (pos != std::string::npos) { | ||
| 447 | line.erase(pos); | 446 | line.erase(pos); |
| 448 | } | 447 | } |
| 449 | // skip empty lines | 448 | // skip empty lines |
| 450 | Trim(&line); | 449 | Trim(&line); |
| 451 | - if (line.length() == 0) continue; | 450 | + if (line.empty()) continue; |
| 452 | 451 | ||
| 453 | if (line.substr(0, 2) != "--") { | 452 | if (line.substr(0, 2) != "--") { |
| 454 | SHERPA_ONNX_LOGE( | 453 | SHERPA_ONNX_LOGE( |
| @@ -461,7 +460,7 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { | @@ -461,7 +460,7 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { | ||
| 461 | } | 460 | } |
| 462 | 461 | ||
| 463 | // parse option | 462 | // parse option |
| 464 | - bool has_equal_sign; | 463 | + bool has_equal_sign = false; |
| 465 | SplitLongArg(line, &key, &value, &has_equal_sign); | 464 | SplitLongArg(line, &key, &value, &has_equal_sign); |
| 466 | NormalizeArgName(&key); | 465 | NormalizeArgName(&key); |
| 467 | Trim(&value); | 466 | Trim(&value); |
| @@ -527,7 +526,7 @@ void ParseOptions::Trim(std::string *str) const { | @@ -527,7 +526,7 @@ void ParseOptions::Trim(std::string *str) const { | ||
| 527 | bool ParseOptions::SetOption(const std::string &key, const std::string &value, | 526 | bool ParseOptions::SetOption(const std::string &key, const std::string &value, |
| 528 | bool has_equal_sign) { | 527 | bool has_equal_sign) { |
| 529 | if (bool_map_.end() != bool_map_.find(key)) { | 528 | if (bool_map_.end() != bool_map_.find(key)) { |
| 530 | - if (has_equal_sign && value == "") { | 529 | + if (has_equal_sign && value.empty()) { |
| 531 | SHERPA_ONNX_LOGE("Invalid option --%s=", key.c_str()); | 530 | SHERPA_ONNX_LOGE("Invalid option --%s=", key.c_str()); |
| 532 | exit(-1); | 531 | exit(-1); |
| 533 | } | 532 | } |
| @@ -557,12 +556,10 @@ bool ParseOptions::ToBool(std::string str) const { | @@ -557,12 +556,10 @@ bool ParseOptions::ToBool(std::string str) const { | ||
| 557 | std::transform(str.begin(), str.end(), str.begin(), ::tolower); | 556 | std::transform(str.begin(), str.end(), str.begin(), ::tolower); |
| 558 | 557 | ||
| 559 | // allow "" as a valid option for "true", so that --x is the same as --x=true | 558 | // allow "" as a valid option for "true", so that --x is the same as --x=true |
| 560 | - if ((str.compare("true") == 0) || (str.compare("t") == 0) || | ||
| 561 | - (str.compare("1") == 0) || (str.compare("") == 0)) { | 559 | + if (str == "true" || str == "t" || str == "1" || str.empty()) { |
| 562 | return true; | 560 | return true; |
| 563 | } | 561 | } |
| 564 | - if ((str.compare("false") == 0) || (str.compare("f") == 0) || | ||
| 565 | - (str.compare("0") == 0)) { | 562 | + if (str == "false" || str == "f" || str == "0") { |
| 566 | return false; | 563 | return false; |
| 567 | } | 564 | } |
| 568 | // if it is neither true nor false: | 565 | // if it is neither true nor false: |
| @@ -593,7 +590,7 @@ uint32_t ParseOptions::ToUint(const std::string &str) const { | @@ -593,7 +590,7 @@ uint32_t ParseOptions::ToUint(const std::string &str) const { | ||
| 593 | } | 590 | } |
| 594 | 591 | ||
| 595 | float ParseOptions::ToFloat(const std::string &str) const { | 592 | float ParseOptions::ToFloat(const std::string &str) const { |
| 596 | - float ret; | 593 | + float ret = 0; |
| 597 | if (!ConvertStringToReal(str, &ret)) { | 594 | if (!ConvertStringToReal(str, &ret)) { |
| 598 | SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); | 595 | SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); |
| 599 | exit(-1); | 596 | exit(-1); |
| @@ -602,7 +599,7 @@ float ParseOptions::ToFloat(const std::string &str) const { | @@ -602,7 +599,7 @@ float ParseOptions::ToFloat(const std::string &str) const { | ||
| 602 | } | 599 | } |
| 603 | 600 | ||
| 604 | double ParseOptions::ToDouble(const std::string &str) const { | 601 | double ParseOptions::ToDouble(const std::string &str) const { |
| 605 | - double ret; | 602 | + double ret = 0; |
| 606 | if (!ConvertStringToReal(str, &ret)) { | 603 | if (!ConvertStringToReal(str, &ret)) { |
| 607 | SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); | 604 | SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); |
| 608 | exit(-1); | 605 | exit(-1); |
| @@ -37,7 +37,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) { | @@ -37,7 +37,7 @@ static std::unordered_map<char32_t, int32_t> ReadTokens(std::istream &is) { | ||
| 37 | 37 | ||
| 38 | std::string sym; | 38 | std::string sym; |
| 39 | std::u32string s; | 39 | std::u32string s; |
| 40 | - int32_t id; | 40 | + int32_t id = 0; |
| 41 | while (std::getline(is, line)) { | 41 | while (std::getline(is, line)) { |
| 42 | std::istringstream iss(line); | 42 | std::istringstream iss(line); |
| 43 | iss >> sym; | 43 | iss >> sym; |
| @@ -24,10 +24,9 @@ | @@ -24,10 +24,9 @@ | ||
| 24 | 24 | ||
| 25 | #include "sherpa-onnx/csrc/resample.h" | 25 | #include "sherpa-onnx/csrc/resample.h" |
| 26 | 26 | ||
| 27 | -#include <assert.h> | ||
| 28 | -#include <math.h> | ||
| 29 | -#include <stdio.h> | ||
| 30 | - | 27 | +#include <cassert> |
| 28 | +#include <cmath> | ||
| 29 | +#include <cstdio> | ||
| 31 | #include <cstdlib> | 30 | #include <cstdlib> |
| 32 | #include <type_traits> | 31 | #include <type_traits> |
| 33 | 32 | ||
| @@ -54,8 +53,8 @@ I Gcd(I m, I n) { | @@ -54,8 +53,8 @@ I Gcd(I m, I n) { | ||
| 54 | } | 53 | } |
| 55 | // could use compile-time assertion | 54 | // could use compile-time assertion |
| 56 | // but involves messing with complex template stuff. | 55 | // but involves messing with complex template stuff. |
| 57 | - static_assert(std::is_integral<I>::value, ""); | ||
| 58 | - while (1) { | 56 | + static_assert(std::is_integral_v<I>); |
| 57 | + while (true) { | ||
| 59 | m %= n; | 58 | m %= n; |
| 60 | if (m == 0) return (n > 0 ? n : -n); | 59 | if (m == 0) return (n > 0 ? n : -n); |
| 61 | n %= m; | 60 | n %= m; |
| @@ -139,10 +138,10 @@ void LinearResample::SetIndexesAndWeights() { | @@ -139,10 +138,10 @@ void LinearResample::SetIndexesAndWeights() { | ||
| 139 | in the header as h(t) = f(t)g(t), evaluated at t. | 138 | in the header as h(t) = f(t)g(t), evaluated at t. |
| 140 | */ | 139 | */ |
| 141 | float LinearResample::FilterFunc(float t) const { | 140 | float LinearResample::FilterFunc(float t) const { |
| 142 | - float window, // raised-cosine (Hanning) window of width | 141 | + float window = 0, // raised-cosine (Hanning) window of width |
| 143 | // num_zeros_/2*filter_cutoff_ | 142 | // num_zeros_/2*filter_cutoff_ |
| 144 | - filter; // sinc filter function | ||
| 145 | - if (fabs(t) < num_zeros_ / (2.0 * filter_cutoff_)) | 143 | + filter = 0; // sinc filter function |
| 144 | + if (std::fabs(t) < num_zeros_ / (2.0 * filter_cutoff_)) | ||
| 146 | window = 0.5 * (1 + cos(M_2PI * filter_cutoff_ / num_zeros_ * t)); | 145 | window = 0.5 * (1 + cos(M_2PI * filter_cutoff_ / num_zeros_ * t)); |
| 147 | else | 146 | else |
| 148 | window = 0.0; // outside support of window function | 147 | window = 0.0; // outside support of window function |
| @@ -172,15 +171,15 @@ void LinearResample::Resample(const float *input, int32_t input_dim, bool flush, | @@ -172,15 +171,15 @@ void LinearResample::Resample(const float *input, int32_t input_dim, bool flush, | ||
| 172 | // of it we are producing here. | 171 | // of it we are producing here. |
| 173 | for (int64_t samp_out = output_sample_offset_; samp_out < tot_output_samp; | 172 | for (int64_t samp_out = output_sample_offset_; samp_out < tot_output_samp; |
| 174 | samp_out++) { | 173 | samp_out++) { |
| 175 | - int64_t first_samp_in; | ||
| 176 | - int32_t samp_out_wrapped; | 174 | + int64_t first_samp_in = 0; |
| 175 | + int32_t samp_out_wrapped = 0; | ||
| 177 | GetIndexes(samp_out, &first_samp_in, &samp_out_wrapped); | 176 | GetIndexes(samp_out, &first_samp_in, &samp_out_wrapped); |
| 178 | const std::vector<float> &weights = weights_[samp_out_wrapped]; | 177 | const std::vector<float> &weights = weights_[samp_out_wrapped]; |
| 179 | // first_input_index is the first index into "input" that we have a weight | 178 | // first_input_index is the first index into "input" that we have a weight |
| 180 | // for. | 179 | // for. |
| 181 | int32_t first_input_index = | 180 | int32_t first_input_index = |
| 182 | static_cast<int32_t>(first_samp_in - input_sample_offset_); | 181 | static_cast<int32_t>(first_samp_in - input_sample_offset_); |
| 183 | - float this_output; | 182 | + float this_output = 0; |
| 184 | if (first_input_index >= 0 && | 183 | if (first_input_index >= 0 && |
| 185 | first_input_index + static_cast<int32_t>(weights.size()) <= input_dim) { | 184 | first_input_index + static_cast<int32_t>(weights.size()) <= input_dim) { |
| 186 | this_output = | 185 | this_output = |
| @@ -239,7 +238,7 @@ int64_t LinearResample::GetNumOutputSamples(int64_t input_num_samp, | @@ -239,7 +238,7 @@ int64_t LinearResample::GetNumOutputSamples(int64_t input_num_samp, | ||
| 239 | // largest integer in the interval [ 0, 2 - 0.9 ) are the same (both one). | 238 | // largest integer in the interval [ 0, 2 - 0.9 ) are the same (both one). |
| 240 | // So when we're subtracting the window-width we can ignore the fractional | 239 | // So when we're subtracting the window-width we can ignore the fractional |
| 241 | // part. | 240 | // part. |
| 242 | - int32_t window_width_ticks = floor(window_width * tick_freq); | 241 | + int32_t window_width_ticks = std::floor(window_width * tick_freq); |
| 243 | // The time-period of the output that we can sample gets reduced | 242 | // The time-period of the output that we can sample gets reduced |
| 244 | // by the window-width (which is actually the distance from the | 243 | // by the window-width (which is actually the distance from the |
| 245 | // center to the edge of the windowing function) if we're not | 244 | // center to the edge of the windowing function) if we're not |
| @@ -287,7 +286,7 @@ void LinearResample::SetRemainder(const float *input, int32_t input_dim) { | @@ -287,7 +286,7 @@ void LinearResample::SetRemainder(const float *input, int32_t input_dim) { | ||
| 287 | // that are "in the past" relative to the beginning of the latest | 286 | // that are "in the past" relative to the beginning of the latest |
| 288 | // input... anyway, storing more remainder than needed is not harmful. | 287 | // input... anyway, storing more remainder than needed is not harmful. |
| 289 | int32_t max_remainder_needed = | 288 | int32_t max_remainder_needed = |
| 290 | - ceil(samp_rate_in_ * num_zeros_ / filter_cutoff_); | 289 | + std::ceil(samp_rate_in_ * num_zeros_ / filter_cutoff_); |
| 291 | input_remainder_.resize(max_remainder_needed); | 290 | input_remainder_.resize(max_remainder_needed); |
| 292 | for (int32_t index = -static_cast<int32_t>(input_remainder_.size()); | 291 | for (int32_t index = -static_cast<int32_t>(input_remainder_.size()); |
| 293 | index < 0; index++) { | 292 | index < 0; index++) { |
| @@ -130,10 +130,10 @@ class LinearResample { | @@ -130,10 +130,10 @@ class LinearResample { | ||
| 130 | // the following variables keep track of where we are in a particular signal, | 130 | // the following variables keep track of where we are in a particular signal, |
| 131 | // if it is being provided over multiple calls to Resample(). | 131 | // if it is being provided over multiple calls to Resample(). |
| 132 | 132 | ||
| 133 | - int64_t input_sample_offset_; ///< The number of input samples we have | 133 | + int64_t input_sample_offset_ = 0; ///< The number of input samples we have |
| 134 | ///< already received for this signal | 134 | ///< already received for this signal |
| 135 | ///< (including anything in remainder_) | 135 | ///< (including anything in remainder_) |
| 136 | - int64_t output_sample_offset_; ///< The number of samples we have already | 136 | + int64_t output_sample_offset_ = 0; ///< The number of samples we have already |
| 137 | ///< output for this signal. | 137 | ///< output for this signal. |
| 138 | std::vector<float> input_remainder_; ///< A small trailing part of the | 138 | std::vector<float> input_remainder_; ///< A small trailing part of the |
| 139 | ///< previously seen input signal. | 139 | ///< previously seen input signal. |
| @@ -21,13 +21,13 @@ | @@ -21,13 +21,13 @@ | ||
| 21 | 21 | ||
| 22 | namespace sherpa_onnx { | 22 | namespace sherpa_onnx { |
| 23 | 23 | ||
| 24 | - | ||
| 25 | static void OrtStatusFailure(OrtStatus *status, const char *s) { | 24 | static void OrtStatusFailure(OrtStatus *status, const char *s) { |
| 26 | const auto &api = Ort::GetApi(); | 25 | const auto &api = Ort::GetApi(); |
| 27 | const char *msg = api.GetErrorMessage(status); | 26 | const char *msg = api.GetErrorMessage(status); |
| 28 | SHERPA_ONNX_LOGE( | 27 | SHERPA_ONNX_LOGE( |
| 29 | "Failed to enable TensorRT : %s." | 28 | "Failed to enable TensorRT : %s." |
| 30 | - "Available providers: %s. Fallback to cuda", msg, s); | 29 | + "Available providers: %s. Fallback to cuda", |
| 30 | + msg, s); | ||
| 31 | api.ReleaseStatus(status); | 31 | api.ReleaseStatus(status); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| @@ -65,8 +65,8 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | @@ -65,8 +65,8 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | ||
| 65 | } | 65 | } |
| 66 | case Provider::kTRT: { | 66 | case Provider::kTRT: { |
| 67 | struct TrtPairs { | 67 | struct TrtPairs { |
| 68 | - const char* op_keys; | ||
| 69 | - const char* op_values; | 68 | + const char *op_keys; |
| 69 | + const char *op_values; | ||
| 70 | }; | 70 | }; |
| 71 | 71 | ||
| 72 | std::vector<TrtPairs> trt_options = { | 72 | std::vector<TrtPairs> trt_options = { |
| @@ -79,15 +79,14 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | @@ -79,15 +79,14 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | ||
| 79 | {"trt_engine_cache_enable", "1"}, | 79 | {"trt_engine_cache_enable", "1"}, |
| 80 | {"trt_engine_cache_path", "."}, | 80 | {"trt_engine_cache_path", "."}, |
| 81 | {"trt_timing_cache_enable", "1"}, | 81 | {"trt_timing_cache_enable", "1"}, |
| 82 | - {"trt_timing_cache_path", "."} | ||
| 83 | - }; | 82 | + {"trt_timing_cache_path", "."}}; |
| 84 | // ToDo : Trt configs | 83 | // ToDo : Trt configs |
| 85 | // "trt_int8_enable" | 84 | // "trt_int8_enable" |
| 86 | // "trt_int8_use_native_calibration_table" | 85 | // "trt_int8_use_native_calibration_table" |
| 87 | // "trt_dump_subgraphs" | 86 | // "trt_dump_subgraphs" |
| 88 | 87 | ||
| 89 | - std::vector<const char*> option_keys, option_values; | ||
| 90 | - for (const TrtPairs& pair : trt_options) { | 88 | + std::vector<const char *> option_keys, option_values; |
| 89 | + for (const TrtPairs &pair : trt_options) { | ||
| 91 | option_keys.emplace_back(pair.op_keys); | 90 | option_keys.emplace_back(pair.op_keys); |
| 92 | option_values.emplace_back(pair.op_values); | 91 | option_values.emplace_back(pair.op_values); |
| 93 | } | 92 | } |
| @@ -96,18 +95,22 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | @@ -96,18 +95,22 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, | ||
| 96 | Ort::GetAvailableProviders(); | 95 | Ort::GetAvailableProviders(); |
| 97 | if (std::find(available_providers.begin(), available_providers.end(), | 96 | if (std::find(available_providers.begin(), available_providers.end(), |
| 98 | "TensorrtExecutionProvider") != available_providers.end()) { | 97 | "TensorrtExecutionProvider") != available_providers.end()) { |
| 99 | - const auto& api = Ort::GetApi(); | 98 | + const auto &api = Ort::GetApi(); |
| 100 | 99 | ||
| 101 | - OrtTensorRTProviderOptionsV2* tensorrt_options; | ||
| 102 | - OrtStatus *statusC = api.CreateTensorRTProviderOptions( | ||
| 103 | - &tensorrt_options); | 100 | + OrtTensorRTProviderOptionsV2 *tensorrt_options = nullptr; |
| 101 | + OrtStatus *statusC = | ||
| 102 | + api.CreateTensorRTProviderOptions(&tensorrt_options); | ||
| 104 | OrtStatus *statusU = api.UpdateTensorRTProviderOptions( | 103 | OrtStatus *statusU = api.UpdateTensorRTProviderOptions( |
| 105 | tensorrt_options, option_keys.data(), option_values.data(), | 104 | tensorrt_options, option_keys.data(), option_values.data(), |
| 106 | option_keys.size()); | 105 | option_keys.size()); |
| 107 | sess_opts.AppendExecutionProvider_TensorRT_V2(*tensorrt_options); | 106 | sess_opts.AppendExecutionProvider_TensorRT_V2(*tensorrt_options); |
| 108 | 107 | ||
| 109 | - if (statusC) { OrtStatusFailure(statusC, os.str().c_str()); } | ||
| 110 | - if (statusU) { OrtStatusFailure(statusU, os.str().c_str()); } | 108 | + if (statusC) { |
| 109 | + OrtStatusFailure(statusC, os.str().c_str()); | ||
| 110 | + } | ||
| 111 | + if (statusU) { | ||
| 112 | + OrtStatusFailure(statusU, os.str().c_str()); | ||
| 113 | + } | ||
| 111 | 114 | ||
| 112 | api.ReleaseTensorRTProviderOptions(tensorrt_options); | 115 | api.ReleaseTensorRTProviderOptions(tensorrt_options); |
| 113 | } | 116 | } |
| @@ -20,11 +20,11 @@ class SileroVadModel::Impl { | @@ -20,11 +20,11 @@ class SileroVadModel::Impl { | ||
| 20 | : config_(config), | 20 | : config_(config), |
| 21 | env_(ORT_LOGGING_LEVEL_ERROR), | 21 | env_(ORT_LOGGING_LEVEL_ERROR), |
| 22 | sess_opts_(GetSessionOptions(config)), | 22 | sess_opts_(GetSessionOptions(config)), |
| 23 | - allocator_{} { | 23 | + allocator_{}, |
| 24 | + sample_rate_(config.sample_rate) { | ||
| 24 | auto buf = ReadFile(config.silero_vad.model); | 25 | auto buf = ReadFile(config.silero_vad.model); |
| 25 | Init(buf.data(), buf.size()); | 26 | Init(buf.data(), buf.size()); |
| 26 | 27 | ||
| 27 | - sample_rate_ = config.sample_rate; | ||
| 28 | if (sample_rate_ != 16000) { | 28 | if (sample_rate_ != 16000) { |
| 29 | SHERPA_ONNX_LOGE("Expected sample rate 16000. Given: %d", | 29 | SHERPA_ONNX_LOGE("Expected sample rate 16000. Given: %d", |
| 30 | config.sample_rate); | 30 | config.sample_rate); |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/slice.h" | 5 | #include "sherpa-onnx/csrc/slice.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <vector> | 9 | #include <vector> |
| 11 | 10 | ||
| 12 | namespace sherpa_onnx { | 11 | namespace sherpa_onnx { |
| @@ -122,7 +122,7 @@ class SpeakerEmbeddingManager::Impl { | @@ -122,7 +122,7 @@ class SpeakerEmbeddingManager::Impl { | ||
| 122 | 122 | ||
| 123 | Eigen::VectorXf scores = embedding_matrix_ * v; | 123 | Eigen::VectorXf scores = embedding_matrix_ * v; |
| 124 | 124 | ||
| 125 | - Eigen::VectorXf::Index max_index; | 125 | + Eigen::VectorXf::Index max_index = 0; |
| 126 | float max_score = scores.maxCoeff(&max_index); | 126 | float max_score = scores.maxCoeff(&max_index); |
| 127 | if (max_score < threshold) { | 127 | if (max_score < threshold) { |
| 128 | return {}; | 128 | return {}; |
| @@ -178,11 +178,12 @@ class SpeakerEmbeddingManager::Impl { | @@ -178,11 +178,12 @@ class SpeakerEmbeddingManager::Impl { | ||
| 178 | 178 | ||
| 179 | std::vector<std::string> GetAllSpeakers() const { | 179 | std::vector<std::string> GetAllSpeakers() const { |
| 180 | std::vector<std::string> all_speakers; | 180 | std::vector<std::string> all_speakers; |
| 181 | + all_speakers.reserve(name2row_.size()); | ||
| 181 | for (const auto &p : name2row_) { | 182 | for (const auto &p : name2row_) { |
| 182 | all_speakers.push_back(p.first); | 183 | all_speakers.push_back(p.first); |
| 183 | } | 184 | } |
| 184 | 185 | ||
| 185 | - std::stable_sort(all_speakers.begin(), all_speakers.end()); | 186 | + std::sort(all_speakers.begin(), all_speakers.end()); |
| 186 | return all_speakers; | 187 | return all_speakers; |
| 187 | } | 188 | } |
| 188 | 189 |
| @@ -71,8 +71,8 @@ Ort::Value Stack(OrtAllocator *allocator, | @@ -71,8 +71,8 @@ Ort::Value Stack(OrtAllocator *allocator, | ||
| 71 | T *dst = ans.GetTensorMutableData<T>(); | 71 | T *dst = ans.GetTensorMutableData<T>(); |
| 72 | 72 | ||
| 73 | for (int32_t i = 0; i != leading_size; ++i) { | 73 | for (int32_t i = 0; i != leading_size; ++i) { |
| 74 | - for (int32_t n = 0; n != static_cast<int32_t>(values.size()); ++n) { | ||
| 75 | - const T *src = values[n]->GetTensorData<T>(); | 74 | + for (auto value : values) { |
| 75 | + const T *src = value->GetTensorData<T>(); | ||
| 76 | src += i * trailing_size; | 76 | src += i * trailing_size; |
| 77 | 77 | ||
| 78 | std::copy(src, src + trailing_size, dst); | 78 | std::copy(src, src + trailing_size, dst); |
| @@ -36,7 +36,7 @@ SymbolTable::SymbolTable(AAssetManager *mgr, const std::string &filename) { | @@ -36,7 +36,7 @@ SymbolTable::SymbolTable(AAssetManager *mgr, const std::string &filename) { | ||
| 36 | 36 | ||
| 37 | void SymbolTable::Init(std::istream &is) { | 37 | void SymbolTable::Init(std::istream &is) { |
| 38 | std::string sym; | 38 | std::string sym; |
| 39 | - int32_t id; | 39 | + int32_t id = 0; |
| 40 | while (is >> sym >> id) { | 40 | while (is >> sym >> id) { |
| 41 | #if 0 | 41 | #if 0 |
| 42 | // we disable the test here since for some multi-lingual BPE models | 42 | // we disable the test here since for some multi-lingual BPE models |
| @@ -5,9 +5,8 @@ | @@ -5,9 +5,8 @@ | ||
| 5 | 5 | ||
| 6 | #include "sherpa-onnx/csrc/text-utils.h" | 6 | #include "sherpa-onnx/csrc/text-utils.h" |
| 7 | 7 | ||
| 8 | -#include <assert.h> | ||
| 9 | - | ||
| 10 | #include <algorithm> | 8 | #include <algorithm> |
| 9 | +#include <cassert> | ||
| 11 | #include <cctype> | 10 | #include <cctype> |
| 12 | #include <cstdint> | 11 | #include <cstdint> |
| 13 | #include <limits> | 12 | #include <limits> |
| @@ -151,7 +151,6 @@ void TransducerKeywordDecoder::Decode( | @@ -151,7 +151,6 @@ void TransducerKeywordDecoder::Decode( | ||
| 151 | 151 | ||
| 152 | if (matched) { | 152 | if (matched) { |
| 153 | float ys_prob = 0.0; | 153 | float ys_prob = 0.0; |
| 154 | - int32_t length = best_hyp.ys_probs.size(); | ||
| 155 | for (int32_t i = 0; i < matched_state->level; ++i) { | 154 | for (int32_t i = 0; i < matched_state->level; ++i) { |
| 156 | ys_prob += best_hyp.ys_probs[i]; | 155 | ys_prob += best_hyp.ys_probs[i]; |
| 157 | } | 156 | } |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/transpose.h" | 5 | #include "sherpa-onnx/csrc/transpose.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <vector> | 9 | #include <vector> |
| 11 | 10 | ||
| 12 | namespace sherpa_onnx { | 11 | namespace sherpa_onnx { |
| @@ -4,9 +4,8 @@ | @@ -4,9 +4,8 @@ | ||
| 4 | 4 | ||
| 5 | #include "sherpa-onnx/csrc/unbind.h" | 5 | #include "sherpa-onnx/csrc/unbind.h" |
| 6 | 6 | ||
| 7 | -#include <assert.h> | ||
| 8 | - | ||
| 9 | #include <algorithm> | 7 | #include <algorithm> |
| 8 | +#include <cassert> | ||
| 10 | #include <functional> | 9 | #include <functional> |
| 11 | #include <numeric> | 10 | #include <numeric> |
| 12 | #include <utility> | 11 | #include <utility> |
| @@ -30,7 +30,6 @@ static bool EncodeBase(const std::vector<std::string> &lines, | @@ -30,7 +30,6 @@ static bool EncodeBase(const std::vector<std::string> &lines, | ||
| 30 | std::vector<float> tmp_thresholds; | 30 | std::vector<float> tmp_thresholds; |
| 31 | std::vector<std::string> tmp_phrases; | 31 | std::vector<std::string> tmp_phrases; |
| 32 | 32 | ||
| 33 | - std::string line; | ||
| 34 | std::string word; | 33 | std::string word; |
| 35 | bool has_scores = false; | 34 | bool has_scores = false; |
| 36 | bool has_thresholds = false; | 35 | bool has_thresholds = false; |
| @@ -72,6 +71,7 @@ static bool EncodeBase(const std::vector<std::string> &lines, | @@ -72,6 +71,7 @@ static bool EncodeBase(const std::vector<std::string> &lines, | ||
| 72 | } | 71 | } |
| 73 | } | 72 | } |
| 74 | ids->push_back(std::move(tmp_ids)); | 73 | ids->push_back(std::move(tmp_ids)); |
| 74 | + tmp_ids = {}; | ||
| 75 | tmp_scores.push_back(score); | 75 | tmp_scores.push_back(score); |
| 76 | tmp_phrases.push_back(phrase); | 76 | tmp_phrases.push_back(phrase); |
| 77 | tmp_thresholds.push_back(threshold); | 77 | tmp_thresholds.push_back(threshold); |
| @@ -100,13 +100,13 @@ struct WaveHeader { | @@ -100,13 +100,13 @@ struct WaveHeader { | ||
| 100 | int32_t subchunk2_id; // a tag of this chunk | 100 | int32_t subchunk2_id; // a tag of this chunk |
| 101 | int32_t subchunk2_size; // size of subchunk2 | 101 | int32_t subchunk2_size; // size of subchunk2 |
| 102 | }; | 102 | }; |
| 103 | -static_assert(sizeof(WaveHeader) == 44, ""); | 103 | +static_assert(sizeof(WaveHeader) == 44); |
| 104 | 104 | ||
| 105 | // Read a wave file of mono-channel. | 105 | // Read a wave file of mono-channel. |
| 106 | // Return its samples normalized to the range [-1, 1). | 106 | // Return its samples normalized to the range [-1, 1). |
| 107 | std::vector<float> ReadWaveImpl(std::istream &is, int32_t *sampling_rate, | 107 | std::vector<float> ReadWaveImpl(std::istream &is, int32_t *sampling_rate, |
| 108 | bool *is_ok) { | 108 | bool *is_ok) { |
| 109 | - WaveHeader header; | 109 | + WaveHeader header{}; |
| 110 | is.read(reinterpret_cast<char *>(&header), sizeof(header)); | 110 | is.read(reinterpret_cast<char *>(&header), sizeof(header)); |
| 111 | if (!is) { | 111 | if (!is) { |
| 112 | *is_ok = false; | 112 | *is_ok = false; |
| @@ -37,7 +37,7 @@ struct WaveHeader { | @@ -37,7 +37,7 @@ struct WaveHeader { | ||
| 37 | 37 | ||
| 38 | bool WriteWave(const std::string &filename, int32_t sampling_rate, | 38 | bool WriteWave(const std::string &filename, int32_t sampling_rate, |
| 39 | const float *samples, int32_t n) { | 39 | const float *samples, int32_t n) { |
| 40 | - WaveHeader header; | 40 | + WaveHeader header{}; |
| 41 | header.chunk_id = 0x46464952; // FFIR | 41 | header.chunk_id = 0x46464952; // FFIR |
| 42 | header.format = 0x45564157; // EVAW | 42 | header.format = 0x45564157; // EVAW |
| 43 | header.subchunk1_id = 0x20746d66; // "fmt " | 43 | header.subchunk1_id = 0x20746d66; // "fmt " |
-
请 注册 或 登录 后发表评论