blob: d26e949ccc89d7ac4e0a85c7187708a2d486d695 [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}
22
23AudioLevel::~AudioLevel() {}
24
Henrik Boströmd2c336f2019-07-03 17:11:10 +020025void AudioLevel::Reset() {
26 rtc::CritScope cs(&crit_sect_);
27 abs_max_ = 0;
28 count_ = 0;
29 current_level_full_range_ = 0;
30 total_energy_ = 0.0;
31 total_duration_ = 0.0;
32}
33
henrik.lundin92a7a182017-03-07 01:58:55 -080034int16_t AudioLevel::LevelFullRange() const {
35 rtc::CritScope cs(&crit_sect_);
36 return current_level_full_range_;
37}
38
Henrik Boströmd2c336f2019-07-03 17:11:10 +020039void AudioLevel::ResetLevelFullRange() {
henrik.lundin92a7a182017-03-07 01:58:55 -080040 rtc::CritScope cs(&crit_sect_);
41 abs_max_ = 0;
42 count_ = 0;
henrik.lundin92a7a182017-03-07 01:58:55 -080043 current_level_full_range_ = 0;
44}
45
zstein3c451862017-07-20 09:57:42 -070046double AudioLevel::TotalEnergy() const {
47 rtc::CritScope cs(&crit_sect_);
48 return total_energy_;
49}
50
51double AudioLevel::TotalDuration() const {
52 rtc::CritScope cs(&crit_sect_);
53 return total_duration_;
54}
55
56void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) {
henrik.lundin92a7a182017-03-07 01:58:55 -080057 // Check speech level (works for 2 channels as well)
Yves Gerey665174f2018-06-19 15:03:05 +020058 int16_t abs_value =
59 audioFrame.muted()
60 ? 0
61 : WebRtcSpl_MaxAbsValueW16(
62 audioFrame.data(),
63 audioFrame.samples_per_channel_ * audioFrame.num_channels_);
henrik.lundin92a7a182017-03-07 01:58:55 -080064
65 // Protect member access using a lock since this method is called on a
66 // dedicated audio thread in the RecordedDataIsAvailable() callback.
67 rtc::CritScope cs(&crit_sect_);
68
69 if (abs_value > abs_max_)
70 abs_max_ = abs_value;
71
Henrik Boströmd2c336f2019-07-03 17:11:10 +020072 // Update level approximately 9 times per second, assuming audio frame
73 // duration is approximately 10 ms. (The update frequency is every
74 // 11th (= |kUpdateFrequency+1|) call: 1000/(11*10)=9.09..., we should
75 // probably change this behavior, see https://crbug.com/webrtc/10784).
henrik.lundin92a7a182017-03-07 01:58:55 -080076 if (count_++ == kUpdateFrequency) {
77 current_level_full_range_ = abs_max_;
78
79 count_ = 0;
80
henrik.lundin92a7a182017-03-07 01:58:55 -080081 // Decay the absolute maximum (divide by 4)
82 abs_max_ >>= 2;
83 }
zstein3c451862017-07-20 09:57:42 -070084
85 // See the description for "totalAudioEnergy" in the WebRTC stats spec
86 // (https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy)
87 // for an explanation of these formulas. In short, we need a value that can
88 // be used to compute RMS audio levels over different time intervals, by
89 // taking the difference between the results from two getStats calls. To do
90 // this, the value needs to be of units "squared sample value * time".
91 double additional_energy =
92 static_cast<double>(current_level_full_range_) / INT16_MAX;
93 additional_energy *= additional_energy;
94 total_energy_ += additional_energy * duration;
95 total_duration_ += duration;
henrik.lundin92a7a182017-03-07 01:58:55 -080096}
97
98} // namespace voe
99} // namespace webrtc