diff --git a/webrtc/common_audio/include/audio_util.h b/webrtc/common_audio/include/audio_util.h index 9972a0e067..0ce034be34 100644 --- a/webrtc/common_audio/include/audio_util.h +++ b/webrtc/common_audio/include/audio_util.h @@ -62,7 +62,7 @@ void ScaleToFloat(const int16_t* src, size_t size, float* dest); // per buffer). template 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/webrtc/modules/audio_processing/common.h b/webrtc/modules/audio_processing/common.h index 98e36cb098..c1bae896ce 100644 --- a/webrtc/modules/audio_processing/common.h +++ b/webrtc/modules/audio_processing/common.h @@ -14,6 +14,7 @@ #include #include +#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 = this; return const_cast(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 data_; scoped_ptr channels_; - int samples_per_channel_; - int num_channels_; + const int samples_per_channel_; + const int num_channels_; }; } // namespace webrtc