Move the downmixing out of AudioBuffer

This provides more flexibility if some component in AudioProcessing wants to operate before downmixing.
Now the AudioProcessing does only track the processing rate, but not the processing number of channels. This is tracked by the AudioBuffer itself and can be changed at any time to one smaller or equal the input number of channels. For each chunk it is reset to input number of channels and the end it should be equal to the output number of channels.

R=andrew@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7879 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.cc b/webrtc/modules/audio_processing/audio_processing_impl.cc
index 0c50ecb..ba22f33 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.cc
+++ b/webrtc/modules/audio_processing/audio_processing_impl.cc
@@ -87,8 +87,8 @@
       event_msg_(new audioproc::Event()),
 #endif
       fwd_in_format_(kSampleRate16kHz, 1),
-      fwd_proc_format_(kSampleRate16kHz, 1),
-      fwd_out_format_(kSampleRate16kHz),
+      fwd_proc_format_(kSampleRate16kHz),
+      fwd_out_format_(kSampleRate16kHz, 1),
       rev_in_format_(kSampleRate16kHz, 1),
       rev_proc_format_(kSampleRate16kHz, 1),
       split_rate_(kSampleRate16kHz),
@@ -152,7 +152,7 @@
                           rate,
                           rev_in_format_.rate(),
                           fwd_in_format_.num_channels(),
-                          fwd_proc_format_.num_channels(),
+                          fwd_out_format_.num_channels(),
                           rev_in_format_.num_channels());
 }
 
@@ -180,7 +180,7 @@
   capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
                                        fwd_in_format_.num_channels(),
                                        fwd_proc_format_.samples_per_channel(),
-                                       fwd_proc_format_.num_channels(),
+                                       fwd_out_format_.num_channels(),
                                        fwd_out_format_.samples_per_channel()));
 
   // Initialize all components.
@@ -226,7 +226,7 @@
   }
 
   fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
-  fwd_out_format_.set(output_sample_rate_hz);
+  fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
   rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
 
   // We process at the closest native rate >= min(input rate, output rate)...
@@ -244,7 +244,7 @@
     fwd_proc_rate = kSampleRate16kHz;
   }
 
-  fwd_proc_format_.set(fwd_proc_rate, num_output_channels);
+  fwd_proc_format_.set(fwd_proc_rate);
 
   // We normally process the reverse stream at 16 kHz. Unless...
   int rev_proc_rate = kSampleRate16kHz;
@@ -285,7 +285,7 @@
       output_sample_rate_hz == fwd_out_format_.rate() &&
       reverse_sample_rate_hz == rev_in_format_.rate() &&
       num_input_channels == fwd_in_format_.num_channels() &&
-      num_output_channels == fwd_proc_format_.num_channels() &&
+      num_output_channels == fwd_out_format_.num_channels() &&
       num_reverse_channels == rev_in_format_.num_channels()) {
     return kNoError;
   }
@@ -332,7 +332,7 @@
 }
 
 int AudioProcessingImpl::num_output_channels() const {
-  return fwd_proc_format_.num_channels();
+  return fwd_out_format_.num_channels();
 }
 
 void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
@@ -389,7 +389,7 @@
     audioproc::Stream* msg = event_msg_->mutable_stream();
     const size_t channel_size =
         sizeof(float) * fwd_out_format_.samples_per_channel();
-    for (int i = 0; i < fwd_proc_format_.num_channels(); ++i)
+    for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
       msg->add_output_channel(dest[i], channel_size);
     RETURN_ON_ERR(WriteMessageToDebugFile());
   }
@@ -513,7 +513,7 @@
                                       fwd_out_format_.rate(),
                                       sample_rate_hz,
                                       fwd_in_format_.num_channels(),
-                                      fwd_proc_format_.num_channels(),
+                                      fwd_out_format_.num_channels(),
                                       num_channels));
   if (samples_per_channel != rev_in_format_.samples_per_channel()) {
     return kBadDataLengthError;
@@ -774,7 +774,7 @@
 
 bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
   // Check if we've upmixed or downmixed the audio.
-  return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) ||
+  return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
           is_data_processed);
 }
 
@@ -828,7 +828,7 @@
   audioproc::Init* msg = event_msg_->mutable_init();
   msg->set_sample_rate(fwd_in_format_.rate());
   msg->set_num_input_channels(fwd_in_format_.num_channels());
-  msg->set_num_output_channels(fwd_proc_format_.num_channels());
+  msg->set_num_output_channels(fwd_out_format_.num_channels());
   msg->set_num_reverse_channels(rev_in_format_.num_channels());
   msg->set_reverse_sample_rate(rev_in_format_.rate());
   msg->set_output_sample_rate(fwd_out_format_.rate());