From ccf7ba303f7c0eff298c28e078b0ed075f1a75ea Mon Sep 17 00:00:00 2001 From: xenakios Date: Sun, 3 Dec 2017 22:51:57 +0200 Subject: [PATCH] SpectralVisualizer optimizations, create objects only when needed etc --- Source/PluginEditor.cpp | 57 +++++++++++++++++++++++++++-------------- Source/PluginEditor.h | 4 +++ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index 4d5b1fd..4588856 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -56,7 +56,7 @@ PaulstretchpluginAudioProcessorEditor::PaulstretchpluginAudioProcessorEditor (Pa m_wavecomponent.ShowFileCacheRange = true; startTimer(1, 100); startTimer(2, 1000); - startTimer(3, 500); + startTimer(3, 200); m_wavecomponent.startTimer(100); } @@ -446,48 +446,67 @@ SpectralVisualizer::SpectralVisualizer() void SpectralVisualizer::setState(ProcessParameters & pars, int nfreqs, double samplerate) { - m_img = Image(Image::RGB, getWidth(), getHeight(), true); - std::vector insamples(nfreqs*2); - std::vector freqs1(nfreqs*2); - std::vector freqs2(nfreqs*2); - std::vector freqs3(nfreqs*2); + double t0 = Time::getMillisecondCounterHiRes(); double hz = 440.0; int numharmonics = 40; double scaler = 1.0 / numharmonics; - for (int i = 0; i < nfreqs; ++i) + if (m_img.getWidth()!=getWidth() || m_img.getHeight()!=getHeight()) + m_img = Image(Image::RGB, getWidth(), getHeight(), true); + if (m_nfreqs == 0 || nfreqs != m_nfreqs) { - for (int j = 0; j < numharmonics; ++j) + m_nfreqs = nfreqs; + m_insamples = std::vector(nfreqs * 2); + m_freqs1 = std::vector(nfreqs); + m_freqs2 = std::vector(nfreqs); + m_freqs3 = std::vector(nfreqs); + m_fft = std::make_unique(nfreqs*2); + std::fill(m_insamples.begin(), m_insamples.end(), 0.0f); + for (int i = 0; i < nfreqs; ++i) { - double oscgain = 1.0 - (1.0 / numharmonics)*j; - insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz+hz*j)); + for (int j = 0; j < numharmonics; ++j) + { + double oscgain = 1.0 - (1.0 / numharmonics)*j; + m_insamples[i] += scaler * oscgain * sin(2 * 3.141592653 / samplerate * i* (hz + hz * j)); + } } } - FFT fft(nfreqs*2); + + //std::fill(m_freqs1.begin(), m_freqs1.end(), 0.0f); + //std::fill(m_freqs2.begin(), m_freqs2.end(), 0.0f); + //std::fill(m_freqs3.begin(), m_freqs3.end(), 0.0f); + //std::fill(m_fft->freq.begin(), m_fft->freq.end(), 0.0f); + + for (int i = 0; i < nfreqs; ++i) { - fft.smp[i] = insamples[i]; + m_fft->smp[i] = m_insamples[i]; } - fft.applywindow(W_HAMMING); - fft.smp2freq(); + m_fft->applywindow(W_HAMMING); + m_fft->smp2freq(); double ratio = pow(2.0f, pars.pitch_shift.cents / 1200.0f); - spectrum_do_pitch_shift(pars, nfreqs, fft.freq.data(), freqs2.data(), ratio); - spectrum_do_freq_shift(pars, nfreqs, samplerate, freqs2.data(), freqs1.data()); - spectrum_do_compressor(pars, nfreqs, freqs1.data(), freqs2.data()); - spectrum_spread(nfreqs, samplerate, freqs3, freqs2.data(), freqs1.data(), pars.spread.bandwidth); + spectrum_do_pitch_shift(pars, nfreqs, m_fft->freq.data(), m_freqs2.data(), ratio); + spectrum_do_freq_shift(pars, nfreqs, samplerate, m_freqs2.data(), m_freqs1.data()); + spectrum_do_compressor(pars, nfreqs, m_freqs1.data(), m_freqs2.data()); + spectrum_spread(nfreqs, samplerate, m_freqs3, m_freqs2.data(), m_freqs1.data(), pars.spread.bandwidth); Graphics g(m_img); + g.fillAll(Colours::black); g.setColour(Colours::white); for (int i = 0; i < nfreqs; ++i) { double binfreq = (samplerate / 2 / nfreqs)*i; double xcor = jmap(binfreq, 0.0, samplerate / 2.0, 0.0, getWidth()); - double ycor = getHeight()- jmap(freqs1[i], 0.0, nfreqs/64, 0.0, getHeight()); + double ycor = getHeight()- jmap(m_freqs1[i], 0.0, nfreqs/64, 0.0, getHeight()); ycor = jlimit(0.0, getHeight(), ycor); g.drawLine(xcor, getHeight(), xcor, ycor, 1.0); } + double t1 = Time::getMillisecondCounterHiRes(); + m_elapsed = t1 - t0; repaint(); } void SpectralVisualizer::paint(Graphics & g) { g.drawImage(m_img, 0, 0, getWidth(), getHeight(), 0, 0, m_img.getWidth(), m_img.getHeight()); + g.setColour(Colours::yellow); + g.drawText(String(m_elapsed, 1)+" ms", 1, 1, getWidth(), 30, Justification::topLeft); } diff --git a/Source/PluginEditor.h b/Source/PluginEditor.h index 8c07a4f..420573e 100644 --- a/Source/PluginEditor.h +++ b/Source/PluginEditor.h @@ -24,6 +24,10 @@ public: private: Image m_img; + std::vector m_insamples,m_freqs1, m_freqs2, m_freqs3; + std::unique_ptr m_fft; + int m_nfreqs = 0; + double m_elapsed = 0.0; }; inline void attachCallback(Button& button, std::function callback)