summaryrefslogtreecommitdiff
path: root/Modulator/JttyPcmFifo.hpp
blob: f084a6efc1212db3b9c703b7d4262e202dadfc02 (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
#ifndef JTTY_PCM_FIFO_HPP__
#define JTTY_PCM_FIFO_HPP__

#include <atomic>

#include <QVector>

class JttyPcmFifo
{
public:
  // Lock-free SPSC PCM queue: one producer appends rendered messages, one
  // backend thread pulls samples. clear() only publishes a reset generation;
  // the consumer applies it so FIFO indices are not concurrently rewritten.
  // The audio backend reports this edge later, outside the pull callback. The
  // epoch/total pair lets MainWindow ignore a tail event that belonged to an
  // older queue state.
  struct DrainState
  {
    bool ready;
    qint64 epoch;
    qint64 totalAtDrain;
  };

  explicit JttyPcmFifo (qint64 capacitySamples);

  bool enqueue (QVector<qint16> const& samples, qint64 epoch);
  bool enqueue (qint16 const * samples, qint64 count, qint64 epoch);
  void clear (qint64 epoch = 0);
  void applyPendingReset () noexcept;

  qint16 pullSample (qint64 drainGuard) noexcept;
  DrainState takeDrainReady () noexcept;

  qint64 capacity () const {return m_capacity;}
  qint64 queuedReal () const noexcept;
  qint64 servedReal () const noexcept;
  qint64 totalReal () const noexcept;

private:
  // Single producer, single consumer: MainWindow appends complete rendered
  // messages, and one audio pull path consumes frames. The pull side is kept to
  // atomics and array access so it can run in a real-time-ish audio callback.
  void updateDrainState (qint64 drainGuard) noexcept;
  qint64 logicalServedReal () const noexcept;
  qint64 logicalTotalReal () const noexcept;

  QVector<qint16> m_buffer;
  qint64 const m_capacity;

  std::atomic<qint64> m_head;
  std::atomic<qint64> m_tail;
  std::atomic<qint64> m_used;

  std::atomic<qint64> m_totalReal;
  std::atomic<qint64> m_servedReal;
  std::atomic<qint64> m_streamPos;
  std::atomic<qint64> m_realEndStreamPos;
  std::atomic<qint64> m_epoch;

  // clear() publishes a reset generation boundary; pullSample() and other
  // consumer-owned entry points apply it. Enqueue, progress getters, and drain
  // signaling must interpret this same generation baseline.
  std::atomic<qint64> m_resetRequestedGeneration;
  std::atomic<qint64> m_resetAppliedGeneration;
  std::atomic<qint64> m_resetEpoch;
  std::atomic<qint64> m_resetTail;
  std::atomic<qint64> m_resetTotalBaseline;

  std::atomic<bool> m_drainSignaled;
  std::atomic<bool> m_drainReady;
  std::atomic<qint64> m_drainReadyEpoch;
  std::atomic<qint64> m_drainReadyTotal;
};

bool jttyTxDrained (qint64 servedReal, qint64 totalReal,
                    qint64 streamPos, qint64 realEndStreamPos,
                    qint64 drainGuard);
bool jttyPcmEnqueueFits (qint64 capacitySamples, qint64 usedSamples,
                         qint64 count) noexcept;

#endif