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