| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ | 5 #ifndef SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
| 6 #define SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ | 6 #define SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 namespace media { | 12 namespace media { |
| 13 namespace audio { | 13 namespace audio { |
| 14 namespace mixers { | 14 namespace mixers { |
| 15 namespace utils { | 15 namespace utils { |
| 16 | 16 |
| 17 // mixer_utils.h is a collection of inline templated utility functions meant to | 17 // mixer_utils.h is a collection of inline templated utility functions meant to |
| 18 // be used by mixer implementations and expanded/optimized at compile time in | 18 // be used by mixer implementations and expanded/optimized at compile time in |
| 19 // order to produce efficient inner mixing loops for all of the different | 19 // order to produce efficient inner mixing loops for all of the different |
| 20 // variations of source/destination sample type/channel counts. | 20 // variations of source/destination sample type/channel counts. |
| 21 | 21 |
| 22 // Template to read samples and normalize them into signed 32 bit integers. | 22 // Template to read samples and normalize them into signed 16 bit integers |
| 23 // stored in 32 bit integers. |
| 23 template <typename SType, typename Enable = void> class SampleNormalizer; | 24 template <typename SType, typename Enable = void> class SampleNormalizer; |
| 24 | 25 |
| 25 template <typename SType> | 26 template <typename SType> |
| 26 class SampleNormalizer< | 27 class SampleNormalizer< |
| 27 SType, | 28 SType, |
| 28 typename std::enable_if< | 29 typename std::enable_if< |
| 29 std::is_same<SType, uint8_t>::value, | 30 std::is_same<SType, uint8_t>::value, |
| 30 void>::type> { | 31 void>::type> { |
| 31 public: | 32 public: |
| 32 static inline int32_t Read(const SType* src) { | 33 static inline int32_t Read(const SType* src) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 (SChCount == 2) && (DChCount == 1), | 78 (SChCount == 2) && (DChCount == 1), |
| 78 void>::type> { | 79 void>::type> { |
| 79 public: | 80 public: |
| 80 static constexpr size_t DstPerSrc = 1; | 81 static constexpr size_t DstPerSrc = 1; |
| 81 static inline int32_t Read(const SType* src) { | 82 static inline int32_t Read(const SType* src) { |
| 82 return (SampleNormalizer<SType>::Read(src + 0) + | 83 return (SampleNormalizer<SType>::Read(src + 0) + |
| 83 SampleNormalizer<SType>::Read(src + 1)) >> 1; | 84 SampleNormalizer<SType>::Read(src + 1)) >> 1; |
| 84 } | 85 } |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 // Template to produce destination samples from normalized samples. | 88 // Template to mix normalized destination samples with normalized source samples |
| 88 template <typename DType, typename Enable = void> class DstConverter; | 89 // based on accumulation policy. |
| 89 | 90 template <bool DoAccumulate, |
| 90 template <typename DType> | |
| 91 class DstConverter<DType, | |
| 92 typename std::enable_if< | |
| 93 std::is_same<DType, int16_t>::value, | |
| 94 void>::type> { | |
| 95 public: | |
| 96 static inline DType Convert(int32_t sample) { | |
| 97 return static_cast<DType>(sample); | |
| 98 } | |
| 99 }; | |
| 100 | |
| 101 template <typename DType> | |
| 102 class DstConverter<DType, | |
| 103 typename std::enable_if< | |
| 104 std::is_same<DType, uint8_t>::value, | |
| 105 void>::type> { | |
| 106 public: | |
| 107 static inline DType Convert(int32_t sample) { | |
| 108 return static_cast<DType>((sample >> 8) + 0x80); | |
| 109 } | |
| 110 }; | |
| 111 | |
| 112 // Template to mix destination samples with normalized source samples based on | |
| 113 // accumulation policy. | |
| 114 template <typename DType, | |
| 115 bool DoAccumulate, | |
| 116 typename Enable = void> | 91 typename Enable = void> |
| 117 class DstMixer; | 92 class DstMixer; |
| 118 | 93 |
| 119 template <typename DType, | 94 template <bool DoAccumulate> |
| 120 bool DoAccumulate> | 95 class DstMixer<DoAccumulate, |
| 121 class DstMixer<DType, DoAccumulate, | |
| 122 typename std::enable_if< | 96 typename std::enable_if< |
| 123 DoAccumulate == false, | 97 DoAccumulate == false, |
| 124 void>::type> { | 98 void>::type> { |
| 125 public: | 99 public: |
| 126 static inline int32_t Mix(const DType* dst, uint32_t sample) { | 100 static inline constexpr int32_t Mix(int32_t dst, int32_t sample) { |
| 127 return DstConverter<DType>::Convert(sample); | 101 return sample; |
| 128 } | 102 } |
| 129 }; | 103 }; |
| 130 | 104 |
| 131 template <typename DType, | 105 template <bool DoAccumulate> |
| 132 bool DoAccumulate> | 106 class DstMixer<DoAccumulate, |
| 133 class DstMixer<DType, DoAccumulate, | |
| 134 typename std::enable_if< | 107 typename std::enable_if< |
| 135 DoAccumulate == true, | 108 DoAccumulate == true, |
| 136 void>::type> { | 109 void>::type> { |
| 137 public: | 110 public: |
| 138 static inline int32_t Mix(const DType* dst, int32_t sample) { | 111 static inline constexpr int32_t Mix(int32_t dst, int32_t sample) { |
| 139 sample += SampleNormalizer<DType>::Read(dst); | 112 return sample + dst; |
| 140 | |
| 141 if (sample > std::numeric_limits<int16_t>::max()) { | |
| 142 return DstConverter<DType>::Convert(std::numeric_limits<int16_t>::max()); | |
| 143 } else if (sample < std::numeric_limits<int16_t>::min()) { | |
| 144 return DstConverter<DType>::Convert(std::numeric_limits<int16_t>::min()); | |
| 145 } else { | |
| 146 return DstConverter<DType>::Convert(sample); | |
| 147 } | |
| 148 } | 113 } |
| 149 }; | 114 }; |
| 150 | 115 |
| 151 | 116 |
| 152 } // namespace utils | 117 } // namespace utils |
| 153 } // namespace mixers | 118 } // namespace mixers |
| 154 } // namespace audio | 119 } // namespace audio |
| 155 } // namespace media | 120 } // namespace media |
| 156 } // namespace mojo | 121 } // namespace mojo |
| 157 | 122 |
| 158 #endif // SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ | 123 #endif // SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
| OLD | NEW |