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

Side by Side Diff: chromecast/media/cma/backend/alsa/slew_volume.cc

Issue 2341783004: [chromecast] Slew stream volume changes in StreamMixerAlsa. (Closed)
Patch Set: fix unittests 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 unified diff | Download patch
OLDNEW
(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)
wzhong 2016/09/19 04:48:13 Please add period.
31 void SlewVolume::SetSampleRate(int sample_rate) {
32 max_slew_up_ = 1000.0 / (max_slew_time_up_ms_ * sample_rate);
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);
45 DCHECK(dest);
46 // Ensure |src| and |dest| are 16-byte aligned.
47 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) &
48 (::media::vector_math::kRequiredAlignment - 1));
49 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) &
50 (::media::vector_math::kRequiredAlignment - 1));
51
52 if (!frames) {
53 return;
54 }
55
56 if (repeat_transition) {
57 current_volume_ = last_starting_volume_;
58 } else {
59 last_starting_volume_ = current_volume_;
60 }
61
62 if (current_volume_ == volume_scale_) {
63 if (current_volume_ == 0.0) {
64 return;
65 }
66 ::media::vector_math::FMAC(src, current_volume_, frames, dest);
67 return;
68 } else if (current_volume_ < volume_scale_) {
wzhong 2016/09/19 04:48:13 Nit: else is not needed.
69 do {
70 (*dest) += (*src) * current_volume_;
71 ++src;
72 ++dest;
73 --frames;
74 current_volume_ += max_slew_up_;
75 } while (current_volume_ < volume_scale_ && frames);
76 current_volume_ = std::min(current_volume_, volume_scale_);
77 } else { // current_volume_ > volume_scale_
78 do {
79 (*dest) += (*src) * current_volume_;
80 ++src;
81 ++dest;
82 --frames;
83 current_volume_ -= max_slew_down_;
84 } while (current_volume_ > volume_scale_ && frames);
85 current_volume_ = std::max(current_volume_, volume_scale_);
86 }
87
88 if (frames) {
89 for (int f = 0; f < frames; ++f) {
90 dest[f] += src[f] * current_volume_;
91 }
92 }
93 }
94
95 // Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz
96 // on pineapple.
97 // Assumes 2 channel audio.
98 bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) {
99 DCHECK(data);
100
101 if (!frames) {
102 return true;
103 }
104
105 if (current_volume_ == volume_scale_) {
106 if (current_volume_ == 1.0) {
107 return true;
108 }
109 for (int i = 0; i < 2 * frames; ++i) {
110 data[i] *= current_volume_;
111 }
112 return true;
113 } else if (current_volume_ < volume_scale_) {
wzhong 2016/09/19 04:48:13 Ditto.
114 do {
115 (*data) *= current_volume_;
116 ++data;
117 (*data) *= current_volume_;
118 ++data;
119 --frames;
120 current_volume_ += max_slew_up_;
121 } while (current_volume_ < volume_scale_ && frames);
122 current_volume_ = std::min(current_volume_, volume_scale_);
123 } else {
124 do {
125 (*data) *= current_volume_;
126 ++data;
127 (*data) *= current_volume_;
128 ++data;
129 --frames;
130 current_volume_ -= max_slew_down_;
131 } while (current_volume_ > volume_scale_ && frames);
132 current_volume_ = std::max(current_volume_, volume_scale_);
133 }
134
135 if (current_volume_ == 1.0) {
136 return true;
137 }
138
139 if (current_volume_ == 0.0) {
140 std::fill_n(data, frames * 2, 0);
141 return true;
142 }
143
144 for (int i = 0; i < 2 * frames; ++i) {
145 data[i] *= current_volume_;
146 }
147 return true;
148 }
149
150 } // namespace media
151 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/media/cma/backend/alsa/slew_volume.h ('k') | chromecast/media/cma/backend/alsa/stream_mixer_alsa.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698