OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
fbarchard1
2011/01/06 20:34:29
update to 2011
| |
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 // Software adjust volume of samples, allows each audio stream its own | 5 // Software adjust volume of samples, allows each audio stream its own |
6 // volume without impacting master volume for chrome and other applications. | 6 // volume without impacting master volume for chrome and other applications. |
7 | 7 |
8 // Implemented as templates to allow 8, 16 and 32 bit implementations. | 8 // Implemented as templates to allow 8, 16 and 32 bit implementations. |
9 // 8 bit is unsigned and biased by 128. | 9 // 8 bit is unsigned and biased by 128. |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 } | 86 } |
87 } // namespace | 87 } // namespace |
88 | 88 |
89 // AdjustVolume() does an in place audio sample change. | 89 // AdjustVolume() does an in place audio sample change. |
90 bool AdjustVolume(void* buf, | 90 bool AdjustVolume(void* buf, |
91 size_t buflen, | 91 size_t buflen, |
92 int channels, | 92 int channels, |
93 int bytes_per_sample, | 93 int bytes_per_sample, |
94 float volume) { | 94 float volume) { |
95 DCHECK(buf); | 95 DCHECK(buf); |
96 DCHECK(volume >= 0.0f && volume <= 1.0f); | 96 if (volume < 0.0f || volume > 1.0f) |
97 return false; | |
97 if (volume == 1.0f) { | 98 if (volume == 1.0f) { |
98 return true; | 99 return true; |
99 } else if (volume == 0.0f) { | 100 } else if (volume == 0.0f) { |
100 memset(buf, 0, buflen); | 101 memset(buf, 0, buflen); |
101 return true; | 102 return true; |
102 } | 103 } |
103 if (channels > 0 && channels <= 8 && bytes_per_sample > 0) { | 104 if (channels > 0 && channels <= 8 && bytes_per_sample > 0) { |
104 int sample_count = buflen / bytes_per_sample; | 105 int sample_count = buflen / bytes_per_sample; |
105 const int fixed_volume = static_cast<int>(volume * 65536); | 106 const int fixed_volume = static_cast<int>(volume * 65536); |
106 if (bytes_per_sample == 1) { | 107 if (bytes_per_sample == 1) { |
(...skipping 15 matching lines...) Expand all Loading... | |
122 } | 123 } |
123 return false; | 124 return false; |
124 } | 125 } |
125 | 126 |
126 bool FoldChannels(void* buf, | 127 bool FoldChannels(void* buf, |
127 size_t buflen, | 128 size_t buflen, |
128 int channels, | 129 int channels, |
129 int bytes_per_sample, | 130 int bytes_per_sample, |
130 float volume) { | 131 float volume) { |
131 DCHECK(buf); | 132 DCHECK(buf); |
132 DCHECK(volume >= 0.0f && volume <= 1.0f); | 133 if (volume < 0.0f || volume > 1.0f) |
134 return false; | |
133 if (channels > 2 && channels <= 8 && bytes_per_sample > 0) { | 135 if (channels > 2 && channels <= 8 && bytes_per_sample > 0) { |
134 int sample_count = buflen / (channels * bytes_per_sample); | 136 int sample_count = buflen / (channels * bytes_per_sample); |
135 if (bytes_per_sample == 1) { | 137 if (bytes_per_sample == 1) { |
136 FoldChannels<uint8, int32, -128, 127, 128>( | 138 FoldChannels<uint8, int32, -128, 127, 128>( |
137 reinterpret_cast<uint8*>(buf), | 139 reinterpret_cast<uint8*>(buf), |
138 sample_count, | 140 sample_count, |
139 volume, | 141 volume, |
140 channels); | 142 channels); |
141 return true; | 143 return true; |
142 } else if (bytes_per_sample == 2) { | 144 } else if (bytes_per_sample == 2) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 return true; | 200 return true; |
199 } | 201 } |
200 | 202 |
201 default: | 203 default: |
202 break; | 204 break; |
203 } | 205 } |
204 return false; | 206 return false; |
205 } | 207 } |
206 | 208 |
207 } // namespace media | 209 } // namespace media |
OLD | NEW |