hanvox 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Nathaniel Barnes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,12 @@
1
+ Hanvox is a library for processing audio files to search for certain signatures. It was originally part of AT-5001, but has been decoupled to allow for use in other projects.
2
+
3
+ WARNING: Hanvox only functions on UNIX based systems.
4
+
5
+ Installation:
6
+ 1. Install sox
7
+ 2. Install gnuplot
8
+ 3. gem install hanvox
9
+
10
+ Example Usage:
11
+ pa = Hanvox::ProcAudio.new "path_to_file"
12
+ pa.process
data/ROADMAP ADDED
File without changes
@@ -0,0 +1,150 @@
1
+ /*
2
+ Copyright (c) 2003-2004, Mark Borgerding
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+ * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13
+ */
14
+
15
+ /* kiss_fft.h
16
+ defines kiss_fft_scalar as either short or a float type
17
+ and defines
18
+ typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
19
+ #include "kiss_fft.h"
20
+ #include <limits.h>
21
+
22
+ #define MAXFACTORS 32
23
+ /* e.g. an fft of length 128 has 4 factors
24
+ as far as kissfft is concerned
25
+ 4*4*4*2
26
+ */
27
+
28
+ struct kiss_fft_state{
29
+ int nfft;
30
+ int inverse;
31
+ int factors[2*MAXFACTORS];
32
+ kiss_fft_cpx twiddles[1];
33
+ };
34
+
35
+ /*
36
+ Explanation of macros dealing with complex math:
37
+
38
+ C_MUL(m,a,b) : m = a*b
39
+ C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
40
+ C_SUB( res, a,b) : res = a - b
41
+ C_SUBFROM( res , a) : res -= a
42
+ C_ADDTO( res , a) : res += a
43
+ * */
44
+ #ifdef FIXED_POINT
45
+ #if (FIXED_POINT==32)
46
+ # define FRACBITS 31
47
+ # define SAMPPROD int64_t
48
+ #define SAMP_MAX 2147483647
49
+ #else
50
+ # define FRACBITS 15
51
+ # define SAMPPROD int32_t
52
+ #define SAMP_MAX 32767
53
+ #endif
54
+
55
+ #define SAMP_MIN -SAMP_MAX
56
+
57
+ #if defined(CHECK_OVERFLOW)
58
+ # define CHECK_OVERFLOW_OP(a,op,b) \
59
+ if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
60
+ fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
61
+ #endif
62
+
63
+
64
+ # define smul(a,b) ( (SAMPPROD)(a)*(b) )
65
+ # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
66
+
67
+ # define S_MUL(a,b) sround( smul(a,b) )
68
+
69
+ # define C_MUL(m,a,b) \
70
+ do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
71
+ (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
72
+
73
+ # define DIVSCALAR(x,k) \
74
+ (x) = sround( smul( x, SAMP_MAX/k ) )
75
+
76
+ # define C_FIXDIV(c,div) \
77
+ do { DIVSCALAR( (c).r , div); \
78
+ DIVSCALAR( (c).i , div); }while (0)
79
+
80
+ # define C_MULBYSCALAR( c, s ) \
81
+ do{ (c).r = sround( smul( (c).r , s ) ) ;\
82
+ (c).i = sround( smul( (c).i , s ) ) ; }while(0)
83
+
84
+ #else /* not FIXED_POINT*/
85
+
86
+ # define S_MUL(a,b) ( (a)*(b) )
87
+ #define C_MUL(m,a,b) \
88
+ do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
89
+ (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
90
+ # define C_FIXDIV(c,div) /* NOOP */
91
+ # define C_MULBYSCALAR( c, s ) \
92
+ do{ (c).r *= (s);\
93
+ (c).i *= (s); }while(0)
94
+ #endif
95
+
96
+ #ifndef CHECK_OVERFLOW_OP
97
+ # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
98
+ #endif
99
+
100
+ #define C_ADD( res, a,b)\
101
+ do { \
102
+ CHECK_OVERFLOW_OP((a).r,+,(b).r)\
103
+ CHECK_OVERFLOW_OP((a).i,+,(b).i)\
104
+ (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
105
+ }while(0)
106
+ #define C_SUB( res, a,b)\
107
+ do { \
108
+ CHECK_OVERFLOW_OP((a).r,-,(b).r)\
109
+ CHECK_OVERFLOW_OP((a).i,-,(b).i)\
110
+ (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
111
+ }while(0)
112
+ #define C_ADDTO( res , a)\
113
+ do { \
114
+ CHECK_OVERFLOW_OP((res).r,+,(a).r)\
115
+ CHECK_OVERFLOW_OP((res).i,+,(a).i)\
116
+ (res).r += (a).r; (res).i += (a).i;\
117
+ }while(0)
118
+
119
+ #define C_SUBFROM( res , a)\
120
+ do {\
121
+ CHECK_OVERFLOW_OP((res).r,-,(a).r)\
122
+ CHECK_OVERFLOW_OP((res).i,-,(a).i)\
123
+ (res).r -= (a).r; (res).i -= (a).i; \
124
+ }while(0)
125
+
126
+
127
+ #ifdef FIXED_POINT
128
+ # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
129
+ # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
130
+ # define HALF_OF(x) ((x)>>1)
131
+ #elif defined(USE_SIMD)
132
+ # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
133
+ # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
134
+ # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
135
+ #else
136
+ # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
137
+ # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
138
+ # define HALF_OF(x) ((x)*.5)
139
+ #endif
140
+
141
+ #define kf_cexp(x,phase) \
142
+ do{ \
143
+ (x)->r = KISS_FFT_COS(phase);\
144
+ (x)->i = KISS_FFT_SIN(phase);\
145
+ }while(0)
146
+
147
+
148
+ /* a debugging function */
149
+ #define pcpx(c)\
150
+ fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ if(have_library("m"))
4
+ create_makefile("kissfft/kissfft")
5
+ end
@@ -0,0 +1,427 @@
1
+ /*
2
+ Copyright (c) 2003-2004, Mark Borgerding
3
+
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+ * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13
+ */
14
+
15
+
16
+ #include "_kiss_fft_guts.h"
17
+ /* The guts header contains all the multiplication and addition macros that are defined for
18
+ fixed or floating point complex numbers. It also delares the kf_ internal functions.
19
+ */
20
+
21
+ static kiss_fft_cpx *scratchbuf=NULL;
22
+ static size_t nscratchbuf=0;
23
+ static kiss_fft_cpx *tmpbuf=NULL;
24
+ static size_t ntmpbuf=0;
25
+
26
+ #define CHECKBUF(buf,nbuf,n) \
27
+ do { \
28
+ if ( nbuf < (size_t)(n) ) {\
29
+ free(buf); \
30
+ buf = (kiss_fft_cpx*)KISS_FFT_MALLOC(sizeof(kiss_fft_cpx)*(n)); \
31
+ nbuf = (size_t)(n); \
32
+ } \
33
+ }while(0)
34
+
35
+
36
+ static void kf_bfly2(
37
+ kiss_fft_cpx * Fout,
38
+ const size_t fstride,
39
+ const kiss_fft_cfg st,
40
+ int m
41
+ )
42
+ {
43
+ kiss_fft_cpx * Fout2;
44
+ kiss_fft_cpx * tw1 = st->twiddles;
45
+ kiss_fft_cpx t;
46
+ Fout2 = Fout + m;
47
+ do{
48
+ C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
49
+
50
+ C_MUL (t, *Fout2 , *tw1);
51
+ tw1 += fstride;
52
+ C_SUB( *Fout2 , *Fout , t );
53
+ C_ADDTO( *Fout , t );
54
+ ++Fout2;
55
+ ++Fout;
56
+ }while (--m);
57
+ }
58
+
59
+ static void kf_bfly4(
60
+ kiss_fft_cpx * Fout,
61
+ const size_t fstride,
62
+ const kiss_fft_cfg st,
63
+ const size_t m
64
+ )
65
+ {
66
+ kiss_fft_cpx *tw1,*tw2,*tw3;
67
+ kiss_fft_cpx scratch[6];
68
+ size_t k=m;
69
+ const size_t m2=2*m;
70
+ const size_t m3=3*m;
71
+
72
+ tw3 = tw2 = tw1 = st->twiddles;
73
+
74
+ do {
75
+ C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
76
+
77
+ C_MUL(scratch[0],Fout[m] , *tw1 );
78
+ C_MUL(scratch[1],Fout[m2] , *tw2 );
79
+ C_MUL(scratch[2],Fout[m3] , *tw3 );
80
+
81
+ C_SUB( scratch[5] , *Fout, scratch[1] );
82
+ C_ADDTO(*Fout, scratch[1]);
83
+ C_ADD( scratch[3] , scratch[0] , scratch[2] );
84
+ C_SUB( scratch[4] , scratch[0] , scratch[2] );
85
+ C_SUB( Fout[m2], *Fout, scratch[3] );
86
+ tw1 += fstride;
87
+ tw2 += fstride*2;
88
+ tw3 += fstride*3;
89
+ C_ADDTO( *Fout , scratch[3] );
90
+
91
+ if(st->inverse) {
92
+ Fout[m].r = scratch[5].r - scratch[4].i;
93
+ Fout[m].i = scratch[5].i + scratch[4].r;
94
+ Fout[m3].r = scratch[5].r + scratch[4].i;
95
+ Fout[m3].i = scratch[5].i - scratch[4].r;
96
+ }else{
97
+ Fout[m].r = scratch[5].r + scratch[4].i;
98
+ Fout[m].i = scratch[5].i - scratch[4].r;
99
+ Fout[m3].r = scratch[5].r - scratch[4].i;
100
+ Fout[m3].i = scratch[5].i + scratch[4].r;
101
+ }
102
+ ++Fout;
103
+ }while(--k);
104
+ }
105
+
106
+ static void kf_bfly3(
107
+ kiss_fft_cpx * Fout,
108
+ const size_t fstride,
109
+ const kiss_fft_cfg st,
110
+ size_t m
111
+ )
112
+ {
113
+ size_t k=m;
114
+ const size_t m2 = 2*m;
115
+ kiss_fft_cpx *tw1,*tw2;
116
+ kiss_fft_cpx scratch[5];
117
+ kiss_fft_cpx epi3;
118
+ epi3 = st->twiddles[fstride*m];
119
+
120
+ tw1=tw2=st->twiddles;
121
+
122
+ do{
123
+ C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
124
+
125
+ C_MUL(scratch[1],Fout[m] , *tw1);
126
+ C_MUL(scratch[2],Fout[m2] , *tw2);
127
+
128
+ C_ADD(scratch[3],scratch[1],scratch[2]);
129
+ C_SUB(scratch[0],scratch[1],scratch[2]);
130
+ tw1 += fstride;
131
+ tw2 += fstride*2;
132
+
133
+ Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
134
+ Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
135
+
136
+ C_MULBYSCALAR( scratch[0] , epi3.i );
137
+
138
+ C_ADDTO(*Fout,scratch[3]);
139
+
140
+ Fout[m2].r = Fout[m].r + scratch[0].i;
141
+ Fout[m2].i = Fout[m].i - scratch[0].r;
142
+
143
+ Fout[m].r -= scratch[0].i;
144
+ Fout[m].i += scratch[0].r;
145
+
146
+ ++Fout;
147
+ }while(--k);
148
+ }
149
+
150
+ static void kf_bfly5(
151
+ kiss_fft_cpx * Fout,
152
+ const size_t fstride,
153
+ const kiss_fft_cfg st,
154
+ int m
155
+ )
156
+ {
157
+ kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
158
+ int u;
159
+ kiss_fft_cpx scratch[13];
160
+ kiss_fft_cpx * twiddles = st->twiddles;
161
+ kiss_fft_cpx *tw;
162
+ kiss_fft_cpx ya,yb;
163
+ ya = twiddles[fstride*m];
164
+ yb = twiddles[fstride*2*m];
165
+
166
+ Fout0=Fout;
167
+ Fout1=Fout0+m;
168
+ Fout2=Fout0+2*m;
169
+ Fout3=Fout0+3*m;
170
+ Fout4=Fout0+4*m;
171
+
172
+ tw=st->twiddles;
173
+ for ( u=0; u<m; ++u ) {
174
+ C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
175
+ scratch[0] = *Fout0;
176
+
177
+ C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
178
+ C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
179
+ C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
180
+ C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
181
+
182
+ C_ADD( scratch[7],scratch[1],scratch[4]);
183
+ C_SUB( scratch[10],scratch[1],scratch[4]);
184
+ C_ADD( scratch[8],scratch[2],scratch[3]);
185
+ C_SUB( scratch[9],scratch[2],scratch[3]);
186
+
187
+ Fout0->r += scratch[7].r + scratch[8].r;
188
+ Fout0->i += scratch[7].i + scratch[8].i;
189
+
190
+ scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
191
+ scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
192
+
193
+ scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
194
+ scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
195
+
196
+ C_SUB(*Fout1,scratch[5],scratch[6]);
197
+ C_ADD(*Fout4,scratch[5],scratch[6]);
198
+
199
+ scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
200
+ scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
201
+ scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
202
+ scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
203
+
204
+ C_ADD(*Fout2,scratch[11],scratch[12]);
205
+ C_SUB(*Fout3,scratch[11],scratch[12]);
206
+
207
+ ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
208
+ }
209
+ }
210
+
211
+ /* perform the butterfly for one stage of a mixed radix FFT */
212
+ static void kf_bfly_generic(
213
+ kiss_fft_cpx * Fout,
214
+ const size_t fstride,
215
+ const kiss_fft_cfg st,
216
+ int m,
217
+ int p
218
+ )
219
+ {
220
+ int u,k,q1,q;
221
+ kiss_fft_cpx * twiddles = st->twiddles;
222
+ kiss_fft_cpx t;
223
+ int Norig = st->nfft;
224
+
225
+ CHECKBUF(scratchbuf,nscratchbuf,p);
226
+
227
+ for ( u=0; u<m; ++u ) {
228
+ k=u;
229
+ for ( q1=0 ; q1<p ; ++q1 ) {
230
+ scratchbuf[q1] = Fout[ k ];
231
+ C_FIXDIV(scratchbuf[q1],p);
232
+ k += m;
233
+ }
234
+
235
+ k=u;
236
+ for ( q1=0 ; q1<p ; ++q1 ) {
237
+ int twidx=0;
238
+ Fout[ k ] = scratchbuf[0];
239
+ for (q=1;q<p;++q ) {
240
+ twidx += fstride * k;
241
+ if (twidx>=Norig) twidx-=Norig;
242
+ C_MUL(t,scratchbuf[q] , twiddles[twidx] );
243
+ C_ADDTO( Fout[ k ] ,t);
244
+ }
245
+ k += m;
246
+ }
247
+ }
248
+ }
249
+
250
+ static
251
+ void kf_work(
252
+ kiss_fft_cpx * Fout,
253
+ const kiss_fft_cpx * f,
254
+ const size_t fstride,
255
+ int in_stride,
256
+ int * factors,
257
+ const kiss_fft_cfg st
258
+ )
259
+ {
260
+ kiss_fft_cpx * Fout_beg=Fout;
261
+ const int p=*factors++; /* the radix */
262
+ const int m=*factors++; /* stage's fft length/p */
263
+ const kiss_fft_cpx * Fout_end = Fout + p*m;
264
+
265
+ #ifdef _OPENMP
266
+ // use openmp extensions at the
267
+ // top-level (not recursive)
268
+ if (fstride==1) {
269
+ int k;
270
+
271
+ // execute the p different work units in different threads
272
+ # pragma omp parallel for
273
+ for (k=0;k<p;++k)
274
+ kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
275
+ // all threads have joined by this point
276
+
277
+ switch (p) {
278
+ case 2: kf_bfly2(Fout,fstride,st,m); break;
279
+ case 3: kf_bfly3(Fout,fstride,st,m); break;
280
+ case 4: kf_bfly4(Fout,fstride,st,m); break;
281
+ case 5: kf_bfly5(Fout,fstride,st,m); break;
282
+ default: kf_bfly_generic(Fout,fstride,st,m,p); break;
283
+ }
284
+ return;
285
+ }
286
+ #endif
287
+
288
+ if (m==1) {
289
+ do{
290
+ *Fout = *f;
291
+ f += fstride*in_stride;
292
+ }while(++Fout != Fout_end );
293
+ }else{
294
+ do{
295
+ // recursive call:
296
+ // DFT of size m*p performed by doing
297
+ // p instances of smaller DFTs of size m,
298
+ // each one takes a decimated version of the input
299
+ kf_work( Fout , f, fstride*p, in_stride, factors,st);
300
+ f += fstride*in_stride;
301
+ }while( (Fout += m) != Fout_end );
302
+ }
303
+
304
+ Fout=Fout_beg;
305
+
306
+ // recombine the p smaller DFTs
307
+ switch (p) {
308
+ case 2: kf_bfly2(Fout,fstride,st,m); break;
309
+ case 3: kf_bfly3(Fout,fstride,st,m); break;
310
+ case 4: kf_bfly4(Fout,fstride,st,m); break;
311
+ case 5: kf_bfly5(Fout,fstride,st,m); break;
312
+ default: kf_bfly_generic(Fout,fstride,st,m,p); break;
313
+ }
314
+ }
315
+
316
+ /* facbuf is populated by p1,m1,p2,m2, ...
317
+ where
318
+ p[i] * m[i] = m[i-1]
319
+ m0 = n */
320
+ static
321
+ void kf_factor(int n,int * facbuf)
322
+ {
323
+ int p=4;
324
+ double floor_sqrt;
325
+ floor_sqrt = floor( sqrt((double)n) );
326
+
327
+ /*factor out powers of 4, powers of 2, then any remaining primes */
328
+ do {
329
+ while (n % p) {
330
+ switch (p) {
331
+ case 4: p = 2; break;
332
+ case 2: p = 3; break;
333
+ default: p += 2; break;
334
+ }
335
+ if (p > floor_sqrt)
336
+ p = n; /* no more factors, skip to end */
337
+ }
338
+ n /= p;
339
+ *facbuf++ = p;
340
+ *facbuf++ = n;
341
+ } while (n > 1);
342
+ }
343
+
344
+ /*
345
+ *
346
+ * User-callable function to allocate all necessary storage space for the fft.
347
+ *
348
+ * The return value is a contiguous block of memory, allocated with malloc. As such,
349
+ * It can be freed with free(), rather than a kiss_fft-specific function.
350
+ * */
351
+ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
352
+ {
353
+ kiss_fft_cfg st=NULL;
354
+ size_t memneeded = sizeof(struct kiss_fft_state)
355
+ + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
356
+
357
+ if ( lenmem==NULL ) {
358
+ st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
359
+ }else{
360
+ if (mem != NULL && *lenmem >= memneeded)
361
+ st = (kiss_fft_cfg)mem;
362
+ *lenmem = memneeded;
363
+ }
364
+ if (st) {
365
+ int i;
366
+ st->nfft=nfft;
367
+ st->inverse = inverse_fft;
368
+
369
+ for (i=0;i<nfft;++i) {
370
+ const double pi=3.141592653589793238462643383279502884197169399375105820974944;
371
+ double phase = -2*pi*i / nfft;
372
+ if (st->inverse)
373
+ phase *= -1;
374
+ kf_cexp(st->twiddles+i, phase );
375
+ }
376
+
377
+ kf_factor(nfft,st->factors);
378
+ }
379
+ return st;
380
+ }
381
+
382
+
383
+
384
+
385
+ void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
386
+ {
387
+ if (fin == fout) {
388
+ CHECKBUF(tmpbuf,ntmpbuf,st->nfft);
389
+ kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
390
+ memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
391
+ }else{
392
+ kf_work( fout, fin, 1,in_stride, st->factors,st );
393
+ }
394
+ }
395
+
396
+ void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
397
+ {
398
+ kiss_fft_stride(cfg,fin,fout,1);
399
+ }
400
+
401
+
402
+ /* not really necessary to call, but if someone is doing in-place ffts, they may want to free the
403
+ buffers from CHECKBUF
404
+ */
405
+ void kiss_fft_cleanup(void)
406
+ {
407
+ free(scratchbuf);
408
+ scratchbuf = NULL;
409
+ nscratchbuf=0;
410
+ free(tmpbuf);
411
+ tmpbuf=NULL;
412
+ ntmpbuf=0;
413
+ }
414
+
415
+ int kiss_fft_next_fast_size(int n)
416
+ {
417
+ while(1) {
418
+ int m=n;
419
+ while ( (m%2) == 0 ) m/=2;
420
+ while ( (m%3) == 0 ) m/=3;
421
+ while ( (m%5) == 0 ) m/=5;
422
+ if (m<=1)
423
+ break; /* n is completely factorable by twos, threes, and fives */
424
+ n++;
425
+ }
426
+ return n;
427
+ }