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

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

Issue 2958393002: Add a simple, safe gain PostProcessor. (Closed)
Patch Set: Combine cma/backend/alsa tests into a single target Created 3 years, 5 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 "chromecast/media/cma/backend/alsa/post_processors/saturated_gain.h"
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "base/logging.h"
11 #include "base/values.h"
12 #include "chromecast/base/serializers.h"
13 #include "chromecast/media/cma/backend/alsa/slew_volume.h"
14
15 namespace chromecast {
16 namespace media {
17
18 namespace {
19 const int kNoSampleRate = -1;
20
21 // Configuration strings:
22 const char kGainKey[] = "gain_db";
23
24 float DbFsToScale(float db) {
25 return std::pow(10, db / 20);
26 }
27
28 } // namespace
29
30 SaturatedGain::SaturatedGain(const std::string& config, int channels)
31 : channels_(channels), sample_rate_(kNoSampleRate), last_volume_(-1) {
32 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config));
33 CHECK(config_dict) << "SaturatedGain config is not valid json: " << config;
34 double gain_db;
35 CHECK(config_dict->GetDouble(kGainKey, &gain_db)) << config;
36 gain_ = DbFsToScale(gain_db);
37 LOG(INFO) << "Created a SaturatedGain: gain = " << gain_db;
38 }
39
40 SaturatedGain::~SaturatedGain() = default;
41
42 bool SaturatedGain::SetSampleRate(int sample_rate) {
43 sample_rate_ = sample_rate;
44 slew_volume_.SetSampleRate(sample_rate);
45 return true;
46 }
47
48 int SaturatedGain::ProcessFrames(float* data, int frames, float volume) {
49 if (volume != last_volume_) {
50 last_volume_ = volume;
51 // Don't apply more gain than attenuation.
52 float effective_gain = std::min(
53 1.0f / DbFsToScale(volume_map_.VolumeToDbFS(last_volume_)), gain_);
54 slew_volume_.SetVolume(effective_gain);
55 }
56
57 slew_volume_.ProcessFMUL(false, data, frames, channels_, data);
58
59 return 0; // No delay in this pipeline.
60 }
61
62 int SaturatedGain::GetRingingTimeInFrames() {
63 return 0;
64 }
65
66 } // namespace media
67 } // namespace chromecast
68
69 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create(
70 const std::string& config,
71 int channels) {
72 return new chromecast::media::SaturatedGain(config, channels);
73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698