MOS
buffer.hpp
1 #pragma once
2 
3 #include <atomic>
4 #include <filesystem>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 namespace mos::aud {
10 
11 class Buffer;
12 using Shared_buffer = std::shared_ptr<Buffer>;
13 
15 class Buffer final {
16 public:
17  using Samples = std::vector<short>;
18 
19  template <class T>
21  Buffer(const T begin, const T end, const int channels = 1,
22  const int sample_rate = 44100)
23  : id_(current_id_++), samples_(begin, end), channels_(channels),
24  sample_rate_(sample_rate) {}
25 
27  explicit Buffer(int channels = 1, int sample_rate = 44100);
28 
30  static auto load(const std::filesystem::path &path) -> Buffer;
31 
32  Samples::const_iterator begin() const;
33 
34  Samples::const_iterator end() const;
35 
37  auto data() const -> const short *;
38 
40  auto id() const -> unsigned int;
41 
43  auto channels() const -> int;
44 
46  auto sample_rate() const -> int;
47 
49  auto duration() const -> float;
50 
52  auto size() const -> size_t;
53 
54 private:
55  static std::atomic_uint current_id_;
56  unsigned int id_;
57  Samples samples_;
58  int channels_;
59  int sample_rate_;
60 };
61 } // namespace mos::aud
auto id() const -> unsigned int
Unique id.
auto sample_rate() const -> int
Get sample rate.
auto channels() const -> int
Get number of channels.
static auto load(const std::filesystem::path &path) -> Buffer
Load shared buffer.
16bit integer audio buffer.
Definition: buffer.hpp:15
auto size() const -> size_t
Size of samples container.
auto duration() const -> float
Duration in seconds.
Buffer(const T begin, const T end, const int channels=1, const int sample_rate=44100)
Construct buffer from a container of shorts.
Definition: buffer.hpp:21
auto data() const -> const short *
Raw data.
Definition: filter.hpp:13