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 bool IsLoaded() const
33 {
34 return m_pData != nullptr;
35 }
36};
37
39{
40 int m_Vol;
41 int m_Pan;
42};
43
44struct CVoice
45{
48 int m_Age; // increases when reused
49 int m_Tick;
50 int m_Vol; // 0 - 255
53 float m_Falloff; // [0.0, 1.0]
54
56 union
57 {
60 };
61};
62
63class CSound : public IEngineSound
64{
65 enum
66 {
70 };
71
72 bool m_SoundEnabled = false;
73 SDL_AudioDeviceID m_Device = 0;
75
77 int m_FirstFreeSampleIndex GUARDED_BY(m_SoundLock) = 0;
78
80 CChannel m_aChannels[NUM_CHANNELS] GUARDED_BY(m_SoundLock) = {{255, 0}};
81 int m_NextVoice GUARDED_BY(m_SoundLock) = 0;
82 uint32_t m_MaxFrames = 0;
83
84 // This is not an std::atomic<vec2> as this would require linking with
85 // libatomic with clang x86 as there is no native support for this.
86 std::atomic<float> m_ListenerPositionX = 0.0f;
87 std::atomic<float> m_ListenerPositionY = 0.0f;
88 std::atomic<int> m_SoundVolume = 100;
89 int m_MixingRate = 48000;
90
91 class IEngineGraphics *m_pGraphics = nullptr;
92 IStorage *m_pStorage = nullptr;
93
94 int *m_pMixBuffer = nullptr;
95
97 void RateConvert(CSample &Sample) const;
98
99 bool DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) const;
100 bool DecodeWV(CSample &Sample, const void *pData, unsigned DataSize) const;
101
102 void UpdateVolume();
103
104public:
105 int Init() override REQUIRES(!m_SoundLock);
106 int Update() override;
107 void Shutdown() override REQUIRES(!m_SoundLock);
108
109 bool IsSoundEnabled() override { return m_SoundEnabled; }
110
111 int LoadOpus(const char *pFilename, int StorageType = IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock);
112 int LoadWV(const char *pFilename, int StorageType = IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock);
113 int LoadOpusFromMem(const void *pData, unsigned DataSize, bool ForceLoad) override REQUIRES(!m_SoundLock);
114 int LoadWVFromMem(const void *pData, unsigned DataSize, bool ForceLoad) override REQUIRES(!m_SoundLock);
115 void UnloadSample(int SampleId) override REQUIRES(!m_SoundLock);
116
117 float GetSampleTotalTime(int SampleId) override REQUIRES(!m_SoundLock); // in s
118 float GetSampleCurrentTime(int SampleId) override REQUIRES(!m_SoundLock); // in s
119 void SetSampleCurrentTime(int SampleId, float Time) override REQUIRES(!m_SoundLock);
120
121 void SetChannel(int ChannelId, float Vol, float Pan) override REQUIRES(!m_SoundLock);
122 void SetListenerPosition(vec2 Position) override;
123
124 void SetVoiceVolume(CVoiceHandle Voice, float Volume) override REQUIRES(!m_SoundLock);
125 void SetVoiceFalloff(CVoiceHandle Voice, float Falloff) override REQUIRES(!m_SoundLock);
126 void SetVoicePosition(CVoiceHandle Voice, vec2 Position) override REQUIRES(!m_SoundLock);
127 void SetVoiceTimeOffset(CVoiceHandle Voice, float TimeOffset) override REQUIRES(!m_SoundLock); // in s
128
129 void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock);
130 void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) override REQUIRES(!m_SoundLock);
131
132 CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock);
133 CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) override REQUIRES(!m_SoundLock);
134 CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume) override REQUIRES(!m_SoundLock);
135 void Pause(int SampleId) override REQUIRES(!m_SoundLock);
136 void Stop(int SampleId) override REQUIRES(!m_SoundLock);
137 void StopAll() override REQUIRES(!m_SoundLock);
138 void StopVoice(CVoiceHandle Voice) override REQUIRES(!m_SoundLock);
139 bool IsPlaying(int SampleId) override REQUIRES(!m_SoundLock);
140
141 int MixingRate() const override { return m_MixingRate; }
142 void Mix(short *pFinalOut, unsigned Frames) override REQUIRES(!m_SoundLock);
143
144 void PauseAudioDevice() override;
145 void UnpauseAudioDevice() override;
146};
147
148#endif
Definition: lock.h:88
Definition: sound.h:64
int LoadOpus(const char *pFilename, int StorageType=IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:521
void Pause(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:894
int m_FirstFreeSampleIndex GUARDED_BY(m_SoundLock)=0
void RateConvert(CSample &Sample) const
Definition: sound.cpp:300
void Shutdown() override REQUIRES(!m_SoundLock)
Definition: sound.cpp:260
void SetVoiceVolume(CVoiceHandle Voice, float Volume) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:729
bool m_SoundEnabled
Definition: sound.h:72
void SetListenerPosition(vec2 Position) override
Definition: sound.cpp:723
void StopVoice(CVoiceHandle Voice) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:950
CLock m_SoundLock
Definition: sound.h:74
CSample * AllocSample() REQUIRES(!m_SoundLock)
Definition: sound.cpp:281
float GetSampleCurrentTime(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:677
void UnloadSample(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:635
void SetChannel(int ChannelId, float Vol, float Pan) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:714
CVoiceHandle PlayAt(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:884
void SetVoicePosition(CVoiceHandle Voice, vec2 Position) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:759
@ NUM_VOICES
Definition: sound.h:68
@ NUM_CHANNELS
Definition: sound.h:69
@ NUM_SAMPLES
Definition: sound.h:67
bool IsPlaying(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:965
uint32_t m_MaxFrames
Definition: sound.h:82
bool DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize) const
Definition: sound.cpp:336
CVoiceHandle Play(int ChannelId, int SampleId, int Flags, float Volume, vec2 Position) REQUIRES(!m_SoundLock)
Definition: sound.cpp:838
int LoadOpusFromMem(const void *pData, unsigned DataSize, bool ForceLoad) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:595
class IEngineGraphics * m_pGraphics
Definition: sound.h:91
void Mix(short *pFinalOut, unsigned Frames) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:27
float GetSampleTotalTime(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:668
void PauseAudioDevice() override
Definition: sound.cpp:974
void Stop(int SampleId) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:912
void SetSampleCurrentTime(int SampleId, float Time) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:695
bool IsSoundEnabled() override
Definition: sound.h:109
int LoadWV(const char *pFilename, int StorageType=IStorage::TYPE_ALL) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:558
int m_MixingRate
Definition: sound.h:89
int LoadWVFromMem(const void *pData, unsigned DataSize, bool ForceLoad) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:615
std::atomic< float > m_ListenerPositionY
Definition: sound.h:87
std::atomic< float > m_ListenerPositionX
Definition: sound.h:86
void SetVoiceTimeOffset(CVoiceHandle Voice, float TimeOffset) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:773
void StopAll() override REQUIRES(!m_SoundLock)
Definition: sound.cpp:933
int m_NextVoice GUARDED_BY(m_SoundLock)=0
CChannel m_aChannels[NUM_CHANNELS] GUARDED_BY(m_SoundLock)
int Init() override REQUIRES(!m_SoundLock)
Definition: sound.cpp:186
bool DecodeWV(CSample &Sample, const void *pData, unsigned DataSize) const
Definition: sound.cpp:440
void UnpauseAudioDevice() override
Definition: sound.cpp:979
int MixingRate() const override
Definition: sound.h:141
SDL_AudioDeviceID m_Device
Definition: sound.h:73
int Update() override
Definition: sound.cpp:246
void SetVoiceRectangle(CVoiceHandle Voice, float Width, float Height) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:822
void UpdateVolume()
Definition: sound.cpp:252
int * m_pMixBuffer
Definition: sound.h:94
std::atomic< int > m_SoundVolume
Definition: sound.h:88
void SetVoiceCircle(CVoiceHandle Voice, float Radius) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:807
CSample m_aSamples[NUM_SAMPLES] GUARDED_BY(m_SoundLock)
IStorage * m_pStorage
Definition: sound.h:92
CVoice m_aVoices[NUM_VOICES] GUARDED_BY(m_SoundLock)
void SetVoiceFalloff(CVoiceHandle Voice, float Falloff) override REQUIRES(!m_SoundLock)
Definition: sound.cpp:744
Definition: graphics.h:487
Definition: sound.h:115
Definition: storage.h:20
@ TYPE_ALL
Definition: storage.h:26
#define REQUIRES(...)
Definition: lock.h:32
Definition: sound.h:39
int m_Pan
Definition: sound.h:41
int m_Vol
Definition: sound.h:40
Definition: sound.h:15
int m_PausedAt
Definition: sound.h:25
bool IsLoaded() const
Definition: sound.h:32
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:45
CSample * m_pSample
Definition: sound.h:46
ISound::CVoiceShapeRectangle m_Rectangle
Definition: sound.h:59
CChannel * m_pChannel
Definition: sound.h:47
int m_Vol
Definition: sound.h:50
int m_Flags
Definition: sound.h:51
int m_Age
Definition: sound.h:48
float m_Falloff
Definition: sound.h:53
ISound::CVoiceShapeCircle m_Circle
Definition: sound.h:58
int m_Tick
Definition: sound.h:49
vec2 m_Position
Definition: sound.h:52
int m_Shape
Definition: sound.h:55
Definition: sound.h:37
Definition: sound.h:42