2010-05-25 21:49:05 +01:00
|
|
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|
|
|
|
|
|
|
|
|
/*
|
2012-09-09 16:57:42 +01:00
|
|
|
Rubber Band Library
|
2010-05-25 21:49:05 +01:00
|
|
|
An audio time-stretching and pitch-shifting library.
|
2014-05-29 18:00:22 +01:00
|
|
|
Copyright 2007-2014 Particular Programs Ltd.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
2010-05-25 21:49:05 +01:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version. See the file
|
|
|
|
|
COPYING included with this distribution for more information.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
|
|
|
|
Alternatively, if you have a valid commercial licence for the
|
|
|
|
|
Rubber Band Library obtained by agreement with the copyright
|
|
|
|
|
holders, you may redistribute and/or modify it under the terms
|
|
|
|
|
described in that licence.
|
|
|
|
|
|
|
|
|
|
If you wish to distribute code using the Rubber Band Library
|
|
|
|
|
under terms other than those of the GNU General Public License,
|
|
|
|
|
you must obtain a valid commercial licence before doing so.
|
2010-05-25 21:49:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _RUBBERBAND_SINC_WINDOW_H_
|
|
|
|
|
#define _RUBBERBAND_SINC_WINDOW_H_
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
#include "system/sysutils.h"
|
|
|
|
|
#include "system/VectorOps.h"
|
|
|
|
|
#include "system/Allocators.h"
|
|
|
|
|
|
|
|
|
|
namespace RubberBand {
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
class SincWindow
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2010-05-29 22:07:54 +01:00
|
|
|
* Construct a sinc windower which produces a window of size n
|
|
|
|
|
* containing the values of sinc(x) with x=0 at index n/2, such
|
|
|
|
|
* that the distance from -pi to pi (the point at which the sinc
|
|
|
|
|
* function first crosses zero, for negative and positive
|
2010-05-25 21:49:05 +01:00
|
|
|
* arguments respectively) is p samples.
|
|
|
|
|
*/
|
2010-05-29 22:07:54 +01:00
|
|
|
SincWindow(int n, int p) : m_size(n), m_p(p), m_cache(0) {
|
|
|
|
|
encache();
|
|
|
|
|
}
|
|
|
|
|
SincWindow(const SincWindow &w) : m_size(w.m_size), m_p(w.m_p), m_cache(0) {
|
|
|
|
|
encache();
|
|
|
|
|
}
|
2010-05-25 21:49:05 +01:00
|
|
|
SincWindow &operator=(const SincWindow &w) {
|
|
|
|
|
if (&w == this) return *this;
|
|
|
|
|
m_size = w.m_size;
|
|
|
|
|
m_p = w.m_p;
|
2010-05-29 22:07:54 +01:00
|
|
|
m_cache = 0;
|
2010-05-25 21:49:05 +01:00
|
|
|
encache();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2010-05-29 22:07:54 +01:00
|
|
|
virtual ~SincWindow() {
|
|
|
|
|
deallocate(m_cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regenerate the sinc window with the same size, but a new scale
|
|
|
|
|
* (the p value is interpreted as for the argument of the same
|
|
|
|
|
* name to the constructor). If p is unchanged from the previous
|
|
|
|
|
* value, do nothing (quickly).
|
|
|
|
|
*/
|
|
|
|
|
inline void rewrite(int p) {
|
|
|
|
|
if (m_p == p) return;
|
|
|
|
|
m_p = p;
|
|
|
|
|
encache();
|
|
|
|
|
}
|
2010-05-25 21:49:05 +01:00
|
|
|
|
2010-05-29 22:07:54 +01:00
|
|
|
inline void cut(T *const R__ dst) const {
|
|
|
|
|
v_multiply(dst, m_cache, m_size);
|
2010-05-25 21:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void cut(const T *const R__ src, T *const R__ dst) const {
|
|
|
|
|
v_multiply(dst, src, m_cache, m_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void add(T *const R__ dst, T scale) const {
|
|
|
|
|
v_add_with_gain(dst, m_cache, m_size, scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline T getArea() const { return m_area; }
|
|
|
|
|
inline T getValue(int i) const { return m_cache[i]; }
|
|
|
|
|
|
|
|
|
|
inline int getSize() const { return m_size; }
|
|
|
|
|
inline int getP() const { return m_p; }
|
|
|
|
|
|
2010-05-29 22:07:54 +01:00
|
|
|
/**
|
|
|
|
|
* Write a sinc window of size n with scale p (the p value is
|
|
|
|
|
* interpreted as for the argument of the same name to the
|
|
|
|
|
* constructor).
|
|
|
|
|
*/
|
|
|
|
|
static
|
|
|
|
|
void write(T *const R__ dst, const int n, const int p) {
|
|
|
|
|
const int half = n/2;
|
|
|
|
|
writeHalf(dst, n, p);
|
|
|
|
|
int target = half - 1;
|
|
|
|
|
for (int i = half + 1; i < n; ++i) {
|
|
|
|
|
dst[target--] = dst[i];
|
|
|
|
|
}
|
|
|
|
|
const T twopi = 2. * M_PI;
|
|
|
|
|
T arg = T(half) * twopi / p;
|
|
|
|
|
dst[0] = sin(arg) / arg;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-25 21:49:05 +01:00
|
|
|
protected:
|
|
|
|
|
int m_size;
|
|
|
|
|
int m_p;
|
|
|
|
|
T *R__ m_cache;
|
|
|
|
|
T m_area;
|
|
|
|
|
|
2010-05-29 22:07:54 +01:00
|
|
|
/**
|
|
|
|
|
* Write the positive half (i.e. n/2 to n-1) of a sinc window of
|
|
|
|
|
* size n with scale p (the p value is interpreted as for the
|
|
|
|
|
* argument of the same name to the constructor). The negative
|
|
|
|
|
* half (indices 0 to n/2-1) of dst is left unchanged.
|
|
|
|
|
*/
|
|
|
|
|
static
|
|
|
|
|
void writeHalf(T *const R__ dst, const int n, const int p) {
|
|
|
|
|
const int half = n/2;
|
|
|
|
|
const T twopi = 2. * M_PI;
|
|
|
|
|
dst[half] = T(1.0);
|
|
|
|
|
for (int i = 1; i < half; ++i) {
|
|
|
|
|
T arg = T(i) * twopi / p;
|
|
|
|
|
dst[half+i] = sin(arg) / arg;
|
|
|
|
|
}
|
2010-05-25 21:49:05 +01:00
|
|
|
}
|
2010-05-29 22:07:54 +01:00
|
|
|
|
|
|
|
|
void encache() {
|
|
|
|
|
if (!m_cache) {
|
|
|
|
|
m_cache = allocate<T>(m_size);
|
|
|
|
|
}
|
2010-05-25 21:49:05 +01:00
|
|
|
|
2010-05-29 22:07:54 +01:00
|
|
|
write(m_cache, m_size, m_p);
|
|
|
|
|
|
|
|
|
|
m_area = 0;
|
|
|
|
|
for (int i = 0; i < m_size; ++i) {
|
|
|
|
|
m_area += m_cache[i];
|
|
|
|
|
}
|
|
|
|
|
m_area /= m_size;
|
2010-05-25 21:49:05 +01:00
|
|
|
}
|
2010-05-29 22:07:54 +01:00
|
|
|
};
|
2010-05-25 21:49:05 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|