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.

134 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A simple component that can be used to show a scrolling waveform of audio data.
  23. This is a handy way to get a quick visualisation of some audio data. Just create
  24. one of these, set its size and oversampling rate, and then feed it with incoming
  25. data by calling one of its pushBuffer() or pushSample() methods.
  26. You can override its paint method for more customised views, but it's only designed
  27. as a quick-and-dirty class for simple tasks, so please don't send us feature requests
  28. for fancy additional features that you'd like it to support! If you're building a
  29. real-world app that requires more powerful waveform display, you'll probably want to
  30. create your own component instead.
  31. @tags{Audio}
  32. */
  33. class JUCE_API AudioVisualiserComponent : public Component,
  34. private Timer
  35. {
  36. public:
  37. /** Creates a visualiser with the given number of channels. */
  38. AudioVisualiserComponent (int initialNumChannels);
  39. /** Destructor. */
  40. ~AudioVisualiserComponent() override;
  41. /** Changes the number of channels that the visualiser stores. */
  42. void setNumChannels (int numChannels);
  43. /** Changes the number of samples that the visualiser keeps in its history.
  44. Note that this value refers to the number of averaged sample blocks, and each
  45. block is calculated as the peak of a number of incoming audio samples. To set
  46. the number of incoming samples per block, use setSamplesPerBlock().
  47. */
  48. void setBufferSize (int bufferSize);
  49. /** */
  50. void setSamplesPerBlock (int newNumInputSamplesPerBlock) noexcept;
  51. /** */
  52. int getSamplesPerBlock() const noexcept { return inputSamplesPerBlock; }
  53. /** Clears the contents of the buffers. */
  54. void clear();
  55. /** Pushes a buffer of channels data.
  56. The number of channels provided here is expected to match the number of channels
  57. that this AudioVisualiserComponent has been told to use.
  58. */
  59. void pushBuffer (const AudioBuffer<float>& bufferToPush);
  60. /** Pushes a buffer of channels data.
  61. The number of channels provided here is expected to match the number of channels
  62. that this AudioVisualiserComponent has been told to use.
  63. */
  64. void pushBuffer (const AudioSourceChannelInfo& bufferToPush);
  65. /** Pushes a buffer of channels data.
  66. The number of channels provided here is expected to match the number of channels
  67. that this AudioVisualiserComponent has been told to use.
  68. */
  69. void pushBuffer (const float** channelData, int numChannels, int numSamples);
  70. /** Pushes a single sample (per channel).
  71. The number of channels provided here is expected to match the number of channels
  72. that this AudioVisualiserComponent has been told to use.
  73. */
  74. void pushSample (const float* samplesForEachChannel, int numChannels);
  75. /** Sets the colours used to paint the */
  76. void setColours (Colour backgroundColour, Colour waveformColour) noexcept;
  77. /** Sets the frequency at which the component repaints itself. */
  78. void setRepaintRate (int frequencyInHz);
  79. /** Draws a channel of audio data in the given bounds.
  80. The default implementation just calls getChannelAsPath() and fits this into the given
  81. area. You may want to override this to draw things differently.
  82. */
  83. virtual void paintChannel (Graphics&, Rectangle<float> bounds,
  84. const Range<float>* levels, int numLevels, int nextSample);
  85. /** Creates a path which contains the waveform shape of a given set of range data.
  86. The path is normalised so that -1 and +1 are its upper and lower bounds, and it
  87. goes from 0 to numLevels on the X axis.
  88. */
  89. void getChannelAsPath (Path& result, const Range<float>* levels, int numLevels, int nextSample);
  90. //==============================================================================
  91. /** @internal */
  92. void paint (Graphics&) override;
  93. private:
  94. struct 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. } // namespace juce