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.

412 lines
14KB

  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 (nullptr, thread)
  142. {
  143. addAndMakeVisible (zoomLabel = new Label (String::empty,
  144. "zoom:"));
  145. zoomLabel->setFont (Font (15.00f, Font::plain));
  146. zoomLabel->setJustificationType (Justification::centredRight);
  147. zoomLabel->setEditable (false, false, false);
  148. zoomLabel->setColour (TextEditor::textColourId, Colours::black);
  149. zoomLabel->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  150. addAndMakeVisible (explanation = new Label (String::empty,
  151. "Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."));
  152. explanation->setFont (Font (14.00f, Font::plain));
  153. explanation->setJustificationType (Justification::bottomRight);
  154. explanation->setEditable (false, false, false);
  155. explanation->setColour (TextEditor::textColourId, Colours::black);
  156. explanation->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
  157. addAndMakeVisible (zoomSlider = new Slider (String::empty));
  158. zoomSlider->setRange (0, 1, 0);
  159. zoomSlider->setSliderStyle (Slider::LinearHorizontal);
  160. zoomSlider->setTextBoxStyle (Slider::NoTextBox, false, 80, 20);
  161. zoomSlider->addListener (this);
  162. zoomSlider->setSkewFactor (2);
  163. addAndMakeVisible (thumbnail = new DemoThumbnailComp (formatManager, transportSource, *zoomSlider));
  164. addAndMakeVisible (startStopButton = new TextButton (String::empty));
  165. startStopButton->setButtonText ("Play/Stop");
  166. startStopButton->addListener (this);
  167. startStopButton->setColour (TextButton::buttonColourId, Colour (0xff79ed7f));
  168. addAndMakeVisible (fileTreeComp = new FileTreeComponent (directoryList));
  169. //[UserPreSize]
  170. //[/UserPreSize]
  171. setSize (600, 400);
  172. //[Constructor] You can add your own custom stuff here..
  173. formatManager.registerBasicFormats();
  174. directoryList.setDirectory (File::getSpecialLocation (File::userHomeDirectory), true, true);
  175. thread.startThread (3);
  176. fileTreeComp->setColour (FileTreeComponent::backgroundColourId, Colours::white);
  177. fileTreeComp->addListener (this);
  178. deviceManager.addAudioCallback (&audioSourcePlayer);
  179. audioSourcePlayer.setSource (&transportSource);
  180. //[/Constructor]
  181. }
  182. AudioDemoPlaybackPage::~AudioDemoPlaybackPage()
  183. {
  184. //[Destructor_pre]. You can add your own custom destruction code here..
  185. transportSource.setSource (nullptr);
  186. audioSourcePlayer.setSource (nullptr);
  187. deviceManager.removeAudioCallback (&audioSourcePlayer);
  188. fileTreeComp->removeListener (this);
  189. //[/Destructor_pre]
  190. zoomLabel = nullptr;
  191. explanation = nullptr;
  192. zoomSlider = nullptr;
  193. thumbnail = nullptr;
  194. startStopButton = nullptr;
  195. fileTreeComp = nullptr;
  196. //[Destructor]. You can add your own custom destruction code here..
  197. //[/Destructor]
  198. }
  199. //==============================================================================
  200. void AudioDemoPlaybackPage::paint (Graphics& g)
  201. {
  202. //[UserPrePaint] Add your own custom painting code here..
  203. //[/UserPrePaint]
  204. g.fillAll (Colours::lightgrey);
  205. //[UserPaint] Add your own custom painting code here..
  206. //[/UserPaint]
  207. }
  208. void AudioDemoPlaybackPage::resized()
  209. {
  210. zoomLabel->setBounds (16, getHeight() - 90, 55, 24);
  211. explanation->setBounds (256, getHeight() - 82, getWidth() - 275, 64);
  212. zoomSlider->setBounds (72, getHeight() - 90, 200, 24);
  213. thumbnail->setBounds (16, getHeight() - 221, getWidth() - 32, 123);
  214. startStopButton->setBounds (16, getHeight() - 46, 150, 32);
  215. fileTreeComp->setBounds (16, 8, getWidth() - 32, getHeight() - 245);
  216. //[UserResized] Add your own custom resize handling here..
  217. //[/UserResized]
  218. }
  219. void AudioDemoPlaybackPage::sliderValueChanged (Slider* sliderThatWasMoved)
  220. {
  221. //[UsersliderValueChanged_Pre]
  222. //[/UsersliderValueChanged_Pre]
  223. if (sliderThatWasMoved == zoomSlider)
  224. {
  225. //[UserSliderCode_zoomSlider] -- add your slider handling code here..
  226. thumbnail->setZoomFactor (zoomSlider->getValue());
  227. //[/UserSliderCode_zoomSlider]
  228. }
  229. //[UsersliderValueChanged_Post]
  230. //[/UsersliderValueChanged_Post]
  231. }
  232. void AudioDemoPlaybackPage::buttonClicked (Button* buttonThatWasClicked)
  233. {
  234. //[UserbuttonClicked_Pre]
  235. //[/UserbuttonClicked_Pre]
  236. if (buttonThatWasClicked == startStopButton)
  237. {
  238. //[UserButtonCode_startStopButton] -- add your button handler code here..
  239. if (transportSource.isPlaying())
  240. {
  241. transportSource.stop();
  242. }
  243. else
  244. {
  245. transportSource.setPosition (0);
  246. transportSource.start();
  247. }
  248. //[/UserButtonCode_startStopButton]
  249. }
  250. //[UserbuttonClicked_Post]
  251. //[/UserbuttonClicked_Post]
  252. }
  253. //[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
  254. void AudioDemoPlaybackPage::showFile (const File& file)
  255. {
  256. loadFileIntoTransport (file);
  257. zoomSlider->setValue (0, dontSendNotification);
  258. thumbnail->setFile (file);
  259. }
  260. void AudioDemoPlaybackPage::loadFileIntoTransport (const File& audioFile)
  261. {
  262. // unload the previous file source and delete it..
  263. transportSource.stop();
  264. transportSource.setSource (nullptr);
  265. currentAudioFileSource = nullptr;
  266. AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
  267. if (reader != nullptr)
  268. {
  269. currentAudioFileSource = new AudioFormatReaderSource (reader, true);
  270. // ..and plug it into our transport source
  271. transportSource.setSource (currentAudioFileSource,
  272. 32768, // tells it to buffer this many samples ahead
  273. &thread, // this is the background thread to use for reading-ahead
  274. reader->sampleRate);
  275. }
  276. }
  277. void AudioDemoPlaybackPage::selectionChanged()
  278. {
  279. showFile (fileTreeComp->getSelectedFile());
  280. }
  281. void AudioDemoPlaybackPage::fileClicked (const File&, const MouseEvent&)
  282. {
  283. }
  284. void AudioDemoPlaybackPage::fileDoubleClicked (const File&)
  285. {
  286. }
  287. //[/MiscUserCode]
  288. //==============================================================================
  289. #if 0
  290. /* -- Introjucer information section --
  291. This is where the Introjucer stores the metadata that describe this GUI layout, so
  292. make changes in here at your peril!
  293. BEGIN_JUCER_METADATA
  294. <JUCER_COMPONENT documentType="Component" className="AudioDemoPlaybackPage" componentName=""
  295. parentClasses="public Component, public FileBrowserListener"
  296. constructorParams="AudioDeviceManager&amp; deviceManager_" variableInitialisers="deviceManager (deviceManager_),&#10;thread (&quot;audio file preview&quot;),&#10;directoryList (nullptr, thread)"
  297. snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330000013"
  298. fixedSize="0" initialWidth="600" initialHeight="400">
  299. <BACKGROUND backgroundColour="ffd3d3d3"/>
  300. <LABEL name="" id="d4f78f975d81c8d3" memberName="zoomLabel" virtualName=""
  301. explicitFocusOrder="0" pos="16 90R 55 24" edTextCol="ff000000"
  302. edBkgCol="0" labelText="zoom:" editableSingleClick="0" editableDoubleClick="0"
  303. focusDiscardsChanges="0" fontname="Default font" fontsize="15"
  304. bold="0" italic="0" justification="34"/>
  305. <LABEL name="" id="7db7d0a64ef21311" memberName="explanation" virtualName=""
  306. explicitFocusOrder="0" pos="256 82R 275M 64" edTextCol="ff000000"
  307. edBkgCol="0" labelText="Select an audio file in the treeview above, and this page will display its waveform, and let you play it.."
  308. editableSingleClick="0" editableDoubleClick="0" focusDiscardsChanges="0"
  309. fontname="Default font" fontsize="14" bold="0" italic="0" justification="18"/>
  310. <SLIDER name="" id="38bbc108f4c96092" memberName="zoomSlider" virtualName=""
  311. explicitFocusOrder="0" pos="72 90R 200 24" min="0" max="1" int="0"
  312. style="LinearHorizontal" textBoxPos="NoTextBox" textBoxEditable="1"
  313. textBoxWidth="80" textBoxHeight="20" skewFactor="2"/>
  314. <GENERICCOMPONENT name="" id="beef657b0e007936" memberName="thumbnail" virtualName=""
  315. explicitFocusOrder="0" pos="16 221R 32M 123" class="DemoThumbnailComp"
  316. params="formatManager, transportSource, *zoomSlider"/>
  317. <TEXTBUTTON name="" id="abe446e2f3f09420" memberName="startStopButton" virtualName=""
  318. explicitFocusOrder="0" pos="16 46R 150 32" bgColOff="ff79ed7f"
  319. buttonText="Play/Stop" connectedEdges="0" needsCallback="1" radioGroupId="0"/>
  320. <GENERICCOMPONENT name="" id="1de1dc6a18a9032b" memberName="fileTreeComp" virtualName=""
  321. explicitFocusOrder="0" pos="16 8 32M 245M" class="FileTreeComponent"
  322. params="directoryList"/>
  323. </JUCER_COMPONENT>
  324. END_JUCER_METADATA
  325. */
  326. #endif
  327. //[EndFile] You can add extra defines here...
  328. //[/EndFile]