henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Solenberg | a8b7c7f | 2018-01-17 11:18:31 +0100 | [diff] [blame] | 11 | #include "audio/audio_level.h" |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 12 | |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 13 | #include "api/audio/audio_frame.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace voe { |
| 18 | |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 19 | AudioLevel::AudioLevel() |
Niels Möller | f120cba | 2018-01-30 09:33:03 +0100 | [diff] [blame] | 20 | : abs_max_(0), count_(0), current_level_full_range_(0) { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | AudioLevel::~AudioLevel() {} |
| 24 | |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame^] | 25 | void 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 34 | int16_t AudioLevel::LevelFullRange() const { |
| 35 | rtc::CritScope cs(&crit_sect_); |
| 36 | return current_level_full_range_; |
| 37 | } |
| 38 | |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame^] | 39 | void AudioLevel::ResetLevelFullRange() { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 40 | rtc::CritScope cs(&crit_sect_); |
| 41 | abs_max_ = 0; |
| 42 | count_ = 0; |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 43 | current_level_full_range_ = 0; |
| 44 | } |
| 45 | |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 46 | double AudioLevel::TotalEnergy() const { |
| 47 | rtc::CritScope cs(&crit_sect_); |
| 48 | return total_energy_; |
| 49 | } |
| 50 | |
| 51 | double AudioLevel::TotalDuration() const { |
| 52 | rtc::CritScope cs(&crit_sect_); |
| 53 | return total_duration_; |
| 54 | } |
| 55 | |
| 56 | void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 57 | // Check speech level (works for 2 channels as well) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | int16_t abs_value = |
| 59 | audioFrame.muted() |
| 60 | ? 0 |
| 61 | : WebRtcSpl_MaxAbsValueW16( |
| 62 | audioFrame.data(), |
| 63 | audioFrame.samples_per_channel_ * audioFrame.num_channels_); |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 64 | |
| 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öm | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame^] | 72 | // 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 76 | if (count_++ == kUpdateFrequency) { |
| 77 | current_level_full_range_ = abs_max_; |
| 78 | |
| 79 | count_ = 0; |
| 80 | |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 81 | // Decay the absolute maximum (divide by 4) |
| 82 | abs_max_ >>= 2; |
| 83 | } |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 84 | |
| 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace voe |
| 99 | } // namespace webrtc |