1902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/***********************************************************************
2902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comCopyright (c) 2006-2011, Skype Limited. All rights reserved.
3902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comRedistribution and use in source and binary forms, with or without
4902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.commodification, are permitted provided that the following conditions
5902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comare met:
6902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com- Redistributions of source code must retain the above copyright notice,
7902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comthis list of conditions and the following disclaimer.
8902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com- Redistributions in binary form must reproduce the above copyright
9902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comnotice, this list of conditions and the following disclaimer in the
10902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comdocumentation and/or other materials provided with the distribution.
11902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com- Neither the name of Internet Society, IETF or IETF Trust, nor the
12902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comnames of specific contributors, may be used to endorse or promote
13902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comproducts derived from this software without specific prior written
14902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.compermission.
15902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168a1cdaece7e1d009befb84f21bb82370025bf4d6robertphillips@google.comAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1797cee9735350cb472249ce1a827ba1aa6b2a5f59chudy@google.comIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
1986681b37bd20204e47a492119b345c01d00bc939fmalita@google.comLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2180a4a60f96c33ccd850f9b0eb4b69ab08c198196chudy@google.comSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.comINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comPOSSIBILITY OF SUCH DAMAGE.
26902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com***********************************************************************/
27f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.com
28f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.com#ifdef HAVE_CONFIG_H
29f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.com#include "config.h"
30f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.com#endif
31f4741c1322944e194ca34a8f5cf8188fe2c0efe2robertphillips@google.com
32902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com#include "SigProc_FIX.h"
33902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com
34902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/* Apply sine window to signal vector.                                      */
35902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/* Window types:                                                            */
36902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/*    1 -> sine window from 0 to pi/2                                       */
37902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/*    2 -> sine window from pi/2 to pi                                      */
38902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/* Every other sample is linearly interpolated, for speed.                  */
39902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/* Window length must be between 16 and 120 (incl) and a multiple of 4.     */
40902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com
41902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com/* Matlab code for table:
42902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com   for k=16:9*4:16+2*9*4, fprintf(' %7.d,', -round(65536*pi ./ (k:4:k+8*4))); fprintf('\n'); end
43902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com*/
44902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comstatic const opus_int16 freq_table_Q16[ 27 ] = {
45902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com   12111,    9804,    8235,    7100,    6239,    5565,    5022,    4575,    4202,
46902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com    3885,    3612,    3375,    3167,    2984,    2820,    2674,    2542,    2422,
47902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com    2313,    2214,    2123,    2038,    1961,    1889,    1822,    1760,    1702,
48902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com};
49902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com
50902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.comvoid silk_apply_sine_window(
510b5bbb0f82e022c8acfbcb6312f0ed18e1ab90cechudy@google.com    opus_int16                  px_win[],           /* O    Pointer to windowed signal                                  */
520b5bbb0f82e022c8acfbcb6312f0ed18e1ab90cechudy@google.com    const opus_int16            px[],               /* I    Pointer to input signal                                     */
530b5bbb0f82e022c8acfbcb6312f0ed18e1ab90cechudy@google.com    const opus_int              win_type,           /* I    Selects a window type                                       */
54a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    const opus_int              length              /* I    Window length, multiple of 4                                */
55a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com)
56a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com{
57a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    opus_int   k, f_Q16, c_Q16;
58a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    opus_int32 S0_Q16, S1_Q16;
59a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com
60a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    silk_assert( win_type == 1 || win_type == 2 );
61a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com
62a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    /* Length must be in a range from 16 to 120 and a multiple of 4 */
63a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    silk_assert( length >= 16 && length <= 120 );
64a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    silk_assert( ( length & 3 ) == 0 );
65a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com
66a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    /* Frequency */
67a9e937c7b712b024de108fa963f92d0e70e4a296chudy@google.com    k = ( length >> 2 ) - 4;
680b5bbb0f82e022c8acfbcb6312f0ed18e1ab90cechudy@google.com    silk_assert( k >= 0 && k <= 26 );
690b5bbb0f82e022c8acfbcb6312f0ed18e1ab90cechudy@google.com    f_Q16 = (opus_int)freq_table_Q16[ k ];
70830b8793bb1646bb76817bdc228dd8e2a92bef7dchudy@google.com
71902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com    /* Factor used for cosine approximation */
72902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com    c_Q16 = silk_SMULWB( (opus_int32)f_Q16, -f_Q16 );
7350c84da68b17647371a81593402e897d639989e4robertphillips@google.com    silk_assert( c_Q16 >= -32768 );
7450c84da68b17647371a81593402e897d639989e4robertphillips@google.com
7550c84da68b17647371a81593402e897d639989e4robertphillips@google.com    /* initialize state */
7650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    if( win_type == 1 ) {
7750c84da68b17647371a81593402e897d639989e4robertphillips@google.com        /* start from 0 */
7850c84da68b17647371a81593402e897d639989e4robertphillips@google.com        S0_Q16 = 0;
79902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        /* approximation of sin(f) */
80902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        S1_Q16 = f_Q16 + silk_RSHIFT( length, 3 );
81902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com    } else {
82902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        /* start from 1 */
83902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        S0_Q16 = ( (opus_int32)1 << 16 );
84902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        /* approximation of cos(f) */
8550c84da68b17647371a81593402e897d639989e4robertphillips@google.com        S1_Q16 = ( (opus_int32)1 << 16 ) + silk_RSHIFT( c_Q16, 1 ) + silk_RSHIFT( length, 4 );
8650c84da68b17647371a81593402e897d639989e4robertphillips@google.com    }
8750c84da68b17647371a81593402e897d639989e4robertphillips@google.com
8850c84da68b17647371a81593402e897d639989e4robertphillips@google.com    /* Uses the recursive equation:   sin(n*f) = 2 * cos(f) * sin((n-1)*f) - sin((n-2)*f)    */
8950c84da68b17647371a81593402e897d639989e4robertphillips@google.com    /* 4 samples at a time */
9050c84da68b17647371a81593402e897d639989e4robertphillips@google.com    for( k = 0; k < length; k += 4 ) {
9150c84da68b17647371a81593402e897d639989e4robertphillips@google.com        px_win[ k ]     = (opus_int16)silk_SMULWB( silk_RSHIFT( S0_Q16 + S1_Q16, 1 ), px[ k ] );
92902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        px_win[ k + 1 ] = (opus_int16)silk_SMULWB( S1_Q16, px[ k + 1] );
93902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        S0_Q16 = silk_SMULWB( S1_Q16, c_Q16 ) + silk_LSHIFT( S1_Q16, 1 ) - S0_Q16 + 1;
94902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        S0_Q16 = silk_min( S0_Q16, ( (opus_int32)1 << 16 ) );
9597cee9735350cb472249ce1a827ba1aa6b2a5f59chudy@google.com
96902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        px_win[ k + 2 ] = (opus_int16)silk_SMULWB( silk_RSHIFT( S0_Q16 + S1_Q16, 1 ), px[ k + 2] );
97902ebe5eb41a350b766238b3b103c22fe9fc0fb5chudy@google.com        px_win[ k + 3 ] = (opus_int16)silk_SMULWB( S0_Q16, px[ k + 3 ] );
987e4cfbf144af7d530d552946cee2a21d30b9b50fchudy@google.com        S1_Q16 = silk_SMULWB( S0_Q16, c_Q16 ) + silk_LSHIFT( S0_Q16, 1 ) - S1_Q16;
997e4cfbf144af7d530d552946cee2a21d30b9b50fchudy@google.com        S1_Q16 = silk_min( S1_Q16, ( (opus_int32)1 << 16 ) );
1007e4cfbf144af7d530d552946cee2a21d30b9b50fchudy@google.com    }
1017e4cfbf144af7d530d552946cee2a21d30b9b50fchudy@google.com}
1027e4cfbf144af7d530d552946cee2a21d30b9b50fchudy@google.com