The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. JUCE demo code - use at your own risk!
  4. ==============================================================================
  5. */
  6. class SpectrogramComponent : public AudioAppComponent,
  7. private Timer
  8. {
  9. public:
  10. SpectrogramComponent()
  11. : forwardFFT (fftOrder, false),
  12. spectrogramImage (Image::RGB, 512, 512, true),
  13. fifoIndex (0),
  14. nextFFTBlockReady (false)
  15. {
  16. setOpaque (true);
  17. setAudioChannels (2, 0); // we want a couple of input channels but no outputs
  18. startTimerHz (60);
  19. setSize (700, 500);
  20. }
  21. ~SpectrogramComponent()
  22. {
  23. shutdownAudio();
  24. }
  25. //==============================================================================
  26. void prepareToPlay (int /*samplesPerBlockExpected*/, double /*newSampleRate*/) override
  27. {
  28. // (nothing to do here)
  29. }
  30. void releaseResources() override
  31. {
  32. // (nothing to do here)
  33. }
  34. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  35. {
  36. if (bufferToFill.buffer->getNumChannels() > 0)
  37. {
  38. const float* channelData = bufferToFill.buffer->getWritePointer (0, bufferToFill.startSample);
  39. for (int i = 0; i < bufferToFill.numSamples; ++i)
  40. pushNextSampleIntoFifo (channelData[i]);
  41. }
  42. }
  43. //==============================================================================
  44. void paint (Graphics& g) override
  45. {
  46. g.fillAll (Colours::black);
  47. g.setOpacity (1.0f);
  48. g.drawImage (spectrogramImage, getLocalBounds().toFloat());
  49. }
  50. void timerCallback() override
  51. {
  52. if (nextFFTBlockReady)
  53. {
  54. drawNextLineOfSpectrogram();
  55. nextFFTBlockReady = false;
  56. repaint();
  57. }
  58. }
  59. void pushNextSampleIntoFifo (float sample) noexcept
  60. {
  61. // if the fifo contains enough data, set a flag to say
  62. // that the next line should now be rendered..
  63. if (fifoIndex == fftSize)
  64. {
  65. if (! nextFFTBlockReady)
  66. {
  67. zeromem (fftData, sizeof (fftData));
  68. memcpy (fftData, fifo, sizeof (fifo));
  69. nextFFTBlockReady = true;
  70. }
  71. fifoIndex = 0;
  72. }
  73. fifo[fifoIndex++] = sample;
  74. }
  75. void drawNextLineOfSpectrogram()
  76. {
  77. const int rightHandEdge = spectrogramImage.getWidth() - 1;
  78. const int imageHeight = spectrogramImage.getHeight();
  79. // first, shuffle our image leftwards by 1 pixel..
  80. spectrogramImage.moveImageSection (0, 0, 1, 0, rightHandEdge, imageHeight);
  81. // then render our FFT data..
  82. forwardFFT.performFrequencyOnlyForwardTransform (fftData);
  83. // find the range of values produced, so we can scale our rendering to
  84. // show up the detail clearly
  85. Range<float> maxLevel = FloatVectorOperations::findMinAndMax (fftData, fftSize / 2);
  86. for (int y = 1; y < imageHeight; ++y)
  87. {
  88. const float skewedProportionY = 1.0f - std::exp (std::log (y / (float) imageHeight) * 0.2f);
  89. const int fftDataIndex = jlimit (0, fftSize / 2, (int) (skewedProportionY * fftSize / 2));
  90. const float level = jmap (fftData[fftDataIndex], 0.0f, maxLevel.getEnd(), 0.0f, 1.0f);
  91. spectrogramImage.setPixelAt (rightHandEdge, y, Colour::fromHSV (level, 1.0f, level, 1.0f));
  92. }
  93. }
  94. enum
  95. {
  96. fftOrder = 10,
  97. fftSize = 1 << fftOrder
  98. };
  99. private:
  100. FFT forwardFFT;
  101. Image spectrogramImage;
  102. float fifo [fftSize];
  103. float fftData [2 * fftSize];
  104. int fifoIndex;
  105. bool nextFFTBlockReady;
  106. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SpectrogramComponent)
  107. };