Skip to content
This repository was archived by the owner on Jul 18, 2023. It is now read-only.

Commit cb1dc9d

Browse files
SimakengSeng-Jik
authored andcommitted
GetRealtimeVolume BugFixed
Fix #93
1 parent 9726cd9 commit cb1dc9d

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

WindowsImpl/XASoundPlayer.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,37 @@ float Snowing::PlatformImpls::WindowsImpl::XAudio2::XASoundPlayer::GetRealtimeVo
190190
const size_t currentSampleID = GetPosition();
191191
constexpr size_t sampleNeed = 4096;
192192

193-
const auto beginSample = std::clamp(size_t(currentSampleID - sampleNeed / 2), size_t(0u), sampleCount);
194-
const auto endSample = std::clamp(size_t(currentSampleID + sampleNeed / 2), size_t(0u), sampleCount);
193+
const auto beginSample = std::clamp(size_t(currentSampleID - sampleNeed), size_t(0u), sampleCount);
194+
const auto endSample = std::clamp(size_t(currentSampleID), size_t(0u), sampleCount);
195195

196-
float volume = 0;
196+
int32_t dbNow = 0;
197197
for (size_t currentSampleID = beginSample; currentSampleID < endSample; ++currentSampleID)
198198
{
199199
const size_t bytePosition = currentSampleID * (format.wBitsPerSample / 8) * format.nChannels;
200-
const auto pSample = blob->Get<uint16_t*>(bytePosition);
200+
const auto pSample = blob->Get<int16_t*>(bytePosition);
201201

202+
int32_t sample = 0;
202203
for (size_t channel = 0; channel < format.nChannels; ++channel)
204+
sample += std::abs(pSample[channel]);
205+
sample /= format.nChannels;
206+
207+
dbNow = max(sample, dbNow);
208+
209+
const auto pow2 = [](const double v)
203210
{
204-
const float vol = static_cast<float>(pSample[channel]) / 65535.0f;
205-
volume += vol;
211+
const auto magic = 10;
212+
return v * v / magic;
213+
//这里的magic用来削弱衰减速度的,我试过1,10,100,总之效果都还可以
214+
//司马坑 2019-7-4
215+
};
216+
if (dbNow != sample)
217+
{
218+
dbNow -= pow2(dbNow - sample) / format.nSamplesPerSec;
206219
}
207220
}
221+
const auto maxValue = 1 << (format.wBitsPerSample - 1);
208222

209-
210-
return volume / (endSample - beginSample) / format.nChannels;
223+
return 1.0 * dbNow / (maxValue - 1);
211224
}
212225
else
213226
return 0;

0 commit comments

Comments
 (0)