1 /*
2  *  Copyright (c) 2011 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_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
12 #define WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
13 
14 #include "webrtc/base/scoped_ptr.h"
15 #include "webrtc/common_audio/channel_buffer.h"
16 #include "webrtc/modules/audio_processing/include/audio_processing.h"
17 #include "webrtc/modules/audio_processing/splitting_filter.h"
18 #include "webrtc/modules/include/module_common_types.h"
19 #include "webrtc/system_wrappers/include/scoped_vector.h"
20 #include "webrtc/typedefs.h"
21 
22 namespace webrtc {
23 
24 class PushSincResampler;
25 class IFChannelBuffer;
26 
27 enum Band {
28   kBand0To8kHz = 0,
29   kBand8To16kHz = 1,
30   kBand16To24kHz = 2
31 };
32 
33 class AudioBuffer {
34  public:
35   // TODO(ajm): Switch to take ChannelLayouts.
36   AudioBuffer(size_t input_num_frames,
37               size_t num_input_channels,
38               size_t process_num_frames,
39               size_t num_process_channels,
40               size_t output_num_frames);
41   virtual ~AudioBuffer();
42 
43   size_t num_channels() const;
44   void set_num_channels(size_t num_channels);
45   size_t num_frames() const;
46   size_t num_frames_per_band() const;
47   size_t num_keyboard_frames() const;
48   size_t num_bands() const;
49 
50   // Returns a pointer array to the full-band channels.
51   // Usage:
52   // channels()[channel][sample].
53   // Where:
54   // 0 <= channel < |num_proc_channels_|
55   // 0 <= sample < |proc_num_frames_|
56   int16_t* const* channels();
57   const int16_t* const* channels_const() const;
58   float* const* channels_f();
59   const float* const* channels_const_f() const;
60 
61   // Returns a pointer array to the bands for a specific channel.
62   // Usage:
63   // split_bands(channel)[band][sample].
64   // Where:
65   // 0 <= channel < |num_proc_channels_|
66   // 0 <= band < |num_bands_|
67   // 0 <= sample < |num_split_frames_|
68   int16_t* const* split_bands(size_t channel);
69   const int16_t* const* split_bands_const(size_t channel) const;
70   float* const* split_bands_f(size_t channel);
71   const float* const* split_bands_const_f(size_t channel) const;
72 
73   // Returns a pointer array to the channels for a specific band.
74   // Usage:
75   // split_channels(band)[channel][sample].
76   // Where:
77   // 0 <= band < |num_bands_|
78   // 0 <= channel < |num_proc_channels_|
79   // 0 <= sample < |num_split_frames_|
80   int16_t* const* split_channels(Band band);
81   const int16_t* const* split_channels_const(Band band) const;
82   float* const* split_channels_f(Band band);
83   const float* const* split_channels_const_f(Band band) const;
84 
85   // Returns a pointer to the ChannelBuffer that encapsulates the full-band
86   // data.
87   ChannelBuffer<int16_t>* data();
88   const ChannelBuffer<int16_t>* data() const;
89   ChannelBuffer<float>* data_f();
90   const ChannelBuffer<float>* data_f() const;
91 
92   // Returns a pointer to the ChannelBuffer that encapsulates the split data.
93   ChannelBuffer<int16_t>* split_data();
94   const ChannelBuffer<int16_t>* split_data() const;
95   ChannelBuffer<float>* split_data_f();
96   const ChannelBuffer<float>* split_data_f() const;
97 
98   // Returns a pointer to the low-pass data downmixed to mono. If this data
99   // isn't already available it re-calculates it.
100   const int16_t* mixed_low_pass_data();
101   const int16_t* low_pass_reference(int channel) const;
102 
103   const float* keyboard_data() const;
104 
105   void set_activity(AudioFrame::VADActivity activity);
106   AudioFrame::VADActivity activity() const;
107 
108   // Use for int16 interleaved data.
109   void DeinterleaveFrom(AudioFrame* audioFrame);
110   // If |data_changed| is false, only the non-audio data members will be copied
111   // to |frame|.
112   void InterleaveTo(AudioFrame* frame, bool data_changed);
113 
114   // Use for float deinterleaved data.
115   void CopyFrom(const float* const* data, const StreamConfig& stream_config);
116   void CopyTo(const StreamConfig& stream_config, float* const* data);
117   void CopyLowPassToReference();
118 
119   // Splits the signal into different bands.
120   void SplitIntoFrequencyBands();
121   // Recombine the different bands into one signal.
122   void MergeFrequencyBands();
123 
124  private:
125   // Called from DeinterleaveFrom() and CopyFrom().
126   void InitForNewData();
127 
128   // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
129   // format (samples per channel and number of channels).
130   const size_t input_num_frames_;
131   const size_t num_input_channels_;
132   // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
133   // format.
134   const size_t proc_num_frames_;
135   const size_t num_proc_channels_;
136   // The audio is returned by InterleaveTo() and CopyTo() with output samples
137   // per channels and the current number of channels. This last one can be
138   // changed at any time using set_num_channels().
139   const size_t output_num_frames_;
140   size_t num_channels_;
141 
142   size_t num_bands_;
143   size_t num_split_frames_;
144   bool mixed_low_pass_valid_;
145   bool reference_copied_;
146   AudioFrame::VADActivity activity_;
147 
148   const float* keyboard_data_;
149   rtc::scoped_ptr<IFChannelBuffer> data_;
150   rtc::scoped_ptr<IFChannelBuffer> split_data_;
151   rtc::scoped_ptr<SplittingFilter> splitting_filter_;
152   rtc::scoped_ptr<ChannelBuffer<int16_t> > mixed_low_pass_channels_;
153   rtc::scoped_ptr<ChannelBuffer<int16_t> > low_pass_reference_channels_;
154   rtc::scoped_ptr<IFChannelBuffer> input_buffer_;
155   rtc::scoped_ptr<IFChannelBuffer> output_buffer_;
156   rtc::scoped_ptr<ChannelBuffer<float> > process_buffer_;
157   ScopedVector<PushSincResampler> input_resamplers_;
158   ScopedVector<PushSincResampler> output_resamplers_;
159 };
160 
161 }  // namespace webrtc
162 
163 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
164