blob: 3ed9528de769861a3a1c794e65596e221fba73f4 [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#ifndef WEBRTC_COMMON_AUDIO_LAPPED_TRANSFORM_H_
12#define WEBRTC_COMMON_AUDIO_LAPPED_TRANSFORM_H_
13
14#include <complex>
15
16#include "webrtc/base/checks.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
andrew@webrtc.org325cff02014-10-01 17:42:18 +000018#include "webrtc/common_audio/blocker.h"
19#include "webrtc/common_audio/real_fourier.h"
20#include "webrtc/system_wrappers/interface/aligned_array.h"
andrew@webrtc.org325cff02014-10-01 17:42:18 +000021
22namespace webrtc {
23
24// Helper class for audio processing modules which operate on frequency domain
25// input derived from the windowed time domain audio stream.
26//
27// The input audio chunk is sliced into possibly overlapping blocks, multiplied
28// by a window and transformed with an FFT implementation. The transformed data
29// is supplied to the given callback for processing. The processed output is
30// then inverse transformed into the time domain and spliced back into a chunk
31// which constitutes the final output of this processing module.
32class LappedTransform {
33 public:
34 class Callback {
35 public:
36 virtual ~Callback() {}
37
38 virtual void ProcessAudioBlock(const std::complex<float>* const* in_block,
39 int in_channels, int frames,
40 int out_channels,
41 std::complex<float>* const* out_block) = 0;
42 };
43
44 // Construct a transform instance. |chunk_length| is the number of samples in
45 // each channel. |window| defines the window, owned by the caller (a copy is
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000046 // made internally); |window| should have length equal to |block_length|.
47 // |block_length| defines the length of a block, in samples.
48 // |shift_amount| is in samples. |callback| is the caller-owned audio
49 // processing function called for each block of the input chunk.
andrew@webrtc.org325cff02014-10-01 17:42:18 +000050 LappedTransform(int in_channels, int out_channels, int chunk_length,
51 const float* window, int block_length, int shift_amount,
52 Callback* callback);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000053 ~LappedTransform() {}
andrew@webrtc.org325cff02014-10-01 17:42:18 +000054
55 // Main audio processing helper method. Internally slices |in_chunk| into
56 // blocks, transforms them to frequency domain, calls the callback for each
57 // block and returns a de-blocked time domain chunk of audio through
58 // |out_chunk|. Both buffers are caller-owned.
59 void ProcessChunk(const float* const* in_chunk, float* const* out_chunk);
60
61 private:
62 // Internal middleware callback, given to the blocker. Transforms each block
63 // and hands it over to the processing method given at construction time.
andrew@webrtc.org325cff02014-10-01 17:42:18 +000064 class BlockThunk : public BlockerCallback {
65 public:
66 explicit BlockThunk(LappedTransform* parent) : parent_(parent) {}
andrew@webrtc.org325cff02014-10-01 17:42:18 +000067
68 virtual void ProcessBlock(const float* const* input, int num_frames,
69 int num_input_channels, int num_output_channels,
70 float* const* output);
71
72 private:
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000073 LappedTransform* const parent_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000074 } blocker_callback_;
75
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000076 const int in_channels_;
77 const int out_channels_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000078
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000079 const int block_length_;
80 const int chunk_length_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000081
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000082 Callback* const block_processor_;
83 Blocker blocker_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000084
andrew@webrtc.org04c50982015-03-19 20:06:29 +000085 rtc::scoped_ptr<RealFourier> fft_;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +000086 const int cplx_length_;
andrew@webrtc.org325cff02014-10-01 17:42:18 +000087 AlignedArray<float> real_buf_;
88 AlignedArray<std::complex<float> > cplx_pre_;
89 AlignedArray<std::complex<float> > cplx_post_;
90};
91
92} // namespace webrtc
93
94#endif // WEBRTC_COMMON_AUDIO_LAPPED_TRANSFORM_H_
95