diff options
author | VaL Doroshchuk <valentyn.doroshchuk@qt.io> | 2018-11-08 10:31:18 +0100 |
---|---|---|
committer | VaL Doroshchuk <valentyn.doroshchuk@qt.io> | 2018-11-16 08:16:00 +0000 |
commit | 6dd0e0141bbb8ca8571f7d142c7b0314e8c63f57 (patch) | |
tree | c61deb5d7f0b82653df17c84406097ce6c678a89 | |
parent | 05a58a81db51564f93397dcf8f7cd37c8d4d1a9d (diff) | |
download | qtmultimedia-6dd0e0141bbb8ca8571f7d142c7b0314e8c63f57.tar.gz |
CoreAudio: Decrease volume when playback is stopped on macOS
Since suddenly stopping the playback might cause popping/clicking noise,
added decreasing volume when the stop is already requested but
buffers are still available.
Task-number: QTBUG-63492
Change-Id: Id6509faeab7fef445b13c45c7f424afa65efd1ab
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
-rw-r--r-- | src/plugins/coreaudio/coreaudiooutput.h | 1 | ||||
-rw-r--r-- | src/plugins/coreaudio/coreaudiooutput.mm | 44 |
2 files changed, 27 insertions, 18 deletions
diff --git a/src/plugins/coreaudio/coreaudiooutput.h b/src/plugins/coreaudio/coreaudiooutput.h index 9f62cd77d..97b1e0438 100644 --- a/src/plugins/coreaudio/coreaudiooutput.h +++ b/src/plugins/coreaudio/coreaudiooutput.h @@ -193,6 +193,7 @@ private: QTimer *m_intervalTimer; CoreAudioDeviceInfo *m_audioDeviceInfo; qreal m_cachedVolume; + qreal m_volume; bool m_pullMode; QAudio::Error m_errorCode; diff --git a/src/plugins/coreaudio/coreaudiooutput.mm b/src/plugins/coreaudio/coreaudiooutput.mm index 900e34e16..caa4a1abb 100644 --- a/src/plugins/coreaudio/coreaudiooutput.mm +++ b/src/plugins/coreaudio/coreaudiooutput.mm @@ -228,6 +228,7 @@ CoreAudioOutput::CoreAudioOutput(const QByteArray &device) , m_startTime(0) , m_audioBuffer(0) , m_cachedVolume(1.0) + , m_volume(1.0) , m_pullMode(false) , m_errorCode(QAudio::NoError) , m_stateCode(QAudio::StoppedState) @@ -435,11 +436,9 @@ QAudioFormat CoreAudioOutput::format() const void CoreAudioOutput::setVolume(qreal volume) { - const qreal normalizedVolume = qBound(qreal(0.0), volume, qreal(1.0)); - if (!m_isOpen) { - m_cachedVolume = normalizedVolume; + m_cachedVolume = qBound(qreal(0.0), volume, qreal(1.0)); + if (!m_isOpen) return; - } #if defined(Q_OS_OSX) //on OS X the volume can be set directly on the AudioUnit @@ -447,11 +446,9 @@ void CoreAudioOutput::setVolume(qreal volume) kHALOutputParam_Volume, kAudioUnitScope_Global, 0 /* bus */, - (float)normalizedVolume, + m_cachedVolume, 0) == noErr) - m_cachedVolume = normalizedVolume; -#else - m_cachedVolume = normalizedVolume; + m_volume = m_cachedVolume; #endif } @@ -513,15 +510,24 @@ OSStatus CoreAudioOutput::renderCallback(void *inRefCon, AudioUnitRenderActionFl if (framesRead > 0) { ioData->mBuffers[0].mDataByteSize = framesRead * bytesPerFrame; d->m_totalFrames += framesRead; -#if defined(Q_OS_IOS) || defined(Q_OS_TVOS) - // on iOS we have to adjust the sound volume ourselves - if (!qFuzzyCompare(d->m_cachedVolume, qreal(1.0f))) { - QAudioHelperInternal::qMultiplySamples(d->m_cachedVolume, - d->m_audioFormat, - ioData->mBuffers[0].mData, /* input */ - ioData->mBuffers[0].mData, /* output */ - ioData->mBuffers[0].mDataByteSize); - } + +#if defined(Q_OS_MACOS) + // If playback is already stopped. + if (threadState != Running) { + qreal oldVolume = d->m_cachedVolume; + // Decrease volume smoothly. + d->setVolume(d->m_volume / 2); + d->m_cachedVolume = oldVolume; + } +#elif defined(Q_OS_IOS) || defined(Q_OS_TVOS) + // on iOS we have to adjust the sound volume ourselves + if (!qFuzzyCompare(d->m_cachedVolume, qreal(1.0f))) { + QAudioHelperInternal::qMultiplySamples(d->m_cachedVolume, + d->m_audioFormat, + ioData->mBuffers[0].mData, /* input */ + ioData->mBuffers[0].mData, /* output */ + ioData->mBuffers[0].mDataByteSize); + } #endif } @@ -546,8 +552,10 @@ bool CoreAudioOutput::open() if (m_errorCode != QAudio::NoError) return false; - if (m_isOpen) + if (m_isOpen) { + setVolume(m_cachedVolume); return true; + } AudioComponentDescription componentDescription; componentDescription.componentType = kAudioUnitType_Output; |