summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_util.cc
blob: dabe44893a9eac3892ca351b018fc925b84da50f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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