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.

159 lines
6.5KB

  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. class AudioThumbnailCache;
  21. //==============================================================================
  22. /**
  23. Provides a base for classes that can store and draw scaled views of an
  24. audio waveform.
  25. Typically, you'll want to use the derived class AudioThumbnail, which provides
  26. a concrete implementation.
  27. @see AudioThumbnail, AudioThumbnailCache
  28. @tags{Audio}
  29. */
  30. class JUCE_API AudioThumbnailBase : public ChangeBroadcaster,
  31. public AudioFormatWriter::ThreadedWriter::IncomingDataReceiver
  32. {
  33. public:
  34. //==============================================================================
  35. AudioThumbnailBase() = default;
  36. ~AudioThumbnailBase() override = default;
  37. //==============================================================================
  38. /** Clears and resets the thumbnail. */
  39. virtual void clear() = 0;
  40. /** Specifies the file or stream that contains the audio file.
  41. For a file, just call
  42. @code
  43. setSource (new FileInputSource (file))
  44. @endcode
  45. You can pass a nullptr in here to clear the thumbnail.
  46. The source that is passed in will be deleted by this object when it is no longer needed.
  47. @returns true if the source could be opened as a valid audio file, false if this failed for
  48. some reason.
  49. */
  50. virtual bool setSource (InputSource* newSource) = 0;
  51. /** Gives the thumbnail an AudioFormatReader to use directly.
  52. This will start parsing the audio in a background thread (unless the hash code
  53. can be looked-up successfully in the thumbnail cache). Note that the reader
  54. object will be held by the thumbnail and deleted later when no longer needed.
  55. The thumbnail will actually keep hold of this reader until you clear the thumbnail
  56. or change the input source, so the file will be held open for all this time. If
  57. you don't want the thumbnail to keep a file handle open continuously, you
  58. should use the setSource() method instead, which will only open the file when
  59. it needs to.
  60. */
  61. virtual void setReader (AudioFormatReader* newReader, int64 hashCode) = 0;
  62. //==============================================================================
  63. /** Reloads the low res thumbnail data from an input stream.
  64. This is not an audio file stream! It takes a stream of thumbnail data that would
  65. previously have been created by the saveTo() method.
  66. @see saveTo
  67. */
  68. virtual bool loadFrom (InputStream& input) = 0;
  69. /** Saves the low res thumbnail data to an output stream.
  70. The data that is written can later be reloaded using loadFrom().
  71. @see loadFrom
  72. */
  73. virtual void saveTo (OutputStream& output) const = 0;
  74. //==============================================================================
  75. /** Returns the number of channels in the file. */
  76. virtual int getNumChannels() const noexcept = 0;
  77. /** Returns the length of the audio file, in seconds. */
  78. virtual double getTotalLength() const noexcept = 0;
  79. /** Draws the waveform for a channel.
  80. The waveform will be drawn within the specified rectangle, where startTime
  81. and endTime specify the times within the audio file that should be positioned
  82. at the left and right edges of the rectangle.
  83. The waveform will be scaled vertically so that a full-volume sample will fill
  84. the rectangle vertically, but you can also specify an extra vertical scale factor
  85. with the verticalZoomFactor parameter.
  86. */
  87. virtual void drawChannel (Graphics& g,
  88. const Rectangle<int>& area,
  89. double startTimeSeconds,
  90. double endTimeSeconds,
  91. int channelNum,
  92. float verticalZoomFactor) = 0;
  93. /** Draws the waveforms for all channels in the thumbnail.
  94. This will call drawChannel() to render each of the thumbnail's channels, stacked
  95. above each other within the specified area.
  96. @see drawChannel
  97. */
  98. virtual void drawChannels (Graphics& g,
  99. const Rectangle<int>& area,
  100. double startTimeSeconds,
  101. double endTimeSeconds,
  102. float verticalZoomFactor) = 0;
  103. /** Returns true if the low res preview is fully generated. */
  104. virtual bool isFullyLoaded() const noexcept = 0;
  105. /** Returns the number of samples that have been set in the thumbnail. */
  106. virtual int64 getNumSamplesFinished() const noexcept = 0;
  107. /** Returns the highest level in the thumbnail.
  108. Note that because the thumb only stores low-resolution data, this isn't
  109. an accurate representation of the highest value, it's only a rough approximation.
  110. */
  111. virtual float getApproximatePeak() const = 0;
  112. /** Reads the approximate min and max levels from a section of the thumbnail.
  113. The lowest and highest samples are returned in minValue and maxValue, but obviously
  114. because the thumb only stores low-resolution data, these numbers will only be a rough
  115. approximation of the true values.
  116. */
  117. virtual void getApproximateMinMax (double startTime, double endTime, int channelIndex,
  118. float& minValue, float& maxValue) const noexcept = 0;
  119. /** Returns the hash code that was set by setSource() or setReader(). */
  120. virtual int64 getHashCode() const = 0;
  121. };
  122. } // namespace juce