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.

127 lines
5.0KB

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