blob: 1ab2a9fee758300d863267aa31ac50938a10fc4a [file] [log] [blame]
andrew@webrtc.org325cff02014-10-01 17:42:18 +00001/*
peahfaed4ab2016-04-05 14:57:48 -07002 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
andrew@webrtc.org325cff02014-10-01 17:42:18 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_AUDIO_LAPPED_TRANSFORM_H_
12#define COMMON_AUDIO_LAPPED_TRANSFORM_H_
andrew@webrtc.org325cff02014-10-01 17:42:18 +000013
peahfaed4ab2016-04-05 14:57:48 -070014#include <complex>
15#include <memory>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/blocker.h"
18#include "common_audio/real_fourier.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010019#include "rtc_base/memory/aligned_array.h"
peahfaed4ab2016-04-05 14:57:48 -070020
21namespace webrtc {
22
23// Helper class for audio processing modules which operate on frequency domain
24// input derived from the windowed time domain audio stream.
25//
26// The input audio chunk is sliced into possibly overlapping blocks, multiplied
27// by a window and transformed with an FFT implementation. The transformed data
28// is supplied to the given callback for processing. The processed output is
29// then inverse transformed into the time domain and spliced back into a chunk
30// which constitutes the final output of this processing module.
31class LappedTransform {
32 public:
33 class Callback {
34 public:
35 virtual ~Callback() {}
36
37 virtual void ProcessAudioBlock(const std::complex<float>* const* in_block,
Yves Gerey665174f2018-06-19 15:03:05 +020038 size_t num_in_channels,
39 size_t frames,
peahfaed4ab2016-04-05 14:57:48 -070040 size_t num_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
46 // 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.
50 LappedTransform(size_t num_in_channels,
51 size_t num_out_channels,
52 size_t chunk_length,
53 const float* window,
54 size_t block_length,
55 size_t shift_amount,
56 Callback* callback);
kwiberg942c8512016-08-29 13:10:29 -070057 ~LappedTransform();
peahfaed4ab2016-04-05 14:57:48 -070058
59 // Main audio processing helper method. Internally slices |in_chunk| into
60 // blocks, transforms them to frequency domain, calls the callback for each
61 // block and returns a de-blocked time domain chunk of audio through
62 // |out_chunk|. Both buffers are caller-owned.
63 void ProcessChunk(const float* const* in_chunk, float* const* out_chunk);
64
65 // Get the chunk length.
66 //
67 // The chunk length is the number of samples per channel that must be passed
68 // to ProcessChunk via the parameter in_chunk.
69 //
70 // Returns the same chunk_length passed to the LappedTransform constructor.
71 size_t chunk_length() const { return chunk_length_; }
72
73 // Get the number of input channels.
74 //
75 // This is the number of arrays that must be passed to ProcessChunk via
76 // in_chunk.
77 //
78 // Returns the same num_in_channels passed to the LappedTransform constructor.
79 size_t num_in_channels() const { return num_in_channels_; }
80
81 // Get the number of output channels.
82 //
83 // This is the number of arrays that must be passed to ProcessChunk via
84 // out_chunk.
85 //
86 // Returns the same num_out_channels passed to the LappedTransform
87 // constructor.
88 size_t num_out_channels() const { return num_out_channels_; }
89
Alejandro Luebsef009252016-09-20 14:51:56 -070090 // Returns the initial delay.
91 //
92 // This is the delay introduced by the |blocker_| to be able to get and return
93 // chunks of |chunk_length|, but process blocks of |block_length|.
94 size_t initial_delay() const { return blocker_.initial_delay(); }
95
peahfaed4ab2016-04-05 14:57:48 -070096 private:
97 // Internal middleware callback, given to the blocker. Transforms each block
98 // and hands it over to the processing method given at construction time.
99 class BlockThunk : public BlockerCallback {
100 public:
101 explicit BlockThunk(LappedTransform* parent) : parent_(parent) {}
102
oprypin67fdb802017-03-09 06:25:06 -0800103 void ProcessBlock(const float* const* input,
104 size_t num_frames,
105 size_t num_input_channels,
106 size_t num_output_channels,
107 float* const* output) override;
peahfaed4ab2016-04-05 14:57:48 -0700108
109 private:
110 LappedTransform* const parent_;
111 } blocker_callback_;
112
113 const size_t num_in_channels_;
114 const size_t num_out_channels_;
115
116 const size_t block_length_;
117 const size_t chunk_length_;
118
119 Callback* const block_processor_;
120 Blocker blocker_;
121
122 std::unique_ptr<RealFourier> fft_;
123 const size_t cplx_length_;
124 AlignedArray<float> real_buf_;
125 AlignedArray<std::complex<float> > cplx_pre_;
126 AlignedArray<std::complex<float> > cplx_post_;
127};
128
129} // namespace webrtc
andrew@webrtc.org325cff02014-10-01 17:42:18 +0000130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // COMMON_AUDIO_LAPPED_TRANSFORM_H_