comparison src/vamp-hostsdk/Window.h @ 317:5cb298435765

Add support for changing window shape in PluginInputDomainAdapter
author Chris Cannam
date Tue, 21 Jun 2011 15:40:50 +0100
parents
children 7bab0c5422f4
comparison
equal deleted inserted replaced
316:c10fb3b3d029 317:5cb298435765
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006-2011 Chris Cannam and QMUL.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 #ifndef _WINDOW_H_
38 #define _WINDOW_H_
39
40 #include <vamp-hostsdk/hostguard.h>
41
42 #include <cmath>
43 #include <cstdlib>
44
45 _VAMP_SDK_HOSTSPACE_BEGIN(Window.h)
46
47 template <typename T>
48 class Window
49 {
50 public:
51 enum WindowType {
52 RectangularWindow,
53 BartlettWindow,
54 HammingWindow,
55 HanningWindow,
56 BlackmanWindow,
57 NuttallWindow,
58 BlackmanHarrisWindow
59 };
60
61 /**
62 * Construct a windower of the given type.
63 */
64 Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
65 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
66 Window &operator=(const Window &w) {
67 if (&w == this) return *this;
68 m_type = w.m_type;
69 m_size = w.m_size;
70 encache();
71 return *this;
72 }
73 virtual ~Window() { delete[] m_cache; }
74
75 void cut(T *src) const { cut(src, src); }
76 void cut(T *src, T *dst) const {
77 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
78 }
79 template <typename T0>
80 void cut(T0 *src, T *dst) const {
81 for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
82 }
83
84 T getArea() { return m_area; }
85 T getValue(size_t i) { return m_cache[i]; }
86
87 WindowType getType() const { return m_type; }
88 size_t getSize() const { return m_size; }
89
90 protected:
91 WindowType m_type;
92 size_t m_size;
93 T *m_cache;
94 T m_area;
95
96 void encache();
97 void cosinewin(T *, T, T, T, T);
98 };
99
100 template <typename T>
101 void Window<T>::encache()
102 {
103 int n = int(m_size);
104 T *mult = new T[n];
105 int i;
106 for (i = 0; i < n; ++i) mult[i] = 1.0;
107
108 switch (m_type) {
109
110 case RectangularWindow:
111 for (i = 0; i < n; ++i) {
112 mult[i] *= 0.5;
113 }
114 break;
115
116 case BartlettWindow:
117 for (i = 0; i < n/2; ++i) {
118 mult[i] *= (i / T(n/2));
119 mult[i + n/2] *= (1.0 - (i / T(n/2)));
120 }
121 break;
122
123 case HammingWindow:
124 cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
125 break;
126
127 case HanningWindow:
128 cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
129 break;
130
131 case BlackmanWindow:
132 cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
133 break;
134
135 case NuttallWindow:
136 cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
137 break;
138
139 case BlackmanHarrisWindow:
140 cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
141 break;
142 }
143
144 m_cache = mult;
145
146 m_area = 0;
147 for (int i = 0; i < n; ++i) {
148 m_area += m_cache[i];
149 }
150 m_area /= n;
151 }
152
153 template <typename T>
154 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
155 {
156 int n = int(m_size);
157 for (int i = 0; i < n; ++i) {
158 mult[i] *= (a0
159 - a1 * cos((2 * M_PI * i) / n)
160 + a2 * cos((4 * M_PI * i) / n)
161 - a3 * cos((6 * M_PI * i) / n));
162 }
163 }
164
165 _VAMP_SDK_HOSTSPACE_END(Window.h)
166
167 #endif