This source file includes following definitions.
- MaxAmplitude
- level_
- Calculate
#include "content/renderer/media/media_stream_audio_level_calculator.h"
#include "base/logging.h"
#include "base/stl_util.h"
namespace content {
namespace {
int MaxAmplitude(const int16* audio_data, int length) {
int max = 0, absolute = 0;
for (int i = 0; i < length; ++i) {
absolute = std::abs(audio_data[i]);
if (absolute > max)
max = absolute;
}
DCHECK(max <= std::abs(std::numeric_limits<int16>::min()));
return max;
}
}
MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator()
: counter_(0),
max_amplitude_(0),
level_(0) {
}
MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
}
int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data,
int number_of_channels,
int number_of_frames) {
DCHECK(thread_checker_.CalledOnValidThread());
static const int kUpdateFrequency = 10;
int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames);
max_amplitude_ = std::max(max_amplitude_, max);
if (counter_++ == kUpdateFrequency) {
level_ = max_amplitude_;
max_amplitude_ >>= 2;
counter_ = 0;
}
return level_;
}
}