Pioneer
Sound.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef __OGGMIX_H
5 #define __OGGMIX_H
6 
7 #include <SDL.h>
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 class Body;
13 
14 namespace Sound {
15 
16  enum {
17  OP_REPEAT = (1 << 0),
18  OP_STOP_AT_TARGET_VOLUME = (1 << 1)
19  };
20  typedef Uint32 Op;
21 
22  struct Sample {
23  Uint16 *buf;
24  Uint32 buf_len;
25  Uint32 channels;
26  int upsample; // 1 = 44100, 2=22050
27  /* if buf is null, this will be path to an ogg we must stream */
28  std::string path;
29  bool isMusic;
30  };
31 
32  class Event {
33  public:
34  Event() :
35  eid(0) {}
36  Event(Uint32 id) :
37  eid(id) {}
38  virtual void Play(const char *fx, const float volume_left, const float volume_right, Op op);
39  void Play(const char *fx) { Play(fx, 1.0f, 1.0f, 0); }
40  bool Stop();
41  bool IsPlaying() const;
42  Uint32 EventId() { return eid; }
43  bool SetOp(Op op);
44  bool VolumeAnimate(const float targetVol1, const float targetVol2, const float dv_dt1, const float dv_dt2);
45  bool VolumeAnimate(const float targetVols[2], const float dv_dt[2])
46  {
47  return VolumeAnimate(targetVols[0], targetVols[1],
48  dv_dt[0], dv_dt[1]);
49  }
50  bool SetVolume(const float vol_left, const float vol_right);
51  bool SetVolume(const float vol)
52  {
53  return SetVolume(vol, vol);
54  }
55 
56  protected:
57  Uint32 eid;
58  };
59  typedef Uint32 eventid;
60 
61  bool Init(bool automaticallyOpenDevice = true);
62  bool InitDevice(std::string &name);
63  void Uninit();
64  std::vector<std::string> &GetAudioDevices();
65  void UpdateAudioDevices();
69  void DestroyAllEvents();
71  void Pause(int on);
72  eventid PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op);
73  eventid PlayMusic(const char *fx, const float volume_left, const float volume_right, const Op op);
74  inline static eventid PlaySfx(const char *fx) { return PlaySfx(fx, 1.0f, 1.0f, 0); }
75  void CalculateStereo(const Body *b, float vol, float *volLeftOut, float *volRightOut);
76  eventid BodyMakeNoise(const Body *b, const char *fx, float vol);
77  void SetMasterVolume(const float vol);
78  float GetMasterVolume();
79  void SetSfxVolume(const float vol);
80  float GetSfxVolume();
81  const std::map<std::string, Sample> &GetSamples();
82 
83 } /* namespace Sound */
84 
85 #endif /* __OGGMIX_H */
Definition: Body.h:57
Definition: Sound.h:32
Event(Uint32 id)
Definition: Sound.h:36
Event()
Definition: Sound.h:34
bool SetVolume(const float vol_left, const float vol_right)
Definition: Sound.cpp:746
bool SetOp(Op op)
Definition: Sound.cpp:718
void Play(const char *fx)
Definition: Sound.h:39
bool VolumeAnimate(const float targetVol1, const float targetVol2, const float dv_dt1, const float dv_dt2)
Definition: Sound.cpp:732
bool Stop()
Definition: Sound.cpp:695
Uint32 EventId()
Definition: Sound.h:42
bool IsPlaying() const
Definition: Sound.cpp:710
bool VolumeAnimate(const float targetVols[2], const float dv_dt[2])
Definition: Sound.h:45
Uint32 eid
Definition: Sound.h:57
bool SetVolume(const float vol)
Definition: Sound.h:51
virtual void Play(const char *fx, const float volume_left, const float volume_right, Op op)
Definition: Sound.cpp:689
Definition: Pi.h:54
eventid BodyMakeNoise(const Body *b, const char *sfx, float vol)
Definition: Sound.cpp:174
void CalculateStereo(const Body *b, float vol, float *volLeftOut, float *volRightOut)
Definition: Sound.cpp:151
eventid PlaySfx(const char *fx, const float volume_left, const float volume_right, const Op op)
Definition: Sound.cpp:247
void SetSfxVolume(const float vol)
Definition: Sound.cpp:141
void UpdateAudioDevices()
Definition: Sound.cpp:674
bool InitDevice(std::string &name)
Definition: Sound.cpp:634
void SetMasterVolume(const float vol)
Definition: Sound.cpp:131
Uint32 Op
Definition: Sound.h:20
@ OP_STOP_AT_TARGET_VOLUME
Definition: Sound.h:18
@ OP_REPEAT
Definition: Sound.h:17
bool Init(bool automaticallyOpenDevice)
Definition: Sound.cpp:586
float GetSfxVolume()
Definition: Sound.cpp:146
const std::map< std::string, Sample > & GetSamples()
Definition: Sound.cpp:764
void Pause(int on)
Definition: Sound.cpp:684
std::vector< std::string > & GetAudioDevices()
void DestroyAllEvents()
Definition: Sound.cpp:464
void DestroyAllEventsExceptMusic()
Definition: Sound.cpp:474
Uint32 eventid
Definition: Sound.h:59
eventid PlayMusic(const char *fx, const float volume_left, const float volume_right, const Op op)
Definition: Sound.cpp:285
void Uninit()
Definition: Sound.cpp:661
float GetMasterVolume()
Definition: Sound.cpp:136
Definition: Sound.h:22
Uint32 buf_len
Definition: Sound.h:24
bool isMusic
Definition: Sound.h:29
Uint16 * buf
Definition: Sound.h:23
Uint32 channels
Definition: Sound.h:25
std::string path
Definition: Sound.h:28
int upsample
Definition: Sound.h:26