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_
26 namespace RubberBand {
45 * Construct a windower of the given type.
47 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
48 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
49 Window &operator=(const Window &w) {
50 if (&w == this) return *this;
56 virtual ~Window() { delete[] m_cache; }
58 void cut(T *R__ src) const
60 const int sz = m_size;
61 for (int i = 0; i < sz; ++i) {
66 void cut(T *R__ src, T *dst) const {
67 const int sz = m_size;
68 for (int i = 0; i < sz; ++i) {
71 for (int i = 0; i < sz; ++i) {
76 T getArea() { return m_area; }
77 T getValue(int i) { return m_cache[i]; }
79 WindowType getType() const { return m_type; }
80 int getSize() const { return m_size; }
89 void cosinewin(T *, T, T, T, T);
93 void Window<T>::encache()
98 for (i = 0; i < n; ++i) mult[i] = 1.0;
102 case RectangularWindow:
103 for (i = 0; i < n; ++i) {
109 for (i = 0; i < n/2; ++i) {
110 mult[i] *= (i / T(n/2));
111 mult[i + n/2] *= (1.0 - (i / T(n/2)));
116 cosinewin(mult, 0.54, 0.46, 0.0, 0.0);
120 cosinewin(mult, 0.50, 0.50, 0.0, 0.0);
124 cosinewin(mult, 0.42, 0.50, 0.08, 0.0);
128 for (i = 0; i < n; ++i) {
129 mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2));
136 for (i = 0; i < N/4; ++i) {
137 T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3);
141 for (i = N/4; i <= N/2; ++i) {
143 T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2));
151 cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411);
154 case BlackmanHarrisWindow:
155 cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168);
162 for (int i = 0; i < n; ++i) {
163 m_area += m_cache[i];
168 template <typename T>
169 void Window<T>::cosinewin(T *mult, T a0, T a1, T a2, T a3)
172 for (int i = 0; i < n; ++i) {
174 - a1 * cos(2 * M_PI * i / n)
175 + a2 * cos(4 * M_PI * i / n)
176 - a3 * cos(6 * M_PI * i / n));