audio_hash.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_AUDIO_HASH_H_
  5. #define MEDIA_BASE_AUDIO_HASH_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include "base/strings/string_piece.h"
  10. #include "media/base/media_export.h"
  11. namespace media {
  12. class AudioBus;
  13. // Computes a running hash for a series of AudioBus objects. The hash is the
  14. // sum of each sample bucketed based on the frame index, channel number, and
  15. // current hash count. The hash was designed with two properties in mind:
  16. //
  17. // 1. Uniform error distribution across the input sample.
  18. // 2. Resilience to error below a certain threshold.
  19. //
  20. // The first is achieved by using a simple summing approach and moving position
  21. // weighting into the bucket choice. The second is handled during conversion to
  22. // string by rounding out values to only two decimal places.
  23. //
  24. // Using only two decimal places allows for roughly -40 dBFS of error. For
  25. // reference, SincResampler produces an RMS error of around -15 dBFS. See
  26. // http://en.wikipedia.org/wiki/DBFS and http://crbug.com/168204 for more info.
  27. class MEDIA_EXPORT AudioHash {
  28. public:
  29. AudioHash();
  30. AudioHash(const AudioHash&) = delete;
  31. AudioHash& operator=(const AudioHash&) = delete;
  32. ~AudioHash();
  33. // Update current hash with the contents of the provided AudioBus.
  34. void Update(const AudioBus* audio_bus, int frames);
  35. // Return a string representation of the current hash.
  36. std::string ToString() const;
  37. // Compare with another hash value given as string representation.
  38. // Returns true if for every bucket the difference between this and
  39. // other is less than tolerance.
  40. bool IsEquivalent(const std::string& other, double tolerance) const;
  41. private:
  42. // Storage for the audio hash. The number of buckets controls the importance
  43. // of position in the hash. A higher number reduces the chance of false
  44. // positives related to incorrect sample position. Value chosen by dice roll.
  45. float audio_hash_[6];
  46. // The total number of samples processed per channel. Uses a uint32_t instead
  47. // of size_t so overflows on 64-bit and 32-bit machines are equivalent.
  48. uint32_t sample_count_;
  49. };
  50. } // namespace media
  51. #endif // MEDIA_BASE_AUDIO_HASH_H_