swift
Loading...
Searching...
No Matches
receiversampleprovider.cpp
Go to the documentation of this file.
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
7
8#include <algorithm>
9
10#include <QStringBuilder>
11
12#include "misc/logmessage.h"
13#include "misc/metadatautils.h"
14#include "sound/sampleprovider/resourcesoundsampleprovider.h"
16
17using namespace swift::misc;
18using namespace swift::misc::audio;
19using namespace swift::misc::aviation;
20using namespace swift::sound::sample_provider;
21
22namespace swift::core::afv::audio
23{
29
30 CReceiverSampleProvider::CReceiverSampleProvider(const QAudioFormat &audioFormat, quint16 id, int voiceInputNumber,
32 : ISampleProvider(parent), m_id(id)
33 {
34 const QString on = QStringLiteral("%1 id: %2").arg(classNameShort(this)).arg(id);
35 this->setObjectName(on);
36
37 m_mixer = new CMixingSampleProvider(this);
38 for (int i = 0; i < voiceInputNumber; i++)
39 {
40 const auto voiceInput = new CCallsignSampleProvider(audioFormat, this, m_mixer);
41 m_voiceInputs.push_back(voiceInput);
42 m_mixer->addMixerInput(voiceInput);
43 }
44
45 m_blockTone = new CSinusGenerator(180, this);
46 m_mixer->addMixerInput(m_blockTone);
47 m_volume = new CVolumeSampleProvider(m_mixer);
48 }
49
51 {
52 for (CCallsignSampleProvider *voiceInput : std::as_const(m_voiceInputs))
53 {
54 voiceInput->setBypassEffects(value);
55 }
56 }
57
58 void CReceiverSampleProvider::setFrequency(const uint &frequencyHz)
59 {
60 if (frequencyHz != m_frequencyHz)
61 {
62 for (CCallsignSampleProvider *voiceInput : std::as_const(m_voiceInputs))
63 {
64 voiceInput->clear();
65 }
66 }
67 m_frequencyHz = frequencyHz;
68 }
69
71 {
72 const int numberOfCallsigns = static_cast<int>(std::count_if(
73 m_voiceInputs.begin(), m_voiceInputs.end(), [](const CCallsignSampleProvider *p) { return p->inUse(); }));
74 return numberOfCallsigns;
75 }
76
78 {
79 m_mute = value;
80 if (value)
81 {
82 for (CCallsignSampleProvider *voiceInput : std::as_const(m_voiceInputs))
83 {
84 voiceInput->clear();
85 }
86 }
87 }
88
90 {
91 int numberOfInUseInputs = activeCallsigns();
92 if (numberOfInUseInputs > 1 && m_doBlockWhenAppropriate)
93 {
94 m_blockTone->setFrequency(180.0);
95 m_blockTone->setGain(m_blockToneGain);
96 }
97 else
98 {
99 m_blockTone->setGain(0.0);
100 }
101
102 if (m_doClickWhenAppropriate && numberOfInUseInputs == 0)
103 {
104 auto *resourceSound = new CResourceSoundSampleProvider(Samples::instance().click(), m_mixer);
105 m_mixer->addMixerInput(resourceSound);
106 m_doClickWhenAppropriate = false;
107 }
108
110 if (numberOfInUseInputs != m_lastNumberOfInUseInputs)
111 {
112 QStringList receivingCallsigns;
113 for (const CCallsignSampleProvider *voiceInput : std::as_const(m_voiceInputs))
114 {
115 const QString callsign = voiceInput->callsign();
116 if (!callsign.isEmpty())
117 {
118 receivingCallsigns.push_back(callsign);
119 }
120 }
121
122 m_receivingCallsignsString = receivingCallsigns.join(',');
123 m_receivingCallsigns = CCallsignSet(receivingCallsigns);
124 const TransceiverReceivingCallsignsChangedArgs args = { .transceiverID = m_id,
125 .receivingCallsigns = receivingCallsigns };
126 emit receivingCallsignsChanged(args);
127 }
128 m_lastNumberOfInUseInputs = numberOfInUseInputs;
129 return m_volume->readSamples(samples, count);
130 }
131
132 void CReceiverSampleProvider::addOpusSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
133 {
134 if (m_frequencyHz != frequency)
135 {
136 return;
137 } // Lag in the backend means we get the tail end of a transmission
138 CCallsignSampleProvider *voiceInput = nullptr;
139
140 auto it = std::ranges::find_if(
141 m_voiceInputs, [audioDto](const CCallsignSampleProvider *p) { return p->callsign() == audioDto.callsign; });
142
143 if (it != m_voiceInputs.end())
144 {
145 voiceInput = *it;
146 }
147
148 if (!voiceInput)
149 {
150 it = std::ranges::find_if(m_voiceInputs, [](const CCallsignSampleProvider *p) { return !p->inUse(); });
151 if (it != m_voiceInputs.end())
152 {
153 voiceInput = *it;
154 voiceInput->active(audioDto.callsign, "");
155 }
156 }
157
158 if (voiceInput)
159 {
160 voiceInput->addOpusSamples(audioDto, distanceRatio);
161 }
162
163 const CSettings s = m_audioSettings.get();
164 m_doClickWhenAppropriate = s.afvClicked();
165 m_doBlockWhenAppropriate = s.afvBlocked();
166 }
167
168 void CReceiverSampleProvider::addSilentSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
169 {
170 Q_UNUSED(distanceRatio)
171 if (m_frequencyHz != frequency)
172 {
173 return;
174 } // Lag in the backend means we get the tail end of a transmission
175
176 CCallsignSampleProvider *voiceInput = nullptr;
177 auto it = std::ranges::find_if(
178 m_voiceInputs, [audioDto](const CCallsignSampleProvider *p) { return p->callsign() == audioDto.callsign; });
179
180 if (it != m_voiceInputs.end())
181 {
182 voiceInput = *it;
183 }
184
185 if (!voiceInput)
186 {
187 it = std::ranges::find_if(m_voiceInputs, [](const CCallsignSampleProvider *p) { return !p->inUse(); });
188 if (it != m_voiceInputs.end())
189 {
190 voiceInput = *it;
191 voiceInput->active(audioDto.callsign, "");
192 }
193 }
194
195 if (voiceInput)
196 {
197 voiceInput->addSilentSamples(audioDto);
198 }
199 }
200
201 uint CReceiverSampleProvider::getFrequencyHz() const { return m_frequencyHz; }
202
203 void CReceiverSampleProvider::logVoiceInputs(const QString &prefix, qint64 timeCheckOffsetMs)
204 {
205 if (timeCheckOffsetMs > 100)
206 {
207 const qint64 now = QDateTime::currentMSecsSinceEpoch();
208 if (m_lastLogMessage + timeCheckOffsetMs > now)
209 {
210 return;
211 }
212 m_lastLogMessage = now;
213 }
214
215 QString l;
216 int no = 0;
217 for (const CCallsignSampleProvider *sp : std::as_const(m_voiceInputs))
218 {
219 if (!sp || !sp->inUse())
220 {
221 continue;
222 } // only log the ones in use
223 l += (l.isEmpty() ? QStringLiteral("") : QStringLiteral("\n")) % prefix % QString::number(no++) %
224 QStringLiteral(": ") % sp->toQString();
225 }
226
227 if (l.isEmpty())
228 {
229 return;
230 }
231 CLogMessage(this).debug(l);
232 }
233
234} // namespace swift::core::afv::audio
void addOpusSamples(const IAudioDto &audioDto, float distanceRatio)
Add samples.
void active(const QString &callsign, const QString &aircraftType)
Is active?
void addSilentSamples(const IAudioDto &audioDto)
Add samples.
void addSilentSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
Add samples.
void setFrequency(const uint &frequencyHz)
Frequency.
void receivingCallsignsChanged(const TransceiverReceivingCallsignsChangedArgs &args)
Receving callsigns have changed.
int activeCallsigns() const
Number of active callsign.
int readSamples(QVector< float > &samples, qint64 count)
Read samples.
CReceiverSampleProvider(const QAudioFormat &audioFormat, quint16 id, int voiceInputNumber, QObject *parent=nullptr)
Ctor.
static const QStringList & getLogCategories()
Log.categories.
void addOpusSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
Add samples.
static const QString & vatsimSpecific()
VATSIM specific.
static const QString & audio()
Audio related.
Value object encapsulating information of audio related settings.
bool afvClicked() const
Simplified functions.
bool afvBlocked() const
Simplified functions.
Value object for a set of callsigns.
Definition callsignset.h:26
ISampleProvider(QObject *parent=nullptr)
Ctor.
static const Samples & instance()
Singleton.
Definition samples.cpp:14
Free functions in swift::misc.
QString classNameShort(const QObject *object)
Class name as from QMetaObject::className without namespace.
qint64 currentMSecsSinceEpoch()
void push_back(QList< T >::parameter_type value)
QObject(QObject *parent)
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QString join(QChar separator) const const
QString callsign
Callsign that audio originates from.
Definition dto.h:277