diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 00000000..9f2baf4c --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,73 @@ +--- +# NOTE there must be no spaces before the '-', so put the comma last. +# The check bugprone-unchecked-optional-access is also turned off atm +# because it causes clang-tidy to hang randomly. The tracking issue +# can be found at https://github.com/llvm/llvm-project/issues/69369. +# +# Modified from +# https://github.com/pytorch/pytorch/blob/main/.clang-tidy +InheritParentConfig: true +Checks: ' +bugprone-*, +-bugprone-easily-swappable-parameters, +-bugprone-forward-declaration-namespace, +-bugprone-implicit-widening-of-multiplication-result, +-bugprone-macro-parentheses, +-bugprone-lambda-function-name, +-bugprone-narrowing-conversions, +-bugprone-reserved-identifier, +-bugprone-swapped-arguments, +-bugprone-unchecked-optional-access, +clang-diagnostic-missing-prototypes, +cppcoreguidelines-*, +-cppcoreguidelines-avoid-const-or-ref-data-members, +-cppcoreguidelines-avoid-do-while, +-cppcoreguidelines-avoid-magic-numbers, +-cppcoreguidelines-avoid-non-const-global-variables, +-cppcoreguidelines-interfaces-global-init, +-cppcoreguidelines-macro-usage, +-cppcoreguidelines-narrowing-conversions, +-cppcoreguidelines-owning-memory, +-cppcoreguidelines-pro-bounds-array-to-pointer-decay, +-cppcoreguidelines-pro-bounds-constant-array-index, +-cppcoreguidelines-pro-bounds-pointer-arithmetic, +-cppcoreguidelines-pro-type-const-cast, +-cppcoreguidelines-pro-type-cstyle-cast, +-cppcoreguidelines-pro-type-reinterpret-cast, +-cppcoreguidelines-pro-type-static-cast-downcast, +-cppcoreguidelines-pro-type-union-access, +-cppcoreguidelines-pro-type-vararg, +-cppcoreguidelines-special-member-functions, +-cppcoreguidelines-non-private-member-variables-in-classes, +-facebook-hte-RelativeInclude, +hicpp-exception-baseclass, +hicpp-avoid-goto, +misc-*, +-misc-const-correctness, +-misc-include-cleaner, +-misc-use-anonymous-namespace, +-misc-unused-parameters, +-misc-no-recursion, +-misc-non-private-member-variables-in-classes, +-misc-confusable-identifiers, +modernize-*, +-modernize-macro-to-enum, +-modernize-pass-by-value, +-modernize-return-braced-init-list, +-modernize-use-auto, +-modernize-use-default-member-init, +-modernize-use-using, +-modernize-use-trailing-return-type, +-modernize-use-nodiscard, +performance-*, +readability-container-size-empty, +readability-delete-null-pointer, +readability-duplicate-include +readability-misplaced-array-index, +readability-redundant-function-ptr-dereference, +readability-redundant-smartptr-get, +readability-simplify-subscript-expr, +readability-string-compare, +' +WarningsAsErrors: '*' +... diff --git a/.github/workflows/clang-tidy.yaml b/.github/workflows/clang-tidy.yaml new file mode 100644 index 00000000..3295fc64 --- /dev/null +++ b/.github/workflows/clang-tidy.yaml @@ -0,0 +1,57 @@ +name: clang-tidy + +on: + push: + branches: + - master + - clang-tidy + paths: + - 'sherpa-onnx/csrc/**' + + pull_request: + branches: + - master + paths: + - 'sherpa-onnx/csrc/**' + + workflow_dispatch: + +concurrency: + group: clang-tidy-${{ github.ref }} + cancel-in-progress: true + +jobs: + clang-tidy: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.8] + fail-fast: false + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install clang-tidy + shell: bash + run: | + pip install clang-tidy + + - name: Configure + shell: bash + run: | + mkdir build + cd build + cmake -DSHERPA_ONNX_ENABLE_PYTHON=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. + + - name: Check with clang-tidy + shell: bash + run: | + cd build + make check diff --git a/.github/workflows/flutter-macos.yaml b/.github/workflows/flutter-macos.yaml index d92d3087..d148fb04 100644 --- a/.github/workflows/flutter-macos.yaml +++ b/.github/workflows/flutter-macos.yaml @@ -184,6 +184,7 @@ jobs: path: ./*.tar.bz2 - name: Publish to huggingface + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release' env: HF_TOKEN: ${{ secrets.HF_TOKEN }} uses: nick-fields/retry@v3 diff --git a/.github/workflows/flutter-windows-x64.yaml b/.github/workflows/flutter-windows-x64.yaml index ce4e7456..7dfc197e 100644 --- a/.github/workflows/flutter-windows-x64.yaml +++ b/.github/workflows/flutter-windows-x64.yaml @@ -133,6 +133,7 @@ jobs: shell: bash run: | d=$PWD + SHERPA_ONNX_VERSION=v$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) pushd sherpa-onnx/flutter dart pub get @@ -159,6 +160,7 @@ jobs: path: ./*.tar.bz2 - name: Publish to huggingface + if: (github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa') && github.event_name == 'push' && contains(github.ref, 'refs/tags/') && matrix.build_type == 'Release' env: HF_TOKEN: ${{ secrets.HF_TOKEN }} uses: nick-fields/retry@v3 diff --git a/CMakeLists.txt b/CMakeLists.txt index dedf5276..d474c6aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -167,7 +167,7 @@ if(SHERPA_ONNX_ENABLE_WASM_KWS) endif() if(NOT CMAKE_CXX_STANDARD) - set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.") + set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ version to be used.") endif() set(CMAKE_CXX_EXTENSIONS OFF) message(STATUS "C++ Standard version: ${CMAKE_CXX_STANDARD}") diff --git a/cmake/openfst.cmake b/cmake/openfst.cmake index 59d4f9fc..77e4c157 100644 --- a/cmake/openfst.cmake +++ b/cmake/openfst.cmake @@ -3,18 +3,18 @@ function(download_openfst) include(FetchContent) - set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz") - set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-13.tar.gz") - set(openfst_HASH "SHA256=f10a71c6b64d89eabdc316d372b956c30c825c7c298e2f20c780320e8181ffb6") + set(openfst_URL "https://github.com/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz") + set(openfst_URL2 "https://hub.nuaa.cf/csukuangfj/openfst/archive/refs/tags/sherpa-onnx-2024-06-19.tar.gz") + set(openfst_HASH "SHA256=5c98e82cc509c5618502dde4860b8ea04d843850ed57e6d6b590b644b268853d") # If you don't have access to the Internet, # please pre-download it set(possible_file_locations - $ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-13.tar.gz - ${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz - ${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-13.tar.gz - /tmp/openfst-sherpa-onnx-2024-06-13.tar.gz - /star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-13.tar.gz + $ENV{HOME}/Downloads/openfst-sherpa-onnx-2024-06-19.tar.gz + ${CMAKE_SOURCE_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz + ${CMAKE_BINARY_DIR}/openfst-sherpa-onnx-2024-06-19.tar.gz + /tmp/openfst-sherpa-onnx-2024-06-19.tar.gz + /star-fj/fangjun/download/github/openfst-sherpa-onnx-2024-06-19.tar.gz ) foreach(f IN LISTS possible_file_locations) diff --git a/sherpa-onnx/csrc/CMakeLists.txt b/sherpa-onnx/csrc/CMakeLists.txt index bac0499a..16da143f 100644 --- a/sherpa-onnx/csrc/CMakeLists.txt +++ b/sherpa-onnx/csrc/CMakeLists.txt @@ -534,3 +534,17 @@ if(SHERPA_ONNX_ENABLE_TESTS) sherpa_onnx_add_test(${source}) endforeach() endif() + +set(srcs_to_check) +foreach(s IN LISTS sources) + list(APPEND srcs_to_check ${CMAKE_CURRENT_LIST_DIR}/${s}) +endforeach() + +# For clang-tidy +add_custom_target( + clang-tidy-check + clang-tidy -p ${CMAKE_BINARY_DIR}/compile_commands.json --config-file ${CMAKE_SOURCE_DIR}/.clang-tidy ${srcs_to_check} + DEPENDS ${sources}) + +add_custom_target(check DEPENDS clang-tidy-check) + diff --git a/sherpa-onnx/csrc/audio-tagging-label-file.cc b/sherpa-onnx/csrc/audio-tagging-label-file.cc index ea103299..bd71bcb1 100644 --- a/sherpa-onnx/csrc/audio-tagging-label-file.cc +++ b/sherpa-onnx/csrc/audio-tagging-label-file.cc @@ -60,7 +60,7 @@ void AudioTaggingLabels::Init(std::istream &is) { std::size_t pos{}; int32_t i = std::stoi(index, &pos); - if (index.size() == 0 || pos != index.size()) { + if (index.empty() || pos != index.size()) { SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str()); exit(-1); } diff --git a/sherpa-onnx/csrc/base64-decode.cc b/sherpa-onnx/csrc/base64-decode.cc index b22e443e..5723790f 100644 --- a/sherpa-onnx/csrc/base64-decode.cc +++ b/sherpa-onnx/csrc/base64-decode.cc @@ -34,7 +34,7 @@ std::string Base64Decode(const std::string &s) { exit(-1); } - int32_t n = s.size() / 4 * 3; + int32_t n = static_cast(s.size()) / 4 * 3; std::string ans; ans.reserve(n); @@ -46,16 +46,16 @@ std::string Base64Decode(const std::string &s) { } int32_t first = (Ord(s[i]) << 2) + ((Ord(s[i + 1]) & 0x30) >> 4); - ans.push_back(first); + ans.push_back(static_cast(first)); if (i + 2 < static_cast(s.size()) && s[i + 2] != '=') { int32_t second = ((Ord(s[i + 1]) & 0x0f) << 4) + ((Ord(s[i + 2]) & 0x3c) >> 2); - ans.push_back(second); + ans.push_back(static_cast(second)); if (i + 3 < static_cast(s.size()) && s[i + 3] != '=') { int32_t third = ((Ord(s[i + 2]) & 0x03) << 6) + Ord(s[i + 3]); - ans.push_back(third); + ans.push_back(static_cast(third)); } } i += 4; diff --git a/sherpa-onnx/csrc/cat.cc b/sherpa-onnx/csrc/cat.cc index 00da2263..05c40b0c 100644 --- a/sherpa-onnx/csrc/cat.cc +++ b/sherpa-onnx/csrc/cat.cc @@ -82,9 +82,9 @@ Ort::Value Cat(OrtAllocator *allocator, T *dst = ans.GetTensorMutableData(); for (int32_t i = 0; i != leading_size; ++i) { - for (int32_t n = 0; n != static_cast(values.size()); ++n) { - auto this_dim = values[n]->GetTensorTypeAndShapeInfo().GetShape()[dim]; - const T *src = values[n]->GetTensorData(); + for (auto value : values) { + auto this_dim = value->GetTensorTypeAndShapeInfo().GetShape()[dim]; + const T *src = value->GetTensorData(); src += i * this_dim * trailing_size; std::copy(src, src + this_dim * trailing_size, dst); diff --git a/sherpa-onnx/csrc/circular-buffer.cc b/sherpa-onnx/csrc/circular-buffer.cc index 4ed66733..ef937cab 100644 --- a/sherpa-onnx/csrc/circular-buffer.cc +++ b/sherpa-onnx/csrc/circular-buffer.cc @@ -20,7 +20,7 @@ CircularBuffer::CircularBuffer(int32_t capacity) { } void CircularBuffer::Resize(int32_t new_capacity) { - int32_t capacity = buffer_.size(); + int32_t capacity = static_cast(buffer_.size()); if (new_capacity <= capacity) { SHERPA_ONNX_LOGE("new_capacity (%d) <= original capacity (%d). Skip it.", new_capacity, capacity); @@ -86,7 +86,7 @@ void CircularBuffer::Resize(int32_t new_capacity) { } void CircularBuffer::Push(const float *p, int32_t n) { - int32_t capacity = buffer_.size(); + int32_t capacity = static_cast(buffer_.size()); int32_t size = Size(); if (n + size > capacity) { int32_t new_capacity = std::max(capacity * 2, n + size); @@ -126,7 +126,7 @@ std::vector CircularBuffer::Get(int32_t start_index, int32_t n) const { return {}; } - int32_t capacity = buffer_.size(); + int32_t capacity = static_cast(buffer_.size()); if (start_index - head_ + n > size) { SHERPA_ONNX_LOGE("Invalid start_index: %d and n: %d. head_: %d, size: %d", diff --git a/sherpa-onnx/csrc/context-graph.cc b/sherpa-onnx/csrc/context-graph.cc index ef876b36..336208b1 100644 --- a/sherpa-onnx/csrc/context-graph.cc +++ b/sherpa-onnx/csrc/context-graph.cc @@ -67,8 +67,8 @@ void ContextGraph::Build(const std::vector> &token_ids, std::tuple ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, bool strict_mode /*= true*/) const { - const ContextState *node; - float score; + const ContextState *node = nullptr; + float score = 0; if (1 == state->next.count(token)) { node = state->next.at(token).get(); score = node->token_score; @@ -84,7 +84,10 @@ ContextGraph::ForwardOneStep(const ContextState *state, int32_t token, score = node->node_score - state->node_score; } - SHERPA_ONNX_CHECK(nullptr != node); + if (!node) { + SHERPA_ONNX_LOGE("Some bad things happened."); + exit(-1); + } const ContextState *matched_node = node->is_end ? node : (node->output != nullptr ? node->output : nullptr); diff --git a/sherpa-onnx/csrc/endpoint.cc b/sherpa-onnx/csrc/endpoint.cc index 3a9a424c..afe76fdb 100644 --- a/sherpa-onnx/csrc/endpoint.cc +++ b/sherpa-onnx/csrc/endpoint.cc @@ -73,10 +73,15 @@ std::string EndpointConfig::ToString() const { return os.str(); } -bool Endpoint::IsEndpoint(int num_frames_decoded, int trailing_silence_frames, +bool Endpoint::IsEndpoint(int32_t num_frames_decoded, + int32_t trailing_silence_frames, float frame_shift_in_seconds) const { - float utterance_length = num_frames_decoded * frame_shift_in_seconds; - float trailing_silence = trailing_silence_frames * frame_shift_in_seconds; + float utterance_length = + static_cast(num_frames_decoded) * frame_shift_in_seconds; + + float trailing_silence = + static_cast(trailing_silence_frames) * frame_shift_in_seconds; + if (RuleActivated(config_.rule1, "rule1", trailing_silence, utterance_length) || RuleActivated(config_.rule2, "rule2", trailing_silence, diff --git a/sherpa-onnx/csrc/endpoint.h b/sherpa-onnx/csrc/endpoint.h index 73995840..aea5d7d2 100644 --- a/sherpa-onnx/csrc/endpoint.h +++ b/sherpa-onnx/csrc/endpoint.h @@ -64,7 +64,7 @@ class Endpoint { /// This function returns true if this set of endpointing rules thinks we /// should terminate decoding. - bool IsEndpoint(int num_frames_decoded, int trailing_silence_frames, + bool IsEndpoint(int32_t num_frames_decoded, int32_t trailing_silence_frames, float frame_shift_in_seconds) const; private: diff --git a/sherpa-onnx/csrc/jieba-lexicon.cc b/sherpa-onnx/csrc/jieba-lexicon.cc index bffc1187..1bf64cd5 100644 --- a/sherpa-onnx/csrc/jieba-lexicon.cc +++ b/sherpa-onnx/csrc/jieba-lexicon.cc @@ -103,6 +103,7 @@ class JiebaLexicon::Impl { if (w == "。" || w == "!" || w == "?" || w == ",") { ans.push_back(std::move(this_sentence)); + this_sentence = {}; } } // for (const auto &w : words) diff --git a/sherpa-onnx/csrc/keyword-spotter.cc b/sherpa-onnx/csrc/keyword-spotter.cc index 7e93d7a0..1110ee58 100644 --- a/sherpa-onnx/csrc/keyword-spotter.cc +++ b/sherpa-onnx/csrc/keyword-spotter.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/keyword-spotter.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/lexicon.cc b/sherpa-onnx/csrc/lexicon.cc index 3a502c24..91307a21 100644 --- a/sherpa-onnx/csrc/lexicon.cc +++ b/sherpa-onnx/csrc/lexicon.cc @@ -82,7 +82,7 @@ std::unordered_map ReadTokens(std::istream &is) { std::string line; std::string sym; - int32_t id; + int32_t id = -1; while (std::getline(is, line)) { std::istringstream iss(line); iss >> sym; @@ -254,6 +254,7 @@ std::vector> Lexicon::ConvertTextToTokenIdsChinese( this_sentence.push_back(eos); } ans.push_back(std::move(this_sentence)); + this_sentence = {}; if (sil != -1) { this_sentence.push_back(sil); @@ -324,6 +325,7 @@ std::vector> Lexicon::ConvertTextToTokenIdsNotChinese( if (w != ",") { this_sentence.push_back(blank); ans.push_back(std::move(this_sentence)); + this_sentence = {}; } continue; diff --git a/sherpa-onnx/csrc/lexicon.h b/sherpa-onnx/csrc/lexicon.h index e26a2dec..81a51095 100644 --- a/sherpa-onnx/csrc/lexicon.h +++ b/sherpa-onnx/csrc/lexicon.h @@ -62,8 +62,8 @@ class Lexicon : public OfflineTtsFrontend { std::unordered_map> word2ids_; std::unordered_set punctuations_; std::unordered_map token2id_; - Language language_; - bool debug_; + Language language_ = Language::kUnknown; + bool debug_ = false; }; } // namespace sherpa_onnx diff --git a/sherpa-onnx/csrc/offline-ct-transformer-model.cc b/sherpa-onnx/csrc/offline-ct-transformer-model.cc index 7f7e63b0..2ce593b3 100644 --- a/sherpa-onnx/csrc/offline-ct-transformer-model.cc +++ b/sherpa-onnx/csrc/offline-ct-transformer-model.cc @@ -67,7 +67,7 @@ class OfflineCtTransformerModel::Impl { std::vector tokens; SHERPA_ONNX_READ_META_DATA_VEC_STRING_SEP(tokens, "tokens", "|"); - int32_t vocab_size; + int32_t vocab_size = 0; SHERPA_ONNX_READ_META_DATA(vocab_size, "vocab_size"); if (static_cast(tokens.size()) != vocab_size) { SHERPA_ONNX_LOGE("tokens.size() %d != vocab_size %d", diff --git a/sherpa-onnx/csrc/offline-ctc-model.cc b/sherpa-onnx/csrc/offline-ctc-model.cc index cfa8ab45..bd646ece 100644 --- a/sherpa-onnx/csrc/offline-ctc-model.cc +++ b/sherpa-onnx/csrc/offline-ctc-model.cc @@ -19,7 +19,7 @@ namespace { -enum class ModelType { +enum class ModelType : std::uint8_t { kEncDecCTCModelBPE, kEncDecHybridRNNTCTCBPEModel, kTdnn, diff --git a/sherpa-onnx/csrc/offline-stream.cc b/sherpa-onnx/csrc/offline-stream.cc index 6e72a4a1..79bdb5c5 100644 --- a/sherpa-onnx/csrc/offline-stream.cc +++ b/sherpa-onnx/csrc/offline-stream.cc @@ -4,11 +4,11 @@ #include "sherpa-onnx/csrc/offline-stream.h" -#include - #include +#include #include #include +#include #include "kaldi-native-fbank/csrc/online-feature.h" #include "sherpa-onnx/csrc/macros.h" @@ -56,7 +56,7 @@ class OfflineStream::Impl { public: explicit Impl(const FeatureExtractorConfig &config, ContextGraphPtr context_graph) - : config_(config), context_graph_(context_graph) { + : config_(config), context_graph_(std::move(context_graph)) { if (config.is_mfcc) { mfcc_opts_.frame_opts.dither = config_.dither; mfcc_opts_.frame_opts.snip_edges = config_.snip_edges; @@ -266,7 +266,7 @@ class OfflineStream::Impl { OfflineStream::OfflineStream(const FeatureExtractorConfig &config /*= {}*/, ContextGraphPtr context_graph /*= nullptr*/) - : impl_(std::make_unique(config, context_graph)) {} + : impl_(std::make_unique(config, std::move(context_graph))) {} OfflineStream::OfflineStream(WhisperTag tag) : impl_(std::make_unique(tag)) {} diff --git a/sherpa-onnx/csrc/offline-transducer-modified-beam-search-decoder.cc b/sherpa-onnx/csrc/offline-transducer-modified-beam-search-decoder.cc index 317c7ad8..391620a0 100644 --- a/sherpa-onnx/csrc/offline-transducer-modified-beam-search-decoder.cc +++ b/sherpa-onnx/csrc/offline-transducer-modified-beam-search-decoder.cc @@ -42,7 +42,7 @@ OfflineTransducerModifiedBeamSearchDecoder::Decode( std::vector context_graphs(batch_size, nullptr); for (int32_t i = 0; i < batch_size; ++i) { - const ContextState *context_state; + const ContextState *context_state = nullptr; if (ss != nullptr) { context_graphs[i] = ss[packed_encoder_out.sorted_indexes[i]]->GetContextGraph(); diff --git a/sherpa-onnx/csrc/offline-tts-character-frontend.cc b/sherpa-onnx/csrc/offline-tts-character-frontend.cc index 2b64e67d..857200e9 100644 --- a/sherpa-onnx/csrc/offline-tts-character-frontend.cc +++ b/sherpa-onnx/csrc/offline-tts-character-frontend.cc @@ -30,7 +30,7 @@ static std::unordered_map ReadTokens(std::istream &is) { std::string sym; std::u32string s; - int32_t id; + int32_t id = 0; while (std::getline(is, line)) { std::istringstream iss(line); iss >> sym; @@ -138,6 +138,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( } ans.push_back(std::move(this_sentence)); + this_sentence = {}; // re-initialize this_sentence if (use_eos_bos) { @@ -172,6 +173,7 @@ OfflineTtsCharacterFrontend::ConvertTextToTokenIds( } ans.push_back(std::move(this_sentence)); + this_sentence = {}; // re-initialize this_sentence if (use_eos_bos) { diff --git a/sherpa-onnx/csrc/offline-tts.cc b/sherpa-onnx/csrc/offline-tts.cc index 4349f98e..12feda0b 100644 --- a/sherpa-onnx/csrc/offline-tts.cc +++ b/sherpa-onnx/csrc/offline-tts.cc @@ -5,6 +5,7 @@ #include "sherpa-onnx/csrc/offline-tts.h" #include +#include #include "sherpa-onnx/csrc/file-utils.h" #include "sherpa-onnx/csrc/macros.h" @@ -87,7 +88,7 @@ OfflineTts::~OfflineTts() = default; GeneratedAudio OfflineTts::Generate( const std::string &text, int64_t sid /*=0*/, float speed /*= 1.0*/, GeneratedAudioCallback callback /*= nullptr*/) const { - return impl_->Generate(text, sid, speed, callback); + return impl_->Generate(text, sid, speed, std::move(callback)); } int32_t OfflineTts::SampleRate() const { return impl_->SampleRate(); } diff --git a/sherpa-onnx/csrc/offline-whisper-model.cc b/sherpa-onnx/csrc/offline-whisper-model.cc index 183574f9..f73234d9 100644 --- a/sherpa-onnx/csrc/offline-whisper-model.cc +++ b/sherpa-onnx/csrc/offline-whisper-model.cc @@ -22,9 +22,9 @@ class OfflineWhisperModel::Impl { explicit Impl(const OfflineModelConfig &config) : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), + debug_(config.debug), sess_opts_(GetSessionOptions(config)), allocator_{} { - debug_ = config_.debug; { auto buf = ReadFile(config.whisper.encoder); InitEncoder(buf.data(), buf.size()); @@ -39,9 +39,9 @@ class OfflineWhisperModel::Impl { explicit Impl(const SpokenLanguageIdentificationConfig &config) : lid_config_(config), env_(ORT_LOGGING_LEVEL_ERROR), + debug_(config_.debug), sess_opts_(GetSessionOptions(config)), allocator_{} { - debug_ = config_.debug; { auto buf = ReadFile(config.whisper.encoder); InitEncoder(buf.data(), buf.size()); @@ -148,7 +148,6 @@ class OfflineWhisperModel::Impl { cross_v = std::move(std::get<4>(decoder_out)); const float *p_logits = std::get<0>(decoder_out).GetTensorData(); - int32_t vocab_size = VocabSize(); const auto &all_language_ids = GetAllLanguageIDs(); int32_t lang_id = all_language_ids[0]; @@ -317,18 +316,18 @@ class OfflineWhisperModel::Impl { std::unordered_map id2lang_; // model meta data - int32_t n_text_layer_; - int32_t n_text_ctx_; - int32_t n_text_state_; - int32_t n_vocab_; - int32_t sot_; - int32_t eot_; - int32_t blank_; - int32_t translate_; - int32_t transcribe_; - int32_t no_timestamps_; - int32_t no_speech_; - int32_t is_multilingual_; + int32_t n_text_layer_ = 0; + int32_t n_text_ctx_ = 0; + int32_t n_text_state_ = 0; + int32_t n_vocab_ = 0; + int32_t sot_ = 0; + int32_t eot_ = 0; + int32_t blank_ = 0; + int32_t translate_ = 0; + int32_t transcribe_ = 0; + int32_t no_timestamps_ = 0; + int32_t no_speech_ = 0; + int32_t is_multilingual_ = 0; std::vector sot_sequence_; }; diff --git a/sherpa-onnx/csrc/online-conformer-transducer-model.cc b/sherpa-onnx/csrc/online-conformer-transducer-model.cc index 58cbce01..8e7dfa1f 100644 --- a/sherpa-onnx/csrc/online-conformer-transducer-model.cc +++ b/sherpa-onnx/csrc/online-conformer-transducer-model.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/online-conformer-transducer-model.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/online-ctc-fst-decoder.cc b/sherpa-onnx/csrc/online-ctc-fst-decoder.cc index f5057883..95e16ba7 100644 --- a/sherpa-onnx/csrc/online-ctc-fst-decoder.cc +++ b/sherpa-onnx/csrc/online-ctc-fst-decoder.cc @@ -52,8 +52,9 @@ static void DecodeOne(const float *log_probs, int32_t num_rows, if (ok) { std::vector isymbols_out; std::vector osymbols_out; - ok = fst::GetLinearSymbolSequence(fst_out, &isymbols_out, &osymbols_out, - nullptr); + /*ok =*/fst::GetLinearSymbolSequence(fst_out, &isymbols_out, + &osymbols_out, nullptr); + // TODO(fangjun): handle ok is false std::vector tokens; tokens.reserve(isymbols_out.size()); diff --git a/sherpa-onnx/csrc/online-lstm-transducer-model.cc b/sherpa-onnx/csrc/online-lstm-transducer-model.cc index 4a0e838d..eca11593 100644 --- a/sherpa-onnx/csrc/online-lstm-transducer-model.cc +++ b/sherpa-onnx/csrc/online-lstm-transducer-model.cc @@ -3,9 +3,8 @@ // Copyright (c) 2023 Xiaomi Corporation #include "sherpa-onnx/csrc/online-lstm-transducer-model.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/online-nemo-ctc-model.cc b/sherpa-onnx/csrc/online-nemo-ctc-model.cc index 3f796e2d..e4335b4b 100644 --- a/sherpa-onnx/csrc/online-nemo-ctc-model.cc +++ b/sherpa-onnx/csrc/online-nemo-ctc-model.cc @@ -265,16 +265,16 @@ class OnlineNeMoCtcModel::Impl { std::vector output_names_; std::vector output_names_ptr_; - int32_t window_size_; - int32_t chunk_shift_; - int32_t subsampling_factor_; - int32_t vocab_size_; - int32_t cache_last_channel_dim1_; - int32_t cache_last_channel_dim2_; - int32_t cache_last_channel_dim3_; - int32_t cache_last_time_dim1_; - int32_t cache_last_time_dim2_; - int32_t cache_last_time_dim3_; + int32_t window_size_ = 0; + int32_t chunk_shift_ = 0; + int32_t subsampling_factor_ = 0; + int32_t vocab_size_ = 0; + int32_t cache_last_channel_dim1_ = 0; + int32_t cache_last_channel_dim2_ = 0; + int32_t cache_last_channel_dim3_ = 0; + int32_t cache_last_time_dim1_ = 0; + int32_t cache_last_time_dim2_ = 0; + int32_t cache_last_time_dim3_ = 0; Ort::Value cache_last_channel_{nullptr}; Ort::Value cache_last_time_{nullptr}; diff --git a/sherpa-onnx/csrc/online-recognizer.cc b/sherpa-onnx/csrc/online-recognizer.cc index a49a62f6..599a0553 100644 --- a/sherpa-onnx/csrc/online-recognizer.cc +++ b/sherpa-onnx/csrc/online-recognizer.cc @@ -5,9 +5,8 @@ #include "sherpa-onnx/csrc/online-recognizer.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/online-stream.cc b/sherpa-onnx/csrc/online-stream.cc index 0a30cf40..972133c7 100644 --- a/sherpa-onnx/csrc/online-stream.cc +++ b/sherpa-onnx/csrc/online-stream.cc @@ -8,6 +8,7 @@ #include #include "sherpa-onnx/csrc/features.h" +#include "sherpa-onnx/csrc/transducer-keyword-decoder.h" namespace sherpa_onnx { @@ -15,7 +16,7 @@ class OnlineStream::Impl { public: explicit Impl(const FeatureExtractorConfig &config, ContextGraphPtr context_graph) - : feat_extractor_(config), context_graph_(context_graph) {} + : feat_extractor_(config), context_graph_(std::move(context_graph)) {} void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) { feat_extractor_.AcceptWaveform(sampling_rate, waveform, n); @@ -146,7 +147,7 @@ class OnlineStream::Impl { OnlineStream::OnlineStream(const FeatureExtractorConfig &config /*= {}*/, ContextGraphPtr context_graph /*= nullptr */) - : impl_(std::make_unique(config, context_graph)) {} + : impl_(std::make_unique(config, std::move(context_graph))) {} OnlineStream::~OnlineStream() = default; diff --git a/sherpa-onnx/csrc/online-stream.h b/sherpa-onnx/csrc/online-stream.h index e9958d73..1cf29de7 100644 --- a/sherpa-onnx/csrc/online-stream.h +++ b/sherpa-onnx/csrc/online-stream.h @@ -15,7 +15,6 @@ #include "sherpa-onnx/csrc/online-ctc-decoder.h" #include "sherpa-onnx/csrc/online-paraformer-decoder.h" #include "sherpa-onnx/csrc/online-transducer-decoder.h" -#include "sherpa-onnx/csrc/transducer-keyword-decoder.h" namespace sherpa_onnx { diff --git a/sherpa-onnx/csrc/online-transducer-decoder.cc b/sherpa-onnx/csrc/online-transducer-decoder.cc index 0c51eabb..682b9bc7 100644 --- a/sherpa-onnx/csrc/online-transducer-decoder.cc +++ b/sherpa-onnx/csrc/online-transducer-decoder.cc @@ -45,13 +45,13 @@ OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( } OnlineTransducerDecoderResult::OnlineTransducerDecoderResult( - OnlineTransducerDecoderResult &&other) + OnlineTransducerDecoderResult &&other) noexcept : OnlineTransducerDecoderResult() { *this = std::move(other); } OnlineTransducerDecoderResult &OnlineTransducerDecoderResult::operator=( - OnlineTransducerDecoderResult &&other) { + OnlineTransducerDecoderResult &&other) noexcept { if (this == &other) { return *this; } diff --git a/sherpa-onnx/csrc/online-transducer-decoder.h b/sherpa-onnx/csrc/online-transducer-decoder.h index 25d798fb..e507a0fc 100644 --- a/sherpa-onnx/csrc/online-transducer-decoder.h +++ b/sherpa-onnx/csrc/online-transducer-decoder.h @@ -44,10 +44,10 @@ struct OnlineTransducerDecoderResult { OnlineTransducerDecoderResult &operator=( const OnlineTransducerDecoderResult &other); - OnlineTransducerDecoderResult(OnlineTransducerDecoderResult &&other); + OnlineTransducerDecoderResult(OnlineTransducerDecoderResult &&other) noexcept; OnlineTransducerDecoderResult &operator=( - OnlineTransducerDecoderResult &&other); + OnlineTransducerDecoderResult &&other) noexcept; }; class OnlineStream; diff --git a/sherpa-onnx/csrc/online-transducer-model.cc b/sherpa-onnx/csrc/online-transducer-model.cc index 6a1fe5d5..a6cd49d9 100644 --- a/sherpa-onnx/csrc/online-transducer-model.cc +++ b/sherpa-onnx/csrc/online-transducer-model.cc @@ -23,7 +23,7 @@ namespace { -enum class ModelType { +enum class ModelType : std::uint8_t { kConformer, kLstm, kZipformer, diff --git a/sherpa-onnx/csrc/online-transducer-nemo-model.cc b/sherpa-onnx/csrc/online-transducer-nemo-model.cc index 3869a8b2..8882c6ca 100644 --- a/sherpa-onnx/csrc/online-transducer-nemo-model.cc +++ b/sherpa-onnx/csrc/online-transducer-nemo-model.cc @@ -5,10 +5,9 @@ #include "sherpa-onnx/csrc/online-transducer-nemo-model.h" -#include -#include - #include +#include +#include #include #include #include @@ -429,8 +428,8 @@ class OnlineTransducerNeMoModel::Impl { std::vector joiner_output_names_; std::vector joiner_output_names_ptr_; - int32_t window_size_; - int32_t chunk_shift_; + int32_t window_size_ = 0; + int32_t chunk_shift_ = 0; int32_t vocab_size_ = 0; int32_t subsampling_factor_ = 8; std::string normalize_type_; @@ -438,12 +437,12 @@ class OnlineTransducerNeMoModel::Impl { int32_t pred_hidden_ = -1; // encoder states - int32_t cache_last_channel_dim1_; - int32_t cache_last_channel_dim2_; - int32_t cache_last_channel_dim3_; - int32_t cache_last_time_dim1_; - int32_t cache_last_time_dim2_; - int32_t cache_last_time_dim3_; + int32_t cache_last_channel_dim1_ = 0; + int32_t cache_last_channel_dim2_ = 0; + int32_t cache_last_channel_dim3_ = 0; + int32_t cache_last_time_dim1_ = 0; + int32_t cache_last_time_dim2_ = 0; + int32_t cache_last_time_dim3_ = 0; // init encoder states Ort::Value cache_last_channel_{nullptr}; diff --git a/sherpa-onnx/csrc/online-wenet-ctc-model.cc b/sherpa-onnx/csrc/online-wenet-ctc-model.cc index 34557bf1..50a7a10a 100644 --- a/sherpa-onnx/csrc/online-wenet-ctc-model.cc +++ b/sherpa-onnx/csrc/online-wenet-ctc-model.cc @@ -192,15 +192,15 @@ class OnlineWenetCtcModel::Impl { std::vector output_names_; std::vector output_names_ptr_; - int32_t head_; - int32_t num_blocks_; - int32_t output_size_; - int32_t cnn_module_kernel_; - int32_t right_context_; - int32_t subsampling_factor_; - int32_t vocab_size_; + int32_t head_ = 0; + int32_t num_blocks_ = 0; + int32_t output_size_ = 0; + int32_t cnn_module_kernel_ = 0; + int32_t right_context_ = 0; + int32_t subsampling_factor_ = 0; + int32_t vocab_size_ = 0; - int32_t required_cache_size_; + int32_t required_cache_size_ = 0; Ort::Value attn_cache_{nullptr}; Ort::Value conv_cache_{nullptr}; diff --git a/sherpa-onnx/csrc/online-zipformer-transducer-model.cc b/sherpa-onnx/csrc/online-zipformer-transducer-model.cc index b7e16cb6..2c9b9737 100644 --- a/sherpa-onnx/csrc/online-zipformer-transducer-model.cc +++ b/sherpa-onnx/csrc/online-zipformer-transducer-model.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/online-zipformer-transducer-model.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/online-zipformer2-ctc-model.cc b/sherpa-onnx/csrc/online-zipformer2-ctc-model.cc index aff4e5cb..3dc3eedf 100644 --- a/sherpa-onnx/csrc/online-zipformer2-ctc-model.cc +++ b/sherpa-onnx/csrc/online-zipformer2-ctc-model.cc @@ -4,10 +4,8 @@ #include "sherpa-onnx/csrc/online-zipformer2-ctc-model.h" -#include -#include - #include +#include #include #include #include @@ -90,7 +88,6 @@ class OnlineZipformer2CtcModel::Impl { std::vector StackStates( std::vector> states) const { int32_t batch_size = static_cast(states.size()); - int32_t num_encoders = static_cast(num_encoder_layers_.size()); std::vector buf(batch_size); @@ -168,7 +165,6 @@ class OnlineZipformer2CtcModel::Impl { assert(states.size() == m * 6 + 2); int32_t batch_size = states[0].GetTensorTypeAndShapeInfo().GetShape()[1]; - int32_t num_encoders = num_encoder_layers_.size(); std::vector> ans; ans.resize(batch_size); diff --git a/sherpa-onnx/csrc/online-zipformer2-transducer-model.cc b/sherpa-onnx/csrc/online-zipformer2-transducer-model.cc index 5d481854..e5c44821 100644 --- a/sherpa-onnx/csrc/online-zipformer2-transducer-model.cc +++ b/sherpa-onnx/csrc/online-zipformer2-transducer-model.cc @@ -4,10 +4,9 @@ #include "sherpa-onnx/csrc/online-zipformer2-transducer-model.h" -#include -#include - #include +#include +#include #include #include #include diff --git a/sherpa-onnx/csrc/onnx-utils.cc b/sherpa-onnx/csrc/onnx-utils.cc index 2d5c2082..5d9f3745 100644 --- a/sherpa-onnx/csrc/onnx-utils.cc +++ b/sherpa-onnx/csrc/onnx-utils.cc @@ -281,11 +281,12 @@ CopyableOrtValue &CopyableOrtValue::operator=(const CopyableOrtValue &other) { return *this; } -CopyableOrtValue::CopyableOrtValue(CopyableOrtValue &&other) { +CopyableOrtValue::CopyableOrtValue(CopyableOrtValue &&other) noexcept { *this = std::move(other); } -CopyableOrtValue &CopyableOrtValue::operator=(CopyableOrtValue &&other) { +CopyableOrtValue &CopyableOrtValue::operator=( + CopyableOrtValue &&other) noexcept { if (this == &other) { return *this; } diff --git a/sherpa-onnx/csrc/onnx-utils.h b/sherpa-onnx/csrc/onnx-utils.h index 0b7fcc75..b179b378 100644 --- a/sherpa-onnx/csrc/onnx-utils.h +++ b/sherpa-onnx/csrc/onnx-utils.h @@ -110,9 +110,9 @@ struct CopyableOrtValue { CopyableOrtValue &operator=(const CopyableOrtValue &other); - CopyableOrtValue(CopyableOrtValue &&other); + CopyableOrtValue(CopyableOrtValue &&other) noexcept; - CopyableOrtValue &operator=(CopyableOrtValue &&other); + CopyableOrtValue &operator=(CopyableOrtValue &&other) noexcept; }; std::vector Convert(std::vector values); diff --git a/sherpa-onnx/csrc/packed-sequence.cc b/sherpa-onnx/csrc/packed-sequence.cc index df5b9202..1c3fe91c 100644 --- a/sherpa-onnx/csrc/packed-sequence.cc +++ b/sherpa-onnx/csrc/packed-sequence.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/packed-sequence.h" -#include - #include +#include #include #include @@ -57,7 +56,7 @@ PackedSequence PackPaddedSequence(OrtAllocator *allocator, int64_t max_T = p_length[indexes[0]]; - int32_t sum_T = std::accumulate(p_length, p_length + n, 0); + auto sum_T = std::accumulate(p_length, p_length + n, static_cast(0)); std::array data_shape{sum_T, v_shape[2]}; diff --git a/sherpa-onnx/csrc/pad-sequence.cc b/sherpa-onnx/csrc/pad-sequence.cc index d9f8ebf9..3ecf277d 100644 --- a/sherpa-onnx/csrc/pad-sequence.cc +++ b/sherpa-onnx/csrc/pad-sequence.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/pad-sequence.h" -#include - #include +#include #include namespace sherpa_onnx { diff --git a/sherpa-onnx/csrc/parse-options.cc b/sherpa-onnx/csrc/parse-options.cc index f0658223..33a07f32 100644 --- a/sherpa-onnx/csrc/parse-options.cc +++ b/sherpa-onnx/csrc/parse-options.cc @@ -11,9 +11,8 @@ #include "sherpa-onnx/csrc/parse-options.h" -#include - #include +#include #include #include #include @@ -33,7 +32,7 @@ ParseOptions::ParseOptions(const std::string &prefix, ParseOptions *po) } else { other_parser_ = po; } - if (po != nullptr && po->prefix_ != "") { + if (po != nullptr && !po->prefix_.empty()) { prefix_ = po->prefix_ + std::string(".") + prefix; } else { prefix_ = prefix; @@ -179,10 +178,10 @@ void ParseOptions::DisableOption(const std::string &name) { string_map_.erase(name); } -int ParseOptions::NumArgs() const { return positional_args_.size(); } +int32_t ParseOptions::NumArgs() const { return positional_args_.size(); } -std::string ParseOptions::GetArg(int i) const { - if (i < 1 || i > static_cast(positional_args_.size())) { +std::string ParseOptions::GetArg(int32_t i) const { + if (i < 1 || i > static_cast(positional_args_.size())) { SHERPA_ONNX_LOGE("ParseOptions::GetArg, invalid index %d", i); exit(-1); } @@ -191,7 +190,7 @@ std::string ParseOptions::GetArg(int i) const { } // We currently do not support any other options. -enum ShellType { kBash = 0 }; +enum ShellType : std::uint8_t { kBash = 0 }; // This can be changed in the code if it ever does need to be changed (as it's // 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) { if (*c == '\0') { return true; // Must quote empty string } else { - const char *ok_chars[2]; + std::array ok_chars{}; // These seem not to be interpreted as long as there are no other "bad" // characters involved (e.g. "," would be interpreted as part of something @@ -229,7 +228,7 @@ static bool MustBeQuoted(const std::string &str, ShellType st) { // are OK. All others are forbidden (this is easier since the shell // interprets most non-alphanumeric characters). if (!isalnum(*c)) { - const char *d; + const char *d = nullptr; for (d = ok_chars[st]; *d != '\0'; ++d) { if (*c == *d) break; } @@ -269,22 +268,22 @@ static std::string QuoteAndEscape(const std::string &str, ShellType /*st*/) { escape_str = "\\\""; // should never be accessed. } - char buf[2]; + std::array buf{}; buf[1] = '\0'; buf[0] = quote_char; - std::string ans = buf; + std::string ans = buf.data(); const char *c = str.c_str(); for (; *c != '\0'; ++c) { if (*c == quote_char) { ans += escape_str; } else { buf[0] = *c; - ans += buf; + ans += buf.data(); } } buf[0] = quote_char; - ans += buf; + ans += buf.data(); return ans; } @@ -293,11 +292,11 @@ std::string ParseOptions::Escape(const std::string &str) { return MustBeQuoted(str, kShellType) ? QuoteAndEscape(str, kShellType) : str; } -int ParseOptions::Read(int argc, const char *const argv[]) { +int32_t ParseOptions::Read(int32_t argc, const char *const *argv) { argc_ = argc; argv_ = argv; std::string key, value; - int i; + int32_t i = 0; // first pass: look for config parameter, look for priority for (i = 1; i < argc; ++i) { @@ -306,13 +305,13 @@ int ParseOptions::Read(int argc, const char *const argv[]) { // a lone "--" marks the end of named options break; } - bool has_equal_sign; + bool has_equal_sign = false; SplitLongArg(argv[i], &key, &value, &has_equal_sign); NormalizeArgName(&key); Trim(&value); - if (key.compare("config") == 0) { + if (key == "config") { ReadConfigFile(value); - } else if (key.compare("help") == 0) { + } else if (key == "help") { PrintUsage(); exit(0); } @@ -330,7 +329,7 @@ int ParseOptions::Read(int argc, const char *const argv[]) { double_dash_seen = true; break; } - bool has_equal_sign; + bool has_equal_sign = false; SplitLongArg(argv[i], &key, &value, &has_equal_sign); NormalizeArgName(&key); Trim(&value); @@ -349,14 +348,14 @@ int ParseOptions::Read(int argc, const char *const argv[]) { if ((std::strcmp(argv[i], "--") == 0) && !double_dash_seen) { double_dash_seen = true; } else { - positional_args_.push_back(std::string(argv[i])); + positional_args_.emplace_back(argv[i]); } } // if the user did not suppress this with --print-args = false.... if (print_args_) { std::ostringstream strm; - for (int j = 0; j < argc; ++j) strm << Escape(argv[j]) << " "; + for (int32_t j = 0; j < argc; ++j) strm << Escape(argv[j]) << " "; strm << '\n'; SHERPA_ONNX_LOGE("%s", strm.str().c_str()); } @@ -368,14 +367,14 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { os << '\n' << usage_ << '\n'; // first we print application-specific options bool app_specific_header_printed = false; - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { - if (it->second.is_standard_ == false) { // application-specific option + for (const auto &it : doc_map_) { + if (it.second.is_standard_ == false) { // application-specific option if (app_specific_header_printed == false) { // header was not yet printed os << "Options:" << '\n'; app_specific_header_printed = true; } - os << " --" << std::setw(25) << std::left << it->second.name_ << " : " - << it->second.use_msg_ << '\n'; + os << " --" << std::setw(25) << std::left << it.second.name_ << " : " + << it.second.use_msg_ << '\n'; } } if (app_specific_header_printed == true) { @@ -384,17 +383,17 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { // then the standard options os << "Standard options:" << '\n'; - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { - if (it->second.is_standard_ == true) { // we have standard option - os << " --" << std::setw(25) << std::left << it->second.name_ << " : " - << it->second.use_msg_ << '\n'; + for (const auto &it : doc_map_) { + if (it.second.is_standard_ == true) { // we have standard option + os << " --" << std::setw(25) << std::left << it.second.name_ << " : " + << it.second.use_msg_ << '\n'; } } os << '\n'; if (print_command_line) { std::ostringstream strm; strm << "Command line was: "; - for (int j = 0; j < argc_; ++j) strm << Escape(argv_[j]) << " "; + for (int32_t j = 0; j < argc_; ++j) strm << Escape(argv_[j]) << " "; strm << '\n'; os << strm.str(); } @@ -405,9 +404,9 @@ void ParseOptions::PrintUsage(bool print_command_line /*=false*/) const { void ParseOptions::PrintConfig(std::ostream &os) const { os << '\n' << "[[ Configuration of UI-Registered options ]]" << '\n'; std::string key; - for (auto it = doc_map_.begin(); it != doc_map_.end(); ++it) { - key = it->first; - os << it->second.name_ << " = "; + for (const auto &it : doc_map_) { + key = it.first; + os << it.second.name_ << " = "; if (bool_map_.end() != bool_map_.find(key)) { os << (*bool_map_.at(key) ? "true" : "false"); } else if (int_map_.end() != int_map_.find(key)) { @@ -442,13 +441,13 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { while (std::getline(is, line)) { ++line_number; // trim out the comments - size_t pos; - if ((pos = line.find_first_of('#')) != std::string::npos) { + size_t pos = line.find_first_of('#'); + if (pos != std::string::npos) { line.erase(pos); } // skip empty lines Trim(&line); - if (line.length() == 0) continue; + if (line.empty()) continue; if (line.substr(0, 2) != "--") { SHERPA_ONNX_LOGE( @@ -461,7 +460,7 @@ void ParseOptions::ReadConfigFile(const std::string &filename) { } // parse option - bool has_equal_sign; + bool has_equal_sign = false; SplitLongArg(line, &key, &value, &has_equal_sign); NormalizeArgName(&key); Trim(&value); @@ -527,7 +526,7 @@ void ParseOptions::Trim(std::string *str) const { bool ParseOptions::SetOption(const std::string &key, const std::string &value, bool has_equal_sign) { if (bool_map_.end() != bool_map_.find(key)) { - if (has_equal_sign && value == "") { + if (has_equal_sign && value.empty()) { SHERPA_ONNX_LOGE("Invalid option --%s=", key.c_str()); exit(-1); } @@ -557,12 +556,10 @@ bool ParseOptions::ToBool(std::string str) const { std::transform(str.begin(), str.end(), str.begin(), ::tolower); // allow "" as a valid option for "true", so that --x is the same as --x=true - if ((str.compare("true") == 0) || (str.compare("t") == 0) || - (str.compare("1") == 0) || (str.compare("") == 0)) { + if (str == "true" || str == "t" || str == "1" || str.empty()) { return true; } - if ((str.compare("false") == 0) || (str.compare("f") == 0) || - (str.compare("0") == 0)) { + if (str == "false" || str == "f" || str == "0") { return false; } // if it is neither true nor false: @@ -593,7 +590,7 @@ uint32_t ParseOptions::ToUint(const std::string &str) const { } float ParseOptions::ToFloat(const std::string &str) const { - float ret; + float ret = 0; if (!ConvertStringToReal(str, &ret)) { SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); exit(-1); @@ -602,7 +599,7 @@ float ParseOptions::ToFloat(const std::string &str) const { } double ParseOptions::ToDouble(const std::string &str) const { - double ret; + double ret = 0; if (!ConvertStringToReal(str, &ret)) { SHERPA_ONNX_LOGE("Invalid floating-point option \"%s\"", str.c_str()); exit(-1); diff --git a/sherpa-onnx/csrc/piper-phonemize-lexicon.cc b/sherpa-onnx/csrc/piper-phonemize-lexicon.cc index ff5e9abe..aa7b9a2c 100644 --- a/sherpa-onnx/csrc/piper-phonemize-lexicon.cc +++ b/sherpa-onnx/csrc/piper-phonemize-lexicon.cc @@ -37,7 +37,7 @@ static std::unordered_map ReadTokens(std::istream &is) { std::string sym; std::u32string s; - int32_t id; + int32_t id = 0; while (std::getline(is, line)) { std::istringstream iss(line); iss >> sym; diff --git a/sherpa-onnx/csrc/resample.cc b/sherpa-onnx/csrc/resample.cc index f82c61a9..ad5ef2a6 100644 --- a/sherpa-onnx/csrc/resample.cc +++ b/sherpa-onnx/csrc/resample.cc @@ -24,10 +24,9 @@ #include "sherpa-onnx/csrc/resample.h" -#include -#include -#include - +#include +#include +#include #include #include @@ -54,8 +53,8 @@ I Gcd(I m, I n) { } // could use compile-time assertion // but involves messing with complex template stuff. - static_assert(std::is_integral::value, ""); - while (1) { + static_assert(std::is_integral_v); + while (true) { m %= n; if (m == 0) return (n > 0 ? n : -n); n %= m; @@ -139,10 +138,10 @@ void LinearResample::SetIndexesAndWeights() { in the header as h(t) = f(t)g(t), evaluated at t. */ float LinearResample::FilterFunc(float t) const { - float window, // raised-cosine (Hanning) window of width - // num_zeros_/2*filter_cutoff_ - filter; // sinc filter function - if (fabs(t) < num_zeros_ / (2.0 * filter_cutoff_)) + float window = 0, // raised-cosine (Hanning) window of width + // num_zeros_/2*filter_cutoff_ + filter = 0; // sinc filter function + if (std::fabs(t) < num_zeros_ / (2.0 * filter_cutoff_)) window = 0.5 * (1 + cos(M_2PI * filter_cutoff_ / num_zeros_ * t)); else window = 0.0; // outside support of window function @@ -172,15 +171,15 @@ void LinearResample::Resample(const float *input, int32_t input_dim, bool flush, // of it we are producing here. for (int64_t samp_out = output_sample_offset_; samp_out < tot_output_samp; samp_out++) { - int64_t first_samp_in; - int32_t samp_out_wrapped; + int64_t first_samp_in = 0; + int32_t samp_out_wrapped = 0; GetIndexes(samp_out, &first_samp_in, &samp_out_wrapped); const std::vector &weights = weights_[samp_out_wrapped]; // first_input_index is the first index into "input" that we have a weight // for. int32_t first_input_index = static_cast(first_samp_in - input_sample_offset_); - float this_output; + float this_output = 0; if (first_input_index >= 0 && first_input_index + static_cast(weights.size()) <= input_dim) { this_output = @@ -239,7 +238,7 @@ int64_t LinearResample::GetNumOutputSamples(int64_t input_num_samp, // largest integer in the interval [ 0, 2 - 0.9 ) are the same (both one). // So when we're subtracting the window-width we can ignore the fractional // part. - int32_t window_width_ticks = floor(window_width * tick_freq); + int32_t window_width_ticks = std::floor(window_width * tick_freq); // The time-period of the output that we can sample gets reduced // by the window-width (which is actually the distance from the // 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) { // that are "in the past" relative to the beginning of the latest // input... anyway, storing more remainder than needed is not harmful. int32_t max_remainder_needed = - ceil(samp_rate_in_ * num_zeros_ / filter_cutoff_); + std::ceil(samp_rate_in_ * num_zeros_ / filter_cutoff_); input_remainder_.resize(max_remainder_needed); for (int32_t index = -static_cast(input_remainder_.size()); index < 0; index++) { diff --git a/sherpa-onnx/csrc/resample.h b/sherpa-onnx/csrc/resample.h index 2006ae90..d6e22a19 100644 --- a/sherpa-onnx/csrc/resample.h +++ b/sherpa-onnx/csrc/resample.h @@ -130,11 +130,11 @@ class LinearResample { // the following variables keep track of where we are in a particular signal, // if it is being provided over multiple calls to Resample(). - int64_t input_sample_offset_; ///< The number of input samples we have - ///< already received for this signal - ///< (including anything in remainder_) - int64_t output_sample_offset_; ///< The number of samples we have already - ///< output for this signal. + int64_t input_sample_offset_ = 0; ///< The number of input samples we have + ///< already received for this signal + ///< (including anything in remainder_) + int64_t output_sample_offset_ = 0; ///< The number of samples we have already + ///< output for this signal. std::vector input_remainder_; ///< A small trailing part of the ///< previously seen input signal. }; diff --git a/sherpa-onnx/csrc/session.cc b/sherpa-onnx/csrc/session.cc index 431a6a76..0f6ed89d 100644 --- a/sherpa-onnx/csrc/session.cc +++ b/sherpa-onnx/csrc/session.cc @@ -21,14 +21,14 @@ namespace sherpa_onnx { - static void OrtStatusFailure(OrtStatus *status, const char *s) { - const auto &api = Ort::GetApi(); - const char *msg = api.GetErrorMessage(status); - SHERPA_ONNX_LOGE( + const auto &api = Ort::GetApi(); + const char *msg = api.GetErrorMessage(status); + SHERPA_ONNX_LOGE( "Failed to enable TensorRT : %s." - "Available providers: %s. Fallback to cuda", msg, s); - api.ReleaseStatus(status); + "Available providers: %s. Fallback to cuda", + msg, s); + api.ReleaseStatus(status); } static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, @@ -65,29 +65,28 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, } case Provider::kTRT: { struct TrtPairs { - const char* op_keys; - const char* op_values; + const char *op_keys; + const char *op_values; }; std::vector trt_options = { - {"device_id", "0"}, - {"trt_max_workspace_size", "2147483648"}, - {"trt_max_partition_iterations", "10"}, - {"trt_min_subgraph_size", "5"}, - {"trt_fp16_enable", "0"}, - {"trt_detailed_build_log", "0"}, - {"trt_engine_cache_enable", "1"}, - {"trt_engine_cache_path", "."}, - {"trt_timing_cache_enable", "1"}, - {"trt_timing_cache_path", "."} - }; + {"device_id", "0"}, + {"trt_max_workspace_size", "2147483648"}, + {"trt_max_partition_iterations", "10"}, + {"trt_min_subgraph_size", "5"}, + {"trt_fp16_enable", "0"}, + {"trt_detailed_build_log", "0"}, + {"trt_engine_cache_enable", "1"}, + {"trt_engine_cache_path", "."}, + {"trt_timing_cache_enable", "1"}, + {"trt_timing_cache_path", "."}}; // ToDo : Trt configs // "trt_int8_enable" // "trt_int8_use_native_calibration_table" // "trt_dump_subgraphs" - std::vector option_keys, option_values; - for (const TrtPairs& pair : trt_options) { + std::vector option_keys, option_values; + for (const TrtPairs &pair : trt_options) { option_keys.emplace_back(pair.op_keys); option_values.emplace_back(pair.op_values); } @@ -95,19 +94,23 @@ static Ort::SessionOptions GetSessionOptionsImpl(int32_t num_threads, std::vector available_providers = Ort::GetAvailableProviders(); if (std::find(available_providers.begin(), available_providers.end(), - "TensorrtExecutionProvider") != available_providers.end()) { - const auto& api = Ort::GetApi(); + "TensorrtExecutionProvider") != available_providers.end()) { + const auto &api = Ort::GetApi(); - OrtTensorRTProviderOptionsV2* tensorrt_options; - OrtStatus *statusC = api.CreateTensorRTProviderOptions( - &tensorrt_options); + OrtTensorRTProviderOptionsV2 *tensorrt_options = nullptr; + OrtStatus *statusC = + api.CreateTensorRTProviderOptions(&tensorrt_options); OrtStatus *statusU = api.UpdateTensorRTProviderOptions( - tensorrt_options, option_keys.data(), option_values.data(), - option_keys.size()); + tensorrt_options, option_keys.data(), option_values.data(), + option_keys.size()); sess_opts.AppendExecutionProvider_TensorRT_V2(*tensorrt_options); - if (statusC) { OrtStatusFailure(statusC, os.str().c_str()); } - if (statusU) { OrtStatusFailure(statusU, os.str().c_str()); } + if (statusC) { + OrtStatusFailure(statusC, os.str().c_str()); + } + if (statusU) { + OrtStatusFailure(statusU, os.str().c_str()); + } api.ReleaseTensorRTProviderOptions(tensorrt_options); } diff --git a/sherpa-onnx/csrc/silero-vad-model.cc b/sherpa-onnx/csrc/silero-vad-model.cc index a0c1e6c5..3c8233ea 100644 --- a/sherpa-onnx/csrc/silero-vad-model.cc +++ b/sherpa-onnx/csrc/silero-vad-model.cc @@ -20,11 +20,11 @@ class SileroVadModel::Impl { : config_(config), env_(ORT_LOGGING_LEVEL_ERROR), sess_opts_(GetSessionOptions(config)), - allocator_{} { + allocator_{}, + sample_rate_(config.sample_rate) { auto buf = ReadFile(config.silero_vad.model); Init(buf.data(), buf.size()); - sample_rate_ = config.sample_rate; if (sample_rate_ != 16000) { SHERPA_ONNX_LOGE("Expected sample rate 16000. Given: %d", config.sample_rate); diff --git a/sherpa-onnx/csrc/slice.cc b/sherpa-onnx/csrc/slice.cc index 6cc57758..5b7cf97d 100644 --- a/sherpa-onnx/csrc/slice.cc +++ b/sherpa-onnx/csrc/slice.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/slice.h" -#include - #include +#include #include namespace sherpa_onnx { diff --git a/sherpa-onnx/csrc/speaker-embedding-extractor-impl.cc b/sherpa-onnx/csrc/speaker-embedding-extractor-impl.cc index 53ff9940..fac2ea65 100644 --- a/sherpa-onnx/csrc/speaker-embedding-extractor-impl.cc +++ b/sherpa-onnx/csrc/speaker-embedding-extractor-impl.cc @@ -12,7 +12,7 @@ namespace sherpa_onnx { namespace { -enum class ModelType { +enum class ModelType : std::uint8_t { kWeSpeaker, k3dSpeaker, kNeMo, diff --git a/sherpa-onnx/csrc/speaker-embedding-manager.cc b/sherpa-onnx/csrc/speaker-embedding-manager.cc index 569b8d4f..6c90c195 100644 --- a/sherpa-onnx/csrc/speaker-embedding-manager.cc +++ b/sherpa-onnx/csrc/speaker-embedding-manager.cc @@ -122,7 +122,7 @@ class SpeakerEmbeddingManager::Impl { Eigen::VectorXf scores = embedding_matrix_ * v; - Eigen::VectorXf::Index max_index; + Eigen::VectorXf::Index max_index = 0; float max_score = scores.maxCoeff(&max_index); if (max_score < threshold) { return {}; @@ -178,11 +178,12 @@ class SpeakerEmbeddingManager::Impl { std::vector GetAllSpeakers() const { std::vector all_speakers; + all_speakers.reserve(name2row_.size()); for (const auto &p : name2row_) { all_speakers.push_back(p.first); } - std::stable_sort(all_speakers.begin(), all_speakers.end()); + std::sort(all_speakers.begin(), all_speakers.end()); return all_speakers; } diff --git a/sherpa-onnx/csrc/spoken-language-identification-impl.cc b/sherpa-onnx/csrc/spoken-language-identification-impl.cc index 016ac7e0..b8984a66 100644 --- a/sherpa-onnx/csrc/spoken-language-identification-impl.cc +++ b/sherpa-onnx/csrc/spoken-language-identification-impl.cc @@ -18,7 +18,7 @@ namespace sherpa_onnx { namespace { -enum class ModelType { +enum class ModelType : std::uint8_t { kWhisper, kUnknown, }; diff --git a/sherpa-onnx/csrc/stack.cc b/sherpa-onnx/csrc/stack.cc index 302ec733..1d0fac51 100644 --- a/sherpa-onnx/csrc/stack.cc +++ b/sherpa-onnx/csrc/stack.cc @@ -71,8 +71,8 @@ Ort::Value Stack(OrtAllocator *allocator, T *dst = ans.GetTensorMutableData(); for (int32_t i = 0; i != leading_size; ++i) { - for (int32_t n = 0; n != static_cast(values.size()); ++n) { - const T *src = values[n]->GetTensorData(); + for (auto value : values) { + const T *src = value->GetTensorData(); src += i * trailing_size; std::copy(src, src + trailing_size, dst); diff --git a/sherpa-onnx/csrc/symbol-table.cc b/sherpa-onnx/csrc/symbol-table.cc index 524b2689..8862972b 100644 --- a/sherpa-onnx/csrc/symbol-table.cc +++ b/sherpa-onnx/csrc/symbol-table.cc @@ -36,7 +36,7 @@ SymbolTable::SymbolTable(AAssetManager *mgr, const std::string &filename) { void SymbolTable::Init(std::istream &is) { std::string sym; - int32_t id; + int32_t id = 0; while (is >> sym >> id) { #if 0 // we disable the test here since for some multi-lingual BPE models diff --git a/sherpa-onnx/csrc/text-utils.cc b/sherpa-onnx/csrc/text-utils.cc index 04586dd8..3f12e146 100644 --- a/sherpa-onnx/csrc/text-utils.cc +++ b/sherpa-onnx/csrc/text-utils.cc @@ -5,9 +5,8 @@ #include "sherpa-onnx/csrc/text-utils.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/transducer-keyword-decoder.cc b/sherpa-onnx/csrc/transducer-keyword-decoder.cc index af78cb9c..b94b7ba1 100644 --- a/sherpa-onnx/csrc/transducer-keyword-decoder.cc +++ b/sherpa-onnx/csrc/transducer-keyword-decoder.cc @@ -151,7 +151,6 @@ void TransducerKeywordDecoder::Decode( if (matched) { float ys_prob = 0.0; - int32_t length = best_hyp.ys_probs.size(); for (int32_t i = 0; i < matched_state->level; ++i) { ys_prob += best_hyp.ys_probs[i]; } diff --git a/sherpa-onnx/csrc/transpose.cc b/sherpa-onnx/csrc/transpose.cc index 5ec32667..90a34599 100644 --- a/sherpa-onnx/csrc/transpose.cc +++ b/sherpa-onnx/csrc/transpose.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/transpose.h" -#include - #include +#include #include namespace sherpa_onnx { diff --git a/sherpa-onnx/csrc/unbind.cc b/sherpa-onnx/csrc/unbind.cc index ffe5a6c3..587e268c 100644 --- a/sherpa-onnx/csrc/unbind.cc +++ b/sherpa-onnx/csrc/unbind.cc @@ -4,9 +4,8 @@ #include "sherpa-onnx/csrc/unbind.h" -#include - #include +#include #include #include #include diff --git a/sherpa-onnx/csrc/utils.cc b/sherpa-onnx/csrc/utils.cc index 93de43e7..b5df9682 100644 --- a/sherpa-onnx/csrc/utils.cc +++ b/sherpa-onnx/csrc/utils.cc @@ -30,7 +30,6 @@ static bool EncodeBase(const std::vector &lines, std::vector tmp_thresholds; std::vector tmp_phrases; - std::string line; std::string word; bool has_scores = false; bool has_thresholds = false; @@ -72,6 +71,7 @@ static bool EncodeBase(const std::vector &lines, } } ids->push_back(std::move(tmp_ids)); + tmp_ids = {}; tmp_scores.push_back(score); tmp_phrases.push_back(phrase); tmp_thresholds.push_back(threshold); diff --git a/sherpa-onnx/csrc/wave-reader.cc b/sherpa-onnx/csrc/wave-reader.cc index b9d26279..1058db59 100644 --- a/sherpa-onnx/csrc/wave-reader.cc +++ b/sherpa-onnx/csrc/wave-reader.cc @@ -100,13 +100,13 @@ struct WaveHeader { int32_t subchunk2_id; // a tag of this chunk int32_t subchunk2_size; // size of subchunk2 }; -static_assert(sizeof(WaveHeader) == 44, ""); +static_assert(sizeof(WaveHeader) == 44); // Read a wave file of mono-channel. // Return its samples normalized to the range [-1, 1). std::vector ReadWaveImpl(std::istream &is, int32_t *sampling_rate, bool *is_ok) { - WaveHeader header; + WaveHeader header{}; is.read(reinterpret_cast(&header), sizeof(header)); if (!is) { *is_ok = false; diff --git a/sherpa-onnx/csrc/wave-writer.cc b/sherpa-onnx/csrc/wave-writer.cc index f20af4b1..069b6276 100644 --- a/sherpa-onnx/csrc/wave-writer.cc +++ b/sherpa-onnx/csrc/wave-writer.cc @@ -37,7 +37,7 @@ struct WaveHeader { bool WriteWave(const std::string &filename, int32_t sampling_rate, const float *samples, int32_t n) { - WaveHeader header; + WaveHeader header{}; header.chunk_id = 0x46464952; // FFIR header.format = 0x45564157; // EVAW header.subchunk1_id = 0x20746d66; // "fmt "