forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_hash.cc
69 lines (56 loc) · 1.93 KB
/
audio_hash.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2013 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 <cmath>
#include <sstream>
#include "media/base/audio_hash.h"
#include "base/numerics/math_constants.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "media/base/audio_bus.h"
namespace media {
AudioHash::AudioHash()
: audio_hash_(),
sample_count_(0) {
}
AudioHash::~AudioHash() = default;
void AudioHash::Update(const AudioBus* audio_bus, int frames) {
// Use uint32_t to ensure overflow is a defined operation.
for (uint32_t ch = 0; ch < static_cast<uint32_t>(audio_bus->channels());
++ch) {
const float* channel = audio_bus->channel(ch);
for (uint32_t i = 0; i < static_cast<uint32_t>(frames); ++i) {
const uint32_t kSampleIndex = sample_count_ + i;
const uint32_t kHashIndex =
(kSampleIndex * (ch + 1)) % base::size(audio_hash_);
// Mix in a sine wave with the result so we ensure that sequences of empty
// buffers don't result in an empty hash.
if (ch == 0) {
audio_hash_[kHashIndex] +=
channel[i] +
std::sin(2.0 * base::kPiDouble * base::kPiDouble * kSampleIndex);
} else {
audio_hash_[kHashIndex] += channel[i];
}
}
}
sample_count_ += static_cast<uint32_t>(frames);
}
std::string AudioHash::ToString() const {
std::string result;
for (size_t i = 0; i < base::size(audio_hash_); ++i)
result += base::StringPrintf("%.2f,", audio_hash_[i]);
return result;
}
bool AudioHash::IsEquivalent(const std::string& other, double tolerance) const {
float other_hash;
char comma;
std::stringstream is(other);
for (size_t i = 0; i < base::size(audio_hash_); ++i) {
is >> other_hash >> comma;
if (std::fabs(audio_hash_[i] - other_hash) > tolerance)
return false;
}
return true;
}
} // namespace media