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

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

Issue 2958393002: Add a simple, safe gain PostProcessor. (Closed)
Patch Set: Unrelated cleanup 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/volume_map.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chromecast/base/serializers.h"
12 #include "chromecast/media/cma/backend/alsa/cast_audio_json.h"
13
14 namespace chromecast {
15 namespace media {
16
17 namespace {
18 constexpr char kKeyVolumeMap[] = "volume_map";
19 constexpr char kKeyLevel[] = "level";
20 constexpr char kKeyDb[] = "db";
21 constexpr float kMinDbFS = -120.0f;
22
23 } // namespace
24
25 VolumeMap::VolumeMap() {
26 auto cast_audio_config =
27 DeserializeJsonFromFile(base::FilePath(kCastAudioJsonFilePath));
28 const base::DictionaryValue* cast_audio_dict;
29 if (!cast_audio_config ||
30 !cast_audio_config->GetAsDictionary(&cast_audio_dict)) {
31 LOG(WARNING) << "No cast audio config found; using default volume map.";
32 UseDefaultVolumeMap();
33 return;
34 }
35
36 const base::ListValue* volume_map_list;
37 if (!cast_audio_dict->GetList(kKeyVolumeMap, &volume_map_list)) {
38 LOG(WARNING) << "No volume map found; using default volume map.";
39 UseDefaultVolumeMap();
40 return;
41 }
42
43 double prev_level = -1.0;
44 for (size_t i = 0; i < volume_map_list->GetSize(); ++i) {
45 const base::DictionaryValue* volume_map_entry;
46 CHECK(volume_map_list->GetDictionary(i, &volume_map_entry));
47
48 double level;
49 CHECK(volume_map_entry->GetDouble(kKeyLevel, &level));
50 CHECK_GE(level, 0.0);
51 CHECK_LE(level, 1.0);
52 CHECK_GT(level, prev_level);
53 prev_level = level;
54
55 double db;
56 CHECK(volume_map_entry->GetDouble(kKeyDb, &db));
57 CHECK_LE(db, 0.0);
58 if (level == 1.0) {
59 CHECK_EQ(db, 0.0);
60 }
61
62 volume_map_.push_back({level, db});
63 }
64
65 if (volume_map_.empty()) {
66 LOG(FATAL) << "No entries in volume map.";
67 return;
68 }
69
70 if (volume_map_[0].level > 0.0) {
71 volume_map_.insert(volume_map_.begin(), {0.0, kMinDbFS});
72 }
73
74 if (volume_map_.rbegin()->level < 1.0) {
75 volume_map_.push_back({1.0, 0.0});
76 }
77 }
78
79 VolumeMap::~VolumeMap() = default;
80
81 float VolumeMap::VolumeToDbFS(float volume) {
82 if (volume <= volume_map_[0].level) {
83 return volume_map_[0].db;
84 }
85 for (size_t i = 1; i < volume_map_.size(); ++i) {
86 if (volume < volume_map_[i].level) {
87 const float x_range = volume_map_[i].level - volume_map_[i - 1].level;
88 const float y_range = volume_map_[i].db - volume_map_[i - 1].db;
89 const float x_pos = volume - volume_map_[i - 1].level;
90
91 return volume_map_[i - 1].db + x_pos * y_range / x_range;
92 }
93 }
94 return volume_map_[volume_map_.size() - 1].db;
95 }
96
97 float VolumeMap::DbFSToVolume(float db) {
98 if (db <= volume_map_[0].db) {
99 return volume_map_[0].level;
100 }
101 for (size_t i = 1; i < volume_map_.size(); ++i) {
102 if (db < volume_map_[i].db) {
103 const float x_range = volume_map_[i].db - volume_map_[i - 1].db;
104 const float y_range = volume_map_[i].level - volume_map_[i - 1].level;
105 const float x_pos = db - volume_map_[i - 1].db;
106
107 return volume_map_[i - 1].level + x_pos * y_range / x_range;
108 }
109 }
110 return volume_map_[volume_map_.size() - 1].level;
111 }
112
113 // static
114 void VolumeMap::UseDefaultVolumeMap() {
115 volume_map_ = {{0.0f, kMinDbFS},
116 {0.01f, -58.0f},
117 {0.090909f, -48.0f},
118 {0.818182f, -8.0f},
119 {1.0f, 0.0f}};
120 }
121
122 } // namespace media
123 } // namespace chromecast
OLDNEW
« no previous file with comments | « chromecast/media/cma/backend/alsa/volume_map.h ('k') | chromecast/media/service/cast_renderer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698