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.

142 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef __AUDIOLIVESCROLLINGDISPLAY_H_4C3BD3A7__
  18. #define __AUDIOLIVESCROLLINGDISPLAY_H_4C3BD3A7__
  19. #include "../JuceDemoHeader.h"
  20. //==============================================================================
  21. /* This component scrolls a continuous waveform showing the audio that's
  22. coming into whatever audio inputs this object is connected to.
  23. */
  24. class LiveScrollingAudioDisplay : public Component,
  25. public AudioIODeviceCallback,
  26. private Timer
  27. {
  28. public:
  29. LiveScrollingAudioDisplay()
  30. : nextSample (0), subSample (0), accumulator (0)
  31. {
  32. setOpaque (true);
  33. clear();
  34. startTimerHz (75); // use a timer to keep repainting this component
  35. }
  36. //==============================================================================
  37. void audioDeviceAboutToStart (AudioIODevice*) override
  38. {
  39. clear();
  40. }
  41. void audioDeviceStopped() override
  42. {
  43. clear();
  44. }
  45. void audioDeviceIOCallback (const float** inputChannelData, int numInputChannels,
  46. float** outputChannelData, int numOutputChannels,
  47. int numSamples) override
  48. {
  49. for (int i = 0; i < numSamples; ++i)
  50. {
  51. float inputSample = 0;
  52. for (int chan = 0; chan < numInputChannels; ++chan)
  53. if (inputChannelData[chan] != nullptr)
  54. inputSample += std::abs (inputChannelData[chan][i]); // find the sum of all the channels
  55. pushSample (10.0f * inputSample); // boost the level to make it more easily visible.
  56. }
  57. // We need to clear the output buffers before returning, in case they're full of junk..
  58. for (int j = 0; j < numOutputChannels; ++j)
  59. if (outputChannelData[j] != nullptr)
  60. zeromem (outputChannelData[j], sizeof (float) * (size_t) numSamples);
  61. }
  62. private:
  63. float samples[1024];
  64. int nextSample, subSample;
  65. float accumulator;
  66. void clear()
  67. {
  68. zeromem (samples, sizeof (samples));
  69. accumulator = 0;
  70. subSample = 0;
  71. }
  72. void paint (Graphics& g) override
  73. {
  74. g.fillAll (Colours::black);
  75. const float midY = getHeight() * 0.5f;
  76. int samplesAgo = (nextSample + numElementsInArray (samples) - 1);
  77. RectangleList<float> waveform;
  78. waveform.ensureStorageAllocated ((int) numElementsInArray (samples));
  79. for (int x = jmin (getWidth(), (int) numElementsInArray (samples)); --x >= 0;)
  80. {
  81. const float sampleSize = midY * samples [samplesAgo-- % numElementsInArray (samples)];
  82. waveform.addWithoutMerging (Rectangle<float> ((float) x, midY - sampleSize, 1.0f, sampleSize * 2.0f));
  83. }
  84. g.setColour (Colours::lightgreen);
  85. g.fillRectList (waveform);
  86. }
  87. void timerCallback() override
  88. {
  89. repaint();
  90. }
  91. void pushSample (const float newSample)
  92. {
  93. accumulator += newSample;
  94. if (subSample == 0)
  95. {
  96. const int inputSamplesPerPixel = 200;
  97. samples[nextSample] = accumulator / inputSamplesPerPixel;
  98. nextSample = (nextSample + 1) % numElementsInArray (samples);
  99. subSample = inputSamplesPerPixel;
  100. accumulator = 0;
  101. }
  102. else
  103. {
  104. --subSample;
  105. }
  106. }
  107. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LiveScrollingAudioDisplay);
  108. };
  109. #endif // __AUDIOLIVESCROLLINGDISPLAY_H_4C3BD3A7__