You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
3 years ago
|
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
||
|
//
|
||
|
// 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.
|
||
|
|
||
2 years ago
|
#include "frontend/feature_cache.h"
|
||
3 years ago
|
|
||
3 years ago
|
namespace ppspeech {
|
||
|
|
||
|
using kaldi::BaseFloat;
|
||
|
using std::unique_ptr;
|
||
2 years ago
|
using std::vector;
|
||
3 years ago
|
|
||
2 years ago
|
FeatureCache::FeatureCache(size_t max_size,
|
||
3 years ago
|
unique_ptr<FrontendInterface> base_extractor) {
|
||
2 years ago
|
max_size_ = max_size;
|
||
3 years ago
|
base_extractor_ = std::move(base_extractor);
|
||
3 years ago
|
dim_ = base_extractor_->Dim();
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
void FeatureCache::Accept(const std::vector<kaldi::BaseFloat>& inputs) {
|
||
3 years ago
|
// read inputs
|
||
3 years ago
|
base_extractor_->Accept(inputs);
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// pop feature chunk
|
||
2 years ago
|
bool FeatureCache::Read(std::vector<kaldi::BaseFloat>* feats) {
|
||
3 years ago
|
kaldi::Timer timer;
|
||
|
std::unique_lock<std::mutex> lock(mutex_);
|
||
2 years ago
|
// feed current data
|
||
|
if (cache_.empty()) {
|
||
|
bool result = false;
|
||
|
do {
|
||
|
result = Compute();
|
||
|
} while (result);
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
3 years ago
|
if (cache_.empty()) return false;
|
||
3 years ago
|
|
||
|
// read from cache
|
||
2 years ago
|
*feats = cache_.front();
|
||
3 years ago
|
cache_.pop();
|
||
2 years ago
|
VLOG(1) << "FeatureCache::Read cost: " << timer.Elapsed() << " sec.";
|
||
3 years ago
|
return true;
|
||
3 years ago
|
}
|
||
|
|
||
|
// read all data from base_feature_extractor_ into cache_
|
||
3 years ago
|
bool FeatureCache::Compute() {
|
||
3 years ago
|
// compute and feed
|
||
2 years ago
|
vector<BaseFloat> feature;
|
||
3 years ago
|
bool result = base_extractor_->Read(&feature);
|
||
2 years ago
|
if (result == false || feature.size() == 0) return false;
|
||
3 years ago
|
|
||
2 years ago
|
kaldi::Timer timer;
|
||
|
|
||
2 years ago
|
int32 num_chunk = feature.size() / dim_;
|
||
2 years ago
|
VLOG(3) << "nframe computed: " << nframe_;
|
||
2 years ago
|
|
||
3 years ago
|
for (int chunk_idx = 0; chunk_idx < num_chunk; ++chunk_idx) {
|
||
2 years ago
|
int32 start = chunk_idx * dim_;
|
||
2 years ago
|
vector<BaseFloat> feature_chunk(feature.data() + start,
|
||
2 years ago
|
feature.data() + start + dim_);
|
||
3 years ago
|
// feed cache
|
||
3 years ago
|
cache_.push(feature_chunk);
|
||
2 years ago
|
++nframe_;
|
||
3 years ago
|
}
|
||
2 years ago
|
|
||
|
VLOG(1) << "FeatureCache::Compute cost: " << timer.Elapsed() << " sec. "
|
||
|
<< num_chunk << " feats.";
|
||
|
return true;
|
||
3 years ago
|
}
|
||
|
|
||
2 years ago
|
} // namespace ppspeech
|