1
/*  This file is part of the KDE project.
2
3
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5
This library is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 2.1 or 3 of the License.
8
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Lesser General Public License for more details.
13
14
You should have received a copy of the GNU Lesser General Public License
15
along with this library.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include "audiooutput.h"
19
#include "mediaobject.h"
20
21
#include <QtCore/qmath.h>
22
23
QT_BEGIN_NAMESPACE
24
25
namespace Phonon
26
{
27
    namespace DS9
28
    {
29
        AudioOutput::AudioOutput(Backend *back, QObject *parent)
30
            : BackendNode(parent), m_currentIndex(0), m_crossfadeProgress(1.),
31
              m_device(-1), m_backend(back), m_volume(0.)
32
        {
33
        }
34
35
        AudioOutput::~AudioOutput()
36
        {
37
        }
38
39
        int AudioOutput::outputDevice() const
40
        {
41
            return m_device;
42
        }
43
44
        static const qreal log10over20 = qreal(0.1151292546497022842); // ln(10) / 20
45
46
        void AudioOutput::setVolume(qreal newVolume)
47
        {
48
            for(int i = 0; i < FILTER_COUNT; ++i) {
49
                ComPointer<IBasicAudio> audio(m_filters[i], IID_IBasicAudio);
50
                if (audio) {
51
                    const qreal currentVolume = newVolume * (m_currentIndex == i ? m_crossfadeProgress : 1-m_crossfadeProgress);
52
                    const qreal newDbVolume = (qMax(0., 1.-::log(::pow(currentVolume, -log10over20)))-1.) * 10000;
53
                    audio->put_Volume(qRound(newDbVolume));
54
                }
55
            }
56
57
            if (m_volume != newVolume) {
58
                m_volume = newVolume;
59
                emit volumeChanged(newVolume);
60
            }
61
        }
62
63
        void AudioOutput::setCrossFadingProgress(short currentIndex, qreal progress)
64
        {
65
            m_crossfadeProgress = progress;
66
            m_currentIndex = currentIndex;
67
            setVolume(m_volume);
68
        }
69
70
        bool AudioOutput::setOutputDevice(const AudioOutputDevice & newDevice)
71
        {
72
            //stub implementation
73
            return setOutputDevice(newDevice.index());
74
        }
75
76
        qreal AudioOutput::volume() const
77
        {
78
            return m_volume;
79
        }
80
81
        bool AudioOutput::setOutputDevice(int newDevice)
82
        {
83
            if (newDevice == m_device) {
84
                return true;
85
            }
86
87
            //free the previous one if it was already set
88
            for(int i = 0; i < FILTER_COUNT; ++i) {
89
                const Filter &oldFilter = m_filters[i];
90
91
                Filter newFilter = m_backend->getAudioOutputFilter(newDevice);
92
93
                if (m_mediaObject && oldFilter && newFilter) {
94
                    m_mediaObject->switchFilters(i, oldFilter, newFilter);
95
                }
96
97
                m_filters[i] = newFilter; 
98
99
100
            }
101
102
            m_device = newDevice;
103
            setVolume(m_volume);
104
            return true;
105
        }
106
    }
107
}
108
109
QT_END_NAMESPACE
110
111
#include "moc_audiooutput.cpp"