Displaying spectrogram data in plugin

I know this may be a trivial question, but I’m trying to implement a spectrogram display in my plugin. I’ve gone through the tutorial of creating a spectrogram with the FFT transform in JUCE, and I thought it would be straightforward to just addAndMakeVisible to the PluginEditor since the class extends an AudioAppComponent.

The black background shows up in my UI, but the actual spectrogram data doesn’t seem to be registering. I was wondering how I would go about providing the audio data that is running through my processBlock callback in PluginProcessor into the getNextAudioBlock callback of my spectrogram component.

Thanks for any suggestions.

You may need to separate that exmaple into a backend part and a front end part cause you are developing a plugin instead of an app. BTW, running FFT on the message thread might be a bad idea for a plugin. Instead, you may create a background thread to run the FFT. A good example is here:

In the AudioProcessor, you should maintain a buffer updated with the audio data so that it can be periodically read from a timer in the editor to generate the spectrogram.

But you have to control that it’s not read and written simultaneously. A double buffer could be used so that one is used for writing and the other for reading alternately, but it’s better to use a circular buffer since the spectrogram doesn’t have to wait for the buffer to be filled. Instead, it will always read the last block of FFT size, regardless of the amount of updated audio data.

The AudioSourceChannelInfo has a constructor that takes an AudioBuffer, so it is as easy as putting squiggly braces around the buffer:

// juce::AudioBuffer<float>& buffer from call

spectrogram->getNextAudioBlock ({buffer});

// or explicitly:
juce::AudioSourceChannelInfo info (buffer);
spectrogram->getNextAudioSource (info);

You see, that AudioSource is not necessarily the right choice, but you can use it. It’s not a source but a sink after all :wink:

The main problm about how to make sure the processBlock doesn’t call into the editor was covered by the other answers. Let us know if you have questions about that.