File indexing completed on 2024-05-05 04:48:36

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Matěj Laitl <matej@laitl.cz>                                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  * **************************************************************************************/
0016 
0017 #include "Fadeouter.h"
0018 
0019 #include <phonon/MediaObject>
0020 #include <phonon/VolumeFaderEffect>
0021 
0022 #include <QTimer>
0023 
0024 static const int safetyDelay = 300; // in ms
0025 
0026 Fadeouter::Fadeouter( const QPointer<Phonon::MediaObject> &media,
0027                       const QPointer<Phonon::VolumeFaderEffect> &fader,
0028                       int fadeOutLength )
0029     : QObject( fader.data() )
0030     , m_fader( fader )
0031 {
0032     Q_ASSERT( media );
0033     Q_ASSERT( fader );
0034     Q_ASSERT( fadeOutLength > 0 );
0035 
0036     m_fader->fadeOut( fadeOutLength );
0037     // add a bit of a second so that the effect is not cut even if there are some delays
0038     QTimer::singleShot( fadeOutLength + safetyDelay, this, &Fadeouter::slotFinalizeFadeout );
0039 
0040     // in case a new track starts playing before the fadeout ends, we skip
0041     // slotFinalizeFadeout() and go directly to destructor, which resets fader volume
0042     connect( media.data(), &Phonon::MediaObject::currentSourceChanged, this, &QObject::deleteLater );
0043 
0044     // no point in having dangling Fadeouters
0045     connect( media.data(), &QObject::destroyed, this, &QObject::deleteLater );
0046 }
0047 
0048 Fadeouter::~Fadeouter()
0049 {
0050     if( m_fader )
0051         // warning: phonon-gstreamer bug 313551 (still present in 4.6.2) prevents
0052         // following call to succeed if a fade is still in progress
0053         m_fader->fadeIn( safetyDelay ); // fade-in, just in case, be nice to ears
0054 }
0055 
0056 void
0057 Fadeouter::slotFinalizeFadeout()
0058 {
0059     Q_EMIT fadeoutFinished();
0060     deleteLater();
0061 }