summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_util.cc
blob: c1c1bc8bd7954974980c7f5f6c04e9ff9bf64462 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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;
}

// 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,
                  int bytes_per_sample,
                  float volume) {
  DCHECK(buf);
  DCHECK(volume >= 0.0f && volume <= 1.0f);
  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);
      return true;
    } else if (bytes_per_sample == 2) {
      FoldChannelsInternal(reinterpret_cast<int16*>(buf),
                           sample_count,
                           volume,
                           channels,
                           -32768,
                           32767);
      return true;
    } else if (bytes_per_sample == 4) {
      FoldChannelsInternal(reinterpret_cast<int32*>(buf),
                           sample_count,
                           volume,
                           channels,
                           0x80000000,
                           0x7fffffff);
      return true;
    }
  }
  return false;
}

}  // namespace media