SimpleDownsample.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "SimpleDownsample.h"
  9. #include <cmath>
  10. size_t GetDownsampleSize(size_t sourceSize, AZ::u32 sourceSampleRate, AZ::u32 targetSampleRate)
  11. {
  12. return (size_t)round((float)sourceSize / ((float)sourceSampleRate / (float)targetSampleRate));
  13. }
  14. void Downsample(AZ::s16* inBuffer, size_t inBufferSize, AZ::u32 inBufferSampleRate,
  15. AZ::s16* outBuffer, size_t outBufferSize, AZ::u32 outBufferSampleRate)
  16. {
  17. if(inBufferSampleRate == outBufferSampleRate)
  18. {
  19. return; // nothing to do here!
  20. }
  21. if(inBufferSampleRate < outBufferSampleRate)
  22. {
  23. AZ_Error("SimpleDownsample", false, "Out buffer sample rate is higher than in buffer sample rate, must be lower");
  24. return;
  25. }
  26. float sampleRateRatio = (float)inBufferSampleRate / (float)outBufferSampleRate;
  27. size_t offsetResult = 0;
  28. size_t offsetBuffer = 0;
  29. while(offsetResult < outBufferSize)
  30. {
  31. size_t nextOffsetBuffer = static_cast<size_t>(round((float)(offsetResult + 1) * sampleRateRatio));
  32. AZ::s32 accum = 0; // this must be int 32 so it doesn't overflow with multiple int 16's possibly at max
  33. size_t count = 0;
  34. for(size_t i = offsetBuffer; i < nextOffsetBuffer && i < inBufferSize; ++i)
  35. {
  36. accum += inBuffer[i];
  37. count++;
  38. }
  39. // do the math in floating point then convert to signed 16
  40. outBuffer[offsetResult] = (AZ::s16)((float)accum / (float)count);
  41. offsetResult++;
  42. offsetBuffer = nextOffsetBuffer;
  43. }
  44. }