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.

418 lines
15KB

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