blob: 8125707f120981c40817152a80bcbea43f2e3006 [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>
19
ekmdb4fecf2015-06-22 17:49:08 -070020#include "webrtc/base/scoped_ptr.h"
ekm030249d2015-06-15 13:02:24 -070021#include "webrtc/common_audio/lapped_transform.h"
22#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
ekm030249d2015-06-15 13:02:24 -070023
24struct WebRtcVadInst;
25typedef struct WebRtcVadInst VadInst;
26
27namespace webrtc {
28
29// Speech intelligibility enhancement module. Reads render and capture
30// audio streams and modifies the render stream with a set of gains per
31// frequency bin to enhance speech against the noise background.
ekmdb4fecf2015-06-22 17:49:08 -070032// Note: assumes speech and noise streams are already separated.
ekm030249d2015-06-15 13:02:24 -070033class IntelligibilityEnhancer {
34 public:
35 // Construct a new instance with the given filter bank resolution,
36 // sampling rate, number of channels and analysis rates.
37 // |analysis_rate| sets the number of input blocks (containing speech!)
38 // to elapse before a new gain computation is made. |variance_rate| specifies
39 // the number of gain recomputations after which the variances are reset.
40 // |cv_*| are parameters for the VarianceArray constructor for the
ekmdb4fecf2015-06-22 17:49:08 -070041 // clear speech stream.
ekm030249d2015-06-15 13:02:24 -070042 // TODO(bercic): the |cv_*|, |*_rate| and |gain_limit| parameters should
43 // probably go away once fine tuning is done. They override the internal
44 // constants in the class (kGainChangeLimit, kAnalyzeRate, kVarianceRate).
ekmdb4fecf2015-06-22 17:49:08 -070045 IntelligibilityEnhancer(int erb_resolution,
46 int sample_rate_hz,
47 int channels,
48 int cv_type,
49 float cv_alpha,
50 int cv_win,
51 int analysis_rate,
52 int variance_rate,
ekm030249d2015-06-15 13:02:24 -070053 float gain_limit);
54 ~IntelligibilityEnhancer();
55
ekmdb4fecf2015-06-22 17:49:08 -070056 // Reads and processes chunk of noise stream in time domain.
aluebsc555b992015-06-16 20:26:16 -070057 void ProcessCaptureAudio(float* const* audio);
ekmb7553df2015-06-16 18:57:32 -070058
ekmdb4fecf2015-06-22 17:49:08 -070059 // Reads chunk of speech in time domain and updates with modified signal.
60 void ProcessRenderAudio(float* const* audio);
61
ekm030249d2015-06-15 13:02:24 -070062 private:
63 enum AudioSource {
ekmdb4fecf2015-06-22 17:49:08 -070064 kRenderStream = 0, // Clear speech stream.
65 kCaptureStream, // Noise stream.
ekm030249d2015-06-15 13:02:24 -070066 };
67
ekmdb4fecf2015-06-22 17:49:08 -070068 // Provides access point to the frequency domain.
ekm030249d2015-06-15 13:02:24 -070069 class TransformCallback : public LappedTransform::Callback {
70 public:
71 TransformCallback(IntelligibilityEnhancer* parent, AudioSource source);
ekmdb4fecf2015-06-22 17:49:08 -070072
73 // All in frequency domain, receives input |in_block|, applies
74 // intelligibility enhancement, and writes result to |out_block|.
ekm030249d2015-06-15 13:02:24 -070075 virtual void ProcessAudioBlock(const std::complex<float>* const* in_block,
ekmdb4fecf2015-06-22 17:49:08 -070076 int in_channels,
77 int frames,
ekm030249d2015-06-15 13:02:24 -070078 int out_channels,
79 std::complex<float>* const* out_block);
80
81 private:
82 IntelligibilityEnhancer* parent_;
83 AudioSource source_;
84 };
85 friend class TransformCallback;
86
ekmdb4fecf2015-06-22 17:49:08 -070087 // Sends streams to ProcessClearBlock or ProcessNoiseBlock based on source.
88 void DispatchAudio(AudioSource source,
89 const std::complex<float>* in_block,
ekm030249d2015-06-15 13:02:24 -070090 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070091
92 // Updates variance computation and analysis with |in_block_|,
93 // and writes modified speech to |out_block|.
ekm030249d2015-06-15 13:02:24 -070094 void ProcessClearBlock(const std::complex<float>* in_block,
95 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070096
97 // Computes and sets modified gains.
ekm030249d2015-06-15 13:02:24 -070098 void AnalyzeClearBlock(float power_target);
ekmdb4fecf2015-06-22 17:49:08 -070099
100 // Updates variance calculation for noise input with |in_block|.
ekm030249d2015-06-15 13:02:24 -0700101 void ProcessNoiseBlock(const std::complex<float>* in_block,
102 std::complex<float>* out_block);
103
ekmdb4fecf2015-06-22 17:49:08 -0700104 // Returns number of ERB filters.
ekm030249d2015-06-15 13:02:24 -0700105 static int GetBankSize(int sample_rate, int erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -0700106
107 // Initializes ERB filterbank.
ekm030249d2015-06-15 13:02:24 -0700108 void CreateErbBank();
ekmdb4fecf2015-06-22 17:49:08 -0700109
110 // Analytically solves quadratic for optimal gains given |lambda|.
111 // Negative gains are set to 0. Stores the results in |sols|.
112 void SolveForGainsGivenLambda(float lambda, int start_freq, float* sols);
113
114 // Computes variance across ERB filters from freq variance |var|.
115 // Stores in |result|.
ekm030249d2015-06-15 13:02:24 -0700116 void FilterVariance(const float* var, float* result);
ekmdb4fecf2015-06-22 17:49:08 -0700117
118 // Returns dot product of vectors specified by size |length| arrays |a|,|b|.
ekm030249d2015-06-15 13:02:24 -0700119 static float DotProduct(const float* a, const float* b, int length);
120
121 static const int kErbResolution;
122 static const int kWindowSizeMs;
123 static const int kChunkSizeMs;
ekmdb4fecf2015-06-22 17:49:08 -0700124 static const int kAnalyzeRate; // Default for |analysis_rate_|.
125 static const int kVarianceRate; // Default for |variance_rate_|.
ekm030249d2015-06-15 13:02:24 -0700126 static const float kClipFreq;
ekmdb4fecf2015-06-22 17:49:08 -0700127 static const float kConfigRho; // Default production and interpretation SNR.
ekm030249d2015-06-15 13:02:24 -0700128 static const float kKbdAlpha;
129 static const float kGainChangeLimit;
130
ekmdb4fecf2015-06-22 17:49:08 -0700131 const int freqs_; // Num frequencies in frequency domain.
132 const int window_size_; // Window size in samples; also the block size.
133 const int chunk_length_; // Chunk size in samples.
134 const int bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -0700135 const int sample_rate_hz_;
136 const int erb_resolution_;
ekmdb4fecf2015-06-22 17:49:08 -0700137 const int channels_; // Num channels.
138 const int analysis_rate_; // Num blocks before gains recalculated.
139 const int variance_rate_; // Num recalculations before history is cleared.
ekm030249d2015-06-15 13:02:24 -0700140
141 intelligibility::VarianceArray clear_variance_;
142 intelligibility::VarianceArray noise_variance_;
ekmdb4fecf2015-06-22 17:49:08 -0700143 rtc::scoped_ptr<float[]> filtered_clear_var_;
144 rtc::scoped_ptr<float[]> filtered_noise_var_;
145 float** filter_bank_; // TODO(ekmeyerson): Switch to using ChannelBuffer.
146 rtc::scoped_ptr<float[]> center_freqs_;
ekm030249d2015-06-15 13:02:24 -0700147 int start_freq_;
ekmdb4fecf2015-06-22 17:49:08 -0700148 rtc::scoped_ptr<float[]> rho_; // Production and interpretation SNR.
149 // for each ERB band.
150 rtc::scoped_ptr<float[]> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700151 intelligibility::GainApplier gain_applier_;
152
153 // Destination buffer used to reassemble blocked chunks before overwriting
154 // the original input array with modifications.
ekmdb4fecf2015-06-22 17:49:08 -0700155 // TODO(ekmeyerson): Switch to using ChannelBuffer.
ekm030249d2015-06-15 13:02:24 -0700156 float** temp_out_buffer_;
ekmdb4fecf2015-06-22 17:49:08 -0700157
158 rtc::scoped_ptr<float* []> input_audio_;
159 rtc::scoped_ptr<float[]> kbd_window_;
ekm030249d2015-06-15 13:02:24 -0700160 TransformCallback render_callback_;
161 TransformCallback capture_callback_;
ekmdb4fecf2015-06-22 17:49:08 -0700162 rtc::scoped_ptr<LappedTransform> render_mangler_;
163 rtc::scoped_ptr<LappedTransform> capture_mangler_;
ekm030249d2015-06-15 13:02:24 -0700164 int block_count_;
165 int analysis_step_;
166
167 // TODO(bercic): Quick stopgap measure for voice detection in the clear
168 // and noise streams.
ekmdb4fecf2015-06-22 17:49:08 -0700169 // Note: VAD currently does not affect anything in IntelligibilityEnhancer.
ekm030249d2015-06-15 13:02:24 -0700170 VadInst* vad_high_;
171 VadInst* vad_low_;
ekmdb4fecf2015-06-22 17:49:08 -0700172 rtc::scoped_ptr<int16_t[]> vad_tmp_buffer_;
173 bool has_voice_low_; // Whether voice detected in speech stream.
ekm030249d2015-06-15 13:02:24 -0700174};
175
176} // namespace webrtc
177
178#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_