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.

162 lines
5.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. AudioOscilloscope::AudioOscilloscope()
  25. : verticalZoomFactor (1.0f),
  26. horizontalZoomFactor (1.0f),
  27. backgroundColour(Colours::black),
  28. traceColour(Colours::green)
  29. {
  30. lastBufferPos = bufferPos = 0;
  31. bufferSize = 2048; // Needs to be a power of 2 and larger than the width of your scope!
  32. bufferSizeMask = bufferSize - 1;
  33. circularBufferMax.calloc(bufferSize);
  34. circularBufferMin.calloc(bufferSize);
  35. clear();
  36. currentMin = bufferLastMin = 1.0e6f;
  37. currentMax = bufferLastMax = -currentMin;
  38. numSamplesIn = 0;
  39. setOpaque (true);
  40. resized(); // initialise image
  41. startTimer (1000 / 60); // repaint every 1/50 of a second
  42. }
  43. AudioOscilloscope::~AudioOscilloscope()
  44. {
  45. }
  46. //==============================================================================
  47. void AudioOscilloscope::processBlock (const float* inputChannelData,
  48. int numSamples)
  49. {
  50. if (inputChannelData != 0)
  51. {
  52. for (int i = 0; i < numSamples; ++i)
  53. addSample (inputChannelData [i]);
  54. }
  55. }
  56. void AudioOscilloscope::clear()
  57. {
  58. zeromem (circularBufferMax, sizeof (float) * bufferSize);
  59. zeromem (circularBufferMin, sizeof (float) * bufferSize);
  60. }
  61. //==============================================================================
  62. void AudioOscilloscope::resized()
  63. {
  64. Image oldImage (waveformImage);
  65. waveformImage = Image (Image::RGB,
  66. jmax (1, getWidth()), jmax (1, getHeight()),
  67. true);
  68. waveformImage.clear(waveformImage.getBounds(), Colours::black);
  69. if (oldImage.isValid())
  70. waveformImage = oldImage.rescaled (waveformImage.getWidth(), waveformImage.getHeight());
  71. }
  72. void AudioOscilloscope::paint (Graphics& g)
  73. {
  74. g.drawImageAt (waveformImage, 0, 0);
  75. }
  76. void AudioOscilloscope::timerCallback()
  77. {
  78. const int width = getWidth();
  79. const float halfHeight = getHeight() * 0.5f;
  80. const int numPixelsToDraw = bufferPos - lastBufferPos;
  81. const int newSectionStart = width - numPixelsToDraw;
  82. // shuffle image along
  83. waveformImage.moveImageSection (0, 0,
  84. numPixelsToDraw, 0,
  85. newSectionStart, waveformImage.getHeight());
  86. // draw new section
  87. Graphics g (waveformImage);
  88. g.setColour (backgroundColour);
  89. g.fillRect (newSectionStart, 0, numPixelsToDraw, waveformImage.getHeight());
  90. g.setColour (traceColour);
  91. const int bp = bufferPos + bufferSize;
  92. for (int x = width; --x >= newSectionStart;)
  93. {
  94. const int samplesAgo = width - x;
  95. float max = circularBufferMax [(bp - samplesAgo) &bufferSizeMask];
  96. float min = circularBufferMin [(bp - samplesAgo) &bufferSizeMask];
  97. if (min > bufferLastMax)
  98. min = bufferLastMax;
  99. if (max < bufferLastMin)
  100. max = bufferLastMin;
  101. bufferLastMax = max;
  102. bufferLastMin = min;
  103. g.drawLine ((float) x, halfHeight + (halfHeight * verticalZoomFactor * max),
  104. (float) x, halfHeight + (halfHeight * verticalZoomFactor * min));
  105. }
  106. lastBufferPos = bufferPos;
  107. repaint();
  108. }
  109. void AudioOscilloscope::addSample (const float sample)
  110. {
  111. if (sample > currentMax)
  112. currentMax = sample;
  113. if (sample < currentMin)
  114. currentMin = sample;
  115. const int samplesToAverage = 1 + (int) (127.0 * horizontalZoomFactor);
  116. if (++numSamplesIn > samplesToAverage)
  117. {
  118. bufferPos = bufferPos & bufferSizeMask;
  119. circularBufferMax [bufferPos] = currentMax;
  120. circularBufferMin [bufferPos] = currentMin;
  121. bufferPos++;
  122. numSamplesIn = 0;
  123. currentMin = 1.0e6f;
  124. currentMax = -currentMin;
  125. }
  126. }