view devuvuzelator.cpp @ 10:25580443645c

* Images
author Chris Cannam
date Mon, 14 Jun 2010 09:25:01 +0100
parents a1539d4e3b08
children 366f678dea8c
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

#ifdef VST
#include "devuvuzelator-vst.h"
#else
#ifdef LADSPA
#include "devuvuzelator-ladspa.h"
#else
#error Need to define either VST or LADSPA
#endif
#endif

void
Devuvuzelator::processSpectralFrame()
{
    const int hs = m_fftsize/2 + 1;
    double *mags = (double *)alloca(hs * sizeof(double));
    for (int i = 0; i < hs; ++i) {
        mags[i] = sqrt(m_real[i] * m_real[i] + m_imag[i] * m_imag[i]);
    }

    double lowfun = m_fundamental - m_bandwidth/2;
    double highfun = m_fundamental + m_bandwidth/2;

    int ix = 0;

    for (int h = 1; h <= m_harmonics; ++h) {

        double lowfreq = lowfun * h;
        double highfreq = highfun * h;

        int lowbin = 0.5 + (m_fftsize * lowfreq) / m_sampleRate;
        int highbin = 0.5 + (m_fftsize * highfreq) / m_sampleRate;

        for (int i = lowbin; i <= highbin; ++i) {

            if (!m_medians[i]) {
                // allocation in RT context, sorry! but this happens only
                // the first time through
                int filterlen = (m_filtersecs * m_sampleRate) / m_increment;
                m_medians[i] = new MedianFilter<double>(filterlen);
            }

            m_medians[i]->push(mags[i]);
            double threshold = m_medians[i]->getAt(m_reduction);

//            if (ix == 2) {
                std::cerr << "at freq " << (float(i) * m_sampleRate) / m_fftsize
                          << ", threshold = " << threshold << std::endl;
//            }

//            if (ix == 2) {
                std::cerr << "changing from (" << m_real[i] << ","
                          << m_imag[i] << ") to ";
//            }

            if (mags[i] > threshold && mags[i] > 0.0) {
                double target = mags[i] - threshold;
                double ratio = (target / mags[i]);
                m_real[i] *= ratio;
                m_imag[i] *= ratio;
            } else {
                m_real[i] = 0.0;
                m_imag[i] = 0.0;
            }

//            if (ix == 2) {
                std::cerr << "(" << m_real[i] << ","
                          << m_imag[i] << ")" << std::endl;
//            }

            ++ix;
        }
    }
}