File: snd_dispatcher.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (215 lines) | stat: -rw-r--r-- 5,152 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/config-manager.h"
#include "qdengine/qd_fwd.h"
#include "qdengine/xmath.h"
#include "qdengine/system/graphics/gr_dispatcher.h"
#include "qdengine/system/sound/snd_dispatcher.h"
#include "qdengine/qdcore/util/plaympp_api.h"

namespace QDEngine {

sndDispatcher *sndDispatcher::_dispatcher_ptr;

static bool operator == (const sndSound &snd0, const sndSound &snd1) {
	return snd0.sound() == snd1.sound();
}

static bool operator == (const sndSound &snd, const sndHandle &h) {
	return snd.handle() == &h;
}

sndDispatcher::sndDispatcher() : _is_enabled(true),
		_is_paused(false),
		_volume(255),
		_volume_dB(0),
		_frequency_coeff(1.0f) {

	if (!_dispatcher_ptr)
		_dispatcher_ptr = this;
}

sndDispatcher::~sndDispatcher() {
	_sounds.clear();

	if (_dispatcher_ptr == this)
		_dispatcher_ptr = NULL;
}

void sndDispatcher::set_volume(uint32 vol) {
	_volume = vol & 0xFF;

	_volume_dB = convert_volume_to_dB(_volume);

	update_volume();
}

int sndDispatcher::convert_volume_to_dB(int vol) {
	if (vol > 255) vol = 255;
	if (vol < 0) vol = 0;

	if (vol != 255) {
		const int DB_MIN = -10000;
		const int DB_MAX = 0;
		const int DB_SIZE = DB_MAX - DB_MIN;

		return (DB_MIN + round(log10(9.0 * log(double(vol + 1)) / (log(2.0) * 8) + 1.0) * DB_SIZE));
	} else
		return 0;
}

void sndDispatcher::quant() {
	for (auto it = _sounds.begin(); it != _sounds.end(); ) {
			if (it->is_stopped())
				it = _sounds.erase(it);
			else
				++it;
	}
}

bool sndDispatcher::play_sound(const sndSound *snd, bool loop, int vol) {
	if (is_enabled()) {
		_sounds.push_back(sndSound(*snd));
		sndSound &p = _sounds.back();

		if (loop)
			p.toggle_looping();

		int snd_volume = (vol == 255) ? volume_dB() : convert_volume_to_dB((volume() * vol) >> 8);

		if (!p.create_sound_buffer())
			return false;

		p.set_volume(snd_volume);
		p.change_frequency(frequency_coeff());

		if (!is_paused()) {
			if (!p.play()) return false;
		} else
			p.pause();
	}

	return true;
}

bool sndDispatcher::stop_sound(const sndSound *snd) {
	sound_list_t::iterator it = Common::find(_sounds.begin(), _sounds.end(), *snd);

	if (it != _sounds.end()) {
		it->stop();
		_sounds.erase(it);

		return true;
	}

	return false;
}

bool sndDispatcher::stop_sound(const sndHandle *handle) {
	sound_list_t::iterator it = Common::find(_sounds.begin(), _sounds.end(), *handle);

	if (it != _sounds.end()) {
		it->stop();
		_sounds.erase(it);

		return true;
	}

	return false;
}

sndSound::status_t sndDispatcher::sound_status(const sndHandle *handle) const {
	sound_list_t::const_iterator it = Common::find(_sounds.begin(), _sounds.end(), *handle);

	if (it != _sounds.end()) {
		if (is_paused())
			return sndSound::SOUND_PAUSED;

		return sndSound::SOUND_PLAYING;
	}

	return sndSound::SOUND_STOPPED;
}

sndSound::status_t sndDispatcher::sound_status(const sndSound *snd) const {
	sound_list_t::const_iterator it = Common::find(_sounds.begin(), _sounds.end(), *snd);

	if (it != _sounds.end())
		return it->status();

	return sndSound::SOUND_STOPPED;
}

bool sndDispatcher::update_volume() {
	for (sound_list_t::iterator it = _sounds.begin(); it != _sounds.end(); ++it)
		it->set_volume(volume_dB());

	return true;
}

bool sndDispatcher::update_frequency() {
	for (sound_list_t::iterator it = _sounds.begin(); it != _sounds.end(); ++it)
		it->change_frequency(frequency_coeff());

	return true;
}

void sndDispatcher::stop_sounds() {
	for (sound_list_t::iterator it = _sounds.begin(); it != _sounds.end(); ++it)
		it->stop();

	_sounds.clear();
}

bool sndDispatcher::set_sound_frequency(const sndHandle *snd, float coeff) {
	sound_list_t::iterator it = Common::find(_sounds.begin(), _sounds.end(), *snd);

	if (it != _sounds.end()) {
		it->change_frequency(frequency_coeff() * coeff);
		return true;
	}

	return false;
}

void sndDispatcher::pause_sounds() {
	for (sound_list_t::iterator it = _sounds.begin(); it != _sounds.end(); ++it)
		it->pause();
}

void sndDispatcher::resume_sounds() {
	for (sound_list_t::iterator it = _sounds.begin(); it != _sounds.end(); ++it) {
		if (it->is_paused())
			it->resume();
	}
}

void sndDispatcher::syncSoundSettings() {
	set_volume(ConfMan.getInt("sound_volume"));

	if (ConfMan.getBool("enable_sound"))
		enable();
	else
		disable();
}

} // namespace QDEngine