Chris@317: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@317: Chris@317: /* Chris@317: Vamp Chris@317: Chris@317: An API for audio analysis and feature extraction plugins. Chris@317: Chris@317: Centre for Digital Music, Queen Mary, University of London. Chris@317: Copyright 2006-2011 Chris Cannam and QMUL. Chris@317: Chris@317: Permission is hereby granted, free of charge, to any person Chris@317: obtaining a copy of this software and associated documentation Chris@317: files (the "Software"), to deal in the Software without Chris@317: restriction, including without limitation the rights to use, copy, Chris@317: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@317: of the Software, and to permit persons to whom the Software is Chris@317: furnished to do so, subject to the following conditions: Chris@317: Chris@317: The above copyright notice and this permission notice shall be Chris@317: included in all copies or substantial portions of the Software. Chris@317: Chris@317: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@317: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@317: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@317: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@317: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@317: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@317: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@317: Chris@317: Except as contained in this notice, the names of the Centre for Chris@317: Digital Music; Queen Mary, University of London; and Chris Cannam Chris@317: shall not be used in advertising or otherwise to promote the sale, Chris@317: use or other dealings in this Software without prior written Chris@317: authorization. Chris@317: */ Chris@317: Chris@317: #ifndef _WINDOW_H_ Chris@317: #define _WINDOW_H_ Chris@317: Chris@317: #include Chris@317: Chris@317: #include Chris@317: #include Chris@317: Chris@317: _VAMP_SDK_HOSTSPACE_BEGIN(Window.h) Chris@317: Chris@317: template Chris@317: class Window Chris@317: { Chris@317: public: Chris@317: enum WindowType { Chris@317: RectangularWindow, Chris@317: BartlettWindow, Chris@317: HammingWindow, Chris@317: HanningWindow, Chris@317: BlackmanWindow, Chris@317: NuttallWindow, Chris@317: BlackmanHarrisWindow Chris@317: }; Chris@317: Chris@317: /** Chris@317: * Construct a windower of the given type. Chris@317: */ Chris@317: Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } Chris@317: Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } Chris@317: Window &operator=(const Window &w) { Chris@317: if (&w == this) return *this; Chris@317: m_type = w.m_type; Chris@317: m_size = w.m_size; Chris@317: encache(); Chris@317: return *this; Chris@317: } Chris@317: virtual ~Window() { delete[] m_cache; } Chris@317: Chris@317: void cut(T *src) const { cut(src, src); } Chris@317: void cut(T *src, T *dst) const { Chris@317: for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; Chris@317: } Chris@317: template Chris@317: void cut(T0 *src, T *dst) const { Chris@317: for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; Chris@317: } Chris@317: Chris@317: T getArea() { return m_area; } Chris@317: T getValue(size_t i) { return m_cache[i]; } Chris@317: Chris@317: WindowType getType() const { return m_type; } Chris@317: size_t getSize() const { return m_size; } Chris@317: Chris@317: protected: Chris@317: WindowType m_type; Chris@317: size_t m_size; Chris@317: T *m_cache; Chris@317: T m_area; Chris@317: Chris@317: void encache(); Chris@317: void cosinewin(T *, T, T, T, T); Chris@317: }; Chris@317: Chris@317: template Chris@317: void Window::encache() Chris@317: { Chris@317: int n = int(m_size); Chris@317: T *mult = new T[n]; Chris@317: int i; Chris@317: for (i = 0; i < n; ++i) mult[i] = 1.0; Chris@317: Chris@317: switch (m_type) { Chris@317: Chris@317: case RectangularWindow: Chris@317: for (i = 0; i < n; ++i) { Chris@317: mult[i] *= 0.5; Chris@317: } Chris@317: break; Chris@317: Chris@317: case BartlettWindow: Chris@317: for (i = 0; i < n/2; ++i) { Chris@317: mult[i] *= (i / T(n/2)); Chris@317: mult[i + n/2] *= (1.0 - (i / T(n/2))); Chris@317: } Chris@317: break; Chris@317: Chris@317: case HammingWindow: Chris@317: cosinewin(mult, 0.54, 0.46, 0.0, 0.0); Chris@317: break; Chris@317: Chris@317: case HanningWindow: Chris@317: cosinewin(mult, 0.50, 0.50, 0.0, 0.0); Chris@317: break; Chris@317: Chris@317: case BlackmanWindow: Chris@317: cosinewin(mult, 0.42, 0.50, 0.08, 0.0); Chris@317: break; Chris@317: Chris@317: case NuttallWindow: Chris@317: cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411); Chris@317: break; Chris@317: Chris@317: case BlackmanHarrisWindow: Chris@317: cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168); Chris@317: break; Chris@317: } Chris@317: Chris@317: m_cache = mult; Chris@317: Chris@317: m_area = 0; Chris@317: for (int i = 0; i < n; ++i) { Chris@317: m_area += m_cache[i]; Chris@317: } Chris@317: m_area /= n; Chris@317: } Chris@317: Chris@317: template Chris@317: void Window::cosinewin(T *mult, T a0, T a1, T a2, T a3) Chris@317: { Chris@317: int n = int(m_size); Chris@317: for (int i = 0; i < n; ++i) { Chris@317: mult[i] *= (a0 Chris@317: - a1 * cos((2 * M_PI * i) / n) Chris@317: + a2 * cos((4 * M_PI * i) / n) Chris@317: - a3 * cos((6 * M_PI * i) / n)); Chris@317: } Chris@317: } Chris@317: Chris@317: _VAMP_SDK_HOSTSPACE_END(Window.h) Chris@317: Chris@317: #endif