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.

135 lines
5.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 JUCE_AUDIOVISUALISER_H_INCLUDED
  18. #define JUCE_AUDIOVISUALISER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A simple component that can be used to show a scrolling waveform of audio data.
  22. This is a handy way to get a quick visualisation of some audio data. Just create
  23. one of these, set its size and oversampling rate, and then feed it with incoming
  24. data by calling one of its pushBuffer() or pushSample() methods.
  25. You can override its paint method for more customised views, but it's only designed
  26. as a quick-and-dirty class for simple tasks, so please don't send us feature requests
  27. for fancy additional features that you'd like it to support! If you're building a
  28. real-world app that requires more powerful waveform display, you'll probably want to
  29. create your own component instead.
  30. */
  31. class AudioVisualiserComponent : public Component,
  32. private Timer
  33. {
  34. public:
  35. /** Creates a visualiser with the given number of channels. */
  36. AudioVisualiserComponent (int initialNumChannels);
  37. /** Destructor. */
  38. ~AudioVisualiserComponent();
  39. /** Changes the number of channels that the visualiser stores. */
  40. void setNumChannels (int numChannels);
  41. /** Changes the number of samples that the visualiser keeps in its history.
  42. Note that this value refers to the number of averaged sample blocks, and each
  43. block is calculated as the peak of a number of incoming audio samples. To set
  44. the number of incoming samples per block, use setSamplesPerBlock().
  45. */
  46. void setBufferSize (int bufferSize);
  47. /** */
  48. void setSamplesPerBlock (int newNumInputSamplesPerBlock) noexcept;
  49. /** */
  50. int getSamplesPerBlock() const noexcept { return inputSamplesPerBlock; }
  51. /** Clears the contents of the buffers. */
  52. void clear();
  53. /** Pushes a buffer of channels data.
  54. The number of channels provided here is expected to match the number of channels
  55. that this AudioVisualiserComponent has been told to use.
  56. */
  57. void pushBuffer (const AudioSampleBuffer& bufferToPush);
  58. /** Pushes a buffer of channels data.
  59. The number of channels provided here is expected to match the number of channels
  60. that this AudioVisualiserComponent has been told to use.
  61. */
  62. void pushBuffer (const AudioSourceChannelInfo& bufferToPush);
  63. /** Pushes a buffer of channels data.
  64. The number of channels provided here is expected to match the number of channels
  65. that this AudioVisualiserComponent has been told to use.
  66. */
  67. void pushBuffer (const float** channelData, int numChannels, int numSamples);
  68. /** Pushes a single sample (per channel).
  69. The number of channels provided here is expected to match the number of channels
  70. that this AudioVisualiserComponent has been told to use.
  71. */
  72. void pushSample (const float* samplesForEachChannel, int numChannels);
  73. /** Sets the colours used to paint the */
  74. void setColours (Colour backgroundColour, Colour waveformColour) noexcept;
  75. /** Sets the frequency at which the component repaints itself. */
  76. void setRepaintRate (int frequencyInHz);
  77. /** Draws a channel of audio data in the given bounds.
  78. The default implementation just calls getChannelAsPath() and fits this into the given
  79. area. You may want to override this to draw things differently.
  80. */
  81. virtual void paintChannel (Graphics&, Rectangle<float> bounds,
  82. const Range<float>* levels, int numLevels, int nextSample);
  83. /** Creates a path which contains the waveform shape of a given set of range data.
  84. The path is normalised so that -1 and +1 are its upper and lower bounds, and it
  85. goes from 0 to numLevels on the X axis.
  86. */
  87. void getChannelAsPath (Path& result, const Range<float>* levels, int numLevels, int nextSample);
  88. //==========================================================================
  89. /** @internal */
  90. void paint (Graphics&) override;
  91. private:
  92. struct ChannelInfo;
  93. friend struct ChannelInfo;
  94. friend struct ContainerDeletePolicy<ChannelInfo>;
  95. OwnedArray<ChannelInfo> channels;
  96. int numSamples, inputSamplesPerBlock;
  97. Colour backgroundColour, waveformColour;
  98. void timerCallback() override;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioVisualiserComponent)
  100. };
  101. #endif // JUCE_AUDIOVISUALISER_H_INCLUDED