Mercurial > hg > devuvuzelator
view devuvuzelator.cpp @ 1:0d2126c32309
* split out core code, fix some things
author | Chris Cannam |
---|---|
date | Fri, 11 Jun 2010 10:31:29 +0100 |
parents | fe4c331213c5 |
children | e621e794011f |
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ void Devuvuzelator::processSpectralFrame() { const int hs = m_fftsize/2 + 1; double *mags = (double *)alloca(hs * sizeof(double)); double *ratios = (double *)alloca(hs * sizeof(double)); for (int i = 0; i < hs; ++i) { ratios[i] = 1.0; mags[i] = sqrt(m_real[i] * m_real[i] + m_imag[i] * m_imag[i]); } double low = -35; double high = -20; if (m_low) low = *m_low; if (m_high) high = *m_high; int harmonics = 3; if (m_harmonics) harmonics = int(*m_harmonics + 0.5); double fun = 200; if (m_fundamental) fun = *m_fundamental; double bw = 40; if (m_bandwidth) bw = *m_bandwidth; double lowfun = fun - bw/2; double highfun = fun + bw/2; double reduction = 10; if (m_reduction) reduction = *m_reduction; for (int h = 1; h <= 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; // std::cerr << "partials " << h << ": freqs " << lowfreq << "->" // << highfreq << ", bins " << lowbin << "->" << highbin << std::endl; for (int i = lowbin; i <= highbin; ++i) { // std::cerr << "bin " << i << " freq " << (m_sampleRate * i) / m_fftsize << std::endl; ratios[i] = 1.0; double db = 10 * log10(mags[i]); if (db > low && db < high) { double r = reduction; ratios[i] = pow(10, -r / 10); } } } /* for (int i = 0; i < hs-1; ++i) { if (ratios[i] == 1.0 && ratios[i+1] < 1.0) { ratios[i] = (ratios[i+1] + 1) / 2; } else if (ratios[i] < 1.0 && ratios[i+1] == 1.0) { ratios[i+1] = (ratios[i] + 1) / 2; ++i; } } */ for (int i = 0; i < hs; ++i) { m_real[i] *= ratios[i]; m_imag[i] *= ratios[i]; } }