Zao SDK for Jetson / libzep API リファレンス  1.0.0.0 (2023-05-08)
PcmBufferWriter.hpp
1 #ifndef ZEP_AUDIO_PCM_BUFFER_WRITER_HPP_
2 #define ZEP_AUDIO_PCM_BUFFER_WRITER_HPP_
3 
4 #include <cstddef>
5 #include <cstdint>
6 #include <memory>
7 
8 #include "../TimestampInterface.hpp"
9 
10 namespace zep {
11 namespace audio {
12 
13 class PcmBufferPoolInterface;
14 
20 class PcmBufferWriter final {
21  public:
26  PcmBufferWriter() noexcept {}
27 
35  PcmBufferWriter(const std::weak_ptr<PcmBufferPoolInterface>& pool_weak,
36  std::int16_t* pointer, std::size_t writable_samples) noexcept
37  : pool_weak_(pool_weak),
38  pointer_(pointer),
39  writable_samples_(writable_samples) {}
40 
46  PcmBufferWriter(PcmBufferWriter&& other) noexcept {
47  *this = std::move(other);
48  }
49 
56  Cancel();
57  std::swap(pool_weak_, other.pool_weak_);
58  std::swap(pointer_, other.pointer_);
59  std::swap(writable_samples_, other.writable_samples_);
60  return *this;
61  }
62 
63  // コピー構築禁止
64  PcmBufferWriter(const PcmBufferWriter&) = delete;
65 
66  // コピー代入禁止
67  PcmBufferWriter& operator=(const PcmBufferWriter&) = delete;
68 
72  ~PcmBufferWriter() noexcept { Cancel(); }
73 
80  bool IsValid() const noexcept { return !!pointer_; }
81 
89  explicit operator bool() const noexcept { return IsValid(); }
90 
96  std::int16_t* GetPointer() const noexcept { return pointer_; }
97 
104  std::size_t GetWritableSamples() const noexcept { return writable_samples_; }
105 
120  inline void Finalize(std::size_t written_samples,
121  TimestampInterface::Rep head_timestamp) noexcept;
122 
129  void Cancel() noexcept { Finalize(0, 0); }
130 
131  private:
132  std::weak_ptr<PcmBufferPoolInterface> pool_weak_;
133  std::int16_t* pointer_ = nullptr;
134  std::size_t writable_samples_ = 0;
135 };
136 
137 } // namespace audio
138 } // namespace zep
139 
140 #include "PcmBufferPoolInterface.hpp"
141 
143  std::size_t written_samples,
144  TimestampInterface::Rep head_timestamp) noexcept {
145  if (!pointer_) {
146  return;
147  }
148  if (auto pool = pool_weak_.lock()) {
149  pool->FinalizeWriter(*this, written_samples, head_timestamp);
150  }
151  pool_weak_.reset();
152  pointer_ = nullptr;
153  writable_samples_ = 0;
154 }
155 
156 #endif // ZEP_AUDIO_PCM_BUFFER_WRITER_HPP_
std::uint64_t Rep
タイムスタンプの表現に用いる整数型
Definition: TimestampInterface.hpp:16
ZEP SDK用名前空間
Definition: FactoryInterface.hpp:10
PcmBufferWriter & operator=(PcmBufferWriter &&other) noexcept
PcmBufferWriterオブジェクトをムーブ代入する。
Definition: PcmBufferWriter.hpp:55
PcmBufferWriter() noexcept
PcmBufferWriterオブジェクトを管理対象無しでデフォルト構築する。
Definition: PcmBufferWriter.hpp:26
std::int16_t * GetPointer() const noexcept
管理対象バッファの先頭ポインタを取得する
Definition: PcmBufferWriter.hpp:96
bool IsValid() const noexcept
有効なバッファを保持しているか否かを取得する。
Definition: PcmBufferWriter.hpp:80
~PcmBufferWriter() noexcept
PcmBufferWriterオブジェクトを破棄する。
Definition: PcmBufferWriter.hpp:72
void Cancel() noexcept
バッファへのデータ書き込みキャンセルを通知する。
Definition: PcmBufferWriter.hpp:129
PcmBufferWriter(const std::weak_ptr< PcmBufferPoolInterface > &pool_weak, std::int16_t *pointer, std::size_t writable_samples) noexcept
PcmBufferWriterオブジェクトを構築する。
Definition: PcmBufferWriter.hpp:35
void Finalize(std::size_t written_samples, TimestampInterface::Rep head_timestamp) noexcept
バッファへのデータ書き込み完了を通知する。
Definition: PcmBufferWriter.hpp:142
PCMバッファのWriter実装
Definition: PcmBufferWriter.hpp:20
PcmBufferWriter(PcmBufferWriter &&other) noexcept
PcmBufferWriterオブジェクトをムーブ構築する。
Definition: PcmBufferWriter.hpp:46
std::size_t GetWritableSamples() const noexcept
書き込める最大サンプル数を取得する。
Definition: PcmBufferWriter.hpp:104