1 | /* This file is part of the KDE project. |
2 | * |
3 | * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
4 | * Copyright (C) 2013 Martin Sandsmark <martin.sandsmark@kde.org> |
5 | * |
6 | * This library is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation, either version 2.1 or 3 of the License. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public License |
16 | * along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include "volumefadereffect.h" |
20 | #include <mediaplayer.h> |
21 | |
22 | #include "utils/debug.h" |
23 | |
24 | #include <QtCore/QTimeLine> |
25 | |
26 | #ifndef QT_NO_PHONON_VOLUMEFADEREFFECT |
27 | namespace Phonon |
28 | { |
29 | namespace VLC |
30 | { |
31 | VolumeFaderEffect::VolumeFaderEffect(QObject *parent) |
32 | : QObject(parent) |
33 | , SinkNode() |
34 | , m_fadeCurve(Phonon::VolumeFaderEffect::Fade3Decibel) |
35 | , m_fadeFromVolume(0) |
36 | , m_fadeToVolume(0) |
37 | { |
38 | m_fadeTimeline = new QTimeLine(1000, this); |
39 | connect(sender: m_fadeTimeline, SIGNAL(valueChanged(qreal)), receiver: this, SLOT(slotSetVolume(qreal))); |
40 | } |
41 | |
42 | VolumeFaderEffect::~VolumeFaderEffect() |
43 | { |
44 | } |
45 | |
46 | float VolumeFaderEffect::volume() const |
47 | { |
48 | Q_ASSERT(m_player); |
49 | return m_player->audioVolume() / 100.0f; |
50 | } |
51 | |
52 | void VolumeFaderEffect::slotSetVolume(qreal volume) |
53 | { |
54 | float gstVolume = m_fadeFromVolume + (volume * (m_fadeToVolume - m_fadeFromVolume)); |
55 | setVolumeInternal(gstVolume); |
56 | } |
57 | |
58 | Phonon::VolumeFaderEffect::FadeCurve VolumeFaderEffect::fadeCurve() const |
59 | { |
60 | return m_fadeCurve; |
61 | } |
62 | |
63 | void VolumeFaderEffect::setFadeCurve(Phonon::VolumeFaderEffect::FadeCurve pFadeCurve) |
64 | { |
65 | m_fadeCurve = pFadeCurve; |
66 | QEasingCurve fadeCurve; |
67 | switch(pFadeCurve) { |
68 | case Phonon::VolumeFaderEffect::Fade3Decibel: |
69 | fadeCurve = QEasingCurve::InQuad; |
70 | break; |
71 | case Phonon::VolumeFaderEffect::Fade6Decibel: |
72 | fadeCurve = QEasingCurve::Linear; |
73 | break; |
74 | case Phonon::VolumeFaderEffect::Fade9Decibel: |
75 | fadeCurve = QEasingCurve::OutCubic; |
76 | break; |
77 | case Phonon::VolumeFaderEffect::Fade12Decibel: |
78 | fadeCurve = QEasingCurve::OutQuart; |
79 | break; |
80 | } |
81 | m_fadeTimeline->setEasingCurve(fadeCurve); |
82 | } |
83 | |
84 | void VolumeFaderEffect::fadeTo(float targetVolume, int fadeTime) |
85 | { |
86 | Q_ASSERT(m_player); |
87 | abortFade(); |
88 | m_fadeToVolume = targetVolume; |
89 | m_fadeFromVolume = m_player->audioVolume() / 100.0f; |
90 | |
91 | // Don't call QTimeLine::setDuration() with zero. |
92 | // It is not supported and breaks fading. |
93 | if (fadeTime <= 0) { |
94 | debug() << "Called with retarded fade time " << fadeTime; |
95 | setVolumeInternal(targetVolume); |
96 | return; |
97 | } |
98 | |
99 | m_fadeTimeline->setDuration(fadeTime); |
100 | m_fadeTimeline->start(); |
101 | } |
102 | |
103 | void VolumeFaderEffect::setVolume(float v) |
104 | { |
105 | abortFade(); |
106 | setVolumeInternal(v); |
107 | } |
108 | |
109 | void VolumeFaderEffect::abortFade() |
110 | { |
111 | m_fadeTimeline->stop(); |
112 | } |
113 | |
114 | void VolumeFaderEffect::setVolumeInternal(float v) |
115 | { |
116 | if (m_player) |
117 | m_player->setAudioFade(v); |
118 | else |
119 | warning() << Q_FUNC_INFO << this << "no m_player set" ; |
120 | } |
121 | |
122 | } |
123 | } //namespace Phonon::VLC |
124 | |
125 | #endif //QT_NO_PHONON_VOLUMEFADEREFFECT |
126 | #include "moc_volumefadereffect.cpp" |
127 | |
128 | |