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