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{
16 int m_Index;
17 int m_NextFreeSampleIndex;
18
19 short *m_pData;
20 int m_NumFrames;
21 int m_Rate;
22 int m_Channels;
23 int m_LoopStart;
24 int m_PausedAt;
25
26 float TotalTime() const
27 {
28 return m_NumFrames / (float)m_Rate;
29 }
30
31 bool IsLoaded() const
32 {
33 return m_pData != nullptr;
34 }
35};
36
37struct CChannel
38{
39 int m_Vol;
40 int m_Pan;
41};
42
43struct CVoice
44{
45 CSample *m_pSample;
46 CChannel *m_pChannel;
47 int m_Age; // increases when reused
48 int m_Tick;
49 int m_Vol; // 0 - 255
50 int m_Flags;
51 vec2 m_Position;
52 float m_Falloff; // [0.0, 1.0]
53
54 int m_Shape;
55 union
56 {
57 ISound::CVoiceShapeCircle m_Circle;
58 ISound::CVoiceShapeRectangle m_Rectangle;
59 };
60};
61
62class CSound : public IEngineSound
63{
64 enum
65 {
66 NUM_SAMPLES = 512,
67 NUM_VOICES = 256,
68 NUM_CHANNELS = 16,
69 };
70
71 bool m_SoundEnabled = false;
72 SDL_AudioDeviceID m_Device = 0;
73 CLock m_SoundLock;
74
75 CSample m_aSamples[NUM_SAMPLES] GUARDED_BY(m_SoundLock) = {{.m_Index: 0}};
76 int m_FirstFreeSampleIndex GUARDED_BY(m_SoundLock) = 0;
77
78 CVoice m_aVoices[NUM_VOICES] GUARDED_BY(m_SoundLock) = {{.m_pSample: nullptr}};
79 CChannel m_aChannels[NUM_CHANNELS] GUARDED_BY(m_SoundLock) = {{.m_Vol: 255, .m_Pan: 0}};
80 int m_NextVoice GUARDED_BY(m_SoundLock) = 0;
81 uint32_t m_MaxFrames = 0;
82
83 // This is not an std::atomic<vec2> as this would require linking with
84 // libatomic with clang x86 as there is no native support for this.
85 std::atomic<float> m_ListenerPositionX = 0.0f;
86 std::atomic<float> m_ListenerPositionY = 0.0f;
87 std::atomic<int> m_SoundVolume = 100;
88 int m_MixingRate = 48000;
89
90 class IEngineGraphics *m_pGraphics = nullptr;
91 IStorage *m_pStorage = nullptr;
92
93 int *m_pMixBuffer = nullptr;
94
95 CSample *AllocSample() REQUIRES(!m_SoundLock);
96 void RateConvert(CSample &Sample) const;
97
98 // pContextName used for error
99 bool DecodeOpus(CSample &Sample, const void *pData, unsigned DataSize, const char *pContextName) const;
100 bool DecodeWV(CSample &Sample, const void *pData, unsigned DataSize, const char *pContextName) 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, const char *pContextName) override REQUIRES(!m_SoundLock);
114 int LoadWVFromMem(const void *pData, unsigned DataSize, bool ForceLoad, const char *pContextName) 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
149