summaryrefslogtreecommitdiff
path: root/src/audio.cpp
blob: b8069596c268fe0658c8ce36be22bf34e17f8d8e (plain)
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
/*
This file is part of Minitrem.

Minitrem 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 2 of the License, or
(at your option) any later version.

Minitrem 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 Minitrem.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.hpp"

namespace audio {

static std::list<sf::Sound> playing_sounds;
static std::list<ambient_t*> ambient_sounds;

void sound_t::load(const char *path)
{
	printf("load %s\n", path);

	auto sound = sounds.emplace(sounds.end());
	sound->loadFromFile(path);
}

void sound_t::play(void)
{
	if (!sounds.size())
		return;

	auto sound = playing_sounds.emplace(playing_sounds.end());
	sound->setBuffer(sounds[rand() % sounds.size()]);
	sound->setVolume(volume * 100.0f);
	sound->setRelativeToListener(true); // disable spatialization
	sound->play();
}

void sound_t::play_3d(v2f_t x)
{
	if (!sounds.size())
		return;

	auto sound = playing_sounds.emplace(playing_sounds.end());
	sound->setBuffer(sounds[rand() % sounds.size()]);
	sound->setVolume(volume * 100.0f);
	sound->setPosition(x[0], x[1], 0.0f);
	sound->play();
}

void ambient_t::load(const char *path)
{
	printf("load %s\n", path);
	sound.openFromFile(path);
	sound.setLoop(true);
	ambient_sounds.push_back(this);
}

void update(v3f_t camera, bool paused)
{
	// update 3D settings
	// the listener is looking down at the XY plane with +X pointing right
	// and +Y pointing down
	sf::Listener::setPosition(camera);
	sf::Listener::setDirection(0.0f, 0.0f, -1.0f);
	sf::Listener::setUpVector(0.0f, 1.0f, 0.0f); 

	// remove sounds that finished playing from playing_sounds
	playing_sounds.remove_if( [](sf::Sound &sound) {
		return sound.getStatus() == sf::Sound::Stopped;
	});

	// update ambient sounds (weights set by game)
	for (ambient_t *ambient : ambient_sounds) {
		v3f_t origin;

		if (paused) {
			if (ambient->playing) {
				ambient->sound.stop();
				ambient->playing = false;
			}
			continue;
		}

		if (!ambient->playing) {
			ambient->sound.play();
			ambient->playing = true;
		}

		origin[0] = ambient->origin[0];
		origin[1] = ambient->origin[1];
		origin[2] = 0.0f;
		ambient->sound.setPosition(origin);
		ambient->sound.setVolume(ambient->volume * ambient->weight * 100.0f);
	}
}

} // namespace audio