OLD | NEW |
---|---|
1 // Copyright (c) 2011 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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 bool AudioHandler::IsInitialized() { | 52 bool AudioHandler::IsMixerInitialized() { |
53 return mixer_->IsInitialized(); | 53 return mixer_->IsInitialized(); |
54 } | 54 } |
55 | 55 |
56 // static | |
57 AudioHandler* AudioHandler::GetInitialized() { | |
58 return g_audio_handler && g_audio_handler->IsMixerInitialized() ? | |
59 g_audio_handler : NULL; | |
60 } | |
61 | |
56 double AudioHandler::GetVolumePercent() { | 62 double AudioHandler::GetVolumePercent() { |
57 return VolumeDbToPercent(mixer_->GetVolumeDb()); | 63 return VolumeDbToPercent(mixer_->GetVolumeDb()); |
58 } | 64 } |
59 | 65 |
60 void AudioHandler::SetVolumePercent(double volume_percent) { | 66 void AudioHandler::SetVolumePercent(double volume_percent) { |
61 volume_percent = min(max(volume_percent, 0.0), 100.0); | 67 volume_percent = min(max(volume_percent, 0.0), 100.0); |
62 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); | 68 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); |
69 VolumeChanged(); | |
Daniel Erat
2012/01/26 17:55:21
nit: unless you anticipate adding more code to Vol
xiyuan
2012/01/26 18:07:10
nit: If you want to keep this function, please ren
achuithb
2012/01/27 01:48:05
Done.
| |
63 } | 70 } |
64 | 71 |
65 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { | 72 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { |
66 const double old_volume_db = mixer_->GetVolumeDb(); | 73 const double old_volume_db = mixer_->GetVolumeDb(); |
67 const double old_percent = VolumeDbToPercent(old_volume_db); | 74 const double old_percent = VolumeDbToPercent(old_volume_db); |
68 SetVolumePercent(old_percent + adjust_by_percent); | 75 SetVolumePercent(old_percent + adjust_by_percent); |
69 } | 76 } |
70 | 77 |
71 bool AudioHandler::IsMuted() { | 78 bool AudioHandler::IsMuted() { |
72 return mixer_->IsMuted(); | 79 return mixer_->IsMuted(); |
73 } | 80 } |
74 | 81 |
75 void AudioHandler::SetMuted(bool mute) { | 82 void AudioHandler::SetMuted(bool mute) { |
76 mixer_->SetMuted(mute); | 83 mixer_->SetMuted(mute); |
84 VolumeChanged(); | |
77 } | 85 } |
78 | 86 |
79 AudioHandler::AudioHandler() | 87 AudioHandler::AudioHandler() |
80 : mixer_(new AudioMixerAlsa()) { | 88 : mixer_(new AudioMixerAlsa()) { |
81 mixer_->Init(); | 89 mixer_->Init(); |
82 } | 90 } |
83 | 91 |
84 AudioHandler::~AudioHandler() { | 92 AudioHandler::~AudioHandler() { |
85 mixer_.reset(); | 93 mixer_.reset(); |
86 }; | 94 }; |
(...skipping 19 matching lines...) Expand all Loading... | |
106 } | 114 } |
107 | 115 |
108 double AudioHandler::PercentToVolumeDb(double volume_percent) const { | 116 double AudioHandler::PercentToVolumeDb(double volume_percent) const { |
109 double min_volume_db, max_volume_db; | 117 double min_volume_db, max_volume_db; |
110 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); | 118 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); |
111 | 119 |
112 return pow(volume_percent / 100.0, kVolumeBias) * | 120 return pow(volume_percent / 100.0, kVolumeBias) * |
113 (max_volume_db - min_volume_db) + min_volume_db; | 121 (max_volume_db - min_volume_db) + min_volume_db; |
114 } | 122 } |
115 | 123 |
124 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) { | |
Daniel Erat
2012/01/26 17:55:21
please match the order from the header file
achuithb
2012/01/27 01:48:05
Done. These files have been moved to the bottom in
| |
125 volume_observers_.AddObserver(observer); | |
126 } | |
127 | |
128 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) { | |
129 volume_observers_.RemoveObserver(observer); | |
130 } | |
131 | |
132 void AudioHandler::VolumeChanged() { | |
133 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); | |
134 } | |
135 | |
136 AudioHandler::VolumeObserver::VolumeObserver() { | |
137 if (AudioHandler::GetInstance()) // Mixer may be uninitialized here. | |
Daniel Erat
2012/01/26 17:55:21
so after instantiating an object derived from Volu
achuithb
2012/01/26 23:19:34
The comment refers to the fact that we are using G
Daniel Erat
2012/01/26 23:53:28
My comment wasn't related to the comment in the co
achuithb
2012/01/27 00:16:54
We create the AudioHandler singleton before the st
| |
138 AudioHandler::GetInstance()->AddVolumeObserver(this); | |
139 } | |
140 | |
141 AudioHandler::VolumeObserver::~VolumeObserver() { | |
142 if (AudioHandler::GetInstance()) | |
143 AudioHandler::GetInstance()->RemoveVolumeObserver(this); | |
144 } | |
145 | |
116 } // namespace chromeos | 146 } // namespace chromeos |
OLD | NEW |