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@7107 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
andrew@webrtc.org 2014-09-08 20:27:04 +00:00
parent fd42f9dd6f
commit 17454f79dc
2 changed files with 42 additions and 12 deletions

View File

@ -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;

View File

@ -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