-
Notifications
You must be signed in to change notification settings - Fork 1
/
Playback.cpp
251 lines (193 loc) · 7.63 KB
/
Playback.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// by Weikton 05.09.23
//
#include "../main.h"
#include "../game/common.h"
#include "../game/game.h"
#include "Playback.h"
#include "PluginConfig.h"
#include "Header.h"
extern CGame *pGame;
bool Playback::Init() noexcept
{
if(Playback::initStatus)
return false;
LogVoice("[sv:dbg:playback:init] : module initializing...");
if(!PluginConfig::IsPlaybackLoaded())
{
PluginConfig::SetPlaybackLoaded(true);
Playback::ResetConfigs();
}
LogVoice("[sv:dbg:playback:init] : module initialized");
Playback::initStatus = true;
if(!Playback::BassInitHookFunc())
{
LogVoice("[sv:err:bassinithook] : failed to initializing bass");
return false;
}
return true;
}
void Playback::Free() noexcept
{
if(!Playback::initStatus)
return;
LogVoice("[sv:dbg:playback:free] : module releasing...");
Playback::loadStatus = false;
LogVoice("[sv:dbg:playback:free] : module released");
Playback::initStatus = false;
}
void Playback::Tick() noexcept
{
if(!Playback::loadStatus) return;
}
bool Playback::GetSoundEnable() noexcept
{
return PluginConfig::GetSoundEnable();
}
int Playback::GetSoundVolume() noexcept
{
return PluginConfig::GetSoundVolume();
}
bool Playback::GetSoundBalancer() noexcept
{
return PluginConfig::GetSoundBalancer();
}
bool Playback::GetSoundFilter() noexcept
{
return PluginConfig::GetSoundFilter();
}
void Playback::SetSoundEnable(const bool soundEnable) noexcept
{
if(!Playback::loadStatus) return;
PluginConfig::SetSoundEnable(soundEnable);
if(!PluginConfig::GetSoundEnable()) BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 0);
else BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 100 * PluginConfig::GetSoundVolume());
}
void Playback::SetSoundVolume(const int soundVolume) noexcept
{
if(!Playback::loadStatus) return;
PluginConfig::SetSoundVolume(clampEx(soundVolume, 0, 100));
if(PluginConfig::GetSoundEnable())
{
BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, 100 * PluginConfig::GetSoundVolume());
}
}
void Playback::SetSoundBalancer(const bool soundBalancer) noexcept
{
static HFX balancerFxHandle { NULL };
if(!Playback::loadStatus) return;
PluginConfig::SetSoundBalancer(soundBalancer);
if (PluginConfig::GetSoundBalancer() && balancerFxHandle == NULL)
{
balancerFxHandle = BASS_ChannelSetFX(Playback::deviceOutputChannel, BASS_FX_BFX_COMPRESSOR2);
if(balancerFxHandle == NULL)
{
LogVoice("[sv:err:playback:setsoundbalancer] : failed to set balancer effect (code:%d)", BASS_ErrorGetCode());
return PluginConfig::SetSoundBalancer(false);
}
BASS_BFX_COMPRESSOR2 balancerParameters {};
balancerParameters.lChannel = BASS_BFX_CHANALL;
balancerParameters.fGain = 10.f;
balancerParameters.fAttack = 0.01f;
balancerParameters.fRelease = 0.01f;
balancerParameters.fThreshold = -40.f;
balancerParameters.fRatio = 12.f;
if(BASS_FXSetParameters(balancerFxHandle, &balancerParameters) == 0)
{
LogVoice("[sv:err:playback:setsoundbalancer] : failed to set parameters (code:%d)", BASS_ErrorGetCode());
BASS_ChannelRemoveFX(Playback::deviceOutputChannel, balancerFxHandle);
balancerFxHandle = NULL;
PluginConfig::SetSoundBalancer(false);
}
}
else if(!PluginConfig::GetSoundBalancer() && balancerFxHandle != NULL)
{
BASS_ChannelRemoveFX(Playback::deviceOutputChannel, balancerFxHandle);
balancerFxHandle = NULL;
}
}
void Playback::SetSoundFilter(const bool soundFilter) noexcept
{
static HFX filterFxHandle { NULL };
if(!Playback::loadStatus) return;
PluginConfig::SetSoundFilter(soundFilter);
if(PluginConfig::GetSoundFilter() && filterFxHandle == NULL)
{
filterFxHandle = BASS_ChannelSetFX(Playback::deviceOutputChannel, BASS_FX_BFX_BQF);
if(filterFxHandle == NULL)
{
LogVoice("[sv:err:playback:setsoundfilter] : failed to set filter effect (code:%d)", BASS_ErrorGetCode());
return PluginConfig::SetSoundFilter(false);
}
BASS_BFX_BQF parameqParameters {};
parameqParameters.lChannel = BASS_BFX_CHANALL;
parameqParameters.lFilter = BASS_BFX_BQF_LOWPASS;
parameqParameters.fCenter = 3400.f;
parameqParameters.fBandwidth = 0;
parameqParameters.fQ = 0.707f;
if(BASS_FXSetParameters(filterFxHandle, ¶meqParameters) == 0)
{
LogVoice("[sv:err:playback:setsoundfilter] : failed to set parameters (code:%d)", BASS_ErrorGetCode());
BASS_ChannelRemoveFX(Playback::deviceOutputChannel, filterFxHandle);
filterFxHandle = NULL;
PluginConfig::SetSoundFilter(false);
}
}
else if(!PluginConfig::GetSoundFilter() && filterFxHandle != NULL)
{
BASS_ChannelRemoveFX(Playback::deviceOutputChannel, filterFxHandle);
filterFxHandle = NULL;
}
}
void Playback::SyncConfigs() noexcept
{
Playback::SetSoundEnable(PluginConfig::GetSoundEnable());
Playback::SetSoundVolume(PluginConfig::GetSoundVolume());
Playback::SetSoundBalancer(PluginConfig::GetSoundBalancer());
Playback::SetSoundFilter(PluginConfig::GetSoundFilter());
}
void Playback::ResetConfigs() noexcept
{
PluginConfig::SetSoundEnable(PluginConfig::kDefValSoundEnable);
PluginConfig::SetSoundVolume(PluginConfig::kDefValSoundVolume);
PluginConfig::SetSoundBalancer(PluginConfig::kDefValSoundBalancer);
PluginConfig::SetSoundFilter(PluginConfig::kDefValSoundFilter);
}
bool Playback::BassInitHookFunc() noexcept
{
static const BASS_3DVECTOR kZeroVector { 0, 0, 0 };
LogVoice("[sv:dbg:playback:bassinithook] : module loading...");
BASS_SetConfig(BASS_CONFIG_UNICODE, 1);
BASS_SetConfig(BASS_CONFIG_BUFFER, SV::kChannelBufferSizeInMs);
BASS_SetConfig(BASS_CONFIG_UPDATEPERIOD, SV::kAudioUpdatePeriod);
BASS_SetConfig(BASS_CONFIG_UPDATETHREADS, SV::kAudioUpdateThreads);
// BASS_SetConfig(BASS_CONFIG_3DALGORITHM, BASS_3DALG_LIGHT);
BASS_SetConfig(BASS_CONFIG_ANDROID_AAUDIO, 2);
/*LogVoice("[sv:dbg:playback:bassinithook] : hooked function BASS_Init(device:%d, "
"freq:%u, flags:0x%x, win:0x%x, dsguid:0x%x)...", device, freq, flags, win, dsguid);*/
LogVoice("[sv:dbg:playback:bassinithook] : calling function BASS_Init(device:-1, "
"freq:%u, flags:0x%x)...", SV::kFrequency2, BASS_DEVICE_MONO);
if(BASS_Init(-1, SV::kFrequency2, BASS_DEVICE_MONO) == 0)
{
LogVoice("[sv:err:playback:bassinithook] : failed to init bass library (code:%d)", BASS_ErrorGetCode());
return false;
}
/*if(HIWORD(BASS_FX_GetVersion()) != BASSVERSION)
{
LogVoice("[sv:err:playback:init] : failed to check version bassfx library (code:%d)", BASS_ErrorGetCode());
return false;
}*/
Playback::deviceOutputChannel = BASS_StreamCreate(0, 0, NULL, STREAMPROC_DEVICE, nullptr);
if(Playback::deviceOutputChannel == NULL)
{
LogVoice("[sv:err:playback:init] : failed to create device output channel (code:%d)", BASS_ErrorGetCode());
return false;
}
LogVoice("[sv:dbg:playback:bassinithook] : module loaded");
Playback::loadStatus = true;
Playback::SyncConfigs();
return true;
}
bool Playback::initStatus { false };
bool Playback::loadStatus { false };
HSTREAM Playback::deviceOutputChannel { NULL };