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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 14 | #include "modules/include/module_common_types.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 | WebRtcSpl_Init(); |
| 22 | } |
| 23 | |
| 24 | AudioLevel::~AudioLevel() {} |
| 25 | |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 26 | int16_t AudioLevel::LevelFullRange() const { |
| 27 | rtc::CritScope cs(&crit_sect_); |
| 28 | return current_level_full_range_; |
| 29 | } |
| 30 | |
| 31 | void AudioLevel::Clear() { |
| 32 | rtc::CritScope cs(&crit_sect_); |
| 33 | abs_max_ = 0; |
| 34 | count_ = 0; |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 35 | current_level_full_range_ = 0; |
| 36 | } |
| 37 | |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 38 | double AudioLevel::TotalEnergy() const { |
| 39 | rtc::CritScope cs(&crit_sect_); |
| 40 | return total_energy_; |
| 41 | } |
| 42 | |
| 43 | double AudioLevel::TotalDuration() const { |
| 44 | rtc::CritScope cs(&crit_sect_); |
| 45 | return total_duration_; |
| 46 | } |
| 47 | |
| 48 | void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) { |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 49 | // Check speech level (works for 2 channels as well) |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 50 | int16_t abs_value = audioFrame.muted() ? 0 : |
| 51 | WebRtcSpl_MaxAbsValueW16( |
| 52 | audioFrame.data(), |
| 53 | audioFrame.samples_per_channel_ * audioFrame.num_channels_); |
henrik.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 54 | |
| 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 68 | // Decay the absolute maximum (divide by 4) |
| 69 | abs_max_ >>= 2; |
| 70 | } |
zstein | 3c45186 | 2017-07-20 09:57:42 -0700 | [diff] [blame] | 71 | |
| 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.lundin | 92a7a18 | 2017-03-07 01:58:55 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace voe |
| 86 | } // namespace webrtc |