BGC Tools
Public Types | Static Public Member Functions | Static Private Member Functions
BGC.Audio.Windowing Class Reference

A collection of some common operations related to Windowing Audio. More...

Public Types

enum  Function {
  Function.Hamming = 0, Function.Hann, Function.Sine, Function.Linear,
  Function.Square, Function.Silence, Function.MAX
}
 

Static Public Member Functions

static float [] GetHalfWindow (Function function, int sampleCount)
 
static double [] GetHalfWindow64 (Function function, int sampleCount)
 
static void ApplyWindow (float[] samples, Function function, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
 Applies a window to the passed-in samples More...
 
static void ApplyWindow (float[] samples, Function function, int startSample=-1, int windowWidth=-1, double smoothingTime=0.01, double samplingRate=44100.0, int channels=2)
 Applies a window to the passed-in samples More...
 
static void Linear (float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
 
static void Silence (float[] samples, int startSample=-1, int windowWidth=-1, int channels=2)
 
static void Hamming (float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
 
static void Hann (float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
 
static void Sine (float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
 

Static Private Member Functions

static float [] HammingHalfWindow (int sampleCount)
 
static float [] HannHalfWindow (int sampleCount)
 
static float [] SineHalfWindow (int sampleCount)
 
static float [] LinearHalfWindow (int sampleCount)
 
static float [] SquareHalfWindow (int sampleCount)
 
static float [] SilenceHalfWindow (int sampleCount)
 
static double [] HammingHalfWindow64 (int sampleCount)
 
static double [] HannHalfWindow64 (int sampleCount)
 
static double [] SineHalfWindow64 (int sampleCount)
 
static double [] LinearHalfWindow64 (int sampleCount)
 
static double [] SquareHalfWindow64 (int sampleCount)
 
static double [] SilenceHalfWindow64 (int sampleCount)
 

Detailed Description

A collection of some common operations related to Windowing Audio.

Definition at line 9 of file Windowing.cs.

Member Enumeration Documentation

◆ Function

Enumerator
Hamming 
Hann 
Sine 
Linear 
Square 
Silence 
MAX 

Definition at line 11 of file Windowing.cs.

12  {
13  Hamming = 0,
14  Hann,
15  Sine,
16  Linear,
17  Square,
18  Silence,
19  MAX
20  }
static void Hamming(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:265
static void Silence(float[] samples, int startSample=-1, int windowWidth=-1, int channels=2)
Definition: Windowing.cs:238
static void Linear(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:199
static void Sine(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:353
static void Hann(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:309

Member Function Documentation

◆ ApplyWindow() [1/2]

static void BGC.Audio.Windowing.ApplyWindow ( float []  samples,
Function  function,
int  startSample = -1,
int  windowWidth = -1,
int  smoothingSamples = 1000,
int  channels = 2 
)
inlinestatic

Applies a window to the passed-in samples

Parameters
startSampleFirst sample number (within, not across, channels)

Definition at line 64 of file Windowing.cs.

71  {
72  switch (function)
73  {
74  case Function.Hamming:
75  Hamming(
76  samples: samples,
77  startSample: startSample,
78  windowWidth: windowWidth,
79  smoothingSamples: smoothingSamples,
80  channels: channels);
81  break;
82 
83  case Function.Hann:
84  Hann(
85  samples: samples,
86  startSample: startSample,
87  windowWidth: windowWidth,
88  smoothingSamples: smoothingSamples,
89  channels: channels);
90  break;
91 
92  case Function.Sine:
93  Sine(
94  samples: samples,
95  startSample: startSample,
96  windowWidth: windowWidth,
97  smoothingSamples: smoothingSamples,
98  channels: channels);
99  break;
100 
101  case Function.Linear:
102  Linear(
103  samples: samples,
104  startSample: startSample,
105  windowWidth: windowWidth,
106  smoothingSamples: smoothingSamples,
107  channels: channels);
108  break;
109 
110  case Function.Square:
111  //Do nothing
112  break;
113 
114  case Function.Silence:
115  Silence(
116  samples: samples,
117  startSample: startSample,
118  windowWidth: windowWidth,
119  channels: channels);
120  break;
121 
122  default:
123  Debug.LogError($"Unexpected Function: {function}");
124  break;
125  }
126  }
static void Hamming(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:265
static void Silence(float[] samples, int startSample=-1, int windowWidth=-1, int channels=2)
Definition: Windowing.cs:238
static void Linear(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:199
static void Sine(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:353
static void Hann(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:309

◆ ApplyWindow() [2/2]

static void BGC.Audio.Windowing.ApplyWindow ( float []  samples,
Function  function,
int  startSample = -1,
int  windowWidth = -1,
double  smoothingTime = 0.01,
double  samplingRate = 44100.0,
int  channels = 2 
)
inlinestatic

Applies a window to the passed-in samples

Parameters
startSampleFirst sample number (within, not across, channels)

Definition at line 132 of file Windowing.cs.

140  {
141  int smoothingSamples = (int)(smoothingTime * samplingRate);
142 
143  switch (function)
144  {
145  case Function.Hamming:
146  Hamming(
147  samples: samples,
148  startSample: startSample,
149  windowWidth: windowWidth,
150  smoothingSamples: smoothingSamples,
151  channels: channels);
152  break;
153 
154  case Function.Hann:
155  Hann(
156  samples: samples,
157  startSample: startSample,
158  windowWidth: windowWidth,
159  smoothingSamples: smoothingSamples,
160  channels: channels);
161  break;
162 
163  case Function.Sine:
164  Sine(
165  samples: samples,
166  startSample: startSample,
167  windowWidth: windowWidth,
168  smoothingSamples: smoothingSamples,
169  channels: channels);
170  break;
171 
172  case Function.Linear:
173  Linear(
174  samples: samples,
175  startSample: startSample,
176  windowWidth: windowWidth,
177  smoothingSamples: smoothingSamples,
178  channels: channels);
179  break;
180 
181  case Function.Square:
182  //Do nothing
183  break;
184 
185  case Function.Silence:
186  Silence(
187  samples: samples,
188  startSample: startSample,
189  windowWidth: windowWidth,
190  channels: channels);
191  break;
192 
193  default:
194  Debug.LogError($"Unexpected Function: {function}");
195  break;
196  }
197  }
static void Hamming(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:265
static void Silence(float[] samples, int startSample=-1, int windowWidth=-1, int channels=2)
Definition: Windowing.cs:238
static void Linear(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:199
static void Sine(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:353
static void Hann(float[] samples, int startSample=-1, int windowWidth=-1, int smoothingSamples=1000, int channels=2)
Definition: Windowing.cs:309

◆ GetHalfWindow()

static float [] BGC.Audio.Windowing.GetHalfWindow ( Function  function,
int  sampleCount 
)
inlinestatic

Definition at line 22 of file Windowing.cs.

Referenced by BGC.Audio.Filters.StreamWindower.StreamWindower().

25  {
26  switch (function)
27  {
28  case Function.Hamming: return HammingHalfWindow(sampleCount);
29  case Function.Hann: return HannHalfWindow(sampleCount);
30  case Function.Sine: return SineHalfWindow(sampleCount);
31  case Function.Linear: return LinearHalfWindow(sampleCount);
32  case Function.Square: return SquareHalfWindow(sampleCount);
33  case Function.Silence: return SilenceHalfWindow(sampleCount);
34 
35  default:
36  Debug.LogError($"Unexpected Function: {function}");
37  goto case Function.Square;
38  }
39  }
static float [] SineHalfWindow(int sampleCount)
Definition: Windowing.cs:428
static float [] LinearHalfWindow(int sampleCount)
Definition: Windowing.cs:442
static float [] HannHalfWindow(int sampleCount)
Definition: Windowing.cs:411
static float [] SquareHalfWindow(int sampleCount)
Definition: Windowing.cs:456
static float [] SilenceHalfWindow(int sampleCount)
Definition: Windowing.cs:468
static float [] HammingHalfWindow(int sampleCount)
Definition: Windowing.cs:394
Here is the caller graph for this function:

◆ GetHalfWindow64()

static double [] BGC.Audio.Windowing.GetHalfWindow64 ( Function  function,
int  sampleCount 
)
inlinestatic

Definition at line 41 of file Windowing.cs.

Referenced by BGC.Audio.AnalyticStreams.AnalyticStreamWindower.AnalyticStreamWindower().

44  {
45  switch (function)
46  {
47  case Function.Hamming: return HammingHalfWindow64(sampleCount);
48  case Function.Hann: return HannHalfWindow64(sampleCount);
49  case Function.Sine: return SineHalfWindow64(sampleCount);
50  case Function.Linear: return LinearHalfWindow64(sampleCount);
51  case Function.Square: return SquareHalfWindow64(sampleCount);
52  case Function.Silence: return SilenceHalfWindow64(sampleCount);
53 
54  default:
55  Debug.LogError($"Unexpected Function: {function}");
56  goto case Function.Square;
57  }
58  }
static double [] SilenceHalfWindow64(int sampleCount)
Definition: Windowing.cs:547
static double [] SineHalfWindow64(int sampleCount)
Definition: Windowing.cs:507
static double [] HannHalfWindow64(int sampleCount)
Definition: Windowing.cs:490
static double [] HammingHalfWindow64(int sampleCount)
Definition: Windowing.cs:473
static double [] SquareHalfWindow64(int sampleCount)
Definition: Windowing.cs:535
static double [] LinearHalfWindow64(int sampleCount)
Definition: Windowing.cs:521
Here is the caller graph for this function:

◆ Hamming()

static void BGC.Audio.Windowing.Hamming ( float []  samples,
int  startSample = -1,
int  windowWidth = -1,
int  smoothingSamples = 1000,
int  channels = 2 
)
inlinestatic

Definition at line 265 of file Windowing.cs.

271  {
272  const float alpha = 0.54f;
273  const float beta = 1f - alpha;
274 
275  //Default value of startSample
276  if (startSample == -1)
277  {
278  startSample = 0;
279  }
280 
281  //Default value of windowWidth
282  if (windowWidth == -1)
283  {
284  windowWidth = (samples.Length / channels) - startSample;
285  }
286 
287  //Correct smoothingSamples for small windows
288  if (2 * smoothingSamples > windowWidth)
289  {
290  smoothingSamples = windowWidth / 2;
291  }
292 
293  int lastSample = startSample + windowWidth - 1;
294 
295  float cosineArgument = Mathf.PI / (smoothingSamples - 1);
296 
297  for (int i = 0; i < smoothingSamples; i++)
298  {
299  float factor = alpha - beta * Mathf.Cos(i * cosineArgument);
300 
301  for (int chan = 0; chan < channels; chan++)
302  {
303  samples[(startSample + i) * channels + chan] *= factor;
304  samples[(lastSample - i) * channels + chan] *= factor;
305  }
306  }
307  }

◆ HammingHalfWindow()

static float [] BGC.Audio.Windowing.HammingHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 394 of file Windowing.cs.

395  {
396  const float alpha = 0.54f;
397  const float beta = 1f - alpha;
398 
399  float[] window = new float[sampleCount];
400 
401  float cosineArgument = Mathf.PI / (sampleCount - 1);
402 
403  for (int i = 0; i < sampleCount; i++)
404  {
405  window[i] = alpha - beta * Mathf.Cos(i * cosineArgument);
406  }
407 
408  return window;
409  }

◆ HammingHalfWindow64()

static double [] BGC.Audio.Windowing.HammingHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 473 of file Windowing.cs.

474  {
475  const double alpha = 0.54;
476  const double beta = 1.0 - alpha;
477 
478  double[] window = new double[sampleCount];
479 
480  double cosineArgument = Math.PI / (sampleCount - 1);
481 
482  for (int i = 0; i < sampleCount; i++)
483  {
484  window[i] = alpha - beta * Math.Cos(i * cosineArgument);
485  }
486 
487  return window;
488  }

◆ Hann()

static void BGC.Audio.Windowing.Hann ( float []  samples,
int  startSample = -1,
int  windowWidth = -1,
int  smoothingSamples = 1000,
int  channels = 2 
)
inlinestatic

Definition at line 309 of file Windowing.cs.

315  {
316  const float alpha = 0.5f;
317  const float beta = 1f - alpha;
318 
319  //Default value of startSample
320  if (startSample == -1)
321  {
322  startSample = 0;
323  }
324 
325  //Default value of windowWidth
326  if (windowWidth == -1)
327  {
328  windowWidth = (samples.Length / channels) - startSample;
329  }
330 
331  //Correct smoothingSamples for small windows
332  if (2 * smoothingSamples > windowWidth)
333  {
334  smoothingSamples = windowWidth / 2;
335  }
336 
337  int lastSample = startSample + windowWidth - 1;
338 
339  float cosineArgument = Mathf.PI / (smoothingSamples - 1);
340 
341  for (int i = 0; i < smoothingSamples; i++)
342  {
343  float factor = alpha - beta * Mathf.Cos(i * cosineArgument);
344 
345  for (int chan = 0; chan < channels; chan++)
346  {
347  samples[(startSample + i) * channels + chan] *= factor;
348  samples[(lastSample - i) * channels + chan] *= factor;
349  }
350  }
351  }

◆ HannHalfWindow()

static float [] BGC.Audio.Windowing.HannHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 411 of file Windowing.cs.

412  {
413  const float alpha = 0.5f;
414  const float beta = 1f - alpha;
415 
416  float[] window = new float[sampleCount];
417 
418  float cosineArgument = Mathf.PI / (sampleCount - 1);
419 
420  for (int i = 0; i < sampleCount; i++)
421  {
422  window[i] = alpha - beta * Mathf.Cos(i * cosineArgument);
423  }
424 
425  return window;
426  }

◆ HannHalfWindow64()

static double [] BGC.Audio.Windowing.HannHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 490 of file Windowing.cs.

491  {
492  const double alpha = 0.5;
493  const double beta = 1.0 - alpha;
494 
495  double[] window = new double[sampleCount];
496 
497  double cosineArgument = Math.PI / (sampleCount - 1);
498 
499  for (int i = 0; i < sampleCount; i++)
500  {
501  window[i] = alpha - beta * Math.Cos(i * cosineArgument);
502  }
503 
504  return window;
505  }

◆ Linear()

static void BGC.Audio.Windowing.Linear ( float []  samples,
int  startSample = -1,
int  windowWidth = -1,
int  smoothingSamples = 1000,
int  channels = 2 
)
inlinestatic

Definition at line 199 of file Windowing.cs.

205  {
206  //Default value of startSample
207  if (startSample == -1)
208  {
209  startSample = 0;
210  }
211 
212  //Default value of windowWidth
213  if (windowWidth == -1)
214  {
215  windowWidth = (samples.Length / channels) - startSample;
216  }
217 
218  //Correct smoothingSamples for small windows
219  if (2 * smoothingSamples > windowWidth)
220  {
221  smoothingSamples = windowWidth / 2;
222  }
223 
224  int lastSample = startSample + windowWidth - 1;
225 
226  float count = smoothingSamples - 1;
227 
228  for (int i = 0; i < smoothingSamples; i++)
229  {
230  for (int chan = 0; chan < channels; chan++)
231  {
232  samples[(startSample + i) * channels + chan] *= i / count;
233  samples[(lastSample - i) * channels + chan] *= i / count;
234  }
235  }
236  }

◆ LinearHalfWindow()

static float [] BGC.Audio.Windowing.LinearHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 442 of file Windowing.cs.

443  {
444  float[] window = new float[sampleCount];
445 
446  float limit = sampleCount - 1;
447 
448  for (int i = 0; i < sampleCount; i++)
449  {
450  window[i] = i / limit;
451  }
452 
453  return window;
454  }

◆ LinearHalfWindow64()

static double [] BGC.Audio.Windowing.LinearHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 521 of file Windowing.cs.

522  {
523  double[] window = new double[sampleCount];
524 
525  double limit = sampleCount - 1;
526 
527  for (int i = 0; i < sampleCount; i++)
528  {
529  window[i] = i / limit;
530  }
531 
532  return window;
533  }

◆ Silence()

static void BGC.Audio.Windowing.Silence ( float []  samples,
int  startSample = -1,
int  windowWidth = -1,
int  channels = 2 
)
inlinestatic

Definition at line 238 of file Windowing.cs.

243  {
244  //Default value of startSample
245  if (startSample == -1)
246  {
247  startSample = 0;
248  }
249 
250  //Default value of windowWidth
251  if (windowWidth == -1)
252  {
253  windowWidth = (samples.Length / channels) - startSample;
254  }
255 
256  for (int i = 0; i < windowWidth; i++)
257  {
258  for (int chan = 0; chan < channels; chan++)
259  {
260  samples[(startSample + i) * channels + chan] = 0f;
261  }
262  }
263  }

◆ SilenceHalfWindow()

static float [] BGC.Audio.Windowing.SilenceHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 468 of file Windowing.cs.

469  {
470  return new float[sampleCount];
471  }

◆ SilenceHalfWindow64()

static double [] BGC.Audio.Windowing.SilenceHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 547 of file Windowing.cs.

548  {
549  return new double[sampleCount];
550  }

◆ Sine()

static void BGC.Audio.Windowing.Sine ( float []  samples,
int  startSample = -1,
int  windowWidth = -1,
int  smoothingSamples = 1000,
int  channels = 2 
)
inlinestatic

Definition at line 353 of file Windowing.cs.

359  {
360  //Default value of startSample
361  if (startSample == -1)
362  {
363  startSample = 0;
364  }
365 
366  //Default value of windowWidth
367  if (windowWidth == -1)
368  {
369  windowWidth = (samples.Length / channels) - startSample;
370  }
371 
372  //Correct smoothingSamples for small windows
373  if (2 * smoothingSamples > windowWidth)
374  {
375  smoothingSamples = windowWidth / 2;
376  }
377 
378  int lastSample = startSample + windowWidth - 1;
379 
380  float sineArgument = Mathf.PI / (2 * smoothingSamples - 1);
381 
382  for (int i = 0; i < smoothingSamples; i++)
383  {
384  float factor = Mathf.Sin(i * sineArgument);
385 
386  for (int chan = 0; chan < channels; chan++)
387  {
388  samples[(startSample + i) * channels + chan] *= factor;
389  samples[(lastSample - i) * channels + chan] *= factor;
390  }
391  }
392  }

◆ SineHalfWindow()

static float [] BGC.Audio.Windowing.SineHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 428 of file Windowing.cs.

429  {
430  float[] window = new float[sampleCount];
431 
432  float sineArgument = Mathf.PI / (2 * sampleCount - 1);
433 
434  for (int i = 0; i < sampleCount; i++)
435  {
436  window[i] = Mathf.Sin(i * sineArgument);
437  }
438 
439  return window;
440  }

◆ SineHalfWindow64()

static double [] BGC.Audio.Windowing.SineHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 507 of file Windowing.cs.

508  {
509  double[] window = new double[sampleCount];
510 
511  double sineArgument = Math.PI / (2 * sampleCount - 1);
512 
513  for (int i = 0; i < sampleCount; i++)
514  {
515  window[i] = Math.Sin(i * sineArgument);
516  }
517 
518  return window;
519  }

◆ SquareHalfWindow()

static float [] BGC.Audio.Windowing.SquareHalfWindow ( int  sampleCount)
inlinestaticprivate

Definition at line 456 of file Windowing.cs.

457  {
458  float[] window = new float[sampleCount];
459 
460  for (int i = 0; i < sampleCount; i++)
461  {
462  window[i] = 1f;
463  }
464 
465  return window;
466  }

◆ SquareHalfWindow64()

static double [] BGC.Audio.Windowing.SquareHalfWindow64 ( int  sampleCount)
inlinestaticprivate

Definition at line 535 of file Windowing.cs.

536  {
537  double[] window = new double[sampleCount];
538 
539  for (int i = 0; i < sampleCount; i++)
540  {
541  window[i] = 1.0;
542  }
543 
544  return window;
545  }

The documentation for this class was generated from the following file: