JS8Call-Improved master
Loading...
Searching...
No Matches
Detector.h
Go to the documentation of this file.
1
11
12#ifndef DETECTOR_HPP__
13#define DETECTOR_HPP__
14
16
17#include <QMutex>
18#include <vendor/Eigen/Dense>
19
20#include <array>
21
22// Output device that distributes data in predefined chunks via a signal;
23// underlying device for this abstraction is just the buffer that stores
24// samples throughout a receiving period.
25
26class Detector : public AudioDevice {
27 Q_OBJECT;
28
37 class Filter final {
38 public:
40 static constexpr std::size_t NDOWN = 48 / 12;
42 static constexpr std::size_t NTAPS = 49;
44 static constexpr std::size_t SHIFT = NTAPS - NDOWN;
45
46 using Vector = Eigen::Vector<float, NTAPS>;
47 using Sample = Eigen::Map<Eigen::Vector<short, NDOWN> const>;
48
53 explicit Filter(std::array<Vector::value_type, NTAPS> const &lowpass)
54 : m_w(lowpass.data()), m_t(Vector::Zero()) {}
55
61 auto downSample(Sample::value_type const *const data) {
62 m_t.head(SHIFT) = m_t.segment(NDOWN, SHIFT);
63 m_t.tail(NDOWN) = Sample(data).cast<Vector::value_type>();
64
65 return static_cast<Sample::value_type>(std::round(m_w.dot(m_t)));
66 }
67
68 private:
69 Eigen::Map<Vector const> m_w;
70 Vector m_t;
71 };
72
74 static constexpr std::size_t MaxBufferSize = 7 * 512;
75
77 using Buffer = std::array<short, MaxBufferSize * Filter::NDOWN>;
78
79 public:
86 Detector(unsigned frameRate, unsigned periodLengthInSeconds,
87 QObject *parent = nullptr);
88
90 unsigned period() const { return m_period; }
91
93 QMutex *getMutex() { return &m_lock; }
94
96 void setTRPeriod(unsigned p) { m_period = p; }
97
101 unsigned secondInPeriod() const;
102
104 void clear();
105
107 bool reset() override;
108
110 void resetBufferContent();
111
113 void resetBufferPosition();
114
116 Q_SIGNAL void framesWritten(qint64) const;
117
119 Q_SLOT void setBlockSize(unsigned);
120
121 protected:
123 qint64 readData(char *, qint64) override { return -1; }
124
126 qint64 writeData(char const *, qint64) override;
127
128 private:
129 unsigned m_frameRate;
130 unsigned m_period;
131 QMutex m_lock;
132 Filter m_filter;
133 Buffer m_buffer;
134 Buffer::size_type m_bufferPos = 0;
135 std::size_t m_samplesPerFFT = MaxBufferSize;
136 qint32 m_ns = 999;
137};
138
139#endif
Audio Device declarations for JS8Call-improved.
bool reset() override
Reset the detector state.
Definition Detector.cpp:85
qint64 readData(char *, qint64) override
Definition Detector.h:123
QMutex * getMutex()
Definition Detector.h:93
void resetBufferContent()
Reset the buffer content to zero.
Definition Detector.cpp:148
Detector(unsigned frameRate, unsigned periodLengthInSeconds, QObject *parent=nullptr)
Construct a Detector.
Definition Detector.cpp:65
void resetBufferPosition()
Reset the buffer position based on current time.
Definition Detector.cpp:114
unsigned period() const
Definition Detector.h:90
unsigned secondInPeriod() const
Return how many seconds into the current period we are.
Definition Detector.cpp:227
Q_SLOT void setBlockSize(unsigned)
Set the block size for FFT processing.
Definition Detector.cpp:77
void clear()
Clear the detector buffer.
Definition Detector.cpp:96
qint64 writeData(char const *, qint64) override
Write data to the detector buffer.
Definition Detector.cpp:162
void setTRPeriod(unsigned p)
Definition Detector.h:96
Q_SIGNAL void framesWritten(qint64) const