mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-16 18:35:35 +08:00
[Audio] Get/set audio device volume (this is the device's value, NOT the logical AFV value)
This commit is contained in:
committed by
Mat Sutcliffe
parent
fc9bb1277d
commit
091fa5f338
@@ -86,6 +86,20 @@ namespace BlackCore
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double CInput::getDeviceInputVolume() const
|
||||||
|
{
|
||||||
|
if (m_audioInput) { return static_cast<double>(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)
|
void CInput::start(const CAudioDeviceInfo &inputDevice)
|
||||||
{
|
{
|
||||||
if (m_started) { return; }
|
if (m_started) { return; }
|
||||||
|
|||||||
@@ -64,8 +64,8 @@ namespace BlackCore
|
|||||||
//! Opus data arguments
|
//! Opus data arguments
|
||||||
struct OpusDataAvailableArgs
|
struct OpusDataAvailableArgs
|
||||||
{
|
{
|
||||||
uint sequenceCounter = 0; //!< sequence counter
|
uint sequenceCounter = 0; //!< sequence counter
|
||||||
QByteArray audio; //!< audio data
|
QByteArray audio; //!< audio data
|
||||||
};
|
};
|
||||||
|
|
||||||
//! Input volume stream arguments
|
//! Input volume stream arguments
|
||||||
@@ -101,11 +101,16 @@ namespace BlackCore
|
|||||||
void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; }
|
void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; }
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
//! Volume @{
|
//! Volume 0..1 @{
|
||||||
double volume() const { return m_volume; }
|
double volume() const { return m_volume; }
|
||||||
bool setVolume(double volume);
|
bool setVolume(double volume);
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
|
//! The device's volume 0..1 @{
|
||||||
|
double getDeviceInputVolume() const;
|
||||||
|
bool setDeviceInputVolume(double volume);
|
||||||
|
//! @}
|
||||||
|
|
||||||
//! Started?
|
//! Started?
|
||||||
bool started() const { return m_started; }
|
bool started() const { return m_started; }
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ namespace BlackCore
|
|||||||
{
|
{
|
||||||
const int sampleBytes = m_outputFormat.sampleSize() / 8;
|
const int sampleBytes = m_outputFormat.sampleSize() / 8;
|
||||||
const int channelCount = m_outputFormat.channelCount();
|
const int channelCount = m_outputFormat.channelCount();
|
||||||
const qint64 count = maxlen / (sampleBytes * channelCount);
|
const qint64 count = maxlen / (sampleBytes * channelCount);
|
||||||
QVector<float> buffer;
|
QVector<float> buffer;
|
||||||
m_sampleProvider->readSamples(buffer, count);
|
m_sampleProvider->readSamples(buffer, count);
|
||||||
|
|
||||||
@@ -70,8 +70,7 @@ namespace BlackCore
|
|||||||
buffer = convertFromMonoToStereo(buffer);
|
buffer = convertFromMonoToStereo(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(data, buffer.constData(), maxlen);
|
memcpy(data, buffer.constData(), static_cast<size_t>(maxlen));
|
||||||
|
|
||||||
return maxlen;
|
return maxlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,10 +110,10 @@ namespace BlackCore
|
|||||||
const QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat);
|
const QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat);
|
||||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.deviceName() << format;
|
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->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||||
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
||||||
m_audioOutputCom->start(m_audioOutputBuffer);
|
m_audioOutput->start(m_audioOutputBuffer);
|
||||||
|
|
||||||
m_started = true;
|
m_started = true;
|
||||||
}
|
}
|
||||||
@@ -123,14 +122,29 @@ namespace BlackCore
|
|||||||
{
|
{
|
||||||
if (!m_started) { return; }
|
if (!m_started) { return; }
|
||||||
m_started = false;
|
m_started = false;
|
||||||
m_audioOutputCom->stop();
|
m_audioOutput->stop();
|
||||||
m_audioOutputCom.reset();
|
m_audioOutput.reset();
|
||||||
if (m_audioOutputBuffer)
|
if (m_audioOutputBuffer)
|
||||||
{
|
{
|
||||||
m_audioOutputBuffer->deleteLater();
|
m_audioOutputBuffer->deleteLater();
|
||||||
m_audioOutputBuffer = nullptr;
|
m_audioOutputBuffer = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double COutput::getDeviceOutputVolume() const
|
||||||
|
{
|
||||||
|
if (m_audioOutput && m_started) { return static_cast<double>(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
|
} // ns
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
@@ -89,6 +89,11 @@ namespace BlackCore
|
|||||||
//! Corresponding device
|
//! Corresponding device
|
||||||
const BlackMisc::Audio::CAudioDeviceInfo &device() const { return m_device; }
|
const BlackMisc::Audio::CAudioDeviceInfo &device() const { return m_device; }
|
||||||
|
|
||||||
|
//! The device's volume 0..1 @{
|
||||||
|
double getDeviceOutputVolume() const;
|
||||||
|
bool setDeviceOutputVolume(double volume);
|
||||||
|
//! @}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
//! Streaming data
|
//! Streaming data
|
||||||
void outputVolumeStream(const OutputVolumeStreamArgs &args);
|
void outputVolumeStream(const OutputVolumeStreamArgs &args);
|
||||||
@@ -96,7 +101,7 @@ namespace BlackCore
|
|||||||
private:
|
private:
|
||||||
bool m_started = false;
|
bool m_started = false;
|
||||||
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
||||||
QScopedPointer<QAudioOutput> m_audioOutputCom;
|
QScopedPointer<QAudioOutput> m_audioOutput;
|
||||||
CAudioOutputBuffer *m_audioOutputBuffer = nullptr;
|
CAudioOutputBuffer *m_audioOutputBuffer = nullptr;
|
||||||
};
|
};
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
@@ -357,6 +357,30 @@ namespace BlackCore
|
|||||||
emit this->stoppedAudio();
|
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)
|
void CAfvClient::setReceiveAudio(bool receive)
|
||||||
{
|
{
|
||||||
QMutexLocker lock(&m_mutexConnection);
|
QMutexLocker lock(&m_mutexConnection);
|
||||||
|
|||||||
@@ -128,6 +128,13 @@ namespace BlackCore
|
|||||||
void stopAudio();
|
void stopAudio();
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
|
//! The device's volume 0..1 @{
|
||||||
|
double getDeviceInputVolume() const;
|
||||||
|
bool setDeviceInputVolume(double volume);
|
||||||
|
double getDeviceOutputVolume() const;
|
||||||
|
bool setDeviceOutputVolume(double volume);
|
||||||
|
//! @}
|
||||||
|
|
||||||
//! Receive audio
|
//! Receive audio
|
||||||
//! \threadsafe
|
//! \threadsafe
|
||||||
void setReceiveAudio(bool receive);
|
void setReceiveAudio(bool receive);
|
||||||
|
|||||||
@@ -233,4 +233,16 @@ namespace BlackSound
|
|||||||
static const QString unknown("unknown");
|
static const QString unknown("unknown");
|
||||||
return 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<qreal>(normalize0to100(in));
|
||||||
|
}
|
||||||
|
|
||||||
} // ns
|
} // ns
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ namespace BlackSound
|
|||||||
BLACKSOUND_EXPORT const QString &toQString(QAudioFormat::Endian e);
|
BLACKSOUND_EXPORT const QString &toQString(QAudioFormat::Endian e);
|
||||||
BLACKSOUND_EXPORT const QString &toQString(QAudioFormat::SampleType s);
|
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
|
} // ns
|
||||||
|
|
||||||
#endif // guard
|
#endif // guard
|
||||||
|
|||||||
Reference in New Issue
Block a user