Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom X-Plane dataref for connection state #305

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/core/afv/audio/callsigndelaycache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@

#include "core/afv/audio/callsigndelaycache.h"

#include <algorithm>

namespace swift::core::afv::audio
{
void CallsignDelayCache::initialise(const QString &callsign)
{
if (!m_delayCache.contains(callsign)) { m_delayCache[callsign] = delayDefault; }
if (!successfulTransmissionsCache.contains(callsign)) { successfulTransmissionsCache[callsign] = 0; }
if (!m_successfulTransmissionsCache.contains(callsign)) { m_successfulTransmissionsCache[callsign] = 0; }
}

int CallsignDelayCache::get(const QString &callsign) { return m_delayCache[callsign]; }

void CallsignDelayCache::underflow(const QString &callsign)
{
if (!successfulTransmissionsCache.contains(callsign)) return;
if (!m_successfulTransmissionsCache.contains(callsign)) return;

successfulTransmissionsCache[callsign] = 0;
m_successfulTransmissionsCache[callsign] = 0;
increaseDelayMs(callsign);
}

void CallsignDelayCache::success(const QString &callsign)
{
if (!successfulTransmissionsCache.contains(callsign)) return;
if (!m_successfulTransmissionsCache.contains(callsign)) return;

successfulTransmissionsCache[callsign]++;
if (successfulTransmissionsCache[callsign] > 5)
m_successfulTransmissionsCache[callsign]++;
if (m_successfulTransmissionsCache[callsign] > 5)
{
decreaseDelayMs(callsign);
successfulTransmissionsCache[callsign] = 0;
m_successfulTransmissionsCache[callsign] = 0;
}
}

Expand All @@ -38,15 +40,15 @@ namespace swift::core::afv::audio
if (!m_delayCache.contains(callsign)) return;

m_delayCache[callsign] += delayIncrement;
if (m_delayCache[callsign] > delayMax) { m_delayCache[callsign] = delayMax; }
m_delayCache[callsign] = std::min(m_delayCache[callsign], delayMax);
}

void CallsignDelayCache::decreaseDelayMs(const QString &callsign)
{
if (!m_delayCache.contains(callsign)) return;

m_delayCache[callsign] -= delayIncrement;
if (m_delayCache[callsign] < delayMin) { m_delayCache[callsign] = delayMin; }
m_delayCache[callsign] = std::max(m_delayCache[callsign], delayMin);
}

CallsignDelayCache &CallsignDelayCache::instance()
Expand Down
8 changes: 4 additions & 4 deletions src/core/afv/audio/callsigndelaycache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

//! \file

#ifndef SWIFT_ORE_AFV_AUDIO_CALLSIGNDELAYCACHE_H
#define SWIFT_ORE_AFV_AUDIO_CALLSIGNDELAYCACHE_H
#ifndef SWIFT_CORE_AFV_AUDIO_CALLSIGNDELAYCACHE_H
#define SWIFT_CORE_AFV_AUDIO_CALLSIGNDELAYCACHE_H

#include <QHash>
#include <QString>
Expand Down Expand Up @@ -46,9 +46,9 @@ namespace swift::core::afv::audio
static constexpr int delayMax = 300;

QHash<QString, int> m_delayCache;
QHash<QString, int> successfulTransmissionsCache;
QHash<QString, int> m_successfulTransmissionsCache;
};

} // namespace swift::core::afv::audio

#endif // guard
#endif // SWIFT_CORE_AFV_AUDIO_CALLSIGNDELAYCACHE_H
4 changes: 1 addition & 3 deletions src/core/afv/audio/callsignsampleprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "core/afv/audio/callsignsampleprovider.h"

#include <QDebug>
#include <QStringBuilder>
#include <QStringLiteral>
#include <QtMath>
Expand Down Expand Up @@ -205,8 +204,7 @@ namespace swift::core::afv::audio
{
double crackleFactor = (((qExp(m_distanceRatio) * qPow(m_distanceRatio, -4.0)) / 350.0) - 0.00776652);

if (crackleFactor < 0.0) { crackleFactor = 0.0; }
if (crackleFactor > 0.20) { crackleFactor = 0.20; }
crackleFactor = std::clamp(crackleFactor, 0.0, 0.2);

m_crackleSoundProvider->setGain(crackleFactor * 2);
m_whiteNoise->setGain(m_whiteNoiseGainMin);
Expand Down
4 changes: 1 addition & 3 deletions src/core/afv/audio/callsignsampleprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <QAudioFormat>
#include <QDateTime>
#include <QSharedPointer>
#include <QSoundEffect>
#include <QTimer>

Expand All @@ -17,7 +16,6 @@
#include "sound/sampleprovider/bufferedwaveprovider.h"
#include "sound/sampleprovider/equalizersampleprovider.h"
#include "sound/sampleprovider/mixingsampleprovider.h"
#include "sound/sampleprovider/pinknoisegenerator.h"
#include "sound/sampleprovider/resourcesoundsampleprovider.h"
#include "sound/sampleprovider/sawtoothgenerator.h"
#include "sound/sampleprovider/simplecompressoreffect.h"
Expand Down Expand Up @@ -108,4 +106,4 @@ namespace swift::core::afv::audio
};
} // namespace swift::core::afv::audio

#endif // guard
#endif // SWIFT_CORE_AFV_AUDIO_CALLSIGNSAMPLEPROVIDER_H
3 changes: 0 additions & 3 deletions src/core/afv/audio/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <cmath>

#include <QAudioDevice>
#include <QDebug>
#include <QStringBuilder>
#include <QtGlobal>

#include "misc/logmessage.h"
Expand Down Expand Up @@ -49,7 +47,6 @@ namespace swift::core::afv::audio
const int byteCount = 1920 * m_format.channelCount();
while (m_buffer.size() > byteCount)
{
// qDebug() << QDateTime::currentMSecsSinceEpoch() << "CAudioInputBuffer::writeData " << m_buffer.size();
emit frameAvailable(m_buffer.left(byteCount));
m_buffer.remove(0, byteCount);
}
Expand Down
2 changes: 0 additions & 2 deletions src/core/afv/audio/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#endif

#include <QAudioSource>
#include <QDateTime>
#include <QSharedPointer>
#include <QString>

namespace swift::core::afv::audio
Expand Down
1 change: 0 additions & 1 deletion src/core/afv/audio/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <cmath>

#include <QDebug>
#include <QStringBuilder>

#include "misc/logmessage.h"
Expand Down
8 changes: 4 additions & 4 deletions src/core/afv/audio/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ namespace swift::core::afv::audio
#endif

//! \copydoc QIODevice::readData
virtual qint64 readData(char *data, qint64 maxlen) override;
qint64 readData(char *data, qint64 maxlen) override;

//! \copydoc QIODevice::writeData
virtual qint64 writeData(const char *data, qint64 len) override;
qint64 writeData(const char *data, qint64 len) override;

private:
swift::sound::sample_provider::ISampleProvider *m_sampleProvider = nullptr; //!< related provider
Expand All @@ -71,7 +71,7 @@ namespace swift::core::afv::audio
COutput(QObject *parent = nullptr);

//! Dtor
virtual ~COutput() override { this->stop(); }
~COutput() override { this->stop(); }

//! Start output
void start(const swift::misc::audio::CAudioDeviceInfo &outputDevice,
Expand Down Expand Up @@ -103,4 +103,4 @@ namespace swift::core::afv::audio
};
} // namespace swift::core::afv::audio

#endif // guard
#endif // SWIFT_CORE_AFV_AUDIO_OUTPUT_H
14 changes: 5 additions & 9 deletions src/core/afv/audio/receiversampleprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "core/afv/audio/receiversampleprovider.h"

#include <QDebug>
#include <QStringBuilder>

#include "misc/logmessage.h"
Expand Down Expand Up @@ -65,9 +64,8 @@ namespace swift::core::afv::audio

int CReceiverSampleProvider::activeCallsigns() const
{
const int numberOfCallsigns =
static_cast<int>(std::count_if(m_voiceInputs.begin(), m_voiceInputs.end(),
[](const CCallsignSampleProvider *p) { return p->inUse() == true; }));
const int numberOfCallsigns = static_cast<int>(std::count_if(
m_voiceInputs.begin(), m_voiceInputs.end(), [](const CCallsignSampleProvider *p) { return p->inUse(); }));
return numberOfCallsigns;
}

Expand All @@ -92,11 +90,9 @@ namespace swift::core::afv::audio

if (m_doClickWhenAppropriate && numberOfInUseInputs == 0)
{
CResourceSoundSampleProvider *resourceSound =
new CResourceSoundSampleProvider(Samples::instance().click(), m_mixer);
auto *resourceSound = new CResourceSoundSampleProvider(Samples::instance().click(), m_mixer);
m_mixer->addMixerInput(resourceSound);
m_doClickWhenAppropriate = false;
// CLogMessage(this).debug(u"AFV Click...");
}

//! \todo KB 2020-04 not entirely correct, as it can be the number is the same, but changed callsign
Expand Down Expand Up @@ -132,7 +128,7 @@ namespace swift::core::afv::audio
if (!voiceInput)
{
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(),
[](const CCallsignSampleProvider *p) { return p->inUse() == false; });
[](const CCallsignSampleProvider *p) { return !p->inUse(); });
if (it != m_voiceInputs.end())
{
voiceInput = *it;
Expand Down Expand Up @@ -162,7 +158,7 @@ namespace swift::core::afv::audio
if (!voiceInput)
{
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(),
[](const CCallsignSampleProvider *p) { return p->inUse() == false; });
[](const CCallsignSampleProvider *p) { return !p->inUse(); });
if (it != m_voiceInputs.end())
{
voiceInput = *it;
Expand Down
7 changes: 3 additions & 4 deletions src/core/afv/audio/receiversampleprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "core/afv/audio/callsignsampleprovider.h"
#include "misc/audio/audiosettings.h"
#include "misc/aviation/callsignset.h"
#include "misc/logcategories.h"
#include "sound/sampleprovider/mixingsampleprovider.h"
#include "sound/sampleprovider/sampleprovider.h"
#include "sound/sampleprovider/sinusgenerator.h"
Expand All @@ -22,7 +21,7 @@ namespace swift::core::afv::audio
//! Arguments
struct TransceiverReceivingCallsignsChangedArgs
{
quint16 transceiverID; //!< transceiver id
quint16 transceiverID {}; //!< transceiver id
QStringList receivingCallsigns; //!< callsigns
};

Expand Down Expand Up @@ -58,7 +57,7 @@ namespace swift::core::afv::audio
//! @}

//! \copydoc swift::sound::sample_provider::ISampleProvider::readSamples
virtual int readSamples(QVector<float> &samples, qint64 count) override;
int readSamples(QVector<float> &samples, qint64 count) override;

//! @{
//! Add samples
Expand Down Expand Up @@ -117,4 +116,4 @@ namespace swift::core::afv::audio

Q_DECLARE_METATYPE(swift::core::afv::audio::TransceiverReceivingCallsignsChangedArgs)

#endif // guard
#endif // SWIFT_CORE_AFV_AUDIO_RECEIVERSAMPLEPROVIDER_H
4 changes: 2 additions & 2 deletions src/core/afv/audio/soundcardsampleprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace swift::core::afv::audio
void pttUpdate(bool active, const QVector<TxTransceiverDto> &txTransceivers);

//! \copydoc swift::sound::sample_provider::ISampleProvider::readSamples
virtual int readSamples(QVector<float> &samples, qint64 count) override;
int readSamples(QVector<float> &samples, qint64 count) override;

//! Add OPUS samples
void addOpusSamples(const IAudioDto &audioDto, const QVector<RxTransceiverDto> &rxTransceivers);
Expand Down Expand Up @@ -65,4 +65,4 @@ namespace swift::core::afv::audio

} // namespace swift::core::afv::audio

#endif // guard
#endif // SWIFT_CORE_AFV_AUDIO_SOUNDCARDSAMPLEPROVIDER_H
11 changes: 0 additions & 11 deletions src/core/afv/clients/afvclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "core/afv/clients/afvclient.h"

#include <QDebug>

#ifdef Q_OS_WIN
# include "comdef.h"
#endif
Expand Down Expand Up @@ -33,15 +31,6 @@ using namespace swift::sound::sample_provider;

namespace swift::core::afv::clients
{
constexpr int CAfvClient::PositionUpdatesMs;
constexpr int CAfvClient::SampleRate;
constexpr int CAfvClient::FrameSize;
constexpr double CAfvClient::MinDbIn;
constexpr double CAfvClient::MaxDbIn;
constexpr double CAfvClient::MinDbOut;
constexpr double CAfvClient::MaxDbOut;
constexpr quint32 CAfvClient::UniCom;

const QStringList &CAfvClient::getLogCategories()
{
static const QStringList cats { CLogCategories::audio(), CLogCategories::vatsimSpecific() };
Expand Down
3 changes: 0 additions & 3 deletions src/core/afv/clients/afvclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#include <atomic>

#include <QAudioInput>
#include <QAudioOutput>
#include <QDateTime>
#include <QObject>
#include <QString>
#include <QVector>
Expand Down
1 change: 0 additions & 1 deletion src/core/afv/connection/apiserverconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <QJsonArray>
#include <QJsonObject>
#include <QMetaEnum>
#include <QPointer>
#include <QScopedPointer>
#include <QUrl>
Expand Down
2 changes: 0 additions & 2 deletions src/core/afv/connection/apiserverconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#ifndef SWIFT_CORE_AFV_CONNECTION_APISERVERCONNECTION_H
#define SWIFT_CORE_AFV_CONNECTION_APISERVERCONNECTION_H

#include <QDebug>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QJsonDocument>
#include <QNetworkAccessManager>
Expand Down
3 changes: 2 additions & 1 deletion src/core/afv/connection/clientconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QString>
#include <QTimer>
#include <QUdpSocket>
#include <QUuid>

#include "core/afv/connection/apiserverconnection.h"
#include "core/afv/connection/clientconnectiondata.h"
Expand Down Expand Up @@ -137,4 +138,4 @@ namespace swift::core::afv::connection
};
} // namespace swift::core::afv::connection

#endif // guard
#endif // SWIFT_CORE_AFV_CONNECTION_CLIENTCONNECTION_H
2 changes: 0 additions & 2 deletions src/core/afv/connection/clientconnectiondata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#include "core/afv/connection/clientconnectiondata.h"

#include <QDebug>

#include "misc/logmessage.h"

using namespace swift::misc;
Expand Down
3 changes: 1 addition & 2 deletions src/core/afv/connection/clientconnectiondata.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "core/afv/connection/apiserverconnection.h"
#include "core/afv/crypto/cryptodtochannel.h"
#include "core/afv/dto.h"
#include "misc/logcategories.h"

namespace swift::core::afv::connection
{
Expand Down Expand Up @@ -114,4 +113,4 @@ namespace swift::core::afv::connection
};
} // namespace swift::core::afv::connection

#endif // guard
#endif // SWIFT_CORE_AFV_CONNECTION_CLIENTCONNECTIONDATA_H
2 changes: 1 addition & 1 deletion src/core/afv/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ namespace swift::core::afv
constexpr int c_sampleRate = 48000;
} // namespace swift::core::afv

#endif // guard
#endif // SWIFT_CORE_AFV_CONSTANTS_H
Loading
Loading