SuperTuxKart
sfx_manager.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2008-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_SFX_MANAGER_HPP
20 #define HEADER_SFX_MANAGER_HPP
21 
22 #include "utils/can_be_deleted.hpp"
23 #include "utils/leak_check.hpp"
24 #include "utils/no_copy.hpp"
25 #include "utils/synchronised.hpp"
26 #include "utils/vec3.hpp"
27 
28 #include <condition_variable>
29 #include <map>
30 #include <string>
31 #include <thread>
32 
33 #include <vector>
34 
35 #ifdef ENABLE_SOUND
36 # include <AL/al.h>
37 # include <AL/alc.h>
38 #else
39  typedef unsigned int ALuint;
40 #endif
41 
42 class MusicInformation;
43 class SFXBase;
44 class SFXBuffer;
45 class XMLNode;
46 
54 class SFXManager : public NoCopy, public CanBeDeleted
55 {
56 private:
59 
60 public:
61 
65  {
66  SFX_PLAY = 1,
67  SFX_PLAY_POSITION,
68  SFX_STOP,
69  SFX_PAUSE,
70  SFX_PAUSE_ALL,
71  SFX_RESUME,
72  SFX_RESUME_ALL,
73  SFX_DELETE,
74  SFX_SPEED,
75  SFX_POSITION,
76  SFX_SPEED_POSITION,
77  SFX_VOLUME,
78  SFX_MASTER_VOLUME,
79  SFX_LOOP,
80  SFX_LISTENER,
81  SFX_UPDATE,
82  SFX_MUSIC_START,
83  SFX_MUSIC_STOP,
84  SFX_MUSIC_PAUSE,
85  SFX_MUSIC_RESUME,
86  SFX_MUSIC_SWITCH_FAST,
87  SFX_MUSIC_SET_TMP_VOLUME,
88  SFX_MUSIC_WAITING,
89  SFX_MUSIC_DEFAULT_VOLUME,
90  SFX_EXIT,
91  SFX_CREATE_SOURCE
92  }; // SFXCommands
93 
98  enum CustomSFX : int
99  {
110  NUM_CUSTOMS
111  };
112 
113 private:
114 
117  class SFXCommand : public NoCopy
118  {
119  private:
120  LEAK_CHECK()
121  public:
124 
127 
130 
136  // --------------------------------------------------------------------
137  SFXCommand(SFXCommands command, SFXBase *base)
138  {
139  m_command = command;
140  m_sfx = base;
141  } // SFXCommand()
142  // --------------------------------------------------------------------
145  {
146  m_command = command;
147  m_music_information = mi;
148  } // SFXCommnd(MusicInformation*)
149  // --------------------------------------------------------------------
152  SFXCommand(SFXCommands command, MusicInformation *mi, float f)
153  {
154  m_command = command;
155  m_parameter.setX(f);
156  m_music_information = mi;
157  } // SFXCommnd(MusicInformation *, float)
158  // --------------------------------------------------------------------
159  SFXCommand(SFXCommands command, SFXBase *base, float parameter)
160  {
161  m_command = command;
162  m_sfx = base;
163  m_parameter.setX(parameter);
164  } // SFXCommand(float)
165  // --------------------------------------------------------------------
166  SFXCommand(SFXCommands command, SFXBase *base, const Vec3 &parameter)
167  {
168  m_command = command;
169  m_sfx = base;
170  m_parameter = parameter;
171  } // SFXCommand(Vec3)
172  // --------------------------------------------------------------------
176  SFXCommand(SFXCommands command, SFXBase *base, float f,
177  const Vec3 &parameter)
178  {
179  m_command = command;
180  m_sfx = base;
181  m_parameter = parameter;
182  m_parameter.setW(f);
183  } // SFXCommand(Vec3)
184  }; // SFXCommand
185  // ========================================================================
186 
190 
193 
196 
197 
200  std::map<std::string, SFXBuffer*> m_all_sfx_types;
201 
204 
207 
211 
214 
217 
218 #ifndef __SWITCH__
219 
220  std::thread m_thread;
221 #endif
222 
223  uint64_t m_last_update_time;
224 
226  std::condition_variable m_condition_variable;
227 
228  void loadSfx();
229  SFXManager();
230  virtual ~SFXManager();
231 
232  static void mainLoop(void *obj);
233  void deleteSFX(SFXBase *sfx);
234  void queueCommand(SFXCommand *command);
236 
237 public:
238  static void create();
239  static void destroy();
240  void queue(SFXCommands command, SFXBase *sfx=NULL);
241  void queue(SFXCommands command, SFXBase *sfx, float f);
242  void queue(SFXCommands command, SFXBase *sfx, const Vec3 &p);
243  void queue(SFXCommands command, SFXBase *sfx, float f, const Vec3 &p);
244  void queue(SFXCommands command, MusicInformation *mi);
245  void queue(SFXCommands command, MusicInformation *mi, float f);
246  void queue(SFXCommands command, SFXBase *sfx, const Vec3 &p, SFXBuffer* buffer);
247 
248  // ------------------------------------------------------------------------
250  static SFXManager *get()
251  {
252  return m_sfx_manager;
253  } // get
254 
255  // ------------------------------------------------------------------------
256  void stopThread();
257  bool sfxAllowed();
258  SFXBuffer* loadSingleSfx(const XMLNode* node,
259  const std::string &path=std::string(""),
260  const bool load = true);
261  SFXBuffer* addSingleSfx(const std::string &sfx_name,
262  const std::string &filename,
263  bool positional,
264  float rolloff,
265  float max_dist,
266  float gain,
267  const bool load = true);
268 
270  const bool add_to_SFX_list=true,
271  const bool owns_buffer=false);
272  SFXBase* createSoundSource(const std::string &name,
273  const bool addToSFXList=true);
274 
275  void deleteSFXMapping(const std::string &name);
276  void pauseAll();
277  void reallyPauseAllNow();
278  void resumeAll();
279  void reallyResumeAllNow();
280  void update();
281  void reallyUpdateNow(SFXCommand *current);
282  bool soundExist(const std::string &name);
283  void setMasterSFXVolume(float gain);
284  float getMasterSFXVolume() const { return m_master_gain; }
285 
286  static bool checkError(const std::string &context);
287  static const std::string getErrorString(int err);
288 
289  void positionListener(const Vec3 &position,
290  const Vec3 &front, const Vec3 &up);
291  SFXBase* quickSound(const std::string &soundName);
292 
294  void toggleSound(const bool newValue);
295 
296  // ------------------------------------------------------------------------
299  void dump();
300 
301  // ------------------------------------------------------------------------
303  Vec3 getListenerPos() const { return m_listener_position.getData(); }
304 
305  // ------------------------------------------------------------------------
306 
307  SFXBuffer* getBuffer(const std::string &name);
308 };
309 
310 #endif // HEADER_SFX_MANAGER_HPP
311 
bool sfxAllowed()
Returns if sfx can be played.
Definition: sfx_manager.cpp:544
MusicInformation * m_music_information
Stores music information for music commands.
Definition: sfx_manager.hpp:129
void toggleSound(const bool newValue)
Called when sound was muted/unmuted.
Definition: sfx_manager.cpp:502
std::map< std::string, SFXBuffer * > m_all_sfx_types
The buffers and info for all sound effects.
Definition: sfx_manager.hpp:200
Data structure for the queue, which stores a sfx and the command to execute for it.
Definition: sfx_manager.hpp:117
Wrapper around an instance of the Music interface Adds information like composer, song title...
Definition: music_information.hpp:42
void queue(SFXCommands command, SFXBase *sfx=NULL)
Adds a sound effect command to the queue of the sfx manager.
Definition: sfx_manager.cpp:166
SFXBuffer * addSingleSfx(const std::string &sfx_name, const std::string &filename, bool positional, float rolloff, float max_dist, float gain, const bool load=true)
Introduces a mechanism by which one can load sound effects beyond the basic enumerated types...
Definition: sfx_manager.cpp:618
Synchronised< std::map< std::string, SFXBase * > > m_quick_sounds
To play non-positional sounds without having to create a new object for each.
Definition: sfx_manager.hpp:210
A wrapper around bullets btVector3 to include conventient conversion functions (e.g.
Definition: vec3.hpp:34
Vec3 getListenerPos() const
Returns the current position of the listener.
Definition: sfx_manager.hpp:303
static bool checkError(const std::string &context)
Returns whether or not an openal error has occurred.
Definition: sfx_manager.cpp:962
void setMasterSFXVolume(float gain)
Sets the master volume for all sound effects.
Definition: sfx_manager.cpp:985
Introduction (e.g. "I&#39;m Tux!")
Definition: sfx_manager.hpp:107
bool soundExist(const std::string &name)
Returns true if a sfx with the given name exists.
Definition: sfx_manager.cpp:776
std::thread m_thread
Thread id of the thread running in this object.
Definition: sfx_manager.hpp:220
SFXCommands m_command
The command to execute.
Definition: sfx_manager.hpp:132
SFXCommand(SFXCommands command, MusicInformation *mi)
Constructor for music information commands.
Definition: sfx_manager.hpp:144
void pauseAll()
Pauses all looping SFXs.
Definition: sfx_manager.cpp:906
void reallyPositionListenerNow()
Sets the position and orientation of the listener.
Definition: sfx_manager.cpp:1060
SFXManager()
Initialises the SFX manager and loads the sfx from a config file.
Definition: sfx_manager.cpp:77
The buffer (data) for one kind of sound effects.
Definition: sfx_buffer.hpp:43
Synchronised< std::vector< SFXCommand * > > m_sfx_commands
The list of sound effects to be played in the next update.
Definition: sfx_manager.hpp:206
SFXCommand(SFXCommands command, SFXBase *base, float f, const Vec3 &parameter)
Store a float and vec3 parameter.
Definition: sfx_manager.hpp:176
SFXBuffer * loadSingleSfx(const XMLNode *node, const std::string &path=std::string(""), const bool load=true)
Loads a single sfx from the XML specification.
Definition: sfx_manager.cpp:651
SFXCommand(SFXCommands command, MusicInformation *mi, float f)
Constructor for music information commands that take a floating point parameter (which is stored in t...
Definition: sfx_manager.hpp:152
Played when something is attached to kart (Uh-Oh)
Definition: sfx_manager.hpp:108
void queueCommand(SFXCommand *command)
Enqueues a command to the sfx queue threadsafe.
Definition: sfx_manager.cpp:286
Played when driving through goo.
Definition: sfx_manager.hpp:104
void update()
Make sure that the sfx thread is started at least once per frame.
Definition: sfx_manager.cpp:808
void positionListener(const Vec3 &position, const Vec3 &front, const Vec3 &up)
Sets the position and orientation of the listener.
Definition: sfx_manager.cpp:1044
float m_master_gain
Master gain value, taken from the user config value.
Definition: sfx_manager.hpp:216
Vec3 m_listener_front
The direction the listener is facing.
Definition: sfx_manager.hpp:192
CustomSFX
Entries for custom SFX sounds.
Definition: sfx_manager.hpp:98
Played when passing another kart.
Definition: sfx_manager.hpp:105
SFXBase * quickSound(const std::string &soundName)
Positional sound is cool, but creating a new object just to play a simple menu sound is not...
Definition: sfx_manager.cpp:1095
SFXBase * createSoundSource(SFXBuffer *info, const bool add_to_SFX_list=true, const bool owns_buffer=false)
Creates a new SFX object.
Definition: sfx_manager.cpp:698
Manager of all sound effects.
Definition: sfx_manager.hpp:54
std::condition_variable m_condition_variable
A conditional variable to wake up the main loop.
Definition: sfx_manager.hpp:226
static SFXManager * m_sfx_manager
Singleton pointer.
Definition: sfx_manager.hpp:58
Played when kart hits zipper.
Definition: sfx_manager.hpp:106
TYPE & getData()
Returns a reference to the original data file.
Definition: synchronised.hpp:82
void deleteSFX(SFXBase *sfx)
Delete a sound effect object, and removes it from the internal list of all SFXs.
Definition: sfx_manager.cpp:876
bool m_initialized
If the sfx manager has been initialised.
Definition: sfx_manager.hpp:213
SFXBuffer * m_buffer
The sound buffer to play (null = no change)
Definition: sfx_manager.hpp:126
void reallyUpdateNow(SFXCommand *current)
Updates the status of all playing sfx (to test if they are finished).
Definition: sfx_manager.cpp:829
static void mainLoop(void *obj)
This loops runs in a different threads, and starts sfx to be played.
Definition: sfx_manager.cpp:344
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:25
Vec3 m_listener_up
Up vector of the listener.
Definition: sfx_manager.hpp:195
static void destroy()
Static function to delete the singleton sfx manager.
Definition: sfx_manager.cpp:68
Synchronised< Vec3 > m_listener_position
The position of the listener.
Definition: sfx_manager.hpp:189
void reallyPauseAllNow()
Pauses all looping SFXs.
Definition: sfx_manager.cpp:917
static void create()
Static function to create the singleton sfx manager.
Definition: sfx_manager.cpp:59
void loadSfx()
Loads all sounds specified in the sound config file.
Definition: sfx_manager.cpp:557
void resumeAll()
Resumes all paused SFXs.
Definition: sfx_manager.cpp:931
Synchronised< std::vector< SFXBase * > > m_all_sfx
The actual instances (sound sources)
Definition: sfx_manager.hpp:203
A simple class that a adds a function to wait with a timeout for a class to be ready to be deleted...
Definition: can_be_deleted.hpp:37
utility class used to parse XML files
Definition: xml_node.hpp:47
Replaces default horn.
Definition: sfx_manager.hpp:100
virtual ~SFXManager()
Destructor, frees all sound effects.
Definition: sfx_manager.cpp:111
Played when struck by bowling ball or dynamite.
Definition: sfx_manager.hpp:103
void stopThread()
Puts a NULL request into the queue, which will trigger the thread to exit.
Definition: sfx_manager.cpp:322
The base class for sound effects.
Definition: sfx_base.hpp:42
void dump()
Prints the list of currently loaded sounds to stdout.
void deleteSFXMapping(const std::string &name)
This function removes a sfx buffer info entry from the mapping, and frees the openal buffer...
Definition: sfx_manager.cpp:786
void reallyResumeAllNow()
Resumes all paused SFXs.
Definition: sfx_manager.cpp:941
Played when weapon is used.
Definition: sfx_manager.hpp:109
Played when colliding with another kart.
Definition: sfx_manager.hpp:101
SFXBase * m_sfx
The sound effect for which the command should be executed.
Definition: sfx_manager.hpp:123
SFXCommands
The various commands to be executed by the sfx manager thread for each sfx.
Definition: sfx_manager.hpp:64
Played when racer wins.
Definition: sfx_manager.hpp:102
Vec3 m_parameter
Optional parameter for commands that need more input.
Definition: sfx_manager.hpp:135