Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromecast/media/cma/backend/alsa/slew_volume.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "media/base/vector_math.h" | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 // The time to slew from 0.0 to 1.0. | |
| 15 const int kMaxSlewTimeMs = 100; | |
| 16 const int kDefaultSampleRate = 44100; | |
| 17 } | |
| 18 | |
| 19 namespace chromecast { | |
| 20 namespace media { | |
| 21 | |
| 22 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs, kMaxSlewTimeMs) {} | |
| 23 | |
| 24 SlewVolume::SlewVolume(int max_slew_time_up_ms, int max_slew_time_down_ms) | |
| 25 : max_slew_time_up_ms_(max_slew_time_up_ms), | |
| 26 max_slew_time_down_ms_(max_slew_time_down_ms), | |
| 27 max_slew_up_(1000.0 / (max_slew_time_up_ms * kDefaultSampleRate)), | |
| 28 max_slew_down_(1000.0 / (max_slew_time_down_ms * kDefaultSampleRate)) {} | |
| 29 | |
| 30 // Slew rate should be 1 / (slew_time * sample_rate) | |
| 31 void SlewVolume::SetSampleRate(int sample_rate) { | |
| 32 max_slew_up_ = (1000.0 / (max_slew_time_up_ms_ * sample_rate)); | |
|
wzhong
2016/09/16 01:03:46
Nit: () is not needed.
jyw
2016/09/16 20:35:41
Done.
| |
| 33 max_slew_down_ = (1000.0 / (max_slew_time_down_ms_ * sample_rate)); | |
| 34 } | |
| 35 | |
| 36 void SlewVolume::SetVolume(double volume_scale) { | |
| 37 volume_scale_ = volume_scale; | |
| 38 } | |
| 39 | |
| 40 void SlewVolume::ProcessFMAC(bool repeat_transition, | |
| 41 const float* src, | |
| 42 int frames, | |
| 43 float* dest) { | |
| 44 DCHECK(src); | |
|
wzhong
2016/09/16 01:03:47
DCHECK alignment.
jyw
2016/09/16 20:35:42
Done.
| |
| 45 DCHECK(dest); | |
| 46 | |
| 47 if (!frames) { | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 if (repeat_transition) { | |
| 52 current_volume_ = last_starting_volume_; | |
| 53 } else { | |
| 54 last_starting_volume_ = current_volume_; | |
| 55 } | |
| 56 | |
| 57 if (current_volume_ == volume_scale_) { | |
| 58 if (current_volume_ == 0.0) { | |
|
wzhong
2016/09/16 01:03:47
NO need to clear dest?
jyw
2016/09/16 20:35:42
FMAC does dest[i] += src[i] * current_volume_; so
| |
| 59 return; | |
| 60 } | |
| 61 ::media::vector_math::FMAC(src, current_volume_, frames, dest); | |
| 62 return; | |
| 63 } else if (current_volume_ < volume_scale_) { | |
| 64 do { | |
| 65 (*dest) += (*src) * current_volume_; | |
| 66 ++src; | |
| 67 ++dest; | |
| 68 --frames; | |
| 69 current_volume_ += max_slew_up_; | |
| 70 } while (current_volume_ < volume_scale_ && frames); | |
| 71 current_volume_ = std::min(current_volume_, volume_scale_); | |
| 72 } else { // current_volume_ > volume_scale_ | |
| 73 do { | |
| 74 (*dest) += (*src) * current_volume_; | |
| 75 ++src; | |
| 76 ++dest; | |
| 77 --frames; | |
| 78 current_volume_ -= max_slew_down_; | |
| 79 } while (current_volume_ > volume_scale_ && frames); | |
| 80 current_volume_ = std::max(current_volume_, volume_scale_); | |
| 81 } | |
| 82 | |
| 83 if (frames) { | |
| 84 for (int f = 0; f < frames; ++f) { | |
| 85 dest[f] += src[f] * current_volume_; | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 // Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz | |
| 91 // on pineapple. | |
| 92 // Assumes 2 channel audio. | |
| 93 bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) { | |
| 94 DCHECK(data); | |
| 95 | |
| 96 if (!frames) { | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 if (current_volume_ == volume_scale_) { | |
| 101 if (current_volume_ == 1.0) { | |
| 102 return true; | |
| 103 } | |
| 104 for (int i = 0; i < 2 * frames; ++i) { | |
| 105 data[i] *= current_volume_; | |
| 106 } | |
| 107 return true; | |
| 108 } else if (current_volume_ < volume_scale_) { | |
| 109 do { | |
| 110 (*data) *= current_volume_; | |
| 111 ++data; | |
| 112 (*data) *= current_volume_; | |
| 113 ++data; | |
| 114 --frames; | |
| 115 current_volume_ += max_slew_up_; | |
| 116 } while (current_volume_ < volume_scale_ && frames); | |
| 117 current_volume_ = std::min(current_volume_, volume_scale_); | |
| 118 } else { | |
| 119 do { | |
| 120 (*data) *= current_volume_; | |
| 121 ++data; | |
| 122 (*data) *= current_volume_; | |
| 123 ++data; | |
| 124 --frames; | |
| 125 current_volume_ -= max_slew_down_; | |
| 126 } while (current_volume_ > volume_scale_ && frames); | |
| 127 current_volume_ = std::max(current_volume_, volume_scale_); | |
| 128 } | |
| 129 | |
| 130 if (current_volume_ == 1.0) { | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 if (current_volume_ == 0.0) { | |
| 135 std::fill_n(data, frames * 2, 0); | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 for (int i = 0; i < 2 * frames; ++i) { | |
| 140 data[i] *= current_volume_; | |
| 141 } | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 } // namespace media | |
| 146 } // namespace chromecast | |
| OLD | NEW |