Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Side by Side Diff: chromecast/media/cma/backend/alsa/post_processors/governor_shlib.cc

Issue 2771143002: Implement runtime audio post-processing pipeline. See go/cast_audio.json (Closed)
Patch Set: Fix stray keystroke Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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();
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 governor_.SetVolume(1.0);
62 LOG(INFO) << "Created a governor: onset_volume = " << onset_volume_
63 << ", clamp_multiplier = " << clamp_multiplier_;
64 }
65
66 Governor::~Governor() = default;
67
68 bool Governor::SetSampleRate(int sample_rate) {
69 sample_rate_ = sample_rate;
70 governor_.SetSampleRate(sample_rate);
71 return true;
72 }
73
74 int Governor::ProcessFrames(uint8_t* data, int frames, float volume) {
75 if (volume != volume_) {
76 volume_ = volume;
77 governor_.SetVolume(GetGovernorMultiplier());
78 }
79
80 governor_.ProcessInterleaved(reinterpret_cast<int32_t*>(data), frames);
81 return 0; // No delay in this pipeline.
82 }
83
84 int Governor::GetRingingTimeInFrames() {
85 return 0;
86 }
87
88 float Governor::GetGovernorMultiplier() {
89 if (volume_ >= onset_volume_) {
90 return clamp_multiplier_;
91 }
92 return 1.0;
93 }
94
95 } // namespace media
96 } // namespace chromecast
97
98 std::unique_ptr<chromecast::media::AudioPostProcessor>
99 AudioPostProcessorShlib_Create(const std::string& config, int channels) {
100 return std::unique_ptr<chromecast::media::AudioPostProcessor>(
101 new chromecast::media::Governor(config, channels));
102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698