summaryrefslogtreecommitdiff
path: root/libs/rubberband/src/PercussiveAudioCurve.cpp
blob: aced9e08c27623f456dce0c0606b1ea4160f2d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Rubber Band
    An audio time-stretching and pitch-shifting library.
    Copyright 2007-2008 Chris Cannam.
    
    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.
*/

#include "PercussiveAudioCurve.h"

#include "Profiler.h"

#include <cmath>


namespace RubberBand
{

PercussiveAudioCurve::PercussiveAudioCurve(size_t sampleRate, size_t windowSize) :
    AudioCurve(sampleRate, windowSize)
{
    m_prevMag = new float[m_windowSize/2 + 1];

    for (size_t i = 0; i <= m_windowSize/2; ++i) {
        m_prevMag[i] = 0.f;
    }
}

PercussiveAudioCurve::~PercussiveAudioCurve()
{
    delete[] m_prevMag;
}

void
PercussiveAudioCurve::reset()
{
    for (size_t i = 0; i <= m_windowSize/2; ++i) {
        m_prevMag[i] = 0;
    }
}

void
PercussiveAudioCurve::setWindowSize(size_t newSize)
{
    m_windowSize = newSize;

    delete[] m_prevMag;
    m_prevMag = new float[m_windowSize/2 + 1];

    reset();
}

float
PercussiveAudioCurve::process(const float *R__ mag, size_t /*increment*/)
{
    static float threshold = powf(10.f, 0.15f); // 3dB rise in square of magnitude
    static float zeroThresh = powf(10.f, -8);

    size_t count = 0;
    size_t nonZeroCount = 0;

    const int sz = m_windowSize / 2;

    for (int n = 1; n <= sz; ++n) {
        bool above = ((mag[n] / m_prevMag[n]) >= threshold);
        if (above) ++count;
        if (mag[n] > zeroThresh) ++nonZeroCount;
    }

    for (int n = 1; n <= sz; ++n) {
	m_prevMag[n] = mag[n];
    }

    if (nonZeroCount == 0) return 0;
    else return float(count) / float(nonZeroCount);
}

float
PercussiveAudioCurve::processDouble(const double *R__ mag, size_t /*increment*/)
{
    Profiler profiler("PercussiveAudioCurve::process");

    static double threshold = pow(10.0, 0.15); // 3dB rise in square of magnitude
    static double zeroThresh = pow(10.0, -8);

    size_t count = 0;
    size_t nonZeroCount = 0;

    const int sz = m_windowSize / 2;

    for (int n = 1; n <= sz; ++n) {
        bool above = ((mag[n] / m_prevMag[n]) >= threshold);
        if (above) ++count;
        if (mag[n] > zeroThresh) ++nonZeroCount;
    }

    for (int n = 1; n <= sz; ++n) {
	m_prevMag[n] = mag[n];
    }

    if (nonZeroCount == 0) return 0;
    else return float(count) / float(nonZeroCount);
}

}