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.

184 lines
6.0KB

  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. #ifdef JUCE_DEMO_RUNNER
  42. AudioAppComponent (getSharedAudioDeviceManager (1, 0)),
  43. #endif
  44. forwardFFT (fftOrder),
  45. spectrogramImage (Image::RGB, 512, 512, true)
  46. {
  47. setOpaque (true);
  48. auto audioDevice = deviceManager.getCurrentAudioDevice();
  49. auto numInputChannels = jmax (audioDevice != nullptr ? audioDevice->getActiveInputChannels() .countNumberOfSetBits() : 1, 1);
  50. auto numOutputChannels = audioDevice != nullptr ? audioDevice->getActiveOutputChannels().countNumberOfSetBits() : 2;
  51. // Specify the number of input and output channels that we want to open
  52. setAudioChannels (numInputChannels, numOutputChannels);
  53. startTimerHz (60);
  54. setSize (700, 500);
  55. }
  56. ~SimpleFFTDemo()
  57. {
  58. shutdownAudio();
  59. }
  60. //==============================================================================
  61. void prepareToPlay (int /*samplesPerBlockExpected*/, double /*newSampleRate*/) override
  62. {
  63. // (nothing to do here)
  64. }
  65. void releaseResources() override
  66. {
  67. // (nothing to do here)
  68. }
  69. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
  70. {
  71. if (bufferToFill.buffer->getNumChannels() > 0)
  72. {
  73. const auto* channelData = bufferToFill.buffer->getReadPointer (0, bufferToFill.startSample);
  74. for (auto i = 0; i < bufferToFill.numSamples; ++i)
  75. pushNextSampleIntoFifo (channelData[i]);
  76. bufferToFill.clearActiveBufferRegion();
  77. }
  78. }
  79. //==============================================================================
  80. void paint (Graphics& g) override
  81. {
  82. g.fillAll (Colours::black);
  83. g.setOpacity (1.0f);
  84. g.drawImage (spectrogramImage, getLocalBounds().toFloat());
  85. }
  86. void timerCallback() override
  87. {
  88. if (nextFFTBlockReady)
  89. {
  90. drawNextLineOfSpectrogram();
  91. nextFFTBlockReady = false;
  92. repaint();
  93. }
  94. }
  95. void pushNextSampleIntoFifo (float sample) noexcept
  96. {
  97. // if the fifo contains enough data, set a flag to say
  98. // that the next line should now be rendered..
  99. if (fifoIndex == fftSize)
  100. {
  101. if (! nextFFTBlockReady)
  102. {
  103. zeromem (fftData, sizeof (fftData));
  104. memcpy (fftData, fifo, sizeof (fifo));
  105. nextFFTBlockReady = true;
  106. }
  107. fifoIndex = 0;
  108. }
  109. fifo[fifoIndex++] = sample;
  110. }
  111. void drawNextLineOfSpectrogram()
  112. {
  113. auto rightHandEdge = spectrogramImage.getWidth() - 1;
  114. auto imageHeight = spectrogramImage.getHeight();
  115. // first, shuffle our image leftwards by 1 pixel..
  116. spectrogramImage.moveImageSection (0, 0, 1, 0, rightHandEdge, imageHeight);
  117. // then render our FFT data..
  118. forwardFFT.performFrequencyOnlyForwardTransform (fftData);
  119. // find the range of values produced, so we can scale our rendering to
  120. // show up the detail clearly
  121. auto maxLevel = FloatVectorOperations::findMinAndMax (fftData, fftSize / 2);
  122. for (auto y = 1; y < imageHeight; ++y)
  123. {
  124. auto skewedProportionY = 1.0f - std::exp (std::log (y / (float) imageHeight) * 0.2f);
  125. auto fftDataIndex = jlimit (0, fftSize / 2, (int) (skewedProportionY * fftSize / 2));
  126. auto level = jmap (fftData[fftDataIndex], 0.0f, jmax (maxLevel.getEnd(), 1e-5f), 0.0f, 1.0f);
  127. spectrogramImage.setPixelAt (rightHandEdge, y, Colour::fromHSV (level, 1.0f, level, 1.0f));
  128. }
  129. }
  130. enum
  131. {
  132. fftOrder = 10,
  133. fftSize = 1 << fftOrder
  134. };
  135. private:
  136. dsp::FFT forwardFFT;
  137. Image spectrogramImage;
  138. float fifo [fftSize];
  139. float fftData [2 * fftSize];
  140. int fifoIndex = 0;
  141. bool nextFFTBlockReady = false;
  142. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleFFTDemo)
  143. };