Bug 797671: Import Webrtc.org code from stable branch 3.12 (rev 2820) rs=jesup
[gecko.git] / media / webrtc / trunk / src / voice_engine / utility.cc
blob1ef108ea7720d320da4e751eb297cba6b846865e
1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
11 #include "utility.h"
13 #include "module.h"
14 #include "trace.h"
15 #include "signal_processing_library.h"
17 namespace webrtc
20 namespace voe
22 enum{kMaxTargetLen = 2*32*10}; // stereo 32KHz 10ms
24 void Utility::MixWithSat(WebRtc_Word16 target[],
25 int target_channel,
26 const WebRtc_Word16 source[],
27 int source_channel,
28 int source_len)
30 assert((target_channel == 1) || (target_channel == 2));
31 assert((source_channel == 1) || (source_channel == 2));
32 assert(source_len <= kMaxTargetLen);
34 if ((target_channel == 2) && (source_channel == 1))
36 // Convert source from mono to stereo.
37 WebRtc_Word32 left = 0;
38 WebRtc_Word32 right = 0;
39 for (int i = 0; i < source_len; ++i) {
40 left = source[i] + target[i*2];
41 right = source[i] + target[i*2 + 1];
42 target[i*2] = WebRtcSpl_SatW32ToW16(left);
43 target[i*2 + 1] = WebRtcSpl_SatW32ToW16(right);
46 else if ((target_channel == 1) && (source_channel == 2))
48 // Convert source from stereo to mono.
49 WebRtc_Word32 temp = 0;
50 for (int i = 0; i < source_len/2; ++i) {
51 temp = ((source[i*2] + source[i*2 + 1])>>1) + target[i];
52 target[i] = WebRtcSpl_SatW32ToW16(temp);
55 else
57 WebRtc_Word32 temp = 0;
58 for (int i = 0; i < source_len; ++i) {
59 temp = source[i] + target[i];
60 target[i] = WebRtcSpl_SatW32ToW16(temp);
65 void Utility::MixSubtractWithSat(WebRtc_Word16 target[],
66 const WebRtc_Word16 source[],
67 WebRtc_UWord16 len)
69 WebRtc_Word32 temp(0);
70 for (int i = 0; i < len; i++)
72 temp = target[i] - source[i];
73 if (temp > 32767)
74 target[i] = 32767;
75 else if (temp < -32768)
76 target[i] = -32768;
77 else
78 target[i] = (WebRtc_Word16) temp;
82 void Utility::MixAndScaleWithSat(WebRtc_Word16 target[],
83 const WebRtc_Word16 source[], float scale,
84 WebRtc_UWord16 len)
86 WebRtc_Word32 temp(0);
87 for (int i = 0; i < len; i++)
89 temp = (WebRtc_Word32) (target[i] + scale * source[i]);
90 if (temp > 32767)
91 target[i] = 32767;
92 else if (temp < -32768)
93 target[i] = -32768;
94 else
95 target[i] = (WebRtc_Word16) temp;
99 void Utility::Scale(WebRtc_Word16 vector[], float scale, WebRtc_UWord16 len)
101 for (int i = 0; i < len; i++)
103 vector[i] = (WebRtc_Word16) (scale * vector[i]);
107 void Utility::ScaleWithSat(WebRtc_Word16 vector[], float scale,
108 WebRtc_UWord16 len)
110 WebRtc_Word32 temp(0);
111 for (int i = 0; i < len; i++)
113 temp = (WebRtc_Word32) (scale * vector[i]);
114 if (temp > 32767)
115 vector[i] = 32767;
116 else if (temp < -32768)
117 vector[i] = -32768;
118 else
119 vector[i] = (WebRtc_Word16) temp;
123 } // namespace voe
125 } // namespace webrtc