Coverage Report

Created: 2025-07-18 06:29

/src/flac/src/libFLAC/window.c
Line
Count
Source (jump to first uncovered line)
1
/* libFLAC - Free Lossless Audio Codec library
2
 * Copyright (C) 2006-2009  Josh Coalson
3
 * Copyright (C) 2011-2025  Xiph.Org Foundation
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 * notice, this list of conditions and the following disclaimer.
11
 *
12
 * - Redistributions in binary form must reproduce the above copyright
13
 * notice, this list of conditions and the following disclaimer in the
14
 * documentation and/or other materials provided with the distribution.
15
 *
16
 * - Neither the name of the Xiph.org Foundation nor the names of its
17
 * contributors may be used to endorse or promote products derived from
18
 * this software without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
32
33
#ifdef HAVE_CONFIG_H
34
#  include <config.h>
35
#endif
36
37
#include <math.h>
38
#include "share/compat.h"
39
#include "FLAC/assert.h"
40
#include "FLAC/format.h"
41
#include "private/window.h"
42
43
#ifndef FLAC__INTEGER_ONLY_LIBRARY
44
45
#if defined(_MSC_VER)
46
// silence 25 MSVC warnings 'conversion from 'double' to 'float', possible loss of data'
47
#pragma warning ( disable : 4244 )
48
#endif
49
50
void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
51
0
{
52
0
  const FLAC__int32 N = L - 1;
53
0
  FLAC__int32 n;
54
55
0
  if (L & 1) {
56
0
    for (n = 0; n <= N/2; n++)
57
0
      window[n] = 2.0f * n / (float)N;
58
0
    for (; n <= N; n++)
59
0
      window[n] = 2.0f - 2.0f * n / (float)N;
60
0
  }
61
0
  else {
62
0
    for (n = 0; n <= L/2-1; n++)
63
0
      window[n] = 2.0f * n / (float)N;
64
0
    for (; n <= N; n++)
65
0
      window[n] = 2.0f - 2.0f * n / (float)N;
66
0
  }
67
0
}
68
69
void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
70
0
{
71
0
  const FLAC__int32 N = L - 1;
72
0
  FLAC__int32 n;
73
74
0
  for (n = 0; n < L; n++)
75
0
    window[n] = (FLAC__real)(0.62f - 0.48f * fabsf((float)n/(float)N-0.5f) - 0.38f * cosf(2.0f * M_PI * ((float)n/(float)N)));
76
0
}
77
78
void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
79
0
{
80
0
  const FLAC__int32 N = L - 1;
81
0
  FLAC__int32 n;
82
83
0
  for (n = 0; n < L; n++)
84
0
    window[n] = (FLAC__real)(0.42f - 0.5f * cosf(2.0f * M_PI * n / N) + 0.08f * cosf(4.0f * M_PI * n / N));
85
0
}
86
87
/* 4-term -92dB side-lobe */
88
void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
89
0
{
90
0
  const FLAC__int32 N = L - 1;
91
0
  FLAC__int32 n;
92
93
0
  for (n = 0; n <= N; n++)
94
0
    window[n] = (FLAC__real)(0.35875f - 0.48829f * cosf(2.0f * M_PI * n / N) + 0.14128f * cosf(4.0f * M_PI * n / N) - 0.01168f * cosf(6.0f * M_PI * n / N));
95
0
}
96
97
void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
98
0
{
99
0
  const FLAC__int32 N = L - 1;
100
0
  const double N2 = (double)N / 2.;
101
0
  FLAC__int32 n;
102
103
0
  for (n = 0; n <= N; n++) {
104
0
    double k = ((double)n - N2) / N2;
105
0
    k = 1.0f - k * k;
106
0
    window[n] = (FLAC__real)(k * k);
107
0
  }
108
0
}
109
110
void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
111
0
{
112
0
  const FLAC__int32 N = L - 1;
113
0
  FLAC__int32 n;
114
115
0
  for (n = 0; n < L; n++)
116
0
    window[n] = (FLAC__real)(0.21557895f - 0.41663158f * cosf(2.0f * M_PI * n / N) + 0.277263158f * cosf(4.0f * M_PI * n / N) - 0.083578947f * cosf(6.0f * M_PI * n / N) + 0.006947368f * cosf(8.0f * M_PI * n / N));
117
0
}
118
119
void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
120
0
{
121
0
  const FLAC__int32 N = L - 1;
122
0
  const double N2 = (double)N / 2.;
123
0
  FLAC__int32 n;
124
125
0
  if(!(stddev > 0.0f && stddev <= 0.5f))
126
    /* stddev is not between 0 and 0.5, might be NaN.
127
     * Default to 0.5 */
128
0
    FLAC__window_gauss(window, L, 0.25f);
129
0
  else {
130
0
    for (n = 0; n <= N; n++) {
131
0
      const double k = ((double)n - N2) / (stddev * N2);
132
0
      window[n] = (FLAC__real)exp(-0.5f * k * k);
133
0
    }
134
0
  }
135
0
}
136
137
void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
138
0
{
139
0
  const FLAC__int32 N = L - 1;
140
0
  FLAC__int32 n;
141
142
0
  for (n = 0; n < L; n++)
143
0
    window[n] = (FLAC__real)(0.54f - 0.46f * cosf(2.0f * M_PI * n / N));
144
0
}
145
146
void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
147
0
{
148
0
  const FLAC__int32 N = L - 1;
149
0
  FLAC__int32 n;
150
151
0
  for (n = 0; n < L; n++)
152
0
    window[n] = (FLAC__real)(0.5f - 0.5f * cosf(2.0f * M_PI * n / N));
153
0
}
154
155
void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
156
0
{
157
0
  const FLAC__int32 N = L - 1;
158
0
  FLAC__int32 n;
159
160
0
  for (n = 0; n < L; n++)
161
0
    window[n] = (FLAC__real)(0.402f - 0.498f * cosf(2.0f * M_PI * n / N) + 0.098f * cosf(4.0f * M_PI * n / N) - 0.001f * cosf(6.0f * M_PI * n / N));
162
0
}
163
164
void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
165
0
{
166
0
  const FLAC__int32 N = L - 1;
167
0
  FLAC__int32 n;
168
169
0
  for (n = 0; n < L; n++)
170
0
    window[n] = (FLAC__real)(0.3635819f - 0.4891775f*cosf(2.0f*M_PI*n/N) + 0.1365995f*cosf(4.0f*M_PI*n/N) - 0.0106411f*cosf(6.0f*M_PI*n/N));
171
0
}
172
173
void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
174
8.20k
{
175
8.20k
  FLAC__int32 n;
176
177
78.4M
  for (n = 0; n < L; n++)
178
78.4M
    window[n] = 1.0f;
179
8.20k
}
180
181
void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
182
0
{
183
0
  FLAC__int32 n;
184
185
0
  if (L & 1) {
186
0
    for (n = 1; n <= (L+1)/2; n++)
187
0
      window[n-1] = 2.0f * n / ((float)L + 1.0f);
188
0
    for (; n <= L; n++)
189
0
      window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
190
0
  }
191
0
  else {
192
0
    for (n = 1; n <= L/2; n++)
193
0
      window[n-1] = 2.0f * n / ((float)L + 1.0f);
194
0
    for (; n <= L; n++)
195
0
      window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
196
0
  }
197
0
}
198
199
void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
200
8.20k
{
201
8.20k
  if (p <= 0.0)
202
0
    FLAC__window_rectangle(window, L);
203
8.20k
  else if (p >= 1.0)
204
0
    FLAC__window_hann(window, L);
205
8.20k
  else if (!(p > 0.0f && p < 1.0f))
206
    /* p is not between 0 and 1, probably NaN.
207
     * Default to 0.5 */
208
0
    FLAC__window_tukey(window, L, 0.5f);
209
8.20k
  else {
210
8.20k
    const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
211
8.20k
    FLAC__int32 n;
212
    /* start with rectangle... */
213
8.20k
    FLAC__window_rectangle(window, L);
214
    /* ...replace ends with hann */
215
8.20k
    if (Np > 0) {
216
9.04M
      for (n = 0; n <= Np; n++) {
217
9.03M
        window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * n / Np));
218
9.03M
        window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * (n+Np) / Np));
219
9.03M
      }
220
7.55k
    }
221
8.20k
  }
222
8.20k
}
223
224
void FLAC__window_partial_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p, const FLAC__real start, const FLAC__real end)
225
0
{
226
0
  const FLAC__int32 start_n = (FLAC__int32)(start * L);
227
0
  const FLAC__int32 end_n = (FLAC__int32)(end * L);
228
0
  const FLAC__int32 N = end_n - start_n;
229
0
  FLAC__int32 Np, n, i;
230
231
0
  if (p <= 0.0f)
232
0
    FLAC__window_partial_tukey(window, L, 0.05f, start, end);
233
0
  else if (p >= 1.0f)
234
0
    FLAC__window_partial_tukey(window, L, 0.95f, start, end);
235
0
  else if (!(p > 0.0f && p < 1.0f))
236
    /* p is not between 0 and 1, probably NaN.
237
     * Default to 0.5 */
238
0
    FLAC__window_partial_tukey(window, L, 0.5f, start, end);
239
0
  else {
240
241
0
    Np = (FLAC__int32)(p / 2.0f * N);
242
243
0
    for (n = 0; n < start_n && n < L; n++)
244
0
      window[n] = 0.0f;
245
0
    for (i = 1; n < (start_n+Np) && n < L; n++, i++)
246
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Np));
247
0
    for (; n < (end_n-Np) && n < L; n++)
248
0
      window[n] = 1.0f;
249
0
    for (i = Np; n < end_n && n < L; n++, i--)
250
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Np));
251
0
    for (; n < L; n++)
252
0
      window[n] = 0.0f;
253
0
  }
254
0
}
255
256
void FLAC__window_punchout_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p, const FLAC__real start, const FLAC__real end)
257
0
{
258
0
  const FLAC__int32 start_n = (FLAC__int32)(start * L);
259
0
  const FLAC__int32 end_n = (FLAC__int32)(end * L);
260
0
  FLAC__int32 Ns, Ne, n, i;
261
262
0
  if (p <= 0.0f)
263
0
    FLAC__window_punchout_tukey(window, L, 0.05f, start, end);
264
0
  else if (p >= 1.0f)
265
0
    FLAC__window_punchout_tukey(window, L, 0.95f, start, end);
266
0
  else if (!(p > 0.0f && p < 1.0f))
267
    /* p is not between 0 and 1, probably NaN.
268
     * Default to 0.5 */
269
0
    FLAC__window_punchout_tukey(window, L, 0.5f, start, end);
270
0
  else {
271
272
0
    Ns = (FLAC__int32)(p / 2.0f * start_n);
273
0
    Ne = (FLAC__int32)(p / 2.0f * (L - end_n));
274
275
0
    for (n = 0, i = 1; n < Ns && n < L; n++, i++)
276
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Ns));
277
0
    for (; n < start_n-Ns && n < L; n++)
278
0
      window[n] = 1.0f;
279
0
    for (i = Ns; n < start_n && n < L; n++, i--)
280
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Ns));
281
0
    for (; n < end_n && n < L; n++)
282
0
      window[n] = 0.0f;
283
0
    for (i = 1; n < end_n+Ne && n < L; n++, i++)
284
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Ne));
285
0
    for (; n < L - (Ne) && n < L; n++)
286
0
      window[n] = 1.0f;
287
0
    for (i = Ne; n < L; n++, i--)
288
0
      window[n] = (FLAC__real)(0.5f - 0.5f * cosf(M_PI * i / Ne));
289
0
  }
290
0
}
291
292
void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
293
0
{
294
0
  const FLAC__int32 N = L - 1;
295
0
  const double N2 = (double)N / 2.;
296
0
  FLAC__int32 n;
297
298
0
  for (n = 0; n <= N; n++) {
299
0
    const double k = ((double)n - N2) / N2;
300
0
    window[n] = (FLAC__real)(1.0f - k * k);
301
0
  }
302
0
}
303
304
#if defined(_MSC_VER)
305
#pragma warning ( default : 4244 )
306
#endif
307
308
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */