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.

117 lines
4.8KB

  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. #ifndef __DROWAUDIO_SPECTROGRAPH_H__
  25. #define __DROWAUDIO_SPECTROGRAPH_H__
  26. #if JUCE_MAC || JUCE_IOS || DROWAUDIO_USE_FFTREAL
  27. //==============================================================================
  28. /**
  29. Creates a standard right-left greyscale Spectrograph.
  30. */
  31. class Spectrograph
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a Spectrograph with a given FFT size.
  36. Note that the fft size given here is log2 of the FFT size so for example,
  37. for a 1024 size FFT use 10 as the argument.
  38. */
  39. Spectrograph (int fftSizeLog2);
  40. /** Destructor. */
  41. ~Spectrograph();
  42. //==============================================================================
  43. /** Creates a Spetrograph based on the whole set of samples provided.
  44. This effectively calls reset, preAllocateStorage, processSamples and then getImage.
  45. */
  46. Image generateImage (const float* samples, int numSamples);
  47. /** Clears all the internal buffers ready for a new set of samples. */
  48. void reset() noexcept;
  49. /** Pre-allocates the internal storage required for a number of samples.
  50. If you are creating a graph of an existing buffer it is more efficient to call this
  51. first. Other wise there may be many re-allocations goinf on as data is added to be processed.
  52. */
  53. void ensureStorageAllocated (int numSamples);
  54. /** Processes a set of samples, to be added to the graph.
  55. Once enough samples have been gathered to perform an FFT operation they will
  56. do so. Once you have finished prcessing all your samples use getImage to retrieve
  57. the Spectrograph.
  58. */
  59. void processSamples (const float* samples, int numSamples);
  60. /** Returns the graph of the current set of processed samples.
  61. Note that this actually generates a new Image based on the internal buffers so if
  62. you need to take copies etc. don't repeatedly call this method.
  63. */
  64. Image createImage() const;
  65. //==============================================================================
  66. /** Sets the scope to display in log or normal mode. */
  67. void setLogFrequencyDisplay (bool shouldDisplayLog);
  68. /** Returns true if the scope is being displayed in log mode. */
  69. bool getLogFrequencyDisplay() const { return logFrequency; }
  70. /** Sets the size for one bin of fft data. This must be greater than 0.
  71. Higher values will effectively cause the graph to be wider and taller.
  72. */
  73. void setBinSize (const Rectangle<float>& size) noexcept;
  74. /** Returns the current bin size. */
  75. Rectangle<float> getBinSize() const { return binSize; }
  76. private:
  77. //==============================================================================
  78. FFTEngine fftEngine;
  79. int numBins;
  80. FifoBuffer<float> circularBuffer, fftMagnitudesData;
  81. HeapBlock<float> tempBlock;
  82. Array<float*> fftMagnitudesBlocks;
  83. bool logFrequency;
  84. Rectangle<float> binSize;
  85. void addMagnitudesBlock (const float* data, int size);
  86. void renderScopeLine();
  87. //==============================================================================
  88. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Spectrograph);
  89. };
  90. #endif
  91. #endif // __DROWAUDIO_SPECTROGRAPH_H__