Use PBD::ffs for portability
[ardour.git] / libs / rubberband / src / Window.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Rubber Band
5     An audio time-stretching and pitch-shifting library.
6     Copyright 2007-2008 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 _RUBBERBAND_WINDOW_H_
16 #define _RUBBERBAND_WINDOW_H_
17
18 #include <cstdlib>
19 #include <cmath>
20 #include <cstdlib>
21 #include <iostream>
22 #include <cstdlib>
23 #include <map>
24
25 #include "sysutils.h"
26
27 namespace RubberBand {
28
29 enum WindowType {
30     RectangularWindow,
31     BartlettWindow,
32     HammingWindow,
33     HanningWindow,
34     BlackmanWindow,
35     GaussianWindow,
36     ParzenWindow,
37     NuttallWindow,
38     BlackmanHarrisWindow
39 };
40
41 template <typename T>
42 class Window
43 {
44 public:
45     /**
46      * Construct a windower of the given type.
47      */
48     Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
49     Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
50     Window &operator=(const Window &w) {
51         if (&w == this) return *this;
52         m_type = w.m_type;
53         m_size = w.m_size;
54         encache();
55         return *this;
56     }
57     virtual ~Window() { delete[] m_cache; }
58     
59     void cut(T *R__ src) const
60     {
61         const int sz = m_size;
62         for (int i = 0; i < sz; ++i) {
63             src[i] *= m_cache[i];
64         }
65     }
66
67     void cut(T *R__ src, T *dst) const {
68         const int sz = m_size;
69         for (int i = 0; i < sz; ++i) {
70             dst[i] = src[i];
71         }
72         for (int i = 0; i < sz; ++i) {
73             dst[i] *= m_cache[i];
74         }
75     }
76
77     T getArea() { return m_area; }
78     T getValue(int i) { return m_cache[i]; }
79
80     WindowType getType() const { return m_type; }
81     int getSize() const { return m_size; }
82
83 protected:
84     WindowType m_type;
85     int m_size;
86     T *R__ m_cache;
87     T m_area;
88     
89     void encache();
90     void cosinewin(T *, T, T, T, T);
91 };
92
93 template <typename T>
94 void Window<T>::encache()
95 {
96     int n = int(m_size);
97     T *mult = new T[n];
98     int i;
99     for (i = 0; i < n; ++i) mult[i] = 1.0;
100
101     switch (m_type) {
102                 
103     case RectangularWindow:
104         for (i = 0; i < n; ++i) {
105             mult[i] *= 0.5;
106         }
107         break;
108             
109     case BartlettWindow:
110         for (i = 0; i < n/2; ++i) {
111             mult[i] *= (i / T(n/2));
112             mult[i + n/2] *= (1.0 - (i / T(n/2)));
113         }
114         break;
115             
116     case HammingWindow:
117         cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
118         break;
119             
120     case HanningWindow:
121         cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
122         break;
123             
124     case BlackmanWindow:
125         cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
126         break;
127             
128     case GaussianWindow:
129         for (i = 0; i < n; ++i) {
130             mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2));
131         }
132         break;
133             
134     case ParzenWindow:
135     {
136         int N = n-1;
137         for (i = 0; i < N/4; ++i) {
138             T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3);
139             mult[i] *= m;
140             mult[N-i] *= m;
141         }
142         for (i = N/4; i <= N/2; ++i) {
143             int wn = i - N/2;
144             T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2));
145             mult[i] *= m;
146             mult[N-i] *= m;
147         }            
148         break;
149     }
150
151     case NuttallWindow:
152         cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
153         break;
154
155     case BlackmanHarrisWindow:
156         cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
157         break;
158     }
159         
160     m_cache = mult;
161
162     m_area = 0;
163     for (int i = 0; i < n; ++i) {
164         m_area += m_cache[i];
165     }
166     m_area /= n;
167 }
168
169 template <typename T>
170 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
171 {
172     int n = int(m_size);
173     for (int i = 0; i < n; ++i) {
174         mult[i] *= (a0
175                     - a1 * cos(2 * M_PI * i / n)
176                     + a2 * cos(4 * M_PI * i / n)
177                     - a3 * cos(6 * M_PI * i / n));
178     }
179 }
180
181 }
182
183 #endif