Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "chromecast/media/cma/backend/alsa/slew_volume.h" | 5 #include "chromecast/media/cma/backend/alsa/slew_volume.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/vector_math.h" | 10 #include "media/base/vector_math.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // The time to slew from current volume to target volume. | 14 // The time to slew from current volume to target volume. |
| 15 const int kMaxSlewTimeMs = 100; | 15 const int kMaxSlewTimeMs = 100; |
| 16 const int kDefaultSampleRate = 44100; | 16 const int kDefaultSampleRate = 44100; |
| 17 } | 17 |
| 18 } // namespace | |
| 19 | |
| 20 struct FMACTraits { | |
| 21 static void ProcessBulkData(const float* src, | |
| 22 float volume, | |
| 23 int frames, | |
| 24 float* dest) { | |
| 25 ::media::vector_math::FMAC(src, volume, frames, dest); | |
| 26 } | |
| 27 | |
| 28 static void ProcessSingleDatum(const float* src, float volume, float* dest) { | |
| 29 (*dest) += (*src) * volume; | |
| 30 } | |
| 31 | |
| 32 static void ProcessZeroVolume(const float* src, int frames, float* dest) {} | |
| 33 | |
| 34 static void ProcessUnityVolume(const float* src, int frames, float* dest) { | |
| 35 ProcessBulkData(src, 1.0, frames, dest); | |
| 36 } | |
| 37 }; | |
| 38 | |
| 39 struct FMULTraits { | |
| 40 static void ProcessBulkData(const float* src, | |
| 41 float volume, | |
| 42 int frames, | |
| 43 float* dest) { | |
| 44 ::media::vector_math::FMUL(src, volume, frames, dest); | |
| 45 } | |
| 46 | |
| 47 static void ProcessSingleDatum(const float* src, float volume, float* dest) { | |
| 48 (*dest) = (*src) * volume; | |
| 49 } | |
| 50 | |
| 51 static void ProcessZeroVolume(const float* src, int frames, float* dest) { | |
| 52 memset(dest, 0, frames * sizeof(*dest)); | |
|
halliwell
2017/05/05 17:24:50
inconsistent with std::memcpy below?
bshaya
2017/05/05 17:54:03
Done.
| |
| 53 } | |
| 54 | |
| 55 static void ProcessUnityVolume(const float* src, int frames, float* dest) { | |
| 56 if (src == dest) { | |
| 57 return; | |
| 58 } | |
| 59 std::memcpy(dest, src, frames * sizeof(*dest)); | |
| 60 } | |
| 61 }; | |
| 18 | 62 |
| 19 namespace chromecast { | 63 namespace chromecast { |
| 20 namespace media { | 64 namespace media { |
| 21 | 65 |
| 22 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs) {} | 66 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs) {} |
| 23 | 67 |
| 24 SlewVolume::SlewVolume(int max_slew_time_ms) | 68 SlewVolume::SlewVolume(int max_slew_time_ms) |
| 25 : sample_rate_(kDefaultSampleRate), | 69 : sample_rate_(kDefaultSampleRate), |
| 26 max_slew_time_ms_(max_slew_time_ms), | 70 max_slew_time_ms_(max_slew_time_ms), |
| 27 max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) { | 71 max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) {} |
| 28 LOG(INFO) << "Creating a slew volume: " << max_slew_time_ms; | |
| 29 } | |
| 30 | 72 |
| 31 void SlewVolume::SetSampleRate(int sample_rate) { | 73 void SlewVolume::SetSampleRate(int sample_rate) { |
| 74 DCHECK_GT(sample_rate, 0); | |
| 75 | |
| 32 sample_rate_ = sample_rate; | 76 sample_rate_ = sample_rate; |
| 33 SetVolume(volume_scale_); | 77 SetVolume(volume_scale_); |
| 34 } | 78 } |
| 35 | 79 |
| 36 // Slew rate should be volume_to_slew / slew_time / sample_rate | 80 // Slew rate should be volume_to_slew / slew_time / sample_rate |
| 37 void SlewVolume::SetVolume(double volume_scale) { | 81 void SlewVolume::SetVolume(double volume_scale) { |
| 38 volume_scale_ = volume_scale; | 82 volume_scale_ = volume_scale; |
| 39 if (interrupted_) { | 83 if (interrupted_) { |
| 40 current_volume_ = volume_scale_; | 84 current_volume_ = volume_scale_; |
| 41 } | 85 } |
| 42 if (volume_scale_ > current_volume_) { | 86 if (volume_scale_ > current_volume_) { |
| 43 max_slew_per_sample_ = (volume_scale_ - current_volume_) * 1000.0 / | 87 max_slew_per_sample_ = (volume_scale_ - current_volume_) * 1000.0 / |
| 44 (max_slew_time_ms_ * sample_rate_); | 88 (max_slew_time_ms_ * sample_rate_); |
| 45 } else { | 89 } else { |
| 46 max_slew_per_sample_ = (current_volume_ - volume_scale_) * 1000.0 / | 90 max_slew_per_sample_ = (current_volume_ - volume_scale_) * 1000.0 / |
| 47 (max_slew_time_ms_ * sample_rate_); | 91 (max_slew_time_ms_ * sample_rate_); |
| 48 } | 92 } |
| 49 } | 93 } |
| 50 | 94 |
| 51 void SlewVolume::SetMaxSlewTimeMs(int max_slew_time_ms) { | 95 void SlewVolume::SetMaxSlewTimeMs(int max_slew_time_ms) { |
| 96 DCHECK_GE(max_slew_time_ms, 0); | |
| 97 | |
| 52 max_slew_time_ms_ = max_slew_time_ms; | 98 max_slew_time_ms_ = max_slew_time_ms; |
| 53 } | 99 } |
| 54 | 100 |
| 55 void SlewVolume::Interrupted() { | 101 void SlewVolume::Interrupted() { |
| 56 interrupted_ = true; | 102 interrupted_ = true; |
| 57 current_volume_ = volume_scale_; | 103 current_volume_ = volume_scale_; |
| 58 } | 104 } |
| 59 | 105 |
| 60 void SlewVolume::ProcessFMAC(bool repeat_transition, | 106 void SlewVolume::ProcessFMAC(bool repeat_transition, |
| 61 const float* src, | 107 const float* src, |
| 62 int frames, | 108 int frames, |
| 63 float* dest) { | 109 float* dest) { |
| 110 ProcessData<FMACTraits>(repeat_transition, src, frames, dest); | |
| 111 } | |
| 112 | |
| 113 void SlewVolume::ProcessFMUL(bool repeat_transition, | |
| 114 const float* src, | |
| 115 int frames, | |
| 116 float* dest) { | |
| 117 ProcessData<FMULTraits>(repeat_transition, src, frames, dest); | |
| 118 } | |
| 119 | |
| 120 template <typename Traits> | |
| 121 void SlewVolume::ProcessData(bool repeat_transition, | |
| 122 const float* src, | |
| 123 int frames, | |
| 124 float* dest) { | |
| 64 DCHECK(src); | 125 DCHECK(src); |
| 65 DCHECK(dest); | 126 DCHECK(dest); |
| 66 // Ensure |src| and |dest| are 16-byte aligned. | 127 /* Ensure |src| and |dest| are 16-byte aligned. */ |
|
halliwell
2017/05/05 17:24:50
why switching comment style in this file?
bshaya
2017/05/05 17:54:03
Done.
| |
| 67 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & | 128 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & |
| 68 (::media::vector_math::kRequiredAlignment - 1)); | 129 (::media::vector_math::kRequiredAlignment - 1)); |
| 69 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & | 130 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & |
| 70 (::media::vector_math::kRequiredAlignment - 1)); | 131 (::media::vector_math::kRequiredAlignment - 1)); |
| 71 | 132 |
| 72 if (!frames) { | 133 if (!frames) { |
| 73 return; | 134 return; |
| 74 } | 135 } |
| 75 | 136 |
| 76 interrupted_ = false; | 137 interrupted_ = false; |
| 77 if (repeat_transition) { | 138 if (repeat_transition) { |
| 78 current_volume_ = last_starting_volume_; | 139 current_volume_ = last_starting_volume_; |
| 79 } else { | 140 } else { |
| 80 last_starting_volume_ = current_volume_; | 141 last_starting_volume_ = current_volume_; |
| 81 } | 142 } |
| 82 | 143 |
| 83 if (current_volume_ == volume_scale_) { | 144 if (current_volume_ == volume_scale_) { |
| 84 if (current_volume_ == 0.0) { | 145 if (current_volume_ == 0.0) { |
| 146 Traits::ProcessZeroVolume(src, frames, dest); | |
| 85 return; | 147 return; |
| 86 } | 148 } |
| 87 ::media::vector_math::FMAC(src, current_volume_, frames, dest); | 149 if (current_volume_ == 1.0) { |
| 150 Traits::ProcessUnityVolume(src, frames, dest); | |
| 151 return; | |
| 152 } | |
| 153 Traits::ProcessBulkData(src, current_volume_, frames, dest); | |
| 88 return; | 154 return; |
| 89 } else if (current_volume_ < volume_scale_) { | 155 } |
| 156 | |
| 157 if (current_volume_ < volume_scale_) { | |
| 90 do { | 158 do { |
| 91 (*dest) += (*src) * current_volume_; | 159 Traits::ProcessSingleDatum(src, current_volume_, dest); |
| 92 ++src; | 160 ++src; |
| 93 ++dest; | 161 ++dest; |
| 94 --frames; | 162 --frames; |
| 95 current_volume_ += max_slew_per_sample_; | 163 current_volume_ += max_slew_per_sample_; |
| 96 } while (current_volume_ < volume_scale_ && frames); | 164 } while (current_volume_ < volume_scale_ && frames); |
| 97 current_volume_ = std::min(current_volume_, volume_scale_); | 165 current_volume_ = std::min(current_volume_, volume_scale_); |
| 98 } else { // current_volume_ > volume_scale_ | 166 } else { /* current_volume_ > volume_scale_ */ |
| 99 do { | 167 do { |
| 100 (*dest) += (*src) * current_volume_; | 168 Traits::ProcessSingleDatum(src, current_volume_, dest); |
| 101 ++src; | 169 ++src; |
| 102 ++dest; | 170 ++dest; |
| 103 --frames; | 171 --frames; |
| 104 current_volume_ -= max_slew_per_sample_; | 172 current_volume_ -= max_slew_per_sample_; |
| 105 } while (current_volume_ > volume_scale_ && frames); | 173 } while (current_volume_ > volume_scale_ && frames); |
| 106 current_volume_ = std::max(current_volume_, volume_scale_); | 174 current_volume_ = std::max(current_volume_, volume_scale_); |
| 107 } | 175 } |
| 108 | 176 while (frames && (reinterpret_cast<uintptr_t>(src) & |
| 109 if (frames) { | 177 (::media::vector_math::kRequiredAlignment - 1))) { |
| 110 for (int f = 0; f < frames; ++f) { | 178 Traits::ProcessSingleDatum(src, current_volume_, dest); |
| 111 dest[f] += src[f] * current_volume_; | 179 ++src; |
| 112 } | 180 ++dest; |
| 181 --frames; | |
| 113 } | 182 } |
| 114 } | |
| 115 | |
| 116 // Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz | |
| 117 // on pineapple. | |
| 118 // Assumes 2 channel audio. | |
| 119 bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) { | |
| 120 DCHECK(data); | |
| 121 | |
| 122 if (!frames) { | 183 if (!frames) { |
| 123 return true; | 184 return; |
| 124 } | 185 } |
| 125 | 186 Traits::ProcessBulkData(src, current_volume_, frames, dest); |
| 126 interrupted_ = false; | |
| 127 if (current_volume_ == volume_scale_) { | |
| 128 if (current_volume_ == 1.0) { | |
| 129 return true; | |
| 130 } | |
| 131 for (int i = 0; i < 2 * frames; ++i) { | |
| 132 data[i] *= current_volume_; | |
| 133 } | |
| 134 return true; | |
| 135 } else if (current_volume_ < volume_scale_) { | |
| 136 do { | |
| 137 (*data) *= current_volume_; | |
| 138 ++data; | |
| 139 (*data) *= current_volume_; | |
| 140 ++data; | |
| 141 --frames; | |
| 142 current_volume_ += max_slew_per_sample_; | |
| 143 } while (current_volume_ < volume_scale_ && frames); | |
| 144 current_volume_ = std::min(current_volume_, volume_scale_); | |
| 145 } else { | |
| 146 do { | |
| 147 (*data) *= current_volume_; | |
| 148 ++data; | |
| 149 (*data) *= current_volume_; | |
| 150 ++data; | |
| 151 --frames; | |
| 152 current_volume_ -= max_slew_per_sample_; | |
| 153 } while (current_volume_ > volume_scale_ && frames); | |
| 154 current_volume_ = std::max(current_volume_, volume_scale_); | |
| 155 } | |
| 156 | |
| 157 if (current_volume_ == 1.0) { | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 if (current_volume_ == 0.0) { | |
| 162 std::fill_n(data, frames * 2, 0); | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 for (int i = 0; i < 2 * frames; ++i) { | |
| 167 data[i] *= current_volume_; | |
| 168 } | |
| 169 return true; | |
| 170 } | 187 } |
| 171 | 188 |
| 172 } // namespace media | 189 } // namespace media |
| 173 } // namespace chromecast | 190 } // namespace chromecast |
| OLD | NEW |