blob: f1c5b68dc17c491c6589b1ade0a6d954f5a5bf38 [file] [log] [blame]
henrik.lundin92a7a182017-03-07 01:58:55 -08001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010011#include "audio/audio_level.h"
henrik.lundin92a7a182017-03-07 01:58:55 -080012
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020013#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "common_audio/signal_processing/include/signal_processing_library.h"
henrik.lundin92a7a182017-03-07 01:58:55 -080015
16namespace webrtc {
17namespace voe {
18
henrik.lundin92a7a182017-03-07 01:58:55 -080019AudioLevel::AudioLevel()
Niels Möllerf120cba2018-01-30 09:33:03 +010020 : abs_max_(0), count_(0), current_level_full_range_(0) {
henrik.lundin92a7a182017-03-07 01:58:55 -080021 WebRtcSpl_Init();
22}
23
24AudioLevel::~AudioLevel() {}
25
henrik.lundin92a7a182017-03-07 01:58:55 -080026int16_t AudioLevel::LevelFullRange() const {
27 rtc::CritScope cs(&crit_sect_);
28 return current_level_full_range_;
29}
30
31void AudioLevel::Clear() {
32 rtc::CritScope cs(&crit_sect_);
33 abs_max_ = 0;
34 count_ = 0;
henrik.lundin92a7a182017-03-07 01:58:55 -080035 current_level_full_range_ = 0;
36}
37
zstein3c451862017-07-20 09:57:42 -070038double AudioLevel::TotalEnergy() const {
39 rtc::CritScope cs(&crit_sect_);
40 return total_energy_;
41}
42
43double AudioLevel::TotalDuration() const {
44 rtc::CritScope cs(&crit_sect_);
45 return total_duration_;
46}
47
48void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) {
henrik.lundin92a7a182017-03-07 01:58:55 -080049 // Check speech level (works for 2 channels as well)
yujo36b1a5f2017-06-12 12:45:32 -070050 int16_t abs_value = audioFrame.muted() ? 0 :
51 WebRtcSpl_MaxAbsValueW16(
52 audioFrame.data(),
53 audioFrame.samples_per_channel_ * audioFrame.num_channels_);
henrik.lundin92a7a182017-03-07 01:58:55 -080054
55 // Protect member access using a lock since this method is called on a
56 // dedicated audio thread in the RecordedDataIsAvailable() callback.
57 rtc::CritScope cs(&crit_sect_);
58
59 if (abs_value > abs_max_)
60 abs_max_ = abs_value;
61
62 // Update level approximately 10 times per second
63 if (count_++ == kUpdateFrequency) {
64 current_level_full_range_ = abs_max_;
65
66 count_ = 0;
67
henrik.lundin92a7a182017-03-07 01:58:55 -080068 // Decay the absolute maximum (divide by 4)
69 abs_max_ >>= 2;
70 }
zstein3c451862017-07-20 09:57:42 -070071
72 // See the description for "totalAudioEnergy" in the WebRTC stats spec
73 // (https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy)
74 // for an explanation of these formulas. In short, we need a value that can
75 // be used to compute RMS audio levels over different time intervals, by
76 // taking the difference between the results from two getStats calls. To do
77 // this, the value needs to be of units "squared sample value * time".
78 double additional_energy =
79 static_cast<double>(current_level_full_range_) / INT16_MAX;
80 additional_energy *= additional_energy;
81 total_energy_ += additional_energy * duration;
82 total_duration_ += duration;
henrik.lundin92a7a182017-03-07 01:58:55 -080083}
84
85} // namespace voe
86} // namespace webrtc