DDraceNetwork Docs
sound.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_CLIENT_SOUND_H
4#define ENGINE_CLIENT_SOUND_H
5
6#include <base/lock.h>
7
8#include <engine/sound.h>
9
10#include <SDL_audio.h>
11
12#include <atomic>
13
14struct CSample
15{
18
19 short *m_pData;
21 int m_Rate;
26
27 float TotalTime() const
28 {
29 return m_NumFrames / (float)m_Rate;
30 }
31};
32
34{
35 int m_Vol;
36 int m_Pan;
37};
38
39struct CVoice
40{
43 int m_Age; // increases when reused
44 int m_Tick;
45 int m_Vol; // 0 - 255
48 float m_Falloff; // [0.0, 1.0]
49
51 union
52 {
55 };
56};
57
58class CSound : public IEngineSound
59{
60 enum
61 {
65 };
66
67 bool m_SoundEnabled = false;
68 SDL_AudioDeviceID m_Device = 0;
70
73
76 int m_NextVoice = 0;
77 uint32_t m_MaxFrames = 0;
78
79 // This is not an std::atomic<vec2> as this would require linking with
80 // libatomic with clang x86 as there is no native support for this.
81 std::atomic<float> m_ListenerPositionX = 0.0f;
82 std::atomic<float> m_ListenerPositionY = 0.0f;
83 std::atomic<int> m_SoundVolume = 100;
84 int m_MixingRate = 48000;
85
86 class IEngineGraphics *m_pGraphics = nullptr;
87 IStorage *m_pStorage = nullptr;
88
89 int *m_pMixBuffer = nullptr;
90
92 void RateConvert(CSample &Sample) const;
93
94 bool DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) const;
95 bool DecodeWV(CSample &Sample, const void *pData, unsigned DataSize) const;
96
97 void UpdateVolume();
98
99public:
100 int Init() override;
101 int Update() override;
102 void Shutdown() override REQUIRES(!m_SoundLock);
103
104 bool IsSoundEnabled() override { return m_SoundEnabled; }
105
106 int LoadOpus(const char *pFilename, int StorageType = IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock);
107 int LoadWV(const char *pFilename, int StorageType = IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock);
108 int LoadOpusFromMem(const void *pData, unsigned DataSize, bool FromEditor) override REQUIRES(!m_SoundLock);
109 int LoadWVFromMem(const void *pData, unsigned DataSize, bool FromEditor) override REQUIRES(!m_SoundLock);
110 void UnloadSample(int SampleId) override REQUIRES(!m_SoundLock);
111
112 float GetSampleTotalTime(int SampleId) override; // in s
113 float GetSampleCurrentTime(int SampleId) override REQUIRES(!m_SoundLock); // in s
114 void SetSampleCurrentTime(int SampleId, float Time) override REQUIRES(!m_SoundLock);
115
116 void SetChannel(int ChannelId, float Vol, float Pan) override;
117 void SetListenerPosition(vec2 Position) override;
118
119 void SetVoiceVolume(CVoiceHandle Voice, float Volume) override REQUIRES(!m_SoundLock);
120 void SetVoiceFalloff(CVoiceHandle Voice, float Falloff) override REQUIRES(!m_SoundLock);
121 void SetVoicePosition(CVoiceHandle Voice, vec2 Position) override REQUIRES(!m_SoundLock);
122 void SetVoiceTimeOffset(CVoiceHandle Voice, float TimeOffset) override REQUIRES(!m_SoundLock); // in s
123
124 void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock);
125 void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) override REQUIRES(!m_SoundLock);
126
127 CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock);
128 CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) override REQUIRES(!m_SoundLock);
129 CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume) override REQUIRES(!m_SoundLock);
130 void Pause(int SampleId) override REQUIRES(!m_SoundLock);
131 void Stop(int SampleId) override REQUIRES(!m_SoundLock);
132 void StopAll() override REQUIRES(!m_SoundLock);
133 void StopVoice(CVoiceHandle Voice) override REQUIRES(!m_SoundLock);
134 bool IsPlaying(int SampleId) override REQUIRES(!m_SoundLock);
135
136 int MixingRate() const override { return m_MixingRate; }
137 void Mix(short *pFinalOut, unsigned Frames) override REQUIRES(!m_SoundLock);
138
139 void PauseAudioDevice() override;
140 void UnpauseAudioDevice() override;
141};
142
143#endif
Definition: lock.h:88
Definition: sound.h:59
int LoadOpus(const char *pFilename, int StorageType=IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:511
void Pause(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:881
void RateConvert(CSample &Sample) const
Definition: sound.cpp:290
float GetSampleTotalTime(int SampleId) override
Definition: sound.cpp:657
void Shutdown() override REQUIRES(!m_SoundLock)
Definition: sound.cpp:259
void SetVoiceVolume(CVoiceHandle Voice, float Volume) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:714
int LoadWVFromMem(const void *pData, unsigned DataSize, bool FromEditor) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:614
bool m_SoundEnabled
Definition: sound.h:67
void SetListenerPosition(vec2 Position) override
Definition: sound.cpp:708
void StopVoice(CVoiceHandle Voice) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:931
CLock m_SoundLock
Definition: sound.h:69
float GetSampleCurrentTime(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:665
void SetChannel(int ChannelId, float Vol, float Pan) override
Definition: sound.cpp:702
int Init() override
Definition: sound.cpp:186
void UnloadSample(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:637
CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:871
void SetVoicePosition(CVoiceHandle Voice, vec2 Position) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:744
bool IsPlaying(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:946
uint32_t m_MaxFrames
Definition: sound.h:77
CVoice m_aVoices[NUM_VOICES]
Definition: sound.h:74
bool DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) const
Definition: sound.cpp:326
CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock)
Definition: sound.cpp:823
class IEngineGraphics * m_pGraphics
Definition: sound.h:86
void Mix(short *pFinalOut, unsigned Frames) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:27
void PauseAudioDevice() override
Definition: sound.cpp:953
void Stop(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:896
CChannel m_aChannels[NUM_CHANNELS]
Definition: sound.h:75
void SetSampleCurrentTime(int SampleId, float Time) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:683
CSample m_aSamples[NUM_SAMPLES]
Definition: sound.h:71
bool IsSoundEnabled() override
Definition: sound.h:104
int LoadWV(const char *pFilename, int StorageType=IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:551
int m_MixingRate
Definition: sound.h:84
std::atomic< float > m_ListenerPositionY
Definition: sound.h:82
std::atomic< float > m_ListenerPositionX
Definition: sound.h:81
int LoadOpusFromMem(const void *pData, unsigned DataSize, bool FromEditor) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:591
void SetVoiceTimeOffset(CVoiceHandle Voice, float TimeOffset) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:758
void StopAll() override REQUIRES(!m_SoundLock)
Definition: sound.cpp:914
CSample * AllocSample()
Definition: sound.cpp:272
int m_FirstFreeSampleIndex
Definition: sound.h:72
int m_NextVoice
Definition: sound.h:76
bool DecodeWV(CSample &Sample, const void *pData, unsigned DataSize) const
Definition: sound.cpp:430
void UnpauseAudioDevice() override
Definition: sound.cpp:958
int MixingRate() const override
Definition: sound.h:136
SDL_AudioDeviceID m_Device
Definition: sound.h:68
@ NUM_VOICES
Definition: sound.h:63
@ NUM_CHANNELS
Definition: sound.h:64
@ NUM_SAMPLES
Definition: sound.h:62
int Update() override
Definition: sound.cpp:245
void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:807
void UpdateVolume()
Definition: sound.cpp:251
int * m_pMixBuffer
Definition: sound.h:89
std::atomic< int > m_SoundVolume
Definition: sound.h:83
void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:792
IStorage * m_pStorage
Definition: sound.h:87
void SetVoiceFalloff(CVoiceHandle Voice, float Falloff) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:729
Definition: graphics.h:501
Definition: sound.h:115
Definition: storage.h:20
@ TYPE_ALL
Definition: storage.h:26
#define REQUIRES(...)
Definition: lock.h:32
Definition: sound.h:34
int m_Pan
Definition: sound.h:36
int m_Vol
Definition: sound.h:35
Definition: sound.h:15
int m_PausedAt
Definition: sound.h:25
int m_Index
Definition: sound.h:16
int m_NumFrames
Definition: sound.h:20
int m_Channels
Definition: sound.h:22
int m_Rate
Definition: sound.h:21
short * m_pData
Definition: sound.h:19
float TotalTime() const
Definition: sound.h:27
int m_NextFreeSampleIndex
Definition: sound.h:17
int m_LoopStart
Definition: sound.h:23
int m_LoopEnd
Definition: sound.h:24
Definition: sound.h:40
CSample * m_pSample
Definition: sound.h:41
ISound::CVoiceShapeRectangle m_Rectangle
Definition: sound.h:54
CChannel * m_pChannel
Definition: sound.h:42
int m_Vol
Definition: sound.h:45
int m_Flags
Definition: sound.h:46
int m_Age
Definition: sound.h:43
float m_Falloff
Definition: sound.h:48
ISound::CVoiceShapeCircle m_Circle
Definition: sound.h:53
int m_Tick
Definition: sound.h:44
vec2 m_Position
Definition: sound.h:47
int m_Shape
Definition: sound.h:50
Definition: sound.h:37
Definition: sound.h:42