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.

274 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. AudioThumbnailImage::AudioThumbnailImage (AudioFilePlayer& sourceToBeUsed,
  25. TimeSliceThread& backgroundThread_,
  26. AudioThumbnailBase& thumbnailToUse,
  27. int sourceSamplesPerThumbnailSample_)
  28. : filePlayer (sourceToBeUsed),
  29. backgroundThread (backgroundThread_),
  30. audioThumbnail (thumbnailToUse),
  31. sourceSamplesPerThumbnailSample (sourceSamplesPerThumbnailSample_),
  32. backgroundColour (Colours::black),
  33. waveformColour (Colours::green),
  34. sourceLoaded (false),
  35. renderComplete (true),
  36. currentSampleRate (44100.0),
  37. oneOverSampleRate (1.0 / currentSampleRate),
  38. lastTimeDrawn (0.0),
  39. resolution (3.0)
  40. {
  41. waveformImage = Image (Image::RGB, 1, 1, false);
  42. refreshFromFilePlayer();
  43. // register with the file player to recieve update messages
  44. filePlayer.addListener (this);
  45. }
  46. AudioThumbnailImage::~AudioThumbnailImage()
  47. {
  48. filePlayer.removeListener (this);
  49. backgroundThread.removeTimeSliceClient (this);
  50. stopTimer();
  51. }
  52. void AudioThumbnailImage::setBackgroundColour (const Colour& newBackgroundColour)
  53. {
  54. backgroundColour = newBackgroundColour;
  55. triggerWaveformRefresh();
  56. }
  57. void AudioThumbnailImage::setWaveformColour (const Colour& newWaveformColour)
  58. {
  59. waveformColour = newWaveformColour;
  60. triggerWaveformRefresh();
  61. }
  62. void AudioThumbnailImage::setResolution (double newResolution)
  63. {
  64. resolution = newResolution;
  65. triggerWaveformRefresh();
  66. }
  67. //====================================================================================
  68. const Image AudioThumbnailImage::getImageAtTime (double startTime, double duration)
  69. {
  70. if (sourceLoaded)
  71. {
  72. const int startPixel = roundToInt (startTime * oneOverFileLength * waveformImage.getWidth());
  73. const int numPixels = roundToInt (duration * oneOverFileLength * waveformImage.getWidth());
  74. return waveformImage.getClippedImage (Rectangle<int> (startPixel, 0, numPixels, waveformImage.getHeight()));
  75. }
  76. else
  77. {
  78. return Image();
  79. }
  80. }
  81. //====================================================================================
  82. void AudioThumbnailImage::timerCallback()
  83. {
  84. const ScopedReadLock sl (imageLock);
  85. listeners.call (&Listener::imageUpdated, this);
  86. if (renderComplete)
  87. {
  88. listeners.call (&Listener::imageFinished, this);
  89. stopTimer();
  90. }
  91. }
  92. int AudioThumbnailImage::useTimeSlice()
  93. {
  94. refreshWaveform();
  95. return 25;
  96. }
  97. void AudioThumbnailImage::fileChanged (AudioFilePlayer* player)
  98. {
  99. if (player == &filePlayer)
  100. refreshFromFilePlayer();
  101. }
  102. //==============================================================================
  103. void AudioThumbnailImage::addListener (AudioThumbnailImage::Listener* const listener)
  104. {
  105. listeners.add (listener);
  106. }
  107. void AudioThumbnailImage::removeListener (AudioThumbnailImage::Listener* const listener)
  108. {
  109. listeners.remove (listener);
  110. }
  111. //==============================================================================
  112. void AudioThumbnailImage::refreshFromFilePlayer()
  113. {
  114. sourceLoaded = false;
  115. if (filePlayer.getAudioFormatReaderSource() != nullptr)
  116. {
  117. currentSampleRate = filePlayer.getAudioFormatReaderSource()->getAudioFormatReader()->sampleRate;
  118. if (currentSampleRate > 0.0)
  119. {
  120. oneOverSampleRate = 1.0 / currentSampleRate;
  121. fileLength = filePlayer.getLengthInSeconds();
  122. if (fileLength > 0)
  123. {
  124. oneOverFileLength = 1.0 / fileLength;
  125. const ScopedWriteLock sl (imageLock);
  126. const int imageWidth = roundToInt (filePlayer.getTotalLength() / sourceSamplesPerThumbnailSample);
  127. waveformImage = Image (Image::RGB, jmax (1, imageWidth), 100, true);
  128. // image will be cleared in triggerWaveformRefresh()
  129. const File newFile (filePlayer.getFile());
  130. if (newFile.existsAsFile())
  131. {
  132. audioThumbnail.setSource (new FileInputSource (newFile));
  133. sourceLoaded = true;
  134. }
  135. else if (filePlayer.getInputType() == AudioFilePlayer::memoryInputStream
  136. || filePlayer.getInputType() == AudioFilePlayer::memoryBlock)
  137. {
  138. MemoryInputStream* memoryInputStream = dynamic_cast<MemoryInputStream*> (filePlayer.getInputStream());
  139. if (memoryInputStream != nullptr)
  140. {
  141. audioThumbnail.setSource (new MemoryInputSource (memoryInputStream));
  142. sourceLoaded = true;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. if (sourceLoaded)
  149. {
  150. renderComplete = false;
  151. }
  152. else
  153. {
  154. audioThumbnail.setSource (nullptr);
  155. renderComplete = true;
  156. }
  157. triggerWaveformRefresh();
  158. }
  159. void AudioThumbnailImage::triggerWaveformRefresh()
  160. {
  161. // we need to remove ourselves from the thread first so we don't
  162. // end up drawing into the middle of the image
  163. backgroundThread.removeTimeSliceClient (this);
  164. {
  165. const ScopedWriteLock sl (imageLock);
  166. lastTimeDrawn = 0.0;
  167. waveformImage.clear (waveformImage.getBounds(), backgroundColour);
  168. renderComplete = false;
  169. }
  170. listeners.call (&Listener::imageChanged, this);
  171. if (sourceLoaded)
  172. backgroundThread.addTimeSliceClient (this);
  173. timerCallback();
  174. startTimer (100);
  175. }
  176. void AudioThumbnailImage::refreshWaveform()
  177. {
  178. if (sourceLoaded && audioThumbnail.getNumSamplesFinished() > 0)
  179. {
  180. const double timeRendered = audioThumbnail.getNumSamplesFinished() * oneOverSampleRate;
  181. const double timeToDraw = jmin (1.0, timeRendered - lastTimeDrawn);
  182. const double endTime = lastTimeDrawn + timeToDraw;
  183. const int64 endSamples = roundToInt (endTime * currentSampleRate);
  184. imageLock.enterRead();
  185. const int waveformImageWidth = waveformImage.getWidth();
  186. const int waveformImageHeight = waveformImage.getHeight();
  187. imageLock.exitRead();
  188. const int nextPixel = roundToInt (endTime * oneOverFileLength * waveformImageWidth);
  189. const int startPixelX = roundToInt (lastTimeDrawn * oneOverFileLength * waveformImageWidth);
  190. const int numPixels = nextPixel - startPixelX;
  191. const int numTempPixels = roundToInt (numPixels * resolution);
  192. if (numTempPixels > 0)
  193. {
  194. if (tempSectionImage.getWidth() < numTempPixels)
  195. {
  196. tempSectionImage = Image (Image::RGB,
  197. numTempPixels, waveformImageHeight,
  198. false);
  199. }
  200. Rectangle<int> rectangleToDraw (0, 0, numTempPixels, waveformImageHeight);
  201. Graphics gTemp (tempSectionImage);
  202. tempSectionImage.clear (tempSectionImage.getBounds(), backgroundColour);
  203. gTemp.setColour (waveformColour);
  204. audioThumbnail.drawChannel (gTemp, rectangleToDraw,
  205. lastTimeDrawn, endTime,
  206. 0, 1.0f);
  207. lastTimeDrawn = endTime;
  208. const ScopedWriteLock sl (imageLock);
  209. Graphics g (waveformImage);
  210. g.drawImage (tempSectionImage,
  211. startPixelX, 0, numPixels, waveformImageHeight,
  212. 0, 0, numTempPixels, tempSectionImage.getHeight());
  213. }
  214. if (endSamples == audioThumbnail.getNumSamplesFinished())
  215. renderComplete = true;
  216. }
  217. if (renderComplete)
  218. backgroundThread.removeTimeSliceClient (this);
  219. }