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.
70 lines
2.2 KiB
70 lines
2.2 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.
|
||
|
|
||
3 years ago
|
|
||
|
#pragma once
|
||
|
|
||
|
#include "base/common.h"
|
||
2 years ago
|
#include "frontend/frontend_itf.h"
|
||
3 years ago
|
|
||
|
namespace ppspeech {
|
||
|
|
||
3 years ago
|
// waves cache
|
||
3 years ago
|
class AudioCache : public FrontendInterface {
|
||
3 years ago
|
public:
|
||
3 years ago
|
explicit AudioCache(int buffer_size = 1000 * kint16max,
|
||
3 years ago
|
bool to_float32 = false);
|
||
3 years ago
|
|
||
2 years ago
|
virtual void Accept(const std::vector<BaseFloat>& waves);
|
||
3 years ago
|
|
||
2 years ago
|
virtual bool Read(std::vector<kaldi::BaseFloat>* waves);
|
||
3 years ago
|
|
||
3 years ago
|
// the audio dim is 1, one sample, which is useless,
|
||
3 years ago
|
// so we return size_(cache samples) instead.
|
||
3 years ago
|
virtual size_t Dim() const { return size_; }
|
||
3 years ago
|
|
||
3 years ago
|
virtual void SetFinished() {
|
||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||
|
finished_ = true;
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
virtual bool IsFinished() const { return finished_; }
|
||
3 years ago
|
|
||
2 years ago
|
void Reset() override {
|
||
3 years ago
|
offset_ = 0;
|
||
|
size_ = 0;
|
||
3 years ago
|
finished_ = false;
|
||
2 years ago
|
nsamples_ = 0;
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
private:
|
||
3 years ago
|
kaldi::BaseFloat Convert2PCM32(kaldi::BaseFloat val);
|
||
|
|
||
3 years ago
|
std::vector<kaldi::BaseFloat> ring_buffer_;
|
||
3 years ago
|
size_t offset_; // offset in ring_buffer_, begin of data
|
||
|
size_t size_; // samples in ring_buffer_, size of valid data
|
||
|
size_t capacity_; // capacity of ring_buffer_, full size of data buffer,
|
||
|
// unit: sample
|
||
3 years ago
|
bool finished_; // reach audio end
|
||
3 years ago
|
std::mutex mutex_;
|
||
3 years ago
|
std::condition_variable ready_feed_condition_;
|
||
3 years ago
|
kaldi::int32 timeout_; // millisecond
|
||
3 years ago
|
bool to_float32_; // int16 -> float32. used in linear_spectrogram
|
||
3 years ago
|
|
||
2 years ago
|
int32 nsamples_; // number samples readed.
|
||
3 years ago
|
DISALLOW_COPY_AND_ASSIGN(AudioCache);
|
||
3 years ago
|
};
|
||
|
|
||
3 years ago
|
} // namespace ppspeech
|