blob: fade1449ccdb3886af3dae5154fef3ee6f176bac [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"
ekmeyerson60d9b332015-08-14 10:35:55 -070023#include "webrtc/common_audio/channel_buffer.h"
ekm030249d2015-06-15 13:02:24 -070024#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
ekm030249d2015-06-15 13:02:24 -070025
ekm030249d2015-06-15 13:02:24 -070026namespace webrtc {
27
28// Speech intelligibility enhancement module. Reads render and capture
29// audio streams and modifies the render stream with a set of gains per
30// frequency bin to enhance speech against the noise background.
ekmdb4fecf2015-06-22 17:49:08 -070031// Note: assumes speech and noise streams are already separated.
ekm030249d2015-06-15 13:02:24 -070032class IntelligibilityEnhancer {
33 public:
ekmeyerson60d9b332015-08-14 10:35:55 -070034 struct Config {
35 // |var_*| are parameters for the VarianceArray constructor for the
36 // clear speech stream.
37 // TODO(bercic): the |var_*|, |*_rate| and |gain_limit| parameters should
38 // probably go away once fine tuning is done.
39 Config()
40 : sample_rate_hz(16000),
41 num_capture_channels(1),
42 num_render_channels(1),
43 var_type(intelligibility::VarianceArray::kStepDecaying),
44 var_decay_rate(0.9f),
45 var_window_size(10),
46 analysis_rate(800),
47 gain_change_limit(0.1f),
48 rho(0.02f) {}
49 int sample_rate_hz;
Peter Kasting69558702016-01-12 16:26:35 -080050 size_t num_capture_channels;
51 size_t num_render_channels;
ekmeyerson60d9b332015-08-14 10:35:55 -070052 intelligibility::VarianceArray::StepType var_type;
53 float var_decay_rate;
Peter Kastingdce40cf2015-08-24 14:52:23 -070054 size_t var_window_size;
ekmeyerson60d9b332015-08-14 10:35:55 -070055 int analysis_rate;
56 float gain_change_limit;
57 float rho;
58 };
59
60 explicit IntelligibilityEnhancer(const Config& config);
61 IntelligibilityEnhancer(); // Initialize with default config.
ekm030249d2015-06-15 13:02:24 -070062
aluebsc466bad2016-02-10 12:03:00 -080063 // Sets the capture noise magnitude spectrum estimate.
64 void SetCaptureNoiseEstimate(std::vector<float> noise);
ekmb7553df2015-06-16 18:57:32 -070065
ekmdb4fecf2015-06-22 17:49:08 -070066 // Reads chunk of speech in time domain and updates with modified signal.
ekmeyerson60d9b332015-08-14 10:35:55 -070067 void ProcessRenderAudio(float* const* audio,
68 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080069 size_t num_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -070070 bool active() const;
ekmdb4fecf2015-06-22 17:49:08 -070071
ekm030249d2015-06-15 13:02:24 -070072 private:
ekmdb4fecf2015-06-22 17:49:08 -070073 // Provides access point to the frequency domain.
ekm030249d2015-06-15 13:02:24 -070074 class TransformCallback : public LappedTransform::Callback {
75 public:
aluebsc466bad2016-02-10 12:03:00 -080076 TransformCallback(IntelligibilityEnhancer* parent);
ekmdb4fecf2015-06-22 17:49:08 -070077
78 // All in frequency domain, receives input |in_block|, applies
79 // intelligibility enhancement, and writes result to |out_block|.
pkastingb297c5a2015-07-22 15:17:22 -070080 void ProcessAudioBlock(const std::complex<float>* const* in_block,
Peter Kasting69558702016-01-12 16:26:35 -080081 size_t in_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070082 size_t frames,
Peter Kasting69558702016-01-12 16:26:35 -080083 size_t out_channels,
pkastingb297c5a2015-07-22 15:17:22 -070084 std::complex<float>* const* out_block) override;
ekm030249d2015-06-15 13:02:24 -070085
86 private:
87 IntelligibilityEnhancer* parent_;
ekm030249d2015-06-15 13:02:24 -070088 };
89 friend class TransformCallback;
ekm35b72fb2015-07-10 14:11:52 -070090 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
91 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
ekm030249d2015-06-15 13:02:24 -070092
ekmdb4fecf2015-06-22 17:49:08 -070093 // Updates variance computation and analysis with |in_block_|,
94 // and writes modified speech to |out_block|.
ekm030249d2015-06-15 13:02:24 -070095 void ProcessClearBlock(const std::complex<float>* in_block,
96 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070097
98 // Computes and sets modified gains.
ekm030249d2015-06-15 13:02:24 -070099 void AnalyzeClearBlock(float power_target);
ekmdb4fecf2015-06-22 17:49:08 -0700100
ekm35b72fb2015-07-10 14:11:52 -0700101 // Bisection search for optimal |lambda|.
102 void SolveForLambda(float power_target, float power_bot, float power_top);
103
104 // Transforms freq gains to ERB gains.
105 void UpdateErbGains();
106
ekmdb4fecf2015-06-22 17:49:08 -0700107 // Returns number of ERB filters.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 static size_t GetBankSize(int sample_rate, size_t erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -0700109
110 // Initializes ERB filterbank.
aluebsc466bad2016-02-10 12:03:00 -0800111 std::vector<std::vector<float>> CreateErbBank(size_t num_freqs);
ekmdb4fecf2015-06-22 17:49:08 -0700112
113 // Analytically solves quadratic for optimal gains given |lambda|.
114 // Negative gains are set to 0. Stores the results in |sols|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700115 void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols);
ekmdb4fecf2015-06-22 17:49:08 -0700116
Peter Kastingdce40cf2015-08-24 14:52:23 -0700117 const size_t freqs_; // Num frequencies in frequency domain.
118 const size_t window_size_; // Window size in samples; also the block size.
119 const size_t chunk_length_; // Chunk size in samples.
120 const size_t bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -0700121 const int sample_rate_hz_;
122 const int erb_resolution_;
Peter Kasting69558702016-01-12 16:26:35 -0800123 const size_t num_capture_channels_;
124 const size_t num_render_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700125 const int analysis_rate_; // Num blocks before gains recalculated.
ekmeyerson60d9b332015-08-14 10:35:55 -0700126
Peter Kastingdce40cf2015-08-24 14:52:23 -0700127 const bool active_; // Whether render gains are being updated.
128 // TODO(ekm): Add logic for updating |active_|.
ekm030249d2015-06-15 13:02:24 -0700129
130 intelligibility::VarianceArray clear_variance_;
aluebsc466bad2016-02-10 12:03:00 -0800131 std::vector<float> noise_power_;
ekmdb4fecf2015-06-22 17:49:08 -0700132 rtc::scoped_ptr<float[]> filtered_clear_var_;
133 rtc::scoped_ptr<float[]> filtered_noise_var_;
ekmdb4fecf2015-06-22 17:49:08 -0700134 rtc::scoped_ptr<float[]> center_freqs_;
aluebsc466bad2016-02-10 12:03:00 -0800135 std::vector<std::vector<float>> capture_filter_bank_;
136 std::vector<std::vector<float>> render_filter_bank_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 size_t start_freq_;
ekmdb4fecf2015-06-22 17:49:08 -0700138 rtc::scoped_ptr<float[]> rho_; // Production and interpretation SNR.
139 // for each ERB band.
140 rtc::scoped_ptr<float[]> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700141 intelligibility::GainApplier gain_applier_;
142
ekmeyerson60d9b332015-08-14 10:35:55 -0700143 // Destination buffers used to reassemble blocked chunks before overwriting
ekm030249d2015-06-15 13:02:24 -0700144 // the original input array with modifications.
ekmeyerson60d9b332015-08-14 10:35:55 -0700145 ChannelBuffer<float> temp_render_out_buffer_;
ekmdb4fecf2015-06-22 17:49:08 -0700146
ekmdb4fecf2015-06-22 17:49:08 -0700147 rtc::scoped_ptr<float[]> kbd_window_;
ekm030249d2015-06-15 13:02:24 -0700148 TransformCallback render_callback_;
ekmdb4fecf2015-06-22 17:49:08 -0700149 rtc::scoped_ptr<LappedTransform> render_mangler_;
ekm030249d2015-06-15 13:02:24 -0700150 int block_count_;
151 int analysis_step_;
ekm030249d2015-06-15 13:02:24 -0700152};
153
154} // namespace webrtc
155
156#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_