OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 #include <string> |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/values.h" |
| 11 #include "chromecast/base/serializers.h" |
| 12 #include "chromecast/media/cma/backend/alsa/slew_volume.h" |
| 13 #include "chromecast/public/media/audio_post_processor_shlib.h" |
| 14 |
| 15 namespace chromecast { |
| 16 namespace media { |
| 17 |
| 18 namespace { |
| 19 const int kNoSampleRate = -1; |
| 20 |
| 21 // Configuration strings: |
| 22 const char kOnsetVolumeKey[] = "onset_volume"; |
| 23 const char kClampMultiplierKey[] = "clamp_multiplier"; |
| 24 |
| 25 } // namespace |
| 26 |
| 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(uint8_t* data, int frames, float volume) override; |
| 41 int GetRingingTimeInFrames() override; |
| 42 |
| 43 private: |
| 44 float GetGovernorMultiplier(); |
| 45 |
| 46 int channels_; |
| 47 int sample_rate_; |
| 48 float volume_; |
| 49 double onset_volume_; |
| 50 double clamp_multiplier_; |
| 51 SlewVolume governor_; |
| 52 DISALLOW_COPY_AND_ASSIGN(Governor); |
| 53 }; |
| 54 |
| 55 Governor::Governor(const std::string& config, int channels) |
| 56 : channels_(channels), sample_rate_(kNoSampleRate), volume_(1.0) { |
| 57 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config)); |
| 58 CHECK(config_dict) << "Governor config is not valid json: " << config; |
| 59 CHECK(config_dict->GetDouble(kOnsetVolumeKey, &onset_volume_)); |
| 60 CHECK(config_dict->GetDouble(kClampMultiplierKey, &clamp_multiplier_)); |
| 61 DCHECK_EQ(channels_, 2); |
| 62 governor_.SetVolume(1.0); |
| 63 LOG(INFO) << "Created a governor: onset_volume = " << onset_volume_ |
| 64 << ", clamp_multiplier = " << clamp_multiplier_; |
| 65 } |
| 66 |
| 67 Governor::~Governor() = default; |
| 68 |
| 69 bool Governor::SetSampleRate(int sample_rate) { |
| 70 sample_rate_ = sample_rate; |
| 71 governor_.SetSampleRate(sample_rate); |
| 72 return true; |
| 73 } |
| 74 |
| 75 int Governor::ProcessFrames(uint8_t* data, int frames, float volume) { |
| 76 if (volume != volume_) { |
| 77 volume_ = volume; |
| 78 governor_.SetVolume(GetGovernorMultiplier()); |
| 79 } |
| 80 |
| 81 governor_.ProcessInterleaved(reinterpret_cast<int32_t*>(data), frames); |
| 82 return 0; // No delay in this pipeline. |
| 83 } |
| 84 |
| 85 int Governor::GetRingingTimeInFrames() { |
| 86 return 0; |
| 87 } |
| 88 |
| 89 float Governor::GetGovernorMultiplier() { |
| 90 if (volume_ >= onset_volume_) { |
| 91 return clamp_multiplier_; |
| 92 } |
| 93 return 1.0; |
| 94 } |
| 95 |
| 96 } // namespace media |
| 97 } // namespace chromecast |
| 98 |
| 99 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( |
| 100 const std::string& config, |
| 101 int channels) { |
| 102 return static_cast<chromecast::media::AudioPostProcessor*>( |
| 103 new chromecast::media::Governor(config, channels)); |
| 104 } |
OLD | NEW |