OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/post_processors/governor.h" |
| 6 |
5 #include <memory> | 7 #include <memory> |
6 #include <string> | 8 #include <string> |
7 | 9 |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/macros.h" | |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chromecast/base/serializers.h" | 12 #include "chromecast/base/serializers.h" |
12 #include "chromecast/media/cma/backend/alsa/slew_volume.h" | 13 #include "chromecast/media/cma/backend/alsa/slew_volume.h" |
13 #include "chromecast/public/media/audio_post_processor_shlib.h" | |
14 | 14 |
15 namespace chromecast { | 15 namespace chromecast { |
16 namespace media { | 16 namespace media { |
17 | 17 |
18 namespace { | 18 namespace { |
19 const int kNoSampleRate = -1; | 19 const int kNoSampleRate = -1; |
| 20 const int kNoVolume = -1; |
20 | 21 |
21 // Configuration strings: | 22 // Configuration strings: |
22 const char kOnsetVolumeKey[] = "onset_volume"; | 23 const char kOnsetVolumeKey[] = "onset_volume"; |
23 const char kClampMultiplierKey[] = "clamp_multiplier"; | 24 const char kClampMultiplierKey[] = "clamp_multiplier"; |
24 | 25 |
25 } // namespace | 26 } // namespace |
26 | 27 |
27 // Provides a flat reduction in output volume if the input volume is above a | |
28 // given threshold. | |
29 // Used to protect speakers at high output levels | |
30 // while providing dyanmic range at low output level. | |
31 // The configuration string for this plugin is: | |
32 // {"onset_volume": |VOLUME_TO_CLAMP|, "clamp_multiplier": |CLAMP_MULTIPLIER|} | |
33 // Input volumes > |VOLUME_TO_CLAMP| will be attenuated by |CLAMP_MULTIPLIER|. | |
34 class Governor : public AudioPostProcessor { | |
35 public: | |
36 Governor(const std::string& config, int channels); | |
37 ~Governor() override; | |
38 | |
39 bool SetSampleRate(int sample_rate) override; | |
40 int ProcessFrames(const std::vector<float*>& data, | |
41 int frames, | |
42 float volume) override; | |
43 int GetRingingTimeInFrames() override; | |
44 | |
45 private: | |
46 float GetGovernorMultiplier(); | |
47 | |
48 int channels_; | |
49 int sample_rate_; | |
50 float volume_; | |
51 double onset_volume_; | |
52 double clamp_multiplier_; | |
53 SlewVolume governor_; | |
54 DISALLOW_COPY_AND_ASSIGN(Governor); | |
55 }; | |
56 | |
57 Governor::Governor(const std::string& config, int channels) | 28 Governor::Governor(const std::string& config, int channels) |
58 : channels_(channels), sample_rate_(kNoSampleRate), volume_(1.0) { | 29 : channels_(channels), |
| 30 sample_rate_(kNoSampleRate), |
| 31 volume_(kNoVolume), |
| 32 slew_volume_() { |
59 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config)); | 33 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config)); |
60 CHECK(config_dict) << "Governor config is not valid json: " << config; | 34 CHECK(config_dict) << "Governor config is not valid json: " << config; |
61 CHECK(config_dict->GetDouble(kOnsetVolumeKey, &onset_volume_)); | 35 CHECK(config_dict->GetDouble(kOnsetVolumeKey, &onset_volume_)); |
62 CHECK(config_dict->GetDouble(kClampMultiplierKey, &clamp_multiplier_)); | 36 CHECK(config_dict->GetDouble(kClampMultiplierKey, &clamp_multiplier_)); |
63 DCHECK_EQ(channels_, 2); | 37 DCHECK_EQ(channels_, 2); |
64 governor_.SetVolume(1.0); | 38 slew_volume_.SetVolume(1.0); |
65 LOG(INFO) << "Created a governor: onset_volume = " << onset_volume_ | 39 LOG(INFO) << "Created a governor: onset_volume = " << onset_volume_ |
66 << ", clamp_multiplier = " << clamp_multiplier_; | 40 << ", clamp_multiplier = " << clamp_multiplier_; |
67 } | 41 } |
68 | 42 |
69 Governor::~Governor() = default; | 43 Governor::~Governor() = default; |
70 | 44 |
71 bool Governor::SetSampleRate(int sample_rate) { | 45 bool Governor::SetSampleRate(int sample_rate) { |
72 sample_rate_ = sample_rate; | 46 sample_rate_ = sample_rate; |
73 governor_.SetSampleRate(sample_rate); | 47 slew_volume_.SetSampleRate(sample_rate); |
74 return true; | 48 return true; |
75 } | 49 } |
76 | 50 |
77 int Governor::ProcessFrames(const std::vector<float*>& data, | 51 int Governor::ProcessFrames(const std::vector<float*>& data, |
78 int frames, | 52 int frames, |
79 float volume) { | 53 float volume) { |
80 DCHECK_EQ(data.size(), static_cast<size_t>(channels_)); | 54 DCHECK_EQ(data.size(), static_cast<size_t>(channels_)); |
81 | 55 |
82 if (volume != volume_) { | 56 if (volume != volume_) { |
83 volume_ = volume; | 57 volume_ = volume; |
84 governor_.SetVolume(GetGovernorMultiplier()); | 58 slew_volume_.SetVolume(GetGovernorMultiplier()); |
85 } | 59 } |
86 | 60 |
87 for (int c = 0; c < channels_; ++c) { | 61 for (int c = 0; c < channels_; ++c) { |
88 DCHECK(data[c]); | 62 DCHECK(data[c]); |
89 governor_.ProcessFMAC(c != 0 /* repeat_transition */, data[c], frames, | 63 slew_volume_.ProcessFMUL(c != 0 /* repeat_transition */, data[c], frames, |
90 data[c]); | 64 data[c]); |
91 } | 65 } |
92 | 66 |
93 return 0; // No delay in this pipeline. | 67 return 0; // No delay in this pipeline. |
94 } | 68 } |
95 | 69 |
96 int Governor::GetRingingTimeInFrames() { | 70 int Governor::GetRingingTimeInFrames() { |
97 return 0; | 71 return 0; |
98 } | 72 } |
99 | 73 |
100 float Governor::GetGovernorMultiplier() { | 74 float Governor::GetGovernorMultiplier() { |
101 if (volume_ >= onset_volume_) { | 75 if (volume_ >= onset_volume_) { |
102 return clamp_multiplier_; | 76 return clamp_multiplier_; |
103 } | 77 } |
104 return 1.0; | 78 return 1.0; |
105 } | 79 } |
106 | 80 |
| 81 void Governor::SetSlewTimeMsForTest(int slew_time_ms) { |
| 82 slew_volume_.SetMaxSlewTimeMs(slew_time_ms); |
| 83 } |
| 84 |
107 } // namespace media | 85 } // namespace media |
108 } // namespace chromecast | 86 } // namespace chromecast |
109 | 87 |
110 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( | 88 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( |
111 const std::string& config, | 89 const std::string& config, |
112 int channels) { | 90 int channels) { |
113 return static_cast<chromecast::media::AudioPostProcessor*>( | 91 return static_cast<chromecast::media::AudioPostProcessor*>( |
114 new chromecast::media::Governor(config, channels)); | 92 new chromecast::media::Governor(config, channels)); |
115 } | 93 } |
OLD | NEW |