summaryrefslogtreecommitdiffstats
path: root/media/audio
diff options
context:
space:
mode:
authorfbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-04 01:34:01 +0000
committerfbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-04 01:34:01 +0000
commit83e68b1f6ecb246a0985850c1a9eecce59984cba (patch)
tree5587cbf31040af40ae18e5462caa8efe62f7a793 /media/audio
parenteb6e29ac1f49cf263b6089e127660bc66dadf28e (diff)
downloadchromium_src-83e68b1f6ecb246a0985850c1a9eecce59984cba.zip
chromium_src-83e68b1f6ecb246a0985850c1a9eecce59984cba.tar.gz
chromium_src-83e68b1f6ecb246a0985850c1a9eecce59984cba.tar.bz2
floating point audio support in util functions for volume and folding
BUG=42861 TEST=none Review URL: http://codereview.chromium.org/1856002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@46314 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio')
-rw-r--r--media/audio/audio_util.cc57
-rw-r--r--media/audio/audio_util_unittest.cc33
-rw-r--r--media/audio/win/waveout_output_win.cc4
3 files changed, 65 insertions, 29 deletions
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc
index 8774a72..d72cbe7 100644
--- a/media/audio/audio_util.cc
+++ b/media/audio/audio_util.cc
@@ -24,15 +24,20 @@ static int ScaleChannel(int channel, int volume) {
}
template<class Format, class Fixed, int bias>
-void AdjustVolume(Format* buf_out,
- int sample_count,
- int fixed_volume) {
+static 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>(ScaleChannel<Fixed>(buf_out[i] - bias,
fixed_volume) + bias);
}
}
+static void AdjustVolumeFloat(float* buf_out, int sample_count, float volume) {
+ for (int i = 0; i < sample_count; ++i) {
+ buf_out[i] = buf_out[i] * volume;
+ }
+}
+
+
// Channel order for AAC
// From http://www.hydrogenaudio.org/forums/lofiversion/index.php/t40046.html
@@ -41,8 +46,7 @@ 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) {
+static int AddChannel(int val, int adder) {
Fixed sum = static_cast<Fixed>(val) + static_cast<Fixed>(adder);
if (sum > max_value)
return max_value;
@@ -62,9 +66,9 @@ static int AddChannel(int val,
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) {
+ 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);
@@ -87,6 +91,31 @@ static void FoldChannels(Format* buf_out,
buf_in += channels;
}
}
+
+static void FoldChannelsFloat(float* buf_out,
+ int sample_count,
+ const float volume,
+ int channels) {
+ float* buf_in = buf_out;
+ const float center_volume = volume * 0.707f;
+
+ for (int i = 0; i < sample_count; ++i) {
+ float center = buf_in[kChannel_C];
+ float left = buf_in[kChannel_L];
+ float right = buf_in[kChannel_R];
+
+ center = center * center_volume;
+ left = left * volume;
+ right = right * volume;
+
+ buf_out[0] = left + center;
+ buf_out[1] = right + center;
+
+ buf_out += 2;
+ buf_in += channels;
+ }
+}
+
} // namespace
// AdjustVolume() does an in place audio sample change.
@@ -117,9 +146,10 @@ bool AdjustVolume(void* buf,
fixed_volume);
return true;
} else if (bytes_per_sample == 4) {
- AdjustVolume<int32, int64, 0>(reinterpret_cast<int32*>(buf),
- sample_count,
- fixed_volume);
+ // 4 byte per sample is float.
+ AdjustVolumeFloat(reinterpret_cast<float*>(buf),
+ sample_count,
+ volume);
return true;
}
}
@@ -150,8 +180,9 @@ bool FoldChannels(void* buf,
channels);
return true;
} else if (bytes_per_sample == 4) {
- FoldChannels<int32, int64, 0x80000000, 0x7fffffff, 0>(
- reinterpret_cast<int32*>(buf),
+ // 4 byte per sample is float.
+ FoldChannelsFloat(
+ reinterpret_cast<float*>(buf),
sample_count,
volume,
channels);
diff --git a/media/audio/audio_util_unittest.cc b/media/audio/audio_util_unittest.cc
index c0ed0fc..22e9afe 100644
--- a/media/audio/audio_util_unittest.cc
+++ b/media/audio/audio_util_unittest.cc
@@ -66,16 +66,19 @@ TEST(AudioUtilTest, AdjustVolume_s16_one) {
EXPECT_EQ(0, expected_test);
}
-TEST(AudioUtilTest, AdjustVolume_s32) {
+TEST(AudioUtilTest, AdjustVolume_f32) {
// Test AdjustVolume() on 32 bit samples.
- int32 samples_s32[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
- int32 expected_s32[kNumberOfSamples] = { -1, 0x10, -8192, 30 };
- bool result_s32 = media::AdjustVolume(samples_s32, sizeof(samples_s32),
+ float samples_f32[kNumberOfSamples] = { -4.0f, 0.5f, -.05f, 123.0f };
+ float expected_f32[kNumberOfSamples] = { -4.0f * 0.25f,
+ 0.5f * 0.25f,
+ -.05f * 0.25f,
+ 123.0f * 0.25f };
+ bool result_f32 = media::AdjustVolume(samples_f32, sizeof(samples_f32),
4, // channels.
- sizeof(samples_s32[0]),
+ sizeof(samples_f32[0]),
0.25f);
- EXPECT_EQ(true, result_s32);
- int expected_test = memcmp(samples_s32, expected_s32, sizeof(expected_s32));
+ EXPECT_EQ(true, result_f32);
+ int expected_test = memcmp(samples_f32, expected_f32, sizeof(expected_f32));
EXPECT_EQ(0, expected_test);
}
@@ -109,17 +112,17 @@ TEST(AudioUtilTest, FoldChannels_s16) {
EXPECT_EQ(0, expected_test);
}
-TEST(AudioUtilTest, FoldChannels_s32) {
+TEST(AudioUtilTest, FoldChannels_f32) {
// Test FoldChannels() on 32 bit samples.
- int32 samples_s32[6] = { 12, 1, 3, 7, 13, 17 };
- int32 expected_s32[2] = { static_cast<int16>(12 * .707 + 1),
- static_cast<int16>(12 * .707 + 3) };
- bool result_s32 = media::FoldChannels(samples_s32, sizeof(samples_s32),
+ float samples_f32[6] = { 12.0f, 1.0f, 3.0f, 7.0f, 13.0f, 17.0f };
+ float expected_f32[2] = { 12.0f * .707f + 1.0f,
+ 12.0f * .707f + 3.0f };
+ bool result_f32 = media::FoldChannels(samples_f32, sizeof(samples_f32),
6, // channels.
- sizeof(samples_s32[0]),
+ sizeof(samples_f32[0]),
1.00f);
- EXPECT_EQ(true, result_s32);
- int expected_test = memcmp(samples_s32, expected_s32, sizeof(expected_s32));
+ EXPECT_EQ(true, result_f32);
+ int expected_test = memcmp(samples_f32, expected_f32, sizeof(expected_f32));
EXPECT_EQ(0, expected_test);
}
diff --git a/media/audio/win/waveout_output_win.cc b/media/audio/win/waveout_output_win.cc
index 59f4c1c..073d0fc 100644
--- a/media/audio/win/waveout_output_win.cc
+++ b/media/audio/win/waveout_output_win.cc
@@ -5,6 +5,7 @@
#include "media/audio/win/waveout_output_win.h"
#include <windows.h>
+#include <mmreg.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
@@ -55,7 +56,8 @@ PCMWaveOutAudioOutputStream::PCMWaveOutAudioOutputStream(
volume_(1),
channels_(channels),
pending_bytes_(0) {
- format_.wFormatTag = WAVE_FORMAT_PCM;
+ format_.wFormatTag = bits_per_sample == 32 ?
+ WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
format_.nChannels = channels > 2 ? 2 : channels;
format_.nSamplesPerSec = sampling_rate;
format_.wBitsPerSample = bits_per_sample;