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.

224 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Makes it easy to quickly draw scaled views of the waveform shape of an
  21. audio file.
  22. To use this class, just create an AudioThumbNail class for the file you want
  23. to draw, call setSource to tell it which file or resource to use, then call
  24. drawChannel() to draw it.
  25. The class will asynchronously scan the wavefile to create its scaled-down view,
  26. so you should make your UI repaint itself as this data comes in. To do this, the
  27. AudioThumbnail is a ChangeBroadcaster, and will broadcast a message when its
  28. listeners should repaint themselves.
  29. The thumbnail stores an internal low-res version of the wave data, and this can
  30. be loaded and saved to avoid having to scan the file again.
  31. @see AudioThumbnailCache, AudioThumbnailBase
  32. */
  33. class JUCE_API AudioThumbnail : public AudioThumbnailBase
  34. {
  35. public:
  36. //==============================================================================
  37. /** Creates an audio thumbnail.
  38. @param sourceSamplesPerThumbnailSample when creating a stored, low-res version
  39. of the audio data, this is the scale at which it should be done. (This
  40. number is the number of original samples that will be averaged for each
  41. low-res sample)
  42. @param formatManagerToUse the audio format manager that is used to open the file
  43. @param cacheToUse an instance of an AudioThumbnailCache - this provides a background
  44. thread and storage that is used to by the thumbnail, and the cache
  45. object can be shared between multiple thumbnails
  46. */
  47. AudioThumbnail (int sourceSamplesPerThumbnailSample,
  48. AudioFormatManager& formatManagerToUse,
  49. AudioThumbnailCache& cacheToUse);
  50. /** Destructor. */
  51. ~AudioThumbnail();
  52. //==============================================================================
  53. /** Clears and resets the thumbnail. */
  54. void clear() override;
  55. /** Specifies the file or stream that contains the audio file.
  56. For a file, just call
  57. @code
  58. setSource (new FileInputSource (file))
  59. @endcode
  60. You can pass a nullptr in here to clear the thumbnail.
  61. The source that is passed in will be deleted by this object when it is no longer needed.
  62. @returns true if the source could be opened as a valid audio file, false if this failed for
  63. some reason.
  64. */
  65. bool setSource (InputSource* newSource) override;
  66. /** Gives the thumbnail an AudioFormatReader to use directly.
  67. This will start parsing the audio in a background thread (unless the hash code
  68. can be looked-up successfully in the thumbnail cache). Note that the reader
  69. object will be held by the thumbnail and deleted later when no longer needed.
  70. The thumbnail will actually keep hold of this reader until you clear the thumbnail
  71. or change the input source, so the file will be held open for all this time. If
  72. you don't want the thumbnail to keep a file handle open continuously, you
  73. should use the setSource() method instead, which will only open the file when
  74. it needs to.
  75. */
  76. void setReader (AudioFormatReader* newReader, int64 hashCode) override;
  77. /** Resets the thumbnail, ready for adding data with the specified format.
  78. If you're going to generate a thumbnail yourself, call this before using addBlock()
  79. to add the data.
  80. */
  81. void reset (int numChannels, double sampleRate, int64 totalSamplesInSource = 0) override;
  82. /** Adds a block of level data to the thumbnail.
  83. Call reset() before using this, to tell the thumbnail about the data format.
  84. */
  85. void addBlock (int64 sampleNumberInSource, const AudioSampleBuffer& newData,
  86. int startOffsetInBuffer, int numSamples) override;
  87. //==============================================================================
  88. /** Reloads the low res thumbnail data from an input stream.
  89. This is not an audio file stream! It takes a stream of thumbnail data that would
  90. previously have been created by the saveTo() method.
  91. @see saveTo
  92. */
  93. bool loadFrom (InputStream& input) override;
  94. /** Saves the low res thumbnail data to an output stream.
  95. The data that is written can later be reloaded using loadFrom().
  96. @see loadFrom
  97. */
  98. void saveTo (OutputStream& output) const override;
  99. //==============================================================================
  100. /** Returns the number of channels in the file. */
  101. int getNumChannels() const noexcept override;
  102. /** Returns the length of the audio file, in seconds. */
  103. double getTotalLength() const noexcept override;
  104. /** Draws the waveform for a channel.
  105. The waveform will be drawn within the specified rectangle, where startTime
  106. and endTime specify the times within the audio file that should be positioned
  107. at the left and right edges of the rectangle.
  108. The waveform will be scaled vertically so that a full-volume sample will fill
  109. the rectangle vertically, but you can also specify an extra vertical scale factor
  110. with the verticalZoomFactor parameter.
  111. */
  112. void drawChannel (Graphics& g,
  113. const Rectangle<int>& area,
  114. double startTimeSeconds,
  115. double endTimeSeconds,
  116. int channelNum,
  117. float verticalZoomFactor) override;
  118. /** Draws the waveforms for all channels in the thumbnail.
  119. This will call drawChannel() to render each of the thumbnail's channels, stacked
  120. above each other within the specified area.
  121. @see drawChannel
  122. */
  123. void drawChannels (Graphics& g,
  124. const Rectangle<int>& area,
  125. double startTimeSeconds,
  126. double endTimeSeconds,
  127. float verticalZoomFactor) override;
  128. /** Returns true if the low res preview is fully generated. */
  129. bool isFullyLoaded() const noexcept override;
  130. /** Returns a value between 0 and 1 to indicate the progress towards loading the entire file. */
  131. double getProportionComplete() const noexcept;
  132. /** Returns the number of samples that have been set in the thumbnail. */
  133. int64 getNumSamplesFinished() const noexcept override;
  134. /** Returns the highest level in the thumbnail.
  135. Note that because the thumb only stores low-resolution data, this isn't
  136. an accurate representation of the highest value, it's only a rough approximation.
  137. */
  138. float getApproximatePeak() const override;
  139. /** Reads the approximate min and max levels from a section of the thumbnail.
  140. The lowest and highest samples are returned in minValue and maxValue, but obviously
  141. because the thumb only stores low-resolution data, these numbers will only be a rough
  142. approximation of the true values.
  143. */
  144. void getApproximateMinMax (double startTime, double endTime, int channelIndex,
  145. float& minValue, float& maxValue) const noexcept override;
  146. /** Returns the hash code that was set by setSource() or setReader(). */
  147. int64 getHashCode() const override;
  148. private:
  149. //==============================================================================
  150. AudioFormatManager& formatManagerToUse;
  151. AudioThumbnailCache& cache;
  152. class LevelDataSource;
  153. struct MinMaxValue;
  154. class ThumbData;
  155. class CachedWindow;
  156. friend class LevelDataSource;
  157. friend class ThumbData;
  158. friend class CachedWindow;
  159. friend struct ContainerDeletePolicy<LevelDataSource>;
  160. friend struct ContainerDeletePolicy<ThumbData>;
  161. friend struct ContainerDeletePolicy<CachedWindow>;
  162. ScopedPointer<LevelDataSource> source;
  163. ScopedPointer<CachedWindow> window;
  164. OwnedArray<ThumbData> channels;
  165. int32 samplesPerThumbSample;
  166. int64 totalSamples, numSamplesFinished;
  167. int32 numChannels;
  168. double sampleRate;
  169. CriticalSection lock;
  170. void clearChannelData();
  171. bool setDataSource (LevelDataSource* newSource);
  172. void setLevels (const MinMaxValue* const* values, int thumbIndex, int numChans, int numValues);
  173. void createChannels (int length);
  174. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioThumbnail)
  175. };