summaryrefslogtreecommitdiff
path: root/ReceiveAudio.hpp
blob: 227c5de6f9e479a5c21b9f55c2e08be83776a5a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef RECEIVE_AUDIO_HPP
#define RECEIVE_AUDIO_HPP

#include "commons.h"
#include "DecDataMutex.hpp"
#include <QMetaType>
#include <QMutexLocker>
#include <algorithm>
#include <atomic>
#include <cmath>
#include <memory>
#include <vector>

struct ReceiveAudioSourceState
{
  quint64 epoch = 0;
  int frames = 0;
};

struct ReceiveAudioBlock
{
  quint64 epoch;
  int start;
  int periodFrames;
  std::vector<short> samples;
  std::shared_ptr<ReceiveAudioSourceState const> source;
  int end () const { return start + int (samples.size ()); }
  int sourceFrames () const { return source ? source->frames : 0; }
};
using ReceiveAudio = std::shared_ptr<ReceiveAudioBlock const>;
Q_DECLARE_METATYPE (ReceiveAudio)

inline std::atomic<quint64>& receiveAudioEpochCounter ()
{
  static std::atomic<quint64> counter {0};
  return counter;
}

// Producer methods are called under dec_data_mutex. Each receive backend owns
// independent staging storage; queued blocks retain source freshness state.
class ReceiveAudioProducer
{
public:
  ReceiveAudioProducer ()
    : data_ {new dec_data_t {}}
    , source_ {std::make_shared<ReceiveAudioSourceState> ()}
  {
  }

  dec_data_t& data () { return *data_; }
  int frames () const { return source_->frames; }
  int capturedEnd () const { return capturedEnd_; }

  void setFrames (int frames)
  {
    data_->params.kin = frames;
    source_->frames = frames;
  }

  void reset (double period)
  {
    source_->epoch = receiveAudioEpochCounter ().fetch_add (1) + 1;
    capturedEnd_ = 0;
    auto const capacity = sizeof data_->d2 / sizeof data_->d2[0];
    auto const periodFrames = static_cast<std::size_t> (
      std::ceil (period * RX_SAMPLE_RATE));
    std::fill_n (data_->d2, std::min (capacity, periodFrames), qint16 {0});
    setFrames (0);
  }

  ReceiveAudio capture (int end, double period)
  {
    static int const registered = qRegisterMetaType<ReceiveAudio> ("ReceiveAudio");
    (void) registered;
    auto block = std::make_shared<ReceiveAudioBlock> ();
    block->epoch = source_->epoch;
    block->start = capturedEnd_;
    block->periodFrames = std::min (NTMAX * RX_SAMPLE_RATE,
                                    int (period * RX_SAMPLE_RATE));
    block->samples.assign (data_->d2 + capturedEnd_, data_->d2 + end);
    block->source = source_;
    capturedEnd_ = end;
    return block;
  }

private:
  std::unique_ptr<dec_data_t> data_;
  std::shared_ptr<ReceiveAudioSourceState> source_;
  int capturedEnd_ = 0;
};

// GUI-owned assembly. Reject obsolete periods (their timestamps and settings
// need no longer describe current operation), gaps and duplicate delivery.
// DSP may mutate the accepted buffer without affecting producer storage.
class ReceiveAudioConsumer
{
public:
  bool accept (ReceiveAudio const& block, dec_data_t& target)
  {
    {
      QMutexLocker lock {&dec_data_mutex ()};
      if (!block || !block->source || block->epoch != block->source->epoch
          || block->epoch <= invalidThrough_) return false;
    }
    if (block->samples.empty () || block->start < 0 || block->end () > NTMAX * RX_SAMPLE_RATE
        || block->periodFrames < 0 || block->periodFrames > NTMAX * RX_SAMPLE_RATE)
      return false;
    if (block->epoch != epoch_)
      {
        if (block->epoch < epoch_ || block->start != 0) return false;
        auto const clearFrames = std::min (NTMAX * RX_SAMPLE_RATE,
          std::max ({periodFrames_, block->periodFrames, target.params.kin}));
        std::fill_n (target.d2, clearFrames, short {0});
        periodFrames_ = block->periodFrames;
        target.params.kin = 0;
        epoch_ = block->epoch;
      }
    if (block->start != target.params.kin) return false;
    std::copy (block->samples.begin (), block->samples.end (), target.d2 + block->start);
    target.params.kin = block->end ();
    return true;
  }
  void invalidate ()
  {
    QMutexLocker lock {&dec_data_mutex ()};
    invalidThrough_ = receiveAudioEpochCounter ().load ();
    epoch_ = 0;
  }
  quint64 epoch () const { return epoch_; }
private:
  quint64 epoch_ = 0;
  quint64 invalidThrough_ = 0;
  int periodFrames_ = 0;
};
#endif