| 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 |
| 57 if (volume_ == kNoVolume) { |
| 58 slew_volume_->Interrupted(); |
| 59 } |
| 60 |
| 82 if (volume != volume_) { | 61 if (volume != volume_) { |
| 83 volume_ = volume; | 62 volume_ = volume; |
| 84 governor_.SetVolume(GetGovernorMultiplier()); | 63 slew_volume_->SetVolume(GetGovernorMultiplier()); |
| 85 } | 64 } |
| 86 | 65 |
| 87 for (int c = 0; c < channels_; ++c) { | 66 for (int c = 0; c < channels_; ++c) { |
| 88 DCHECK(data[c]); | 67 DCHECK(data[c]); |
| 89 governor_.ProcessFMAC(c != 0 /* repeat_transition */, data[c], frames, | 68 slew_volume_->ProcessFMUL(c != 0 /* repeat_transition */, data[c], frames, |
| 90 data[c]); | 69 data[c]); |
| 91 } | 70 } |
| 92 | 71 |
| 93 return 0; // No delay in this pipeline. | 72 return 0; // No delay in this pipeline. |
| 94 } | 73 } |
| 95 | 74 |
| 96 int Governor::GetRingingTimeInFrames() { | 75 int Governor::GetRingingTimeInFrames() { |
| 97 return 0; | 76 return 0; |
| 98 } | 77 } |
| 99 | 78 |
| 100 float Governor::GetGovernorMultiplier() { | 79 float Governor::GetGovernorMultiplier() { |
| 101 if (volume_ >= onset_volume_) { | 80 if (volume_ >= onset_volume_) { |
| 102 return clamp_multiplier_; | 81 return clamp_multiplier_; |
| 103 } | 82 } |
| 104 return 1.0; | 83 return 1.0; |
| 105 } | 84 } |
| 106 | 85 |
| 86 void Governor::SetSlewTimeMsForTest(int slew_time_ms) { |
| 87 slew_volume_->SetMaxSlewTimeMs(slew_time_ms); |
| 88 } |
| 89 |
| 107 } // namespace media | 90 } // namespace media |
| 108 } // namespace chromecast | 91 } // namespace chromecast |
| 109 | 92 |
| 110 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( | 93 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( |
| 111 const std::string& config, | 94 const std::string& config, |
| 112 int channels) { | 95 int channels) { |
| 113 return static_cast<chromecast::media::AudioPostProcessor*>( | 96 return static_cast<chromecast::media::AudioPostProcessor*>( |
| 114 new chromecast::media::Governor(config, channels)); | 97 new chromecast::media::Governor(config, channels)); |
| 115 } | 98 } |
| OLD | NEW |