diff --git a/src/blackcore/afv/audio/input.cpp b/src/blackcore/afv/audio/input.cpp index be8744a36..d51560a0f 100644 --- a/src/blackcore/afv/audio/input.cpp +++ b/src/blackcore/afv/audio/input.cpp @@ -86,6 +86,20 @@ namespace BlackCore return true; } + double CInput::getDeviceInputVolume() const + { + if (m_audioInput) { return static_cast(m_audioInput->volume()); } + return 0.0; + } + + bool CInput::setDeviceInputVolume(double volume) + { + if (!m_audioInput && m_started) { return false; } + const qreal v = normalize0to100qr(volume); + m_audioInput->setVolume(v); + return true; + } + void CInput::start(const CAudioDeviceInfo &inputDevice) { if (m_started) { return; } diff --git a/src/blackcore/afv/audio/input.h b/src/blackcore/afv/audio/input.h index 04d7042a9..5b14fad93 100644 --- a/src/blackcore/afv/audio/input.h +++ b/src/blackcore/afv/audio/input.h @@ -64,8 +64,8 @@ namespace BlackCore //! Opus data arguments struct OpusDataAvailableArgs { - uint sequenceCounter = 0; //!< sequence counter - QByteArray audio; //!< audio data + uint sequenceCounter = 0; //!< sequence counter + QByteArray audio; //!< audio data }; //! Input volume stream arguments @@ -101,11 +101,16 @@ namespace BlackCore void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; } //! @} - //! Volume @{ + //! Volume 0..1 @{ double volume() const { return m_volume; } bool setVolume(double volume); //! @} + //! The device's volume 0..1 @{ + double getDeviceInputVolume() const; + bool setDeviceInputVolume(double volume); + //! @} + //! Started? bool started() const { return m_started; } diff --git a/src/blackcore/afv/audio/output.cpp b/src/blackcore/afv/audio/output.cpp index 143ebed59..6dc58bd66 100644 --- a/src/blackcore/afv/audio/output.cpp +++ b/src/blackcore/afv/audio/output.cpp @@ -39,7 +39,7 @@ namespace BlackCore { const int sampleBytes = m_outputFormat.sampleSize() / 8; const int channelCount = m_outputFormat.channelCount(); - const qint64 count = maxlen / (sampleBytes * channelCount); + const qint64 count = maxlen / (sampleBytes * channelCount); QVector buffer; m_sampleProvider->readSamples(buffer, count); @@ -70,8 +70,7 @@ namespace BlackCore buffer = convertFromMonoToStereo(buffer); } - memcpy(data, buffer.constData(), maxlen); - + memcpy(data, buffer.constData(), static_cast(maxlen)); return maxlen; } @@ -111,10 +110,10 @@ namespace BlackCore const QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat); CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.deviceName() << format; - m_audioOutputCom.reset(new QAudioOutput(selectedDevice, outputFormat)); + m_audioOutput.reset(new QAudioOutput(selectedDevice, outputFormat)); m_audioOutputBuffer->open(QIODevice::ReadWrite | QIODevice::Unbuffered); m_audioOutputBuffer->setAudioFormat(outputFormat); - m_audioOutputCom->start(m_audioOutputBuffer); + m_audioOutput->start(m_audioOutputBuffer); m_started = true; } @@ -123,14 +122,29 @@ namespace BlackCore { if (!m_started) { return; } m_started = false; - m_audioOutputCom->stop(); - m_audioOutputCom.reset(); + m_audioOutput->stop(); + m_audioOutput.reset(); if (m_audioOutputBuffer) { m_audioOutputBuffer->deleteLater(); m_audioOutputBuffer = nullptr; } } + + double COutput::getDeviceOutputVolume() const + { + if (m_audioOutput && m_started) { return static_cast(m_audioOutput->volume()); } + return 0.0; + } + + bool COutput::setDeviceOutputVolume(double volume) + { + if (!m_audioOutput) { return false; } + const qreal v = normalize0to100qr(volume); + m_audioOutput->setVolume(v); + return true; + } + } // ns } // ns } // ns diff --git a/src/blackcore/afv/audio/output.h b/src/blackcore/afv/audio/output.h index 85075ffea..e22cc6b26 100644 --- a/src/blackcore/afv/audio/output.h +++ b/src/blackcore/afv/audio/output.h @@ -89,6 +89,11 @@ namespace BlackCore //! Corresponding device const BlackMisc::Audio::CAudioDeviceInfo &device() const { return m_device; } + //! The device's volume 0..1 @{ + double getDeviceOutputVolume() const; + bool setDeviceOutputVolume(double volume); + //! @} + signals: //! Streaming data void outputVolumeStream(const OutputVolumeStreamArgs &args); @@ -96,7 +101,7 @@ namespace BlackCore private: bool m_started = false; BlackMisc::Audio::CAudioDeviceInfo m_device; - QScopedPointer m_audioOutputCom; + QScopedPointer m_audioOutput; CAudioOutputBuffer *m_audioOutputBuffer = nullptr; }; } // ns diff --git a/src/blackcore/afv/clients/afvclient.cpp b/src/blackcore/afv/clients/afvclient.cpp index 5f47c418e..374522192 100644 --- a/src/blackcore/afv/clients/afvclient.cpp +++ b/src/blackcore/afv/clients/afvclient.cpp @@ -357,6 +357,30 @@ namespace BlackCore emit this->stoppedAudio(); } + double CAfvClient::getDeviceInputVolume() const + { + if (m_input) { return m_input->getDeviceInputVolume(); } + return 0; + } + + bool CAfvClient::setDeviceInputVolume(double volume) + { + if (m_input) { return m_input->setDeviceInputVolume(volume); } + return false; + } + + double CAfvClient::getDeviceOutputVolume() const + { + if (m_output) { return m_output->getDeviceOutputVolume(); } + return 0; + } + + bool CAfvClient::setDeviceOutputVolume(double volume) + { + if (m_output) { return m_output->setDeviceOutputVolume(volume); } + return false; + } + void CAfvClient::setReceiveAudio(bool receive) { QMutexLocker lock(&m_mutexConnection); diff --git a/src/blackcore/afv/clients/afvclient.h b/src/blackcore/afv/clients/afvclient.h index 8e469aef5..346cf36c9 100644 --- a/src/blackcore/afv/clients/afvclient.h +++ b/src/blackcore/afv/clients/afvclient.h @@ -128,6 +128,13 @@ namespace BlackCore void stopAudio(); //! @} + //! The device's volume 0..1 @{ + double getDeviceInputVolume() const; + bool setDeviceInputVolume(double volume); + double getDeviceOutputVolume() const; + bool setDeviceOutputVolume(double volume); + //! @} + //! Receive audio //! \threadsafe void setReceiveAudio(bool receive); diff --git a/src/blacksound/audioutilities.cpp b/src/blacksound/audioutilities.cpp index 6020f97c2..3973d9873 100644 --- a/src/blacksound/audioutilities.cpp +++ b/src/blacksound/audioutilities.cpp @@ -233,4 +233,16 @@ namespace BlackSound static const QString unknown("unknown"); return unknown; } + + double normalize0to100(double in) + { + if (in < 0) { return 0; } + return (in >= 1.0) ? 1.0 : in; + } + + qreal normalize0to100qr(double in) + { + return static_cast(normalize0to100(in)); + } + } // ns diff --git a/src/blacksound/audioutilities.h b/src/blacksound/audioutilities.h index c21fa6b81..3a599af90 100644 --- a/src/blacksound/audioutilities.h +++ b/src/blacksound/audioutilities.h @@ -35,6 +35,12 @@ namespace BlackSound BLACKSOUND_EXPORT const QString &toQString(QAudioFormat::Endian e); BLACKSOUND_EXPORT const QString &toQString(QAudioFormat::SampleType s); //! @} + + //! Normalize audio volume to 0..100 @{ + BLACKSOUND_EXPORT double normalize0to100(double in); + BLACKSOUND_EXPORT qreal normalize0to100qr(double in); + //! @} + } // ns #endif // guard