[AFV] misc style issues:

* object name
* simple style fixes
* some renamings
* comments
This commit is contained in:
Klaus Basan
2020-04-23 20:19:59 +02:00
committed by Mat Sutcliffe
parent c1622951b3
commit 7d51bedc3e
18 changed files with 170 additions and 107 deletions

View File

@@ -6,8 +6,6 @@
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \file
#include "callsignsampleprovider.h"
#include "callsigndelaycache.h"
@@ -15,6 +13,7 @@
#include "blacksound/sampleprovider/samples.h"
#include "blacksound/audioutilities.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/metadatautils.h"
#include "blackconfig/buildconfig.h"
#include <QtMath>
@@ -30,15 +29,18 @@ namespace BlackCore
{
namespace Audio
{
CallsignSampleProvider::CallsignSampleProvider(const QAudioFormat &audioFormat, const CReceiverSampleProvider *receiver, QObject *parent) :
CCallsignSampleProvider::CCallsignSampleProvider(const QAudioFormat &audioFormat, const CReceiverSampleProvider *receiver, QObject *parent) :
ISampleProvider(parent),
m_audioFormat(audioFormat),
m_receiver(receiver),
m_decoder(audioFormat.sampleRate(), 1)
{
Q_ASSERT(audioFormat.channelCount() == 1);
Q_ASSERT(receiver);
const QString on = QStringLiteral("%1").arg(classNameShort(this));
this->setObjectName(on);
this->setObjectName("CallsignSampleProvider");
m_mixer = new CMixingSampleProvider(this);
m_crackleSoundProvider = new CResourceSoundSampleProvider(Samples::instance().crackle(), m_mixer);
m_crackleSoundProvider->setLooping(true);
@@ -57,23 +59,23 @@ namespace BlackCore
m_simpleCompressorEffect->setMakeUpGain(-5.5);
// Create the voice EQ
m_voiceEq = new CEqualizerSampleProvider(m_simpleCompressorEffect, EqualizerPresets::VHFEmulation, m_mixer);
m_voiceEqualizer = new CEqualizerSampleProvider(m_simpleCompressorEffect, EqualizerPresets::VHFEmulation, m_mixer);
m_mixer->addMixerInput(m_whiteNoise);
m_mixer->addMixerInput(m_acBusNoise);
m_mixer->addMixerInput(m_hfWhiteNoise);
m_mixer->addMixerInput(m_voiceEq);
m_mixer->addMixerInput(m_voiceEqualizer);
m_timer = new QTimer(this);
m_timer->setObjectName(this->objectName() + "m_timer");
m_timer->setObjectName(this->objectName() + ":m_timer");
m_timer->setInterval(100);
connect(m_timer, &QTimer::timeout, this, &CallsignSampleProvider::timerElapsed);
connect(m_timer, &QTimer::timeout, this, &CCallsignSampleProvider::timerElapsed);
}
int CallsignSampleProvider::readSamples(QVector<float> &samples, qint64 count)
int CCallsignSampleProvider::readSamples(QVector<float> &samples, qint64 count)
{
int noOfSamples = m_mixer->readSamples(samples, count);
const int noOfSamples = m_mixer->readSamples(samples, count);
if (m_inUse && m_lastPacketLatch && m_audioInput->getBufferedBytes() == 0)
{
@@ -91,7 +93,7 @@ namespace BlackCore
return noOfSamples;
}
void CallsignSampleProvider::timerElapsed()
void CCallsignSampleProvider::timerElapsed()
{
if (m_inUse && m_audioInput->getBufferedBytes() == 0 && m_lastSamplesAddedUtc.msecsTo(QDateTime::currentDateTimeUtc()) > m_idleTimeoutMs)
{
@@ -99,7 +101,7 @@ namespace BlackCore
}
}
void CallsignSampleProvider::active(const QString &callsign, const QString &aircraftType)
void CCallsignSampleProvider::active(const QString &callsign, const QString &aircraftType)
{
m_callsign = callsign;
CallsignDelayCache::instance().initialise(callsign);
@@ -109,17 +111,17 @@ namespace BlackCore
setEffects();
m_underflow = false;
int delayMs = CallsignDelayCache::instance().get(callsign);
const int delayMs = CallsignDelayCache::instance().get(callsign);
if (verbose()) { CLogMessage(this).debug(u"[%1] [Delay %2ms]") << m_callsign << delayMs; }
if (delayMs > 0)
{
int phaseDelayLength = (m_audioFormat.sampleRate() / 1000) * delayMs;
QVector<float> phaseDelay(phaseDelayLength * 2, 0);
const int phaseDelayLength = (m_audioFormat.sampleRate() / 1000) * delayMs;
const QVector<float> phaseDelay(phaseDelayLength * 2, 0);
m_audioInput->addSamples(phaseDelay);
}
}
void CallsignSampleProvider::activeSilent(const QString &callsign, const QString &aircraftType)
void CCallsignSampleProvider::activeSilent(const QString &callsign, const QString &aircraftType)
{
m_callsign = callsign;
CallsignDelayCache::instance().initialise(callsign);
@@ -130,18 +132,18 @@ namespace BlackCore
m_underflow = true;
}
void CallsignSampleProvider::clear()
void CCallsignSampleProvider::clear()
{
idle();
m_audioInput->clearBuffer();
}
void CallsignSampleProvider::addOpusSamples(const IAudioDto &audioDto, float distanceRatio)
void CCallsignSampleProvider::addOpusSamples(const IAudioDto &audioDto, float distanceRatio)
{
m_distanceRatio = distanceRatio;
setEffects();
QVector<qint16> audio = decodeOpus(audioDto.audio);
const QVector<qint16> audio = decodeOpus(audioDto.audio);
m_audioInput->addSamples(BlackSound::convertFromShortToFloat(audio));
m_lastPacketLatch = audioDto.lastPacket;
if (audioDto.lastPacket && !m_underflow) { CallsignDelayCache::instance().success(m_callsign); }
@@ -149,7 +151,7 @@ namespace BlackCore
if (!m_timer->isActive()) { m_timer->start(); }
}
void CallsignSampleProvider::addSilentSamples(const IAudioDto &audioDto)
void CCallsignSampleProvider::addSilentSamples(const IAudioDto &audioDto)
{
// Disable all audio effects
setEffects(true);
@@ -161,7 +163,7 @@ namespace BlackCore
if (!m_timer->isActive()) { m_timer->start(); }
}
void CallsignSampleProvider::idle()
void CCallsignSampleProvider::idle()
{
m_timer->stop();
m_inUse = false;
@@ -170,14 +172,14 @@ namespace BlackCore
m_type.clear();
}
QVector<qint16> CallsignSampleProvider::decodeOpus(const QByteArray &opusData)
QVector<qint16> CCallsignSampleProvider::decodeOpus(const QByteArray &opusData)
{
int decodedLength = 0;
QVector<qint16> decoded = m_decoder.decode(opusData, opusData.size(), &decodedLength);
const QVector<qint16> decoded = m_decoder.decode(opusData, opusData.size(), &decodedLength);
return decoded;
}
void CallsignSampleProvider::setEffects(bool noEffects)
void CCallsignSampleProvider::setEffects(bool noEffects)
{
if (noEffects || m_bypassEffects || !m_inUse)
{
@@ -186,7 +188,7 @@ namespace BlackCore
m_hfWhiteNoise->setGain(0.0);
m_acBusNoise->setGain(0.0);
m_simpleCompressorEffect->setEnabled(false);
m_voiceEq->setBypassEffects(true);
m_voiceEqualizer->setBypassEffects(true);
}
else
{
@@ -201,8 +203,8 @@ namespace BlackCore
m_hfWhiteNoise->setGain(m_hfWhiteNoiseGainMin);
m_acBusNoise->setGain(m_acBusGainMin + 0.001f);
m_simpleCompressorEffect->setEnabled(true);
m_voiceEq->setBypassEffects(false);
m_voiceEq->setOutputGain(0.38);
m_voiceEqualizer->setBypassEffects(false);
m_voiceEqualizer->setOutputGain(0.38);
m_whiteNoise->setGain(0.0);
}
else
@@ -216,13 +218,13 @@ namespace BlackCore
m_whiteNoise->setGain(m_whiteNoiseGainMin);
m_acBusNoise->setGain(m_acBusGainMin);
m_simpleCompressorEffect->setEnabled(true);
m_voiceEq->setBypassEffects(false);
m_voiceEq->setOutputGain(1.0 - crackleFactor * 3.7);
m_voiceEqualizer->setBypassEffects(false);
m_voiceEqualizer->setOutputGain(1.0 - crackleFactor * 3.7);
}
}
}
void CallsignSampleProvider::setBypassEffects(bool bypassEffects)
void CCallsignSampleProvider::setBypassEffects(bool bypassEffects)
{
m_bypassEffects = bypassEffects;
setEffects();

View File

@@ -35,14 +35,14 @@ namespace BlackCore
{
class CReceiverSampleProvider;
//! Callsign provide
class CallsignSampleProvider : public BlackSound::SampleProvider::ISampleProvider
//! Callsign provider
class CCallsignSampleProvider : public BlackSound::SampleProvider::ISampleProvider
{
Q_OBJECT
public:
//! Ctor
CallsignSampleProvider(const QAudioFormat &audioFormat, const BlackCore::Afv::Audio::CReceiverSampleProvider *receiver, QObject *parent = nullptr);
CCallsignSampleProvider(const QAudioFormat &audioFormat, const BlackCore::Afv::Audio::CReceiverSampleProvider *receiver, QObject *parent = nullptr);
//! Read samples
int readSamples(QVector<float> &samples, qint64 count) override;
@@ -93,15 +93,15 @@ namespace BlackCore
bool m_bypassEffects = false;
float m_distanceRatio = 1.0;
const CReceiverSampleProvider *m_receiver = nullptr;
BlackSound::SampleProvider::CMixingSampleProvider *m_mixer = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_crackleSoundProvider = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_whiteNoise = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_hfWhiteNoise = nullptr;
BlackSound::SampleProvider::CSawToothGenerator *m_acBusNoise = nullptr;
const CReceiverSampleProvider *m_receiver = nullptr;
BlackSound::SampleProvider::CMixingSampleProvider *m_mixer = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_crackleSoundProvider = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_whiteNoise = nullptr;
BlackSound::SampleProvider::CResourceSoundSampleProvider *m_hfWhiteNoise = nullptr;
BlackSound::SampleProvider::CSawToothGenerator *m_acBusNoise = nullptr;
BlackSound::SampleProvider::CSimpleCompressorEffect *m_simpleCompressorEffect = nullptr;
BlackSound::SampleProvider::CEqualizerSampleProvider *m_voiceEq = nullptr;
BlackSound::SampleProvider::CBufferedWaveProvider *m_audioInput = nullptr;
BlackSound::SampleProvider::CEqualizerSampleProvider *m_voiceEqualizer = nullptr;
BlackSound::SampleProvider::CBufferedWaveProvider *m_audioInput = nullptr;
QTimer *m_timer = nullptr;
BlackSound::Codecs::COpusDecoder m_decoder;

View File

@@ -6,10 +6,9 @@
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \file
#include "output.h"
#include "blacksound/audioutilities.h"
#include "blackmisc/metadatautils.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/verify.h"
@@ -32,7 +31,9 @@ namespace BlackCore
QIODevice(parent),
m_sampleProvider(sampleProvider)
{
this->setObjectName("CAudioOutputBuffer");
Q_ASSERT_X(sampleProvider, Q_FUNC_INFO, "need sample provide");
const QString on = QStringLiteral("%1 for %2").arg(classNameShort(this), sampleProvider->objectName());
this->setObjectName(on);
}
qint64 CAudioOutputBuffer::readData(char *data, qint64 maxlen)
@@ -43,7 +44,7 @@ namespace BlackCore
QVector<float> buffer;
m_sampleProvider->readSamples(buffer, count);
for (float sample : buffer)
for (float sample : as_const(buffer))
{
const float absSample = qAbs(sample);
if (absSample > m_maxSampleOutput) { m_maxSampleOutput = absSample; }
@@ -54,14 +55,14 @@ namespace BlackCore
{
OutputVolumeStreamArgs outputVolumeStreamArgs;
outputVolumeStreamArgs.PeakRaw = m_maxSampleOutput / 1.0;
outputVolumeStreamArgs.PeakDb = static_cast<float>(20 * std::log10(outputVolumeStreamArgs.PeakRaw));
outputVolumeStreamArgs.PeakDb = static_cast<float>(20 * std::log10(outputVolumeStreamArgs.PeakRaw));
const double db = qBound(m_minDb, outputVolumeStreamArgs.PeakDb, m_maxDb);
double ratio = (db - m_minDb) / (m_maxDb - m_minDb);
if (ratio < 0.30) { ratio = 0.0; }
if (ratio > 1.0) { ratio = 1.0; }
outputVolumeStreamArgs.PeakVU = ratio;
emit outputVolumeStream(outputVolumeStreamArgs);
m_sampleCount = 0;
m_sampleCount = 0;
m_maxSampleOutput = 0;
}
@@ -83,7 +84,7 @@ namespace BlackCore
COutput::COutput(QObject *parent) : QObject(parent)
{
this->setObjectName("COutput");
this->setObjectName(classNameShort(this));
}
void COutput::start(const CAudioDeviceInfo &outputDevice, ISampleProvider *sampleProvider)

View File

@@ -10,6 +10,7 @@
#include "receiversampleprovider.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/metadatautils.h"
#include "blacksound/sampleprovider/resourcesoundsampleprovider.h"
#include "blacksound/sampleprovider/samples.h"
@@ -36,11 +37,13 @@ namespace BlackCore
ISampleProvider(parent),
m_id(id)
{
m_mixer = new CMixingSampleProvider(this);
const QString on = QStringLiteral("%1 id: %2").arg(classNameShort(this)).arg(id);
this->setObjectName(on);
m_mixer = new CMixingSampleProvider(this);
for (int i = 0; i < voiceInputNumber; i++)
{
const auto voiceInput = new CallsignSampleProvider(audioFormat, this, m_mixer);
const auto voiceInput = new CCallsignSampleProvider(audioFormat, this, m_mixer);
m_voiceInputs.push_back(voiceInput);
m_mixer->addMixerInput(voiceInput);
}
@@ -52,7 +55,7 @@ namespace BlackCore
void CReceiverSampleProvider::setBypassEffects(bool value)
{
for (CallsignSampleProvider *voiceInput : m_voiceInputs)
for (CCallsignSampleProvider *voiceInput : m_voiceInputs)
{
voiceInput->setBypassEffects(value);
}
@@ -62,7 +65,7 @@ namespace BlackCore
{
if (frequencyHz != m_frequencyHz)
{
for (CallsignSampleProvider *voiceInput : m_voiceInputs)
for (CCallsignSampleProvider *voiceInput : m_voiceInputs)
{
voiceInput->clear();
}
@@ -72,7 +75,7 @@ namespace BlackCore
int CReceiverSampleProvider::activeCallsigns() const
{
const int numberOfCallsigns = static_cast<int>(std::count_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CallsignSampleProvider * p)
const int numberOfCallsigns = static_cast<int>(std::count_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CCallsignSampleProvider * p)
{
return p->inUse() == true;
}));
@@ -84,7 +87,7 @@ namespace BlackCore
m_mute = value;
if (value)
{
for (CallsignSampleProvider *voiceInput : m_voiceInputs)
for (CCallsignSampleProvider *voiceInput : m_voiceInputs)
{
voiceInput->clear();
}
@@ -108,14 +111,14 @@ namespace BlackCore
{
CResourceSoundSampleProvider *resourceSound = new CResourceSoundSampleProvider(Samples::instance().click(), m_mixer);
m_mixer->addMixerInput(resourceSound);
// CLogMessage(this).debug(u"AFV Click...");
m_doClickWhenAppropriate = false;
// CLogMessage(this).debug(u"AFV Click...");
}
if (numberOfInUseInputs != m_lastNumberOfInUseInputs)
{
QStringList receivingCallsigns;
for (const CallsignSampleProvider *voiceInput : m_voiceInputs)
for (const CCallsignSampleProvider *voiceInput : m_voiceInputs)
{
const QString callsign = voiceInput->callsign();
if (! callsign.isEmpty())
@@ -136,9 +139,9 @@ namespace BlackCore
void CReceiverSampleProvider::addOpusSamples(const IAudioDto &audioDto, uint frequency, float distanceRatio)
{
if (m_frequencyHz != frequency) { return; } // Lag in the backend means we get the tail end of a transmission
CallsignSampleProvider *voiceInput = nullptr;
CCallsignSampleProvider *voiceInput = nullptr;
auto it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [audioDto](const CallsignSampleProvider * p)
auto it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [audioDto](const CCallsignSampleProvider * p)
{
return p->callsign() == audioDto.callsign;
});
@@ -150,7 +153,7 @@ namespace BlackCore
if (! voiceInput)
{
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CallsignSampleProvider * p) { return p->inUse() == false; });
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CCallsignSampleProvider * p) { return p->inUse() == false; });
if (it != m_voiceInputs.end())
{
voiceInput = *it;
@@ -173,8 +176,8 @@ namespace BlackCore
Q_UNUSED(distanceRatio)
if (m_frequencyHz != frequency) { return; } // Lag in the backend means we get the tail end of a transmission
CallsignSampleProvider *voiceInput = nullptr;
auto it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [audioDto](const CallsignSampleProvider * p)
CCallsignSampleProvider *voiceInput = nullptr;
auto it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [audioDto](const CCallsignSampleProvider * p)
{
return p->callsign() == audioDto.callsign;
});
@@ -186,7 +189,7 @@ namespace BlackCore
if (!voiceInput)
{
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CallsignSampleProvider * p) { return p->inUse() == false; });
it = std::find_if(m_voiceInputs.begin(), m_voiceInputs.end(), [](const CCallsignSampleProvider * p) { return p->inUse() == false; });
if (it != m_voiceInputs.end())
{
voiceInput = *it;

View File

@@ -103,7 +103,7 @@ namespace BlackCore
BlackSound::SampleProvider::CVolumeSampleProvider *m_volume = nullptr;
BlackSound::SampleProvider::CMixingSampleProvider *m_mixer = nullptr;
BlackSound::SampleProvider::CSinusGenerator *m_blockTone = nullptr;
QVector<CallsignSampleProvider *> m_voiceInputs;
QVector<CCallsignSampleProvider *> m_voiceInputs;
QString m_receivingCallsignsString;
BlackMisc::Aviation::CCallsignSet m_receivingCallsigns;

View File

@@ -6,10 +6,10 @@
* or distributed except according to the terms contained in the LICENSE file.
*/
//! \file
#include "soundcardsampleprovider.h"
#include "blackmisc/metadatautils.h"
using namespace BlackMisc;
using namespace BlackSound::SampleProvider;
namespace BlackCore
@@ -22,6 +22,9 @@ namespace BlackCore
ISampleProvider(parent),
m_mixer(new CMixingSampleProvider())
{
const QString on = QStringLiteral("%1 sample rate: %2, transceivers: %3").arg(classNameShort(this)).arg(sampleRate).arg(transceiverIDs.size());
this->setObjectName(on);
m_waveFormat.setSampleRate(sampleRate);
m_waveFormat.setChannelCount(1);
m_waveFormat.setSampleSize(16);
@@ -32,9 +35,10 @@ namespace BlackCore
m_mixer = new CMixingSampleProvider(this);
m_receiverIDs = transceiverIDs;
constexpr int voiceInputNumber = 4; // number of CallsignSampleProviders
for (quint16 transceiverID : transceiverIDs)
{
CReceiverSampleProvider *transceiverInput = new CReceiverSampleProvider(m_waveFormat, transceiverID, 4, m_mixer);
CReceiverSampleProvider *transceiverInput = new CReceiverSampleProvider(m_waveFormat, transceiverID, voiceInputNumber, m_mixer);
connect(transceiverInput, &CReceiverSampleProvider::receivingCallsignsChanged, this, &CSoundcardSampleProvider::receivingCallsignsChanged);
m_receiverInputs.push_back(transceiverInput);
m_receiverIDs.push_back(transceiverID);
@@ -111,7 +115,7 @@ namespace BlackCore
QVector<quint16> handledTransceiverIDs;
for (int i = 0; i < rxTransceiversFilteredAndSorted.size(); i++)
{
RxTransceiverDto rxTransceiver = rxTransceiversFilteredAndSorted[i];
const RxTransceiverDto rxTransceiver = rxTransceiversFilteredAndSorted[i];
if (!handledTransceiverIDs.contains(rxTransceiver.id))
{
handledTransceiverIDs.push_back(rxTransceiver.id);
@@ -121,12 +125,13 @@ namespace BlackCore
{
return p->getId() == rxTransceiver.id;
});
if (it != m_receiverInputs.end())
{
receiverInput = *it;
}
if (! receiverInput) { continue; }
if (!receiverInput) { continue; }
if (receiverInput->getMute()) { continue; }
if (!audioPlayed)
@@ -160,9 +165,9 @@ namespace BlackCore
for (CReceiverSampleProvider *receiverInput : m_receiverInputs)
{
quint16 transceiverID = receiverInput->getId();
bool contains = std::any_of(radioTransceivers.begin(), radioTransceivers.end(), [&](const auto & tx) { return transceiverID == tx.id; });
if (! contains)
const quint16 transceiverID = receiverInput->getId();
const bool contains = std::any_of(radioTransceivers.cbegin(), radioTransceivers.cend(), [ transceiverID ](const auto &tx) { return transceiverID == tx.id; });
if (!contains)
{
receiverInput->setFrequency(0);
}

View File

@@ -8,7 +8,6 @@
#include "cryptodtoserializer.h"
namespace BlackCore
{
namespace Afv

View File

@@ -149,7 +149,7 @@ namespace BlackCore
info.m_index = index;
++index;
info.m_function = function;
info.m_action = action;
info.m_action = action;
info.m_receiver = receiver;
m_boundActions.push_back(info);
return info.m_index;

View File

@@ -1,4 +1,5 @@
#include "bufferedwaveprovider.h"
#include "blacksound/audioutilities.h"
#include <QDebug>
@@ -9,6 +10,9 @@ namespace BlackSound
CBufferedWaveProvider::CBufferedWaveProvider(const QAudioFormat &format, QObject *parent) :
ISampleProvider(parent)
{
const QString on = QStringLiteral("%1 format: ").arg(this->metaObject()->className(), BlackSound::toQString(format));
this->setObjectName(on);
// Set buffer size to 10 secs
m_maxBufferSize = format.bytesForDuration(10 * 1000 * 1000);
}

View File

@@ -11,13 +11,17 @@ namespace BlackSound
CEqualizerSampleProvider::CEqualizerSampleProvider(ISampleProvider *sourceProvider, EqualizerPresets preset, QObject *parent) :
ISampleProvider(parent)
{
Q_ASSERT_X(sourceProvider, Q_FUNC_INFO, "Need provider");
const QString on = QStringLiteral("%1 of %2").arg(this->metaObject()->className(), sourceProvider->objectName());
this->setObjectName(on);
m_sourceProvider = sourceProvider;
setupPreset(preset);
}
int CEqualizerSampleProvider::readSamples(QVector<float> &samples, qint64 count)
{
int samplesRead = m_sourceProvider->readSamples(samples, count);
const int samplesRead = m_sourceProvider->readSamples(samples, count);
if (m_bypass) return samplesRead;
for (int n = 0; n < samplesRead; n++)
@@ -26,7 +30,7 @@ namespace BlackSound
{
samples[n] = m_filters[band].transform(samples[n]);
}
samples[n] *= m_outputGain;
samples[n] *= static_cast<float>(m_outputGain);
}
return samplesRead;
}

View File

@@ -7,11 +7,29 @@
*/
#include "mixingsampleprovider.h"
#include "blackmisc/metadatautils.h"
using namespace BlackMisc;
namespace BlackSound
{
namespace SampleProvider
{
CMixingSampleProvider::CMixingSampleProvider(QObject *parent) : ISampleProvider(parent)
{
const QString on = QStringLiteral("%1").arg(classNameShort(this));
this->setObjectName(on);
}
void CMixingSampleProvider::addMixerInput(ISampleProvider *provider)
{
Q_ASSERT(provider);
m_sources.append(provider);
const QString on = QStringLiteral("%1 sources: %2").arg(classNameShort(this)).arg(m_sources.size());
this->setObjectName(on);
}
int CMixingSampleProvider::readSamples(QVector<float> &samples, qint64 count)
{
samples.clear();
@@ -23,15 +41,14 @@ namespace BlackSound
{
ISampleProvider *sampleProvider = m_sources.at(i);
QVector<float> sourceBuffer;
int len = sampleProvider->readSamples(sourceBuffer, count);
const int len = sampleProvider->readSamples(sourceBuffer, count);
for (int n = 0; n < len; n++)
{
samples[n] += sourceBuffer[n];
}
outputLen = qMax(len, outputLen);
if (sampleProvider->isFinished())
{
finishedProviders.push_back(sampleProvider);

View File

@@ -25,10 +25,10 @@ namespace BlackSound
{
public:
//! Ctor mixing provider
CMixingSampleProvider(QObject *parent = nullptr) : ISampleProvider(parent) {}
CMixingSampleProvider(QObject *parent = nullptr);
//! Add a provider
void addMixerInput(ISampleProvider *provider) { m_sources.append(provider); }
void addMixerInput(ISampleProvider *provider);
//! \copydoc ISampleProvider::readSamples
virtual int readSamples(QVector<float> &samples, qint64 count) override;

View File

@@ -54,6 +54,12 @@ namespace BlackSound
return true;
}
const QString &CResourceSound::getFileName() const
{
static const QString empty;
return m_data ? empty : m_data->fileName;
}
bool CResourceSound::isSameFileName(const QString &fn) const
{
if (fn.isEmpty()) { return false; }

View File

@@ -52,7 +52,7 @@ namespace BlackSound
const QVector<float> &audioData() const { return m_data->samples; }
//! Corresponding file
const QString &getFileName() { return m_data->fileName; }
const QString &getFileName() const;
//! Is same file?
bool isSameFileName(const QString &fn) const;

View File

@@ -1,7 +1,18 @@
/* Copyright (C) 2019
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
* or distributed except according to the terms contained in the LICENSE file.
*/
#include "resourcesoundsampleprovider.h"
#include "resourcesoundsampleprovider.h"
#include "blackmisc/metadatautils.h"
#include <QDebug>
using namespace BlackMisc;
namespace BlackSound
{
namespace SampleProvider
@@ -10,21 +21,21 @@ namespace BlackSound
ISampleProvider(parent),
m_resourceSound(resourceSound)
{
const QString on = QStringLiteral("%1 %2").arg(classNameShort(this), resourceSound.getFileName());
this->setObjectName(on);
m_tempBuffer.resize(m_tempBufferSize);
}
int CResourceSoundSampleProvider::readSamples(QVector<float> &samples, qint64 count)
{
if (! m_resourceSound.isLoaded()) { return 0; }
if (!m_resourceSound.isLoaded()) { return 0; }
if (count > m_tempBufferSize)
{
qDebug() << "Count too large for temp buffer" << count;
return 0;
}
qint64 availableSamples = m_resourceSound.audioData().size() - m_position;
const qint64 samplesToCopy = qMin(availableSamples, count);
const qint64 availableSamples = m_resourceSound.audioData().size() - m_position;
const qint64 samplesToCopy = qMin(availableSamples, count);
samples.clear();
samples.fill(0, static_cast<int>(samplesToCopy));
@@ -37,7 +48,7 @@ namespace BlackSound
{
for (int i = 0; i < samplesToCopy; i++)
{
m_tempBuffer[i] *= m_gain;
m_tempBuffer[i] = static_cast<float>(m_gain * m_tempBuffer[i]);
}
}

View File

@@ -7,16 +7,22 @@
*/
#include "sinusgenerator.h"
#include "blackmisc/metadatautils.h"
#include <cmath>
using namespace BlackMisc;
namespace BlackSound
{
namespace SampleProvider
{
CSinusGenerator::CSinusGenerator(double frequency, QObject *parent) :
CSinusGenerator::CSinusGenerator(double frequencyHz, QObject *parent) :
ISampleProvider(parent),
m_frequency(frequency)
{}
m_frequencyHz(frequencyHz)
{
const QString on = QStringLiteral("%1 frequency: %2Hz").arg(classNameShort(this)).arg(frequencyHz);
this->setObjectName(on);
}
int CSinusGenerator::readSamples(QVector<float> &samples, qint64 count)
{
@@ -25,17 +31,17 @@ namespace BlackSound
for (int sampleCount = 0; sampleCount < count; sampleCount++)
{
double multiple = m_twoPi * m_frequency / m_sampleRate;
double sampleValue = m_gain * qSin(m_nSample * multiple);
samples[sampleCount] = static_cast<float>(sampleValue);
const double multiple = s_twoPi * m_frequencyHz / m_sampleRate;
const double sampleValue = m_gain * qSin(m_nSample * multiple);
samples[sampleCount] = static_cast<float>(sampleValue);
m_nSample++;
}
return static_cast<int>(count);
}
void CSinusGenerator::setFrequency(double frequency)
void CSinusGenerator::setFrequency(double frequencyHz)
{
m_frequency = frequency;
m_frequencyHz = frequencyHz;
}
} // ns
} // ns

View File

@@ -27,7 +27,7 @@ namespace BlackSound
public:
//! Ctor
CSinusGenerator(double frequency, QObject *parent = nullptr);
CSinusGenerator(double frequencyHz, QObject *parent = nullptr);
//! \copydoc ISampleProvider::readSamples
virtual int readSamples(QVector<float> &samples, qint64 count) override;
@@ -36,14 +36,14 @@ namespace BlackSound
void setGain(double gain) { m_gain = gain; }
//! Set frequency in Hz
void setFrequency(double frequency);
void setFrequency(double frequencyHz);
private:
double m_gain = 0.0;
double m_frequency = 0.0;
double m_sampleRate = 48000;
int m_nSample = 0;
const double m_twoPi = 2 * M_PI;
double m_gain = 0.0;
double m_frequencyHz = 0.0;
double m_sampleRate = 48000;
int m_nSample = 0;
static constexpr double s_twoPi = 2 * M_PI;
};
} // ns
} // ns

View File

@@ -9,7 +9,9 @@
//! \file
#include "volumesampleprovider.h"
#include "volumesampleprovider.h"
#include "blackmisc/metadatautils.h"
using namespace BlackMisc;
namespace BlackSound
{
@@ -18,17 +20,20 @@ namespace BlackSound
CVolumeSampleProvider::CVolumeSampleProvider(ISampleProvider *sourceProvider, QObject *parent) :
ISampleProvider(parent),
m_sourceProvider(sourceProvider)
{ }
{
Q_ASSERT_X(sourceProvider, Q_FUNC_INFO, "Need source provider");
const QString on = QStringLiteral("%1 with source: %2").arg(classNameShort(this), sourceProvider->objectName());
this->setObjectName(on);
}
int CVolumeSampleProvider::readSamples(QVector<float> &samples, qint64 count)
{
int samplesRead = m_sourceProvider->readSamples(samples, count);
const int samplesRead = m_sourceProvider->readSamples(samples, count);
if (!qFuzzyCompare(m_volume, 1.0))
{
for (int n = 0; n < samplesRead; n++)
{
samples[n] *= static_cast<float>(m_volume);
samples[n] = static_cast<float>(m_volume * samples[n]);
}
}
return samplesRead;