| 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 |
| 11 | #include "audio/audio_level.h" |
| 12 |
| 13 | #include "api/audio/audio_frame.h" |
| 14 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 15 |
| 16 | namespace webrtc { |
| 17 | namespace voe { |
| 18 |
| 19 | AudioLevel::AudioLevel() |
| 20 | : abs_max_(0), count_(0), current_level_full_range_(0) {} |
| 21 |
| 22 | AudioLevel::~AudioLevel() {} |
| 23 |
| 24 | void AudioLevel::Reset() { |
| 25 | MutexLock lock(&mutex_); |
| 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 |
| 33 | int16_t AudioLevel::LevelFullRange() const { |
| 34 | MutexLock lock(&mutex_); |
| 35 | return current_level_full_range_; |
| 36 | } |
| 37 |
| 38 | void AudioLevel::ResetLevelFullRange() { |
| 39 | MutexLock lock(&mutex_); |
| 40 | abs_max_ = 0; |
| 41 | count_ = 0; |
| 42 | current_level_full_range_ = 0; |
| 43 | } |
| 44 |
| 45 | double AudioLevel::TotalEnergy() const { |
| 46 | MutexLock lock(&mutex_); |
| 47 | return total_energy_; |
| 48 | } |
| 49 |
| 50 | double AudioLevel::TotalDuration() const { |
| 51 | MutexLock lock(&mutex_); |
| 52 | return total_duration_; |
| 53 | } |
| 54 |
| 55 | void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) { |
| 56 | // Check speech level (works for 2 channels as well) |
| 57 | int16_t abs_value = |
| 58 | audioFrame.muted() |
| 59 | ? 0 |
| 60 | : WebRtcSpl_MaxAbsValueW16( |
| 61 | audioFrame.data(), |
| 62 | audioFrame.samples_per_channel_ * audioFrame.num_channels_); |
| 63 |
| 64 | // Protect member access using a lock since this method is called on a |
| 65 | // dedicated audio thread in the RecordedDataIsAvailable() callback. |
| 66 | MutexLock lock(&mutex_); |
| 67 |
| 68 | if (abs_value > abs_max_) |
| 69 | abs_max_ = abs_value; |
| 70 |
| 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). |
| 75 | if (count_++ == kUpdateFrequency) { |
| 76 | current_level_full_range_ = abs_max_; |
| 77 |
| 78 | count_ = 0; |
| 79 |
| 80 | // Decay the absolute maximum (divide by 4) |
| 81 | abs_max_ >>= 2; |
| 82 | } |
| 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; |
| 95 | } |
| 96 |
| 97 | } // namespace voe |
| 98 | } // namespace webrtc |
| 99 |