diff options
author | fbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-20 20:20:47 +0000 |
---|---|---|
committer | fbarchard@chromium.org <fbarchard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-20 20:20:47 +0000 |
commit | 762b625b3de00f106016a42bc9cdf774af4e1027 (patch) | |
tree | bf234d8459e12d671207e2341cccde9e6877da54 /media/audio/audio_util.cc | |
parent | 3b28936e5f88399fd4256df0ad02f117da38c8cb (diff) | |
download | chromium_src-762b625b3de00f106016a42bc9cdf774af4e1027.zip chromium_src-762b625b3de00f106016a42bc9cdf774af4e1027.tar.gz chromium_src-762b625b3de00f106016a42bc9cdf774af4e1027.tar.bz2 |
Software volume adjustment by changing samples.
BUG=16500
TEST=play youtube in different window. play another video with video tag and mute sound and make sure it does not affect youtube.
Review URL: http://codereview.chromium.org/159092
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/audio_util.cc')
-rw-r--r-- | media/audio/audio_util.cc | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc new file mode 100644 index 0000000..dabe448 --- /dev/null +++ b/media/audio/audio_util.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Software adjust volume of samples, allows each audio stream its own +// volume without impacting master volume for chrome and other applications. + +#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. + +template<class Format> +void AdjustVolumeInternal(Format* buf_out, + int sample_count, + float volume) { + for (int i = 0; i < sample_count; ++i) { + buf_out[i] = static_cast<Format>(buf_out[i] * volume); + } +} + +// AdjustVolume() does an in place audio sample change. +bool AdjustVolume(void* buf, + size_t buflen, + int channels, + int bytes_per_sample, + float volume) { + DCHECK(buf); + DCHECK(volume >= 0.0f && volume <= 1.0f); + if (volume == 1.0f) { + return true; + } else if (volume == 0.0f) { + memset(buf, 0, buflen); + return true; + } + if (channels > 0 && channels <= 6 && bytes_per_sample > 0) { + int sample_count = buflen / bytes_per_sample; + if (bytes_per_sample == 1) { + AdjustVolumeInternal(reinterpret_cast<uint8*>(buf), + sample_count, + volume); + return true; + } else if (bytes_per_sample == 2) { + AdjustVolumeInternal(reinterpret_cast<int16*>(buf), + sample_count, + volume); + return true; + } else if (bytes_per_sample == 4) { + AdjustVolumeInternal(reinterpret_cast<int32*>(buf), + sample_count, + volume); + return true; + } + } + return false; +} + +} // namespace media |