Fix constness of AudioBuffer accessors.

Don't return non-const pointers from const accessors and deal with the
spillover. Provide overloaded versions as needed.

Inspired by kwiberg:
https://webrtc-codereview.appspot.com/12379005/

R=bjornv@webrtc.org, kwiberg@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/15379004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6030 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
andrew@webrtc.org 2014-04-30 16:44:13 +00:00
parent 740e6b339a
commit 65f933899b
12 changed files with 64 additions and 46 deletions

View File

@ -69,7 +69,7 @@ int WebRtcVad_set_mode(VadInst* handle, int mode);
// returns : 1 - (Active Voice),
// 0 - (Non-active Voice),
// -1 - (Error)
int WebRtcVad_Process(VadInst* handle, int fs, int16_t* audio_frame,
int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
int frame_length);
// Checks for valid combinations of |rate| and |frame_length|. We support 10,

View File

@ -603,7 +603,7 @@ int WebRtcVad_set_mode_core(VadInstT* self, int mode) {
// Calculate VAD decision by first extracting feature values and then calculate
// probability for both speech and background noise.
int WebRtcVad_CalcVad48khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad48khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length) {
int vad;
int i;
@ -628,7 +628,7 @@ int WebRtcVad_CalcVad48khz(VadInstT* inst, int16_t* speech_frame,
return vad;
}
int WebRtcVad_CalcVad32khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad32khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length)
{
int len, vad;
@ -650,7 +650,7 @@ int WebRtcVad_CalcVad32khz(VadInstT* inst, int16_t* speech_frame,
return vad;
}
int WebRtcVad_CalcVad16khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad16khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length)
{
int len, vad;
@ -666,7 +666,7 @@ int WebRtcVad_CalcVad16khz(VadInstT* inst, int16_t* speech_frame,
return vad;
}
int WebRtcVad_CalcVad8khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad8khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length)
{
int16_t feature_vector[kNumChannels], total_power;

View File

@ -85,9 +85,9 @@ int WebRtcVad_set_mode_core(VadInstT* self, int mode);
/****************************************************************************
* WebRtcVad_CalcVad48khz(...)
* WebRtcVad_CalcVad32khz(...)
* WebRtcVad_CalcVad16khz(...)
* WebRtcVad_CalcVad8khz(...)
* WebRtcVad_CalcVad32khz(...)
* WebRtcVad_CalcVad16khz(...)
* WebRtcVad_CalcVad8khz(...)
*
* Calculate probability for active speech and make VAD decision.
*
@ -103,13 +103,13 @@ int WebRtcVad_set_mode_core(VadInstT* self, int mode);
* 0 - No active speech
* 1-6 - Active speech
*/
int WebRtcVad_CalcVad48khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad48khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length);
int WebRtcVad_CalcVad32khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad32khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length);
int WebRtcVad_CalcVad16khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad16khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length);
int WebRtcVad_CalcVad8khz(VadInstT* inst, int16_t* speech_frame,
int WebRtcVad_CalcVad8khz(VadInstT* inst, const int16_t* speech_frame,
int frame_length);
#endif // WEBRTC_COMMON_AUDIO_VAD_VAD_CORE_H_

View File

@ -24,7 +24,7 @@ static const int16_t kSmoothingUp = 32439; // 0.99 in Q15.
// TODO(bjornv): Move this function to vad_filterbank.c.
// Downsampling filter based on splitting filter and allpass functions.
void WebRtcVad_Downsampling(int16_t* signal_in,
void WebRtcVad_Downsampling(const int16_t* signal_in,
int16_t* signal_out,
int32_t* filter_state,
int in_length) {

View File

@ -30,7 +30,7 @@
//
// Output:
// - signal_out : Downsampled signal (of length |in_length| / 2).
void WebRtcVad_Downsampling(int16_t* signal_in,
void WebRtcVad_Downsampling(const int16_t* signal_in,
int16_t* signal_out,
int32_t* filter_state,
int in_length);

View File

@ -68,7 +68,7 @@ int WebRtcVad_set_mode(VadInst* handle, int mode) {
return WebRtcVad_set_mode_core(self, mode);
}
int WebRtcVad_Process(VadInst* handle, int fs, int16_t* audio_frame,
int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
int frame_length) {
int vad = -1;
VadInstT* self = (VadInstT*) handle;

View File

@ -228,7 +228,7 @@ void AudioBuffer::InitForNewData() {
is_muted_ = false;
}
int16_t* AudioBuffer::data(int channel) const {
const int16_t* AudioBuffer::data(int channel) const {
assert(channel >= 0 && channel < num_proc_channels_);
if (data_ != NULL) {
return data_;
@ -237,7 +237,12 @@ int16_t* AudioBuffer::data(int channel) const {
return channels_->channel(channel);
}
int16_t* AudioBuffer::low_pass_split_data(int channel) const {
int16_t* AudioBuffer::data(int channel) {
const AudioBuffer* t = this;
return const_cast<int16_t*>(t->data(channel));
}
const int16_t* AudioBuffer::low_pass_split_data(int channel) const {
assert(channel >= 0 && channel < num_proc_channels_);
if (split_channels_.get() == NULL) {
return data(channel);
@ -246,7 +251,12 @@ int16_t* AudioBuffer::low_pass_split_data(int channel) const {
return split_channels_->low_channel(channel);
}
int16_t* AudioBuffer::high_pass_split_data(int channel) const {
int16_t* AudioBuffer::low_pass_split_data(int channel) {
const AudioBuffer* t = this;
return const_cast<int16_t*>(t->low_pass_split_data(channel));
}
const int16_t* AudioBuffer::high_pass_split_data(int channel) const {
assert(channel >= 0 && channel < num_proc_channels_);
if (split_channels_.get() == NULL) {
return NULL;
@ -255,19 +265,24 @@ int16_t* AudioBuffer::high_pass_split_data(int channel) const {
return split_channels_->high_channel(channel);
}
int16_t* AudioBuffer::mixed_data(int channel) const {
int16_t* AudioBuffer::high_pass_split_data(int channel) {
const AudioBuffer* t = this;
return const_cast<int16_t*>(t->high_pass_split_data(channel));
}
const int16_t* AudioBuffer::mixed_data(int channel) const {
assert(channel >= 0 && channel < num_mixed_channels_);
return mixed_channels_->channel(channel);
}
int16_t* AudioBuffer::mixed_low_pass_data(int channel) const {
const int16_t* AudioBuffer::mixed_low_pass_data(int channel) const {
assert(channel >= 0 && channel < num_mixed_low_pass_channels_);
return mixed_low_pass_channels_->channel(channel);
}
int16_t* AudioBuffer::low_pass_reference(int channel) const {
const int16_t* AudioBuffer::low_pass_reference(int channel) const {
assert(channel >= 0 && channel < num_proc_channels_);
if (!reference_copied_) {
return NULL;
@ -280,7 +295,7 @@ const float* AudioBuffer::keyboard_data() const {
return keyboard_data_;
}
SplitFilterStates* AudioBuffer::filter_states(int channel) const {
SplitFilterStates* AudioBuffer::filter_states(int channel) {
assert(channel >= 0 && channel < num_proc_channels_);
return &filter_states_[channel];
}

View File

@ -55,15 +55,18 @@ class AudioBuffer {
int samples_per_split_channel() const;
int samples_per_keyboard_channel() const;
int16_t* data(int channel) const;
int16_t* low_pass_split_data(int channel) const;
int16_t* high_pass_split_data(int channel) const;
int16_t* mixed_data(int channel) const;
int16_t* mixed_low_pass_data(int channel) const;
int16_t* low_pass_reference(int channel) const;
int16_t* data(int channel);
const int16_t* data(int channel) const;
int16_t* low_pass_split_data(int channel);
const int16_t* low_pass_split_data(int channel) const;
int16_t* high_pass_split_data(int channel);
const int16_t* high_pass_split_data(int channel) const;
const int16_t* mixed_data(int channel) const;
const int16_t* mixed_low_pass_data(int channel) const;
const int16_t* low_pass_reference(int channel) const;
const float* keyboard_data() const;
SplitFilterStates* filter_states(int channel) const;
SplitFilterStates* filter_states(int channel);
void set_activity(AudioFrame::VADActivity activity);
AudioFrame::VADActivity activity() const;

View File

@ -128,7 +128,7 @@ int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) {
for (int i = 0; i < audio->num_channels(); i++) {
// TODO(ajm): improve how this works, possibly inside AECM.
// This is kind of hacked up.
int16_t* noisy = audio->low_pass_reference(i);
const int16_t* noisy = audio->low_pass_reference(i);
int16_t* clean = audio->low_pass_split_data(i);
if (noisy == NULL) {
noisy = clean;

View File

@ -59,7 +59,7 @@ int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) {
assert(audio->samples_per_split_channel() <= 160);
int16_t* mixed_data = audio->low_pass_split_data(0);
const int16_t* mixed_data = audio->low_pass_split_data(0);
if (audio->num_channels() > 1) {
audio->CopyAndMixLowPass(1);
mixed_data = audio->mixed_low_pass_data(0);

View File

@ -20,7 +20,15 @@
namespace webrtc {
namespace {
const double kMaxSquaredLevel = 32768.0 * 32768.0;
const float kMaxSquaredLevel = 32768.0 * 32768.0;
float SumSquare(const int16_t* data, int length) {
float sum_square = 0.f;
for (int i = 0; i < length; ++i) {
sum_square += data[i] * data[i];
}
return sum_square;
}
class Level {
public:
@ -36,7 +44,7 @@ class Level {
sample_count_ = 0;
}
void Process(int16_t* data, int length) {
void Process(const int16_t* data, int length) {
assert(data != NULL);
assert(length > 0);
sum_square_ += SumSquare(data, length);
@ -55,7 +63,7 @@ class Level {
}
// Normalize by the max level.
double rms = sum_square_ / (sample_count_ * kMaxSquaredLevel);
float rms = sum_square_ / (sample_count_ * kMaxSquaredLevel);
// 20log_10(x^0.5) = 10log_10(x)
rms = 10 * log10(rms);
if (rms > 0)
@ -69,18 +77,10 @@ class Level {
}
private:
static double SumSquare(int16_t* data, int length) {
double sum_square = 0.0;
for (int i = 0; i < length; ++i) {
double data_d = static_cast<double>(data[i]);
sum_square += data_d * data_d;
}
return sum_square;
}
double sum_square_;
float sum_square_;
int sample_count_;
};
} // namespace
LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessing* apm,
@ -102,7 +102,7 @@ int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
return apm_->kNoError;
}
int16_t* mixed_data = audio->data(0);
const int16_t* mixed_data = audio->data(0);
if (audio->num_channels() > 1) {
audio->CopyAndMix(1);
mixed_data = audio->mixed_data(0);

View File

@ -61,7 +61,7 @@ int VoiceDetectionImpl::ProcessCaptureAudio(AudioBuffer* audio) {
}
assert(audio->samples_per_split_channel() <= 160);
int16_t* mixed_data = audio->low_pass_split_data(0);
const int16_t* mixed_data = audio->low_pass_split_data(0);
if (audio->num_channels() > 1) {
audio->CopyAndMixLowPass(1);
mixed_data = audio->mixed_low_pass_data(0);