aboutsummaryrefslogtreecommitdiff
path: root/src/modules/audio_processing/main/source/audio_buffer.cc
blob: 6b20fceceea225155968f5bb37acffd7567b8ebd (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "audio_buffer.h"

#include "module_common_types.h"

namespace webrtc {
namespace {

enum {
  kSamplesPer8kHzChannel = 80,
  kSamplesPer16kHzChannel = 160,
  kSamplesPer32kHzChannel = 320
};

void StereoToMono(const WebRtc_Word16* left, const WebRtc_Word16* right,
                  WebRtc_Word16* out, int samples_per_channel) {
  WebRtc_Word32 data_int32 = 0;
  for (int i = 0; i < samples_per_channel; i++) {
    data_int32 = (left[i] + right[i]) >> 1;
    if (data_int32 > 32767) {
      data_int32 = 32767;
    } else if (data_int32 < -32768) {
      data_int32 = -32768;
    }

    out[i] = static_cast<WebRtc_Word16>(data_int32);
  }
}
}  // namespace

struct AudioChannel {
  AudioChannel() {
    memset(data, 0, sizeof(data));
  }

  WebRtc_Word16 data[kSamplesPer32kHzChannel];
};

struct SplitAudioChannel {
  SplitAudioChannel() {
    memset(low_pass_data, 0, sizeof(low_pass_data));
    memset(high_pass_data, 0, sizeof(high_pass_data));
    memset(analysis_filter_state1, 0, sizeof(analysis_filter_state1));
    memset(analysis_filter_state2, 0, sizeof(analysis_filter_state2));
    memset(synthesis_filter_state1, 0, sizeof(synthesis_filter_state1));
    memset(synthesis_filter_state2, 0, sizeof(synthesis_filter_state2));
  }

  WebRtc_Word16 low_pass_data[kSamplesPer16kHzChannel];
  WebRtc_Word16 high_pass_data[kSamplesPer16kHzChannel];

  WebRtc_Word32 analysis_filter_state1[6];
  WebRtc_Word32 analysis_filter_state2[6];
  WebRtc_Word32 synthesis_filter_state1[6];
  WebRtc_Word32 synthesis_filter_state2[6];
};

// TODO(am): check range of input parameters?
AudioBuffer::AudioBuffer(WebRtc_Word32 max_num_channels,
                         WebRtc_Word32 samples_per_channel)
    : max_num_channels_(max_num_channels),
      num_channels_(0),
      num_mixed_channels_(0),
      num_mixed_low_pass_channels_(0),
      samples_per_channel_(samples_per_channel),
      samples_per_split_channel_(samples_per_channel),
      reference_copied_(false),
      data_(NULL),
      channels_(NULL),
      split_channels_(NULL),
      mixed_low_pass_channels_(NULL),
      low_pass_reference_channels_(NULL) {
  if (max_num_channels_ > 1) {
    channels_ = new AudioChannel[max_num_channels_];
    mixed_low_pass_channels_ = new AudioChannel[max_num_channels_];
  }
  low_pass_reference_channels_ = new AudioChannel[max_num_channels_];

  if (samples_per_channel_ == kSamplesPer32kHzChannel) {
    split_channels_ = new SplitAudioChannel[max_num_channels_];
    samples_per_split_channel_ = kSamplesPer16kHzChannel;
  }
}

AudioBuffer::~AudioBuffer() {
  if (channels_ != NULL) {
    delete [] channels_;
  }

  if (mixed_low_pass_channels_ != NULL) {
    delete [] mixed_low_pass_channels_;
  }

  if (low_pass_reference_channels_ != NULL) {
    delete [] low_pass_reference_channels_;
  }

  if (split_channels_ != NULL) {
    delete [] split_channels_;
  }
}

WebRtc_Word16* AudioBuffer::data(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  if (data_ != NULL) {
    return data_;
  }

  return channels_[channel].data;
}

WebRtc_Word16* AudioBuffer::low_pass_split_data(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  if (split_channels_ == NULL) {
    return data(channel);
  }

  return split_channels_[channel].low_pass_data;
}

WebRtc_Word16* AudioBuffer::high_pass_split_data(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  if (split_channels_ == NULL) {
    return NULL;
  }

  return split_channels_[channel].high_pass_data;
}

WebRtc_Word16* AudioBuffer::mixed_low_pass_data(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_mixed_low_pass_channels_);

  return mixed_low_pass_channels_[channel].data;
}

WebRtc_Word16* AudioBuffer::low_pass_reference(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  if (!reference_copied_) {
    return NULL;
  }

  return low_pass_reference_channels_[channel].data;
}

WebRtc_Word32* AudioBuffer::analysis_filter_state1(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  return split_channels_[channel].analysis_filter_state1;
}

WebRtc_Word32* AudioBuffer::analysis_filter_state2(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  return split_channels_[channel].analysis_filter_state2;
}

WebRtc_Word32* AudioBuffer::synthesis_filter_state1(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  return split_channels_[channel].synthesis_filter_state1;
}

WebRtc_Word32* AudioBuffer::synthesis_filter_state2(WebRtc_Word32 channel) const {
  assert(channel >= 0 && channel < num_channels_);
  return split_channels_[channel].synthesis_filter_state2;
}

WebRtc_Word32 AudioBuffer::num_channels() const {
  return num_channels_;
}

WebRtc_Word32 AudioBuffer::samples_per_channel() const {
  return samples_per_channel_;
}

WebRtc_Word32 AudioBuffer::samples_per_split_channel() const {
  return samples_per_split_channel_;
}

// TODO(ajm): Do deinterleaving and mixing in one step?
void AudioBuffer::DeinterleaveFrom(AudioFrame* audioFrame) {
  assert(audioFrame->_audioChannel <= max_num_channels_);
  assert(audioFrame->_payloadDataLengthInSamples ==  samples_per_channel_);

  num_channels_ = audioFrame->_audioChannel;
  num_mixed_channels_ = 0;
  num_mixed_low_pass_channels_ = 0;
  reference_copied_ = false;

  if (num_channels_ == 1) {
    // We can get away with a pointer assignment in this case.
    data_ = audioFrame->_payloadData;
    return;
  }

  for (int i = 0; i < num_channels_; i++) {
    WebRtc_Word16* deinterleaved = channels_[i].data;
    WebRtc_Word16* interleaved = audioFrame->_payloadData;
    WebRtc_Word32 interleaved_idx = i;
    for (int j = 0; j < samples_per_channel_; j++) {
      deinterleaved[j] = interleaved[interleaved_idx];
      interleaved_idx += num_channels_;
    }
  }
}

void AudioBuffer::InterleaveTo(AudioFrame* audioFrame) const {
  assert(audioFrame->_audioChannel == num_channels_);
  assert(audioFrame->_payloadDataLengthInSamples == samples_per_channel_);

  if (num_channels_ == 1) {
    if (num_mixed_channels_ == 1) {
      memcpy(audioFrame->_payloadData,
             channels_[0].data,
             sizeof(WebRtc_Word16) * samples_per_channel_);
    } else {
      // These should point to the same buffer in this case.
      assert(data_ == audioFrame->_payloadData);
    }

    return;
  }

  for (int i = 0; i < num_channels_; i++) {
    WebRtc_Word16* deinterleaved = channels_[i].data;
    WebRtc_Word16* interleaved = audioFrame->_payloadData;
    WebRtc_Word32 interleaved_idx = i;
    for (int j = 0; j < samples_per_channel_; j++) {
      interleaved[interleaved_idx] = deinterleaved[j];
      interleaved_idx += num_channels_;
    }
  }
}

// TODO(ajm): would be good to support the no-mix case with pointer assignment.
// TODO(ajm): handle mixing to multiple channels?
void AudioBuffer::Mix(WebRtc_Word32 num_mixed_channels) {
  // We currently only support the stereo to mono case.
  assert(num_channels_ == 2);
  assert(num_mixed_channels == 1);

  StereoToMono(channels_[0].data,
               channels_[1].data,
               channels_[0].data,
               samples_per_channel_);

  num_channels_ = num_mixed_channels;
  num_mixed_channels_ = num_mixed_channels;
}

void AudioBuffer::CopyAndMixLowPass(WebRtc_Word32 num_mixed_channels) {
  // We currently only support the stereo to mono case.
  assert(num_channels_ == 2);
  assert(num_mixed_channels == 1);

  StereoToMono(low_pass_split_data(0),
               low_pass_split_data(1),
               mixed_low_pass_channels_[0].data,
               samples_per_split_channel_);

  num_mixed_low_pass_channels_ = num_mixed_channels;
}

void AudioBuffer::CopyLowPassToReference() {
  reference_copied_ = true;
  for (int i = 0; i < num_channels_; i++) {
    memcpy(low_pass_reference_channels_[i].data,
           low_pass_split_data(i),
           sizeof(WebRtc_Word16) * samples_per_split_channel_);
  }
}
}  // namespace webrtc