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

Side by Side Diff: webrtc/audio/utility/audio_frame_operations.cc

Issue 2712743004: Support 4 channel mic in Windows Core Audio (Closed)
Patch Set: Fix another non-Windows build error Created 3 years, 9 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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_); 92 MonoToStereo(data_copy, frame->samples_per_channel_, frame->data_);
93 frame->num_channels_ = 2; 93 frame->num_channels_ = 2;
94 94
95 return 0; 95 return 0;
96 } 96 }
97 97
98 void AudioFrameOperations::StereoToMono(const int16_t* src_audio, 98 void AudioFrameOperations::StereoToMono(const int16_t* src_audio,
99 size_t samples_per_channel, 99 size_t samples_per_channel,
100 int16_t* dst_audio) { 100 int16_t* dst_audio) {
101 for (size_t i = 0; i < samples_per_channel; i++) { 101 for (size_t i = 0; i < samples_per_channel; i++) {
102 dst_audio[i] = (src_audio[2 * i] + src_audio[2 * i + 1]) >> 1; 102 dst_audio[i] =
103 (static_cast<int32_t>(src_audio[2 * i]) + src_audio[2 * i + 1]) >> 1;
103 } 104 }
104 } 105 }
105 106
106 int AudioFrameOperations::StereoToMono(AudioFrame* frame) { 107 int AudioFrameOperations::StereoToMono(AudioFrame* frame) {
107 if (frame->num_channels_ != 2) { 108 if (frame->num_channels_ != 2) {
108 return -1; 109 return -1;
109 } 110 }
110 111
112 RTC_DCHECK_LE(frame->samples_per_channel_ * 2,
113 AudioFrame::kMaxDataSizeSamples);
114
111 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_); 115 StereoToMono(frame->data_, frame->samples_per_channel_, frame->data_);
112 frame->num_channels_ = 1; 116 frame->num_channels_ = 1;
113 117
114 return 0; 118 return 0;
115 } 119 }
116 120
121 void AudioFrameOperations::QuadToStereo(const int16_t* src_audio,
122 size_t samples_per_channel,
123 int16_t* dst_audio) {
124 for (size_t i = 0; i < samples_per_channel; i++) {
125 dst_audio[i * 2] =
126 (static_cast<int32_t>(src_audio[4 * i]) + src_audio[4 * i + 1]) >> 1;
127 dst_audio[i * 2 + 1] =
128 (static_cast<int32_t>(src_audio[4 * i + 2]) + src_audio[4 * i + 3]) >>
129 1;
130 }
131 }
132
133 int AudioFrameOperations::QuadToStereo(AudioFrame* frame) {
134 if (frame->num_channels_ != 4) {
135 return -1;
136 }
137
138 RTC_DCHECK_LE(frame->samples_per_channel_ * 4,
139 AudioFrame::kMaxDataSizeSamples);
140
141 QuadToStereo(frame->data_, frame->samples_per_channel_, frame->data_);
142 frame->num_channels_ = 2;
143
144 return 0;
145 }
146
147 void AudioFrameOperations::QuadToMono(const int16_t* src_audio,
148 size_t samples_per_channel,
149 int16_t* dst_audio) {
150 for (size_t i = 0; i < samples_per_channel; i++) {
151 dst_audio[i] =
152 (static_cast<int32_t>(src_audio[4 * i]) + src_audio[4 * i + 1] +
153 src_audio[4 * i + 2] + src_audio[4 * i + 3]) >> 2;
154 }
155 }
156
157 int AudioFrameOperations::QuadToMono(AudioFrame* frame) {
158 if (frame->num_channels_ != 4) {
159 return -1;
160 }
161
162 RTC_DCHECK_LE(frame->samples_per_channel_ * 4,
163 AudioFrame::kMaxDataSizeSamples);
164
165 QuadToMono(frame->data_, frame->samples_per_channel_, frame->data_);
166 frame->num_channels_ = 1;
167
168 return 0;
169 }
170
171 void AudioFrameOperations::DownmixChannels(const int16_t* src_audio,
172 size_t src_channels,
173 size_t samples_per_channel,
174 size_t dst_channels,
175 int16_t* dst_audio) {
176 if (src_channels == 2 && dst_channels == 1) {
177 StereoToMono(src_audio, samples_per_channel, dst_audio);
178 return;
179 } else if (src_channels == 4 && dst_channels == 2) {
180 QuadToStereo(src_audio, samples_per_channel, dst_audio);
181 return;
182 } else if (src_channels == 4 && dst_channels == 1) {
183 QuadToMono(src_audio, samples_per_channel, dst_audio);
184 return;
185 }
186
187 RTC_NOTREACHED() << "src_channels: " << src_channels
188 << ", dst_channels: " << dst_channels;
189 }
190
191 int AudioFrameOperations::DownmixChannels(size_t dst_channels,
192 AudioFrame* frame) {
193 if (frame->num_channels_ == 2 && dst_channels == 1) {
194 return StereoToMono(frame);
195 } else if (frame->num_channels_ == 4 && dst_channels == 2) {
196 return QuadToStereo(frame);
197 } else if (frame->num_channels_ == 4 && dst_channels == 1) {
198 return QuadToMono(frame);
199 }
200
201 return -1;
202 }
203
117 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) { 204 void AudioFrameOperations::SwapStereoChannels(AudioFrame* frame) {
118 RTC_DCHECK(frame); 205 RTC_DCHECK(frame);
119 if (frame->num_channels_ != 2) { 206 if (frame->num_channels_ != 2) {
120 return; 207 return;
121 } 208 }
122 209
123 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { 210 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
124 int16_t temp_data = frame->data_[i]; 211 int16_t temp_data = frame->data_[i];
125 frame->data_[i] = frame->data_[i + 1]; 212 frame->data_[i] = frame->data_[i + 1];
126 frame->data_[i + 1] = temp_data; 213 frame->data_[i + 1] = temp_data;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 frame.data_[i] = -32768; 304 frame.data_[i] = -32768;
218 } else if (temp_data > 32767) { 305 } else if (temp_data > 32767) {
219 frame.data_[i] = 32767; 306 frame.data_[i] = 32767;
220 } else { 307 } else {
221 frame.data_[i] = static_cast<int16_t>(temp_data); 308 frame.data_[i] = static_cast<int16_t>(temp_data);
222 } 309 }
223 } 310 }
224 return 0; 311 return 0;
225 } 312 }
226 } // namespace webrtc 313 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698