Minetest  5.4.0
sound.h
Go to the documentation of this file.
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 "irr_v3d.h"
25 #include "../sound.h"
26 
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 
36 {
37 public:
38  virtual ~ISoundManager() = default;
39 
40  // Multiple sounds can be loaded per name; when played, the sound
41  // should be chosen randomly from alternatives
42  // Return value determines success/failure
43  virtual bool loadSoundFile(
44  const std::string &name, const std::string &filepath) = 0;
45  virtual bool loadSoundData(
46  const std::string &name, const std::string &filedata) = 0;
47 
48  virtual void updateListener(
49  const v3f &pos, const v3f &vel, const v3f &at, const v3f &up) = 0;
50  virtual void setListenerGain(float gain) = 0;
51 
52  // playSound functions return -1 on failure, otherwise a handle to the
53  // sound. If name=="", call should be ignored without error.
54  virtual int playSound(const std::string &name, bool loop, float volume,
55  float fade = 0.0f, float pitch = 1.0f) = 0;
56  virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
57  float pitch = 1.0f) = 0;
58  virtual void stopSound(int sound) = 0;
59  virtual bool soundExists(int sound) = 0;
60  virtual void updateSoundPosition(int sound, v3f pos) = 0;
61  virtual bool updateSoundGain(int id, float gain) = 0;
62  virtual float getSoundGain(int id) = 0;
63  virtual void step(float dtime) = 0;
64  virtual void fadeSound(int sound, float step, float gain) = 0;
65 
66  int playSound(const SimpleSoundSpec &spec, bool loop)
67  {
68  return playSound(spec.name, loop, spec.gain, spec.fade, spec.pitch);
69  }
70  int playSoundAt(const SimpleSoundSpec &spec, bool loop, const v3f &pos)
71  {
72  return playSoundAt(spec.name, loop, spec.gain, pos, spec.pitch);
73  }
74 };
75 
77 {
78 public:
79  virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
80  {
81  return true;
82  }
83  virtual bool loadSoundData(const std::string &name, const std::string &filedata)
84  {
85  return true;
86  }
87  void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up)
88  {
89  }
90  void setListenerGain(float gain) {}
91  int playSound(const std::string &name, bool loop, float volume, float fade,
92  float pitch)
93  {
94  return 0;
95  }
96  int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
97  float pitch)
98  {
99  return 0;
100  }
101  void stopSound(int sound) {}
102  bool soundExists(int sound) { return false; }
103  void updateSoundPosition(int sound, v3f pos) {}
104  bool updateSoundGain(int id, float gain) { return false; }
105  float getSoundGain(int id) { return 0; }
106  void step(float dtime) {}
107  void fadeSound(int sound, float step, float gain) {}
108 };
109 
110 // Global DummySoundManager singleton
Definition: sound.h:77
bool soundExists(int sound)
Definition: sound.h:102
virtual bool loadSoundData(const std::string &name, const std::string &filedata)
Definition: sound.h:83
void step(float dtime)
Definition: sound.h:106
virtual bool loadSoundFile(const std::string &name, const std::string &filepath)
Definition: sound.h:79
void setListenerGain(float gain)
Definition: sound.h:90
bool updateSoundGain(int id, float gain)
Definition: sound.h:104
int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
Definition: sound.h:91
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
Definition: sound.h:96
void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up)
Definition: sound.h:87
void stopSound(int sound)
Definition: sound.h:101
void updateSoundPosition(int sound, v3f pos)
Definition: sound.h:103
void fadeSound(int sound, float step, float gain)
Definition: sound.h:107
float getSoundGain(int id)
Definition: sound.h:105
Definition: sound.h:36
int playSound(const SimpleSoundSpec &spec, bool loop)
Definition: sound.h:66
virtual bool updateSoundGain(int id, float gain)=0
virtual int playSound(const std::string &name, bool loop, float volume, float fade=0.0f, float pitch=1.0f)=0
virtual bool soundExists(int sound)=0
virtual void step(float dtime)=0
virtual float getSoundGain(int id)=0
virtual ~ISoundManager()=default
virtual bool loadSoundData(const std::string &name, const std::string &filedata)=0
virtual void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up)=0
virtual void updateSoundPosition(int sound, v3f pos)=0
virtual void fadeSound(int sound, float step, float gain)=0
virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch=1.0f)=0
virtual void stopSound(int sound)=0
int playSoundAt(const SimpleSoundSpec &spec, bool loop, const v3f &pos)
Definition: sound.h:70
virtual void setListenerGain(float gain)=0
virtual bool loadSoundFile(const std::string &name, const std::string &filepath)=0
Definition: sound.h:28
virtual void fetchSounds(const std::string &name, std::set< std::string > &dst_paths, std::set< std::string > &dst_datas)=0
DummySoundManager dummySoundManager
Definition: sound.cpp:23
core::vector3df v3f
Definition: irr_v3d.h:26
Definition: sound.h:28
std::string name
Definition: sound.h:58
float fade
Definition: sound.h:60
float gain
Definition: sound.h:59
float pitch
Definition: sound.h:61