transient_detector.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_
  11. #define MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_
  12. #include <stddef.h>
  13. #include <deque>
  14. #include <memory>
  15. #include "modules/audio_processing/transient/moving_moments.h"
  16. #include "modules/audio_processing/transient/wpd_tree.h"
  17. namespace webrtc {
  18. // This is an implementation of the transient detector described in "Causal
  19. // Wavelet based transient detector".
  20. // Calculates the log-likelihood of a transient to happen on a signal at any
  21. // given time based on the previous samples; it uses a WPD tree to analyze the
  22. // signal. It preserves its state, so it can be multiple-called.
  23. class TransientDetector {
  24. public:
  25. // TODO(chadan): The only supported wavelet is Daubechies 8 using a WPD tree
  26. // of 3 levels. Make an overloaded constructor to allow different wavelets and
  27. // depths of the tree. When needed.
  28. // Creates a wavelet based transient detector.
  29. TransientDetector(int sample_rate_hz);
  30. ~TransientDetector();
  31. // Calculates the log-likelihood of the existence of a transient in |data|.
  32. // |data_length| has to be equal to |samples_per_chunk_|.
  33. // Returns a value between 0 and 1, as a non linear representation of this
  34. // likelihood.
  35. // Returns a negative value on error.
  36. float Detect(const float* data,
  37. size_t data_length,
  38. const float* reference_data,
  39. size_t reference_length);
  40. bool using_reference() { return using_reference_; }
  41. private:
  42. float ReferenceDetectionValue(const float* data, size_t length);
  43. static const size_t kLevels = 3;
  44. static const size_t kLeaves = 1 << kLevels;
  45. size_t samples_per_chunk_;
  46. std::unique_ptr<WPDTree> wpd_tree_;
  47. size_t tree_leaves_data_length_;
  48. // A MovingMoments object is needed for each leaf in the WPD tree.
  49. std::unique_ptr<MovingMoments> moving_moments_[kLeaves];
  50. std::unique_ptr<float[]> first_moments_;
  51. std::unique_ptr<float[]> second_moments_;
  52. // Stores the last calculated moments from the previous detection.
  53. float last_first_moment_[kLeaves];
  54. float last_second_moment_[kLeaves];
  55. // We keep track of the previous results from the previous chunks, so it can
  56. // be used to effectively give results according to the |transient_length|.
  57. std::deque<float> previous_results_;
  58. // Number of chunks that are going to return only zeros at the beginning of
  59. // the detection. It helps to avoid infs and nans due to the lack of
  60. // information.
  61. int chunks_at_startup_left_to_delete_;
  62. float reference_energy_;
  63. bool using_reference_;
  64. };
  65. } // namespace webrtc
  66. #endif // MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_