Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(750)

Side by Side Diff: webrtc/common_audio/include/audio_util.h

Issue 1226093007: Allow more than 2 input channels in AudioProcessing. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix docs Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/common_audio/audio_util_unittest.cc ('k') | webrtc/common_audio/wav_file.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ 11 #ifndef WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
12 #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ 12 #define WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
13 13
14 #include <limits> 14 #include <limits>
15 #include <cstring>
15 16
17 #include "webrtc/base/checks.h"
16 #include "webrtc/base/scoped_ptr.h" 18 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/typedefs.h" 19 #include "webrtc/typedefs.h"
18 20
19 namespace webrtc { 21 namespace webrtc {
20 22
21 typedef std::numeric_limits<int16_t> limits_int16; 23 typedef std::numeric_limits<int16_t> limits_int16;
22 24
23 // The conversion functions use the following naming convention: 25 // The conversion functions use the following naming convention:
24 // S16: int16_t [-32768, 32767] 26 // S16: int16_t [-32768, 32767]
25 // Float: float [-1.0, 1.0] 27 // Float: float [-1.0, 1.0]
26 // FloatS16: float [-32768.0, 32767.0] 28 // FloatS16: float [-32768.0, 32767.0]
27 static inline int16_t FloatToS16(float v) { 29 static inline int16_t FloatToS16(float v) {
28 if (v > 0) 30 if (v > 0)
29 return v >= 1 ? limits_int16::max() : 31 return v >= 1 ? limits_int16::max()
30 static_cast<int16_t>(v * limits_int16::max() + 0.5f); 32 : static_cast<int16_t>(v * limits_int16::max() + 0.5f);
31 return v <= -1 ? limits_int16::min() : 33 return v <= -1 ? limits_int16::min()
32 static_cast<int16_t>(-v * limits_int16::min() - 0.5f); 34 : static_cast<int16_t>(-v * limits_int16::min() - 0.5f);
33 } 35 }
34 36
35 static inline float S16ToFloat(int16_t v) { 37 static inline float S16ToFloat(int16_t v) {
36 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); 38 static const float kMaxInt16Inverse = 1.f / limits_int16::max();
37 static const float kMinInt16Inverse = 1.f / limits_int16::min(); 39 static const float kMinInt16Inverse = 1.f / limits_int16::min();
38 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); 40 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
39 } 41 }
40 42
41 static inline int16_t FloatS16ToS16(float v) { 43 static inline int16_t FloatS16ToS16(float v) {
42 static const float kMaxRound = limits_int16::max() - 0.5f; 44 static const float kMaxRound = limits_int16::max() - 0.5f;
43 static const float kMinRound = limits_int16::min() + 0.5f; 45 static const float kMinRound = limits_int16::min() + 0.5f;
44 if (v > 0) 46 if (v > 0)
45 return v >= kMaxRound ? limits_int16::max() : 47 return v >= kMaxRound ? limits_int16::max()
46 static_cast<int16_t>(v + 0.5f); 48 : static_cast<int16_t>(v + 0.5f);
47 return v <= kMinRound ? limits_int16::min() : 49 return v <= kMinRound ? limits_int16::min() : static_cast<int16_t>(v - 0.5f);
48 static_cast<int16_t>(v - 0.5f);
49 } 50 }
50 51
51 static inline float FloatToFloatS16(float v) { 52 static inline float FloatToFloatS16(float v) {
52 return v * (v > 0 ? limits_int16::max() : -limits_int16::min()); 53 return v * (v > 0 ? limits_int16::max() : -limits_int16::min());
53 } 54 }
54 55
55 static inline float FloatS16ToFloat(float v) { 56 static inline float FloatS16ToFloat(float v) {
56 static const float kMaxInt16Inverse = 1.f / limits_int16::max(); 57 static const float kMaxInt16Inverse = 1.f / limits_int16::max();
57 static const float kMinInt16Inverse = 1.f / limits_int16::min(); 58 static const float kMinInt16Inverse = 1.f / limits_int16::min();
58 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse); 59 return v * (v > 0 ? kMaxInt16Inverse : -kMinInt16Inverse);
59 } 60 }
60 61
61 void FloatToS16(const float* src, size_t size, int16_t* dest); 62 void FloatToS16(const float* src, size_t size, int16_t* dest);
62 void S16ToFloat(const int16_t* src, size_t size, float* dest); 63 void S16ToFloat(const int16_t* src, size_t size, float* dest);
63 void FloatS16ToS16(const float* src, size_t size, int16_t* dest); 64 void FloatS16ToS16(const float* src, size_t size, int16_t* dest);
64 void FloatToFloatS16(const float* src, size_t size, float* dest); 65 void FloatToFloatS16(const float* src, size_t size, float* dest);
65 void FloatS16ToFloat(const float* src, size_t size, float* dest); 66 void FloatS16ToFloat(const float* src, size_t size, float* dest);
66 67
67 // Deinterleave audio from |interleaved| to the channel buffers pointed to 68 // Deinterleave audio from |interleaved| to the channel buffers pointed to
68 // by |deinterleaved|. There must be sufficient space allocated in the 69 // by |deinterleaved|. There must be sufficient space allocated in the
69 // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel| 70 // |deinterleaved| buffers (|num_channel| buffers with |samples_per_channel|
70 // per buffer). 71 // per buffer).
71 template <typename T> 72 template <typename T>
72 void Deinterleave(const T* interleaved, int samples_per_channel, 73 void Deinterleave(const T* interleaved,
73 int num_channels, T* const* deinterleaved) { 74 int samples_per_channel,
75 int num_channels,
76 T* const* deinterleaved) {
74 for (int i = 0; i < num_channels; ++i) { 77 for (int i = 0; i < num_channels; ++i) {
75 T* channel = deinterleaved[i]; 78 T* channel = deinterleaved[i];
76 int interleaved_idx = i; 79 int interleaved_idx = i;
77 for (int j = 0; j < samples_per_channel; ++j) { 80 for (int j = 0; j < samples_per_channel; ++j) {
78 channel[j] = interleaved[interleaved_idx]; 81 channel[j] = interleaved[interleaved_idx];
79 interleaved_idx += num_channels; 82 interleaved_idx += num_channels;
80 } 83 }
81 } 84 }
82 } 85 }
83 86
84 // Interleave audio from the channel buffers pointed to by |deinterleaved| to 87 // Interleave audio from the channel buffers pointed to by |deinterleaved| to
85 // |interleaved|. There must be sufficient space allocated in |interleaved| 88 // |interleaved|. There must be sufficient space allocated in |interleaved|
86 // (|samples_per_channel| * |num_channels|). 89 // (|samples_per_channel| * |num_channels|).
87 template <typename T> 90 template <typename T>
88 void Interleave(const T* const* deinterleaved, int samples_per_channel, 91 void Interleave(const T* const* deinterleaved,
89 int num_channels, T* interleaved) { 92 int samples_per_channel,
93 int num_channels,
94 T* interleaved) {
90 for (int i = 0; i < num_channels; ++i) { 95 for (int i = 0; i < num_channels; ++i) {
91 const T* channel = deinterleaved[i]; 96 const T* channel = deinterleaved[i];
92 int interleaved_idx = i; 97 int interleaved_idx = i;
93 for (int j = 0; j < samples_per_channel; ++j) { 98 for (int j = 0; j < samples_per_channel; ++j) {
94 interleaved[interleaved_idx] = channel[j]; 99 interleaved[interleaved_idx] = channel[j];
95 interleaved_idx += num_channels; 100 interleaved_idx += num_channels;
96 } 101 }
97 } 102 }
98 } 103 }
99 104
105 template <typename T, typename Intermediate>
106 void DownmixToMono(const T* const* input_channels,
107 int num_frames,
108 int num_channels,
109 T* out) {
110 for (int i = 0; i < num_frames; ++i) {
111 Intermediate value = input_channels[0][i];
112 for (int j = 1; j < num_channels; ++j) {
113 value += input_channels[j][i];
114 }
115 out[i] = value / num_channels;
116 }
117 }
118
119 // Downmixes an interleaved multichannel signal to a single channel by averaging
120 // all channels.
121 template <typename T, typename Intermediate>
122 void DownmixInterleavedToMonoImpl(const T* interleaved,
123 int num_frames,
124 int num_channels,
125 T* deinterleaved) {
126 DCHECK_GT(num_channels, 0);
127 DCHECK_GT(num_frames, 0);
128
129 const T* const end = interleaved + num_frames * num_channels;
130
131 while (interleaved < end) {
132 const T* const frame_end = interleaved + num_channels;
133
134 Intermediate value = *interleaved++;
135 while (interleaved < frame_end) {
136 value += *interleaved++;
137 }
138
139 *deinterleaved++ = value / num_channels;
140 }
141 }
142
143 template <typename T>
144 void DownmixInterleavedToMono(const T* interleaved,
145 int num_frames,
146 int num_channels,
147 T* deinterleaved);
148
149 template <>
150 void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved,
151 int num_frames,
152 int num_channels,
153 int16_t* deinterleaved);
154
100 } // namespace webrtc 155 } // namespace webrtc
101 156
102 #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ 157 #endif // WEBRTC_COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_
OLDNEW
« no previous file with comments | « webrtc/common_audio/audio_util_unittest.cc ('k') | webrtc/common_audio/wav_file.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698