@@ -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