blob: 320b1ab728d736b58c1a7f4450ea080e07fbb186 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/media_stream_audio_level_calculator.h"
#include "base/float_util.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "media/base/audio_bus.h"
namespace content {
namespace {
// Calculates the maximum absolute amplitude of the audio data.
float MaxAmplitude(const float* audio_data, int length) {
float max = 0.0f;
for (int i = 0; i < length; ++i) {
const float absolute = fabsf(audio_data[i]);
if (absolute > max)
max = absolute;
}
DCHECK(base::IsFinite(max));
return max;
}
} // namespace
MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator()
: counter_(0),
max_amplitude_(0.0f),
level_(0.0f) {
}
MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
}
float MediaStreamAudioLevelCalculator::Calculate(
const media::AudioBus& audio_bus) {
DCHECK(thread_checker_.CalledOnValidThread());
// |level_| is updated every 10 callbacks. For the case where callback comes
// every 10ms, |level_| will be updated approximately every 100ms.
static const int kUpdateFrequency = 10;
float max = 0.0f;
for (int i = 0; i < audio_bus.channels(); ++i) {
const float max_this_channel =
MaxAmplitude(audio_bus.channel(i), audio_bus.frames());
if (max_this_channel > max)
max = max_this_channel;
}
max_amplitude_ = std::max(max_amplitude_, max);
if (counter_++ == kUpdateFrequency) {
level_ = max_amplitude_;
// Decay the absolute maximum amplitude by 1/4.
max_amplitude_ /= 4.0f;
// Reset the counter.
counter_ = 0;
}
return level_;
}
} // namespace content
|