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.

369 lines
13KB

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