OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/media/cma/backend/alsa/slew_volume.h" | 5 #include "chromecast/media/cma/backend/alsa/slew_volume.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "media/base/vector_math.h" | 11 #include "media/base/vector_math.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // The time to slew from current volume to target volume. | 15 // The time to slew from current volume to target volume. |
15 const int kMaxSlewTimeMs = 100; | 16 const int kMaxSlewTimeMs = 100; |
16 const int kDefaultSampleRate = 44100; | 17 const int kDefaultSampleRate = 44100; |
17 } | 18 |
| 19 } // namespace |
| 20 |
| 21 struct FMACTraits { |
| 22 static void ProcessBulkData(const float* src, |
| 23 float volume, |
| 24 int frames, |
| 25 float* dest) { |
| 26 ::media::vector_math::FMAC(src, volume, frames, dest); |
| 27 } |
| 28 |
| 29 static void ProcessSingleDatum(const float* src, float volume, float* dest) { |
| 30 (*dest) += (*src) * volume; |
| 31 } |
| 32 |
| 33 static void ProcessZeroVolume(const float* src, int frames, float* dest) {} |
| 34 |
| 35 static void ProcessUnityVolume(const float* src, int frames, float* dest) { |
| 36 ProcessBulkData(src, 1.0, frames, dest); |
| 37 } |
| 38 }; |
| 39 |
| 40 struct FMULTraits { |
| 41 static void ProcessBulkData(const float* src, |
| 42 float volume, |
| 43 int frames, |
| 44 float* dest) { |
| 45 ::media::vector_math::FMUL(src, volume, frames, dest); |
| 46 } |
| 47 |
| 48 static void ProcessSingleDatum(const float* src, float volume, float* dest) { |
| 49 (*dest) = (*src) * volume; |
| 50 } |
| 51 |
| 52 static void ProcessZeroVolume(const float* src, int frames, float* dest) { |
| 53 std::memset(dest, 0, frames * sizeof(*dest)); |
| 54 } |
| 55 |
| 56 static void ProcessUnityVolume(const float* src, int frames, float* dest) { |
| 57 if (src == dest) { |
| 58 return; |
| 59 } |
| 60 std::memcpy(dest, src, frames * sizeof(*dest)); |
| 61 } |
| 62 }; |
18 | 63 |
19 namespace chromecast { | 64 namespace chromecast { |
20 namespace media { | 65 namespace media { |
21 | 66 |
22 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs) {} | 67 SlewVolume::SlewVolume() : SlewVolume(kMaxSlewTimeMs) {} |
23 | 68 |
24 SlewVolume::SlewVolume(int max_slew_time_ms) | 69 SlewVolume::SlewVolume(int max_slew_time_ms) |
25 : sample_rate_(kDefaultSampleRate), | 70 : sample_rate_(kDefaultSampleRate), |
26 max_slew_time_ms_(max_slew_time_ms), | 71 max_slew_time_ms_(max_slew_time_ms), |
27 max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) { | 72 max_slew_per_sample_(1000.0 / (max_slew_time_ms_ * sample_rate_)) {} |
28 LOG(INFO) << "Creating a slew volume: " << max_slew_time_ms; | |
29 } | |
30 | 73 |
31 void SlewVolume::SetSampleRate(int sample_rate) { | 74 void SlewVolume::SetSampleRate(int sample_rate) { |
| 75 DCHECK_GT(sample_rate, 0); |
| 76 |
32 sample_rate_ = sample_rate; | 77 sample_rate_ = sample_rate; |
33 SetVolume(volume_scale_); | 78 SetVolume(volume_scale_); |
34 } | 79 } |
35 | 80 |
36 // Slew rate should be volume_to_slew / slew_time / sample_rate | 81 // Slew rate should be volume_to_slew / slew_time / sample_rate |
37 void SlewVolume::SetVolume(double volume_scale) { | 82 void SlewVolume::SetVolume(double volume_scale) { |
38 volume_scale_ = volume_scale; | 83 volume_scale_ = volume_scale; |
39 if (interrupted_) { | 84 if (interrupted_) { |
40 current_volume_ = volume_scale_; | 85 current_volume_ = volume_scale_; |
41 } | 86 } |
42 if (volume_scale_ > current_volume_) { | 87 if (volume_scale_ > current_volume_) { |
43 max_slew_per_sample_ = (volume_scale_ - current_volume_) * 1000.0 / | 88 max_slew_per_sample_ = (volume_scale_ - current_volume_) * 1000.0 / |
44 (max_slew_time_ms_ * sample_rate_); | 89 (max_slew_time_ms_ * sample_rate_); |
45 } else { | 90 } else { |
46 max_slew_per_sample_ = (current_volume_ - volume_scale_) * 1000.0 / | 91 max_slew_per_sample_ = (current_volume_ - volume_scale_) * 1000.0 / |
47 (max_slew_time_ms_ * sample_rate_); | 92 (max_slew_time_ms_ * sample_rate_); |
48 } | 93 } |
49 } | 94 } |
50 | 95 |
51 void SlewVolume::SetMaxSlewTimeMs(int max_slew_time_ms) { | 96 void SlewVolume::SetMaxSlewTimeMs(int max_slew_time_ms) { |
| 97 DCHECK_GE(max_slew_time_ms, 0); |
| 98 |
52 max_slew_time_ms_ = max_slew_time_ms; | 99 max_slew_time_ms_ = max_slew_time_ms; |
53 } | 100 } |
54 | 101 |
55 void SlewVolume::Interrupted() { | 102 void SlewVolume::Interrupted() { |
56 interrupted_ = true; | 103 interrupted_ = true; |
57 current_volume_ = volume_scale_; | 104 current_volume_ = volume_scale_; |
58 } | 105 } |
59 | 106 |
60 void SlewVolume::ProcessFMAC(bool repeat_transition, | 107 void SlewVolume::ProcessFMAC(bool repeat_transition, |
61 const float* src, | 108 const float* src, |
62 int frames, | 109 int frames, |
63 float* dest) { | 110 float* dest) { |
| 111 ProcessData<FMACTraits>(repeat_transition, src, frames, dest); |
| 112 } |
| 113 |
| 114 void SlewVolume::ProcessFMUL(bool repeat_transition, |
| 115 const float* src, |
| 116 int frames, |
| 117 float* dest) { |
| 118 ProcessData<FMULTraits>(repeat_transition, src, frames, dest); |
| 119 } |
| 120 |
| 121 template <typename Traits> |
| 122 void SlewVolume::ProcessData(bool repeat_transition, |
| 123 const float* src, |
| 124 int frames, |
| 125 float* dest) { |
64 DCHECK(src); | 126 DCHECK(src); |
65 DCHECK(dest); | 127 DCHECK(dest); |
66 // Ensure |src| and |dest| are 16-byte aligned. | 128 // Ensure |src| and |dest| are 16-byte aligned. |
67 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & | 129 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(src) & |
68 (::media::vector_math::kRequiredAlignment - 1)); | 130 (::media::vector_math::kRequiredAlignment - 1)); |
69 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & | 131 DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(dest) & |
70 (::media::vector_math::kRequiredAlignment - 1)); | 132 (::media::vector_math::kRequiredAlignment - 1)); |
71 | 133 |
72 if (!frames) { | 134 if (!frames) { |
73 return; | 135 return; |
74 } | 136 } |
75 | 137 |
76 interrupted_ = false; | 138 interrupted_ = false; |
77 if (repeat_transition) { | 139 if (repeat_transition) { |
78 current_volume_ = last_starting_volume_; | 140 current_volume_ = last_starting_volume_; |
79 } else { | 141 } else { |
80 last_starting_volume_ = current_volume_; | 142 last_starting_volume_ = current_volume_; |
81 } | 143 } |
82 | 144 |
83 if (current_volume_ == volume_scale_) { | 145 if (current_volume_ == volume_scale_) { |
84 if (current_volume_ == 0.0) { | 146 if (current_volume_ == 0.0) { |
| 147 Traits::ProcessZeroVolume(src, frames, dest); |
85 return; | 148 return; |
86 } | 149 } |
87 ::media::vector_math::FMAC(src, current_volume_, frames, dest); | 150 if (current_volume_ == 1.0) { |
| 151 Traits::ProcessUnityVolume(src, frames, dest); |
| 152 return; |
| 153 } |
| 154 Traits::ProcessBulkData(src, current_volume_, frames, dest); |
88 return; | 155 return; |
89 } else if (current_volume_ < volume_scale_) { | 156 } |
| 157 |
| 158 if (current_volume_ < volume_scale_) { |
90 do { | 159 do { |
91 (*dest) += (*src) * current_volume_; | 160 Traits::ProcessSingleDatum(src, current_volume_, dest); |
92 ++src; | 161 ++src; |
93 ++dest; | 162 ++dest; |
94 --frames; | 163 --frames; |
95 current_volume_ += max_slew_per_sample_; | 164 current_volume_ += max_slew_per_sample_; |
96 } while (current_volume_ < volume_scale_ && frames); | 165 } while (current_volume_ < volume_scale_ && frames); |
97 current_volume_ = std::min(current_volume_, volume_scale_); | 166 current_volume_ = std::min(current_volume_, volume_scale_); |
98 } else { // current_volume_ > volume_scale_ | 167 } else { // current_volume_ > volume_scale_ |
99 do { | 168 do { |
100 (*dest) += (*src) * current_volume_; | 169 Traits::ProcessSingleDatum(src, current_volume_, dest); |
101 ++src; | 170 ++src; |
102 ++dest; | 171 ++dest; |
103 --frames; | 172 --frames; |
104 current_volume_ -= max_slew_per_sample_; | 173 current_volume_ -= max_slew_per_sample_; |
105 } while (current_volume_ > volume_scale_ && frames); | 174 } while (current_volume_ > volume_scale_ && frames); |
106 current_volume_ = std::max(current_volume_, volume_scale_); | 175 current_volume_ = std::max(current_volume_, volume_scale_); |
107 } | 176 } |
108 | 177 while (frames && (reinterpret_cast<uintptr_t>(src) & |
109 if (frames) { | 178 (::media::vector_math::kRequiredAlignment - 1))) { |
110 for (int f = 0; f < frames; ++f) { | 179 Traits::ProcessSingleDatum(src, current_volume_, dest); |
111 dest[f] += src[f] * current_volume_; | 180 ++src; |
112 } | 181 ++dest; |
| 182 --frames; |
113 } | 183 } |
114 } | |
115 | |
116 // Scaling samples naively like this takes 0.2% of the CPU's time @ 44100hz | |
117 // on pineapple. | |
118 // Assumes 2 channel audio. | |
119 bool SlewVolume::ProcessInterleaved(int32_t* data, int frames) { | |
120 DCHECK(data); | |
121 | |
122 if (!frames) { | 184 if (!frames) { |
123 return true; | 185 return; |
124 } | 186 } |
125 | 187 Traits::ProcessBulkData(src, current_volume_, frames, dest); |
126 interrupted_ = false; | |
127 if (current_volume_ == volume_scale_) { | |
128 if (current_volume_ == 1.0) { | |
129 return true; | |
130 } | |
131 for (int i = 0; i < 2 * frames; ++i) { | |
132 data[i] *= current_volume_; | |
133 } | |
134 return true; | |
135 } else if (current_volume_ < volume_scale_) { | |
136 do { | |
137 (*data) *= current_volume_; | |
138 ++data; | |
139 (*data) *= current_volume_; | |
140 ++data; | |
141 --frames; | |
142 current_volume_ += max_slew_per_sample_; | |
143 } while (current_volume_ < volume_scale_ && frames); | |
144 current_volume_ = std::min(current_volume_, volume_scale_); | |
145 } else { | |
146 do { | |
147 (*data) *= current_volume_; | |
148 ++data; | |
149 (*data) *= current_volume_; | |
150 ++data; | |
151 --frames; | |
152 current_volume_ -= max_slew_per_sample_; | |
153 } while (current_volume_ > volume_scale_ && frames); | |
154 current_volume_ = std::max(current_volume_, volume_scale_); | |
155 } | |
156 | |
157 if (current_volume_ == 1.0) { | |
158 return true; | |
159 } | |
160 | |
161 if (current_volume_ == 0.0) { | |
162 std::fill_n(data, frames * 2, 0); | |
163 return true; | |
164 } | |
165 | |
166 for (int i = 0; i < 2 * frames; ++i) { | |
167 data[i] *= current_volume_; | |
168 } | |
169 return true; | |
170 } | 188 } |
171 | 189 |
172 } // namespace media | 190 } // namespace media |
173 } // namespace chromecast | 191 } // namespace chromecast |
OLD | NEW |