Skip to content

Commit

Permalink
Merge pull request #2150 from daschuer/qm-dsp-update
Browse files Browse the repository at this point in the history
Update qm-dsp from upstream
  • Loading branch information
uklotzde authored Jun 8, 2019
2 parents ddbaae4 + b8fb433 commit 7b465cc
Show file tree
Hide file tree
Showing 87 changed files with 3,999 additions and 54,643 deletions.
1 change: 0 additions & 1 deletion build/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ def sources(self, build):
#"#lib/qm-dsp/base/KaiserWindow.cpp",
"#lib/qm-dsp/base/Pitch.cpp",
#"#lib/qm-dsp/base/SincWindow.cpp",
"#lib/qm-dsp/dsp/chromagram/CQprecalc.cpp",
"#lib/qm-dsp/dsp/chromagram/Chromagram.cpp",
"#lib/qm-dsp/dsp/chromagram/ConstantQ.cpp",
"#lib/qm-dsp/dsp/keydetection/GetKeyMode.cpp",
Expand Down
10 changes: 6 additions & 4 deletions lib/qm-dsp/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ Code style
at the start of a function or class definition where it gets a line
of its own

* Please use braces around any conditional or loop block that
occupies its own line
* Please use braces around any conditional or loop block that is not
on the same line as the test

Some of the older code in this library does not follow these
guidelines - usually this means the code needs to be updated.
* Please keep lines to no more than 80 characters in length

* Avoid using unsigned int types, unless doing bit manipulation (see
http://soundsoftware.ac.uk/c-pitfall-unsigned.html for rationale)

26 changes: 13 additions & 13 deletions lib/qm-dsp/base/KaiserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@

KaiserWindow::Parameters
KaiserWindow::parametersForTransitionWidth(double attenuation,
double transition)
double transition)
{
Parameters p;
p.length = 1 + (attenuation > 21.0 ?
ceil((attenuation - 7.95) / (2.285 * transition)) :
ceil(5.79 / transition));
ceil((attenuation - 7.95) / (2.285 * transition)) :
ceil(5.79 / transition));
p.beta = (attenuation > 50.0 ?
0.1102 * (attenuation - 8.7) :
attenuation > 21.0 ?
0.5842 * pow(attenuation - 21.0, 0.4) + 0.07886 * (attenuation - 21.0) :
0);
0.1102 * (attenuation - 8.7) :
attenuation > 21.0 ?
0.5842 * pow(attenuation - 21.0, 0.4) + 0.07886 * (attenuation - 21.0) :
0);
return p;
}

static double besselTerm(double x, int i)
{
if (i == 0) {
return 1;
return 1;
} else {
double f = MathUtilities::factorial(i);
return pow(x/2, i*2) / (f*f);
double f = MathUtilities::factorial(i);
return pow(x/2, i*2) / (f*f);
}
}

static double bessel0(double x)
{
double b = 0.0;
for (int i = 0; i < 20; ++i) {
b += besselTerm(x, i);
b += besselTerm(x, i);
}
return b;
}
Expand All @@ -56,8 +56,8 @@ KaiserWindow::init()
double denominator = bessel0(m_beta);
bool even = (m_length % 2 == 0);
for (int i = 0; i < (even ? m_length/2 : (m_length+1)/2); ++i) {
double k = double(2*i) / double(m_length-1) - 1.0;
m_window.push_back(bessel0(m_beta * sqrt(1.0 - k*k)) / denominator);
double k = double(2*i) / double(m_length-1) - 1.0;
m_window.push_back(bessel0(m_beta * sqrt(1.0 - k*k)) / denominator);
}
for (int i = 0; i < (even ? m_length/2 : (m_length-1)/2); ++i) {
m_window.push_back(m_window[int(m_length/2) - i - 1]);
Expand Down
44 changes: 22 additions & 22 deletions lib/qm-dsp/base/KaiserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
COPYING included with this distribution for more information.
*/

#ifndef KAISER_WINDOW_H
#define KAISER_WINDOW_H
#ifndef QM_DSP_KAISER_WINDOW_H
#define QM_DSP_KAISER_WINDOW_H

#include <vector>
#include <cmath>
Expand All @@ -26,8 +26,8 @@ class KaiserWindow
{
public:
struct Parameters {
int length;
double beta;
int length;
double beta;
};

/**
Expand All @@ -41,57 +41,57 @@ class KaiserWindow
* and transition width in samples.
*/
static KaiserWindow byTransitionWidth(double attenuation,
double transition) {
return KaiserWindow
(parametersForTransitionWidth(attenuation, transition));
double transition) {
return KaiserWindow
(parametersForTransitionWidth(attenuation, transition));
}

/**
* Construct a Kaiser windower with the given attenuation in dB
* and transition bandwidth in Hz for the given samplerate.
*/
static KaiserWindow byBandwidth(double attenuation,
double bandwidth,
double samplerate) {
return KaiserWindow
(parametersForBandwidth(attenuation, bandwidth, samplerate));
double bandwidth,
double samplerate) {
return KaiserWindow
(parametersForBandwidth(attenuation, bandwidth, samplerate));
}

/**
* Obtain the parameters necessary for a Kaiser window of the
* given attenuation in dB and transition width in samples.
*/
static Parameters parametersForTransitionWidth(double attenuation,
double transition);
double transition);

/**
* Obtain the parameters necessary for a Kaiser window of the
* given attenuation in dB and transition bandwidth in Hz for the
* given samplerate.
*/
static Parameters parametersForBandwidth(double attenuation,
double bandwidth,
double samplerate) {
return parametersForTransitionWidth
(attenuation, (bandwidth * 2 * M_PI) / samplerate);
double bandwidth,
double samplerate) {
return parametersForTransitionWidth
(attenuation, (bandwidth * 2 * M_PI) / samplerate);
}

int getLength() const {
return m_length;
return m_length;
}

const double *getWindow() const {
return m_window.data();
return m_window.data();
}

void cut(double *src) const {
cut(src, src);
cut(src, src);
}

void cut(const double *src, double *dst) const {
for (int i = 0; i < m_length; ++i) {
dst[i] = src[i] * m_window[i];
}
for (int i = 0; i < m_length; ++i) {
dst[i] = src[i] * m_window[i];
}
}

private:
Expand Down
12 changes: 6 additions & 6 deletions lib/qm-dsp/base/Pitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@

float
Pitch::getFrequencyForPitch(int midiPitch,
float centsOffset,
float concertA)
float centsOffset,
float concertA)
{
float p = float(midiPitch) + (centsOffset / 100);
return concertA * powf(2.0, (p - 69.0) / 12.0);
}

int
Pitch::getPitchForFrequency(float frequency,
float *centsOffsetReturn,
float concertA)
float *centsOffsetReturn,
float concertA)
{
float p = 12.0 * (log(frequency / (concertA / 2.0)) / log(2.0)) + 57.0;

int midiPitch = int(p + 0.00001);
float centsOffset = (p - midiPitch) * 100.0;

if (centsOffset >= 50.0) {
midiPitch = midiPitch + 1;
centsOffset = -(100.0 - centsOffset);
midiPitch = midiPitch + 1;
centsOffset = -(100.0 - centsOffset);
}

if (centsOffsetReturn) *centsOffsetReturn = centsOffset;
Expand Down
12 changes: 6 additions & 6 deletions lib/qm-dsp/base/Pitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
COPYING included with this distribution for more information.
*/

#ifndef _PITCH_H_
#define _PITCH_H_
#ifndef QM_DSP_PITCH_H
#define QM_DSP_PITCH_H

/**
* Convert between musical pitch (i.e. MIDI pitch number) and
Expand All @@ -23,12 +23,12 @@ class Pitch
{
public:
static float getFrequencyForPitch(int midiPitch,
float centsOffset = 0,
float concertA = 440.0);
float centsOffset = 0,
float concertA = 440.0);

static int getPitchForFrequency(float frequency,
float *centsOffsetReturn = 0,
float concertA = 440.0);
float *centsOffsetReturn = 0,
float concertA = 440.0);
};


Expand Down
35 changes: 18 additions & 17 deletions lib/qm-dsp/base/Restrict.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@

#ifndef QM_DSP_RESTRICT_H
#define QM_DSP_RESTRICT_H

#ifdef _MSC_VER
#define QM_R__ __restrict
#endif

#ifdef __GNUC__
#define QM_R__ __restrict__
#endif

#ifndef QM_R__
#define QM_R__
#endif

#endif
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */

#ifndef QM_DSP_RESTRICT_H
#define QM_DSP_RESTRICT_H

#ifdef _MSC_VER
#define QM_R__ __restrict
#endif

#ifdef __GNUC__
#define QM_R__ __restrict__
#endif

#ifndef QM_R__
#define QM_R__
#endif

#endif
30 changes: 15 additions & 15 deletions lib/qm-dsp/base/SincWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ void
SincWindow::init()
{
if (m_length < 1) {
return;
return;
} else if (m_length < 2) {
m_window.push_back(1);
return;
m_window.push_back(1);
return;
} else {

int n0 = (m_length % 2 == 0 ? m_length/2 : (m_length - 1)/2);
int n1 = (m_length % 2 == 0 ? m_length/2 : (m_length + 1)/2);
double m = 2 * M_PI / m_p;
int n0 = (m_length % 2 == 0 ? m_length/2 : (m_length - 1)/2);
int n1 = (m_length % 2 == 0 ? m_length/2 : (m_length + 1)/2);
double m = 2 * M_PI / m_p;

for (int i = 0; i < n0; ++i) {
double x = ((m_length / 2) - i) * m;
m_window.push_back(sin(x) / x);
}
for (int i = 0; i < n0; ++i) {
double x = ((m_length / 2) - i) * m;
m_window.push_back(sin(x) / x);
}

m_window.push_back(1.0);
m_window.push_back(1.0);

for (int i = 1; i < n1; ++i) {
double x = i * m;
m_window.push_back(sin(x) / x);
}
for (int i = 1; i < n1; ++i) {
double x = i * m;
m_window.push_back(sin(x) / x);
}
}
}

16 changes: 8 additions & 8 deletions lib/qm-dsp/base/SincWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
COPYING included with this distribution for more information.
*/

#ifndef SINC_WINDOW_H
#define SINC_WINDOW_H
#ifndef QM_DSP_SINC_WINDOW_H
#define QM_DSP_SINC_WINDOW_H

#include <vector>

Expand All @@ -33,21 +33,21 @@ class SincWindow
SincWindow(int length, double p) : m_length(length), m_p(p) { init(); }

int getLength() const {
return m_length;
return m_length;
}

const double *getWindow() const {
return m_window.data();
return m_window.data();
}

void cut(double *src) const {
cut(src, src);
cut(src, src);
}

void cut(const double *src, double *dst) const {
for (int i = 0; i < m_length; ++i) {
dst[i] = src[i] * m_window[i];
}
for (int i = 0; i < m_length; ++i) {
dst[i] = src[i] * m_window[i];
}
}

private:
Expand Down
Loading

0 comments on commit 7b465cc

Please sign in to comment.