Ref T730, style

* avoid float, use double
* const correctness
* use fuzzyCompare for float/double
This commit is contained in:
Klaus Basan
2019-09-22 17:12:24 +02:00
committed by Mat Sutcliffe
parent e21fdeb039
commit 7c765654c0
7 changed files with 51 additions and 90 deletions

View File

@@ -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; }

View File

@@ -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;

View File

@@ -39,7 +39,7 @@ namespace BlackMisc
QString CCallsign::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
Q_UNUSED(i18n)
return m_callsign;
}

View File

@@ -32,17 +32,20 @@ namespace BlackSound
CBufferedWaveProvider(const QAudioFormat &format, QObject *parent = nullptr);
void addSamples(const QVector<qint16> &samples);
//! ISampleProvider::readSamples
virtual int readSamples(QVector<qint16> &samples, qint64 count) override;
int getBufferedBytes() const { return m_audioBuffer.size(); }
void clearBuffer();
private:
QVector<qint16> m_audioBuffer;
qint32 m_maxBufferSize;
};
}
}
} // ns
} // ns
#endif // guard

View File

@@ -1,4 +1,5 @@
#include "resourcesoundsampleprovider.h"
#include "resourcesoundsampleprovider.h"
#include <QDebug>
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<qint16> &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<qint16>(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<int>(samplesToCopy);
}
}
}

View File

@@ -27,23 +27,26 @@ namespace BlackSound
CResourceSoundSampleProvider(const CResourceSound &resourceSound, QObject *parent = nullptr);
virtual int readSamples(QVector<qint16> &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<qint16> getTempBuffer() const { return m_tempBuffer; }
void setTempBuffer(const QVector<qint16> &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<qint16> tempBuffer;
bool m_isFinished = false;
CResourceSound m_resourceSound;
qint64 m_position = 0;
const int m_tempBufferSize = 9600; //9600 = 200ms
QVector<qint16> m_tempBuffer;
bool m_isFinished = false;
};
}
}

View File

@@ -8,8 +8,8 @@
//! \file
#ifndef SAMPLEPROVIDER_H
#define SAMPLEPROVIDER_H
#ifndef BLACKSOUND_SAMPLEPROVIDER_H
#define BLACKSOUND_SAMPLEPROVIDER_H
#include "blacksound/blacksoundexport.h"
#include <QObject>
@@ -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<qint16> &samples, qint64 count) = 0;
virtual bool isFinished() { return false; }
//! Finished?
virtual bool isFinished() const { return false; }
};
}
}
} // ns
} // ns
#endif // guard