Index: webrtc/common_audio/include/audio_util.h |
diff --git a/webrtc/common_audio/include/audio_util.h b/webrtc/common_audio/include/audio_util.h |
index 8262649145546180f23b6e82094d324b7f604e7f..909afed5156047fa701007fc513ab076f31b5944 100644 |
--- a/webrtc/common_audio/include/audio_util.h |
+++ b/webrtc/common_audio/include/audio_util.h |
@@ -12,6 +12,7 @@ |
#define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |
#include <limits> |
+#include <cstring> |
#include "webrtc/base/scoped_ptr.h" |
#include "webrtc/typedefs.h" |
@@ -97,6 +98,56 @@ void Interleave(const T* const* deinterleaved, int samples_per_channel, |
} |
} |
+template <typename T, typename Intermediate> |
+void DownmixToMono(const T* const* input_channels, |
+ int num_frames, |
+ int num_channels, |
+ T* out) { |
+ for (int i = 0; i < num_frames; ++i) { |
+ Intermediate value = input_channels[0][i]; |
+ for (int j = 1; j < num_channels; ++j) { |
+ value += input_channels[j][i]; |
+ } |
+ out[i] = value / num_channels; |
+ } |
+} |
+ |
+// Downmixes an interleaved multichannel signal to a single channel by averaging |
+// all channels. |
+template <typename T, typename Intermediate> |
+void DownmixInterleavedToMonoImpl(const T* interleaved, |
+ int num_multichannel_frames, |
Andrew MacDonald
2015/07/22 22:47:21
Why "multichannel"? To differentiate from the "fra
mgraczyk
2015/07/23 00:16:54
Done.
|
+ int num_channels, |
+ T* deinterleaved) { |
+ assert(num_channels > 0); |
Andrew MacDonald
2015/07/22 22:47:21
For new code we should use DCHECK.
mgraczyk
2015/07/23 00:16:54
Done.
|
+ assert(num_multichannel_frames > 0); |
+ |
+ const T* const end = interleaved + num_multichannel_frames * num_channels; |
+ |
+ while (interleaved < end) { |
+ const T* const frame_end = interleaved + num_channels; |
+ |
+ Intermediate value = *interleaved++; |
+ while (interleaved < frame_end) { |
+ value += *interleaved++; |
+ } |
+ |
+ *deinterleaved++ = value / num_channels; |
+ } |
+} |
+ |
+template <typename T> |
+void DownmixInterleavedToMono(const T* interleaved, |
+ int num_multichannel_frames, |
+ int num_channels, |
+ T* deinterleaved); |
+ |
+template <> |
+void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, |
+ int num_multichannel_frames, |
+ int num_channels, |
+ int16_t* deinterleaved); |
+ |
} // namespace webrtc |
#endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ |