blob: 8faf919920a8b97ee3eab8bf653c3afd4856ade5 [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
ekmeyerson60d9b332015-08-14 10:35:55 -070018#include "webrtc/common_audio/channel_buffer.h"
Alejandro Luebsef009252016-09-20 14:51:56 -070019#include "webrtc/common_audio/lapped_transform.h"
20#include "webrtc/modules/audio_processing/audio_buffer.h"
ekm030249d2015-06-15 13:02:24 -070021#include "webrtc/modules/audio_processing/intelligibility/intelligibility_utils.h"
peah737f4b82016-03-10 23:05:28 -080022#include "webrtc/modules/audio_processing/render_queue_item_verifier.h"
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080023#include "webrtc/modules/audio_processing/vad/voice_activity_detector.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020024#include "webrtc/rtc_base/swap_queue.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.
Alejandro Luebs32348192016-02-17 20:04:19 -080031// Details of the model and algorithm can be found in the original paper:
32// http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
aluebs0a007592016-02-26 17:17:38 -080033class IntelligibilityEnhancer : public LappedTransform::Callback {
ekm030249d2015-06-15 13:02:24 -070034 public:
Alex Luebs57ae8292016-03-09 16:24:34 +010035 IntelligibilityEnhancer(int sample_rate_hz,
36 size_t num_render_channels,
Alejandro Luebsef009252016-09-20 14:51:56 -070037 size_t num_bands,
Alex Luebs57ae8292016-03-09 16:24:34 +010038 size_t num_noise_bins);
ekm030249d2015-06-15 13:02:24 -070039
Alejandro Luebs37062ed2016-09-06 11:25:40 -070040 ~IntelligibilityEnhancer() override;
Alejandro Luebs1aa82192016-07-01 17:16:01 -070041
aluebsc466bad2016-02-10 12:03:00 -080042 // Sets the capture noise magnitude spectrum estimate.
Alejandro Luebs50411102016-06-30 15:35:41 -070043 void SetCaptureNoiseEstimate(std::vector<float> noise, float gain);
ekmb7553df2015-06-16 18:57:32 -070044
ekmdb4fecf2015-06-22 17:49:08 -070045 // Reads chunk of speech in time domain and updates with modified signal.
Alejandro Luebsef009252016-09-20 14:51:56 -070046 void ProcessRenderAudio(AudioBuffer* audio);
ekmeyerson60d9b332015-08-14 10:35:55 -070047 bool active() const;
ekmdb4fecf2015-06-22 17:49:08 -070048
aluebs0a007592016-02-26 17:17:38 -080049 protected:
50 // All in frequency domain, receives input |in_block|, applies
51 // intelligibility enhancement, and writes result to |out_block|.
52 void ProcessAudioBlock(const std::complex<float>* const* in_block,
53 size_t in_channels,
54 size_t frames,
55 size_t out_channels,
56 std::complex<float>* const* out_block) override;
57
ekm030249d2015-06-15 13:02:24 -070058 private:
Alejandro Luebsef009252016-09-20 14:51:56 -070059 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestRenderUpdate);
ekm35b72fb2015-07-10 14:11:52 -070060 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
61 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
aluebs11d4a422016-04-28 14:58:32 -070062 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest,
63 TestNoiseGainHasExpectedResult);
Alejandro Luebsef009252016-09-20 14:51:56 -070064 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest,
65 TestAllBandsHaveSameDelay);
ekm030249d2015-06-15 13:02:24 -070066
aluebs2fae89e2016-04-13 11:24:06 -070067 // Updates the SNR estimation and enables or disables this component using a
68 // hysteresis.
69 void SnrBasedEffectActivation();
70
ekm35b72fb2015-07-10 14:11:52 -070071 // Bisection search for optimal |lambda|.
aluebsf99af6b2016-02-24 17:25:42 -080072 void SolveForLambda(float power_target);
ekm35b72fb2015-07-10 14:11:52 -070073
74 // Transforms freq gains to ERB gains.
75 void UpdateErbGains();
76
ekmdb4fecf2015-06-22 17:49:08 -070077 // Returns number of ERB filters.
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 static size_t GetBankSize(int sample_rate, size_t erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -070079
80 // Initializes ERB filterbank.
aluebsc466bad2016-02-10 12:03:00 -080081 std::vector<std::vector<float>> CreateErbBank(size_t num_freqs);
ekmdb4fecf2015-06-22 17:49:08 -070082
83 // Analytically solves quadratic for optimal gains given |lambda|.
84 // Negative gains are set to 0. Stores the results in |sols|.
Alejandro Luebsdd56fa82016-03-29 13:05:40 -070085 void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols);
ekmdb4fecf2015-06-22 17:49:08 -070086
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080087 // Returns true if the audio is speech.
88 bool IsSpeech(const float* audio);
89
Alejandro Luebsef009252016-09-20 14:51:56 -070090 // Delays the high bands to compensate for the processing delay in the low
91 // band.
92 void DelayHighBands(AudioBuffer* audio);
93
Alex Luebs57ae8292016-03-09 16:24:34 +010094 static const size_t kMaxNumNoiseEstimatesToBuffer = 5;
95
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 const size_t freqs_; // Num frequencies in frequency domain.
Alex Luebs57ae8292016-03-09 16:24:34 +010097 const size_t num_noise_bins_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070098 const size_t chunk_length_; // Chunk size in samples.
99 const size_t bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -0700100 const int sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800101 const size_t num_render_channels_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700102
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800103 intelligibility::PowerEstimator<std::complex<float>> clear_power_estimator_;
Alex Luebs57ae8292016-03-09 16:24:34 +0100104 intelligibility::PowerEstimator<float> noise_power_estimator_;
aluebs0a007592016-02-26 17:17:38 -0800105 std::vector<float> filtered_clear_pow_;
106 std::vector<float> filtered_noise_pow_;
107 std::vector<float> center_freqs_;
aluebsc466bad2016-02-10 12:03:00 -0800108 std::vector<std::vector<float>> capture_filter_bank_;
109 std::vector<std::vector<float>> render_filter_bank_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700110 size_t start_freq_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800111
aluebs0a007592016-02-26 17:17:38 -0800112 std::vector<float> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700113 intelligibility::GainApplier gain_applier_;
114
kwiberg88788ad2016-02-19 07:04:49 -0800115 std::unique_ptr<LappedTransform> render_mangler_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800116
117 VoiceActivityDetector vad_;
118 std::vector<int16_t> audio_s16_;
119 size_t chunks_since_voice_;
120 bool is_speech_;
aluebs2fae89e2016-04-13 11:24:06 -0700121 float snr_;
122 bool is_active_;
Alex Luebs57ae8292016-03-09 16:24:34 +0100123
Alejandro Luebs1aa82192016-07-01 17:16:01 -0700124 unsigned long int num_chunks_;
125 unsigned long int num_active_chunks_;
aluebse8f8f602016-06-08 09:53:18 -0700126
Alex Luebs57ae8292016-03-09 16:24:34 +0100127 std::vector<float> noise_estimation_buffer_;
128 SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>
129 noise_estimation_queue_;
Alejandro Luebsef009252016-09-20 14:51:56 -0700130
131 std::vector<std::unique_ptr<intelligibility::DelayBuffer>>
132 high_bands_buffers_;
ekm030249d2015-06-15 13:02:24 -0700133};
134
135} // namespace webrtc
136
137#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_