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.

223 lines
9.3KB

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