1 /***********************************************************************
2  *
3  * Copyright (C) 2010, 2011, 2012, 2013, 2016 Graeme Gott <graeme@gottcode.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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 General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #include "sound.h"
21 
22 #include <QApplication>
23 #include <QHash>
24 #include <QSoundEffect>
25 
26 //-----------------------------------------------------------------------------
27 
28 namespace
29 {
30 	// Shared data
31 	QString f_path;
32 	bool f_enabled = false;
33 
34 	QHash<int, Sound*> f_sound_objects;
35 }
36 
37 //-----------------------------------------------------------------------------
38 
Sound(int name,const QString & filename,QObject * parent)39 Sound::Sound(int name, const QString& filename, QObject* parent) :
40 	QObject(parent),
41 	m_name(name)
42 {
43 	QSoundEffect* sound = new QSoundEffect(this);
44 	sound->setSource(QUrl::fromLocalFile(f_path + "/" + filename));
45 	while (sound->status() == QSoundEffect::Loading) {
46 		QApplication::processEvents();
47 	}
48 	m_sounds.append(sound);
49 
50 	f_sound_objects.remove(m_name);
51 	if (sound->status() != QSoundEffect::Error) {
52 		f_sound_objects.insert(m_name, this);
53 	}
54 }
55 
56 //-----------------------------------------------------------------------------
57 
~Sound()58 Sound::~Sound()
59 {
60 	f_sound_objects.remove(m_name);
61 }
62 
63 //-----------------------------------------------------------------------------
64 
isValid() const65 bool Sound::isValid() const
66 {
67 	return f_sound_objects.contains(m_name);
68 }
69 
70 //-----------------------------------------------------------------------------
71 
play()72 void Sound::play()
73 {
74 	QSoundEffect* sound = nullptr;
75 	for (int i = 0, end = m_sounds.size(); i < end; ++i) {
76 		if (!m_sounds.at(i)->isPlaying()) {
77 			sound = m_sounds.at(i);
78 			break;
79 		}
80 	}
81 	if (sound == nullptr) {
82 		sound = new QSoundEffect(this);
83 		sound->setSource(m_sounds.first()->source());
84 		m_sounds.append(sound);
85 	}
86 	sound->play();
87 }
88 
89 //-----------------------------------------------------------------------------
90 
play(int name)91 void Sound::play(int name)
92 {
93 	if (!f_enabled) {
94 		return;
95 	}
96 
97 	Sound* sound = f_sound_objects.value(name);
98 	if (sound) {
99 		sound->play();
100 	}
101 }
102 
103 //-----------------------------------------------------------------------------
104 
setEnabled(bool enabled)105 void Sound::setEnabled(bool enabled)
106 {
107 	f_enabled = enabled;
108 }
109 
110 //-----------------------------------------------------------------------------
111 
setPath(const QString & path)112 void Sound::setPath(const QString& path)
113 {
114 	f_path = path;
115 }
116 
117 //-----------------------------------------------------------------------------
118