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 <vector>
15
16#include "webrtc/modules/audio_processing/common.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
18#include "webrtc/modules/interface/module_common_types.h"
19#include "webrtc/system_wrappers/interface/scoped_ptr.h"
20#include "webrtc/system_wrappers/interface/scoped_vector.h"
21#include "webrtc/typedefs.h"
22
23namespace webrtc {
24
25class PushSincResampler;
26class IFChannelBuffer;
27
28struct SplitFilterStates {
29  SplitFilterStates() {
30    memset(analysis_filter_state1, 0, sizeof(analysis_filter_state1));
31    memset(analysis_filter_state2, 0, sizeof(analysis_filter_state2));
32    memset(synthesis_filter_state1, 0, sizeof(synthesis_filter_state1));
33    memset(synthesis_filter_state2, 0, sizeof(synthesis_filter_state2));
34  }
35
36  static const int kStateSize = 6;
37  int analysis_filter_state1[kStateSize];
38  int analysis_filter_state2[kStateSize];
39  int synthesis_filter_state1[kStateSize];
40  int synthesis_filter_state2[kStateSize];
41};
42
43class AudioBuffer {
44 public:
45  // TODO(ajm): Switch to take ChannelLayouts.
46  AudioBuffer(int input_samples_per_channel,
47              int num_input_channels,
48              int process_samples_per_channel,
49              int num_process_channels,
50              int output_samples_per_channel);
51  virtual ~AudioBuffer();
52
53  int num_channels() const;
54  int samples_per_channel() const;
55  int samples_per_split_channel() const;
56  int samples_per_keyboard_channel() const;
57
58  // Sample array accessors. Channels are guaranteed to be stored contiguously
59  // in memory. Prefer to use the const variants of each accessor when
60  // possible, since they incur less float<->int16 conversion overhead.
61  int16_t* data(int channel);
62  const int16_t* data(int channel) const;
63  int16_t* low_pass_split_data(int channel);
64  const int16_t* low_pass_split_data(int channel) const;
65  int16_t* high_pass_split_data(int channel);
66  const int16_t* high_pass_split_data(int channel) const;
67  // Returns a pointer to the low-pass data downmixed to mono. If this data
68  // isn't already available it re-calculates it.
69  const int16_t* mixed_low_pass_data();
70  const int16_t* low_pass_reference(int channel) const;
71
72  // Float versions of the accessors, with automatic conversion back and forth
73  // as necessary. The range of the numbers are the same as for int16_t.
74  float* data_f(int channel);
75  const float* data_f(int channel) const;
76  float* low_pass_split_data_f(int channel);
77  const float* low_pass_split_data_f(int channel) const;
78  float* high_pass_split_data_f(int channel);
79  const float* high_pass_split_data_f(int channel) const;
80
81  const float* keyboard_data() const;
82
83  SplitFilterStates* filter_states(int channel);
84
85  void set_activity(AudioFrame::VADActivity activity);
86  AudioFrame::VADActivity activity() const;
87
88  // Use for int16 interleaved data.
89  void DeinterleaveFrom(AudioFrame* audioFrame);
90  // If |data_changed| is false, only the non-audio data members will be copied
91  // to |frame|.
92  void InterleaveTo(AudioFrame* frame, bool data_changed) const;
93
94  // Use for float deinterleaved data.
95  void CopyFrom(const float* const* data,
96                int samples_per_channel,
97                AudioProcessing::ChannelLayout layout);
98  void CopyTo(int samples_per_channel,
99              AudioProcessing::ChannelLayout layout,
100              float* const* data);
101  void CopyLowPassToReference();
102
103 private:
104  // Called from DeinterleaveFrom() and CopyFrom().
105  void InitForNewData();
106
107  const int input_samples_per_channel_;
108  const int num_input_channels_;
109  const int proc_samples_per_channel_;
110  const int num_proc_channels_;
111  const int output_samples_per_channel_;
112  int samples_per_split_channel_;
113  bool mixed_low_pass_valid_;
114  bool reference_copied_;
115  AudioFrame::VADActivity activity_;
116
117  const float* keyboard_data_;
118  scoped_ptr<IFChannelBuffer> channels_;
119  scoped_ptr<IFChannelBuffer> split_channels_low_;
120  scoped_ptr<IFChannelBuffer> split_channels_high_;
121  scoped_ptr<SplitFilterStates[]> filter_states_;
122  scoped_ptr<ChannelBuffer<int16_t> > mixed_low_pass_channels_;
123  scoped_ptr<ChannelBuffer<int16_t> > low_pass_reference_channels_;
124  scoped_ptr<ChannelBuffer<float> > input_buffer_;
125  scoped_ptr<ChannelBuffer<float> > process_buffer_;
126  ScopedVector<PushSincResampler> input_resamplers_;
127  ScopedVector<PushSincResampler> output_resamplers_;
128};
129
130}  // namespace webrtc
131
132#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
133