blob: 111b765f973485bb888785cdd6946693b4c445ba [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
terelius85fa7d52016-03-24 01:51:52 -070018#include "webrtc/base/swap_queue.h"
peahfaed4ab2016-04-05 14:57:48 -070019#include "webrtc/common_audio/lapped_transform.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070020#include "webrtc/common_audio/channel_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"
ekm030249d2015-06-15 13:02:24 -070024
ekm030249d2015-06-15 13:02:24 -070025namespace webrtc {
26
27// Speech intelligibility enhancement module. Reads render and capture
28// audio streams and modifies the render stream with a set of gains per
29// frequency bin to enhance speech against the noise background.
Alejandro Luebs32348192016-02-17 20:04:19 -080030// Details of the model and algorithm can be found in the original paper:
31// http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
aluebs0a007592016-02-26 17:17:38 -080032class IntelligibilityEnhancer : public LappedTransform::Callback {
ekm030249d2015-06-15 13:02:24 -070033 public:
Alex Luebs57ae8292016-03-09 16:24:34 +010034 IntelligibilityEnhancer(int sample_rate_hz,
35 size_t num_render_channels,
36 size_t num_noise_bins);
ekm030249d2015-06-15 13:02:24 -070037
aluebsc466bad2016-02-10 12:03:00 -080038 // Sets the capture noise magnitude spectrum estimate.
39 void SetCaptureNoiseEstimate(std::vector<float> noise);
ekmb7553df2015-06-16 18:57:32 -070040
ekmdb4fecf2015-06-22 17:49:08 -070041 // Reads chunk of speech in time domain and updates with modified signal.
ekmeyerson60d9b332015-08-14 10:35:55 -070042 void ProcessRenderAudio(float* const* audio,
43 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080044 size_t num_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -070045 bool active() const;
ekmdb4fecf2015-06-22 17:49:08 -070046
aluebs0a007592016-02-26 17:17:38 -080047 protected:
48 // All in frequency domain, receives input |in_block|, applies
49 // intelligibility enhancement, and writes result to |out_block|.
50 void ProcessAudioBlock(const std::complex<float>* const* in_block,
51 size_t in_channels,
52 size_t frames,
53 size_t out_channels,
54 std::complex<float>* const* out_block) override;
55
ekm030249d2015-06-15 13:02:24 -070056 private:
ekm35b72fb2015-07-10 14:11:52 -070057 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
58 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
ekm030249d2015-06-15 13:02:24 -070059
aluebs2fae89e2016-04-13 11:24:06 -070060 // Updates the SNR estimation and enables or disables this component using a
61 // hysteresis.
62 void SnrBasedEffectActivation();
63
ekm35b72fb2015-07-10 14:11:52 -070064 // Bisection search for optimal |lambda|.
aluebsf99af6b2016-02-24 17:25:42 -080065 void SolveForLambda(float power_target);
ekm35b72fb2015-07-10 14:11:52 -070066
67 // Transforms freq gains to ERB gains.
68 void UpdateErbGains();
69
ekmdb4fecf2015-06-22 17:49:08 -070070 // Returns number of ERB filters.
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 static size_t GetBankSize(int sample_rate, size_t erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -070072
73 // Initializes ERB filterbank.
aluebsc466bad2016-02-10 12:03:00 -080074 std::vector<std::vector<float>> CreateErbBank(size_t num_freqs);
ekmdb4fecf2015-06-22 17:49:08 -070075
76 // Analytically solves quadratic for optimal gains given |lambda|.
77 // Negative gains are set to 0. Stores the results in |sols|.
Alejandro Luebsdd56fa82016-03-29 13:05:40 -070078 void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols);
ekmdb4fecf2015-06-22 17:49:08 -070079
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080080 // Returns true if the audio is speech.
81 bool IsSpeech(const float* audio);
82
Alex Luebs57ae8292016-03-09 16:24:34 +010083 static const size_t kMaxNumNoiseEstimatesToBuffer = 5;
84
Peter Kastingdce40cf2015-08-24 14:52:23 -070085 const size_t freqs_; // Num frequencies in frequency domain.
Alex Luebs57ae8292016-03-09 16:24:34 +010086 const size_t num_noise_bins_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070087 const size_t chunk_length_; // Chunk size in samples.
88 const size_t bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -070089 const int sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -080090 const size_t num_render_channels_;
ekmeyerson60d9b332015-08-14 10:35:55 -070091
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080092 intelligibility::PowerEstimator<std::complex<float>> clear_power_estimator_;
Alex Luebs57ae8292016-03-09 16:24:34 +010093 intelligibility::PowerEstimator<float> noise_power_estimator_;
aluebs0a007592016-02-26 17:17:38 -080094 std::vector<float> filtered_clear_pow_;
95 std::vector<float> filtered_noise_pow_;
96 std::vector<float> center_freqs_;
aluebsc466bad2016-02-10 12:03:00 -080097 std::vector<std::vector<float>> capture_filter_bank_;
98 std::vector<std::vector<float>> render_filter_bank_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070099 size_t start_freq_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800100
aluebs0a007592016-02-26 17:17:38 -0800101 std::vector<float> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700102 intelligibility::GainApplier gain_applier_;
103
kwiberg88788ad2016-02-19 07:04:49 -0800104 std::unique_ptr<LappedTransform> render_mangler_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800105
106 VoiceActivityDetector vad_;
107 std::vector<int16_t> audio_s16_;
108 size_t chunks_since_voice_;
109 bool is_speech_;
aluebs2fae89e2016-04-13 11:24:06 -0700110 float snr_;
111 bool is_active_;
Alex Luebs57ae8292016-03-09 16:24:34 +0100112
113 std::vector<float> noise_estimation_buffer_;
114 SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>
115 noise_estimation_queue_;
ekm030249d2015-06-15 13:02:24 -0700116};
117
118} // namespace webrtc
119
120#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_