Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / common_audio / lapped_transform.h
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"
17 #include "webrtc/common_audio/blocker.h"
18 #include "webrtc/common_audio/real_fourier.h"
19 #include "webrtc/system_wrappers/interface/aligned_array.h"
20 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
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
46   // made internally); can be NULL to disable windowing entirely.
47   // |block_length| defines the length of a block, in samples, even when
48   // windowing is disabled. |shift_length| is in samples. |callback| is the
49   // caller-owned audio processing function called for each block of the input
50   // chunk.
51   LappedTransform(int in_channels, int out_channels, int chunk_length,
52                   const float* window, int block_length, int shift_amount,
53                   Callback* callback);
54   ~LappedTransform();
55
56   // Main audio processing helper method. Internally slices |in_chunk| into
57   // blocks, transforms them to frequency domain, calls the callback for each
58   // block and returns a de-blocked time domain chunk of audio through
59   // |out_chunk|. Both buffers are caller-owned.
60   void ProcessChunk(const float* const* in_chunk, float* const* out_chunk);
61
62  private:
63   // Internal middleware callback, given to the blocker. Transforms each block
64   // and hands it over to the processing method given at construction time.
65   friend class BlockThunk;
66   class BlockThunk : public BlockerCallback {
67    public:
68     explicit BlockThunk(LappedTransform* parent) : parent_(parent) {}
69     virtual ~BlockThunk() {}
70
71     virtual void ProcessBlock(const float* const* input, int num_frames,
72                               int num_input_channels, int num_output_channels,
73                               float* const* output);
74
75    private:
76     LappedTransform* parent_;
77   } blocker_callback_;
78
79   int in_channels_;
80   int out_channels_;
81
82   const float* window_;
83   bool own_window_;
84   int window_shift_amount_;
85
86   int block_length_;
87   int chunk_length_;
88   Callback* block_processor_;
89   scoped_ptr<Blocker> blocker_;
90
91   RealFourier fft_;
92   int cplx_length_;
93   AlignedArray<float> real_buf_;
94   AlignedArray<std::complex<float> > cplx_pre_;
95   AlignedArray<std::complex<float> > cplx_post_;
96 };
97
98 }  // namespace webrtc
99
100 #endif  // WEBRTC_COMMON_AUDIO_LAPPED_TRANSFORM_H_
101