From 7c765654c062d0e3b7136ef7e385b86a59fe1c62 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 22 Sep 2019 17:12:24 +0200 Subject: [PATCH] Ref T730, style * avoid float, use double * const correctness * use fuzzyCompare for float/double --- src/blackcore/afv/audio/input.cpp | 27 +--------- src/blackcore/afv/audio/input.h | 13 +++-- src/blackmisc/aviation/callsign.cpp | 2 +- .../sampleprovider/bufferedwaveprovider.h | 7 ++- .../resourcesoundsampleprovider.cpp | 52 +++++-------------- .../resourcesoundsampleprovider.h | 25 +++++---- .../sampleprovider/sampleprovider.h | 15 ++++-- 7 files changed, 51 insertions(+), 90 deletions(-) diff --git a/src/blackcore/afv/audio/input.cpp b/src/blackcore/afv/audio/input.cpp index 215327fef..66533485d 100644 --- a/src/blackcore/afv/audio/input.cpp +++ b/src/blackcore/afv/audio/input.cpp @@ -58,7 +58,7 @@ namespace BlackCore void CAudioInputBuffer::timerEvent(QTimerEvent *event) { - Q_UNUSED(event); + Q_UNUSED(event) // 20 ms = 960 samples * 2 bytes = 1920 Bytes if (m_buffer.size() >= 1920) { @@ -75,31 +75,6 @@ namespace BlackCore m_encoder.setBitRate(16 * 1024); } - bool CInput::started() const - { - return m_started; - } - - int CInput::opusBytesEncoded() const - { - return m_opusBytesEncoded; - } - - void CInput::setOpusBytesEncoded(int opusBytesEncoded) - { - m_opusBytesEncoded = opusBytesEncoded; - } - - double CInput::volume() const - { - return m_volume; - } - - void CInput::setVolume(double volume) - { - m_volume = volume; - } - void CInput::start(const QAudioDeviceInfo &inputDevice) { if (m_started) { return; } diff --git a/src/blackcore/afv/audio/input.h b/src/blackcore/afv/audio/input.h index 712ae8c9b..aac37d686 100644 --- a/src/blackcore/afv/audio/input.h +++ b/src/blackcore/afv/audio/input.h @@ -80,13 +80,12 @@ namespace BlackCore //! Ctor CInput(int sampleRate, QObject *parent = nullptr); - bool started() const; + bool started() const { return m_started; } - int opusBytesEncoded() const; - void setOpusBytesEncoded(int opusBytesEncoded); - - double volume() const; - void setVolume(double volume); + int opusBytesEncoded() const { return m_opusBytesEncoded; } + void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; } + double volume() const { return m_volume; } + void setVolume(double volume) { m_volume = volume; } void start(const QAudioDeviceInfo &inputDevice); void stop(); @@ -106,8 +105,8 @@ namespace BlackCore bool m_started = false; int m_opusBytesEncoded = 0; - double m_volume = 1.0; int m_sampleCount = 0; + double m_volume = 1.0; qint16 m_maxSampleInput = 0.0; const int c_sampleCountPerEvent = 4800; diff --git a/src/blackmisc/aviation/callsign.cpp b/src/blackmisc/aviation/callsign.cpp index 6f5a5e3ed..c4ebda6df 100644 --- a/src/blackmisc/aviation/callsign.cpp +++ b/src/blackmisc/aviation/callsign.cpp @@ -39,7 +39,7 @@ namespace BlackMisc QString CCallsign::convertToQString(bool i18n) const { - Q_UNUSED(i18n); + Q_UNUSED(i18n) return m_callsign; } diff --git a/src/blacksound/sampleprovider/bufferedwaveprovider.h b/src/blacksound/sampleprovider/bufferedwaveprovider.h index 349277481..30bbc26c4 100644 --- a/src/blacksound/sampleprovider/bufferedwaveprovider.h +++ b/src/blacksound/sampleprovider/bufferedwaveprovider.h @@ -32,17 +32,20 @@ namespace BlackSound CBufferedWaveProvider(const QAudioFormat &format, QObject *parent = nullptr); void addSamples(const QVector &samples); + + //! ISampleProvider::readSamples virtual int readSamples(QVector &samples, qint64 count) override; int getBufferedBytes() const { return m_audioBuffer.size(); } + void clearBuffer(); private: QVector m_audioBuffer; qint32 m_maxBufferSize; }; - } -} + } // ns +} // ns #endif // guard diff --git a/src/blacksound/sampleprovider/resourcesoundsampleprovider.cpp b/src/blacksound/sampleprovider/resourcesoundsampleprovider.cpp index b96e8123a..ca61c18d3 100644 --- a/src/blacksound/sampleprovider/resourcesoundsampleprovider.cpp +++ b/src/blacksound/sampleprovider/resourcesoundsampleprovider.cpp @@ -1,4 +1,5 @@ #include "resourcesoundsampleprovider.h" +#include "resourcesoundsampleprovider.h" #include namespace BlackSound @@ -9,74 +10,49 @@ namespace BlackSound ISampleProvider(parent), m_resourceSound(resourceSound) { - tempBuffer.resize(tempBufferSize); + m_tempBuffer.resize(m_tempBufferSize); } int CResourceSoundSampleProvider::readSamples(QVector &samples, qint64 count) { - if (count > tempBufferSize) + if (count > m_tempBufferSize) { qDebug() << "Count too large for temp buffer"; return 0; } - qint64 availableSamples = m_resourceSound.audioData().size() - position; + qint64 availableSamples = m_resourceSound.audioData().size() - m_position; qint64 samplesToCopy = qMin(availableSamples, count); samples.clear(); samples.fill(0, samplesToCopy); - for (qint64 i = 0; i < samplesToCopy; i++) + for (int i = 0; i < samplesToCopy; i++) { - tempBuffer[i] = m_resourceSound.audioData().at(position + i); + m_tempBuffer[i] = m_resourceSound.audioData().at(m_position + i); } - if (m_gain != 1.0f) + if (!qFuzzyCompare(m_gain, 1.0)) { for (int i = 0; i < samplesToCopy; i++) { - tempBuffer[i] *= m_gain; + m_tempBuffer[i] = static_cast(qRound(m_gain * m_tempBuffer[i])); } } - for (qint64 i = 0; i < samplesToCopy; i++) + for (int i = 0; i < samplesToCopy; i++) { - samples[i] = tempBuffer.at(i); + samples[i] = m_tempBuffer.at(i); } - position += samplesToCopy; + m_position += samplesToCopy; - if (position > availableSamples - 1) + if (m_position > availableSamples - 1) { - if (m_looping) { position = 0; } + if (m_looping) { m_position = 0; } else { m_isFinished = true; } } - return (int)samplesToCopy; - } - - bool CResourceSoundSampleProvider::isFinished() - { - return m_isFinished; - } - - bool CResourceSoundSampleProvider::looping() const - { - return m_looping; - } - - void CResourceSoundSampleProvider::setLooping(bool looping) - { - m_looping = looping; - } - - float CResourceSoundSampleProvider::gain() const - { - return m_gain; - } - - void CResourceSoundSampleProvider::setGain(float gain) - { - m_gain = gain; + return static_cast(samplesToCopy); } } } diff --git a/src/blacksound/sampleprovider/resourcesoundsampleprovider.h b/src/blacksound/sampleprovider/resourcesoundsampleprovider.h index f795fe27c..4ded6372b 100644 --- a/src/blacksound/sampleprovider/resourcesoundsampleprovider.h +++ b/src/blacksound/sampleprovider/resourcesoundsampleprovider.h @@ -27,23 +27,26 @@ namespace BlackSound CResourceSoundSampleProvider(const CResourceSound &resourceSound, QObject *parent = nullptr); virtual int readSamples(QVector &samples, qint64 count) override; - virtual bool isFinished() override; + virtual bool isFinished() const override { return m_isFinished; } - bool looping() const; - void setLooping(bool looping); + bool looping() const { return m_looping; } + void setLooping(bool looping) { m_looping = looping; } - float gain() const; - void setGain(float gain); + double gain() const { return m_gain; } + void setGain(double gain) { m_gain = gain; } + + QVector getTempBuffer() const { return m_tempBuffer; } + void setTempBuffer(const QVector &value) { m_tempBuffer = value; } private: - float m_gain = 1.0f; + double m_gain = 1.0; bool m_looping = false; - CResourceSound m_resourceSound; - qint64 position = 0; - const int tempBufferSize = 9600; //9600 = 200ms - QVector tempBuffer; - bool m_isFinished = false; + CResourceSound m_resourceSound; + qint64 m_position = 0; + const int m_tempBufferSize = 9600; //9600 = 200ms + QVector m_tempBuffer; + bool m_isFinished = false; }; } } diff --git a/src/blacksound/sampleprovider/sampleprovider.h b/src/blacksound/sampleprovider/sampleprovider.h index 9fd610403..79d57e3c6 100644 --- a/src/blacksound/sampleprovider/sampleprovider.h +++ b/src/blacksound/sampleprovider/sampleprovider.h @@ -8,8 +8,8 @@ //! \file -#ifndef SAMPLEPROVIDER_H -#define SAMPLEPROVIDER_H +#ifndef BLACKSOUND_SAMPLEPROVIDER_H +#define BLACKSOUND_SAMPLEPROVIDER_H #include "blacksound/blacksoundexport.h" #include @@ -25,14 +25,19 @@ namespace BlackSound Q_OBJECT public: + //! Ctor ISampleProvider(QObject *parent = nullptr) : QObject(parent) {} + + //! Dtor virtual ~ISampleProvider() override {} + //! Read samples virtual int readSamples(QVector &samples, qint64 count) = 0; - virtual bool isFinished() { return false; } + //! Finished? + virtual bool isFinished() const { return false; } }; - } -} + } // ns +} // ns #endif // guard