swift
Loading...
Searching...
No Matches
soundcardsampleprovider.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <algorithm>
7
10
11using namespace swift::config;
12using namespace swift::misc;
13using namespace swift::sound::sample_provider;
14
15namespace swift::core::afv::audio
16{
20 {
21 const QString on = QStringLiteral("%1 sample rate: %2, transceivers: %3")
22 .arg(classNameShort(this))
23 .arg(sampleRate)
24 .arg(transceiverIDs.size());
25 this->setObjectName(on);
26
27 m_waveFormat.setSampleRate(sampleRate);
28 m_waveFormat.setChannelCount(1);
29 m_waveFormat.setSampleFormat(QAudioFormat::Int16);
30 static_assert(Q_BYTE_ORDER == Q_LITTLE_ENDIAN);
31
32 m_mixer = new CMixingSampleProvider(this);
33 m_receiverIDs = transceiverIDs;
34
35 constexpr int voiceInputNumber = 4; // number of CallsignSampleProviders
36 for (quint16 transceiverID : transceiverIDs)
37 {
38 auto transceiverInput = new CReceiverSampleProvider(m_waveFormat, transceiverID, voiceInputNumber, m_mixer);
41 m_receiverInputs.push_back(transceiverInput);
42 m_receiverIDs.push_back(transceiverID);
43 m_mixer->addMixerInput(transceiverInput);
44 }
45 }
46
48 {
49 for (CReceiverSampleProvider *receiverInput : std::as_const(m_receiverInputs))
50 {
51 receiverInput->setBypassEffects(value);
52 }
53 }
54
55 void CSoundcardSampleProvider::pttUpdate(bool active, const QVector<TxTransceiverDto> &txTransceivers)
56 {
57 if (active)
58 {
59 if (!txTransceivers.isEmpty())
60 {
61 QVector<TxTransceiverDto> txTransceiversFiltered = txTransceivers;
62
63 txTransceiversFiltered.erase(
64 std::ranges::remove_if(txTransceiversFiltered,
65 [this](const TxTransceiverDto &d) { return !m_receiverIDs.contains(d.id); })
66 .begin(),
67 txTransceiversFiltered.end());
68
69 for (const TxTransceiverDto &txTransceiver : txTransceiversFiltered)
70 {
71 auto it = std::ranges::find_if(m_receiverInputs, [txTransceiver](const CReceiverSampleProvider *p) {
72 return p->getId() == txTransceiver.id;
73 });
74
75 if (it != m_receiverInputs.end())
76 {
77 (*it)->setMute(true);
78 }
79 }
80 }
81 }
82 else
83 {
84 for (CReceiverSampleProvider *receiverInput : std::as_const(m_receiverInputs))
85 {
86 receiverInput->setMute(false);
87 }
88 }
89 }
90
92 {
93 return m_mixer->readSamples(samples, count);
94 }
95
97 const QVector<RxTransceiverDto> &rxTransceivers)
98 {
99 QVector<RxTransceiverDto> rxTransceiversFilteredAndSorted = rxTransceivers;
100
101 rxTransceiversFilteredAndSorted.erase(
102 std::ranges::remove_if(rxTransceiversFilteredAndSorted,
103 [this](const RxTransceiverDto &r) { return !m_receiverIDs.contains(r.id); })
104 .begin(),
105 rxTransceiversFilteredAndSorted.end());
106
107 std::ranges::sort(rxTransceiversFilteredAndSorted,
108 [](const RxTransceiverDto &a, const RxTransceiverDto &b) -> bool {
109 return a.distanceRatio > b.distanceRatio;
110 });
111
112 if (!rxTransceiversFilteredAndSorted.isEmpty())
113 {
114 bool audioPlayed = false;
115 QVector<quint16> handledTransceiverIDs;
116 for (const auto rxTransceiver : rxTransceiversFilteredAndSorted)
117 {
118 if (!handledTransceiverIDs.contains(rxTransceiver.id))
119 {
120 handledTransceiverIDs.push_back(rxTransceiver.id);
121
122 CReceiverSampleProvider *receiverInput = nullptr;
123 auto it = std::ranges::find_if(m_receiverInputs, [rxTransceiver](const CReceiverSampleProvider *p) {
124 return p->getId() == rxTransceiver.id;
125 });
126
127 if (it != m_receiverInputs.end())
128 {
129 receiverInput = *it;
130 }
131
132 if (!receiverInput)
133 {
134 continue;
135 }
136 if (receiverInput->getMute())
137 {
138 continue;
139 }
140
141 if (!audioPlayed)
142 {
143 receiverInput->addOpusSamples(audioDto, rxTransceiver.frequency, rxTransceiver.distanceRatio);
144 audioPlayed = true;
145 }
146 else
147 {
148 receiverInput->addSilentSamples(audioDto, rxTransceiver.frequency, rxTransceiver.distanceRatio);
149 }
150
151 // debug ONLY
153 {
154 receiverInput->logVoiceInputs(QStringLiteral("Transceiver %1 ").arg(rxTransceiver.id), 1500);
155 }
156 }
157 } // each transceiver
158 } // filtered rx transceivers
159 }
160
162 {
163 for (const TransceiverDto &radioTransceiver : radioTransceivers)
164 {
165 auto it = std::ranges::find_if(m_receiverInputs, [radioTransceiver](const CReceiverSampleProvider *p) {
166 return p->getId() == radioTransceiver.id;
167 });
168
169 if (it != m_receiverInputs.end())
170 {
171 (*it)->setFrequency(radioTransceiver.frequencyHz);
172 }
173 }
174
175 for (CReceiverSampleProvider *receiverInput : std::as_const(m_receiverInputs))
176 {
177 const quint16 transceiverID = receiverInput->getId();
178 const bool contains = std::ranges::any_of(
179 radioTransceivers, [transceiverID](const auto &tx) { return transceiverID == tx.id; });
180 if (!contains)
181 {
182 receiverInput->setFrequency(0);
183 }
184 }
185 }
186
188 {
189 return m_receiverInputs.at(transceiverID)->getReceivingCallsignsString();
190 }
191
192 bool CSoundcardSampleProvider::setGainRatioForTransceiver(quint16 transceiverID, double gainRatio)
193 {
194 auto receiverInput = std::ranges::find_if(
195 m_receiverInputs, [&](const auto receiver) { return receiver->getId() == transceiverID; });
196 if (receiverInput == m_receiverInputs.end())
197 {
198 return false;
199 }
200 return (*receiverInput)->setGainRatio(gainRatio);
201 }
202
204 {
205 return m_receiverInputs.at(transceiverID)->getReceivingCallsigns();
206 }
207
208} // namespace swift::core::afv::audio
static bool isLocalDeveloperDebugBuild()
Local build for developers.
void addSilentSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
Add samples.
void receivingCallsignsChanged(const TransceiverReceivingCallsignsChangedArgs &args)
Receving callsigns have changed.
void addOpusSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
Add samples.
int readSamples(QVector< float > &samples, qint64 count)
Read samples.
bool setGainRatioForTransceiver(quint16 transceiverID, double gainRatio)
Setting gain for specified receiver.
swift::misc::aviation::CCallsignSet getReceivingCallsigns(quint16 transceiverID) const
Receiving callsign as single string.
void receivingCallsignsChanged(const TransceiverReceivingCallsignsChangedArgs &args)
Changed callsigns.
void updateRadioTransceivers(const QVector< TransceiverDto > &radioTransceivers)
Update all tranceivers.
QString getReceivingCallsignsString(quint16 transceiverID) const
Receiving callsign as single string.
void pttUpdate(bool active, const QVector< TxTransceiverDto > &txTransceivers)
Update PTT.
CSoundcardSampleProvider(int sampleRate, const QVector< quint16 > &transceiverIDs, QObject *parent=nullptr)
Ctor.
void addOpusSamples(const IAudioDto &audioDto, const QVector< RxTransceiverDto > &rxTransceivers)
Add OPUS samples.
Value object for a set of callsigns.
Definition callsignset.h:26
ISampleProvider(QObject *parent=nullptr)
Ctor.
Free functions in swift::misc.
QString classNameShort(const QObject *object)
Class name as from QMetaObject::className without namespace.
bool contains(const AT &value) const const
QList< T >::iterator end()
QList< T >::iterator erase(QList< T >::const_iterator begin, QList< T >::const_iterator end)
bool isEmpty() const const
void push_back(QList< T >::parameter_type value)
qsizetype size() const const
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
Receive transceiver DTO.
Definition dto.h:206
uint16_t id
Properties.
Definition dto.h:209
float distanceRatio
Properties.
Definition dto.h:211
Transmit transceiver DTO.
Definition dto.h:220