diff options
author | andrew@webrtc.org <andrew@webrtc.org> | 2014-09-08 20:27:04 +0000 |
---|---|---|
committer | andrew@webrtc.org <andrew@webrtc.org> | 2014-09-08 20:27:04 +0000 |
commit | a0d235763fdcf0820ec97bb51b94a01f547f8334 (patch) | |
tree | 0e34a2cefe28e020c8ea25d706f0f10ac24c456b | |
parent | e8d2da26ff458f6988bd7d12de7cdffae66a3882 (diff) | |
download | webrtc-a0d235763fdcf0820ec97bb51b94a01f547f8334.tar.gz |
Add ctors to ChannelBuffer to enable copying on construction.
Also:
- Fix the constness of some parameters.
- Add more const overloads.
- Use DCHECK in place of assert.
- Removed an unnecessary memset.
R=claguna@google.com
Review URL: https://webrtc-codereview.appspot.com/24469004
git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@7107 4adac7df-926f-26a2-2b94-8c16560cd09d
-rw-r--r-- | common_audio/include/audio_util.h | 2 | ||||
-rw-r--r-- | modules/audio_processing/common.h | 52 |
2 files changed, 42 insertions, 12 deletions
diff --git a/common_audio/include/audio_util.h b/common_audio/include/audio_util.h index 9972a0e0..0ce034be 100644 --- a/common_audio/include/audio_util.h +++ b/common_audio/include/audio_util.h @@ -62,7 +62,7 @@ void ScaleToFloat(const int16_t* src, size_t size, float* dest); // per buffer). template <typename T> void Deinterleave(const T* interleaved, int samples_per_channel, - int num_channels, T** deinterleaved) { + int num_channels, T* const* deinterleaved) { for (int i = 0; i < num_channels; ++i) { T* channel = deinterleaved[i]; int interleaved_idx = i; diff --git a/modules/audio_processing/common.h b/modules/audio_processing/common.h index 98e36cb0..c1bae896 100644 --- a/modules/audio_processing/common.h +++ b/modules/audio_processing/common.h @@ -14,6 +14,7 @@ #include <assert.h> #include <string.h> +#include "webrtc/base/checks.h" #include "webrtc/modules/audio_processing/include/audio_processing.h" #include "webrtc/system_wrappers/interface/scoped_ptr.h" @@ -42,37 +43,66 @@ class ChannelBuffer { channels_(new T*[num_channels]), samples_per_channel_(samples_per_channel), num_channels_(num_channels) { - memset(data_.get(), 0, sizeof(T) * samples_per_channel * num_channels); - for (int i = 0; i < num_channels; ++i) - channels_[i] = &data_[i * samples_per_channel]; + SetChannelPtrs(); } + + ChannelBuffer(const T* data, int samples_per_channel, int num_channels) + : data_(new T[samples_per_channel * num_channels]), + channels_(new T*[num_channels]), + samples_per_channel_(samples_per_channel), + num_channels_(num_channels) { + SetChannelPtrs(); + memcpy(data_.get(), data, length() * sizeof(T)); + } + + ChannelBuffer(const T* const* channels, int samples_per_channel, + int num_channels) + : data_(new T[samples_per_channel * num_channels]), + channels_(new T*[num_channels]), + samples_per_channel_(samples_per_channel), + num_channels_(num_channels) { + SetChannelPtrs(); + for (int i = 0; i < num_channels_; ++i) + CopyFrom(channels[i], i); + } + ~ChannelBuffer() {} void CopyFrom(const void* channel_ptr, int i) { - assert(i < num_channels_); + DCHECK_LT(i, num_channels_); memcpy(channels_[i], channel_ptr, samples_per_channel_ * sizeof(T)); } T* data() { return data_.get(); } + const T* data() const { return data_.get(); } + const T* channel(int i) const { - assert(i >= 0 && i < num_channels_); + DCHECK_GE(i, 0); + DCHECK_LT(i, num_channels_); return channels_[i]; } T* channel(int i) { const ChannelBuffer<T>* t = this; return const_cast<T*>(t->channel(i)); } - T** channels() { return channels_.get(); } - int samples_per_channel() { return samples_per_channel_; } - int num_channels() { return num_channels_; } - int length() { return samples_per_channel_ * num_channels_; } + T* const* channels() { return channels_.get(); } + const T* const* channels() const { return channels_.get(); } + + int samples_per_channel() const { return samples_per_channel_; } + int num_channels() const { return num_channels_; } + int length() const { return samples_per_channel_ * num_channels_; } private: + void SetChannelPtrs() { + for (int i = 0; i < num_channels_; ++i) + channels_[i] = &data_[i * samples_per_channel_]; + } + scoped_ptr<T[]> data_; scoped_ptr<T*[]> channels_; - int samples_per_channel_; - int num_channels_; + const int samples_per_channel_; + const int num_channels_; }; } // namespace webrtc |