From 8fc89c86f47668bfd151a8b05cc63a39d173c4c7 Mon Sep 17 00:00:00 2001 From: cdliang11 <1404056823@qq.com> Date: Wed, 4 Sep 2024 23:47:30 +0800 Subject: [PATCH] [wesep] remove useless code --- wesep/runtime/.gitignore | 2 - wesep/runtime/CMakeLists.txt | 27 --- wesep/runtime/README.md | 23 -- wesep/runtime/bin/CMakeLists.txt | 2 - wesep/runtime/bin/separate_main.cc | 101 -------- wesep/runtime/cmake/gflags.cmake | 6 - wesep/runtime/cmake/glog.cmake | 6 - wesep/runtime/cmake/libtorch.cmake | 20 -- wesep/runtime/frontend/CMakeLists.txt | 5 - wesep/runtime/frontend/fbank.h | 222 ------------------ wesep/runtime/frontend/feature_pipeline.cc | 113 --------- wesep/runtime/frontend/feature_pipeline.h | 118 ---------- wesep/runtime/frontend/fft.cc | 121 ---------- wesep/runtime/frontend/fft.h | 25 -- wesep/runtime/frontend/wav.h | 257 --------------------- wesep/runtime/separate/CMakeLists.txt | 2 - wesep/runtime/separate/separate_engine.cc | 106 --------- wesep/runtime/separate/separate_engine.h | 38 --- wesep/runtime/utils/CMakeLists.txt | 4 - wesep/runtime/utils/blocking_queue.h | 98 -------- wesep/runtime/utils/timer.h | 39 ---- wesep/runtime/utils/utils.cc | 70 ------ wesep/runtime/utils/utils.h | 39 ---- 23 files changed, 1444 deletions(-) delete mode 100644 wesep/runtime/.gitignore delete mode 100644 wesep/runtime/CMakeLists.txt delete mode 100644 wesep/runtime/README.md delete mode 100644 wesep/runtime/bin/CMakeLists.txt delete mode 100644 wesep/runtime/bin/separate_main.cc delete mode 100644 wesep/runtime/cmake/gflags.cmake delete mode 100644 wesep/runtime/cmake/glog.cmake delete mode 100644 wesep/runtime/cmake/libtorch.cmake delete mode 100644 wesep/runtime/frontend/CMakeLists.txt delete mode 100644 wesep/runtime/frontend/fbank.h delete mode 100644 wesep/runtime/frontend/feature_pipeline.cc delete mode 100644 wesep/runtime/frontend/feature_pipeline.h delete mode 100644 wesep/runtime/frontend/fft.cc delete mode 100644 wesep/runtime/frontend/fft.h delete mode 100644 wesep/runtime/frontend/wav.h delete mode 100644 wesep/runtime/separate/CMakeLists.txt delete mode 100644 wesep/runtime/separate/separate_engine.cc delete mode 100644 wesep/runtime/separate/separate_engine.h delete mode 100644 wesep/runtime/utils/CMakeLists.txt delete mode 100644 wesep/runtime/utils/blocking_queue.h delete mode 100644 wesep/runtime/utils/timer.h delete mode 100644 wesep/runtime/utils/utils.cc delete mode 100644 wesep/runtime/utils/utils.h diff --git a/wesep/runtime/.gitignore b/wesep/runtime/.gitignore deleted file mode 100644 index 855f8b3..0000000 --- a/wesep/runtime/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -fc_base -build* diff --git a/wesep/runtime/CMakeLists.txt b/wesep/runtime/CMakeLists.txt deleted file mode 100644 index c8a1d56..0000000 --- a/wesep/runtime/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ - -cmake_minimum_required(VERSION 3.14) -project(wesep VERSION 0.1) - -option(CXX11_ABI "whether to use CXX11_ABI libtorch" OFF) - -set(CMAKE_VERBOSE_MAKEFILE OFF) - -include(FetchContent) -set(FETCHCONTENT_QUIET OFF) -get_filename_component(fc_base "fc_base" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") -set(FETCHCONTENT_BASE_DIR ${fc_base}) - -list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fPIC") - -include(libtorch) -include(glog) -include(gflags) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - -# build all libraries -add_subdirectory(utils) -add_subdirectory(frontend) -add_subdirectory(separate) -add_subdirectory(bin) diff --git a/wesep/runtime/README.md b/wesep/runtime/README.md deleted file mode 100644 index 19bceb7..0000000 --- a/wesep/runtime/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Libtorch backend on wesep - -* Build. The build requires cmake 3.14 or above, and gcc/g++ 5.4 or above. - -``` sh -mkdir build && cd build -cmake .. -cmake --build . -``` - -* Testing. - -1. the RTF(real time factor) is shown in the console, and outputs will be written to the wav file. - -``` sh -export GLOG_logtostderr=1 -export GLOG_v=2 - -./build/bin/separate_main \ - --wav_scp $wav_scp \ - --model /path/to/model.zip \ - --output_dir /output/dir/ -``` diff --git a/wesep/runtime/bin/CMakeLists.txt b/wesep/runtime/bin/CMakeLists.txt deleted file mode 100644 index dcc8f5e..0000000 --- a/wesep/runtime/bin/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(separate_main separate_main.cc) -target_link_libraries(separate_main PUBLIC frontend separate) diff --git a/wesep/runtime/bin/separate_main.cc b/wesep/runtime/bin/separate_main.cc deleted file mode 100644 index 5fc3b39..0000000 --- a/wesep/runtime/bin/separate_main.cc +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include - -#include "gflags/gflags.h" -#include "glog/logging.h" - -#include "frontend/wav.h" -#include "separate/separate_engine.h" -#include "utils/timer.h" -#include "utils/utils.h" - -DEFINE_string(wav_path, "", "the path of mixing audio."); -DEFINE_string(spk1_emb, "", "the emb of spk1."); -DEFINE_string(spk2_emb, "", "the emb of spk2."); -DEFINE_string(wav_scp, "", "input wav scp."); -DEFINE_string(model, "", "the path of wesep model."); -DEFINE_string(output_dir, "", "output path."); -DEFINE_int32(sample_rate, 16000, "sample rate"); -DEFINE_int32(feat_dim, 80, "fbank feature dimension."); - -int main(int argc, char* argv[]) { - gflags::ParseCommandLineFlags(&argc, &argv, false); - google::InitGoogleLogging(argv[0]); - - std::vector> waves; - if (!FLAGS_wav_path.empty() && !FLAGS_spk1_emb.empty() && - !FLAGS_spk2_emb.empty()) { - waves.push_back(std::vector( - {"test", FLAGS_wav_path, FLAGS_spk1_emb, FLAGS_spk2_emb})); - } else { - std::ifstream wav_scp(FLAGS_wav_scp); - std::string line; - while (getline(wav_scp, line)) { - std::vector strs; - wesep::SplitString(line, &strs); - CHECK_EQ(strs.size(), 4); - waves.push_back( - std::vector({strs[0], strs[1], strs[2], strs[3]})); - } - if (waves.empty()) { - LOG(FATAL) << "Please provide non-empty wav scp."; - } - } - - if (FLAGS_output_dir.empty()) { - LOG(FATAL) << "Invalid output path."; - } - - int g_total_waves_dur = 0; - int g_total_process_time = 0; - - auto model = std::make_shared( - FLAGS_model, FLAGS_feat_dim, FLAGS_sample_rate); - - for (auto wav : waves) { - // mix wav - wenet::WavReader wav_reader(wav[1]); - CHECK_EQ(wav_reader.sample_rate(), 16000); - int16_t* mix_wav_data = const_cast(wav_reader.data()); - - int wave_dur = - static_cast(static_cast(wav_reader.num_sample()) / - wav_reader.sample_rate() * 1000); - - // spk1 - wenet::WavReader spk1_reader(wav[2]); - CHECK_EQ(spk1_reader.sample_rate(), 16000); - int16_t* spk1_data = const_cast(spk1_reader.data()); - - // spk2 - wenet::WavReader spk2_reader(wav[3]); - CHECK_EQ(spk2_reader.sample_rate(), 16000); - int16_t* spk2_data = const_cast(spk2_reader.data()); - - // forward - std::vector> outputs; - int process_time = 0; - wenet::Timer timer; - model->ForwardFunc( - std::vector(mix_wav_data, - mix_wav_data + wav_reader.num_sample()), - spk1_data, spk2_data, - std::min(spk1_reader.num_sample(), spk2_reader.num_sample()), &outputs); - process_time = timer.Elapsed(); - LOG(INFO) << "process: " << wav[0] - << " RTF: " << static_cast(process_time) / wave_dur; - // 保存音频 - wenet::WriteWavFile(outputs[0].data(), outputs[0].size(), 16000, - FLAGS_output_dir + "/" + wav[0] + "-spk1.wav"); - wenet::WriteWavFile(outputs[1].data(), outputs[1].size(), 16000, - FLAGS_output_dir + "/" + wav[0] + "-spk2.wav"); - g_total_process_time += process_time; - g_total_waves_dur += wave_dur; - } - LOG(INFO) << "Total: process " << g_total_waves_dur << "ms audio taken " - << g_total_process_time << "ms."; - LOG(INFO) << "RTF: " << std::setprecision(4) - << static_cast(g_total_process_time) / g_total_waves_dur; - return 0; -} diff --git a/wesep/runtime/cmake/gflags.cmake b/wesep/runtime/cmake/gflags.cmake deleted file mode 100644 index 278d9d7..0000000 --- a/wesep/runtime/cmake/gflags.cmake +++ /dev/null @@ -1,6 +0,0 @@ -FetchContent_Declare(gflags - URL https://github.com/gflags/gflags/archive/v2.2.2.zip - URL_HASH SHA256=19713a36c9f32b33df59d1c79b4958434cb005b5b47dc5400a7a4b078111d9b5 -) -FetchContent_MakeAvailable(gflags) -include_directories(${gflags_BINARY_DIR}/include) diff --git a/wesep/runtime/cmake/glog.cmake b/wesep/runtime/cmake/glog.cmake deleted file mode 100644 index 623e3ee..0000000 --- a/wesep/runtime/cmake/glog.cmake +++ /dev/null @@ -1,6 +0,0 @@ -FetchContent_Declare(glog - URL https://github.com/google/glog/archive/v0.4.0.zip - URL_HASH SHA256=9e1b54eb2782f53cd8af107ecf08d2ab64b8d0dc2b7f5594472f3bd63ca85cdc -) -FetchContent_MakeAvailable(glog) -include_directories(${glog_SOURCE_DIR}/src ${glog_BINARY_DIR}) diff --git a/wesep/runtime/cmake/libtorch.cmake b/wesep/runtime/cmake/libtorch.cmake deleted file mode 100644 index 949d2a6..0000000 --- a/wesep/runtime/cmake/libtorch.cmake +++ /dev/null @@ -1,20 +0,0 @@ -if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - if(CXX11_ABI) - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.13.0%2Bcpu.zip") - set(URL_HASH "SHA256=d52f63577a07adb0bfd6d77c90f7da21896e94f71eb7dcd55ed7835ccb3b2b59") - else() - set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.13.0%2Bcpu.zip") - set(URL_HASH "SHA256=bee1b7be308792aa60fc95a4f5274d9658cb7248002d0e333d49eb81ec88430c") - endif() -else() - message(FATAL_ERROR "Unsported System '${CMAKE_SYSTEM_NAME}' (expected 'Linux')") -endif() - -FetchContent_Declare(libtorch - URL ${LIBTORCH_URL} - URL_HASH ${URL_HASH} -) -FetchContent_MakeAvailable(libtorch) -find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH) -include_directories(${TORCH_INCLUDE_DIRS}) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG") diff --git a/wesep/runtime/frontend/CMakeLists.txt b/wesep/runtime/frontend/CMakeLists.txt deleted file mode 100644 index bad571a..0000000 --- a/wesep/runtime/frontend/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_library(frontend STATIC - feature_pipeline.cc - fft.cc -) -target_link_libraries(frontend PUBLIC utils) diff --git a/wesep/runtime/frontend/fbank.h b/wesep/runtime/frontend/fbank.h deleted file mode 100644 index 05ac5c6..0000000 --- a/wesep/runtime/frontend/fbank.h +++ /dev/null @@ -1,222 +0,0 @@ -// Copyright (c) 2017 Personal (Binbin Zhang) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef FRONTEND_FBANK_H_ -#define FRONTEND_FBANK_H_ - -#include -#include -#include -#include -#include - -#include "frontend/fft.h" -#include "glog/logging.h" - -namespace wenet { - -// This code is based on kaldi Fbank implentation, please see -// https://github.com/kaldi-asr/kaldi/blob/master/src/feat/feature-fbank.cc -class Fbank { - public: - Fbank(int num_bins, int sample_rate, int frame_length, int frame_shift) - : num_bins_(num_bins), - sample_rate_(sample_rate), - frame_length_(frame_length), - frame_shift_(frame_shift), - use_log_(true), - remove_dc_offset_(true), - generator_(0), - distribution_(0, 1.0), - dither_(0.0) { - fft_points_ = UpperPowerOfTwo(frame_length_); - // generate bit reversal table and trigonometric function table - const int fft_points_4 = fft_points_ / 4; - bitrev_.resize(fft_points_); - sintbl_.resize(fft_points_ + fft_points_4); - make_sintbl(fft_points_, sintbl_.data()); - make_bitrev(fft_points_, bitrev_.data()); - - int num_fft_bins = fft_points_ / 2; - float fft_bin_width = static_cast(sample_rate_) / fft_points_; - int low_freq = 20, high_freq = sample_rate_ / 2; - float mel_low_freq = MelScale(low_freq); - float mel_high_freq = MelScale(high_freq); - float mel_freq_delta = (mel_high_freq - mel_low_freq) / (num_bins + 1); - bins_.resize(num_bins_); - center_freqs_.resize(num_bins_); - for (int bin = 0; bin < num_bins; ++bin) { - float left_mel = mel_low_freq + bin * mel_freq_delta, - center_mel = mel_low_freq + (bin + 1) * mel_freq_delta, - right_mel = mel_low_freq + (bin + 2) * mel_freq_delta; - center_freqs_[bin] = InverseMelScale(center_mel); - std::vector this_bin(num_fft_bins); - int first_index = -1, last_index = -1; - for (int i = 0; i < num_fft_bins; ++i) { - float freq = (fft_bin_width * i); // Center frequency of this fft - // bin. - float mel = MelScale(freq); - if (mel > left_mel && mel < right_mel) { - float weight; - if (mel <= center_mel) - weight = (mel - left_mel) / (center_mel - left_mel); - else - weight = (right_mel - mel) / (right_mel - center_mel); - this_bin[i] = weight; - if (first_index == -1) first_index = i; - last_index = i; - } - } - CHECK(first_index != -1 && last_index >= first_index); - bins_[bin].first = first_index; - int size = last_index + 1 - first_index; - bins_[bin].second.resize(size); - for (int i = 0; i < size; ++i) { - bins_[bin].second[i] = this_bin[first_index + i]; - } - } - - // NOTE(cdliang): add hamming window - hamming_window_.resize(frame_length_); - double a = M_2PI / (frame_length - 1); - for (int i = 0; i < frame_length; i++) { - double i_fl = static_cast(i); - hamming_window_[i] = 0.54 - 0.46 * cos(a * i_fl); - } - } - - void set_use_log(bool use_log) { use_log_ = use_log; } - - void set_remove_dc_offset(bool remove_dc_offset) { - remove_dc_offset_ = remove_dc_offset; - } - - void set_dither(float dither) { dither_ = dither; } - - int num_bins() const { return num_bins_; } - - static inline float InverseMelScale(float mel_freq) { - return 700.0f * (expf(mel_freq / 1127.0f) - 1.0f); - } - - static inline float MelScale(float freq) { - return 1127.0f * logf(1.0f + freq / 700.0f); - } - - static int UpperPowerOfTwo(int n) { - return static_cast(pow(2, ceil(log(n) / log(2)))); - } - - // preemphasis - void PreEmphasis(float coeff, std::vector* data) const { - if (coeff == 0.0) return; - for (int i = data->size() - 1; i > 0; i--) - (*data)[i] -= coeff * (*data)[i - 1]; - (*data)[0] -= coeff * (*data)[0]; - } - - // add hamming window - void Hamming(std::vector* data) const { - CHECK_GE(data->size(), hamming_window_.size()); - for (size_t i = 0; i < hamming_window_.size(); ++i) { - (*data)[i] *= hamming_window_[i]; - } - } - - // Compute fbank feat, return num frames - int Compute(const std::vector& wave, - std::vector>* feat) { - int num_samples = wave.size(); - if (num_samples < frame_length_) return 0; - int num_frames = 1 + ((num_samples - frame_length_) / frame_shift_); - feat->resize(num_frames); - std::vector fft_real(fft_points_, 0), fft_img(fft_points_, 0); - std::vector power(fft_points_ / 2); - for (int i = 0; i < num_frames; ++i) { - std::vector data(wave.data() + i * frame_shift_, - wave.data() + i * frame_shift_ + frame_length_); - // optional add noise - if (dither_ != 0.0) { - for (size_t j = 0; j < data.size(); ++j) - data[j] += dither_ * distribution_(generator_); - } - // optinal remove dc offset - if (remove_dc_offset_) { - float mean = 0.0; - for (size_t j = 0; j < data.size(); ++j) mean += data[j]; - mean /= data.size(); - for (size_t j = 0; j < data.size(); ++j) data[j] -= mean; - } - - PreEmphasis(0.97, &data); - // Povey(&data); - Hamming(&data); - // copy data to fft_real - memset(fft_img.data(), 0, sizeof(float) * fft_points_); - memset(fft_real.data() + frame_length_, 0, - sizeof(float) * (fft_points_ - frame_length_)); - memcpy(fft_real.data(), data.data(), sizeof(float) * frame_length_); - fft(bitrev_.data(), sintbl_.data(), fft_real.data(), fft_img.data(), - fft_points_); - // power - for (int j = 0; j < fft_points_ / 2; ++j) { - power[j] = fft_real[j] * fft_real[j] + fft_img[j] * fft_img[j]; - } - - (*feat)[i].resize(num_bins_); - // cepstral coefficients, triangle filter array - for (int j = 0; j < num_bins_; ++j) { - float mel_energy = 0.0; - int s = bins_[j].first; - for (size_t k = 0; k < bins_[j].second.size(); ++k) { - mel_energy += bins_[j].second[k] * power[s + k]; - } - // optional use log - if (use_log_) { - if (mel_energy < std::numeric_limits::epsilon()) - mel_energy = std::numeric_limits::epsilon(); - mel_energy = logf(mel_energy); - } - - (*feat)[i][j] = mel_energy; - // printf("%f ", mel_energy); - } - // printf("\n"); - } - return num_frames; - } - - private: - int num_bins_; - int sample_rate_; - int frame_length_, frame_shift_; - int fft_points_; - bool use_log_; - bool remove_dc_offset_; - std::vector center_freqs_; - std::vector>> bins_; - std::vector hamming_window_; - std::default_random_engine generator_; - std::normal_distribution distribution_; - float dither_; - - // bit reversal table - std::vector bitrev_; - // trigonometric function table - std::vector sintbl_; -}; - -} // namespace wenet - -#endif // FRONTEND_FBANK_H_ diff --git a/wesep/runtime/frontend/feature_pipeline.cc b/wesep/runtime/frontend/feature_pipeline.cc deleted file mode 100644 index c59d6f7..0000000 --- a/wesep/runtime/frontend/feature_pipeline.cc +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright (c) 2017 Personal (Binbin Zhang) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "frontend/feature_pipeline.h" - -#include -#include - -namespace wenet { - -FeaturePipeline::FeaturePipeline(const FeaturePipelineConfig& config) - : config_(config), - feature_dim_(config.num_bins), - fbank_(config.num_bins, config.sample_rate, config.frame_length, - config.frame_shift), - num_frames_(0), - input_finished_(false) {} - -void FeaturePipeline::AcceptWaveform(const std::vector& wav) { - std::vector> feats; - std::vector waves; - waves.insert(waves.end(), remained_wav_.begin(), remained_wav_.end()); - waves.insert(waves.end(), wav.begin(), wav.end()); - int num_frames = fbank_.Compute(waves, &feats); - for (size_t i = 0; i < feats.size(); ++i) { - feature_queue_.Push(std::move(feats[i])); - } - num_frames_ += num_frames; - - int left_samples = waves.size() - config_.frame_shift * num_frames; - remained_wav_.resize(left_samples); - std::copy(waves.begin() + config_.frame_shift * num_frames, waves.end(), - remained_wav_.begin()); - // We are still adding wave, notify input is not finished - finish_condition_.notify_one(); -} - -void FeaturePipeline::AcceptWaveform(const std::vector& wav) { - std::vector float_wav(wav.size()); - for (size_t i = 0; i < wav.size(); i++) { - float_wav[i] = static_cast(wav[i]); - } - this->AcceptWaveform(float_wav); -} - -void FeaturePipeline::set_input_finished() { - CHECK(!input_finished_); - { - std::lock_guard lock(mutex_); - input_finished_ = true; - } - finish_condition_.notify_one(); -} - -bool FeaturePipeline::ReadOne(std::vector* feat) { - if (!feature_queue_.Empty()) { - *feat = std::move(feature_queue_.Pop()); - return true; - } else { - std::unique_lock lock(mutex_); - while (!input_finished_) { - // This will release the lock and wait for notify_one() - // from AcceptWaveform() or set_input_finished() - finish_condition_.wait(lock); - if (!feature_queue_.Empty()) { - *feat = std::move(feature_queue_.Pop()); - return true; - } - } - CHECK(input_finished_); - // Double check queue.empty, see issue#893 for detailed discussions. - if (!feature_queue_.Empty()) { - *feat = std::move(feature_queue_.Pop()); - return true; - } else { - return false; - } - } -} - -bool FeaturePipeline::Read(int num_frames, - std::vector>* feats) { - feats->clear(); - std::vector feat; - while (feats->size() < num_frames) { - if (ReadOne(&feat)) { - feats->push_back(std::move(feat)); - } else { - return false; - } - } - return true; -} - -void FeaturePipeline::Reset() { - input_finished_ = false; - num_frames_ = 0; - remained_wav_.clear(); - feature_queue_.Clear(); -} - -} // namespace wenet diff --git a/wesep/runtime/frontend/feature_pipeline.h b/wesep/runtime/frontend/feature_pipeline.h deleted file mode 100644 index a020c2a..0000000 --- a/wesep/runtime/frontend/feature_pipeline.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright (c) 2017 Personal (Binbin Zhang) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef FRONTEND_FEATURE_PIPELINE_H_ -#define FRONTEND_FEATURE_PIPELINE_H_ - -#include -#include -#include -#include - -#include "frontend/fbank.h" -#include "glog/logging.h" -#include "utils/blocking_queue.h" - -namespace wenet { - -struct FeaturePipelineConfig { - int num_bins; - int sample_rate; - int frame_length; - int frame_shift; - FeaturePipelineConfig(int num_bins, int sample_rate) - : num_bins(num_bins), // 80 dim fbank - sample_rate(sample_rate) { // 16k sample rate - frame_length = sample_rate / 1000 * 25; // frame length 25ms - frame_shift = sample_rate / 1000 * 10; // frame shift 10ms - } - - void Info() const { - LOG(INFO) << "feature pipeline config" - << " num_bins " << num_bins << " frame_length " << frame_length - << " frame_shift " << frame_shift; - } -}; - -// Typically, FeaturePipeline is used in two threads: one thread A calls -// AcceptWaveform() to add raw wav data and set_input_finished() to notice -// the end of input wav, another thread B (decoder thread) calls Read() to -// consume features.So a BlockingQueue is used to make this class thread safe. - -// The Read() is designed as a blocking method when there is no feature -// in feature_queue_ and the input is not finished. - -class FeaturePipeline { - public: - explicit FeaturePipeline(const FeaturePipelineConfig& config); - - // The feature extraction is done in AcceptWaveform(). - void AcceptWaveform(const std::vector& wav); - void AcceptWaveform(const std::vector& wav); - - // Current extracted frames number. - int num_frames() const { return num_frames_; } - int feature_dim() const { return feature_dim_; } - const FeaturePipelineConfig& config() const { return config_; } - - // The caller should call this method when speech input is end. - // Never call AcceptWaveform() after calling set_input_finished() ! - void set_input_finished(); - bool input_finished() const { return input_finished_; } - - // Return False if input is finished and no feature could be read. - // Return True if a feature is read. - // This function is a blocking method. It will block the thread when - // there is no feature in feature_queue_ and the input is not finished. - bool ReadOne(std::vector* feat); - - // Read #num_frames frame features. - // Return False if less then #num_frames features are read and the - // input is finished. - // Return True if #num_frames features are read. - // This function is a blocking method when there is no feature - // in feature_queue_ and the input is not finished. - bool Read(int num_frames, std::vector>* feats); - - void Reset(); - bool IsLastFrame(int frame) const { - return input_finished_ && (frame == num_frames_ - 1); - } - - int NumQueuedFrames() const { return feature_queue_.Size(); } - - private: - const FeaturePipelineConfig& config_; - int feature_dim_; - Fbank fbank_; - - BlockingQueue> feature_queue_; - int num_frames_; - bool input_finished_; - - // The feature extraction is done in AcceptWaveform(). - // This wavefrom sample points are consumed by frame size. - // The residual wavefrom sample points after framing are - // kept to be used in next AcceptWaveform() calling. - std::vector remained_wav_; - - // Used to block the Read when there is no feature in feature_queue_ - // and the input is not finished. - mutable std::mutex mutex_; - std::condition_variable finish_condition_; -}; - -} // namespace wenet - -#endif // FRONTEND_FEATURE_PIPELINE_H_ diff --git a/wesep/runtime/frontend/fft.cc b/wesep/runtime/frontend/fft.cc deleted file mode 100644 index d293203..0000000 --- a/wesep/runtime/frontend/fft.cc +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright (c) 2016 HR - -#include -#include -#include - -#include "frontend/fft.h" - -namespace wenet { - -void make_sintbl(int n, float* sintbl) { - int i, n2, n4, n8; - float c, s, dc, ds, t; - - n2 = n / 2; - n4 = n / 4; - n8 = n / 8; - t = sin(M_PI / n); - dc = 2 * t * t; - ds = sqrt(dc * (2 - dc)); - t = 2 * dc; - c = sintbl[n4] = 1; - s = sintbl[0] = 0; - for (i = 1; i < n8; ++i) { - c -= dc; - dc += t * c; - s += ds; - ds -= t * s; - sintbl[i] = s; - sintbl[n4 - i] = c; - } - if (n8 != 0) sintbl[n8] = sqrt(0.5); - for (i = 0; i < n4; ++i) sintbl[n2 - i] = sintbl[i]; - for (i = 0; i < n2 + n4; ++i) sintbl[i + n2] = -sintbl[i]; -} - -void make_bitrev(int n, int* bitrev) { - int i, j, k, n2; - - n2 = n / 2; - i = j = 0; - for (;;) { - bitrev[i] = j; - if (++i >= n) break; - k = n2; - while (k <= j) { - j -= k; - k /= 2; - } - j += k; - } -} - -// bitrev: bit reversal table -// sintbl: trigonometric function table -// x:real part -// y:image part -// n: fft length -int fft(const int* bitrev, const float* sintbl, float* x, float* y, int n) { - int i, j, k, ik, h, d, k2, n4, inverse; - float t, s, c, dx, dy; - - /* preparation */ - if (n < 0) { - n = -n; - inverse = 1; /* inverse transform */ - } else { - inverse = 0; - } - n4 = n / 4; - if (n == 0) { - return 0; - } - - /* bit reversal */ - for (i = 0; i < n; ++i) { - j = bitrev[i]; - if (i < j) { - t = x[i]; - x[i] = x[j]; - x[j] = t; - t = y[i]; - y[i] = y[j]; - y[j] = t; - } - } - - /* transformation */ - for (k = 1; k < n; k = k2) { - h = 0; - k2 = k + k; - d = n / k2; - for (j = 0; j < k; ++j) { - c = sintbl[h + n4]; - if (inverse) - s = -sintbl[h]; - else - s = sintbl[h]; - for (i = j; i < n; i += k2) { - ik = i + k; - dx = s * y[ik] + c * x[ik]; - dy = c * y[ik] - s * x[ik]; - x[ik] = x[i] - dx; - x[i] += dx; - y[ik] = y[i] - dy; - y[i] += dy; - } - h += d; - } - } - if (inverse) { - /* divide by n in case of the inverse transformation */ - for (i = 0; i < n; ++i) { - x[i] /= n; - y[i] /= n; - } - } - return 0; /* finished successfully */ -} - -} // namespace wenet diff --git a/wesep/runtime/frontend/fft.h b/wesep/runtime/frontend/fft.h deleted file mode 100644 index 5015311..0000000 --- a/wesep/runtime/frontend/fft.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2016 HR - -#ifndef FRONTEND_FFT_H_ -#define FRONTEND_FFT_H_ - -#ifndef M_PI -#define M_PI 3.1415926535897932384626433832795 -#endif -#ifndef M_2PI -#define M_2PI 6.283185307179586476925286766559005 -#endif - -namespace wenet { - -// Fast Fourier Transform - -void make_sintbl(int n, float* sintbl); - -void make_bitrev(int n, int* bitrev); - -int fft(const int* bitrev, const float* sintbl, float* x, float* y, int n); - -} // namespace wenet - -#endif // FRONTEND_FFT_H_ diff --git a/wesep/runtime/frontend/wav.h b/wesep/runtime/frontend/wav.h deleted file mode 100644 index c1a5485..0000000 --- a/wesep/runtime/frontend/wav.h +++ /dev/null @@ -1,257 +0,0 @@ -// Copyright (c) 2016 Personal (Binbin Zhang) -// Created on 2016-08-15 -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef FRONTEND_WAV_H_ -#define FRONTEND_WAV_H_ - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "gflags/gflags.h" -#include "glog/logging.h" - -DEFINE_int32(pcm_sample_rate, 16000, "pcm data sample rate"); - -namespace wenet { - -class AudioReader { - public: - AudioReader() {} - explicit AudioReader(const std::string& filename) {} - virtual ~AudioReader() {} - - virtual int num_channel() const = 0; - virtual int sample_rate() const = 0; - virtual int bits_per_sample() const = 0; - virtual int num_sample() const = 0; - virtual const int16_t* data() const = 0; -}; - -struct WavHeader { - char riff[4]; // "riff" - unsigned int size; - char wav[4]; // "WAVE" - char fmt[4]; // "fmt " - unsigned int fmt_size; - uint16_t format; - uint16_t channels; - unsigned int sample_rate; - unsigned int bytes_per_second; - uint16_t block_size; - uint16_t bit; - char data[4]; // "data" - unsigned int data_size; -}; - -class WavReader : public AudioReader { - public: - WavReader() {} - explicit WavReader(const std::string& filename) { Open(filename); } - - bool Open(const std::string& filename) { - FILE* fp = fopen(filename.c_str(), "rb"); - if (NULL == fp) { - LOG(WARNING) << "Error in read " << filename; - return false; - } - - WavHeader header; - fread(&header, 1, sizeof(header), fp); - if (header.fmt_size < 16) { - fprintf(stderr, - "WaveData: expect PCM format data " - "to have fmt chunk of at least size 16.\n"); - return false; - } else if (header.fmt_size > 16) { - int offset = 44 - 8 + header.fmt_size - 16; - fseek(fp, offset, SEEK_SET); - fread(header.data, 8, sizeof(char), fp); - } - // check "riff" "WAVE" "fmt " "data" - - // Skip any subchunks between "fmt" and "data". Usually there will - // be a single "fact" subchunk, but on Windows there can also be a - // "list" subchunk. - while (0 != strncmp(header.data, "data", 4)) { - // We will just ignore the data in these chunks. - fseek(fp, header.data_size, SEEK_CUR); - // read next subchunk - fread(header.data, 8, sizeof(char), fp); - } - - num_channel_ = header.channels; - sample_rate_ = header.sample_rate; - bits_per_sample_ = header.bit; - int num_data = header.data_size / (bits_per_sample_ / 8); - data_.resize(num_data); - int num_read = fread(&data_[0], 1, header.data_size, fp); - if (num_read < header.data_size) { - // If the header size is wrong, adjust - header.data_size = num_read; - num_data = header.data_size / (bits_per_sample_ / 8); - data_.resize(num_data); - } - num_sample_ = num_data / num_channel_; - fclose(fp); - return true; - } - - int num_channel() const { return num_channel_; } - int sample_rate() const { return sample_rate_; } - int bits_per_sample() const { return bits_per_sample_; } - int num_sample() const { return num_sample_; } - const int16_t* data() const { return data_.data(); } - - private: - int num_channel_; - int sample_rate_; - int bits_per_sample_; - int num_sample_; // sample points per channel - std::vector data_; -}; - -class WavWriter { - public: - WavWriter(const float* data, int num_sample, int num_channel, int sample_rate, - int bits_per_sample) - : data_(data), - num_sample_(num_sample), - num_channel_(num_channel), - sample_rate_(sample_rate), - bits_per_sample_(bits_per_sample) {} - - void Write(const std::string& filename) { - FILE* fp = fopen(filename.c_str(), "w"); - // init char 'riff' 'WAVE' 'fmt ' 'data' - WavHeader header; - char wav_header[44] = {0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, - 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20, 0x10, 0x00, - 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00}; - memcpy(&header, wav_header, sizeof(header)); - header.channels = num_channel_; - header.bit = bits_per_sample_; - header.sample_rate = sample_rate_; - header.data_size = num_sample_ * num_channel_ * (bits_per_sample_ / 8); - header.size = sizeof(header) - 8 + header.data_size; - header.bytes_per_second = - sample_rate_ * num_channel_ * (bits_per_sample_ / 8); - header.block_size = num_channel_ * (bits_per_sample_ / 8); - - fwrite(&header, 1, sizeof(header), fp); - - for (int i = 0; i < num_sample_; ++i) { - for (int j = 0; j < num_channel_; ++j) { - switch (bits_per_sample_) { - case 8: { - char sample = static_cast(data_[i * num_channel_ + j]); - fwrite(&sample, 1, sizeof(sample), fp); - break; - } - case 16: { - int16_t sample = static_cast(data_[i * num_channel_ + j]); - fwrite(&sample, 1, sizeof(sample), fp); - break; - } - case 32: { - int sample = static_cast(data_[i * num_channel_ + j]); - fwrite(&sample, 1, sizeof(sample), fp); - break; - } - } - } - } - fclose(fp); - } - - private: - const float* data_; - int num_sample_; // total float points in data_ - int num_channel_; - int sample_rate_; - int bits_per_sample_; -}; - -class PcmReader : public AudioReader { - public: - PcmReader() {} - explicit PcmReader(const std::string& filename) { Open(filename); } - - bool Open(const std::string& filename) { - FILE* fp = fopen(filename.c_str(), "rb"); - if (NULL == fp) { - LOG(WARNING) << "Error in read " << filename; - return false; - } - - num_channel_ = 1; - sample_rate_ = FLAGS_pcm_sample_rate; - bits_per_sample_ = 16; - fseek(fp, 0, SEEK_END); - int data_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - num_sample_ = data_size / sizeof(int16_t); - data_.resize(num_sample_); - fread(&data_[0], data_size, 1, fp); - fclose(fp); - return true; - } - - int num_channel() const { return num_channel_; } - int sample_rate() const { return sample_rate_; } - int bits_per_sample() const { return bits_per_sample_; } - int num_sample() const { return num_sample_; } - - const int16_t* data() const { return data_.data(); } - - private: - int num_channel_; - int sample_rate_; - int bits_per_sample_; - int num_sample_; // sample points per channel - std::vector data_; -}; - -std::shared_ptr ReadAudioFile(const std::string& filename) { - size_t pos = filename.rfind('.'); - std::string suffix = filename.substr(pos); - if (suffix == ".wav" || suffix == ".WAV") { - return std::make_shared(filename); - } else { - return std::make_shared(filename); - } -} - -void WriteWavFile(const float* data, int data_size, int sample_rate, - const std::string& wav_path) { - std::vector tmp_wav(data, data + data_size); - for (int i = 0; i < tmp_wav.size(); i++) { - tmp_wav[i] *= (1 << 15); - } - WavWriter wav_write(tmp_wav.data(), tmp_wav.size(), 1, sample_rate, 16); - wav_write.Write(wav_path); -} - -} // namespace wenet - -#endif // FRONTEND_WAV_H_ diff --git a/wesep/runtime/separate/CMakeLists.txt b/wesep/runtime/separate/CMakeLists.txt deleted file mode 100644 index 8ed7035..0000000 --- a/wesep/runtime/separate/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_library(separate STATIC separate_engine.cc) -target_link_libraries(separate PUBLIC frontend ${TORCH_LIBRARIES}) diff --git a/wesep/runtime/separate/separate_engine.cc b/wesep/runtime/separate/separate_engine.cc deleted file mode 100644 index 962b60d..0000000 --- a/wesep/runtime/separate/separate_engine.cc +++ /dev/null @@ -1,106 +0,0 @@ - - -#include "separate/separate_engine.h" - -#include "gflags/gflags.h" -#include "glog/logging.h" -#include "torch/script.h" -#include "torch/torch.h" - -namespace wesep { - -void SeparateEngine::InitEngineThreads(int num_threads) { - // for multi-thread performance - at::set_num_threads(num_threads); - VLOG(1) << "Num intra-op threads: " << at::get_num_threads(); -} - -SeparateEngine::SeparateEngine(const std::string& model_path, - const int feat_dim, const int sample_rate) { - sample_rate_ = sample_rate; - feat_dim_ = feat_dim; - feature_config_ = - std::make_shared(feat_dim, sample_rate); - feature_pipeline_ = - std::make_shared(*feature_config_); - feature_pipeline_->Reset(); - - InitEngineThreads(1); - torch::jit::script::Module model = torch::jit::load(model_path); - model_ = std::make_shared(std::move(model)); - model_->eval(); -} - -void SeparateEngine::ExtractFeature(const int16_t* data, int data_size, - std::vector>* feat) { - feature_pipeline_->AcceptWaveform( - std::vector(data, data + data_size)); - feature_pipeline_->set_input_finished(); - feature_pipeline_->Read(feature_pipeline_->num_frames(), feat); - feature_pipeline_->Reset(); - this->ApplyMean(feat); -} - -void SeparateEngine::ApplyMean(std::vector>* feat) { - std::vector mean(feat_dim_, 0); - for (auto& i : *feat) { - std::transform(i.begin(), i.end(), mean.begin(), mean.begin(), - std::plus<>{}); - } - std::transform(mean.begin(), mean.end(), mean.begin(), - [&](const float d) { return d / feat->size(); }); - for (auto& i : *feat) { - std::transform(i.begin(), i.end(), mean.begin(), i.begin(), std::minus<>{}); - } -} - -void SeparateEngine::ForwardFunc(const std::vector& mix_wav, - const int16_t* spk1_emb, - const int16_t* spk2_emb, int data_size, - std::vector>* output) { - // pre-process - std::vector input_wav(mix_wav.size()); - for (int i = 0; i < mix_wav.size(); i++) { - input_wav[i] = static_cast(mix_wav[i]) / (1 << 15); - } - std::vector> spk1_emb_feat; - this->ExtractFeature(spk1_emb, data_size, &spk1_emb_feat); - std::vector> spk2_emb_feat; - this->ExtractFeature(spk2_emb, data_size, &spk2_emb_feat); - - // torch mix_wav - torch::Tensor torch_wav = torch::zeros({2, mix_wav.size()}, torch::kFloat32); - for (size_t i = 0; i < 2; i++) { - torch::Tensor row = - torch::from_blob(input_wav.data(), {input_wav.size()}, torch::kFloat32) - .clone(); - torch_wav[i] = std::move(row); - } - - // torch spk_emb_feat - torch::Tensor torch_spk_emb_feat = - torch::zeros({2, spk1_emb_feat.size(), feat_dim_}, torch::kFloat32); - for (size_t i = 0; i < spk1_emb_feat.size(); i++) { - torch::Tensor row1 = - torch::from_blob(spk1_emb_feat[i].data(), {feat_dim_}, torch::kFloat32); - torch_spk_emb_feat[0][i] = std::move(row1); - torch::Tensor row2 = - torch::from_blob(spk2_emb_feat[i].data(), {feat_dim_}, torch::kFloat32); - torch_spk_emb_feat[1][i] = std::move(row2); - } - - // forward - torch::NoGradGuard no_grad; - auto outputs = - model_->forward({torch_wav, torch_spk_emb_feat}).toTuple()->elements(); - torch::Tensor wav_out = outputs[0].toTensor(); - auto accessor = wav_out.accessor(); - - output->resize(2, std::vector(wav_out.size(1), 0.0)); - for (int i = 0; i < wav_out.size(1); i++) { - (*output)[0][i] = accessor[0][i]; - (*output)[1][i] = accessor[1][i]; - } -} - -} // namespace wesep diff --git a/wesep/runtime/separate/separate_engine.h b/wesep/runtime/separate/separate_engine.h deleted file mode 100644 index b7e876b..0000000 --- a/wesep/runtime/separate/separate_engine.h +++ /dev/null @@ -1,38 +0,0 @@ - -#ifndef SEPARATE_SEPARATE_ENGINE_H_ -#define SEPARATE_SEPARATE_ENGINE_H_ - -#include "torch/script.h" -#include "torch/torch.h" - -#include "frontend/feature_pipeline.h" - -namespace wesep { - -class SeparateEngine { - public: - explicit SeparateEngine(const std::string& model_path, const int feat_dim, - const int sample_rate); - - void InitEngineThreads(int num_threads = 1); - - void ForwardFunc(const std::vector& mix_wav, const int16_t* spk1_emb, - const int16_t* spk2_emb, int data_size, - std::vector>* output); - - void ExtractFeature(const int16_t* data, int data_size, - std::vector>* feat); - - void ApplyMean(std::vector>* feat); - - private: - std::shared_ptr model_ = nullptr; - std::shared_ptr feature_config_ = nullptr; - std::shared_ptr feature_pipeline_ = nullptr; - int sample_rate_ = 16000; - int feat_dim_ = 80; -}; - -} // namespace wesep - -#endif // SEPARATE_SEPARATE_ENGINE_H_ diff --git a/wesep/runtime/utils/CMakeLists.txt b/wesep/runtime/utils/CMakeLists.txt deleted file mode 100644 index a65cf41..0000000 --- a/wesep/runtime/utils/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -add_library(utils STATIC - utils.cc -) -target_link_libraries(utils PUBLIC glog gflags frontend) diff --git a/wesep/runtime/utils/blocking_queue.h b/wesep/runtime/utils/blocking_queue.h deleted file mode 100644 index b1748f6..0000000 --- a/wesep/runtime/utils/blocking_queue.h +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2020 Mobvoi Inc (Binbin Zhang) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef UTILS_BLOCKING_QUEUE_H_ -#define UTILS_BLOCKING_QUEUE_H_ - -#include -#include -#include -#include -#include - -namespace wenet { - -#define WENET_DISALLOW_COPY_AND_ASSIGN(Type) \ - Type(const Type&) = delete; \ - Type& operator=(const Type&) = delete; - -template -class BlockingQueue { - public: - explicit BlockingQueue(size_t capacity = std::numeric_limits::max()) - : capacity_(capacity) {} - - void Push(const T& value) { - { - std::unique_lock lock(mutex_); - while (queue_.size() >= capacity_) { - not_full_condition_.wait(lock); - } - queue_.push(value); - } - not_empty_condition_.notify_one(); - } - - void Push(T&& value) { - { - std::unique_lock lock(mutex_); - while (queue_.size() >= capacity_) { - not_full_condition_.wait(lock); - } - queue_.push(std::move(value)); - } - not_empty_condition_.notify_one(); - } - - T Pop() { - std::unique_lock lock(mutex_); - while (queue_.empty()) { - not_empty_condition_.wait(lock); - } - T t(std::move(queue_.front())); - queue_.pop(); - not_full_condition_.notify_one(); - return t; - } - - bool Empty() const { - std::lock_guard lock(mutex_); - return queue_.empty(); - } - - size_t Size() const { - std::lock_guard lock(mutex_); - return queue_.size(); - } - - void Clear() { - while (!Empty()) { - Pop(); - } - } - - private: - size_t capacity_; - mutable std::mutex mutex_; - std::condition_variable not_full_condition_; - std::condition_variable not_empty_condition_; - std::queue queue_; - - public: - WENET_DISALLOW_COPY_AND_ASSIGN(BlockingQueue); -}; - -} // namespace wenet - -#endif // UTILS_BLOCKING_QUEUE_H_ diff --git a/wesep/runtime/utils/timer.h b/wesep/runtime/utils/timer.h deleted file mode 100644 index 068519f..0000000 --- a/wesep/runtime/utils/timer.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2021 Mobvoi Inc (Binbin Zhang) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef UTILS_TIMER_H_ -#define UTILS_TIMER_H_ - -#include - -namespace wenet { - -class Timer { - public: - Timer() : time_start_(std::chrono::steady_clock::now()) {} - void Reset() { time_start_ = std::chrono::steady_clock::now(); } - // return int in milliseconds - int Elapsed() const { - auto time_now = std::chrono::steady_clock::now(); - return std::chrono::duration_cast(time_now - - time_start_) - .count(); - } - - private: - std::chrono::time_point time_start_; -}; -} // namespace wenet - -#endif // UTILS_TIMER_H_ diff --git a/wesep/runtime/utils/utils.cc b/wesep/runtime/utils/utils.cc deleted file mode 100644 index 6bf86c0..0000000 --- a/wesep/runtime/utils/utils.cc +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2023 Chengdong Liang (liangchengdong@mail.nwpu.edu.cn) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include -#include -#include -#include -#include - -#include "glog/logging.h" -#include "utils/utils.h" - -namespace wesep { - -std::string Ltrim(const std::string& str) { - size_t start = str.find_first_not_of(WHITESPACE); - return (start == std::string::npos) ? "" : str.substr(start); -} - -std::string Rtrim(const std::string& str) { - size_t end = str.find_last_not_of(WHITESPACE); - return (end == std::string::npos) ? "" : str.substr(0, end + 1); -} - -std::string Trim(const std::string& str) { return Rtrim(Ltrim(str)); } - -void SplitString(const std::string& str, std::vector* strs) { - SplitStringToVector(Trim(str), " \t", true, strs); -} - -void SplitStringToVector(const std::string& full, const char* delim, - bool omit_empty_strings, - std::vector* out) { - size_t start = 0, found = 0, end = full.size(); - out->clear(); - while (found != std::string::npos) { - found = full.find_first_of(delim, start); - // start != end condition is for when the delimiter is at the end - if (!omit_empty_strings || (found != start && start != end)) - out->push_back(full.substr(start, found - start)); - start = found + 1; - } -} - -#ifdef _MSC_VER -std::wstring ToWString(const std::string& str) { - unsigned len = str.size() * 2; - setlocale(LC_CTYPE, ""); - wchar_t* p = new wchar_t[len]; - mbstowcs(p, str.c_str(), len); - std::wstring wstr(p); - delete[] p; - return wstr; -} -#endif - -} // namespace wesep diff --git a/wesep/runtime/utils/utils.h b/wesep/runtime/utils/utils.h deleted file mode 100644 index 55e600d..0000000 --- a/wesep/runtime/utils/utils.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2023 Chengdong Liang (liangchengdong@mail.nwpu.edu.cn) -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef UTILS_UTILS_H_ -#define UTILS_UTILS_H_ - -#include -#include -#include - -namespace wesep { - -const char WHITESPACE[] = " \n\r\t\f\v"; - -// Split the string with space or tab. -void SplitString(const std::string& str, std::vector* strs); - -void SplitStringToVector(const std::string& full, const char* delim, - bool omit_empty_strings, - std::vector* out); - -#ifdef _MSC_VER -std::wstring ToWString(const std::string& str); -#endif - -} // namespace wesep - -#endif // UTILS_UTILS_H_