aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 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_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |
| 13 | |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
| 16 | #include "webrtc/base/checks.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/include/audio_util.h" |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 21 | // Helper to encapsulate a contiguous data buffer with access to a pointer |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame^] | 22 | // array of the deinterleaved channels. The buffer is zero initialized at |
| 23 | // creation. |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 24 | template <typename T> |
| 25 | class ChannelBuffer { |
| 26 | public: |
| 27 | ChannelBuffer(int samples_per_channel, int num_channels) |
| 28 | : data_(new T[samples_per_channel * num_channels]), |
| 29 | channels_(new T*[num_channels]), |
| 30 | samples_per_channel_(samples_per_channel), |
| 31 | num_channels_(num_channels) { |
| 32 | Initialize(); |
| 33 | } |
| 34 | |
| 35 | ChannelBuffer(const T* data, int samples_per_channel, int num_channels) |
| 36 | : data_(new T[samples_per_channel * num_channels]), |
| 37 | channels_(new T*[num_channels]), |
| 38 | samples_per_channel_(samples_per_channel), |
| 39 | num_channels_(num_channels) { |
| 40 | Initialize(); |
| 41 | memcpy(data_.get(), data, length() * sizeof(T)); |
| 42 | } |
| 43 | |
| 44 | ChannelBuffer(const T* const* channels, int samples_per_channel, |
| 45 | int num_channels) |
| 46 | : data_(new T[samples_per_channel * num_channels]), |
| 47 | channels_(new T*[num_channels]), |
| 48 | samples_per_channel_(samples_per_channel), |
| 49 | num_channels_(num_channels) { |
| 50 | Initialize(); |
| 51 | for (int i = 0; i < num_channels_; ++i) |
| 52 | CopyFrom(channels[i], i); |
| 53 | } |
| 54 | |
| 55 | ~ChannelBuffer() {} |
| 56 | |
| 57 | void CopyFrom(const void* channel_ptr, int i) { |
| 58 | DCHECK_LT(i, num_channels_); |
| 59 | memcpy(channels_[i], channel_ptr, samples_per_channel_ * sizeof(T)); |
| 60 | } |
| 61 | |
| 62 | T* data() { return data_.get(); } |
| 63 | const T* data() const { return data_.get(); } |
| 64 | |
| 65 | const T* channel(int i) const { |
| 66 | DCHECK_GE(i, 0); |
| 67 | DCHECK_LT(i, num_channels_); |
| 68 | return channels_[i]; |
| 69 | } |
| 70 | T* channel(int i) { |
| 71 | const ChannelBuffer<T>* t = this; |
| 72 | return const_cast<T*>(t->channel(i)); |
| 73 | } |
| 74 | |
| 75 | T* const* channels() { return channels_.get(); } |
| 76 | const T* const* channels() const { return channels_.get(); } |
| 77 | |
andrew@webrtc.org | 041035b | 2015-01-26 21:23:53 +0000 | [diff] [blame^] | 78 | // Sets the |slice| pointers to the |start_frame| position for each channel. |
| 79 | // Returns |slice| for convenience. |
| 80 | const T* const* Slice(T** slice, int start_frame) const { |
| 81 | DCHECK_LT(start_frame, samples_per_channel_); |
| 82 | for (int i = 0; i < num_channels_; ++i) |
| 83 | slice[i] = &channels_[i][start_frame]; |
| 84 | return slice; |
| 85 | } |
| 86 | T** Slice(T** slice, int start_frame) { |
| 87 | const ChannelBuffer<T>* t = this; |
| 88 | return const_cast<T**>(t->Slice(slice, start_frame)); |
| 89 | } |
| 90 | |
aluebs@webrtc.org | 8789376 | 2014-11-27 23:40:25 +0000 | [diff] [blame] | 91 | int samples_per_channel() const { return samples_per_channel_; } |
| 92 | int num_channels() const { return num_channels_; } |
| 93 | int length() const { return samples_per_channel_ * num_channels_; } |
| 94 | |
| 95 | private: |
| 96 | void Initialize() { |
| 97 | memset(data_.get(), 0, sizeof(T) * length()); |
| 98 | for (int i = 0; i < num_channels_; ++i) |
| 99 | channels_[i] = &data_[i * samples_per_channel_]; |
| 100 | } |
| 101 | |
| 102 | scoped_ptr<T[]> data_; |
| 103 | scoped_ptr<T*[]> channels_; |
| 104 | const int samples_per_channel_; |
| 105 | const int num_channels_; |
| 106 | }; |
| 107 | |
aluebs@webrtc.org | 79b9eba | 2014-11-26 20:21:38 +0000 | [diff] [blame] | 108 | // One int16_t and one float ChannelBuffer that are kept in sync. The sync is |
| 109 | // broken when someone requests write access to either ChannelBuffer, and |
| 110 | // reestablished when someone requests the outdated ChannelBuffer. It is |
| 111 | // therefore safe to use the return value of ibuf_const() and fbuf_const() |
| 112 | // until the next call to ibuf() or fbuf(), and the return value of ibuf() and |
| 113 | // fbuf() until the next call to any of the other functions. |
| 114 | class IFChannelBuffer { |
| 115 | public: |
| 116 | IFChannelBuffer(int samples_per_channel, int num_channels); |
| 117 | |
| 118 | ChannelBuffer<int16_t>* ibuf(); |
| 119 | ChannelBuffer<float>* fbuf(); |
| 120 | const ChannelBuffer<int16_t>* ibuf_const() const; |
| 121 | const ChannelBuffer<float>* fbuf_const() const; |
| 122 | |
| 123 | int num_channels() const { return ibuf_.num_channels(); } |
| 124 | int samples_per_channel() const { return ibuf_.samples_per_channel(); } |
| 125 | |
| 126 | private: |
| 127 | void RefreshF() const; |
| 128 | void RefreshI() const; |
| 129 | |
| 130 | mutable bool ivalid_; |
| 131 | mutable ChannelBuffer<int16_t> ibuf_; |
| 132 | mutable bool fvalid_; |
| 133 | mutable ChannelBuffer<float> fbuf_; |
| 134 | }; |
| 135 | |
| 136 | } // namespace webrtc |
| 137 | |
| 138 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_CHANNEL_BUFFER_H_ |