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 // 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" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "media/audio/audio_util.h" | 13 #include "media/audio/audio_util.h" |
14 | 14 |
15 namespace media { | 15 namespace media { |
16 | 16 |
17 namespace { | |
18 | |
19 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 17 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
20 | |
21 template<class Fixed> | 18 template<class Fixed> |
22 static int ScaleChannel(int channel, int volume) { | 19 static int ScaleChannel(int channel, int volume) { |
23 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | 20 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); |
24 } | 21 } |
25 | 22 |
26 template<class Format, class Fixed, int bias> | 23 template<class Format, class Fixed, int bias> |
27 void AdjustVolume(Format* buf_out, | 24 static void AdjustVolume(Format* buf_out, |
28 int sample_count, | 25 int sample_count, |
29 int fixed_volume) { | 26 int fixed_volume) { |
30 for (int i = 0; i < sample_count; ++i) { | 27 for (int i = 0; i < sample_count; ++i) { |
31 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, | 28 buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias, |
32 fixed_volume) + bias); | 29 fixed_volume) + bias); |
33 } | 30 } |
34 } | 31 } |
35 | 32 |
36 static const int kChannel_L = 0; | 33 static const int kChannel_L = 0; |
37 static const int kChannel_R = 1; | 34 static const int kChannel_R = 1; |
38 static const int kChannel_C = 2; | 35 static const int kChannel_C = 2; |
39 | 36 |
40 template<class Fixed, int min_value, int max_value> | 37 template<class Fixed, int min_value, int max_value> |
41 static int AddChannel(int val, | 38 static int AddChannel(int val, int adder) { |
42 int adder) { | |
43 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); | 39 Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder); |
44 if (sum > max_value) | 40 if (sum > max_value) |
45 return max_value; | 41 return max_value; |
46 if (sum < min_value) | 42 if (sum < min_value) |
47 return min_value; | 43 return min_value; |
48 return static_cast<int>(sum); | 44 return static_cast<int>(sum); |
49 } | 45 } |
50 | 46 |
51 // FoldChannels() downmixes multichannel (ie 5.1 Surround Sound) to Stereo. | 47 // FoldChannels() downmixes multichannel (ie 5.1 Surround Sound) to Stereo. |
52 // Left and Right channels are preserved asis, and Center channel is | 48 // Left and Right channels are preserved asis, and Center channel is |
53 // distributed equally to both sides. To be perceptually 1/2 volume on | 49 // distributed equally to both sides. To be perceptually 1/2 volume on |
54 // both channels, 1/sqrt(2) is used instead of 1/2. | 50 // both channels, 1/sqrt(2) is used instead of 1/2. |
55 // Fixed point math is used for efficiency. 16 bits of fraction and 8,16 or 32 | 51 // Fixed point math is used for efficiency. 16 bits of fraction and 8,16 or 32 |
56 // bits of integer are used. | 52 // bits of integer are used. |
57 // 8 bit samples are unsigned and 128 represents 0, so a bias is removed before | 53 // 8 bit samples are unsigned and 128 represents 0, so a bias is removed before |
58 // doing calculations, then readded for the final output. | 54 // doing calculations, then readded for the final output. |
59 | |
60 template<class Format, class Fixed, int min_value, int max_value, int bias> | 55 template<class Format, class Fixed, int min_value, int max_value, int bias> |
61 static void FoldChannels(Format* buf_out, | 56 static void FoldChannels(Format* buf_out, |
62 int sample_count, | 57 int sample_count, |
63 const float volume, | 58 const float volume, |
64 int channels) { | 59 int channels) { |
65 Format* buf_in = buf_out; | 60 Format* buf_in = buf_out; |
66 const int center_volume = static_cast<int>(volume * 0.707f * 65536); | 61 const int center_volume = static_cast<int>(volume * 0.707f * 65536); |
67 const int fixed_volume = static_cast<int>(volume * 65536); | 62 const int fixed_volume = static_cast<int>(volume * 65536); |
68 | 63 |
69 for (int i = 0; i < sample_count; ++i) { | 64 for (int i = 0; i < sample_count; ++i) { |
70 int center = static_cast<int>(buf_in[kChannel_C] - bias); | 65 int center = static_cast<int>(buf_in[kChannel_C] - bias); |
71 int left = static_cast<int>(buf_in[kChannel_L] - bias); | 66 int left = static_cast<int>(buf_in[kChannel_L] - bias); |
72 int right = static_cast<int>(buf_in[kChannel_R] - bias); | 67 int right = static_cast<int>(buf_in[kChannel_R] - bias); |
73 | 68 |
74 center = ScaleChannel<Fixed>(center, center_volume); | 69 center = ScaleChannel<Fixed>(center, center_volume); |
75 left = ScaleChannel<Fixed>(left, fixed_volume); | 70 left = ScaleChannel<Fixed>(left, fixed_volume); |
76 right = ScaleChannel<Fixed>(right, fixed_volume); | 71 right = ScaleChannel<Fixed>(right, fixed_volume); |
77 | 72 |
78 buf_out[0] = static_cast<Format>( | 73 buf_out[0] = static_cast<Format>( |
79 AddChannel<Fixed, min_value, max_value>(left, center) + bias); | 74 AddChannel<Fixed, min_value, max_value>(left, center) + bias); |
80 buf_out[1] = static_cast<Format>( | 75 buf_out[1] = static_cast<Format>( |
81 AddChannel<Fixed, min_value, max_value>(right, center) + bias); | 76 AddChannel<Fixed, min_value, max_value>(right, center) + bias); |
82 | 77 |
83 buf_out += 2; | 78 buf_out += 2; |
84 buf_in += channels; | 79 buf_in += channels; |
85 } | 80 } |
86 } | 81 } |
87 } // namespace | |
88 | 82 |
89 // AdjustVolume() does an in place audio sample change. | 83 // AdjustVolume() does an in place audio sample change. |
90 bool AdjustVolume(void* buf, | 84 bool AdjustVolume(void* buf, |
91 size_t buflen, | 85 size_t buflen, |
92 int channels, | 86 int channels, |
93 int bytes_per_sample, | 87 int bytes_per_sample, |
94 float volume) { | 88 float volume) { |
95 DCHECK(buf); | 89 DCHECK(buf); |
96 if (volume < 0.0f || volume > 1.0f) | 90 if (volume < 0.0f || volume > 1.0f) |
97 return false; | 91 return false; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 sample = -32768.0; | 213 sample = -32768.0; |
220 else if (sample > 32767.0) | 214 else if (sample > 32767.0) |
221 sample = 32767.0; | 215 sample = 32767.0; |
222 | 216 |
223 destination[j * channels + i] = static_cast<int16>(sample); | 217 destination[j * channels + i] = static_cast<int16>(sample); |
224 } | 218 } |
225 } | 219 } |
226 } | 220 } |
227 | 221 |
228 } // namespace media | 222 } // namespace media |
OLD | NEW |