Libprimis
Imprimis' 3D destroyable world engine
Loading...
Searching...
No Matches
sound.h
Go to the documentation of this file.
1
8
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
24class SoundEngine
25{
26 public:
27 SoundEngine();
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
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 * vol);
185
192
200 void setmusicvol(const int * musicvol);
201
208
216 void setaudiodriver(char * f);
217
223 void setsound(const int * 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 *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
330 void cleanup();
331
332 bool load(const char *dir);
333 };
334
335 struct soundslot
336 {
337 SoundSample *sample;
338 int volume;
339 };
340
341 struct SoundConfig
342 {
343 int slots, numslots;
344 int maxuses;
345
346 bool hasslot(const soundslot *p, const std::vector<soundslot> &v) const;
347
348 int chooseslot(int flags) const;
349 };
350
351 //sound channel object that is allocated to every sound emitter in use
352 //(entities, players, weapons, etc.)
353 //defined in world coordinates, and position mixing is done for the player dynamically
354 class SoundChannel
355 {
356 public:
357 bool inuse;
358 const soundslot *slot;
359 extentity *ent;
360 int volume;
361
362 SoundChannel(int id, SoundEngine& p);
363
364 bool updatechannel();
365 void syncchannel();
366 void setupchannel(const soundslot *slot, const vec *loc, extentity *ent, int radius);
367
368 void setloc(const vec& newloc);
369
370 bool hasloc() const;
371
372 void clearloc();
373 void reset();
374 private:
375 SoundEngine * const parent;
376 int id;
377 int radius, pan, flags;
378 bool dirty;
379 vec loc;
380 };
381
382 std::vector<SoundChannel> channels;
383 int maxchannels = 0;
384
385 int soundvol = 255;
386
387 int musicvol = 60;
388
389 bool shouldinitaudio = true;
390
391 #ifdef WIN32
392 #define AUDIODRIVER "directsound winmm"
393 #else
394 #define AUDIODRIVER "pulseaudio alsa arts esd jack pipewire dsp"
395 #endif
396
397 std::string audiodriver = AUDIODRIVER;
398 bool sound;
399
400 //# of sound channels (not physical output channels, but individual sound samples in use, such as weaps and light ents)
401 int soundchans = 32;
402 //max sound frequency (44.1KHz = CD)
403 int soundfreq = 44100;
404 //length of sound buffer in milliseconds
405 int soundbufferlen = 1024;
406
407 bool initaudio();
408
409 struct SoundType
410 {
411 SoundEngine * const parent;
412 std::map<std::string, SoundSample> samples;
413 std::vector<soundslot> slots;
414 std::vector<SoundConfig> configs;
415 const char *dir;
416 SoundType(const char *dir, SoundEngine& p);
417 int findsound(const char *name, int vol);
418 int addslot(const char *name, int vol);
419 int addsound(const char *name, int vol, int maxuses = 0);
420 void addalt(const char *name, int vol);
421 void clear();
422 void reset();
423 void cleanupsamples();
424 void cleanup();
425 void preloadsound(int n);
426 bool playing(const SoundChannel &chan, const SoundConfig &config) const;
427 };
428 SoundType gamesounds;
429 SoundType mapsounds;
430
431 //free all channels
432 void resetchannels();
433
434 void musicdone();
435 Mix_Music *loadmusic(const char *name);
436 Mix_Chunk *loadwav(const char *name);
437
438 //creates a new SoundChannel object with passed properties
439 SoundChannel &newchannel(int n, const soundslot *slot, const vec *loc = nullptr, extentity *ent = nullptr, int radius = 0);
440
441 //sets a channel as not being in use
442 void freechannel(int n);
443
444 void stopchannels();
445
446 void setmusicvol(int musicvol);
447
448 char *musicfile = nullptr,
449 *musicdonecmd = nullptr;
450
451 Mix_Music *music = nullptr;
452 SDL_RWops *musicrw = nullptr;
453 stream *musicstream = nullptr;
454
455 void stopmusic();
456};
457#endif
void altmapsound(char *name, int *vol)
Adds an alternative map sound.
void setmusicvol(const int *musicvol)
Sets the music sound volume.
void setsoundvol(const int *vol)
Sets the map sound volume.
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 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 setsound(const int *on)
Sets whether the sound engine is enabled.
void mapsoundreset()
Resets the loaded map sounds.
void updatesounds()
Restarts and updates all sound indices.
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
three dimensional Cartesian vector object
Definition geom.h:207