OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/audio/audio_handler.h" | 5 #include "chrome/browser/chromeos/audio/audio_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
(...skipping 30 matching lines...) Expand all Loading... |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 // static | 45 // static |
46 AudioHandler* AudioHandler::GetInstance() { | 46 AudioHandler* AudioHandler::GetInstance() { |
47 VLOG_IF(1, !g_audio_handler) | 47 VLOG_IF(1, !g_audio_handler) |
48 << "AudioHandler::GetInstance() called with NULL global instance."; | 48 << "AudioHandler::GetInstance() called with NULL global instance."; |
49 return g_audio_handler; | 49 return g_audio_handler; |
50 } | 50 } |
51 | 51 |
52 // static | 52 bool AudioHandler::IsInitialized() { |
53 AudioHandler* AudioHandler::GetInstanceIfInitialized() { | 53 return mixer_->IsInitialized(); |
54 return g_audio_handler && g_audio_handler->IsMixerInitialized() ? | |
55 g_audio_handler : NULL; | |
56 } | 54 } |
57 | 55 |
58 double AudioHandler::GetVolumePercent() { | 56 double AudioHandler::GetVolumePercent() { |
59 return VolumeDbToPercent(mixer_->GetVolumeDb()); | 57 return VolumeDbToPercent(mixer_->GetVolumeDb()); |
60 } | 58 } |
61 | 59 |
62 void AudioHandler::SetVolumePercent(double volume_percent) { | 60 void AudioHandler::SetVolumePercent(double volume_percent) { |
63 volume_percent = min(max(volume_percent, 0.0), 100.0); | 61 volume_percent = min(max(volume_percent, 0.0), 100.0); |
64 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); | 62 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); |
65 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); | |
66 } | 63 } |
67 | 64 |
68 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { | 65 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { |
69 const double old_volume_db = mixer_->GetVolumeDb(); | 66 const double old_volume_db = mixer_->GetVolumeDb(); |
70 const double old_percent = VolumeDbToPercent(old_volume_db); | 67 const double old_percent = VolumeDbToPercent(old_volume_db); |
71 SetVolumePercent(old_percent + adjust_by_percent); | 68 SetVolumePercent(old_percent + adjust_by_percent); |
72 } | 69 } |
73 | 70 |
74 bool AudioHandler::IsMuted() { | 71 bool AudioHandler::IsMuted() { |
75 return mixer_->IsMuted(); | 72 return mixer_->IsMuted(); |
76 } | 73 } |
77 | 74 |
78 void AudioHandler::SetMuted(bool mute) { | 75 void AudioHandler::SetMuted(bool mute) { |
79 mixer_->SetMuted(mute); | 76 mixer_->SetMuted(mute); |
80 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); | |
81 } | |
82 | |
83 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) { | |
84 volume_observers_.AddObserver(observer); | |
85 } | |
86 | |
87 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) { | |
88 volume_observers_.RemoveObserver(observer); | |
89 } | 77 } |
90 | 78 |
91 AudioHandler::AudioHandler() | 79 AudioHandler::AudioHandler() |
92 : mixer_(new AudioMixerAlsa()) { | 80 : mixer_(new AudioMixerAlsa()) { |
93 mixer_->Init(); | 81 mixer_->Init(); |
94 } | 82 } |
95 | 83 |
96 AudioHandler::~AudioHandler() { | 84 AudioHandler::~AudioHandler() { |
97 mixer_.reset(); | 85 mixer_.reset(); |
98 }; | 86 }; |
99 | 87 |
100 bool AudioHandler::IsMixerInitialized() { | |
101 return mixer_->IsInitialized(); | |
102 } | |
103 | |
104 // VolumeDbToPercent() and PercentToVolumeDb() conversion functions allow us | 88 // VolumeDbToPercent() and PercentToVolumeDb() conversion functions allow us |
105 // complete control over how the 0 to 100% range is mapped to actual loudness. | 89 // complete control over how the 0 to 100% range is mapped to actual loudness. |
106 // | 90 // |
107 // The mapping is confined to these two functions to make it easy to adjust and | 91 // The mapping is confined to these two functions to make it easy to adjust and |
108 // have everything else just work. The range is biased to give finer resolution | 92 // have everything else just work. The range is biased to give finer resolution |
109 // in the higher volumes if kVolumeBias is less than 1.0. | 93 // in the higher volumes if kVolumeBias is less than 1.0. |
110 | 94 |
111 double AudioHandler::VolumeDbToPercent(double volume_db) const { | 95 double AudioHandler::VolumeDbToPercent(double volume_db) const { |
112 double min_volume_db, max_volume_db; | 96 double min_volume_db, max_volume_db; |
113 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); | 97 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); |
114 | 98 |
115 if (volume_db < min_volume_db) | 99 if (volume_db < min_volume_db) |
116 return 0.0; | 100 return 0.0; |
117 // TODO(derat): Choose a better mapping between percent and decibels. The | 101 // TODO(derat): Choose a better mapping between percent and decibels. The |
118 // bottom twenty-five percent or so is useless on a CR-48's internal speakers; | 102 // bottom twenty-five percent or so is useless on a CR-48's internal speakers; |
119 // it's all inaudible. | 103 // it's all inaudible. |
120 return 100.0 * pow((volume_db - min_volume_db) / | 104 return 100.0 * pow((volume_db - min_volume_db) / |
121 (max_volume_db - min_volume_db), 1/kVolumeBias); | 105 (max_volume_db - min_volume_db), 1/kVolumeBias); |
122 } | 106 } |
123 | 107 |
124 double AudioHandler::PercentToVolumeDb(double volume_percent) const { | 108 double AudioHandler::PercentToVolumeDb(double volume_percent) const { |
125 double min_volume_db, max_volume_db; | 109 double min_volume_db, max_volume_db; |
126 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); | 110 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); |
127 | 111 |
128 return pow(volume_percent / 100.0, kVolumeBias) * | 112 return pow(volume_percent / 100.0, kVolumeBias) * |
129 (max_volume_db - min_volume_db) + min_volume_db; | 113 (max_volume_db - min_volume_db) + min_volume_db; |
130 } | 114 } |
131 | 115 |
132 } // namespace chromeos | 116 } // namespace chromeos |
OLD | NEW |