C++ modernize: Pragma once (#6264)
[oweals/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 #pragma once
21
22 #include <set>
23 #include <string>
24 #include "irrlichttypes_bloated.h"
25
26 class OnDemandSoundFetcher
27 {
28 public:
29         virtual void fetchSounds(const std::string &name,
30                         std::set<std::string> &dst_paths,
31                         std::set<std::string> &dst_datas) = 0;
32 };
33
34 struct SimpleSoundSpec
35 {
36         SimpleSoundSpec(const std::string &name = "", float gain = 1.0f,
37                         float fade = 0.0f, float pitch = 1.0f)
38             : name(name), gain(gain), fade(fade), pitch(pitch)
39         {
40         }
41
42         bool exists() const { return name != ""; }
43
44         std::string name = "";
45         float gain = 1.0f;
46         float fade = 0.0f;
47         float pitch = 1.0f;
48 };
49
50 class ISoundManager
51 {
52 public:
53         virtual ~ISoundManager() {}
54         // Multiple sounds can be loaded per name; when played, the sound
55         // should be chosen randomly from alternatives
56         // Return value determines success/failure
57         virtual bool loadSoundFile(
58                         const std::string &name, const std::string &filepath) = 0;
59         virtual bool loadSoundData(
60                         const std::string &name, const std::string &filedata) = 0;
61
62         virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
63         virtual void setListenerGain(float gain) = 0;
64
65         // playSound functions return -1 on failure, otherwise a handle to the
66         // sound. If name=="", call should be ignored without error.
67         virtual int playSound(const std::string &name, bool loop, float volume,
68                         float fade = 0.0f, float pitch = 1.0f) = 0;
69         virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
70                         float pitch = 1.0f) = 0;
71         virtual void stopSound(int sound) = 0;
72         virtual bool soundExists(int sound) = 0;
73         virtual void updateSoundPosition(int sound, v3f pos) = 0;
74         virtual bool updateSoundGain(int id, float gain) = 0;
75         virtual float getSoundGain(int id) = 0;
76         virtual void step(float dtime) = 0;
77         virtual void fadeSound(int sound, float step, float gain) = 0;
78
79         int playSound(const SimpleSoundSpec &spec, bool loop)
80         {
81                 return playSound(spec.name, loop, spec.gain, spec.fade, spec.pitch);
82         }
83         int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
84         {
85                 return playSoundAt(spec.name, loop, spec.gain, pos, spec.pitch);
86         }
87 };
88
89 class DummySoundManager : public ISoundManager
90 {
91 public:
92         virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
93         {
94                 return true;
95         }
96         virtual bool loadSoundData(const std::string &name, const std::string &filedata)
97         {
98                 return true;
99         }
100         void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
101         void setListenerGain(float gain) {}
102         int playSound(const std::string &name, bool loop, float volume, float fade,
103                         float pitch)
104         {
105                 return 0;
106         }
107         int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
108                         float pitch)
109         {
110                 return 0;
111         }
112         void stopSound(int sound) {}
113         bool soundExists(int sound) { return false; }
114         void updateSoundPosition(int sound, v3f pos) {}
115         bool updateSoundGain(int id, float gain) { return false; }
116         float getSoundGain(int id) { return 0; }
117         void step(float dtime) {}
118         void fadeSound(int sound, float step, float gain) {}
119 };
120
121 // Global DummySoundManager singleton
122 extern DummySoundManager dummySoundManager;