diff --git a/src/audio/backend/alsink.cpp b/src/audio/backend/alsink.cpp index 97cfc51a7..80eb18d21 100644 --- a/src/audio/backend/alsink.cpp +++ b/src/audio/backend/alsink.cpp @@ -14,28 +14,28 @@ AlSink::~AlSink() QMutexLocker{&killLock}; // unsubscribe only if not already killed - if(!killed) { + if (!killed) { audio.destroySink(*this); killed = true; } } -void AlSink::playAudioBuffer(const int16_t *data, int samples, unsigned channels, int sampleRate) const +void AlSink::playAudioBuffer(const int16_t* data, int samples, unsigned channels, int sampleRate) const { QMutexLocker{&killLock}; - if(killed) { + if (killed) { qCritical() << "Trying to play audio on an invalid sink"; } else { audio.playAudioBuffer(sourceId, data, samples, channels, sampleRate); } } -void AlSink::playMono16Sound(const IAudioSink::Sound &sound) +void AlSink::playMono16Sound(const IAudioSink::Sound& sound) { QMutexLocker{&killLock}; - if(killed) { + if (killed) { qCritical() << "Trying to play sound on an invalid sink"; } else { audio.playMono16Sound(*this, sound); @@ -46,7 +46,7 @@ void AlSink::startLoop() { QMutexLocker{&killLock}; - if(killed) { + if (killed) { qCritical() << "Trying to start loop on an invalid sink"; } else { audio.startLoop(sourceId); @@ -57,7 +57,7 @@ void AlSink::stopLoop() { QMutexLocker{&killLock}; - if(killed) { + if (killed) { qCritical() << "Trying to stop loop on an invalid sink"; } else { audio.stopLoop(sourceId); @@ -83,8 +83,7 @@ AlSink::AlSink(OpenAL& al, uint sourceId) : audio{al} , sourceId{sourceId} , killLock{QMutex::Recursive} -{ -} +{} AlSink::operator bool() const { diff --git a/src/audio/backend/alsink.h b/src/audio/backend/alsink.h index b29001aaa..0505fccfa 100644 --- a/src/audio/backend/alsink.h +++ b/src/audio/backend/alsink.h @@ -1,8 +1,8 @@ #ifndef ALSINK_H #define ALSINK_H -#include #include +#include #include "src/audio/iaudiosink.h" @@ -12,15 +12,14 @@ class AlSink : public IAudioSink { Q_OBJECT public: - AlSink(OpenAL &al, uint sourceId); - AlSink(const AlSink &src) = delete; - AlSink & operator=(const AlSink&) = delete; + AlSink(OpenAL& al, uint sourceId); + AlSink(const AlSink& src) = delete; + AlSink& operator=(const AlSink&) = delete; AlSink(AlSink&& other) = delete; - AlSink & operator=(AlSink&& other) = delete; + AlSink& operator=(AlSink&& other) = delete; ~AlSink(); - void playAudioBuffer(const int16_t* data, int samples, unsigned channels, - int sampleRate) const; + void playAudioBuffer(const int16_t* data, int samples, unsigned channels, int sampleRate) const; void playMono16Sound(const IAudioSink::Sound& sound); void startLoop(); void stopLoop(); diff --git a/src/audio/backend/openal.cpp b/src/audio/backend/openal.cpp index 1e97dc2c7..fa5c6c6fa 100644 --- a/src/audio/backend/openal.cpp +++ b/src/audio/backend/openal.cpp @@ -33,16 +33,16 @@ #include namespace { - void applyGain(int16_t* buffer, uint32_t bufferSize, qreal gainFactor) - { - for (quint32 i = 0; i < bufferSize; ++i) { - // gain amplification with clipping to 16-bit boundaries - buffer[i] = qBound(std::numeric_limits::min(), - qRound(buffer[i] * gainFactor), - std::numeric_limits::max()); - } +void applyGain(int16_t* buffer, uint32_t bufferSize, qreal gainFactor) +{ + for (quint32 i = 0; i < bufferSize; ++i) { + // gain amplification with clipping to 16-bit boundaries + buffer[i] = + qBound(std::numeric_limits::min(), qRound(buffer[i] * gainFactor), + std::numeric_limits::max()); } } +} // namespace /** * @class OpenAL @@ -82,13 +82,15 @@ OpenAL::OpenAL() captureTimer.setSingleShot(false); captureTimer.moveToThread(audioThread); // TODO for Qt 5.6+: use qOverload - connect(audioThread, &QThread::started, &captureTimer, static_cast(&QTimer::start)); + connect(audioThread, &QThread::started, &captureTimer, + static_cast(&QTimer::start)); cleanupTimer.setInterval(1000); cleanupTimer.setSingleShot(false); connect(&cleanupTimer, &QTimer::timeout, this, &OpenAL::cleanupSound); // TODO for Qt 5.6+: use qOverload - connect(audioThread, &QThread::started, &cleanupTimer, static_cast(&QTimer::start)); + connect(audioThread, &QThread::started, &cleanupTimer, + static_cast(&QTimer::start)); audioThread->start(); } @@ -245,7 +247,7 @@ bool OpenAL::reinitOutput(const QString& outDevDesc) * @brief Allocates ressources for a new audio output * @return AudioSink on success, nullptr on failure */ -IAudioSink *OpenAL::makeSink() +IAudioSink* OpenAL::makeSink() { QMutexLocker locker(&audioLock); @@ -332,8 +334,7 @@ void OpenAL::unsubscribeInput() return; inSubscriptions--; - qDebug() << "Unsubscribed from audio input device [" << inSubscriptions - << "subscriptions left ]"; + qDebug() << "Unsubscribed from audio input device [" << inSubscriptions << "subscriptions left ]"; if (!inSubscriptions) cleanupInput(); @@ -377,8 +378,8 @@ bool OpenAL::initInput(const QString& deviceName, uint32_t channels) this->channels = channels; int stereoFlag = channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; const int bytesPerSample = 2; - const int safetyFactor = 2; // internal OpenAL ring buffer. must be larger than our inputBuffer to avoid the ring - // from overwriting itself between captures. + const int safetyFactor = 2; // internal OpenAL ring buffer. must be larger than our inputBuffer + // to avoid the ring from overwriting itself between captures. AUDIO_FRAME_SAMPLE_COUNT_TOTAL = AUDIO_FRAME_SAMPLE_COUNT_PER_CHANNEL * channels; const ALCsizei ringBufSize = AUDIO_FRAME_SAMPLE_COUNT_TOTAL * bytesPerSample * safetyFactor; @@ -446,18 +447,18 @@ bool OpenAL::initOutput(const QString& deviceName) /** * @brief Play a 48kHz mono 16bit PCM sound */ -void OpenAL::playMono16Sound(AlSink& sink, const IAudioSink::Sound &sound) +void OpenAL::playMono16Sound(AlSink& sink, const IAudioSink::Sound& sound) { const uint sourceId = sink.getSourceId(); QFile sndFile(IAudioSink::getSound(sound)); - if(!sndFile.exists()) { + if (!sndFile.exists()) { qDebug() << "Trying to open non existent sound file"; return; } sndFile.open(QIODevice::ReadOnly); const QByteArray data{sndFile.readAll()}; - if(data.isEmpty()) { + if (data.isEmpty()) { qDebug() << "Sound file contained no data"; return; } @@ -606,9 +607,9 @@ float OpenAL::getVolume() float sumOfSquares = 0; for (quint32 i = 0; i < samples; i++) { float sample = static_cast(inputBuffer[i]) / std::numeric_limits::max(); - sumOfSquares += std::pow(sample , 2); + sumOfSquares += std::pow(sample, 2); } - const float rms = std::sqrt(sumOfSquares/samples); + const float rms = std::sqrt(sumOfSquares / samples); // our calculated normalized volume could possibly be above 1 because our RMS assumes a sinusoidal wave const float normalizedVolume = std::min(rms * rootTwo, 1.0f); return normalizedVolume; @@ -650,7 +651,8 @@ void OpenAL::doInput() return; } - emit Audio::frameAvailable(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT_PER_CHANNEL, channels, AUDIO_SAMPLE_RATE); + emit Audio::frameAvailable(inputBuffer, AUDIO_FRAME_SAMPLE_COUNT_PER_CHANNEL, channels, + AUDIO_SAMPLE_RATE); } void OpenAL::doOutput() @@ -725,7 +727,8 @@ QStringList OpenAL::inDeviceNames() * @brief Free all buffers that finished playing on a source * @param sourceId where to remove the buffers from */ -void OpenAL::cleanupBuffers(uint sourceId) { +void OpenAL::cleanupBuffers(uint sourceId) +{ // unqueue all buffers from the source ALint processed = 0; alGetSourcei(sourceId, AL_BUFFERS_PROCESSED, &processed); diff --git a/src/audio/backend/openal.h b/src/audio/backend/openal.h index 854f87e40..1d46e70f1 100644 --- a/src/audio/backend/openal.h +++ b/src/audio/backend/openal.h @@ -52,8 +52,14 @@ public: OpenAL(); virtual ~OpenAL(); - qreal maxOutputVolume() const { return 1; } - qreal minOutputVolume() const { return 0; } + qreal maxOutputVolume() const + { + return 1; + } + qreal minOutputVolume() const + { + return 0; + } qreal outputVolume() const; void setOutputVolume(qreal volume); diff --git a/src/audio/backend/openal2.cpp b/src/audio/backend/openal2.cpp index 66a11a56a..5da941a4f 100644 --- a/src/audio/backend/openal2.cpp +++ b/src/audio/backend/openal2.cpp @@ -32,7 +32,8 @@ #include -extern "C" { +extern "C" +{ #include } @@ -80,8 +81,7 @@ OpenAL2::OpenAL2() , alProxyContext{nullptr} , alProxySource{0} , alProxyBuffer{0} -{ -} +{} bool OpenAL2::initInput(const QString& deviceName) { @@ -324,7 +324,8 @@ void OpenAL2::doOutput() alcMakeContextCurrent(alOutContext); } - alBufferData(bufids[0], AL_FORMAT_MONO16, outBuf, AUDIO_FRAME_SAMPLE_COUNT_PER_CHANNEL * 2, AUDIO_SAMPLE_RATE); + alBufferData(bufids[0], AL_FORMAT_MONO16, outBuf, AUDIO_FRAME_SAMPLE_COUNT_PER_CHANNEL * 2, + AUDIO_SAMPLE_RATE); alSourceQueueBuffers(alProxySource, 1, bufids); diff --git a/src/audio/backend/openal2.h b/src/audio/backend/openal2.h index 9f9c6c3a2..85fcb0503 100644 --- a/src/audio/backend/openal2.h +++ b/src/audio/backend/openal2.h @@ -37,7 +37,8 @@ #include #include -extern "C" { +extern "C" +{ #include } diff --git a/src/audio/iaudiosink.h b/src/audio/iaudiosink.h index 9ddbec7f5..a526a685b 100644 --- a/src/audio/iaudiosink.h +++ b/src/audio/iaudiosink.h @@ -79,7 +79,7 @@ public: virtual ~IAudioSink() {} virtual void playAudioBuffer(const int16_t* data, int samples, unsigned channels, - int sampleRate) const = 0; + int sampleRate) const = 0; virtual void playMono16Sound(const Sound& sound) = 0; virtual void startLoop() = 0; virtual void stopLoop() = 0; diff --git a/src/core/toxcall.cpp b/src/core/toxcall.cpp index b9629f319..d7f6e963a 100644 --- a/src/core/toxcall.cpp +++ b/src/core/toxcall.cpp @@ -31,8 +31,7 @@ ToxCall::ToxCall(bool VideoEnabled, CoreAV& av) : av{&av} , videoEnabled{VideoEnabled} -{ -} +{} ToxCall::~ToxCall() { @@ -110,19 +109,17 @@ ToxFriendCall::ToxFriendCall(uint32_t FriendNum, bool VideoEnabled, CoreAV& av) Audio& audio = Audio::getInstance(); audio.subscribeInput(); audioInConn = QObject::connect(&Audio::getInstance(), &Audio::frameAvailable, - [&av, FriendNum](const int16_t* pcm, size_t samples, uint8_t chans, - uint32_t rate) { - av.sendCallAudio(FriendNum, pcm, samples, chans, rate); - }); + [&av, FriendNum](const int16_t* pcm, size_t samples, + uint8_t chans, uint32_t rate) { + av.sendCallAudio(FriendNum, pcm, samples, chans, rate); + }); if (!audioInConn) { - qDebug() << "Audio input connection not working"; + qDebug() << "Audio input connection not working"; } audioSinkInvalid = QObject::connect(sink.get(), &IAudioSink::invalidated, - [this]() { - this->onAudioSinkInvalidated(); - }); + [this]() { this->onAudioSinkInvalidated(); }); // register video @@ -154,9 +151,7 @@ void ToxFriendCall::onAudioSinkInvalidated() const auto newSink = Audio::getInstance().makeSink(); audioSinkInvalid = QObject::connect(newSink, &IAudioSink::invalidated, - [this]() { - this->onAudioSinkInvalidated(); - }); + [this]() { this->onAudioSinkInvalidated(); }); sink.reset(newSink); } @@ -193,7 +188,7 @@ void ToxFriendCall::setState(const TOXAV_FRIEND_CALL_STATE& value) state = value; } -const std::unique_ptr &ToxFriendCall::getAudioSink() const +const std::unique_ptr& ToxFriendCall::getAudioSink() const { return sink; } @@ -206,13 +201,13 @@ ToxGroupCall::ToxGroupCall(int GroupNum, CoreAV& av) Audio& audio = Audio::getInstance(); audio.subscribeInput(); audioInConn = QObject::connect(&Audio::getInstance(), &Audio::frameAvailable, - [&av, GroupNum](const int16_t* pcm, size_t samples, uint8_t chans, - uint32_t rate) { - av.sendGroupCallAudio(GroupNum, pcm, samples, chans, rate); - }); + [&av, GroupNum](const int16_t* pcm, size_t samples, + uint8_t chans, uint32_t rate) { + av.sendGroupCallAudio(GroupNum, pcm, samples, chans, rate); + }); if (!audioInConn) { - qDebug() << "Audio input connection not working"; + qDebug() << "Audio input connection not working"; } } @@ -231,7 +226,7 @@ void ToxGroupCall::onAudioSinkInvalidated(ToxPk peerId) void ToxGroupCall::removePeer(ToxPk peerId) { const auto& source = peers.find(peerId); - if(source == peers.cend()) { + if (source == peers.cend()) { qDebug() << "Peer:" << peerId.toString() << "does not have a source, can't remove"; return; } @@ -247,10 +242,9 @@ void ToxGroupCall::addPeer(ToxPk peerId) IAudioSink* newSink = audio.makeSink(); peers.emplace(peerId, std::unique_ptr(newSink)); - QMetaObject::Connection con = QObject::connect(newSink, &IAudioSink::invalidated, - [this, peerId]() { - this->onAudioSinkInvalidated(peerId); - }); + QMetaObject::Connection con = + QObject::connect(newSink, &IAudioSink::invalidated, + [this, peerId]() { this->onAudioSinkInvalidated(peerId); }); sinkInvalid.insert({peerId, con}); } @@ -264,16 +258,16 @@ bool ToxGroupCall::havePeer(ToxPk peerId) void ToxGroupCall::clearPeers() { peers.clear(); - for(auto con : sinkInvalid) { + for (auto con : sinkInvalid) { QObject::disconnect(con.second); } sinkInvalid.clear(); } -const std::unique_ptr &ToxGroupCall::getAudioSink(ToxPk peer) +const std::unique_ptr& ToxGroupCall::getAudioSink(ToxPk peer) { - if(!havePeer(peer)) { + if (!havePeer(peer)) { addPeer(peer); } const auto& source = peers.find(peer); diff --git a/src/core/toxcall.h b/src/core/toxcall.h index 69b132af6..818ee98de 100644 --- a/src/core/toxcall.h +++ b/src/core/toxcall.h @@ -77,7 +77,7 @@ public: TOXAV_FRIEND_CALL_STATE getState() const; void setState(const TOXAV_FRIEND_CALL_STATE& value); - const std::unique_ptr &getAudioSink() const; + const std::unique_ptr& getAudioSink() const; protected: std::unique_ptr timeoutTimer; @@ -109,7 +109,8 @@ public: bool havePeer(ToxPk peerId); void clearPeers(); - const std::unique_ptr &getAudioSink(ToxPk peer); + const std::unique_ptr& getAudioSink(ToxPk peer); + private: std::map> peers; std::map sinkInvalid; diff --git a/src/widget/form/settings/avform.cpp b/src/widget/form/settings/avform.cpp index 25b1b271e..277402ff4 100644 --- a/src/widget/form/settings/avform.cpp +++ b/src/widget/form/settings/avform.cpp @@ -150,7 +150,7 @@ void AVForm::showEvent(QShowEvent* event) subscribedToAudioIn = true; } - if(audioSink == nullptr) { + if (audioSink == nullptr) { audioSink.reset(audio->makeSink()); } @@ -571,7 +571,7 @@ void AVForm::on_outDevCombobox_currentIndexChanged(int deviceIndex) const QString oldName = audioSettings->getOutDev(); - if(oldName != deviceName) { + if (oldName != deviceName) { audioSettings->setOutDev(deviceName); audio->reinitOutput(deviceName); audioSink.reset(Audio::getInstance().makeSink()); diff --git a/src/widget/form/settings/avform.h b/src/widget/form/settings/avform.h index 692e91c6f..118f6ab93 100644 --- a/src/widget/form/settings/avform.h +++ b/src/widget/form/settings/avform.h @@ -41,8 +41,8 @@ class AVForm : public GenericForm, private Ui::AVForm { Q_OBJECT public: - AVForm(Audio* audio, CoreAV* coreAV, CameraSource& camera, - IAudioSettings* audioSettings, IVideoSettings* videoSettings); + AVForm(Audio* audio, CoreAV* coreAV, CameraSource& camera, IAudioSettings* audioSettings, + IVideoSettings* videoSettings); ~AVForm() override; QString getFormName() final override {