| 356 | } |
| 357 | |
| 358 | LevelBlock measureInt16StereoLevelBlock(const QByteArray& pcm) |
| 359 | { |
| 360 | LevelBlock block; |
| 361 | const int sampleCount = pcm.size() / static_cast<int>(sizeof(int16_t)); |
| 362 | if (sampleCount <= 0) { |
| 363 | return block; |
| 364 | } |
| 365 | |
| 366 | const auto* src = reinterpret_cast<const int16_t*>(pcm.constData()); |
| 367 | const int stereoFrames = sampleCount / 2; |
| 368 | for (int i = 0; i < stereoFrames; ++i) { |
| 369 | const float left = absInt16(src[i * 2]); |
| 370 | const float right = absInt16(src[i * 2 + 1]); |
| 371 | const float s = std::max(left, right); |
| 372 | block.peak = std::max(block.peak, s); |
| 373 | block.sumSq += static_cast<double>(s) * s; |
| 374 | ++block.frames; |
| 375 | } |
| 376 | |
| 377 | if ((sampleCount % 2) != 0) { |
| 378 | const float s = absInt16(src[sampleCount - 1]); |
| 379 | block.peak = std::max(block.peak, s); |
| 380 | block.sumSq += static_cast<double>(s) * s; |
| 381 | ++block.frames; |
| 382 | } |
| 383 | |
| 384 | return block; |
| 385 | } |
| 386 | |
| 387 | float rmsFromLevelBlock(const LevelBlock& block) |
| 388 | { |