summaryrefslogtreecommitdiff
path: root/src/sound.h
blob: 76c0d1be4cfd5e8d9a8ff76fd176c680de800dcd (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef SOUND_HEADER
#define SOUND_HEADER

#include <set>
#include <string>
#include "irrlichttypes_bloated.h"

class OnDemandSoundFetcher
{
public:
	virtual void fetchSounds(const std::string &name,
			std::set<std::string> &dst_paths,
			std::set<std::string> &dst_datas) = 0;
};

struct SimpleSoundSpec
{
	SimpleSoundSpec(const std::string &name = "", float gain = 1.0, float fade = 0.0)
	    : name(name), gain(gain), fade(fade)
	{
	}

	bool exists() const { return name != ""; }

	std::string name;
	float gain;
	float fade;
};

class ISoundManager
{
public:
	virtual ~ISoundManager() {}
	// Multiple sounds can be loaded per name; when played, the sound
	// should be chosen randomly from alternatives
	// Return value determines success/failure
	virtual bool loadSoundFile(
			const std::string &name, const std::string &filepath) = 0;
	virtual bool loadSoundData(
			const std::string &name, const std::string &filedata) = 0;

	virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
	virtual void setListenerGain(float gain) = 0;

	// playSound functions return -1 on failure, otherwise a handle to the
	// sound. If name=="", call should be ignored without error.
	virtual int playSound(const std::string &name, bool loop, float volume,
			float fade = 0) = 0;
	virtual int playSoundAt(
			const std::string &name, bool loop, float volume, v3f pos) = 0;
	virtual void stopSound(int sound) = 0;
	virtual bool soundExists(int sound) = 0;
	virtual void updateSoundPosition(int sound, v3f pos) = 0;
	virtual bool updateSoundGain(int id, float gain) = 0;
	virtual float getSoundGain(int id) = 0;
	virtual void step(float dtime) = 0;
	virtual void fadeSound(int sound, float step, float gain) = 0;

	int playSound(const SimpleSoundSpec &spec, bool loop)
	{
		return playSound(spec.name, loop, spec.gain, spec.fade);
	}
	int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
	{
		return playSoundAt(spec.name, loop, spec.gain, pos);
	}
};

class DummySoundManager : public ISoundManager
{
public:
	virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
	{
		return true;
	}
	virtual bool loadSoundData(const std::string &name, const std::string &filedata)
	{
		return true;
	}
	void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
	void setListenerGain(float gain) {}
	int playSound(const std::string &name, bool loop, float volume, float fade)
	{
		return 0;
	}
	int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
	{
		return 0;
	}
	void stopSound(int sound) {}
	bool soundExists(int sound) { return false; }
	void updateSoundPosition(int sound, v3f pos) {}
	bool updateSoundGain(int id, float gain) { return false; }
	float getSoundGain(int id) { return 0; }
	void step(float dtime) {}
	void fadeSound(int sound, float step, float gain) {}
};

// Global DummySoundManager singleton
extern DummySoundManager dummySoundManager;

#endif