blob: 1ef108ea7720d320da4e751eb297cba6b846865e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 */
10
11#include "utility.h"
12
13#include "module.h"
14#include "trace.h"
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000015#include "signal_processing_library.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
17namespace webrtc
18{
19
20namespace voe
21{
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000022enum{kMaxTargetLen = 2*32*10}; // stereo 32KHz 10ms
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24void Utility::MixWithSat(WebRtc_Word16 target[],
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000025 int target_channel,
niklase@google.com470e71d2011-07-07 08:21:25 +000026 const WebRtc_Word16 source[],
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000027 int source_channel,
28 int source_len)
niklase@google.com470e71d2011-07-07 08:21:25 +000029{
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000030 assert((target_channel == 1) || (target_channel == 2));
31 assert((source_channel == 1) || (source_channel == 2));
32 assert(source_len <= kMaxTargetLen);
33
34 if ((target_channel == 2) && (source_channel == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +000035 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +000036 // 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);
44 }
45 }
46 else if ((target_channel == 1) && (source_channel == 2))
47 {
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);
53 }
54 }
55 else
56 {
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);
61 }
niklase@google.com470e71d2011-07-07 08:21:25 +000062 }
63}
64
65void Utility::MixSubtractWithSat(WebRtc_Word16 target[],
66 const WebRtc_Word16 source[],
67 WebRtc_UWord16 len)
68{
69 WebRtc_Word32 temp(0);
70 for (int i = 0; i < len; i++)
71 {
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;
79 }
80}
81
82void Utility::MixAndScaleWithSat(WebRtc_Word16 target[],
83 const WebRtc_Word16 source[], float scale,
84 WebRtc_UWord16 len)
85{
86 WebRtc_Word32 temp(0);
87 for (int i = 0; i < len; i++)
88 {
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;
96 }
97}
98
99void Utility::Scale(WebRtc_Word16 vector[], float scale, WebRtc_UWord16 len)
100{
101 for (int i = 0; i < len; i++)
102 {
103 vector[i] = (WebRtc_Word16) (scale * vector[i]);
104 }
105}
106
107void Utility::ScaleWithSat(WebRtc_Word16 vector[], float scale,
108 WebRtc_UWord16 len)
109{
110 WebRtc_Word32 temp(0);
111 for (int i = 0; i < len; i++)
112 {
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;
120 }
121}
122
niklase@google.com470e71d2011-07-07 08:21:25 +0000123} // namespace voe
124
125} // namespace webrtc