blob: ec733832f7817d001a7caf60f310af975ccb4145 [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/strings/string_piece.h"
#include "media/base/media_export.h"
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;
// 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 instead
// of size_t so overflows on 64-bit and 32-bit machines are equivalent.
uint32 sample_count_;
} // namespace media