Libprimis
Imprimis' 3D destroyable world engine
Loading...
Searching...
No Matches
sound.h
Go to the documentation of this file.
1
9#ifndef SOUND_H_
10#define SOUND_H_
11
12//incomplete types from SDL
13struct musicrw;
14struct _Mix_Music;
15typedef struct _Mix_Music Mix_Music;
16struct Mix_Chunk;
17
25{
26 public:
28
36
44 void preloadsound(int n);
45
52
66 int playsound(int n, const vec *loc = nullptr, extentity *ent = nullptr, int flags = 0, int loops = 0, int fade = 0, int chanid = -1, int radius = 0, int expire = -1);
67
75 bool stopsound(int n, int chanid, int fade);
76
82 void stopsounds();
83
90 void updatesounds();
91
105 int playsoundname(const char *s, const vec *loc, int vol, int flags, int loops, int fade, int chanid, int radius, int expire);
106
112 void initsound();
113
120 void startmusic(char *name, char *cmd);
121
126
132 void setstereo(const int * const on);
133
142
150 void setmaxradius(const int * const dist);
151
160
168 void setsoundchans(const int * const val);
169
176
184 void setsoundvol(const int * const vol);
185
192
200 void setmusicvol(const int * const musicvol);
201
208
216 void setaudiodriver(char * f);
217
223 void setsound(const int * const on);
224
230 int getsound();
231
238 void registersound(char *name, int *vol);
239
247 void mapsound(char *name, int *vol, int *maxuses);
248
255 void altsound(char *name, int *vol);
256
263 void altmapsound(char *name, int *vol);
264
270 void numsounds();
271
278
286
294
295 private:
296 void stopmapsound(extentity *e);
297
298 bool stereo = true;
299
300 int maxsoundradius = 340;
301
302 //free channels that are not playing sounds
303 void reclaimchannels();
304 void syncchannels();
305 bool minimizedsounds = false;
306 int getminimizedsounds();
307 void setminimizedsounds(int minimize);
308
309 void stopmapsounds();
310 //check map entities to see what sounds need to be played because of them
311 void checkmapsounds();
312
313 //number of sounds before the game will refuse to play another sound (with `playsound()`);
314 //set to 0 to disable checking (0 does not set no sounds to be playable)
315 int maxsoundsatonce = 7;
316 int getmaxsoundsatonce();
317 void setmaxsoundsatonce(const int * const num);
318 int debugsound = 0;
319
320 bool nosound = true;
321
322 struct SoundSample
323 {
324 SoundEngine * const parent;
325 std::string name;
326 Mix_Chunk *chunk;
327
328 SoundSample(SoundEngine& p);
329 ~SoundSample();
330
331 void cleanup();
332
333 bool load(const char *dir);
334 };
335
336 struct soundslot
337 {
338 SoundSample *sample;
339 int volume;
340 };
341
342 struct SoundConfig
343 {
344 int slots, numslots;
345 int maxuses;
346
347 bool hasslot(const soundslot *p, const std::vector<soundslot> &v) const;
348
349 int chooseslot(int flags) const;
350 };
351
352 //sound channel object that is allocated to every sound emitter in use
353 //(entities, players, weapons, etc.)
354 //defined in world coordinates, and position mixing is done for the player dynamically
355 class SoundChannel
356 {
357 public:
358 bool inuse;
359 soundslot *slot;
360 extentity *ent;
361 int volume;
362
363 SoundChannel(int id, SoundEngine& p);
364
365 bool updatechannel();
366 void syncchannel();
367 void setupchannel(int n, soundslot *slot, const vec *loc, extentity *ent, int flags, int radius);
368
369 void setloc(const vec& newloc);
370
371 bool hasloc() const;
372
373 void clearloc();
374 void reset();
375 private:
376 SoundEngine * const parent;
377 int id;
378 int radius, pan, flags;
379 bool dirty;
380 vec loc;
381 };
382
383 std::vector<SoundChannel> channels;
384 int maxchannels = 0;
385
386 int soundvol = 255;
387
388 int musicvol = 60;
389
390 bool shouldinitaudio = true;
391
392 #ifdef WIN32
393 #define AUDIODRIVER "directsound winmm"
394 #else
395 #define AUDIODRIVER "pulseaudio alsa arts esd jack pipewire dsp"
396 #endif
397
398 std::string audiodriver = AUDIODRIVER;
399 bool sound;
400
401 //# of sound channels (not physical output channels, but individual sound samples in use, such as weaps and light ents)
402 int soundchans = 32;
403 //max sound frequency (44.1KHz = CD)
404 int soundfreq = 44100;
405 //length of sound buffer in milliseconds
406 int soundbufferlen = 1024;
407
408 bool initaudio();
409
410 struct SoundType
411 {
412 SoundEngine * const parent;
413 std::map<std::string, SoundSample> samples;
414 std::vector<soundslot> slots;
415 std::vector<SoundConfig> configs;
416 const char *dir;
417 SoundType(const char *dir, SoundEngine& p);
418 int findsound(const char *name, int vol);
419 int addslot(const char *name, int vol);
420 int addsound(const char *name, int vol, int maxuses = 0);
421 void addalt(const char *name, int vol);
422 void clear();
423 void reset();
424 void cleanupsamples();
425 void cleanup();
426 void preloadsound(int n);
427 bool playing(const SoundChannel &chan, const SoundConfig &config) const;
428 };
429 SoundType gamesounds;
430 SoundType mapsounds;
431
432 //free all channels
433 void resetchannels();
434
435 void musicdone();
436 Mix_Music *loadmusic(const char *name);
437 Mix_Chunk *loadwav(const char *name);
438
439 //creates a new SoundChannel object with passed properties
440 SoundChannel &newchannel(int n, soundslot *slot, const vec *loc = nullptr, extentity *ent = nullptr, int flags = 0, int radius = 0);
441
442 //sets a channel as not being in use
443 void freechannel(int n);
444
445 void stopchannels();
446
447 void setmusicvol(int musicvol);
448
449 char *musicfile = nullptr,
450 *musicdonecmd = nullptr;
451
452 Mix_Music *music = nullptr;
453 SDL_RWops *musicrw = nullptr;
454 stream *musicstream = nullptr;
455
456 void stopmusic();
457};
458#endif
The wrapper around SDL_Mixer that plays sounds ingame.
Definition sound.h:25
void altmapsound(char *name, int *vol)
Adds an alternative map sound.
void clear_sound()
Shuts down sound system.
void mapsound(char *name, int *vol, int *maxuses)
Adds a sound to the list of map sounds.
void setmaxradius(const int *const dist)
Sets the maximum radius to play sounds from.
void preloadsound(int n)
Loads the approriate sound file at the given index.
void soundreset()
Resets the loaded game sounds.
int playsound(int n, const vec *loc=nullptr, extentity *ent=nullptr, int flags=0, int loops=0, int fade=0, int chanid=-1, int radius=0, int expire=-1)
Plays a sound with the given attributes.
void preloadmapsounds()
Loads the sounds for every element in the map sounds vector.
int getsoundchans()
Returns the number of sound channels in use.
void nummapsounds()
Returns to the console the number of map sounds loaded.
void setsoundvol(const int *const vol)
Sets the map sound volume.
void setaudiodriver(char *f)
Sends a list of audio subsystems for SDL to bind to.
int getsoundvol()
Returns the current sound volume.
void numsounds()
Returns to the console the number of game sounds loaded.
void startmusic(char *name, char *cmd)
Starts a nonpositional music track.
int getmusicvol()
Returns the current music volume.
int getsound()
Returns whether sound is on.
int playsoundname(const char *s, const vec *loc, int vol, int flags, int loops, int fade, int chanid, int radius, int expire)
Plays a sound with the given attributes.
void setsoundchans(const int *const val)
Sets the number of sound channels to use.
void stopsounds()
Immediately kills all active sounds.
void initsound()
Starts SDL_Mixer and initializes startup sound channels.
void setstereo(const int *const on)
Sets whether stereo audio is enabled.
void mapsoundreset()
Resets the loaded map sounds.
void setsound(const int *const on)
Sets whether the sound engine is enabled.
void updatesounds()
Restarts and updates all sound indices.
void setmusicvol(const int *const musicvol)
Sets the music sound volume.
int getstereo()
Returns whether stereo or mono is enabled.
void altsound(char *name, int *vol)
Adds an alternative game sound.
void registersound(char *name, int *vol)
Adds a sound to the sound list at a given volume.
bool stopsound(int n, int chanid, int fade)
Stops playing a sound of index n in the specificied channel.
void resetsound()
Resets the sound subsystem.
int getmaxradius()
Returns the maximum sound play radius.
Extended entity data not of the subset which is saved to disk.
Definition ents.h:93
Definition tools.h:466
three dimensional Cartesian vector object
Definition geom.h:185