Chromium Code Reviews| Index: chromecast/media/cma/backend/alsa/slew_volume.cc |
| diff --git a/chromecast/media/cma/backend/alsa/slew_volume.cc b/chromecast/media/cma/backend/alsa/slew_volume.cc |
| index 38ce1a72a186e43f64ebc3b301fb627e2013a2e2..7891e24a4bc87aa3c3a9fc716549666daae7222e 100644 |
| --- a/chromecast/media/cma/backend/alsa/slew_volume.cc |
| +++ b/chromecast/media/cma/backend/alsa/slew_volume.cc |
| @@ -14,7 +14,98 @@ namespace { |
| // The time to slew from current volume to target volume. |
| const int kMaxSlewTimeMs = 100; |
| const int kDefaultSampleRate = 44100; |
| -} |
| + |
| +} // namespace |
| + |
| +// Used to create ProcessFMAC and ProcessFMUL. |
| +// |BULK_OPERATOR| processes all remaining data. |
| +// |SINGLE_OPERATOR| processes a single datum. |
| +// |ZERO_OPERATOR| is run when |current_volume_| is 0 |
| +// |UNITY_OPERATOR| is run when |current_volume_| is 1 |
| +#define PROCESS_DATA(BULK_OPERATOR, SINGLE_OPERATOR, ZERO_OPERATOR, \ |
|
kmackay
2017/05/05 00:13:30
Not exactly what I had in mind...
bshaya
2017/05/05 00:30:35
Done.
|
| + UNITY_OPERATOR) \ |
| + do { \ |
| + DCHECK(src); \ |
| + DCHECK(dest); \ |
| + /* Ensure |src| and |dest| are 16-byte aligned. */ \ |
| + DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & \ |
| + (::media::vector_math::kRequiredAlignment - 1)); \ |
| + DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & \ |
| + (::media::vector_math::kRequiredAlignment - 1)); \ |
| + \ |
| + if (!frames) { \ |
| + return; \ |
| + } \ |
| + \ |
| + interrupted_ = false; \ |
| + if (repeat_transition) { \ |
| + current_volume_ = last_starting_volume_; \ |
| + } else { \ |
| + last_starting_volume_ = current_volume_; \ |
| + } \ |
| + \ |
| + if (current_volume_ == volume_scale_) { \ |
| + if (current_volume_ == 0.0) { \ |
| + ZERO_OPERATOR; \ |
| + return; \ |
| + } \ |
| + if (current_volume_ == 1.0) { \ |
| + UNITY_OPERATOR; \ |
| + return; \ |
| + } \ |
| + BULK_OPERATOR; \ |
| + return; \ |
| + } \ |
| + \ |
| + if (current_volume_ < volume_scale_) { \ |
| + do { \ |
| + SINGLE_OPERATOR; \ |
| + ++src; \ |
| + ++dest; \ |
| + --frames; \ |
| + current_volume_ += max_slew_per_sample_; \ |
| + } while (current_volume_ < volume_scale_ && frames); \ |
| + current_volume_ = std::min(current_volume_, volume_scale_); \ |
| + } else { /* current_volume_ > volume_scale_ */ \ |
| + do { \ |
| + SINGLE_OPERATOR; \ |
| + ++src; \ |
| + ++dest; \ |
| + --frames; \ |
| + current_volume_ -= max_slew_per_sample_; \ |
| + } while (current_volume_ > volume_scale_ && frames); \ |
| + current_volume_ = std::max(current_volume_, volume_scale_); \ |
| + } \ |
| + while (frames && (reinterpret_cast<uintptr_t>(src) & \ |
| + (::media::vector_math::kRequiredAlignment - 1))) { \ |
| + SINGLE_OPERATOR; \ |
| + ++src; \ |
| + ++dest; \ |
| + --frames; \ |
| + } \ |
| + if (!frames) { \ |
| + return; \ |
| + } \ |
| + BULK_OPERATOR; \ |
| + } while (0); |
| + |
| +#define BULK_OPERATOR_FMAC \ |
| + ::media::vector_math::FMAC(src, current_volume_, frames, dest) |
| +#define SINGLE_OPERATOR_FMAC (*dest) += (*src) * current_volume_ |
| +#define ZERO_OPERATOR_FMAC |
| +#define UNITY_OPERATOR_FMAC BULK_OPERATOR_FMAC |
| + |
| +#define BULK_OPERATOR_FMUL \ |
| + ::media::vector_math::FMUL(src, current_volume_, frames, dest) |
| +#define SINGLE_OPERATOR_FMUL (*dest) = (*src) * current_volume_ |
| +#define ZERO_OPERATOR_FMUL memset(dest, 0, frames * sizeof(*dest)); |
| +#define UNITY_OPERATOR_FMUL \ |
| + do { \ |
| + if (src == dest) { \ |
| + return; \ |
| + } \ |
| + std::memcpy(dest, src, frames * sizeof(*dest)); \ |
| + } while (0); |
| namespace chromecast { |
| namespace media { |
| @@ -24,11 +115,11 @@ SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs) {} |
| SlewVolume::SlewVolume(int max_slew_time_ms) |
| : sample_rate_(kDefaultSampleRate), |
| max_slew_time_ms_(max_slew_time_ms), |
| - max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) { |
| - LOG(INFO) << "Creating a slew volume: " << max_slew_time_ms; |
| -} |
| + max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) {} |
| void SlewVolume::SetSampleRate(int sample_rate) { |
| + DCHECK_GT(sample_rate, 0); |
| + |
| sample_rate_ = sample_rate; |
| SetVolume(volume_scale_); |
| } |
| @@ -49,6 +140,8 @@ void SlewVolume::SetVolume(double volume_scale) { |
| } |
| void SlewVolume::SetMaxSlewTimeMs(int max_slew_time_ms) { |
| + DCHECK_GE(max_slew_time_ms, 0); |
| + |
| max_slew_time_ms_ = max_slew_time_ms; |
| } |
| @@ -61,112 +154,16 @@ void SlewVolume::ProcessFMAC(bool repeat_transition, |
| const float* src, |
| int frames, |
| float* dest) { |
| - DCHECK(src); |
| - DCHECK(dest); |
| - // Ensure |src| and |dest| are 16-byte aligned. |
| - DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & |
| - (::media::vector_math::kRequiredAlignment - 1)); |
| - DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & |
| - (::media::vector_math::kRequiredAlignment - 1)); |
| - |
| - if (!frames) { |
| - return; |
| - } |
| - |
| - interrupted_ = false; |
| - if (repeat_transition) { |
| - current_volume_ = last_starting_volume_; |
| - } else { |
| - last_starting_volume_ = current_volume_; |
| - } |
| - |
| - if (current_volume_ == volume_scale_) { |
| - if (current_volume_ == 0.0) { |
| - return; |
| - } |
| - ::media::vector_math::FMAC(src, current_volume_, frames, dest); |
| - return; |
| - } else if (current_volume_ < volume_scale_) { |
| - do { |
| - (*dest) += (*src) * current_volume_; |
| - ++src; |
| - ++dest; |
| - --frames; |
| - current_volume_ += max_slew_per_sample_; |
| - } while (current_volume_ < volume_scale_ && frames); |
| - current_volume_ = std::min(current_volume_, volume_scale_); |
| - } else { // current_volume_ > volume_scale_ |
| - do { |
| - (*dest) += (*src) * current_volume_; |
| - ++src; |
| - ++dest; |
| - --frames; |
| - current_volume_ -= max_slew_per_sample_; |
| - } while (current_volume_ > volume_scale_ && frames); |
| - current_volume_ = std::max(current_volume_, volume_scale_); |
| - } |
| - |
| - if (frames) { |
| - for (int f = 0; f < frames; ++f) { |
| - dest[f] += src[f] * current_volume_; |
| - } |
| - } |
| + PROCESS_DATA(BULK_OPERATOR_FMAC, SINGLE_OPERATOR_FMAC, ZERO_OPERATOR_FMAC, |
| + UNITY_OPERATOR_FMAC); |
| } |
| -// Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz |
| -// on pineapple. |
| -// Assumes 2 channel audio. |
| -bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) { |
| - DCHECK(data); |
| - |
| - if (!frames) { |
| - return true; |
| - } |
| - |
| - interrupted_ = false; |
| - if (current_volume_ == volume_scale_) { |
| - if (current_volume_ == 1.0) { |
| - return true; |
| - } |
| - for (int i = 0; i < 2 * frames; ++i) { |
| - data[i] *= current_volume_; |
| - } |
| - return true; |
| - } else if (current_volume_ < volume_scale_) { |
| - do { |
| - (*data) *= current_volume_; |
| - ++data; |
| - (*data) *= current_volume_; |
| - ++data; |
| - --frames; |
| - current_volume_ += max_slew_per_sample_; |
| - } while (current_volume_ < volume_scale_ && frames); |
| - current_volume_ = std::min(current_volume_, volume_scale_); |
| - } else { |
| - do { |
| - (*data) *= current_volume_; |
| - ++data; |
| - (*data) *= current_volume_; |
| - ++data; |
| - --frames; |
| - current_volume_ -= max_slew_per_sample_; |
| - } while (current_volume_ > volume_scale_ && frames); |
| - current_volume_ = std::max(current_volume_, volume_scale_); |
| - } |
| - |
| - if (current_volume_ == 1.0) { |
| - return true; |
| - } |
| - |
| - if (current_volume_ == 0.0) { |
| - std::fill_n(data, frames * 2, 0); |
| - return true; |
| - } |
| - |
| - for (int i = 0; i < 2 * frames; ++i) { |
| - data[i] *= current_volume_; |
| - } |
| - return true; |
| +void SlewVolume::ProcessFMUL(bool repeat_transition, |
| + const float* src, |
| + int frames, |
| + float* dest) { |
| + PROCESS_DATA(BULK_OPERATOR_FMUL, SINGLE_OPERATOR_FMUL, ZERO_OPERATOR_FMUL, |
| + UNITY_OPERATOR_FMUL); |
| } |
| } // namespace media |