1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
5 An audio time-stretching and pitch-shifting library.
6 Copyright 2007-2008 Chris Cannam.
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.
15 #ifndef _RUBBERBAND_WINDOW_H_
16 #define _RUBBERBAND_WINDOW_H_
27 namespace RubberBand {
46 * Construct a windower of the given type.
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;
57 virtual ~Window() { delete[] m_cache; }
59 void cut(T *R__ src) const
61 const int sz = m_size;
62 for (int i = 0; i < sz; ++i) {
67 void cut(T *R__ src, T *dst) const {
68 const int sz = m_size;
69 for (int i = 0; i < sz; ++i) {
72 for (int i = 0; i < sz; ++i) {
77 T getArea() { return m_area; }
78 T getValue(int i) { return m_cache[i]; }
80 WindowType getType() const { return m_type; }
81 int getSize() const { return m_size; }
90 void cosinewin(T *, T, T, T, T);
94 void Window<T>::encache()
99 for (i = 0; i < n; ++i) mult[i] = 1.0;
103 case RectangularWindow:
104 for (i = 0; i < n; ++i) {
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)));
117 cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
121 cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
125 cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
129 for (i = 0; i < n; ++i) {
130 mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2));
137 for (i = 0; i < N/4; ++i) {
138 T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3);
142 for (i = N/4; i <= N/2; ++i) {
144 T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2));
152 cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
155 case BlackmanHarrisWindow:
156 cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
163 for (int i = 0; i < n; ++i) {
164 m_area += m_cache[i];
169 template <typename T>
170 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
173 for (int i = 0; i < n; ++i) {
175 - a1 * cos(2 * M_PI * i / n)
176 + a2 * cos(4 * M_PI * i / n)
177 - a3 * cos(6 * M_PI * i / n));