blob: 2deb4d243967779b7cfe790707311055aaf9ba0d [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
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_
13
14#include <complex>
kwiberg88788ad2016-02-19 07:04:49 -080015#include <memory>
ekm35b72fb2015-07-10 14:11:52 -070016#include <vector>
ekm030249d2015-06-15 13:02:24 -070017
18#include "webrtc/common_audio/lapped_transform.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070019#include "webrtc/common_audio/channel_buffer.h"
ekm030249d2015-06-15 13:02:24 -070020#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
ekm030249d2015-06-15 13:02:24 -070021
ekm030249d2015-06-15 13:02:24 -070022namespace webrtc {
23
24// Speech intelligibility enhancement module. Reads render and capture
25// audio streams and modifies the render stream with a set of gains per
26// frequency bin to enhance speech against the noise background.
Alejandro Luebs32348192016-02-17 20:04:19 -080027// Details of the model and algorithm can be found in the original paper:
28// http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
ekm030249d2015-06-15 13:02:24 -070029class IntelligibilityEnhancer {
30 public:
ekmeyerson60d9b332015-08-14 10:35:55 -070031 struct Config {
Alejandro Luebs32348192016-02-17 20:04:19 -080032 // TODO(bercic): the |decay_rate|, |analysis_rate| and |gain_limit|
33 // parameters should probably go away once fine tuning is done.
ekmeyerson60d9b332015-08-14 10:35:55 -070034 Config()
35 : sample_rate_hz(16000),
36 num_capture_channels(1),
37 num_render_channels(1),
Alejandro Luebs32348192016-02-17 20:04:19 -080038 decay_rate(0.9f),
39 analysis_rate(60),
ekmeyerson60d9b332015-08-14 10:35:55 -070040 gain_change_limit(0.1f),
41 rho(0.02f) {}
42 int sample_rate_hz;
Peter Kasting69558702016-01-12 16:26:35 -080043 size_t num_capture_channels;
44 size_t num_render_channels;
Alejandro Luebs32348192016-02-17 20:04:19 -080045 float decay_rate;
ekmeyerson60d9b332015-08-14 10:35:55 -070046 int analysis_rate;
47 float gain_change_limit;
48 float rho;
49 };
50
51 explicit IntelligibilityEnhancer(const Config& config);
52 IntelligibilityEnhancer(); // Initialize with default config.
ekm030249d2015-06-15 13:02:24 -070053
aluebsc466bad2016-02-10 12:03:00 -080054 // Sets the capture noise magnitude spectrum estimate.
55 void SetCaptureNoiseEstimate(std::vector<float> noise);
ekmb7553df2015-06-16 18:57:32 -070056
ekmdb4fecf2015-06-22 17:49:08 -070057 // Reads chunk of speech in time domain and updates with modified signal.
ekmeyerson60d9b332015-08-14 10:35:55 -070058 void ProcessRenderAudio(float* const* audio,
59 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080060 size_t num_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -070061 bool active() const;
ekmdb4fecf2015-06-22 17:49:08 -070062
ekm030249d2015-06-15 13:02:24 -070063 private:
ekmdb4fecf2015-06-22 17:49:08 -070064 // Provides access point to the frequency domain.
ekm030249d2015-06-15 13:02:24 -070065 class TransformCallback : public LappedTransform::Callback {
66 public:
aluebsc466bad2016-02-10 12:03:00 -080067 TransformCallback(IntelligibilityEnhancer* parent);
ekmdb4fecf2015-06-22 17:49:08 -070068
69 // All in frequency domain, receives input |in_block|, applies
70 // intelligibility enhancement, and writes result to |out_block|.
pkastingb297c5a2015-07-22 15:17:22 -070071 void ProcessAudioBlock(const std::complex<float>* const* in_block,
Peter Kasting69558702016-01-12 16:26:35 -080072 size_t in_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070073 size_t frames,
Peter Kasting69558702016-01-12 16:26:35 -080074 size_t out_channels,
pkastingb297c5a2015-07-22 15:17:22 -070075 std::complex<float>* const* out_block) override;
ekm030249d2015-06-15 13:02:24 -070076
77 private:
78 IntelligibilityEnhancer* parent_;
ekm030249d2015-06-15 13:02:24 -070079 };
80 friend class TransformCallback;
ekm35b72fb2015-07-10 14:11:52 -070081 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
82 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
ekm030249d2015-06-15 13:02:24 -070083
Alejandro Luebs32348192016-02-17 20:04:19 -080084 // Updates power computation and analysis with |in_block_|,
ekmdb4fecf2015-06-22 17:49:08 -070085 // and writes modified speech to |out_block|.
ekm030249d2015-06-15 13:02:24 -070086 void ProcessClearBlock(const std::complex<float>* in_block,
87 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070088
89 // Computes and sets modified gains.
Alejandro Luebs32348192016-02-17 20:04:19 -080090 void AnalyzeClearBlock();
ekmdb4fecf2015-06-22 17:49:08 -070091
ekm35b72fb2015-07-10 14:11:52 -070092 // Bisection search for optimal |lambda|.
93 void SolveForLambda(float power_target, float power_bot, float power_top);
94
95 // Transforms freq gains to ERB gains.
96 void UpdateErbGains();
97
ekmdb4fecf2015-06-22 17:49:08 -070098 // Returns number of ERB filters.
Peter Kastingdce40cf2015-08-24 14:52:23 -070099 static size_t GetBankSize(int sample_rate, size_t erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -0700100
101 // Initializes ERB filterbank.
aluebsc466bad2016-02-10 12:03:00 -0800102 std::vector<std::vector<float>> CreateErbBank(size_t num_freqs);
ekmdb4fecf2015-06-22 17:49:08 -0700103
104 // Analytically solves quadratic for optimal gains given |lambda|.
105 // Negative gains are set to 0. Stores the results in |sols|.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols);
ekmdb4fecf2015-06-22 17:49:08 -0700107
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 const size_t freqs_; // Num frequencies in frequency domain.
109 const size_t window_size_; // Window size in samples; also the block size.
110 const size_t chunk_length_; // Chunk size in samples.
111 const size_t bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -0700112 const int sample_rate_hz_;
113 const int erb_resolution_;
Peter Kasting69558702016-01-12 16:26:35 -0800114 const size_t num_capture_channels_;
115 const size_t num_render_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 const int analysis_rate_; // Num blocks before gains recalculated.
ekmeyerson60d9b332015-08-14 10:35:55 -0700117
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 const bool active_; // Whether render gains are being updated.
119 // TODO(ekm): Add logic for updating |active_|.
ekm030249d2015-06-15 13:02:24 -0700120
Alejandro Luebs32348192016-02-17 20:04:19 -0800121 intelligibility::PowerEstimator clear_power_;
aluebsc466bad2016-02-10 12:03:00 -0800122 std::vector<float> noise_power_;
kwiberg88788ad2016-02-19 07:04:49 -0800123 std::unique_ptr<float[]> filtered_clear_pow_;
124 std::unique_ptr<float[]> filtered_noise_pow_;
125 std::unique_ptr<float[]> center_freqs_;
aluebsc466bad2016-02-10 12:03:00 -0800126 std::vector<std::vector<float>> capture_filter_bank_;
127 std::vector<std::vector<float>> render_filter_bank_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700128 size_t start_freq_;
kwiberg88788ad2016-02-19 07:04:49 -0800129 std::unique_ptr<float[]> rho_; // Production and interpretation SNR.
ekmdb4fecf2015-06-22 17:49:08 -0700130 // for each ERB band.
kwiberg88788ad2016-02-19 07:04:49 -0800131 std::unique_ptr<float[]> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700132 intelligibility::GainApplier gain_applier_;
133
ekmeyerson60d9b332015-08-14 10:35:55 -0700134 // Destination buffers used to reassemble blocked chunks before overwriting
ekm030249d2015-06-15 13:02:24 -0700135 // the original input array with modifications.
ekmeyerson60d9b332015-08-14 10:35:55 -0700136 ChannelBuffer<float> temp_render_out_buffer_;
ekmdb4fecf2015-06-22 17:49:08 -0700137
kwiberg88788ad2016-02-19 07:04:49 -0800138 std::unique_ptr<float[]> kbd_window_;
ekm030249d2015-06-15 13:02:24 -0700139 TransformCallback render_callback_;
kwiberg88788ad2016-02-19 07:04:49 -0800140 std::unique_ptr<LappedTransform> render_mangler_;
ekm030249d2015-06-15 13:02:24 -0700141 int block_count_;
142 int analysis_step_;
ekm030249d2015-06-15 13:02:24 -0700143};
144
145} // namespace webrtc
146
147#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_