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.
15 #include "signal_processing_library.h"
22 enum{kMaxTargetLen
= 2*32*10}; // stereo 32KHz 10ms
24 void Utility::MixWithSat(WebRtc_Word16 target
[],
26 const WebRtc_Word16 source
[],
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
);
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
[],
69 WebRtc_Word32
temp(0);
70 for (int i
= 0; i
< len
; i
++)
72 temp
= target
[i
] - source
[i
];
75 else if (temp
< -32768)
78 target
[i
] = (WebRtc_Word16
) temp
;
82 void Utility::MixAndScaleWithSat(WebRtc_Word16 target
[],
83 const WebRtc_Word16 source
[], float scale
,
86 WebRtc_Word32
temp(0);
87 for (int i
= 0; i
< len
; i
++)
89 temp
= (WebRtc_Word32
) (target
[i
] + scale
* source
[i
]);
92 else if (temp
< -32768)
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
,
110 WebRtc_Word32
temp(0);
111 for (int i
= 0; i
< len
; i
++)
113 temp
= (WebRtc_Word32
) (scale
* vector
[i
]);
116 else if (temp
< -32768)
119 vector
[i
] = (WebRtc_Word16
) temp
;
125 } // namespace webrtc