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 MODULES_AUDIO_PROCESSING_SPLITTING_FILTER_H_
12 #define MODULES_AUDIO_PROCESSING_SPLITTING_FILTER_H_
13 
14 #include <cstring>
15 #include <memory>
16 #include <vector>
17 
18 #include "common_audio/channel_buffer.h"
19 #include "modules/audio_processing/three_band_filter_bank.h"
20 
21 namespace webrtc {
22 
23 struct TwoBandsStates {
TwoBandsStatesTwoBandsStates24   TwoBandsStates() {
25     memset(analysis_state1, 0, sizeof(analysis_state1));
26     memset(analysis_state2, 0, sizeof(analysis_state2));
27     memset(synthesis_state1, 0, sizeof(synthesis_state1));
28     memset(synthesis_state2, 0, sizeof(synthesis_state2));
29   }
30 
31   static const int kStateSize = 6;
32   int analysis_state1[kStateSize];
33   int analysis_state2[kStateSize];
34   int synthesis_state1[kStateSize];
35   int synthesis_state2[kStateSize];
36 };
37 
38 // Splitting filter which is able to split into and merge from 2 or 3 frequency
39 // bands. The number of channels needs to be provided at construction time.
40 //
41 // For each block, Analysis() is called to split into bands and then Synthesis()
42 // to merge these bands again. The input and output signals are contained in
43 // ChannelBuffers and for the different bands an array of ChannelBuffers is
44 // used.
45 class SplittingFilter {
46  public:
47   SplittingFilter(size_t num_channels, size_t num_bands, size_t num_frames);
48   ~SplittingFilter();
49 
50   void Analysis(const ChannelBuffer<float>* data, ChannelBuffer<float>* bands);
51   void Synthesis(const ChannelBuffer<float>* bands, ChannelBuffer<float>* data);
52 
53  private:
54   // Two-band analysis and synthesis work for 640 samples or less.
55   void TwoBandsAnalysis(const ChannelBuffer<float>* data,
56                         ChannelBuffer<float>* bands);
57   void TwoBandsSynthesis(const ChannelBuffer<float>* bands,
58                          ChannelBuffer<float>* data);
59   void ThreeBandsAnalysis(const ChannelBuffer<float>* data,
60                           ChannelBuffer<float>* bands);
61   void ThreeBandsSynthesis(const ChannelBuffer<float>* bands,
62                            ChannelBuffer<float>* data);
63   void InitBuffers();
64 
65   const size_t num_bands_;
66   std::vector<TwoBandsStates> two_bands_states_;
67   std::vector<ThreeBandFilterBank> three_band_filter_banks_;
68 };
69 
70 }  // namespace webrtc
71 
72 #endif  // MODULES_AUDIO_PROCESSING_SPLITTING_FILTER_H_
73