blob: 66594b4d39b95b5880fe46918b842ddd61126dd6 [file] [log] [blame]
// 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 <string>
#include "base/basictypes.h"
#include "base/string_piece.h"
#include "cobalt/media/base/media_export.h"
#include "starboard/types.h"
namespace cobalt {
namespace media {
class AudioBus;
// Computes a running hash for a series of AudioBus objects. The hash is the
// sum of each sample bucketed based on the frame index, channel number, and
// current hash count. The hash was designed with two properties in mind:
// 1. Uniform error distribution across the input sample.
// 2. Resilience to error below a certain threshold.
// The first is achieved by using a simple summing approach and moving position
// weighting into the bucket choice. The second is handled during conversion to
// string by rounding out values to only two decimal places.
// Using only two decimal places allows for roughly -40 dBFS of error. For
// reference, SincResampler produces an RMS error of around -15 dBFS. See
// and for more info.
class MEDIA_EXPORT AudioHash {
// Update current hash with the contents of the provided AudioBus.
void Update(const AudioBus* audio_bus, int frames);
// Return a string representation of the current hash.
std::string ToString() const;
// Compare with another hash value given as string representation.
// Returns true if for every bucket the difference between this and
// other is less than tolerance.
bool IsEquivalent(const std::string& other, double tolerance) const;
// Storage for the audio hash. The number of buckets controls the importance
// of position in the hash. A higher number reduces the chance of false
// positives related to incorrect sample position. Value chosen by dice roll.
float audio_hash_[6];
// The total number of samples processed per channel. Uses a uint32_t instead
// of size_t so overflows on 64-bit and 32-bit machines are equivalent.
uint32_t sample_count_;
} // namespace media
} // namespace cobalt