OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
| 6 #define SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
| 7 |
| 8 #include <limits> |
| 9 #include <type_traits> |
| 10 |
| 11 namespace mojo { |
| 12 namespace media { |
| 13 namespace audio { |
| 14 namespace mixers { |
| 15 namespace utils { |
| 16 |
| 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 |
| 19 // order to produce efficient inner mixing loops for all of the different |
| 20 // variations of source/destination sample type/channel counts. |
| 21 |
| 22 // Template to read samples and normalize them into signed 32 bit integers. |
| 23 template <typename SType, typename Enable = void> class SampleNormalizer; |
| 24 |
| 25 template <typename SType> |
| 26 class SampleNormalizer< |
| 27 SType, |
| 28 typename std::enable_if< |
| 29 std::is_same<SType, uint8_t>::value, |
| 30 void>::type> { |
| 31 public: |
| 32 static inline int32_t Read(const SType* src) { |
| 33 register SType tmp = *src; |
| 34 return (static_cast<int32_t>(tmp) << 8) - 0x8000; |
| 35 } |
| 36 }; |
| 37 |
| 38 template <typename SType> |
| 39 class SampleNormalizer< |
| 40 SType, |
| 41 typename std::enable_if< |
| 42 std::is_same<SType, int16_t>::value, |
| 43 void>::type> { |
| 44 public: |
| 45 static inline int32_t Read(const SType* src) { |
| 46 return static_cast<int32_t>(*src); |
| 47 } |
| 48 }; |
| 49 |
| 50 // Template to read normalized source samples, and combine channels if required. |
| 51 template <typename SType, |
| 52 size_t SChCount, |
| 53 size_t DChCount, |
| 54 typename Enable = void> |
| 55 class SrcReader; |
| 56 |
| 57 template <typename SType, |
| 58 size_t SChCount, |
| 59 size_t DChCount> |
| 60 class SrcReader<SType, SChCount, DChCount, |
| 61 typename std::enable_if< |
| 62 (SChCount == DChCount) || |
| 63 ((SChCount == 1) && (DChCount == 2)), |
| 64 void>::type> { |
| 65 public: |
| 66 static constexpr size_t DstPerSrc = DChCount / SChCount; |
| 67 static inline int32_t Read(const SType* src) { |
| 68 return SampleNormalizer<SType>::Read(src); |
| 69 } |
| 70 }; |
| 71 |
| 72 template <typename SType, |
| 73 size_t SChCount, |
| 74 size_t DChCount> |
| 75 class SrcReader<SType, SChCount, DChCount, |
| 76 typename std::enable_if< |
| 77 (SChCount == 2) && (DChCount == 1), |
| 78 void>::type> { |
| 79 public: |
| 80 static constexpr size_t DstPerSrc = 1; |
| 81 static inline int32_t Read(const SType* src) { |
| 82 return (SampleNormalizer<SType>::Read(src + 0) + |
| 83 SampleNormalizer<SType>::Read(src + 1)) >> 1; |
| 84 } |
| 85 }; |
| 86 |
| 87 // Template to produce destination samples from normalized samples. |
| 88 template <typename DType, typename Enable = void> class DstConverter; |
| 89 |
| 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> |
| 117 class DstMixer; |
| 118 |
| 119 template <typename DType, |
| 120 bool DoAccumulate> |
| 121 class DstMixer<DType, DoAccumulate, |
| 122 typename std::enable_if< |
| 123 DoAccumulate == false, |
| 124 void>::type> { |
| 125 public: |
| 126 static inline int32_t Mix(const DType* dst, uint32_t sample) { |
| 127 return DstConverter<DType>::Convert(sample); |
| 128 } |
| 129 }; |
| 130 |
| 131 template <typename DType, |
| 132 bool DoAccumulate> |
| 133 class DstMixer<DType, DoAccumulate, |
| 134 typename std::enable_if< |
| 135 DoAccumulate == true, |
| 136 void>::type> { |
| 137 public: |
| 138 static inline int32_t Mix(const DType* dst, int32_t sample) { |
| 139 sample += SampleNormalizer<DType>::Read(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 } |
| 149 }; |
| 150 |
| 151 |
| 152 } // namespace utils |
| 153 } // namespace mixers |
| 154 } // namespace audio |
| 155 } // namespace media |
| 156 } // namespace mojo |
| 157 |
| 158 #endif // SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_ |
OLD | NEW |