blob: 16d5616a46ec21305f1368665ab2e3ba10da5291 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
12#define MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
kwiberg88788ad2016-02-19 07:04:49 -080017#include <memory>
kwiberg4a206a92016-03-31 10:24:26 -070018#include <vector>
kwiberg88788ad2016-02-19 07:04:49 -080019
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020020#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_audio/channel_buffer.h"
22#include "modules/audio_processing/include/audio_processing.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
niklase@google.com470e71d2011-07-07 08:21:25 +000024namespace webrtc {
25
Steve Antonf254e9e2019-08-21 17:52:28 +000026class IFChannelBuffer;
Yves Gerey988cc082018-10-23 12:03:01 +020027class PushSincResampler;
28class SplittingFilter;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000029
Yves Gerey665174f2018-06-19 15:03:05 +020030enum Band { kBand0To8kHz = 0, kBand8To16kHz = 1, kBand16To24kHz = 2 };
aluebs@webrtc.orga7384a12014-12-03 01:06:35 +000031
niklase@google.com470e71d2011-07-07 08:21:25 +000032class AudioBuffer {
33 public:
Steve Antonf254e9e2019-08-21 17:52:28 +000034 // TODO(ajm): Switch to take ChannelLayouts.
35 AudioBuffer(size_t input_num_frames,
36 size_t num_input_channels,
37 size_t process_num_frames,
38 size_t num_process_channels,
39 size_t output_num_frames);
niklase@google.com470e71d2011-07-07 08:21:25 +000040 virtual ~AudioBuffer();
41
Steve Antonf254e9e2019-08-21 17:52:28 +000042 size_t num_channels() const;
43 size_t num_proc_channels() const { return num_proc_channels_; }
Per Åhgren81c0cf22019-08-21 15:02:37 +020044 void set_num_channels(size_t num_channels);
Steve Antonf254e9e2019-08-21 17:52:28 +000045 size_t num_frames() const;
46 size_t num_frames_per_band() const;
47 size_t num_bands() const;
Per Åhgren81c0cf22019-08-21 15:02:37 +020048
Steve Antonf254e9e2019-08-21 17:52:28 +000049 // Returns a pointer array to the full-band channels.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000050 // Usage:
51 // channels()[channel][sample].
52 // Where:
Steve Antonf254e9e2019-08-21 17:52:28 +000053 // 0 <= channel < |num_proc_channels_|
54 // 0 <= sample < |proc_num_frames_|
55 float* const* channels_f();
56 const float* const* channels_const_f() const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000057
Steve Antonf254e9e2019-08-21 17:52:28 +000058 // Returns a pointer array to the bands for a specific channel.
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000059 // Usage:
60 // split_bands(channel)[band][sample].
61 // Where:
Steve Antonf254e9e2019-08-21 17:52:28 +000062 // 0 <= channel < |num_proc_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000063 // 0 <= band < |num_bands_|
64 // 0 <= sample < |num_split_frames_|
Steve Antonf254e9e2019-08-21 17:52:28 +000065 float* const* split_bands_f(size_t channel);
66 const float* const* split_bands_const_f(size_t channel) const;
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000067
68 // Returns a pointer array to the channels for a specific band.
69 // Usage:
70 // split_channels(band)[channel][sample].
71 // Where:
72 // 0 <= band < |num_bands_|
Steve Antonf254e9e2019-08-21 17:52:28 +000073 // 0 <= channel < |num_proc_channels_|
aluebs@webrtc.org3aca0b02015-02-26 21:52:20 +000074 // 0 <= sample < |num_split_frames_|
Steve Antonf254e9e2019-08-21 17:52:28 +000075 const float* const* split_channels_const_f(Band band) const;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000076
Steve Antonf254e9e2019-08-21 17:52:28 +000077 // Use for int16 interleaved data.
78 void DeinterleaveFrom(const AudioFrame* audioFrame);
79 // If |data_changed| is false, only the non-audio data members will be copied
80 // to |frame|.
81 void InterleaveTo(AudioFrame* frame) const;
82
83 // Use for float deinterleaved data.
Michael Graczyk86c6d332015-07-23 11:41:39 -070084 void CopyFrom(const float* const* data, const StreamConfig& stream_config);
85 void CopyTo(const StreamConfig& stream_config, float* const* data);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
Steve Antonf254e9e2019-08-21 17:52:28 +000087 // Splits the signal into different bands.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000088 void SplitIntoFrequencyBands();
Steve Antonf254e9e2019-08-21 17:52:28 +000089 // Recombine the different bands into one signal.
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +000090 void MergeFrequencyBands();
91
Per Åhgren928146f2019-08-20 09:19:21 +020092 // Copies the split bands data into the integer two-dimensional array.
Steve Antonf254e9e2019-08-21 17:52:28 +000093 void CopySplitChannelDataTo(size_t channel, int16_t* const* split_band_data);
Per Åhgren928146f2019-08-20 09:19:21 +020094
95 // Copies the data in the integer two-dimensional array into the split_bands
96 // data.
Steve Antonf254e9e2019-08-21 17:52:28 +000097 void CopySplitChannelDataFrom(size_t channel,
98 const int16_t* const* split_band_data);
Per Åhgren928146f2019-08-20 09:19:21 +020099
100 static const size_t kMaxSplitFrameLength = 160;
101 static const size_t kMaxNumBands = 3;
102
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 private:
Alejandro Luebsa181c9a2016-06-30 15:33:37 -0700104 FRIEND_TEST_ALL_PREFIXES(AudioBufferTest,
105 SetNumChannelsSetsChannelBuffersNumChannels);
Steve Antonf254e9e2019-08-21 17:52:28 +0000106 // Called from DeinterleaveFrom() and CopyFrom().
107 void InitForNewData();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000108
Steve Antonf254e9e2019-08-21 17:52:28 +0000109 // The audio is passed into DeinterleaveFrom() or CopyFrom() with input
110 // format (samples per channel and number of channels).
Peter Kastingdce40cf2015-08-24 14:52:23 -0700111 const size_t input_num_frames_;
Steve Antonf254e9e2019-08-21 17:52:28 +0000112 const size_t num_input_channels_;
113 // The audio is stored by DeinterleaveFrom() or CopyFrom() with processing
114 // format.
115 const size_t proc_num_frames_;
116 const size_t num_proc_channels_;
117 // The audio is returned by InterleaveTo() and CopyTo() with output samples
118 // per channels and the current number of channels. This last one can be
119 // changed at any time using set_num_channels().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 const size_t output_num_frames_;
Per Åhgren81c0cf22019-08-21 15:02:37 +0200121 size_t num_channels_;
Steve Antonf254e9e2019-08-21 17:52:28 +0000122
Peter Kastingdce40cf2015-08-24 14:52:23 -0700123 size_t num_bands_;
124 size_t num_split_frames_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
Steve Antonf254e9e2019-08-21 17:52:28 +0000126 std::unique_ptr<IFChannelBuffer> data_;
127 std::unique_ptr<IFChannelBuffer> split_data_;
kwiberg88788ad2016-02-19 07:04:49 -0800128 std::unique_ptr<SplittingFilter> splitting_filter_;
Steve Antonf254e9e2019-08-21 17:52:28 +0000129 std::unique_ptr<IFChannelBuffer> input_buffer_;
130 std::unique_ptr<IFChannelBuffer> output_buffer_;
131 std::unique_ptr<ChannelBuffer<float>> process_buffer_;
kwiberg4a206a92016-03-31 10:24:26 -0700132 std::vector<std::unique_ptr<PushSincResampler>> input_resamplers_;
133 std::vector<std::unique_ptr<PushSincResampler>> output_resamplers_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134};
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000135
niklase@google.com470e71d2011-07-07 08:21:25 +0000136} // namespace webrtc
137
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200138#endif // MODULES_AUDIO_PROCESSING_AUDIO_BUFFER_H_