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 #include "PercussiveAudioCurve.h"
25 PercussiveAudioCurve::PercussiveAudioCurve(size_t sampleRate, size_t windowSize) :
26 AudioCurve(sampleRate, windowSize)
28 m_prevMag = new float[m_windowSize/2 + 1];
30 for (size_t i = 0; i <= m_windowSize/2; ++i) {
35 PercussiveAudioCurve::~PercussiveAudioCurve()
41 PercussiveAudioCurve::reset()
43 for (size_t i = 0; i <= m_windowSize/2; ++i) {
49 PercussiveAudioCurve::setWindowSize(size_t newSize)
51 m_windowSize = newSize;
54 m_prevMag = new float[m_windowSize/2 + 1];
60 PercussiveAudioCurve::process(const float *R__ mag, size_t /*increment*/)
62 static float threshold = powf(10.f, 0.15f); // 3dB rise in square of magnitude
63 static float zeroThresh = powf(10.f, -8);
66 size_t nonZeroCount = 0;
68 const int sz = m_windowSize / 2;
70 for (int n = 1; n <= sz; ++n) {
71 bool above = ((mag[n] / m_prevMag[n]) >= threshold);
73 if (mag[n] > zeroThresh) ++nonZeroCount;
76 for (int n = 1; n <= sz; ++n) {
77 m_prevMag[n] = mag[n];
80 if (nonZeroCount == 0) return 0;
81 else return float(count) / float(nonZeroCount);
85 PercussiveAudioCurve::processDouble(const double *R__ mag, size_t /*increment*/)
87 Profiler profiler("PercussiveAudioCurve::process");
89 static double threshold = pow(10.0, 0.15); // 3dB rise in square of magnitude
90 static double zeroThresh = pow(10.0, -8);
93 size_t nonZeroCount = 0;
95 const int sz = m_windowSize / 2;
97 for (int n = 1; n <= sz; ++n) {
98 bool above = ((mag[n] / m_prevMag[n]) >= threshold);
100 if (mag[n] > zeroThresh) ++nonZeroCount;
103 for (int n = 1; n <= sz; ++n) {
104 m_prevMag[n] = mag[n];
107 if (nonZeroCount == 0) return 0;
108 else return float(count) / float(nonZeroCount);