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.

172 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  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, vs2017
  29. type: Component
  30. mainClass: SimpleFFTDemo
  31. useLocalCopy: 1
  32. END_JUCE_PIP_METADATA
  33. *******************************************************************************/
  34. #pragma once
  35. //==============================================================================
  36. class SimpleFFTDemo : public AudioAppComponent,
  37. private Timer
  38. {
  39. public:
  40. SimpleFFTDemo()
  41. : forwardFFT (fftOrder),
  42. spectrogramImage (Image::RGB, 512, 512, true)
  43. {
  44. setOpaque (true);
  45. setAudioChannels (2, 0); // we want a couple of input channels but no outputs
  46. startTimerHz (60);
  47. setSize (700, 500);
  48. }
  49. ~SimpleFFTDemo()
  50. {
  51. shutdownAudio();
  52. }
  53. //==============================================================================
  54. void prepareToPlay (int /*samplesPerBlockExpected*/, double /*newSampleRate*/) override
  55. {
  56. // (nothing to do here)
  57. }
  58. void releaseResources() override
  59. {
  60. // (nothing to do here)
  61. }
  62. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  63. {
  64. if (bufferToFill.buffer->getNumChannels() > 0)
  65. {
  66. auto* channelData = bufferToFill.buffer->getWritePointer (0, bufferToFill.startSample);
  67. for (auto i = 0; i < bufferToFill.numSamples; ++i)
  68. pushNextSampleIntoFifo (channelData[i]);
  69. }
  70. }
  71. //==============================================================================
  72. void paint (Graphics& g) override
  73. {
  74. g.fillAll (Colours::black);
  75. g.setOpacity (1.0f);
  76. g.drawImage (spectrogramImage, getLocalBounds().toFloat());
  77. }
  78. void timerCallback() override
  79. {
  80. if (nextFFTBlockReady)
  81. {
  82. drawNextLineOfSpectrogram();
  83. nextFFTBlockReady = false;
  84. repaint();
  85. }
  86. }
  87. void pushNextSampleIntoFifo (float sample) noexcept
  88. {
  89. // if the fifo contains enough data, set a flag to say
  90. // that the next line should now be rendered..
  91. if (fifoIndex == fftSize)
  92. {
  93. if (! nextFFTBlockReady)
  94. {
  95. zeromem (fftData, sizeof (fftData));
  96. memcpy (fftData, fifo, sizeof (fifo));
  97. nextFFTBlockReady = true;
  98. }
  99. fifoIndex = 0;
  100. }
  101. fifo[fifoIndex++] = sample;
  102. }
  103. void drawNextLineOfSpectrogram()
  104. {
  105. auto rightHandEdge = spectrogramImage.getWidth() - 1;
  106. auto imageHeight = spectrogramImage.getHeight();
  107. // first, shuffle our image leftwards by 1 pixel..
  108. spectrogramImage.moveImageSection (0, 0, 1, 0, rightHandEdge, imageHeight);
  109. // then render our FFT data..
  110. forwardFFT.performFrequencyOnlyForwardTransform (fftData);
  111. // find the range of values produced, so we can scale our rendering to
  112. // show up the detail clearly
  113. auto maxLevel = FloatVectorOperations::findMinAndMax (fftData, fftSize / 2);
  114. for (auto y = 1; y < imageHeight; ++y)
  115. {
  116. auto skewedProportionY = 1.0f - std::exp (std::log (y / (float) imageHeight) * 0.2f);
  117. auto fftDataIndex = jlimit (0, fftSize / 2, (int) (skewedProportionY * fftSize / 2));
  118. auto level = jmap (fftData[fftDataIndex], 0.0f, jmax (maxLevel.getEnd(), 1e-5f), 0.0f, 1.0f);
  119. spectrogramImage.setPixelAt (rightHandEdge, y, Colour::fromHSV (level, 1.0f, level, 1.0f));
  120. }
  121. }
  122. enum
  123. {
  124. fftOrder = 10,
  125. fftSize = 1 << fftOrder
  126. };
  127. private:
  128. dsp::FFT forwardFFT;
  129. Image spectrogramImage;
  130. float fifo [fftSize];
  131. float fftData [2 * fftSize];
  132. int fifoIndex = 0;
  133. bool nextFFTBlockReady = false;
  134. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleFFTDemo)
  135. };