allow Lua session scripts to inject [immediate] RT-events
[ardour.git] / libs / qm-dsp / base / Window.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     QM DSP library
5     Centre for Digital Music, Queen Mary, University of London.
6     This file Copyright 2006 Chris Cannam.
7
8     This program is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License as
10     published by the Free Software Foundation; either version 2 of the
11     License, or (at your option) any later version.  See the file
12     COPYING included with this distribution for more information.
13 */
14
15 #ifndef _WINDOW_H_
16 #define _WINDOW_H_
17
18 #include <cmath>
19 #include <iostream>
20 #include <map>
21 #include <vector>
22
23 enum WindowType {
24     RectangularWindow,
25     BartlettWindow,
26     HammingWindow,
27     HanningWindow,
28     BlackmanWindow,
29     BlackmanHarrisWindow,
30
31     FirstWindow = RectangularWindow,
32     LastWindow = BlackmanHarrisWindow
33 };
34
35 /**
36  * Various shaped windows for sample frame conditioning, including
37  * cosine windows (Hann etc) and triangular and rectangular windows.
38  */
39 template <typename T>
40 class Window
41 {
42 public:
43     /**
44      * Construct a windower of the given type and size. 
45      *
46      * Note that the cosine windows are periodic by design, rather
47      * than symmetrical. (A window of size N is equivalent to a
48      * symmetrical window of size N+1 with the final element missing.)
49      */
50     Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
51     Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
52     Window &operator=(const Window &w) {
53         if (&w == this) return *this;
54         m_type = w.m_type;
55         m_size = w.m_size;
56         encache();
57         return *this;
58     }
59     virtual ~Window() { delete[] m_cache; }
60     
61     void cut(T *src) const { cut(src, src); }
62     void cut(const T *src, T *dst) const {
63         for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
64     }
65
66     WindowType getType() const { return m_type; }
67     int getSize() const { return m_size; }
68
69     std::vector<T> getWindowData() const {
70         std::vector<T> d;
71         for (int i = 0; i < m_size; ++i) {
72             d.push_back(m_cache[i]);
73         }
74         return d;
75     }
76
77 protected:
78     WindowType m_type;
79     int m_size;
80     T *m_cache;
81     
82     void encache();
83 };
84
85 template <typename T>
86 void Window<T>::encache()
87 {
88     int n = m_size;
89     T *mult = new T[n];
90     int i;
91     for (i = 0; i < n; ++i) mult[i] = 1.0;
92
93     switch (m_type) {
94                 
95     case RectangularWindow:
96         for (i = 0; i < n; ++i) {
97             mult[i] = mult[i] * 0.5;
98         }
99         break;
100             
101     case BartlettWindow:
102         if (n == 2) {
103             mult[0] = mult[1] = 0; // "matlab compatible"
104         } else if (n == 3) {
105             mult[0] = 0;
106             mult[1] = mult[2] = 2./3.;
107         } else if (n > 3) {
108             for (i = 0; i < n/2; ++i) {
109                 mult[i] = mult[i] * (i / T(n/2));
110                 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
111             }
112         }
113         break;
114             
115     case HammingWindow:
116         if (n > 1) {
117             for (i = 0; i < n; ++i) {
118                 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
119             }
120         }
121         break;
122             
123     case HanningWindow:
124         if (n > 1) {
125             for (i = 0; i < n; ++i) {
126                 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
127             }
128         }
129         break;
130             
131     case BlackmanWindow:
132         if (n > 1) {
133             for (i = 0; i < n; ++i) {
134                 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
135                                      + 0.08 * cos(4 * M_PI * i / n));
136             }
137         }
138         break;
139             
140     case BlackmanHarrisWindow:
141         if (n > 1) {
142             for (i = 0; i < n; ++i) {
143                 mult[i] = mult[i] * (0.35875
144                                      - 0.48829 * cos(2 * M_PI * i / n)
145                                      + 0.14128 * cos(4 * M_PI * i / n)
146                                      - 0.01168 * cos(6 * M_PI * i / n));
147             }
148         }
149         break;
150     }
151            
152     m_cache = mult;
153 }
154
155 #endif