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.

190 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: SimpleFFTDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Simple FFT application.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_processors, juce_audio_utils, juce_core,
  26. juce_data_structures, juce_dsp, juce_events, juce_graphics,
  27. juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022, linux_make, androidstudio, xcode_iphone
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: SimpleFFTDemo
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. //==============================================================================
  37. class SimpleFFTDemo final : public AudioAppComponent,
  38. private Timer
  39. {
  40. public:
  41. SimpleFFTDemo() :
  42. #ifdef JUCE_DEMO_RUNNER
  43. AudioAppComponent (getSharedAudioDeviceManager (1, 0)),
  44. #endif
  45. forwardFFT (fftOrder),
  46. spectrogramImage (Image::RGB, 512, 512, true)
  47. {
  48. setOpaque (true);
  49. #ifndef JUCE_DEMO_RUNNER
  50. RuntimePermissions::request (RuntimePermissions::recordAudio,
  51. [this] (bool granted)
  52. {
  53. int numInputChannels = granted ? 2 : 0;
  54. setAudioChannels (numInputChannels, 2);
  55. });
  56. #else
  57. setAudioChannels (2, 2);
  58. #endif
  59. startTimerHz (60);
  60. setSize (700, 500);
  61. }
  62. ~SimpleFFTDemo() override
  63. {
  64. shutdownAudio();
  65. }
  66. //==============================================================================
  67. void prepareToPlay (int /*samplesPerBlockExpected*/, double /*newSampleRate*/) override
  68. {
  69. // (nothing to do here)
  70. }
  71. void releaseResources() override
  72. {
  73. // (nothing to do here)
  74. }
  75. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  76. {
  77. if (bufferToFill.buffer->getNumChannels() > 0)
  78. {
  79. const auto* channelData = bufferToFill.buffer->getReadPointer (0, bufferToFill.startSample);
  80. for (auto i = 0; i < bufferToFill.numSamples; ++i)
  81. pushNextSampleIntoFifo (channelData[i]);
  82. bufferToFill.clearActiveBufferRegion();
  83. }
  84. }
  85. //==============================================================================
  86. void paint (Graphics& g) override
  87. {
  88. g.fillAll (Colours::black);
  89. g.setOpacity (1.0f);
  90. g.drawImage (spectrogramImage, getLocalBounds().toFloat());
  91. }
  92. void timerCallback() override
  93. {
  94. if (nextFFTBlockReady)
  95. {
  96. drawNextLineOfSpectrogram();
  97. nextFFTBlockReady = false;
  98. repaint();
  99. }
  100. }
  101. void pushNextSampleIntoFifo (float sample) noexcept
  102. {
  103. // if the fifo contains enough data, set a flag to say
  104. // that the next line should now be rendered..
  105. if (fifoIndex == fftSize)
  106. {
  107. if (! nextFFTBlockReady)
  108. {
  109. zeromem (fftData, sizeof (fftData));
  110. memcpy (fftData, fifo, sizeof (fifo));
  111. nextFFTBlockReady = true;
  112. }
  113. fifoIndex = 0;
  114. }
  115. fifo[fifoIndex++] = sample;
  116. }
  117. void drawNextLineOfSpectrogram()
  118. {
  119. auto rightHandEdge = spectrogramImage.getWidth() - 1;
  120. auto imageHeight = spectrogramImage.getHeight();
  121. // first, shuffle our image leftwards by 1 pixel..
  122. spectrogramImage.moveImageSection (0, 0, 1, 0, rightHandEdge, imageHeight);
  123. // then render our FFT data..
  124. forwardFFT.performFrequencyOnlyForwardTransform (fftData);
  125. // find the range of values produced, so we can scale our rendering to
  126. // show up the detail clearly
  127. auto maxLevel = FloatVectorOperations::findMinAndMax (fftData, fftSize / 2);
  128. for (auto y = 1; y < imageHeight; ++y)
  129. {
  130. auto skewedProportionY = 1.0f - std::exp (std::log ((float) y / (float) imageHeight) * 0.2f);
  131. auto fftDataIndex = jlimit (0, fftSize / 2, (int) (skewedProportionY * (int) fftSize / 2));
  132. auto level = jmap (fftData[fftDataIndex], 0.0f, jmax (maxLevel.getEnd(), 1e-5f), 0.0f, 1.0f);
  133. spectrogramImage.setPixelAt (rightHandEdge, y, Colour::fromHSV (level, 1.0f, level, 1.0f));
  134. }
  135. }
  136. enum
  137. {
  138. fftOrder = 10,
  139. fftSize = 1 << fftOrder
  140. };
  141. private:
  142. dsp::FFT forwardFFT;
  143. Image spectrogramImage;
  144. float fifo [fftSize];
  145. float fftData [2 * fftSize];
  146. int fifoIndex = 0;
  147. bool nextFFTBlockReady = false;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleFFTDemo)
  149. };