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() |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [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 | AudioLevel::~AudioLevel() {} |
| 23 | |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame] | 24 | void 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 33 | int16_t AudioLevel::LevelFullRange() const { |
| 34 | rtc::CritScope cs(&crit_sect_); |
| 35 | return current_level_full_range_; |
| 36 | } |
| 37 | |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame] | 38 | void AudioLevel::ResetLevelFullRange() { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 39 | rtc::CritScope cs(&crit_sect_); |
| 40 | abs_max_ = 0; |
| 41 | count_ = 0; |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 42 | current_level_full_range_ = 0; |
| 43 | } |
| 44 | |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 45 | double AudioLevel::TotalEnergy() const { |
| 46 | rtc::CritScope cs(&crit_sect_); |
| 47 | return total_energy_; |
| 48 | } |
| 49 | |
| 50 | double AudioLevel::TotalDuration() const { |
| 51 | rtc::CritScope cs(&crit_sect_); |
| 52 | return total_duration_; |
| 53 | } |
| 54 | |
| 55 | void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 56 | // Check speech level (works for 2 channels as well) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 57 | int16_t abs_value = |
| 58 | audioFrame.muted() |
| 59 | ? 0 |
| 60 | : WebRtcSpl_MaxAbsValueW16( |
| 61 | audioFrame.data(), |
| 62 | audioFrame.samples_per_channel_ * audioFrame.num_channels_); |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 63 | |
| 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öm | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame] | 71 | // 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 75 | if (count_++ == kUpdateFrequency) { |
| 76 | current_level_full_range_ = abs_max_; |
| 77 | |
| 78 | count_ = 0; |
| 79 | |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 80 | // Decay the absolute maximum (divide by 4) |
| 81 | abs_max_ >>= 2; |
| 82 | } |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 83 | |
| 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace voe |
| 98 | } // namespace webrtc |