ARASamplePositionConversion.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //------------------------------------------------------------------------------
  2. //! \file ARASamplePositionConversion.h
  3. //! convenience functions to ensure consistent conversion from
  4. //! continuous time to discrete sample positions and vice versa.
  5. //! \project ARA SDK Library
  6. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  7. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  8. //! you may not use this file except in compliance with the License.
  9. //! You may obtain a copy of the License at
  10. //!
  11. //! http://www.apache.org/licenses/LICENSE-2.0
  12. //!
  13. //! Unless required by applicable law or agreed to in writing, software
  14. //! distributed under the License is distributed on an "AS IS" BASIS,
  15. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. //! See the License for the specific language governing permissions and
  17. //! limitations under the License.
  18. //------------------------------------------------------------------------------
  19. #ifndef ARASamplePositionConversion_h
  20. #define ARASamplePositionConversion_h
  21. #include <vector>
  22. #include <algorithm>
  23. #include <cmath>
  24. namespace ARA {
  25. //! @addtogroup ARA_Library_Utility_Rounding
  26. //! @{
  27. /*******************************************************************************/
  28. //! @name Rounding to Sample Position
  29. //! Rounding from a continuous time or sample position to a discrete sample position.
  30. //! The rounding intervals are closed on the negative and open on the positive end, which keeps them
  31. //! consistent with all other ARA intervals and prevents potential rounding issues when ranges cross 0.
  32. //! For positive values, this matches regular llround(), but for negative values it rounds -x.5 towards
  33. //! +inf not -inf.
  34. //@{
  35. template<typename SamplePositionType = ARASamplePosition>
  36. inline SamplePositionType roundSamplePosition (double continuousSample) { return static_cast<SamplePositionType> (std::floor (continuousSample + 0.5)); }
  37. template<typename SamplePositionType = ARASamplePosition>
  38. inline SamplePositionType samplePositionAtTime (ARATimePosition timePosition, ARASampleRate sampleRate) { return roundSamplePosition<SamplePositionType> (timePosition * sampleRate); }
  39. //@}
  40. /*******************************************************************************/
  41. //! @name Converting Sample Position To Time
  42. //! Inverse operation of samplePositionAtTime (ARATimePosition t, ARASampleRate sampleRate).
  43. //@{
  44. template<typename SamplePositionType = ARASamplePosition>
  45. inline ARATimePosition timeAtSamplePosition (SamplePositionType samplePositon, ARASampleRate sampleRate) { return static_cast<double> (samplePositon) / sampleRate; }
  46. //@}
  47. //! @} ARA_Library_Utility_Rounding
  48. } // namespace ARA
  49. #endif // ARASamplePositionConversion_h