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 // static |
53 return mixer_->IsInitialized(); | 53 AudioHandler* AudioHandler::GetInstanceIfInitialized() { |
| 54 return g_audio_handler && g_audio_handler->IsMixerInitialized() ? |
| 55 g_audio_handler : NULL; |
54 } | 56 } |
55 | 57 |
56 double AudioHandler::GetVolumePercent() { | 58 double AudioHandler::GetVolumePercent() { |
57 return VolumeDbToPercent(mixer_->GetVolumeDb()); | 59 return VolumeDbToPercent(mixer_->GetVolumeDb()); |
58 } | 60 } |
59 | 61 |
60 void AudioHandler::SetVolumePercent(double volume_percent) { | 62 void AudioHandler::SetVolumePercent(double volume_percent) { |
61 volume_percent = min(max(volume_percent, 0.0), 100.0); | 63 volume_percent = min(max(volume_percent, 0.0), 100.0); |
62 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); | 64 mixer_->SetVolumeDb(PercentToVolumeDb(volume_percent)); |
| 65 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); |
63 } | 66 } |
64 | 67 |
65 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { | 68 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { |
66 const double old_volume_db = mixer_->GetVolumeDb(); | 69 const double old_volume_db = mixer_->GetVolumeDb(); |
67 const double old_percent = VolumeDbToPercent(old_volume_db); | 70 const double old_percent = VolumeDbToPercent(old_volume_db); |
68 SetVolumePercent(old_percent + adjust_by_percent); | 71 SetVolumePercent(old_percent + adjust_by_percent); |
69 } | 72 } |
70 | 73 |
71 bool AudioHandler::IsMuted() { | 74 bool AudioHandler::IsMuted() { |
72 return mixer_->IsMuted(); | 75 return mixer_->IsMuted(); |
73 } | 76 } |
74 | 77 |
75 void AudioHandler::SetMuted(bool mute) { | 78 void AudioHandler::SetMuted(bool mute) { |
76 mixer_->SetMuted(mute); | 79 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); |
77 } | 89 } |
78 | 90 |
79 AudioHandler::AudioHandler() | 91 AudioHandler::AudioHandler() |
80 : mixer_(new AudioMixerAlsa()) { | 92 : mixer_(new AudioMixerAlsa()) { |
81 mixer_->Init(); | 93 mixer_->Init(); |
82 } | 94 } |
83 | 95 |
84 AudioHandler::~AudioHandler() { | 96 AudioHandler::~AudioHandler() { |
85 mixer_.reset(); | 97 mixer_.reset(); |
86 }; | 98 }; |
87 | 99 |
| 100 bool AudioHandler::IsMixerInitialized() { |
| 101 return mixer_->IsInitialized(); |
| 102 } |
| 103 |
88 // VolumeDbToPercent() and PercentToVolumeDb() conversion functions allow us | 104 // VolumeDbToPercent() and PercentToVolumeDb() conversion functions allow us |
89 // complete control over how the 0 to 100% range is mapped to actual loudness. | 105 // complete control over how the 0 to 100% range is mapped to actual loudness. |
90 // | 106 // |
91 // The mapping is confined to these two functions to make it easy to adjust and | 107 // The mapping is confined to these two functions to make it easy to adjust and |
92 // have everything else just work. The range is biased to give finer resolution | 108 // have everything else just work. The range is biased to give finer resolution |
93 // in the higher volumes if kVolumeBias is less than 1.0. | 109 // in the higher volumes if kVolumeBias is less than 1.0. |
94 | 110 |
95 double AudioHandler::VolumeDbToPercent(double volume_db) const { | 111 double AudioHandler::VolumeDbToPercent(double volume_db) const { |
96 double min_volume_db, max_volume_db; | 112 double min_volume_db, max_volume_db; |
97 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); | 113 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); |
98 | 114 |
99 if (volume_db < min_volume_db) | 115 if (volume_db < min_volume_db) |
100 return 0.0; | 116 return 0.0; |
101 // TODO(derat): Choose a better mapping between percent and decibels. The | 117 // TODO(derat): Choose a better mapping between percent and decibels. The |
102 // bottom twenty-five percent or so is useless on a CR-48's internal speakers; | 118 // bottom twenty-five percent or so is useless on a CR-48's internal speakers; |
103 // it's all inaudible. | 119 // it's all inaudible. |
104 return 100.0 * pow((volume_db - min_volume_db) / | 120 return 100.0 * pow((volume_db - min_volume_db) / |
105 (max_volume_db - min_volume_db), 1/kVolumeBias); | 121 (max_volume_db - min_volume_db), 1/kVolumeBias); |
106 } | 122 } |
107 | 123 |
108 double AudioHandler::PercentToVolumeDb(double volume_percent) const { | 124 double AudioHandler::PercentToVolumeDb(double volume_percent) const { |
109 double min_volume_db, max_volume_db; | 125 double min_volume_db, max_volume_db; |
110 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); | 126 mixer_->GetVolumeLimits(&min_volume_db, &max_volume_db); |
111 | 127 |
112 return pow(volume_percent / 100.0, kVolumeBias) * | 128 return pow(volume_percent / 100.0, kVolumeBias) * |
113 (max_volume_db - min_volume_db) + min_volume_db; | 129 (max_volume_db - min_volume_db) + min_volume_db; |
114 } | 130 } |
115 | 131 |
116 } // namespace chromeos | 132 } // namespace chromeos |
OLD | NEW |