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.

415 lines
15KB

  1. /*
  2. ==============================================================================
  3. This is an automatically generated file created by the Jucer!
  4. Creation date: 21 Sep 2012 12:10:00pm
  5. Be careful when adding custom code to these files, as only the code within
  6. the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  7. and re-saved.
  8. Jucer version: 1.12
  9. ------------------------------------------------------------------------------
  10. The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  11. Copyright 2004-6 by Raw Material Software ltd.
  12. ==============================================================================
  13. */
  14. //[Headers] You can add your own extra header files here...
  15. //[/Headers]
  16. #include "AudioDemoPlaybackPage.h"
  17. //[MiscUserDefs] You can add your own user definitions and misc code here...
  18. class DemoThumbnailComp : public Component,
  19. public ChangeListener,
  20. public FileDragAndDropTarget,
  21. private Timer
  22. {
  23. public:
  24. DemoThumbnailComp (AudioFormatManager& formatManager,
  25. AudioTransportSource& transportSource_,
  26. Slider& zoomSlider_)
  27. : transportSource (transportSource_),
  28. zoomSlider (zoomSlider_),
  29. thumbnailCache (5),
  30. thumbnail (512, formatManager, thumbnailCache)
  31. {
  32. startTime = endTime = 0;
  33. thumbnail.addChangeListener (this);
  34. currentPositionMarker.setFill (Colours::purple.withAlpha (0.7f));
  35. addAndMakeVisible (&currentPositionMarker);
  36. }
  37. ~DemoThumbnailComp()
  38. {
  39. thumbnail.removeChangeListener (this);
  40. }
  41. void setFile (const File& file)
  42. {
  43. thumbnail.setSource (new FileInputSource (file));
  44. startTime = 0;
  45. endTime = thumbnail.getTotalLength();
  46. startTimer (1000 / 40);
  47. }
  48. void setZoomFactor (double amount)
  49. {
  50. if (thumbnail.getTotalLength() > 0)
  51. {
  52. const double newScale = jmax (0.001, thumbnail.getTotalLength() * (1.0 - jlimit (0.0, 0.99, amount)));
  53. const double timeAtCentre = xToTime (getWidth() / 2.0f);
  54. startTime = timeAtCentre - newScale * 0.5;
  55. endTime = timeAtCentre + newScale * 0.5;
  56. repaint();
  57. }
  58. }
  59. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  60. {
  61. if (thumbnail.getTotalLength() > 0)
  62. {
  63. double newStart = startTime - wheel.deltaX * (endTime - startTime) / 10.0;
  64. newStart = jlimit (0.0, jmax (0.0, thumbnail.getTotalLength() - (endTime - startTime)), newStart);
  65. endTime = newStart + (endTime - startTime);
  66. startTime = newStart;
  67. if (wheel.deltaY != 0)
  68. zoomSlider.setValue (zoomSlider.getValue() - wheel.deltaY);
  69. repaint();
  70. }
  71. }
  72. void paint (Graphics& g)
  73. {
  74. g.fillAll (Colours::white);
  75. g.setColour (Colours::lightblue);
  76. if (thumbnail.getTotalLength() > 0)
  77. {
  78. thumbnail.drawChannels (g, getLocalBounds().reduced (2),
  79. startTime, endTime, 1.0f);
  80. }
  81. else
  82. {
  83. g.setFont (14.0f);
  84. g.drawFittedText ("(No audio file selected)", getLocalBounds(), Justification::centred, 2);
  85. }
  86. }
  87. void changeListenerCallback (ChangeBroadcaster*)
  88. {
  89. // this method is called by the thumbnail when it has changed, so we should repaint it..
  90. repaint();
  91. }
  92. bool isInterestedInFileDrag (const StringArray& /*files*/)
  93. {
  94. return true;
  95. }
  96. void filesDropped (const StringArray& files, int /*x*/, int /*y*/)
  97. {
  98. AudioDemoPlaybackPage* demoPage = findParentComponentOfClass<AudioDemoPlaybackPage>();
  99. if (demoPage != nullptr)
  100. demoPage->showFile (File (files[0]));
  101. }
  102. void mouseDown (const MouseEvent& e)
  103. {
  104. mouseDrag (e);
  105. }
  106. void mouseDrag (const MouseEvent& e)
  107. {
  108. transportSource.setPosition (jmax (0.0, xToTime ((float) e.x)));
  109. }
  110. void mouseUp (const MouseEvent&)
  111. {
  112. transportSource.start();
  113. }
  114. void timerCallback()
  115. {
  116. currentPositionMarker.setVisible (transportSource.isPlaying() || isMouseButtonDown());
  117. double currentPlayPosition = transportSource.getCurrentPosition();
  118. currentPositionMarker.setRectangle (Rectangle<float> (timeToX (currentPlayPosition) - 0.75f, 0,
  119. 1.5f, (float) getHeight()));
  120. }
  121. private:
  122. AudioTransportSource& transportSource;
  123. Slider& zoomSlider;
  124. AudioThumbnailCache thumbnailCache;
  125. AudioThumbnail thumbnail;
  126. double startTime, endTime;
  127. DrawableRectangle currentPositionMarker;
  128. float timeToX (const double time) const
  129. {
  130. return getWidth() * (float) ((time - startTime) / (endTime - startTime));
  131. }
  132. double xToTime (const float x) const
  133. {
  134. return (x / getWidth()) * (endTime - startTime) + startTime;
  135. }
  136. };
  137. //[/MiscUserDefs]
  138. //==============================================================================
  139. AudioDemoPlaybackPage::AudioDemoPlaybackPage (AudioDeviceManager& deviceManager_)
  140. : deviceManager (deviceManager_),
  141. thread ("audio file preview"),
  142. directoryList (0, thread),
  143. zoomLabel (0),
  144. explanation (0),
  145. zoomSlider (0),
  146. thumbnail (0),
  147. startStopButton (0),
  148. fileTreeComp (0)
  149. {
  150. addAndMakeVisible (zoomLabel = new Label (String::empty,
  151. "zoom:"));
  152. zoomLabel->setFont (Font (15.0000f, Font::plain));
  153. zoomLabel->setJustificationType (Justification::centredRight);
  154. zoomLabel->setEditable (false, false, false);
  155. zoomLabel->setColour (TextEditor::textColourId, Colours::black);
  156. zoomLabel->setColour (TextEditor::backgroundColourId, Colour (0x0));
  157. addAndMakeVisible (explanation = new Label (String::empty,
  158. "Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."));
  159. explanation->setFont (Font (14.0000f, Font::plain));
  160. explanation->setJustificationType (Justification::bottomRight);
  161. explanation->setEditable (false, false, false);
  162. explanation->setColour (TextEditor::textColourId, Colours::black);
  163. explanation->setColour (TextEditor::backgroundColourId, Colour (0x0));
  164. addAndMakeVisible (zoomSlider = new Slider (String::empty));
  165. zoomSlider->setRange (0, 1, 0);
  166. zoomSlider->setSliderStyle (Slider::LinearHorizontal);
  167. zoomSlider->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
  168. zoomSlider->addListener (this);
  169. zoomSlider->setSkewFactor (2);
  170. addAndMakeVisible (thumbnail = new DemoThumbnailComp (formatManager, transportSource, *zoomSlider));
  171. addAndMakeVisible (startStopButton = new TextButton (String::empty));
  172. startStopButton->setButtonText ("Play/Stop");
  173. startStopButton->addListener (this);
  174. startStopButton->setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
  175. addAndMakeVisible (fileTreeComp = new FileTreeComponent (directoryList));
  176. //[UserPreSize]
  177. //[/UserPreSize]
  178. setSize (600, 400);
  179. //[Constructor] You can add your own custom stuff here..
  180. formatManager.registerBasicFormats();
  181. directoryList.setDirectory (File::getSpecialLocation (File::userHomeDirectory), true, true);
  182. thread.startThread (3);
  183. fileTreeComp->setColour (FileTreeComponent::backgroundColourId, Colours::white);
  184. fileTreeComp->addListener (this);
  185. deviceManager.addAudioCallback (&audioSourcePlayer);
  186. audioSourcePlayer.setSource (&transportSource);
  187. //[/Constructor]
  188. }
  189. AudioDemoPlaybackPage::~AudioDemoPlaybackPage()
  190. {
  191. //[Destructor_pre]. You can add your own custom destruction code here..
  192. transportSource.setSource (nullptr);
  193. audioSourcePlayer.setSource (nullptr);
  194. deviceManager.removeAudioCallback (&audioSourcePlayer);
  195. fileTreeComp->removeListener (this);
  196. //[/Destructor_pre]
  197. deleteAndZero (zoomLabel);
  198. deleteAndZero (explanation);
  199. deleteAndZero (zoomSlider);
  200. deleteAndZero (thumbnail);
  201. deleteAndZero (startStopButton);
  202. deleteAndZero (fileTreeComp);
  203. //[Destructor]. You can add your own custom destruction code here..
  204. //[/Destructor]
  205. }
  206. //==============================================================================
  207. void AudioDemoPlaybackPage::paint (Graphics& g)
  208. {
  209. //[UserPrePaint] Add your own custom painting code here..
  210. //[/UserPrePaint]
  211. g.fillAll (Colours::lightgrey);
  212. //[UserPaint] Add your own custom painting code here..
  213. //[/UserPaint]
  214. }
  215. void AudioDemoPlaybackPage::resized()
  216. {
  217. zoomLabel->setBounds (16, getHeight() - 90, 55, 24);
  218. explanation->setBounds (256, getHeight() - 82, getWidth() - 275, 64);
  219. zoomSlider->setBounds (72, getHeight() - 90, 200, 24);
  220. thumbnail->setBounds (16, getHeight() - 221, getWidth() - 32, 123);
  221. startStopButton->setBounds (16, getHeight() - 46, 150, 32);
  222. fileTreeComp->setBounds (16, 8, getWidth() - 32, getHeight() - 245);
  223. //[UserResized] Add your own custom resize handling here..
  224. //[/UserResized]
  225. }
  226. void AudioDemoPlaybackPage::sliderValueChanged (Slider* sliderThatWasMoved)
  227. {
  228. //[UsersliderValueChanged_Pre]
  229. //[/UsersliderValueChanged_Pre]
  230. if (sliderThatWasMoved == zoomSlider)
  231. {
  232. //[UserSliderCode_zoomSlider] -- add your slider handling code here..
  233. thumbnail->setZoomFactor (zoomSlider->getValue());
  234. //[/UserSliderCode_zoomSlider]
  235. }
  236. //[UsersliderValueChanged_Post]
  237. //[/UsersliderValueChanged_Post]
  238. }
  239. void AudioDemoPlaybackPage::buttonClicked (Button* buttonThatWasClicked)
  240. {
  241. //[UserbuttonClicked_Pre]
  242. //[/UserbuttonClicked_Pre]
  243. if (buttonThatWasClicked == startStopButton)
  244. {
  245. //[UserButtonCode_startStopButton] -- add your button handler code here..
  246. if (transportSource.isPlaying())
  247. {
  248. transportSource.stop();
  249. }
  250. else
  251. {
  252. transportSource.setPosition (0);
  253. transportSource.start();
  254. }
  255. //[/UserButtonCode_startStopButton]
  256. }
  257. //[UserbuttonClicked_Post]
  258. //[/UserbuttonClicked_Post]
  259. }
  260. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  261. void AudioDemoPlaybackPage::showFile (const File& file)
  262. {
  263. loadFileIntoTransport (file);
  264. zoomSlider->setValue (0, dontSendNotification);
  265. thumbnail->setFile (file);
  266. }
  267. void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
  268. {
  269. // unload the previous file source and delete it..
  270. transportSource.stop();
  271. transportSource.setSource (nullptr);
  272. currentAudioFileSource = nullptr;
  273. AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
  274. if (reader != nullptr)
  275. {
  276. currentAudioFileSource = new AudioFormatReaderSource (reader, true);
  277. // ..and plug it into our transport source
  278. transportSource.setSource (currentAudioFileSource,
  279. 32768, // tells it to buffer this many samples ahead
  280. &thread, // this is the background thread to use for reading-ahead
  281. reader->sampleRate);
  282. }
  283. }
  284. void AudioDemoPlaybackPage::selectionChanged()
  285. {
  286. showFile (fileTreeComp->getSelectedFile());
  287. }
  288. void AudioDemoPlaybackPage::fileClicked (const File&, const MouseEvent&)
  289. {
  290. }
  291. void AudioDemoPlaybackPage::fileDoubleClicked (const File&)
  292. {
  293. }
  294. //[/MiscUserCode]
  295. //==============================================================================
  296. #if 0
  297. /* -- Jucer information section --
  298. This is where the Jucer puts all of its metadata, so don't change anything in here!
  299. BEGIN_JUCER_METADATA
  300. <JUCER_COMPONENT documentType="Component" className="AudioDemoPlaybackPage" componentName=""
  301. parentClasses="public Component, public FileBrowserListener"
  302. constructorParams="AudioDeviceManager&amp; deviceManager_" variableInitialisers="deviceManager (deviceManager_),&#10;thread (&quot;audio file preview&quot;),&#10;directoryList (0, thread)"
  303. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330000013"
  304. fixedSize="0" initialWidth="600" initialHeight="400">
  305. <BACKGROUND backgroundColour="ffd3d3d3"/>
  306. <LABEL name="" id="d4f78f975d81c8d3" memberName="zoomLabel" virtualName=""
  307. explicitFocusOrder="0" pos="16 90R 55 24" edTextCol="ff000000"
  308. edBkgCol="0" labelText="zoom:" editableSingleClick="0" editableDoubleClick="0"
  309. focusDiscardsChanges="0" fontname="Default font" fontsize="15"
  310. bold="0" italic="0" justification="34"/>
  311. <LABEL name="" id="7db7d0a64ef21311" memberName="explanation" virtualName=""
  312. explicitFocusOrder="0" pos="256 82R 275M 64" edTextCol="ff000000"
  313. edBkgCol="0" labelText="Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."
  314. editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
  315. fontname="Default font" fontsize="14" bold="0" italic="0" justification="18"/>
  316. <SLIDER name="" id="38bbc108f4c96092" memberName="zoomSlider" virtualName=""
  317. explicitFocusOrder="0" pos="72 90R 200 24" min="0" max="1" int="0"
  318. style="LinearHorizontal" textBoxPos="NoTextBox" textBoxEditable="1"
  319. textBoxWidth="80" textBoxHeight="20" skewFactor="2"/>
  320. <GENERICCOMPONENT name="" id="beef657b0e007936" memberName="thumbnail" virtualName=""
  321. explicitFocusOrder="0" pos="16 221R 32M 123" class="DemoThumbnailComp"
  322. params="formatManager, transportSource, *zoomSlider"/>
  323. <TEXTBUTTON name="" id="abe446e2f3f09420" memberName="startStopButton" virtualName=""
  324. explicitFocusOrder="0" pos="16 46R 150 32" bgColOff="ff79ed7f"
  325. buttonText="Play/Stop" connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  326. <GENERICCOMPONENT name="" id="1de1dc6a18a9032b" memberName="fileTreeComp" virtualName=""
  327. explicitFocusOrder="0" pos="16 8 32M 245M" class="FileTreeComponent"
  328. params="directoryList"/>
  329. </JUCER_COMPONENT>
  330. END_JUCER_METADATA
  331. */
  332. #endif