~ubuntu-branches/ubuntu/vivid/libkdegames/vivid

« back to all changes in this revision

Viewing changes to audio/kgsound-phonon.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-01 18:03:49 UTC
  • mfrom: (1.3.13) (42.1.2 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141201180349-is1dvtxoi01klupf
Tags: 4:14.11.97-0ubuntu1
* New upstream RC release
* Merge with Debian, remaining changes:
 - not-installed
* kdegames-card-data replaces/breaks old split out packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright 2011 Stefan Majewsky <majewsky@gmx.net>                     *
3
 
 *                                                                         *
4
 
 *   This program is free software; you can redistribute it and/or modify  *
5
 
 *   it under the terms of the GNU Library General Public License          *
6
 
 *   version 2 as published by the Free Software Foundation                *
7
 
 *                                                                         *
8
 
 *   This program is distributed in the hope that it will be useful,       *
9
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11
 
 *   GNU Library General Public License for more details.                  *
12
 
 *                                                                         *
13
 
 *   You should have received a copy of the GNU Library General Public     *
14
 
 *   License along with this program; if not, write to the                 *
15
 
 *   Free Software Foundation, Inc.,                                       *
16
 
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
17
 
 ***************************************************************************/
18
 
 
19
 
#include "kgsound.h"
20
 
 
21
 
#include <Phonon/MediaObject>
22
 
#include <QDateTime>
23
 
 
24
 
class KgSound::Private
25
 
{
26
 
    public:
27
 
        qreal m_volume;
28
 
        bool m_valid;
29
 
        qint64 m_lastPlayedTime;
30
 
        int m_nextSource;
31
 
        Phonon::MediaObject* m_sound1;
32
 
        Phonon::MediaObject* m_sound2;
33
 
 
34
 
        Private() : m_volume(1.0), m_valid(false), m_lastPlayedTime(0), m_nextSource(1), m_sound1(NULL), m_sound2(NULL) {}
35
 
 
36
 
        ~Private()
37
 
        {
38
 
            delete m_sound1;
39
 
            delete m_sound2;
40
 
            m_sound1 = 0;
41
 
            m_sound2 = 0;
42
 
        }
43
 
};
44
 
 
45
 
KgSound::KgSound(const QString& file, QObject* parent)
46
 
        : QObject(parent)
47
 
        , d(new Private)
48
 
{
49
 
        d->m_sound1 = Phonon::createPlayer(Phonon::GameCategory);
50
 
        d->m_sound1->setCurrentSource(file);
51
 
        d->m_sound2 = Phonon::createPlayer(Phonon::GameCategory);
52
 
        d->m_sound2->setCurrentSource(file);
53
 
        d->m_valid = d->m_sound1->isValid() && d->m_sound2->isValid();
54
 
}
55
 
 
56
 
KgSound::~KgSound()
57
 
{
58
 
        delete d;
59
 
}
60
 
 
61
 
bool KgSound::isValid() const
62
 
{
63
 
        return d->m_valid;
64
 
}
65
 
 
66
 
KgSound::PlaybackType KgSound::playbackType() const
67
 
{
68
 
        return KgSound::AmbientPlayback;
69
 
}
70
 
 
71
 
void KgSound::setPlaybackType(KgSound::PlaybackType type)
72
 
{
73
 
        Q_UNUSED(type)
74
 
}
75
 
 
76
 
QPointF KgSound::pos() const
77
 
{
78
 
        return QPointF(0.0, 0.0);
79
 
}
80
 
 
81
 
void KgSound::setPos(const QPointF& pos)
82
 
{
83
 
        Q_UNUSED(pos)
84
 
}
85
 
 
86
 
qreal KgSound::volume() const
87
 
{
88
 
        //FIXME
89
 
        return 1.0;
90
 
}
91
 
 
92
 
void KgSound::setVolume(qreal volume)
93
 
{
94
 
        //FIXME
95
 
}
96
 
 
97
 
bool KgSound::hasError() const
98
 
{
99
 
        if (d->m_sound1 && d->m_sound1->state() == Phonon::ErrorState)
100
 
        {
101
 
                return true;
102
 
        }
103
 
        if (d->m_sound2 && d->m_sound2->state() == Phonon::ErrorState)
104
 
        {
105
 
                return true;
106
 
        }
107
 
        return false;
108
 
}
109
 
 
110
 
void KgSound::start()
111
 
{
112
 
        if(!d->m_sound1 || !d->m_sound2)
113
 
        {
114
 
                return;
115
 
        }
116
 
        
117
 
        QDateTime now = QDateTime::currentDateTime();
118
 
        qint64 timeNow = now.toTime_t() * 1000 + now.time().msec();
119
 
        
120
 
        if(timeNow - d->m_lastPlayedTime > 20)
121
 
        {
122
 
                if(d->m_nextSource == 1)
123
 
                {                    
124
 
                        if(d->m_sound1->state() == Phonon::StoppedState)
125
 
                        {
126
 
                                d->m_nextSource = 2;
127
 
                                d->m_sound1->play();
128
 
                        }
129
 
                        else
130
 
                        {
131
 
                                d->m_sound1->stop();
132
 
                        }
133
 
                }
134
 
                else
135
 
                {
136
 
                        if(d->m_sound2->state() == Phonon::StoppedState)
137
 
                        {
138
 
                                d->m_nextSource = 1;
139
 
                                d->m_sound2->play();
140
 
                        }
141
 
                        else
142
 
                        {
143
 
                                d->m_sound2->stop();
144
 
                        }
145
 
                }
146
 
                d->m_lastPlayedTime = timeNow;
147
 
        }
148
 
}
149
 
 
150
 
void KgSound::start(const QPointF& pos)
151
 
{
152
 
        Q_UNUSED(pos)
153
 
        //ignore parameter
154
 
        start();
155
 
}
156
 
 
157
 
void KgSound::stop()
158
 
{
159
 
        if(!d->m_sound1 || !d->m_sound2)
160
 
        {
161
 
                return;
162
 
        }
163
 
        
164
 
        d->m_sound1->stop();
165
 
        d->m_sound2->stop();
166
 
}
167
 
 
168
 
#include "kgsound.moc"