andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 17 | #include "webrtc/base/scoped_ptr.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 18 | #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.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 21 | |
| 22 | namespace 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. |
| 32 | class 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.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 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. |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 50 | 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.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 53 | ~LappedTransform() {} |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 54 | |
| 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.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 64 | class BlockThunk : public BlockerCallback { |
| 65 | public: |
| 66 | explicit BlockThunk(LappedTransform* parent) : parent_(parent) {} |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 67 | |
| 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.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 73 | LappedTransform* const parent_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 74 | } blocker_callback_; |
| 75 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 76 | const int in_channels_; |
| 77 | const int out_channels_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 78 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 79 | const int block_length_; |
| 80 | const int chunk_length_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 81 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 82 | Callback* const block_processor_; |
| 83 | Blocker blocker_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 84 | |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame^] | 85 | rtc::scoped_ptr<RealFourier> fft_; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 86 | const int cplx_length_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 87 | 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 | |