]> git.lizzy.rs Git - minetest.git/blob - src/sound.h
Document that write_json will error on unserializable types. (#5539)
[minetest.git] / src / sound.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef SOUND_HEADER
21 #define SOUND_HEADER
22
23 #include <set>
24 #include <string>
25 #include "irrlichttypes_bloated.h"
26
27 class OnDemandSoundFetcher
28 {
29 public:
30         virtual void fetchSounds(const std::string &name,
31                         std::set<std::string> &dst_paths,
32                         std::set<std::string> &dst_datas) = 0;
33 };
34
35 struct SimpleSoundSpec
36 {
37         std::string name;
38         float gain;
39         SimpleSoundSpec(std::string name = "", float gain = 1.0) : name(name), gain(gain)
40         {
41         }
42         bool exists() { return name != ""; }
43         // Serialization intentionally left out
44 };
45
46 class ISoundManager
47 {
48 public:
49         virtual ~ISoundManager() {}
50
51         // Multiple sounds can be loaded per name; when played, the sound
52         // should be chosen randomly from alternatives
53         // Return value determines success/failure
54         virtual bool loadSoundFile(
55                         const std::string &name, const std::string &filepath) = 0;
56         virtual bool loadSoundData(
57                         const std::string &name, const std::string &filedata) = 0;
58
59         virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
60         virtual void setListenerGain(float gain) = 0;
61
62         // playSound functions return -1 on failure, otherwise a handle to the
63         // sound. If name=="", call should be ignored without error.
64         virtual int playSound(const std::string &name, bool loop, float volume) = 0;
65         virtual int playSoundAt(
66                         const std::string &name, bool loop, float volume, v3f pos) = 0;
67         virtual void stopSound(int sound) = 0;
68         virtual bool soundExists(int sound) = 0;
69         virtual void updateSoundPosition(int sound, v3f pos) = 0;
70
71         int playSound(const SimpleSoundSpec &spec, bool loop)
72         {
73                 return playSound(spec.name, loop, spec.gain);
74         }
75         int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
76         {
77                 return playSoundAt(spec.name, loop, spec.gain, pos);
78         }
79 };
80
81 class DummySoundManager : public ISoundManager
82 {
83 public:
84         virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
85         {
86                 return true;
87         }
88         virtual bool loadSoundData(const std::string &name, const std::string &filedata)
89         {
90                 return true;
91         }
92         void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
93         void setListenerGain(float gain) {}
94         int playSound(const std::string &name, bool loop, float volume) { return 0; }
95         int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
96         {
97                 return 0;
98         }
99         void stopSound(int sound) {}
100         bool soundExists(int sound) { return false; }
101         void updateSoundPosition(int sound, v3f pos) {}
102 };
103
104 // Global DummySoundManager singleton
105 extern DummySoundManager dummySoundManager;
106
107 #endif