blob: 7f18be8c6e4ef7e428c5bb73331ed84408f9cf15 [file] [log] [blame]
ekm030249d2015-06-15 13:02:24 -07001/*
2 * Copyright (c) 2014 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
ekmdb4fecf2015-06-22 17:49:08 -070011//
12// Specifies core class for intelligbility enhancement.
13//
14
ekm030249d2015-06-15 13:02:24 -070015#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_
16#define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_
17
18#include <complex>
ekm35b72fb2015-07-10 14:11:52 -070019#include <vector>
ekm030249d2015-06-15 13:02:24 -070020
ekmdb4fecf2015-06-22 17:49:08 -070021#include "webrtc/base/scoped_ptr.h"
ekm030249d2015-06-15 13:02:24 -070022#include "webrtc/common_audio/lapped_transform.h"
23#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
ekm030249d2015-06-15 13:02:24 -070024
25struct WebRtcVadInst;
26typedef struct WebRtcVadInst VadInst;
27
28namespace webrtc {
29
30// Speech intelligibility enhancement module. Reads render and capture
31// audio streams and modifies the render stream with a set of gains per
32// frequency bin to enhance speech against the noise background.
ekmdb4fecf2015-06-22 17:49:08 -070033// Note: assumes speech and noise streams are already separated.
ekm030249d2015-06-15 13:02:24 -070034class IntelligibilityEnhancer {
35 public:
36 // Construct a new instance with the given filter bank resolution,
37 // sampling rate, number of channels and analysis rates.
38 // |analysis_rate| sets the number of input blocks (containing speech!)
39 // to elapse before a new gain computation is made. |variance_rate| specifies
40 // the number of gain recomputations after which the variances are reset.
41 // |cv_*| are parameters for the VarianceArray constructor for the
ekmdb4fecf2015-06-22 17:49:08 -070042 // clear speech stream.
ekm030249d2015-06-15 13:02:24 -070043 // TODO(bercic): the |cv_*|, |*_rate| and |gain_limit| parameters should
44 // probably go away once fine tuning is done. They override the internal
45 // constants in the class (kGainChangeLimit, kAnalyzeRate, kVarianceRate).
ekmdb4fecf2015-06-22 17:49:08 -070046 IntelligibilityEnhancer(int erb_resolution,
47 int sample_rate_hz,
48 int channels,
49 int cv_type,
50 float cv_alpha,
51 int cv_win,
52 int analysis_rate,
53 int variance_rate,
ekm030249d2015-06-15 13:02:24 -070054 float gain_limit);
55 ~IntelligibilityEnhancer();
56
ekmdb4fecf2015-06-22 17:49:08 -070057 // Reads and processes chunk of noise stream in time domain.
aluebsc555b992015-06-16 20:26:16 -070058 void ProcessCaptureAudio(float* const* audio);
ekmb7553df2015-06-16 18:57:32 -070059
ekmdb4fecf2015-06-22 17:49:08 -070060 // Reads chunk of speech in time domain and updates with modified signal.
61 void ProcessRenderAudio(float* const* audio);
62
ekm030249d2015-06-15 13:02:24 -070063 private:
64 enum AudioSource {
ekmdb4fecf2015-06-22 17:49:08 -070065 kRenderStream = 0, // Clear speech stream.
66 kCaptureStream, // Noise stream.
ekm030249d2015-06-15 13:02:24 -070067 };
68
ekmdb4fecf2015-06-22 17:49:08 -070069 // Provides access point to the frequency domain.
ekm030249d2015-06-15 13:02:24 -070070 class TransformCallback : public LappedTransform::Callback {
71 public:
72 TransformCallback(IntelligibilityEnhancer* parent, AudioSource source);
ekmdb4fecf2015-06-22 17:49:08 -070073
74 // All in frequency domain, receives input |in_block|, applies
75 // intelligibility enhancement, and writes result to |out_block|.
pkastingb297c5a2015-07-22 15:17:22 -070076 void ProcessAudioBlock(const std::complex<float>* const* in_block,
77 int in_channels,
78 int frames,
79 int out_channels,
80 std::complex<float>* const* out_block) override;
ekm030249d2015-06-15 13:02:24 -070081
82 private:
83 IntelligibilityEnhancer* parent_;
84 AudioSource source_;
85 };
86 friend class TransformCallback;
ekm35b72fb2015-07-10 14:11:52 -070087 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
88 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
ekm030249d2015-06-15 13:02:24 -070089
ekmdb4fecf2015-06-22 17:49:08 -070090 // Sends streams to ProcessClearBlock or ProcessNoiseBlock based on source.
91 void DispatchAudio(AudioSource source,
92 const std::complex<float>* in_block,
ekm030249d2015-06-15 13:02:24 -070093 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070094
95 // Updates variance computation and analysis with |in_block_|,
96 // and writes modified speech to |out_block|.
ekm030249d2015-06-15 13:02:24 -070097 void ProcessClearBlock(const std::complex<float>* in_block,
98 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070099
100 // Computes and sets modified gains.
ekm030249d2015-06-15 13:02:24 -0700101 void AnalyzeClearBlock(float power_target);
ekmdb4fecf2015-06-22 17:49:08 -0700102
ekm35b72fb2015-07-10 14:11:52 -0700103 // Bisection search for optimal |lambda|.
104 void SolveForLambda(float power_target, float power_bot, float power_top);
105
106 // Transforms freq gains to ERB gains.
107 void UpdateErbGains();
108
ekmdb4fecf2015-06-22 17:49:08 -0700109 // Updates variance calculation for noise input with |in_block|.
ekm030249d2015-06-15 13:02:24 -0700110 void ProcessNoiseBlock(const std::complex<float>* in_block,
111 std::complex<float>* out_block);
112
ekmdb4fecf2015-06-22 17:49:08 -0700113 // Returns number of ERB filters.
ekm030249d2015-06-15 13:02:24 -0700114 static int GetBankSize(int sample_rate, int erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -0700115
116 // Initializes ERB filterbank.
ekm030249d2015-06-15 13:02:24 -0700117 void CreateErbBank();
ekmdb4fecf2015-06-22 17:49:08 -0700118
119 // Analytically solves quadratic for optimal gains given |lambda|.
120 // Negative gains are set to 0. Stores the results in |sols|.
121 void SolveForGainsGivenLambda(float lambda, int start_freq, float* sols);
122
123 // Computes variance across ERB filters from freq variance |var|.
124 // Stores in |result|.
ekm030249d2015-06-15 13:02:24 -0700125 void FilterVariance(const float* var, float* result);
ekmdb4fecf2015-06-22 17:49:08 -0700126
127 // Returns dot product of vectors specified by size |length| arrays |a|,|b|.
ekm030249d2015-06-15 13:02:24 -0700128 static float DotProduct(const float* a, const float* b, int length);
129
ekmdb4fecf2015-06-22 17:49:08 -0700130 const int freqs_; // Num frequencies in frequency domain.
131 const int window_size_; // Window size in samples; also the block size.
132 const int chunk_length_; // Chunk size in samples.
133 const int bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -0700134 const int sample_rate_hz_;
135 const int erb_resolution_;
ekmdb4fecf2015-06-22 17:49:08 -0700136 const int channels_; // Num channels.
137 const int analysis_rate_; // Num blocks before gains recalculated.
138 const int variance_rate_; // Num recalculations before history is cleared.
ekm030249d2015-06-15 13:02:24 -0700139
140 intelligibility::VarianceArray clear_variance_;
141 intelligibility::VarianceArray noise_variance_;
ekmdb4fecf2015-06-22 17:49:08 -0700142 rtc::scoped_ptr<float[]> filtered_clear_var_;
143 rtc::scoped_ptr<float[]> filtered_noise_var_;
ekm35b72fb2015-07-10 14:11:52 -0700144 std::vector<std::vector<float>> filter_bank_;
ekmdb4fecf2015-06-22 17:49:08 -0700145 rtc::scoped_ptr<float[]> center_freqs_;
ekm030249d2015-06-15 13:02:24 -0700146 int start_freq_;
ekmdb4fecf2015-06-22 17:49:08 -0700147 rtc::scoped_ptr<float[]> rho_; // Production and interpretation SNR.
148 // for each ERB band.
149 rtc::scoped_ptr<float[]> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700150 intelligibility::GainApplier gain_applier_;
151
152 // Destination buffer used to reassemble blocked chunks before overwriting
153 // the original input array with modifications.
ekmdb4fecf2015-06-22 17:49:08 -0700154 // TODO(ekmeyerson): Switch to using ChannelBuffer.
ekm030249d2015-06-15 13:02:24 -0700155 float** temp_out_buffer_;
ekmdb4fecf2015-06-22 17:49:08 -0700156
157 rtc::scoped_ptr<float* []> input_audio_;
158 rtc::scoped_ptr<float[]> kbd_window_;
ekm030249d2015-06-15 13:02:24 -0700159 TransformCallback render_callback_;
160 TransformCallback capture_callback_;
ekmdb4fecf2015-06-22 17:49:08 -0700161 rtc::scoped_ptr<LappedTransform> render_mangler_;
162 rtc::scoped_ptr<LappedTransform> capture_mangler_;
ekm030249d2015-06-15 13:02:24 -0700163 int block_count_;
164 int analysis_step_;
165
166 // TODO(bercic): Quick stopgap measure for voice detection in the clear
167 // and noise streams.
ekmdb4fecf2015-06-22 17:49:08 -0700168 // Note: VAD currently does not affect anything in IntelligibilityEnhancer.
ekm030249d2015-06-15 13:02:24 -0700169 VadInst* vad_high_;
170 VadInst* vad_low_;
ekmdb4fecf2015-06-22 17:49:08 -0700171 rtc::scoped_ptr<int16_t[]> vad_tmp_buffer_;
172 bool has_voice_low_; // Whether voice detected in speech stream.
ekm030249d2015-06-15 13:02:24 -0700173};
174
175} // namespace webrtc
176
177#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_