Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1374)

Unified Diff: content/renderer/media/media_stream_audio_level_calculator.cc

Issue 178223013: Calculate the signal level on the media stream local audio track (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fixed the thread check and max amplitude Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/renderer/media/media_stream_audio_level_calculator.cc
diff --git a/content/renderer/media/media_stream_audio_level_calculator.cc b/content/renderer/media/media_stream_audio_level_calculator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fef88718e654662a54c0d9aeb88b33a4ffddcf32
--- /dev/null
+++ b/content/renderer/media/media_stream_audio_level_calculator.cc
@@ -0,0 +1,80 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/media/media_stream_audio_level_calculator.h"
+
+#include "base/logging.h"
+#include "base/stl_util.h"
+
+namespace content {
+
+namespace {
+
+int MaxAmplitude(const int16* audio_data, int length) {
+ int max = 0, absolute = 0;
+ for (int i = 0; i < length; ++i) {
+ absolute = std::abs(audio_data[i]);
+ if (absolute > max)
+ max = absolute;
+ }
+ // The range of int16 is [-32768, 32767], verify the |max| should be smaller
+ // than 32768.
+ DCHECK(max <= std::abd(std::numeric_limits<int16>::min()));
tommi (sloooow) - chröme 2014/03/01 11:49:33 a few questions: * does this compile? (abd?) * Th
no longer working on chromium 2014/03/01 12:37:00 No, it should be abs.
tommi (sloooow) - chröme 2014/03/02 10:19:53 OK, now I get it. Can you add documentation to Ma
no longer working on chromium 2014/03/03 13:07:12 Done with adding a comment.
+
+ return max;
+}
+
+} // namespace
+
+MediaStreamAudioLevelCalculator::MediaStreamAudioLevelCalculator()
+ : counter_(0),
+ max_amplitude_(0),
+ level_(0) {
+}
+
+MediaStreamAudioLevelCalculator::~MediaStreamAudioLevelCalculator() {
+}
+
+int MediaStreamAudioLevelCalculator::Calculate(const int16* audio_data,
+ int number_of_channels,
+ int number_of_frames) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ // Permutation of bars that reprents the amplitude level of the audio signal.
+ // The number of elements is 33 because we are indexing them in the range of
+ // [0, 32].
+ static const int kPermutation[33] =
+ {0,1,2,3,4,4,5,5,5,5,6,6,6,6,6,7,7,7,7,8,8,8,9,9,9,9,9,9,9,9,9,9,9};
+
+ // |level_| is updated every 10 callbacks. For the case where callback comes
+ // every 10ms, |level_| will be updated approximately every 100ms.
+ static const int kUpdateFrequency = 10;
+
+ int max = MaxAmplitude(audio_data, number_of_channels * number_of_frames);
+ max_amplitude_ = std::max(max_amplitude_, max);
+
+ if (counter_++ == kUpdateFrequency) {
+ // Divide the max amplitude (32768) by 1000 to get in the range of [0,32]
tommi (sloooow) - chröme 2014/03/01 11:49:33 it can't be 32768. that's higher than int16 can c
no longer working on chromium 2014/03/01 12:37:00 The same explaination, absolute value of <int16>::
+ // which is the range of the permutation array.
+ int index = static_cast<int>(max_amplitude_ / 1000);
+
+ // Make it less likely that the bar stays at position 0. I.e. only if
+ // its in the range 0-250 (instead of 0-1000)
+ if (index == 0 && max_amplitude_ > 250)
+ index = 1;
+
+ // |level_| will be the value in the permutation array that the |index| is
+ // pointing to.
+ level_ = kPermutation[index];
+
+ // Decay the absolute maximum amplitude by 1/4.
+ max_amplitude_ >>= 2;
+
+ // Reset the counter.
+ counter_ = 0;
+ }
+
+ return level_;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698