Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: chromecast/media/cma/backend/alsa/slew_volume.cc

Issue 2341783004: [chromecast] Slew stream volume changes in StreamMixerAlsa. (Closed)
Patch Set: address comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..cce02f0b0f3d985788aa8d676ad54f3e768845d9
--- /dev/null
+++ b/chromecast/media/cma/backend/alsa/slew_volume.cc
@@ -0,0 +1,139 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromecast/media/cma/backend/alsa/slew_volume.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+#include "media/base/vector_math.h"
+
+namespace {
+
+// The time to slew from 0.0 to 1.0.
+const int kMaxSlewTimeMs = 100;
+const int kDefaultSampleRate = 44100;
+// ::media::base::FMAC requires |src| to be 16-byte aligned
+const int kRequiredAlignment = 16;
kmackay 2016/09/15 01:51:36 not used?
jyw 2016/09/15 22:16:22 Done.
+}
+
+namespace chromecast {
+namespace media {
+
+SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs, kMaxSlewTimeMs) {}
+
+SlewVolume::SlewVolume(int max_slew_time_up_ms, int max_slew_time_down_ms)
+ : max_slew_time_up_ms_(max_slew_time_up_ms),
+ max_slew_time_down_ms_(max_slew_time_down_ms),
+ max_slew_up_(1000.0 / (max_slew_time_up_ms * kDefaultSampleRate)),
+ max_slew_down_(1000.0 / (max_slew_time_down_ms * kDefaultSampleRate)) {}
+
+// Slew rate should be 1 / (slew_time * sample_rate)
+void SlewVolume::SetSampleRate(int sample_rate) {
+ max_slew_up_ = (1000.0 / (max_slew_time_up_ms_ * sample_rate));
+ max_slew_down_ = (1000.0 / (max_slew_time_down_ms_ * sample_rate));
+}
+
+void SlewVolume::SetVolume(double volume_scale) {
+ volume_scale_ = volume_scale;
+}
+
+void SlewVolume::ProcessFMAC(const float* src, int frames, float* dest) {
+ DCHECK(src);
+ DCHECK(dest);
+
+ if (!frames) {
+ return;
+ }
+
+ if (current_volume_ == volume_scale_) {
+ if (current_volume_ == 0.0) {
+ return;
+ }
kmackay 2016/09/15 01:51:36 Include alignment requirements in the header comme
jyw 2016/09/15 22:16:23 Done.
+ ::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_up_;
+ } 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_down_;
+ } 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_;
+ }
+ }
+}
+
+// Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz
+// (profiled with waves_standalone).
+// Assumes 2 channel audio.
+bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) {
kmackay 2016/09/15 01:51:36 this one also only ever returns true.
jyw 2016/09/15 22:16:22 There's an internal callsite for ProcessInterleave
+ DCHECK(data);
+
+ if (!frames) {
+ return true;
+ }
+
+ if (current_volume_ == volume_scale_) {
+ if (current_volume_ == 1.0) {
+ return true;
+ }
+ for (int i = 0; i < 2 * frames; ++i) {
+ data[i] *= volume_scale_;
kmackay 2016/09/15 01:51:36 *= current_volume_, for consistency
jyw 2016/09/15 22:16:22 Done.
+ }
+ return true;
+ } else if (current_volume_ < volume_scale_) {
+ do {
+ (*data) *= current_volume_;
+ ++data;
+ (*data) *= current_volume_;
+ ++data;
+ --frames;
+ current_volume_ += max_slew_up_;
+ } 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_down_;
+ } 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;
+}
+
+} // namespace media
+} // namespace chromecast

Powered by Google App Engine
This is Rietveld 408576698