1
0

SampleCopy.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * SampleCopy.h
  3. * ------------
  4. * Purpose: Functions for copying sample data.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "openmpt/soundbase/SampleConvert.hpp"
  12. #include "openmpt/soundbase/SampleDecode.hpp"
  13. OPENMPT_NAMESPACE_BEGIN
  14. // Copy a sample data buffer.
  15. // targetBuffer: Buffer in which the sample should be copied into.
  16. // numSamples: Number of samples of size T that should be copied. targetBuffer is expected to be able to hold "numSamples * incTarget" samples.
  17. // incTarget: Number of samples by which the target data pointer is increased each time.
  18. // sourceBuffer: Buffer from which the samples should be read.
  19. // sourceSize: Size of source buffer, in bytes.
  20. // incSource: Number of samples by which the source data pointer is increased each time.
  21. //
  22. // Template arguments:
  23. // SampleConversion: Functor of type SampleConversionFunctor to apply sample conversion (see above for existing functors).
  24. template <typename SampleConversion>
  25. size_t CopySample(typename SampleConversion::output_t *MPT_RESTRICT outBuf, size_t numSamples, size_t incTarget, const typename SampleConversion::input_t *MPT_RESTRICT inBuf, size_t sourceSize, size_t incSource, SampleConversion conv = SampleConversion())
  26. {
  27. const size_t sampleSize = incSource * SampleConversion::input_inc * sizeof(typename SampleConversion::input_t);
  28. LimitMax(numSamples, sourceSize / sampleSize);
  29. const size_t copySize = numSamples * sampleSize;
  30. SampleConversion sampleConv(conv);
  31. while(numSamples--)
  32. {
  33. *outBuf = sampleConv(inBuf);
  34. outBuf += incTarget;
  35. inBuf += incSource * SampleConversion::input_inc;
  36. }
  37. return copySize;
  38. }
  39. OPENMPT_NAMESPACE_END