blob: 7ced4606374a49e0d429713b79e26df04c85ead9 [file] [log] [blame]
andrew@webrtc.org325cff02014-10-01 17:42:18 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/common_audio/blocker.h"
12
13#include <string.h>
14
15#include "webrtc/base/checks.h"
16
17namespace {
18
19// Adds |a| and |b| frame by frame into |result| (basically matrix addition).
20void AddFrames(const float* const* a,
21 int a_start_index,
22 const float* const* b,
23 int b_start_index,
24 int num_frames,
25 int num_channels,
26 float* const* result,
27 int result_start_index) {
28 for (int i = 0; i < num_channels; ++i) {
29 for (int j = 0; j < num_frames; ++j) {
30 result[i][j + result_start_index] =
31 a[i][j + a_start_index] + b[i][j + b_start_index];
32 }
33 }
34}
35
36// Copies |src| into |dst| channel by channel.
37void CopyFrames(const float* const* src,
38 int src_start_index,
39 int num_frames,
40 int num_channels,
41 float* const* dst,
42 int dst_start_index) {
43 for (int i = 0; i < num_channels; ++i) {
44 memcpy(&dst[i][dst_start_index],
45 &src[i][src_start_index],
46 num_frames * sizeof(float));
47 }
48}
49
aluebs@webrtc.org6f10ae22014-12-17 17:28:31 +000050// Moves |src| into |dst| channel by channel.
51void MoveFrames(const float* const* src,
52 int src_start_index,
53 int num_frames,
54 int num_channels,
55 float* const* dst,
56 int dst_start_index) {
57 for (int i = 0; i < num_channels; ++i) {
58 memmove(&dst[i][dst_start_index],
59 &src[i][src_start_index],
60 num_frames * sizeof(float));
61 }
62}
63
andrew@webrtc.org325cff02014-10-01 17:42:18 +000064void ZeroOut(float* const* buffer,
65 int starting_idx,
66 int num_frames,
67 int num_channels) {
68 for (int i = 0; i < num_channels; ++i) {
69 memset(&buffer[i][starting_idx], 0, num_frames * sizeof(float));
70 }
71}
72
73// Pointwise multiplies each channel of |frames| with |window|. Results are
74// stored in |frames|.
75void ApplyWindow(const float* window,
76 int num_frames,
77 int num_channels,
78 float* const* frames) {
79 for (int i = 0; i < num_channels; ++i) {
80 for (int j = 0; j < num_frames; ++j) {
81 frames[i][j] = frames[i][j] * window[j];
82 }
83 }
84}
85
aluebs@webrtc.orgc0da63c2015-01-13 22:28:35 +000086int gcd(int a, int b) {
87 int tmp;
88 while (b) {
89 tmp = a;
90 a = b;
91 b = tmp % b;
92 }
93 return a;
94}
95
andrew@webrtc.org325cff02014-10-01 17:42:18 +000096} // namespace
97
98namespace webrtc {
99
100Blocker::Blocker(int chunk_size,
101 int block_size,
102 int num_input_channels,
103 int num_output_channels,
104 const float* window,
105 int shift_amount,
106 BlockerCallback* callback)
107 : chunk_size_(chunk_size),
108 block_size_(block_size),
109 num_input_channels_(num_input_channels),
110 num_output_channels_(num_output_channels),
aluebs@webrtc.orgc0da63c2015-01-13 22:28:35 +0000111 initial_delay_(block_size_ - gcd(chunk_size, shift_amount)),
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000112 frame_offset_(0),
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000113 input_buffer_(num_input_channels_, chunk_size_ + initial_delay_),
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000114 output_buffer_(chunk_size_ + initial_delay_, num_output_channels_),
115 input_block_(block_size_, num_input_channels_),
116 output_block_(block_size_, num_output_channels_),
117 window_(new float[block_size_]),
118 shift_amount_(shift_amount),
119 callback_(callback) {
120 CHECK_LE(num_output_channels_, num_input_channels_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000121 memcpy(window_.get(), window, block_size_ * sizeof(float));
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000122 input_buffer_.MoveReadPosition(-initial_delay_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000123}
124
aluebs@webrtc.org6f10ae22014-12-17 17:28:31 +0000125// When block_size < chunk_size the input and output buffers look like this:
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000126//
127// delay* chunk_size chunk_size + delay*
128// buffer: <-------------|---------------------|---------------|>
129// _a_ _b_ _c_
130//
131// On each call to ProcessChunk():
132// 1. New input gets read into sections _b_ and _c_ of the input buffer.
133// 2. We block starting from frame_offset.
134// 3. We block until we reach a block |bl| that doesn't contain any frames
135// from sections _a_ or _b_ of the input buffer.
136// 4. We window the current block, fire the callback for processing, window
137// again, and overlap/add to the output buffer.
138// 5. We copy sections _a_ and _b_ of the output buffer into output.
aluebs@webrtc.org6f10ae22014-12-17 17:28:31 +0000139// 6. For both the input and the output buffers, we copy section _c_ into
140// section _a_.
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000141// 7. We set the new frame_offset to be the difference between the first frame
142// of |bl| and the border between sections _b_ and _c_.
143//
aluebs@webrtc.org6f10ae22014-12-17 17:28:31 +0000144// When block_size > chunk_size the input and output buffers look like this:
145//
146// chunk_size delay* chunk_size + delay*
147// buffer: <-------------|---------------------|---------------|>
148// _a_ _b_ _c_
149//
150// On each call to ProcessChunk():
151// The procedure is the same as above, except for:
152// 1. New input gets read into section _c_ of the input buffer.
153// 3. We block until we reach a block |bl| that doesn't contain any frames
154// from section _a_ of the input buffer.
155// 5. We copy section _a_ of the output buffer into output.
156// 6. For both the input and the output buffers, we copy sections _b_ and _c_
157// into section _a_ and _b_.
158// 7. We set the new frame_offset to be the difference between the first frame
159// of |bl| and the border between sections _a_ and _b_.
160//
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000161// * delay here refers to inintial_delay_
162//
163// TODO(claguna): Look at using ring buffers to eliminate some copies.
164void Blocker::ProcessChunk(const float* const* input,
165 int chunk_size,
166 int num_input_channels,
167 int num_output_channels,
168 float* const* output) {
169 CHECK_EQ(chunk_size, chunk_size_);
170 CHECK_EQ(num_input_channels, num_input_channels_);
171 CHECK_EQ(num_output_channels, num_output_channels_);
172
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000173 input_buffer_.Write(input, num_input_channels, chunk_size_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000174 int first_frame_in_block = frame_offset_;
175
176 // Loop through blocks.
177 while (first_frame_in_block < chunk_size_) {
andrew@webrtc.org041035b2015-01-26 21:23:53 +0000178 input_buffer_.Read(input_block_.channels(), num_input_channels,
179 block_size_);
180 input_buffer_.MoveReadPosition(-block_size_ + shift_amount_);
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000181
182 ApplyWindow(window_.get(),
183 block_size_,
184 num_input_channels_,
185 input_block_.channels());
186 callback_->ProcessBlock(input_block_.channels(),
187 block_size_,
188 num_input_channels_,
189 num_output_channels_,
190 output_block_.channels());
191 ApplyWindow(window_.get(),
192 block_size_,
193 num_output_channels_,
194 output_block_.channels());
195
196 AddFrames(output_buffer_.channels(),
197 first_frame_in_block,
198 output_block_.channels(),
199 0,
200 block_size_,
201 num_output_channels_,
202 output_buffer_.channels(),
203 first_frame_in_block);
204
205 first_frame_in_block += shift_amount_;
206 }
207
208 // Copy output buffer to output
209 CopyFrames(output_buffer_.channels(),
210 0,
211 chunk_size_,
212 num_output_channels_,
213 output,
214 0);
215
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000216 // Copy output buffer [chunk_size_, chunk_size_ + initial_delay]
217 // to output buffer [0, initial_delay], zero the rest.
aluebs@webrtc.org6f10ae22014-12-17 17:28:31 +0000218 MoveFrames(output_buffer_.channels(),
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000219 chunk_size,
220 initial_delay_,
221 num_output_channels_,
222 output_buffer_.channels(),
223 0);
224 ZeroOut(output_buffer_.channels(),
225 initial_delay_,
226 chunk_size_,
227 num_output_channels_);
228
229 // Calculate new starting frames.
230 frame_offset_ = first_frame_in_block - chunk_size_;
231}
232
233} // namespace webrtc