summaryrefslogtreecommitdiff
path: root/Detector/Detector.hpp
blob: b1d2ee4b8fdbd4f64912dadbf61900b561ff43a1 (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
#ifndef DETECTOR_HPP__
#define DETECTOR_HPP__
#include "Audio/AudioDevice.hpp"
#include "Audio/AudioStreamClock.hpp"
#include "ReceiveAudio.hpp"
#include "JttyReceiveAudio.hpp"
#include <QScopedArrayPointer>
#include <array>

//
// output device that distributes data in predefined chunks via a signal
//
// the underlying device for this abstraction is just the buffer that
// stores samples throughout a receiving period
//
class Detector : public AudioDevice
{
  Q_OBJECT;

public:
  //
  // if the data buffer were not global storage and fixed size then we
  // might want maximum size passed as constructor arguments
  //
  // we down sample by a factor of 4
  //
  // the samplesPerFFT argument is the number after down sampling
  //
  Detector (unsigned frameRate, double periodLengthInSeconds, unsigned downSampleFactor = 4u,
            QObject * parent = 0);

  void setTRPeriod (double period);
  Q_SLOT void setReceivePolicy (ReceivePolicy);
  Q_SLOT void setReceiveContext (quint64);
  Q_SLOT void setInputStopReason (JttyReceiveReason);
  Q_SLOT void requestContinuousReceiveDrain (quint64 requestId);
  Q_SLOT void endContinuousReception (JttyReceiveReason reason);
  void finishInput () override;
  void inputInterrupted () override;
  bool reset () override;

  Q_SIGNAL void framesWritten (qint64) const;
  Q_SIGNAL void audioBlock (ReceiveAudio) const;
  Q_SIGNAL void continuousAudioAvailable (JttyReceiveMailboxPtr) const;
  Q_SIGNAL void continuousReceptionStopped (JttyReceiveMailboxPtr) const;
  Q_SIGNAL void continuousReceiveDrained (quint64 requestId, JttyReceiveMailboxPtr) const;
  Q_SLOT void setBlockSize (unsigned);
  Q_SLOT void flushBufferedFrames (qint64 frameLimit);
  Q_SLOT void setStreamDescriptor (AudioStreamDescriptor);

protected:
  qint64 readData (char * /* data */, qint64 /* maxSize */) override
  {
    return -1;			// we don't produce data
  }

  qint64 writeData (char const * data, qint64 maxSize) override;

private:
  void clear ();		// discard buffer contents
  void resetPeriodBuffer ();

  unsigned m_frameRate;
  double   m_period;
  unsigned m_downSampleFactor;
  qint32 m_samplesPerFFT;	// after any down sampling
  AudioStreamClock m_stream_clock;
  ReceiveAudioProducer m_receiveAudioProducer;
  ReceivePolicy m_receivePolicy = ReceivePolicy::Timed;
  quint64 m_receiveContext = 0;
  JttyReceiveReason m_inputStopReason = JttyReceiveReason::MonitorStopped;
  JttyReceivePublisher m_jttyPublisher;
  std::array<short, 7 * 512> m_jttyOutput {};
  std::array<float, 49> m_downsampleState {};
  qint64 m_last_period_offset_ms {-1};
  static size_t const max_buffer_size {7 * 512};
  QScopedArrayPointer<short> m_buffer; // de-interleaved sample buffer
  // big enough for all the
  // samples for one increment of
  // data (a signals worth) at
  // the input sample rate
  unsigned m_bufferPos;
};

#endif