diff options
author | Artem Dyomin <artem.dyomin@qt.io> | 2024-01-17 12:16:31 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-01-19 15:23:13 +0000 |
commit | cee4a0ea3f71f8004d6991625a63673aade9092f (patch) | |
tree | 95e7f80406fc9ca8daf5a807e9a41e2236a6e5de | |
parent | 7a2a69d65bcbe8b785ea2abdc2a88b3aecda5587 (diff) |
Minor cleanup in QSoundEffect
* rename m_audioOutput -> m_audioSink in order not to mix it up
with QAudioOutput
* use unique_ptr
Pick-to: 6.5
Change-Id: I2d23e6fb36ba6691c4d5c6fd030f2eb3ec193717
Reviewed-by: Lars Knoll <lars@knoll.priv.no>
(cherry picked from commit 61f2d81206ca08361fab2c0c3fc3441e764cc84a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 1dd2c014ad433b3d9b221e27150972fc1f0af1c3)
-rw-r--r-- | src/multimedia/audio/qsoundeffect.cpp | 98 |
1 files changed, 57 insertions, 41 deletions
diff --git a/src/multimedia/audio/qsoundeffect.cpp b/src/multimedia/audio/qsoundeffect.cpp index d65df2502..bff9dc093 100644 --- a/src/multimedia/audio/qsoundeffect.cpp +++ b/src/multimedia/audio/qsoundeffect.cpp @@ -16,6 +16,27 @@ QT_BEGIN_NAMESPACE Q_GLOBAL_STATIC(QSampleCache, sampleCache) +namespace +{ +struct AudioSinkDeleter +{ + void operator ()(QAudioSink* sink) const + { + sink->stop(); + // Investigate:should we just delete? + sink->deleteLater(); + } +}; + +struct SampleDeleter +{ + void operator ()(QSample* sample) const + { + sample->release(); + } +}; +} + class QSoundEffectPrivate : public QIODevice { public: @@ -57,9 +78,9 @@ public: int m_loopCount = 1; int m_runningCount = 0; bool m_playing = false; - QSoundEffect::Status m_status = QSoundEffect::Null; - QAudioSink *m_audioOutput = nullptr; - QSample *m_sample = nullptr; + QSoundEffect::Status m_status = QSoundEffect::Null; + std::unique_ptr<QAudioSink, AudioSinkDeleter> m_audioSink; + std::unique_ptr<QSample, SampleDeleter> m_sample; bool m_muted = false; float m_volume = 1.0; bool m_sampleReady = false; @@ -83,30 +104,30 @@ void QSoundEffectPrivate::sampleReady() return; qCDebug(qLcSoundEffect) << this << "sampleReady: sample size:" << m_sample->data().size(); - disconnect(m_sample, &QSample::error, this, &QSoundEffectPrivate::decoderError); - disconnect(m_sample, &QSample::ready, this, &QSoundEffectPrivate::sampleReady); - if (!m_audioOutput) { - m_audioOutput = new QAudioSink(m_audioDevice, m_sample->format()); - connect(m_audioOutput, &QAudioSink::stateChanged, this, &QSoundEffectPrivate::stateChanged); + disconnect(m_sample.get(), &QSample::error, this, &QSoundEffectPrivate::decoderError); + disconnect(m_sample.get(), &QSample::ready, this, &QSoundEffectPrivate::sampleReady); + if (!m_audioSink) { + m_audioSink.reset(new QAudioSink(m_audioDevice, m_sample->format())); + connect(m_audioSink.get(), &QAudioSink::stateChanged, this, &QSoundEffectPrivate::stateChanged); if (!m_muted) - m_audioOutput->setVolume(m_volume); + m_audioSink->setVolume(m_volume); else - m_audioOutput->setVolume(0); + m_audioSink->setVolume(0); } m_sampleReady = true; setStatus(QSoundEffect::Ready); - if (m_playing && m_audioOutput->state() == QAudio::StoppedState) { + if (m_playing && m_audioSink->state() == QAudio::StoppedState) { qCDebug(qLcSoundEffect) << this << "starting playback on audiooutput"; - m_audioOutput->start(this); + m_audioSink->start(this); } } void QSoundEffectPrivate::decoderError() { qWarning("QSoundEffect(qaudio): Error decoding source %ls", qUtf16Printable(m_url.toString())); - disconnect(m_sample, &QSample::ready, this, &QSoundEffectPrivate::sampleReady); - disconnect(m_sample, &QSample::error, this, &QSoundEffectPrivate::decoderError); + disconnect(m_sample.get(), &QSample::ready, this, &QSoundEffectPrivate::sampleReady); + disconnect(m_sample.get(), &QSample::error, this, &QSoundEffectPrivate::decoderError); m_playing = false; setStatus(QSoundEffect::Error); } @@ -181,8 +202,8 @@ void QSoundEffectPrivate::setStatus(QSoundEffect::Status status) void QSoundEffectPrivate::setPlaying(bool playing) { qCDebug(qLcSoundEffect) << this << "setPlaying(" << playing << ")" << m_playing; - if (m_audioOutput) { - m_audioOutput->stop(); + if (m_audioSink) { + m_audioSink->stop(); if (playing && !m_sampleReady) return; } @@ -191,8 +212,8 @@ void QSoundEffectPrivate::setPlaying(bool playing) return; m_playing = playing; - if (m_audioOutput && playing) - m_audioOutput->start(this); + if (m_audioSink && playing) + m_audioSink->start(this); emit q_ptr->playingChanged(); } @@ -283,11 +304,8 @@ QSoundEffect::QSoundEffect(const QAudioDevice &audioDevice, QObject *parent) QSoundEffect::~QSoundEffect() { stop(); - if (d->m_audioOutput) { - d->m_audioOutput->stop(); - d->m_audioOutput->deleteLater(); - d->m_sample->release(); - } + d->m_audioSink.reset(); + d->m_sample.reset(); delete d; } @@ -358,24 +376,22 @@ void QSoundEffect::setSource(const QUrl &url) if (d->m_sample) { if (!d->m_sampleReady) { - QObject::disconnect(d->m_sample, &QSample::error, d, &QSoundEffectPrivate::decoderError); - QObject::disconnect(d->m_sample, &QSample::ready, d, &QSoundEffectPrivate::sampleReady); + disconnect(d->m_sample.get(), &QSample::error, d, &QSoundEffectPrivate::decoderError); + disconnect(d->m_sample.get(), &QSample::ready, d, &QSoundEffectPrivate::sampleReady); } d->m_sample->release(); d->m_sample = nullptr; } - if (d->m_audioOutput) { - QObject::disconnect(d->m_audioOutput, &QAudioSink::stateChanged, d, &QSoundEffectPrivate::stateChanged); - d->m_audioOutput->stop(); - d->m_audioOutput->deleteLater(); - d->m_audioOutput = nullptr; + if (d->m_audioSink) { + disconnect(d->m_audioSink.get(), &QAudioSink::stateChanged, d, &QSoundEffectPrivate::stateChanged); + d->m_audioSink.reset(); } d->setStatus(QSoundEffect::Loading); - d->m_sample = sampleCache()->requestSample(url); - QObject::connect(d->m_sample, &QSample::error, d, &QSoundEffectPrivate::decoderError); - QObject::connect(d->m_sample, &QSample::ready, d, &QSoundEffectPrivate::sampleReady); + d->m_sample.reset(sampleCache()->requestSample(url)); + connect(d->m_sample.get(), &QSample::error, d, &QSoundEffectPrivate::decoderError); + connect(d->m_sample.get(), &QSample::ready, d, &QSoundEffectPrivate::sampleReady); switch (d->m_sample->state()) { case QSample::Ready: @@ -516,8 +532,8 @@ int QSoundEffect::loopsRemaining() const */ float QSoundEffect::volume() const { - if (d->m_audioOutput && !d->m_muted) - return d->m_audioOutput->volume(); + if (d->m_audioSink && !d->m_muted) + return d->m_audioSink->volume(); return d->m_volume; } @@ -542,8 +558,8 @@ void QSoundEffect::setVolume(float volume) d->m_volume = volume; - if (d->m_audioOutput && !d->m_muted) - d->m_audioOutput->setVolume(volume); + if (d->m_audioSink && !d->m_muted) + d->m_audioSink->setVolume(volume); emit volumeChanged(); } @@ -578,10 +594,10 @@ void QSoundEffect::setMuted(bool muted) if (d->m_muted == muted) return; - if (muted && d->m_audioOutput) - d->m_audioOutput->setVolume(0); - else if (!muted && d->m_audioOutput && d->m_muted) - d->m_audioOutput->setVolume(d->m_volume); + if (muted && d->m_audioSink) + d->m_audioSink->setVolume(0); + else if (!muted && d->m_audioSink && d->m_muted) + d->m_audioSink->setVolume(d->m_volume); d->m_muted = muted; emit mutedChanged(); |