summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_util.cc
diff options
context:
space:
mode:
Diffstat (limited to 'media/audio/audio_util.cc')
-rw-r--r--media/audio/audio_util.cc183
1 files changed, 98 insertions, 85 deletions
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc
index c1c1bc8..8774a72 100644
--- a/media/audio/audio_util.cc
+++ b/media/audio/audio_util.cc
@@ -5,24 +5,90 @@
// Software adjust volume of samples, allows each audio stream its own
// volume without impacting master volume for chrome and other applications.
+// Implemented as templates to allow 8, 16 and 32 bit implementations.
+// 8 bit is unsigned and biased by 128.
+
#include "base/basictypes.h"
#include "base/logging.h"
#include "media/audio/audio_util.h"
namespace media {
-// Done as C for future assembly optimization and/or to move to different class.
-// TODO(fbarchard): add channels_in to allow number of channels to be reduced.
+namespace {
+
+// TODO(fbarchard): Convert to intrinsics for better efficiency.
-template<class Format>
-void AdjustVolumeInternal(Format* buf_out,
- int sample_count,
- float volume) {
+template<class Fixed>
+static int ScaleChannel(int channel, int volume) {
+ return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16);
+}
+
+template<class Format, class Fixed, int bias>
+void AdjustVolume(Format* buf_out,
+ int sample_count,
+ int fixed_volume) {
for (int i = 0; i < sample_count; ++i) {
- buf_out[i] = static_cast<Format>(buf_out[i] * volume);
+ buf_out[i] = static_cast<Format>(ScaleChannel<Fixed>(buf_out[i] - bias,
+ fixed_volume) + bias);
}
}
+// Channel order for AAC
+// From http://www.hydrogenaudio.org/forums/lofiversion/index.php/t40046.html
+
+static const int kChannel_C = 0;
+static const int kChannel_L = 1;
+static const int kChannel_R = 2;
+
+template<class Fixed, int min_value, int max_value>
+static int AddChannel(int val,
+ int adder) {
+ Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder);
+ if (sum > max_value)
+ return max_value;
+ if (sum < min_value)
+ return min_value;
+ return static_cast<int>(sum);
+}
+
+// FoldChannels() downmixes multichannel (ie 5.1 Surround Sound) to Stereo.
+// Left and Right channels are preserved asis, and Center channel is
+// distributed equally to both sides. To be perceptually 1/2 volume on
+// both channels, 1/sqrt(2) is used instead of 1/2.
+// Fixed point math is used for efficiency. 16 bits of fraction and 8,16 or 32
+// bits of integer are used.
+// 8 bit samples are unsigned and 128 represents 0, so a bias is removed before
+// doing calculations, then readded for the final output.
+
+template<class Format, class Fixed, int min_value, int max_value, int bias>
+static void FoldChannels(Format* buf_out,
+ int sample_count,
+ const float volume,
+ int channels) {
+ Format* buf_in = buf_out;
+ const int center_volume = static_cast<int>(volume * 0.707f * 65536);
+ const int fixed_volume = static_cast<int>(volume * 65536);
+
+ for (int i = 0; i < sample_count; ++i) {
+ int center = static_cast<int>(buf_in[kChannel_C] - bias);
+ int left = static_cast<int>(buf_in[kChannel_L] - bias);
+ int right = static_cast<int>(buf_in[kChannel_R] - bias);
+
+ center = ScaleChannel<Fixed>(center, center_volume);
+ left = ScaleChannel<Fixed>(left, fixed_volume);
+ right = ScaleChannel<Fixed>(right, fixed_volume);
+
+ buf_out[0] = static_cast<Format>(
+ AddChannel<Fixed, min_value, max_value>(left, center) + bias);
+ buf_out[1] = static_cast<Format>(
+ AddChannel<Fixed, min_value, max_value>(right, center) + bias);
+
+ buf_out += 2;
+ buf_in += channels;
+ }
+}
+} // namespace
+
// AdjustVolume() does an in place audio sample change.
bool AdjustVolume(void* buf,
size_t buflen,
@@ -39,78 +105,27 @@ bool AdjustVolume(void* buf,
}
if (channels > 0 && channels <= 6 && bytes_per_sample > 0) {
int sample_count = buflen / bytes_per_sample;
+ const int fixed_volume = static_cast<int>(volume * 65536);
if (bytes_per_sample == 1) {
- AdjustVolumeInternal(reinterpret_cast<uint8*>(buf),
- sample_count,
- volume);
+ AdjustVolume<uint8, int32, 128>(reinterpret_cast<uint8*>(buf),
+ sample_count,
+ fixed_volume);
return true;
} else if (bytes_per_sample == 2) {
- AdjustVolumeInternal(reinterpret_cast<int16*>(buf),
- sample_count,
- volume);
+ AdjustVolume<int16, int32, 0>(reinterpret_cast<int16*>(buf),
+ sample_count,
+ fixed_volume);
return true;
} else if (bytes_per_sample == 4) {
- AdjustVolumeInternal(reinterpret_cast<int32*>(buf),
- sample_count,
- volume);
+ AdjustVolume<int32, int64, 0>(reinterpret_cast<int32*>(buf),
+ sample_count,
+ fixed_volume);
return true;
}
}
return false;
}
-// Channel order for AAC
-// From http://www.hydrogenaudio.org/forums/lofiversion/index.php/t40046.html
-// And Quicktime Pro, Movie Inspector
-const int kChannel_C = 0;
-const int kChannel_L = 1;
-const int kChannel_R = 2;
-const int kChannel_SL = 3;
-const int kChannel_SR = 4;
-const int kChannel_LFE = 5;
-
-template<class Format>
-Format ChannelsClampInternal(float val,
- const Format min_value,
- const Format max_value) {
- if (val > static_cast<float>(max_value)) {
- return max_value;
- }
- if (val < static_cast<float>(min_value)) {
- return min_value;
- }
- return static_cast<Format>(val);
-}
-
-template<class Format>
-void FoldChannelsInternal(Format* buf_out,
- int sample_count,
- float volume,
- int channels,
- const int min_value,
- const int max_value) {
- Format* buf_in = buf_out;
- // mid_value is to adjust excess 128 notation in unsigned 8 bit samples
- // to signed before adding channels.
- const int mid_value = (max_value + min_value + 1) / 2;
- const float kHalfPerceived = 0.707f; // 1/sqrt(2)
- for (int i = 0; i < sample_count; ++i) {
- float center_half = static_cast<float>(buf_in[kChannel_C] - mid_value) * kHalfPerceived;
- float left = static_cast<float>(buf_in[kChannel_L] - mid_value);
- float right = static_cast<float>(buf_in[kChannel_R] - mid_value);
- float surround_left = static_cast<float>(buf_in[kChannel_SL] - mid_value);
- float surround_right = static_cast<float>(buf_in[kChannel_SR] - mid_value);
- buf_out[0] = ChannelsClampInternal(
- (left + surround_left + center_half) * volume + mid_value,
- static_cast<Format>(min_value), static_cast<Format>(max_value));
- buf_out[1] = ChannelsClampInternal(
- (right + surround_right + center_half) * volume + mid_value,
- static_cast<Format>(min_value), static_cast<Format>(max_value));
- buf_out += 2;
- buf_in += channels;
- }
-}
-
bool FoldChannels(void* buf,
size_t buflen,
int channels,
@@ -121,27 +136,25 @@ bool FoldChannels(void* buf,
if (channels >= 5 && channels <= 6 && bytes_per_sample > 0) {
int sample_count = buflen / (channels * bytes_per_sample);
if (bytes_per_sample == 1) {
- FoldChannelsInternal(reinterpret_cast<uint8*>(buf),
- sample_count,
- volume,
- channels,
- 0, 255);
+ FoldChannels<uint8, int32, -128, 127, 128>(
+ reinterpret_cast<uint8*>(buf),
+ sample_count,
+ volume,
+ channels);
return true;
} else if (bytes_per_sample == 2) {
- FoldChannelsInternal(reinterpret_cast<int16*>(buf),
- sample_count,
- volume,
- channels,
- -32768,
- 32767);
+ FoldChannels<int16, int32, -32768, 32767, 0>(
+ reinterpret_cast<int16*>(buf),
+ sample_count,
+ volume,
+ channels);
return true;
} else if (bytes_per_sample == 4) {
- FoldChannelsInternal(reinterpret_cast<int32*>(buf),
- sample_count,
- volume,
- channels,
- 0x80000000,
- 0x7fffffff);
+ FoldChannels<int32, int64, 0x80000000, 0x7fffffff, 0>(
+ reinterpret_cast<int32*>(buf),
+ sample_count,
+ volume,
+ channels);
return true;
}
}