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