1 /*
2     SPDX-FileCopyrightText: 2011 Ian Wadham <iandw.au at gmail dot com>
3     SPDX-FileCopyrightText: 2007 Luciano Montanaro <mikelima@cirulla.net>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "kgrsounds.h"
9 
10 #include "kgoldrunner_debug.h"
11 
KGrSounds()12 KGrSounds::KGrSounds() :
13     QObject(),
14     sounds()
15 {
16     t.start();
17 }
18 
~KGrSounds()19 KGrSounds::~KGrSounds()
20 {
21     qDeleteAll(sounds);
22 }
23 
loadSound(const QString & fileName)24 int KGrSounds::loadSound (const QString &fileName)
25 {
26     //qCDebug(KGOLDRUNNER_LOG) << "Loading sound" << fileName;
27     sounds << (new KgSound (fileName));
28     startTime << 0;
29     return sounds.count() - 1;
30 }
31 
setTimedSound(int i)32 void KGrSounds::setTimedSound (int i)
33 {
34     startTime[i] = 1;
35 }
36 
stopAllSounds()37 void KGrSounds::stopAllSounds()
38 {
39     for (int i = 0; i < sounds.count(); i++) {
40 	sounds[i]->stop();
41     }
42 }
43 
reset()44 void KGrSounds::reset()
45 {
46     stopAllSounds();
47     sounds.clear();
48 }
49 
play(int effect)50 int KGrSounds::play (int effect)
51 {
52     if (muted) return -1;
53 
54     // Delete all previously playing instances of this sound, but allow gold and
55     // dig sounds to play for > 1 sec, so that rapid sequences of digging or
56     // gold collection will have properly overlapping sounds.
57 
58     int  started    = startTime[effect];
59     bool timedSound = (started != 0);
60     int  current    = timedSound ? t.elapsed() : 0;
61 
62     if ((! timedSound) || ((current - started) > 1000)) {
63         sounds[effect]->stop();
64     }
65 
66     sounds[effect]->start();
67 
68     if (timedSound) {
69         startTime[effect] = current;
70     }
71     return effect;
72 }
73 
stop(int effect)74 void KGrSounds::stop (int effect)
75 {
76     if (muted) return;
77 
78     sounds[effect]->stop();
79 }
80 
setMuted(bool mute)81 void KGrSounds::setMuted (bool mute)
82 {
83     muted = mute;
84     if (mute) {
85 	stopAllSounds();
86     }
87 }
88 
setVolume(int effect,qreal volume)89 void KGrSounds::setVolume (int effect, qreal volume)
90 {
91     sounds[effect]->setVolume (volume);
92 }
93 
94 
95