libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
audiodatadecoder.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_PROAV
13#include <cstdint>
14#include <cstddef>
15#include <promeki/namespace.h>
16#include <promeki/list.h>
17#include <promeki/error.h>
18#include <promeki/audiodesc.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class PcmAudioPayload;
24
80class AudioDataDecoder {
81 public:
83 static constexpr uint32_t BitsPerPacket = AudioDataEncoder::BitsPerPacket;
85 static constexpr uint32_t SyncBits = AudioDataEncoder::SyncBits;
87 static constexpr uint32_t PayloadBits = AudioDataEncoder::PayloadBits;
89 static constexpr uint32_t CrcBits = AudioDataEncoder::CrcBits;
91 static constexpr uint8_t SyncNibble = AudioDataEncoder::SyncNibble;
92
97 struct Band {
99 uint64_t firstSample = 0;
101 uint64_t sampleCount = 0;
103 uint32_t channel = 0;
104 };
105
116 struct DecodedItem {
118 Error error = Error::Ok;
120 uint8_t decodedSync = 0;
122 uint8_t decodedCrc = 0;
124 uint8_t expectedCrc = 0;
126 uint64_t payload = 0;
128 double samplesPerBit = 0.0;
132 uint64_t syncStartSample = 0;
144 int64_t streamSampleStart = -1;
156 int64_t packetSampleCount = 0;
157 };
158
160 using DecodedList = List<DecodedItem>;
161
163 AudioDataDecoder() = default;
164
177 explicit AudioDataDecoder(const AudioDesc &desc,
178 uint32_t expectedSamplesPerBit = AudioDataEncoder::DefaultSamplesPerBit);
179
181 bool isValid() const { return _valid; }
182
184 uint32_t expectedSamplesPerBit() const { return _expectedSamplesPerBit; }
185
187 const AudioDesc &desc() const { return _desc; }
188
201 Error decode(const PcmAudioPayload &payload, const List<Band> &bands, DecodedList &out) const;
202
204 DecodedItem decode(const PcmAudioPayload &payload, const Band &band) const;
205
230 DecodedItem decode(const float *samples, size_t count) const;
231
249 struct StreamState {
254 List<float> buffer;
262 int64_t sampleAnchor = 0;
263 };
264
274 static constexpr size_t kStreamBufferMaxSamples = 8192;
275
325 void decodeAll(StreamState &state, const float *newSamples, size_t count, DecodedList &out) const;
326
327 private:
328 AudioDesc _desc;
329 uint32_t _expectedSamplesPerBit = 0;
330 uint32_t _samplesPerBitMin = 0;
331 uint32_t _samplesPerBitMax = 0;
332 bool _valid = false;
333
334 DecodedItem decodeOne(const PcmAudioPayload &payload, const Band &band) const;
335 DecodedItem decodeSamples(const float *samples, size_t count) const;
336 // Shared bandwidth-check + demod helper. Given a
337 // pre-measured sync (@p syncStart sub-sample-accurate
338 // codeword leading edge, @p samplesPerBit measured
339 // pitch, @p syncStartSampleInt integer first-positive
340 // sample), runs the 76-bit integrate-and-compare
341 // demod across @p samples and verifies the sync
342 // nibble + CRC. Used by both the per-band decode
343 // path and the streaming decodeAll loop.
344 DecodedItem demodulate(const float *samples, size_t count, double syncStart, double samplesPerBit,
345 uint64_t syncStartSampleInt) const;
346};
347
348PROMEKI_NAMESPACE_END
349
350#endif // PROMEKI_ENABLE_PROAV