OLD | NEW |
1 /* libFLAC - Free Lossless Audio Codec library | 1 /* libFLAC - Free Lossless Audio Codec library |
2 * Copyright (C) 2006,2007 Josh Coalson | 2 * Copyright (C) 2006-2009 Josh Coalson |
| 3 * Copyright (C) 2011-2014 Xiph.Org Foundation |
3 * | 4 * |
4 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
6 * are met: | 7 * are met: |
7 * | 8 * |
8 * - Redistributions of source code must retain the above copyright | 9 * - Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
10 * | 11 * |
11 * - Redistributions in binary form must reproduce the above copyright | 12 * - Redistributions in binary form must reproduce the above copyright |
12 * notice, this list of conditions and the following disclaimer in the | 13 * notice, this list of conditions and the following disclaimer in the |
13 * documentation and/or other materials provided with the distribution. | 14 * documentation and/or other materials provided with the distribution. |
14 * | 15 * |
15 * - Neither the name of the Xiph.org Foundation nor the names of its | 16 * - Neither the name of the Xiph.org Foundation nor the names of its |
16 * contributors may be used to endorse or promote products derived from | 17 * contributors may be used to endorse or promote products derived from |
17 * this software without specific prior written permission. | 18 * this software without specific prior written permission. |
18 * | 19 * |
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR | 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 */ | 31 */ |
31 | 32 |
32 #if HAVE_CONFIG_H | 33 #ifdef HAVE_CONFIG_H |
33 # include <config.h> | 34 # include <config.h> |
34 #endif | 35 #endif |
35 | 36 |
36 #include <math.h> | 37 #include <math.h> |
| 38 #include "share/compat.h" |
37 #include "FLAC/assert.h" | 39 #include "FLAC/assert.h" |
38 #include "FLAC/format.h" | 40 #include "FLAC/format.h" |
39 #include "private/window.h" | 41 #include "private/window.h" |
40 | 42 |
41 #ifndef FLAC__INTEGER_ONLY_LIBRARY | 43 #ifndef FLAC__INTEGER_ONLY_LIBRARY |
42 | 44 |
43 #ifndef M_PI | |
44 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */ | |
45 #define M_PI 3.14159265358979323846 | |
46 #endif | |
47 | |
48 | 45 |
49 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L) | 46 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L) |
50 { | 47 { |
51 const FLAC__int32 N = L - 1; | 48 const FLAC__int32 N = L - 1; |
52 FLAC__int32 n; | 49 FLAC__int32 n; |
53 | 50 |
54 if (L & 1) { | 51 if (L & 1) { |
55 for (n = 0; n <= N/2; n++) | 52 for (n = 0; n <= N/2; n++) |
56 window[n] = 2.0f * n / (float)N; | 53 window[n] = 2.0f * n / (float)N; |
57 for (; n <= N; n++) | 54 for (; n <= N; n++) |
58 window[n] = 2.0f - 2.0f * n / (float)N; | 55 window[n] = 2.0f - 2.0f * n / (float)N; |
59 } | 56 } |
60 else { | 57 else { |
61 for (n = 0; n <= L/2-1; n++) | 58 for (n = 0; n <= L/2-1; n++) |
62 window[n] = 2.0f * n / (float)N; | 59 window[n] = 2.0f * n / (float)N; |
63 for (; n <= N; n++) | 60 for (; n <= N; n++) |
64 » » » window[n] = 2.0f - 2.0f * (N-n) / (float)N; | 61 » » » window[n] = 2.0f - 2.0f * n / (float)N; |
65 } | 62 } |
66 } | 63 } |
67 | 64 |
68 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L) | 65 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L) |
69 { | 66 { |
70 const FLAC__int32 N = L - 1; | 67 const FLAC__int32 N = L - 1; |
71 FLAC__int32 n; | 68 FLAC__int32 n; |
72 | 69 |
73 for (n = 0; n < L; n++) | 70 for (n = 0; n < L; n++) |
74 » » window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N+
0.5f) + 0.38f * cos(2.0f * M_PI * ((float)n/(float)N+0.5f))); | 71 » » window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N-
0.5f) - 0.38f * cos(2.0f * M_PI * ((float)n/(float)N))); |
75 } | 72 } |
76 | 73 |
77 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L) | 74 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L) |
78 { | 75 { |
79 const FLAC__int32 N = L - 1; | 76 const FLAC__int32 N = L - 1; |
80 FLAC__int32 n; | 77 FLAC__int32 n; |
81 | 78 |
82 for (n = 0; n < L; n++) | 79 for (n = 0; n < L; n++) |
83 window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N)
+ 0.08f * cos(4.0f * M_PI * n / N)); | 80 window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N)
+ 0.08f * cos(4.0f * M_PI * n / N)); |
84 } | 81 } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 166 |
170 for (n = 0; n < L; n++) | 167 for (n = 0; n < L; n++) |
171 window[n] = 1.0f; | 168 window[n] = 1.0f; |
172 } | 169 } |
173 | 170 |
174 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L) | 171 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L) |
175 { | 172 { |
176 FLAC__int32 n; | 173 FLAC__int32 n; |
177 | 174 |
178 if (L & 1) { | 175 if (L & 1) { |
179 » » for (n = 1; n <= L+1/2; n++) | 176 » » for (n = 1; n <= (L+1)/2; n++) |
180 window[n-1] = 2.0f * n / ((float)L + 1.0f); | 177 window[n-1] = 2.0f * n / ((float)L + 1.0f); |
181 for (; n <= L; n++) | 178 for (; n <= L; n++) |
182 » » » window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1
.0f); | 179 » » » window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0
f); |
183 } | 180 } |
184 else { | 181 else { |
185 for (n = 1; n <= L/2; n++) | 182 for (n = 1; n <= L/2; n++) |
186 » » » window[n-1] = 2.0f * n / (float)L; | 183 » » » window[n-1] = 2.0f * n / ((float)L + 1.0f); |
187 for (; n <= L; n++) | 184 for (; n <= L; n++) |
188 » » » window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L; | 185 » » » window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0
f); |
189 } | 186 } |
190 } | 187 } |
191 | 188 |
192 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__rea
l p) | 189 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__rea
l p) |
193 { | 190 { |
194 if (p <= 0.0) | 191 if (p <= 0.0) |
195 FLAC__window_rectangle(window, L); | 192 FLAC__window_rectangle(window, L); |
196 else if (p >= 1.0) | 193 else if (p >= 1.0) |
197 FLAC__window_hann(window, L); | 194 FLAC__window_hann(window, L); |
198 else { | 195 else { |
199 const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1; | 196 const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1; |
200 FLAC__int32 n; | 197 FLAC__int32 n; |
201 /* start with rectangle... */ | 198 /* start with rectangle... */ |
202 FLAC__window_rectangle(window, L); | 199 FLAC__window_rectangle(window, L); |
203 /* ...replace ends with hann */ | 200 /* ...replace ends with hann */ |
204 if (Np > 0) { | 201 if (Np > 0) { |
205 for (n = 0; n <= Np; n++) { | 202 for (n = 0; n <= Np; n++) { |
206 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI
* n / Np)); | 203 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI
* n / Np)); |
207 window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * co
s(M_PI * (n+Np) / Np)); | 204 window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * co
s(M_PI * (n+Np) / Np)); |
208 } | 205 } |
209 } | 206 } |
210 } | 207 } |
211 } | 208 } |
212 | 209 |
| 210 void FLAC__window_partial_tukey(FLAC__real *window, const FLAC__int32 L, const F
LAC__real p, const FLAC__real start, const FLAC__real end) |
| 211 { |
| 212 const FLAC__int32 start_n = (FLAC__int32)(start * L); |
| 213 const FLAC__int32 end_n = (FLAC__int32)(end * L); |
| 214 const FLAC__int32 N = end_n - start_n; |
| 215 FLAC__int32 Np, n, i; |
| 216 |
| 217 if (p <= 0.0f) |
| 218 FLAC__window_partial_tukey(window, L, 0.05f, start, end); |
| 219 else if (p >= 1.0f) |
| 220 FLAC__window_partial_tukey(window, L, 0.95f, start, end); |
| 221 else { |
| 222 |
| 223 Np = (FLAC__int32)(p / 2.0f * N); |
| 224 |
| 225 for (n = 0; n < start_n && n < L; n++) |
| 226 window[n] = 0.0f; |
| 227 for (i = 1; n < (start_n+Np) && n < L; n++, i++) |
| 228 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Np
)); |
| 229 for (; n < (end_n-Np) && n < L; n++) |
| 230 window[n] = 1.0f; |
| 231 for (i = Np; n < end_n && n < L; n++, i--) |
| 232 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Np
)); |
| 233 for (; n < L; n++) |
| 234 window[n] = 0.0f; |
| 235 } |
| 236 } |
| 237 |
| 238 void FLAC__window_punchout_tukey(FLAC__real *window, const FLAC__int32 L, const
FLAC__real p, const FLAC__real start, const FLAC__real end) |
| 239 { |
| 240 const FLAC__int32 start_n = (FLAC__int32)(start * L); |
| 241 const FLAC__int32 end_n = (FLAC__int32)(end * L); |
| 242 FLAC__int32 Ns, Ne, n, i; |
| 243 |
| 244 if (p <= 0.0f) |
| 245 FLAC__window_punchout_tukey(window, L, 0.05f, start, end); |
| 246 else if (p >= 1.0f) |
| 247 FLAC__window_punchout_tukey(window, L, 0.95f, start, end); |
| 248 else { |
| 249 |
| 250 Ns = (FLAC__int32)(p / 2.0f * start_n); |
| 251 Ne = (FLAC__int32)(p / 2.0f * (L - end_n)); |
| 252 |
| 253 for (n = 0, i = 1; n < Ns && n < L; n++, i++) |
| 254 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ns
)); |
| 255 for (; n < start_n-Ns && n < L; n++) |
| 256 window[n] = 1.0f; |
| 257 for (i = Ns; n < start_n && n < L; n++, i--) |
| 258 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ns
)); |
| 259 for (; n < end_n && n < L; n++) |
| 260 window[n] = 0.0f; |
| 261 for (i = 1; n < end_n+Ne && n < L; n++, i++) |
| 262 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ne
)); |
| 263 for (; n < L - (Ne) && n < L; n++) |
| 264 window[n] = 1.0f; |
| 265 for (i = Ne; n < L; n++, i--) |
| 266 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ne
)); |
| 267 } |
| 268 } |
| 269 |
213 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L) | 270 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L) |
214 { | 271 { |
215 const FLAC__int32 N = L - 1; | 272 const FLAC__int32 N = L - 1; |
216 const double N2 = (double)N / 2.; | 273 const double N2 = (double)N / 2.; |
217 FLAC__int32 n; | 274 FLAC__int32 n; |
218 | 275 |
219 for (n = 0; n <= N; n++) { | 276 for (n = 0; n <= N; n++) { |
220 const double k = ((double)n - N2) / N2; | 277 const double k = ((double)n - N2) / N2; |
221 window[n] = (FLAC__real)(1.0f - k * k); | 278 window[n] = (FLAC__real)(1.0f - k * k); |
222 } | 279 } |
223 } | 280 } |
224 | 281 |
225 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ | 282 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
OLD | NEW |