5 * Copyright (C) 2002-2005 Monty and Xiph.Org
7 * Postfish is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * Postfish is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Postfish; see the file COPYING. If not, write to the
19 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 static pthread_mutex_t window_mutex
=PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
28 static float ***window_func
=0; /* sin(), sin()^2, sin(sin()^2),sin(sin^2)^2
30 1,2,4,8,16,32,64,128,256,512,1024,
31 2048,4096,8192,16384,32768,65536 */
33 /* type is one of 0==sin(x),
37 /* returns quarter-cycle (left half) n+1 samples of a window from 0. to 1. */
38 static int ilog(long x
){
48 float *window_get(int type
,int n
){
50 if((1<<bits
)!=n
)return 0;
58 pthread_mutex_lock(&window_mutex
);
60 window_func
=calloc(4,sizeof(*window_func
));
62 if(!window_func
[type
])
63 window_func
[type
]=calloc(17,sizeof(**window_func
));
65 if(!window_func
[type
][bits
]){
66 window_func
[type
][bits
]=malloc((n
+1)*sizeof(***window_func
));
67 for(i
=0;i
<n
+1;i
++)window_func
[type
][bits
][i
]=sin(M_PIl
*.5*i
/n
);
69 for(i
=0;i
<n
+1;i
++)window_func
[type
][bits
][i
]*=
70 window_func
[type
][bits
][i
];
72 for(i
=0;i
<n
+1;i
++)window_func
[type
][bits
][i
]=
73 sin(window_func
[type
][bits
][i
]*M_PIl
*.5);
75 for(i
=0;i
<n
+1;i
++)window_func
[type
][bits
][i
]*=
76 window_func
[type
][bits
][i
];
79 pthread_mutex_unlock(&window_mutex
);
80 return window_func
[type
][bits
];
83 void window_apply(float *data
, float *window
, float scale
, int halfn
){
84 float *data2
=data
+halfn
*2;
87 *(data
++) *= window
[0]*scale
;
89 float val
=window
[i
]*scale
;
93 *(data
++) *= window
[i
]*scale
;