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(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) | |
58 : channels_(channels), sample_rate_(kNoSampleRate), volume_(1.0) { | |
59 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config)); | |
60 CHECK(config_dict) << "Governor config is not valid json: " << config; | |
61 CHECK(config_dict->GetDouble(kOnsetVolumeKey, &onset_volume_)); | |
62 CHECK(config_dict->GetDouble(kClampMultiplierKey, &clamp_multiplier_)); | |
63 DCHECK_EQ(channels_, 2); | |
64 governor_.SetVolume(1.0); | |
65 LOG(INFO) << "Created a governor: onset_volume = " << onset_volume_ | |
66 << ", clamp_multiplier = " << clamp_multiplier_; | |
67 } | |
68 | |
69 Governor::~Governor() = default; | |
70 | |
71 bool Governor::SetSampleRate(int sample_rate) { | |
72 sample_rate_ = sample_rate; | |
73 governor_.SetSampleRate(sample_rate); | |
74 return true; | |
75 } | |
76 | |
77 int Governor::ProcessFrames(const std::vector<float*>& data, | |
78 int frames, | |
79 float volume) { | |
80 DCHECK_EQ(data.size(), static_cast<size_t>(channels_)); | |
81 | |
82 if (volume != volume_) { | |
83 volume_ = volume; | |
84 governor_.SetVolume(GetGovernorMultiplier()); | |
85 } | |
86 | |
87 for (int c = 0; c < channels_; ++c) { | |
88 DCHECK(data[c]); | |
89 governor_.ProcessFMAC(c != 0 /* repeat_transition */, data[c], frames, | |
90 data[c]); | |
91 } | |
92 | |
93 return 0; // No delay in this pipeline. | |
94 } | |
95 | |
96 int Governor::GetRingingTimeInFrames() { | |
97 return 0; | |
98 } | |
99 | |
100 float Governor::GetGovernorMultiplier() { | |
101 if (volume_ >= onset_volume_) { | |
102 return clamp_multiplier_; | |
103 } | |
104 return 1.0; | |
105 } | |
106 | |
107 } // namespace media | |
108 } // namespace chromecast | |
109 | |
110 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create( | |
111 const std::string& config, | |
112 int channels) { | |
113 return static_cast<chromecast::media::AudioPostProcessor*>( | |
114 new chromecast::media::Governor(config, channels)); | |
115 } | |
OLD | NEW |