blob: c18bac0d85b0aa5d16c45d98c01797716bdf9453 [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"
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080021#include "webrtc/modules/audio_processing/vad/voice_activity_detector.h"
ekm030249d2015-06-15 13:02:24 -070022
ekm030249d2015-06-15 13:02:24 -070023namespace webrtc {
24
25// Speech intelligibility enhancement module. Reads render and capture
26// audio streams and modifies the render stream with a set of gains per
27// frequency bin to enhance speech against the noise background.
Alejandro Luebs32348192016-02-17 20:04:19 -080028// Details of the model and algorithm can be found in the original paper:
29// http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=6882788
ekm030249d2015-06-15 13:02:24 -070030class IntelligibilityEnhancer {
31 public:
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080032 IntelligibilityEnhancer(int sample_rate_hz, size_t num_render_channels);
ekm030249d2015-06-15 13:02:24 -070033
aluebsc466bad2016-02-10 12:03:00 -080034 // Sets the capture noise magnitude spectrum estimate.
35 void SetCaptureNoiseEstimate(std::vector<float> noise);
ekmb7553df2015-06-16 18:57:32 -070036
ekmdb4fecf2015-06-22 17:49:08 -070037 // Reads chunk of speech in time domain and updates with modified signal.
ekmeyerson60d9b332015-08-14 10:35:55 -070038 void ProcessRenderAudio(float* const* audio,
39 int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080040 size_t num_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -070041 bool active() const;
ekmdb4fecf2015-06-22 17:49:08 -070042
ekm030249d2015-06-15 13:02:24 -070043 private:
ekmdb4fecf2015-06-22 17:49:08 -070044 // Provides access point to the frequency domain.
ekm030249d2015-06-15 13:02:24 -070045 class TransformCallback : public LappedTransform::Callback {
46 public:
aluebsc466bad2016-02-10 12:03:00 -080047 TransformCallback(IntelligibilityEnhancer* parent);
ekmdb4fecf2015-06-22 17:49:08 -070048
49 // All in frequency domain, receives input |in_block|, applies
50 // intelligibility enhancement, and writes result to |out_block|.
pkastingb297c5a2015-07-22 15:17:22 -070051 void ProcessAudioBlock(const std::complex<float>* const* in_block,
Peter Kasting69558702016-01-12 16:26:35 -080052 size_t in_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070053 size_t frames,
Peter Kasting69558702016-01-12 16:26:35 -080054 size_t out_channels,
pkastingb297c5a2015-07-22 15:17:22 -070055 std::complex<float>* const* out_block) override;
ekm030249d2015-06-15 13:02:24 -070056
57 private:
58 IntelligibilityEnhancer* parent_;
ekm030249d2015-06-15 13:02:24 -070059 };
60 friend class TransformCallback;
ekm35b72fb2015-07-10 14:11:52 -070061 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestErbCreation);
62 FRIEND_TEST_ALL_PREFIXES(IntelligibilityEnhancerTest, TestSolveForGains);
ekm030249d2015-06-15 13:02:24 -070063
Alejandro Luebs32348192016-02-17 20:04:19 -080064 // Updates power computation and analysis with |in_block_|,
ekmdb4fecf2015-06-22 17:49:08 -070065 // and writes modified speech to |out_block|.
ekm030249d2015-06-15 13:02:24 -070066 void ProcessClearBlock(const std::complex<float>* in_block,
67 std::complex<float>* out_block);
ekmdb4fecf2015-06-22 17:49:08 -070068
ekm35b72fb2015-07-10 14:11:52 -070069 // Bisection search for optimal |lambda|.
70 void SolveForLambda(float power_target, float power_bot, float power_top);
71
72 // Transforms freq gains to ERB gains.
73 void UpdateErbGains();
74
ekmdb4fecf2015-06-22 17:49:08 -070075 // Returns number of ERB filters.
Peter Kastingdce40cf2015-08-24 14:52:23 -070076 static size_t GetBankSize(int sample_rate, size_t erb_resolution);
ekmdb4fecf2015-06-22 17:49:08 -070077
78 // Initializes ERB filterbank.
aluebsc466bad2016-02-10 12:03:00 -080079 std::vector<std::vector<float>> CreateErbBank(size_t num_freqs);
ekmdb4fecf2015-06-22 17:49:08 -070080
81 // Analytically solves quadratic for optimal gains given |lambda|.
82 // Negative gains are set to 0. Stores the results in |sols|.
Peter Kastingdce40cf2015-08-24 14:52:23 -070083 void SolveForGainsGivenLambda(float lambda, size_t start_freq, float* sols);
ekmdb4fecf2015-06-22 17:49:08 -070084
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080085 // Returns true if the audio is speech.
86 bool IsSpeech(const float* audio);
87
Peter Kastingdce40cf2015-08-24 14:52:23 -070088 const size_t freqs_; // Num frequencies in frequency domain.
Peter Kastingdce40cf2015-08-24 14:52:23 -070089 const size_t chunk_length_; // Chunk size in samples.
90 const size_t bank_size_; // Num ERB filters.
ekm030249d2015-06-15 13:02:24 -070091 const int sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -080092 const size_t num_render_channels_;
ekmeyerson60d9b332015-08-14 10:35:55 -070093
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080094 intelligibility::PowerEstimator<std::complex<float>> clear_power_estimator_;
95 std::unique_ptr<intelligibility::PowerEstimator<float>>
96 noise_power_estimator_;
kwiberg88788ad2016-02-19 07:04:49 -080097 std::unique_ptr<float[]> filtered_clear_pow_;
98 std::unique_ptr<float[]> filtered_noise_pow_;
99 std::unique_ptr<float[]> center_freqs_;
aluebsc466bad2016-02-10 12:03:00 -0800100 std::vector<std::vector<float>> capture_filter_bank_;
101 std::vector<std::vector<float>> render_filter_bank_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700102 size_t start_freq_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800103
kwiberg88788ad2016-02-19 07:04:49 -0800104 std::unique_ptr<float[]> gains_eq_; // Pre-filter modified gains.
ekm030249d2015-06-15 13:02:24 -0700105 intelligibility::GainApplier gain_applier_;
106
ekmeyerson60d9b332015-08-14 10:35:55 -0700107 // Destination buffers used to reassemble blocked chunks before overwriting
ekm030249d2015-06-15 13:02:24 -0700108 // the original input array with modifications.
ekmeyerson60d9b332015-08-14 10:35:55 -0700109 ChannelBuffer<float> temp_render_out_buffer_;
ekmdb4fecf2015-06-22 17:49:08 -0700110
ekm030249d2015-06-15 13:02:24 -0700111 TransformCallback render_callback_;
kwiberg88788ad2016-02-19 07:04:49 -0800112 std::unique_ptr<LappedTransform> render_mangler_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -0800113
114 VoiceActivityDetector vad_;
115 std::vector<int16_t> audio_s16_;
116 size_t chunks_since_voice_;
117 bool is_speech_;
ekm030249d2015-06-15 13:02:24 -0700118};
119
120} // namespace webrtc
121
122#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_ENHANCER_H_