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 // ::media::base::FMAC requires |src| to be 16-byte aligned | |
18 const int kRequiredAlignment = 16; | |
kmackay
2016/09/15 01:51:36
not used?
jyw
2016/09/15 22:16:22
Done.
| |
19 } | |
20 | |
21 namespace chromecast { | |
22 namespace media { | |
23 | |
24 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs, kMaxSlewTimeMs) {} | |
25 | |
26 SlewVolume::SlewVolume(int max_slew_time_up_ms, int max_slew_time_down_ms) | |
27 : max_slew_time_up_ms_(max_slew_time_up_ms), | |
28 max_slew_time_down_ms_(max_slew_time_down_ms), | |
29 max_slew_up_(1000.0 / (max_slew_time_up_ms * kDefaultSampleRate)), | |
30 max_slew_down_(1000.0 / (max_slew_time_down_ms * kDefaultSampleRate)) {} | |
31 | |
32 // Slew rate should be 1 / (slew_time * sample_rate) | |
33 void SlewVolume::SetSampleRate(int sample_rate) { | |
34 max_slew_up_ = (1000.0 / (max_slew_time_up_ms_ * sample_rate)); | |
35 max_slew_down_ = (1000.0 / (max_slew_time_down_ms_ * sample_rate)); | |
36 } | |
37 | |
38 void SlewVolume::SetVolume(double volume_scale) { | |
39 volume_scale_ = volume_scale; | |
40 } | |
41 | |
42 void SlewVolume::ProcessFMAC(const float* src, int frames, float* dest) { | |
43 DCHECK(src); | |
44 DCHECK(dest); | |
45 | |
46 if (!frames) { | |
47 return; | |
48 } | |
49 | |
50 if (current_volume_ == volume_scale_) { | |
51 if (current_volume_ == 0.0) { | |
52 return; | |
53 } | |
kmackay
2016/09/15 01:51:36
Include alignment requirements in the header comme
jyw
2016/09/15 22:16:23
Done.
| |
54 ::media::vector_math::FMAC(src, current_volume_, frames, dest); | |
55 return; | |
56 } else if (current_volume_ < volume_scale_) { | |
57 do { | |
58 (*dest) += (*src) * current_volume_; | |
59 ++src; | |
60 ++dest; | |
61 --frames; | |
62 current_volume_ += max_slew_up_; | |
63 } while (current_volume_ < volume_scale_ && frames); | |
64 current_volume_ = std::min(current_volume_, volume_scale_); | |
65 } else { // current_volume_ > volume_scale_ | |
66 do { | |
67 (*dest) += (*src) * current_volume_; | |
68 ++src; | |
69 ++dest; | |
70 --frames; | |
71 current_volume_ -= max_slew_down_; | |
72 } while (current_volume_ > volume_scale_ && frames); | |
73 current_volume_ = std::max(current_volume_, volume_scale_); | |
74 } | |
75 | |
76 if (frames) { | |
77 for (int f = 0; f < frames; ++f) { | |
78 dest[f] += src[f] * current_volume_; | |
79 } | |
80 } | |
81 } | |
82 | |
83 // Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz | |
84 // (profiled with waves_standalone). | |
85 // Assumes 2 channel audio. | |
86 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
| |
87 DCHECK(data); | |
88 | |
89 if (!frames) { | |
90 return true; | |
91 } | |
92 | |
93 if (current_volume_ == volume_scale_) { | |
94 if (current_volume_ == 1.0) { | |
95 return true; | |
96 } | |
97 for (int i = 0; i < 2 * frames; ++i) { | |
98 data[i] *= volume_scale_; | |
kmackay
2016/09/15 01:51:36
*= current_volume_, for consistency
jyw
2016/09/15 22:16:22
Done.
| |
99 } | |
100 return true; | |
101 } else if (current_volume_ < volume_scale_) { | |
102 do { | |
103 (*data) *= current_volume_; | |
104 ++data; | |
105 (*data) *= current_volume_; | |
106 ++data; | |
107 --frames; | |
108 current_volume_ += max_slew_up_; | |
109 } while (current_volume_ < volume_scale_ && frames); | |
110 current_volume_ = std::min(current_volume_, volume_scale_); | |
111 } else { | |
112 do { | |
113 (*data) *= current_volume_; | |
114 ++data; | |
115 (*data) *= current_volume_; | |
116 ++data; | |
117 --frames; | |
118 current_volume_ -= max_slew_down_; | |
119 } while (current_volume_ > volume_scale_ && frames); | |
120 current_volume_ = std::max(current_volume_, volume_scale_); | |
121 } | |
122 | |
123 if (current_volume_ == 1.0) { | |
124 return true; | |
125 } | |
126 | |
127 if (current_volume_ == 0.0) { | |
128 std::fill_n(data, frames * 2, 0); | |
129 return true; | |
130 } | |
131 | |
132 for (int i = 0; i < 2 * frames; ++i) { | |
133 data[i] *= current_volume_; | |
134 } | |
135 return true; | |
136 } | |
137 | |
138 } // namespace media | |
139 } // namespace chromecast | |
OLD | NEW |