Recognizes if input is ogg or not.
[xiph.git] / postfish / window.c
blob562ea00f007c4a6b918a647bfd1c4669f94d6ec5
1 /*
3 * postfish
4 *
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)
10 * any later version.
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.
24 #include "postfish.h"
25 #include "window.h"
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),
34 1==sin(x)^2,
35 2==sin(sin(x)^2),
36 3==sin(sin(x)^2)^2 */
37 /* returns quarter-cycle (left half) n+1 samples of a window from 0. to 1. */
38 static int ilog(long x){
39 int ret=-1;
41 while(x>0){
42 x>>=1;
43 ret++;
45 return ret;
48 float *window_get(int type,int n){
49 int bits=ilog(n),i;
50 if((1<<bits)!=n)return 0;
52 if(bits<0)return 0;
53 if(bits>=17)return 0;
55 if(type<0)return 0;
56 if(type>3)return 0;
58 pthread_mutex_lock(&window_mutex);
59 if(!window_func)
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);
68 if(type>0)
69 for(i=0;i<n+1;i++)window_func[type][bits][i]*=
70 window_func[type][bits][i];
71 if(type>1)
72 for(i=0;i<n+1;i++)window_func[type][bits][i]=
73 sin(window_func[type][bits][i]*M_PIl*.5);
74 if(type>2)
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;
85 int i;
87 *(data++) *= window[0]*scale;
88 for(i=1;i<halfn;i++){
89 float val=window[i]*scale;
90 *(data++) *= val;
91 *(--data2)*= val;
93 *(data++) *= window[i]*scale;