RMSData.hpp
1 #pragma once
2 #include <Process/TimeValue.hpp>
3 
4 #include <Media/AudioArray.hpp>
5 
6 #include <ossia/detail/span.hpp>
7 
8 #include <QBuffer>
9 #include <QFile>
10 
11 namespace Media
12 {
13 
14 using rms_sample_t = int16_t;
15 struct RMSData : public QObject
16 {
17  W_OBJECT(RMSData)
18 public:
19  struct Header
20  {
21  uint32_t sampleRate{};
22  uint32_t bufferSize{};
23  uint32_t channels{};
24  uint32_t padding{};
25  };
26 
27  RMSData();
28 
29  void load(QString abspath, int channels, int rate, TimeVal duration);
30  bool exists() const;
31 
32  // deinterleaved
33  void decode(const std::vector<tcb::span<const ossia::audio_sample>>& audio);
34  void decodeLast(const std::vector<tcb::span<const ossia::audio_sample>>& audio);
35 
36  // interleaved
37  void decode(ossia::drwav_handle& audio);
38  double sampleRateRatio(double expectedRate) const noexcept;
39 
40  ossia::small_vector<float, 8>
41  frame(int64_t start_sample, int64_t end_sample) const noexcept;
42 
43  int64_t frames_count = 0;
44  int64_t samples_count = 0;
45 
46  Header* header_ptr() const noexcept { return header; }
47  rms_sample_t* data_ptr() const noexcept { return data; }
48 
49  void newData() W_SIGNAL(newData);
50  void finishedDecoding() W_SIGNAL(finishedDecoding);
51 
52 private:
53  rms_sample_t computeChannelRMS(
54  tcb::span<const ossia::audio_sample> chan, int64_t start_idx, int64_t buffer_size);
55  void computeRMS(
56  const std::vector<tcb::span<const ossia::audio_sample>>& audio, int buffer_size);
57  void computeLastRMS(
58  const std::vector<tcb::span<const ossia::audio_sample>>& audio, int buffer_size);
59  void
60  computeChannelRMS(ossia::drwav_handle& wav, rms_sample_t* bytes, int64_t buffer_size);
61 
62  QFile m_file;
63  bool m_exists{false};
64 
65  Header* header{};
66  rms_sample_t* data{};
67 
68  QByteArray m_ramData;
69  QBuffer m_ramBuffer;
70 };
71 
72 }
Definition: RMSData.hpp:20
Definition: RMSData.hpp:16
Definition: TimeValue.hpp:21