blob: 23b88f82b10cf2e6ec37728b11631c7a2e175ac1 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_
12#define MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000015#include <deque>
kwiberg85d8bb02016-02-16 20:39:36 -080016#include <memory>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/transient/moving_moments.h"
19#include "modules/audio_processing/transient/wpd_tree.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
21namespace webrtc {
22
23// This is an implementation of the transient detector described in "Causal
24// Wavelet based transient detector".
25// Calculates the log-likelihood of a transient to happen on a signal at any
26// given time based on the previous samples; it uses a WPD tree to analyze the
27// signal. It preserves its state, so it can be multiple-called.
28class TransientDetector {
29 public:
30 // TODO(chadan): The only supported wavelet is Daubechies 8 using a WPD tree
31 // of 3 levels. Make an overloaded constructor to allow different wavelets and
32 // depths of the tree. When needed.
33
34 // Creates a wavelet based transient detector.
35 TransientDetector(int sample_rate_hz);
36
37 ~TransientDetector();
38
39 // Calculates the log-likelihood of the existence of a transient in |data|.
40 // |data_length| has to be equal to |samples_per_chunk_|.
41 // Returns a value between 0 and 1, as a non linear representation of this
42 // likelihood.
43 // Returns a negative value on error.
44 float Detect(const float* data,
45 size_t data_length,
46 const float* reference_data,
47 size_t reference_length);
48
49 bool using_reference() { return using_reference_; }
50
51 private:
52 float ReferenceDetectionValue(const float* data, size_t length);
53
54 static const size_t kLevels = 3;
55 static const size_t kLeaves = 1 << kLevels;
56
57 size_t samples_per_chunk_;
58
kwiberg85d8bb02016-02-16 20:39:36 -080059 std::unique_ptr<WPDTree> wpd_tree_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060 size_t tree_leaves_data_length_;
61
62 // A MovingMoments object is needed for each leaf in the WPD tree.
kwiberg85d8bb02016-02-16 20:39:36 -080063 std::unique_ptr<MovingMoments> moving_moments_[kLeaves];
pbos@webrtc.org788acd12014-12-15 09:41:24 +000064
kwiberg85d8bb02016-02-16 20:39:36 -080065 std::unique_ptr<float[]> first_moments_;
66 std::unique_ptr<float[]> second_moments_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000067
68 // Stores the last calculated moments from the previous detection.
69 float last_first_moment_[kLeaves];
70 float last_second_moment_[kLeaves];
71
72 // We keep track of the previous results from the previous chunks, so it can
73 // be used to effectively give results according to the |transient_length|.
74 std::deque<float> previous_results_;
75
76 // Number of chunks that are going to return only zeros at the beginning of
77 // the detection. It helps to avoid infs and nans due to the lack of
78 // information.
79 int chunks_at_startup_left_to_delete_;
80
81 float reference_energy_;
82
83 bool using_reference_;
84};
85
86} // namespace webrtc
87
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020088#endif // MODULES_AUDIO_PROCESSING_TRANSIENT_TRANSIENT_DETECTOR_H_