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

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: Move cast_audio.json filepath string to a separate file 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 #include <memory>
kmackay 2017/06/30 17:59:38 <memory> not used
bshaya 2017/07/06 21:04:44 Done.
10 #include <string>
11
12 #include "base/logging.h"
13 #include "base/values.h"
14 #include "chromecast/base/serializers.h"
15 #include "chromecast/media/cma/backend/alsa/slew_volume.h"
16
17 namespace chromecast {
18 namespace media {
19
20 namespace {
21 const int kNoSampleRate = -1;
22
23 // Configuration strings:
24 const char kGainKey[] = "gain_db";
25
26 float DbFsToScale(float db) {
27 return std::pow(10, db / 20);
28 }
29
30 } // namespace
31
32 SaturatedGain::SaturatedGain(const std::string& config, int channels)
33 : channels_(channels), sample_rate_(kNoSampleRate), last_volume_(-1) {
34 auto config_dict = base::DictionaryValue::From(DeserializeFromJson(config));
35 CHECK(config_dict) << "SaturatedGain config is not valid json: " << config;
36 double gain_db;
37 CHECK(config_dict->GetDouble(kGainKey, &gain_db));
38 gain_ = DbFsToScale(gain_db);
39 LOG(INFO) << "Created a SaturatedGain: gain = " << gain_db;
40 }
41
42 SaturatedGain::~SaturatedGain() = default;
43
44 bool SaturatedGain::SetSampleRate(int sample_rate) {
45 sample_rate_ = sample_rate;
46 slew_volume_.SetSampleRate(sample_rate);
47 return true;
48 }
49
50 int SaturatedGain::ProcessFrames(float* data, int frames, float volume) {
51 if (volume != last_volume_) {
52 last_volume_ = volume;
53 // Don't apply more gain than attenuation.
54 float effective_gain = std::min(
55 1.0f / DbFsToScale(volume_map_.VolumeToDbFS(last_volume_)), gain_);
56 slew_volume_.SetVolume(effective_gain);
57 }
58
59 slew_volume_.ProcessFMUL(false, data, frames, channels_, data);
60
61 return 0; // No delay in this pipeline.
62 }
63
64 int SaturatedGain::GetRingingTimeInFrames() {
65 return 0;
66 }
67
68 } // namespace media
69 } // namespace chromecast
70
71 chromecast::media::AudioPostProcessor* AudioPostProcessorShlib_Create(
72 const std::string& config,
73 int channels) {
74 return static_cast<chromecast::media::AudioPostProcessor*>(
kmackay 2017/06/30 17:59:38 Why do you need static_cast?
bshaya 2017/07/06 21:04:44 Thought bcf told me I needed it in a similar CL. G
75 new chromecast::media::SaturatedGain(config, channels));
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698