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.

228 lines
9.4KB

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