From fda2c2e810a815d98fe8b03e8c6687d14227b3ff Mon Sep 17 00:00:00 2001
From: "aluebs@webrtc.org" <aluebs@webrtc.org>
Date: Thu, 18 Sep 2014 09:54:06 +0000
Subject: [PATCH] Add Analyze API to NS

This adds an empty API.
In a next CL I will separate the noise estimation from the Process API and fill this function.

BUG=webrtc:3811
R=bjornv@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/23599004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7218 4adac7df-926f-26a2-2b94-8c16560cd09d
---
 .../audio_processing/audio_processing_impl.cc |  1 +
 .../noise_suppression_impl.cc                 | 27 ++++++++++++++++---
 .../audio_processing/noise_suppression_impl.h |  1 +
 .../ns/include/noise_suppression.h            | 15 +++++++++++
 .../audio_processing/ns/noise_suppression.c   |  3 +++
 webrtc/modules/audio_processing/ns/ns_core.c  |  4 +++
 webrtc/modules/audio_processing/ns/ns_core.h  | 19 +++++++++++--
 7 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/webrtc/modules/audio_processing/audio_processing_impl.cc b/webrtc/modules/audio_processing/audio_processing_impl.cc
index 9d7f1ec31..659b794a6 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.cc
+++ b/webrtc/modules/audio_processing/audio_processing_impl.cc
@@ -487,6 +487,7 @@ int AudioProcessingImpl::ProcessStreamLocked() {
   if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
     ca->CopyLowPassToReference();
   }
+  RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
   RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
   RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
   RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
diff --git a/webrtc/modules/audio_processing/noise_suppression_impl.cc b/webrtc/modules/audio_processing/noise_suppression_impl.cc
index eea0a04a2..ab8dadae2 100644
--- a/webrtc/modules/audio_processing/noise_suppression_impl.cc
+++ b/webrtc/modules/audio_processing/noise_suppression_impl.cc
@@ -55,6 +55,27 @@ NoiseSuppressionImpl::NoiseSuppressionImpl(const AudioProcessing* apm,
 
 NoiseSuppressionImpl::~NoiseSuppressionImpl() {}
 
+int NoiseSuppressionImpl::AnalyzeCaptureAudio(AudioBuffer* audio) {
+#if defined(WEBRTC_NS_FLOAT)
+  if (!is_component_enabled()) {
+    return apm_->kNoError;
+  }
+  assert(audio->samples_per_split_channel() <= 160);
+  assert(audio->num_channels() == num_handles());
+
+  for (int i = 0; i < num_handles(); ++i) {
+    Handle* my_handle = static_cast<Handle*>(handle(i));
+
+    int err = WebRtcNs_Analyze(my_handle,
+                               audio->low_pass_split_data_f(i));
+    if (err != apm_->kNoError) {
+      return GetHandleError(my_handle);
+    }
+  }
+#endif
+  return apm_->kNoError;
+}
+
 int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) {
   int err = apm_->kNoError;
 
@@ -64,16 +85,16 @@ int NoiseSuppressionImpl::ProcessCaptureAudio(AudioBuffer* audio) {
   assert(audio->samples_per_split_channel() <= 160);
   assert(audio->num_channels() == num_handles());
 
-  for (int i = 0; i < num_handles(); i++) {
+  for (int i = 0; i < num_handles(); ++i) {
     Handle* my_handle = static_cast<Handle*>(handle(i));
 #if defined(WEBRTC_NS_FLOAT)
-    err = WebRtcNs_Process(static_cast<Handle*>(handle(i)),
+    err = WebRtcNs_Process(my_handle,
                            audio->low_pass_split_data_f(i),
                            audio->high_pass_split_data_f(i),
                            audio->low_pass_split_data_f(i),
                            audio->high_pass_split_data_f(i));
 #elif defined(WEBRTC_NS_FIXED)
-    err = WebRtcNsx_Process(static_cast<Handle*>(handle(i)),
+    err = WebRtcNsx_Process(my_handle,
                             audio->low_pass_split_data(i),
                             audio->high_pass_split_data(i),
                             audio->low_pass_split_data(i),
diff --git a/webrtc/modules/audio_processing/noise_suppression_impl.h b/webrtc/modules/audio_processing/noise_suppression_impl.h
index cadbbd9cd..14abe66b6 100644
--- a/webrtc/modules/audio_processing/noise_suppression_impl.h
+++ b/webrtc/modules/audio_processing/noise_suppression_impl.h
@@ -26,6 +26,7 @@ class NoiseSuppressionImpl : public NoiseSuppression,
                        CriticalSectionWrapper* crit);
   virtual ~NoiseSuppressionImpl();
 
+  int AnalyzeCaptureAudio(AudioBuffer* audio);
   int ProcessCaptureAudio(AudioBuffer* audio);
 
   // NoiseSuppression implementation.
diff --git a/webrtc/modules/audio_processing/ns/include/noise_suppression.h b/webrtc/modules/audio_processing/ns/include/noise_suppression.h
index 3cf889e2d..093f11852 100644
--- a/webrtc/modules/audio_processing/ns/include/noise_suppression.h
+++ b/webrtc/modules/audio_processing/ns/include/noise_suppression.h
@@ -79,6 +79,21 @@ int WebRtcNs_Init(NsHandle* NS_inst, uint32_t fs);
  */
 int WebRtcNs_set_policy(NsHandle* NS_inst, int mode);
 
+/*
+ * This functions estimates the background noise for the inserted speech frame.
+ * The input and output signals should always be 10ms (80 or 160 samples).
+ *
+ * Input
+ *      - NS_inst       : Noise suppression instance.
+ *      - spframe       : Pointer to speech frame buffer for L band
+ *
+ * Output:
+ *      - NS_inst       : Updated NS instance
+ *
+ * Return value         :  0 - OK
+ *                        -1 - Error
+ */
+int WebRtcNs_Analyze(NsHandle* NS_inst, float* spframe);
 
 /*
  * This functions does Noise Suppression for the inserted speech frame. The
diff --git a/webrtc/modules/audio_processing/ns/noise_suppression.c b/webrtc/modules/audio_processing/ns/noise_suppression.c
index 075ab88c1..0015e3857 100644
--- a/webrtc/modules/audio_processing/ns/noise_suppression.c
+++ b/webrtc/modules/audio_processing/ns/noise_suppression.c
@@ -42,6 +42,9 @@ int WebRtcNs_set_policy(NsHandle* NS_inst, int mode) {
   return WebRtcNs_set_policy_core((NSinst_t*) NS_inst, mode);
 }
 
+int WebRtcNs_Analyze(NsHandle* NS_inst, float* spframe) {
+  return WebRtcNs_AnalyzeCore((NSinst_t*) NS_inst, spframe);
+}
 
 int WebRtcNs_Process(NsHandle* NS_inst, float* spframe, float* spframe_H,
                      float* outframe, float* outframe_H) {
diff --git a/webrtc/modules/audio_processing/ns/ns_core.c b/webrtc/modules/audio_processing/ns/ns_core.c
index a6a2deab8..0d60aafc0 100644
--- a/webrtc/modules/audio_processing/ns/ns_core.c
+++ b/webrtc/modules/audio_processing/ns/ns_core.c
@@ -714,6 +714,10 @@ void WebRtcNs_SpeechNoiseProb(NSinst_t* inst, float* probSpeechFinal, float* snr
   }
 }
 
+int WebRtcNs_AnalyzeCore(NSinst_t* inst, float* inFrame) {
+  return 0;
+}
+
 int WebRtcNs_ProcessCore(NSinst_t* inst,
                          float* speechFrame,
                          float* speechFrameHB,
diff --git a/webrtc/modules/audio_processing/ns/ns_core.h b/webrtc/modules/audio_processing/ns/ns_core.h
index 785239ebd..2d4b23cf5 100644
--- a/webrtc/modules/audio_processing/ns/ns_core.h
+++ b/webrtc/modules/audio_processing/ns/ns_core.h
@@ -146,6 +146,23 @@ int WebRtcNs_InitCore(NSinst_t* inst, uint32_t fs);
  */
 int WebRtcNs_set_policy_core(NSinst_t* inst, int mode);
 
+/****************************************************************************
+ * WebRtcNs_AnalyzeCore
+ *
+ * Estimate the background noise.
+ *
+ * Input:
+ *      - inst          : Instance that should be initialized
+ *      - inFrame       : Input speech frame for lower band
+ *
+ * Output:
+ *      - inst          : Updated instance
+ *
+ * Return value         :  0 - OK
+ *                        -1 - Error
+ */
+int WebRtcNs_AnalyzeCore(NSinst_t* inst, float* inFrame);
+
 /****************************************************************************
  * WebRtcNs_ProcessCore
  *
@@ -164,8 +181,6 @@ int WebRtcNs_set_policy_core(NSinst_t* inst, int mode);
  * Return value         :  0 - OK
  *                        -1 - Error
  */
-
-
 int WebRtcNs_ProcessCore(NSinst_t* inst,
                          float* inFrameLow,
                          float* inFrameHigh,