File indexing completed on 2024-05-12 04:44:37

0001 /*  This file is part of the KDE project.
0002  *
0003  *    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
0004  *    Copyright (C) 2013 Martin Sandsmark <martin.sandsmark@kde.org>
0005  *
0006  *    This library is free software: you can redistribute it and/or modify
0007  *    it under the terms of the GNU Lesser General Public License as published by
0008  *    the Free Software Foundation, either version 2.1 or 3 of the License.
0009  *
0010  *    This library is distributed in the hope that it will be useful,
0011  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  *    GNU Lesser General Public License for more details.
0014  *
0015  *    You should have received a copy of the GNU Lesser General Public License
0016  *    along with this library.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 #include "volumefadereffect.h"
0020 #include <mediaplayer.h>
0021 
0022 #include "utils/debug.h"
0023 
0024 #include <QtCore/QTimeLine>
0025 
0026 #ifndef QT_NO_PHONON_VOLUMEFADEREFFECT
0027 namespace Phonon
0028 {
0029 namespace VLC
0030 {
0031 VolumeFaderEffect::VolumeFaderEffect(QObject *parent)
0032     : QObject(parent)
0033     , SinkNode()
0034     , m_fadeCurve(Phonon::VolumeFaderEffect::Fade3Decibel)
0035     , m_fadeFromVolume(0)
0036     , m_fadeToVolume(0)
0037 {
0038     m_fadeTimeline = new QTimeLine(1000, this);
0039     connect(m_fadeTimeline, SIGNAL(valueChanged(qreal)), this, SLOT(slotSetVolume(qreal)));
0040 }
0041 
0042 VolumeFaderEffect::~VolumeFaderEffect()
0043 {
0044 }
0045 
0046 float VolumeFaderEffect::volume() const
0047 {
0048     Q_ASSERT(m_player);
0049     return m_player->audioVolume() / 100.0f;
0050 }
0051 
0052 void VolumeFaderEffect::slotSetVolume(qreal volume)
0053 {
0054     float gstVolume = m_fadeFromVolume + (volume * (m_fadeToVolume - m_fadeFromVolume));
0055     setVolumeInternal(gstVolume);
0056 }
0057 
0058 Phonon::VolumeFaderEffect::FadeCurve VolumeFaderEffect::fadeCurve() const
0059 {
0060     return m_fadeCurve;
0061 }
0062 
0063 void VolumeFaderEffect::setFadeCurve(Phonon::VolumeFaderEffect::FadeCurve pFadeCurve)
0064 {
0065     m_fadeCurve = pFadeCurve;
0066     QEasingCurve fadeCurve;
0067     switch(pFadeCurve) {
0068     case Phonon::VolumeFaderEffect::Fade3Decibel:
0069         fadeCurve = QEasingCurve::InQuad;
0070         break;
0071     case Phonon::VolumeFaderEffect::Fade6Decibel:
0072         fadeCurve = QEasingCurve::Linear;
0073         break;
0074     case Phonon::VolumeFaderEffect::Fade9Decibel:
0075         fadeCurve = QEasingCurve::OutCubic;
0076         break;
0077     case Phonon::VolumeFaderEffect::Fade12Decibel:
0078         fadeCurve = QEasingCurve::OutQuart;
0079         break;
0080     }
0081     m_fadeTimeline->setEasingCurve(fadeCurve);
0082 }
0083 
0084 void VolumeFaderEffect::fadeTo(float targetVolume, int fadeTime)
0085 {
0086     Q_ASSERT(m_player);
0087     abortFade();
0088     m_fadeToVolume = targetVolume;
0089     m_fadeFromVolume = m_player->audioVolume() / 100.0f;
0090 
0091     // Don't call QTimeLine::setDuration() with zero.
0092     // It is not supported and breaks fading.
0093     if (fadeTime <= 0) {
0094         debug() << "Called with retarded fade time " << fadeTime;
0095         setVolumeInternal(targetVolume);
0096         return;
0097     }
0098 
0099     m_fadeTimeline->setDuration(fadeTime);
0100     m_fadeTimeline->start();
0101 }
0102 
0103 void VolumeFaderEffect::setVolume(float v)
0104 {
0105     abortFade();
0106     setVolumeInternal(v);
0107 }
0108 
0109 void VolumeFaderEffect::abortFade()
0110 {
0111     m_fadeTimeline->stop();
0112 }
0113 
0114 void VolumeFaderEffect::setVolumeInternal(float v)
0115 {
0116     if (m_player)
0117         m_player->setAudioFade(v);
0118     else
0119         warning() << Q_FUNC_INFO << this << "no m_player set";
0120 }
0121 
0122 }
0123 } //namespace Phonon::VLC
0124 
0125 #endif //QT_NO_PHONON_VOLUMEFADEREFFECT
0126 #include "moc_volumefadereffect.cpp"
0127