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 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/blocker.h" |
| 18 | #include "webrtc/common_audio/real_fourier.h" |
| 19 | #include "webrtc/system_wrappers/interface/aligned_array.h" |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 20 | |
| 21 | namespace 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. |
| 31 | class LappedTransform { |
| 32 | public: |
| 33 | class Callback { |
| 34 | public: |
| 35 | virtual ~Callback() {} |
| 36 | |
| 37 | virtual void ProcessAudioBlock(const std::complex<float>* const* in_block, |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame^] | 38 | int num_in_channels, int frames, |
| 39 | int num_out_channels, |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 40 | std::complex<float>* const* out_block) = 0; |
| 41 | }; |
| 42 | |
| 43 | // Construct a transform instance. |chunk_length| is the number of samples in |
| 44 | // 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] | 45 | // made internally); |window| should have length equal to |block_length|. |
| 46 | // |block_length| defines the length of a block, in samples. |
| 47 | // |shift_amount| is in samples. |callback| is the caller-owned audio |
| 48 | // processing function called for each block of the input chunk. |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame^] | 49 | LappedTransform(int num_in_channels, int num_out_channels, int chunk_length, |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 50 | const float* window, int block_length, int shift_amount, |
| 51 | Callback* callback); |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 52 | ~LappedTransform() {} |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 53 | |
| 54 | // Main audio processing helper method. Internally slices |in_chunk| into |
| 55 | // blocks, transforms them to frequency domain, calls the callback for each |
| 56 | // block and returns a de-blocked time domain chunk of audio through |
| 57 | // |out_chunk|. Both buffers are caller-owned. |
| 58 | void ProcessChunk(const float* const* in_chunk, float* const* out_chunk); |
| 59 | |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 60 | // Get the chunk length. |
| 61 | // |
| 62 | // The chunk length is the number of samples per channel that must be passed |
| 63 | // to ProcessChunk via the parameter in_chunk. |
| 64 | // |
| 65 | // Returns the same chunk_length passed to the LappedTransform constructor. |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame^] | 66 | int chunk_length() const { return chunk_length_; } |
| 67 | |
| 68 | // Get the number of input channels. |
| 69 | // |
| 70 | // This is the number of arrays that must be passed to ProcessChunk via |
| 71 | // in_chunk. |
| 72 | // |
| 73 | // Returns the same num_in_channels passed to the LappedTransform constructor. |
| 74 | int num_in_channels() const { return num_in_channels_; } |
| 75 | |
| 76 | // Get the number of output channels. |
| 77 | // |
| 78 | // This is the number of arrays that must be passed to ProcessChunk via |
| 79 | // out_chunk. |
| 80 | // |
| 81 | // Returns the same num_out_channels passed to the LappedTransform |
| 82 | // constructor. |
| 83 | int num_out_channels() const { return num_out_channels_; } |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 84 | |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 85 | private: |
| 86 | // Internal middleware callback, given to the blocker. Transforms each block |
| 87 | // 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] | 88 | class BlockThunk : public BlockerCallback { |
| 89 | public: |
| 90 | explicit BlockThunk(LappedTransform* parent) : parent_(parent) {} |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 91 | |
| 92 | virtual void ProcessBlock(const float* const* input, int num_frames, |
| 93 | int num_input_channels, int num_output_channels, |
| 94 | float* const* output); |
| 95 | |
| 96 | private: |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 97 | LappedTransform* const parent_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 98 | } blocker_callback_; |
| 99 | |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame^] | 100 | const int num_in_channels_; |
| 101 | const int num_out_channels_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 102 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 103 | const int block_length_; |
| 104 | const int chunk_length_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 105 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 106 | Callback* const block_processor_; |
| 107 | Blocker blocker_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 108 | |
andrew@webrtc.org | 04c5098 | 2015-03-19 20:06:29 +0000 | [diff] [blame] | 109 | rtc::scoped_ptr<RealFourier> fft_; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 110 | const int cplx_length_; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 111 | AlignedArray<float> real_buf_; |
| 112 | AlignedArray<std::complex<float> > cplx_pre_; |
| 113 | AlignedArray<std::complex<float> > cplx_post_; |
| 114 | }; |
| 115 | |
| 116 | } // namespace webrtc |
| 117 | |
| 118 | #endif // WEBRTC_COMMON_AUDIO_LAPPED_TRANSFORM_H_ |
| 119 | |