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.

216 lines
6.1KB

  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. struct AudioVisualiserComponent::ChannelInfo
  16. {
  17. ChannelInfo (AudioVisualiserComponent& o, int bufferSize) : owner (o)
  18. {
  19. setBufferSize (bufferSize);
  20. clear();
  21. }
  22. void clear() noexcept
  23. {
  24. levels.fill ({});
  25. value = {};
  26. subSample = 0;
  27. }
  28. void pushSamples (const float* inputSamples, int num) noexcept
  29. {
  30. for (int i = 0; i < num; ++i)
  31. pushSample (inputSamples[i]);
  32. }
  33. void pushSample (float newSample) noexcept
  34. {
  35. if (--subSample <= 0)
  36. {
  37. if (++nextSample == levels.size())
  38. nextSample = 0;
  39. levels.getReference (nextSample) = value;
  40. subSample = owner.getSamplesPerBlock();
  41. value = Range<float> (newSample, newSample);
  42. }
  43. else
  44. {
  45. value = value.getUnionWith (newSample);
  46. }
  47. }
  48. void setBufferSize (int newSize)
  49. {
  50. levels.removeRange (newSize, levels.size());
  51. levels.insertMultiple (-1, {}, newSize - levels.size());
  52. if (nextSample >= newSize)
  53. nextSample = 0;
  54. }
  55. AudioVisualiserComponent& owner;
  56. Array<Range<float>> levels;
  57. Range<float> value;
  58. std::atomic<int> nextSample { 0 }, subSample { 0 };
  59. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelInfo)
  60. };
  61. //==============================================================================
  62. AudioVisualiserComponent::AudioVisualiserComponent (int initialNumChannels)
  63. : numSamples (1024),
  64. inputSamplesPerBlock (256),
  65. backgroundColour (Colours::black),
  66. waveformColour (Colours::white)
  67. {
  68. setOpaque (true);
  69. setNumChannels (initialNumChannels);
  70. setRepaintRate (60);
  71. }
  72. AudioVisualiserComponent::~AudioVisualiserComponent()
  73. {
  74. }
  75. void AudioVisualiserComponent::setNumChannels (int numChannels)
  76. {
  77. channels.clear();
  78. for (int i = 0; i < numChannels; ++i)
  79. channels.add (new ChannelInfo (*this, numSamples));
  80. }
  81. void AudioVisualiserComponent::setBufferSize (int newNumSamples)
  82. {
  83. numSamples = newNumSamples;
  84. for (auto* c : channels)
  85. c->setBufferSize (newNumSamples);
  86. }
  87. void AudioVisualiserComponent::clear()
  88. {
  89. for (auto* c : channels)
  90. c->clear();
  91. }
  92. void AudioVisualiserComponent::pushBuffer (const float** d, int numChannels, int num)
  93. {
  94. numChannels = jmin (numChannels, channels.size());
  95. for (int i = 0; i < numChannels; ++i)
  96. channels.getUnchecked(i)->pushSamples (d[i], num);
  97. }
  98. void AudioVisualiserComponent::pushBuffer (const AudioBuffer<float>& buffer)
  99. {
  100. pushBuffer (buffer.getArrayOfReadPointers(),
  101. buffer.getNumChannels(),
  102. buffer.getNumSamples());
  103. }
  104. void AudioVisualiserComponent::pushBuffer (const AudioSourceChannelInfo& buffer)
  105. {
  106. auto numChannels = jmin (buffer.buffer->getNumChannels(), channels.size());
  107. for (int i = 0; i < numChannels; ++i)
  108. channels.getUnchecked(i)->pushSamples (buffer.buffer->getReadPointer (i, buffer.startSample),
  109. buffer.numSamples);
  110. }
  111. void AudioVisualiserComponent::pushSample (const float* d, int numChannels)
  112. {
  113. numChannels = jmin (numChannels, channels.size());
  114. for (int i = 0; i < numChannels; ++i)
  115. channels.getUnchecked(i)->pushSample (d[i]);
  116. }
  117. void AudioVisualiserComponent::setSamplesPerBlock (int newSamplesPerPixel) noexcept
  118. {
  119. inputSamplesPerBlock = newSamplesPerPixel;
  120. }
  121. void AudioVisualiserComponent::setRepaintRate (int frequencyInHz)
  122. {
  123. startTimerHz (frequencyInHz);
  124. }
  125. void AudioVisualiserComponent::timerCallback()
  126. {
  127. repaint();
  128. }
  129. void AudioVisualiserComponent::setColours (Colour bk, Colour fg) noexcept
  130. {
  131. backgroundColour = bk;
  132. waveformColour = fg;
  133. repaint();
  134. }
  135. void AudioVisualiserComponent::paint (Graphics& g)
  136. {
  137. g.fillAll (backgroundColour);
  138. auto r = getLocalBounds().toFloat();
  139. auto channelHeight = r.getHeight() / channels.size();
  140. g.setColour (waveformColour);
  141. for (auto* c : channels)
  142. paintChannel (g, r.removeFromTop (channelHeight),
  143. c->levels.begin(), c->levels.size(), c->nextSample);
  144. }
  145. void AudioVisualiserComponent::getChannelAsPath (Path& path, const Range<float>* levels,
  146. int numLevels, int nextSample)
  147. {
  148. path.preallocateSpace (4 * numLevels + 8);
  149. for (int i = 0; i < numLevels; ++i)
  150. {
  151. auto level = -(levels[(nextSample + i) % numLevels].getEnd());
  152. if (i == 0)
  153. path.startNewSubPath (0.0f, level);
  154. else
  155. path.lineTo ((float) i, level);
  156. }
  157. for (int i = numLevels; --i >= 0;)
  158. path.lineTo ((float) i, -(levels[(nextSample + i) % numLevels].getStart()));
  159. path.closeSubPath();
  160. }
  161. void AudioVisualiserComponent::paintChannel (Graphics& g, Rectangle<float> area,
  162. const Range<float>* levels, int numLevels, int nextSample)
  163. {
  164. Path p;
  165. getChannelAsPath (p, levels, numLevels, nextSample);
  166. g.fillPath (p, AffineTransform::fromTargetPoints (0.0f, -1.0f, area.getX(), area.getY(),
  167. 0.0f, 1.0f, area.getX(), area.getBottom(),
  168. (float) numLevels, -1.0f, area.getRight(), area.getY()));
  169. }
  170. } // namespace juce